staging: vboxvideo: Embed drm_device into driver structure
[linux/fpc-iii.git] / net / ieee802154 / 6lowpan / rx.c
blob649e7d45e88fae2430bff9a2baf7005fe3fba938
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.
9 */
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;
35 return netif_rx(skb);
38 static int lowpan_rx_handlers_result(struct sk_buff *skb, lowpan_rx_result res)
40 switch (res) {
41 case RX_CONTINUE:
42 /* nobody cared about this packet */
43 net_warn_ratelimited("%s: received unknown dispatch\n",
44 __func__);
46 /* fall-through */
47 case RX_DROP_UNUSABLE:
48 kfree_skb(skb);
50 /* fall-through */
51 case RX_DROP:
52 return NET_RX_DROP;
53 case RX_QUEUED:
54 return lowpan_give_skb_to_device(skb);
55 default:
56 break;
59 return NET_RX_DROP;
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)
74 int ret;
76 if (!(lowpan_is_frag1(*skb_network_header(skb)) ||
77 lowpan_is_fragn(*skb_network_header(skb))))
78 return RX_CONTINUE;
80 ret = lowpan_frag_rcv(skb, *skb_network_header(skb) &
81 LOWPAN_DISPATCH_FRAG_MASK);
82 if (ret == 1)
83 return RX_QUEUED;
85 /* Packet is freed by lowpan_frag_rcv on error or put into the frag
86 * bucket.
88 return RX_DROP;
91 int lowpan_iphc_decompress(struct sk_buff *skb)
93 struct ieee802154_hdr hdr;
95 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
96 return -EINVAL;
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)
103 int ret;
105 if (!lowpan_is_iphc(*skb_network_header(skb)))
106 return RX_CONTINUE;
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);
114 if (ret < 0)
115 return RX_DROP_UNUSABLE;
117 return RX_QUEUED;
120 lowpan_rx_result lowpan_rx_h_ipv6(struct sk_buff *skb)
122 if (!lowpan_is_ipv6(*skb_network_header(skb)))
123 return RX_CONTINUE;
125 /* Pull off the 1-byte of 6lowpan header. */
126 skb_pull(skb, 1);
127 return RX_QUEUED;
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)))
138 return RX_CONTINUE;
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)))
154 return RX_CONTINUE;
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)))
170 return RX_CONTINUE;
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)))
186 return RX_CONTINUE;
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)))
202 return RX_CONTINUE;
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) \
215 do { \
216 res = rxh(skb); \
217 if (res != RX_CONTINUE) \
218 goto rxh_next; \
219 } while (0)
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);
231 rxh_next:
232 return lowpan_rx_handlers_result(skb, res);
233 #undef CALL_RXH
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_skb_is_intra_pan_addressing(fc, skb))
266 return false;
268 /* check if we can dereference the dispatch */
269 if (unlikely(!skb->len))
270 return false;
272 if (lowpan_is_nalp(*skb_network_header(skb)) ||
273 lowpan_is_reserved(*skb_network_header(skb)))
274 return false;
276 return true;
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))
287 goto drop;
289 ldev = wdev->ieee802154_ptr->lowpan_dev;
290 if (!ldev || !netif_running(ldev))
291 goto drop;
293 /* Replacing skb->dev and followed rx handlers will manipulate skb. */
294 skb = skb_share_check(skb, GFP_ATOMIC);
295 if (!skb)
296 goto out;
297 skb->dev = ldev;
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);
306 if (!skb)
307 goto out;
310 return lowpan_invoke_rx_handlers(skb);
312 drop:
313 kfree_skb(skb);
314 out:
315 return NET_RX_DROP;
318 static struct packet_type lowpan_packet_type = {
319 .type = htons(ETH_P_IEEE802154),
320 .func = lowpan_rcv,
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);