]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/ieee802154/6lowpan/rx.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net...
[karo-tx-linux.git] / net / ieee802154 / 6lowpan / rx.c
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.
4  *
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  */
10
11 #include <linux/if_arp.h>
12
13 #include <net/6lowpan.h>
14 #include <net/mac802154.h>
15 #include <net/ieee802154_netdev.h>
16
17 #include "6lowpan_i.h"
18
19 #define LOWPAN_DISPATCH_FIRST           0xc0
20 #define LOWPAN_DISPATCH_FRAG_MASK       0xf8
21
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
28
29 static int lowpan_give_skb_to_device(struct sk_buff *skb)
30 {
31         skb->protocol = htons(ETH_P_IPV6);
32         skb->dev->stats.rx_packets++;
33         skb->dev->stats.rx_bytes += skb->len;
34
35         return netif_rx(skb);
36 }
37
38 static int lowpan_rx_handlers_result(struct sk_buff *skb, lowpan_rx_result res)
39 {
40         switch (res) {
41         case RX_CONTINUE:
42                 /* nobody cared about this packet */
43                 net_warn_ratelimited("%s: received unknown dispatch\n",
44                                      __func__);
45
46                 /* fall-through */
47         case RX_DROP_UNUSABLE:
48                 kfree_skb(skb);
49
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;
57         }
58
59         return NET_RX_DROP;
60 }
61
62 static inline bool lowpan_is_frag1(u8 dispatch)
63 {
64         return (dispatch & LOWPAN_DISPATCH_FRAG_MASK) == LOWPAN_DISPATCH_FRAG1;
65 }
66
67 static inline bool lowpan_is_fragn(u8 dispatch)
68 {
69         return (dispatch & LOWPAN_DISPATCH_FRAG_MASK) == LOWPAN_DISPATCH_FRAGN;
70 }
71
72 static lowpan_rx_result lowpan_rx_h_frag(struct sk_buff *skb)
73 {
74         int ret;
75
76         if (!(lowpan_is_frag1(*skb_network_header(skb)) ||
77               lowpan_is_fragn(*skb_network_header(skb))))
78                 return RX_CONTINUE;
79
80         ret = lowpan_frag_rcv(skb, *skb_network_header(skb) &
81                               LOWPAN_DISPATCH_FRAG_MASK);
82         if (ret == 1)
83                 return RX_QUEUED;
84
85         /* Packet is freed by lowpan_frag_rcv on error or put into the frag
86          * bucket.
87          */
88         return RX_DROP;
89 }
90
91 int lowpan_iphc_decompress(struct sk_buff *skb)
92 {
93         struct ieee802154_addr_sa sa, da;
94         struct ieee802154_hdr hdr;
95         u8 iphc0, iphc1;
96         void *sap, *dap;
97
98         if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
99                 return -EINVAL;
100
101         raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
102
103         if (lowpan_fetch_skb_u8(skb, &iphc0) ||
104             lowpan_fetch_skb_u8(skb, &iphc1))
105                 return -EINVAL;
106
107         ieee802154_addr_to_sa(&sa, &hdr.source);
108         ieee802154_addr_to_sa(&da, &hdr.dest);
109
110         if (sa.addr_type == IEEE802154_ADDR_SHORT)
111                 sap = &sa.short_addr;
112         else
113                 sap = &sa.hwaddr;
114
115         if (da.addr_type == IEEE802154_ADDR_SHORT)
116                 dap = &da.short_addr;
117         else
118                 dap = &da.hwaddr;
119
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);
123 }
124
125 static lowpan_rx_result lowpan_rx_h_iphc(struct sk_buff *skb)
126 {
127         int ret;
128
129         if (!lowpan_is_iphc(*skb_network_header(skb)))
130                 return RX_CONTINUE;
131
132         /* Setting datagram_offset to zero indicates non frag handling
133          * while doing lowpan_header_decompress.
134          */
135         lowpan_802154_cb(skb)->d_size = 0;
136
137         ret = lowpan_iphc_decompress(skb);
138         if (ret < 0)
139                 return RX_DROP_UNUSABLE;
140
141         return RX_QUEUED;
142 }
143
144 lowpan_rx_result lowpan_rx_h_ipv6(struct sk_buff *skb)
145 {
146         if (!lowpan_is_ipv6(*skb_network_header(skb)))
147                 return RX_CONTINUE;
148
149         /* Pull off the 1-byte of 6lowpan header. */
150         skb_pull(skb, 1);
151         return RX_QUEUED;
152 }
153
154 static inline bool lowpan_is_esc(u8 dispatch)
155 {
156         return dispatch == LOWPAN_DISPATCH_ESC;
157 }
158
159 static lowpan_rx_result lowpan_rx_h_esc(struct sk_buff *skb)
160 {
161         if (!lowpan_is_esc(*skb_network_header(skb)))
162                 return RX_CONTINUE;
163
164         net_warn_ratelimited("%s: %s\n", skb->dev->name,
165                              "6LoWPAN ESC not supported\n");
166
167         return RX_DROP_UNUSABLE;
168 }
169
170 static inline bool lowpan_is_hc1(u8 dispatch)
171 {
172         return dispatch == LOWPAN_DISPATCH_HC1;
173 }
174
175 static lowpan_rx_result lowpan_rx_h_hc1(struct sk_buff *skb)
176 {
177         if (!lowpan_is_hc1(*skb_network_header(skb)))
178                 return RX_CONTINUE;
179
180         net_warn_ratelimited("%s: %s\n", skb->dev->name,
181                              "6LoWPAN HC1 not supported\n");
182
183         return RX_DROP_UNUSABLE;
184 }
185
186 static inline bool lowpan_is_dff(u8 dispatch)
187 {
188         return dispatch == LOWPAN_DISPATCH_DFF;
189 }
190
191 static lowpan_rx_result lowpan_rx_h_dff(struct sk_buff *skb)
192 {
193         if (!lowpan_is_dff(*skb_network_header(skb)))
194                 return RX_CONTINUE;
195
196         net_warn_ratelimited("%s: %s\n", skb->dev->name,
197                              "6LoWPAN DFF not supported\n");
198
199         return RX_DROP_UNUSABLE;
200 }
201
202 static inline bool lowpan_is_bc0(u8 dispatch)
203 {
204         return dispatch == LOWPAN_DISPATCH_BC0;
205 }
206
207 static lowpan_rx_result lowpan_rx_h_bc0(struct sk_buff *skb)
208 {
209         if (!lowpan_is_bc0(*skb_network_header(skb)))
210                 return RX_CONTINUE;
211
212         net_warn_ratelimited("%s: %s\n", skb->dev->name,
213                              "6LoWPAN BC0 not supported\n");
214
215         return RX_DROP_UNUSABLE;
216 }
217
218 static inline bool lowpan_is_mesh(u8 dispatch)
219 {
220         return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_MESH;
221 }
222
223 static lowpan_rx_result lowpan_rx_h_mesh(struct sk_buff *skb)
224 {
225         if (!lowpan_is_mesh(*skb_network_header(skb)))
226                 return RX_CONTINUE;
227
228         net_warn_ratelimited("%s: %s\n", skb->dev->name,
229                              "6LoWPAN MESH not supported\n");
230
231         return RX_DROP_UNUSABLE;
232 }
233
234 static int lowpan_invoke_rx_handlers(struct sk_buff *skb)
235 {
236         lowpan_rx_result res;
237
238 #define CALL_RXH(rxh)                   \
239         do {                            \
240                 res = rxh(skb); \
241                 if (res != RX_CONTINUE) \
242                         goto rxh_next;  \
243         } while (0)
244
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);
254
255 rxh_next:
256         return lowpan_rx_handlers_result(skb, res);
257 #undef CALL_RXH
258 }
259
260 static inline bool lowpan_is_nalp(u8 dispatch)
261 {
262         return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_NALP;
263 }
264
265 /* Lookup for reserved dispatch values at:
266  * https://www.iana.org/assignments/_6lowpan-parameters/_6lowpan-parameters.xhtml#_6lowpan-parameters-1
267  *
268  * Last Updated: 2015-01-22
269  */
270 static inline bool lowpan_is_reserved(u8 dispatch)
271 {
272         return ((dispatch >= 0x44 && dispatch <= 0x4F) ||
273                 (dispatch >= 0x51 && dispatch <= 0x5F) ||
274                 (dispatch >= 0xc8 && dispatch <= 0xdf) ||
275                 (dispatch >= 0xe8 && dispatch <= 0xff));
276 }
277
278 /* lowpan_rx_h_check checks on generic 6LoWPAN requirements
279  * in MAC and 6LoWPAN header.
280  *
281  * Don't manipulate the skb here, it could be shared buffer.
282  */
283 static inline bool lowpan_rx_h_check(struct sk_buff *skb)
284 {
285         __le16 fc = ieee802154_get_fc_from_skb(skb);
286
287         /* check on ieee802154 conform 6LoWPAN header */
288         if (!ieee802154_is_data(fc) ||
289             !ieee802154_is_intra_pan(fc))
290                 return false;
291
292         /* check if we can dereference the dispatch */
293         if (unlikely(!skb->len))
294                 return false;
295
296         if (lowpan_is_nalp(*skb_network_header(skb)) ||
297             lowpan_is_reserved(*skb_network_header(skb)))
298                 return false;
299
300         return true;
301 }
302
303 static int lowpan_rcv(struct sk_buff *skb, struct net_device *wdev,
304                       struct packet_type *pt, struct net_device *orig_wdev)
305 {
306         struct net_device *ldev;
307
308         if (wdev->type != ARPHRD_IEEE802154 ||
309             skb->pkt_type == PACKET_OTHERHOST ||
310             !lowpan_rx_h_check(skb))
311                 return NET_RX_DROP;
312
313         ldev = wdev->ieee802154_ptr->lowpan_dev;
314         if (!ldev || !netif_running(ldev))
315                 return NET_RX_DROP;
316
317         /* Replacing skb->dev and followed rx handlers will manipulate skb. */
318         skb = skb_share_check(skb, GFP_ATOMIC);
319         if (!skb)
320                 return NET_RX_DROP;
321         skb->dev = ldev;
322
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.
326          */
327         if (lowpan_is_frag1(*skb_network_header(skb)) ||
328             lowpan_is_iphc(*skb_network_header(skb))) {
329                 skb = skb_unshare(skb, GFP_ATOMIC);
330                 if (!skb)
331                         return NET_RX_DROP;
332         }
333
334         return lowpan_invoke_rx_handlers(skb);
335 }
336
337 static struct packet_type lowpan_packet_type = {
338         .type = htons(ETH_P_IEEE802154),
339         .func = lowpan_rcv,
340 };
341
342 void lowpan_rx_init(void)
343 {
344         dev_add_pack(&lowpan_packet_type);
345 }
346
347 void lowpan_rx_exit(void)
348 {
349         dev_remove_pack(&lowpan_packet_type);
350 }