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; 5889cee8b1SHerbert Xu 591da177e4SLinus Torvalds struct ipfrag_skb_cb 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds struct inet_skb_parm h; 621da177e4SLinus Torvalds int offset; 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb)) 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */ 681da177e4SLinus Torvalds struct ipq { 695ab11c98SPavel Emelyanov struct inet_frag_queue q; 705ab11c98SPavel Emelyanov 711da177e4SLinus Torvalds u32 user; 7218277770SAl Viro __be32 saddr; 7318277770SAl Viro __be32 daddr; 7418277770SAl Viro __be16 id; 751da177e4SLinus Torvalds u8 protocol; 766623e3b2SEric Dumazet u8 ecn; /* RFC3168 support */ 7789cee8b1SHerbert Xu int iif; 7889cee8b1SHerbert Xu unsigned int rid; 7989cee8b1SHerbert Xu struct inet_peer *peer; 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds 826623e3b2SEric Dumazet static inline u8 ip4_frag_ecn(u8 tos) 836623e3b2SEric Dumazet { 845173cc05SEric Dumazet return 1 << (tos & INET_ECN_MASK); 856623e3b2SEric Dumazet } 866623e3b2SEric Dumazet 877eb95156SPavel Emelyanov static struct inet_frags ip4_frags; 881da177e4SLinus Torvalds 89e5a2bb84SPavel Emelyanov int ip_frag_nqueues(struct net *net) 907eb95156SPavel Emelyanov { 91e5a2bb84SPavel Emelyanov return net->ipv4.frags.nqueues; 927eb95156SPavel Emelyanov } 931da177e4SLinus Torvalds 946ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net) 957eb95156SPavel Emelyanov { 96d433673eSJesper Dangaard Brouer return sum_frag_mem_limit(&net->ipv4.frags); 977eb95156SPavel Emelyanov } 981da177e4SLinus Torvalds 991706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, 1001706d587SHerbert Xu struct net_device *dev); 1011706d587SHerbert Xu 102c6fda282SPavel Emelyanov struct ip4_create_arg { 103c6fda282SPavel Emelyanov struct iphdr *iph; 104c6fda282SPavel Emelyanov u32 user; 105c6fda282SPavel Emelyanov }; 106c6fda282SPavel Emelyanov 10718277770SAl Viro static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot) 1081da177e4SLinus Torvalds { 109*e7b519baSHannes Frederic Sowa net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd)); 11018277770SAl Viro return jhash_3words((__force u32)id << 16 | prot, 11118277770SAl Viro (__force u32)saddr, (__force u32)daddr, 1127eb95156SPavel Emelyanov ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 115321a3a99SPavel Emelyanov static unsigned int ip4_hashfn(struct inet_frag_queue *q) 1161da177e4SLinus Torvalds { 117321a3a99SPavel Emelyanov struct ipq *ipq; 1181da177e4SLinus Torvalds 119321a3a99SPavel Emelyanov ipq = container_of(q, struct ipq, q); 120321a3a99SPavel Emelyanov return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 123cbc264caSEric Dumazet static bool ip4_frag_match(struct inet_frag_queue *q, void *a) 124abd6523dSPavel Emelyanov { 125abd6523dSPavel Emelyanov struct ipq *qp; 126abd6523dSPavel Emelyanov struct ip4_create_arg *arg = a; 127abd6523dSPavel Emelyanov 128abd6523dSPavel Emelyanov qp = container_of(q, struct ipq, q); 129a02cec21SEric Dumazet return qp->id == arg->iph->id && 130abd6523dSPavel Emelyanov qp->saddr == arg->iph->saddr && 131abd6523dSPavel Emelyanov qp->daddr == arg->iph->daddr && 132abd6523dSPavel Emelyanov qp->protocol == arg->iph->protocol && 133a02cec21SEric Dumazet qp->user == arg->user; 134abd6523dSPavel Emelyanov } 135abd6523dSPavel Emelyanov 136c6fda282SPavel Emelyanov static void ip4_frag_init(struct inet_frag_queue *q, void *a) 137c6fda282SPavel Emelyanov { 138c6fda282SPavel Emelyanov struct ipq *qp = container_of(q, struct ipq, q); 13954db0cc2SGao feng struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4, 14054db0cc2SGao feng frags); 14154db0cc2SGao feng struct net *net = container_of(ipv4, struct net, ipv4); 14254db0cc2SGao feng 143c6fda282SPavel Emelyanov struct ip4_create_arg *arg = a; 144c6fda282SPavel Emelyanov 145c6fda282SPavel Emelyanov qp->protocol = arg->iph->protocol; 146c6fda282SPavel Emelyanov qp->id = arg->iph->id; 1476623e3b2SEric Dumazet qp->ecn = ip4_frag_ecn(arg->iph->tos); 148c6fda282SPavel Emelyanov qp->saddr = arg->iph->saddr; 149c6fda282SPavel Emelyanov qp->daddr = arg->iph->daddr; 150c6fda282SPavel Emelyanov qp->user = arg->user; 151c6fda282SPavel Emelyanov qp->peer = sysctl_ipfrag_max_dist ? 152c0efc887SDavid S. Miller inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL; 153c6fda282SPavel Emelyanov } 154c6fda282SPavel Emelyanov 1551e4b8287SPavel Emelyanov static __inline__ void ip4_frag_free(struct inet_frag_queue *q) 1561da177e4SLinus Torvalds { 1571e4b8287SPavel Emelyanov struct ipq *qp; 1581e4b8287SPavel Emelyanov 1591e4b8287SPavel Emelyanov qp = container_of(q, struct ipq, q); 1601e4b8287SPavel Emelyanov if (qp->peer) 1611e4b8287SPavel Emelyanov inet_putpeer(qp->peer); 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* Destruction primitives. */ 1661da177e4SLinus Torvalds 1674b6cb5d8SPavel Emelyanov static __inline__ void ipq_put(struct ipq *ipq) 1681da177e4SLinus Torvalds { 169762cc408SPavel Emelyanov inet_frag_put(&ipq->q, &ip4_frags); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately, 1731da177e4SLinus Torvalds * because caller (and someone more) holds reference count. 1741da177e4SLinus Torvalds */ 1751da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq) 1761da177e4SLinus Torvalds { 177277e650dSPavel Emelyanov inet_frag_kill(&ipq->q, &ip4_frags); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* Memory limiting on fragments. Evictor trashes the oldest 1811da177e4SLinus Torvalds * fragment queue until we are back under the threshold. 1821da177e4SLinus Torvalds */ 1836ddc0822SPavel Emelyanov static void ip_evictor(struct net *net) 1841da177e4SLinus Torvalds { 1858e7999c4SPavel Emelyanov int evicted; 1861da177e4SLinus Torvalds 1876b102865SAmerigo Wang evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags, false); 1888e7999c4SPavel Emelyanov if (evicted) 189c5346fe3SPavel Emelyanov IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted); 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* 1931da177e4SLinus Torvalds * Oops, a fragment queue timed out. Kill it and send an ICMP reply. 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds static void ip_expire(unsigned long arg) 1961da177e4SLinus Torvalds { 197e521db9dSPavel Emelyanov struct ipq *qp; 19884a3aa00SPavel Emelyanov struct net *net; 199e521db9dSPavel Emelyanov 200e521db9dSPavel Emelyanov qp = container_of((struct inet_frag_queue *) arg, struct ipq, q); 20184a3aa00SPavel Emelyanov net = container_of(qp->q.net, struct net, ipv4.frags); 2021da177e4SLinus Torvalds 2035ab11c98SPavel Emelyanov spin_lock(&qp->q.lock); 2041da177e4SLinus Torvalds 205bc578a54SJoe Perches if (qp->q.last_in & INET_FRAG_COMPLETE) 2061da177e4SLinus Torvalds goto out; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds ipq_kill(qp); 2091da177e4SLinus Torvalds 2107c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT); 2117c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 2121da177e4SLinus Torvalds 213bc578a54SJoe Perches if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) { 2145ab11c98SPavel Emelyanov struct sk_buff *head = qp->q.fragments; 21564f3b9e2SEric Dumazet const struct iphdr *iph; 21664f3b9e2SEric Dumazet int err; 217cb84663eSDenis V. Lunev 21869df9d59SEric Dumazet rcu_read_lock(); 21969df9d59SEric Dumazet head->dev = dev_get_by_index_rcu(net, qp->iif); 220e9017b55SShan Wei if (!head->dev) 221e9017b55SShan Wei goto out_rcu_unlock; 222e9017b55SShan Wei 22397599dc7SEric Dumazet /* skb has no dst, perform route lookup again */ 22464f3b9e2SEric Dumazet iph = ip_hdr(head); 225c6cffba4SDavid S. Miller err = ip_route_input_noref(head, iph->daddr, iph->saddr, 226c10237e0SDavid S. Miller iph->tos, head->dev); 22764f3b9e2SEric Dumazet if (err) 228e9017b55SShan Wei goto out_rcu_unlock; 229e9017b55SShan Wei 230e9017b55SShan Wei /* 231e9017b55SShan Wei * Only an end host needs to send an ICMP 232e9017b55SShan Wei * "Fragment Reassembly Timeout" message, per RFC792. 233e9017b55SShan Wei */ 234595fc71bSDavid S. Miller if (qp->user == IP_DEFRAG_AF_PACKET || 235595fc71bSDavid S. Miller (qp->user == IP_DEFRAG_CONNTRACK_IN && 236595fc71bSDavid S. Miller skb_rtable(head)->rt_type != RTN_LOCAL)) 237e9017b55SShan Wei goto out_rcu_unlock; 238e9017b55SShan Wei 239e9017b55SShan Wei 240e9017b55SShan Wei /* Send an ICMP "Fragment Reassembly Timeout" message. */ 241e9017b55SShan Wei icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); 242e9017b55SShan Wei out_rcu_unlock: 243e9017b55SShan Wei rcu_read_unlock(); 244d1c9ae6dSPatrick McHardy } 2451da177e4SLinus Torvalds out: 2465ab11c98SPavel Emelyanov spin_unlock(&qp->q.lock); 2474b6cb5d8SPavel Emelyanov ipq_put(qp); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 250abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for 251abd6523dSPavel Emelyanov * this IP datagram, and create new one, if nothing is found. 252abd6523dSPavel Emelyanov */ 253ac18e750SPavel Emelyanov static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user) 2541da177e4SLinus Torvalds { 255c6fda282SPavel Emelyanov struct inet_frag_queue *q; 256c6fda282SPavel Emelyanov struct ip4_create_arg arg; 257abd6523dSPavel Emelyanov unsigned int hash; 2581da177e4SLinus Torvalds 259c6fda282SPavel Emelyanov arg.iph = iph; 260c6fda282SPavel Emelyanov arg.user = user; 2619a375803SPavel Emelyanov 2629a375803SPavel Emelyanov read_lock(&ip4_frags.lock); 263abd6523dSPavel Emelyanov hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol); 264c6fda282SPavel Emelyanov 265ac18e750SPavel Emelyanov q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash); 2665a3da1feSHannes Frederic Sowa if (IS_ERR_OR_NULL(q)) { 2675a3da1feSHannes Frederic Sowa inet_frag_maybe_warn_overflow(q, pr_fmt()); 2681da177e4SLinus Torvalds return NULL; 2691da177e4SLinus Torvalds } 2705a3da1feSHannes Frederic Sowa return container_of(q, struct ipq, q); 2715a3da1feSHannes Frederic Sowa } 2721da177e4SLinus Torvalds 27389cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */ 27489cee8b1SHerbert Xu static inline int ip_frag_too_far(struct ipq *qp) 27589cee8b1SHerbert Xu { 27689cee8b1SHerbert Xu struct inet_peer *peer = qp->peer; 27789cee8b1SHerbert Xu unsigned int max = sysctl_ipfrag_max_dist; 27889cee8b1SHerbert Xu unsigned int start, end; 27989cee8b1SHerbert Xu 28089cee8b1SHerbert Xu int rc; 28189cee8b1SHerbert Xu 28289cee8b1SHerbert Xu if (!peer || !max) 28389cee8b1SHerbert Xu return 0; 28489cee8b1SHerbert Xu 28589cee8b1SHerbert Xu start = qp->rid; 28689cee8b1SHerbert Xu end = atomic_inc_return(&peer->rid); 28789cee8b1SHerbert Xu qp->rid = end; 28889cee8b1SHerbert Xu 2895ab11c98SPavel Emelyanov rc = qp->q.fragments && (end - start) > max; 29089cee8b1SHerbert Xu 29189cee8b1SHerbert Xu if (rc) { 2927c73a6faSPavel Emelyanov struct net *net; 2937c73a6faSPavel Emelyanov 2947c73a6faSPavel Emelyanov net = container_of(qp->q.net, struct net, ipv4.frags); 2957c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 29689cee8b1SHerbert Xu } 29789cee8b1SHerbert Xu 29889cee8b1SHerbert Xu return rc; 29989cee8b1SHerbert Xu } 30089cee8b1SHerbert Xu 30189cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp) 30289cee8b1SHerbert Xu { 30389cee8b1SHerbert Xu struct sk_buff *fp; 304d433673eSJesper Dangaard Brouer unsigned int sum_truesize = 0; 30589cee8b1SHerbert Xu 306b2fd5321SPavel Emelyanov if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) { 3075ab11c98SPavel Emelyanov atomic_inc(&qp->q.refcnt); 30889cee8b1SHerbert Xu return -ETIMEDOUT; 30989cee8b1SHerbert Xu } 31089cee8b1SHerbert Xu 3115ab11c98SPavel Emelyanov fp = qp->q.fragments; 31289cee8b1SHerbert Xu do { 31389cee8b1SHerbert Xu struct sk_buff *xp = fp->next; 314d433673eSJesper Dangaard Brouer 315d433673eSJesper Dangaard Brouer sum_truesize += fp->truesize; 316d433673eSJesper Dangaard Brouer kfree_skb(fp); 31789cee8b1SHerbert Xu fp = xp; 31889cee8b1SHerbert Xu } while (fp); 319d433673eSJesper Dangaard Brouer sub_frag_mem_limit(&qp->q, sum_truesize); 32089cee8b1SHerbert Xu 3215ab11c98SPavel Emelyanov qp->q.last_in = 0; 3225ab11c98SPavel Emelyanov qp->q.len = 0; 3235ab11c98SPavel Emelyanov qp->q.meat = 0; 3245ab11c98SPavel Emelyanov qp->q.fragments = NULL; 325d6bebca9SChangli Gao qp->q.fragments_tail = NULL; 32689cee8b1SHerbert Xu qp->iif = 0; 3276623e3b2SEric Dumazet qp->ecn = 0; 32889cee8b1SHerbert Xu 32989cee8b1SHerbert Xu return 0; 33089cee8b1SHerbert Xu } 33189cee8b1SHerbert Xu 3321da177e4SLinus Torvalds /* Add new segment to existing queue. */ 3331706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds struct sk_buff *prev, *next; 3361706d587SHerbert Xu struct net_device *dev; 3371da177e4SLinus Torvalds int flags, offset; 3381da177e4SLinus Torvalds int ihl, end; 3391706d587SHerbert Xu int err = -ENOENT; 3406623e3b2SEric Dumazet u8 ecn; 3411da177e4SLinus Torvalds 342bc578a54SJoe Perches if (qp->q.last_in & INET_FRAG_COMPLETE) 3431da177e4SLinus Torvalds goto err; 3441da177e4SLinus Torvalds 34589cee8b1SHerbert Xu if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && 3461706d587SHerbert Xu unlikely(ip_frag_too_far(qp)) && 3471706d587SHerbert Xu unlikely(err = ip_frag_reinit(qp))) { 34889cee8b1SHerbert Xu ipq_kill(qp); 34989cee8b1SHerbert Xu goto err; 35089cee8b1SHerbert Xu } 35189cee8b1SHerbert Xu 3526623e3b2SEric Dumazet ecn = ip4_frag_ecn(ip_hdr(skb)->tos); 353eddc9ec5SArnaldo Carvalho de Melo offset = ntohs(ip_hdr(skb)->frag_off); 3541da177e4SLinus Torvalds flags = offset & ~IP_OFFSET; 3551da177e4SLinus Torvalds offset &= IP_OFFSET; 3561da177e4SLinus Torvalds offset <<= 3; /* offset is in 8-byte chunks */ 357c9bdd4b5SArnaldo Carvalho de Melo ihl = ip_hdrlen(skb); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* Determine the position of this fragment. */ 3601da177e4SLinus Torvalds end = offset + skb->len - ihl; 3611706d587SHerbert Xu err = -EINVAL; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds /* Is this the final fragment? */ 3641da177e4SLinus Torvalds if ((flags & IP_MF) == 0) { 3651da177e4SLinus Torvalds /* If we already have some bits beyond end 36642b2aa86SJustin P. Mattock * or have different end, the segment is corrupted. 3671da177e4SLinus Torvalds */ 3685ab11c98SPavel Emelyanov if (end < qp->q.len || 369bc578a54SJoe Perches ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len)) 3701da177e4SLinus Torvalds goto err; 371bc578a54SJoe Perches qp->q.last_in |= INET_FRAG_LAST_IN; 3725ab11c98SPavel Emelyanov qp->q.len = end; 3731da177e4SLinus Torvalds } else { 3741da177e4SLinus Torvalds if (end&7) { 3751da177e4SLinus Torvalds end &= ~7; 3761da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 3771da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3781da177e4SLinus Torvalds } 3795ab11c98SPavel Emelyanov if (end > qp->q.len) { 3801da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 381bc578a54SJoe Perches if (qp->q.last_in & INET_FRAG_LAST_IN) 3821da177e4SLinus Torvalds goto err; 3835ab11c98SPavel Emelyanov qp->q.len = end; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds if (end == offset) 3871da177e4SLinus Torvalds goto err; 3881da177e4SLinus Torvalds 3891706d587SHerbert Xu err = -ENOMEM; 3901da177e4SLinus Torvalds if (pskb_pull(skb, ihl) == NULL) 3911da177e4SLinus Torvalds goto err; 3921706d587SHerbert Xu 3931706d587SHerbert Xu err = pskb_trim_rcsum(skb, end - offset); 3941706d587SHerbert Xu if (err) 3951da177e4SLinus Torvalds goto err; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* Find out which fragments are in front and at the back of us 3981da177e4SLinus Torvalds * in the chain of fragments so far. We must know where to put 3991da177e4SLinus Torvalds * this fragment, right? 4001da177e4SLinus Torvalds */ 401d6bebca9SChangli Gao prev = qp->q.fragments_tail; 402d6bebca9SChangli Gao if (!prev || FRAG_CB(prev)->offset < offset) { 403d6bebca9SChangli Gao next = NULL; 404d6bebca9SChangli Gao goto found; 405d6bebca9SChangli Gao } 4061da177e4SLinus Torvalds prev = NULL; 4075ab11c98SPavel Emelyanov for (next = qp->q.fragments; next != NULL; next = next->next) { 4081da177e4SLinus Torvalds if (FRAG_CB(next)->offset >= offset) 4091da177e4SLinus Torvalds break; /* bingo! */ 4101da177e4SLinus Torvalds prev = next; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 413d6bebca9SChangli Gao found: 4141da177e4SLinus Torvalds /* We found where to put this one. Check for overlap with 4151da177e4SLinus Torvalds * preceding fragment, and, if needed, align things so that 4161da177e4SLinus Torvalds * any overlaps are eliminated. 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds if (prev) { 4191da177e4SLinus Torvalds int i = (FRAG_CB(prev)->offset + prev->len) - offset; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds if (i > 0) { 4221da177e4SLinus Torvalds offset += i; 4231706d587SHerbert Xu err = -EINVAL; 4241da177e4SLinus Torvalds if (end <= offset) 4251da177e4SLinus Torvalds goto err; 4261706d587SHerbert Xu err = -ENOMEM; 4271da177e4SLinus Torvalds if (!pskb_pull(skb, i)) 4281da177e4SLinus Torvalds goto err; 4291da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 4301da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341706d587SHerbert Xu err = -ENOMEM; 4351706d587SHerbert Xu 4361da177e4SLinus Torvalds while (next && FRAG_CB(next)->offset < end) { 4371da177e4SLinus Torvalds int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */ 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (i < next->len) { 4401da177e4SLinus Torvalds /* Eat head of the next overlapped fragment 4411da177e4SLinus Torvalds * and leave the loop. The next ones cannot overlap. 4421da177e4SLinus Torvalds */ 4431da177e4SLinus Torvalds if (!pskb_pull(next, i)) 4441da177e4SLinus Torvalds goto err; 4451da177e4SLinus Torvalds FRAG_CB(next)->offset += i; 4465ab11c98SPavel Emelyanov qp->q.meat -= i; 4471da177e4SLinus Torvalds if (next->ip_summed != CHECKSUM_UNNECESSARY) 4481da177e4SLinus Torvalds next->ip_summed = CHECKSUM_NONE; 4491da177e4SLinus Torvalds break; 4501da177e4SLinus Torvalds } else { 4511da177e4SLinus Torvalds struct sk_buff *free_it = next; 4521da177e4SLinus Torvalds 45347c6bf77SPeter Zijlstra /* Old fragment is completely overridden with 4541da177e4SLinus Torvalds * new one drop it. 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds next = next->next; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds if (prev) 4591da177e4SLinus Torvalds prev->next = next; 4601da177e4SLinus Torvalds else 4615ab11c98SPavel Emelyanov qp->q.fragments = next; 4621da177e4SLinus Torvalds 4635ab11c98SPavel Emelyanov qp->q.meat -= free_it->len; 464d433673eSJesper Dangaard Brouer sub_frag_mem_limit(&qp->q, free_it->truesize); 465d433673eSJesper Dangaard Brouer kfree_skb(free_it); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds FRAG_CB(skb)->offset = offset; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* Insert this fragment in the chain of fragments. */ 4721da177e4SLinus Torvalds skb->next = next; 473d6bebca9SChangli Gao if (!next) 474d6bebca9SChangli Gao qp->q.fragments_tail = skb; 4751da177e4SLinus Torvalds if (prev) 4761da177e4SLinus Torvalds prev->next = skb; 4771da177e4SLinus Torvalds else 4785ab11c98SPavel Emelyanov qp->q.fragments = skb; 4791da177e4SLinus Torvalds 4801706d587SHerbert Xu dev = skb->dev; 4811706d587SHerbert Xu if (dev) { 4821706d587SHerbert Xu qp->iif = dev->ifindex; 4831da177e4SLinus Torvalds skb->dev = NULL; 4841706d587SHerbert Xu } 4855ab11c98SPavel Emelyanov qp->q.stamp = skb->tstamp; 4865ab11c98SPavel Emelyanov qp->q.meat += skb->len; 4876623e3b2SEric Dumazet qp->ecn |= ecn; 488d433673eSJesper Dangaard Brouer add_frag_mem_limit(&qp->q, skb->truesize); 4891da177e4SLinus Torvalds if (offset == 0) 490bc578a54SJoe Perches qp->q.last_in |= INET_FRAG_FIRST_IN; 4911da177e4SLinus Torvalds 4925f2d04f1SPatrick McHardy if (ip_hdr(skb)->frag_off & htons(IP_DF) && 4935f2d04f1SPatrick McHardy skb->len + ihl > qp->q.max_size) 4945f2d04f1SPatrick McHardy qp->q.max_size = skb->len + ihl; 4955f2d04f1SPatrick McHardy 496bc578a54SJoe Perches if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 49797599dc7SEric Dumazet qp->q.meat == qp->q.len) { 49897599dc7SEric Dumazet unsigned long orefdst = skb->_skb_refdst; 4991706d587SHerbert Xu 50097599dc7SEric Dumazet skb->_skb_refdst = 0UL; 50197599dc7SEric Dumazet err = ip_frag_reasm(qp, prev, dev); 50297599dc7SEric Dumazet skb->_skb_refdst = orefdst; 50397599dc7SEric Dumazet return err; 50497599dc7SEric Dumazet } 50597599dc7SEric Dumazet 50697599dc7SEric Dumazet skb_dst_drop(skb); 5073ef0eb0dSJesper Dangaard Brouer inet_frag_lru_move(&qp->q); 5081706d587SHerbert Xu return -EINPROGRESS; 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds err: 5111da177e4SLinus Torvalds kfree_skb(skb); 5121706d587SHerbert Xu return err; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */ 5171da177e4SLinus Torvalds 5181706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, 5191706d587SHerbert Xu struct net_device *dev) 5201da177e4SLinus Torvalds { 5212bad35b7SJorge Boncompte [DTI2] struct net *net = container_of(qp->q.net, struct net, ipv4.frags); 5221da177e4SLinus Torvalds struct iphdr *iph; 5235ab11c98SPavel Emelyanov struct sk_buff *fp, *head = qp->q.fragments; 5241da177e4SLinus Torvalds int len; 5251da177e4SLinus Torvalds int ihlen; 5261706d587SHerbert Xu int err; 5273cc49492SEric Dumazet int sum_truesize; 5285173cc05SEric Dumazet u8 ecn; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds ipq_kill(qp); 5311da177e4SLinus Torvalds 532be991971SHannes Frederic Sowa ecn = ip_frag_ecn_table[qp->ecn]; 5335173cc05SEric Dumazet if (unlikely(ecn == 0xff)) { 5345173cc05SEric Dumazet err = -EINVAL; 5355173cc05SEric Dumazet goto out_fail; 5365173cc05SEric Dumazet } 5371706d587SHerbert Xu /* Make the one we just received the head. */ 5381706d587SHerbert Xu if (prev) { 5391706d587SHerbert Xu head = prev->next; 5401706d587SHerbert Xu fp = skb_clone(head, GFP_ATOMIC); 5411706d587SHerbert Xu if (!fp) 5421706d587SHerbert Xu goto out_nomem; 5431706d587SHerbert Xu 5441706d587SHerbert Xu fp->next = head->next; 545d6bebca9SChangli Gao if (!fp->next) 546d6bebca9SChangli Gao qp->q.fragments_tail = fp; 5471706d587SHerbert Xu prev->next = fp; 5481706d587SHerbert Xu 5495ab11c98SPavel Emelyanov skb_morph(head, qp->q.fragments); 5505ab11c98SPavel Emelyanov head->next = qp->q.fragments->next; 5511706d587SHerbert Xu 552cbf8f7bbSEric Dumazet consume_skb(qp->q.fragments); 5535ab11c98SPavel Emelyanov qp->q.fragments = head; 5541706d587SHerbert Xu } 5551706d587SHerbert Xu 556547b792cSIlpo Järvinen WARN_ON(head == NULL); 557547b792cSIlpo Järvinen WARN_ON(FRAG_CB(head)->offset != 0); 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds /* Allocate a new buffer for the datagram. */ 560c9bdd4b5SArnaldo Carvalho de Melo ihlen = ip_hdrlen(head); 5615ab11c98SPavel Emelyanov len = ihlen + qp->q.len; 5621da177e4SLinus Torvalds 5631706d587SHerbert Xu err = -E2BIG; 5641da177e4SLinus Torvalds if (len > 65535) 5651da177e4SLinus Torvalds goto out_oversize; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* Head of list must not be cloned. */ 56814bbd6a5SPravin B Shelar if (skb_unclone(head, GFP_ATOMIC)) 5691da177e4SLinus Torvalds goto out_nomem; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* If the first fragment is fragmented itself, we split 5721da177e4SLinus Torvalds * it to two chunks: the first with data and paged part 5731da177e4SLinus Torvalds * and the second, holding only fragments. */ 57421dc3301SDavid S. Miller if (skb_has_frag_list(head)) { 5751da177e4SLinus Torvalds struct sk_buff *clone; 5761da177e4SLinus Torvalds int i, plen = 0; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) 5791da177e4SLinus Torvalds goto out_nomem; 5801da177e4SLinus Torvalds clone->next = head->next; 5811da177e4SLinus Torvalds head->next = clone; 5821da177e4SLinus Torvalds skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 583d7fcf1a5SDavid S. Miller skb_frag_list_init(head); 5841da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(head)->nr_frags; i++) 5859e903e08SEric Dumazet plen += skb_frag_size(&skb_shinfo(head)->frags[i]); 5861da177e4SLinus Torvalds clone->len = clone->data_len = head->data_len - plen; 5871da177e4SLinus Torvalds head->data_len -= clone->len; 5881da177e4SLinus Torvalds head->len -= clone->len; 5891da177e4SLinus Torvalds clone->csum = 0; 5901da177e4SLinus Torvalds clone->ip_summed = head->ip_summed; 591d433673eSJesper Dangaard Brouer add_frag_mem_limit(&qp->q, clone->truesize); 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 594d56f90a7SArnaldo Carvalho de Melo skb_push(head, head->data - skb_network_header(head)); 5951da177e4SLinus Torvalds 5963cc49492SEric Dumazet sum_truesize = head->truesize; 5973cc49492SEric Dumazet for (fp = head->next; fp;) { 5983cc49492SEric Dumazet bool headstolen; 5993cc49492SEric Dumazet int delta; 6003cc49492SEric Dumazet struct sk_buff *next = fp->next; 6013cc49492SEric Dumazet 6023cc49492SEric Dumazet sum_truesize += fp->truesize; 6031da177e4SLinus Torvalds if (head->ip_summed != fp->ip_summed) 6041da177e4SLinus Torvalds head->ip_summed = CHECKSUM_NONE; 60584fa7933SPatrick McHardy else if (head->ip_summed == CHECKSUM_COMPLETE) 6061da177e4SLinus Torvalds head->csum = csum_add(head->csum, fp->csum); 6073cc49492SEric Dumazet 6083cc49492SEric Dumazet if (skb_try_coalesce(head, fp, &headstolen, &delta)) { 6093cc49492SEric Dumazet kfree_skb_partial(fp, headstolen); 6103cc49492SEric Dumazet } else { 6113cc49492SEric Dumazet if (!skb_shinfo(head)->frag_list) 6123cc49492SEric Dumazet skb_shinfo(head)->frag_list = fp; 6133cc49492SEric Dumazet head->data_len += fp->len; 6143cc49492SEric Dumazet head->len += fp->len; 6151da177e4SLinus Torvalds head->truesize += fp->truesize; 6161da177e4SLinus Torvalds } 6173cc49492SEric Dumazet fp = next; 6183cc49492SEric Dumazet } 619d433673eSJesper Dangaard Brouer sub_frag_mem_limit(&qp->q, sum_truesize); 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds head->next = NULL; 6221da177e4SLinus Torvalds head->dev = dev; 6235ab11c98SPavel Emelyanov head->tstamp = qp->q.stamp; 6245f2d04f1SPatrick McHardy IPCB(head)->frag_max_size = qp->q.max_size; 6251da177e4SLinus Torvalds 626eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(head); 6275f2d04f1SPatrick McHardy /* max_size != 0 implies at least one fragment had IP_DF set */ 6285f2d04f1SPatrick McHardy iph->frag_off = qp->q.max_size ? htons(IP_DF) : 0; 6291da177e4SLinus Torvalds iph->tot_len = htons(len); 6305173cc05SEric Dumazet iph->tos |= ecn; 6312bad35b7SJorge Boncompte [DTI2] IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS); 6325ab11c98SPavel Emelyanov qp->q.fragments = NULL; 633d6bebca9SChangli Gao qp->q.fragments_tail = NULL; 6341706d587SHerbert Xu return 0; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds out_nomem: 637afd46503SJoe Perches LIMIT_NETDEBUG(KERN_ERR pr_fmt("queue_glue: no memory for gluing queue %p\n"), 638afd46503SJoe Perches qp); 63945542479SDavid Howells err = -ENOMEM; 6401da177e4SLinus Torvalds goto out_fail; 6411da177e4SLinus Torvalds out_oversize: 642e87cc472SJoe Perches net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr); 6431da177e4SLinus Torvalds out_fail: 644bbf31bf1SDavid Ford IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 6451706d587SHerbert Xu return err; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */ 649776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user) 6501da177e4SLinus Torvalds { 6511da177e4SLinus Torvalds struct ipq *qp; 652ac18e750SPavel Emelyanov struct net *net; 6531da177e4SLinus Torvalds 654adf30907SEric Dumazet net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev); 6557c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds /* Start by cleaning up the memory. */ 6586ddc0822SPavel Emelyanov ip_evictor(net); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds /* Lookup (or create) queue header */ 661ac18e750SPavel Emelyanov if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) { 6621706d587SHerbert Xu int ret; 6631da177e4SLinus Torvalds 6645ab11c98SPavel Emelyanov spin_lock(&qp->q.lock); 6651da177e4SLinus Torvalds 6661706d587SHerbert Xu ret = ip_frag_queue(qp, skb); 6671da177e4SLinus Torvalds 6685ab11c98SPavel Emelyanov spin_unlock(&qp->q.lock); 6694b6cb5d8SPavel Emelyanov ipq_put(qp); 670776c729eSHerbert Xu return ret; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6737c73a6faSPavel Emelyanov IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS); 6741da177e4SLinus Torvalds kfree_skb(skb); 675776c729eSHerbert Xu return -ENOMEM; 6761da177e4SLinus Torvalds } 6774bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_defrag); 6781da177e4SLinus Torvalds 679bc416d97SEric Dumazet struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user) 680bc416d97SEric Dumazet { 6811bf3751eSJohannes Berg struct iphdr iph; 682bc416d97SEric Dumazet u32 len; 683bc416d97SEric Dumazet 684bc416d97SEric Dumazet if (skb->protocol != htons(ETH_P_IP)) 685bc416d97SEric Dumazet return skb; 686bc416d97SEric Dumazet 6871bf3751eSJohannes Berg if (!skb_copy_bits(skb, 0, &iph, sizeof(iph))) 688bc416d97SEric Dumazet return skb; 689bc416d97SEric Dumazet 6901bf3751eSJohannes Berg if (iph.ihl < 5 || iph.version != 4) 691bc416d97SEric Dumazet return skb; 692bc416d97SEric Dumazet 6931bf3751eSJohannes Berg len = ntohs(iph.tot_len); 6941bf3751eSJohannes Berg if (skb->len < len || len < (iph.ihl * 4)) 6951bf3751eSJohannes Berg return skb; 6961bf3751eSJohannes Berg 6971bf3751eSJohannes Berg if (ip_is_fragment(&iph)) { 698bc416d97SEric Dumazet skb = skb_share_check(skb, GFP_ATOMIC); 699bc416d97SEric Dumazet if (skb) { 7001bf3751eSJohannes Berg if (!pskb_may_pull(skb, iph.ihl*4)) 7011bf3751eSJohannes Berg return skb; 702bc416d97SEric Dumazet if (pskb_trim_rcsum(skb, len)) 703bc416d97SEric Dumazet return skb; 704bc416d97SEric Dumazet memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 705bc416d97SEric Dumazet if (ip_defrag(skb, user)) 706bc416d97SEric Dumazet return NULL; 707bc416d97SEric Dumazet skb->rxhash = 0; 708bc416d97SEric Dumazet } 709bc416d97SEric Dumazet } 710bc416d97SEric Dumazet return skb; 711bc416d97SEric Dumazet } 712bc416d97SEric Dumazet EXPORT_SYMBOL(ip_check_defrag); 713bc416d97SEric Dumazet 7148d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 7158d8354d2SPavel Emelyanov static int zero; 7168d8354d2SPavel Emelyanov 7170a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = { 7188d8354d2SPavel Emelyanov { 7198d8354d2SPavel Emelyanov .procname = "ipfrag_high_thresh", 720e31e0bdcSPavel Emelyanov .data = &init_net.ipv4.frags.high_thresh, 7218d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7228d8354d2SPavel Emelyanov .mode = 0644, 7236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec 7248d8354d2SPavel Emelyanov }, 7258d8354d2SPavel Emelyanov { 7268d8354d2SPavel Emelyanov .procname = "ipfrag_low_thresh", 727e31e0bdcSPavel Emelyanov .data = &init_net.ipv4.frags.low_thresh, 7288d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7298d8354d2SPavel Emelyanov .mode = 0644, 7306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec 7318d8354d2SPavel Emelyanov }, 7328d8354d2SPavel Emelyanov { 7338d8354d2SPavel Emelyanov .procname = "ipfrag_time", 734b2fd5321SPavel Emelyanov .data = &init_net.ipv4.frags.timeout, 7358d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7368d8354d2SPavel Emelyanov .mode = 0644, 7376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 7388d8354d2SPavel Emelyanov }, 7397d291ebbSPavel Emelyanov { } 7407d291ebbSPavel Emelyanov }; 7417d291ebbSPavel Emelyanov 7427d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = { 7438d8354d2SPavel Emelyanov { 7448d8354d2SPavel Emelyanov .procname = "ipfrag_secret_interval", 7453b4bc4a2SPavel Emelyanov .data = &ip4_frags.secret_interval, 7468d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7478d8354d2SPavel Emelyanov .mode = 0644, 7486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 7498d8354d2SPavel Emelyanov }, 7508d8354d2SPavel Emelyanov { 7518d8354d2SPavel Emelyanov .procname = "ipfrag_max_dist", 7528d8354d2SPavel Emelyanov .data = &sysctl_ipfrag_max_dist, 7538d8354d2SPavel Emelyanov .maxlen = sizeof(int), 7548d8354d2SPavel Emelyanov .mode = 0644, 7556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_minmax, 7568d8354d2SPavel Emelyanov .extra1 = &zero 7578d8354d2SPavel Emelyanov }, 7588d8354d2SPavel Emelyanov { } 7598d8354d2SPavel Emelyanov }; 7608d8354d2SPavel Emelyanov 7612c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net) 7628d8354d2SPavel Emelyanov { 763e4a2d5c2SPavel Emelyanov struct ctl_table *table; 7648d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 7658d8354d2SPavel Emelyanov 7660a64b4b8SPavel Emelyanov table = ip4_frags_ns_ctl_table; 76709ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 7680a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); 769e4a2d5c2SPavel Emelyanov if (table == NULL) 770e4a2d5c2SPavel Emelyanov goto err_alloc; 771e4a2d5c2SPavel Emelyanov 772e31e0bdcSPavel Emelyanov table[0].data = &net->ipv4.frags.high_thresh; 773e31e0bdcSPavel Emelyanov table[1].data = &net->ipv4.frags.low_thresh; 774b2fd5321SPavel Emelyanov table[2].data = &net->ipv4.frags.timeout; 775464dc801SEric W. Biederman 776464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 777464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 778464dc801SEric W. Biederman table[0].procname = NULL; 779e4a2d5c2SPavel Emelyanov } 780e4a2d5c2SPavel Emelyanov 781ec8f23ceSEric W. Biederman hdr = register_net_sysctl(net, "net/ipv4", table); 782e4a2d5c2SPavel Emelyanov if (hdr == NULL) 783e4a2d5c2SPavel Emelyanov goto err_reg; 784e4a2d5c2SPavel Emelyanov 785e4a2d5c2SPavel Emelyanov net->ipv4.frags_hdr = hdr; 786e4a2d5c2SPavel Emelyanov return 0; 787e4a2d5c2SPavel Emelyanov 788e4a2d5c2SPavel Emelyanov err_reg: 78909ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 790e4a2d5c2SPavel Emelyanov kfree(table); 791e4a2d5c2SPavel Emelyanov err_alloc: 792e4a2d5c2SPavel Emelyanov return -ENOMEM; 793e4a2d5c2SPavel Emelyanov } 794e4a2d5c2SPavel Emelyanov 7952c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) 796e4a2d5c2SPavel Emelyanov { 797e4a2d5c2SPavel Emelyanov struct ctl_table *table; 798e4a2d5c2SPavel Emelyanov 799e4a2d5c2SPavel Emelyanov table = net->ipv4.frags_hdr->ctl_table_arg; 800e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv4.frags_hdr); 801e4a2d5c2SPavel Emelyanov kfree(table); 8028d8354d2SPavel Emelyanov } 8037d291ebbSPavel Emelyanov 8047d291ebbSPavel Emelyanov static void ip4_frags_ctl_register(void) 8057d291ebbSPavel Emelyanov { 80643444757SEric W. Biederman register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); 8077d291ebbSPavel Emelyanov } 8088d8354d2SPavel Emelyanov #else 8090a64b4b8SPavel Emelyanov static inline int ip4_frags_ns_ctl_register(struct net *net) 8108d8354d2SPavel Emelyanov { 8118d8354d2SPavel Emelyanov return 0; 8128d8354d2SPavel Emelyanov } 813e4a2d5c2SPavel Emelyanov 8140a64b4b8SPavel Emelyanov static inline void ip4_frags_ns_ctl_unregister(struct net *net) 815e4a2d5c2SPavel Emelyanov { 816e4a2d5c2SPavel Emelyanov } 8177d291ebbSPavel Emelyanov 8187d291ebbSPavel Emelyanov static inline void ip4_frags_ctl_register(void) 8197d291ebbSPavel Emelyanov { 8207d291ebbSPavel Emelyanov } 8218d8354d2SPavel Emelyanov #endif 8228d8354d2SPavel Emelyanov 8232c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net) 8248d8354d2SPavel Emelyanov { 825c2a93660SJesper Dangaard Brouer /* Fragment cache limits. 826c2a93660SJesper Dangaard Brouer * 827c2a93660SJesper Dangaard Brouer * The fragment memory accounting code, (tries to) account for 828c2a93660SJesper Dangaard Brouer * the real memory usage, by measuring both the size of frag 829c2a93660SJesper Dangaard Brouer * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) 830c2a93660SJesper Dangaard Brouer * and the SKB's truesize. 831c2a93660SJesper Dangaard Brouer * 832c2a93660SJesper Dangaard Brouer * A 64K fragment consumes 129736 bytes (44*2944)+200 833c2a93660SJesper Dangaard Brouer * (1500 truesize == 2944, sizeof(struct ipq) == 200) 834c2a93660SJesper Dangaard Brouer * 835c2a93660SJesper Dangaard Brouer * We will commit 4MB at one time. Should we cross that limit 836c2a93660SJesper Dangaard Brouer * we will prune down to 3MB, making room for approx 8 big 64K 837c2a93660SJesper Dangaard Brouer * fragments 8x128k. 838e31e0bdcSPavel Emelyanov */ 839c2a93660SJesper Dangaard Brouer net->ipv4.frags.high_thresh = 4 * 1024 * 1024; 840c2a93660SJesper Dangaard Brouer net->ipv4.frags.low_thresh = 3 * 1024 * 1024; 841e31e0bdcSPavel Emelyanov /* 842b2fd5321SPavel Emelyanov * Important NOTE! Fragment queue must be destroyed before MSL expires. 843b2fd5321SPavel Emelyanov * RFC791 is wrong proposing to prolongate timer each fragment arrival 844b2fd5321SPavel Emelyanov * by TTL. 845b2fd5321SPavel Emelyanov */ 846b2fd5321SPavel Emelyanov net->ipv4.frags.timeout = IP_FRAG_TIME; 847b2fd5321SPavel Emelyanov 848e5a2bb84SPavel Emelyanov inet_frags_init_net(&net->ipv4.frags); 849e5a2bb84SPavel Emelyanov 8500a64b4b8SPavel Emelyanov return ip4_frags_ns_ctl_register(net); 8518d8354d2SPavel Emelyanov } 8528d8354d2SPavel Emelyanov 8532c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net) 85481566e83SPavel Emelyanov { 8550a64b4b8SPavel Emelyanov ip4_frags_ns_ctl_unregister(net); 85681566e83SPavel Emelyanov inet_frags_exit_net(&net->ipv4.frags, &ip4_frags); 85781566e83SPavel Emelyanov } 85881566e83SPavel Emelyanov 85981566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = { 86081566e83SPavel Emelyanov .init = ipv4_frags_init_net, 86181566e83SPavel Emelyanov .exit = ipv4_frags_exit_net, 86281566e83SPavel Emelyanov }; 86381566e83SPavel Emelyanov 864b7aa0bf7SEric Dumazet void __init ipfrag_init(void) 8651da177e4SLinus Torvalds { 8667d291ebbSPavel Emelyanov ip4_frags_ctl_register(); 86781566e83SPavel Emelyanov register_pernet_subsys(&ip4_frags_ops); 868321a3a99SPavel Emelyanov ip4_frags.hashfn = ip4_hashfn; 869c6fda282SPavel Emelyanov ip4_frags.constructor = ip4_frag_init; 8701e4b8287SPavel Emelyanov ip4_frags.destructor = ip4_frag_free; 8711e4b8287SPavel Emelyanov ip4_frags.skb_free = NULL; 8721e4b8287SPavel Emelyanov ip4_frags.qsize = sizeof(struct ipq); 873abd6523dSPavel Emelyanov ip4_frags.match = ip4_frag_match; 874e521db9dSPavel Emelyanov ip4_frags.frag_expire = ip_expire; 8753b4bc4a2SPavel Emelyanov ip4_frags.secret_interval = 10 * 60 * HZ; 8767eb95156SPavel Emelyanov inet_frags_init(&ip4_frags); 8771da177e4SLinus Torvalds } 878