1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/if_arp.h>
5 #include <net/6lowpan.h>
6 #include <net/mac802154.h>
7 #include <net/ieee802154_netdev.h>
11 #define LOWPAN_DISPATCH_FIRST 0xc0
12 #define LOWPAN_DISPATCH_FRAG_MASK 0xf8
14 #define LOWPAN_DISPATCH_NALP 0x00
15 #define LOWPAN_DISPATCH_ESC 0x40
16 #define LOWPAN_DISPATCH_HC1 0x42
17 #define LOWPAN_DISPATCH_DFF 0x43
18 #define LOWPAN_DISPATCH_BC0 0x50
19 #define LOWPAN_DISPATCH_MESH 0x80
21 static int lowpan_give_skb_to_device(struct sk_buff
*skb
)
23 skb
->protocol
= htons(ETH_P_IPV6
);
24 skb
->dev
->stats
.rx_packets
++;
25 skb
->dev
->stats
.rx_bytes
+= skb
->len
;
30 static int lowpan_rx_handlers_result(struct sk_buff
*skb
, lowpan_rx_result res
)
34 /* nobody cared about this packet */
35 net_warn_ratelimited("%s: received unknown dispatch\n",
39 case RX_DROP_UNUSABLE
:
46 return lowpan_give_skb_to_device(skb
);
54 static inline bool lowpan_is_frag1(u8 dispatch
)
56 return (dispatch
& LOWPAN_DISPATCH_FRAG_MASK
) == LOWPAN_DISPATCH_FRAG1
;
59 static inline bool lowpan_is_fragn(u8 dispatch
)
61 return (dispatch
& LOWPAN_DISPATCH_FRAG_MASK
) == LOWPAN_DISPATCH_FRAGN
;
64 static lowpan_rx_result
lowpan_rx_h_frag(struct sk_buff
*skb
)
68 if (!(lowpan_is_frag1(*skb_network_header(skb
)) ||
69 lowpan_is_fragn(*skb_network_header(skb
))))
72 ret
= lowpan_frag_rcv(skb
, *skb_network_header(skb
) &
73 LOWPAN_DISPATCH_FRAG_MASK
);
77 /* Packet is freed by lowpan_frag_rcv on error or put into the frag
83 int lowpan_iphc_decompress(struct sk_buff
*skb
)
85 struct ieee802154_hdr hdr
;
87 if (ieee802154_hdr_peek_addrs(skb
, &hdr
) < 0)
90 return lowpan_header_decompress(skb
, skb
->dev
, &hdr
.dest
, &hdr
.source
);
93 static lowpan_rx_result
lowpan_rx_h_iphc(struct sk_buff
*skb
)
97 if (!lowpan_is_iphc(*skb_network_header(skb
)))
100 /* Setting datagram_offset to zero indicates non frag handling
101 * while doing lowpan_header_decompress.
103 lowpan_802154_cb(skb
)->d_size
= 0;
105 ret
= lowpan_iphc_decompress(skb
);
107 return RX_DROP_UNUSABLE
;
112 lowpan_rx_result
lowpan_rx_h_ipv6(struct sk_buff
*skb
)
114 if (!lowpan_is_ipv6(*skb_network_header(skb
)))
117 /* Pull off the 1-byte of 6lowpan header. */
122 static inline bool lowpan_is_esc(u8 dispatch
)
124 return dispatch
== LOWPAN_DISPATCH_ESC
;
127 static lowpan_rx_result
lowpan_rx_h_esc(struct sk_buff
*skb
)
129 if (!lowpan_is_esc(*skb_network_header(skb
)))
132 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
133 "6LoWPAN ESC not supported\n");
135 return RX_DROP_UNUSABLE
;
138 static inline bool lowpan_is_hc1(u8 dispatch
)
140 return dispatch
== LOWPAN_DISPATCH_HC1
;
143 static lowpan_rx_result
lowpan_rx_h_hc1(struct sk_buff
*skb
)
145 if (!lowpan_is_hc1(*skb_network_header(skb
)))
148 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
149 "6LoWPAN HC1 not supported\n");
151 return RX_DROP_UNUSABLE
;
154 static inline bool lowpan_is_dff(u8 dispatch
)
156 return dispatch
== LOWPAN_DISPATCH_DFF
;
159 static lowpan_rx_result
lowpan_rx_h_dff(struct sk_buff
*skb
)
161 if (!lowpan_is_dff(*skb_network_header(skb
)))
164 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
165 "6LoWPAN DFF not supported\n");
167 return RX_DROP_UNUSABLE
;
170 static inline bool lowpan_is_bc0(u8 dispatch
)
172 return dispatch
== LOWPAN_DISPATCH_BC0
;
175 static lowpan_rx_result
lowpan_rx_h_bc0(struct sk_buff
*skb
)
177 if (!lowpan_is_bc0(*skb_network_header(skb
)))
180 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
181 "6LoWPAN BC0 not supported\n");
183 return RX_DROP_UNUSABLE
;
186 static inline bool lowpan_is_mesh(u8 dispatch
)
188 return (dispatch
& LOWPAN_DISPATCH_FIRST
) == LOWPAN_DISPATCH_MESH
;
191 static lowpan_rx_result
lowpan_rx_h_mesh(struct sk_buff
*skb
)
193 if (!lowpan_is_mesh(*skb_network_header(skb
)))
196 net_warn_ratelimited("%s: %s\n", skb
->dev
->name
,
197 "6LoWPAN MESH not supported\n");
199 return RX_DROP_UNUSABLE
;
202 static int lowpan_invoke_rx_handlers(struct sk_buff
*skb
)
204 lowpan_rx_result res
;
206 #define CALL_RXH(rxh) \
209 if (res != RX_CONTINUE) \
213 /* likely at first */
214 CALL_RXH(lowpan_rx_h_iphc
);
215 CALL_RXH(lowpan_rx_h_frag
);
216 CALL_RXH(lowpan_rx_h_ipv6
);
217 CALL_RXH(lowpan_rx_h_esc
);
218 CALL_RXH(lowpan_rx_h_hc1
);
219 CALL_RXH(lowpan_rx_h_dff
);
220 CALL_RXH(lowpan_rx_h_bc0
);
221 CALL_RXH(lowpan_rx_h_mesh
);
224 return lowpan_rx_handlers_result(skb
, res
);
228 static inline bool lowpan_is_nalp(u8 dispatch
)
230 return (dispatch
& LOWPAN_DISPATCH_FIRST
) == LOWPAN_DISPATCH_NALP
;
233 /* Lookup for reserved dispatch values at:
234 * https://www.iana.org/assignments/_6lowpan-parameters/_6lowpan-parameters.xhtml#_6lowpan-parameters-1
236 * Last Updated: 2015-01-22
238 static inline bool lowpan_is_reserved(u8 dispatch
)
240 return ((dispatch
>= 0x44 && dispatch
<= 0x4F) ||
241 (dispatch
>= 0x51 && dispatch
<= 0x5F) ||
242 (dispatch
>= 0xc8 && dispatch
<= 0xdf) ||
243 (dispatch
>= 0xe8 && dispatch
<= 0xff));
246 /* lowpan_rx_h_check checks on generic 6LoWPAN requirements
247 * in MAC and 6LoWPAN header.
249 * Don't manipulate the skb here, it could be shared buffer.
251 static inline bool lowpan_rx_h_check(struct sk_buff
*skb
)
253 __le16 fc
= ieee802154_get_fc_from_skb(skb
);
255 /* check on ieee802154 conform 6LoWPAN header */
256 if (!ieee802154_is_data(fc
) ||
257 !ieee802154_skb_is_intra_pan_addressing(fc
, skb
))
260 /* check if we can dereference the dispatch */
261 if (unlikely(!skb
->len
))
264 if (lowpan_is_nalp(*skb_network_header(skb
)) ||
265 lowpan_is_reserved(*skb_network_header(skb
)))
271 static int lowpan_rcv(struct sk_buff
*skb
, struct net_device
*wdev
,
272 struct packet_type
*pt
, struct net_device
*orig_wdev
)
274 struct net_device
*ldev
;
276 if (wdev
->type
!= ARPHRD_IEEE802154
||
277 skb
->pkt_type
== PACKET_OTHERHOST
||
278 !lowpan_rx_h_check(skb
))
281 ldev
= wdev
->ieee802154_ptr
->lowpan_dev
;
282 if (!ldev
|| !netif_running(ldev
))
285 /* Replacing skb->dev and followed rx handlers will manipulate skb. */
286 skb
= skb_share_check(skb
, GFP_ATOMIC
);
291 /* When receive frag1 it's likely that we manipulate the buffer.
292 * When recevie iphc we manipulate the data buffer. So we need
293 * to unshare the buffer.
295 if (lowpan_is_frag1(*skb_network_header(skb
)) ||
296 lowpan_is_iphc(*skb_network_header(skb
))) {
297 skb
= skb_unshare(skb
, GFP_ATOMIC
);
302 return lowpan_invoke_rx_handlers(skb
);
310 static struct packet_type lowpan_packet_type
= {
311 .type
= htons(ETH_P_IEEE802154
),
315 void lowpan_rx_init(void)
317 dev_add_pack(&lowpan_packet_type
);
320 void lowpan_rx_exit(void)
322 dev_remove_pack(&lowpan_packet_type
);