blob: 65d55e05516c02310dfcf277d5024f2f3f0727d1 [file] [log] [blame]
Alexander Aring4662a0d2015-01-04 17:10:55 +01001/* 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>
Alexander Aring54552d02015-09-02 14:21:29 +020014#include <net/mac802154.h>
Alexander Aring4662a0d2015-01-04 17:10:55 +010015#include <net/ieee802154_netdev.h>
16
17#include "6lowpan_i.h"
18
Alexander Aringfaf7d362015-09-02 14:21:26 +020019#define LOWPAN_DISPATCH_FIRST 0xc0
Alexander Aring72a5e6b2015-09-02 14:21:25 +020020#define LOWPAN_DISPATCH_FRAG_MASK 0xf8
21
Alexander Aringfaf7d362015-09-02 14:21:26 +020022#define LOWPAN_DISPATCH_NALP 0x00
Stefan Schmidta1d8d9a52015-09-03 14:54:19 +020023#define LOWPAN_DISPATCH_ESC 0x40
Alexander Aringad663602015-09-02 14:21:27 +020024#define LOWPAN_DISPATCH_HC1 0x42
25#define LOWPAN_DISPATCH_DFF 0x43
26#define LOWPAN_DISPATCH_BC0 0x50
27#define LOWPAN_DISPATCH_MESH 0x80
Alexander Aringfaf7d362015-09-02 14:21:26 +020028
Alexander Aring72a5e6b2015-09-02 14:21:25 +020029static int lowpan_give_skb_to_device(struct sk_buff *skb)
Alexander Aring4662a0d2015-01-04 17:10:55 +010030{
Alexander Aring4662a0d2015-01-04 17:10:55 +010031 skb->protocol = htons(ETH_P_IPV6);
Alexander Aring1c64f142015-09-30 10:20:11 +020032 skb->dev->stats.rx_packets++;
33 skb->dev->stats.rx_bytes += skb->len;
Alexander Aring4662a0d2015-01-04 17:10:55 +010034
Alexander Aring51e0e5d2015-08-10 21:15:53 +020035 return netif_rx(skb);
Alexander Aring4662a0d2015-01-04 17:10:55 +010036}
37
Alexander Aring72a5e6b2015-09-02 14:21:25 +020038static int lowpan_rx_handlers_result(struct sk_buff *skb, lowpan_rx_result res)
Alexander Aring4662a0d2015-01-04 17:10:55 +010039{
Alexander Aring72a5e6b2015-09-02 14:21:25 +020040 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
62static inline bool lowpan_is_frag1(u8 dispatch)
63{
64 return (dispatch & LOWPAN_DISPATCH_FRAG_MASK) == LOWPAN_DISPATCH_FRAG1;
65}
66
67static inline bool lowpan_is_fragn(u8 dispatch)
68{
69 return (dispatch & LOWPAN_DISPATCH_FRAG_MASK) == LOWPAN_DISPATCH_FRAGN;
70}
71
72static 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
91int lowpan_iphc_decompress(struct sk_buff *skb)
92{
Alexander Aring4662a0d2015-01-04 17:10:55 +010093 struct ieee802154_addr_sa sa, da;
Alexander Aring72a5e6b2015-09-02 14:21:25 +020094 struct ieee802154_hdr hdr;
95 u8 iphc0, iphc1;
Alexander Aring4662a0d2015-01-04 17:10:55 +010096 void *sap, *dap;
97
Alexander Aring72a5e6b2015-09-02 14:21:25 +020098 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
99 return -EINVAL;
100
Alexander Aring4662a0d2015-01-04 17:10:55 +0100101 raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
Alexander Aring4662a0d2015-01-04 17:10:55 +0100102
Alexander Aringad23d5b2015-09-02 14:21:22 +0200103 if (lowpan_fetch_skb_u8(skb, &iphc0) ||
104 lowpan_fetch_skb_u8(skb, &iphc1))
Alexander Aring4662a0d2015-01-04 17:10:55 +0100105 return -EINVAL;
106
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200107 ieee802154_addr_to_sa(&sa, &hdr.source);
108 ieee802154_addr_to_sa(&da, &hdr.dest);
Alexander Aring4662a0d2015-01-04 17:10:55 +0100109
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
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200125static 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
144lowpan_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
Alexander Aringad663602015-09-02 14:21:27 +0200154static inline bool lowpan_is_esc(u8 dispatch)
155{
156 return dispatch == LOWPAN_DISPATCH_ESC;
157}
158
159static 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
170static inline bool lowpan_is_hc1(u8 dispatch)
171{
172 return dispatch == LOWPAN_DISPATCH_HC1;
173}
174
175static 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
186static inline bool lowpan_is_dff(u8 dispatch)
187{
188 return dispatch == LOWPAN_DISPATCH_DFF;
189}
190
191static 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
202static inline bool lowpan_is_bc0(u8 dispatch)
203{
204 return dispatch == LOWPAN_DISPATCH_BC0;
205}
206
207static 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
218static inline bool lowpan_is_mesh(u8 dispatch)
219{
220 return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_MESH;
221}
222
223static 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
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200234static 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);
Alexander Aringad663602015-09-02 14:21:27 +0200249 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);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200254
255rxh_next:
256 return lowpan_rx_handlers_result(skb, res);
257#undef CALL_RXH
258}
259
Alexander Aringfaf7d362015-09-02 14:21:26 +0200260static inline bool lowpan_is_nalp(u8 dispatch)
261{
262 return (dispatch & LOWPAN_DISPATCH_FIRST) == LOWPAN_DISPATCH_NALP;
263}
264
Alexander Aringc6fdbba2015-09-02 14:21:28 +0200265/* 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 */
270static 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
Alexander Aringfaf7d362015-09-02 14:21:26 +0200278/* 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 */
283static inline bool lowpan_rx_h_check(struct sk_buff *skb)
284{
Alexander Aring54552d02015-09-02 14:21:29 +0200285 __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
Alexander Aringfaf7d362015-09-02 14:21:26 +0200292 /* check if we can dereference the dispatch */
293 if (unlikely(!skb->len))
294 return false;
295
Alexander Aringc6fdbba2015-09-02 14:21:28 +0200296 if (lowpan_is_nalp(*skb_network_header(skb)) ||
297 lowpan_is_reserved(*skb_network_header(skb)))
Alexander Aringfaf7d362015-09-02 14:21:26 +0200298 return false;
299
300 return true;
301}
302
Alexander Aringf4606582015-09-02 14:21:16 +0200303static int lowpan_rcv(struct sk_buff *skb, struct net_device *wdev,
304 struct packet_type *pt, struct net_device *orig_wdev)
Alexander Aring4662a0d2015-01-04 17:10:55 +0100305{
Alexander Aring989d4332015-09-02 14:21:21 +0200306 struct net_device *ldev;
Alexander Aring4662a0d2015-01-04 17:10:55 +0100307
Alexander Aring742c3af2015-09-02 14:21:23 +0200308 if (wdev->type != ARPHRD_IEEE802154 ||
Alexander Aringfaf7d362015-09-02 14:21:26 +0200309 skb->pkt_type == PACKET_OTHERHOST ||
310 !lowpan_rx_h_check(skb))
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200311 return NET_RX_DROP;
Alexander Aring989d4332015-09-02 14:21:21 +0200312
313 ldev = wdev->ieee802154_ptr->lowpan_dev;
314 if (!ldev || !netif_running(ldev))
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200315 return NET_RX_DROP;
Alexander Aringc0015bf2015-08-15 11:00:33 +0200316
Alexander Aringf801cf42015-09-02 14:21:24 +0200317 /* Replacing skb->dev and followed rx handlers will manipulate skb. */
Alexander Aring4662a0d2015-01-04 17:10:55 +0100318 skb = skb_share_check(skb, GFP_ATOMIC);
319 if (!skb)
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200320 return NET_RX_DROP;
Alexander Aringf801cf42015-09-02 14:21:24 +0200321 skb->dev = ldev;
Alexander Aring4662a0d2015-01-04 17:10:55 +0100322
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200323 /* 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)
Alexander Aring776857a2015-09-02 20:05:42 +0200331 return NET_RX_DROP;
Alexander Aring4662a0d2015-01-04 17:10:55 +0100332 }
333
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200334 return lowpan_invoke_rx_handlers(skb);
Alexander Aring4662a0d2015-01-04 17:10:55 +0100335}
336
337static struct packet_type lowpan_packet_type = {
338 .type = htons(ETH_P_IEEE802154),
339 .func = lowpan_rcv,
340};
341
342void lowpan_rx_init(void)
343{
344 dev_add_pack(&lowpan_packet_type);
345}
346
347void lowpan_rx_exit(void)
348{
349 dev_remove_pack(&lowpan_packet_type);
350}