11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * The IP fragmentation functionality. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Version: $Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $ 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> 111da177e4SLinus Torvalds * Alan Cox <Alan.Cox@linux.org> 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Fixes: 141da177e4SLinus Torvalds * Alan Cox : Split from ip.c , see ip_input.c for history. 151da177e4SLinus Torvalds * David S. Miller : Begin massive cleanup... 161da177e4SLinus Torvalds * Andi Kleen : Add sysctls. 171da177e4SLinus Torvalds * xxxx : Overlapfrag bug. 181da177e4SLinus Torvalds * Ultima : ip_expire() kernel panic. 191da177e4SLinus Torvalds * Bill Hawes : Frag accounting and evictor fixes. 201da177e4SLinus Torvalds * John McDonald : 0 length frag bug. 211da177e4SLinus Torvalds * Alexey Kuznetsov: SMP races, threading, cleanup. 221da177e4SLinus Torvalds * Patrick McHardy : LRU queue of frag heads for evictor. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 2589cee8b1SHerbert Xu #include <linux/compiler.h> 261da177e4SLinus Torvalds #include <linux/config.h> 271da177e4SLinus Torvalds #include <linux/module.h> 281da177e4SLinus Torvalds #include <linux/types.h> 291da177e4SLinus Torvalds #include <linux/mm.h> 301da177e4SLinus Torvalds #include <linux/jiffies.h> 311da177e4SLinus Torvalds #include <linux/skbuff.h> 321da177e4SLinus Torvalds #include <linux/list.h> 331da177e4SLinus Torvalds #include <linux/ip.h> 341da177e4SLinus Torvalds #include <linux/icmp.h> 351da177e4SLinus Torvalds #include <linux/netdevice.h> 361da177e4SLinus Torvalds #include <linux/jhash.h> 371da177e4SLinus Torvalds #include <linux/random.h> 381da177e4SLinus Torvalds #include <net/sock.h> 391da177e4SLinus Torvalds #include <net/ip.h> 401da177e4SLinus Torvalds #include <net/icmp.h> 411da177e4SLinus Torvalds #include <net/checksum.h> 4289cee8b1SHerbert Xu #include <net/inetpeer.h> 431da177e4SLinus Torvalds #include <linux/tcp.h> 441da177e4SLinus Torvalds #include <linux/udp.h> 451da177e4SLinus Torvalds #include <linux/inet.h> 461da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6 491da177e4SLinus Torvalds * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c 501da177e4SLinus Torvalds * as well. Or notify me, at least. --ANK 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* Fragment cache limits. We will commit 256K at one time. Should we 541da177e4SLinus Torvalds * cross that limit we will prune down to 192K. This should cope with 551da177e4SLinus Torvalds * even the most extreme cases without allowing an attacker to measurably 561da177e4SLinus Torvalds * harm machine performance. 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds int sysctl_ipfrag_high_thresh = 256*1024; 591da177e4SLinus Torvalds int sysctl_ipfrag_low_thresh = 192*1024; 601da177e4SLinus Torvalds 6189cee8b1SHerbert Xu int sysctl_ipfrag_max_dist = 64; 6289cee8b1SHerbert Xu 631da177e4SLinus Torvalds /* Important NOTE! Fragment queue must be destroyed before MSL expires. 641da177e4SLinus Torvalds * RFC791 is wrong proposing to prolongate timer each fragment arrival by TTL. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds int sysctl_ipfrag_time = IP_FRAG_TIME; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds struct ipfrag_skb_cb 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds struct inet_skb_parm h; 711da177e4SLinus Torvalds int offset; 721da177e4SLinus Torvalds }; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds #define FRAG_CB(skb) ((struct ipfrag_skb_cb*)((skb)->cb)) 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */ 771da177e4SLinus Torvalds struct ipq { 78e7c8a41eSYasuyuki Kozakai struct hlist_node list; 791da177e4SLinus Torvalds struct list_head lru_list; /* lru list member */ 801da177e4SLinus Torvalds u32 user; 811da177e4SLinus Torvalds u32 saddr; 821da177e4SLinus Torvalds u32 daddr; 831da177e4SLinus Torvalds u16 id; 841da177e4SLinus Torvalds u8 protocol; 851da177e4SLinus Torvalds u8 last_in; 861da177e4SLinus Torvalds #define COMPLETE 4 871da177e4SLinus Torvalds #define FIRST_IN 2 881da177e4SLinus Torvalds #define LAST_IN 1 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds struct sk_buff *fragments; /* linked list of received fragments */ 911da177e4SLinus Torvalds int len; /* total length of original datagram */ 921da177e4SLinus Torvalds int meat; 931da177e4SLinus Torvalds spinlock_t lock; 941da177e4SLinus Torvalds atomic_t refcnt; 951da177e4SLinus Torvalds struct timer_list timer; /* when will this queue expire? */ 961da177e4SLinus Torvalds struct timeval stamp; 9789cee8b1SHerbert Xu int iif; 9889cee8b1SHerbert Xu unsigned int rid; 9989cee8b1SHerbert Xu struct inet_peer *peer; 1001da177e4SLinus Torvalds }; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* Hash table. */ 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds #define IPQ_HASHSZ 64 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* Per-bucket lock is easy to add now. */ 107e7c8a41eSYasuyuki Kozakai static struct hlist_head ipq_hash[IPQ_HASHSZ]; 1081da177e4SLinus Torvalds static DEFINE_RWLOCK(ipfrag_lock); 1091da177e4SLinus Torvalds static u32 ipfrag_hash_rnd; 1101da177e4SLinus Torvalds static LIST_HEAD(ipq_lru_list); 1111da177e4SLinus Torvalds int ip_frag_nqueues = 0; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds static __inline__ void __ipq_unlink(struct ipq *qp) 1141da177e4SLinus Torvalds { 115e7c8a41eSYasuyuki Kozakai hlist_del(&qp->list); 1161da177e4SLinus Torvalds list_del(&qp->lru_list); 1171da177e4SLinus Torvalds ip_frag_nqueues--; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds static __inline__ void ipq_unlink(struct ipq *ipq) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds write_lock(&ipfrag_lock); 1231da177e4SLinus Torvalds __ipq_unlink(ipq); 1241da177e4SLinus Torvalds write_unlock(&ipfrag_lock); 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static unsigned int ipqhashfn(u16 id, u32 saddr, u32 daddr, u8 prot) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds return jhash_3words((u32)id << 16 | prot, saddr, daddr, 1301da177e4SLinus Torvalds ipfrag_hash_rnd) & (IPQ_HASHSZ - 1); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static struct timer_list ipfrag_secret_timer; 1341da177e4SLinus Torvalds int sysctl_ipfrag_secret_interval = 10 * 60 * HZ; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds static void ipfrag_secret_rebuild(unsigned long dummy) 1371da177e4SLinus Torvalds { 1381da177e4SLinus Torvalds unsigned long now = jiffies; 1391da177e4SLinus Torvalds int i; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds write_lock(&ipfrag_lock); 1421da177e4SLinus Torvalds get_random_bytes(&ipfrag_hash_rnd, sizeof(u32)); 1431da177e4SLinus Torvalds for (i = 0; i < IPQ_HASHSZ; i++) { 1441da177e4SLinus Torvalds struct ipq *q; 145e7c8a41eSYasuyuki Kozakai struct hlist_node *p, *n; 1461da177e4SLinus Torvalds 147e7c8a41eSYasuyuki Kozakai hlist_for_each_entry_safe(q, p, n, &ipq_hash[i], list) { 1481da177e4SLinus Torvalds unsigned int hval = ipqhashfn(q->id, q->saddr, 1491da177e4SLinus Torvalds q->daddr, q->protocol); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds if (hval != i) { 152e7c8a41eSYasuyuki Kozakai hlist_del(&q->list); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* Relink to new hash chain. */ 155e7c8a41eSYasuyuki Kozakai hlist_add_head(&q->list, &ipq_hash[hval]); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds write_unlock(&ipfrag_lock); 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds mod_timer(&ipfrag_secret_timer, now + sysctl_ipfrag_secret_interval); 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds atomic_t ip_frag_mem = ATOMIC_INIT(0); /* Memory used for fragments */ 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* Memory Tracking Functions. */ 1671da177e4SLinus Torvalds static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds if (work) 1701da177e4SLinus Torvalds *work -= skb->truesize; 1711da177e4SLinus Torvalds atomic_sub(skb->truesize, &ip_frag_mem); 1721da177e4SLinus Torvalds kfree_skb(skb); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static __inline__ void frag_free_queue(struct ipq *qp, int *work) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds if (work) 1781da177e4SLinus Torvalds *work -= sizeof(struct ipq); 1791da177e4SLinus Torvalds atomic_sub(sizeof(struct ipq), &ip_frag_mem); 1801da177e4SLinus Torvalds kfree(qp); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds static __inline__ struct ipq *frag_alloc_queue(void) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds struct ipq *qp = kmalloc(sizeof(struct ipq), GFP_ATOMIC); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds if(!qp) 1881da177e4SLinus Torvalds return NULL; 1891da177e4SLinus Torvalds atomic_add(sizeof(struct ipq), &ip_frag_mem); 1901da177e4SLinus Torvalds return qp; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* Destruction primitives. */ 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* Complete destruction of ipq. */ 1971da177e4SLinus Torvalds static void ip_frag_destroy(struct ipq *qp, int *work) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds struct sk_buff *fp; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds BUG_TRAP(qp->last_in&COMPLETE); 2021da177e4SLinus Torvalds BUG_TRAP(del_timer(&qp->timer) == 0); 2031da177e4SLinus Torvalds 20489cee8b1SHerbert Xu if (qp->peer) 20589cee8b1SHerbert Xu inet_putpeer(qp->peer); 20689cee8b1SHerbert Xu 2071da177e4SLinus Torvalds /* Release all fragment data. */ 2081da177e4SLinus Torvalds fp = qp->fragments; 2091da177e4SLinus Torvalds while (fp) { 2101da177e4SLinus Torvalds struct sk_buff *xp = fp->next; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds frag_kfree_skb(fp, work); 2131da177e4SLinus Torvalds fp = xp; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* Finally, release the queue descriptor itself. */ 2171da177e4SLinus Torvalds frag_free_queue(qp, work); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static __inline__ void ipq_put(struct ipq *ipq, int *work) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds if (atomic_dec_and_test(&ipq->refcnt)) 2231da177e4SLinus Torvalds ip_frag_destroy(ipq, work); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately, 2271da177e4SLinus Torvalds * because caller (and someone more) holds reference count. 2281da177e4SLinus Torvalds */ 2291da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds if (del_timer(&ipq->timer)) 2321da177e4SLinus Torvalds atomic_dec(&ipq->refcnt); 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds if (!(ipq->last_in & COMPLETE)) { 2351da177e4SLinus Torvalds ipq_unlink(ipq); 2361da177e4SLinus Torvalds atomic_dec(&ipq->refcnt); 2371da177e4SLinus Torvalds ipq->last_in |= COMPLETE; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* Memory limiting on fragments. Evictor trashes the oldest 2421da177e4SLinus Torvalds * fragment queue until we are back under the threshold. 2431da177e4SLinus Torvalds */ 2441da177e4SLinus Torvalds static void ip_evictor(void) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds struct ipq *qp; 2471da177e4SLinus Torvalds struct list_head *tmp; 2481da177e4SLinus Torvalds int work; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds work = atomic_read(&ip_frag_mem) - sysctl_ipfrag_low_thresh; 2511da177e4SLinus Torvalds if (work <= 0) 2521da177e4SLinus Torvalds return; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds while (work > 0) { 2551da177e4SLinus Torvalds read_lock(&ipfrag_lock); 2561da177e4SLinus Torvalds if (list_empty(&ipq_lru_list)) { 2571da177e4SLinus Torvalds read_unlock(&ipfrag_lock); 2581da177e4SLinus Torvalds return; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds tmp = ipq_lru_list.next; 2611da177e4SLinus Torvalds qp = list_entry(tmp, struct ipq, lru_list); 2621da177e4SLinus Torvalds atomic_inc(&qp->refcnt); 2631da177e4SLinus Torvalds read_unlock(&ipfrag_lock); 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds spin_lock(&qp->lock); 2661da177e4SLinus Torvalds if (!(qp->last_in&COMPLETE)) 2671da177e4SLinus Torvalds ipq_kill(qp); 2681da177e4SLinus Torvalds spin_unlock(&qp->lock); 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds ipq_put(qp, &work); 2711da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * Oops, a fragment queue timed out. Kill it and send an ICMP reply. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds static void ip_expire(unsigned long arg) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds struct ipq *qp = (struct ipq *) arg; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds spin_lock(&qp->lock); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds if (qp->last_in & COMPLETE) 2851da177e4SLinus Torvalds goto out; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds ipq_kill(qp); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT); 2901da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds if ((qp->last_in&FIRST_IN) && qp->fragments != NULL) { 2931da177e4SLinus Torvalds struct sk_buff *head = qp->fragments; 2941da177e4SLinus Torvalds /* Send an ICMP "Fragment Reassembly Timeout" message. */ 2951da177e4SLinus Torvalds if ((head->dev = dev_get_by_index(qp->iif)) != NULL) { 2961da177e4SLinus Torvalds icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); 2971da177e4SLinus Torvalds dev_put(head->dev); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds out: 3011da177e4SLinus Torvalds spin_unlock(&qp->lock); 3021da177e4SLinus Torvalds ipq_put(qp, NULL); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* Creation primitives. */ 3061da177e4SLinus Torvalds 307*55c0022eSDavid S. Miller static struct ipq *ip_frag_intern(struct ipq *qp_in) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds struct ipq *qp; 310e7c8a41eSYasuyuki Kozakai #ifdef CONFIG_SMP 311e7c8a41eSYasuyuki Kozakai struct hlist_node *n; 312e7c8a41eSYasuyuki Kozakai #endif 313*55c0022eSDavid S. Miller unsigned int hash; 314*55c0022eSDavid S. Miller 3151da177e4SLinus Torvalds write_lock(&ipfrag_lock); 316*55c0022eSDavid S. Miller hash = ipqhashfn(qp_in->id, qp_in->saddr, qp_in->daddr, 317*55c0022eSDavid S. Miller qp_in->protocol); 3181da177e4SLinus Torvalds #ifdef CONFIG_SMP 3191da177e4SLinus Torvalds /* With SMP race we have to recheck hash table, because 3201da177e4SLinus Torvalds * such entry could be created on other cpu, while we 3211da177e4SLinus Torvalds * promoted read lock to write lock. 3221da177e4SLinus Torvalds */ 323e7c8a41eSYasuyuki Kozakai hlist_for_each_entry(qp, n, &ipq_hash[hash], list) { 3241da177e4SLinus Torvalds if(qp->id == qp_in->id && 3251da177e4SLinus Torvalds qp->saddr == qp_in->saddr && 3261da177e4SLinus Torvalds qp->daddr == qp_in->daddr && 3271da177e4SLinus Torvalds qp->protocol == qp_in->protocol && 3281da177e4SLinus Torvalds qp->user == qp_in->user) { 3291da177e4SLinus Torvalds atomic_inc(&qp->refcnt); 3301da177e4SLinus Torvalds write_unlock(&ipfrag_lock); 3311da177e4SLinus Torvalds qp_in->last_in |= COMPLETE; 3321da177e4SLinus Torvalds ipq_put(qp_in, NULL); 3331da177e4SLinus Torvalds return qp; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds #endif 3371da177e4SLinus Torvalds qp = qp_in; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time)) 3401da177e4SLinus Torvalds atomic_inc(&qp->refcnt); 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds atomic_inc(&qp->refcnt); 343e7c8a41eSYasuyuki Kozakai hlist_add_head(&qp->list, &ipq_hash[hash]); 3441da177e4SLinus Torvalds INIT_LIST_HEAD(&qp->lru_list); 3451da177e4SLinus Torvalds list_add_tail(&qp->lru_list, &ipq_lru_list); 3461da177e4SLinus Torvalds ip_frag_nqueues++; 3471da177e4SLinus Torvalds write_unlock(&ipfrag_lock); 3481da177e4SLinus Torvalds return qp; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* Add an entry to the 'ipq' queue for a newly received IP datagram. */ 352*55c0022eSDavid S. Miller static struct ipq *ip_frag_create(struct iphdr *iph, u32 user) 3531da177e4SLinus Torvalds { 3541da177e4SLinus Torvalds struct ipq *qp; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds if ((qp = frag_alloc_queue()) == NULL) 3571da177e4SLinus Torvalds goto out_nomem; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds qp->protocol = iph->protocol; 3601da177e4SLinus Torvalds qp->last_in = 0; 3611da177e4SLinus Torvalds qp->id = iph->id; 3621da177e4SLinus Torvalds qp->saddr = iph->saddr; 3631da177e4SLinus Torvalds qp->daddr = iph->daddr; 3641da177e4SLinus Torvalds qp->user = user; 3651da177e4SLinus Torvalds qp->len = 0; 3661da177e4SLinus Torvalds qp->meat = 0; 3671da177e4SLinus Torvalds qp->fragments = NULL; 3681da177e4SLinus Torvalds qp->iif = 0; 36989cee8b1SHerbert Xu qp->peer = sysctl_ipfrag_max_dist ? inet_getpeer(iph->saddr, 1) : NULL; 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds /* Initialize a timer for this entry. */ 3721da177e4SLinus Torvalds init_timer(&qp->timer); 3731da177e4SLinus Torvalds qp->timer.data = (unsigned long) qp; /* pointer to queue */ 3741da177e4SLinus Torvalds qp->timer.function = ip_expire; /* expire function */ 3751da177e4SLinus Torvalds spin_lock_init(&qp->lock); 3761da177e4SLinus Torvalds atomic_set(&qp->refcnt, 1); 3771da177e4SLinus Torvalds 378*55c0022eSDavid S. Miller return ip_frag_intern(qp); 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds out_nomem: 38164ce2073SPatrick McHardy LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n"); 3821da177e4SLinus Torvalds return NULL; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* Find the correct entry in the "incomplete datagrams" queue for 3861da177e4SLinus Torvalds * this IP datagram, and create new one, if nothing is found. 3871da177e4SLinus Torvalds */ 3881da177e4SLinus Torvalds static inline struct ipq *ip_find(struct iphdr *iph, u32 user) 3891da177e4SLinus Torvalds { 39076ab608dSAlexey Dobriyan __be16 id = iph->id; 3911da177e4SLinus Torvalds __u32 saddr = iph->saddr; 3921da177e4SLinus Torvalds __u32 daddr = iph->daddr; 3931da177e4SLinus Torvalds __u8 protocol = iph->protocol; 394*55c0022eSDavid S. Miller unsigned int hash; 3951da177e4SLinus Torvalds struct ipq *qp; 396e7c8a41eSYasuyuki Kozakai struct hlist_node *n; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds read_lock(&ipfrag_lock); 399*55c0022eSDavid S. Miller hash = ipqhashfn(id, saddr, daddr, protocol); 400e7c8a41eSYasuyuki Kozakai hlist_for_each_entry(qp, n, &ipq_hash[hash], list) { 4011da177e4SLinus Torvalds if(qp->id == id && 4021da177e4SLinus Torvalds qp->saddr == saddr && 4031da177e4SLinus Torvalds qp->daddr == daddr && 4041da177e4SLinus Torvalds qp->protocol == protocol && 4051da177e4SLinus Torvalds qp->user == user) { 4061da177e4SLinus Torvalds atomic_inc(&qp->refcnt); 4071da177e4SLinus Torvalds read_unlock(&ipfrag_lock); 4081da177e4SLinus Torvalds return qp; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds read_unlock(&ipfrag_lock); 4121da177e4SLinus Torvalds 413*55c0022eSDavid S. Miller return ip_frag_create(iph, user); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 41689cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */ 41789cee8b1SHerbert Xu static inline int ip_frag_too_far(struct ipq *qp) 41889cee8b1SHerbert Xu { 41989cee8b1SHerbert Xu struct inet_peer *peer = qp->peer; 42089cee8b1SHerbert Xu unsigned int max = sysctl_ipfrag_max_dist; 42189cee8b1SHerbert Xu unsigned int start, end; 42289cee8b1SHerbert Xu 42389cee8b1SHerbert Xu int rc; 42489cee8b1SHerbert Xu 42589cee8b1SHerbert Xu if (!peer || !max) 42689cee8b1SHerbert Xu return 0; 42789cee8b1SHerbert Xu 42889cee8b1SHerbert Xu start = qp->rid; 42989cee8b1SHerbert Xu end = atomic_inc_return(&peer->rid); 43089cee8b1SHerbert Xu qp->rid = end; 43189cee8b1SHerbert Xu 43289cee8b1SHerbert Xu rc = qp->fragments && (end - start) > max; 43389cee8b1SHerbert Xu 43489cee8b1SHerbert Xu if (rc) { 43589cee8b1SHerbert Xu IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS); 43689cee8b1SHerbert Xu } 43789cee8b1SHerbert Xu 43889cee8b1SHerbert Xu return rc; 43989cee8b1SHerbert Xu } 44089cee8b1SHerbert Xu 44189cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp) 44289cee8b1SHerbert Xu { 44389cee8b1SHerbert Xu struct sk_buff *fp; 44489cee8b1SHerbert Xu 44589cee8b1SHerbert Xu if (!mod_timer(&qp->timer, jiffies + sysctl_ipfrag_time)) { 44689cee8b1SHerbert Xu atomic_inc(&qp->refcnt); 44789cee8b1SHerbert Xu return -ETIMEDOUT; 44889cee8b1SHerbert Xu } 44989cee8b1SHerbert Xu 45089cee8b1SHerbert Xu fp = qp->fragments; 45189cee8b1SHerbert Xu do { 45289cee8b1SHerbert Xu struct sk_buff *xp = fp->next; 45389cee8b1SHerbert Xu frag_kfree_skb(fp, NULL); 45489cee8b1SHerbert Xu fp = xp; 45589cee8b1SHerbert Xu } while (fp); 45689cee8b1SHerbert Xu 45789cee8b1SHerbert Xu qp->last_in = 0; 45889cee8b1SHerbert Xu qp->len = 0; 45989cee8b1SHerbert Xu qp->meat = 0; 46089cee8b1SHerbert Xu qp->fragments = NULL; 46189cee8b1SHerbert Xu qp->iif = 0; 46289cee8b1SHerbert Xu 46389cee8b1SHerbert Xu return 0; 46489cee8b1SHerbert Xu } 46589cee8b1SHerbert Xu 4661da177e4SLinus Torvalds /* Add new segment to existing queue. */ 4671da177e4SLinus Torvalds static void ip_frag_queue(struct ipq *qp, struct sk_buff *skb) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds struct sk_buff *prev, *next; 4701da177e4SLinus Torvalds int flags, offset; 4711da177e4SLinus Torvalds int ihl, end; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds if (qp->last_in & COMPLETE) 4741da177e4SLinus Torvalds goto err; 4751da177e4SLinus Torvalds 47689cee8b1SHerbert Xu if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && 47789cee8b1SHerbert Xu unlikely(ip_frag_too_far(qp)) && unlikely(ip_frag_reinit(qp))) { 47889cee8b1SHerbert Xu ipq_kill(qp); 47989cee8b1SHerbert Xu goto err; 48089cee8b1SHerbert Xu } 48189cee8b1SHerbert Xu 4821da177e4SLinus Torvalds offset = ntohs(skb->nh.iph->frag_off); 4831da177e4SLinus Torvalds flags = offset & ~IP_OFFSET; 4841da177e4SLinus Torvalds offset &= IP_OFFSET; 4851da177e4SLinus Torvalds offset <<= 3; /* offset is in 8-byte chunks */ 4861da177e4SLinus Torvalds ihl = skb->nh.iph->ihl * 4; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* Determine the position of this fragment. */ 4891da177e4SLinus Torvalds end = offset + skb->len - ihl; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* Is this the final fragment? */ 4921da177e4SLinus Torvalds if ((flags & IP_MF) == 0) { 4931da177e4SLinus Torvalds /* If we already have some bits beyond end 4941da177e4SLinus Torvalds * or have different end, the segment is corrrupted. 4951da177e4SLinus Torvalds */ 4961da177e4SLinus Torvalds if (end < qp->len || 4971da177e4SLinus Torvalds ((qp->last_in & LAST_IN) && end != qp->len)) 4981da177e4SLinus Torvalds goto err; 4991da177e4SLinus Torvalds qp->last_in |= LAST_IN; 5001da177e4SLinus Torvalds qp->len = end; 5011da177e4SLinus Torvalds } else { 5021da177e4SLinus Torvalds if (end&7) { 5031da177e4SLinus Torvalds end &= ~7; 5041da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 5051da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds if (end > qp->len) { 5081da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 5091da177e4SLinus Torvalds if (qp->last_in & LAST_IN) 5101da177e4SLinus Torvalds goto err; 5111da177e4SLinus Torvalds qp->len = end; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds if (end == offset) 5151da177e4SLinus Torvalds goto err; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds if (pskb_pull(skb, ihl) == NULL) 5181da177e4SLinus Torvalds goto err; 51948bc41a4SStephen Hemminger if (pskb_trim_rcsum(skb, end-offset)) 5201da177e4SLinus Torvalds goto err; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* Find out which fragments are in front and at the back of us 5231da177e4SLinus Torvalds * in the chain of fragments so far. We must know where to put 5241da177e4SLinus Torvalds * this fragment, right? 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds prev = NULL; 5271da177e4SLinus Torvalds for(next = qp->fragments; next != NULL; next = next->next) { 5281da177e4SLinus Torvalds if (FRAG_CB(next)->offset >= offset) 5291da177e4SLinus Torvalds break; /* bingo! */ 5301da177e4SLinus Torvalds prev = next; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* We found where to put this one. Check for overlap with 5341da177e4SLinus Torvalds * preceding fragment, and, if needed, align things so that 5351da177e4SLinus Torvalds * any overlaps are eliminated. 5361da177e4SLinus Torvalds */ 5371da177e4SLinus Torvalds if (prev) { 5381da177e4SLinus Torvalds int i = (FRAG_CB(prev)->offset + prev->len) - offset; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds if (i > 0) { 5411da177e4SLinus Torvalds offset += i; 5421da177e4SLinus Torvalds if (end <= offset) 5431da177e4SLinus Torvalds goto err; 5441da177e4SLinus Torvalds if (!pskb_pull(skb, i)) 5451da177e4SLinus Torvalds goto err; 5461da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 5471da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds while (next && FRAG_CB(next)->offset < end) { 5521da177e4SLinus Torvalds int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */ 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds if (i < next->len) { 5551da177e4SLinus Torvalds /* Eat head of the next overlapped fragment 5561da177e4SLinus Torvalds * and leave the loop. The next ones cannot overlap. 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds if (!pskb_pull(next, i)) 5591da177e4SLinus Torvalds goto err; 5601da177e4SLinus Torvalds FRAG_CB(next)->offset += i; 5611da177e4SLinus Torvalds qp->meat -= i; 5621da177e4SLinus Torvalds if (next->ip_summed != CHECKSUM_UNNECESSARY) 5631da177e4SLinus Torvalds next->ip_summed = CHECKSUM_NONE; 5641da177e4SLinus Torvalds break; 5651da177e4SLinus Torvalds } else { 5661da177e4SLinus Torvalds struct sk_buff *free_it = next; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* Old fragmnet is completely overridden with 5691da177e4SLinus Torvalds * new one drop it. 5701da177e4SLinus Torvalds */ 5711da177e4SLinus Torvalds next = next->next; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds if (prev) 5741da177e4SLinus Torvalds prev->next = next; 5751da177e4SLinus Torvalds else 5761da177e4SLinus Torvalds qp->fragments = next; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds qp->meat -= free_it->len; 5791da177e4SLinus Torvalds frag_kfree_skb(free_it, NULL); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds FRAG_CB(skb)->offset = offset; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /* Insert this fragment in the chain of fragments. */ 5861da177e4SLinus Torvalds skb->next = next; 5871da177e4SLinus Torvalds if (prev) 5881da177e4SLinus Torvalds prev->next = skb; 5891da177e4SLinus Torvalds else 5901da177e4SLinus Torvalds qp->fragments = skb; 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds if (skb->dev) 5931da177e4SLinus Torvalds qp->iif = skb->dev->ifindex; 5941da177e4SLinus Torvalds skb->dev = NULL; 595a61bbcf2SPatrick McHardy skb_get_timestamp(skb, &qp->stamp); 5961da177e4SLinus Torvalds qp->meat += skb->len; 5971da177e4SLinus Torvalds atomic_add(skb->truesize, &ip_frag_mem); 5981da177e4SLinus Torvalds if (offset == 0) 5991da177e4SLinus Torvalds qp->last_in |= FIRST_IN; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds write_lock(&ipfrag_lock); 6021da177e4SLinus Torvalds list_move_tail(&qp->lru_list, &ipq_lru_list); 6031da177e4SLinus Torvalds write_unlock(&ipfrag_lock); 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds return; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds err: 6081da177e4SLinus Torvalds kfree_skb(skb); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */ 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds static struct sk_buff *ip_frag_reasm(struct ipq *qp, struct net_device *dev) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds struct iphdr *iph; 6171da177e4SLinus Torvalds struct sk_buff *fp, *head = qp->fragments; 6181da177e4SLinus Torvalds int len; 6191da177e4SLinus Torvalds int ihlen; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds ipq_kill(qp); 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds BUG_TRAP(head != NULL); 6241da177e4SLinus Torvalds BUG_TRAP(FRAG_CB(head)->offset == 0); 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds /* Allocate a new buffer for the datagram. */ 6271da177e4SLinus Torvalds ihlen = head->nh.iph->ihl*4; 6281da177e4SLinus Torvalds len = ihlen + qp->len; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds if(len > 65535) 6311da177e4SLinus Torvalds goto out_oversize; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* Head of list must not be cloned. */ 6341da177e4SLinus Torvalds if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) 6351da177e4SLinus Torvalds goto out_nomem; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds /* If the first fragment is fragmented itself, we split 6381da177e4SLinus Torvalds * it to two chunks: the first with data and paged part 6391da177e4SLinus Torvalds * and the second, holding only fragments. */ 6401da177e4SLinus Torvalds if (skb_shinfo(head)->frag_list) { 6411da177e4SLinus Torvalds struct sk_buff *clone; 6421da177e4SLinus Torvalds int i, plen = 0; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) 6451da177e4SLinus Torvalds goto out_nomem; 6461da177e4SLinus Torvalds clone->next = head->next; 6471da177e4SLinus Torvalds head->next = clone; 6481da177e4SLinus Torvalds skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 6491da177e4SLinus Torvalds skb_shinfo(head)->frag_list = NULL; 6501da177e4SLinus Torvalds for (i=0; i<skb_shinfo(head)->nr_frags; i++) 6511da177e4SLinus Torvalds plen += skb_shinfo(head)->frags[i].size; 6521da177e4SLinus Torvalds clone->len = clone->data_len = head->data_len - plen; 6531da177e4SLinus Torvalds head->data_len -= clone->len; 6541da177e4SLinus Torvalds head->len -= clone->len; 6551da177e4SLinus Torvalds clone->csum = 0; 6561da177e4SLinus Torvalds clone->ip_summed = head->ip_summed; 6571da177e4SLinus Torvalds atomic_add(clone->truesize, &ip_frag_mem); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds skb_shinfo(head)->frag_list = head->next; 6611da177e4SLinus Torvalds skb_push(head, head->data - head->nh.raw); 6621da177e4SLinus Torvalds atomic_sub(head->truesize, &ip_frag_mem); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds for (fp=head->next; fp; fp = fp->next) { 6651da177e4SLinus Torvalds head->data_len += fp->len; 6661da177e4SLinus Torvalds head->len += fp->len; 6671da177e4SLinus Torvalds if (head->ip_summed != fp->ip_summed) 6681da177e4SLinus Torvalds head->ip_summed = CHECKSUM_NONE; 6691da177e4SLinus Torvalds else if (head->ip_summed == CHECKSUM_HW) 6701da177e4SLinus Torvalds head->csum = csum_add(head->csum, fp->csum); 6711da177e4SLinus Torvalds head->truesize += fp->truesize; 6721da177e4SLinus Torvalds atomic_sub(fp->truesize, &ip_frag_mem); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds head->next = NULL; 6761da177e4SLinus Torvalds head->dev = dev; 677a61bbcf2SPatrick McHardy skb_set_timestamp(head, &qp->stamp); 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds iph = head->nh.iph; 6801da177e4SLinus Torvalds iph->frag_off = 0; 6811da177e4SLinus Torvalds iph->tot_len = htons(len); 6821da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS); 6831da177e4SLinus Torvalds qp->fragments = NULL; 6841da177e4SLinus Torvalds return head; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds out_nomem: 68764ce2073SPatrick McHardy LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing " 68864ce2073SPatrick McHardy "queue %p\n", qp); 6891da177e4SLinus Torvalds goto out_fail; 6901da177e4SLinus Torvalds out_oversize: 6911da177e4SLinus Torvalds if (net_ratelimit()) 6921da177e4SLinus Torvalds printk(KERN_INFO 6931da177e4SLinus Torvalds "Oversized IP packet from %d.%d.%d.%d.\n", 6941da177e4SLinus Torvalds NIPQUAD(qp->saddr)); 6951da177e4SLinus Torvalds out_fail: 6961da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS); 6971da177e4SLinus Torvalds return NULL; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */ 7011da177e4SLinus Torvalds struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user) 7021da177e4SLinus Torvalds { 7031da177e4SLinus Torvalds struct iphdr *iph = skb->nh.iph; 7041da177e4SLinus Torvalds struct ipq *qp; 7051da177e4SLinus Torvalds struct net_device *dev; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* Start by cleaning up the memory. */ 7101da177e4SLinus Torvalds if (atomic_read(&ip_frag_mem) > sysctl_ipfrag_high_thresh) 7111da177e4SLinus Torvalds ip_evictor(); 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds dev = skb->dev; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* Lookup (or create) queue header */ 7161da177e4SLinus Torvalds if ((qp = ip_find(iph, user)) != NULL) { 7171da177e4SLinus Torvalds struct sk_buff *ret = NULL; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds spin_lock(&qp->lock); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds ip_frag_queue(qp, skb); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds if (qp->last_in == (FIRST_IN|LAST_IN) && 7241da177e4SLinus Torvalds qp->meat == qp->len) 7251da177e4SLinus Torvalds ret = ip_frag_reasm(qp, dev); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds spin_unlock(&qp->lock); 7281da177e4SLinus Torvalds ipq_put(qp, NULL); 7291da177e4SLinus Torvalds return ret; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS); 7331da177e4SLinus Torvalds kfree_skb(skb); 7341da177e4SLinus Torvalds return NULL; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds void ipfrag_init(void) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds ipfrag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^ 7401da177e4SLinus Torvalds (jiffies ^ (jiffies >> 6))); 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds init_timer(&ipfrag_secret_timer); 7431da177e4SLinus Torvalds ipfrag_secret_timer.function = ipfrag_secret_rebuild; 7441da177e4SLinus Torvalds ipfrag_secret_timer.expires = jiffies + sysctl_ipfrag_secret_interval; 7451da177e4SLinus Torvalds add_timer(&ipfrag_secret_timer); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds EXPORT_SYMBOL(ip_defrag); 749