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 <asm/system.h> 471da177e4SLinus Torvalds #include <linux/module.h> 481da177e4SLinus Torvalds #include <linux/types.h> 491da177e4SLinus Torvalds #include <linux/kernel.h> 501da177e4SLinus Torvalds #include <linux/mm.h> 511da177e4SLinus Torvalds #include <linux/string.h> 521da177e4SLinus Torvalds #include <linux/errno.h> 53a1f8e7f7SAl Viro #include <linux/highmem.h> 545a0e3ad6STejun Heo #include <linux/slab.h> 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include <linux/socket.h> 571da177e4SLinus Torvalds #include <linux/sockios.h> 581da177e4SLinus Torvalds #include <linux/in.h> 591da177e4SLinus Torvalds #include <linux/inet.h> 601da177e4SLinus Torvalds #include <linux/netdevice.h> 611da177e4SLinus Torvalds #include <linux/etherdevice.h> 621da177e4SLinus Torvalds #include <linux/proc_fs.h> 631da177e4SLinus Torvalds #include <linux/stat.h> 641da177e4SLinus Torvalds #include <linux/init.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #include <net/snmp.h> 671da177e4SLinus Torvalds #include <net/ip.h> 681da177e4SLinus Torvalds #include <net/protocol.h> 691da177e4SLinus Torvalds #include <net/route.h> 70cfacb057SPatrick McHardy #include <net/xfrm.h> 711da177e4SLinus Torvalds #include <linux/skbuff.h> 721da177e4SLinus Torvalds #include <net/sock.h> 731da177e4SLinus Torvalds #include <net/arp.h> 741da177e4SLinus Torvalds #include <net/icmp.h> 751da177e4SLinus Torvalds #include <net/checksum.h> 761da177e4SLinus Torvalds #include <net/inetpeer.h> 771da177e4SLinus Torvalds #include <linux/igmp.h> 781da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 791da177e4SLinus Torvalds #include <linux/netfilter_bridge.h> 801da177e4SLinus Torvalds #include <linux/mroute.h> 811da177e4SLinus Torvalds #include <linux/netlink.h> 826cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h> 831da177e4SLinus Torvalds 84ab32ea5dSBrian Haley int sysctl_ip_default_ttl __read_mostly = IPDEFTTL; 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 } 921da177e4SLinus Torvalds 93c439cb2eSHerbert Xu int __ip_local_out(struct sk_buff *skb) 94c439cb2eSHerbert Xu { 95c439cb2eSHerbert Xu struct iphdr *iph = ip_hdr(skb); 96c439cb2eSHerbert Xu 97c439cb2eSHerbert Xu iph->tot_len = htons(skb->len); 98c439cb2eSHerbert Xu ip_send_check(iph); 999bbc768aSJan Engelhardt return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL, 1009bbc768aSJan Engelhardt skb_dst(skb)->dev, dst_output); 101c439cb2eSHerbert Xu } 102c439cb2eSHerbert Xu 103c439cb2eSHerbert Xu int ip_local_out(struct sk_buff *skb) 104c439cb2eSHerbert Xu { 105c439cb2eSHerbert Xu int err; 106c439cb2eSHerbert Xu 107c439cb2eSHerbert Xu err = __ip_local_out(skb); 108c439cb2eSHerbert Xu if (likely(err == 1)) 109c439cb2eSHerbert Xu err = dst_output(skb); 110c439cb2eSHerbert Xu 111c439cb2eSHerbert Xu return err; 112c439cb2eSHerbert Xu } 113c439cb2eSHerbert Xu EXPORT_SYMBOL_GPL(ip_local_out); 114c439cb2eSHerbert Xu 1151da177e4SLinus Torvalds /* dev_loopback_xmit for use with netfilter. */ 1161da177e4SLinus Torvalds static int ip_dev_loopback_xmit(struct sk_buff *newskb) 1171da177e4SLinus Torvalds { 118459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(newskb); 119bbe735e4SArnaldo Carvalho de Melo __skb_pull(newskb, skb_network_offset(newskb)); 1201da177e4SLinus Torvalds newskb->pkt_type = PACKET_LOOPBACK; 1211da177e4SLinus Torvalds newskb->ip_summed = CHECKSUM_UNNECESSARY; 122adf30907SEric Dumazet WARN_ON(!skb_dst(newskb)); 123e30b38c2SEric Dumazet netif_rx_ni(newskb); 1241da177e4SLinus Torvalds return 0; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds int ttl = inet->uc_ttl; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds if (ttl < 0) 1321da177e4SLinus Torvalds ttl = dst_metric(dst, RTAX_HOPLIMIT); 1331da177e4SLinus Torvalds return ttl; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * Add an ip header to a skbuff and send it out. 1381da177e4SLinus Torvalds * 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk, 14113d8eaa0SAl Viro __be32 saddr, __be32 daddr, struct ip_options *opt) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 144511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1451da177e4SLinus Torvalds struct iphdr *iph; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* Build the IP header. */ 1488856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr) + (opt ? opt->optlen : 0)); 1498856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 150eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1511da177e4SLinus Torvalds iph->version = 4; 1521da177e4SLinus Torvalds iph->ihl = 5; 1531da177e4SLinus Torvalds iph->tos = inet->tos; 154*d8d1f30bSChangli Gao if (ip_dont_fragment(sk, &rt->dst)) 1551da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 1561da177e4SLinus Torvalds else 1571da177e4SLinus Torvalds iph->frag_off = 0; 158*d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 1591da177e4SLinus Torvalds iph->daddr = rt->rt_dst; 1601da177e4SLinus Torvalds iph->saddr = rt->rt_src; 1611da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 162*d8d1f30bSChangli Gao ip_select_ident(iph, &rt->dst, sk); 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds if (opt && opt->optlen) { 1651da177e4SLinus Torvalds iph->ihl += opt->optlen>>2; 1661da177e4SLinus Torvalds ip_options_build(skb, opt, daddr, rt, 0); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds skb->priority = sk->sk_priority; 1704a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* Send it out. */ 173c439cb2eSHerbert Xu return ip_local_out(skb); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 176d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 177d8c97a94SArnaldo Carvalho de Melo 1781da177e4SLinus Torvalds static inline int ip_finish_output2(struct sk_buff *skb) 1791da177e4SLinus Torvalds { 180adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 18180787ebcSMitsuru Chinen struct rtable *rt = (struct rtable *)dst; 1821da177e4SLinus Torvalds struct net_device *dev = dst->dev; 183c2636b4dSChuck Lever unsigned int hh_len = LL_RESERVED_SPACE(dev); 1841da177e4SLinus Torvalds 185edf391ffSNeil Horman if (rt->rt_type == RTN_MULTICAST) { 186edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len); 187edf391ffSNeil Horman } else if (rt->rt_type == RTN_BROADCAST) 188edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len); 18980787ebcSMitsuru Chinen 1901da177e4SLinus Torvalds /* Be paranoid, rather than too clever. */ 1913b04dddeSStephen Hemminger if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 1921da177e4SLinus Torvalds struct sk_buff *skb2; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); 1951da177e4SLinus Torvalds if (skb2 == NULL) { 1961da177e4SLinus Torvalds kfree_skb(skb); 1971da177e4SLinus Torvalds return -ENOMEM; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds if (skb->sk) 2001da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 2011da177e4SLinus Torvalds kfree_skb(skb); 2021da177e4SLinus Torvalds skb = skb2; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2053644f0ceSStephen Hemminger if (dst->hh) 2063644f0ceSStephen Hemminger return neigh_hh_output(dst->hh, skb); 2073644f0ceSStephen Hemminger else if (dst->neighbour) 2081da177e4SLinus Torvalds return dst->neighbour->output(skb); 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds if (net_ratelimit()) 2111da177e4SLinus Torvalds printk(KERN_DEBUG "ip_finish_output2: No header cache and no neighbour!\n"); 2121da177e4SLinus Torvalds kfree_skb(skb); 2131da177e4SLinus Torvalds return -EINVAL; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 216628a5c56SJohn Heffner static inline int ip_skb_dst_mtu(struct sk_buff *skb) 217628a5c56SJohn Heffner { 218628a5c56SJohn Heffner struct inet_sock *inet = skb->sk ? inet_sk(skb->sk) : NULL; 219628a5c56SJohn Heffner 220628a5c56SJohn Heffner return (inet && inet->pmtudisc == IP_PMTUDISC_PROBE) ? 221adf30907SEric Dumazet skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb)); 222628a5c56SJohn Heffner } 223628a5c56SJohn Heffner 224861d0486SPatrick McHardy static int ip_finish_output(struct sk_buff *skb) 2251da177e4SLinus Torvalds { 2265c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 2275c901daaSPatrick McHardy /* Policy lookup after SNAT yielded a new policy */ 228adf30907SEric Dumazet if (skb_dst(skb)->xfrm != NULL) { 22948d5cad8SPatrick McHardy IPCB(skb)->flags |= IPSKB_REROUTED; 23048d5cad8SPatrick McHardy return dst_output(skb); 23148d5cad8SPatrick McHardy } 2325c901daaSPatrick McHardy #endif 233628a5c56SJohn Heffner if (skb->len > ip_skb_dst_mtu(skb) && !skb_is_gso(skb)) 2341bd9bef6SPatrick McHardy return ip_fragment(skb, ip_finish_output2); 2351bd9bef6SPatrick McHardy else 2361bd9bef6SPatrick McHardy return ip_finish_output2(skb); 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds int ip_mc_output(struct sk_buff *skb) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds struct sock *sk = skb->sk; 242511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 243*d8d1f30bSChangli Gao struct net_device *dev = rt->dst.dev; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* 2461da177e4SLinus Torvalds * If the indicated interface is up and running, send the packet. 2471da177e4SLinus Torvalds */ 248edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds skb->dev = dev; 2511da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * Multicasts are looped back for other local users 2551da177e4SLinus Torvalds */ 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (rt->rt_flags&RTCF_MULTICAST) { 2587ad6848cSOctavian Purdila if (sk_mc_loop(sk) 2591da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 2601da177e4SLinus Torvalds /* Small optimization: do not loopback not local frames, 2611da177e4SLinus Torvalds which returned after forwarding; they will be dropped 2621da177e4SLinus Torvalds by ip_mr_input in any case. 2631da177e4SLinus Torvalds Note, that local frames are looped back to be delivered 2641da177e4SLinus Torvalds to local recipients. 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds This check is duplicated in ip_mr_input at the moment. 2671da177e4SLinus Torvalds */ 2689d4fb27dSJoe Perches && 2699d4fb27dSJoe Perches ((rt->rt_flags & RTCF_LOCAL) || 2709d4fb27dSJoe Perches !(IPCB(skb)->flags & IPSKB_FORWARDED)) 2711da177e4SLinus Torvalds #endif 2721da177e4SLinus Torvalds ) { 2731da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 2741da177e4SLinus Torvalds if (newskb) 2759bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 2769bbc768aSJan Engelhardt newskb, NULL, newskb->dev, 2771da177e4SLinus Torvalds ip_dev_loopback_xmit); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* Multicasts with ttl 0 must not go beyond the host */ 2811da177e4SLinus Torvalds 282eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl == 0) { 2831da177e4SLinus Torvalds kfree_skb(skb); 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds if (rt->rt_flags&RTCF_BROADCAST) { 2891da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 2901da177e4SLinus Torvalds if (newskb) 2919bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, newskb, 2929bbc768aSJan Engelhardt NULL, newskb->dev, ip_dev_loopback_xmit); 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2959bbc768aSJan Engelhardt return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, 2969bbc768aSJan Engelhardt skb->dev, ip_finish_output, 29748d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds int ip_output(struct sk_buff *skb) 3011da177e4SLinus Torvalds { 302adf30907SEric Dumazet struct net_device *dev = skb_dst(skb)->dev; 3031bd9bef6SPatrick McHardy 304edf391ffSNeil Horman IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); 3051da177e4SLinus Torvalds 3061bd9bef6SPatrick McHardy skb->dev = dev; 3071bd9bef6SPatrick McHardy skb->protocol = htons(ETH_P_IP); 3081bd9bef6SPatrick McHardy 3099bbc768aSJan Engelhardt return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, dev, 31048d5cad8SPatrick McHardy ip_finish_output, 31148d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3144e15ed4dSShan Wei int ip_queue_xmit(struct sk_buff *skb) 3151da177e4SLinus Torvalds { 316e89862f4SDavid S. Miller struct sock *sk = skb->sk; 3171da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 3181da177e4SLinus Torvalds struct ip_options *opt = inet->opt; 3191da177e4SLinus Torvalds struct rtable *rt; 3201da177e4SLinus Torvalds struct iphdr *iph; 321ab6e3febSEric Dumazet int res; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* Skip all of this if the packet is already routed, 3241da177e4SLinus Torvalds * f.e. by something like SCTP. 3251da177e4SLinus Torvalds */ 326ab6e3febSEric Dumazet rcu_read_lock(); 327511c3f92SEric Dumazet rt = skb_rtable(skb); 3281da177e4SLinus Torvalds if (rt != NULL) 3291da177e4SLinus Torvalds goto packet_routed; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* Make sure we can route this packet. */ 3321da177e4SLinus Torvalds rt = (struct rtable *)__sk_dst_check(sk, 0); 3331da177e4SLinus Torvalds if (rt == NULL) { 3343ca3c68eSAl Viro __be32 daddr; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* Use correct destination address if we have options. */ 337c720c7e8SEric Dumazet daddr = inet->inet_daddr; 3381da177e4SLinus Torvalds if(opt && opt->srr) 3391da177e4SLinus Torvalds daddr = opt->faddr; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds struct flowi fl = { .oif = sk->sk_bound_dev_if, 343914a9ab3SAtis Elsts .mark = sk->sk_mark, 3441da177e4SLinus Torvalds .nl_u = { .ip4_u = 3451da177e4SLinus Torvalds { .daddr = daddr, 346c720c7e8SEric Dumazet .saddr = inet->inet_saddr, 3471da177e4SLinus Torvalds .tos = RT_CONN_FLAGS(sk) } }, 3481da177e4SLinus Torvalds .proto = sk->sk_protocol, 34986b08d86SKOVACS Krisztian .flags = inet_sk_flowi_flags(sk), 3501da177e4SLinus Torvalds .uli_u = { .ports = 351c720c7e8SEric Dumazet { .sport = inet->inet_sport, 352c720c7e8SEric Dumazet .dport = inet->inet_dport } } }; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* If this fails, retransmit mechanism of transport layer will 3551da177e4SLinus Torvalds * keep trying until route appears or the connection times 3561da177e4SLinus Torvalds * itself out. 3571da177e4SLinus Torvalds */ 358beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 3593b1e0a65SYOSHIFUJI Hideaki if (ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0)) 3601da177e4SLinus Torvalds goto no_route; 3611da177e4SLinus Torvalds } 362*d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 3631da177e4SLinus Torvalds } 364*d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds packet_routed: 3671da177e4SLinus Torvalds if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) 3681da177e4SLinus Torvalds goto no_route; 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* OK, we know where to send it, allocate and build IP header. */ 3718856dfa3SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct iphdr) + (opt ? opt->optlen : 0)); 3728856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 373eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 374714e85beSAl Viro *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff)); 375*d8d1f30bSChangli Gao if (ip_dont_fragment(sk, &rt->dst) && !skb->local_df) 3761da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 3771da177e4SLinus Torvalds else 3781da177e4SLinus Torvalds iph->frag_off = 0; 379*d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 3801da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 3811da177e4SLinus Torvalds iph->saddr = rt->rt_src; 3821da177e4SLinus Torvalds iph->daddr = rt->rt_dst; 3831da177e4SLinus Torvalds /* Transport layer set skb->h.foo itself. */ 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds if (opt && opt->optlen) { 3861da177e4SLinus Torvalds iph->ihl += opt->optlen >> 2; 387c720c7e8SEric Dumazet ip_options_build(skb, opt, inet->inet_daddr, rt, 0); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 390*d8d1f30bSChangli Gao ip_select_ident_more(iph, &rt->dst, sk, 3917967168cSHerbert Xu (skb_shinfo(skb)->gso_segs ?: 1) - 1); 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds skb->priority = sk->sk_priority; 3944a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 3951da177e4SLinus Torvalds 396ab6e3febSEric Dumazet res = ip_local_out(skb); 397ab6e3febSEric Dumazet rcu_read_unlock(); 398ab6e3febSEric Dumazet return res; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds no_route: 401ab6e3febSEric Dumazet rcu_read_unlock(); 4025e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); 4031da177e4SLinus Torvalds kfree_skb(skb); 4041da177e4SLinus Torvalds return -EHOSTUNREACH; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 4111da177e4SLinus Torvalds to->priority = from->priority; 4121da177e4SLinus Torvalds to->protocol = from->protocol; 413adf30907SEric Dumazet skb_dst_drop(to); 414adf30907SEric Dumazet skb_dst_set(to, dst_clone(skb_dst(from))); 4151da177e4SLinus Torvalds to->dev = from->dev; 41682e91ffeSThomas Graf to->mark = from->mark; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds /* Copy the flags to each fragment. */ 4191da177e4SLinus Torvalds IPCB(to)->flags = IPCB(from)->flags; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4221da177e4SLinus Torvalds to->tc_index = from->tc_index; 4231da177e4SLinus Torvalds #endif 424e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 425ba9dda3aSJozsef Kadlecsik #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 426ba9dda3aSJozsef Kadlecsik defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 427ba9dda3aSJozsef Kadlecsik to->nf_trace = from->nf_trace; 428ba9dda3aSJozsef Kadlecsik #endif 429c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 430c98d80edSJulian Anastasov to->ipvs_property = from->ipvs_property; 431c98d80edSJulian Anastasov #endif 432984bc16cSJames Morris skb_copy_secmark(to, from); 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds /* 4361da177e4SLinus Torvalds * This IP datagram is too large to be sent in one piece. Break it up into 4371da177e4SLinus Torvalds * smaller pieces (each of size equal to IP header plus 4381da177e4SLinus Torvalds * a block of the data of the original IP data part) that will yet fit in a 4391da177e4SLinus Torvalds * single device frame, and queue such a frame for sending. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds 4422e2f7aefSPatrick McHardy int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds struct iphdr *iph; 4451da177e4SLinus Torvalds int raw = 0; 4461da177e4SLinus Torvalds int ptr; 4471da177e4SLinus Torvalds struct net_device *dev; 4481da177e4SLinus Torvalds struct sk_buff *skb2; 4499bcfcaf5SStephen Hemminger unsigned int mtu, hlen, left, len, ll_rs, pad; 4501da177e4SLinus Torvalds int offset; 45176ab608dSAlexey Dobriyan __be16 not_last_frag; 452511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 4531da177e4SLinus Torvalds int err = 0; 4541da177e4SLinus Torvalds 455*d8d1f30bSChangli Gao dev = rt->dst.dev; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* 4581da177e4SLinus Torvalds * Point into the IP datagram header. 4591da177e4SLinus Torvalds */ 4601da177e4SLinus Torvalds 461eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds if (unlikely((iph->frag_off & htons(IP_DF)) && !skb->local_df)) { 4645e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 4651da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 466628a5c56SJohn Heffner htonl(ip_skb_dst_mtu(skb))); 4671da177e4SLinus Torvalds kfree_skb(skb); 4681da177e4SLinus Torvalds return -EMSGSIZE; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* 4721da177e4SLinus Torvalds * Setup starting values. 4731da177e4SLinus Torvalds */ 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds hlen = iph->ihl * 4; 476*d8d1f30bSChangli Gao mtu = dst_mtu(&rt->dst) - hlen; /* Size of data space */ 4776c79bf0fSBart De Schuymer #ifdef CONFIG_BRIDGE_NETFILTER 4786c79bf0fSBart De Schuymer if (skb->nf_bridge) 4796c79bf0fSBart De Schuymer mtu -= nf_bridge_mtu_reduction(skb); 4806c79bf0fSBart De Schuymer #endif 48189cee8b1SHerbert Xu IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* When frag_list is given, use it. First, check its validity: 4841da177e4SLinus Torvalds * some transformers could create wrong frag_list or break existing 4851da177e4SLinus Torvalds * one, it is not prohibited. In this case fall back to copying. 4861da177e4SLinus Torvalds * 4871da177e4SLinus Torvalds * LATER: this step can be merged to real generation of fragments, 4881da177e4SLinus Torvalds * we can switch to copy when see the first bad fragment. 4891da177e4SLinus Torvalds */ 490d7fcf1a5SDavid S. Miller if (skb_has_frags(skb)) { 4911da177e4SLinus Torvalds struct sk_buff *frag; 4921da177e4SLinus Torvalds int first_len = skb_pagelen(skb); 49329ffe1a5SHerbert Xu int truesizes = 0; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds if (first_len - hlen > mtu || 4961da177e4SLinus Torvalds ((first_len - hlen) & 7) || 4971da177e4SLinus Torvalds (iph->frag_off & htons(IP_MF|IP_OFFSET)) || 4981da177e4SLinus Torvalds skb_cloned(skb)) 4991da177e4SLinus Torvalds goto slow_path; 5001da177e4SLinus Torvalds 501d7fcf1a5SDavid S. Miller skb_walk_frags(skb, frag) { 5021da177e4SLinus Torvalds /* Correct geometry. */ 5031da177e4SLinus Torvalds if (frag->len > mtu || 5041da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 5051da177e4SLinus Torvalds skb_headroom(frag) < hlen) 5061da177e4SLinus Torvalds goto slow_path; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds /* Partially cloned skb? */ 5091da177e4SLinus Torvalds if (skb_shared(frag)) 5101da177e4SLinus Torvalds goto slow_path; 5112fdba6b0SHerbert Xu 5122fdba6b0SHerbert Xu BUG_ON(frag->sk); 5132fdba6b0SHerbert Xu if (skb->sk) { 5142fdba6b0SHerbert Xu frag->sk = skb->sk; 5152fdba6b0SHerbert Xu frag->destructor = sock_wfree; 5162fdba6b0SHerbert Xu } 517b2722b1cSPatrick McHardy truesizes += frag->truesize; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* Everything is OK. Generate! */ 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds err = 0; 5231da177e4SLinus Torvalds offset = 0; 5241da177e4SLinus Torvalds frag = skb_shinfo(skb)->frag_list; 525d7fcf1a5SDavid S. Miller skb_frag_list_init(skb); 5261da177e4SLinus Torvalds skb->data_len = first_len - skb_headlen(skb); 52729ffe1a5SHerbert Xu skb->truesize -= truesizes; 5281da177e4SLinus Torvalds skb->len = first_len; 5291da177e4SLinus Torvalds iph->tot_len = htons(first_len); 5301da177e4SLinus Torvalds iph->frag_off = htons(IP_MF); 5311da177e4SLinus Torvalds ip_send_check(iph); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds for (;;) { 5341da177e4SLinus Torvalds /* Prepare header of the next frame, 5351da177e4SLinus Torvalds * before previous one went down. */ 5361da177e4SLinus Torvalds if (frag) { 5371da177e4SLinus Torvalds frag->ip_summed = CHECKSUM_NONE; 538badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(frag); 539e2d1bca7SArnaldo Carvalho de Melo __skb_push(frag, hlen); 540e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(frag); 541d56f90a7SArnaldo Carvalho de Melo memcpy(skb_network_header(frag), iph, hlen); 542eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(frag); 5431da177e4SLinus Torvalds iph->tot_len = htons(frag->len); 5441da177e4SLinus Torvalds ip_copy_metadata(frag, skb); 5451da177e4SLinus Torvalds if (offset == 0) 5461da177e4SLinus Torvalds ip_options_fragment(frag); 5471da177e4SLinus Torvalds offset += skb->len - hlen; 5481da177e4SLinus Torvalds iph->frag_off = htons(offset>>3); 5491da177e4SLinus Torvalds if (frag->next != NULL) 5501da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 5511da177e4SLinus Torvalds /* Ready, complete checksum */ 5521da177e4SLinus Torvalds ip_send_check(iph); 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds err = output(skb); 5561da177e4SLinus Torvalds 557dafee490SWei Dong if (!err) 5585e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES); 5591da177e4SLinus Torvalds if (err || !frag) 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds skb = frag; 5631da177e4SLinus Torvalds frag = skb->next; 5641da177e4SLinus Torvalds skb->next = NULL; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds if (err == 0) { 5685e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS); 5691da177e4SLinus Torvalds return 0; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds while (frag) { 5731da177e4SLinus Torvalds skb = frag->next; 5741da177e4SLinus Torvalds kfree_skb(frag); 5751da177e4SLinus Torvalds frag = skb; 5761da177e4SLinus Torvalds } 5775e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 5781da177e4SLinus Torvalds return err; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds slow_path: 5821da177e4SLinus Torvalds left = skb->len - hlen; /* Space per frame */ 5831da177e4SLinus Torvalds ptr = raw + hlen; /* Where to start from */ 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /* for bridged IP traffic encapsulated inside f.e. a vlan header, 5869bcfcaf5SStephen Hemminger * we need to make room for the encapsulating header 5879bcfcaf5SStephen Hemminger */ 5889bcfcaf5SStephen Hemminger pad = nf_bridge_pad(skb); 589*d8d1f30bSChangli Gao ll_rs = LL_RESERVED_SPACE_EXTRA(rt->dst.dev, pad); 5909bcfcaf5SStephen Hemminger mtu -= pad; 5919bcfcaf5SStephen Hemminger 5921da177e4SLinus Torvalds /* 5931da177e4SLinus Torvalds * Fragment the datagram. 5941da177e4SLinus Torvalds */ 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; 5971da177e4SLinus Torvalds not_last_frag = iph->frag_off & htons(IP_MF); 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* 6001da177e4SLinus Torvalds * Keep copying data until we run out. 6011da177e4SLinus Torvalds */ 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds while (left > 0) { 6041da177e4SLinus Torvalds len = left; 6051da177e4SLinus Torvalds /* IF: it doesn't fit, use 'mtu' - the data space left */ 6061da177e4SLinus Torvalds if (len > mtu) 6071da177e4SLinus Torvalds len = mtu; 6081da177e4SLinus Torvalds /* IF: we are not sending upto and including the packet end 6091da177e4SLinus Torvalds then align the next start on an eight byte boundary */ 6101da177e4SLinus Torvalds if (len < left) { 6111da177e4SLinus Torvalds len &= ~7; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds /* 6141da177e4SLinus Torvalds * Allocate buffer. 6151da177e4SLinus Torvalds */ 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if ((skb2 = alloc_skb(len+hlen+ll_rs, GFP_ATOMIC)) == NULL) { 61864ce2073SPatrick McHardy NETDEBUG(KERN_INFO "IP: frag: no memory for new fragment!\n"); 6191da177e4SLinus Torvalds err = -ENOMEM; 6201da177e4SLinus Torvalds goto fail; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds /* 6241da177e4SLinus Torvalds * Set up data on packet 6251da177e4SLinus Torvalds */ 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds ip_copy_metadata(skb2, skb); 6281da177e4SLinus Torvalds skb_reserve(skb2, ll_rs); 6291da177e4SLinus Torvalds skb_put(skb2, len + hlen); 630c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 631b0e380b1SArnaldo Carvalho de Melo skb2->transport_header = skb2->network_header + hlen; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* 6341da177e4SLinus Torvalds * Charge the memory for the fragment to any owner 6351da177e4SLinus Torvalds * it might possess 6361da177e4SLinus Torvalds */ 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds if (skb->sk) 6391da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds /* 6421da177e4SLinus Torvalds * Copy the packet header into the new buffer. 6431da177e4SLinus Torvalds */ 6441da177e4SLinus Torvalds 645d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* 6481da177e4SLinus Torvalds * Copy a block of the IP datagram. 6491da177e4SLinus Torvalds */ 650bff9b61cSArnaldo Carvalho de Melo if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len)) 6511da177e4SLinus Torvalds BUG(); 6521da177e4SLinus Torvalds left -= len; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds /* 6551da177e4SLinus Torvalds * Fill in the new header fields. 6561da177e4SLinus Torvalds */ 657eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 6581da177e4SLinus Torvalds iph->frag_off = htons((offset >> 3)); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds /* ANK: dirty, but effective trick. Upgrade options only if 6611da177e4SLinus Torvalds * the segment to be fragmented was THE FIRST (otherwise, 6621da177e4SLinus Torvalds * options are already fixed) and make it ONCE 6631da177e4SLinus Torvalds * on the initial skb, so that all the following fragments 6641da177e4SLinus Torvalds * will inherit fixed options. 6651da177e4SLinus Torvalds */ 6661da177e4SLinus Torvalds if (offset == 0) 6671da177e4SLinus Torvalds ip_options_fragment(skb); 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds /* 6701da177e4SLinus Torvalds * Added AC : If we are fragmenting a fragment that's not the 6711da177e4SLinus Torvalds * last fragment then keep MF on each bit 6721da177e4SLinus Torvalds */ 6731da177e4SLinus Torvalds if (left > 0 || not_last_frag) 6741da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 6751da177e4SLinus Torvalds ptr += len; 6761da177e4SLinus Torvalds offset += len; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* 6791da177e4SLinus Torvalds * Put this fragment into the sending queue. 6801da177e4SLinus Torvalds */ 6811da177e4SLinus Torvalds iph->tot_len = htons(len + hlen); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds ip_send_check(iph); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds err = output(skb2); 6861da177e4SLinus Torvalds if (err) 6871da177e4SLinus Torvalds goto fail; 688dafee490SWei Dong 6895e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES); 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds kfree_skb(skb); 6925e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS); 6931da177e4SLinus Torvalds return err; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds fail: 6961da177e4SLinus Torvalds kfree_skb(skb); 6975e38e270SPavel Emelyanov IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS); 6981da177e4SLinus Torvalds return err; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 7012e2f7aefSPatrick McHardy EXPORT_SYMBOL(ip_fragment); 7022e2f7aefSPatrick McHardy 7031da177e4SLinus Torvalds int 7041da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds struct iovec *iov = from; 7071da177e4SLinus Torvalds 70884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 7091da177e4SLinus Torvalds if (memcpy_fromiovecend(to, iov, offset, len) < 0) 7101da177e4SLinus Torvalds return -EFAULT; 7111da177e4SLinus Torvalds } else { 71244bb9363SAl Viro __wsum csum = 0; 7131da177e4SLinus Torvalds if (csum_partial_copy_fromiovecend(to, iov, offset, len, &csum) < 0) 7141da177e4SLinus Torvalds return -EFAULT; 7151da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds return 0; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 72044bb9363SAl Viro static inline __wsum 7211da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds char *kaddr; 72444bb9363SAl Viro __wsum csum; 7251da177e4SLinus Torvalds kaddr = kmap(page); 7261da177e4SLinus Torvalds csum = csum_partial(kaddr + offset, copy, 0); 7271da177e4SLinus Torvalds kunmap(page); 7281da177e4SLinus Torvalds return csum; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7314b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk, 732e89e9cf5SAnanda Raju int getfrag(void *from, char *to, int offset, int len, 733e89e9cf5SAnanda Raju int odd, struct sk_buff *skb), 734e89e9cf5SAnanda Raju void *from, int length, int hh_len, int fragheaderlen, 735e89e9cf5SAnanda Raju int transhdrlen, int mtu, unsigned int flags) 736e89e9cf5SAnanda Raju { 737e89e9cf5SAnanda Raju struct sk_buff *skb; 738e89e9cf5SAnanda Raju int err; 739e89e9cf5SAnanda Raju 740e89e9cf5SAnanda Raju /* There is support for UDP fragmentation offload by network 741e89e9cf5SAnanda Raju * device, so create one single skb packet containing complete 742e89e9cf5SAnanda Raju * udp datagram 743e89e9cf5SAnanda Raju */ 744e89e9cf5SAnanda Raju if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) { 745e89e9cf5SAnanda Raju skb = sock_alloc_send_skb(sk, 746e89e9cf5SAnanda Raju hh_len + fragheaderlen + transhdrlen + 20, 747e89e9cf5SAnanda Raju (flags & MSG_DONTWAIT), &err); 748e89e9cf5SAnanda Raju 749e89e9cf5SAnanda Raju if (skb == NULL) 750e89e9cf5SAnanda Raju return err; 751e89e9cf5SAnanda Raju 752e89e9cf5SAnanda Raju /* reserve space for Hardware header */ 753e89e9cf5SAnanda Raju skb_reserve(skb, hh_len); 754e89e9cf5SAnanda Raju 755e89e9cf5SAnanda Raju /* create space for UDP/IP header */ 756e89e9cf5SAnanda Raju skb_put(skb, fragheaderlen + transhdrlen); 757e89e9cf5SAnanda Raju 758e89e9cf5SAnanda Raju /* initialize network header pointer */ 759c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 760e89e9cf5SAnanda Raju 761e89e9cf5SAnanda Raju /* initialize protocol header pointer */ 762b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + fragheaderlen; 763e89e9cf5SAnanda Raju 76484fa7933SPatrick McHardy skb->ip_summed = CHECKSUM_PARTIAL; 765e89e9cf5SAnanda Raju skb->csum = 0; 766e89e9cf5SAnanda Raju sk->sk_sndmsg_off = 0; 767e89e9cf5SAnanda Raju 768e89e9cf5SAnanda Raju /* specify the length of each IP datagram fragment */ 7697967168cSHerbert Xu skb_shinfo(skb)->gso_size = mtu - fragheaderlen; 770f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 771e89e9cf5SAnanda Raju __skb_queue_tail(&sk->sk_write_queue, skb); 772e89e9cf5SAnanda Raju } 773be9164e7SKostya B 774be9164e7SKostya B return skb_append_datato_frags(sk, skb, getfrag, from, 775be9164e7SKostya B (length - transhdrlen)); 776e89e9cf5SAnanda Raju } 777e89e9cf5SAnanda Raju 7781da177e4SLinus Torvalds /* 7791da177e4SLinus Torvalds * ip_append_data() and ip_append_page() can make one large IP datagram 7801da177e4SLinus Torvalds * from many pieces of data. Each pieces will be holded on the socket 7811da177e4SLinus Torvalds * until ip_push_pending_frames() is called. Each piece can be a page 7821da177e4SLinus Torvalds * or non-page data. 7831da177e4SLinus Torvalds * 7841da177e4SLinus Torvalds * Not only UDP, other transport protocols - e.g. raw sockets - can use 7851da177e4SLinus Torvalds * this interface potentially. 7861da177e4SLinus Torvalds * 7871da177e4SLinus Torvalds * LATER: length must be adjusted by pad at tail, when it is required. 7881da177e4SLinus Torvalds */ 7891da177e4SLinus Torvalds int ip_append_data(struct sock *sk, 7901da177e4SLinus Torvalds int getfrag(void *from, char *to, int offset, int len, 7911da177e4SLinus Torvalds int odd, struct sk_buff *skb), 7921da177e4SLinus Torvalds void *from, int length, int transhdrlen, 7932e77d89bSEric Dumazet struct ipcm_cookie *ipc, struct rtable **rtp, 7941da177e4SLinus Torvalds unsigned int flags) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 7971da177e4SLinus Torvalds struct sk_buff *skb; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds struct ip_options *opt = NULL; 8001da177e4SLinus Torvalds int hh_len; 8011da177e4SLinus Torvalds int exthdrlen; 8021da177e4SLinus Torvalds int mtu; 8031da177e4SLinus Torvalds int copy; 8041da177e4SLinus Torvalds int err; 8051da177e4SLinus Torvalds int offset = 0; 8061da177e4SLinus Torvalds unsigned int maxfraglen, fragheaderlen; 8071da177e4SLinus Torvalds int csummode = CHECKSUM_NONE; 8082e77d89bSEric Dumazet struct rtable *rt; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds if (flags&MSG_PROBE) 8111da177e4SLinus Torvalds return 0; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) { 8141da177e4SLinus Torvalds /* 8151da177e4SLinus Torvalds * setup for corking. 8161da177e4SLinus Torvalds */ 8171da177e4SLinus Torvalds opt = ipc->opt; 8181da177e4SLinus Torvalds if (opt) { 8191da177e4SLinus Torvalds if (inet->cork.opt == NULL) { 8201da177e4SLinus Torvalds inet->cork.opt = kmalloc(sizeof(struct ip_options) + 40, sk->sk_allocation); 8211da177e4SLinus Torvalds if (unlikely(inet->cork.opt == NULL)) 8221da177e4SLinus Torvalds return -ENOBUFS; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds memcpy(inet->cork.opt, opt, sizeof(struct ip_options)+opt->optlen); 8251da177e4SLinus Torvalds inet->cork.flags |= IPCORK_OPT; 8261da177e4SLinus Torvalds inet->cork.addr = ipc->addr; 8271da177e4SLinus Torvalds } 8282e77d89bSEric Dumazet rt = *rtp; 829788d908fSJulien TINNES if (unlikely(!rt)) 830788d908fSJulien TINNES return -EFAULT; 8312e77d89bSEric Dumazet /* 8322e77d89bSEric Dumazet * We steal reference to this route, caller should not release it 8332e77d89bSEric Dumazet */ 8342e77d89bSEric Dumazet *rtp = NULL; 835628a5c56SJohn Heffner inet->cork.fragsize = mtu = inet->pmtudisc == IP_PMTUDISC_PROBE ? 836*d8d1f30bSChangli Gao rt->dst.dev->mtu : 837*d8d1f30bSChangli Gao dst_mtu(rt->dst.path); 838*d8d1f30bSChangli Gao inet->cork.dst = &rt->dst; 8391da177e4SLinus Torvalds inet->cork.length = 0; 8401da177e4SLinus Torvalds sk->sk_sndmsg_page = NULL; 8411da177e4SLinus Torvalds sk->sk_sndmsg_off = 0; 842*d8d1f30bSChangli Gao if ((exthdrlen = rt->dst.header_len) != 0) { 8431da177e4SLinus Torvalds length += exthdrlen; 8441da177e4SLinus Torvalds transhdrlen += exthdrlen; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds } else { 847c8cdaf99SYOSHIFUJI Hideaki rt = (struct rtable *)inet->cork.dst; 8481da177e4SLinus Torvalds if (inet->cork.flags & IPCORK_OPT) 8491da177e4SLinus Torvalds opt = inet->cork.opt; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds transhdrlen = 0; 8521da177e4SLinus Torvalds exthdrlen = 0; 8531da177e4SLinus Torvalds mtu = inet->cork.fragsize; 8541da177e4SLinus Torvalds } 855*d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 8581da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds if (inet->cork.length + length > 0xFFFF - fragheaderlen) { 861c720c7e8SEric Dumazet ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->inet_dport, 862c720c7e8SEric Dumazet mtu-exthdrlen); 8631da177e4SLinus Torvalds return -EMSGSIZE; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds /* 8671da177e4SLinus Torvalds * transhdrlen > 0 means that this is the first fragment and we wish 8681da177e4SLinus Torvalds * it won't be fragmented in the future. 8691da177e4SLinus Torvalds */ 8701da177e4SLinus Torvalds if (transhdrlen && 8711da177e4SLinus Torvalds length + fragheaderlen <= mtu && 872*d8d1f30bSChangli Gao rt->dst.dev->features & NETIF_F_V4_CSUM && 8731da177e4SLinus Torvalds !exthdrlen) 87484fa7933SPatrick McHardy csummode = CHECKSUM_PARTIAL; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds inet->cork.length += length; 877be9164e7SKostya B if (((length> mtu) || !skb_queue_empty(&sk->sk_write_queue)) && 878be9164e7SKostya B (sk->sk_protocol == IPPROTO_UDP) && 879*d8d1f30bSChangli Gao (rt->dst.dev->features & NETIF_F_UFO)) { 880baa829d8SPatrick McHardy err = ip_ufo_append_data(sk, getfrag, from, length, hh_len, 881baa829d8SPatrick McHardy fragheaderlen, transhdrlen, mtu, 882baa829d8SPatrick McHardy flags); 883baa829d8SPatrick McHardy if (err) 884e89e9cf5SAnanda Raju goto error; 885e89e9cf5SAnanda Raju return 0; 886e89e9cf5SAnanda Raju } 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds /* So, what's going on in the loop below? 8891da177e4SLinus Torvalds * 8901da177e4SLinus Torvalds * We use calculated fragment length to generate chained skb, 8911da177e4SLinus Torvalds * each of segments is IP fragment ready for sending to network after 8921da177e4SLinus Torvalds * adding appropriate IP header. 8931da177e4SLinus Torvalds */ 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) 8961da177e4SLinus Torvalds goto alloc_new_skb; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds while (length > 0) { 8991da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 9001da177e4SLinus Torvalds copy = mtu - skb->len; 9011da177e4SLinus Torvalds if (copy < length) 9021da177e4SLinus Torvalds copy = maxfraglen - skb->len; 9031da177e4SLinus Torvalds if (copy <= 0) { 9041da177e4SLinus Torvalds char *data; 9051da177e4SLinus Torvalds unsigned int datalen; 9061da177e4SLinus Torvalds unsigned int fraglen; 9071da177e4SLinus Torvalds unsigned int fraggap; 9081da177e4SLinus Torvalds unsigned int alloclen; 9091da177e4SLinus Torvalds struct sk_buff *skb_prev; 9101da177e4SLinus Torvalds alloc_new_skb: 9111da177e4SLinus Torvalds skb_prev = skb; 9121da177e4SLinus Torvalds if (skb_prev) 9131da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 9141da177e4SLinus Torvalds else 9151da177e4SLinus Torvalds fraggap = 0; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds /* 9181da177e4SLinus Torvalds * If remaining data exceeds the mtu, 9191da177e4SLinus Torvalds * we know we need more fragment(s). 9201da177e4SLinus Torvalds */ 9211da177e4SLinus Torvalds datalen = length + fraggap; 9221da177e4SLinus Torvalds if (datalen > mtu - fragheaderlen) 9231da177e4SLinus Torvalds datalen = maxfraglen - fragheaderlen; 9241da177e4SLinus Torvalds fraglen = datalen + fragheaderlen; 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds if ((flags & MSG_MORE) && 927*d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 9281da177e4SLinus Torvalds alloclen = mtu; 9291da177e4SLinus Torvalds else 9301da177e4SLinus Torvalds alloclen = datalen + fragheaderlen; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds /* The last fragment gets additional space at tail. 9331da177e4SLinus Torvalds * Note, with MSG_MORE we overallocate on fragments, 9341da177e4SLinus Torvalds * because we have no idea what fragment will be 9351da177e4SLinus Torvalds * the last. 9361da177e4SLinus Torvalds */ 9373d9dd756SZach Brown if (datalen == length + fraggap) 938*d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds if (transhdrlen) { 9411da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 9421da177e4SLinus Torvalds alloclen + hh_len + 15, 9431da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 9441da177e4SLinus Torvalds } else { 9451da177e4SLinus Torvalds skb = NULL; 9461da177e4SLinus Torvalds if (atomic_read(&sk->sk_wmem_alloc) <= 9471da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 9481da177e4SLinus Torvalds skb = sock_wmalloc(sk, 9491da177e4SLinus Torvalds alloclen + hh_len + 15, 1, 9501da177e4SLinus Torvalds sk->sk_allocation); 9511da177e4SLinus Torvalds if (unlikely(skb == NULL)) 9521da177e4SLinus Torvalds err = -ENOBUFS; 95351f31cabSPatrick Ohly else 95451f31cabSPatrick Ohly /* only the initial fragment is 95551f31cabSPatrick Ohly time stamped */ 95651f31cabSPatrick Ohly ipc->shtx.flags = 0; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds if (skb == NULL) 9591da177e4SLinus Torvalds goto error; 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * Fill in the control structures 9631da177e4SLinus Torvalds */ 9641da177e4SLinus Torvalds skb->ip_summed = csummode; 9651da177e4SLinus Torvalds skb->csum = 0; 9661da177e4SLinus Torvalds skb_reserve(skb, hh_len); 96751f31cabSPatrick Ohly *skb_tx(skb) = ipc->shtx; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* 9701da177e4SLinus Torvalds * Find where to start putting bytes. 9711da177e4SLinus Torvalds */ 9721da177e4SLinus Torvalds data = skb_put(skb, fraglen); 973c14d2450SArnaldo Carvalho de Melo skb_set_network_header(skb, exthdrlen); 974b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 975b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 9761da177e4SLinus Torvalds data += fragheaderlen; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds if (fraggap) { 9791da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 9801da177e4SLinus Torvalds skb_prev, maxfraglen, 9811da177e4SLinus Torvalds data + transhdrlen, fraggap, 0); 9821da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 9831da177e4SLinus Torvalds skb->csum); 9841da177e4SLinus Torvalds data += fraggap; 985e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds copy = datalen - transhdrlen - fraggap; 9891da177e4SLinus Torvalds if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { 9901da177e4SLinus Torvalds err = -EFAULT; 9911da177e4SLinus Torvalds kfree_skb(skb); 9921da177e4SLinus Torvalds goto error; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds offset += copy; 9961da177e4SLinus Torvalds length -= datalen - fraggap; 9971da177e4SLinus Torvalds transhdrlen = 0; 9981da177e4SLinus Torvalds exthdrlen = 0; 9991da177e4SLinus Torvalds csummode = CHECKSUM_NONE; 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 10021da177e4SLinus Torvalds * Put the packet on the pending queue. 10031da177e4SLinus Torvalds */ 10041da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 10051da177e4SLinus Torvalds continue; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds if (copy > length) 10091da177e4SLinus Torvalds copy = length; 10101da177e4SLinus Torvalds 1011*d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) { 10121da177e4SLinus Torvalds unsigned int off; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds off = skb->len; 10151da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 10161da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 10171da177e4SLinus Torvalds __skb_trim(skb, off); 10181da177e4SLinus Torvalds err = -EFAULT; 10191da177e4SLinus Torvalds goto error; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds } else { 10221da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 10231da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1]; 10241da177e4SLinus Torvalds struct page *page = sk->sk_sndmsg_page; 10251da177e4SLinus Torvalds int off = sk->sk_sndmsg_off; 10261da177e4SLinus Torvalds unsigned int left; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds if (page && (left = PAGE_SIZE - off) > 0) { 10291da177e4SLinus Torvalds if (copy >= left) 10301da177e4SLinus Torvalds copy = left; 10311da177e4SLinus Torvalds if (page != frag->page) { 10321da177e4SLinus Torvalds if (i == MAX_SKB_FRAGS) { 10331da177e4SLinus Torvalds err = -EMSGSIZE; 10341da177e4SLinus Torvalds goto error; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds get_page(page); 10371da177e4SLinus Torvalds skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0); 10381da177e4SLinus Torvalds frag = &skb_shinfo(skb)->frags[i]; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds } else if (i < MAX_SKB_FRAGS) { 10411da177e4SLinus Torvalds if (copy > PAGE_SIZE) 10421da177e4SLinus Torvalds copy = PAGE_SIZE; 10431da177e4SLinus Torvalds page = alloc_pages(sk->sk_allocation, 0); 10441da177e4SLinus Torvalds if (page == NULL) { 10451da177e4SLinus Torvalds err = -ENOMEM; 10461da177e4SLinus Torvalds goto error; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds sk->sk_sndmsg_page = page; 10491da177e4SLinus Torvalds sk->sk_sndmsg_off = 0; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds skb_fill_page_desc(skb, i, page, 0, 0); 10521da177e4SLinus Torvalds frag = &skb_shinfo(skb)->frags[i]; 10531da177e4SLinus Torvalds } else { 10541da177e4SLinus Torvalds err = -EMSGSIZE; 10551da177e4SLinus Torvalds goto error; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) { 10581da177e4SLinus Torvalds err = -EFAULT; 10591da177e4SLinus Torvalds goto error; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds sk->sk_sndmsg_off += copy; 10621da177e4SLinus Torvalds frag->size += copy; 10631da177e4SLinus Torvalds skb->len += copy; 10641da177e4SLinus Torvalds skb->data_len += copy; 1065f945fa7aSHerbert Xu skb->truesize += copy; 1066f945fa7aSHerbert Xu atomic_add(copy, &sk->sk_wmem_alloc); 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds offset += copy; 10691da177e4SLinus Torvalds length -= copy; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds return 0; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds error: 10751da177e4SLinus Torvalds inet->cork.length -= length; 10765e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 10771da177e4SLinus Torvalds return err; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds ssize_t ip_append_page(struct sock *sk, struct page *page, 10811da177e4SLinus Torvalds int offset, size_t size, int flags) 10821da177e4SLinus Torvalds { 10831da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 10841da177e4SLinus Torvalds struct sk_buff *skb; 10851da177e4SLinus Torvalds struct rtable *rt; 10861da177e4SLinus Torvalds struct ip_options *opt = NULL; 10871da177e4SLinus Torvalds int hh_len; 10881da177e4SLinus Torvalds int mtu; 10891da177e4SLinus Torvalds int len; 10901da177e4SLinus Torvalds int err; 10911da177e4SLinus Torvalds unsigned int maxfraglen, fragheaderlen, fraggap; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds if (inet->hdrincl) 10941da177e4SLinus Torvalds return -EPERM; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds if (flags&MSG_PROBE) 10971da177e4SLinus Torvalds return 0; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) 11001da177e4SLinus Torvalds return -EINVAL; 11011da177e4SLinus Torvalds 1102c8cdaf99SYOSHIFUJI Hideaki rt = (struct rtable *)inet->cork.dst; 11031da177e4SLinus Torvalds if (inet->cork.flags & IPCORK_OPT) 11041da177e4SLinus Torvalds opt = inet->cork.opt; 11051da177e4SLinus Torvalds 1106*d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) 11071da177e4SLinus Torvalds return -EOPNOTSUPP; 11081da177e4SLinus Torvalds 1109*d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 11101da177e4SLinus Torvalds mtu = inet->cork.fragsize; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 11131da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds if (inet->cork.length + size > 0xFFFF - fragheaderlen) { 1116c720c7e8SEric Dumazet ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->inet_dport, mtu); 11171da177e4SLinus Torvalds return -EMSGSIZE; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) 11211da177e4SLinus Torvalds return -EINVAL; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds inet->cork.length += size; 1124e89e9cf5SAnanda Raju if ((sk->sk_protocol == IPPROTO_UDP) && 1125*d8d1f30bSChangli Gao (rt->dst.dev->features & NETIF_F_UFO)) { 11267967168cSHerbert Xu skb_shinfo(skb)->gso_size = mtu - fragheaderlen; 1127f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 11287967168cSHerbert Xu } 1129e89e9cf5SAnanda Raju 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds while (size > 0) { 11321da177e4SLinus Torvalds int i; 11331da177e4SLinus Torvalds 113489114afdSHerbert Xu if (skb_is_gso(skb)) 1135e89e9cf5SAnanda Raju len = size; 1136e89e9cf5SAnanda Raju else { 1137e89e9cf5SAnanda Raju 11381da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 11391da177e4SLinus Torvalds len = mtu - skb->len; 11401da177e4SLinus Torvalds if (len < size) 11411da177e4SLinus Torvalds len = maxfraglen - skb->len; 1142e89e9cf5SAnanda Raju } 11431da177e4SLinus Torvalds if (len <= 0) { 11441da177e4SLinus Torvalds struct sk_buff *skb_prev; 11451da177e4SLinus Torvalds int alloclen; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds skb_prev = skb; 11481da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds alloclen = fragheaderlen + hh_len + fraggap + 15; 11511da177e4SLinus Torvalds skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); 11521da177e4SLinus Torvalds if (unlikely(!skb)) { 11531da177e4SLinus Torvalds err = -ENOBUFS; 11541da177e4SLinus Torvalds goto error; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * Fill in the control structures 11591da177e4SLinus Torvalds */ 11601da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 11611da177e4SLinus Torvalds skb->csum = 0; 11621da177e4SLinus Torvalds skb_reserve(skb, hh_len); 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds /* 11651da177e4SLinus Torvalds * Find where to start putting bytes. 11661da177e4SLinus Torvalds */ 1167967b05f6SArnaldo Carvalho de Melo skb_put(skb, fragheaderlen + fraggap); 11682ca9e6f2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1169b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1170b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 11711da177e4SLinus Torvalds if (fraggap) { 1172967b05f6SArnaldo Carvalho de Melo skb->csum = skb_copy_and_csum_bits(skb_prev, 1173967b05f6SArnaldo Carvalho de Melo maxfraglen, 11749c70220bSArnaldo Carvalho de Melo skb_transport_header(skb), 1175967b05f6SArnaldo Carvalho de Melo fraggap, 0); 11761da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 11771da177e4SLinus Torvalds skb->csum); 1178e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* 11821da177e4SLinus Torvalds * Put the packet on the pending queue. 11831da177e4SLinus Torvalds */ 11841da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 11851da177e4SLinus Torvalds continue; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds i = skb_shinfo(skb)->nr_frags; 11891da177e4SLinus Torvalds if (len > size) 11901da177e4SLinus Torvalds len = size; 11911da177e4SLinus Torvalds if (skb_can_coalesce(skb, i, page, offset)) { 11921da177e4SLinus Torvalds skb_shinfo(skb)->frags[i-1].size += len; 11931da177e4SLinus Torvalds } else if (i < MAX_SKB_FRAGS) { 11941da177e4SLinus Torvalds get_page(page); 11951da177e4SLinus Torvalds skb_fill_page_desc(skb, i, page, offset, len); 11961da177e4SLinus Torvalds } else { 11971da177e4SLinus Torvalds err = -EMSGSIZE; 11981da177e4SLinus Torvalds goto error; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds if (skb->ip_summed == CHECKSUM_NONE) { 120244bb9363SAl Viro __wsum csum; 12031da177e4SLinus Torvalds csum = csum_page(page, offset, len); 12041da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, skb->len); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds skb->len += len; 12081da177e4SLinus Torvalds skb->data_len += len; 12091e34a11dSDavid S. Miller skb->truesize += len; 12101e34a11dSDavid S. Miller atomic_add(len, &sk->sk_wmem_alloc); 12111da177e4SLinus Torvalds offset += len; 12121da177e4SLinus Torvalds size -= len; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds return 0; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds error: 12171da177e4SLinus Torvalds inet->cork.length -= size; 12185e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 12191da177e4SLinus Torvalds return err; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 1222429f08e9SPavel Emelyanov static void ip_cork_release(struct inet_sock *inet) 1223429f08e9SPavel Emelyanov { 1224429f08e9SPavel Emelyanov inet->cork.flags &= ~IPCORK_OPT; 1225429f08e9SPavel Emelyanov kfree(inet->cork.opt); 1226429f08e9SPavel Emelyanov inet->cork.opt = NULL; 1227c8cdaf99SYOSHIFUJI Hideaki dst_release(inet->cork.dst); 1228c8cdaf99SYOSHIFUJI Hideaki inet->cork.dst = NULL; 1229429f08e9SPavel Emelyanov } 1230429f08e9SPavel Emelyanov 12311da177e4SLinus Torvalds /* 12321da177e4SLinus Torvalds * Combined all pending IP fragments on the socket as one IP datagram 12331da177e4SLinus Torvalds * and push them out. 12341da177e4SLinus Torvalds */ 12351da177e4SLinus Torvalds int ip_push_pending_frames(struct sock *sk) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 12381da177e4SLinus Torvalds struct sk_buff **tail_skb; 12391da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 12400388b004SPavel Emelyanov struct net *net = sock_net(sk); 12411da177e4SLinus Torvalds struct ip_options *opt = NULL; 1242c8cdaf99SYOSHIFUJI Hideaki struct rtable *rt = (struct rtable *)inet->cork.dst; 12431da177e4SLinus Torvalds struct iphdr *iph; 124476ab608dSAlexey Dobriyan __be16 df = 0; 12451da177e4SLinus Torvalds __u8 ttl; 12461da177e4SLinus Torvalds int err = 0; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL) 12491da177e4SLinus Torvalds goto out; 12501da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1253d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1254bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12551da177e4SLinus Torvalds while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) { 1256cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 12571da177e4SLinus Torvalds *tail_skb = tmp_skb; 12581da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 12591da177e4SLinus Torvalds skb->len += tmp_skb->len; 12601da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 12611da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 12621da177e4SLinus Torvalds tmp_skb->destructor = NULL; 12631da177e4SLinus Torvalds tmp_skb->sk = NULL; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow 12671da177e4SLinus Torvalds * to fragment the frame generated here. No matter, what transforms 12681da177e4SLinus Torvalds * how transforms change size of the packet, it will come out. 12691da177e4SLinus Torvalds */ 1270628a5c56SJohn Heffner if (inet->pmtudisc < IP_PMTUDISC_DO) 12711da177e4SLinus Torvalds skb->local_df = 1; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds /* DF bit is set when we want to see DF on outgoing frames. 12741da177e4SLinus Torvalds * If local_df is set too, we still allow to fragment this frame 12751da177e4SLinus Torvalds * locally. */ 1276628a5c56SJohn Heffner if (inet->pmtudisc >= IP_PMTUDISC_DO || 1277*d8d1f30bSChangli Gao (skb->len <= dst_mtu(&rt->dst) && 1278*d8d1f30bSChangli Gao ip_dont_fragment(sk, &rt->dst))) 12791da177e4SLinus Torvalds df = htons(IP_DF); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds if (inet->cork.flags & IPCORK_OPT) 12821da177e4SLinus Torvalds opt = inet->cork.opt; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (rt->rt_type == RTN_MULTICAST) 12851da177e4SLinus Torvalds ttl = inet->mc_ttl; 12861da177e4SLinus Torvalds else 1287*d8d1f30bSChangli Gao ttl = ip_select_ttl(inet, &rt->dst); 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds iph = (struct iphdr *)skb->data; 12901da177e4SLinus Torvalds iph->version = 4; 12911da177e4SLinus Torvalds iph->ihl = 5; 12921da177e4SLinus Torvalds if (opt) { 12931da177e4SLinus Torvalds iph->ihl += opt->optlen>>2; 12941da177e4SLinus Torvalds ip_options_build(skb, opt, inet->cork.addr, rt, 0); 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds iph->tos = inet->tos; 12971da177e4SLinus Torvalds iph->frag_off = df; 1298*d8d1f30bSChangli Gao ip_select_ident(iph, &rt->dst, sk); 12991da177e4SLinus Torvalds iph->ttl = ttl; 13001da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 13011da177e4SLinus Torvalds iph->saddr = rt->rt_src; 13021da177e4SLinus Torvalds iph->daddr = rt->rt_dst; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds skb->priority = sk->sk_priority; 13054a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1306a21bba94SEric Dumazet /* 1307a21bba94SEric Dumazet * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1308a21bba94SEric Dumazet * on dst refcount 1309a21bba94SEric Dumazet */ 1310a21bba94SEric Dumazet inet->cork.dst = NULL; 1311*d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 13121da177e4SLinus Torvalds 131396793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 13140388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 131596793b48SDavid L Stevens skb_transport_header(skb))->type); 131696793b48SDavid L Stevens 13171da177e4SLinus Torvalds /* Netfilter gets whole the not fragmented skb. */ 1318c439cb2eSHerbert Xu err = ip_local_out(skb); 13191da177e4SLinus Torvalds if (err) { 13201da177e4SLinus Torvalds if (err > 0) 13216ce9e7b5SEric Dumazet err = net_xmit_errno(err); 13221da177e4SLinus Torvalds if (err) 13231da177e4SLinus Torvalds goto error; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds out: 1327429f08e9SPavel Emelyanov ip_cork_release(inet); 13281da177e4SLinus Torvalds return err; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds error: 13315e38e270SPavel Emelyanov IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 13321da177e4SLinus Torvalds goto out; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds /* 13361da177e4SLinus Torvalds * Throw away all pending data on the socket. 13371da177e4SLinus Torvalds */ 13381da177e4SLinus Torvalds void ip_flush_pending_frames(struct sock *sk) 13391da177e4SLinus Torvalds { 13401da177e4SLinus Torvalds struct sk_buff *skb; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) 13431da177e4SLinus Torvalds kfree_skb(skb); 13441da177e4SLinus Torvalds 1345429f08e9SPavel Emelyanov ip_cork_release(inet_sk(sk)); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds /* 13501da177e4SLinus Torvalds * Fetch data from kernel space and fill in checksum if needed. 13511da177e4SLinus Torvalds */ 13521da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset, 13531da177e4SLinus Torvalds int len, int odd, struct sk_buff *skb) 13541da177e4SLinus Torvalds { 13555084205fSAl Viro __wsum csum; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0); 13581da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 13591da177e4SLinus Torvalds return 0; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds /* 13631da177e4SLinus Torvalds * Generic function to send a packet as reply to another packet. 13641da177e4SLinus Torvalds * Used to send TCP resets so far. ICMP should use this function too. 13651da177e4SLinus Torvalds * 13661da177e4SLinus Torvalds * Should run single threaded per socket because it uses the sock 13671da177e4SLinus Torvalds * structure to pass arguments. 13681da177e4SLinus Torvalds */ 13691da177e4SLinus Torvalds void ip_send_reply(struct sock *sk, struct sk_buff *skb, struct ip_reply_arg *arg, 13701da177e4SLinus Torvalds unsigned int len) 13711da177e4SLinus Torvalds { 13721da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 13731da177e4SLinus Torvalds struct { 13741da177e4SLinus Torvalds struct ip_options opt; 13751da177e4SLinus Torvalds char data[40]; 13761da177e4SLinus Torvalds } replyopts; 13771da177e4SLinus Torvalds struct ipcm_cookie ipc; 13783ca3c68eSAl Viro __be32 daddr; 1379511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds if (ip_options_echo(&replyopts.opt, skb)) 13821da177e4SLinus Torvalds return; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds daddr = ipc.addr = rt->rt_src; 13851da177e4SLinus Torvalds ipc.opt = NULL; 138651f31cabSPatrick Ohly ipc.shtx.flags = 0; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (replyopts.opt.optlen) { 13891da177e4SLinus Torvalds ipc.opt = &replyopts.opt; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds if (ipc.opt->srr) 13921da177e4SLinus Torvalds daddr = replyopts.opt.faddr; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds { 1396f0e48dbfSPatrick McHardy struct flowi fl = { .oif = arg->bound_dev_if, 1397f0e48dbfSPatrick McHardy .nl_u = { .ip4_u = 13981da177e4SLinus Torvalds { .daddr = daddr, 13991da177e4SLinus Torvalds .saddr = rt->rt_spec_dst, 1400eddc9ec5SArnaldo Carvalho de Melo .tos = RT_TOS(ip_hdr(skb)->tos) } }, 14011da177e4SLinus Torvalds /* Not quite clean, but right. */ 14021da177e4SLinus Torvalds .uli_u = { .ports = 1403aa8223c7SArnaldo Carvalho de Melo { .sport = tcp_hdr(skb)->dest, 1404aa8223c7SArnaldo Carvalho de Melo .dport = tcp_hdr(skb)->source } }, 140586b08d86SKOVACS Krisztian .proto = sk->sk_protocol, 140686b08d86SKOVACS Krisztian .flags = ip_reply_arg_flowi_flags(arg) }; 1407beb8d13bSVenkat Yekkirala security_skb_classify_flow(skb, &fl); 14083b1e0a65SYOSHIFUJI Hideaki if (ip_route_output_key(sock_net(sk), &rt, &fl)) 14091da177e4SLinus Torvalds return; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds /* And let IP do all the hard work. 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds This chunk is not reenterable, hence spinlock. 14151da177e4SLinus Torvalds Note that it uses the fact, that this function is called 14161da177e4SLinus Torvalds with locally disabled BH and that sk cannot be already spinlocked. 14171da177e4SLinus Torvalds */ 14181da177e4SLinus Torvalds bh_lock_sock(sk); 1419eddc9ec5SArnaldo Carvalho de Melo inet->tos = ip_hdr(skb)->tos; 14201da177e4SLinus Torvalds sk->sk_priority = skb->priority; 1421eddc9ec5SArnaldo Carvalho de Melo sk->sk_protocol = ip_hdr(skb)->protocol; 1422f0e48dbfSPatrick McHardy sk->sk_bound_dev_if = arg->bound_dev_if; 14231da177e4SLinus Torvalds ip_append_data(sk, ip_reply_glue_bits, arg->iov->iov_base, len, 0, 14242e77d89bSEric Dumazet &ipc, &rt, MSG_DONTWAIT); 14251da177e4SLinus Torvalds if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) { 14261da177e4SLinus Torvalds if (arg->csumoffset >= 0) 14279c70220bSArnaldo Carvalho de Melo *((__sum16 *)skb_transport_header(skb) + 14289c70220bSArnaldo Carvalho de Melo arg->csumoffset) = csum_fold(csum_add(skb->csum, 14299c70220bSArnaldo Carvalho de Melo arg->csum)); 14301da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 14311da177e4SLinus Torvalds ip_push_pending_frames(sk); 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds bh_unlock_sock(sk); 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds ip_rt_put(rt); 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds void __init ip_init(void) 14401da177e4SLinus Torvalds { 14411da177e4SLinus Torvalds ip_rt_init(); 14421da177e4SLinus Torvalds inet_initpeers(); 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds #if defined(CONFIG_IP_MULTICAST) && defined(CONFIG_PROC_FS) 14451da177e4SLinus Torvalds igmp_mc_proc_init(); 14461da177e4SLinus Torvalds #endif 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds EXPORT_SYMBOL(ip_generic_getfrag); 14501da177e4SLinus Torvalds EXPORT_SYMBOL(ip_queue_xmit); 14511da177e4SLinus Torvalds EXPORT_SYMBOL(ip_send_check); 1452