blob: e7857a8ac86db517cccd0e0a14b95d9c975c6b09 [file] [log] [blame]
Alexander Aring7240cde2014-02-28 07:32:50 +01001/* 6LoWPAN fragment reassembly
2 *
3 *
4 * Authors:
5 * Alexander Aring <aar@pengutronix.de>
6 *
7 * Based on: net/ipv6/reassembly.c
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#define pr_fmt(fmt) "6LoWPAN: " fmt
16
17#include <linux/net.h>
18#include <linux/list.h>
19#include <linux/netdevice.h>
20#include <linux/random.h>
21#include <linux/jhash.h>
22#include <linux/skbuff.h>
23#include <linux/slab.h>
24#include <linux/export.h>
25
26#include <net/ieee802154_netdev.h>
Alexander Aringcefc8c82014-03-05 14:29:05 +010027#include <net/6lowpan.h>
Florian Westphal70b095c2018-07-14 01:14:01 +020028#include <net/ipv6_frag.h>
Alexander Aring7240cde2014-02-28 07:32:50 +010029#include <net/inet_frag.h>
30
Alexander Aring8691ee52015-01-04 17:10:54 +010031#include "6lowpan_i.h"
Alexander Aring7240cde2014-02-28 07:32:50 +010032
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020033static const char lowpan_frags_cache_name[] = "lowpan-frags";
34
Alexander Aring7240cde2014-02-28 07:32:50 +010035static struct inet_frags lowpan_frags;
36
37static int lowpan_frag_reasm(struct lowpan_frag_queue *fq,
Alexander Aringf4606582015-09-02 14:21:16 +020038 struct sk_buff *prev, struct net_device *ldev);
Alexander Aring7240cde2014-02-28 07:32:50 +010039
Florian Westphal36c77782014-07-24 16:50:29 +020040static void lowpan_frag_init(struct inet_frag_queue *q, const void *a)
Alexander Aring7240cde2014-02-28 07:32:50 +010041{
Eric Dumazet648700f2018-03-31 12:58:49 -070042 const struct frag_lowpan_compare_key *key = a;
Alexander Aring7240cde2014-02-28 07:32:50 +010043
Eric Dumazet648700f2018-03-31 12:58:49 -070044 BUILD_BUG_ON(sizeof(*key) > sizeof(q->key));
45 memcpy(&q->key, key, sizeof(*key));
Alexander Aring7240cde2014-02-28 07:32:50 +010046}
Alexander Aring7240cde2014-02-28 07:32:50 +010047
Kees Cook78802012017-10-16 17:29:20 -070048static void lowpan_frag_expire(struct timer_list *t)
Alexander Aring7240cde2014-02-28 07:32:50 +010049{
Kees Cook78802012017-10-16 17:29:20 -070050 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Alexander Aring7240cde2014-02-28 07:32:50 +010051 struct frag_queue *fq;
Alexander Aring7240cde2014-02-28 07:32:50 +010052
Kees Cook78802012017-10-16 17:29:20 -070053 fq = container_of(frag, struct frag_queue, q);
Alexander Aring7240cde2014-02-28 07:32:50 +010054
Florian Westphal17794322014-03-13 20:58:03 +010055 spin_lock(&fq->q.lock);
56
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +020057 if (fq->q.flags & INET_FRAG_COMPLETE)
Florian Westphal17794322014-03-13 20:58:03 +010058 goto out;
59
Eric Dumazet093ba722018-03-31 12:58:44 -070060 inet_frag_kill(&fq->q);
Florian Westphal17794322014-03-13 20:58:03 +010061out:
62 spin_unlock(&fq->q.lock);
Eric Dumazet093ba722018-03-31 12:58:44 -070063 inet_frag_put(&fq->q);
Alexander Aring7240cde2014-02-28 07:32:50 +010064}
65
66static inline struct lowpan_frag_queue *
Alexander Aring72a5e6b2015-09-02 14:21:25 +020067fq_find(struct net *net, const struct lowpan_802154_cb *cb,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010068 const struct ieee802154_addr *src,
69 const struct ieee802154_addr *dst)
Alexander Aring7240cde2014-02-28 07:32:50 +010070{
Luis R. Rodriguez599018a2014-04-17 18:22:54 -070071 struct netns_ieee802154_lowpan *ieee802154_lowpan =
72 net_ieee802154_lowpan(net);
Alexander Aringf18fa5d2018-04-20 14:54:13 -040073 struct frag_lowpan_compare_key key = {};
Eric Dumazet648700f2018-03-31 12:58:49 -070074 struct inet_frag_queue *q;
Alexander Aring7240cde2014-02-28 07:32:50 +010075
Alexander Aringf18fa5d2018-04-20 14:54:13 -040076 key.tag = cb->d_tag;
77 key.d_size = cb->d_size;
78 key.src = *src;
79 key.dst = *dst;
80
Eric Dumazet648700f2018-03-31 12:58:49 -070081 q = inet_frag_find(&ieee802154_lowpan->frags, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -070082 if (!q)
Alexander Aring7240cde2014-02-28 07:32:50 +010083 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -070084
Alexander Aring7240cde2014-02-28 07:32:50 +010085 return container_of(q, struct lowpan_frag_queue, q);
86}
87
88static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
Alexander Aring72a5e6b2015-09-02 14:21:25 +020089 struct sk_buff *skb, u8 frag_type)
Alexander Aring7240cde2014-02-28 07:32:50 +010090{
91 struct sk_buff *prev, *next;
Alexander Aringf4606582015-09-02 14:21:16 +020092 struct net_device *ldev;
Alexander Aring7240cde2014-02-28 07:32:50 +010093 int end, offset;
94
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +020095 if (fq->q.flags & INET_FRAG_COMPLETE)
Alexander Aring7240cde2014-02-28 07:32:50 +010096 goto err;
97
Alexander Aring72a5e6b2015-09-02 14:21:25 +020098 offset = lowpan_802154_cb(skb)->d_offset << 3;
99 end = lowpan_802154_cb(skb)->d_size;
Alexander Aring7240cde2014-02-28 07:32:50 +0100100
101 /* Is this the final fragment? */
102 if (offset + skb->len == end) {
103 /* If we already have some bits beyond end
104 * or have different end, the segment is corrupted.
105 */
106 if (end < fq->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200107 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
Alexander Aring7240cde2014-02-28 07:32:50 +0100108 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200109 fq->q.flags |= INET_FRAG_LAST_IN;
Alexander Aring7240cde2014-02-28 07:32:50 +0100110 fq->q.len = end;
111 } else {
112 if (end > fq->q.len) {
113 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200114 if (fq->q.flags & INET_FRAG_LAST_IN)
Alexander Aring7240cde2014-02-28 07:32:50 +0100115 goto err;
116 fq->q.len = end;
117 }
118 }
119
120 /* Find out which fragments are in front and at the back of us
121 * in the chain of fragments so far. We must know where to put
122 * this fragment, right?
123 */
124 prev = fq->q.fragments_tail;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200125 if (!prev ||
126 lowpan_802154_cb(prev)->d_offset <
127 lowpan_802154_cb(skb)->d_offset) {
Alexander Aring7240cde2014-02-28 07:32:50 +0100128 next = NULL;
129 goto found;
130 }
131 prev = NULL;
132 for (next = fq->q.fragments; next != NULL; next = next->next) {
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200133 if (lowpan_802154_cb(next)->d_offset >=
134 lowpan_802154_cb(skb)->d_offset)
Alexander Aring7240cde2014-02-28 07:32:50 +0100135 break; /* bingo! */
136 prev = next;
137 }
138
139found:
140 /* Insert this fragment in the chain of fragments. */
141 skb->next = next;
142 if (!next)
143 fq->q.fragments_tail = skb;
144 if (prev)
145 prev->next = skb;
146 else
147 fq->q.fragments = skb;
148
Alexander Aringf4606582015-09-02 14:21:16 +0200149 ldev = skb->dev;
150 if (ldev)
Alexander Aring7240cde2014-02-28 07:32:50 +0100151 skb->dev = NULL;
152
153 fq->q.stamp = skb->tstamp;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200154 if (frag_type == LOWPAN_DISPATCH_FRAG1)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200155 fq->q.flags |= INET_FRAG_FIRST_IN;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200156
157 fq->q.meat += skb->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200158 add_frag_mem_limit(fq->q.net, skb->truesize);
Alexander Aring7240cde2014-02-28 07:32:50 +0100159
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200160 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Alexander Aring7240cde2014-02-28 07:32:50 +0100161 fq->q.meat == fq->q.len) {
162 int res;
163 unsigned long orefdst = skb->_skb_refdst;
164
165 skb->_skb_refdst = 0UL;
Alexander Aringf4606582015-09-02 14:21:16 +0200166 res = lowpan_frag_reasm(fq, prev, ldev);
Alexander Aring7240cde2014-02-28 07:32:50 +0100167 skb->_skb_refdst = orefdst;
168 return res;
169 }
170
Alexander Aring7240cde2014-02-28 07:32:50 +0100171 return -1;
172err:
173 kfree_skb(skb);
174 return -1;
175}
176
177/* Check if this packet is complete.
178 * Returns NULL on failure by any reason, and pointer
179 * to current nexthdr field in reassembled frame.
180 *
181 * It is called with locked fq, and caller must check that
182 * queue is eligible for reassembly i.e. it is not COMPLETE,
183 * the last and the first frames arrived and all the bits are here.
184 */
185static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *prev,
Alexander Aringf4606582015-09-02 14:21:16 +0200186 struct net_device *ldev)
Alexander Aring7240cde2014-02-28 07:32:50 +0100187{
188 struct sk_buff *fp, *head = fq->q.fragments;
189 int sum_truesize;
190
Eric Dumazet093ba722018-03-31 12:58:44 -0700191 inet_frag_kill(&fq->q);
Alexander Aring7240cde2014-02-28 07:32:50 +0100192
193 /* Make the one we just received the head. */
194 if (prev) {
195 head = prev->next;
196 fp = skb_clone(head, GFP_ATOMIC);
197
198 if (!fp)
199 goto out_oom;
200
201 fp->next = head->next;
202 if (!fp->next)
203 fq->q.fragments_tail = fp;
204 prev->next = fp;
205
206 skb_morph(head, fq->q.fragments);
207 head->next = fq->q.fragments->next;
208
209 consume_skb(fq->q.fragments);
210 fq->q.fragments = head;
211 }
212
213 /* Head of list must not be cloned. */
214 if (skb_unclone(head, GFP_ATOMIC))
215 goto out_oom;
216
217 /* If the first fragment is fragmented itself, we split
218 * it to two chunks: the first with data and paged part
219 * and the second, holding only fragments.
220 */
221 if (skb_has_frag_list(head)) {
222 struct sk_buff *clone;
223 int i, plen = 0;
224
225 clone = alloc_skb(0, GFP_ATOMIC);
226 if (!clone)
227 goto out_oom;
228 clone->next = head->next;
229 head->next = clone;
230 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
231 skb_frag_list_init(head);
232 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
233 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
234 clone->len = head->data_len - plen;
235 clone->data_len = clone->len;
236 head->data_len -= clone->len;
237 head->len -= clone->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200238 add_frag_mem_limit(fq->q.net, clone->truesize);
Alexander Aring7240cde2014-02-28 07:32:50 +0100239 }
240
241 WARN_ON(head == NULL);
242
243 sum_truesize = head->truesize;
244 for (fp = head->next; fp;) {
245 bool headstolen;
246 int delta;
247 struct sk_buff *next = fp->next;
248
249 sum_truesize += fp->truesize;
250 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
251 kfree_skb_partial(fp, headstolen);
252 } else {
253 if (!skb_shinfo(head)->frag_list)
254 skb_shinfo(head)->frag_list = fp;
255 head->data_len += fp->len;
256 head->len += fp->len;
257 head->truesize += fp->truesize;
258 }
259 fp = next;
260 }
Florian Westphal0e60d242015-07-23 12:05:38 +0200261 sub_frag_mem_limit(fq->q.net, sum_truesize);
Alexander Aring7240cde2014-02-28 07:32:50 +0100262
263 head->next = NULL;
Alexander Aringf4606582015-09-02 14:21:16 +0200264 head->dev = ldev;
Alexander Aring7240cde2014-02-28 07:32:50 +0100265 head->tstamp = fq->q.stamp;
266
267 fq->q.fragments = NULL;
268 fq->q.fragments_tail = NULL;
269
270 return 1;
271out_oom:
272 net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
273 return -1;
274}
275
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200276static int lowpan_frag_rx_handlers_result(struct sk_buff *skb,
277 lowpan_rx_result res)
278{
279 switch (res) {
280 case RX_QUEUED:
281 return NET_RX_SUCCESS;
282 case RX_CONTINUE:
283 /* nobody cared about this packet */
284 net_warn_ratelimited("%s: received unknown dispatch\n",
285 __func__);
286
287 /* fall-through */
288 default:
289 /* all others failure */
290 return NET_RX_DROP;
291 }
292}
293
294static lowpan_rx_result lowpan_frag_rx_h_iphc(struct sk_buff *skb)
295{
296 int ret;
297
298 if (!lowpan_is_iphc(*skb_network_header(skb)))
299 return RX_CONTINUE;
300
301 ret = lowpan_iphc_decompress(skb);
302 if (ret < 0)
303 return RX_DROP;
304
305 return RX_QUEUED;
306}
307
308static int lowpan_invoke_frag_rx_handlers(struct sk_buff *skb)
309{
310 lowpan_rx_result res;
311
312#define CALL_RXH(rxh) \
313 do { \
314 res = rxh(skb); \
315 if (res != RX_CONTINUE) \
316 goto rxh_next; \
317 } while (0)
318
319 /* likely at first */
320 CALL_RXH(lowpan_frag_rx_h_iphc);
321 CALL_RXH(lowpan_rx_h_ipv6);
322
323rxh_next:
324 return lowpan_frag_rx_handlers_result(skb, res);
325#undef CALL_RXH
326}
327
328#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK 0x07
329#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT 8
330
331static int lowpan_get_cb(struct sk_buff *skb, u8 frag_type,
332 struct lowpan_802154_cb *cb)
Alexander Aring7240cde2014-02-28 07:32:50 +0100333{
334 bool fail;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200335 u8 high = 0, low = 0;
Alexander Aringf870b8c2014-10-06 11:00:51 +0200336 __be16 d_tag = 0;
Alexander Aring7240cde2014-02-28 07:32:50 +0100337
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200338 fail = lowpan_fetch_skb(skb, &high, 1);
Alexander Aring7240cde2014-02-28 07:32:50 +0100339 fail |= lowpan_fetch_skb(skb, &low, 1);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200340 /* remove the dispatch value and use first three bits as high value
341 * for the datagram size
342 */
343 cb->d_size = (high & LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK) <<
344 LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT | low;
Alexander Aringf870b8c2014-10-06 11:00:51 +0200345 fail |= lowpan_fetch_skb(skb, &d_tag, 2);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200346 cb->d_tag = ntohs(d_tag);
Alexander Aring7240cde2014-02-28 07:32:50 +0100347
348 if (frag_type == LOWPAN_DISPATCH_FRAGN) {
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200349 fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1);
Alexander Aring7240cde2014-02-28 07:32:50 +0100350 } else {
351 skb_reset_network_header(skb);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200352 cb->d_offset = 0;
353 /* check if datagram_size has ipv6hdr on FRAG1 */
354 fail |= cb->d_size < sizeof(struct ipv6hdr);
355 /* check if we can dereference the dispatch value */
356 fail |= !skb->len;
Alexander Aring7240cde2014-02-28 07:32:50 +0100357 }
358
359 if (unlikely(fail))
360 return -EIO;
361
362 return 0;
363}
364
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200365int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type)
Alexander Aring7240cde2014-02-28 07:32:50 +0100366{
367 struct lowpan_frag_queue *fq;
368 struct net *net = dev_net(skb->dev);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200369 struct lowpan_802154_cb *cb = lowpan_802154_cb(skb);
Alexander Aringf18fa5d2018-04-20 14:54:13 -0400370 struct ieee802154_hdr hdr = {};
Alexander Aring7240cde2014-02-28 07:32:50 +0100371 int err;
372
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200373 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
374 goto err;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100375
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200376 err = lowpan_get_cb(skb, frag_type, cb);
Alexander Aring7240cde2014-02-28 07:32:50 +0100377 if (err < 0)
378 goto err;
379
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200380 if (frag_type == LOWPAN_DISPATCH_FRAG1) {
381 err = lowpan_invoke_frag_rx_handlers(skb);
382 if (err == NET_RX_DROP)
383 goto err;
384 }
385
386 if (cb->d_size > IPV6_MIN_MTU) {
Martin Townsend6697dab2014-08-19 19:03:32 +0200387 net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
Alexander Aring7240cde2014-02-28 07:32:50 +0100388 goto err;
Martin Townsend6697dab2014-08-19 19:03:32 +0200389 }
Alexander Aring7240cde2014-02-28 07:32:50 +0100390
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200391 fq = fq_find(net, cb, &hdr.source, &hdr.dest);
Alexander Aring7240cde2014-02-28 07:32:50 +0100392 if (fq != NULL) {
393 int ret;
Varka Bhadram4710d802014-07-02 09:01:09 +0530394
Alexander Aring7240cde2014-02-28 07:32:50 +0100395 spin_lock(&fq->q.lock);
396 ret = lowpan_frag_queue(fq, skb, frag_type);
397 spin_unlock(&fq->q.lock);
398
Eric Dumazet093ba722018-03-31 12:58:44 -0700399 inet_frag_put(&fq->q);
Alexander Aring7240cde2014-02-28 07:32:50 +0100400 return ret;
401 }
402
403err:
404 kfree_skb(skb);
405 return -1;
406}
Alexander Aring7240cde2014-02-28 07:32:50 +0100407
408#ifdef CONFIG_SYSCTL
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200409
Alexander Aring7240cde2014-02-28 07:32:50 +0100410static struct ctl_table lowpan_frags_ns_ctl_table[] = {
411 {
412 .procname = "6lowpanfrag_high_thresh",
413 .data = &init_net.ieee802154_lowpan.frags.high_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700414 .maxlen = sizeof(unsigned long),
Alexander Aring7240cde2014-02-28 07:32:50 +0100415 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700416 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200417 .extra1 = &init_net.ieee802154_lowpan.frags.low_thresh
Alexander Aring7240cde2014-02-28 07:32:50 +0100418 },
419 {
420 .procname = "6lowpanfrag_low_thresh",
421 .data = &init_net.ieee802154_lowpan.frags.low_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700422 .maxlen = sizeof(unsigned long),
Alexander Aring7240cde2014-02-28 07:32:50 +0100423 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700424 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200425 .extra2 = &init_net.ieee802154_lowpan.frags.high_thresh
Alexander Aring7240cde2014-02-28 07:32:50 +0100426 },
427 {
428 .procname = "6lowpanfrag_time",
429 .data = &init_net.ieee802154_lowpan.frags.timeout,
430 .maxlen = sizeof(int),
431 .mode = 0644,
432 .proc_handler = proc_dointvec_jiffies,
433 },
Alexander Aring7240cde2014-02-28 07:32:50 +0100434 { }
435};
436
Florian Westphale3a57d12014-07-24 16:50:35 +0200437/* secret interval has been deprecated */
438static int lowpan_frags_secret_interval_unused;
Alexander Aring7240cde2014-02-28 07:32:50 +0100439static struct ctl_table lowpan_frags_ctl_table[] = {
440 {
441 .procname = "6lowpanfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200442 .data = &lowpan_frags_secret_interval_unused,
Alexander Aring7240cde2014-02-28 07:32:50 +0100443 .maxlen = sizeof(int),
444 .mode = 0644,
445 .proc_handler = proc_dointvec_jiffies,
446 },
447 { }
448};
449
450static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
451{
452 struct ctl_table *table;
453 struct ctl_table_header *hdr;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700454 struct netns_ieee802154_lowpan *ieee802154_lowpan =
455 net_ieee802154_lowpan(net);
Alexander Aring7240cde2014-02-28 07:32:50 +0100456
457 table = lowpan_frags_ns_ctl_table;
458 if (!net_eq(net, &init_net)) {
459 table = kmemdup(table, sizeof(lowpan_frags_ns_ctl_table),
460 GFP_KERNEL);
461 if (table == NULL)
462 goto err_alloc;
463
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700464 table[0].data = &ieee802154_lowpan->frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200465 table[0].extra1 = &ieee802154_lowpan->frags.low_thresh;
466 table[0].extra2 = &init_net.ieee802154_lowpan.frags.high_thresh;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700467 table[1].data = &ieee802154_lowpan->frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200468 table[1].extra2 = &ieee802154_lowpan->frags.high_thresh;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700469 table[2].data = &ieee802154_lowpan->frags.timeout;
Alexander Aring7240cde2014-02-28 07:32:50 +0100470
471 /* Don't export sysctls to unprivileged users */
472 if (net->user_ns != &init_user_ns)
473 table[0].procname = NULL;
474 }
475
476 hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
477 if (hdr == NULL)
478 goto err_reg;
479
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700480 ieee802154_lowpan->sysctl.frags_hdr = hdr;
Alexander Aring7240cde2014-02-28 07:32:50 +0100481 return 0;
482
483err_reg:
484 if (!net_eq(net, &init_net))
485 kfree(table);
486err_alloc:
487 return -ENOMEM;
488}
489
490static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net)
491{
492 struct ctl_table *table;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700493 struct netns_ieee802154_lowpan *ieee802154_lowpan =
494 net_ieee802154_lowpan(net);
Alexander Aring7240cde2014-02-28 07:32:50 +0100495
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700496 table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
497 unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100498 if (!net_eq(net, &init_net))
499 kfree(table);
500}
501
502static struct ctl_table_header *lowpan_ctl_header;
503
Fabian Frederick3243acd2014-09-30 22:34:08 +0200504static int __init lowpan_frags_sysctl_register(void)
Alexander Aring7240cde2014-02-28 07:32:50 +0100505{
506 lowpan_ctl_header = register_net_sysctl(&init_net,
507 "net/ieee802154/6lowpan",
508 lowpan_frags_ctl_table);
509 return lowpan_ctl_header == NULL ? -ENOMEM : 0;
510}
511
512static void lowpan_frags_sysctl_unregister(void)
513{
514 unregister_net_sysctl_table(lowpan_ctl_header);
515}
516#else
Fabian Frederickf0a0c1c2014-10-01 07:27:46 +0200517static inline int lowpan_frags_ns_sysctl_register(struct net *net)
Alexander Aring7240cde2014-02-28 07:32:50 +0100518{
519 return 0;
520}
521
522static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
523{
524}
525
Fabian Frederickf0a0c1c2014-10-01 07:27:46 +0200526static inline int __init lowpan_frags_sysctl_register(void)
Alexander Aring7240cde2014-02-28 07:32:50 +0100527{
528 return 0;
529}
530
531static inline void lowpan_frags_sysctl_unregister(void)
532{
533}
534#endif
535
536static int __net_init lowpan_frags_init_net(struct net *net)
537{
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700538 struct netns_ieee802154_lowpan *ieee802154_lowpan =
539 net_ieee802154_lowpan(net);
Eric Dumazet787bea72018-03-31 12:58:43 -0700540 int res;
Alexander Aring7240cde2014-02-28 07:32:50 +0100541
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700542 ieee802154_lowpan->frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
543 ieee802154_lowpan->frags.low_thresh = IPV6_FRAG_LOW_THRESH;
544 ieee802154_lowpan->frags.timeout = IPV6_FRAG_TIMEOUT;
Eric Dumazet093ba722018-03-31 12:58:44 -0700545 ieee802154_lowpan->frags.f = &lowpan_frags;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700546
Eric Dumazet787bea72018-03-31 12:58:43 -0700547 res = inet_frags_init_net(&ieee802154_lowpan->frags);
548 if (res < 0)
549 return res;
550 res = lowpan_frags_ns_sysctl_register(net);
551 if (res < 0)
Eric Dumazet093ba722018-03-31 12:58:44 -0700552 inet_frags_exit_net(&ieee802154_lowpan->frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700553 return res;
Alexander Aring7240cde2014-02-28 07:32:50 +0100554}
555
556static void __net_exit lowpan_frags_exit_net(struct net *net)
557{
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700558 struct netns_ieee802154_lowpan *ieee802154_lowpan =
559 net_ieee802154_lowpan(net);
560
Alexander Aring7240cde2014-02-28 07:32:50 +0100561 lowpan_frags_ns_sysctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700562 inet_frags_exit_net(&ieee802154_lowpan->frags);
Alexander Aring7240cde2014-02-28 07:32:50 +0100563}
564
565static struct pernet_operations lowpan_frags_ops = {
566 .init = lowpan_frags_init_net,
567 .exit = lowpan_frags_exit_net,
568};
569
Eric Dumazet648700f2018-03-31 12:58:49 -0700570static u32 lowpan_key_hashfn(const void *data, u32 len, u32 seed)
571{
572 return jhash2(data,
573 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
574}
575
576static u32 lowpan_obj_hashfn(const void *data, u32 len, u32 seed)
577{
578 const struct inet_frag_queue *fq = data;
579
580 return jhash2((const u32 *)&fq->key,
581 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
582}
583
584static int lowpan_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
585{
586 const struct frag_lowpan_compare_key *key = arg->key;
587 const struct inet_frag_queue *fq = ptr;
588
589 return !!memcmp(&fq->key, key, sizeof(*key));
590}
591
592static const struct rhashtable_params lowpan_rhash_params = {
593 .head_offset = offsetof(struct inet_frag_queue, node),
594 .hashfn = lowpan_key_hashfn,
595 .obj_hashfn = lowpan_obj_hashfn,
596 .obj_cmpfn = lowpan_obj_cmpfn,
597 .automatic_shrinking = true,
598};
599
Alexander Aring7240cde2014-02-28 07:32:50 +0100600int __init lowpan_net_frag_init(void)
601{
602 int ret;
603
Alexander Aring7240cde2014-02-28 07:32:50 +0100604 lowpan_frags.constructor = lowpan_frag_init;
605 lowpan_frags.destructor = NULL;
Alexander Aring7240cde2014-02-28 07:32:50 +0100606 lowpan_frags.qsize = sizeof(struct frag_queue);
Alexander Aring7240cde2014-02-28 07:32:50 +0100607 lowpan_frags.frag_expire = lowpan_frag_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200608 lowpan_frags.frags_cache_name = lowpan_frags_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700609 lowpan_frags.rhash_params = lowpan_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200610 ret = inet_frags_init(&lowpan_frags);
611 if (ret)
Eric Dumazet807f1842018-03-31 12:58:46 -0700612 goto out;
Alexander Aring37147652014-03-07 11:06:54 +0100613
Eric Dumazet807f1842018-03-31 12:58:46 -0700614 ret = lowpan_frags_sysctl_register();
615 if (ret)
616 goto err_sysctl;
617
618 ret = register_pernet_subsys(&lowpan_frags_ops);
619 if (ret)
620 goto err_pernet;
621out:
Alexander Aring37147652014-03-07 11:06:54 +0100622 return ret;
Alexander Aring7240cde2014-02-28 07:32:50 +0100623err_pernet:
624 lowpan_frags_sysctl_unregister();
Eric Dumazet807f1842018-03-31 12:58:46 -0700625err_sysctl:
626 inet_frags_fini(&lowpan_frags);
Alexander Aring7240cde2014-02-28 07:32:50 +0100627 return ret;
628}
629
630void lowpan_net_frag_exit(void)
631{
632 inet_frags_fini(&lowpan_frags);
633 lowpan_frags_sysctl_unregister();
634 unregister_pernet_subsys(&lowpan_frags_ops);
635}