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 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> 9113aa838SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Fixes: 121da177e4SLinus Torvalds * Alan Cox : Split from ip.c , see ip_input.c for history. 131da177e4SLinus Torvalds * David S. Miller : Begin massive cleanup... 141da177e4SLinus Torvalds * Andi Kleen : Add sysctls. 151da177e4SLinus Torvalds * xxxx : Overlapfrag bug. 161da177e4SLinus Torvalds * Ultima : ip_expire() kernel panic. 171da177e4SLinus Torvalds * Bill Hawes : Frag accounting and evictor fixes. 181da177e4SLinus Torvalds * John McDonald : 0 length frag bug. 191da177e4SLinus Torvalds * Alexey Kuznetsov: SMP races, threading, cleanup. 201da177e4SLinus Torvalds * Patrick McHardy : LRU queue of frag heads for evictor. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt 24afd46503SJoe Perches 2589cee8b1SHerbert Xu #include <linux/compiler.h> 261da177e4SLinus Torvalds #include <linux/module.h> 271da177e4SLinus Torvalds #include <linux/types.h> 281da177e4SLinus Torvalds #include <linux/mm.h> 291da177e4SLinus Torvalds #include <linux/jiffies.h> 301da177e4SLinus Torvalds #include <linux/skbuff.h> 311da177e4SLinus Torvalds #include <linux/list.h> 321da177e4SLinus Torvalds #include <linux/ip.h> 331da177e4SLinus Torvalds #include <linux/icmp.h> 341da177e4SLinus Torvalds #include <linux/netdevice.h> 351da177e4SLinus Torvalds #include <linux/jhash.h> 361da177e4SLinus Torvalds #include <linux/random.h> 375a0e3ad6STejun Heo #include <linux/slab.h> 38e9017b55SShan Wei #include <net/route.h> 39e9017b55SShan Wei #include <net/dst.h> 401da177e4SLinus Torvalds #include <net/sock.h> 411da177e4SLinus Torvalds #include <net/ip.h> 421da177e4SLinus Torvalds #include <net/icmp.h> 431da177e4SLinus Torvalds #include <net/checksum.h> 4489cee8b1SHerbert Xu #include <net/inetpeer.h> 455ab11c98SPavel Emelyanov #include <net/inet_frag.h> 461da177e4SLinus Torvalds #include <linux/tcp.h> 471da177e4SLinus Torvalds #include <linux/udp.h> 481da177e4SLinus Torvalds #include <linux/inet.h> 491da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 506623e3b2SEric Dumazet #include <net/inet_ecn.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6 531da177e4SLinus Torvalds * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c 541da177e4SLinus Torvalds * as well. Or notify me, at least. --ANK 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds 578d8354d2SPavel Emelyanov static int sysctl_ipfrag_max_dist __read_mostly = 64; 58d4ad4d22SNikolay Aleksandrov static const char ip_frag_cache_name[] = "ip4-frags"; 5989cee8b1SHerbert Xu 601da177e4SLinus Torvalds struct ipfrag_skb_cb 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds struct inet_skb_parm h; 631da177e4SLinus Torvalds int offset; 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb)) 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */ 691da177e4SLinus Torvalds struct ipq { 705ab11c98SPavel Emelyanov struct inet_frag_queue q; 715ab11c98SPavel Emelyanov 721da177e4SLinus Torvalds u32 user; 7318277770SAl Viro __be32 saddr; 7418277770SAl Viro __be32 daddr; 7518277770SAl Viro __be16 id; 761da177e4SLinus Torvalds u8 protocol; 776623e3b2SEric Dumazet u8 ecn; /* RFC3168 support */ 78d6b915e2SFlorian Westphal u16 max_df_size; /* largest frag with DF set seen */ 7989cee8b1SHerbert Xu int iif; 8089cee8b1SHerbert Xu unsigned int rid; 8189cee8b1SHerbert Xu struct inet_peer *peer; 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 84aa1f731eSFabian Frederick static u8 ip4_frag_ecn(u8 tos) 856623e3b2SEric Dumazet { 865173cc05SEric Dumazet return 1 << (tos & INET_ECN_MASK); 876623e3b2SEric Dumazet } 886623e3b2SEric Dumazet 897eb95156SPavel Emelyanov static struct inet_frags ip4_frags; 901da177e4SLinus Torvalds 916ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net) 927eb95156SPavel Emelyanov { 93d433673eSJesper Dangaard Brouer return sum_frag_mem_limit(&net->ipv4.frags); 947eb95156SPavel Emelyanov } 951da177e4SLinus Torvalds 961706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, 971706d587SHerbert Xu struct net_device *dev); 981706d587SHerbert Xu 99c6fda282SPavel Emelyanov struct ip4_create_arg { 100c6fda282SPavel Emelyanov struct iphdr *iph; 101c6fda282SPavel Emelyanov u32 user; 102c6fda282SPavel Emelyanov }; 103c6fda282SPavel Emelyanov 10418277770SAl Viro static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot) 1051da177e4SLinus Torvalds { 106e7b519baSHannes Frederic Sowa net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd)); 10718277770SAl Viro return jhash_3words((__force u32)id << 16 | prot, 10818277770SAl Viro (__force u32)saddr, (__force u32)daddr, 109fb3cfe6eSFlorian Westphal ip4_frags.rnd); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 11236c77782SFlorian Westphal static unsigned int ip4_hashfn(const struct inet_frag_queue *q) 1131da177e4SLinus Torvalds { 11436c77782SFlorian Westphal const struct ipq *ipq; 1151da177e4SLinus Torvalds 116321a3a99SPavel Emelyanov ipq = container_of(q, struct ipq, q); 117321a3a99SPavel Emelyanov return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 12036c77782SFlorian Westphal static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a) 121abd6523dSPavel Emelyanov { 12236c77782SFlorian Westphal const struct ipq *qp; 12336c77782SFlorian Westphal const struct ip4_create_arg *arg = a; 124abd6523dSPavel Emelyanov 125abd6523dSPavel Emelyanov qp = container_of(q, struct ipq, q); 126a02cec21SEric Dumazet return qp->id == arg->iph->id && 127abd6523dSPavel Emelyanov qp->saddr == arg->iph->saddr && 128abd6523dSPavel Emelyanov qp->daddr == arg->iph->daddr && 129abd6523dSPavel Emelyanov qp->protocol == arg->iph->protocol && 130a02cec21SEric Dumazet qp->user == arg->user; 131abd6523dSPavel Emelyanov } 132abd6523dSPavel Emelyanov 13336c77782SFlorian Westphal static void ip4_frag_init(struct inet_frag_queue *q, const void *a) 134c6fda282SPavel Emelyanov { 135c6fda282SPavel Emelyanov struct ipq *qp = container_of(q, struct ipq, q); 13654db0cc2SGao feng struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4, 13754db0cc2SGao feng frags); 13854db0cc2SGao feng struct net *net = container_of(ipv4, struct net, ipv4); 13954db0cc2SGao feng 14036c77782SFlorian Westphal const struct ip4_create_arg *arg = a; 141c6fda282SPavel Emelyanov 142c6fda282SPavel Emelyanov qp->protocol = arg->iph->protocol; 143c6fda282SPavel Emelyanov qp->id = arg->iph->id; 1446623e3b2SEric Dumazet qp->ecn = ip4_frag_ecn(arg->iph->tos); 145c6fda282SPavel Emelyanov qp->saddr = arg->iph->saddr; 146c6fda282SPavel Emelyanov qp->daddr = arg->iph->daddr; 147c6fda282SPavel Emelyanov qp->user = arg->user; 148c6fda282SPavel Emelyanov qp->peer = sysctl_ipfrag_max_dist ? 149c0efc887SDavid S. Miller inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL; 150c6fda282SPavel Emelyanov } 151c6fda282SPavel Emelyanov 152aa1f731eSFabian Frederick static void ip4_frag_free(struct inet_frag_queue *q) 1531da177e4SLinus Torvalds { 1541e4b8287SPavel Emelyanov struct ipq *qp; 1551e4b8287SPavel Emelyanov 1561e4b8287SPavel Emelyanov qp = container_of(q, struct ipq, q); 1571e4b8287SPavel Emelyanov if (qp->peer) 1581e4b8287SPavel Emelyanov inet_putpeer(qp->peer); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* Destruction primitives. */ 1631da177e4SLinus Torvalds 164aa1f731eSFabian Frederick static void ipq_put(struct ipq *ipq) 1651da177e4SLinus Torvalds { 166762cc408SPavel Emelyanov inet_frag_put(&ipq->q, &ip4_frags); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately, 1701da177e4SLinus Torvalds * because caller (and someone more) holds reference count. 1711da177e4SLinus Torvalds */ 1721da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq) 1731da177e4SLinus Torvalds { 174277e650dSPavel Emelyanov inet_frag_kill(&ipq->q, &ip4_frags); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1775cf42280SAndy Zhou static bool frag_expire_skip_icmp(u32 user) 1785cf42280SAndy Zhou { 1795cf42280SAndy Zhou return user == IP_DEFRAG_AF_PACKET || 1805cf42280SAndy Zhou ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN, 1818bc04864SAndy Zhou __IP_DEFRAG_CONNTRACK_IN_END) || 1828bc04864SAndy Zhou ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN, 1838bc04864SAndy Zhou __IP_DEFRAG_CONNTRACK_BRIDGE_IN); 1845cf42280SAndy Zhou } 1855cf42280SAndy Zhou 1861da177e4SLinus Torvalds /* 1871da177e4SLinus Torvalds * Oops, a fragment queue timed out. Kill it and send an ICMP reply. 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds static void ip_expire(unsigned long arg) 1901da177e4SLinus Torvalds { 191e521db9dSPavel Emelyanov struct ipq *qp; 19284a3aa00SPavel Emelyanov struct net *net; 193e521db9dSPavel Emelyanov 194e521db9dSPavel Emelyanov qp = container_of((struct inet_frag_queue *) arg, struct ipq, q); 19584a3aa00SPavel Emelyanov net = container_of(qp->q.net, struct net, ipv4.frags); 1961da177e4SLinus Torvalds 1975ab11c98SPavel Emelyanov spin_lock(&qp->q.lock); 1981da177e4SLinus Torvalds 19906aa8b8aSNikolay Aleksandrov if (qp->q.flags & INET_FRAG_COMPLETE) 2001da177e4SLinus Torvalds goto out; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds ipq_kill(qp); 2037c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 2041da177e4SLinus Torvalds 2052e404f63SNikolay Aleksandrov if (!(qp->q.flags & INET_FRAG_EVICTED)) { 2065ab11c98SPavel Emelyanov struct sk_buff *head = qp->q.fragments; 20764f3b9e2SEric Dumazet const struct iphdr *iph; 20864f3b9e2SEric Dumazet int err; 209cb84663eSDenis V. Lunev 2102e404f63SNikolay Aleksandrov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT); 2112e404f63SNikolay Aleksandrov 2122e404f63SNikolay Aleksandrov if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !qp->q.fragments) 2132e404f63SNikolay Aleksandrov goto out; 2142e404f63SNikolay Aleksandrov 21569df9d59SEric Dumazet rcu_read_lock(); 21669df9d59SEric Dumazet head->dev = dev_get_by_index_rcu(net, qp->iif); 217e9017b55SShan Wei if (!head->dev) 218e9017b55SShan Wei goto out_rcu_unlock; 219e9017b55SShan Wei 22097599dc7SEric Dumazet /* skb has no dst, perform route lookup again */ 22164f3b9e2SEric Dumazet iph = ip_hdr(head); 222c6cffba4SDavid S. Miller err = ip_route_input_noref(head, iph->daddr, iph->saddr, 223c10237e0SDavid S. Miller iph->tos, head->dev); 22464f3b9e2SEric Dumazet if (err) 225e9017b55SShan Wei goto out_rcu_unlock; 226e9017b55SShan Wei 2272e404f63SNikolay Aleksandrov /* Only an end host needs to send an ICMP 228e9017b55SShan Wei * "Fragment Reassembly Timeout" message, per RFC792. 229e9017b55SShan Wei */ 2305cf42280SAndy Zhou if (frag_expire_skip_icmp(qp->user) && 2315cf42280SAndy Zhou (skb_rtable(head)->rt_type != RTN_LOCAL)) 232e9017b55SShan Wei goto out_rcu_unlock; 233e9017b55SShan Wei 234e9017b55SShan Wei /* Send an ICMP "Fragment Reassembly Timeout" message. */ 235e9017b55SShan Wei icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); 236e9017b55SShan Wei out_rcu_unlock: 237e9017b55SShan Wei rcu_read_unlock(); 238d1c9ae6dSPatrick McHardy } 2391da177e4SLinus Torvalds out: 2405ab11c98SPavel Emelyanov spin_unlock(&qp->q.lock); 2414b6cb5d8SPavel Emelyanov ipq_put(qp); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 244abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for 245abd6523dSPavel Emelyanov * this IP datagram, and create new one, if nothing is found. 246abd6523dSPavel Emelyanov */ 247aa1f731eSFabian Frederick static struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user) 2481da177e4SLinus Torvalds { 249c6fda282SPavel Emelyanov struct inet_frag_queue *q; 250c6fda282SPavel Emelyanov struct ip4_create_arg arg; 251abd6523dSPavel Emelyanov unsigned int hash; 2521da177e4SLinus Torvalds 253c6fda282SPavel Emelyanov arg.iph = iph; 254c6fda282SPavel Emelyanov arg.user = user; 2559a375803SPavel Emelyanov 256abd6523dSPavel Emelyanov hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol); 257c6fda282SPavel Emelyanov 258ac18e750SPavel Emelyanov q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash); 2595a3da1feSHannes Frederic Sowa if (IS_ERR_OR_NULL(q)) { 2605a3da1feSHannes Frederic Sowa inet_frag_maybe_warn_overflow(q, pr_fmt()); 2611da177e4SLinus Torvalds return NULL; 2621da177e4SLinus Torvalds } 2635a3da1feSHannes Frederic Sowa return container_of(q, struct ipq, q); 2645a3da1feSHannes Frederic Sowa } 2651da177e4SLinus Torvalds 26689cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */ 267aa1f731eSFabian Frederick static int ip_frag_too_far(struct ipq *qp) 26889cee8b1SHerbert Xu { 26989cee8b1SHerbert Xu struct inet_peer *peer = qp->peer; 27089cee8b1SHerbert Xu unsigned int max = sysctl_ipfrag_max_dist; 27189cee8b1SHerbert Xu unsigned int start, end; 27289cee8b1SHerbert Xu 27389cee8b1SHerbert Xu int rc; 27489cee8b1SHerbert Xu 27589cee8b1SHerbert Xu if (!peer || !max) 27689cee8b1SHerbert Xu return 0; 27789cee8b1SHerbert Xu 27889cee8b1SHerbert Xu start = qp->rid; 27989cee8b1SHerbert Xu end = atomic_inc_return(&peer->rid); 28089cee8b1SHerbert Xu qp->rid = end; 28189cee8b1SHerbert Xu 2825ab11c98SPavel Emelyanov rc = qp->q.fragments && (end - start) > max; 28389cee8b1SHerbert Xu 28489cee8b1SHerbert Xu if (rc) { 2857c73a6faSPavel Emelyanov struct net *net; 2867c73a6faSPavel Emelyanov 2877c73a6faSPavel Emelyanov net = container_of(qp->q.net, struct net, ipv4.frags); 2887c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 28989cee8b1SHerbert Xu } 29089cee8b1SHerbert Xu 29189cee8b1SHerbert Xu return rc; 29289cee8b1SHerbert Xu } 29389cee8b1SHerbert Xu 29489cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp) 29589cee8b1SHerbert Xu { 29689cee8b1SHerbert Xu struct sk_buff *fp; 297d433673eSJesper Dangaard Brouer unsigned int sum_truesize = 0; 29889cee8b1SHerbert Xu 299b2fd5321SPavel Emelyanov if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) { 3005ab11c98SPavel Emelyanov atomic_inc(&qp->q.refcnt); 30189cee8b1SHerbert Xu return -ETIMEDOUT; 30289cee8b1SHerbert Xu } 30389cee8b1SHerbert Xu 3045ab11c98SPavel Emelyanov fp = qp->q.fragments; 30589cee8b1SHerbert Xu do { 30689cee8b1SHerbert Xu struct sk_buff *xp = fp->next; 307d433673eSJesper Dangaard Brouer 308d433673eSJesper Dangaard Brouer sum_truesize += fp->truesize; 309d433673eSJesper Dangaard Brouer kfree_skb(fp); 31089cee8b1SHerbert Xu fp = xp; 31189cee8b1SHerbert Xu } while (fp); 3120e60d245SFlorian Westphal sub_frag_mem_limit(qp->q.net, sum_truesize); 31389cee8b1SHerbert Xu 31406aa8b8aSNikolay Aleksandrov qp->q.flags = 0; 3155ab11c98SPavel Emelyanov qp->q.len = 0; 3165ab11c98SPavel Emelyanov qp->q.meat = 0; 3175ab11c98SPavel Emelyanov qp->q.fragments = NULL; 318d6bebca9SChangli Gao qp->q.fragments_tail = NULL; 31989cee8b1SHerbert Xu qp->iif = 0; 3206623e3b2SEric Dumazet qp->ecn = 0; 32189cee8b1SHerbert Xu 32289cee8b1SHerbert Xu return 0; 32389cee8b1SHerbert Xu } 32489cee8b1SHerbert Xu 3251da177e4SLinus Torvalds /* Add new segment to existing queue. */ 3261706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds struct sk_buff *prev, *next; 3291706d587SHerbert Xu struct net_device *dev; 330d6b915e2SFlorian Westphal unsigned int fragsize; 3311da177e4SLinus Torvalds int flags, offset; 3321da177e4SLinus Torvalds int ihl, end; 3331706d587SHerbert Xu int err = -ENOENT; 3346623e3b2SEric Dumazet u8 ecn; 3351da177e4SLinus Torvalds 33606aa8b8aSNikolay Aleksandrov if (qp->q.flags & INET_FRAG_COMPLETE) 3371da177e4SLinus Torvalds goto err; 3381da177e4SLinus Torvalds 33989cee8b1SHerbert Xu if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && 3401706d587SHerbert Xu unlikely(ip_frag_too_far(qp)) && 3411706d587SHerbert Xu unlikely(err = ip_frag_reinit(qp))) { 34289cee8b1SHerbert Xu ipq_kill(qp); 34389cee8b1SHerbert Xu goto err; 34489cee8b1SHerbert Xu } 34589cee8b1SHerbert Xu 3466623e3b2SEric Dumazet ecn = ip4_frag_ecn(ip_hdr(skb)->tos); 347eddc9ec5SArnaldo Carvalho de Melo offset = ntohs(ip_hdr(skb)->frag_off); 3481da177e4SLinus Torvalds flags = offset & ~IP_OFFSET; 3491da177e4SLinus Torvalds offset &= IP_OFFSET; 3501da177e4SLinus Torvalds offset <<= 3; /* offset is in 8-byte chunks */ 351c9bdd4b5SArnaldo Carvalho de Melo ihl = ip_hdrlen(skb); 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* Determine the position of this fragment. */ 3540848f642SEdward Hyunkoo Jee end = offset + skb->len - skb_network_offset(skb) - ihl; 3551706d587SHerbert Xu err = -EINVAL; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* Is this the final fragment? */ 3581da177e4SLinus Torvalds if ((flags & IP_MF) == 0) { 3591da177e4SLinus Torvalds /* If we already have some bits beyond end 36042b2aa86SJustin P. Mattock * or have different end, the segment is corrupted. 3611da177e4SLinus Torvalds */ 3625ab11c98SPavel Emelyanov if (end < qp->q.len || 36306aa8b8aSNikolay Aleksandrov ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) 3641da177e4SLinus Torvalds goto err; 36506aa8b8aSNikolay Aleksandrov qp->q.flags |= INET_FRAG_LAST_IN; 3665ab11c98SPavel Emelyanov qp->q.len = end; 3671da177e4SLinus Torvalds } else { 3681da177e4SLinus Torvalds if (end&7) { 3691da177e4SLinus Torvalds end &= ~7; 3701da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 3711da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3721da177e4SLinus Torvalds } 3735ab11c98SPavel Emelyanov if (end > qp->q.len) { 3741da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 37506aa8b8aSNikolay Aleksandrov if (qp->q.flags & INET_FRAG_LAST_IN) 3761da177e4SLinus Torvalds goto err; 3775ab11c98SPavel Emelyanov qp->q.len = end; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds if (end == offset) 3811da177e4SLinus Torvalds goto err; 3821da177e4SLinus Torvalds 3831706d587SHerbert Xu err = -ENOMEM; 3840848f642SEdward Hyunkoo Jee if (!pskb_pull(skb, skb_network_offset(skb) + ihl)) 3851da177e4SLinus Torvalds goto err; 3861706d587SHerbert Xu 3871706d587SHerbert Xu err = pskb_trim_rcsum(skb, end - offset); 3881706d587SHerbert Xu if (err) 3891da177e4SLinus Torvalds goto err; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* Find out which fragments are in front and at the back of us 3921da177e4SLinus Torvalds * in the chain of fragments so far. We must know where to put 3931da177e4SLinus Torvalds * this fragment, right? 3941da177e4SLinus Torvalds */ 395d6bebca9SChangli Gao prev = qp->q.fragments_tail; 396d6bebca9SChangli Gao if (!prev || FRAG_CB(prev)->offset < offset) { 397d6bebca9SChangli Gao next = NULL; 398d6bebca9SChangli Gao goto found; 399d6bebca9SChangli Gao } 4001da177e4SLinus Torvalds prev = NULL; 4015ab11c98SPavel Emelyanov for (next = qp->q.fragments; next != NULL; next = next->next) { 4021da177e4SLinus Torvalds if (FRAG_CB(next)->offset >= offset) 4031da177e4SLinus Torvalds break; /* bingo! */ 4041da177e4SLinus Torvalds prev = next; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 407d6bebca9SChangli Gao found: 4081da177e4SLinus Torvalds /* We found where to put this one. Check for overlap with 4091da177e4SLinus Torvalds * preceding fragment, and, if needed, align things so that 4101da177e4SLinus Torvalds * any overlaps are eliminated. 4111da177e4SLinus Torvalds */ 4121da177e4SLinus Torvalds if (prev) { 4131da177e4SLinus Torvalds int i = (FRAG_CB(prev)->offset + prev->len) - offset; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds if (i > 0) { 4161da177e4SLinus Torvalds offset += i; 4171706d587SHerbert Xu err = -EINVAL; 4181da177e4SLinus Torvalds if (end <= offset) 4191da177e4SLinus Torvalds goto err; 4201706d587SHerbert Xu err = -ENOMEM; 4211da177e4SLinus Torvalds if (!pskb_pull(skb, i)) 4221da177e4SLinus Torvalds goto err; 4231da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 4241da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281706d587SHerbert Xu err = -ENOMEM; 4291706d587SHerbert Xu 4301da177e4SLinus Torvalds while (next && FRAG_CB(next)->offset < end) { 4311da177e4SLinus Torvalds int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */ 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds if (i < next->len) { 4341da177e4SLinus Torvalds /* Eat head of the next overlapped fragment 4351da177e4SLinus Torvalds * and leave the loop. The next ones cannot overlap. 4361da177e4SLinus Torvalds */ 4371da177e4SLinus Torvalds if (!pskb_pull(next, i)) 4381da177e4SLinus Torvalds goto err; 4391da177e4SLinus Torvalds FRAG_CB(next)->offset += i; 4405ab11c98SPavel Emelyanov qp->q.meat -= i; 4411da177e4SLinus Torvalds if (next->ip_summed != CHECKSUM_UNNECESSARY) 4421da177e4SLinus Torvalds next->ip_summed = CHECKSUM_NONE; 4431da177e4SLinus Torvalds break; 4441da177e4SLinus Torvalds } else { 4451da177e4SLinus Torvalds struct sk_buff *free_it = next; 4461da177e4SLinus Torvalds 44747c6bf77SPeter Zijlstra /* Old fragment is completely overridden with 4481da177e4SLinus Torvalds * new one drop it. 4491da177e4SLinus Torvalds */ 4501da177e4SLinus Torvalds next = next->next; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds if (prev) 4531da177e4SLinus Torvalds prev->next = next; 4541da177e4SLinus Torvalds else 4555ab11c98SPavel Emelyanov qp->q.fragments = next; 4561da177e4SLinus Torvalds 4575ab11c98SPavel Emelyanov qp->q.meat -= free_it->len; 4580e60d245SFlorian Westphal sub_frag_mem_limit(qp->q.net, free_it->truesize); 459d433673eSJesper Dangaard Brouer kfree_skb(free_it); 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds FRAG_CB(skb)->offset = offset; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* Insert this fragment in the chain of fragments. */ 4661da177e4SLinus Torvalds skb->next = next; 467d6bebca9SChangli Gao if (!next) 468d6bebca9SChangli Gao qp->q.fragments_tail = skb; 4691da177e4SLinus Torvalds if (prev) 4701da177e4SLinus Torvalds prev->next = skb; 4711da177e4SLinus Torvalds else 4725ab11c98SPavel Emelyanov qp->q.fragments = skb; 4731da177e4SLinus Torvalds 4741706d587SHerbert Xu dev = skb->dev; 4751706d587SHerbert Xu if (dev) { 4761706d587SHerbert Xu qp->iif = dev->ifindex; 4771da177e4SLinus Torvalds skb->dev = NULL; 4781706d587SHerbert Xu } 4795ab11c98SPavel Emelyanov qp->q.stamp = skb->tstamp; 4805ab11c98SPavel Emelyanov qp->q.meat += skb->len; 4816623e3b2SEric Dumazet qp->ecn |= ecn; 4820e60d245SFlorian Westphal add_frag_mem_limit(qp->q.net, skb->truesize); 4831da177e4SLinus Torvalds if (offset == 0) 48406aa8b8aSNikolay Aleksandrov qp->q.flags |= INET_FRAG_FIRST_IN; 4851da177e4SLinus Torvalds 486d6b915e2SFlorian Westphal fragsize = skb->len + ihl; 487d6b915e2SFlorian Westphal 488d6b915e2SFlorian Westphal if (fragsize > qp->q.max_size) 489d6b915e2SFlorian Westphal qp->q.max_size = fragsize; 490d6b915e2SFlorian Westphal 4915f2d04f1SPatrick McHardy if (ip_hdr(skb)->frag_off & htons(IP_DF) && 492d6b915e2SFlorian Westphal fragsize > qp->max_df_size) 493d6b915e2SFlorian Westphal qp->max_df_size = fragsize; 4945f2d04f1SPatrick McHardy 49506aa8b8aSNikolay Aleksandrov if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 49697599dc7SEric Dumazet qp->q.meat == qp->q.len) { 49797599dc7SEric Dumazet unsigned long orefdst = skb->_skb_refdst; 4981706d587SHerbert Xu 49997599dc7SEric Dumazet skb->_skb_refdst = 0UL; 50097599dc7SEric Dumazet err = ip_frag_reasm(qp, prev, dev); 50197599dc7SEric Dumazet skb->_skb_refdst = orefdst; 50297599dc7SEric Dumazet return err; 50397599dc7SEric Dumazet } 50497599dc7SEric Dumazet 50597599dc7SEric Dumazet skb_dst_drop(skb); 5061706d587SHerbert Xu return -EINPROGRESS; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds err: 5091da177e4SLinus Torvalds kfree_skb(skb); 5101706d587SHerbert Xu return err; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */ 5151da177e4SLinus Torvalds 5161706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, 5171706d587SHerbert Xu struct net_device *dev) 5181da177e4SLinus Torvalds { 5192bad35b7SJorge Boncompte [DTI2] struct net *net = container_of(qp->q.net, struct net, ipv4.frags); 5201da177e4SLinus Torvalds struct iphdr *iph; 5215ab11c98SPavel Emelyanov struct sk_buff *fp, *head = qp->q.fragments; 5221da177e4SLinus Torvalds int len; 5231da177e4SLinus Torvalds int ihlen; 5241706d587SHerbert Xu int err; 5253cc49492SEric Dumazet int sum_truesize; 5265173cc05SEric Dumazet u8 ecn; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds ipq_kill(qp); 5291da177e4SLinus Torvalds 530be991971SHannes Frederic Sowa ecn = ip_frag_ecn_table[qp->ecn]; 5315173cc05SEric Dumazet if (unlikely(ecn == 0xff)) { 5325173cc05SEric Dumazet err = -EINVAL; 5335173cc05SEric Dumazet goto out_fail; 5345173cc05SEric Dumazet } 5351706d587SHerbert Xu /* Make the one we just received the head. */ 5361706d587SHerbert Xu if (prev) { 5371706d587SHerbert Xu head = prev->next; 5381706d587SHerbert Xu fp = skb_clone(head, GFP_ATOMIC); 5391706d587SHerbert Xu if (!fp) 5401706d587SHerbert Xu goto out_nomem; 5411706d587SHerbert Xu 5421706d587SHerbert Xu fp->next = head->next; 543d6bebca9SChangli Gao if (!fp->next) 544d6bebca9SChangli Gao qp->q.fragments_tail = fp; 5451706d587SHerbert Xu prev->next = fp; 5461706d587SHerbert Xu 5475ab11c98SPavel Emelyanov skb_morph(head, qp->q.fragments); 5485ab11c98SPavel Emelyanov head->next = qp->q.fragments->next; 5491706d587SHerbert Xu 550cbf8f7bbSEric Dumazet consume_skb(qp->q.fragments); 5515ab11c98SPavel Emelyanov qp->q.fragments = head; 5521706d587SHerbert Xu } 5531706d587SHerbert Xu 55451456b29SIan Morris WARN_ON(!head); 555547b792cSIlpo Järvinen WARN_ON(FRAG_CB(head)->offset != 0); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /* Allocate a new buffer for the datagram. */ 558c9bdd4b5SArnaldo Carvalho de Melo ihlen = ip_hdrlen(head); 5595ab11c98SPavel Emelyanov len = ihlen + qp->q.len; 5601da177e4SLinus Torvalds 5611706d587SHerbert Xu err = -E2BIG; 5621da177e4SLinus Torvalds if (len > 65535) 5631da177e4SLinus Torvalds goto out_oversize; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds /* Head of list must not be cloned. */ 56614bbd6a5SPravin B Shelar if (skb_unclone(head, GFP_ATOMIC)) 5671da177e4SLinus Torvalds goto out_nomem; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds /* If the first fragment is fragmented itself, we split 5701da177e4SLinus Torvalds * it to two chunks: the first with data and paged part 5711da177e4SLinus Torvalds * and the second, holding only fragments. */ 57221dc3301SDavid S. Miller if (skb_has_frag_list(head)) { 5731da177e4SLinus Torvalds struct sk_buff *clone; 5741da177e4SLinus Torvalds int i, plen = 0; 5751da177e4SLinus Torvalds 57651456b29SIan Morris clone = alloc_skb(0, GFP_ATOMIC); 57751456b29SIan Morris if (!clone) 5781da177e4SLinus Torvalds goto out_nomem; 5791da177e4SLinus Torvalds clone->next = head->next; 5801da177e4SLinus Torvalds head->next = clone; 5811da177e4SLinus Torvalds skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 582d7fcf1a5SDavid S. Miller skb_frag_list_init(head); 5831da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(head)->nr_frags; i++) 5849e903e08SEric Dumazet plen += skb_frag_size(&skb_shinfo(head)->frags[i]); 5851da177e4SLinus Torvalds clone->len = clone->data_len = head->data_len - plen; 5861da177e4SLinus Torvalds head->data_len -= clone->len; 5871da177e4SLinus Torvalds head->len -= clone->len; 5881da177e4SLinus Torvalds clone->csum = 0; 5891da177e4SLinus Torvalds clone->ip_summed = head->ip_summed; 5900e60d245SFlorian Westphal add_frag_mem_limit(qp->q.net, clone->truesize); 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 593d56f90a7SArnaldo Carvalho de Melo skb_push(head, head->data - skb_network_header(head)); 5941da177e4SLinus Torvalds 5953cc49492SEric Dumazet sum_truesize = head->truesize; 5963cc49492SEric Dumazet for (fp = head->next; fp;) { 5973cc49492SEric Dumazet bool headstolen; 5983cc49492SEric Dumazet int delta; 5993cc49492SEric Dumazet struct sk_buff *next = fp->next; 6003cc49492SEric Dumazet 6013cc49492SEric Dumazet sum_truesize += fp->truesize; 6021da177e4SLinus Torvalds if (head->ip_summed != fp->ip_summed) 6031da177e4SLinus Torvalds head->ip_summed = CHECKSUM_NONE; 60484fa7933SPatrick McHardy else if (head->ip_summed == CHECKSUM_COMPLETE) 6051da177e4SLinus Torvalds head->csum = csum_add(head->csum, fp->csum); 6063cc49492SEric Dumazet 6073cc49492SEric Dumazet if (skb_try_coalesce(head, fp, &headstolen, &delta)) { 6083cc49492SEric Dumazet kfree_skb_partial(fp, headstolen); 6093cc49492SEric Dumazet } else { 6103cc49492SEric Dumazet if (!skb_shinfo(head)->frag_list) 6113cc49492SEric Dumazet skb_shinfo(head)->frag_list = fp; 6123cc49492SEric Dumazet head->data_len += fp->len; 6133cc49492SEric Dumazet head->len += fp->len; 6141da177e4SLinus Torvalds head->truesize += fp->truesize; 6151da177e4SLinus Torvalds } 6163cc49492SEric Dumazet fp = next; 6173cc49492SEric Dumazet } 6180e60d245SFlorian Westphal sub_frag_mem_limit(qp->q.net, sum_truesize); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds head->next = NULL; 6211da177e4SLinus Torvalds head->dev = dev; 6225ab11c98SPavel Emelyanov head->tstamp = qp->q.stamp; 623d6b915e2SFlorian Westphal IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size); 6241da177e4SLinus Torvalds 625eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(head); 6261da177e4SLinus Torvalds iph->tot_len = htons(len); 6275173cc05SEric Dumazet iph->tos |= ecn; 628d6b915e2SFlorian Westphal 629d6b915e2SFlorian Westphal /* When we set IP_DF on a refragmented skb we must also force a 630d6b915e2SFlorian Westphal * call to ip_fragment to avoid forwarding a DF-skb of size s while 631d6b915e2SFlorian Westphal * original sender only sent fragments of size f (where f < s). 632d6b915e2SFlorian Westphal * 633d6b915e2SFlorian Westphal * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest 634d6b915e2SFlorian Westphal * frag seen to avoid sending tiny DF-fragments in case skb was built 635d6b915e2SFlorian Westphal * from one very small df-fragment and one large non-df frag. 636d6b915e2SFlorian Westphal */ 637d6b915e2SFlorian Westphal if (qp->max_df_size == qp->q.max_size) { 638d6b915e2SFlorian Westphal IPCB(head)->flags |= IPSKB_FRAG_PMTU; 639d6b915e2SFlorian Westphal iph->frag_off = htons(IP_DF); 640d6b915e2SFlorian Westphal } else { 641d6b915e2SFlorian Westphal iph->frag_off = 0; 642d6b915e2SFlorian Westphal } 643d6b915e2SFlorian Westphal 6440848f642SEdward Hyunkoo Jee ip_send_check(iph); 6450848f642SEdward Hyunkoo Jee 6462bad35b7SJorge Boncompte [DTI2] IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS); 6475ab11c98SPavel Emelyanov qp->q.fragments = NULL; 648d6bebca9SChangli Gao qp->q.fragments_tail = NULL; 6491706d587SHerbert Xu return 0; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds out_nomem: 652ba7a46f1SJoe Perches net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp); 65345542479SDavid Howells err = -ENOMEM; 6541da177e4SLinus Torvalds goto out_fail; 6551da177e4SLinus Torvalds out_oversize: 656e87cc472SJoe Perches net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr); 6571da177e4SLinus Torvalds out_fail: 658bbf31bf1SDavid Ford IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 6591706d587SHerbert Xu return err; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */ 663776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user) 6641da177e4SLinus Torvalds { 6651da177e4SLinus Torvalds struct ipq *qp; 666ac18e750SPavel Emelyanov struct net *net; 6671da177e4SLinus Torvalds 668adf30907SEric Dumazet net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev); 6697c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /* Lookup (or create) queue header */ 67200db4124SIan Morris qp = ip_find(net, ip_hdr(skb), user); 67300db4124SIan Morris if (qp) { 6741706d587SHerbert Xu int ret; 6751da177e4SLinus Torvalds 6765ab11c98SPavel Emelyanov spin_lock(&qp->q.lock); 6771da177e4SLinus Torvalds 6781706d587SHerbert Xu ret = ip_frag_queue(qp, skb); 6791da177e4SLinus Torvalds 6805ab11c98SPavel Emelyanov spin_unlock(&qp->q.lock); 6814b6cb5d8SPavel Emelyanov ipq_put(qp); 682776c729eSHerbert Xu return ret; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6857c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 6861da177e4SLinus Torvalds kfree_skb(skb); 687776c729eSHerbert Xu return -ENOMEM; 6881da177e4SLinus Torvalds } 6894bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_defrag); 6901da177e4SLinus Torvalds 691bc416d97SEric Dumazet struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user) 692bc416d97SEric Dumazet { 6931bf3751eSJohannes Berg struct iphdr iph; 6943e32e733SAlexander Drozdov int netoff; 695bc416d97SEric Dumazet u32 len; 696bc416d97SEric Dumazet 697bc416d97SEric Dumazet if (skb->protocol != htons(ETH_P_IP)) 698bc416d97SEric Dumazet return skb; 699bc416d97SEric Dumazet 7003e32e733SAlexander Drozdov netoff = skb_network_offset(skb); 7013e32e733SAlexander Drozdov 7023e32e733SAlexander Drozdov if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) 703bc416d97SEric Dumazet return skb; 704bc416d97SEric Dumazet 7051bf3751eSJohannes Berg if (iph.ihl < 5 || iph.version != 4) 706bc416d97SEric Dumazet return skb; 707bc416d97SEric Dumazet 7081bf3751eSJohannes Berg len = ntohs(iph.tot_len); 7093e32e733SAlexander Drozdov if (skb->len < netoff + len || len < (iph.ihl * 4)) 7101bf3751eSJohannes Berg return skb; 7111bf3751eSJohannes Berg 7121bf3751eSJohannes Berg if (ip_is_fragment(&iph)) { 713bc416d97SEric Dumazet skb = skb_share_check(skb, GFP_ATOMIC); 714bc416d97SEric Dumazet if (skb) { 7153e32e733SAlexander Drozdov if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) 7161bf3751eSJohannes Berg return skb; 7173e32e733SAlexander Drozdov if (pskb_trim_rcsum(skb, netoff + len)) 718bc416d97SEric Dumazet return skb; 719bc416d97SEric Dumazet memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 720bc416d97SEric Dumazet if (ip_defrag(skb, user)) 721bc416d97SEric Dumazet return NULL; 7227539fadcSTom Herbert skb_clear_hash(skb); 723bc416d97SEric Dumazet } 724bc416d97SEric Dumazet } 725bc416d97SEric Dumazet return skb; 726bc416d97SEric Dumazet } 727bc416d97SEric Dumazet EXPORT_SYMBOL(ip_check_defrag); 728bc416d97SEric Dumazet 7298d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 7308d8354d2SPavel Emelyanov static int zero; 7318d8354d2SPavel Emelyanov 7320a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = { 7338d8354d2SPavel Emelyanov { 7348d8354d2SPavel Emelyanov .procname = "ipfrag_high_thresh", 735e31e0bdcSPavel Emelyanov .data = &init_net.ipv4.frags.high_thresh, 7368d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7378d8354d2SPavel Emelyanov .mode = 0644, 7381bab4c75SNikolay Aleksandrov .proc_handler = proc_dointvec_minmax, 7391bab4c75SNikolay Aleksandrov .extra1 = &init_net.ipv4.frags.low_thresh 7408d8354d2SPavel Emelyanov }, 7418d8354d2SPavel Emelyanov { 7428d8354d2SPavel Emelyanov .procname = "ipfrag_low_thresh", 743e31e0bdcSPavel Emelyanov .data = &init_net.ipv4.frags.low_thresh, 7448d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7458d8354d2SPavel Emelyanov .mode = 0644, 7461bab4c75SNikolay Aleksandrov .proc_handler = proc_dointvec_minmax, 7471bab4c75SNikolay Aleksandrov .extra1 = &zero, 7481bab4c75SNikolay Aleksandrov .extra2 = &init_net.ipv4.frags.high_thresh 7498d8354d2SPavel Emelyanov }, 7508d8354d2SPavel Emelyanov { 7518d8354d2SPavel Emelyanov .procname = "ipfrag_time", 752b2fd5321SPavel Emelyanov .data = &init_net.ipv4.frags.timeout, 7538d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7548d8354d2SPavel Emelyanov .mode = 0644, 7556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 7568d8354d2SPavel Emelyanov }, 7577d291ebbSPavel Emelyanov { } 7587d291ebbSPavel Emelyanov }; 7597d291ebbSPavel Emelyanov 760e3a57d18SFlorian Westphal /* secret interval has been deprecated */ 761e3a57d18SFlorian Westphal static int ip4_frags_secret_interval_unused; 7627d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = { 7638d8354d2SPavel Emelyanov { 7648d8354d2SPavel Emelyanov .procname = "ipfrag_secret_interval", 765e3a57d18SFlorian Westphal .data = &ip4_frags_secret_interval_unused, 7668d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7678d8354d2SPavel Emelyanov .mode = 0644, 7686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 7698d8354d2SPavel Emelyanov }, 7708d8354d2SPavel Emelyanov { 7718d8354d2SPavel Emelyanov .procname = "ipfrag_max_dist", 7728d8354d2SPavel Emelyanov .data = &sysctl_ipfrag_max_dist, 7738d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7748d8354d2SPavel Emelyanov .mode = 0644, 7756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_minmax, 7768d8354d2SPavel Emelyanov .extra1 = &zero 7778d8354d2SPavel Emelyanov }, 7788d8354d2SPavel Emelyanov { } 7798d8354d2SPavel Emelyanov }; 7808d8354d2SPavel Emelyanov 7812c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net) 7828d8354d2SPavel Emelyanov { 783e4a2d5c2SPavel Emelyanov struct ctl_table *table; 7848d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 7858d8354d2SPavel Emelyanov 7860a64b4b8SPavel Emelyanov table = ip4_frags_ns_ctl_table; 78709ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 7880a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); 78951456b29SIan Morris if (!table) 790e4a2d5c2SPavel Emelyanov goto err_alloc; 791e4a2d5c2SPavel Emelyanov 792e31e0bdcSPavel Emelyanov table[0].data = &net->ipv4.frags.high_thresh; 7931bab4c75SNikolay Aleksandrov table[0].extra1 = &net->ipv4.frags.low_thresh; 7941bab4c75SNikolay Aleksandrov table[0].extra2 = &init_net.ipv4.frags.high_thresh; 795e31e0bdcSPavel Emelyanov table[1].data = &net->ipv4.frags.low_thresh; 7961bab4c75SNikolay Aleksandrov table[1].extra2 = &net->ipv4.frags.high_thresh; 797b2fd5321SPavel Emelyanov table[2].data = &net->ipv4.frags.timeout; 798464dc801SEric W. Biederman 799464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 800464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 801464dc801SEric W. Biederman table[0].procname = NULL; 802e4a2d5c2SPavel Emelyanov } 803e4a2d5c2SPavel Emelyanov 804ec8f23ceSEric W. Biederman hdr = register_net_sysctl(net, "net/ipv4", table); 80551456b29SIan Morris if (!hdr) 806e4a2d5c2SPavel Emelyanov goto err_reg; 807e4a2d5c2SPavel Emelyanov 808e4a2d5c2SPavel Emelyanov net->ipv4.frags_hdr = hdr; 809e4a2d5c2SPavel Emelyanov return 0; 810e4a2d5c2SPavel Emelyanov 811e4a2d5c2SPavel Emelyanov err_reg: 81209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 813e4a2d5c2SPavel Emelyanov kfree(table); 814e4a2d5c2SPavel Emelyanov err_alloc: 815e4a2d5c2SPavel Emelyanov return -ENOMEM; 816e4a2d5c2SPavel Emelyanov } 817e4a2d5c2SPavel Emelyanov 8182c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) 819e4a2d5c2SPavel Emelyanov { 820e4a2d5c2SPavel Emelyanov struct ctl_table *table; 821e4a2d5c2SPavel Emelyanov 822e4a2d5c2SPavel Emelyanov table = net->ipv4.frags_hdr->ctl_table_arg; 823e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv4.frags_hdr); 824e4a2d5c2SPavel Emelyanov kfree(table); 8258d8354d2SPavel Emelyanov } 8267d291ebbSPavel Emelyanov 82757a02c39SFabian Frederick static void __init ip4_frags_ctl_register(void) 8287d291ebbSPavel Emelyanov { 82943444757SEric W. Biederman register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); 8307d291ebbSPavel Emelyanov } 8318d8354d2SPavel Emelyanov #else 832aa1f731eSFabian Frederick static int ip4_frags_ns_ctl_register(struct net *net) 8338d8354d2SPavel Emelyanov { 8348d8354d2SPavel Emelyanov return 0; 8358d8354d2SPavel Emelyanov } 836e4a2d5c2SPavel Emelyanov 837aa1f731eSFabian Frederick static void ip4_frags_ns_ctl_unregister(struct net *net) 838e4a2d5c2SPavel Emelyanov { 839e4a2d5c2SPavel Emelyanov } 8407d291ebbSPavel Emelyanov 841aa1f731eSFabian Frederick static void __init ip4_frags_ctl_register(void) 8427d291ebbSPavel Emelyanov { 8437d291ebbSPavel Emelyanov } 8448d8354d2SPavel Emelyanov #endif 8458d8354d2SPavel Emelyanov 8462c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net) 8478d8354d2SPavel Emelyanov { 848c2a93660SJesper Dangaard Brouer /* Fragment cache limits. 849c2a93660SJesper Dangaard Brouer * 850c2a93660SJesper Dangaard Brouer * The fragment memory accounting code, (tries to) account for 851c2a93660SJesper Dangaard Brouer * the real memory usage, by measuring both the size of frag 852c2a93660SJesper Dangaard Brouer * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) 853c2a93660SJesper Dangaard Brouer * and the SKB's truesize. 854c2a93660SJesper Dangaard Brouer * 855c2a93660SJesper Dangaard Brouer * A 64K fragment consumes 129736 bytes (44*2944)+200 856c2a93660SJesper Dangaard Brouer * (1500 truesize == 2944, sizeof(struct ipq) == 200) 857c2a93660SJesper Dangaard Brouer * 858c2a93660SJesper Dangaard Brouer * We will commit 4MB at one time. Should we cross that limit 859c2a93660SJesper Dangaard Brouer * we will prune down to 3MB, making room for approx 8 big 64K 860c2a93660SJesper Dangaard Brouer * fragments 8x128k. 861e31e0bdcSPavel Emelyanov */ 862c2a93660SJesper Dangaard Brouer net->ipv4.frags.high_thresh = 4 * 1024 * 1024; 863c2a93660SJesper Dangaard Brouer net->ipv4.frags.low_thresh = 3 * 1024 * 1024; 864e31e0bdcSPavel Emelyanov /* 865b2fd5321SPavel Emelyanov * Important NOTE! Fragment queue must be destroyed before MSL expires. 866b2fd5321SPavel Emelyanov * RFC791 is wrong proposing to prolongate timer each fragment arrival 867b2fd5321SPavel Emelyanov * by TTL. 868b2fd5321SPavel Emelyanov */ 869b2fd5321SPavel Emelyanov net->ipv4.frags.timeout = IP_FRAG_TIME; 870b2fd5321SPavel Emelyanov 871e5a2bb84SPavel Emelyanov inet_frags_init_net(&net->ipv4.frags); 872e5a2bb84SPavel Emelyanov 8730a64b4b8SPavel Emelyanov return ip4_frags_ns_ctl_register(net); 8748d8354d2SPavel Emelyanov } 8758d8354d2SPavel Emelyanov 8762c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net) 87781566e83SPavel Emelyanov { 8780a64b4b8SPavel Emelyanov ip4_frags_ns_ctl_unregister(net); 87981566e83SPavel Emelyanov inet_frags_exit_net(&net->ipv4.frags, &ip4_frags); 88081566e83SPavel Emelyanov } 88181566e83SPavel Emelyanov 88281566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = { 88381566e83SPavel Emelyanov .init = ipv4_frags_init_net, 88481566e83SPavel Emelyanov .exit = ipv4_frags_exit_net, 88581566e83SPavel Emelyanov }; 88681566e83SPavel Emelyanov 887b7aa0bf7SEric Dumazet void __init ipfrag_init(void) 8881da177e4SLinus Torvalds { 8897d291ebbSPavel Emelyanov ip4_frags_ctl_register(); 89081566e83SPavel Emelyanov register_pernet_subsys(&ip4_frags_ops); 891321a3a99SPavel Emelyanov ip4_frags.hashfn = ip4_hashfn; 892c6fda282SPavel Emelyanov ip4_frags.constructor = ip4_frag_init; 8931e4b8287SPavel Emelyanov ip4_frags.destructor = ip4_frag_free; 8941e4b8287SPavel Emelyanov ip4_frags.skb_free = NULL; 8951e4b8287SPavel Emelyanov ip4_frags.qsize = sizeof(struct ipq); 896abd6523dSPavel Emelyanov ip4_frags.match = ip4_frag_match; 897e521db9dSPavel Emelyanov ip4_frags.frag_expire = ip_expire; 898d4ad4d22SNikolay Aleksandrov ip4_frags.frags_cache_name = ip_frag_cache_name; 899d4ad4d22SNikolay Aleksandrov if (inet_frags_init(&ip4_frags)) 900d4ad4d22SNikolay Aleksandrov panic("IP: failed to allocate ip4_frags cache\n"); 9011da177e4SLinus Torvalds } 902