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 Internet Protocol (IP) output module. 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Donald Becker, <becker@super.org> 111da177e4SLinus Torvalds * Alan Cox, <Alan.Cox@linux.org> 121da177e4SLinus Torvalds * Richard Underwood 131da177e4SLinus Torvalds * Stefan Becker, <stefanb@yello.ping.de> 141da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 151da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 161da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * See ip_input.c for original log 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Fixes: 211da177e4SLinus Torvalds * Alan Cox : Missing nonblock feature in ip_build_xmit. 221da177e4SLinus Torvalds * Mike Kilburn : htons() missing in ip_build_xmit. 231da177e4SLinus Torvalds * Bradford Johnson: Fix faulty handling of some frames when 241da177e4SLinus Torvalds * no route is found. 251da177e4SLinus Torvalds * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit 261da177e4SLinus Torvalds * (in case if packet not accepted by 271da177e4SLinus Torvalds * output firewall rules) 281da177e4SLinus Torvalds * Mike McLagan : Routing by source 291da177e4SLinus Torvalds * Alexey Kuznetsov: use new route cache 301da177e4SLinus Torvalds * Andi Kleen: Fix broken PMTU recovery and remove 311da177e4SLinus Torvalds * some redundant tests. 321da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 331da177e4SLinus Torvalds * Andi Kleen : Replace ip_reply with ip_send_reply. 341da177e4SLinus Torvalds * Andi Kleen : Split fast and slow ip_build_xmit path 351da177e4SLinus Torvalds * for decreased register pressure on x86 361da177e4SLinus Torvalds * and more readibility. 371da177e4SLinus Torvalds * Marc Boucher : When call_out_firewall returns FW_QUEUE, 381da177e4SLinus Torvalds * silently drop skb instead of failing with -EPERM. 391da177e4SLinus Torvalds * Detlev Wengorz : Copy protocol for fragments. 401da177e4SLinus Torvalds * Hirokazu Takahashi: HW checksumming for outgoing UDP 411da177e4SLinus Torvalds * datagrams. 421da177e4SLinus Torvalds * Hirokazu Takahashi: sendfile() on UDP works now. 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <asm/uaccess.h> 461da177e4SLinus Torvalds #include <linux/module.h> 471da177e4SLinus Torvalds #include <linux/types.h> 481da177e4SLinus Torvalds #include <linux/kernel.h> 491da177e4SLinus Torvalds #include <linux/mm.h> 501da177e4SLinus Torvalds #include <linux/string.h> 511da177e4SLinus Torvalds #include <linux/errno.h> 52a1f8e7f7SAl Viro #include <linux/highmem.h> 535a0e3ad6STejun Heo #include <linux/slab.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <linux/socket.h> 561da177e4SLinus Torvalds #include <linux/sockios.h> 571da177e4SLinus Torvalds #include <linux/in.h> 581da177e4SLinus Torvalds #include <linux/inet.h> 591da177e4SLinus Torvalds #include <linux/netdevice.h> 601da177e4SLinus Torvalds #include <linux/etherdevice.h> 611da177e4SLinus Torvalds #include <linux/proc_fs.h> 621da177e4SLinus Torvalds #include <linux/stat.h> 631da177e4SLinus Torvalds #include <linux/init.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include <net/snmp.h> 661da177e4SLinus Torvalds #include <net/ip.h> 671da177e4SLinus Torvalds #include <net/protocol.h> 681da177e4SLinus Torvalds #include <net/route.h> 69cfacb057SPatrick McHardy #include <net/xfrm.h> 701da177e4SLinus Torvalds #include <linux/skbuff.h> 711da177e4SLinus Torvalds #include <net/sock.h> 721da177e4SLinus Torvalds #include <net/arp.h> 731da177e4SLinus Torvalds #include <net/icmp.h> 741da177e4SLinus Torvalds #include <net/checksum.h> 751da177e4SLinus Torvalds #include <net/inetpeer.h> 761da177e4SLinus Torvalds #include <linux/igmp.h> 771da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 781da177e4SLinus Torvalds #include <linux/netfilter_bridge.h> 791da177e4SLinus Torvalds #include <linux/mroute.h> 801da177e4SLinus Torvalds #include <linux/netlink.h> 816cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h> 821da177e4SLinus Torvalds 83ab32ea5dSBrian Haley int sysctl_ip_default_ttl __read_mostly = IPDEFTTL; 84323e126fSDavid S. Miller EXPORT_SYMBOL(sysctl_ip_default_ttl); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */ 871da177e4SLinus Torvalds __inline__ void ip_send_check(struct iphdr *iph) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds iph->check = 0; 901da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 911da177e4SLinus Torvalds } 924bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check); 931da177e4SLinus Torvalds 94c439cb2eSHerbert Xu int __ip_local_out(struct sk_buff *skb) 95c439cb2eSHerbert Xu { 96c439cb2eSHerbert Xu struct iphdr *iph = ip_hdr(skb); 97c439cb2eSHerbert Xu 98c439cb2eSHerbert Xu iph->tot_len = htons(skb->len); 99c439cb2eSHerbert Xu ip_send_check(iph); 1009bbc768aSJan Engelhardt return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL, 1019bbc768aSJan Engelhardt skb_dst(skb)->dev, dst_output); 102c439cb2eSHerbert Xu } 103c439cb2eSHerbert Xu 104c439cb2eSHerbert Xu int ip_local_out(struct sk_buff *skb) 105c439cb2eSHerbert Xu { 106c439cb2eSHerbert Xu int err; 107c439cb2eSHerbert Xu 108c439cb2eSHerbert Xu err = __ip_local_out(skb); 109c439cb2eSHerbert Xu if (likely(err == 1)) 110c439cb2eSHerbert Xu err = dst_output(skb); 111c439cb2eSHerbert Xu 112c439cb2eSHerbert Xu return err; 113c439cb2eSHerbert Xu } 114c439cb2eSHerbert Xu EXPORT_SYMBOL_GPL(ip_local_out); 115c439cb2eSHerbert Xu 1161da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds int ttl = inet->uc_ttl; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (ttl < 0) 121323e126fSDavid S. Miller ttl = ip4_dst_hoplimit(dst); 1221da177e4SLinus Torvalds return ttl; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Add an ip header to a skbuff and send it out. 1271da177e4SLinus Torvalds * 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, 130f6d8bd05SEric Dumazet __be32 saddr, __be32 daddr, struct ip_options_rcu *opt) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 133511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1341da177e4SLinus Torvalds struct iphdr *iph; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* Build the IP header. */ 137f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0)); 1388856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 139eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1401da177e4SLinus Torvalds iph->version = 4; 1411da177e4SLinus Torvalds iph->ihl = 5; 1421da177e4SLinus Torvalds iph->tos = inet->tos; 143d8d1f30bSChangli Gao if (ip_dont_fragment(sk, &rt->dst)) 1441da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 1451da177e4SLinus Torvalds else 1461da177e4SLinus Torvalds iph->frag_off = 0; 147d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 148dd927a26SDavid S. Miller iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); 149dd927a26SDavid S. Miller iph->saddr = saddr; 1501da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 151d8d1f30bSChangli Gao ip_select_ident(iph, &rt->dst, sk); 1521da177e4SLinus Torvalds 153f6d8bd05SEric Dumazet if (opt && opt->opt.optlen) { 154f6d8bd05SEric Dumazet iph->ihl += opt->opt.optlen>>2; 155f6d8bd05SEric Dumazet ip_options_build(skb, &opt->opt, daddr, rt, 0); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds skb->priority = sk->sk_priority; 1594a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* Send it out. */ 162c439cb2eSHerbert Xu return ip_local_out(skb); 1631da177e4SLinus Torvalds } 164d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 165d8c97a94SArnaldo Carvalho de Melo 1661da177e4SLinus Torvalds static inline int ip_finish_output2(struct sk_buff *skb) 1671da177e4SLinus Torvalds { 168adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 16980787ebcSMitsuru Chinen struct rtable *rt = (struct rtable *)dst; 1701da177e4SLinus Torvalds struct net_device *dev = dst->dev; 171c2636b4dSChuck Lever unsigned int hh_len = LL_RESERVED_SPACE(dev); 172f6b72b62SDavid S. Miller struct neighbour *neigh; 173a263b309SDavid S. Miller u32 nexthop; 1741da177e4SLinus Torvalds 175edf391ffSNeil Horman if (rt->rt_type == RTN_MULTICAST) { 176edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len); 177edf391ffSNeil Horman } else if (rt->rt_type == RTN_BROADCAST) 178edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len); 17980787ebcSMitsuru Chinen 1801da177e4SLinus Torvalds /* Be paranoid, rather than too clever. */ 1813b04dddeSStephen Hemminger if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 1821da177e4SLinus Torvalds struct sk_buff *skb2; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); 1851da177e4SLinus Torvalds if (skb2 == NULL) { 1861da177e4SLinus Torvalds kfree_skb(skb); 1871da177e4SLinus Torvalds return -ENOMEM; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds if (skb->sk) 1901da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 1915d0ba55bSEric Dumazet consume_skb(skb); 1921da177e4SLinus Torvalds skb = skb2; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195a263b309SDavid S. Miller rcu_read_lock_bh(); 196155e8336SJulian Anastasov nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr); 197a263b309SDavid S. Miller neigh = __ipv4_neigh_lookup_noref(dev, nexthop); 198a263b309SDavid S. Miller if (unlikely(!neigh)) 199a263b309SDavid S. Miller neigh = __neigh_create(&arp_tbl, &nexthop, dev, false); 2009871f1adSVasiliy Kulikov if (!IS_ERR(neigh)) { 2015110effeSDavid S. Miller int res = dst_neigh_output(dst, neigh, skb); 202f2c31e32SEric Dumazet 203a263b309SDavid S. Miller rcu_read_unlock_bh(); 204f2c31e32SEric Dumazet return res; 205f2c31e32SEric Dumazet } 206a263b309SDavid S. Miller rcu_read_unlock_bh(); 20705e3aa09SDavid S. Miller 208e87cc472SJoe Perches net_dbg_ratelimited("%s: No header cache and no neighbour!\n", 209e87cc472SJoe Perches __func__); 2101da177e4SLinus Torvalds kfree_skb(skb); 2111da177e4SLinus Torvalds return -EINVAL; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214628a5c56SJohn Heffner static inline int ip_skb_dst_mtu(struct sk_buff *skb) 215628a5c56SJohn Heffner { 216628a5c56SJohn Heffner struct inet_sock *inet = skb->sk ? inet_sk(skb->sk) : NULL; 217628a5c56SJohn Heffner 218628a5c56SJohn Heffner return (inet && inet->pmtudisc == IP_PMTUDISC_PROBE) ? 219adf30907SEric Dumazet skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb)); 220628a5c56SJohn Heffner } 221628a5c56SJohn Heffner 222861d0486SPatrick McHardy static int ip_finish_output(struct sk_buff *skb) 2231da177e4SLinus Torvalds { 2245c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 2255c901daaSPatrick McHardy /* Policy lookup after SNAT yielded a new policy */ 226adf30907SEric Dumazet if (skb_dst(skb)->xfrm != NULL) { 22748d5cad8SPatrick McHardy IPCB(skb)->flags |= IPSKB_REROUTED; 22848d5cad8SPatrick McHardy return dst_output(skb); 22948d5cad8SPatrick McHardy } 2305c901daaSPatrick McHardy #endif 231628a5c56SJohn Heffner if (skb->len > ip_skb_dst_mtu(skb) && !skb_is_gso(skb)) 2321bd9bef6SPatrick McHardy return ip_fragment(skb, ip_finish_output2); 2331bd9bef6SPatrick McHardy else 2341bd9bef6SPatrick McHardy return ip_finish_output2(skb); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds int ip_mc_output(struct sk_buff *skb) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds struct sock *sk = skb->sk; 240511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 241d8d1f30bSChangli Gao struct net_device *dev = rt->dst.dev; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * If the indicated interface is up and running, send the packet. 2451da177e4SLinus Torvalds */ 246edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds skb->dev = dev; 2491da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /* 2521da177e4SLinus Torvalds * Multicasts are looped back for other local users 2531da177e4SLinus Torvalds */ 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds if (rt->rt_flags&RTCF_MULTICAST) { 2567ad6848cSOctavian Purdila if (sk_mc_loop(sk) 2571da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 2581da177e4SLinus Torvalds /* Small optimization: do not loopback not local frames, 2591da177e4SLinus Torvalds which returned after forwarding; they will be dropped 2601da177e4SLinus Torvalds by ip_mr_input in any case. 2611da177e4SLinus Torvalds Note, that local frames are looped back to be delivered 2621da177e4SLinus Torvalds to local recipients. 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds This check is duplicated in ip_mr_input at the moment. 2651da177e4SLinus Torvalds */ 2669d4fb27dSJoe Perches && 2679d4fb27dSJoe Perches ((rt->rt_flags & RTCF_LOCAL) || 2689d4fb27dSJoe Perches !(IPCB(skb)->flags & IPSKB_FORWARDED)) 2691da177e4SLinus Torvalds #endif 2701da177e4SLinus Torvalds ) { 2711da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 2721da177e4SLinus Torvalds if (newskb) 2739bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 2749bbc768aSJan Engelhardt newskb, NULL, newskb->dev, 27595603e22SMichel Machado dev_loopback_xmit); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /* Multicasts with ttl 0 must not go beyond the host */ 2791da177e4SLinus Torvalds 280eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl == 0) { 2811da177e4SLinus Torvalds kfree_skb(skb); 2821da177e4SLinus Torvalds return 0; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds if (rt->rt_flags&RTCF_BROADCAST) { 2871da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 2881da177e4SLinus Torvalds if (newskb) 2899bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, newskb, 29095603e22SMichel Machado NULL, newskb->dev, dev_loopback_xmit); 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2939bbc768aSJan Engelhardt return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, 2949bbc768aSJan Engelhardt skb->dev, ip_finish_output, 29548d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds int ip_output(struct sk_buff *skb) 2991da177e4SLinus Torvalds { 300adf30907SEric Dumazet struct net_device *dev = skb_dst(skb)->dev; 3011bd9bef6SPatrick McHardy 302edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); 3031da177e4SLinus Torvalds 3041bd9bef6SPatrick McHardy skb->dev = dev; 3051bd9bef6SPatrick McHardy skb->protocol = htons(ETH_P_IP); 3061bd9bef6SPatrick McHardy 3079bbc768aSJan Engelhardt return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, dev, 30848d5cad8SPatrick McHardy ip_finish_output, 30948d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 31284f9307cSEric Dumazet /* 31384f9307cSEric Dumazet * copy saddr and daddr, possibly using 64bit load/stores 31484f9307cSEric Dumazet * Equivalent to : 31584f9307cSEric Dumazet * iph->saddr = fl4->saddr; 31684f9307cSEric Dumazet * iph->daddr = fl4->daddr; 31784f9307cSEric Dumazet */ 31884f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4) 31984f9307cSEric Dumazet { 32084f9307cSEric Dumazet BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) != 32184f9307cSEric Dumazet offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr)); 32284f9307cSEric Dumazet memcpy(&iph->saddr, &fl4->saddr, 32384f9307cSEric Dumazet sizeof(fl4->saddr) + sizeof(fl4->daddr)); 32484f9307cSEric Dumazet } 32584f9307cSEric Dumazet 326d9d8da80SDavid S. Miller int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl) 3271da177e4SLinus Torvalds { 328e89862f4SDavid S. Miller struct sock *sk = skb->sk; 3291da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 330f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 331b57ae01aSDavid S. Miller struct flowi4 *fl4; 3321da177e4SLinus Torvalds struct rtable *rt; 3331da177e4SLinus Torvalds struct iphdr *iph; 334ab6e3febSEric Dumazet int res; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* Skip all of this if the packet is already routed, 3371da177e4SLinus Torvalds * f.e. by something like SCTP. 3381da177e4SLinus Torvalds */ 339ab6e3febSEric Dumazet rcu_read_lock(); 340f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 341ea4fc0d6SDavid S. Miller fl4 = &fl->u.ip4; 342511c3f92SEric Dumazet rt = skb_rtable(skb); 3431da177e4SLinus Torvalds if (rt != NULL) 3441da177e4SLinus Torvalds goto packet_routed; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* Make sure we can route this packet. */ 3471da177e4SLinus Torvalds rt = (struct rtable *)__sk_dst_check(sk, 0); 3481da177e4SLinus Torvalds if (rt == NULL) { 3493ca3c68eSAl Viro __be32 daddr; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* Use correct destination address if we have options. */ 352c720c7e8SEric Dumazet daddr = inet->inet_daddr; 353f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 354f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds /* If this fails, retransmit mechanism of transport layer will 3571da177e4SLinus Torvalds * keep trying until route appears or the connection times 3581da177e4SLinus Torvalds * itself out. 3591da177e4SLinus Torvalds */ 360b57ae01aSDavid S. Miller rt = ip_route_output_ports(sock_net(sk), fl4, sk, 36178fbfd8aSDavid S. Miller daddr, inet->inet_saddr, 36278fbfd8aSDavid S. Miller inet->inet_dport, 36378fbfd8aSDavid S. Miller inet->inet_sport, 36478fbfd8aSDavid S. Miller sk->sk_protocol, 36578fbfd8aSDavid S. Miller RT_CONN_FLAGS(sk), 36678fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 367b23dd4feSDavid S. Miller if (IS_ERR(rt)) 3681da177e4SLinus Torvalds goto no_route; 369d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 3701da177e4SLinus Torvalds } 371d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds packet_routed: 374155e8336SJulian Anastasov if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway) 3751da177e4SLinus Torvalds goto no_route; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /* OK, we know where to send it, allocate and build IP header. */ 378f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0)); 3798856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 380eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 381714e85beSAl Viro *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff)); 382d8d1f30bSChangli Gao if (ip_dont_fragment(sk, &rt->dst) && !skb->local_df) 3831da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 3841da177e4SLinus Torvalds else 3851da177e4SLinus Torvalds iph->frag_off = 0; 386d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 3871da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 38884f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 38984f9307cSEric Dumazet 3901da177e4SLinus Torvalds /* Transport layer set skb->h.foo itself. */ 3911da177e4SLinus Torvalds 392f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.optlen) { 393f6d8bd05SEric Dumazet iph->ihl += inet_opt->opt.optlen >> 2; 394f6d8bd05SEric Dumazet ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 397d8d1f30bSChangli Gao ip_select_ident_more(iph, &rt->dst, sk, 3987967168cSHerbert Xu (skb_shinfo(skb)->gso_segs ?: 1) - 1); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds skb->priority = sk->sk_priority; 4014a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 4021da177e4SLinus Torvalds 403ab6e3febSEric Dumazet res = ip_local_out(skb); 404ab6e3febSEric Dumazet rcu_read_unlock(); 405ab6e3febSEric Dumazet return res; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds no_route: 408ab6e3febSEric Dumazet rcu_read_unlock(); 4095e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); 4101da177e4SLinus Torvalds kfree_skb(skb); 4111da177e4SLinus Torvalds return -EHOSTUNREACH; 4121da177e4SLinus Torvalds } 4134bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit); 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 4191da177e4SLinus Torvalds to->priority = from->priority; 4201da177e4SLinus Torvalds to->protocol = from->protocol; 421adf30907SEric Dumazet skb_dst_drop(to); 422fe76cda3SEric Dumazet skb_dst_copy(to, from); 4231da177e4SLinus Torvalds to->dev = from->dev; 42482e91ffeSThomas Graf to->mark = from->mark; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* Copy the flags to each fragment. */ 4271da177e4SLinus Torvalds IPCB(to)->flags = IPCB(from)->flags; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4301da177e4SLinus Torvalds to->tc_index = from->tc_index; 4311da177e4SLinus Torvalds #endif 432e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 433*f0165888SGao feng #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 434ba9dda3aSJozsef Kadlecsik to->nf_trace = from->nf_trace; 435ba9dda3aSJozsef Kadlecsik #endif 436c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 437c98d80edSJulian Anastasov to->ipvs_property = from->ipvs_property; 438c98d80edSJulian Anastasov #endif 439984bc16cSJames Morris skb_copy_secmark(to, from); 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /* 4431da177e4SLinus Torvalds * This IP datagram is too large to be sent in one piece. Break it up into 4441da177e4SLinus Torvalds * smaller pieces (each of size equal to IP header plus 4451da177e4SLinus Torvalds * a block of the data of the original IP data part) that will yet fit in a 4461da177e4SLinus Torvalds * single device frame, and queue such a frame for sending. 4471da177e4SLinus Torvalds */ 4481da177e4SLinus Torvalds 4492e2f7aefSPatrick McHardy int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds struct iphdr *iph; 4521da177e4SLinus Torvalds int ptr; 4531da177e4SLinus Torvalds struct net_device *dev; 4541da177e4SLinus Torvalds struct sk_buff *skb2; 455c893b806SChangli Gao unsigned int mtu, hlen, left, len, ll_rs; 4561da177e4SLinus Torvalds int offset; 45776ab608dSAlexey Dobriyan __be16 not_last_frag; 458511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 4591da177e4SLinus Torvalds int err = 0; 4601da177e4SLinus Torvalds 461d8d1f30bSChangli Gao dev = rt->dst.dev; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* 4641da177e4SLinus Torvalds * Point into the IP datagram header. 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds 467eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 4681da177e4SLinus Torvalds 4695f2d04f1SPatrick McHardy if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->local_df) || 4705f2d04f1SPatrick McHardy (IPCB(skb)->frag_max_size && 4715f2d04f1SPatrick McHardy IPCB(skb)->frag_max_size > dst_mtu(&rt->dst)))) { 4725e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 4731da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 474628a5c56SJohn Heffner htonl(ip_skb_dst_mtu(skb))); 4751da177e4SLinus Torvalds kfree_skb(skb); 4761da177e4SLinus Torvalds return -EMSGSIZE; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * Setup starting values. 4811da177e4SLinus Torvalds */ 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds hlen = iph->ihl * 4; 484d8d1f30bSChangli Gao mtu = dst_mtu(&rt->dst) - hlen; /* Size of data space */ 4856c79bf0fSBart De Schuymer #ifdef CONFIG_BRIDGE_NETFILTER 4866c79bf0fSBart De Schuymer if (skb->nf_bridge) 4876c79bf0fSBart De Schuymer mtu -= nf_bridge_mtu_reduction(skb); 4886c79bf0fSBart De Schuymer #endif 48989cee8b1SHerbert Xu IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* When frag_list is given, use it. First, check its validity: 4921da177e4SLinus Torvalds * some transformers could create wrong frag_list or break existing 4931da177e4SLinus Torvalds * one, it is not prohibited. In this case fall back to copying. 4941da177e4SLinus Torvalds * 4951da177e4SLinus Torvalds * LATER: this step can be merged to real generation of fragments, 4961da177e4SLinus Torvalds * we can switch to copy when see the first bad fragment. 4971da177e4SLinus Torvalds */ 49821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 4993d13008eSEric Dumazet struct sk_buff *frag, *frag2; 5001da177e4SLinus Torvalds int first_len = skb_pagelen(skb); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds if (first_len - hlen > mtu || 5031da177e4SLinus Torvalds ((first_len - hlen) & 7) || 50456f8a75cSPaul Gortmaker ip_is_fragment(iph) || 5051da177e4SLinus Torvalds skb_cloned(skb)) 5061da177e4SLinus Torvalds goto slow_path; 5071da177e4SLinus Torvalds 508d7fcf1a5SDavid S. Miller skb_walk_frags(skb, frag) { 5091da177e4SLinus Torvalds /* Correct geometry. */ 5101da177e4SLinus Torvalds if (frag->len > mtu || 5111da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 5121da177e4SLinus Torvalds skb_headroom(frag) < hlen) 5133d13008eSEric Dumazet goto slow_path_clean; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* Partially cloned skb? */ 5161da177e4SLinus Torvalds if (skb_shared(frag)) 5173d13008eSEric Dumazet goto slow_path_clean; 5182fdba6b0SHerbert Xu 5192fdba6b0SHerbert Xu BUG_ON(frag->sk); 5202fdba6b0SHerbert Xu if (skb->sk) { 5212fdba6b0SHerbert Xu frag->sk = skb->sk; 5222fdba6b0SHerbert Xu frag->destructor = sock_wfree; 5232fdba6b0SHerbert Xu } 5243d13008eSEric Dumazet skb->truesize -= frag->truesize; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /* Everything is OK. Generate! */ 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds err = 0; 5301da177e4SLinus Torvalds offset = 0; 5311da177e4SLinus Torvalds frag = skb_shinfo(skb)->frag_list; 532d7fcf1a5SDavid S. Miller skb_frag_list_init(skb); 5331da177e4SLinus Torvalds skb->data_len = first_len - skb_headlen(skb); 5341da177e4SLinus Torvalds skb->len = first_len; 5351da177e4SLinus Torvalds iph->tot_len = htons(first_len); 5361da177e4SLinus Torvalds iph->frag_off = htons(IP_MF); 5371da177e4SLinus Torvalds ip_send_check(iph); 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds for (;;) { 5401da177e4SLinus Torvalds /* Prepare header of the next frame, 5411da177e4SLinus Torvalds * before previous one went down. */ 5421da177e4SLinus Torvalds if (frag) { 5431da177e4SLinus Torvalds frag->ip_summed = CHECKSUM_NONE; 544badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(frag); 545e2d1bca7SArnaldo Carvalho de Melo __skb_push(frag, hlen); 546e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(frag); 547d56f90a7SArnaldo Carvalho de Melo memcpy(skb_network_header(frag), iph, hlen); 548eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(frag); 5491da177e4SLinus Torvalds iph->tot_len = htons(frag->len); 5501da177e4SLinus Torvalds ip_copy_metadata(frag, skb); 5511da177e4SLinus Torvalds if (offset == 0) 5521da177e4SLinus Torvalds ip_options_fragment(frag); 5531da177e4SLinus Torvalds offset += skb->len - hlen; 5541da177e4SLinus Torvalds iph->frag_off = htons(offset>>3); 5551da177e4SLinus Torvalds if (frag->next != NULL) 5561da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 5571da177e4SLinus Torvalds /* Ready, complete checksum */ 5581da177e4SLinus Torvalds ip_send_check(iph); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds err = output(skb); 5621da177e4SLinus Torvalds 563dafee490SWei Dong if (!err) 5645e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES); 5651da177e4SLinus Torvalds if (err || !frag) 5661da177e4SLinus Torvalds break; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds skb = frag; 5691da177e4SLinus Torvalds frag = skb->next; 5701da177e4SLinus Torvalds skb->next = NULL; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds if (err == 0) { 5745e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS); 5751da177e4SLinus Torvalds return 0; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds while (frag) { 5791da177e4SLinus Torvalds skb = frag->next; 5801da177e4SLinus Torvalds kfree_skb(frag); 5811da177e4SLinus Torvalds frag = skb; 5821da177e4SLinus Torvalds } 5835e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 5841da177e4SLinus Torvalds return err; 5853d13008eSEric Dumazet 5863d13008eSEric Dumazet slow_path_clean: 5873d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 5883d13008eSEric Dumazet if (frag2 == frag) 5893d13008eSEric Dumazet break; 5903d13008eSEric Dumazet frag2->sk = NULL; 5913d13008eSEric Dumazet frag2->destructor = NULL; 5923d13008eSEric Dumazet skb->truesize += frag2->truesize; 5933d13008eSEric Dumazet } 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds slow_path: 597fc70fb64SAlexander Duyck /* for offloaded checksums cleanup checksum before fragmentation */ 598fc70fb64SAlexander Duyck if ((skb->ip_summed == CHECKSUM_PARTIAL) && skb_checksum_help(skb)) 599fc70fb64SAlexander Duyck goto fail; 600c9af6db4SPravin B Shelar iph = ip_hdr(skb); 601fc70fb64SAlexander Duyck 6021da177e4SLinus Torvalds left = skb->len - hlen; /* Space per frame */ 60349085bd7SGeorge Kadianakis ptr = hlen; /* Where to start from */ 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* for bridged IP traffic encapsulated inside f.e. a vlan header, 6069bcfcaf5SStephen Hemminger * we need to make room for the encapsulating header 6079bcfcaf5SStephen Hemminger */ 608c893b806SChangli Gao ll_rs = LL_RESERVED_SPACE_EXTRA(rt->dst.dev, nf_bridge_pad(skb)); 6099bcfcaf5SStephen Hemminger 6101da177e4SLinus Torvalds /* 6111da177e4SLinus Torvalds * Fragment the datagram. 6121da177e4SLinus Torvalds */ 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; 6151da177e4SLinus Torvalds not_last_frag = iph->frag_off & htons(IP_MF); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /* 6181da177e4SLinus Torvalds * Keep copying data until we run out. 6191da177e4SLinus Torvalds */ 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds while (left > 0) { 6221da177e4SLinus Torvalds len = left; 6231da177e4SLinus Torvalds /* IF: it doesn't fit, use 'mtu' - the data space left */ 6241da177e4SLinus Torvalds if (len > mtu) 6251da177e4SLinus Torvalds len = mtu; 6261da177e4SLinus Torvalds /* IF: we are not sending up to and including the packet end 6271da177e4SLinus Torvalds then align the next start on an eight byte boundary */ 6281da177e4SLinus Torvalds if (len < left) { 6291da177e4SLinus Torvalds len &= ~7; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds /* 6321da177e4SLinus Torvalds * Allocate buffer. 6331da177e4SLinus Torvalds */ 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds if ((skb2 = alloc_skb(len+hlen+ll_rs, GFP_ATOMIC)) == NULL) { 63664ce2073SPatrick McHardy NETDEBUG(KERN_INFO "IP: frag: no memory for new fragment!\n"); 6371da177e4SLinus Torvalds err = -ENOMEM; 6381da177e4SLinus Torvalds goto fail; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds /* 6421da177e4SLinus Torvalds * Set up data on packet 6431da177e4SLinus Torvalds */ 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds ip_copy_metadata(skb2, skb); 6461da177e4SLinus Torvalds skb_reserve(skb2, ll_rs); 6471da177e4SLinus Torvalds skb_put(skb2, len + hlen); 648c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 649b0e380b1SArnaldo Carvalho de Melo skb2->transport_header = skb2->network_header + hlen; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* 6521da177e4SLinus Torvalds * Charge the memory for the fragment to any owner 6531da177e4SLinus Torvalds * it might possess 6541da177e4SLinus Torvalds */ 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (skb->sk) 6571da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds /* 6601da177e4SLinus Torvalds * Copy the packet header into the new buffer. 6611da177e4SLinus Torvalds */ 6621da177e4SLinus Torvalds 663d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen); 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* 6661da177e4SLinus Torvalds * Copy a block of the IP datagram. 6671da177e4SLinus Torvalds */ 668bff9b61cSArnaldo Carvalho de Melo if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len)) 6691da177e4SLinus Torvalds BUG(); 6701da177e4SLinus Torvalds left -= len; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds /* 6731da177e4SLinus Torvalds * Fill in the new header fields. 6741da177e4SLinus Torvalds */ 675eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 6761da177e4SLinus Torvalds iph->frag_off = htons((offset >> 3)); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* ANK: dirty, but effective trick. Upgrade options only if 6791da177e4SLinus Torvalds * the segment to be fragmented was THE FIRST (otherwise, 6801da177e4SLinus Torvalds * options are already fixed) and make it ONCE 6811da177e4SLinus Torvalds * on the initial skb, so that all the following fragments 6821da177e4SLinus Torvalds * will inherit fixed options. 6831da177e4SLinus Torvalds */ 6841da177e4SLinus Torvalds if (offset == 0) 6851da177e4SLinus Torvalds ip_options_fragment(skb); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds /* 6881da177e4SLinus Torvalds * Added AC : If we are fragmenting a fragment that's not the 6891da177e4SLinus Torvalds * last fragment then keep MF on each bit 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds if (left > 0 || not_last_frag) 6921da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 6931da177e4SLinus Torvalds ptr += len; 6941da177e4SLinus Torvalds offset += len; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* 6971da177e4SLinus Torvalds * Put this fragment into the sending queue. 6981da177e4SLinus Torvalds */ 6991da177e4SLinus Torvalds iph->tot_len = htons(len + hlen); 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds ip_send_check(iph); 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds err = output(skb2); 7041da177e4SLinus Torvalds if (err) 7051da177e4SLinus Torvalds goto fail; 706dafee490SWei Dong 7075e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES); 7081da177e4SLinus Torvalds } 7095d0ba55bSEric Dumazet consume_skb(skb); 7105e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS); 7111da177e4SLinus Torvalds return err; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds fail: 7141da177e4SLinus Torvalds kfree_skb(skb); 7155e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 7161da177e4SLinus Torvalds return err; 7171da177e4SLinus Torvalds } 7182e2f7aefSPatrick McHardy EXPORT_SYMBOL(ip_fragment); 7192e2f7aefSPatrick McHardy 7201da177e4SLinus Torvalds int 7211da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds struct iovec *iov = from; 7241da177e4SLinus Torvalds 72584fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 7261da177e4SLinus Torvalds if (memcpy_fromiovecend(to, iov, offset, len) < 0) 7271da177e4SLinus Torvalds return -EFAULT; 7281da177e4SLinus Torvalds } else { 72944bb9363SAl Viro __wsum csum = 0; 7301da177e4SLinus Torvalds if (csum_partial_copy_fromiovecend(to, iov, offset, len, &csum) < 0) 7311da177e4SLinus Torvalds return -EFAULT; 7321da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds return 0; 7351da177e4SLinus Torvalds } 7364bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag); 7371da177e4SLinus Torvalds 73844bb9363SAl Viro static inline __wsum 7391da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds char *kaddr; 74244bb9363SAl Viro __wsum csum; 7431da177e4SLinus Torvalds kaddr = kmap(page); 7441da177e4SLinus Torvalds csum = csum_partial(kaddr + offset, copy, 0); 7451da177e4SLinus Torvalds kunmap(page); 7461da177e4SLinus Torvalds return csum; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7494b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk, 7501470ddf7SHerbert Xu struct sk_buff_head *queue, 751e89e9cf5SAnanda Raju int getfrag(void *from, char *to, int offset, int len, 752e89e9cf5SAnanda Raju int odd, struct sk_buff *skb), 753e89e9cf5SAnanda Raju void *from, int length, int hh_len, int fragheaderlen, 754d9be4f7aSBill Sommerfeld int transhdrlen, int maxfraglen, unsigned int flags) 755e89e9cf5SAnanda Raju { 756e89e9cf5SAnanda Raju struct sk_buff *skb; 757e89e9cf5SAnanda Raju int err; 758e89e9cf5SAnanda Raju 759e89e9cf5SAnanda Raju /* There is support for UDP fragmentation offload by network 760e89e9cf5SAnanda Raju * device, so create one single skb packet containing complete 761e89e9cf5SAnanda Raju * udp datagram 762e89e9cf5SAnanda Raju */ 7631470ddf7SHerbert Xu if ((skb = skb_peek_tail(queue)) == NULL) { 764e89e9cf5SAnanda Raju skb = sock_alloc_send_skb(sk, 765e89e9cf5SAnanda Raju hh_len + fragheaderlen + transhdrlen + 20, 766e89e9cf5SAnanda Raju (flags & MSG_DONTWAIT), &err); 767e89e9cf5SAnanda Raju 768e89e9cf5SAnanda Raju if (skb == NULL) 769e89e9cf5SAnanda Raju return err; 770e89e9cf5SAnanda Raju 771e89e9cf5SAnanda Raju /* reserve space for Hardware header */ 772e89e9cf5SAnanda Raju skb_reserve(skb, hh_len); 773e89e9cf5SAnanda Raju 774e89e9cf5SAnanda Raju /* create space for UDP/IP header */ 775e89e9cf5SAnanda Raju skb_put(skb, fragheaderlen + transhdrlen); 776e89e9cf5SAnanda Raju 777e89e9cf5SAnanda Raju /* initialize network header pointer */ 778c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 779e89e9cf5SAnanda Raju 780e89e9cf5SAnanda Raju /* initialize protocol header pointer */ 781b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + fragheaderlen; 782e89e9cf5SAnanda Raju 78384fa7933SPatrick McHardy skb->ip_summed = CHECKSUM_PARTIAL; 784e89e9cf5SAnanda Raju skb->csum = 0; 785e89e9cf5SAnanda Raju 786e89e9cf5SAnanda Raju /* specify the length of each IP datagram fragment */ 787d9be4f7aSBill Sommerfeld skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen; 788f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 7891470ddf7SHerbert Xu __skb_queue_tail(queue, skb); 790e89e9cf5SAnanda Raju } 791be9164e7SKostya B 792be9164e7SKostya B return skb_append_datato_frags(sk, skb, getfrag, from, 793be9164e7SKostya B (length - transhdrlen)); 794e89e9cf5SAnanda Raju } 795e89e9cf5SAnanda Raju 796f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk, 797f5fca608SDavid S. Miller struct flowi4 *fl4, 798f5fca608SDavid S. Miller struct sk_buff_head *queue, 7991470ddf7SHerbert Xu struct inet_cork *cork, 8005640f768SEric Dumazet struct page_frag *pfrag, 8011470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, 8021470ddf7SHerbert Xu int len, int odd, struct sk_buff *skb), 8031da177e4SLinus Torvalds void *from, int length, int transhdrlen, 8041da177e4SLinus Torvalds unsigned int flags) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 8071da177e4SLinus Torvalds struct sk_buff *skb; 8081da177e4SLinus Torvalds 80907df5294SHerbert Xu struct ip_options *opt = cork->opt; 8101da177e4SLinus Torvalds int hh_len; 8111da177e4SLinus Torvalds int exthdrlen; 8121da177e4SLinus Torvalds int mtu; 8131da177e4SLinus Torvalds int copy; 8141da177e4SLinus Torvalds int err; 8151da177e4SLinus Torvalds int offset = 0; 8161da177e4SLinus Torvalds unsigned int maxfraglen, fragheaderlen; 8171da177e4SLinus Torvalds int csummode = CHECKSUM_NONE; 8181470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 8191da177e4SLinus Torvalds 82096d7303eSSteffen Klassert skb = skb_peek_tail(queue); 82196d7303eSSteffen Klassert 82296d7303eSSteffen Klassert exthdrlen = !skb ? rt->dst.header_len : 0; 82307df5294SHerbert Xu mtu = cork->fragsize; 8241470ddf7SHerbert Xu 825d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 8281da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 8291da177e4SLinus Torvalds 8301470ddf7SHerbert Xu if (cork->length + length > 0xFFFF - fragheaderlen) { 831f5fca608SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 832c720c7e8SEric Dumazet mtu-exthdrlen); 8331da177e4SLinus Torvalds return -EMSGSIZE; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* 8371da177e4SLinus Torvalds * transhdrlen > 0 means that this is the first fragment and we wish 8381da177e4SLinus Torvalds * it won't be fragmented in the future. 8391da177e4SLinus Torvalds */ 8401da177e4SLinus Torvalds if (transhdrlen && 8411da177e4SLinus Torvalds length + fragheaderlen <= mtu && 842d8d1f30bSChangli Gao rt->dst.dev->features & NETIF_F_V4_CSUM && 8431da177e4SLinus Torvalds !exthdrlen) 84484fa7933SPatrick McHardy csummode = CHECKSUM_PARTIAL; 8451da177e4SLinus Torvalds 8461470ddf7SHerbert Xu cork->length += length; 84726cde9f7SHerbert Xu if (((length > mtu) || (skb && skb_is_gso(skb))) && 848be9164e7SKostya B (sk->sk_protocol == IPPROTO_UDP) && 849c146066aSSteffen Klassert (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len) { 8501470ddf7SHerbert Xu err = ip_ufo_append_data(sk, queue, getfrag, from, length, 8511470ddf7SHerbert Xu hh_len, fragheaderlen, transhdrlen, 852d9be4f7aSBill Sommerfeld maxfraglen, flags); 853baa829d8SPatrick McHardy if (err) 854e89e9cf5SAnanda Raju goto error; 855e89e9cf5SAnanda Raju return 0; 856e89e9cf5SAnanda Raju } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds /* So, what's going on in the loop below? 8591da177e4SLinus Torvalds * 8601da177e4SLinus Torvalds * We use calculated fragment length to generate chained skb, 8611da177e4SLinus Torvalds * each of segments is IP fragment ready for sending to network after 8621da177e4SLinus Torvalds * adding appropriate IP header. 8631da177e4SLinus Torvalds */ 8641da177e4SLinus Torvalds 86526cde9f7SHerbert Xu if (!skb) 8661da177e4SLinus Torvalds goto alloc_new_skb; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds while (length > 0) { 8691da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 8701da177e4SLinus Torvalds copy = mtu - skb->len; 8711da177e4SLinus Torvalds if (copy < length) 8721da177e4SLinus Torvalds copy = maxfraglen - skb->len; 8731da177e4SLinus Torvalds if (copy <= 0) { 8741da177e4SLinus Torvalds char *data; 8751da177e4SLinus Torvalds unsigned int datalen; 8761da177e4SLinus Torvalds unsigned int fraglen; 8771da177e4SLinus Torvalds unsigned int fraggap; 8781da177e4SLinus Torvalds unsigned int alloclen; 8791da177e4SLinus Torvalds struct sk_buff *skb_prev; 8801da177e4SLinus Torvalds alloc_new_skb: 8811da177e4SLinus Torvalds skb_prev = skb; 8821da177e4SLinus Torvalds if (skb_prev) 8831da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 8841da177e4SLinus Torvalds else 8851da177e4SLinus Torvalds fraggap = 0; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* 8881da177e4SLinus Torvalds * If remaining data exceeds the mtu, 8891da177e4SLinus Torvalds * we know we need more fragment(s). 8901da177e4SLinus Torvalds */ 8911da177e4SLinus Torvalds datalen = length + fraggap; 8921da177e4SLinus Torvalds if (datalen > mtu - fragheaderlen) 8931da177e4SLinus Torvalds datalen = maxfraglen - fragheaderlen; 8941da177e4SLinus Torvalds fraglen = datalen + fragheaderlen; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds if ((flags & MSG_MORE) && 897d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 8981da177e4SLinus Torvalds alloclen = mtu; 8991da177e4SLinus Torvalds else 90059104f06SEric Dumazet alloclen = fraglen; 9011da177e4SLinus Torvalds 902353e5c9aSSteffen Klassert alloclen += exthdrlen; 903353e5c9aSSteffen Klassert 9041da177e4SLinus Torvalds /* The last fragment gets additional space at tail. 9051da177e4SLinus Torvalds * Note, with MSG_MORE we overallocate on fragments, 9061da177e4SLinus Torvalds * because we have no idea what fragment will be 9071da177e4SLinus Torvalds * the last. 9081da177e4SLinus Torvalds */ 90933f99dc7SSteffen Klassert if (datalen == length + fraggap) 910d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 91133f99dc7SSteffen Klassert 9121da177e4SLinus Torvalds if (transhdrlen) { 9131da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 9141da177e4SLinus Torvalds alloclen + hh_len + 15, 9151da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 9161da177e4SLinus Torvalds } else { 9171da177e4SLinus Torvalds skb = NULL; 9181da177e4SLinus Torvalds if (atomic_read(&sk->sk_wmem_alloc) <= 9191da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 9201da177e4SLinus Torvalds skb = sock_wmalloc(sk, 9211da177e4SLinus Torvalds alloclen + hh_len + 15, 1, 9221da177e4SLinus Torvalds sk->sk_allocation); 9231da177e4SLinus Torvalds if (unlikely(skb == NULL)) 9241da177e4SLinus Torvalds err = -ENOBUFS; 92551f31cabSPatrick Ohly else 92651f31cabSPatrick Ohly /* only the initial fragment is 92751f31cabSPatrick Ohly time stamped */ 9281470ddf7SHerbert Xu cork->tx_flags = 0; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds if (skb == NULL) 9311da177e4SLinus Torvalds goto error; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds /* 9341da177e4SLinus Torvalds * Fill in the control structures 9351da177e4SLinus Torvalds */ 9361da177e4SLinus Torvalds skb->ip_summed = csummode; 9371da177e4SLinus Torvalds skb->csum = 0; 9381da177e4SLinus Torvalds skb_reserve(skb, hh_len); 9391470ddf7SHerbert Xu skb_shinfo(skb)->tx_flags = cork->tx_flags; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* 9421da177e4SLinus Torvalds * Find where to start putting bytes. 9431da177e4SLinus Torvalds */ 944353e5c9aSSteffen Klassert data = skb_put(skb, fraglen + exthdrlen); 945c14d2450SArnaldo Carvalho de Melo skb_set_network_header(skb, exthdrlen); 946b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 947b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 948353e5c9aSSteffen Klassert data += fragheaderlen + exthdrlen; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds if (fraggap) { 9511da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 9521da177e4SLinus Torvalds skb_prev, maxfraglen, 9531da177e4SLinus Torvalds data + transhdrlen, fraggap, 0); 9541da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 9551da177e4SLinus Torvalds skb->csum); 9561da177e4SLinus Torvalds data += fraggap; 957e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds copy = datalen - transhdrlen - fraggap; 9611da177e4SLinus Torvalds if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { 9621da177e4SLinus Torvalds err = -EFAULT; 9631da177e4SLinus Torvalds kfree_skb(skb); 9641da177e4SLinus Torvalds goto error; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds offset += copy; 9681da177e4SLinus Torvalds length -= datalen - fraggap; 9691da177e4SLinus Torvalds transhdrlen = 0; 9701da177e4SLinus Torvalds exthdrlen = 0; 9711da177e4SLinus Torvalds csummode = CHECKSUM_NONE; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* 9741da177e4SLinus Torvalds * Put the packet on the pending queue. 9751da177e4SLinus Torvalds */ 9761470ddf7SHerbert Xu __skb_queue_tail(queue, skb); 9771da177e4SLinus Torvalds continue; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds if (copy > length) 9811da177e4SLinus Torvalds copy = length; 9821da177e4SLinus Torvalds 983d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) { 9841da177e4SLinus Torvalds unsigned int off; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds off = skb->len; 9871da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 9881da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 9891da177e4SLinus Torvalds __skb_trim(skb, off); 9901da177e4SLinus Torvalds err = -EFAULT; 9911da177e4SLinus Torvalds goto error; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds } else { 9941da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds err = -ENOMEM; 9975640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 9981da177e4SLinus Torvalds goto error; 9991da177e4SLinus Torvalds 10005640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 10015640f768SEric Dumazet pfrag->offset)) { 10021da177e4SLinus Torvalds err = -EMSGSIZE; 10035640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 10041da177e4SLinus Torvalds goto error; 10055640f768SEric Dumazet 10065640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 10075640f768SEric Dumazet pfrag->offset, 0); 10085640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 10095640f768SEric Dumazet get_page(pfrag->page); 10101da177e4SLinus Torvalds } 10115640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 10125640f768SEric Dumazet if (getfrag(from, 10135640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 10145640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 10155640f768SEric Dumazet goto error_efault; 10165640f768SEric Dumazet 10175640f768SEric Dumazet pfrag->offset += copy; 10185640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 10191da177e4SLinus Torvalds skb->len += copy; 10201da177e4SLinus Torvalds skb->data_len += copy; 1021f945fa7aSHerbert Xu skb->truesize += copy; 1022f945fa7aSHerbert Xu atomic_add(copy, &sk->sk_wmem_alloc); 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds offset += copy; 10251da177e4SLinus Torvalds length -= copy; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds return 0; 10291da177e4SLinus Torvalds 10305640f768SEric Dumazet error_efault: 10315640f768SEric Dumazet err = -EFAULT; 10321da177e4SLinus Torvalds error: 10331470ddf7SHerbert Xu cork->length -= length; 10345e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 10351da177e4SLinus Torvalds return err; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, 10391470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp) 10401470ddf7SHerbert Xu { 10411470ddf7SHerbert Xu struct inet_sock *inet = inet_sk(sk); 1042f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 10431470ddf7SHerbert Xu struct rtable *rt; 10441470ddf7SHerbert Xu 10451470ddf7SHerbert Xu /* 10461470ddf7SHerbert Xu * setup for corking. 10471470ddf7SHerbert Xu */ 10481470ddf7SHerbert Xu opt = ipc->opt; 10491470ddf7SHerbert Xu if (opt) { 10501470ddf7SHerbert Xu if (cork->opt == NULL) { 10511470ddf7SHerbert Xu cork->opt = kmalloc(sizeof(struct ip_options) + 40, 10521470ddf7SHerbert Xu sk->sk_allocation); 10531470ddf7SHerbert Xu if (unlikely(cork->opt == NULL)) 10541470ddf7SHerbert Xu return -ENOBUFS; 10551470ddf7SHerbert Xu } 1056f6d8bd05SEric Dumazet memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); 10571470ddf7SHerbert Xu cork->flags |= IPCORK_OPT; 10581470ddf7SHerbert Xu cork->addr = ipc->addr; 10591470ddf7SHerbert Xu } 10601470ddf7SHerbert Xu rt = *rtp; 10611470ddf7SHerbert Xu if (unlikely(!rt)) 10621470ddf7SHerbert Xu return -EFAULT; 10631470ddf7SHerbert Xu /* 10641470ddf7SHerbert Xu * We steal reference to this route, caller should not release it 10651470ddf7SHerbert Xu */ 10661470ddf7SHerbert Xu *rtp = NULL; 10671470ddf7SHerbert Xu cork->fragsize = inet->pmtudisc == IP_PMTUDISC_PROBE ? 1068353e5c9aSSteffen Klassert rt->dst.dev->mtu : dst_mtu(&rt->dst); 10691470ddf7SHerbert Xu cork->dst = &rt->dst; 10701470ddf7SHerbert Xu cork->length = 0; 10711470ddf7SHerbert Xu cork->tx_flags = ipc->tx_flags; 10721470ddf7SHerbert Xu 10731470ddf7SHerbert Xu return 0; 10741470ddf7SHerbert Xu } 10751470ddf7SHerbert Xu 10761470ddf7SHerbert Xu /* 10771470ddf7SHerbert Xu * ip_append_data() and ip_append_page() can make one large IP datagram 10781470ddf7SHerbert Xu * from many pieces of data. Each pieces will be holded on the socket 10791470ddf7SHerbert Xu * until ip_push_pending_frames() is called. Each piece can be a page 10801470ddf7SHerbert Xu * or non-page data. 10811470ddf7SHerbert Xu * 10821470ddf7SHerbert Xu * Not only UDP, other transport protocols - e.g. raw sockets - can use 10831470ddf7SHerbert Xu * this interface potentially. 10841470ddf7SHerbert Xu * 10851470ddf7SHerbert Xu * LATER: length must be adjusted by pad at tail, when it is required. 10861470ddf7SHerbert Xu */ 1087f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4, 10881470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, int len, 10891470ddf7SHerbert Xu int odd, struct sk_buff *skb), 10901470ddf7SHerbert Xu void *from, int length, int transhdrlen, 10911470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 10921470ddf7SHerbert Xu unsigned int flags) 10931470ddf7SHerbert Xu { 10941470ddf7SHerbert Xu struct inet_sock *inet = inet_sk(sk); 10951470ddf7SHerbert Xu int err; 10961470ddf7SHerbert Xu 10971470ddf7SHerbert Xu if (flags&MSG_PROBE) 10981470ddf7SHerbert Xu return 0; 10991470ddf7SHerbert Xu 11001470ddf7SHerbert Xu if (skb_queue_empty(&sk->sk_write_queue)) { 1101bdc712b4SDavid S. Miller err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp); 11021470ddf7SHerbert Xu if (err) 11031470ddf7SHerbert Xu return err; 11041470ddf7SHerbert Xu } else { 11051470ddf7SHerbert Xu transhdrlen = 0; 11061470ddf7SHerbert Xu } 11071470ddf7SHerbert Xu 11085640f768SEric Dumazet return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base, 11095640f768SEric Dumazet sk_page_frag(sk), getfrag, 11101470ddf7SHerbert Xu from, length, transhdrlen, flags); 11111470ddf7SHerbert Xu } 11121470ddf7SHerbert Xu 1113f5fca608SDavid S. Miller ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 11141da177e4SLinus Torvalds int offset, size_t size, int flags) 11151da177e4SLinus Torvalds { 11161da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 11171da177e4SLinus Torvalds struct sk_buff *skb; 11181da177e4SLinus Torvalds struct rtable *rt; 11191da177e4SLinus Torvalds struct ip_options *opt = NULL; 1120bdc712b4SDavid S. Miller struct inet_cork *cork; 11211da177e4SLinus Torvalds int hh_len; 11221da177e4SLinus Torvalds int mtu; 11231da177e4SLinus Torvalds int len; 11241da177e4SLinus Torvalds int err; 11251da177e4SLinus Torvalds unsigned int maxfraglen, fragheaderlen, fraggap; 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds if (inet->hdrincl) 11281da177e4SLinus Torvalds return -EPERM; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds if (flags&MSG_PROBE) 11311da177e4SLinus Torvalds return 0; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) 11341da177e4SLinus Torvalds return -EINVAL; 11351da177e4SLinus Torvalds 1136bdc712b4SDavid S. Miller cork = &inet->cork.base; 1137bdc712b4SDavid S. Miller rt = (struct rtable *)cork->dst; 1138bdc712b4SDavid S. Miller if (cork->flags & IPCORK_OPT) 1139bdc712b4SDavid S. Miller opt = cork->opt; 11401da177e4SLinus Torvalds 1141d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) 11421da177e4SLinus Torvalds return -EOPNOTSUPP; 11431da177e4SLinus Torvalds 1144d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 1145bdc712b4SDavid S. Miller mtu = cork->fragsize; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 11481da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 11491da177e4SLinus Torvalds 1150bdc712b4SDavid S. Miller if (cork->length + size > 0xFFFF - fragheaderlen) { 1151f5fca608SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, mtu); 11521da177e4SLinus Torvalds return -EMSGSIZE; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) 11561da177e4SLinus Torvalds return -EINVAL; 11571da177e4SLinus Torvalds 1158bdc712b4SDavid S. Miller cork->length += size; 115926cde9f7SHerbert Xu if ((size + skb->len > mtu) && 116026cde9f7SHerbert Xu (sk->sk_protocol == IPPROTO_UDP) && 1161d8d1f30bSChangli Gao (rt->dst.dev->features & NETIF_F_UFO)) { 11627967168cSHerbert Xu skb_shinfo(skb)->gso_size = mtu - fragheaderlen; 1163f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 11647967168cSHerbert Xu } 1165e89e9cf5SAnanda Raju 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds while (size > 0) { 11681da177e4SLinus Torvalds int i; 11691da177e4SLinus Torvalds 117089114afdSHerbert Xu if (skb_is_gso(skb)) 1171e89e9cf5SAnanda Raju len = size; 1172e89e9cf5SAnanda Raju else { 1173e89e9cf5SAnanda Raju 11741da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 11751da177e4SLinus Torvalds len = mtu - skb->len; 11761da177e4SLinus Torvalds if (len < size) 11771da177e4SLinus Torvalds len = maxfraglen - skb->len; 1178e89e9cf5SAnanda Raju } 11791da177e4SLinus Torvalds if (len <= 0) { 11801da177e4SLinus Torvalds struct sk_buff *skb_prev; 11811da177e4SLinus Torvalds int alloclen; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds skb_prev = skb; 11841da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds alloclen = fragheaderlen + hh_len + fraggap + 15; 11871da177e4SLinus Torvalds skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); 11881da177e4SLinus Torvalds if (unlikely(!skb)) { 11891da177e4SLinus Torvalds err = -ENOBUFS; 11901da177e4SLinus Torvalds goto error; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /* 11941da177e4SLinus Torvalds * Fill in the control structures 11951da177e4SLinus Torvalds */ 11961da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 11971da177e4SLinus Torvalds skb->csum = 0; 11981da177e4SLinus Torvalds skb_reserve(skb, hh_len); 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * Find where to start putting bytes. 12021da177e4SLinus Torvalds */ 1203967b05f6SArnaldo Carvalho de Melo skb_put(skb, fragheaderlen + fraggap); 12042ca9e6f2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1205b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1206b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 12071da177e4SLinus Torvalds if (fraggap) { 1208967b05f6SArnaldo Carvalho de Melo skb->csum = skb_copy_and_csum_bits(skb_prev, 1209967b05f6SArnaldo Carvalho de Melo maxfraglen, 12109c70220bSArnaldo Carvalho de Melo skb_transport_header(skb), 1211967b05f6SArnaldo Carvalho de Melo fraggap, 0); 12121da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 12131da177e4SLinus Torvalds skb->csum); 1214e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds /* 12181da177e4SLinus Torvalds * Put the packet on the pending queue. 12191da177e4SLinus Torvalds */ 12201da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 12211da177e4SLinus Torvalds continue; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds i = skb_shinfo(skb)->nr_frags; 12251da177e4SLinus Torvalds if (len > size) 12261da177e4SLinus Torvalds len = size; 12271da177e4SLinus Torvalds if (skb_can_coalesce(skb, i, page, offset)) { 12289e903e08SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i-1], len); 12291da177e4SLinus Torvalds } else if (i < MAX_SKB_FRAGS) { 12301da177e4SLinus Torvalds get_page(page); 12311da177e4SLinus Torvalds skb_fill_page_desc(skb, i, page, offset, len); 12321da177e4SLinus Torvalds } else { 12331da177e4SLinus Torvalds err = -EMSGSIZE; 12341da177e4SLinus Torvalds goto error; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds if (skb->ip_summed == CHECKSUM_NONE) { 123844bb9363SAl Viro __wsum csum; 12391da177e4SLinus Torvalds csum = csum_page(page, offset, len); 12401da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, skb->len); 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds skb->len += len; 12441da177e4SLinus Torvalds skb->data_len += len; 12451e34a11dSDavid S. Miller skb->truesize += len; 12461e34a11dSDavid S. Miller atomic_add(len, &sk->sk_wmem_alloc); 12471da177e4SLinus Torvalds offset += len; 12481da177e4SLinus Torvalds size -= len; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds return 0; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds error: 1253bdc712b4SDavid S. Miller cork->length -= size; 12545e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 12551da177e4SLinus Torvalds return err; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork) 1259429f08e9SPavel Emelyanov { 12601470ddf7SHerbert Xu cork->flags &= ~IPCORK_OPT; 12611470ddf7SHerbert Xu kfree(cork->opt); 12621470ddf7SHerbert Xu cork->opt = NULL; 12631470ddf7SHerbert Xu dst_release(cork->dst); 12641470ddf7SHerbert Xu cork->dst = NULL; 1265429f08e9SPavel Emelyanov } 1266429f08e9SPavel Emelyanov 12671da177e4SLinus Torvalds /* 12681da177e4SLinus Torvalds * Combined all pending IP fragments on the socket as one IP datagram 12691da177e4SLinus Torvalds * and push them out. 12701da177e4SLinus Torvalds */ 12711c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk, 127277968b78SDavid S. Miller struct flowi4 *fl4, 12731470ddf7SHerbert Xu struct sk_buff_head *queue, 12741470ddf7SHerbert Xu struct inet_cork *cork) 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 12771da177e4SLinus Torvalds struct sk_buff **tail_skb; 12781da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 12790388b004SPavel Emelyanov struct net *net = sock_net(sk); 12801da177e4SLinus Torvalds struct ip_options *opt = NULL; 12811470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 12821da177e4SLinus Torvalds struct iphdr *iph; 128376ab608dSAlexey Dobriyan __be16 df = 0; 12841da177e4SLinus Torvalds __u8 ttl; 12851da177e4SLinus Torvalds 12861470ddf7SHerbert Xu if ((skb = __skb_dequeue(queue)) == NULL) 12871da177e4SLinus Torvalds goto out; 12881da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1291d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1292bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12931470ddf7SHerbert Xu while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1294cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 12951da177e4SLinus Torvalds *tail_skb = tmp_skb; 12961da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 12971da177e4SLinus Torvalds skb->len += tmp_skb->len; 12981da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 12991da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 13001da177e4SLinus Torvalds tmp_skb->destructor = NULL; 13011da177e4SLinus Torvalds tmp_skb->sk = NULL; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow 13051da177e4SLinus Torvalds * to fragment the frame generated here. No matter, what transforms 13061da177e4SLinus Torvalds * how transforms change size of the packet, it will come out. 13071da177e4SLinus Torvalds */ 1308628a5c56SJohn Heffner if (inet->pmtudisc < IP_PMTUDISC_DO) 13091da177e4SLinus Torvalds skb->local_df = 1; 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds /* DF bit is set when we want to see DF on outgoing frames. 13121da177e4SLinus Torvalds * If local_df is set too, we still allow to fragment this frame 13131da177e4SLinus Torvalds * locally. */ 1314628a5c56SJohn Heffner if (inet->pmtudisc >= IP_PMTUDISC_DO || 1315d8d1f30bSChangli Gao (skb->len <= dst_mtu(&rt->dst) && 1316d8d1f30bSChangli Gao ip_dont_fragment(sk, &rt->dst))) 13171da177e4SLinus Torvalds df = htons(IP_DF); 13181da177e4SLinus Torvalds 13191470ddf7SHerbert Xu if (cork->flags & IPCORK_OPT) 13201470ddf7SHerbert Xu opt = cork->opt; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds if (rt->rt_type == RTN_MULTICAST) 13231da177e4SLinus Torvalds ttl = inet->mc_ttl; 13241da177e4SLinus Torvalds else 1325d8d1f30bSChangli Gao ttl = ip_select_ttl(inet, &rt->dst); 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds iph = (struct iphdr *)skb->data; 13281da177e4SLinus Torvalds iph->version = 4; 13291da177e4SLinus Torvalds iph->ihl = 5; 13301da177e4SLinus Torvalds iph->tos = inet->tos; 13311da177e4SLinus Torvalds iph->frag_off = df; 13321da177e4SLinus Torvalds iph->ttl = ttl; 13331da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 133484f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 1335a9915a1bSEric Dumazet ip_select_ident(iph, &rt->dst, sk); 13361da177e4SLinus Torvalds 133722f728f8SDavid S. Miller if (opt) { 133822f728f8SDavid S. Miller iph->ihl += opt->optlen>>2; 133922f728f8SDavid S. Miller ip_options_build(skb, opt, cork->addr, rt, 0); 134022f728f8SDavid S. Miller } 134122f728f8SDavid S. Miller 13421da177e4SLinus Torvalds skb->priority = sk->sk_priority; 13434a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1344a21bba94SEric Dumazet /* 1345a21bba94SEric Dumazet * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1346a21bba94SEric Dumazet * on dst refcount 1347a21bba94SEric Dumazet */ 13481470ddf7SHerbert Xu cork->dst = NULL; 1349d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 13501da177e4SLinus Torvalds 135196793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 13520388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 135396793b48SDavid L Stevens skb_transport_header(skb))->type); 135496793b48SDavid L Stevens 13551c32c5adSHerbert Xu ip_cork_release(cork); 13561c32c5adSHerbert Xu out: 13571c32c5adSHerbert Xu return skb; 13581c32c5adSHerbert Xu } 13591c32c5adSHerbert Xu 1360b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb) 13611c32c5adSHerbert Xu { 13621c32c5adSHerbert Xu int err; 13631c32c5adSHerbert Xu 1364c439cb2eSHerbert Xu err = ip_local_out(skb); 13651da177e4SLinus Torvalds if (err) { 13661da177e4SLinus Torvalds if (err > 0) 13676ce9e7b5SEric Dumazet err = net_xmit_errno(err); 13681da177e4SLinus Torvalds if (err) 13691c32c5adSHerbert Xu IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds return err; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 137577968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4) 13761470ddf7SHerbert Xu { 13771c32c5adSHerbert Xu struct sk_buff *skb; 13781c32c5adSHerbert Xu 137977968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 13801c32c5adSHerbert Xu if (!skb) 13811c32c5adSHerbert Xu return 0; 13821c32c5adSHerbert Xu 13831c32c5adSHerbert Xu /* Netfilter gets whole the not fragmented skb. */ 1384b5ec8eeaSEric Dumazet return ip_send_skb(sock_net(sk), skb); 13851470ddf7SHerbert Xu } 13861470ddf7SHerbert Xu 13871da177e4SLinus Torvalds /* 13881da177e4SLinus Torvalds * Throw away all pending data on the socket. 13891da177e4SLinus Torvalds */ 13901470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk, 13911470ddf7SHerbert Xu struct sk_buff_head *queue, 13921470ddf7SHerbert Xu struct inet_cork *cork) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds struct sk_buff *skb; 13951da177e4SLinus Torvalds 13961470ddf7SHerbert Xu while ((skb = __skb_dequeue_tail(queue)) != NULL) 13971da177e4SLinus Torvalds kfree_skb(skb); 13981da177e4SLinus Torvalds 13991470ddf7SHerbert Xu ip_cork_release(cork); 14001470ddf7SHerbert Xu } 14011470ddf7SHerbert Xu 14021470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk) 14031470ddf7SHerbert Xu { 1404bdc712b4SDavid S. Miller __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk, 140877968b78SDavid S. Miller struct flowi4 *fl4, 14091c32c5adSHerbert Xu int getfrag(void *from, char *to, int offset, 14101c32c5adSHerbert Xu int len, int odd, struct sk_buff *skb), 14111c32c5adSHerbert Xu void *from, int length, int transhdrlen, 14121c32c5adSHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 14131c32c5adSHerbert Xu unsigned int flags) 14141c32c5adSHerbert Xu { 1415b80d7226SDavid S. Miller struct inet_cork cork; 14161c32c5adSHerbert Xu struct sk_buff_head queue; 14171c32c5adSHerbert Xu int err; 14181c32c5adSHerbert Xu 14191c32c5adSHerbert Xu if (flags & MSG_PROBE) 14201c32c5adSHerbert Xu return NULL; 14211c32c5adSHerbert Xu 14221c32c5adSHerbert Xu __skb_queue_head_init(&queue); 14231c32c5adSHerbert Xu 1424b80d7226SDavid S. Miller cork.flags = 0; 1425b80d7226SDavid S. Miller cork.addr = 0; 142670652728SDavid S. Miller cork.opt = NULL; 14271c32c5adSHerbert Xu err = ip_setup_cork(sk, &cork, ipc, rtp); 14281c32c5adSHerbert Xu if (err) 14291c32c5adSHerbert Xu return ERR_PTR(err); 14301c32c5adSHerbert Xu 14315640f768SEric Dumazet err = __ip_append_data(sk, fl4, &queue, &cork, 14325640f768SEric Dumazet ¤t->task_frag, getfrag, 14331c32c5adSHerbert Xu from, length, transhdrlen, flags); 14341c32c5adSHerbert Xu if (err) { 14351c32c5adSHerbert Xu __ip_flush_pending_frames(sk, &queue, &cork); 14361c32c5adSHerbert Xu return ERR_PTR(err); 14371c32c5adSHerbert Xu } 14381c32c5adSHerbert Xu 143977968b78SDavid S. Miller return __ip_make_skb(sk, fl4, &queue, &cork); 14401c32c5adSHerbert Xu } 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds /* 14431da177e4SLinus Torvalds * Fetch data from kernel space and fill in checksum if needed. 14441da177e4SLinus Torvalds */ 14451da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset, 14461da177e4SLinus Torvalds int len, int odd, struct sk_buff *skb) 14471da177e4SLinus Torvalds { 14485084205fSAl Viro __wsum csum; 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0); 14511da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 14521da177e4SLinus Torvalds return 0; 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds /* 14561da177e4SLinus Torvalds * Generic function to send a packet as reply to another packet. 1457be9f4a44SEric Dumazet * Used to send some TCP resets/acks so far. 14581da177e4SLinus Torvalds * 1459be9f4a44SEric Dumazet * Use a fake percpu inet socket to avoid false sharing and contention. 14601da177e4SLinus Torvalds */ 1461be9f4a44SEric Dumazet static DEFINE_PER_CPU(struct inet_sock, unicast_sock) = { 1462be9f4a44SEric Dumazet .sk = { 1463be9f4a44SEric Dumazet .__sk_common = { 1464be9f4a44SEric Dumazet .skc_refcnt = ATOMIC_INIT(1), 1465be9f4a44SEric Dumazet }, 1466be9f4a44SEric Dumazet .sk_wmem_alloc = ATOMIC_INIT(1), 1467be9f4a44SEric Dumazet .sk_allocation = GFP_ATOMIC, 1468be9f4a44SEric Dumazet .sk_flags = (1UL << SOCK_USE_WRITE_QUEUE), 1469be9f4a44SEric Dumazet }, 1470be9f4a44SEric Dumazet .pmtudisc = IP_PMTUDISC_WANT, 14710980e56eSEric Dumazet .uc_ttl = -1, 1472be9f4a44SEric Dumazet }; 1473be9f4a44SEric Dumazet 1474be9f4a44SEric Dumazet void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr, 147570e73416SDavid S. Miller __be32 saddr, const struct ip_reply_arg *arg, 147670e73416SDavid S. Miller unsigned int len) 14771da177e4SLinus Torvalds { 1478f6d8bd05SEric Dumazet struct ip_options_data replyopts; 14791da177e4SLinus Torvalds struct ipcm_cookie ipc; 148077968b78SDavid S. Miller struct flowi4 fl4; 1481511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1482be9f4a44SEric Dumazet struct sk_buff *nskb; 1483be9f4a44SEric Dumazet struct sock *sk; 1484be9f4a44SEric Dumazet struct inet_sock *inet; 14851da177e4SLinus Torvalds 1486f6d8bd05SEric Dumazet if (ip_options_echo(&replyopts.opt.opt, skb)) 14871da177e4SLinus Torvalds return; 14881da177e4SLinus Torvalds 14890a5ebb80SDavid S. Miller ipc.addr = daddr; 14901da177e4SLinus Torvalds ipc.opt = NULL; 14912244d07bSOliver Hartkopp ipc.tx_flags = 0; 14921da177e4SLinus Torvalds 1493f6d8bd05SEric Dumazet if (replyopts.opt.opt.optlen) { 14941da177e4SLinus Torvalds ipc.opt = &replyopts.opt; 14951da177e4SLinus Torvalds 1496f6d8bd05SEric Dumazet if (replyopts.opt.opt.srr) 1497f6d8bd05SEric Dumazet daddr = replyopts.opt.opt.faddr; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 1500538de0e0SDavid S. Miller flowi4_init_output(&fl4, arg->bound_dev_if, 0, 150166b13d99SEric Dumazet RT_TOS(arg->tos), 1502be9f4a44SEric Dumazet RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol, 1503538de0e0SDavid S. Miller ip_reply_arg_flowi_flags(arg), 150470e73416SDavid S. Miller daddr, saddr, 1505538de0e0SDavid S. Miller tcp_hdr(skb)->source, tcp_hdr(skb)->dest); 15069d6ec938SDavid S. Miller security_skb_classify_flow(skb, flowi4_to_flowi(&fl4)); 1507be9f4a44SEric Dumazet rt = ip_route_output_key(net, &fl4); 1508b23dd4feSDavid S. Miller if (IS_ERR(rt)) 15091da177e4SLinus Torvalds return; 15101da177e4SLinus Torvalds 1511be9f4a44SEric Dumazet inet = &get_cpu_var(unicast_sock); 15121da177e4SLinus Torvalds 151366b13d99SEric Dumazet inet->tos = arg->tos; 1514be9f4a44SEric Dumazet sk = &inet->sk; 15151da177e4SLinus Torvalds sk->sk_priority = skb->priority; 1516eddc9ec5SArnaldo Carvalho de Melo sk->sk_protocol = ip_hdr(skb)->protocol; 1517f0e48dbfSPatrick McHardy sk->sk_bound_dev_if = arg->bound_dev_if; 1518be9f4a44SEric Dumazet sock_net_set(sk, net); 1519be9f4a44SEric Dumazet __skb_queue_head_init(&sk->sk_write_queue); 1520be9f4a44SEric Dumazet sk->sk_sndbuf = sysctl_wmem_default; 1521f5fca608SDavid S. Miller ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, len, 0, 15222e77d89bSEric Dumazet &ipc, &rt, MSG_DONTWAIT); 1523be9f4a44SEric Dumazet nskb = skb_peek(&sk->sk_write_queue); 1524be9f4a44SEric Dumazet if (nskb) { 15251da177e4SLinus Torvalds if (arg->csumoffset >= 0) 1526be9f4a44SEric Dumazet *((__sum16 *)skb_transport_header(nskb) + 1527be9f4a44SEric Dumazet arg->csumoffset) = csum_fold(csum_add(nskb->csum, 15289c70220bSArnaldo Carvalho de Melo arg->csum)); 1529be9f4a44SEric Dumazet nskb->ip_summed = CHECKSUM_NONE; 15303a7c384fSEric Dumazet skb_orphan(nskb); 1531be9f4a44SEric Dumazet skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb)); 153277968b78SDavid S. Miller ip_push_pending_frames(sk, &fl4); 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 1535be9f4a44SEric Dumazet put_cpu_var(unicast_sock); 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds ip_rt_put(rt); 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds void __init ip_init(void) 15411da177e4SLinus Torvalds { 15421da177e4SLinus Torvalds ip_rt_init(); 15431da177e4SLinus Torvalds inet_initpeers(); 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds #if defined(CONFIG_IP_MULTICAST) && defined(CONFIG_PROC_FS) 15461da177e4SLinus Torvalds igmp_mc_proc_init(); 15471da177e4SLinus Torvalds #endif 15481da177e4SLinus Torvalds } 1549