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