1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License version 2
3 * as published by the Free Software Foundation.
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
11 #include <linux/if_arp.h>
13 #include <net/6lowpan.h>
14 #include <net/mac802154.h>
15 #include <net/ieee802154_netdev.h>
17 #include "6lowpan_i.h"
19 #define LOWPAN_DISPATCH_FIRST 0xc0
20 #define LOWPAN_DISPATCH_FRAG_MASK 0xf8
22 #define LOWPAN_DISPATCH_NALP 0x00
23 #define LOWPAN_DISPATCH_ESC 0x40
24 #define LOWPAN_DISPATCH_HC1 0x42
25 #define LOWPAN_DISPATCH_DFF 0x43
26 #define LOWPAN_DISPATCH_BC0 0x50
27 #define LOWPAN_DISPATCH_MESH 0x80
29 static int lowpan_give_skb_to_device(struct sk_buff
*skb
)
31 skb
->protocol
= htons(ETH_P_IPV6
);
32 skb
->dev
->stats
.rx_packets
++;
33 skb
->dev
->stats
.rx_bytes
+= skb
->len
;
38 static int lowpan_rx_handlers_result(struct sk_buff
*skb
, lowpan_rx_result res
)
42 /* nobody cared about this packet */
43 net_warn_ratelimited("%s: received unknown dispatch\n",
47 case RX_DROP_UNUSABLE
:
54 return lowpan_give_skb_to_device(skb
);
62 static inline bool lowpan_is_frag1(u8 dispatch
)
64 return (dispatch
& LOWPAN_DISPATCH_FRAG_MASK
) == LOWPAN_DISPATCH_FRAG1
;
67 static inline bool lowpan_is_fragn(u8 dispatch
)
69 return (dispatch
& LOWPAN_DISPATCH_FRAG_MASK
) == LOWPAN_DISPATCH_FRAGN
;
72 static lowpan_rx_result
lowpan_rx_h_frag(struct sk_buff
*skb
)
76 if (!(lowpan_is_frag1(*skb_network_header(skb
)) ||
77 lowpan_is_fragn(*skb_network_header(skb
))))
80 ret
= lowpan_frag_rcv(skb
, *skb_network_header(skb
) &
81 LOWPAN_DISPATCH_FRAG_MASK
);
85 /* Packet is freed by lowpan_frag_rcv on error or put into the frag
91 int lowpan_iphc_decompress(struct sk_buff
*skb
)
93 struct ieee802154_hdr hdr
;
95 if (ieee802154_hdr_peek_addrs(skb
, &hdr
) < 0)
98 return lowpan_header_decompress(skb
, skb
->dev
, &hdr
.dest
, &hdr
.source
);
101 static lowpan_rx_result
lowpan_rx_h_iphc(struct sk_buff
*skb
)
105 if (!lowpan_is_iphc(*skb_network_header(skb
)))
108 /* Setting datagram_offset to zero indicates non frag handling
109 * while doing lowpan_header_decompress.
111 lowpan_802154_cb(skb
)->d_size
= 0;
113 ret
= lowpan_iphc_decompress(skb
);
115 return RX_DROP_UNUSABLE
;
120 lowpan_rx_result
lowpan_rx_h_ipv6(struct sk_buff
*skb
)
122 if (!lowpan_is_ipv6(*skb_network_header(skb
)))
125 /* Pull off the 1-byte of 6lowpan header. */
130 static inline bool lowpan_is_esc(u8 dispatch
)
132 return dispatch
== LOWPAN_DISPATCH_ESC
;
135 static lowpan_rx_result
lowpan_rx_h_esc(struct sk_buff
*skb
)
137 if (!lowpan_is_esc(*skb_network_header(skb
)))
140 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
141 "6LoWPAN ESC not supported\n");
143 return RX_DROP_UNUSABLE
;
146 static inline bool lowpan_is_hc1(u8 dispatch
)
148 return dispatch
== LOWPAN_DISPATCH_HC1
;
151 static lowpan_rx_result
lowpan_rx_h_hc1(struct sk_buff
*skb
)
153 if (!lowpan_is_hc1(*skb_network_header(skb
)))
156 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
157 "6LoWPAN HC1 not supported\n");
159 return RX_DROP_UNUSABLE
;
162 static inline bool lowpan_is_dff(u8 dispatch
)
164 return dispatch
== LOWPAN_DISPATCH_DFF
;
167 static lowpan_rx_result
lowpan_rx_h_dff(struct sk_buff
*skb
)
169 if (!lowpan_is_dff(*skb_network_header(skb
)))
172 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
173 "6LoWPAN DFF not supported\n");
175 return RX_DROP_UNUSABLE
;
178 static inline bool lowpan_is_bc0(u8 dispatch
)
180 return dispatch
== LOWPAN_DISPATCH_BC0
;
183 static lowpan_rx_result
lowpan_rx_h_bc0(struct sk_buff
*skb
)
185 if (!lowpan_is_bc0(*skb_network_header(skb
)))
188 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
189 "6LoWPAN BC0 not supported\n");
191 return RX_DROP_UNUSABLE
;
194 static inline bool lowpan_is_mesh(u8 dispatch
)
196 return (dispatch
& LOWPAN_DISPATCH_FIRST
) == LOWPAN_DISPATCH_MESH
;
199 static lowpan_rx_result
lowpan_rx_h_mesh(struct sk_buff
*skb
)
201 if (!lowpan_is_mesh(*skb_network_header(skb
)))
204 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
205 "6LoWPAN MESH not supported\n");
207 return RX_DROP_UNUSABLE
;
210 static int lowpan_invoke_rx_handlers(struct sk_buff
*skb
)
212 lowpan_rx_result res
;
214 #define CALL_RXH(rxh) \
217 if (res != RX_CONTINUE) \
221 /* likely at first */
222 CALL_RXH(lowpan_rx_h_iphc
);
223 CALL_RXH(lowpan_rx_h_frag
);
224 CALL_RXH(lowpan_rx_h_ipv6
);
225 CALL_RXH(lowpan_rx_h_esc
);
226 CALL_RXH(lowpan_rx_h_hc1
);
227 CALL_RXH(lowpan_rx_h_dff
);
228 CALL_RXH(lowpan_rx_h_bc0
);
229 CALL_RXH(lowpan_rx_h_mesh
);
232 return lowpan_rx_handlers_result(skb
, res
);
236 static inline bool lowpan_is_nalp(u8 dispatch
)
238 return (dispatch
& LOWPAN_DISPATCH_FIRST
) == LOWPAN_DISPATCH_NALP
;
241 /* Lookup for reserved dispatch values at:
242 * https://www.iana.org/assignments/_6lowpan-parameters/_6lowpan-parameters.xhtml#_6lowpan-parameters-1
244 * Last Updated: 2015-01-22
246 static inline bool lowpan_is_reserved(u8 dispatch
)
248 return ((dispatch
>= 0x44 && dispatch
<= 0x4F) ||
249 (dispatch
>= 0x51 && dispatch
<= 0x5F) ||
250 (dispatch
>= 0xc8 && dispatch
<= 0xdf) ||
251 (dispatch
>= 0xe8 && dispatch
<= 0xff));
254 /* lowpan_rx_h_check checks on generic 6LoWPAN requirements
255 * in MAC and 6LoWPAN header.
257 * Don't manipulate the skb here, it could be shared buffer.
259 static inline bool lowpan_rx_h_check(struct sk_buff
*skb
)
261 __le16 fc
= ieee802154_get_fc_from_skb(skb
);
263 /* check on ieee802154 conform 6LoWPAN header */
264 if (!ieee802154_is_data(fc
) ||
265 !ieee802154_is_intra_pan(fc
))
268 /* check if we can dereference the dispatch */
269 if (unlikely(!skb
->len
))
272 if (lowpan_is_nalp(*skb_network_header(skb
)) ||
273 lowpan_is_reserved(*skb_network_header(skb
)))
279 static int lowpan_rcv(struct sk_buff
*skb
, struct net_device
*wdev
,
280 struct packet_type
*pt
, struct net_device
*orig_wdev
)
282 struct net_device
*ldev
;
284 if (wdev
->type
!= ARPHRD_IEEE802154
||
285 skb
->pkt_type
== PACKET_OTHERHOST
||
286 !lowpan_rx_h_check(skb
))
289 ldev
= wdev
->ieee802154_ptr
->lowpan_dev
;
290 if (!ldev
|| !netif_running(ldev
))
293 /* Replacing skb->dev and followed rx handlers will manipulate skb. */
294 skb
= skb_share_check(skb
, GFP_ATOMIC
);
299 /* When receive frag1 it's likely that we manipulate the buffer.
300 * When recevie iphc we manipulate the data buffer. So we need
301 * to unshare the buffer.
303 if (lowpan_is_frag1(*skb_network_header(skb
)) ||
304 lowpan_is_iphc(*skb_network_header(skb
))) {
305 skb
= skb_unshare(skb
, GFP_ATOMIC
);
310 return lowpan_invoke_rx_handlers(skb
);
318 static struct packet_type lowpan_packet_type
= {
319 .type
= htons(ETH_P_IEEE802154
),
323 void lowpan_rx_init(void)
325 dev_add_pack(&lowpan_packet_type
);
328 void lowpan_rx_exit(void)
330 dev_remove_pack(&lowpan_packet_type
);