Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 1 | /* 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 Aring | cefc8c8 | 2014-03-05 14:29:05 +0100 | [diff] [blame] | 27 | #include <net/6lowpan.h> |
Florian Westphal | 70b095c | 2018-07-14 01:14:01 +0200 | [diff] [blame] | 28 | #include <net/ipv6_frag.h> |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 29 | #include <net/inet_frag.h> |
| 30 | |
Alexander Aring | 8691ee5 | 2015-01-04 17:10:54 +0100 | [diff] [blame] | 31 | #include "6lowpan_i.h" |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 32 | |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 33 | static const char lowpan_frags_cache_name[] = "lowpan-frags"; |
| 34 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 35 | static struct inet_frags lowpan_frags; |
| 36 | |
| 37 | static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 38 | struct sk_buff *prev, struct net_device *ldev); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 39 | |
Florian Westphal | 36c7778 | 2014-07-24 16:50:29 +0200 | [diff] [blame] | 40 | static void lowpan_frag_init(struct inet_frag_queue *q, const void *a) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 41 | { |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 42 | const struct frag_lowpan_compare_key *key = a; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 43 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 44 | BUILD_BUG_ON(sizeof(*key) > sizeof(q->key)); |
| 45 | memcpy(&q->key, key, sizeof(*key)); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 46 | } |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 47 | |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 48 | static void lowpan_frag_expire(struct timer_list *t) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 49 | { |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 50 | struct inet_frag_queue *frag = from_timer(frag, t, timer); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 51 | struct frag_queue *fq; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 52 | |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 53 | fq = container_of(frag, struct frag_queue, q); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 54 | |
Florian Westphal | 1779432 | 2014-03-13 20:58:03 +0100 | [diff] [blame] | 55 | spin_lock(&fq->q.lock); |
| 56 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 57 | if (fq->q.flags & INET_FRAG_COMPLETE) |
Florian Westphal | 1779432 | 2014-03-13 20:58:03 +0100 | [diff] [blame] | 58 | goto out; |
| 59 | |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 60 | inet_frag_kill(&fq->q); |
Florian Westphal | 1779432 | 2014-03-13 20:58:03 +0100 | [diff] [blame] | 61 | out: |
| 62 | spin_unlock(&fq->q.lock); |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 63 | inet_frag_put(&fq->q); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static inline struct lowpan_frag_queue * |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 67 | fq_find(struct net *net, const struct lowpan_802154_cb *cb, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 68 | const struct ieee802154_addr *src, |
| 69 | const struct ieee802154_addr *dst) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 70 | { |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 71 | struct netns_ieee802154_lowpan *ieee802154_lowpan = |
| 72 | net_ieee802154_lowpan(net); |
Alexander Aring | f18fa5d | 2018-04-20 14:54:13 -0400 | [diff] [blame] | 73 | struct frag_lowpan_compare_key key = {}; |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 74 | struct inet_frag_queue *q; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 75 | |
Alexander Aring | f18fa5d | 2018-04-20 14:54:13 -0400 | [diff] [blame] | 76 | key.tag = cb->d_tag; |
| 77 | key.d_size = cb->d_size; |
| 78 | key.src = *src; |
| 79 | key.dst = *dst; |
| 80 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 81 | q = inet_frag_find(&ieee802154_lowpan->frags, &key); |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 82 | if (!q) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 83 | return NULL; |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 84 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 85 | return container_of(q, struct lowpan_frag_queue, q); |
| 86 | } |
| 87 | |
| 88 | static int lowpan_frag_queue(struct lowpan_frag_queue *fq, |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 89 | struct sk_buff *skb, u8 frag_type) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 90 | { |
| 91 | struct sk_buff *prev, *next; |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 92 | struct net_device *ldev; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 93 | int end, offset; |
| 94 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 95 | if (fq->q.flags & INET_FRAG_COMPLETE) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 96 | goto err; |
| 97 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 98 | offset = lowpan_802154_cb(skb)->d_offset << 3; |
| 99 | end = lowpan_802154_cb(skb)->d_size; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 100 | |
| 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 Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 107 | ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 108 | goto err; |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 109 | fq->q.flags |= INET_FRAG_LAST_IN; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 110 | fq->q.len = end; |
| 111 | } else { |
| 112 | if (end > fq->q.len) { |
| 113 | /* Some bits beyond end -> corruption. */ |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 114 | if (fq->q.flags & INET_FRAG_LAST_IN) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 115 | 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 Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 125 | if (!prev || |
| 126 | lowpan_802154_cb(prev)->d_offset < |
| 127 | lowpan_802154_cb(skb)->d_offset) { |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 128 | next = NULL; |
| 129 | goto found; |
| 130 | } |
| 131 | prev = NULL; |
| 132 | for (next = fq->q.fragments; next != NULL; next = next->next) { |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 133 | if (lowpan_802154_cb(next)->d_offset >= |
| 134 | lowpan_802154_cb(skb)->d_offset) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 135 | break; /* bingo! */ |
| 136 | prev = next; |
| 137 | } |
| 138 | |
| 139 | found: |
| 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 Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 149 | ldev = skb->dev; |
| 150 | if (ldev) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 151 | skb->dev = NULL; |
| 152 | |
| 153 | fq->q.stamp = skb->tstamp; |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 154 | if (frag_type == LOWPAN_DISPATCH_FRAG1) |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 155 | fq->q.flags |= INET_FRAG_FIRST_IN; |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 156 | |
| 157 | fq->q.meat += skb->len; |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 158 | add_frag_mem_limit(fq->q.net, skb->truesize); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 159 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 160 | if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 161 | fq->q.meat == fq->q.len) { |
| 162 | int res; |
| 163 | unsigned long orefdst = skb->_skb_refdst; |
| 164 | |
| 165 | skb->_skb_refdst = 0UL; |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 166 | res = lowpan_frag_reasm(fq, prev, ldev); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 167 | skb->_skb_refdst = orefdst; |
| 168 | return res; |
| 169 | } |
| 170 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 171 | return -1; |
| 172 | err: |
| 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 | */ |
| 185 | static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *prev, |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 186 | struct net_device *ldev) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 187 | { |
| 188 | struct sk_buff *fp, *head = fq->q.fragments; |
| 189 | int sum_truesize; |
| 190 | |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 191 | inet_frag_kill(&fq->q); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 192 | |
| 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 Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 238 | add_frag_mem_limit(fq->q.net, clone->truesize); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 239 | } |
| 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 Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 261 | sub_frag_mem_limit(fq->q.net, sum_truesize); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 262 | |
| 263 | head->next = NULL; |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 264 | head->dev = ldev; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 265 | head->tstamp = fq->q.stamp; |
| 266 | |
| 267 | fq->q.fragments = NULL; |
| 268 | fq->q.fragments_tail = NULL; |
| 269 | |
| 270 | return 1; |
| 271 | out_oom: |
| 272 | net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n"); |
| 273 | return -1; |
| 274 | } |
| 275 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 276 | static 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 | |
| 294 | static 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 | |
| 308 | static 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 | |
| 323 | rxh_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 | |
| 331 | static int lowpan_get_cb(struct sk_buff *skb, u8 frag_type, |
| 332 | struct lowpan_802154_cb *cb) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 333 | { |
| 334 | bool fail; |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 335 | u8 high = 0, low = 0; |
Alexander Aring | f870b8c | 2014-10-06 11:00:51 +0200 | [diff] [blame] | 336 | __be16 d_tag = 0; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 337 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 338 | fail = lowpan_fetch_skb(skb, &high, 1); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 339 | fail |= lowpan_fetch_skb(skb, &low, 1); |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 340 | /* 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 Aring | f870b8c | 2014-10-06 11:00:51 +0200 | [diff] [blame] | 345 | fail |= lowpan_fetch_skb(skb, &d_tag, 2); |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 346 | cb->d_tag = ntohs(d_tag); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 347 | |
| 348 | if (frag_type == LOWPAN_DISPATCH_FRAGN) { |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 349 | fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 350 | } else { |
| 351 | skb_reset_network_header(skb); |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 352 | 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 Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | if (unlikely(fail)) |
| 360 | return -EIO; |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 365 | int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 366 | { |
| 367 | struct lowpan_frag_queue *fq; |
| 368 | struct net *net = dev_net(skb->dev); |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 369 | struct lowpan_802154_cb *cb = lowpan_802154_cb(skb); |
Alexander Aring | f18fa5d | 2018-04-20 14:54:13 -0400 | [diff] [blame] | 370 | struct ieee802154_hdr hdr = {}; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 371 | int err; |
| 372 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 373 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) |
| 374 | goto err; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 375 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 376 | err = lowpan_get_cb(skb, frag_type, cb); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 377 | if (err < 0) |
| 378 | goto err; |
| 379 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 380 | 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 Townsend | 6697dab | 2014-08-19 19:03:32 +0200 | [diff] [blame] | 387 | net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n"); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 388 | goto err; |
Martin Townsend | 6697dab | 2014-08-19 19:03:32 +0200 | [diff] [blame] | 389 | } |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 390 | |
Alexander Aring | 72a5e6b | 2015-09-02 14:21:25 +0200 | [diff] [blame] | 391 | fq = fq_find(net, cb, &hdr.source, &hdr.dest); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 392 | if (fq != NULL) { |
| 393 | int ret; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 394 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 395 | spin_lock(&fq->q.lock); |
| 396 | ret = lowpan_frag_queue(fq, skb, frag_type); |
| 397 | spin_unlock(&fq->q.lock); |
| 398 | |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 399 | inet_frag_put(&fq->q); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | err: |
| 404 | kfree_skb(skb); |
| 405 | return -1; |
| 406 | } |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 407 | |
| 408 | #ifdef CONFIG_SYSCTL |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 409 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 410 | static struct ctl_table lowpan_frags_ns_ctl_table[] = { |
| 411 | { |
| 412 | .procname = "6lowpanfrag_high_thresh", |
| 413 | .data = &init_net.ieee802154_lowpan.frags.high_thresh, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 414 | .maxlen = sizeof(unsigned long), |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 415 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 416 | .proc_handler = proc_doulongvec_minmax, |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 417 | .extra1 = &init_net.ieee802154_lowpan.frags.low_thresh |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 418 | }, |
| 419 | { |
| 420 | .procname = "6lowpanfrag_low_thresh", |
| 421 | .data = &init_net.ieee802154_lowpan.frags.low_thresh, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 422 | .maxlen = sizeof(unsigned long), |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 423 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 424 | .proc_handler = proc_doulongvec_minmax, |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 425 | .extra2 = &init_net.ieee802154_lowpan.frags.high_thresh |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 426 | }, |
| 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 Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 434 | { } |
| 435 | }; |
| 436 | |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 437 | /* secret interval has been deprecated */ |
| 438 | static int lowpan_frags_secret_interval_unused; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 439 | static struct ctl_table lowpan_frags_ctl_table[] = { |
| 440 | { |
| 441 | .procname = "6lowpanfrag_secret_interval", |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 442 | .data = &lowpan_frags_secret_interval_unused, |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 443 | .maxlen = sizeof(int), |
| 444 | .mode = 0644, |
| 445 | .proc_handler = proc_dointvec_jiffies, |
| 446 | }, |
| 447 | { } |
| 448 | }; |
| 449 | |
| 450 | static 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. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 454 | struct netns_ieee802154_lowpan *ieee802154_lowpan = |
| 455 | net_ieee802154_lowpan(net); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 456 | |
| 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. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 464 | table[0].data = &ieee802154_lowpan->frags.high_thresh; |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 465 | table[0].extra1 = &ieee802154_lowpan->frags.low_thresh; |
| 466 | table[0].extra2 = &init_net.ieee802154_lowpan.frags.high_thresh; |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 467 | table[1].data = &ieee802154_lowpan->frags.low_thresh; |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 468 | table[1].extra2 = &ieee802154_lowpan->frags.high_thresh; |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 469 | table[2].data = &ieee802154_lowpan->frags.timeout; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 470 | |
| 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. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 480 | ieee802154_lowpan->sysctl.frags_hdr = hdr; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 481 | return 0; |
| 482 | |
| 483 | err_reg: |
| 484 | if (!net_eq(net, &init_net)) |
| 485 | kfree(table); |
| 486 | err_alloc: |
| 487 | return -ENOMEM; |
| 488 | } |
| 489 | |
| 490 | static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net) |
| 491 | { |
| 492 | struct ctl_table *table; |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 493 | struct netns_ieee802154_lowpan *ieee802154_lowpan = |
| 494 | net_ieee802154_lowpan(net); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 495 | |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 496 | table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg; |
| 497 | unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 498 | if (!net_eq(net, &init_net)) |
| 499 | kfree(table); |
| 500 | } |
| 501 | |
| 502 | static struct ctl_table_header *lowpan_ctl_header; |
| 503 | |
Fabian Frederick | 3243acd | 2014-09-30 22:34:08 +0200 | [diff] [blame] | 504 | static int __init lowpan_frags_sysctl_register(void) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 505 | { |
| 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 | |
| 512 | static void lowpan_frags_sysctl_unregister(void) |
| 513 | { |
| 514 | unregister_net_sysctl_table(lowpan_ctl_header); |
| 515 | } |
| 516 | #else |
Fabian Frederick | f0a0c1c | 2014-10-01 07:27:46 +0200 | [diff] [blame] | 517 | static inline int lowpan_frags_ns_sysctl_register(struct net *net) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 518 | { |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static inline void lowpan_frags_ns_sysctl_unregister(struct net *net) |
| 523 | { |
| 524 | } |
| 525 | |
Fabian Frederick | f0a0c1c | 2014-10-01 07:27:46 +0200 | [diff] [blame] | 526 | static inline int __init lowpan_frags_sysctl_register(void) |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 527 | { |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static inline void lowpan_frags_sysctl_unregister(void) |
| 532 | { |
| 533 | } |
| 534 | #endif |
| 535 | |
| 536 | static int __net_init lowpan_frags_init_net(struct net *net) |
| 537 | { |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 538 | struct netns_ieee802154_lowpan *ieee802154_lowpan = |
| 539 | net_ieee802154_lowpan(net); |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 540 | int res; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 541 | |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 542 | 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 Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 545 | ieee802154_lowpan->frags.f = &lowpan_frags; |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 546 | |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 547 | 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 Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 552 | inet_frags_exit_net(&ieee802154_lowpan->frags); |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 553 | return res; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static void __net_exit lowpan_frags_exit_net(struct net *net) |
| 557 | { |
Luis R. Rodriguez | 599018a | 2014-04-17 18:22:54 -0700 | [diff] [blame] | 558 | struct netns_ieee802154_lowpan *ieee802154_lowpan = |
| 559 | net_ieee802154_lowpan(net); |
| 560 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 561 | lowpan_frags_ns_sysctl_unregister(net); |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 562 | inet_frags_exit_net(&ieee802154_lowpan->frags); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static struct pernet_operations lowpan_frags_ops = { |
| 566 | .init = lowpan_frags_init_net, |
| 567 | .exit = lowpan_frags_exit_net, |
| 568 | }; |
| 569 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 570 | static 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 | |
| 576 | static 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 | |
| 584 | static 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 | |
| 592 | static 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 Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 600 | int __init lowpan_net_frag_init(void) |
| 601 | { |
| 602 | int ret; |
| 603 | |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 604 | lowpan_frags.constructor = lowpan_frag_init; |
| 605 | lowpan_frags.destructor = NULL; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 606 | lowpan_frags.qsize = sizeof(struct frag_queue); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 607 | lowpan_frags.frag_expire = lowpan_frag_expire; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 608 | lowpan_frags.frags_cache_name = lowpan_frags_cache_name; |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 609 | lowpan_frags.rhash_params = lowpan_rhash_params; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 610 | ret = inet_frags_init(&lowpan_frags); |
| 611 | if (ret) |
Eric Dumazet | 807f184 | 2018-03-31 12:58:46 -0700 | [diff] [blame] | 612 | goto out; |
Alexander Aring | 3714765 | 2014-03-07 11:06:54 +0100 | [diff] [blame] | 613 | |
Eric Dumazet | 807f184 | 2018-03-31 12:58:46 -0700 | [diff] [blame] | 614 | 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; |
| 621 | out: |
Alexander Aring | 3714765 | 2014-03-07 11:06:54 +0100 | [diff] [blame] | 622 | return ret; |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 623 | err_pernet: |
| 624 | lowpan_frags_sysctl_unregister(); |
Eric Dumazet | 807f184 | 2018-03-31 12:58:46 -0700 | [diff] [blame] | 625 | err_sysctl: |
| 626 | inet_frags_fini(&lowpan_frags); |
Alexander Aring | 7240cde | 2014-02-28 07:32:50 +0100 | [diff] [blame] | 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | void 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 | } |