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