]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - net/ieee802154/6lowpan/reassembly.c
Merge tag 'net-next-6.10' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev...
[thirdparty/kernel/linux.git] / net / ieee802154 / 6lowpan / reassembly.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
7240cdec 2/* 6LoWPAN fragment reassembly
7240cdec
AA
3 *
4 * Authors:
5 * Alexander Aring <aar@pengutronix.de>
6 *
7 * Based on: net/ipv6/reassembly.c
7240cdec
AA
8 */
9
10#define pr_fmt(fmt) "6LoWPAN: " fmt
11
12#include <linux/net.h>
13#include <linux/list.h>
14#include <linux/netdevice.h>
15#include <linux/random.h>
16#include <linux/jhash.h>
17#include <linux/skbuff.h>
18#include <linux/slab.h>
19#include <linux/export.h>
20
21#include <net/ieee802154_netdev.h>
cefc8c8a 22#include <net/6lowpan.h>
70b095c8 23#include <net/ipv6_frag.h>
7240cdec 24#include <net/inet_frag.h>
254c5dbe 25#include <net/ip.h>
7240cdec 26
8691ee59 27#include "6lowpan_i.h"
7240cdec 28
d4ad4d22
NA
29static const char lowpan_frags_cache_name[] = "lowpan-frags";
30
7240cdec
AA
31static struct inet_frags lowpan_frags;
32
254c5dbe
PO
33static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *skb,
34 struct sk_buff *prev, struct net_device *ldev);
7240cdec 35
36c77782 36static void lowpan_frag_init(struct inet_frag_queue *q, const void *a)
7240cdec 37{
648700f7 38 const struct frag_lowpan_compare_key *key = a;
7240cdec 39
648700f7
ED
40 BUILD_BUG_ON(sizeof(*key) > sizeof(q->key));
41 memcpy(&q->key, key, sizeof(*key));
7240cdec 42}
7240cdec 43
78802011 44static void lowpan_frag_expire(struct timer_list *t)
7240cdec 45{
78802011 46 struct inet_frag_queue *frag = from_timer(frag, t, timer);
7240cdec 47 struct frag_queue *fq;
7240cdec 48
78802011 49 fq = container_of(frag, struct frag_queue, q);
7240cdec 50
17794326
FW
51 spin_lock(&fq->q.lock);
52
06aa8b8a 53 if (fq->q.flags & INET_FRAG_COMPLETE)
17794326
FW
54 goto out;
55
093ba729 56 inet_frag_kill(&fq->q);
17794326
FW
57out:
58 spin_unlock(&fq->q.lock);
093ba729 59 inet_frag_put(&fq->q);
7240cdec
AA
60}
61
62static inline struct lowpan_frag_queue *
72a5e6bb 63fq_find(struct net *net, const struct lowpan_802154_cb *cb,
ae531b94
PB
64 const struct ieee802154_addr *src,
65 const struct ieee802154_addr *dst)
7240cdec 66{
599018a7
LR
67 struct netns_ieee802154_lowpan *ieee802154_lowpan =
68 net_ieee802154_lowpan(net);
f18fa5de 69 struct frag_lowpan_compare_key key = {};
648700f7 70 struct inet_frag_queue *q;
7240cdec 71
f18fa5de
AA
72 key.tag = cb->d_tag;
73 key.d_size = cb->d_size;
74 key.src = *src;
75 key.dst = *dst;
76
4907abc6 77 q = inet_frag_find(ieee802154_lowpan->fqdir, &key);
2d44ed22 78 if (!q)
7240cdec 79 return NULL;
2d44ed22 80
7240cdec
AA
81 return container_of(q, struct lowpan_frag_queue, q);
82}
83
84static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
72a5e6bb 85 struct sk_buff *skb, u8 frag_type)
7240cdec 86{
254c5dbe 87 struct sk_buff *prev_tail;
f4606583 88 struct net_device *ldev;
254c5dbe
PO
89 int end, offset, err;
90
91 /* inet_frag_queue_* functions use skb->cb; see struct ipfrag_skb_cb
92 * in inet_fragment.c
93 */
94 BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(struct inet_skb_parm));
95 BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(struct inet6_skb_parm));
7240cdec 96
06aa8b8a 97 if (fq->q.flags & INET_FRAG_COMPLETE)
7240cdec
AA
98 goto err;
99
72a5e6bb
AA
100 offset = lowpan_802154_cb(skb)->d_offset << 3;
101 end = lowpan_802154_cb(skb)->d_size;
7240cdec
AA
102
103 /* Is this the final fragment? */
104 if (offset + skb->len == end) {
105 /* If we already have some bits beyond end
106 * or have different end, the segment is corrupted.
107 */
108 if (end < fq->q.len ||
06aa8b8a 109 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
7240cdec 110 goto err;
06aa8b8a 111 fq->q.flags |= INET_FRAG_LAST_IN;
7240cdec
AA
112 fq->q.len = end;
113 } else {
114 if (end > fq->q.len) {
115 /* Some bits beyond end -> corruption. */
06aa8b8a 116 if (fq->q.flags & INET_FRAG_LAST_IN)
7240cdec
AA
117 goto err;
118 fq->q.len = end;
119 }
120 }
121
f4606583
AA
122 ldev = skb->dev;
123 if (ldev)
7240cdec 124 skb->dev = NULL;
254c5dbe
PO
125 barrier();
126
127 prev_tail = fq->q.fragments_tail;
128 err = inet_frag_queue_insert(&fq->q, skb, offset, end);
129 if (err)
130 goto err;
7240cdec
AA
131
132 fq->q.stamp = skb->tstamp;
335c8cf3 133 fq->q.mono_delivery_time = skb->mono_delivery_time;
72a5e6bb 134 if (frag_type == LOWPAN_DISPATCH_FRAG1)
06aa8b8a 135 fq->q.flags |= INET_FRAG_FIRST_IN;
72a5e6bb
AA
136
137 fq->q.meat += skb->len;
6ce3b4dc 138 add_frag_mem_limit(fq->q.fqdir, skb->truesize);
7240cdec 139
06aa8b8a 140 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
7240cdec
AA
141 fq->q.meat == fq->q.len) {
142 int res;
143 unsigned long orefdst = skb->_skb_refdst;
144
145 skb->_skb_refdst = 0UL;
254c5dbe 146 res = lowpan_frag_reasm(fq, skb, prev_tail, ldev);
7240cdec
AA
147 skb->_skb_refdst = orefdst;
148 return res;
149 }
254c5dbe 150 skb_dst_drop(skb);
7240cdec 151
7240cdec
AA
152 return -1;
153err:
154 kfree_skb(skb);
155 return -1;
156}
157
158/* Check if this packet is complete.
7240cdec
AA
159 *
160 * It is called with locked fq, and caller must check that
161 * queue is eligible for reassembly i.e. it is not COMPLETE,
162 * the last and the first frames arrived and all the bits are here.
163 */
254c5dbe
PO
164static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *skb,
165 struct sk_buff *prev_tail, struct net_device *ldev)
7240cdec 166{
254c5dbe 167 void *reasm_data;
7240cdec 168
093ba729 169 inet_frag_kill(&fq->q);
7240cdec 170
254c5dbe
PO
171 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
172 if (!reasm_data)
7240cdec 173 goto out_oom;
891584f4 174 inet_frag_reasm_finish(&fq->q, skb, reasm_data, false);
7240cdec 175
254c5dbe
PO
176 skb->dev = ldev;
177 skb->tstamp = fq->q.stamp;
254c5dbe 178 fq->q.rb_fragments = RB_ROOT;
7240cdec 179 fq->q.fragments_tail = NULL;
254c5dbe 180 fq->q.last_run_head = NULL;
7240cdec
AA
181
182 return 1;
183out_oom:
184 net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
185 return -1;
186}
187
72a5e6bb
AA
188static int lowpan_frag_rx_handlers_result(struct sk_buff *skb,
189 lowpan_rx_result res)
190{
191 switch (res) {
192 case RX_QUEUED:
193 return NET_RX_SUCCESS;
194 case RX_CONTINUE:
195 /* nobody cared about this packet */
196 net_warn_ratelimited("%s: received unknown dispatch\n",
197 __func__);
198
df561f66 199 fallthrough;
72a5e6bb
AA
200 default:
201 /* all others failure */
202 return NET_RX_DROP;
203 }
204}
205
206static lowpan_rx_result lowpan_frag_rx_h_iphc(struct sk_buff *skb)
207{
208 int ret;
209
210 if (!lowpan_is_iphc(*skb_network_header(skb)))
211 return RX_CONTINUE;
212
213 ret = lowpan_iphc_decompress(skb);
214 if (ret < 0)
215 return RX_DROP;
216
217 return RX_QUEUED;
218}
219
220static int lowpan_invoke_frag_rx_handlers(struct sk_buff *skb)
221{
222 lowpan_rx_result res;
223
224#define CALL_RXH(rxh) \
225 do { \
226 res = rxh(skb); \
227 if (res != RX_CONTINUE) \
228 goto rxh_next; \
229 } while (0)
230
231 /* likely at first */
232 CALL_RXH(lowpan_frag_rx_h_iphc);
233 CALL_RXH(lowpan_rx_h_ipv6);
234
235rxh_next:
236 return lowpan_frag_rx_handlers_result(skb, res);
237#undef CALL_RXH
238}
239
240#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK 0x07
241#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT 8
242
243static int lowpan_get_cb(struct sk_buff *skb, u8 frag_type,
244 struct lowpan_802154_cb *cb)
7240cdec
AA
245{
246 bool fail;
72a5e6bb 247 u8 high = 0, low = 0;
f870b8c6 248 __be16 d_tag = 0;
7240cdec 249
72a5e6bb 250 fail = lowpan_fetch_skb(skb, &high, 1);
7240cdec 251 fail |= lowpan_fetch_skb(skb, &low, 1);
72a5e6bb
AA
252 /* remove the dispatch value and use first three bits as high value
253 * for the datagram size
254 */
255 cb->d_size = (high & LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK) <<
256 LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT | low;
f870b8c6 257 fail |= lowpan_fetch_skb(skb, &d_tag, 2);
72a5e6bb 258 cb->d_tag = ntohs(d_tag);
7240cdec
AA
259
260 if (frag_type == LOWPAN_DISPATCH_FRAGN) {
72a5e6bb 261 fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1);
7240cdec
AA
262 } else {
263 skb_reset_network_header(skb);
72a5e6bb
AA
264 cb->d_offset = 0;
265 /* check if datagram_size has ipv6hdr on FRAG1 */
266 fail |= cb->d_size < sizeof(struct ipv6hdr);
267 /* check if we can dereference the dispatch value */
268 fail |= !skb->len;
7240cdec
AA
269 }
270
271 if (unlikely(fail))
272 return -EIO;
273
274 return 0;
275}
276
72a5e6bb 277int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type)
7240cdec
AA
278{
279 struct lowpan_frag_queue *fq;
280 struct net *net = dev_net(skb->dev);
72a5e6bb 281 struct lowpan_802154_cb *cb = lowpan_802154_cb(skb);
f18fa5de 282 struct ieee802154_hdr hdr = {};
7240cdec
AA
283 int err;
284
72a5e6bb
AA
285 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
286 goto err;
ae531b94 287
72a5e6bb 288 err = lowpan_get_cb(skb, frag_type, cb);
7240cdec
AA
289 if (err < 0)
290 goto err;
291
72a5e6bb
AA
292 if (frag_type == LOWPAN_DISPATCH_FRAG1) {
293 err = lowpan_invoke_frag_rx_handlers(skb);
294 if (err == NET_RX_DROP)
295 goto err;
296 }
297
298 if (cb->d_size > IPV6_MIN_MTU) {
6697dabe 299 net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
7240cdec 300 goto err;
6697dabe 301 }
7240cdec 302
72a5e6bb 303 fq = fq_find(net, cb, &hdr.source, &hdr.dest);
7240cdec
AA
304 if (fq != NULL) {
305 int ret;
4710d806 306
7240cdec
AA
307 spin_lock(&fq->q.lock);
308 ret = lowpan_frag_queue(fq, skb, frag_type);
309 spin_unlock(&fq->q.lock);
310
093ba729 311 inet_frag_put(&fq->q);
7240cdec
AA
312 return ret;
313 }
314
315err:
316 kfree_skb(skb);
317 return -1;
318}
7240cdec
AA
319
320#ifdef CONFIG_SYSCTL
1bab4c75 321
7240cdec
AA
322static struct ctl_table lowpan_frags_ns_ctl_table[] = {
323 {
324 .procname = "6lowpanfrag_high_thresh",
3e67f106 325 .maxlen = sizeof(unsigned long),
7240cdec 326 .mode = 0644,
3e67f106 327 .proc_handler = proc_doulongvec_minmax,
7240cdec
AA
328 },
329 {
330 .procname = "6lowpanfrag_low_thresh",
3e67f106 331 .maxlen = sizeof(unsigned long),
7240cdec 332 .mode = 0644,
3e67f106 333 .proc_handler = proc_doulongvec_minmax,
7240cdec
AA
334 },
335 {
336 .procname = "6lowpanfrag_time",
7240cdec
AA
337 .maxlen = sizeof(int),
338 .mode = 0644,
339 .proc_handler = proc_dointvec_jiffies,
340 },
7240cdec
AA
341};
342
e3a57d18
FW
343/* secret interval has been deprecated */
344static int lowpan_frags_secret_interval_unused;
7240cdec
AA
345static struct ctl_table lowpan_frags_ctl_table[] = {
346 {
347 .procname = "6lowpanfrag_secret_interval",
e3a57d18 348 .data = &lowpan_frags_secret_interval_unused,
7240cdec
AA
349 .maxlen = sizeof(int),
350 .mode = 0644,
351 .proc_handler = proc_dointvec_jiffies,
352 },
7240cdec
AA
353};
354
355static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
356{
357 struct ctl_table *table;
358 struct ctl_table_header *hdr;
599018a7
LR
359 struct netns_ieee802154_lowpan *ieee802154_lowpan =
360 net_ieee802154_lowpan(net);
c899710f 361 size_t table_size = ARRAY_SIZE(lowpan_frags_ns_ctl_table);
7240cdec
AA
362
363 table = lowpan_frags_ns_ctl_table;
364 if (!net_eq(net, &init_net)) {
365 table = kmemdup(table, sizeof(lowpan_frags_ns_ctl_table),
366 GFP_KERNEL);
367 if (table == NULL)
368 goto err_alloc;
369
7240cdec 370 /* Don't export sysctls to unprivileged users */
ce218712 371 if (net->user_ns != &init_user_ns)
c899710f 372 table_size = 0;
7240cdec
AA
373 }
374
4907abc6
ED
375 table[0].data = &ieee802154_lowpan->fqdir->high_thresh;
376 table[0].extra1 = &ieee802154_lowpan->fqdir->low_thresh;
377 table[1].data = &ieee802154_lowpan->fqdir->low_thresh;
378 table[1].extra2 = &ieee802154_lowpan->fqdir->high_thresh;
379 table[2].data = &ieee802154_lowpan->fqdir->timeout;
d2dfd435 380
c899710f
JG
381 hdr = register_net_sysctl_sz(net, "net/ieee802154/6lowpan", table,
382 table_size);
7240cdec
AA
383 if (hdr == NULL)
384 goto err_reg;
385
599018a7 386 ieee802154_lowpan->sysctl.frags_hdr = hdr;
7240cdec
AA
387 return 0;
388
389err_reg:
390 if (!net_eq(net, &init_net))
391 kfree(table);
392err_alloc:
393 return -ENOMEM;
394}
395
396static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net)
397{
bfa858f2 398 const struct ctl_table *table;
599018a7
LR
399 struct netns_ieee802154_lowpan *ieee802154_lowpan =
400 net_ieee802154_lowpan(net);
7240cdec 401
599018a7
LR
402 table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
403 unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
7240cdec
AA
404 if (!net_eq(net, &init_net))
405 kfree(table);
406}
407
408static struct ctl_table_header *lowpan_ctl_header;
409
3243acd3 410static int __init lowpan_frags_sysctl_register(void)
7240cdec
AA
411{
412 lowpan_ctl_header = register_net_sysctl(&init_net,
413 "net/ieee802154/6lowpan",
414 lowpan_frags_ctl_table);
415 return lowpan_ctl_header == NULL ? -ENOMEM : 0;
416}
417
418static void lowpan_frags_sysctl_unregister(void)
419{
420 unregister_net_sysctl_table(lowpan_ctl_header);
421}
422#else
f0a0c1ce 423static inline int lowpan_frags_ns_sysctl_register(struct net *net)
7240cdec
AA
424{
425 return 0;
426}
427
428static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
429{
430}
431
f0a0c1ce 432static inline int __init lowpan_frags_sysctl_register(void)
7240cdec
AA
433{
434 return 0;
435}
436
437static inline void lowpan_frags_sysctl_unregister(void)
438{
439}
440#endif
441
442static int __net_init lowpan_frags_init_net(struct net *net)
443{
599018a7
LR
444 struct netns_ieee802154_lowpan *ieee802154_lowpan =
445 net_ieee802154_lowpan(net);
787bea77 446 int res;
7240cdec 447
599018a7 448
a39aca67 449 res = fqdir_init(&ieee802154_lowpan->fqdir, &lowpan_frags, net);
787bea77
ED
450 if (res < 0)
451 return res;
4907abc6
ED
452
453 ieee802154_lowpan->fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
454 ieee802154_lowpan->fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
455 ieee802154_lowpan->fqdir->timeout = IPV6_FRAG_TIMEOUT;
456
787bea77
ED
457 res = lowpan_frags_ns_sysctl_register(net);
458 if (res < 0)
4907abc6 459 fqdir_exit(ieee802154_lowpan->fqdir);
787bea77 460 return res;
7240cdec
AA
461}
462
d5dd8879
ED
463static void __net_exit lowpan_frags_pre_exit_net(struct net *net)
464{
465 struct netns_ieee802154_lowpan *ieee802154_lowpan =
466 net_ieee802154_lowpan(net);
467
468 fqdir_pre_exit(ieee802154_lowpan->fqdir);
469}
470
7240cdec
AA
471static void __net_exit lowpan_frags_exit_net(struct net *net)
472{
599018a7
LR
473 struct netns_ieee802154_lowpan *ieee802154_lowpan =
474 net_ieee802154_lowpan(net);
475
7240cdec 476 lowpan_frags_ns_sysctl_unregister(net);
4907abc6 477 fqdir_exit(ieee802154_lowpan->fqdir);
7240cdec
AA
478}
479
480static struct pernet_operations lowpan_frags_ops = {
d5dd8879
ED
481 .init = lowpan_frags_init_net,
482 .pre_exit = lowpan_frags_pre_exit_net,
483 .exit = lowpan_frags_exit_net,
7240cdec
AA
484};
485
648700f7
ED
486static u32 lowpan_key_hashfn(const void *data, u32 len, u32 seed)
487{
488 return jhash2(data,
489 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
490}
491
492static u32 lowpan_obj_hashfn(const void *data, u32 len, u32 seed)
493{
494 const struct inet_frag_queue *fq = data;
495
496 return jhash2((const u32 *)&fq->key,
497 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
498}
499
500static int lowpan_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
501{
502 const struct frag_lowpan_compare_key *key = arg->key;
503 const struct inet_frag_queue *fq = ptr;
504
505 return !!memcmp(&fq->key, key, sizeof(*key));
506}
507
508static const struct rhashtable_params lowpan_rhash_params = {
509 .head_offset = offsetof(struct inet_frag_queue, node),
510 .hashfn = lowpan_key_hashfn,
511 .obj_hashfn = lowpan_obj_hashfn,
512 .obj_cmpfn = lowpan_obj_cmpfn,
513 .automatic_shrinking = true,
514};
515
7240cdec
AA
516int __init lowpan_net_frag_init(void)
517{
518 int ret;
519
7240cdec
AA
520 lowpan_frags.constructor = lowpan_frag_init;
521 lowpan_frags.destructor = NULL;
7240cdec 522 lowpan_frags.qsize = sizeof(struct frag_queue);
7240cdec 523 lowpan_frags.frag_expire = lowpan_frag_expire;
d4ad4d22 524 lowpan_frags.frags_cache_name = lowpan_frags_cache_name;
648700f7 525 lowpan_frags.rhash_params = lowpan_rhash_params;
d4ad4d22
NA
526 ret = inet_frags_init(&lowpan_frags);
527 if (ret)
807f1844 528 goto out;
37147652 529
807f1844
ED
530 ret = lowpan_frags_sysctl_register();
531 if (ret)
532 goto err_sysctl;
533
534 ret = register_pernet_subsys(&lowpan_frags_ops);
535 if (ret)
536 goto err_pernet;
537out:
37147652 538 return ret;
7240cdec
AA
539err_pernet:
540 lowpan_frags_sysctl_unregister();
807f1844
ED
541err_sysctl:
542 inet_frags_fini(&lowpan_frags);
7240cdec
AA
543 return ret;
544}
545
546void lowpan_net_frag_exit(void)
547{
7240cdec
AA
548 lowpan_frags_sysctl_unregister();
549 unregister_pernet_subsys(&lowpan_frags_ops);
ae7352d3 550 inet_frags_fini(&lowpan_frags);
7240cdec 551}