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