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