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/netlink.h> 806cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h> 811da177e4SLinus Torvalds 82694869b3SEric W. Biederman static int 83694869b3SEric W. Biederman ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 84c5501eb3SFlorian Westphal unsigned int mtu, 85694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)); 8649d16b23SAndy Zhou 871da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */ 882fbd9679SDenis Efremov void ip_send_check(struct iphdr *iph) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds iph->check = 0; 911da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 921da177e4SLinus Torvalds } 934bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check); 941da177e4SLinus Torvalds 95cf91a99dSEric W. Biederman int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 96c439cb2eSHerbert Xu { 97c439cb2eSHerbert Xu struct iphdr *iph = ip_hdr(skb); 98c439cb2eSHerbert Xu 99c439cb2eSHerbert Xu iph->tot_len = htons(skb->len); 100c439cb2eSHerbert Xu ip_send_check(iph); 10129a26a56SEric W. Biederman return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, 10229a26a56SEric W. Biederman net, sk, skb, NULL, skb_dst(skb)->dev, 10313206b6bSEric W. Biederman dst_output); 1047026b1ddSDavid Miller } 1057026b1ddSDavid Miller 10633224b16SEric W. Biederman int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 107c439cb2eSHerbert Xu { 108c439cb2eSHerbert Xu int err; 109c439cb2eSHerbert Xu 110cf91a99dSEric W. Biederman err = __ip_local_out(net, sk, skb); 111c439cb2eSHerbert Xu if (likely(err == 1)) 11213206b6bSEric W. Biederman err = dst_output(net, sk, skb); 113c439cb2eSHerbert Xu 114c439cb2eSHerbert Xu return err; 115c439cb2eSHerbert Xu } 116e2cb77dbSEric W. Biederman EXPORT_SYMBOL_GPL(ip_local_out); 117c439cb2eSHerbert Xu 1181da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int ttl = inet->uc_ttl; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (ttl < 0) 123323e126fSDavid S. Miller ttl = ip4_dst_hoplimit(dst); 1241da177e4SLinus Torvalds return ttl; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* 1281da177e4SLinus Torvalds * Add an ip header to a skbuff and send it out. 1291da177e4SLinus Torvalds * 1301da177e4SLinus Torvalds */ 131cfe673b0SEric Dumazet int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, 132f6d8bd05SEric Dumazet __be32 saddr, __be32 daddr, struct ip_options_rcu *opt) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 135511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 13677589ce0SEric W. Biederman struct net *net = sock_net(sk); 1371da177e4SLinus Torvalds struct iphdr *iph; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* Build the IP header. */ 140f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0)); 1418856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 142eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1431da177e4SLinus Torvalds iph->version = 4; 1441da177e4SLinus Torvalds iph->ihl = 5; 1451da177e4SLinus Torvalds iph->tos = inet->tos; 146d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 147dd927a26SDavid S. Miller iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); 148dd927a26SDavid S. Miller iph->saddr = saddr; 1491da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 150cfe673b0SEric Dumazet if (ip_dont_fragment(sk, &rt->dst)) { 151cfe673b0SEric Dumazet iph->frag_off = htons(IP_DF); 152cfe673b0SEric Dumazet iph->id = 0; 153cfe673b0SEric Dumazet } else { 154cfe673b0SEric Dumazet iph->frag_off = 0; 15577589ce0SEric W. Biederman __ip_select_ident(net, iph, 1); 156cfe673b0SEric Dumazet } 1571da177e4SLinus Torvalds 158f6d8bd05SEric Dumazet if (opt && opt->opt.optlen) { 159f6d8bd05SEric Dumazet iph->ihl += opt->opt.optlen>>2; 160f6d8bd05SEric Dumazet ip_options_build(skb, &opt->opt, daddr, rt, 0); 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds skb->priority = sk->sk_priority; 1644a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* Send it out. */ 16733224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 1681da177e4SLinus Torvalds } 169d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 170d8c97a94SArnaldo Carvalho de Melo 171694869b3SEric W. Biederman static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 1721da177e4SLinus Torvalds { 173adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 17480787ebcSMitsuru Chinen struct rtable *rt = (struct rtable *)dst; 1751da177e4SLinus Torvalds struct net_device *dev = dst->dev; 176c2636b4dSChuck Lever unsigned int hh_len = LL_RESERVED_SPACE(dev); 177f6b72b62SDavid S. Miller struct neighbour *neigh; 178a263b309SDavid S. Miller u32 nexthop; 1791da177e4SLinus Torvalds 180edf391ffSNeil Horman if (rt->rt_type == RTN_MULTICAST) { 1814ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len); 182edf391ffSNeil Horman } else if (rt->rt_type == RTN_BROADCAST) 1834ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len); 18480787ebcSMitsuru Chinen 1851da177e4SLinus Torvalds /* Be paranoid, rather than too clever. */ 1863b04dddeSStephen Hemminger if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 1871da177e4SLinus Torvalds struct sk_buff *skb2; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); 19051456b29SIan Morris if (!skb2) { 1911da177e4SLinus Torvalds kfree_skb(skb); 1921da177e4SLinus Torvalds return -ENOMEM; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds if (skb->sk) 1951da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 1965d0ba55bSEric Dumazet consume_skb(skb); 1971da177e4SLinus Torvalds skb = skb2; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 200a263b309SDavid S. Miller rcu_read_lock_bh(); 201155e8336SJulian Anastasov nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr); 202a263b309SDavid S. Miller neigh = __ipv4_neigh_lookup_noref(dev, nexthop); 203a263b309SDavid S. Miller if (unlikely(!neigh)) 204a263b309SDavid S. Miller neigh = __neigh_create(&arp_tbl, &nexthop, dev, false); 2059871f1adSVasiliy Kulikov if (!IS_ERR(neigh)) { 2065110effeSDavid S. Miller int res = dst_neigh_output(dst, neigh, skb); 207f2c31e32SEric Dumazet 208a263b309SDavid S. Miller rcu_read_unlock_bh(); 209f2c31e32SEric Dumazet return res; 210f2c31e32SEric Dumazet } 211a263b309SDavid S. Miller rcu_read_unlock_bh(); 21205e3aa09SDavid S. Miller 213e87cc472SJoe Perches net_dbg_ratelimited("%s: No header cache and no neighbour!\n", 214e87cc472SJoe Perches __func__); 2151da177e4SLinus Torvalds kfree_skb(skb); 2161da177e4SLinus Torvalds return -EINVAL; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 219694869b3SEric W. Biederman static int ip_finish_output_gso(struct net *net, struct sock *sk, 220694869b3SEric W. Biederman struct sk_buff *skb, unsigned int mtu) 221c7ba65d7SFlorian Westphal { 222c7ba65d7SFlorian Westphal netdev_features_t features; 223c7ba65d7SFlorian Westphal struct sk_buff *segs; 224c7ba65d7SFlorian Westphal int ret = 0; 225c7ba65d7SFlorian Westphal 226*359ebda2SShmulik Ladkani /* common case: fragmentation of segments is not allowed, 227*359ebda2SShmulik Ladkani * or seglen is <= mtu 228*359ebda2SShmulik Ladkani */ 229*359ebda2SShmulik Ladkani if (((IPCB(skb)->flags & IPSKB_FRAG_SEGS) == 0) || 230ae7ef81eSMarcelo Ricardo Leitner skb_gso_validate_mtu(skb, mtu)) 231694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 232c7ba65d7SFlorian Westphal 233c7ba65d7SFlorian Westphal /* Slowpath - GSO segment length is exceeding the dst MTU. 234c7ba65d7SFlorian Westphal * 235c7ba65d7SFlorian Westphal * This can happen in two cases: 236c7ba65d7SFlorian Westphal * 1) TCP GRO packet, DF bit not set 237c7ba65d7SFlorian Westphal * 2) skb arrived via virtio-net, we thus get TSO/GSO skbs directly 238c7ba65d7SFlorian Westphal * from host network stack. 239c7ba65d7SFlorian Westphal */ 240c7ba65d7SFlorian Westphal features = netif_skb_features(skb); 2419207f9d4SKonstantin Khlebnikov BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET); 242c7ba65d7SFlorian Westphal segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 243330966e5SFlorian Westphal if (IS_ERR_OR_NULL(segs)) { 244c7ba65d7SFlorian Westphal kfree_skb(skb); 245c7ba65d7SFlorian Westphal return -ENOMEM; 246c7ba65d7SFlorian Westphal } 247c7ba65d7SFlorian Westphal 248c7ba65d7SFlorian Westphal consume_skb(skb); 249c7ba65d7SFlorian Westphal 250c7ba65d7SFlorian Westphal do { 251c7ba65d7SFlorian Westphal struct sk_buff *nskb = segs->next; 252c7ba65d7SFlorian Westphal int err; 253c7ba65d7SFlorian Westphal 254c7ba65d7SFlorian Westphal segs->next = NULL; 255694869b3SEric W. Biederman err = ip_fragment(net, sk, segs, mtu, ip_finish_output2); 256c7ba65d7SFlorian Westphal 257c7ba65d7SFlorian Westphal if (err && ret == 0) 258c7ba65d7SFlorian Westphal ret = err; 259c7ba65d7SFlorian Westphal segs = nskb; 260c7ba65d7SFlorian Westphal } while (segs); 261c7ba65d7SFlorian Westphal 262c7ba65d7SFlorian Westphal return ret; 263c7ba65d7SFlorian Westphal } 264c7ba65d7SFlorian Westphal 2650c4b51f0SEric W. Biederman static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 2661da177e4SLinus Torvalds { 267c5501eb3SFlorian Westphal unsigned int mtu; 268c5501eb3SFlorian Westphal 2695c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 2705c901daaSPatrick McHardy /* Policy lookup after SNAT yielded a new policy */ 27100db4124SIan Morris if (skb_dst(skb)->xfrm) { 27248d5cad8SPatrick McHardy IPCB(skb)->flags |= IPSKB_REROUTED; 27313206b6bSEric W. Biederman return dst_output(net, sk, skb); 27448d5cad8SPatrick McHardy } 2755c901daaSPatrick McHardy #endif 276fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 277c7ba65d7SFlorian Westphal if (skb_is_gso(skb)) 278694869b3SEric W. Biederman return ip_finish_output_gso(net, sk, skb, mtu); 279c7ba65d7SFlorian Westphal 280d6b915e2SFlorian Westphal if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU)) 281694869b3SEric W. Biederman return ip_fragment(net, sk, skb, mtu, ip_finish_output2); 282c7ba65d7SFlorian Westphal 283694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 286ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb) 2871da177e4SLinus Torvalds { 288511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 289d8d1f30bSChangli Gao struct net_device *dev = rt->dst.dev; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 2921da177e4SLinus Torvalds * If the indicated interface is up and running, send the packet. 2931da177e4SLinus Torvalds */ 29488f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds skb->dev = dev; 2971da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* 3001da177e4SLinus Torvalds * Multicasts are looped back for other local users 3011da177e4SLinus Torvalds */ 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds if (rt->rt_flags&RTCF_MULTICAST) { 3047ad6848cSOctavian Purdila if (sk_mc_loop(sk) 3051da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 3061da177e4SLinus Torvalds /* Small optimization: do not loopback not local frames, 3071da177e4SLinus Torvalds which returned after forwarding; they will be dropped 3081da177e4SLinus Torvalds by ip_mr_input in any case. 3091da177e4SLinus Torvalds Note, that local frames are looped back to be delivered 3101da177e4SLinus Torvalds to local recipients. 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds This check is duplicated in ip_mr_input at the moment. 3131da177e4SLinus Torvalds */ 3149d4fb27dSJoe Perches && 3159d4fb27dSJoe Perches ((rt->rt_flags & RTCF_LOCAL) || 3169d4fb27dSJoe Perches !(IPCB(skb)->flags & IPSKB_FORWARDED)) 3171da177e4SLinus Torvalds #endif 3181da177e4SLinus Torvalds ) { 3191da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 3201da177e4SLinus Torvalds if (newskb) 3219bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 32229a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 32395603e22SMichel Machado dev_loopback_xmit); 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* Multicasts with ttl 0 must not go beyond the host */ 3271da177e4SLinus Torvalds 328eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl == 0) { 3291da177e4SLinus Torvalds kfree_skb(skb); 3301da177e4SLinus Torvalds return 0; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds if (rt->rt_flags&RTCF_BROADCAST) { 3351da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 3361da177e4SLinus Torvalds if (newskb) 33729a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 33829a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 33929a26a56SEric W. Biederman dev_loopback_xmit); 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 34229a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 34329a26a56SEric W. Biederman net, sk, skb, NULL, skb->dev, 34429a26a56SEric W. Biederman ip_finish_output, 34548d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 348ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb) 3491da177e4SLinus Torvalds { 350adf30907SEric Dumazet struct net_device *dev = skb_dst(skb)->dev; 3511bd9bef6SPatrick McHardy 35288f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 3531da177e4SLinus Torvalds 3541bd9bef6SPatrick McHardy skb->dev = dev; 3551bd9bef6SPatrick McHardy skb->protocol = htons(ETH_P_IP); 3561bd9bef6SPatrick McHardy 35729a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 35829a26a56SEric W. Biederman net, sk, skb, NULL, dev, 35948d5cad8SPatrick McHardy ip_finish_output, 36048d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 36384f9307cSEric Dumazet /* 36484f9307cSEric Dumazet * copy saddr and daddr, possibly using 64bit load/stores 36584f9307cSEric Dumazet * Equivalent to : 36684f9307cSEric Dumazet * iph->saddr = fl4->saddr; 36784f9307cSEric Dumazet * iph->daddr = fl4->daddr; 36884f9307cSEric Dumazet */ 36984f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4) 37084f9307cSEric Dumazet { 37184f9307cSEric Dumazet BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) != 37284f9307cSEric Dumazet offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr)); 37384f9307cSEric Dumazet memcpy(&iph->saddr, &fl4->saddr, 37484f9307cSEric Dumazet sizeof(fl4->saddr) + sizeof(fl4->daddr)); 37584f9307cSEric Dumazet } 37684f9307cSEric Dumazet 377b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */ 378b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 38177589ce0SEric W. Biederman struct net *net = sock_net(sk); 382f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 383b57ae01aSDavid S. Miller struct flowi4 *fl4; 3841da177e4SLinus Torvalds struct rtable *rt; 3851da177e4SLinus Torvalds struct iphdr *iph; 386ab6e3febSEric Dumazet int res; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* Skip all of this if the packet is already routed, 3891da177e4SLinus Torvalds * f.e. by something like SCTP. 3901da177e4SLinus Torvalds */ 391ab6e3febSEric Dumazet rcu_read_lock(); 392f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 393ea4fc0d6SDavid S. Miller fl4 = &fl->u.ip4; 394511c3f92SEric Dumazet rt = skb_rtable(skb); 39500db4124SIan Morris if (rt) 3961da177e4SLinus Torvalds goto packet_routed; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* Make sure we can route this packet. */ 3991da177e4SLinus Torvalds rt = (struct rtable *)__sk_dst_check(sk, 0); 40051456b29SIan Morris if (!rt) { 4013ca3c68eSAl Viro __be32 daddr; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* Use correct destination address if we have options. */ 404c720c7e8SEric Dumazet daddr = inet->inet_daddr; 405f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 406f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* If this fails, retransmit mechanism of transport layer will 4091da177e4SLinus Torvalds * keep trying until route appears or the connection times 4101da177e4SLinus Torvalds * itself out. 4111da177e4SLinus Torvalds */ 41277589ce0SEric W. Biederman rt = ip_route_output_ports(net, fl4, sk, 41378fbfd8aSDavid S. Miller daddr, inet->inet_saddr, 41478fbfd8aSDavid S. Miller inet->inet_dport, 41578fbfd8aSDavid S. Miller inet->inet_sport, 41678fbfd8aSDavid S. Miller sk->sk_protocol, 41778fbfd8aSDavid S. Miller RT_CONN_FLAGS(sk), 41878fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 419b23dd4feSDavid S. Miller if (IS_ERR(rt)) 4201da177e4SLinus Torvalds goto no_route; 421d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 4221da177e4SLinus Torvalds } 423d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds packet_routed: 426155e8336SJulian Anastasov if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway) 4271da177e4SLinus Torvalds goto no_route; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* OK, we know where to send it, allocate and build IP header. */ 430f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0)); 4318856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 432eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 433714e85beSAl Viro *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff)); 43460ff7467SWANG Cong if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df) 4351da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 4361da177e4SLinus Torvalds else 4371da177e4SLinus Torvalds iph->frag_off = 0; 438d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 4391da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 44084f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 44184f9307cSEric Dumazet 4421da177e4SLinus Torvalds /* Transport layer set skb->h.foo itself. */ 4431da177e4SLinus Torvalds 444f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.optlen) { 445f6d8bd05SEric Dumazet iph->ihl += inet_opt->opt.optlen >> 2; 446f6d8bd05SEric Dumazet ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 44977589ce0SEric W. Biederman ip_select_ident_segs(net, skb, sk, 450b6a7719aSHannes Frederic Sowa skb_shinfo(skb)->gso_segs ?: 1); 4511da177e4SLinus Torvalds 452b0270e91SEric Dumazet /* TODO : should we use skb->sk here instead of sk ? */ 4531da177e4SLinus Torvalds skb->priority = sk->sk_priority; 4544a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 4551da177e4SLinus Torvalds 45633224b16SEric W. Biederman res = ip_local_out(net, sk, skb); 457ab6e3febSEric Dumazet rcu_read_unlock(); 458ab6e3febSEric Dumazet return res; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds no_route: 461ab6e3febSEric Dumazet rcu_read_unlock(); 46277589ce0SEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 4631da177e4SLinus Torvalds kfree_skb(skb); 4641da177e4SLinus Torvalds return -EHOSTUNREACH; 4651da177e4SLinus Torvalds } 4664bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit); 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 4711da177e4SLinus Torvalds to->priority = from->priority; 4721da177e4SLinus Torvalds to->protocol = from->protocol; 473adf30907SEric Dumazet skb_dst_drop(to); 474fe76cda3SEric Dumazet skb_dst_copy(to, from); 4751da177e4SLinus Torvalds to->dev = from->dev; 47682e91ffeSThomas Graf to->mark = from->mark; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds /* Copy the flags to each fragment. */ 4791da177e4SLinus Torvalds IPCB(to)->flags = IPCB(from)->flags; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4821da177e4SLinus Torvalds to->tc_index = from->tc_index; 4831da177e4SLinus Torvalds #endif 484e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 485c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 486c98d80edSJulian Anastasov to->ipvs_property = from->ipvs_property; 487c98d80edSJulian Anastasov #endif 488984bc16cSJames Morris skb_copy_secmark(to, from); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 491694869b3SEric W. Biederman static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 492c5501eb3SFlorian Westphal unsigned int mtu, 493694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 49449d16b23SAndy Zhou { 49549d16b23SAndy Zhou struct iphdr *iph = ip_hdr(skb); 49649d16b23SAndy Zhou 497d6b915e2SFlorian Westphal if ((iph->frag_off & htons(IP_DF)) == 0) 498694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 499d6b915e2SFlorian Westphal 500d6b915e2SFlorian Westphal if (unlikely(!skb->ignore_df || 50149d16b23SAndy Zhou (IPCB(skb)->frag_max_size && 50249d16b23SAndy Zhou IPCB(skb)->frag_max_size > mtu))) { 5039479b0afSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 50449d16b23SAndy Zhou icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 50549d16b23SAndy Zhou htonl(mtu)); 50649d16b23SAndy Zhou kfree_skb(skb); 50749d16b23SAndy Zhou return -EMSGSIZE; 50849d16b23SAndy Zhou } 50949d16b23SAndy Zhou 510694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 51149d16b23SAndy Zhou } 51249d16b23SAndy Zhou 5131da177e4SLinus Torvalds /* 5141da177e4SLinus Torvalds * This IP datagram is too large to be sent in one piece. Break it up into 5151da177e4SLinus Torvalds * smaller pieces (each of size equal to IP header plus 5161da177e4SLinus Torvalds * a block of the data of the original IP data part) that will yet fit in a 5171da177e4SLinus Torvalds * single device frame, and queue such a frame for sending. 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds 520694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 521694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds struct iphdr *iph; 5241da177e4SLinus Torvalds int ptr; 5251da177e4SLinus Torvalds struct net_device *dev; 5261da177e4SLinus Torvalds struct sk_buff *skb2; 527c893b806SChangli Gao unsigned int mtu, hlen, left, len, ll_rs; 5281da177e4SLinus Torvalds int offset; 52976ab608dSAlexey Dobriyan __be16 not_last_frag; 530511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 5311da177e4SLinus Torvalds int err = 0; 5321da177e4SLinus Torvalds 533d8d1f30bSChangli Gao dev = rt->dst.dev; 5341da177e4SLinus Torvalds 535dbd3393cSHannes Frederic Sowa /* for offloaded checksums cleanup checksum before fragmentation */ 536dbd3393cSHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 537dbd3393cSHannes Frederic Sowa (err = skb_checksum_help(skb))) 538dbd3393cSHannes Frederic Sowa goto fail; 539dbd3393cSHannes Frederic Sowa 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * Point into the IP datagram header. 5421da177e4SLinus Torvalds */ 5431da177e4SLinus Torvalds 544eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 5451da177e4SLinus Torvalds 546fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 547d6b915e2SFlorian Westphal if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu) 548d6b915e2SFlorian Westphal mtu = IPCB(skb)->frag_max_size; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /* 5511da177e4SLinus Torvalds * Setup starting values. 5521da177e4SLinus Torvalds */ 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds hlen = iph->ihl * 4; 555f87c10a8SHannes Frederic Sowa mtu = mtu - hlen; /* Size of data space */ 55689cee8b1SHerbert Xu IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* When frag_list is given, use it. First, check its validity: 5591da177e4SLinus Torvalds * some transformers could create wrong frag_list or break existing 5601da177e4SLinus Torvalds * one, it is not prohibited. In this case fall back to copying. 5611da177e4SLinus Torvalds * 5621da177e4SLinus Torvalds * LATER: this step can be merged to real generation of fragments, 5631da177e4SLinus Torvalds * we can switch to copy when see the first bad fragment. 5641da177e4SLinus Torvalds */ 56521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 5663d13008eSEric Dumazet struct sk_buff *frag, *frag2; 5671da177e4SLinus Torvalds int first_len = skb_pagelen(skb); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (first_len - hlen > mtu || 5701da177e4SLinus Torvalds ((first_len - hlen) & 7) || 57156f8a75cSPaul Gortmaker ip_is_fragment(iph) || 5721da177e4SLinus Torvalds skb_cloned(skb)) 5731da177e4SLinus Torvalds goto slow_path; 5741da177e4SLinus Torvalds 575d7fcf1a5SDavid S. Miller skb_walk_frags(skb, frag) { 5761da177e4SLinus Torvalds /* Correct geometry. */ 5771da177e4SLinus Torvalds if (frag->len > mtu || 5781da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 5791da177e4SLinus Torvalds skb_headroom(frag) < hlen) 5803d13008eSEric Dumazet goto slow_path_clean; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* Partially cloned skb? */ 5831da177e4SLinus Torvalds if (skb_shared(frag)) 5843d13008eSEric Dumazet goto slow_path_clean; 5852fdba6b0SHerbert Xu 5862fdba6b0SHerbert Xu BUG_ON(frag->sk); 5872fdba6b0SHerbert Xu if (skb->sk) { 5882fdba6b0SHerbert Xu frag->sk = skb->sk; 5892fdba6b0SHerbert Xu frag->destructor = sock_wfree; 5902fdba6b0SHerbert Xu } 5913d13008eSEric Dumazet skb->truesize -= frag->truesize; 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds /* Everything is OK. Generate! */ 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds err = 0; 5971da177e4SLinus Torvalds offset = 0; 5981da177e4SLinus Torvalds frag = skb_shinfo(skb)->frag_list; 599d7fcf1a5SDavid S. Miller skb_frag_list_init(skb); 6001da177e4SLinus Torvalds skb->data_len = first_len - skb_headlen(skb); 6011da177e4SLinus Torvalds skb->len = first_len; 6021da177e4SLinus Torvalds iph->tot_len = htons(first_len); 6031da177e4SLinus Torvalds iph->frag_off = htons(IP_MF); 6041da177e4SLinus Torvalds ip_send_check(iph); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds for (;;) { 6071da177e4SLinus Torvalds /* Prepare header of the next frame, 6081da177e4SLinus Torvalds * before previous one went down. */ 6091da177e4SLinus Torvalds if (frag) { 6101da177e4SLinus Torvalds frag->ip_summed = CHECKSUM_NONE; 611badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(frag); 612e2d1bca7SArnaldo Carvalho de Melo __skb_push(frag, hlen); 613e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(frag); 614d56f90a7SArnaldo Carvalho de Melo memcpy(skb_network_header(frag), iph, hlen); 615eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(frag); 6161da177e4SLinus Torvalds iph->tot_len = htons(frag->len); 6171da177e4SLinus Torvalds ip_copy_metadata(frag, skb); 6181da177e4SLinus Torvalds if (offset == 0) 6191da177e4SLinus Torvalds ip_options_fragment(frag); 6201da177e4SLinus Torvalds offset += skb->len - hlen; 6211da177e4SLinus Torvalds iph->frag_off = htons(offset>>3); 62200db4124SIan Morris if (frag->next) 6231da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 6241da177e4SLinus Torvalds /* Ready, complete checksum */ 6251da177e4SLinus Torvalds ip_send_check(iph); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 628694869b3SEric W. Biederman err = output(net, sk, skb); 6291da177e4SLinus Torvalds 630dafee490SWei Dong if (!err) 63126a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 6321da177e4SLinus Torvalds if (err || !frag) 6331da177e4SLinus Torvalds break; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds skb = frag; 6361da177e4SLinus Torvalds frag = skb->next; 6371da177e4SLinus Torvalds skb->next = NULL; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds if (err == 0) { 64126a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 6421da177e4SLinus Torvalds return 0; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds while (frag) { 6461da177e4SLinus Torvalds skb = frag->next; 6471da177e4SLinus Torvalds kfree_skb(frag); 6481da177e4SLinus Torvalds frag = skb; 6491da177e4SLinus Torvalds } 65026a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 6511da177e4SLinus Torvalds return err; 6523d13008eSEric Dumazet 6533d13008eSEric Dumazet slow_path_clean: 6543d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 6553d13008eSEric Dumazet if (frag2 == frag) 6563d13008eSEric Dumazet break; 6573d13008eSEric Dumazet frag2->sk = NULL; 6583d13008eSEric Dumazet frag2->destructor = NULL; 6593d13008eSEric Dumazet skb->truesize += frag2->truesize; 6603d13008eSEric Dumazet } 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds slow_path: 664c9af6db4SPravin B Shelar iph = ip_hdr(skb); 665fc70fb64SAlexander Duyck 6661da177e4SLinus Torvalds left = skb->len - hlen; /* Space per frame */ 66749085bd7SGeorge Kadianakis ptr = hlen; /* Where to start from */ 6681da177e4SLinus Torvalds 6698d045163SFlorian Westphal ll_rs = LL_RESERVED_SPACE(rt->dst.dev); 6709bcfcaf5SStephen Hemminger 6711da177e4SLinus Torvalds /* 6721da177e4SLinus Torvalds * Fragment the datagram. 6731da177e4SLinus Torvalds */ 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; 6761da177e4SLinus Torvalds not_last_frag = iph->frag_off & htons(IP_MF); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* 6791da177e4SLinus Torvalds * Keep copying data until we run out. 6801da177e4SLinus Torvalds */ 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds while (left > 0) { 6831da177e4SLinus Torvalds len = left; 6841da177e4SLinus Torvalds /* IF: it doesn't fit, use 'mtu' - the data space left */ 6851da177e4SLinus Torvalds if (len > mtu) 6861da177e4SLinus Torvalds len = mtu; 6871da177e4SLinus Torvalds /* IF: we are not sending up to and including the packet end 6881da177e4SLinus Torvalds then align the next start on an eight byte boundary */ 6891da177e4SLinus Torvalds if (len < left) { 6901da177e4SLinus Torvalds len &= ~7; 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds 693cbffccc9SJoe Perches /* Allocate buffer */ 694cbffccc9SJoe Perches skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC); 695cbffccc9SJoe Perches if (!skb2) { 6961da177e4SLinus Torvalds err = -ENOMEM; 6971da177e4SLinus Torvalds goto fail; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* 7011da177e4SLinus Torvalds * Set up data on packet 7021da177e4SLinus Torvalds */ 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds ip_copy_metadata(skb2, skb); 7051da177e4SLinus Torvalds skb_reserve(skb2, ll_rs); 7061da177e4SLinus Torvalds skb_put(skb2, len + hlen); 707c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 708b0e380b1SArnaldo Carvalho de Melo skb2->transport_header = skb2->network_header + hlen; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* 7111da177e4SLinus Torvalds * Charge the memory for the fragment to any owner 7121da177e4SLinus Torvalds * it might possess 7131da177e4SLinus Torvalds */ 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds if (skb->sk) 7161da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * Copy the packet header into the new buffer. 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds 722d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds /* 7251da177e4SLinus Torvalds * Copy a block of the IP datagram. 7261da177e4SLinus Torvalds */ 727bff9b61cSArnaldo Carvalho de Melo if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len)) 7281da177e4SLinus Torvalds BUG(); 7291da177e4SLinus Torvalds left -= len; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* 7321da177e4SLinus Torvalds * Fill in the new header fields. 7331da177e4SLinus Torvalds */ 734eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 7351da177e4SLinus Torvalds iph->frag_off = htons((offset >> 3)); 7361da177e4SLinus Torvalds 737d6b915e2SFlorian Westphal if (IPCB(skb)->flags & IPSKB_FRAG_PMTU) 738d6b915e2SFlorian Westphal iph->frag_off |= htons(IP_DF); 739d6b915e2SFlorian Westphal 7401da177e4SLinus Torvalds /* ANK: dirty, but effective trick. Upgrade options only if 7411da177e4SLinus Torvalds * the segment to be fragmented was THE FIRST (otherwise, 7421da177e4SLinus Torvalds * options are already fixed) and make it ONCE 7431da177e4SLinus Torvalds * on the initial skb, so that all the following fragments 7441da177e4SLinus Torvalds * will inherit fixed options. 7451da177e4SLinus Torvalds */ 7461da177e4SLinus Torvalds if (offset == 0) 7471da177e4SLinus Torvalds ip_options_fragment(skb); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * Added AC : If we are fragmenting a fragment that's not the 7511da177e4SLinus Torvalds * last fragment then keep MF on each bit 7521da177e4SLinus Torvalds */ 7531da177e4SLinus Torvalds if (left > 0 || not_last_frag) 7541da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 7551da177e4SLinus Torvalds ptr += len; 7561da177e4SLinus Torvalds offset += len; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* 7591da177e4SLinus Torvalds * Put this fragment into the sending queue. 7601da177e4SLinus Torvalds */ 7611da177e4SLinus Torvalds iph->tot_len = htons(len + hlen); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds ip_send_check(iph); 7641da177e4SLinus Torvalds 765694869b3SEric W. Biederman err = output(net, sk, skb2); 7661da177e4SLinus Torvalds if (err) 7671da177e4SLinus Torvalds goto fail; 768dafee490SWei Dong 76926a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 7701da177e4SLinus Torvalds } 7715d0ba55bSEric Dumazet consume_skb(skb); 77226a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 7731da177e4SLinus Torvalds return err; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds fail: 7761da177e4SLinus Torvalds kfree_skb(skb); 77726a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 7781da177e4SLinus Torvalds return err; 7791da177e4SLinus Torvalds } 78049d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment); 7812e2f7aefSPatrick McHardy 7821da177e4SLinus Torvalds int 7831da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 7841da177e4SLinus Torvalds { 785f69e6d13SAl Viro struct msghdr *msg = from; 7861da177e4SLinus Torvalds 78784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 78821226abbSAl Viro if (copy_from_iter(to, len, &msg->msg_iter) != len) 7891da177e4SLinus Torvalds return -EFAULT; 7901da177e4SLinus Torvalds } else { 79144bb9363SAl Viro __wsum csum = 0; 79221226abbSAl Viro if (csum_and_copy_from_iter(to, len, &csum, &msg->msg_iter) != len) 7931da177e4SLinus Torvalds return -EFAULT; 7941da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds return 0; 7971da177e4SLinus Torvalds } 7984bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag); 7991da177e4SLinus Torvalds 80044bb9363SAl Viro static inline __wsum 8011da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy) 8021da177e4SLinus Torvalds { 8031da177e4SLinus Torvalds char *kaddr; 80444bb9363SAl Viro __wsum csum; 8051da177e4SLinus Torvalds kaddr = kmap(page); 8061da177e4SLinus Torvalds csum = csum_partial(kaddr + offset, copy, 0); 8071da177e4SLinus Torvalds kunmap(page); 8081da177e4SLinus Torvalds return csum; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8114b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk, 8121470ddf7SHerbert Xu struct sk_buff_head *queue, 813e89e9cf5SAnanda Raju int getfrag(void *from, char *to, int offset, int len, 814e89e9cf5SAnanda Raju int odd, struct sk_buff *skb), 815e89e9cf5SAnanda Raju void *from, int length, int hh_len, int fragheaderlen, 816d9be4f7aSBill Sommerfeld int transhdrlen, int maxfraglen, unsigned int flags) 817e89e9cf5SAnanda Raju { 818e89e9cf5SAnanda Raju struct sk_buff *skb; 819e89e9cf5SAnanda Raju int err; 820e89e9cf5SAnanda Raju 821e89e9cf5SAnanda Raju /* There is support for UDP fragmentation offload by network 822e89e9cf5SAnanda Raju * device, so create one single skb packet containing complete 823e89e9cf5SAnanda Raju * udp datagram 824e89e9cf5SAnanda Raju */ 82551456b29SIan Morris skb = skb_peek_tail(queue); 82651456b29SIan Morris if (!skb) { 827e89e9cf5SAnanda Raju skb = sock_alloc_send_skb(sk, 828e89e9cf5SAnanda Raju hh_len + fragheaderlen + transhdrlen + 20, 829e89e9cf5SAnanda Raju (flags & MSG_DONTWAIT), &err); 830e89e9cf5SAnanda Raju 83151456b29SIan Morris if (!skb) 832e89e9cf5SAnanda Raju return err; 833e89e9cf5SAnanda Raju 834e89e9cf5SAnanda Raju /* reserve space for Hardware header */ 835e89e9cf5SAnanda Raju skb_reserve(skb, hh_len); 836e89e9cf5SAnanda Raju 837e89e9cf5SAnanda Raju /* create space for UDP/IP header */ 838e89e9cf5SAnanda Raju skb_put(skb, fragheaderlen + transhdrlen); 839e89e9cf5SAnanda Raju 840e89e9cf5SAnanda Raju /* initialize network header pointer */ 841c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 842e89e9cf5SAnanda Raju 843e89e9cf5SAnanda Raju /* initialize protocol header pointer */ 844b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + fragheaderlen; 845e89e9cf5SAnanda Raju 846e89e9cf5SAnanda Raju skb->csum = 0; 847e89e9cf5SAnanda Raju 848e93b7d74SJiri Pirko __skb_queue_tail(queue, skb); 849e93b7d74SJiri Pirko } else if (skb_is_gso(skb)) { 850e93b7d74SJiri Pirko goto append; 851e93b7d74SJiri Pirko } 852e93b7d74SJiri Pirko 853e93b7d74SJiri Pirko skb->ip_summed = CHECKSUM_PARTIAL; 854e89e9cf5SAnanda Raju /* specify the length of each IP datagram fragment */ 855d9be4f7aSBill Sommerfeld skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen; 856f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 857be9164e7SKostya B 858e93b7d74SJiri Pirko append: 859be9164e7SKostya B return skb_append_datato_frags(sk, skb, getfrag, from, 860be9164e7SKostya B (length - transhdrlen)); 861e89e9cf5SAnanda Raju } 862e89e9cf5SAnanda Raju 863f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk, 864f5fca608SDavid S. Miller struct flowi4 *fl4, 865f5fca608SDavid S. Miller struct sk_buff_head *queue, 8661470ddf7SHerbert Xu struct inet_cork *cork, 8675640f768SEric Dumazet struct page_frag *pfrag, 8681470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, 8691470ddf7SHerbert Xu int len, int odd, struct sk_buff *skb), 8701da177e4SLinus Torvalds void *from, int length, int transhdrlen, 8711da177e4SLinus Torvalds unsigned int flags) 8721da177e4SLinus Torvalds { 8731da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 8741da177e4SLinus Torvalds struct sk_buff *skb; 8751da177e4SLinus Torvalds 87607df5294SHerbert Xu struct ip_options *opt = cork->opt; 8771da177e4SLinus Torvalds int hh_len; 8781da177e4SLinus Torvalds int exthdrlen; 8791da177e4SLinus Torvalds int mtu; 8801da177e4SLinus Torvalds int copy; 8811da177e4SLinus Torvalds int err; 8821da177e4SLinus Torvalds int offset = 0; 883daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, maxnonfragsize; 8841da177e4SLinus Torvalds int csummode = CHECKSUM_NONE; 8851470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 88609c2d251SWillem de Bruijn u32 tskey = 0; 8871da177e4SLinus Torvalds 88896d7303eSSteffen Klassert skb = skb_peek_tail(queue); 88996d7303eSSteffen Klassert 89096d7303eSSteffen Klassert exthdrlen = !skb ? rt->dst.header_len : 0; 89107df5294SHerbert Xu mtu = cork->fragsize; 89209c2d251SWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 89309c2d251SWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 89409c2d251SWillem de Bruijn tskey = sk->sk_tskey++; 8951470ddf7SHerbert Xu 896d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 8991da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 90060ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 9011da177e4SLinus Torvalds 902daba287bSHannes Frederic Sowa if (cork->length + length > maxnonfragsize - fragheaderlen) { 903f5fca608SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 90461e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 9051da177e4SLinus Torvalds return -EMSGSIZE; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds /* 9091da177e4SLinus Torvalds * transhdrlen > 0 means that this is the first fragment and we wish 9101da177e4SLinus Torvalds * it won't be fragmented in the future. 9111da177e4SLinus Torvalds */ 9121da177e4SLinus Torvalds if (transhdrlen && 9131da177e4SLinus Torvalds length + fragheaderlen <= mtu && 914c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) && 915d749c9cbSHannes Frederic Sowa !(flags & MSG_MORE) && 9161da177e4SLinus Torvalds !exthdrlen) 91784fa7933SPatrick McHardy csummode = CHECKSUM_PARTIAL; 9181da177e4SLinus Torvalds 9191470ddf7SHerbert Xu cork->length += length; 92026cde9f7SHerbert Xu if (((length > mtu) || (skb && skb_is_gso(skb))) && 921be9164e7SKostya B (sk->sk_protocol == IPPROTO_UDP) && 922acf8dd0aSMichal Kubeček (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len && 92340ba3302SMichal Kubeček (sk->sk_type == SOCK_DGRAM) && !sk->sk_no_check_tx) { 9241470ddf7SHerbert Xu err = ip_ufo_append_data(sk, queue, getfrag, from, length, 9251470ddf7SHerbert Xu hh_len, fragheaderlen, transhdrlen, 926d9be4f7aSBill Sommerfeld maxfraglen, flags); 927baa829d8SPatrick McHardy if (err) 928e89e9cf5SAnanda Raju goto error; 929e89e9cf5SAnanda Raju return 0; 930e89e9cf5SAnanda Raju } 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds /* So, what's going on in the loop below? 9331da177e4SLinus Torvalds * 9341da177e4SLinus Torvalds * We use calculated fragment length to generate chained skb, 9351da177e4SLinus Torvalds * each of segments is IP fragment ready for sending to network after 9361da177e4SLinus Torvalds * adding appropriate IP header. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds 93926cde9f7SHerbert Xu if (!skb) 9401da177e4SLinus Torvalds goto alloc_new_skb; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds while (length > 0) { 9431da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 9441da177e4SLinus Torvalds copy = mtu - skb->len; 9451da177e4SLinus Torvalds if (copy < length) 9461da177e4SLinus Torvalds copy = maxfraglen - skb->len; 9471da177e4SLinus Torvalds if (copy <= 0) { 9481da177e4SLinus Torvalds char *data; 9491da177e4SLinus Torvalds unsigned int datalen; 9501da177e4SLinus Torvalds unsigned int fraglen; 9511da177e4SLinus Torvalds unsigned int fraggap; 9521da177e4SLinus Torvalds unsigned int alloclen; 9531da177e4SLinus Torvalds struct sk_buff *skb_prev; 9541da177e4SLinus Torvalds alloc_new_skb: 9551da177e4SLinus Torvalds skb_prev = skb; 9561da177e4SLinus Torvalds if (skb_prev) 9571da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 9581da177e4SLinus Torvalds else 9591da177e4SLinus Torvalds fraggap = 0; 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * If remaining data exceeds the mtu, 9631da177e4SLinus Torvalds * we know we need more fragment(s). 9641da177e4SLinus Torvalds */ 9651da177e4SLinus Torvalds datalen = length + fraggap; 9661da177e4SLinus Torvalds if (datalen > mtu - fragheaderlen) 9671da177e4SLinus Torvalds datalen = maxfraglen - fragheaderlen; 9681da177e4SLinus Torvalds fraglen = datalen + fragheaderlen; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if ((flags & MSG_MORE) && 971d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 9721da177e4SLinus Torvalds alloclen = mtu; 9731da177e4SLinus Torvalds else 97459104f06SEric Dumazet alloclen = fraglen; 9751da177e4SLinus Torvalds 976353e5c9aSSteffen Klassert alloclen += exthdrlen; 977353e5c9aSSteffen Klassert 9781da177e4SLinus Torvalds /* The last fragment gets additional space at tail. 9791da177e4SLinus Torvalds * Note, with MSG_MORE we overallocate on fragments, 9801da177e4SLinus Torvalds * because we have no idea what fragment will be 9811da177e4SLinus Torvalds * the last. 9821da177e4SLinus Torvalds */ 98333f99dc7SSteffen Klassert if (datalen == length + fraggap) 984d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 98533f99dc7SSteffen Klassert 9861da177e4SLinus Torvalds if (transhdrlen) { 9871da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 9881da177e4SLinus Torvalds alloclen + hh_len + 15, 9891da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 9901da177e4SLinus Torvalds } else { 9911da177e4SLinus Torvalds skb = NULL; 9921da177e4SLinus Torvalds if (atomic_read(&sk->sk_wmem_alloc) <= 9931da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 9941da177e4SLinus Torvalds skb = sock_wmalloc(sk, 9951da177e4SLinus Torvalds alloclen + hh_len + 15, 1, 9961da177e4SLinus Torvalds sk->sk_allocation); 99751456b29SIan Morris if (unlikely(!skb)) 9981da177e4SLinus Torvalds err = -ENOBUFS; 9991da177e4SLinus Torvalds } 100051456b29SIan Morris if (!skb) 10011da177e4SLinus Torvalds goto error; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* 10041da177e4SLinus Torvalds * Fill in the control structures 10051da177e4SLinus Torvalds */ 10061da177e4SLinus Torvalds skb->ip_summed = csummode; 10071da177e4SLinus Torvalds skb->csum = 0; 10081da177e4SLinus Torvalds skb_reserve(skb, hh_len); 100911878b40SWillem de Bruijn 101011878b40SWillem de Bruijn /* only the initial fragment is time stamped */ 10111470ddf7SHerbert Xu skb_shinfo(skb)->tx_flags = cork->tx_flags; 101211878b40SWillem de Bruijn cork->tx_flags = 0; 101309c2d251SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 101409c2d251SWillem de Bruijn tskey = 0; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* 10171da177e4SLinus Torvalds * Find where to start putting bytes. 10181da177e4SLinus Torvalds */ 1019353e5c9aSSteffen Klassert data = skb_put(skb, fraglen + exthdrlen); 1020c14d2450SArnaldo Carvalho de Melo skb_set_network_header(skb, exthdrlen); 1021b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1022b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 1023353e5c9aSSteffen Klassert data += fragheaderlen + exthdrlen; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (fraggap) { 10261da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 10271da177e4SLinus Torvalds skb_prev, maxfraglen, 10281da177e4SLinus Torvalds data + transhdrlen, fraggap, 0); 10291da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 10301da177e4SLinus Torvalds skb->csum); 10311da177e4SLinus Torvalds data += fraggap; 1032e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds copy = datalen - transhdrlen - fraggap; 10361da177e4SLinus Torvalds if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { 10371da177e4SLinus Torvalds err = -EFAULT; 10381da177e4SLinus Torvalds kfree_skb(skb); 10391da177e4SLinus Torvalds goto error; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds offset += copy; 10431da177e4SLinus Torvalds length -= datalen - fraggap; 10441da177e4SLinus Torvalds transhdrlen = 0; 10451da177e4SLinus Torvalds exthdrlen = 0; 10461da177e4SLinus Torvalds csummode = CHECKSUM_NONE; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds /* 10491da177e4SLinus Torvalds * Put the packet on the pending queue. 10501da177e4SLinus Torvalds */ 10511470ddf7SHerbert Xu __skb_queue_tail(queue, skb); 10521da177e4SLinus Torvalds continue; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (copy > length) 10561da177e4SLinus Torvalds copy = length; 10571da177e4SLinus Torvalds 1058d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) { 10591da177e4SLinus Torvalds unsigned int off; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds off = skb->len; 10621da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 10631da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 10641da177e4SLinus Torvalds __skb_trim(skb, off); 10651da177e4SLinus Torvalds err = -EFAULT; 10661da177e4SLinus Torvalds goto error; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds } else { 10691da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds err = -ENOMEM; 10725640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 10731da177e4SLinus Torvalds goto error; 10741da177e4SLinus Torvalds 10755640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 10765640f768SEric Dumazet pfrag->offset)) { 10771da177e4SLinus Torvalds err = -EMSGSIZE; 10785640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 10791da177e4SLinus Torvalds goto error; 10805640f768SEric Dumazet 10815640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 10825640f768SEric Dumazet pfrag->offset, 0); 10835640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 10845640f768SEric Dumazet get_page(pfrag->page); 10851da177e4SLinus Torvalds } 10865640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 10875640f768SEric Dumazet if (getfrag(from, 10885640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 10895640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 10905640f768SEric Dumazet goto error_efault; 10915640f768SEric Dumazet 10925640f768SEric Dumazet pfrag->offset += copy; 10935640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 10941da177e4SLinus Torvalds skb->len += copy; 10951da177e4SLinus Torvalds skb->data_len += copy; 1096f945fa7aSHerbert Xu skb->truesize += copy; 1097f945fa7aSHerbert Xu atomic_add(copy, &sk->sk_wmem_alloc); 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds offset += copy; 11001da177e4SLinus Torvalds length -= copy; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds return 0; 11041da177e4SLinus Torvalds 11055640f768SEric Dumazet error_efault: 11065640f768SEric Dumazet err = -EFAULT; 11071da177e4SLinus Torvalds error: 11081470ddf7SHerbert Xu cork->length -= length; 11095e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 11101da177e4SLinus Torvalds return err; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, 11141470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp) 11151470ddf7SHerbert Xu { 1116f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 11171470ddf7SHerbert Xu struct rtable *rt; 11181470ddf7SHerbert Xu 11191470ddf7SHerbert Xu /* 11201470ddf7SHerbert Xu * setup for corking. 11211470ddf7SHerbert Xu */ 11221470ddf7SHerbert Xu opt = ipc->opt; 11231470ddf7SHerbert Xu if (opt) { 112451456b29SIan Morris if (!cork->opt) { 11251470ddf7SHerbert Xu cork->opt = kmalloc(sizeof(struct ip_options) + 40, 11261470ddf7SHerbert Xu sk->sk_allocation); 112751456b29SIan Morris if (unlikely(!cork->opt)) 11281470ddf7SHerbert Xu return -ENOBUFS; 11291470ddf7SHerbert Xu } 1130f6d8bd05SEric Dumazet memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); 11311470ddf7SHerbert Xu cork->flags |= IPCORK_OPT; 11321470ddf7SHerbert Xu cork->addr = ipc->addr; 11331470ddf7SHerbert Xu } 11341470ddf7SHerbert Xu rt = *rtp; 11351470ddf7SHerbert Xu if (unlikely(!rt)) 11361470ddf7SHerbert Xu return -EFAULT; 11371470ddf7SHerbert Xu /* 11381470ddf7SHerbert Xu * We steal reference to this route, caller should not release it 11391470ddf7SHerbert Xu */ 11401470ddf7SHerbert Xu *rtp = NULL; 1141482fc609SHannes Frederic Sowa cork->fragsize = ip_sk_use_pmtu(sk) ? 1142482fc609SHannes Frederic Sowa dst_mtu(&rt->dst) : rt->dst.dev->mtu; 11431470ddf7SHerbert Xu cork->dst = &rt->dst; 11441470ddf7SHerbert Xu cork->length = 0; 1145aa661581SFrancesco Fusco cork->ttl = ipc->ttl; 1146aa661581SFrancesco Fusco cork->tos = ipc->tos; 1147aa661581SFrancesco Fusco cork->priority = ipc->priority; 11481470ddf7SHerbert Xu cork->tx_flags = ipc->tx_flags; 11491470ddf7SHerbert Xu 11501470ddf7SHerbert Xu return 0; 11511470ddf7SHerbert Xu } 11521470ddf7SHerbert Xu 11531470ddf7SHerbert Xu /* 11541470ddf7SHerbert Xu * ip_append_data() and ip_append_page() can make one large IP datagram 11551470ddf7SHerbert Xu * from many pieces of data. Each pieces will be holded on the socket 11561470ddf7SHerbert Xu * until ip_push_pending_frames() is called. Each piece can be a page 11571470ddf7SHerbert Xu * or non-page data. 11581470ddf7SHerbert Xu * 11591470ddf7SHerbert Xu * Not only UDP, other transport protocols - e.g. raw sockets - can use 11601470ddf7SHerbert Xu * this interface potentially. 11611470ddf7SHerbert Xu * 11621470ddf7SHerbert Xu * LATER: length must be adjusted by pad at tail, when it is required. 11631470ddf7SHerbert Xu */ 1164f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4, 11651470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, int len, 11661470ddf7SHerbert Xu int odd, struct sk_buff *skb), 11671470ddf7SHerbert Xu void *from, int length, int transhdrlen, 11681470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 11691470ddf7SHerbert Xu unsigned int flags) 11701470ddf7SHerbert Xu { 11711470ddf7SHerbert Xu struct inet_sock *inet = inet_sk(sk); 11721470ddf7SHerbert Xu int err; 11731470ddf7SHerbert Xu 11741470ddf7SHerbert Xu if (flags&MSG_PROBE) 11751470ddf7SHerbert Xu return 0; 11761470ddf7SHerbert Xu 11771470ddf7SHerbert Xu if (skb_queue_empty(&sk->sk_write_queue)) { 1178bdc712b4SDavid S. Miller err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp); 11791470ddf7SHerbert Xu if (err) 11801470ddf7SHerbert Xu return err; 11811470ddf7SHerbert Xu } else { 11821470ddf7SHerbert Xu transhdrlen = 0; 11831470ddf7SHerbert Xu } 11841470ddf7SHerbert Xu 11855640f768SEric Dumazet return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base, 11865640f768SEric Dumazet sk_page_frag(sk), getfrag, 11871470ddf7SHerbert Xu from, length, transhdrlen, flags); 11881470ddf7SHerbert Xu } 11891470ddf7SHerbert Xu 1190f5fca608SDavid S. Miller ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 11911da177e4SLinus Torvalds int offset, size_t size, int flags) 11921da177e4SLinus Torvalds { 11931da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 11941da177e4SLinus Torvalds struct sk_buff *skb; 11951da177e4SLinus Torvalds struct rtable *rt; 11961da177e4SLinus Torvalds struct ip_options *opt = NULL; 1197bdc712b4SDavid S. Miller struct inet_cork *cork; 11981da177e4SLinus Torvalds int hh_len; 11991da177e4SLinus Torvalds int mtu; 12001da177e4SLinus Torvalds int len; 12011da177e4SLinus Torvalds int err; 1202daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds if (inet->hdrincl) 12051da177e4SLinus Torvalds return -EPERM; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds if (flags&MSG_PROBE) 12081da177e4SLinus Torvalds return 0; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) 12111da177e4SLinus Torvalds return -EINVAL; 12121da177e4SLinus Torvalds 1213bdc712b4SDavid S. Miller cork = &inet->cork.base; 1214bdc712b4SDavid S. Miller rt = (struct rtable *)cork->dst; 1215bdc712b4SDavid S. Miller if (cork->flags & IPCORK_OPT) 1216bdc712b4SDavid S. Miller opt = cork->opt; 12171da177e4SLinus Torvalds 1218d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) 12191da177e4SLinus Torvalds return -EOPNOTSUPP; 12201da177e4SLinus Torvalds 1221d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 1222bdc712b4SDavid S. Miller mtu = cork->fragsize; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 12251da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 122660ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 12271da177e4SLinus Torvalds 1228daba287bSHannes Frederic Sowa if (cork->length + size > maxnonfragsize - fragheaderlen) { 122961e7f09dSHannes Frederic Sowa ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 123061e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 12311da177e4SLinus Torvalds return -EMSGSIZE; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds 123451456b29SIan Morris skb = skb_peek_tail(&sk->sk_write_queue); 123551456b29SIan Morris if (!skb) 12361da177e4SLinus Torvalds return -EINVAL; 12371da177e4SLinus Torvalds 123826cde9f7SHerbert Xu if ((size + skb->len > mtu) && 123926cde9f7SHerbert Xu (sk->sk_protocol == IPPROTO_UDP) && 1240d8d1f30bSChangli Gao (rt->dst.dev->features & NETIF_F_UFO)) { 1241a8c4a252SHannes Frederic Sowa if (skb->ip_summed != CHECKSUM_PARTIAL) 1242a8c4a252SHannes Frederic Sowa return -EOPNOTSUPP; 1243a8c4a252SHannes Frederic Sowa 12447967168cSHerbert Xu skb_shinfo(skb)->gso_size = mtu - fragheaderlen; 1245f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 12467967168cSHerbert Xu } 1247a8c4a252SHannes Frederic Sowa cork->length += size; 1248e89e9cf5SAnanda Raju 12491da177e4SLinus Torvalds while (size > 0) { 1250be12a1feSHannes Frederic Sowa if (skb_is_gso(skb)) { 1251e89e9cf5SAnanda Raju len = size; 1252be12a1feSHannes Frederic Sowa } else { 1253e89e9cf5SAnanda Raju 12541da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 12551da177e4SLinus Torvalds len = mtu - skb->len; 12561da177e4SLinus Torvalds if (len < size) 12571da177e4SLinus Torvalds len = maxfraglen - skb->len; 1258e89e9cf5SAnanda Raju } 12591da177e4SLinus Torvalds if (len <= 0) { 12601da177e4SLinus Torvalds struct sk_buff *skb_prev; 12611da177e4SLinus Torvalds int alloclen; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds skb_prev = skb; 12641da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds alloclen = fragheaderlen + hh_len + fraggap + 15; 12671da177e4SLinus Torvalds skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); 12681da177e4SLinus Torvalds if (unlikely(!skb)) { 12691da177e4SLinus Torvalds err = -ENOBUFS; 12701da177e4SLinus Torvalds goto error; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds /* 12741da177e4SLinus Torvalds * Fill in the control structures 12751da177e4SLinus Torvalds */ 12761da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 12771da177e4SLinus Torvalds skb->csum = 0; 12781da177e4SLinus Torvalds skb_reserve(skb, hh_len); 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds /* 12811da177e4SLinus Torvalds * Find where to start putting bytes. 12821da177e4SLinus Torvalds */ 1283967b05f6SArnaldo Carvalho de Melo skb_put(skb, fragheaderlen + fraggap); 12842ca9e6f2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1285b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1286b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 12871da177e4SLinus Torvalds if (fraggap) { 1288967b05f6SArnaldo Carvalho de Melo skb->csum = skb_copy_and_csum_bits(skb_prev, 1289967b05f6SArnaldo Carvalho de Melo maxfraglen, 12909c70220bSArnaldo Carvalho de Melo skb_transport_header(skb), 1291967b05f6SArnaldo Carvalho de Melo fraggap, 0); 12921da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 12931da177e4SLinus Torvalds skb->csum); 1294e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds /* 12981da177e4SLinus Torvalds * Put the packet on the pending queue. 12991da177e4SLinus Torvalds */ 13001da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 13011da177e4SLinus Torvalds continue; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (len > size) 13051da177e4SLinus Torvalds len = size; 1306be12a1feSHannes Frederic Sowa 1307be12a1feSHannes Frederic Sowa if (skb_append_pagefrags(skb, page, offset, len)) { 13081da177e4SLinus Torvalds err = -EMSGSIZE; 13091da177e4SLinus Torvalds goto error; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds if (skb->ip_summed == CHECKSUM_NONE) { 131344bb9363SAl Viro __wsum csum; 13141da177e4SLinus Torvalds csum = csum_page(page, offset, len); 13151da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, skb->len); 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds skb->len += len; 13191da177e4SLinus Torvalds skb->data_len += len; 13201e34a11dSDavid S. Miller skb->truesize += len; 13211e34a11dSDavid S. Miller atomic_add(len, &sk->sk_wmem_alloc); 13221da177e4SLinus Torvalds offset += len; 13231da177e4SLinus Torvalds size -= len; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds return 0; 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds error: 1328bdc712b4SDavid S. Miller cork->length -= size; 13295e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 13301da177e4SLinus Torvalds return err; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 13331470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork) 1334429f08e9SPavel Emelyanov { 13351470ddf7SHerbert Xu cork->flags &= ~IPCORK_OPT; 13361470ddf7SHerbert Xu kfree(cork->opt); 13371470ddf7SHerbert Xu cork->opt = NULL; 13381470ddf7SHerbert Xu dst_release(cork->dst); 13391470ddf7SHerbert Xu cork->dst = NULL; 1340429f08e9SPavel Emelyanov } 1341429f08e9SPavel Emelyanov 13421da177e4SLinus Torvalds /* 13431da177e4SLinus Torvalds * Combined all pending IP fragments on the socket as one IP datagram 13441da177e4SLinus Torvalds * and push them out. 13451da177e4SLinus Torvalds */ 13461c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk, 134777968b78SDavid S. Miller struct flowi4 *fl4, 13481470ddf7SHerbert Xu struct sk_buff_head *queue, 13491470ddf7SHerbert Xu struct inet_cork *cork) 13501da177e4SLinus Torvalds { 13511da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 13521da177e4SLinus Torvalds struct sk_buff **tail_skb; 13531da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 13540388b004SPavel Emelyanov struct net *net = sock_net(sk); 13551da177e4SLinus Torvalds struct ip_options *opt = NULL; 13561470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 13571da177e4SLinus Torvalds struct iphdr *iph; 135876ab608dSAlexey Dobriyan __be16 df = 0; 13591da177e4SLinus Torvalds __u8 ttl; 13601da177e4SLinus Torvalds 136151456b29SIan Morris skb = __skb_dequeue(queue); 136251456b29SIan Morris if (!skb) 13631da177e4SLinus Torvalds goto out; 13641da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1367d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1368bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 13691470ddf7SHerbert Xu while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1370cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 13711da177e4SLinus Torvalds *tail_skb = tmp_skb; 13721da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 13731da177e4SLinus Torvalds skb->len += tmp_skb->len; 13741da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 13751da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 13761da177e4SLinus Torvalds tmp_skb->destructor = NULL; 13771da177e4SLinus Torvalds tmp_skb->sk = NULL; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow 13811da177e4SLinus Torvalds * to fragment the frame generated here. No matter, what transforms 13821da177e4SLinus Torvalds * how transforms change size of the packet, it will come out. 13831da177e4SLinus Torvalds */ 138460ff7467SWANG Cong skb->ignore_df = ip_sk_ignore_df(sk); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds /* DF bit is set when we want to see DF on outgoing frames. 138760ff7467SWANG Cong * If ignore_df is set too, we still allow to fragment this frame 13881da177e4SLinus Torvalds * locally. */ 1389482fc609SHannes Frederic Sowa if (inet->pmtudisc == IP_PMTUDISC_DO || 1390482fc609SHannes Frederic Sowa inet->pmtudisc == IP_PMTUDISC_PROBE || 1391d8d1f30bSChangli Gao (skb->len <= dst_mtu(&rt->dst) && 1392d8d1f30bSChangli Gao ip_dont_fragment(sk, &rt->dst))) 13931da177e4SLinus Torvalds df = htons(IP_DF); 13941da177e4SLinus Torvalds 13951470ddf7SHerbert Xu if (cork->flags & IPCORK_OPT) 13961470ddf7SHerbert Xu opt = cork->opt; 13971da177e4SLinus Torvalds 1398aa661581SFrancesco Fusco if (cork->ttl != 0) 1399aa661581SFrancesco Fusco ttl = cork->ttl; 1400aa661581SFrancesco Fusco else if (rt->rt_type == RTN_MULTICAST) 14011da177e4SLinus Torvalds ttl = inet->mc_ttl; 14021da177e4SLinus Torvalds else 1403d8d1f30bSChangli Gao ttl = ip_select_ttl(inet, &rt->dst); 14041da177e4SLinus Torvalds 1405749154aaSAnsis Atteka iph = ip_hdr(skb); 14061da177e4SLinus Torvalds iph->version = 4; 14071da177e4SLinus Torvalds iph->ihl = 5; 1408aa661581SFrancesco Fusco iph->tos = (cork->tos != -1) ? cork->tos : inet->tos; 14091da177e4SLinus Torvalds iph->frag_off = df; 14101da177e4SLinus Torvalds iph->ttl = ttl; 14111da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 141284f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 1413b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, sk); 14141da177e4SLinus Torvalds 141522f728f8SDavid S. Miller if (opt) { 141622f728f8SDavid S. Miller iph->ihl += opt->optlen>>2; 141722f728f8SDavid S. Miller ip_options_build(skb, opt, cork->addr, rt, 0); 141822f728f8SDavid S. Miller } 141922f728f8SDavid S. Miller 1420aa661581SFrancesco Fusco skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority; 14214a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1422a21bba94SEric Dumazet /* 1423a21bba94SEric Dumazet * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1424a21bba94SEric Dumazet * on dst refcount 1425a21bba94SEric Dumazet */ 14261470ddf7SHerbert Xu cork->dst = NULL; 1427d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 14281da177e4SLinus Torvalds 142996793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 14300388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 143196793b48SDavid L Stevens skb_transport_header(skb))->type); 143296793b48SDavid L Stevens 14331c32c5adSHerbert Xu ip_cork_release(cork); 14341c32c5adSHerbert Xu out: 14351c32c5adSHerbert Xu return skb; 14361c32c5adSHerbert Xu } 14371c32c5adSHerbert Xu 1438b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb) 14391c32c5adSHerbert Xu { 14401c32c5adSHerbert Xu int err; 14411c32c5adSHerbert Xu 144233224b16SEric W. Biederman err = ip_local_out(net, skb->sk, skb); 14431da177e4SLinus Torvalds if (err) { 14441da177e4SLinus Torvalds if (err > 0) 14456ce9e7b5SEric Dumazet err = net_xmit_errno(err); 14461da177e4SLinus Torvalds if (err) 14471c32c5adSHerbert Xu IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds return err; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 145377968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4) 14541470ddf7SHerbert Xu { 14551c32c5adSHerbert Xu struct sk_buff *skb; 14561c32c5adSHerbert Xu 145777968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 14581c32c5adSHerbert Xu if (!skb) 14591c32c5adSHerbert Xu return 0; 14601c32c5adSHerbert Xu 14611c32c5adSHerbert Xu /* Netfilter gets whole the not fragmented skb. */ 1462b5ec8eeaSEric Dumazet return ip_send_skb(sock_net(sk), skb); 14631470ddf7SHerbert Xu } 14641470ddf7SHerbert Xu 14651da177e4SLinus Torvalds /* 14661da177e4SLinus Torvalds * Throw away all pending data on the socket. 14671da177e4SLinus Torvalds */ 14681470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk, 14691470ddf7SHerbert Xu struct sk_buff_head *queue, 14701470ddf7SHerbert Xu struct inet_cork *cork) 14711da177e4SLinus Torvalds { 14721da177e4SLinus Torvalds struct sk_buff *skb; 14731da177e4SLinus Torvalds 14741470ddf7SHerbert Xu while ((skb = __skb_dequeue_tail(queue)) != NULL) 14751da177e4SLinus Torvalds kfree_skb(skb); 14761da177e4SLinus Torvalds 14771470ddf7SHerbert Xu ip_cork_release(cork); 14781470ddf7SHerbert Xu } 14791470ddf7SHerbert Xu 14801470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk) 14811470ddf7SHerbert Xu { 1482bdc712b4SDavid S. Miller __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 14851c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk, 148677968b78SDavid S. Miller struct flowi4 *fl4, 14871c32c5adSHerbert Xu int getfrag(void *from, char *to, int offset, 14881c32c5adSHerbert Xu int len, int odd, struct sk_buff *skb), 14891c32c5adSHerbert Xu void *from, int length, int transhdrlen, 14901c32c5adSHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 14911c32c5adSHerbert Xu unsigned int flags) 14921c32c5adSHerbert Xu { 1493b80d7226SDavid S. Miller struct inet_cork cork; 14941c32c5adSHerbert Xu struct sk_buff_head queue; 14951c32c5adSHerbert Xu int err; 14961c32c5adSHerbert Xu 14971c32c5adSHerbert Xu if (flags & MSG_PROBE) 14981c32c5adSHerbert Xu return NULL; 14991c32c5adSHerbert Xu 15001c32c5adSHerbert Xu __skb_queue_head_init(&queue); 15011c32c5adSHerbert Xu 1502b80d7226SDavid S. Miller cork.flags = 0; 1503b80d7226SDavid S. Miller cork.addr = 0; 150470652728SDavid S. Miller cork.opt = NULL; 15051c32c5adSHerbert Xu err = ip_setup_cork(sk, &cork, ipc, rtp); 15061c32c5adSHerbert Xu if (err) 15071c32c5adSHerbert Xu return ERR_PTR(err); 15081c32c5adSHerbert Xu 15095640f768SEric Dumazet err = __ip_append_data(sk, fl4, &queue, &cork, 15105640f768SEric Dumazet ¤t->task_frag, getfrag, 15111c32c5adSHerbert Xu from, length, transhdrlen, flags); 15121c32c5adSHerbert Xu if (err) { 15131c32c5adSHerbert Xu __ip_flush_pending_frames(sk, &queue, &cork); 15141c32c5adSHerbert Xu return ERR_PTR(err); 15151c32c5adSHerbert Xu } 15161c32c5adSHerbert Xu 151777968b78SDavid S. Miller return __ip_make_skb(sk, fl4, &queue, &cork); 15181c32c5adSHerbert Xu } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds /* 15211da177e4SLinus Torvalds * Fetch data from kernel space and fill in checksum if needed. 15221da177e4SLinus Torvalds */ 15231da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset, 15241da177e4SLinus Torvalds int len, int odd, struct sk_buff *skb) 15251da177e4SLinus Torvalds { 15265084205fSAl Viro __wsum csum; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0); 15291da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 15301da177e4SLinus Torvalds return 0; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds /* 15341da177e4SLinus Torvalds * Generic function to send a packet as reply to another packet. 1535be9f4a44SEric Dumazet * Used to send some TCP resets/acks so far. 15361da177e4SLinus Torvalds */ 1537bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, 153824a2d43dSEric Dumazet const struct ip_options *sopt, 153924a2d43dSEric Dumazet __be32 daddr, __be32 saddr, 154024a2d43dSEric Dumazet const struct ip_reply_arg *arg, 154170e73416SDavid S. Miller unsigned int len) 15421da177e4SLinus Torvalds { 1543f6d8bd05SEric Dumazet struct ip_options_data replyopts; 15441da177e4SLinus Torvalds struct ipcm_cookie ipc; 154577968b78SDavid S. Miller struct flowi4 fl4; 1546511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1547bdbbb852SEric Dumazet struct net *net = sock_net(sk); 1548be9f4a44SEric Dumazet struct sk_buff *nskb; 15494062090eSVasily Averin int err; 1550f7ba868bSDavid Ahern int oif; 15511da177e4SLinus Torvalds 155224a2d43dSEric Dumazet if (__ip_options_echo(&replyopts.opt.opt, skb, sopt)) 15531da177e4SLinus Torvalds return; 15541da177e4SLinus Torvalds 15550a5ebb80SDavid S. Miller ipc.addr = daddr; 15561da177e4SLinus Torvalds ipc.opt = NULL; 15572244d07bSOliver Hartkopp ipc.tx_flags = 0; 1558aa661581SFrancesco Fusco ipc.ttl = 0; 1559aa661581SFrancesco Fusco ipc.tos = -1; 15601da177e4SLinus Torvalds 1561f6d8bd05SEric Dumazet if (replyopts.opt.opt.optlen) { 15621da177e4SLinus Torvalds ipc.opt = &replyopts.opt; 15631da177e4SLinus Torvalds 1564f6d8bd05SEric Dumazet if (replyopts.opt.opt.srr) 1565f6d8bd05SEric Dumazet daddr = replyopts.opt.opt.faddr; 15661da177e4SLinus Torvalds } 15671da177e4SLinus Torvalds 1568f7ba868bSDavid Ahern oif = arg->bound_dev_if; 1569007979eaSDavid Ahern if (!oif && netif_index_is_l3_master(net, skb->skb_iif)) 1570f7ba868bSDavid Ahern oif = skb->skb_iif; 1571f7ba868bSDavid Ahern 1572f7ba868bSDavid Ahern flowi4_init_output(&fl4, oif, 1573e110861fSLorenzo Colitti IP4_REPLY_MARK(net, skb->mark), 157466b13d99SEric Dumazet RT_TOS(arg->tos), 1575be9f4a44SEric Dumazet RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol, 1576538de0e0SDavid S. Miller ip_reply_arg_flowi_flags(arg), 157770e73416SDavid S. Miller daddr, saddr, 1578538de0e0SDavid S. Miller tcp_hdr(skb)->source, tcp_hdr(skb)->dest); 15799d6ec938SDavid S. Miller security_skb_classify_flow(skb, flowi4_to_flowi(&fl4)); 1580be9f4a44SEric Dumazet rt = ip_route_output_key(net, &fl4); 1581b23dd4feSDavid S. Miller if (IS_ERR(rt)) 15821da177e4SLinus Torvalds return; 15831da177e4SLinus Torvalds 1584bdbbb852SEric Dumazet inet_sk(sk)->tos = arg->tos; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds sk->sk_priority = skb->priority; 1587eddc9ec5SArnaldo Carvalho de Melo sk->sk_protocol = ip_hdr(skb)->protocol; 1588f0e48dbfSPatrick McHardy sk->sk_bound_dev_if = arg->bound_dev_if; 1589be9f4a44SEric Dumazet sk->sk_sndbuf = sysctl_wmem_default; 15904062090eSVasily Averin err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, 15914062090eSVasily Averin len, 0, &ipc, &rt, MSG_DONTWAIT); 15924062090eSVasily Averin if (unlikely(err)) { 15934062090eSVasily Averin ip_flush_pending_frames(sk); 15944062090eSVasily Averin goto out; 15954062090eSVasily Averin } 15964062090eSVasily Averin 1597be9f4a44SEric Dumazet nskb = skb_peek(&sk->sk_write_queue); 1598be9f4a44SEric Dumazet if (nskb) { 15991da177e4SLinus Torvalds if (arg->csumoffset >= 0) 1600be9f4a44SEric Dumazet *((__sum16 *)skb_transport_header(nskb) + 1601be9f4a44SEric Dumazet arg->csumoffset) = csum_fold(csum_add(nskb->csum, 16029c70220bSArnaldo Carvalho de Melo arg->csum)); 1603be9f4a44SEric Dumazet nskb->ip_summed = CHECKSUM_NONE; 160477968b78SDavid S. Miller ip_push_pending_frames(sk, &fl4); 16051da177e4SLinus Torvalds } 16064062090eSVasily Averin out: 16071da177e4SLinus Torvalds ip_rt_put(rt); 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds void __init ip_init(void) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds ip_rt_init(); 16131da177e4SLinus Torvalds inet_initpeers(); 16141da177e4SLinus Torvalds 161572c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST) 161672c1d3bdSWANG Cong igmp_mc_init(); 16171da177e4SLinus Torvalds #endif 16181da177e4SLinus Torvalds } 1619