blob: 0fa0121f85d4531751566c26e18cc2a23c017da6 [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>
Alexander Aring7240cde2014-02-28 07:32:50 +010028#include <net/ipv6.h>
29#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 struct lowpan_frag_queue *fq;
Alexander Aring7240cde2014-02-28 07:32:50 +010044
45 fq = container_of(q, struct lowpan_frag_queue, q);
46
Eric Dumazet648700f2018-03-31 12:58:49 -070047 BUILD_BUG_ON(sizeof(*key) > sizeof(q->key));
48 memcpy(&q->key, key, sizeof(*key));
Alexander Aring7240cde2014-02-28 07:32:50 +010049}
Alexander Aring7240cde2014-02-28 07:32:50 +010050
Kees Cook78802012017-10-16 17:29:20 -070051static void lowpan_frag_expire(struct timer_list *t)
Alexander Aring7240cde2014-02-28 07:32:50 +010052{
Kees Cook78802012017-10-16 17:29:20 -070053 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Alexander Aring7240cde2014-02-28 07:32:50 +010054 struct frag_queue *fq;
55 struct net *net;
56
Kees Cook78802012017-10-16 17:29:20 -070057 fq = container_of(frag, struct frag_queue, q);
Alexander Aring7240cde2014-02-28 07:32:50 +010058 net = container_of(fq->q.net, struct net, ieee802154_lowpan.frags);
59
Florian Westphal17794322014-03-13 20:58:03 +010060 spin_lock(&fq->q.lock);
61
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +020062 if (fq->q.flags & INET_FRAG_COMPLETE)
Florian Westphal17794322014-03-13 20:58:03 +010063 goto out;
64
Eric Dumazet093ba722018-03-31 12:58:44 -070065 inet_frag_kill(&fq->q);
Florian Westphal17794322014-03-13 20:58:03 +010066out:
67 spin_unlock(&fq->q.lock);
Eric Dumazet093ba722018-03-31 12:58:44 -070068 inet_frag_put(&fq->q);
Alexander Aring7240cde2014-02-28 07:32:50 +010069}
70
71static inline struct lowpan_frag_queue *
Alexander Aring72a5e6b2015-09-02 14:21:25 +020072fq_find(struct net *net, const struct lowpan_802154_cb *cb,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +010073 const struct ieee802154_addr *src,
74 const struct ieee802154_addr *dst)
Alexander Aring7240cde2014-02-28 07:32:50 +010075{
Luis R. Rodriguez599018a2014-04-17 18:22:54 -070076 struct netns_ieee802154_lowpan *ieee802154_lowpan =
77 net_ieee802154_lowpan(net);
Eric Dumazet648700f2018-03-31 12:58:49 -070078 struct frag_lowpan_compare_key key = {
79 .tag = cb->d_tag,
80 .d_size = cb->d_size,
81 .src = *src,
82 .dst = *dst,
83 };
84 struct inet_frag_queue *q;
Alexander Aring7240cde2014-02-28 07:32:50 +010085
Eric Dumazet648700f2018-03-31 12:58:49 -070086 q = inet_frag_find(&ieee802154_lowpan->frags, &key);
Alexander Aring7240cde2014-02-28 07:32:50 +010087 if (IS_ERR_OR_NULL(q)) {
88 inet_frag_maybe_warn_overflow(q, pr_fmt());
89 return NULL;
90 }
91 return container_of(q, struct lowpan_frag_queue, q);
92}
93
94static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
Alexander Aring72a5e6b2015-09-02 14:21:25 +020095 struct sk_buff *skb, u8 frag_type)
Alexander Aring7240cde2014-02-28 07:32:50 +010096{
97 struct sk_buff *prev, *next;
Alexander Aringf4606582015-09-02 14:21:16 +020098 struct net_device *ldev;
Alexander Aring7240cde2014-02-28 07:32:50 +010099 int end, offset;
100
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200101 if (fq->q.flags & INET_FRAG_COMPLETE)
Alexander Aring7240cde2014-02-28 07:32:50 +0100102 goto err;
103
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200104 offset = lowpan_802154_cb(skb)->d_offset << 3;
105 end = lowpan_802154_cb(skb)->d_size;
Alexander Aring7240cde2014-02-28 07:32:50 +0100106
107 /* Is this the final fragment? */
108 if (offset + skb->len == end) {
109 /* If we already have some bits beyond end
110 * or have different end, the segment is corrupted.
111 */
112 if (end < fq->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200113 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
Alexander Aring7240cde2014-02-28 07:32:50 +0100114 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200115 fq->q.flags |= INET_FRAG_LAST_IN;
Alexander Aring7240cde2014-02-28 07:32:50 +0100116 fq->q.len = end;
117 } else {
118 if (end > fq->q.len) {
119 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200120 if (fq->q.flags & INET_FRAG_LAST_IN)
Alexander Aring7240cde2014-02-28 07:32:50 +0100121 goto err;
122 fq->q.len = end;
123 }
124 }
125
126 /* Find out which fragments are in front and at the back of us
127 * in the chain of fragments so far. We must know where to put
128 * this fragment, right?
129 */
130 prev = fq->q.fragments_tail;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200131 if (!prev ||
132 lowpan_802154_cb(prev)->d_offset <
133 lowpan_802154_cb(skb)->d_offset) {
Alexander Aring7240cde2014-02-28 07:32:50 +0100134 next = NULL;
135 goto found;
136 }
137 prev = NULL;
138 for (next = fq->q.fragments; next != NULL; next = next->next) {
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200139 if (lowpan_802154_cb(next)->d_offset >=
140 lowpan_802154_cb(skb)->d_offset)
Alexander Aring7240cde2014-02-28 07:32:50 +0100141 break; /* bingo! */
142 prev = next;
143 }
144
145found:
146 /* Insert this fragment in the chain of fragments. */
147 skb->next = next;
148 if (!next)
149 fq->q.fragments_tail = skb;
150 if (prev)
151 prev->next = skb;
152 else
153 fq->q.fragments = skb;
154
Alexander Aringf4606582015-09-02 14:21:16 +0200155 ldev = skb->dev;
156 if (ldev)
Alexander Aring7240cde2014-02-28 07:32:50 +0100157 skb->dev = NULL;
158
159 fq->q.stamp = skb->tstamp;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200160 if (frag_type == LOWPAN_DISPATCH_FRAG1)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200161 fq->q.flags |= INET_FRAG_FIRST_IN;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200162
163 fq->q.meat += skb->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200164 add_frag_mem_limit(fq->q.net, skb->truesize);
Alexander Aring7240cde2014-02-28 07:32:50 +0100165
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200166 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Alexander Aring7240cde2014-02-28 07:32:50 +0100167 fq->q.meat == fq->q.len) {
168 int res;
169 unsigned long orefdst = skb->_skb_refdst;
170
171 skb->_skb_refdst = 0UL;
Alexander Aringf4606582015-09-02 14:21:16 +0200172 res = lowpan_frag_reasm(fq, prev, ldev);
Alexander Aring7240cde2014-02-28 07:32:50 +0100173 skb->_skb_refdst = orefdst;
174 return res;
175 }
176
Alexander Aring7240cde2014-02-28 07:32:50 +0100177 return -1;
178err:
179 kfree_skb(skb);
180 return -1;
181}
182
183/* Check if this packet is complete.
184 * Returns NULL on failure by any reason, and pointer
185 * to current nexthdr field in reassembled frame.
186 *
187 * It is called with locked fq, and caller must check that
188 * queue is eligible for reassembly i.e. it is not COMPLETE,
189 * the last and the first frames arrived and all the bits are here.
190 */
191static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *prev,
Alexander Aringf4606582015-09-02 14:21:16 +0200192 struct net_device *ldev)
Alexander Aring7240cde2014-02-28 07:32:50 +0100193{
194 struct sk_buff *fp, *head = fq->q.fragments;
195 int sum_truesize;
196
Eric Dumazet093ba722018-03-31 12:58:44 -0700197 inet_frag_kill(&fq->q);
Alexander Aring7240cde2014-02-28 07:32:50 +0100198
199 /* Make the one we just received the head. */
200 if (prev) {
201 head = prev->next;
202 fp = skb_clone(head, GFP_ATOMIC);
203
204 if (!fp)
205 goto out_oom;
206
207 fp->next = head->next;
208 if (!fp->next)
209 fq->q.fragments_tail = fp;
210 prev->next = fp;
211
212 skb_morph(head, fq->q.fragments);
213 head->next = fq->q.fragments->next;
214
215 consume_skb(fq->q.fragments);
216 fq->q.fragments = head;
217 }
218
219 /* Head of list must not be cloned. */
220 if (skb_unclone(head, GFP_ATOMIC))
221 goto out_oom;
222
223 /* If the first fragment is fragmented itself, we split
224 * it to two chunks: the first with data and paged part
225 * and the second, holding only fragments.
226 */
227 if (skb_has_frag_list(head)) {
228 struct sk_buff *clone;
229 int i, plen = 0;
230
231 clone = alloc_skb(0, GFP_ATOMIC);
232 if (!clone)
233 goto out_oom;
234 clone->next = head->next;
235 head->next = clone;
236 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
237 skb_frag_list_init(head);
238 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
239 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
240 clone->len = head->data_len - plen;
241 clone->data_len = clone->len;
242 head->data_len -= clone->len;
243 head->len -= clone->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200244 add_frag_mem_limit(fq->q.net, clone->truesize);
Alexander Aring7240cde2014-02-28 07:32:50 +0100245 }
246
247 WARN_ON(head == NULL);
248
249 sum_truesize = head->truesize;
250 for (fp = head->next; fp;) {
251 bool headstolen;
252 int delta;
253 struct sk_buff *next = fp->next;
254
255 sum_truesize += fp->truesize;
256 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
257 kfree_skb_partial(fp, headstolen);
258 } else {
259 if (!skb_shinfo(head)->frag_list)
260 skb_shinfo(head)->frag_list = fp;
261 head->data_len += fp->len;
262 head->len += fp->len;
263 head->truesize += fp->truesize;
264 }
265 fp = next;
266 }
Florian Westphal0e60d242015-07-23 12:05:38 +0200267 sub_frag_mem_limit(fq->q.net, sum_truesize);
Alexander Aring7240cde2014-02-28 07:32:50 +0100268
269 head->next = NULL;
Alexander Aringf4606582015-09-02 14:21:16 +0200270 head->dev = ldev;
Alexander Aring7240cde2014-02-28 07:32:50 +0100271 head->tstamp = fq->q.stamp;
272
273 fq->q.fragments = NULL;
274 fq->q.fragments_tail = NULL;
275
276 return 1;
277out_oom:
278 net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
279 return -1;
280}
281
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200282static int lowpan_frag_rx_handlers_result(struct sk_buff *skb,
283 lowpan_rx_result res)
284{
285 switch (res) {
286 case RX_QUEUED:
287 return NET_RX_SUCCESS;
288 case RX_CONTINUE:
289 /* nobody cared about this packet */
290 net_warn_ratelimited("%s: received unknown dispatch\n",
291 __func__);
292
293 /* fall-through */
294 default:
295 /* all others failure */
296 return NET_RX_DROP;
297 }
298}
299
300static lowpan_rx_result lowpan_frag_rx_h_iphc(struct sk_buff *skb)
301{
302 int ret;
303
304 if (!lowpan_is_iphc(*skb_network_header(skb)))
305 return RX_CONTINUE;
306
307 ret = lowpan_iphc_decompress(skb);
308 if (ret < 0)
309 return RX_DROP;
310
311 return RX_QUEUED;
312}
313
314static int lowpan_invoke_frag_rx_handlers(struct sk_buff *skb)
315{
316 lowpan_rx_result res;
317
318#define CALL_RXH(rxh) \
319 do { \
320 res = rxh(skb); \
321 if (res != RX_CONTINUE) \
322 goto rxh_next; \
323 } while (0)
324
325 /* likely at first */
326 CALL_RXH(lowpan_frag_rx_h_iphc);
327 CALL_RXH(lowpan_rx_h_ipv6);
328
329rxh_next:
330 return lowpan_frag_rx_handlers_result(skb, res);
331#undef CALL_RXH
332}
333
334#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK 0x07
335#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT 8
336
337static int lowpan_get_cb(struct sk_buff *skb, u8 frag_type,
338 struct lowpan_802154_cb *cb)
Alexander Aring7240cde2014-02-28 07:32:50 +0100339{
340 bool fail;
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200341 u8 high = 0, low = 0;
Alexander Aringf870b8c2014-10-06 11:00:51 +0200342 __be16 d_tag = 0;
Alexander Aring7240cde2014-02-28 07:32:50 +0100343
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200344 fail = lowpan_fetch_skb(skb, &high, 1);
Alexander Aring7240cde2014-02-28 07:32:50 +0100345 fail |= lowpan_fetch_skb(skb, &low, 1);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200346 /* remove the dispatch value and use first three bits as high value
347 * for the datagram size
348 */
349 cb->d_size = (high & LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK) <<
350 LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT | low;
Alexander Aringf870b8c2014-10-06 11:00:51 +0200351 fail |= lowpan_fetch_skb(skb, &d_tag, 2);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200352 cb->d_tag = ntohs(d_tag);
Alexander Aring7240cde2014-02-28 07:32:50 +0100353
354 if (frag_type == LOWPAN_DISPATCH_FRAGN) {
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200355 fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1);
Alexander Aring7240cde2014-02-28 07:32:50 +0100356 } else {
357 skb_reset_network_header(skb);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200358 cb->d_offset = 0;
359 /* check if datagram_size has ipv6hdr on FRAG1 */
360 fail |= cb->d_size < sizeof(struct ipv6hdr);
361 /* check if we can dereference the dispatch value */
362 fail |= !skb->len;
Alexander Aring7240cde2014-02-28 07:32:50 +0100363 }
364
365 if (unlikely(fail))
366 return -EIO;
367
368 return 0;
369}
370
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200371int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type)
Alexander Aring7240cde2014-02-28 07:32:50 +0100372{
373 struct lowpan_frag_queue *fq;
374 struct net *net = dev_net(skb->dev);
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200375 struct lowpan_802154_cb *cb = lowpan_802154_cb(skb);
376 struct ieee802154_hdr hdr;
Alexander Aring7240cde2014-02-28 07:32:50 +0100377 int err;
378
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200379 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
380 goto err;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100381
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200382 err = lowpan_get_cb(skb, frag_type, cb);
Alexander Aring7240cde2014-02-28 07:32:50 +0100383 if (err < 0)
384 goto err;
385
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200386 if (frag_type == LOWPAN_DISPATCH_FRAG1) {
387 err = lowpan_invoke_frag_rx_handlers(skb);
388 if (err == NET_RX_DROP)
389 goto err;
390 }
391
392 if (cb->d_size > IPV6_MIN_MTU) {
Martin Townsend6697dab2014-08-19 19:03:32 +0200393 net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
Alexander Aring7240cde2014-02-28 07:32:50 +0100394 goto err;
Martin Townsend6697dab2014-08-19 19:03:32 +0200395 }
Alexander Aring7240cde2014-02-28 07:32:50 +0100396
Alexander Aring72a5e6b2015-09-02 14:21:25 +0200397 fq = fq_find(net, cb, &hdr.source, &hdr.dest);
Alexander Aring7240cde2014-02-28 07:32:50 +0100398 if (fq != NULL) {
399 int ret;
Varka Bhadram4710d802014-07-02 09:01:09 +0530400
Alexander Aring7240cde2014-02-28 07:32:50 +0100401 spin_lock(&fq->q.lock);
402 ret = lowpan_frag_queue(fq, skb, frag_type);
403 spin_unlock(&fq->q.lock);
404
Eric Dumazet093ba722018-03-31 12:58:44 -0700405 inet_frag_put(&fq->q);
Alexander Aring7240cde2014-02-28 07:32:50 +0100406 return ret;
407 }
408
409err:
410 kfree_skb(skb);
411 return -1;
412}
Alexander Aring7240cde2014-02-28 07:32:50 +0100413
414#ifdef CONFIG_SYSCTL
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200415static int zero;
416
Alexander Aring7240cde2014-02-28 07:32:50 +0100417static struct ctl_table lowpan_frags_ns_ctl_table[] = {
418 {
419 .procname = "6lowpanfrag_high_thresh",
420 .data = &init_net.ieee802154_lowpan.frags.high_thresh,
421 .maxlen = sizeof(int),
422 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200423 .proc_handler = proc_dointvec_minmax,
424 .extra1 = &init_net.ieee802154_lowpan.frags.low_thresh
Alexander Aring7240cde2014-02-28 07:32:50 +0100425 },
426 {
427 .procname = "6lowpanfrag_low_thresh",
428 .data = &init_net.ieee802154_lowpan.frags.low_thresh,
429 .maxlen = sizeof(int),
430 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200431 .proc_handler = proc_dointvec_minmax,
432 .extra1 = &zero,
433 .extra2 = &init_net.ieee802154_lowpan.frags.high_thresh
Alexander Aring7240cde2014-02-28 07:32:50 +0100434 },
435 {
436 .procname = "6lowpanfrag_time",
437 .data = &init_net.ieee802154_lowpan.frags.timeout,
438 .maxlen = sizeof(int),
439 .mode = 0644,
440 .proc_handler = proc_dointvec_jiffies,
441 },
Alexander Aring7240cde2014-02-28 07:32:50 +0100442 { }
443};
444
Florian Westphale3a57d12014-07-24 16:50:35 +0200445/* secret interval has been deprecated */
446static int lowpan_frags_secret_interval_unused;
Alexander Aring7240cde2014-02-28 07:32:50 +0100447static struct ctl_table lowpan_frags_ctl_table[] = {
448 {
449 .procname = "6lowpanfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200450 .data = &lowpan_frags_secret_interval_unused,
Alexander Aring7240cde2014-02-28 07:32:50 +0100451 .maxlen = sizeof(int),
452 .mode = 0644,
453 .proc_handler = proc_dointvec_jiffies,
454 },
455 { }
456};
457
458static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
459{
460 struct ctl_table *table;
461 struct ctl_table_header *hdr;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700462 struct netns_ieee802154_lowpan *ieee802154_lowpan =
463 net_ieee802154_lowpan(net);
Alexander Aring7240cde2014-02-28 07:32:50 +0100464
465 table = lowpan_frags_ns_ctl_table;
466 if (!net_eq(net, &init_net)) {
467 table = kmemdup(table, sizeof(lowpan_frags_ns_ctl_table),
468 GFP_KERNEL);
469 if (table == NULL)
470 goto err_alloc;
471
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700472 table[0].data = &ieee802154_lowpan->frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200473 table[0].extra1 = &ieee802154_lowpan->frags.low_thresh;
474 table[0].extra2 = &init_net.ieee802154_lowpan.frags.high_thresh;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700475 table[1].data = &ieee802154_lowpan->frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200476 table[1].extra2 = &ieee802154_lowpan->frags.high_thresh;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700477 table[2].data = &ieee802154_lowpan->frags.timeout;
Alexander Aring7240cde2014-02-28 07:32:50 +0100478
479 /* Don't export sysctls to unprivileged users */
480 if (net->user_ns != &init_user_ns)
481 table[0].procname = NULL;
482 }
483
484 hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
485 if (hdr == NULL)
486 goto err_reg;
487
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700488 ieee802154_lowpan->sysctl.frags_hdr = hdr;
Alexander Aring7240cde2014-02-28 07:32:50 +0100489 return 0;
490
491err_reg:
492 if (!net_eq(net, &init_net))
493 kfree(table);
494err_alloc:
495 return -ENOMEM;
496}
497
498static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net)
499{
500 struct ctl_table *table;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700501 struct netns_ieee802154_lowpan *ieee802154_lowpan =
502 net_ieee802154_lowpan(net);
Alexander Aring7240cde2014-02-28 07:32:50 +0100503
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700504 table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
505 unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
Alexander Aring7240cde2014-02-28 07:32:50 +0100506 if (!net_eq(net, &init_net))
507 kfree(table);
508}
509
510static struct ctl_table_header *lowpan_ctl_header;
511
Fabian Frederick3243acd2014-09-30 22:34:08 +0200512static int __init lowpan_frags_sysctl_register(void)
Alexander Aring7240cde2014-02-28 07:32:50 +0100513{
514 lowpan_ctl_header = register_net_sysctl(&init_net,
515 "net/ieee802154/6lowpan",
516 lowpan_frags_ctl_table);
517 return lowpan_ctl_header == NULL ? -ENOMEM : 0;
518}
519
520static void lowpan_frags_sysctl_unregister(void)
521{
522 unregister_net_sysctl_table(lowpan_ctl_header);
523}
524#else
Fabian Frederickf0a0c1c2014-10-01 07:27:46 +0200525static inline int lowpan_frags_ns_sysctl_register(struct net *net)
Alexander Aring7240cde2014-02-28 07:32:50 +0100526{
527 return 0;
528}
529
530static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
531{
532}
533
Fabian Frederickf0a0c1c2014-10-01 07:27:46 +0200534static inline int __init lowpan_frags_sysctl_register(void)
Alexander Aring7240cde2014-02-28 07:32:50 +0100535{
536 return 0;
537}
538
539static inline void lowpan_frags_sysctl_unregister(void)
540{
541}
542#endif
543
544static int __net_init lowpan_frags_init_net(struct net *net)
545{
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700546 struct netns_ieee802154_lowpan *ieee802154_lowpan =
547 net_ieee802154_lowpan(net);
Eric Dumazet787bea72018-03-31 12:58:43 -0700548 int res;
Alexander Aring7240cde2014-02-28 07:32:50 +0100549
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700550 ieee802154_lowpan->frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
551 ieee802154_lowpan->frags.low_thresh = IPV6_FRAG_LOW_THRESH;
552 ieee802154_lowpan->frags.timeout = IPV6_FRAG_TIMEOUT;
Eric Dumazet093ba722018-03-31 12:58:44 -0700553 ieee802154_lowpan->frags.f = &lowpan_frags;
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700554
Eric Dumazet787bea72018-03-31 12:58:43 -0700555 res = inet_frags_init_net(&ieee802154_lowpan->frags);
556 if (res < 0)
557 return res;
558 res = lowpan_frags_ns_sysctl_register(net);
559 if (res < 0)
Eric Dumazet093ba722018-03-31 12:58:44 -0700560 inet_frags_exit_net(&ieee802154_lowpan->frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700561 return res;
Alexander Aring7240cde2014-02-28 07:32:50 +0100562}
563
564static void __net_exit lowpan_frags_exit_net(struct net *net)
565{
Luis R. Rodriguez599018a2014-04-17 18:22:54 -0700566 struct netns_ieee802154_lowpan *ieee802154_lowpan =
567 net_ieee802154_lowpan(net);
568
Alexander Aring7240cde2014-02-28 07:32:50 +0100569 lowpan_frags_ns_sysctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700570 inet_frags_exit_net(&ieee802154_lowpan->frags);
Alexander Aring7240cde2014-02-28 07:32:50 +0100571}
572
573static struct pernet_operations lowpan_frags_ops = {
574 .init = lowpan_frags_init_net,
575 .exit = lowpan_frags_exit_net,
576};
577
Eric Dumazet648700f2018-03-31 12:58:49 -0700578static u32 lowpan_key_hashfn(const void *data, u32 len, u32 seed)
579{
580 return jhash2(data,
581 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
582}
583
584static u32 lowpan_obj_hashfn(const void *data, u32 len, u32 seed)
585{
586 const struct inet_frag_queue *fq = data;
587
588 return jhash2((const u32 *)&fq->key,
589 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
590}
591
592static int lowpan_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
593{
594 const struct frag_lowpan_compare_key *key = arg->key;
595 const struct inet_frag_queue *fq = ptr;
596
597 return !!memcmp(&fq->key, key, sizeof(*key));
598}
599
600static const struct rhashtable_params lowpan_rhash_params = {
601 .head_offset = offsetof(struct inet_frag_queue, node),
602 .hashfn = lowpan_key_hashfn,
603 .obj_hashfn = lowpan_obj_hashfn,
604 .obj_cmpfn = lowpan_obj_cmpfn,
605 .automatic_shrinking = true,
606};
607
Alexander Aring7240cde2014-02-28 07:32:50 +0100608int __init lowpan_net_frag_init(void)
609{
610 int ret;
611
Alexander Aring7240cde2014-02-28 07:32:50 +0100612 lowpan_frags.constructor = lowpan_frag_init;
613 lowpan_frags.destructor = NULL;
Alexander Aring7240cde2014-02-28 07:32:50 +0100614 lowpan_frags.qsize = sizeof(struct frag_queue);
Alexander Aring7240cde2014-02-28 07:32:50 +0100615 lowpan_frags.frag_expire = lowpan_frag_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200616 lowpan_frags.frags_cache_name = lowpan_frags_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700617 lowpan_frags.rhash_params = lowpan_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200618 ret = inet_frags_init(&lowpan_frags);
619 if (ret)
Eric Dumazet807f1842018-03-31 12:58:46 -0700620 goto out;
Alexander Aring37147652014-03-07 11:06:54 +0100621
Eric Dumazet807f1842018-03-31 12:58:46 -0700622 ret = lowpan_frags_sysctl_register();
623 if (ret)
624 goto err_sysctl;
625
626 ret = register_pernet_subsys(&lowpan_frags_ops);
627 if (ret)
628 goto err_pernet;
629out:
Alexander Aring37147652014-03-07 11:06:54 +0100630 return ret;
Alexander Aring7240cde2014-02-28 07:32:50 +0100631err_pernet:
632 lowpan_frags_sysctl_unregister();
Eric Dumazet807f1842018-03-31 12:58:46 -0700633err_sysctl:
634 inet_frags_fini(&lowpan_frags);
Alexander Aring7240cde2014-02-28 07:32:50 +0100635 return ret;
636}
637
638void lowpan_net_frag_exit(void)
639{
640 inet_frags_fini(&lowpan_frags);
641 lowpan_frags_sysctl_unregister();
642 unregister_pernet_subsys(&lowpan_frags_ops);
643}