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