xref: /openbmc/linux/net/ipv4/ip_fragment.c (revision 5ab11c98d3a950faf6922b6166e5f8fc874590e7)
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.
7  *
8  * Version:	$Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
9  *
10  * Authors:	Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
11  *		Alan Cox <Alan.Cox@linux.org>
12  *
13  * Fixes:
14  *		Alan Cox	:	Split from ip.c , see ip_input.c for history.
15  *		David S. Miller :	Begin massive cleanup...
16  *		Andi Kleen	:	Add sysctls.
17  *		xxxx		:	Overlapfrag bug.
18  *		Ultima          :       ip_expire() kernel panic.
19  *		Bill Hawes	:	Frag accounting and evictor fixes.
20  *		John McDonald	:	0 length frag bug.
21  *		Alexey Kuznetsov:	SMP races, threading, cleanup.
22  *		Patrick McHardy :	LRU queue of frag heads for evictor.
23  */
24 
25 #include <linux/compiler.h>
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>
37 #include <net/sock.h>
38 #include <net/ip.h>
39 #include <net/icmp.h>
40 #include <net/checksum.h>
41 #include <net/inetpeer.h>
42 #include <net/inet_frag.h>
43 #include <linux/tcp.h>
44 #include <linux/udp.h>
45 #include <linux/inet.h>
46 #include <linux/netfilter_ipv4.h>
47 
48 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
49  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
50  * as well. Or notify me, at least. --ANK
51  */
52 
53 /* Fragment cache limits. We will commit 256K at one time. Should we
54  * cross that limit we will prune down to 192K. This should cope with
55  * even the most extreme cases without allowing an attacker to measurably
56  * harm machine performance.
57  */
58 int sysctl_ipfrag_high_thresh __read_mostly = 256*1024;
59 int sysctl_ipfrag_low_thresh __read_mostly = 192*1024;
60 
61 int sysctl_ipfrag_max_dist __read_mostly = 64;
62 
63 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
64  * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
65  */
66 int sysctl_ipfrag_time __read_mostly = IP_FRAG_TIME;
67 
68 struct ipfrag_skb_cb
69 {
70 	struct inet_skb_parm	h;
71 	int			offset;
72 };
73 
74 #define FRAG_CB(skb)	((struct ipfrag_skb_cb*)((skb)->cb))
75 
76 /* Describe an entry in the "incomplete datagrams" queue. */
77 struct ipq {
78 	struct inet_frag_queue q;
79 
80 	u32		user;
81 	__be32		saddr;
82 	__be32		daddr;
83 	__be16		id;
84 	u8		protocol;
85 	int             iif;
86 	unsigned int    rid;
87 	struct inet_peer *peer;
88 };
89 
90 /* Hash table. */
91 
92 #define IPQ_HASHSZ	64
93 
94 /* Per-bucket lock is easy to add now. */
95 static struct hlist_head ipq_hash[IPQ_HASHSZ];
96 static DEFINE_RWLOCK(ipfrag_lock);
97 static u32 ipfrag_hash_rnd;
98 static LIST_HEAD(ipq_lru_list);
99 int ip_frag_nqueues = 0;
100 
101 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
102 			 struct net_device *dev);
103 
104 static __inline__ void __ipq_unlink(struct ipq *qp)
105 {
106 	hlist_del(&qp->q.list);
107 	list_del(&qp->q.lru_list);
108 	ip_frag_nqueues--;
109 }
110 
111 static __inline__ void ipq_unlink(struct ipq *ipq)
112 {
113 	write_lock(&ipfrag_lock);
114 	__ipq_unlink(ipq);
115 	write_unlock(&ipfrag_lock);
116 }
117 
118 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
119 {
120 	return jhash_3words((__force u32)id << 16 | prot,
121 			    (__force u32)saddr, (__force u32)daddr,
122 			    ipfrag_hash_rnd) & (IPQ_HASHSZ - 1);
123 }
124 
125 static struct timer_list ipfrag_secret_timer;
126 int sysctl_ipfrag_secret_interval __read_mostly = 10 * 60 * HZ;
127 
128 static void ipfrag_secret_rebuild(unsigned long dummy)
129 {
130 	unsigned long now = jiffies;
131 	int i;
132 
133 	write_lock(&ipfrag_lock);
134 	get_random_bytes(&ipfrag_hash_rnd, sizeof(u32));
135 	for (i = 0; i < IPQ_HASHSZ; i++) {
136 		struct ipq *q;
137 		struct hlist_node *p, *n;
138 
139 		hlist_for_each_entry_safe(q, p, n, &ipq_hash[i], q.list) {
140 			unsigned int hval = ipqhashfn(q->id, q->saddr,
141 						      q->daddr, q->protocol);
142 
143 			if (hval != i) {
144 				hlist_del(&q->q.list);
145 
146 				/* Relink to new hash chain. */
147 				hlist_add_head(&q->q.list, &ipq_hash[hval]);
148 			}
149 		}
150 	}
151 	write_unlock(&ipfrag_lock);
152 
153 	mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval);
154 }
155 
156 atomic_t ip_frag_mem = ATOMIC_INIT(0);	/* Memory used for fragments */
157 
158 /* Memory Tracking Functions. */
159 static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work)
160 {
161 	if (work)
162 		*work -= skb->truesize;
163 	atomic_sub(skb->truesize, &ip_frag_mem);
164 	kfree_skb(skb);
165 }
166 
167 static __inline__ void frag_free_queue(struct ipq *qp, int *work)
168 {
169 	if (work)
170 		*work -= sizeof(struct ipq);
171 	atomic_sub(sizeof(struct ipq), &ip_frag_mem);
172 	kfree(qp);
173 }
174 
175 static __inline__ struct ipq *frag_alloc_queue(void)
176 {
177 	struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC);
178 
179 	if (!qp)
180 		return NULL;
181 	atomic_add(sizeof(struct ipq), &ip_frag_mem);
182 	return qp;
183 }
184 
185 
186 /* Destruction primitives. */
187 
188 /* Complete destruction of ipq. */
189 static void ip_frag_destroy(struct ipq *qp, int *work)
190 {
191 	struct sk_buff *fp;
192 
193 	BUG_TRAP(qp->q.last_in&COMPLETE);
194 	BUG_TRAP(del_timer(&qp->q.timer) == 0);
195 
196 	if (qp->peer)
197 		inet_putpeer(qp->peer);
198 
199 	/* Release all fragment data. */
200 	fp = qp->q.fragments;
201 	while (fp) {
202 		struct sk_buff *xp = fp->next;
203 
204 		frag_kfree_skb(fp, work);
205 		fp = xp;
206 	}
207 
208 	/* Finally, release the queue descriptor itself. */
209 	frag_free_queue(qp, work);
210 }
211 
212 static __inline__ void ipq_put(struct ipq *ipq, int *work)
213 {
214 	if (atomic_dec_and_test(&ipq->q.refcnt))
215 		ip_frag_destroy(ipq, work);
216 }
217 
218 /* Kill ipq entry. It is not destroyed immediately,
219  * because caller (and someone more) holds reference count.
220  */
221 static void ipq_kill(struct ipq *ipq)
222 {
223 	if (del_timer(&ipq->q.timer))
224 		atomic_dec(&ipq->q.refcnt);
225 
226 	if (!(ipq->q.last_in & COMPLETE)) {
227 		ipq_unlink(ipq);
228 		atomic_dec(&ipq->q.refcnt);
229 		ipq->q.last_in |= COMPLETE;
230 	}
231 }
232 
233 /* Memory limiting on fragments.  Evictor trashes the oldest
234  * fragment queue until we are back under the threshold.
235  */
236 static void ip_evictor(void)
237 {
238 	struct ipq *qp;
239 	struct list_head *tmp;
240 	int work;
241 
242 	work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh;
243 	if (work <= 0)
244 		return;
245 
246 	while (work > 0) {
247 		read_lock(&ipfrag_lock);
248 		if (list_empty(&ipq_lru_list)) {
249 			read_unlock(&ipfrag_lock);
250 			return;
251 		}
252 		tmp = ipq_lru_list.next;
253 		qp = list_entry(tmp, struct ipq, q.lru_list);
254 		atomic_inc(&qp->q.refcnt);
255 		read_unlock(&ipfrag_lock);
256 
257 		spin_lock(&qp->q.lock);
258 		if (!(qp->q.last_in&COMPLETE))
259 			ipq_kill(qp);
260 		spin_unlock(&qp->q.lock);
261 
262 		ipq_put(qp, &work);
263 		IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
264 	}
265 }
266 
267 /*
268  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
269  */
270 static void ip_expire(unsigned long arg)
271 {
272 	struct ipq *qp = (struct ipq *) arg;
273 
274 	spin_lock(&qp->q.lock);
275 
276 	if (qp->q.last_in & COMPLETE)
277 		goto out;
278 
279 	ipq_kill(qp);
280 
281 	IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
282 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
283 
284 	if ((qp->q.last_in&FIRST_IN) && qp->q.fragments != NULL) {
285 		struct sk_buff *head = qp->q.fragments;
286 		/* Send an ICMP "Fragment Reassembly Timeout" message. */
287 		if ((head->dev = dev_get_by_index(&init_net, qp->iif)) != NULL) {
288 			icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
289 			dev_put(head->dev);
290 		}
291 	}
292 out:
293 	spin_unlock(&qp->q.lock);
294 	ipq_put(qp, NULL);
295 }
296 
297 /* Creation primitives. */
298 
299 static struct ipq *ip_frag_intern(struct ipq *qp_in)
300 {
301 	struct ipq *qp;
302 #ifdef CONFIG_SMP
303 	struct hlist_node *n;
304 #endif
305 	unsigned int hash;
306 
307 	write_lock(&ipfrag_lock);
308 	hash = ipqhashfn(qp_in->id, qp_in->saddr, qp_in->daddr,
309 			 qp_in->protocol);
310 #ifdef CONFIG_SMP
311 	/* With SMP race we have to recheck hash table, because
312 	 * such entry could be created on other cpu, while we
313 	 * promoted read lock to write lock.
314 	 */
315 	hlist_for_each_entry(qp, n, &ipq_hash[hash], q.list) {
316 		if (qp->id == qp_in->id		&&
317 		    qp->saddr == qp_in->saddr	&&
318 		    qp->daddr == qp_in->daddr	&&
319 		    qp->protocol == qp_in->protocol &&
320 		    qp->user == qp_in->user) {
321 			atomic_inc(&qp->q.refcnt);
322 			write_unlock(&ipfrag_lock);
323 			qp_in->q.last_in |= COMPLETE;
324 			ipq_put(qp_in, NULL);
325 			return qp;
326 		}
327 	}
328 #endif
329 	qp = qp_in;
330 
331 	if (!mod_timer(&qp->q.timer, jiffies + sysctl_ipfrag_time))
332 		atomic_inc(&qp->q.refcnt);
333 
334 	atomic_inc(&qp->q.refcnt);
335 	hlist_add_head(&qp->q.list, &ipq_hash[hash]);
336 	INIT_LIST_HEAD(&qp->q.lru_list);
337 	list_add_tail(&qp->q.lru_list, &ipq_lru_list);
338 	ip_frag_nqueues++;
339 	write_unlock(&ipfrag_lock);
340 	return qp;
341 }
342 
343 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
344 static struct ipq *ip_frag_create(struct iphdr *iph, u32 user)
345 {
346 	struct ipq *qp;
347 
348 	if ((qp = frag_alloc_queue()) == NULL)
349 		goto out_nomem;
350 
351 	qp->protocol = iph->protocol;
352 	qp->q.last_in = 0;
353 	qp->id = iph->id;
354 	qp->saddr = iph->saddr;
355 	qp->daddr = iph->daddr;
356 	qp->user = user;
357 	qp->q.len = 0;
358 	qp->q.meat = 0;
359 	qp->q.fragments = NULL;
360 	qp->iif = 0;
361 	qp->peer = sysctl_ipfrag_max_dist ? inet_getpeer(iph->saddr, 1) : NULL;
362 
363 	/* Initialize a timer for this entry. */
364 	init_timer(&qp->q.timer);
365 	qp->q.timer.data = (unsigned long) qp;	/* pointer to queue	*/
366 	qp->q.timer.function = ip_expire;		/* expire function	*/
367 	spin_lock_init(&qp->q.lock);
368 	atomic_set(&qp->q.refcnt, 1);
369 
370 	return ip_frag_intern(qp);
371 
372 out_nomem:
373 	LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
374 	return NULL;
375 }
376 
377 /* Find the correct entry in the "incomplete datagrams" queue for
378  * this IP datagram, and create new one, if nothing is found.
379  */
380 static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
381 {
382 	__be16 id = iph->id;
383 	__be32 saddr = iph->saddr;
384 	__be32 daddr = iph->daddr;
385 	__u8 protocol = iph->protocol;
386 	unsigned int hash;
387 	struct ipq *qp;
388 	struct hlist_node *n;
389 
390 	read_lock(&ipfrag_lock);
391 	hash = ipqhashfn(id, saddr, daddr, protocol);
392 	hlist_for_each_entry(qp, n, &ipq_hash[hash], q.list) {
393 		if (qp->id == id		&&
394 		    qp->saddr == saddr	&&
395 		    qp->daddr == daddr	&&
396 		    qp->protocol == protocol &&
397 		    qp->user == user) {
398 			atomic_inc(&qp->q.refcnt);
399 			read_unlock(&ipfrag_lock);
400 			return qp;
401 		}
402 	}
403 	read_unlock(&ipfrag_lock);
404 
405 	return ip_frag_create(iph, user);
406 }
407 
408 /* Is the fragment too far ahead to be part of ipq? */
409 static inline int ip_frag_too_far(struct ipq *qp)
410 {
411 	struct inet_peer *peer = qp->peer;
412 	unsigned int max = sysctl_ipfrag_max_dist;
413 	unsigned int start, end;
414 
415 	int rc;
416 
417 	if (!peer || !max)
418 		return 0;
419 
420 	start = qp->rid;
421 	end = atomic_inc_return(&peer->rid);
422 	qp->rid = end;
423 
424 	rc = qp->q.fragments && (end - start) > max;
425 
426 	if (rc) {
427 		IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
428 	}
429 
430 	return rc;
431 }
432 
433 static int ip_frag_reinit(struct ipq *qp)
434 {
435 	struct sk_buff *fp;
436 
437 	if (!mod_timer(&qp->q.timer, jiffies + sysctl_ipfrag_time)) {
438 		atomic_inc(&qp->q.refcnt);
439 		return -ETIMEDOUT;
440 	}
441 
442 	fp = qp->q.fragments;
443 	do {
444 		struct sk_buff *xp = fp->next;
445 		frag_kfree_skb(fp, NULL);
446 		fp = xp;
447 	} while (fp);
448 
449 	qp->q.last_in = 0;
450 	qp->q.len = 0;
451 	qp->q.meat = 0;
452 	qp->q.fragments = NULL;
453 	qp->iif = 0;
454 
455 	return 0;
456 }
457 
458 /* Add new segment to existing queue. */
459 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
460 {
461 	struct sk_buff *prev, *next;
462 	struct net_device *dev;
463 	int flags, offset;
464 	int ihl, end;
465 	int err = -ENOENT;
466 
467 	if (qp->q.last_in & COMPLETE)
468 		goto err;
469 
470 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
471 	    unlikely(ip_frag_too_far(qp)) &&
472 	    unlikely(err = ip_frag_reinit(qp))) {
473 		ipq_kill(qp);
474 		goto err;
475 	}
476 
477 	offset = ntohs(ip_hdr(skb)->frag_off);
478 	flags = offset & ~IP_OFFSET;
479 	offset &= IP_OFFSET;
480 	offset <<= 3;		/* offset is in 8-byte chunks */
481 	ihl = ip_hdrlen(skb);
482 
483 	/* Determine the position of this fragment. */
484 	end = offset + skb->len - ihl;
485 	err = -EINVAL;
486 
487 	/* Is this the final fragment? */
488 	if ((flags & IP_MF) == 0) {
489 		/* If we already have some bits beyond end
490 		 * or have different end, the segment is corrrupted.
491 		 */
492 		if (end < qp->q.len ||
493 		    ((qp->q.last_in & LAST_IN) && end != qp->q.len))
494 			goto err;
495 		qp->q.last_in |= LAST_IN;
496 		qp->q.len = end;
497 	} else {
498 		if (end&7) {
499 			end &= ~7;
500 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
501 				skb->ip_summed = CHECKSUM_NONE;
502 		}
503 		if (end > qp->q.len) {
504 			/* Some bits beyond end -> corruption. */
505 			if (qp->q.last_in & LAST_IN)
506 				goto err;
507 			qp->q.len = end;
508 		}
509 	}
510 	if (end == offset)
511 		goto err;
512 
513 	err = -ENOMEM;
514 	if (pskb_pull(skb, ihl) == NULL)
515 		goto err;
516 
517 	err = pskb_trim_rcsum(skb, end - offset);
518 	if (err)
519 		goto err;
520 
521 	/* Find out which fragments are in front and at the back of us
522 	 * in the chain of fragments so far.  We must know where to put
523 	 * this fragment, right?
524 	 */
525 	prev = NULL;
526 	for (next = qp->q.fragments; next != NULL; next = next->next) {
527 		if (FRAG_CB(next)->offset >= offset)
528 			break;	/* bingo! */
529 		prev = next;
530 	}
531 
532 	/* We found where to put this one.  Check for overlap with
533 	 * preceding fragment, and, if needed, align things so that
534 	 * any overlaps are eliminated.
535 	 */
536 	if (prev) {
537 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
538 
539 		if (i > 0) {
540 			offset += i;
541 			err = -EINVAL;
542 			if (end <= offset)
543 				goto err;
544 			err = -ENOMEM;
545 			if (!pskb_pull(skb, i))
546 				goto err;
547 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
548 				skb->ip_summed = CHECKSUM_NONE;
549 		}
550 	}
551 
552 	err = -ENOMEM;
553 
554 	while (next && FRAG_CB(next)->offset < end) {
555 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
556 
557 		if (i < next->len) {
558 			/* Eat head of the next overlapped fragment
559 			 * and leave the loop. The next ones cannot overlap.
560 			 */
561 			if (!pskb_pull(next, i))
562 				goto err;
563 			FRAG_CB(next)->offset += i;
564 			qp->q.meat -= i;
565 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
566 				next->ip_summed = CHECKSUM_NONE;
567 			break;
568 		} else {
569 			struct sk_buff *free_it = next;
570 
571 			/* Old fragment is completely overridden with
572 			 * new one drop it.
573 			 */
574 			next = next->next;
575 
576 			if (prev)
577 				prev->next = next;
578 			else
579 				qp->q.fragments = next;
580 
581 			qp->q.meat -= free_it->len;
582 			frag_kfree_skb(free_it, NULL);
583 		}
584 	}
585 
586 	FRAG_CB(skb)->offset = offset;
587 
588 	/* Insert this fragment in the chain of fragments. */
589 	skb->next = next;
590 	if (prev)
591 		prev->next = skb;
592 	else
593 		qp->q.fragments = skb;
594 
595 	dev = skb->dev;
596 	if (dev) {
597 		qp->iif = dev->ifindex;
598 		skb->dev = NULL;
599 	}
600 	qp->q.stamp = skb->tstamp;
601 	qp->q.meat += skb->len;
602 	atomic_add(skb->truesize, &ip_frag_mem);
603 	if (offset == 0)
604 		qp->q.last_in |= FIRST_IN;
605 
606 	if (qp->q.last_in == (FIRST_IN | LAST_IN) && qp->q.meat == qp->q.len)
607 		return ip_frag_reasm(qp, prev, dev);
608 
609 	write_lock(&ipfrag_lock);
610 	list_move_tail(&qp->q.lru_list, &ipq_lru_list);
611 	write_unlock(&ipfrag_lock);
612 	return -EINPROGRESS;
613 
614 err:
615 	kfree_skb(skb);
616 	return err;
617 }
618 
619 
620 /* Build a new IP datagram from all its fragments. */
621 
622 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
623 			 struct net_device *dev)
624 {
625 	struct iphdr *iph;
626 	struct sk_buff *fp, *head = qp->q.fragments;
627 	int len;
628 	int ihlen;
629 	int err;
630 
631 	ipq_kill(qp);
632 
633 	/* Make the one we just received the head. */
634 	if (prev) {
635 		head = prev->next;
636 		fp = skb_clone(head, GFP_ATOMIC);
637 
638 		if (!fp)
639 			goto out_nomem;
640 
641 		fp->next = head->next;
642 		prev->next = fp;
643 
644 		skb_morph(head, qp->q.fragments);
645 		head->next = qp->q.fragments->next;
646 
647 		kfree_skb(qp->q.fragments);
648 		qp->q.fragments = head;
649 	}
650 
651 	BUG_TRAP(head != NULL);
652 	BUG_TRAP(FRAG_CB(head)->offset == 0);
653 
654 	/* Allocate a new buffer for the datagram. */
655 	ihlen = ip_hdrlen(head);
656 	len = ihlen + qp->q.len;
657 
658 	err = -E2BIG;
659 	if (len > 65535)
660 		goto out_oversize;
661 
662 	/* Head of list must not be cloned. */
663 	err = -ENOMEM;
664 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
665 		goto out_nomem;
666 
667 	/* If the first fragment is fragmented itself, we split
668 	 * it to two chunks: the first with data and paged part
669 	 * and the second, holding only fragments. */
670 	if (skb_shinfo(head)->frag_list) {
671 		struct sk_buff *clone;
672 		int i, plen = 0;
673 
674 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
675 			goto out_nomem;
676 		clone->next = head->next;
677 		head->next = clone;
678 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
679 		skb_shinfo(head)->frag_list = NULL;
680 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
681 			plen += skb_shinfo(head)->frags[i].size;
682 		clone->len = clone->data_len = head->data_len - plen;
683 		head->data_len -= clone->len;
684 		head->len -= clone->len;
685 		clone->csum = 0;
686 		clone->ip_summed = head->ip_summed;
687 		atomic_add(clone->truesize, &ip_frag_mem);
688 	}
689 
690 	skb_shinfo(head)->frag_list = head->next;
691 	skb_push(head, head->data - skb_network_header(head));
692 	atomic_sub(head->truesize, &ip_frag_mem);
693 
694 	for (fp=head->next; fp; fp = fp->next) {
695 		head->data_len += fp->len;
696 		head->len += fp->len;
697 		if (head->ip_summed != fp->ip_summed)
698 			head->ip_summed = CHECKSUM_NONE;
699 		else if (head->ip_summed == CHECKSUM_COMPLETE)
700 			head->csum = csum_add(head->csum, fp->csum);
701 		head->truesize += fp->truesize;
702 		atomic_sub(fp->truesize, &ip_frag_mem);
703 	}
704 
705 	head->next = NULL;
706 	head->dev = dev;
707 	head->tstamp = qp->q.stamp;
708 
709 	iph = ip_hdr(head);
710 	iph->frag_off = 0;
711 	iph->tot_len = htons(len);
712 	IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
713 	qp->q.fragments = NULL;
714 	return 0;
715 
716 out_nomem:
717 	LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
718 			      "queue %p\n", qp);
719 	goto out_fail;
720 out_oversize:
721 	if (net_ratelimit())
722 		printk(KERN_INFO
723 			"Oversized IP packet from %d.%d.%d.%d.\n",
724 			NIPQUAD(qp->saddr));
725 out_fail:
726 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
727 	return err;
728 }
729 
730 /* Process an incoming IP datagram fragment. */
731 int ip_defrag(struct sk_buff *skb, u32 user)
732 {
733 	struct ipq *qp;
734 
735 	IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
736 
737 	/* Start by cleaning up the memory. */
738 	if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh)
739 		ip_evictor();
740 
741 	/* Lookup (or create) queue header */
742 	if ((qp = ip_find(ip_hdr(skb), user)) != NULL) {
743 		int ret;
744 
745 		spin_lock(&qp->q.lock);
746 
747 		ret = ip_frag_queue(qp, skb);
748 
749 		spin_unlock(&qp->q.lock);
750 		ipq_put(qp, NULL);
751 		return ret;
752 	}
753 
754 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
755 	kfree_skb(skb);
756 	return -ENOMEM;
757 }
758 
759 void __init ipfrag_init(void)
760 {
761 	ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
762 				 (jiffies ^ (jiffies >> 6)));
763 
764 	init_timer(&ipfrag_secret_timer);
765 	ipfrag_secret_timer.function = ipfrag_secret_rebuild;
766 	ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval;
767 	add_timer(&ipfrag_secret_timer);
768 }
769 
770 EXPORT_SYMBOL(ip_defrag);
771