]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 fragment reassembly | |
1ab1457c | 3 | * Linux INET6 implementation |
1da177e4 LT |
4 | * |
5 | * Authors: | |
1ab1457c | 6 | * Pedro Roque <[email protected]> |
1da177e4 | 7 | * |
1da177e4 LT |
8 | * Based on: net/ipv4/ip_fragment.c |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version | |
13 | * 2 of the License, or (at your option) any later version. | |
14 | */ | |
15 | ||
1ab1457c YH |
16 | /* |
17 | * Fixes: | |
1da177e4 LT |
18 | * Andi Kleen Make it work with multiple hosts. |
19 | * More RFC compliance. | |
20 | * | |
21 | * Horst von Brand Add missing #include <linux/string.h> | |
22 | * Alexey Kuznetsov SMP races, threading, cleanup. | |
23 | * Patrick McHardy LRU queue of frag heads for evictor. | |
24 | * Mitsuru KANDA @USAGI Register inet6_protocol{}. | |
25 | * David Stevens and | |
26 | * YOSHIFUJI,H. @USAGI Always remove fragment header to | |
27 | * calculate ICV correctly. | |
28 | */ | |
5a3da1fe HFS |
29 | |
30 | #define pr_fmt(fmt) "IPv6: " fmt | |
31 | ||
1da177e4 LT |
32 | #include <linux/errno.h> |
33 | #include <linux/types.h> | |
34 | #include <linux/string.h> | |
35 | #include <linux/socket.h> | |
36 | #include <linux/sockios.h> | |
37 | #include <linux/jiffies.h> | |
38 | #include <linux/net.h> | |
39 | #include <linux/list.h> | |
40 | #include <linux/netdevice.h> | |
41 | #include <linux/in6.h> | |
42 | #include <linux/ipv6.h> | |
43 | #include <linux/icmpv6.h> | |
44 | #include <linux/random.h> | |
45 | #include <linux/jhash.h> | |
f61944ef | 46 | #include <linux/skbuff.h> |
5a0e3ad6 | 47 | #include <linux/slab.h> |
bc3b2d7f | 48 | #include <linux/export.h> |
1da177e4 LT |
49 | |
50 | #include <net/sock.h> | |
51 | #include <net/snmp.h> | |
52 | ||
53 | #include <net/ipv6.h> | |
a11d206d | 54 | #include <net/ip6_route.h> |
1da177e4 LT |
55 | #include <net/protocol.h> |
56 | #include <net/transp_v6.h> | |
57 | #include <net/rawv6.h> | |
58 | #include <net/ndisc.h> | |
59 | #include <net/addrconf.h> | |
5ab11c98 | 60 | #include <net/inet_frag.h> |
eec2e618 | 61 | #include <net/inet_ecn.h> |
1da177e4 | 62 | |
d4ad4d22 NA |
63 | static const char ip6_frag_cache_name[] = "ip6-frags"; |
64 | ||
fc08c258 | 65 | static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) |
eec2e618 HFS |
66 | { |
67 | return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); | |
68 | } | |
1da177e4 | 69 | |
7eb95156 | 70 | static struct inet_frags ip6_frags; |
1da177e4 | 71 | |
f61944ef HX |
72 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
73 | struct net_device *dev); | |
74 | ||
36c77782 | 75 | void ip6_frag_init(struct inet_frag_queue *q, const void *a) |
1da177e4 | 76 | { |
c6fda282 | 77 | struct frag_queue *fq = container_of(q, struct frag_queue, q); |
648700f7 | 78 | const struct frag_v6_compare_key *key = a; |
c6fda282 | 79 | |
648700f7 ED |
80 | q->key.v6 = *key; |
81 | fq->ecn = 0; | |
1da177e4 | 82 | } |
c6fda282 | 83 | EXPORT_SYMBOL(ip6_frag_init); |
1da177e4 | 84 | |
093ba729 | 85 | void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq) |
1da177e4 | 86 | { |
a11d206d | 87 | struct net_device *dev = NULL; |
05c0b86b | 88 | struct sk_buff *head; |
e521db9d | 89 | |
05c0b86b | 90 | rcu_read_lock(); |
5ab11c98 | 91 | spin_lock(&fq->q.lock); |
1da177e4 | 92 | |
06aa8b8a | 93 | if (fq->q.flags & INET_FRAG_COMPLETE) |
1da177e4 LT |
94 | goto out; |
95 | ||
093ba729 | 96 | inet_frag_kill(&fq->q); |
1da177e4 | 97 | |
69df9d59 | 98 | dev = dev_get_by_index_rcu(net, fq->iif); |
a11d206d | 99 | if (!dev) |
05c0b86b | 100 | goto out; |
a11d206d | 101 | |
1d015503 | 102 | __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); |
1d015503 | 103 | __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT); |
2e404f63 | 104 | |
78c784c4 | 105 | /* Don't send error if the first segment did not arrive. */ |
05c0b86b ED |
106 | head = fq->q.fragments; |
107 | if (!(fq->q.flags & INET_FRAG_FIRST_IN) || !head) | |
108 | goto out; | |
78c784c4 | 109 | |
2e404f63 NA |
110 | /* But use as source device on which LAST ARRIVED |
111 | * segment was received. And do not use fq->dev | |
112 | * pointer directly, device might already disappeared. | |
78c784c4 | 113 | */ |
05c0b86b ED |
114 | head->dev = dev; |
115 | skb_get(head); | |
116 | spin_unlock(&fq->q.lock); | |
117 | ||
118 | icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0); | |
119 | kfree_skb(head); | |
120 | goto out_rcu_unlock; | |
121 | ||
1da177e4 | 122 | out: |
5ab11c98 | 123 | spin_unlock(&fq->q.lock); |
05c0b86b ED |
124 | out_rcu_unlock: |
125 | rcu_read_unlock(); | |
093ba729 | 126 | inet_frag_put(&fq->q); |
b836c99f AW |
127 | } |
128 | EXPORT_SYMBOL(ip6_expire_frag_queue); | |
129 | ||
78802011 | 130 | static void ip6_frag_expire(struct timer_list *t) |
b836c99f | 131 | { |
78802011 | 132 | struct inet_frag_queue *frag = from_timer(frag, t, timer); |
b836c99f AW |
133 | struct frag_queue *fq; |
134 | struct net *net; | |
135 | ||
78802011 | 136 | fq = container_of(frag, struct frag_queue, q); |
b836c99f AW |
137 | net = container_of(fq->q.net, struct net, ipv6.frags); |
138 | ||
093ba729 | 139 | ip6_expire_frag_queue(net, fq); |
1da177e4 LT |
140 | } |
141 | ||
fc08c258 | 142 | static struct frag_queue * |
648700f7 | 143 | fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif) |
1da177e4 | 144 | { |
648700f7 ED |
145 | struct frag_v6_compare_key key = { |
146 | .id = id, | |
147 | .saddr = hdr->saddr, | |
148 | .daddr = hdr->daddr, | |
149 | .user = IP6_DEFRAG_LOCAL_DELIVER, | |
150 | .iif = iif, | |
151 | }; | |
c6fda282 | 152 | struct inet_frag_queue *q; |
9a375803 | 153 | |
648700f7 ED |
154 | if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST | |
155 | IPV6_ADDR_LINKLOCAL))) | |
156 | key.iif = 0; | |
1da177e4 | 157 | |
648700f7 | 158 | q = inet_frag_find(&net->ipv6.frags, &key); |
2d44ed22 | 159 | if (!q) |
9546377c | 160 | return NULL; |
2d44ed22 | 161 | |
c6fda282 | 162 | return container_of(q, struct frag_queue, q); |
1da177e4 LT |
163 | } |
164 | ||
f61944ef | 165 | static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, |
1da177e4 LT |
166 | struct frag_hdr *fhdr, int nhoff) |
167 | { | |
168 | struct sk_buff *prev, *next; | |
f61944ef | 169 | struct net_device *dev; |
dbd1759e | 170 | int offset, end, fragsize; |
adf30907 | 171 | struct net *net = dev_net(skb_dst(skb)->dev); |
eec2e618 | 172 | u8 ecn; |
1da177e4 | 173 | |
06aa8b8a | 174 | if (fq->q.flags & INET_FRAG_COMPLETE) |
1da177e4 LT |
175 | goto err; |
176 | ||
177 | offset = ntohs(fhdr->frag_off) & ~0x7; | |
0660e03f ACM |
178 | end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - |
179 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); | |
1da177e4 LT |
180 | |
181 | if ((unsigned int)end > IPV6_MAXPLEN) { | |
1d015503 ED |
182 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
183 | IPSTATS_MIB_INHDRERRORS); | |
d56f90a7 ACM |
184 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
185 | ((u8 *)&fhdr->frag_off - | |
186 | skb_network_header(skb))); | |
f61944ef | 187 | return -1; |
1da177e4 LT |
188 | } |
189 | ||
eec2e618 HFS |
190 | ecn = ip6_frag_ecn(ipv6_hdr(skb)); |
191 | ||
d56f90a7 ACM |
192 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
193 | const unsigned char *nh = skb_network_header(skb); | |
1ab1457c | 194 | skb->csum = csum_sub(skb->csum, |
d56f90a7 ACM |
195 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
196 | 0)); | |
197 | } | |
1da177e4 LT |
198 | |
199 | /* Is this the final fragment? */ | |
200 | if (!(fhdr->frag_off & htons(IP6_MF))) { | |
201 | /* If we already have some bits beyond end | |
202 | * or have different end, the segment is corrupted. | |
203 | */ | |
5ab11c98 | 204 | if (end < fq->q.len || |
06aa8b8a | 205 | ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) |
1da177e4 | 206 | goto err; |
06aa8b8a | 207 | fq->q.flags |= INET_FRAG_LAST_IN; |
5ab11c98 | 208 | fq->q.len = end; |
1da177e4 LT |
209 | } else { |
210 | /* Check if the fragment is rounded to 8 bytes. | |
211 | * Required by the RFC. | |
212 | */ | |
213 | if (end & 0x7) { | |
214 | /* RFC2460 says always send parameter problem in | |
215 | * this case. -DaveM | |
216 | */ | |
1d015503 ED |
217 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
218 | IPSTATS_MIB_INHDRERRORS); | |
1ab1457c | 219 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
1da177e4 | 220 | offsetof(struct ipv6hdr, payload_len)); |
f61944ef | 221 | return -1; |
1da177e4 | 222 | } |
5ab11c98 | 223 | if (end > fq->q.len) { |
1da177e4 | 224 | /* Some bits beyond end -> corruption. */ |
06aa8b8a | 225 | if (fq->q.flags & INET_FRAG_LAST_IN) |
1da177e4 | 226 | goto err; |
5ab11c98 | 227 | fq->q.len = end; |
1da177e4 LT |
228 | } |
229 | } | |
230 | ||
231 | if (end == offset) | |
232 | goto err; | |
233 | ||
234 | /* Point into the IP datagram 'data' part. */ | |
235 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) | |
236 | goto err; | |
1ab1457c | 237 | |
42ca89c1 SH |
238 | if (pskb_trim_rcsum(skb, end - offset)) |
239 | goto err; | |
1da177e4 LT |
240 | |
241 | /* Find out which fragments are in front and at the back of us | |
242 | * in the chain of fragments so far. We must know where to put | |
243 | * this fragment, right? | |
244 | */ | |
d6bebca9 | 245 | prev = fq->q.fragments_tail; |
219badfa | 246 | if (!prev || prev->ip_defrag_offset < offset) { |
d6bebca9 CG |
247 | next = NULL; |
248 | goto found; | |
249 | } | |
1da177e4 | 250 | prev = NULL; |
67ba4152 | 251 | for (next = fq->q.fragments; next != NULL; next = next->next) { |
219badfa | 252 | if (next->ip_defrag_offset >= offset) |
1da177e4 LT |
253 | break; /* bingo! */ |
254 | prev = next; | |
255 | } | |
256 | ||
d6bebca9 | 257 | found: |
5de658f8 ED |
258 | /* RFC5722, Section 4, amended by Errata ID : 3089 |
259 | * When reassembling an IPv6 datagram, if | |
70789d70 ND |
260 | * one or more its constituent fragments is determined to be an |
261 | * overlapping fragment, the entire datagram (and any constituent | |
5de658f8 | 262 | * fragments) MUST be silently discarded. |
1da177e4 | 263 | */ |
1da177e4 | 264 | |
70789d70 ND |
265 | /* Check for overlap with preceding fragment. */ |
266 | if (prev && | |
219badfa | 267 | (prev->ip_defrag_offset + prev->len) > offset) |
70789d70 | 268 | goto discard_fq; |
1da177e4 | 269 | |
70789d70 | 270 | /* Look for overlap with succeeding segment. */ |
219badfa | 271 | if (next && next->ip_defrag_offset < end) |
70789d70 | 272 | goto discard_fq; |
1da177e4 | 273 | |
219badfa ED |
274 | /* Note : skb->ip_defrag_offset and skb->dev share the same location */ |
275 | dev = skb->dev; | |
276 | if (dev) | |
277 | fq->iif = dev->ifindex; | |
278 | /* Makes sure compiler wont do silly aliasing games */ | |
279 | barrier(); | |
280 | skb->ip_defrag_offset = offset; | |
1da177e4 LT |
281 | |
282 | /* Insert this fragment in the chain of fragments. */ | |
283 | skb->next = next; | |
d6bebca9 CG |
284 | if (!next) |
285 | fq->q.fragments_tail = skb; | |
1da177e4 LT |
286 | if (prev) |
287 | prev->next = skb; | |
288 | else | |
5ab11c98 | 289 | fq->q.fragments = skb; |
1da177e4 | 290 | |
5ab11c98 PE |
291 | fq->q.stamp = skb->tstamp; |
292 | fq->q.meat += skb->len; | |
eec2e618 | 293 | fq->ecn |= ecn; |
0e60d245 | 294 | add_frag_mem_limit(fq->q.net, skb->truesize); |
1da177e4 | 295 | |
dbd1759e WB |
296 | fragsize = -skb_network_offset(skb) + skb->len; |
297 | if (fragsize > fq->q.max_size) | |
298 | fq->q.max_size = fragsize; | |
299 | ||
1da177e4 LT |
300 | /* The first fragment. |
301 | * nhoffset is obtained from the first fragment, of course. | |
302 | */ | |
303 | if (offset == 0) { | |
304 | fq->nhoffset = nhoff; | |
06aa8b8a | 305 | fq->q.flags |= INET_FRAG_FIRST_IN; |
1da177e4 | 306 | } |
f61944ef | 307 | |
06aa8b8a | 308 | if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
97599dc7 ED |
309 | fq->q.meat == fq->q.len) { |
310 | int res; | |
311 | unsigned long orefdst = skb->_skb_refdst; | |
312 | ||
313 | skb->_skb_refdst = 0UL; | |
314 | res = ip6_frag_reasm(fq, prev, dev); | |
315 | skb->_skb_refdst = orefdst; | |
316 | return res; | |
317 | } | |
f61944ef | 318 | |
97599dc7 | 319 | skb_dst_drop(skb); |
f61944ef | 320 | return -1; |
1da177e4 | 321 | |
70789d70 | 322 | discard_fq: |
093ba729 | 323 | inet_frag_kill(&fq->q); |
1da177e4 | 324 | err: |
1d015503 ED |
325 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
326 | IPSTATS_MIB_REASMFAILS); | |
1da177e4 | 327 | kfree_skb(skb); |
f61944ef | 328 | return -1; |
1da177e4 LT |
329 | } |
330 | ||
331 | /* | |
332 | * Check if this packet is complete. | |
333 | * Returns NULL on failure by any reason, and pointer | |
334 | * to current nexthdr field in reassembled frame. | |
335 | * | |
336 | * It is called with locked fq, and caller must check that | |
337 | * queue is eligible for reassembly i.e. it is not COMPLETE, | |
338 | * the last and the first frames arrived and all the bits are here. | |
339 | */ | |
f61944ef | 340 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
1da177e4 LT |
341 | struct net_device *dev) |
342 | { | |
2bad35b7 | 343 | struct net *net = container_of(fq->q.net, struct net, ipv6.frags); |
5ab11c98 | 344 | struct sk_buff *fp, *head = fq->q.fragments; |
1da177e4 LT |
345 | int payload_len; |
346 | unsigned int nhoff; | |
ec16439e | 347 | int sum_truesize; |
eec2e618 | 348 | u8 ecn; |
1da177e4 | 349 | |
093ba729 | 350 | inet_frag_kill(&fq->q); |
1da177e4 | 351 | |
eec2e618 HFS |
352 | ecn = ip_frag_ecn_table[fq->ecn]; |
353 | if (unlikely(ecn == 0xff)) | |
354 | goto out_fail; | |
355 | ||
f61944ef HX |
356 | /* Make the one we just received the head. */ |
357 | if (prev) { | |
358 | head = prev->next; | |
359 | fp = skb_clone(head, GFP_ATOMIC); | |
360 | ||
361 | if (!fp) | |
362 | goto out_oom; | |
363 | ||
364 | fp->next = head->next; | |
d6bebca9 CG |
365 | if (!fp->next) |
366 | fq->q.fragments_tail = fp; | |
f61944ef HX |
367 | prev->next = fp; |
368 | ||
5ab11c98 PE |
369 | skb_morph(head, fq->q.fragments); |
370 | head->next = fq->q.fragments->next; | |
f61944ef | 371 | |
808db80a | 372 | consume_skb(fq->q.fragments); |
5ab11c98 | 373 | fq->q.fragments = head; |
f61944ef HX |
374 | } |
375 | ||
547b792c | 376 | WARN_ON(head == NULL); |
219badfa | 377 | WARN_ON(head->ip_defrag_offset != 0); |
1da177e4 LT |
378 | |
379 | /* Unfragmented part is taken from the first segment. */ | |
d56f90a7 | 380 | payload_len = ((head->data - skb_network_header(head)) - |
5ab11c98 | 381 | sizeof(struct ipv6hdr) + fq->q.len - |
d56f90a7 | 382 | sizeof(struct frag_hdr)); |
1da177e4 LT |
383 | if (payload_len > IPV6_MAXPLEN) |
384 | goto out_oversize; | |
385 | ||
386 | /* Head of list must not be cloned. */ | |
14bbd6a5 | 387 | if (skb_unclone(head, GFP_ATOMIC)) |
1da177e4 LT |
388 | goto out_oom; |
389 | ||
390 | /* If the first fragment is fragmented itself, we split | |
391 | * it to two chunks: the first with data and paged part | |
392 | * and the second, holding only fragments. */ | |
21dc3301 | 393 | if (skb_has_frag_list(head)) { |
1da177e4 LT |
394 | struct sk_buff *clone; |
395 | int i, plen = 0; | |
396 | ||
e5d08d71 | 397 | clone = alloc_skb(0, GFP_ATOMIC); |
63159f29 | 398 | if (!clone) |
1da177e4 LT |
399 | goto out_oom; |
400 | clone->next = head->next; | |
401 | head->next = clone; | |
402 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; | |
4d9092bb | 403 | skb_frag_list_init(head); |
9e903e08 ED |
404 | for (i = 0; i < skb_shinfo(head)->nr_frags; i++) |
405 | plen += skb_frag_size(&skb_shinfo(head)->frags[i]); | |
1da177e4 LT |
406 | clone->len = clone->data_len = head->data_len - plen; |
407 | head->data_len -= clone->len; | |
408 | head->len -= clone->len; | |
409 | clone->csum = 0; | |
410 | clone->ip_summed = head->ip_summed; | |
0e60d245 | 411 | add_frag_mem_limit(fq->q.net, clone->truesize); |
1da177e4 LT |
412 | } |
413 | ||
414 | /* We have to remove fragment header from datagram and to relocate | |
415 | * header in order to calculate ICV correctly. */ | |
416 | nhoff = fq->nhoffset; | |
b0e380b1 | 417 | skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; |
1ab1457c | 418 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
1da177e4 | 419 | (head->data - head->head) - sizeof(struct frag_hdr)); |
b678aa57 JD |
420 | if (skb_mac_header_was_set(head)) |
421 | head->mac_header += sizeof(struct frag_hdr); | |
b0e380b1 | 422 | head->network_header += sizeof(struct frag_hdr); |
1da177e4 | 423 | |
badff6d0 | 424 | skb_reset_transport_header(head); |
d56f90a7 | 425 | skb_push(head, head->data - skb_network_header(head)); |
1da177e4 | 426 | |
ec16439e ED |
427 | sum_truesize = head->truesize; |
428 | for (fp = head->next; fp;) { | |
429 | bool headstolen; | |
430 | int delta; | |
431 | struct sk_buff *next = fp->next; | |
432 | ||
433 | sum_truesize += fp->truesize; | |
1da177e4 LT |
434 | if (head->ip_summed != fp->ip_summed) |
435 | head->ip_summed = CHECKSUM_NONE; | |
84fa7933 | 436 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
1da177e4 | 437 | head->csum = csum_add(head->csum, fp->csum); |
ec16439e ED |
438 | |
439 | if (skb_try_coalesce(head, fp, &headstolen, &delta)) { | |
440 | kfree_skb_partial(fp, headstolen); | |
441 | } else { | |
442 | if (!skb_shinfo(head)->frag_list) | |
443 | skb_shinfo(head)->frag_list = fp; | |
444 | head->data_len += fp->len; | |
445 | head->len += fp->len; | |
446 | head->truesize += fp->truesize; | |
447 | } | |
448 | fp = next; | |
1da177e4 | 449 | } |
0e60d245 | 450 | sub_frag_mem_limit(fq->q.net, sum_truesize); |
1da177e4 LT |
451 | |
452 | head->next = NULL; | |
453 | head->dev = dev; | |
5ab11c98 | 454 | head->tstamp = fq->q.stamp; |
0660e03f | 455 | ipv6_hdr(head)->payload_len = htons(payload_len); |
eec2e618 | 456 | ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn); |
951dbc8a | 457 | IP6CB(head)->nhoff = nhoff; |
f46078cf | 458 | IP6CB(head)->flags |= IP6SKB_FRAGMENTED; |
dbd1759e | 459 | IP6CB(head)->frag_max_size = fq->q.max_size; |
1da177e4 | 460 | |
1da177e4 | 461 | /* Yes, and fold redundant checksum back. 8) */ |
6b83d28a DB |
462 | skb_postpush_rcsum(head, skb_network_header(head), |
463 | skb_network_header_len(head)); | |
1da177e4 | 464 | |
a11d206d | 465 | rcu_read_lock(); |
1d015503 | 466 | __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS); |
a11d206d | 467 | rcu_read_unlock(); |
5ab11c98 | 468 | fq->q.fragments = NULL; |
d6bebca9 | 469 | fq->q.fragments_tail = NULL; |
1da177e4 LT |
470 | return 1; |
471 | ||
472 | out_oversize: | |
e87cc472 | 473 | net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); |
1da177e4 LT |
474 | goto out_fail; |
475 | out_oom: | |
e87cc472 | 476 | net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); |
1da177e4 | 477 | out_fail: |
a11d206d | 478 | rcu_read_lock(); |
1d015503 | 479 | __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); |
a11d206d | 480 | rcu_read_unlock(); |
1da177e4 LT |
481 | return -1; |
482 | } | |
483 | ||
e5bbef20 | 484 | static int ipv6_frag_rcv(struct sk_buff *skb) |
1da177e4 | 485 | { |
1da177e4 LT |
486 | struct frag_hdr *fhdr; |
487 | struct frag_queue *fq; | |
b71d1d42 | 488 | const struct ipv6hdr *hdr = ipv6_hdr(skb); |
adf30907 | 489 | struct net *net = dev_net(skb_dst(skb)->dev); |
648700f7 | 490 | int iif; |
1da177e4 | 491 | |
f46078cf HFS |
492 | if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) |
493 | goto fail_hdr; | |
494 | ||
1d015503 | 495 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); |
1da177e4 LT |
496 | |
497 | /* Jumbo payload inhibits frag. header */ | |
67ba4152 | 498 | if (hdr->payload_len == 0) |
98b3377c DL |
499 | goto fail_hdr; |
500 | ||
ea2ae17d | 501 | if (!pskb_may_pull(skb, (skb_transport_offset(skb) + |
98b3377c DL |
502 | sizeof(struct frag_hdr)))) |
503 | goto fail_hdr; | |
1da177e4 | 504 | |
0660e03f | 505 | hdr = ipv6_hdr(skb); |
9c70220b | 506 | fhdr = (struct frag_hdr *)skb_transport_header(skb); |
1da177e4 LT |
507 | |
508 | if (!(fhdr->frag_off & htons(0xFFF9))) { | |
509 | /* It is not a fragmented frame */ | |
b0e380b1 | 510 | skb->transport_header += sizeof(struct frag_hdr); |
1d015503 ED |
511 | __IP6_INC_STATS(net, |
512 | ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); | |
1da177e4 | 513 | |
d56f90a7 | 514 | IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); |
f46078cf | 515 | IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; |
1da177e4 LT |
516 | return 1; |
517 | } | |
518 | ||
648700f7 ED |
519 | iif = skb->dev ? skb->dev->ifindex : 0; |
520 | fq = fq_find(net, fhdr->identification, hdr, iif); | |
53b24b8f | 521 | if (fq) { |
f61944ef | 522 | int ret; |
1da177e4 | 523 | |
5ab11c98 | 524 | spin_lock(&fq->q.lock); |
1da177e4 | 525 | |
648700f7 | 526 | fq->iif = iif; |
f61944ef | 527 | ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff); |
1da177e4 | 528 | |
5ab11c98 | 529 | spin_unlock(&fq->q.lock); |
093ba729 | 530 | inet_frag_put(&fq->q); |
1da177e4 LT |
531 | return ret; |
532 | } | |
533 | ||
1d015503 | 534 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); |
1da177e4 LT |
535 | kfree_skb(skb); |
536 | return -1; | |
98b3377c DL |
537 | |
538 | fail_hdr: | |
1d015503 ED |
539 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
540 | IPSTATS_MIB_INHDRERRORS); | |
98b3377c DL |
541 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); |
542 | return -1; | |
1da177e4 LT |
543 | } |
544 | ||
cc24beca | 545 | static const struct inet6_protocol frag_protocol = { |
1da177e4 LT |
546 | .handler = ipv6_frag_rcv, |
547 | .flags = INET6_PROTO_NOPOLICY, | |
548 | }; | |
549 | ||
8d8354d2 | 550 | #ifdef CONFIG_SYSCTL |
1bab4c75 | 551 | |
0a64b4b8 | 552 | static struct ctl_table ip6_frags_ns_ctl_table[] = { |
8d8354d2 | 553 | { |
8d8354d2 | 554 | .procname = "ip6frag_high_thresh", |
e31e0bdc | 555 | .data = &init_net.ipv6.frags.high_thresh, |
3e67f106 | 556 | .maxlen = sizeof(unsigned long), |
8d8354d2 | 557 | .mode = 0644, |
3e67f106 | 558 | .proc_handler = proc_doulongvec_minmax, |
1bab4c75 | 559 | .extra1 = &init_net.ipv6.frags.low_thresh |
8d8354d2 PE |
560 | }, |
561 | { | |
8d8354d2 | 562 | .procname = "ip6frag_low_thresh", |
e31e0bdc | 563 | .data = &init_net.ipv6.frags.low_thresh, |
3e67f106 | 564 | .maxlen = sizeof(unsigned long), |
8d8354d2 | 565 | .mode = 0644, |
6e00f7dd | 566 | .proc_handler = proc_doulongvec_minmax, |
1bab4c75 | 567 | .extra2 = &init_net.ipv6.frags.high_thresh |
8d8354d2 PE |
568 | }, |
569 | { | |
8d8354d2 | 570 | .procname = "ip6frag_time", |
b2fd5321 | 571 | .data = &init_net.ipv6.frags.timeout, |
8d8354d2 PE |
572 | .maxlen = sizeof(int), |
573 | .mode = 0644, | |
6d9f239a | 574 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 | 575 | }, |
7d291ebb PE |
576 | { } |
577 | }; | |
578 | ||
e3a57d18 FW |
579 | /* secret interval has been deprecated */ |
580 | static int ip6_frags_secret_interval_unused; | |
7d291ebb | 581 | static struct ctl_table ip6_frags_ctl_table[] = { |
8d8354d2 | 582 | { |
8d8354d2 | 583 | .procname = "ip6frag_secret_interval", |
e3a57d18 | 584 | .data = &ip6_frags_secret_interval_unused, |
8d8354d2 PE |
585 | .maxlen = sizeof(int), |
586 | .mode = 0644, | |
6d9f239a | 587 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 PE |
588 | }, |
589 | { } | |
590 | }; | |
591 | ||
2c8c1e72 | 592 | static int __net_init ip6_frags_ns_sysctl_register(struct net *net) |
8d8354d2 | 593 | { |
e4a2d5c2 | 594 | struct ctl_table *table; |
8d8354d2 PE |
595 | struct ctl_table_header *hdr; |
596 | ||
0a64b4b8 | 597 | table = ip6_frags_ns_ctl_table; |
09ad9bc7 | 598 | if (!net_eq(net, &init_net)) { |
0a64b4b8 | 599 | table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); |
63159f29 | 600 | if (!table) |
e4a2d5c2 PE |
601 | goto err_alloc; |
602 | ||
e31e0bdc | 603 | table[0].data = &net->ipv6.frags.high_thresh; |
1bab4c75 NA |
604 | table[0].extra1 = &net->ipv6.frags.low_thresh; |
605 | table[0].extra2 = &init_net.ipv6.frags.high_thresh; | |
e31e0bdc | 606 | table[1].data = &net->ipv6.frags.low_thresh; |
1bab4c75 | 607 | table[1].extra2 = &net->ipv6.frags.high_thresh; |
b2fd5321 | 608 | table[2].data = &net->ipv6.frags.timeout; |
e4a2d5c2 PE |
609 | } |
610 | ||
ec8f23ce | 611 | hdr = register_net_sysctl(net, "net/ipv6", table); |
63159f29 | 612 | if (!hdr) |
e4a2d5c2 PE |
613 | goto err_reg; |
614 | ||
615 | net->ipv6.sysctl.frags_hdr = hdr; | |
616 | return 0; | |
617 | ||
618 | err_reg: | |
09ad9bc7 | 619 | if (!net_eq(net, &init_net)) |
e4a2d5c2 PE |
620 | kfree(table); |
621 | err_alloc: | |
622 | return -ENOMEM; | |
623 | } | |
624 | ||
2c8c1e72 | 625 | static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) |
e4a2d5c2 PE |
626 | { |
627 | struct ctl_table *table; | |
628 | ||
629 | table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; | |
630 | unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); | |
3705e11a YH |
631 | if (!net_eq(net, &init_net)) |
632 | kfree(table); | |
8d8354d2 | 633 | } |
7d291ebb PE |
634 | |
635 | static struct ctl_table_header *ip6_ctl_header; | |
636 | ||
637 | static int ip6_frags_sysctl_register(void) | |
638 | { | |
43444757 | 639 | ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", |
7d291ebb PE |
640 | ip6_frags_ctl_table); |
641 | return ip6_ctl_header == NULL ? -ENOMEM : 0; | |
642 | } | |
643 | ||
644 | static void ip6_frags_sysctl_unregister(void) | |
645 | { | |
646 | unregister_net_sysctl_table(ip6_ctl_header); | |
647 | } | |
8d8354d2 | 648 | #else |
fc08c258 | 649 | static int ip6_frags_ns_sysctl_register(struct net *net) |
e71e0349 | 650 | { |
8d8354d2 PE |
651 | return 0; |
652 | } | |
e4a2d5c2 | 653 | |
fc08c258 | 654 | static void ip6_frags_ns_sysctl_unregister(struct net *net) |
e4a2d5c2 PE |
655 | { |
656 | } | |
7d291ebb | 657 | |
fc08c258 | 658 | static int ip6_frags_sysctl_register(void) |
7d291ebb PE |
659 | { |
660 | return 0; | |
661 | } | |
662 | ||
fc08c258 | 663 | static void ip6_frags_sysctl_unregister(void) |
7d291ebb PE |
664 | { |
665 | } | |
8d8354d2 | 666 | #endif |
7d460db9 | 667 | |
2c8c1e72 | 668 | static int __net_init ipv6_frags_init_net(struct net *net) |
8d8354d2 | 669 | { |
787bea77 ED |
670 | int res; |
671 | ||
7c070aa9 SW |
672 | net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH; |
673 | net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH; | |
b2fd5321 | 674 | net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT; |
093ba729 | 675 | net->ipv6.frags.f = &ip6_frags; |
8d8354d2 | 676 | |
787bea77 ED |
677 | res = inet_frags_init_net(&net->ipv6.frags); |
678 | if (res < 0) | |
679 | return res; | |
5a63643e | 680 | |
787bea77 ED |
681 | res = ip6_frags_ns_sysctl_register(net); |
682 | if (res < 0) | |
093ba729 | 683 | inet_frags_exit_net(&net->ipv6.frags); |
787bea77 | 684 | return res; |
e71e0349 DL |
685 | } |
686 | ||
2c8c1e72 | 687 | static void __net_exit ipv6_frags_exit_net(struct net *net) |
81566e83 | 688 | { |
0a64b4b8 | 689 | ip6_frags_ns_sysctl_unregister(net); |
093ba729 | 690 | inet_frags_exit_net(&net->ipv6.frags); |
81566e83 PE |
691 | } |
692 | ||
693 | static struct pernet_operations ip6_frags_ops = { | |
694 | .init = ipv6_frags_init_net, | |
695 | .exit = ipv6_frags_exit_net, | |
696 | }; | |
697 | ||
648700f7 ED |
698 | static u32 ip6_key_hashfn(const void *data, u32 len, u32 seed) |
699 | { | |
700 | return jhash2(data, | |
701 | sizeof(struct frag_v6_compare_key) / sizeof(u32), seed); | |
702 | } | |
703 | ||
704 | static u32 ip6_obj_hashfn(const void *data, u32 len, u32 seed) | |
705 | { | |
706 | const struct inet_frag_queue *fq = data; | |
707 | ||
708 | return jhash2((const u32 *)&fq->key.v6, | |
709 | sizeof(struct frag_v6_compare_key) / sizeof(u32), seed); | |
710 | } | |
711 | ||
712 | static int ip6_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) | |
713 | { | |
714 | const struct frag_v6_compare_key *key = arg->key; | |
715 | const struct inet_frag_queue *fq = ptr; | |
716 | ||
717 | return !!memcmp(&fq->key, key, sizeof(*key)); | |
718 | } | |
719 | ||
720 | const struct rhashtable_params ip6_rhash_params = { | |
721 | .head_offset = offsetof(struct inet_frag_queue, node), | |
722 | .hashfn = ip6_key_hashfn, | |
723 | .obj_hashfn = ip6_obj_hashfn, | |
724 | .obj_cmpfn = ip6_obj_cmpfn, | |
725 | .automatic_shrinking = true, | |
726 | }; | |
727 | EXPORT_SYMBOL(ip6_rhash_params); | |
728 | ||
853cbbaa | 729 | int __init ipv6_frag_init(void) |
1da177e4 | 730 | { |
853cbbaa | 731 | int ret; |
1da177e4 | 732 | |
5b975bab ED |
733 | ip6_frags.constructor = ip6_frag_init; |
734 | ip6_frags.destructor = NULL; | |
735 | ip6_frags.qsize = sizeof(struct frag_queue); | |
5b975bab ED |
736 | ip6_frags.frag_expire = ip6_frag_expire; |
737 | ip6_frags.frags_cache_name = ip6_frag_cache_name; | |
648700f7 | 738 | ip6_frags.rhash_params = ip6_rhash_params; |
5b975bab | 739 | ret = inet_frags_init(&ip6_frags); |
853cbbaa DL |
740 | if (ret) |
741 | goto out; | |
e71e0349 | 742 | |
5b975bab ED |
743 | ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
744 | if (ret) | |
745 | goto err_protocol; | |
746 | ||
7d291ebb PE |
747 | ret = ip6_frags_sysctl_register(); |
748 | if (ret) | |
749 | goto err_sysctl; | |
750 | ||
0002c630 PE |
751 | ret = register_pernet_subsys(&ip6_frags_ops); |
752 | if (ret) | |
753 | goto err_pernet; | |
8d8354d2 | 754 | |
853cbbaa DL |
755 | out: |
756 | return ret; | |
0002c630 PE |
757 | |
758 | err_pernet: | |
7d291ebb PE |
759 | ip6_frags_sysctl_unregister(); |
760 | err_sysctl: | |
0002c630 | 761 | inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
5b975bab ED |
762 | err_protocol: |
763 | inet_frags_fini(&ip6_frags); | |
0002c630 | 764 | goto out; |
853cbbaa DL |
765 | } |
766 | ||
767 | void ipv6_frag_exit(void) | |
768 | { | |
769 | inet_frags_fini(&ip6_frags); | |
7d291ebb | 770 | ip6_frags_sysctl_unregister(); |
81566e83 | 771 | unregister_pernet_subsys(&ip6_frags_ops); |
853cbbaa | 772 | inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
1da177e4 | 773 | } |