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