inet: frag: move evictor calls into frag_find function
[linux-2.6-block.git] / net / ipv4 / ip_fragment.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP fragmentation functionality.
e905a9ed 7 *
1da177e4 8 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
113aa838 9 * Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
10 *
11 * Fixes:
12 * Alan Cox : Split from ip.c , see ip_input.c for history.
13 * David S. Miller : Begin massive cleanup...
14 * Andi Kleen : Add sysctls.
15 * xxxx : Overlapfrag bug.
16 * Ultima : ip_expire() kernel panic.
17 * Bill Hawes : Frag accounting and evictor fixes.
18 * John McDonald : 0 length frag bug.
19 * Alexey Kuznetsov: SMP races, threading, cleanup.
20 * Patrick McHardy : LRU queue of frag heads for evictor.
21 */
22
afd46503
JP
23#define pr_fmt(fmt) "IPv4: " fmt
24
89cee8b1 25#include <linux/compiler.h>
1da177e4
LT
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/jiffies.h>
30#include <linux/skbuff.h>
31#include <linux/list.h>
32#include <linux/ip.h>
33#include <linux/icmp.h>
34#include <linux/netdevice.h>
35#include <linux/jhash.h>
36#include <linux/random.h>
5a0e3ad6 37#include <linux/slab.h>
e9017b55
SW
38#include <net/route.h>
39#include <net/dst.h>
1da177e4
LT
40#include <net/sock.h>
41#include <net/ip.h>
42#include <net/icmp.h>
43#include <net/checksum.h>
89cee8b1 44#include <net/inetpeer.h>
5ab11c98 45#include <net/inet_frag.h>
1da177e4
LT
46#include <linux/tcp.h>
47#include <linux/udp.h>
48#include <linux/inet.h>
49#include <linux/netfilter_ipv4.h>
6623e3b2 50#include <net/inet_ecn.h>
1da177e4
LT
51
52/* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
53 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
54 * as well. Or notify me, at least. --ANK
55 */
56
8d8354d2 57static int sysctl_ipfrag_max_dist __read_mostly = 64;
89cee8b1 58
1da177e4
LT
59struct ipfrag_skb_cb
60{
61 struct inet_skb_parm h;
62 int offset;
63};
64
fd3f8c4c 65#define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
1da177e4
LT
66
67/* Describe an entry in the "incomplete datagrams" queue. */
68struct ipq {
5ab11c98
PE
69 struct inet_frag_queue q;
70
1da177e4 71 u32 user;
18277770
AV
72 __be32 saddr;
73 __be32 daddr;
74 __be16 id;
1da177e4 75 u8 protocol;
6623e3b2 76 u8 ecn; /* RFC3168 support */
89cee8b1
HX
77 int iif;
78 unsigned int rid;
79 struct inet_peer *peer;
1da177e4
LT
80};
81
6623e3b2
ED
82static inline u8 ip4_frag_ecn(u8 tos)
83{
5173cc05 84 return 1 << (tos & INET_ECN_MASK);
6623e3b2
ED
85}
86
7eb95156 87static struct inet_frags ip4_frags;
1da177e4 88
e5a2bb84 89int ip_frag_nqueues(struct net *net)
7eb95156 90{
e5a2bb84 91 return net->ipv4.frags.nqueues;
7eb95156 92}
1da177e4 93
6ddc0822 94int ip_frag_mem(struct net *net)
7eb95156 95{
d433673e 96 return sum_frag_mem_limit(&net->ipv4.frags);
7eb95156 97}
1da177e4 98
1706d587
HX
99static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
100 struct net_device *dev);
101
c6fda282
PE
102struct ip4_create_arg {
103 struct iphdr *iph;
104 u32 user;
105};
106
18277770 107static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
1da177e4 108{
e7b519ba 109 net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
18277770
AV
110 return jhash_3words((__force u32)id << 16 | prot,
111 (__force u32)saddr, (__force u32)daddr,
fb3cfe6e 112 ip4_frags.rnd);
1da177e4
LT
113}
114
36c77782 115static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
1da177e4 116{
36c77782 117 const struct ipq *ipq;
1da177e4 118
321a3a99
PE
119 ipq = container_of(q, struct ipq, q);
120 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
1da177e4
LT
121}
122
36c77782 123static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
abd6523d 124{
36c77782
FW
125 const struct ipq *qp;
126 const struct ip4_create_arg *arg = a;
abd6523d
PE
127
128 qp = container_of(q, struct ipq, q);
a02cec21 129 return qp->id == arg->iph->id &&
cbc264ca
ED
130 qp->saddr == arg->iph->saddr &&
131 qp->daddr == arg->iph->daddr &&
132 qp->protocol == arg->iph->protocol &&
133 qp->user == arg->user;
abd6523d
PE
134}
135
36c77782 136static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
c6fda282
PE
137{
138 struct ipq *qp = container_of(q, struct ipq, q);
54db0cc2
G
139 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
140 frags);
141 struct net *net = container_of(ipv4, struct net, ipv4);
142
36c77782 143 const struct ip4_create_arg *arg = a;
c6fda282
PE
144
145 qp->protocol = arg->iph->protocol;
146 qp->id = arg->iph->id;
6623e3b2 147 qp->ecn = ip4_frag_ecn(arg->iph->tos);
c6fda282
PE
148 qp->saddr = arg->iph->saddr;
149 qp->daddr = arg->iph->daddr;
150 qp->user = arg->user;
151 qp->peer = sysctl_ipfrag_max_dist ?
c0efc887 152 inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL;
c6fda282
PE
153}
154
1e4b8287 155static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
1da177e4 156{
1e4b8287
PE
157 struct ipq *qp;
158
159 qp = container_of(q, struct ipq, q);
160 if (qp->peer)
161 inet_putpeer(qp->peer);
1da177e4
LT
162}
163
1da177e4
LT
164
165/* Destruction primitives. */
166
4b6cb5d8 167static __inline__ void ipq_put(struct ipq *ipq)
1da177e4 168{
762cc408 169 inet_frag_put(&ipq->q, &ip4_frags);
1da177e4
LT
170}
171
172/* Kill ipq entry. It is not destroyed immediately,
173 * because caller (and someone more) holds reference count.
174 */
175static void ipq_kill(struct ipq *ipq)
176{
277e650d 177 inet_frag_kill(&ipq->q, &ip4_frags);
1da177e4
LT
178}
179
1da177e4
LT
180/*
181 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
182 */
183static void ip_expire(unsigned long arg)
184{
e521db9d 185 struct ipq *qp;
84a3aa00 186 struct net *net;
e521db9d
PE
187
188 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
84a3aa00 189 net = container_of(qp->q.net, struct net, ipv4.frags);
1da177e4 190
5ab11c98 191 spin_lock(&qp->q.lock);
1da177e4 192
bc578a54 193 if (qp->q.last_in & INET_FRAG_COMPLETE)
1da177e4
LT
194 goto out;
195
196 ipq_kill(qp);
197
7c73a6fa
PE
198 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
199 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
1da177e4 200
bc578a54 201 if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
5ab11c98 202 struct sk_buff *head = qp->q.fragments;
64f3b9e2
ED
203 const struct iphdr *iph;
204 int err;
cb84663e 205
69df9d59
ED
206 rcu_read_lock();
207 head->dev = dev_get_by_index_rcu(net, qp->iif);
e9017b55
SW
208 if (!head->dev)
209 goto out_rcu_unlock;
210
97599dc7 211 /* skb has no dst, perform route lookup again */
64f3b9e2 212 iph = ip_hdr(head);
c6cffba4
DM
213 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
214 iph->tos, head->dev);
64f3b9e2
ED
215 if (err)
216 goto out_rcu_unlock;
217
e9017b55 218 /*
64f3b9e2
ED
219 * Only an end host needs to send an ICMP
220 * "Fragment Reassembly Timeout" message, per RFC792.
e9017b55 221 */
595fc71b 222 if (qp->user == IP_DEFRAG_AF_PACKET ||
7c3d5ab1
VA
223 ((qp->user >= IP_DEFRAG_CONNTRACK_IN) &&
224 (qp->user <= __IP_DEFRAG_CONNTRACK_IN_END) &&
225 (skb_rtable(head)->rt_type != RTN_LOCAL)))
64f3b9e2
ED
226 goto out_rcu_unlock;
227
e9017b55
SW
228
229 /* Send an ICMP "Fragment Reassembly Timeout" message. */
230 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
e9017b55 231out_rcu_unlock:
d1c9ae6d
PM
232 rcu_read_unlock();
233 }
1da177e4 234out:
5ab11c98 235 spin_unlock(&qp->q.lock);
4b6cb5d8 236 ipq_put(qp);
1da177e4
LT
237}
238
abd6523d
PE
239/* Find the correct entry in the "incomplete datagrams" queue for
240 * this IP datagram, and create new one, if nothing is found.
241 */
ac18e750 242static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
1da177e4 243{
c6fda282
PE
244 struct inet_frag_queue *q;
245 struct ip4_create_arg arg;
abd6523d 246 unsigned int hash;
1da177e4 247
c6fda282
PE
248 arg.iph = iph;
249 arg.user = user;
9a375803
PE
250
251 read_lock(&ip4_frags.lock);
abd6523d 252 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
1da177e4 253
ac18e750 254 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
5a3da1fe
HFS
255 if (IS_ERR_OR_NULL(q)) {
256 inet_frag_maybe_warn_overflow(q, pr_fmt());
257 return NULL;
258 }
c6fda282 259 return container_of(q, struct ipq, q);
1da177e4
LT
260}
261
89cee8b1
HX
262/* Is the fragment too far ahead to be part of ipq? */
263static inline int ip_frag_too_far(struct ipq *qp)
264{
265 struct inet_peer *peer = qp->peer;
266 unsigned int max = sysctl_ipfrag_max_dist;
267 unsigned int start, end;
268
269 int rc;
270
271 if (!peer || !max)
272 return 0;
273
274 start = qp->rid;
275 end = atomic_inc_return(&peer->rid);
276 qp->rid = end;
277
5ab11c98 278 rc = qp->q.fragments && (end - start) > max;
89cee8b1
HX
279
280 if (rc) {
7c73a6fa
PE
281 struct net *net;
282
283 net = container_of(qp->q.net, struct net, ipv4.frags);
284 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
89cee8b1
HX
285 }
286
287 return rc;
288}
289
290static int ip_frag_reinit(struct ipq *qp)
291{
292 struct sk_buff *fp;
d433673e 293 unsigned int sum_truesize = 0;
89cee8b1 294
b2fd5321 295 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
5ab11c98 296 atomic_inc(&qp->q.refcnt);
89cee8b1
HX
297 return -ETIMEDOUT;
298 }
299
5ab11c98 300 fp = qp->q.fragments;
89cee8b1
HX
301 do {
302 struct sk_buff *xp = fp->next;
d433673e
JDB
303
304 sum_truesize += fp->truesize;
305 kfree_skb(fp);
89cee8b1
HX
306 fp = xp;
307 } while (fp);
d433673e 308 sub_frag_mem_limit(&qp->q, sum_truesize);
89cee8b1 309
5ab11c98
PE
310 qp->q.last_in = 0;
311 qp->q.len = 0;
312 qp->q.meat = 0;
313 qp->q.fragments = NULL;
d6bebca9 314 qp->q.fragments_tail = NULL;
89cee8b1 315 qp->iif = 0;
6623e3b2 316 qp->ecn = 0;
89cee8b1
HX
317
318 return 0;
319}
320
1da177e4 321/* Add new segment to existing queue. */
1706d587 322static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
1da177e4
LT
323{
324 struct sk_buff *prev, *next;
1706d587 325 struct net_device *dev;
1da177e4
LT
326 int flags, offset;
327 int ihl, end;
1706d587 328 int err = -ENOENT;
6623e3b2 329 u8 ecn;
1da177e4 330
bc578a54 331 if (qp->q.last_in & INET_FRAG_COMPLETE)
1da177e4
LT
332 goto err;
333
89cee8b1 334 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
1706d587
HX
335 unlikely(ip_frag_too_far(qp)) &&
336 unlikely(err = ip_frag_reinit(qp))) {
89cee8b1
HX
337 ipq_kill(qp);
338 goto err;
339 }
340
6623e3b2 341 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
eddc9ec5 342 offset = ntohs(ip_hdr(skb)->frag_off);
1da177e4
LT
343 flags = offset & ~IP_OFFSET;
344 offset &= IP_OFFSET;
345 offset <<= 3; /* offset is in 8-byte chunks */
c9bdd4b5 346 ihl = ip_hdrlen(skb);
1da177e4
LT
347
348 /* Determine the position of this fragment. */
e905a9ed 349 end = offset + skb->len - ihl;
1706d587 350 err = -EINVAL;
1da177e4
LT
351
352 /* Is this the final fragment? */
353 if ((flags & IP_MF) == 0) {
354 /* If we already have some bits beyond end
42b2aa86 355 * or have different end, the segment is corrupted.
1da177e4 356 */
5ab11c98 357 if (end < qp->q.len ||
bc578a54 358 ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
1da177e4 359 goto err;
bc578a54 360 qp->q.last_in |= INET_FRAG_LAST_IN;
5ab11c98 361 qp->q.len = end;
1da177e4
LT
362 } else {
363 if (end&7) {
364 end &= ~7;
365 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
366 skb->ip_summed = CHECKSUM_NONE;
367 }
5ab11c98 368 if (end > qp->q.len) {
1da177e4 369 /* Some bits beyond end -> corruption. */
bc578a54 370 if (qp->q.last_in & INET_FRAG_LAST_IN)
1da177e4 371 goto err;
5ab11c98 372 qp->q.len = end;
1da177e4
LT
373 }
374 }
375 if (end == offset)
376 goto err;
377
1706d587 378 err = -ENOMEM;
1da177e4
LT
379 if (pskb_pull(skb, ihl) == NULL)
380 goto err;
1706d587
HX
381
382 err = pskb_trim_rcsum(skb, end - offset);
383 if (err)
1da177e4
LT
384 goto err;
385
386 /* Find out which fragments are in front and at the back of us
387 * in the chain of fragments so far. We must know where to put
388 * this fragment, right?
389 */
d6bebca9
CG
390 prev = qp->q.fragments_tail;
391 if (!prev || FRAG_CB(prev)->offset < offset) {
392 next = NULL;
393 goto found;
394 }
1da177e4 395 prev = NULL;
5ab11c98 396 for (next = qp->q.fragments; next != NULL; next = next->next) {
1da177e4
LT
397 if (FRAG_CB(next)->offset >= offset)
398 break; /* bingo! */
399 prev = next;
400 }
401
d6bebca9 402found:
1da177e4
LT
403 /* We found where to put this one. Check for overlap with
404 * preceding fragment, and, if needed, align things so that
405 * any overlaps are eliminated.
406 */
407 if (prev) {
408 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
409
410 if (i > 0) {
411 offset += i;
1706d587 412 err = -EINVAL;
1da177e4
LT
413 if (end <= offset)
414 goto err;
1706d587 415 err = -ENOMEM;
1da177e4
LT
416 if (!pskb_pull(skb, i))
417 goto err;
418 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
419 skb->ip_summed = CHECKSUM_NONE;
420 }
421 }
422
1706d587
HX
423 err = -ENOMEM;
424
1da177e4
LT
425 while (next && FRAG_CB(next)->offset < end) {
426 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
427
428 if (i < next->len) {
429 /* Eat head of the next overlapped fragment
430 * and leave the loop. The next ones cannot overlap.
431 */
432 if (!pskb_pull(next, i))
433 goto err;
434 FRAG_CB(next)->offset += i;
5ab11c98 435 qp->q.meat -= i;
1da177e4
LT
436 if (next->ip_summed != CHECKSUM_UNNECESSARY)
437 next->ip_summed = CHECKSUM_NONE;
438 break;
439 } else {
440 struct sk_buff *free_it = next;
441
47c6bf77 442 /* Old fragment is completely overridden with
1da177e4
LT
443 * new one drop it.
444 */
445 next = next->next;
446
447 if (prev)
448 prev->next = next;
449 else
5ab11c98 450 qp->q.fragments = next;
1da177e4 451
5ab11c98 452 qp->q.meat -= free_it->len;
d433673e
JDB
453 sub_frag_mem_limit(&qp->q, free_it->truesize);
454 kfree_skb(free_it);
1da177e4
LT
455 }
456 }
457
458 FRAG_CB(skb)->offset = offset;
459
460 /* Insert this fragment in the chain of fragments. */
461 skb->next = next;
d6bebca9
CG
462 if (!next)
463 qp->q.fragments_tail = skb;
1da177e4
LT
464 if (prev)
465 prev->next = skb;
466 else
5ab11c98 467 qp->q.fragments = skb;
1da177e4 468
1706d587
HX
469 dev = skb->dev;
470 if (dev) {
471 qp->iif = dev->ifindex;
472 skb->dev = NULL;
473 }
5ab11c98
PE
474 qp->q.stamp = skb->tstamp;
475 qp->q.meat += skb->len;
6623e3b2 476 qp->ecn |= ecn;
d433673e 477 add_frag_mem_limit(&qp->q, skb->truesize);
1da177e4 478 if (offset == 0)
bc578a54 479 qp->q.last_in |= INET_FRAG_FIRST_IN;
1da177e4 480
5f2d04f1
PM
481 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
482 skb->len + ihl > qp->q.max_size)
483 qp->q.max_size = skb->len + ihl;
484
bc578a54 485 if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
97599dc7
ED
486 qp->q.meat == qp->q.len) {
487 unsigned long orefdst = skb->_skb_refdst;
1706d587 488
97599dc7
ED
489 skb->_skb_refdst = 0UL;
490 err = ip_frag_reasm(qp, prev, dev);
491 skb->_skb_refdst = orefdst;
492 return err;
493 }
494
495 skb_dst_drop(skb);
3ef0eb0d 496 inet_frag_lru_move(&qp->q);
1706d587 497 return -EINPROGRESS;
1da177e4
LT
498
499err:
500 kfree_skb(skb);
1706d587 501 return err;
1da177e4
LT
502}
503
504
505/* Build a new IP datagram from all its fragments. */
506
1706d587
HX
507static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
508 struct net_device *dev)
1da177e4 509{
2bad35b7 510 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
1da177e4 511 struct iphdr *iph;
5ab11c98 512 struct sk_buff *fp, *head = qp->q.fragments;
1da177e4
LT
513 int len;
514 int ihlen;
1706d587 515 int err;
3cc49492 516 int sum_truesize;
5173cc05 517 u8 ecn;
1da177e4
LT
518
519 ipq_kill(qp);
520
be991971 521 ecn = ip_frag_ecn_table[qp->ecn];
5173cc05
ED
522 if (unlikely(ecn == 0xff)) {
523 err = -EINVAL;
524 goto out_fail;
525 }
1706d587
HX
526 /* Make the one we just received the head. */
527 if (prev) {
528 head = prev->next;
529 fp = skb_clone(head, GFP_ATOMIC);
1706d587
HX
530 if (!fp)
531 goto out_nomem;
532
533 fp->next = head->next;
d6bebca9
CG
534 if (!fp->next)
535 qp->q.fragments_tail = fp;
1706d587
HX
536 prev->next = fp;
537
5ab11c98
PE
538 skb_morph(head, qp->q.fragments);
539 head->next = qp->q.fragments->next;
1706d587 540
cbf8f7bb 541 consume_skb(qp->q.fragments);
5ab11c98 542 qp->q.fragments = head;
1706d587
HX
543 }
544
547b792c
IJ
545 WARN_ON(head == NULL);
546 WARN_ON(FRAG_CB(head)->offset != 0);
1da177e4
LT
547
548 /* Allocate a new buffer for the datagram. */
c9bdd4b5 549 ihlen = ip_hdrlen(head);
5ab11c98 550 len = ihlen + qp->q.len;
1da177e4 551
1706d587 552 err = -E2BIG;
132adf54 553 if (len > 65535)
1da177e4
LT
554 goto out_oversize;
555
556 /* Head of list must not be cloned. */
14bbd6a5 557 if (skb_unclone(head, GFP_ATOMIC))
1da177e4
LT
558 goto out_nomem;
559
560 /* If the first fragment is fragmented itself, we split
561 * it to two chunks: the first with data and paged part
562 * and the second, holding only fragments. */
21dc3301 563 if (skb_has_frag_list(head)) {
1da177e4
LT
564 struct sk_buff *clone;
565 int i, plen = 0;
566
567 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
568 goto out_nomem;
569 clone->next = head->next;
570 head->next = clone;
571 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
d7fcf1a5 572 skb_frag_list_init(head);
9e903e08
ED
573 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
574 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
1da177e4
LT
575 clone->len = clone->data_len = head->data_len - plen;
576 head->data_len -= clone->len;
577 head->len -= clone->len;
578 clone->csum = 0;
579 clone->ip_summed = head->ip_summed;
d433673e 580 add_frag_mem_limit(&qp->q, clone->truesize);
1da177e4
LT
581 }
582
d56f90a7 583 skb_push(head, head->data - skb_network_header(head));
1da177e4 584
3cc49492
ED
585 sum_truesize = head->truesize;
586 for (fp = head->next; fp;) {
587 bool headstolen;
588 int delta;
589 struct sk_buff *next = fp->next;
590
591 sum_truesize += fp->truesize;
1da177e4
LT
592 if (head->ip_summed != fp->ip_summed)
593 head->ip_summed = CHECKSUM_NONE;
84fa7933 594 else if (head->ip_summed == CHECKSUM_COMPLETE)
1da177e4 595 head->csum = csum_add(head->csum, fp->csum);
3cc49492
ED
596
597 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
598 kfree_skb_partial(fp, headstolen);
599 } else {
600 if (!skb_shinfo(head)->frag_list)
601 skb_shinfo(head)->frag_list = fp;
602 head->data_len += fp->len;
603 head->len += fp->len;
604 head->truesize += fp->truesize;
605 }
606 fp = next;
1da177e4 607 }
d433673e 608 sub_frag_mem_limit(&qp->q, sum_truesize);
1da177e4
LT
609
610 head->next = NULL;
611 head->dev = dev;
5ab11c98 612 head->tstamp = qp->q.stamp;
5f2d04f1 613 IPCB(head)->frag_max_size = qp->q.max_size;
1da177e4 614
eddc9ec5 615 iph = ip_hdr(head);
5f2d04f1
PM
616 /* max_size != 0 implies at least one fragment had IP_DF set */
617 iph->frag_off = qp->q.max_size ? htons(IP_DF) : 0;
1da177e4 618 iph->tot_len = htons(len);
5173cc05 619 iph->tos |= ecn;
2bad35b7 620 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
5ab11c98 621 qp->q.fragments = NULL;
d6bebca9 622 qp->q.fragments_tail = NULL;
1706d587 623 return 0;
1da177e4
LT
624
625out_nomem:
afd46503
JP
626 LIMIT_NETDEBUG(KERN_ERR pr_fmt("queue_glue: no memory for gluing queue %p\n"),
627 qp);
45542479 628 err = -ENOMEM;
1da177e4
LT
629 goto out_fail;
630out_oversize:
e87cc472 631 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
1da177e4 632out_fail:
bbf31bf1 633 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
1706d587 634 return err;
1da177e4
LT
635}
636
637/* Process an incoming IP datagram fragment. */
776c729e 638int ip_defrag(struct sk_buff *skb, u32 user)
1da177e4 639{
1da177e4 640 struct ipq *qp;
ac18e750 641 struct net *net;
e905a9ed 642
adf30907 643 net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
7c73a6fa 644 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
1da177e4 645
1da177e4 646 /* Lookup (or create) queue header */
ac18e750 647 if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
1706d587 648 int ret;
1da177e4 649
5ab11c98 650 spin_lock(&qp->q.lock);
1da177e4 651
1706d587 652 ret = ip_frag_queue(qp, skb);
1da177e4 653
5ab11c98 654 spin_unlock(&qp->q.lock);
4b6cb5d8 655 ipq_put(qp);
776c729e 656 return ret;
1da177e4
LT
657 }
658
7c73a6fa 659 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
1da177e4 660 kfree_skb(skb);
776c729e 661 return -ENOMEM;
1da177e4 662}
4bc2f18b 663EXPORT_SYMBOL(ip_defrag);
1da177e4 664
bc416d97
ED
665struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user)
666{
1bf3751e 667 struct iphdr iph;
bc416d97
ED
668 u32 len;
669
670 if (skb->protocol != htons(ETH_P_IP))
671 return skb;
672
1bf3751e 673 if (!skb_copy_bits(skb, 0, &iph, sizeof(iph)))
bc416d97
ED
674 return skb;
675
1bf3751e 676 if (iph.ihl < 5 || iph.version != 4)
bc416d97 677 return skb;
1bf3751e
JB
678
679 len = ntohs(iph.tot_len);
680 if (skb->len < len || len < (iph.ihl * 4))
bc416d97
ED
681 return skb;
682
1bf3751e 683 if (ip_is_fragment(&iph)) {
bc416d97
ED
684 skb = skb_share_check(skb, GFP_ATOMIC);
685 if (skb) {
1bf3751e
JB
686 if (!pskb_may_pull(skb, iph.ihl*4))
687 return skb;
bc416d97
ED
688 if (pskb_trim_rcsum(skb, len))
689 return skb;
690 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
691 if (ip_defrag(skb, user))
692 return NULL;
7539fadc 693 skb_clear_hash(skb);
bc416d97
ED
694 }
695 }
696 return skb;
697}
698EXPORT_SYMBOL(ip_check_defrag);
699
8d8354d2
PE
700#ifdef CONFIG_SYSCTL
701static int zero;
702
0a64b4b8 703static struct ctl_table ip4_frags_ns_ctl_table[] = {
8d8354d2 704 {
8d8354d2 705 .procname = "ipfrag_high_thresh",
e31e0bdc 706 .data = &init_net.ipv4.frags.high_thresh,
8d8354d2
PE
707 .maxlen = sizeof(int),
708 .mode = 0644,
6d9f239a 709 .proc_handler = proc_dointvec
8d8354d2
PE
710 },
711 {
8d8354d2 712 .procname = "ipfrag_low_thresh",
e31e0bdc 713 .data = &init_net.ipv4.frags.low_thresh,
8d8354d2
PE
714 .maxlen = sizeof(int),
715 .mode = 0644,
6d9f239a 716 .proc_handler = proc_dointvec
8d8354d2
PE
717 },
718 {
8d8354d2 719 .procname = "ipfrag_time",
b2fd5321 720 .data = &init_net.ipv4.frags.timeout,
8d8354d2
PE
721 .maxlen = sizeof(int),
722 .mode = 0644,
6d9f239a 723 .proc_handler = proc_dointvec_jiffies,
8d8354d2 724 },
7d291ebb
PE
725 { }
726};
727
728static struct ctl_table ip4_frags_ctl_table[] = {
8d8354d2 729 {
8d8354d2 730 .procname = "ipfrag_secret_interval",
3b4bc4a2 731 .data = &ip4_frags.secret_interval,
8d8354d2
PE
732 .maxlen = sizeof(int),
733 .mode = 0644,
6d9f239a 734 .proc_handler = proc_dointvec_jiffies,
8d8354d2
PE
735 },
736 {
737 .procname = "ipfrag_max_dist",
738 .data = &sysctl_ipfrag_max_dist,
739 .maxlen = sizeof(int),
740 .mode = 0644,
6d9f239a 741 .proc_handler = proc_dointvec_minmax,
8d8354d2
PE
742 .extra1 = &zero
743 },
744 { }
745};
746
2c8c1e72 747static int __net_init ip4_frags_ns_ctl_register(struct net *net)
8d8354d2 748{
e4a2d5c2 749 struct ctl_table *table;
8d8354d2
PE
750 struct ctl_table_header *hdr;
751
0a64b4b8 752 table = ip4_frags_ns_ctl_table;
09ad9bc7 753 if (!net_eq(net, &init_net)) {
0a64b4b8 754 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
e4a2d5c2
PE
755 if (table == NULL)
756 goto err_alloc;
757
e31e0bdc
PE
758 table[0].data = &net->ipv4.frags.high_thresh;
759 table[1].data = &net->ipv4.frags.low_thresh;
b2fd5321 760 table[2].data = &net->ipv4.frags.timeout;
464dc801
EB
761
762 /* Don't export sysctls to unprivileged users */
763 if (net->user_ns != &init_user_ns)
764 table[0].procname = NULL;
e4a2d5c2
PE
765 }
766
ec8f23ce 767 hdr = register_net_sysctl(net, "net/ipv4", table);
e4a2d5c2
PE
768 if (hdr == NULL)
769 goto err_reg;
770
771 net->ipv4.frags_hdr = hdr;
772 return 0;
773
774err_reg:
09ad9bc7 775 if (!net_eq(net, &init_net))
e4a2d5c2
PE
776 kfree(table);
777err_alloc:
778 return -ENOMEM;
779}
780
2c8c1e72 781static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
e4a2d5c2
PE
782{
783 struct ctl_table *table;
784
785 table = net->ipv4.frags_hdr->ctl_table_arg;
786 unregister_net_sysctl_table(net->ipv4.frags_hdr);
787 kfree(table);
8d8354d2 788}
7d291ebb
PE
789
790static void ip4_frags_ctl_register(void)
791{
43444757 792 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
7d291ebb 793}
8d8354d2 794#else
0a64b4b8 795static inline int ip4_frags_ns_ctl_register(struct net *net)
8d8354d2
PE
796{
797 return 0;
798}
e4a2d5c2 799
0a64b4b8 800static inline void ip4_frags_ns_ctl_unregister(struct net *net)
e4a2d5c2
PE
801{
802}
7d291ebb
PE
803
804static inline void ip4_frags_ctl_register(void)
805{
806}
8d8354d2
PE
807#endif
808
2c8c1e72 809static int __net_init ipv4_frags_init_net(struct net *net)
8d8354d2 810{
c2a93660
JDB
811 /* Fragment cache limits.
812 *
813 * The fragment memory accounting code, (tries to) account for
814 * the real memory usage, by measuring both the size of frag
815 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
816 * and the SKB's truesize.
817 *
818 * A 64K fragment consumes 129736 bytes (44*2944)+200
819 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
820 *
821 * We will commit 4MB at one time. Should we cross that limit
822 * we will prune down to 3MB, making room for approx 8 big 64K
823 * fragments 8x128k.
e31e0bdc 824 */
c2a93660
JDB
825 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
826 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
b2fd5321
PE
827 /*
828 * Important NOTE! Fragment queue must be destroyed before MSL expires.
829 * RFC791 is wrong proposing to prolongate timer each fragment arrival
830 * by TTL.
831 */
832 net->ipv4.frags.timeout = IP_FRAG_TIME;
833
e5a2bb84
PE
834 inet_frags_init_net(&net->ipv4.frags);
835
0a64b4b8 836 return ip4_frags_ns_ctl_register(net);
8d8354d2
PE
837}
838
2c8c1e72 839static void __net_exit ipv4_frags_exit_net(struct net *net)
81566e83 840{
0a64b4b8 841 ip4_frags_ns_ctl_unregister(net);
81566e83
PE
842 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
843}
844
845static struct pernet_operations ip4_frags_ops = {
846 .init = ipv4_frags_init_net,
847 .exit = ipv4_frags_exit_net,
848};
849
b7aa0bf7 850void __init ipfrag_init(void)
1da177e4 851{
7d291ebb 852 ip4_frags_ctl_register();
81566e83 853 register_pernet_subsys(&ip4_frags_ops);
321a3a99 854 ip4_frags.hashfn = ip4_hashfn;
c6fda282 855 ip4_frags.constructor = ip4_frag_init;
1e4b8287
PE
856 ip4_frags.destructor = ip4_frag_free;
857 ip4_frags.skb_free = NULL;
858 ip4_frags.qsize = sizeof(struct ipq);
abd6523d 859 ip4_frags.match = ip4_frag_match;
e521db9d 860 ip4_frags.frag_expire = ip_expire;
3b4bc4a2 861 ip4_frags.secret_interval = 10 * 60 * HZ;
7eb95156 862 inet_frags_init(&ip4_frags);
1da177e4 863}