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/compiler.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 <net/inetpeer.h>
42 #include <linux/tcp.h>
43 #include <linux/udp.h>
44 #include <linux/inet.h>
45 #include <linux/netfilter_ipv4.h>
47 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
48 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
49 * as well. Or notify me, at least. --ANK
52 /* Fragment cache limits. We will commit 256K at one time. Should we
53 * cross that limit we will prune down to 192K. This should cope with
54 * even the most extreme cases without allowing an attacker to measurably
55 * harm machine performance.
57 int sysctl_ipfrag_high_thresh __read_mostly
= 256*1024;
58 int sysctl_ipfrag_low_thresh __read_mostly
= 192*1024;
60 int sysctl_ipfrag_max_dist __read_mostly
= 64;
62 /* Important NOTE! Fragment queue must be destroyed before MSL expires.
63 * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL.
65 int sysctl_ipfrag_time __read_mostly
= IP_FRAG_TIME
;
69 struct inet_skb_parm h
;
73 #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb))
75 /* Describe an entry in the "incomplete datagrams" queue. */
77 struct hlist_node list
;
78 struct list_head lru_list
; /* lru list member */
89 struct sk_buff
*fragments
; /* linked list of received fragments */
90 int len
; /* total length of original datagram */
94 struct timer_list timer
; /* when will this queue expire? */
98 struct inet_peer
*peer
;
103 #define IPQ_HASHSZ 64
105 /* Per-bucket lock is easy to add now. */
106 static struct hlist_head ipq_hash
[IPQ_HASHSZ
];
107 static DEFINE_RWLOCK(ipfrag_lock
);
108 static u32 ipfrag_hash_rnd
;
109 static LIST_HEAD(ipq_lru_list
);
110 int ip_frag_nqueues
= 0;
112 static int ip_frag_reasm(struct ipq
*qp
, struct sk_buff
*prev
,
113 struct net_device
*dev
);
115 static __inline__
void __ipq_unlink(struct ipq
*qp
)
117 hlist_del(&qp
->list
);
118 list_del(&qp
->lru_list
);
122 static __inline__
void ipq_unlink(struct ipq
*ipq
)
124 write_lock(&ipfrag_lock
);
126 write_unlock(&ipfrag_lock
);
129 static unsigned int ipqhashfn(__be16 id
, __be32 saddr
, __be32 daddr
, u8 prot
)
131 return jhash_3words((__force u32
)id
<< 16 | prot
,
132 (__force u32
)saddr
, (__force u32
)daddr
,
133 ipfrag_hash_rnd
) & (IPQ_HASHSZ
- 1);
136 static struct timer_list ipfrag_secret_timer
;
137 int sysctl_ipfrag_secret_interval __read_mostly
= 10 * 60 * HZ
;
139 static void ipfrag_secret_rebuild(unsigned long dummy
)
141 unsigned long now
= jiffies
;
144 write_lock(&ipfrag_lock
);
145 get_random_bytes(&ipfrag_hash_rnd
, sizeof(u32
));
146 for (i
= 0; i
< IPQ_HASHSZ
; i
++) {
148 struct hlist_node
*p
, *n
;
150 hlist_for_each_entry_safe(q
, p
, n
, &ipq_hash
[i
], list
) {
151 unsigned int hval
= ipqhashfn(q
->id
, q
->saddr
,
152 q
->daddr
, q
->protocol
);
157 /* Relink to new hash chain. */
158 hlist_add_head(&q
->list
, &ipq_hash
[hval
]);
162 write_unlock(&ipfrag_lock
);
164 mod_timer(&ipfrag_secret_timer
, now
+ sysctl_ipfrag_secret_interval
);
167 atomic_t ip_frag_mem
= ATOMIC_INIT(0); /* Memory used for fragments */
169 /* Memory Tracking Functions. */
170 static __inline__
void frag_kfree_skb(struct sk_buff
*skb
, int *work
)
173 *work
-= skb
->truesize
;
174 atomic_sub(skb
->truesize
, &ip_frag_mem
);
178 static __inline__
void frag_free_queue(struct ipq
*qp
, int *work
)
181 *work
-= sizeof(struct ipq
);
182 atomic_sub(sizeof(struct ipq
), &ip_frag_mem
);
186 static __inline__
struct ipq
*frag_alloc_queue(void)
188 struct ipq
*qp
= kzalloc(sizeof(struct ipq
), GFP_ATOMIC
);
192 atomic_add(sizeof(struct ipq
), &ip_frag_mem
);
197 /* Destruction primitives. */
199 /* Complete destruction of ipq. */
200 static void ip_frag_destroy(struct ipq
*qp
, int *work
)
204 BUG_TRAP(qp
->last_in
&COMPLETE
);
205 BUG_TRAP(del_timer(&qp
->timer
) == 0);
208 inet_putpeer(qp
->peer
);
210 /* Release all fragment data. */
213 struct sk_buff
*xp
= fp
->next
;
215 frag_kfree_skb(fp
, work
);
219 /* Finally, release the queue descriptor itself. */
220 frag_free_queue(qp
, work
);
223 static __inline__
void ipq_put(struct ipq
*ipq
, int *work
)
225 if (atomic_dec_and_test(&ipq
->refcnt
))
226 ip_frag_destroy(ipq
, work
);
229 /* Kill ipq entry. It is not destroyed immediately,
230 * because caller (and someone more) holds reference count.
232 static void ipq_kill(struct ipq
*ipq
)
234 if (del_timer(&ipq
->timer
))
235 atomic_dec(&ipq
->refcnt
);
237 if (!(ipq
->last_in
& COMPLETE
)) {
239 atomic_dec(&ipq
->refcnt
);
240 ipq
->last_in
|= COMPLETE
;
244 /* Memory limiting on fragments. Evictor trashes the oldest
245 * fragment queue until we are back under the threshold.
247 static void ip_evictor(void)
250 struct list_head
*tmp
;
253 work
= atomic_read(&ip_frag_mem
) - sysctl_ipfrag_low_thresh
;
258 read_lock(&ipfrag_lock
);
259 if (list_empty(&ipq_lru_list
)) {
260 read_unlock(&ipfrag_lock
);
263 tmp
= ipq_lru_list
.next
;
264 qp
= list_entry(tmp
, struct ipq
, lru_list
);
265 atomic_inc(&qp
->refcnt
);
266 read_unlock(&ipfrag_lock
);
268 spin_lock(&qp
->lock
);
269 if (!(qp
->last_in
&COMPLETE
))
271 spin_unlock(&qp
->lock
);
274 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
279 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
281 static void ip_expire(unsigned long arg
)
283 struct ipq
*qp
= (struct ipq
*) arg
;
285 spin_lock(&qp
->lock
);
287 if (qp
->last_in
& COMPLETE
)
292 IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT
);
293 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
295 if ((qp
->last_in
&FIRST_IN
) && qp
->fragments
!= NULL
) {
296 struct sk_buff
*head
= qp
->fragments
;
297 /* Send an ICMP "Fragment Reassembly Timeout" message. */
298 if ((head
->dev
= dev_get_by_index(qp
->iif
)) != NULL
) {
299 icmp_send(head
, ICMP_TIME_EXCEEDED
, ICMP_EXC_FRAGTIME
, 0);
304 spin_unlock(&qp
->lock
);
308 /* Creation primitives. */
310 static struct ipq
*ip_frag_intern(struct ipq
*qp_in
)
314 struct hlist_node
*n
;
318 write_lock(&ipfrag_lock
);
319 hash
= ipqhashfn(qp_in
->id
, qp_in
->saddr
, qp_in
->daddr
,
322 /* With SMP race we have to recheck hash table, because
323 * such entry could be created on other cpu, while we
324 * promoted read lock to write lock.
326 hlist_for_each_entry(qp
, n
, &ipq_hash
[hash
], list
) {
327 if (qp
->id
== qp_in
->id
&&
328 qp
->saddr
== qp_in
->saddr
&&
329 qp
->daddr
== qp_in
->daddr
&&
330 qp
->protocol
== qp_in
->protocol
&&
331 qp
->user
== qp_in
->user
) {
332 atomic_inc(&qp
->refcnt
);
333 write_unlock(&ipfrag_lock
);
334 qp_in
->last_in
|= COMPLETE
;
335 ipq_put(qp_in
, NULL
);
342 if (!mod_timer(&qp
->timer
, jiffies
+ sysctl_ipfrag_time
))
343 atomic_inc(&qp
->refcnt
);
345 atomic_inc(&qp
->refcnt
);
346 hlist_add_head(&qp
->list
, &ipq_hash
[hash
]);
347 INIT_LIST_HEAD(&qp
->lru_list
);
348 list_add_tail(&qp
->lru_list
, &ipq_lru_list
);
350 write_unlock(&ipfrag_lock
);
354 /* Add an entry to the 'ipq' queue for a newly received IP datagram. */
355 static struct ipq
*ip_frag_create(struct iphdr
*iph
, u32 user
)
359 if ((qp
= frag_alloc_queue()) == NULL
)
362 qp
->protocol
= iph
->protocol
;
364 qp
->saddr
= iph
->saddr
;
365 qp
->daddr
= iph
->daddr
;
367 qp
->peer
= sysctl_ipfrag_max_dist
? inet_getpeer(iph
->saddr
, 1) : NULL
;
369 /* Initialize a timer for this entry. */
370 init_timer(&qp
->timer
);
371 qp
->timer
.data
= (unsigned long) qp
; /* pointer to queue */
372 qp
->timer
.function
= ip_expire
; /* expire function */
373 spin_lock_init(&qp
->lock
);
374 atomic_set(&qp
->refcnt
, 1);
376 return ip_frag_intern(qp
);
379 LIMIT_NETDEBUG(KERN_ERR
"ip_frag_create: no memory left !\n");
383 /* Find the correct entry in the "incomplete datagrams" queue for
384 * this IP datagram, and create new one, if nothing is found.
386 static inline struct ipq
*ip_find(struct iphdr
*iph
, u32 user
)
389 __be32 saddr
= iph
->saddr
;
390 __be32 daddr
= iph
->daddr
;
391 __u8 protocol
= iph
->protocol
;
394 struct hlist_node
*n
;
396 read_lock(&ipfrag_lock
);
397 hash
= ipqhashfn(id
, saddr
, daddr
, protocol
);
398 hlist_for_each_entry(qp
, n
, &ipq_hash
[hash
], list
) {
400 qp
->saddr
== saddr
&&
401 qp
->daddr
== daddr
&&
402 qp
->protocol
== protocol
&&
404 atomic_inc(&qp
->refcnt
);
405 read_unlock(&ipfrag_lock
);
409 read_unlock(&ipfrag_lock
);
411 return ip_frag_create(iph
, user
);
414 /* Is the fragment too far ahead to be part of ipq? */
415 static inline int ip_frag_too_far(struct ipq
*qp
)
417 struct inet_peer
*peer
= qp
->peer
;
418 unsigned int max
= sysctl_ipfrag_max_dist
;
419 unsigned int start
, end
;
427 end
= atomic_inc_return(&peer
->rid
);
430 rc
= qp
->fragments
&& (end
- start
) > max
;
433 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
439 static int ip_frag_reinit(struct ipq
*qp
)
443 if (!mod_timer(&qp
->timer
, jiffies
+ sysctl_ipfrag_time
)) {
444 atomic_inc(&qp
->refcnt
);
450 struct sk_buff
*xp
= fp
->next
;
451 frag_kfree_skb(fp
, NULL
);
458 qp
->fragments
= NULL
;
464 /* Add new segment to existing queue. */
465 static int ip_frag_queue(struct ipq
*qp
, struct sk_buff
*skb
)
467 struct sk_buff
*prev
, *next
;
468 struct net_device
*dev
;
473 if (qp
->last_in
& COMPLETE
)
476 if (!(IPCB(skb
)->flags
& IPSKB_FRAG_COMPLETE
) &&
477 unlikely(ip_frag_too_far(qp
)) &&
478 unlikely(err
= ip_frag_reinit(qp
))) {
483 offset
= ntohs(ip_hdr(skb
)->frag_off
);
484 flags
= offset
& ~IP_OFFSET
;
486 offset
<<= 3; /* offset is in 8-byte chunks */
487 ihl
= ip_hdrlen(skb
);
489 /* Determine the position of this fragment. */
490 end
= offset
+ skb
->len
- ihl
;
493 /* Is this the final fragment? */
494 if ((flags
& IP_MF
) == 0) {
495 /* If we already have some bits beyond end
496 * or have different end, the segment is corrrupted.
499 ((qp
->last_in
& LAST_IN
) && end
!= qp
->len
))
501 qp
->last_in
|= LAST_IN
;
506 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
507 skb
->ip_summed
= CHECKSUM_NONE
;
510 /* Some bits beyond end -> corruption. */
511 if (qp
->last_in
& LAST_IN
)
520 if (pskb_pull(skb
, ihl
) == NULL
)
523 err
= pskb_trim_rcsum(skb
, end
- offset
);
527 /* Find out which fragments are in front and at the back of us
528 * in the chain of fragments so far. We must know where to put
529 * this fragment, right?
532 for (next
= qp
->fragments
; next
!= NULL
; next
= next
->next
) {
533 if (FRAG_CB(next
)->offset
>= offset
)
538 /* We found where to put this one. Check for overlap with
539 * preceding fragment, and, if needed, align things so that
540 * any overlaps are eliminated.
543 int i
= (FRAG_CB(prev
)->offset
+ prev
->len
) - offset
;
551 if (!pskb_pull(skb
, i
))
553 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
554 skb
->ip_summed
= CHECKSUM_NONE
;
560 while (next
&& FRAG_CB(next
)->offset
< end
) {
561 int i
= end
- FRAG_CB(next
)->offset
; /* overlap is 'i' bytes */
564 /* Eat head of the next overlapped fragment
565 * and leave the loop. The next ones cannot overlap.
567 if (!pskb_pull(next
, i
))
569 FRAG_CB(next
)->offset
+= i
;
571 if (next
->ip_summed
!= CHECKSUM_UNNECESSARY
)
572 next
->ip_summed
= CHECKSUM_NONE
;
575 struct sk_buff
*free_it
= next
;
577 /* Old fragment is completely overridden with
585 qp
->fragments
= next
;
587 qp
->meat
-= free_it
->len
;
588 frag_kfree_skb(free_it
, NULL
);
592 FRAG_CB(skb
)->offset
= offset
;
594 /* Insert this fragment in the chain of fragments. */
603 qp
->iif
= dev
->ifindex
;
606 qp
->stamp
= skb
->tstamp
;
607 qp
->meat
+= skb
->len
;
608 atomic_add(skb
->truesize
, &ip_frag_mem
);
610 qp
->last_in
|= FIRST_IN
;
612 if (qp
->last_in
== (FIRST_IN
| LAST_IN
) && qp
->meat
== qp
->len
)
613 return ip_frag_reasm(qp
, prev
, dev
);
615 write_lock(&ipfrag_lock
);
616 list_move_tail(&qp
->lru_list
, &ipq_lru_list
);
617 write_unlock(&ipfrag_lock
);
626 /* Build a new IP datagram from all its fragments. */
628 static int ip_frag_reasm(struct ipq
*qp
, struct sk_buff
*prev
,
629 struct net_device
*dev
)
632 struct sk_buff
*fp
, *head
= qp
->fragments
;
639 /* Make the one we just received the head. */
642 fp
= skb_clone(head
, GFP_ATOMIC
);
647 fp
->next
= head
->next
;
650 skb_morph(head
, qp
->fragments
);
651 head
->next
= qp
->fragments
->next
;
653 kfree_skb(qp
->fragments
);
654 qp
->fragments
= head
;
657 BUG_TRAP(head
!= NULL
);
658 BUG_TRAP(FRAG_CB(head
)->offset
== 0);
660 /* Allocate a new buffer for the datagram. */
661 ihlen
= ip_hdrlen(head
);
662 len
= ihlen
+ qp
->len
;
668 /* Head of list must not be cloned. */
670 if (skb_cloned(head
) && pskb_expand_head(head
, 0, 0, GFP_ATOMIC
))
673 /* If the first fragment is fragmented itself, we split
674 * it to two chunks: the first with data and paged part
675 * and the second, holding only fragments. */
676 if (skb_shinfo(head
)->frag_list
) {
677 struct sk_buff
*clone
;
680 if ((clone
= alloc_skb(0, GFP_ATOMIC
)) == NULL
)
682 clone
->next
= head
->next
;
684 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
685 skb_shinfo(head
)->frag_list
= NULL
;
686 for (i
=0; i
<skb_shinfo(head
)->nr_frags
; i
++)
687 plen
+= skb_shinfo(head
)->frags
[i
].size
;
688 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
689 head
->data_len
-= clone
->len
;
690 head
->len
-= clone
->len
;
692 clone
->ip_summed
= head
->ip_summed
;
693 atomic_add(clone
->truesize
, &ip_frag_mem
);
696 skb_shinfo(head
)->frag_list
= head
->next
;
697 skb_push(head
, head
->data
- skb_network_header(head
));
698 atomic_sub(head
->truesize
, &ip_frag_mem
);
700 for (fp
=head
->next
; fp
; fp
= fp
->next
) {
701 head
->data_len
+= fp
->len
;
702 head
->len
+= fp
->len
;
703 if (head
->ip_summed
!= fp
->ip_summed
)
704 head
->ip_summed
= CHECKSUM_NONE
;
705 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
706 head
->csum
= csum_add(head
->csum
, fp
->csum
);
707 head
->truesize
+= fp
->truesize
;
708 atomic_sub(fp
->truesize
, &ip_frag_mem
);
713 head
->tstamp
= qp
->stamp
;
717 iph
->tot_len
= htons(len
);
718 IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS
);
719 qp
->fragments
= NULL
;
723 LIMIT_NETDEBUG(KERN_ERR
"IP: queue_glue: no memory for gluing "
729 "Oversized IP packet from %d.%d.%d.%d.\n",
732 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
736 /* Process an incoming IP datagram fragment. */
737 int ip_defrag(struct sk_buff
*skb
, u32 user
)
741 IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS
);
743 /* Start by cleaning up the memory. */
744 if (atomic_read(&ip_frag_mem
) > sysctl_ipfrag_high_thresh
)
747 /* Lookup (or create) queue header */
748 if ((qp
= ip_find(ip_hdr(skb
), user
)) != NULL
) {
751 spin_lock(&qp
->lock
);
753 ret
= ip_frag_queue(qp
, skb
);
755 spin_unlock(&qp
->lock
);
760 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS
);
765 void __init
ipfrag_init(void)
767 ipfrag_hash_rnd
= (u32
) ((num_physpages
^ (num_physpages
>>7)) ^
768 (jiffies
^ (jiffies
>> 6)));
770 init_timer(&ipfrag_secret_timer
);
771 ipfrag_secret_timer
.function
= ipfrag_secret_rebuild
;
772 ipfrag_secret_timer
.expires
= jiffies
+ sysctl_ipfrag_secret_interval
;
773 add_timer(&ipfrag_secret_timer
);
776 EXPORT_SYMBOL(ip_defrag
);