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