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_addr_sa sa, da;
94 struct ieee802154_hdr hdr;
98 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
101 raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
103 if (lowpan_fetch_skb_u8(skb, &iphc0) ||
104 lowpan_fetch_skb_u8(skb, &iphc1))
107 ieee802154_addr_to_sa(&sa, &hdr.source);
108 ieee802154_addr_to_sa(&da, &hdr.dest);
110 if (sa.addr_type == IEEE802154_ADDR_SHORT)
111 sap = &sa.short_addr;
115 if (da.addr_type == IEEE802154_ADDR_SHORT)
116 dap = &da.short_addr;
120 return lowpan_header_decompress(skb, skb->dev, sap, sa.addr_type,
121 IEEE802154_ADDR_LEN, dap, da.addr_type,
122 IEEE802154_ADDR_LEN, iphc0, iphc1);
125 static lowpan_rx_result lowpan_rx_h_iphc(struct sk_buff *skb)
129 if (!lowpan_is_iphc(*skb_network_header(skb)))
132 /* Setting datagram_offset to zero indicates non frag handling
133 * while doing lowpan_header_decompress.
135 lowpan_802154_cb(skb)->d_size = 0;
137 ret = lowpan_iphc_decompress(skb);
139 return RX_DROP_UNUSABLE;
144 lowpan_rx_result lowpan_rx_h_ipv6(struct sk_buff *skb)
146 if (!lowpan_is_ipv6(*skb_network_header(skb)))
149 /* Pull off the 1-byte of 6lowpan header. */
154 static inline bool lowpan_is_esc(u8 dispatch)
156 return dispatch == LOWPAN_DISPATCH_ESC;
159 static lowpan_rx_result lowpan_rx_h_esc(struct sk_buff *skb)
161 if (!lowpan_is_esc(*skb_network_header(skb)))
164 net_warn_ratelimited("%s: %s\n", skb->dev->name,
165 "6LoWPAN ESC not supported\n");
167 return RX_DROP_UNUSABLE;
170 static inline bool lowpan_is_hc1(u8 dispatch)
172 return dispatch == LOWPAN_DISPATCH_HC1;
175 static lowpan_rx_result lowpan_rx_h_hc1(struct sk_buff *skb)
177 if (!lowpan_is_hc1(*skb_network_header(skb)))
180 net_warn_ratelimited("%s: %s\n", skb->dev->name,
181 "6LoWPAN HC1 not supported\n");
183 return RX_DROP_UNUSABLE;
186 static inline bool lowpan_is_dff(u8 dispatch)
188 return dispatch == LOWPAN_DISPATCH_DFF;
191 static lowpan_rx_result lowpan_rx_h_dff(struct sk_buff *skb)
193 if (!lowpan_is_dff(*skb_network_header(skb)))
196 net_warn_ratelimited("%s: %s\n", skb->dev->name,
197 "6LoWPAN DFF not supported\n");
199 return RX_DROP_UNUSABLE;
202 static inline bool lowpan_is_bc0(u8 dispatch)
204 return dispatch == LOWPAN_DISPATCH_BC0;
207 static lowpan_rx_result lowpan_rx_h_bc0(struct sk_buff *skb)
209 if (!lowpan_is_bc0(*skb_network_header(skb)))
212 net_warn_ratelimited("%s: %s\n", skb->dev->name,
213 "6LoWPAN BC0 not supported\n");
215 return RX_DROP_UNUSABLE;
218 static inline bool lowpan_is_mesh(u8 dispatch)
220 return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_MESH;
223 static lowpan_rx_result lowpan_rx_h_mesh(struct sk_buff *skb)
225 if (!lowpan_is_mesh(*skb_network_header(skb)))
228 net_warn_ratelimited("%s: %s\n", skb->dev->name,
229 "6LoWPAN MESH not supported\n");
231 return RX_DROP_UNUSABLE;
234 static int lowpan_invoke_rx_handlers(struct sk_buff *skb)
236 lowpan_rx_result res;
238 #define CALL_RXH(rxh) \
241 if (res != RX_CONTINUE) \
245 /* likely at first */
246 CALL_RXH(lowpan_rx_h_iphc);
247 CALL_RXH(lowpan_rx_h_frag);
248 CALL_RXH(lowpan_rx_h_ipv6);
249 CALL_RXH(lowpan_rx_h_esc);
250 CALL_RXH(lowpan_rx_h_hc1);
251 CALL_RXH(lowpan_rx_h_dff);
252 CALL_RXH(lowpan_rx_h_bc0);
253 CALL_RXH(lowpan_rx_h_mesh);
256 return lowpan_rx_handlers_result(skb, res);
260 static inline bool lowpan_is_nalp(u8 dispatch)
262 return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_NALP;
265 /* Lookup for reserved dispatch values at:
266 * https://www.iana.org/assignments/_6lowpan-parameters/_6lowpan-parameters.xhtml#_6lowpan-parameters-1
268 * Last Updated: 2015-01-22
270 static inline bool lowpan_is_reserved(u8 dispatch)
272 return ((dispatch >= 0x44 && dispatch <= 0x4F) ||
273 (dispatch >= 0x51 && dispatch <= 0x5F) ||
274 (dispatch >= 0xc8 && dispatch <= 0xdf) ||
275 (dispatch >= 0xe8 && dispatch <= 0xff));
278 /* lowpan_rx_h_check checks on generic 6LoWPAN requirements
279 * in MAC and 6LoWPAN header.
281 * Don't manipulate the skb here, it could be shared buffer.
283 static inline bool lowpan_rx_h_check(struct sk_buff *skb)
285 __le16 fc = ieee802154_get_fc_from_skb(skb);
287 /* check on ieee802154 conform 6LoWPAN header */
288 if (!ieee802154_is_data(fc) ||
289 !ieee802154_is_intra_pan(fc))
292 /* check if we can dereference the dispatch */
293 if (unlikely(!skb->len))
296 if (lowpan_is_nalp(*skb_network_header(skb)) ||
297 lowpan_is_reserved(*skb_network_header(skb)))
303 static int lowpan_rcv(struct sk_buff *skb, struct net_device *wdev,
304 struct packet_type *pt, struct net_device *orig_wdev)
306 struct net_device *ldev;
308 if (wdev->type != ARPHRD_IEEE802154 ||
309 skb->pkt_type == PACKET_OTHERHOST ||
310 !lowpan_rx_h_check(skb))
313 ldev = wdev->ieee802154_ptr->lowpan_dev;
314 if (!ldev || !netif_running(ldev))
317 /* Replacing skb->dev and followed rx handlers will manipulate skb. */
318 skb = skb_share_check(skb, GFP_ATOMIC);
323 /* When receive frag1 it's likely that we manipulate the buffer.
324 * When recevie iphc we manipulate the data buffer. So we need
325 * to unshare the buffer.
327 if (lowpan_is_frag1(*skb_network_header(skb)) ||
328 lowpan_is_iphc(*skb_network_header(skb))) {
329 skb = skb_unshare(skb, GFP_ATOMIC);
334 return lowpan_invoke_rx_handlers(skb);
337 static struct packet_type lowpan_packet_type = {
338 .type = htons(ETH_P_IEEE802154),
342 void lowpan_rx_init(void)
344 dev_add_pack(&lowpan_packet_type);
347 void lowpan_rx_exit(void)
349 dev_remove_pack(&lowpan_packet_type);