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.
6 * The IP fragmentation functionality.
8 * Version: $Id: ip_fragment.c,v 1.53 2000/12/08 17:15:53 davem Exp $
10 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
11 * Alan Cox <Alan.Cox@linux.org>
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.
24 #include <linux/config.h>
25 #include <linux/types.h>
27 #include <linux/sched.h>
28 #include <linux/skbuff.h>
30 #include <linux/icmp.h>
31 #include <linux/netdevice.h>
35 #include <net/checksum.h>
36 #include <linux/tcp.h>
37 #include <linux/udp.h>
38 #include <linux/inet.h>
39 #include <linux/netfilter_ipv4.h>
41 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
42 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
43 * as well. Or notify me, at least. --ANK
46 /* Fragment cache limits. We will commit 256K at one time. Should we
47 * cross that limit we will prune down to 192K. This should cope with
48 * even the most extreme cases without allowing an attacker to measurably
49 * harm machine performance.
51 int sysctl_ipfrag_high_thresh
= 256*1024;
52 int sysctl_ipfrag_low_thresh
= 192*1024;
54 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
55 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
57 int sysctl_ipfrag_time
= IP_FRAG_TIME
;
61 struct inet_skb_parm h
;
65 #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
67 /* Describe an entry in the "incomplete datagrams" queue. */
69 struct ipq
*next
; /* linked list pointers */
79 struct sk_buff
*fragments
; /* linked list of received fragments */
80 int len
; /* total length of original datagram */
84 struct timer_list timer
; /* when will this queue expire? */
86 int iif
; /* Device index - for icmp replies */
93 /* Per-bucket lock is easy to add now. */
94 static struct ipq
*ipq_hash
[IPQ_HASHSZ
];
95 static rwlock_t ipfrag_lock
= RW_LOCK_UNLOCKED
;
96 int ip_frag_nqueues
= 0;
98 static __inline__
void __ipq_unlink(struct ipq
*qp
)
101 qp
->next
->pprev
= qp
->pprev
;
102 *qp
->pprev
= qp
->next
;
106 static __inline__
void ipq_unlink(struct ipq
*ipq
)
108 write_lock(&ipfrag_lock
);
110 write_unlock(&ipfrag_lock
);
114 * Was: ((((id) >> 1) ^ (saddr) ^ (daddr) ^ (prot)) & (IPQ_HASHSZ - 1))
116 * I see, I see evil hand of bigendian mafia. On Intel all the packets hit
117 * one hash bucket with this hash function. 8)
119 static __inline__
unsigned int ipqhashfn(u16 id
, u32 saddr
, u32 daddr
, u8 prot
)
121 unsigned int h
= saddr
^ daddr
;
125 return h
& (IPQ_HASHSZ
- 1);
129 atomic_t ip_frag_mem
= ATOMIC_INIT(0); /* Memory used for fragments */
131 /* Memory Tracking Functions. */
132 extern __inline__
void frag_kfree_skb(struct sk_buff
*skb
)
134 atomic_sub(skb
->truesize
, &ip_frag_mem
);
138 extern __inline__
void frag_free_queue(struct ipq
*qp
)
140 atomic_sub(sizeof(struct ipq
), &ip_frag_mem
);
144 extern __inline__
struct ipq
*frag_alloc_queue(void)
146 struct ipq
*qp
= kmalloc(sizeof(struct ipq
), GFP_ATOMIC
);
150 atomic_add(sizeof(struct ipq
), &ip_frag_mem
);
155 /* Destruction primitives. */
157 /* Complete destruction of ipq. */
158 static void ip_frag_destroy(struct ipq
*qp
)
162 BUG_TRAP(qp
->last_in
&COMPLETE
);
163 BUG_TRAP(del_timer(&qp
->timer
) == 0);
165 /* Release all fragment data. */
168 struct sk_buff
*xp
= fp
->next
;
174 /* Finally, release the queue descriptor itself. */
178 static __inline__
void ipq_put(struct ipq
*ipq
)
180 if (atomic_dec_and_test(&ipq
->refcnt
))
181 ip_frag_destroy(ipq
);
184 /* Kill ipq entry. It is not destroyed immediately,
185 * because caller (and someone more) holds reference count.
187 static __inline__
void ipq_kill(struct ipq
*ipq
)
189 if (del_timer(&ipq
->timer
))
190 atomic_dec(&ipq
->refcnt
);
192 if (!(ipq
->last_in
& COMPLETE
)) {
194 atomic_dec(&ipq
->refcnt
);
195 ipq
->last_in
|= COMPLETE
;
199 /* Memory limiting on fragments. Evictor trashes the oldest
200 * fragment queue until we are back under the low threshold.
202 static void ip_evictor(void)
207 if (atomic_read(&ip_frag_mem
) <= sysctl_ipfrag_low_thresh
)
210 /* FIXME: Make LRU queue of frag heads. -DaveM */
211 for (i
= 0; i
< IPQ_HASHSZ
; i
++) {
213 if (ipq_hash
[i
] == NULL
)
216 write_lock(&ipfrag_lock
);
217 if ((qp
= ipq_hash
[i
]) != NULL
) {
218 /* find the oldest queue for this hash bucket */
222 write_unlock(&ipfrag_lock
);
224 spin_lock(&qp
->lock
);
225 if (del_timer(&qp
->timer
))
226 atomic_dec(&qp
->refcnt
);
227 qp
->last_in
|= COMPLETE
;
228 spin_unlock(&qp
->lock
);
231 IP_INC_STATS_BH(IpReasmFails
);
235 write_unlock(&ipfrag_lock
);
241 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
243 static void ip_expire(unsigned long arg
)
245 struct ipq
*qp
= (struct ipq
*) arg
;
247 spin_lock(&qp
->lock
);
249 if (qp
->last_in
& COMPLETE
)
254 IP_INC_STATS_BH(IpReasmTimeout
);
255 IP_INC_STATS_BH(IpReasmFails
);
257 if ((qp
->last_in
&FIRST_IN
) && qp
->fragments
!= NULL
) {
258 struct sk_buff
*head
= qp
->fragments
;
260 /* Send an ICMP "Fragment Reassembly Timeout" message. */
261 if ((head
->dev
= dev_get_by_index(qp
->iif
)) != NULL
) {
262 icmp_send(head
, ICMP_TIME_EXCEEDED
, ICMP_EXC_FRAGTIME
, 0);
267 spin_unlock(&qp
->lock
);
271 /* Creation primitives. */
273 static struct ipq
*ip_frag_intern(unsigned int hash
, struct ipq
*qp_in
)
277 write_lock(&ipfrag_lock
);
279 /* With SMP race we have to recheck hash table, because
280 * such entry could be created on other cpu, while we
281 * promoted read lock to write lock.
283 for(qp
= ipq_hash
[hash
]; qp
; qp
= qp
->next
) {
284 if(qp
->id
== qp_in
->id
&&
285 qp
->saddr
== qp_in
->saddr
&&
286 qp
->daddr
== qp_in
->daddr
&&
287 qp
->protocol
== qp_in
->protocol
) {
288 atomic_inc(&qp
->refcnt
);
289 write_unlock(&ipfrag_lock
);
290 qp_in
->last_in
|= COMPLETE
;
298 if (!mod_timer(&qp
->timer
, jiffies
+ sysctl_ipfrag_time
))
299 atomic_inc(&qp
->refcnt
);
301 atomic_inc(&qp
->refcnt
);
302 if((qp
->next
= ipq_hash
[hash
]) != NULL
)
303 qp
->next
->pprev
= &qp
->next
;
305 qp
->pprev
= &ipq_hash
[hash
];
307 write_unlock(&ipfrag_lock
);
311 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
312 static struct ipq
*ip_frag_create(unsigned hash
, struct iphdr
*iph
)
316 if ((qp
= frag_alloc_queue()) == NULL
)
319 qp
->protocol
= iph
->protocol
;
322 qp
->saddr
= iph
->saddr
;
323 qp
->daddr
= iph
->daddr
;
326 qp
->fragments
= NULL
;
329 /* Initialize a timer for this entry. */
330 init_timer(&qp
->timer
);
331 qp
->timer
.data
= (unsigned long) qp
; /* pointer to queue */
332 qp
->timer
.function
= ip_expire
; /* expire function */
333 qp
->lock
= SPIN_LOCK_UNLOCKED
;
334 atomic_set(&qp
->refcnt
, 1);
336 return ip_frag_intern(hash
, qp
);
339 NETDEBUG(printk(KERN_ERR
"ip_frag_create: no memory left !\n"));
343 /* Find the correct entry in the "incomplete datagrams" queue for
344 * this IP datagram, and create new one, if nothing is found.
346 static inline struct ipq
*ip_find(struct iphdr
*iph
)
349 __u32 saddr
= iph
->saddr
;
350 __u32 daddr
= iph
->daddr
;
351 __u8 protocol
= iph
->protocol
;
352 unsigned int hash
= ipqhashfn(id
, saddr
, daddr
, protocol
);
355 read_lock(&ipfrag_lock
);
356 for(qp
= ipq_hash
[hash
]; qp
; qp
= qp
->next
) {
358 qp
->saddr
== saddr
&&
359 qp
->daddr
== daddr
&&
360 qp
->protocol
== protocol
) {
361 atomic_inc(&qp
->refcnt
);
362 read_unlock(&ipfrag_lock
);
366 read_unlock(&ipfrag_lock
);
368 return ip_frag_create(hash
, iph
);
371 /* Add new segment to existing queue. */
372 static void ip_frag_queue(struct ipq
*qp
, struct sk_buff
*skb
)
374 struct iphdr
*iph
= skb
->nh
.iph
;
375 struct sk_buff
*prev
, *next
;
379 if (qp
->last_in
& COMPLETE
)
382 offset
= ntohs(iph
->frag_off
);
383 flags
= offset
& ~IP_OFFSET
;
385 offset
<<= 3; /* offset is in 8-byte chunks */
388 /* Determine the position of this fragment. */
389 end
= offset
+ (ntohs(iph
->tot_len
) - ihl
);
391 /* Is this the final fragment? */
392 if ((flags
& IP_MF
) == 0) {
393 /* If we already have some bits beyond end
394 * or have different end, the segment is corrrupted.
397 ((qp
->last_in
& LAST_IN
) && end
!= qp
->len
))
399 qp
->last_in
|= LAST_IN
;
404 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
405 skb
->ip_summed
= CHECKSUM_NONE
;
408 /* Some bits beyond end -> corruption. */
409 if (qp
->last_in
& LAST_IN
)
417 /* Point into the IP datagram 'data' part. */
418 skb_pull(skb
, (skb
->nh
.raw
+ihl
) - skb
->data
);
419 skb_trim(skb
, end
- offset
);
421 /* Find out which fragments are in front and at the back of us
422 * in the chain of fragments so far. We must know where to put
423 * this fragment, right?
426 for(next
= qp
->fragments
; next
!= NULL
; next
= next
->next
) {
427 if (FRAG_CB(next
)->offset
>= offset
)
432 /* We found where to put this one. Check for overlap with
433 * preceding fragment, and, if needed, align things so that
434 * any overlaps are eliminated.
437 int i
= (FRAG_CB(prev
)->offset
+ prev
->len
) - offset
;
444 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
445 skb
->ip_summed
= CHECKSUM_NONE
;
449 while (next
&& FRAG_CB(next
)->offset
< end
) {
450 int i
= end
- FRAG_CB(next
)->offset
; /* overlap is 'i' bytes */
453 /* Eat head of the next overlapped fragment
454 * and leave the loop. The next ones cannot overlap.
456 FRAG_CB(next
)->offset
+= i
;
459 if (next
->ip_summed
!= CHECKSUM_UNNECESSARY
)
460 next
->ip_summed
= CHECKSUM_NONE
;
463 struct sk_buff
*free_it
= next
;
465 /* Old fragmnet is completely overridden with
473 qp
->fragments
= next
;
475 qp
->meat
-= free_it
->len
;
476 frag_kfree_skb(free_it
);
480 FRAG_CB(skb
)->offset
= offset
;
482 /* Insert this fragment in the chain of fragments. */
490 qp
->iif
= skb
->dev
->ifindex
;
492 qp
->meat
+= skb
->len
;
493 atomic_add(skb
->truesize
, &ip_frag_mem
);
495 qp
->last_in
|= FIRST_IN
;
504 /* Build a new IP datagram from all its fragments.
506 * FIXME: We copy here because we lack an effective way of handling lists
507 * of bits on input. Until the new skb data handling is in I'm not going
508 * to touch this with a bargepole.
510 static struct sk_buff
*ip_frag_reasm(struct ipq
*qp
, struct net_device
*dev
)
514 struct sk_buff
*fp
, *head
= qp
->fragments
;
520 BUG_TRAP(head
!= NULL
);
521 BUG_TRAP(FRAG_CB(head
)->offset
== 0);
523 /* Allocate a new buffer for the datagram. */
524 ihlen
= head
->nh
.iph
->ihl
*4;
525 len
= ihlen
+ qp
->len
;
530 skb
= dev_alloc_skb(len
);
534 /* Fill in the basic details. */
535 skb
->mac
.raw
= skb
->data
;
536 skb
->nh
.raw
= skb
->data
;
537 FRAG_CB(skb
)->h
= FRAG_CB(head
)->h
;
538 skb
->ip_summed
= head
->ip_summed
;
541 /* Copy the original IP headers into the new buffer. */
542 memcpy(skb_put(skb
, ihlen
), head
->nh
.iph
, ihlen
);
544 /* Copy the data portions of all fragments into the new buffer. */
545 for (fp
=head
; fp
; fp
= fp
->next
) {
546 memcpy(skb_put(skb
, fp
->len
), fp
->data
, fp
->len
);
548 if (skb
->ip_summed
!= fp
->ip_summed
)
549 skb
->ip_summed
= CHECKSUM_NONE
;
550 else if (skb
->ip_summed
== CHECKSUM_HW
)
551 skb
->csum
= csum_add(skb
->csum
, fp
->csum
);
554 skb
->dst
= dst_clone(head
->dst
);
555 skb
->pkt_type
= head
->pkt_type
;
556 skb
->protocol
= head
->protocol
;
560 * Clearly bogus, because security markings of the individual
561 * fragments should have been checked for consistency before
562 * gluing, and intermediate coalescing of fragments may have
563 * taken place in ip_defrag() before ip_glue() ever got called.
564 * If we're not going to do the consistency checking, we might
565 * as well take the value associated with the first fragment.
568 skb
->security
= head
->security
;
570 #ifdef CONFIG_NETFILTER
571 /* Connection association is same as fragment (if any). */
572 skb
->nfct
= head
->nfct
;
573 nf_conntrack_get(skb
->nfct
);
574 #ifdef CONFIG_NETFILTER_DEBUG
575 skb
->nf_debug
= head
->nf_debug
;
579 /* Done with all fragments. Fixup the new IP header. */
582 iph
->tot_len
= htons(len
);
583 IP_INC_STATS_BH(IpReasmOKs
);
587 NETDEBUG(printk(KERN_ERR
588 "IP: queue_glue: no memory for gluing queue %p\n",
594 "Oversized IP packet from %d.%d.%d.%d.\n",
597 IP_INC_STATS_BH(IpReasmFails
);
601 /* Process an incoming IP datagram fragment. */
602 struct sk_buff
*ip_defrag(struct sk_buff
*skb
)
604 struct iphdr
*iph
= skb
->nh
.iph
;
606 struct net_device
*dev
;
608 IP_INC_STATS_BH(IpReasmReqds
);
610 /* Start by cleaning up the memory. */
611 if (atomic_read(&ip_frag_mem
) > sysctl_ipfrag_high_thresh
)
616 /* Lookup (or create) queue header */
617 if ((qp
= ip_find(iph
)) != NULL
) {
618 struct sk_buff
*ret
= NULL
;
620 spin_lock(&qp
->lock
);
622 ip_frag_queue(qp
, skb
);
624 if (qp
->last_in
== (FIRST_IN
|LAST_IN
) &&
626 ret
= ip_frag_reasm(qp
, dev
);
628 spin_unlock(&qp
->lock
);
633 IP_INC_STATS_BH(IpReasmFails
);