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.59 2002/01/12 07:54:56 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.
22 * Patrick McHardy : LRU queue of frag heads for evictor.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
29 #include <linux/jiffies.h>
30 #include <linux/skbuff.h>
31 #include <linux/list.h>
33 #include <linux/icmp.h>
34 #include <linux/netdevice.h>
35 #include <linux/jhash.h>
36 #include <linux/random.h>
40 #include <net/checksum.h>
41 #include <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/inet.h>
44 #include <linux/netfilter_ipv4.h>
46 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
47 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
48 * as well. Or notify me, at least. --ANK
51 /* Fragment cache limits. We will commit 256K at one time. Should we
52 * cross that limit we will prune down to 192K. This should cope with
53 * even the most extreme cases without allowing an attacker to measurably
54 * harm machine performance.
56 int sysctl_ipfrag_high_thresh
= 256*1024;
57 int sysctl_ipfrag_low_thresh
= 192*1024;
59 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
60 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
62 int sysctl_ipfrag_time
= IP_FRAG_TIME
;
66 struct inet_skb_parm h
;
70 #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
72 /* Describe an entry in the "incomplete datagrams" queue. */
74 struct hlist_node list
;
75 struct list_head lru_list
; /* lru list member */
86 struct sk_buff
*fragments
; /* linked list of received fragments */
87 int len
; /* total length of original datagram */
91 struct timer_list timer
; /* when will this queue expire? */
100 /* Per-bucket lock is easy to add now. */
101 static struct hlist_head ipq_hash
[IPQ_HASHSZ
];
102 static DEFINE_RWLOCK(ipfrag_lock
);
103 static u32 ipfrag_hash_rnd
;
104 static LIST_HEAD(ipq_lru_list
);
105 int ip_frag_nqueues
= 0;
107 static __inline__
void __ipq_unlink(struct ipq
*qp
)
109 hlist_del(&qp
->list
);
110 list_del(&qp
->lru_list
);
114 static __inline__
void ipq_unlink(struct ipq
*ipq
)
116 write_lock(&ipfrag_lock
);
118 write_unlock(&ipfrag_lock
);
121 static unsigned int ipqhashfn(u16 id
, u32 saddr
, u32 daddr
, u8 prot
)
123 return jhash_3words((u32
)id
<< 16 | prot
, saddr
, daddr
,
124 ipfrag_hash_rnd
) & (IPQ_HASHSZ
- 1);
127 static struct timer_list ipfrag_secret_timer
;
128 int sysctl_ipfrag_secret_interval
= 10 * 60 * HZ
;
130 static void ipfrag_secret_rebuild(unsigned long dummy
)
132 unsigned long now
= jiffies
;
135 write_lock(&ipfrag_lock
);
136 get_random_bytes(&ipfrag_hash_rnd
, sizeof(u32
));
137 for (i
= 0; i
< IPQ_HASHSZ
; i
++) {
139 struct hlist_node
*p
, *n
;
141 hlist_for_each_entry_safe(q
, p
, n
, &ipq_hash
[i
], list
) {
142 unsigned int hval
= ipqhashfn(q
->id
, q
->saddr
,
143 q
->daddr
, q
->protocol
);
148 /* Relink to new hash chain. */
149 hlist_add_head(&q
->list
, &ipq_hash
[hval
]);
153 write_unlock(&ipfrag_lock
);
155 mod_timer(&ipfrag_secret_timer
, now
+ sysctl_ipfrag_secret_interval
);
158 atomic_t ip_frag_mem
= ATOMIC_INIT(0); /* Memory used for fragments */
160 /* Memory Tracking Functions. */
161 static __inline__
void frag_kfree_skb(struct sk_buff
*skb
, int *work
)
164 *work
-= skb
->truesize
;
165 atomic_sub(skb
->truesize
, &ip_frag_mem
);
169 static __inline__
void frag_free_queue(struct ipq
*qp
, int *work
)
172 *work
-= sizeof(struct ipq
);
173 atomic_sub(sizeof(struct ipq
), &ip_frag_mem
);
177 static __inline__
struct ipq
*frag_alloc_queue(void)
179 struct ipq
*qp
= kmalloc(sizeof(struct ipq
), GFP_ATOMIC
);
183 atomic_add(sizeof(struct ipq
), &ip_frag_mem
);
188 /* Destruction primitives. */
190 /* Complete destruction of ipq. */
191 static void ip_frag_destroy(struct ipq
*qp
, int *work
)
195 BUG_TRAP(qp
->last_in
&COMPLETE
);
196 BUG_TRAP(del_timer(&qp
->timer
) == 0);
198 /* Release all fragment data. */
201 struct sk_buff
*xp
= fp
->next
;
203 frag_kfree_skb(fp
, work
);
207 /* Finally, release the queue descriptor itself. */
208 frag_free_queue(qp
, work
);
211 static __inline__
void ipq_put(struct ipq
*ipq
, int *work
)
213 if (atomic_dec_and_test(&ipq
->refcnt
))
214 ip_frag_destroy(ipq
, work
);
217 /* Kill ipq entry. It is not destroyed immediately,
218 * because caller (and someone more) holds reference count.
220 static void ipq_kill(struct ipq
*ipq
)
222 if (del_timer(&ipq
->timer
))
223 atomic_dec(&ipq
->refcnt
);
225 if (!(ipq
->last_in
& COMPLETE
)) {
227 atomic_dec(&ipq
->refcnt
);
228 ipq
->last_in
|= COMPLETE
;
232 /* Memory limiting on fragments. Evictor trashes the oldest
233 * fragment queue until we are back under the threshold.
235 static void ip_evictor(void)
238 struct list_head
*tmp
;
241 work
= atomic_read(&ip_frag_mem
) - sysctl_ipfrag_low_thresh
;
246 read_lock(&ipfrag_lock
);
247 if (list_empty(&ipq_lru_list
)) {
248 read_unlock(&ipfrag_lock
);
251 tmp
= ipq_lru_list
.next
;
252 qp
= list_entry(tmp
, struct ipq
, lru_list
);
253 atomic_inc(&qp
->refcnt
);
254 read_unlock(&ipfrag_lock
);
256 spin_lock(&qp
->lock
);
257 if (!(qp
->last_in
&COMPLETE
))
259 spin_unlock(&qp
->lock
);
262 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
267 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
269 static void ip_expire(unsigned long arg
)
271 struct ipq
*qp
= (struct ipq
*) arg
;
273 spin_lock(&qp
->lock
);
275 if (qp
->last_in
& COMPLETE
)
280 IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT
);
281 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
283 if ((qp
->last_in
&FIRST_IN
) && qp
->fragments
!= NULL
) {
284 struct sk_buff
*head
= qp
->fragments
;
285 /* Send an ICMP "Fragment Reassembly Timeout" message. */
286 if ((head
->dev
= dev_get_by_index(qp
->iif
)) != NULL
) {
287 icmp_send(head
, ICMP_TIME_EXCEEDED
, ICMP_EXC_FRAGTIME
, 0);
292 spin_unlock(&qp
->lock
);
296 /* Creation primitives. */
298 static struct ipq
*ip_frag_intern(unsigned int hash
, struct ipq
*qp_in
)
302 struct hlist_node
*n
;
304 write_lock(&ipfrag_lock
);
306 /* With SMP race we have to recheck hash table, because
307 * such entry could be created on other cpu, while we
308 * promoted read lock to write lock.
310 hlist_for_each_entry(qp
, n
, &ipq_hash
[hash
], list
) {
311 if(qp
->id
== qp_in
->id
&&
312 qp
->saddr
== qp_in
->saddr
&&
313 qp
->daddr
== qp_in
->daddr
&&
314 qp
->protocol
== qp_in
->protocol
&&
315 qp
->user
== qp_in
->user
) {
316 atomic_inc(&qp
->refcnt
);
317 write_unlock(&ipfrag_lock
);
318 qp_in
->last_in
|= COMPLETE
;
319 ipq_put(qp_in
, NULL
);
326 if (!mod_timer(&qp
->timer
, jiffies
+ sysctl_ipfrag_time
))
327 atomic_inc(&qp
->refcnt
);
329 atomic_inc(&qp
->refcnt
);
330 hlist_add_head(&qp
->list
, &ipq_hash
[hash
]);
331 INIT_LIST_HEAD(&qp
->lru_list
);
332 list_add_tail(&qp
->lru_list
, &ipq_lru_list
);
334 write_unlock(&ipfrag_lock
);
338 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
339 static struct ipq
*ip_frag_create(unsigned hash
, struct iphdr
*iph
, u32 user
)
343 if ((qp
= frag_alloc_queue()) == NULL
)
346 qp
->protocol
= iph
->protocol
;
349 qp
->saddr
= iph
->saddr
;
350 qp
->daddr
= iph
->daddr
;
354 qp
->fragments
= NULL
;
357 /* Initialize a timer for this entry. */
358 init_timer(&qp
->timer
);
359 qp
->timer
.data
= (unsigned long) qp
; /* pointer to queue */
360 qp
->timer
.function
= ip_expire
; /* expire function */
361 spin_lock_init(&qp
->lock
);
362 atomic_set(&qp
->refcnt
, 1);
364 return ip_frag_intern(hash
, qp
);
367 LIMIT_NETDEBUG(KERN_ERR
"ip_frag_create: no memory left !\n");
371 /* Find the correct entry in the "incomplete datagrams" queue for
372 * this IP datagram, and create new one, if nothing is found.
374 static inline struct ipq
*ip_find(struct iphdr
*iph
, u32 user
)
377 __u32 saddr
= iph
->saddr
;
378 __u32 daddr
= iph
->daddr
;
379 __u8 protocol
= iph
->protocol
;
380 unsigned int hash
= ipqhashfn(id
, saddr
, daddr
, protocol
);
382 struct hlist_node
*n
;
384 read_lock(&ipfrag_lock
);
385 hlist_for_each_entry(qp
, n
, &ipq_hash
[hash
], list
) {
387 qp
->saddr
== saddr
&&
388 qp
->daddr
== daddr
&&
389 qp
->protocol
== protocol
&&
391 atomic_inc(&qp
->refcnt
);
392 read_unlock(&ipfrag_lock
);
396 read_unlock(&ipfrag_lock
);
398 return ip_frag_create(hash
, iph
, user
);
401 /* Add new segment to existing queue. */
402 static void ip_frag_queue(struct ipq
*qp
, struct sk_buff
*skb
)
404 struct sk_buff
*prev
, *next
;
408 if (qp
->last_in
& COMPLETE
)
411 offset
= ntohs(skb
->nh
.iph
->frag_off
);
412 flags
= offset
& ~IP_OFFSET
;
414 offset
<<= 3; /* offset is in 8-byte chunks */
415 ihl
= skb
->nh
.iph
->ihl
* 4;
417 /* Determine the position of this fragment. */
418 end
= offset
+ skb
->len
- ihl
;
420 /* Is this the final fragment? */
421 if ((flags
& IP_MF
) == 0) {
422 /* If we already have some bits beyond end
423 * or have different end, the segment is corrrupted.
426 ((qp
->last_in
& LAST_IN
) && end
!= qp
->len
))
428 qp
->last_in
|= LAST_IN
;
433 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
434 skb
->ip_summed
= CHECKSUM_NONE
;
437 /* Some bits beyond end -> corruption. */
438 if (qp
->last_in
& LAST_IN
)
446 if (pskb_pull(skb
, ihl
) == NULL
)
448 if (pskb_trim_rcsum(skb
, end
-offset
))
451 /* Find out which fragments are in front and at the back of us
452 * in the chain of fragments so far. We must know where to put
453 * this fragment, right?
456 for(next
= qp
->fragments
; next
!= NULL
; next
= next
->next
) {
457 if (FRAG_CB(next
)->offset
>= offset
)
462 /* We found where to put this one. Check for overlap with
463 * preceding fragment, and, if needed, align things so that
464 * any overlaps are eliminated.
467 int i
= (FRAG_CB(prev
)->offset
+ prev
->len
) - offset
;
473 if (!pskb_pull(skb
, i
))
475 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
476 skb
->ip_summed
= CHECKSUM_NONE
;
480 while (next
&& FRAG_CB(next
)->offset
< end
) {
481 int i
= end
- FRAG_CB(next
)->offset
; /* overlap is 'i' bytes */
484 /* Eat head of the next overlapped fragment
485 * and leave the loop. The next ones cannot overlap.
487 if (!pskb_pull(next
, i
))
489 FRAG_CB(next
)->offset
+= i
;
491 if (next
->ip_summed
!= CHECKSUM_UNNECESSARY
)
492 next
->ip_summed
= CHECKSUM_NONE
;
495 struct sk_buff
*free_it
= next
;
497 /* Old fragmnet is completely overridden with
505 qp
->fragments
= next
;
507 qp
->meat
-= free_it
->len
;
508 frag_kfree_skb(free_it
, NULL
);
512 FRAG_CB(skb
)->offset
= offset
;
514 /* Insert this fragment in the chain of fragments. */
522 qp
->iif
= skb
->dev
->ifindex
;
524 skb_get_timestamp(skb
, &qp
->stamp
);
525 qp
->meat
+= skb
->len
;
526 atomic_add(skb
->truesize
, &ip_frag_mem
);
528 qp
->last_in
|= FIRST_IN
;
530 write_lock(&ipfrag_lock
);
531 list_move_tail(&qp
->lru_list
, &ipq_lru_list
);
532 write_unlock(&ipfrag_lock
);
541 /* Build a new IP datagram from all its fragments. */
543 static struct sk_buff
*ip_frag_reasm(struct ipq
*qp
, struct net_device
*dev
)
546 struct sk_buff
*fp
, *head
= qp
->fragments
;
552 BUG_TRAP(head
!= NULL
);
553 BUG_TRAP(FRAG_CB(head
)->offset
== 0);
555 /* Allocate a new buffer for the datagram. */
556 ihlen
= head
->nh
.iph
->ihl
*4;
557 len
= ihlen
+ qp
->len
;
562 /* Head of list must not be cloned. */
563 if (skb_cloned(head
) && pskb_expand_head(head
, 0, 0, GFP_ATOMIC
))
566 /* If the first fragment is fragmented itself, we split
567 * it to two chunks: the first with data and paged part
568 * and the second, holding only fragments. */
569 if (skb_shinfo(head
)->frag_list
) {
570 struct sk_buff
*clone
;
573 if ((clone
= alloc_skb(0, GFP_ATOMIC
)) == NULL
)
575 clone
->next
= head
->next
;
577 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
578 skb_shinfo(head
)->frag_list
= NULL
;
579 for (i
=0; i
<skb_shinfo(head
)->nr_frags
; i
++)
580 plen
+= skb_shinfo(head
)->frags
[i
].size
;
581 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
582 head
->data_len
-= clone
->len
;
583 head
->len
-= clone
->len
;
585 clone
->ip_summed
= head
->ip_summed
;
586 atomic_add(clone
->truesize
, &ip_frag_mem
);
589 skb_shinfo(head
)->frag_list
= head
->next
;
590 skb_push(head
, head
->data
- head
->nh
.raw
);
591 atomic_sub(head
->truesize
, &ip_frag_mem
);
593 for (fp
=head
->next
; fp
; fp
= fp
->next
) {
594 head
->data_len
+= fp
->len
;
595 head
->len
+= fp
->len
;
596 if (head
->ip_summed
!= fp
->ip_summed
)
597 head
->ip_summed
= CHECKSUM_NONE
;
598 else if (head
->ip_summed
== CHECKSUM_HW
)
599 head
->csum
= csum_add(head
->csum
, fp
->csum
);
600 head
->truesize
+= fp
->truesize
;
601 atomic_sub(fp
->truesize
, &ip_frag_mem
);
606 skb_set_timestamp(head
, &qp
->stamp
);
610 iph
->tot_len
= htons(len
);
611 IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS
);
612 qp
->fragments
= NULL
;
616 LIMIT_NETDEBUG(KERN_ERR
"IP: queue_glue: no memory for gluing "
622 "Oversized IP packet from %d.%d.%d.%d.\n",
625 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
629 /* Process an incoming IP datagram fragment. */
630 struct sk_buff
*ip_defrag(struct sk_buff
*skb
, u32 user
)
632 struct iphdr
*iph
= skb
->nh
.iph
;
634 struct net_device
*dev
;
636 IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS
);
638 /* Start by cleaning up the memory. */
639 if (atomic_read(&ip_frag_mem
) > sysctl_ipfrag_high_thresh
)
644 /* Lookup (or create) queue header */
645 if ((qp
= ip_find(iph
, user
)) != NULL
) {
646 struct sk_buff
*ret
= NULL
;
648 spin_lock(&qp
->lock
);
650 ip_frag_queue(qp
, skb
);
652 if (qp
->last_in
== (FIRST_IN
|LAST_IN
) &&
654 ret
= ip_frag_reasm(qp
, dev
);
656 spin_unlock(&qp
->lock
);
661 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
666 void ipfrag_init(void)
668 ipfrag_hash_rnd
= (u32
) ((num_physpages
^ (num_physpages
>>7)) ^
669 (jiffies
^ (jiffies
>> 6)));
671 init_timer(&ipfrag_secret_timer
);
672 ipfrag_secret_timer
.function
= ipfrag_secret_rebuild
;
673 ipfrag_secret_timer
.expires
= jiffies
+ sysctl_ipfrag_secret_interval
;
674 add_timer(&ipfrag_secret_timer
);
677 EXPORT_SYMBOL(ip_defrag
);