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> 7614972cbdSRoopa Prabhu #include <net/lwtunnel.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/netlink.h> 816cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h> 821da177e4SLinus Torvalds 83694869b3SEric W. Biederman static int 84694869b3SEric W. Biederman ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 85c5501eb3SFlorian Westphal unsigned int mtu, 86694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)); 8749d16b23SAndy Zhou 881da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */ 892fbd9679SDenis Efremov void ip_send_check(struct iphdr *iph) 901da177e4SLinus Torvalds { 911da177e4SLinus Torvalds iph->check = 0; 921da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 931da177e4SLinus Torvalds } 944bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check); 951da177e4SLinus Torvalds 96cf91a99dSEric W. Biederman int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 97c439cb2eSHerbert Xu { 98c439cb2eSHerbert Xu struct iphdr *iph = ip_hdr(skb); 99c439cb2eSHerbert Xu 100c439cb2eSHerbert Xu iph->tot_len = htons(skb->len); 101c439cb2eSHerbert Xu ip_send_check(iph); 102a8e3e1a9SDavid Ahern 103a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 104a8e3e1a9SDavid Ahern * skb to its handler for processing 105a8e3e1a9SDavid Ahern */ 106a8e3e1a9SDavid Ahern skb = l3mdev_ip_out(sk, skb); 107a8e3e1a9SDavid Ahern if (unlikely(!skb)) 108a8e3e1a9SDavid Ahern return 0; 109a8e3e1a9SDavid Ahern 11029a26a56SEric W. Biederman return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, 11129a26a56SEric W. Biederman net, sk, skb, NULL, skb_dst(skb)->dev, 11213206b6bSEric W. Biederman dst_output); 1137026b1ddSDavid Miller } 1147026b1ddSDavid Miller 11533224b16SEric W. Biederman int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 116c439cb2eSHerbert Xu { 117c439cb2eSHerbert Xu int err; 118c439cb2eSHerbert Xu 119cf91a99dSEric W. Biederman err = __ip_local_out(net, sk, skb); 120c439cb2eSHerbert Xu if (likely(err == 1)) 12113206b6bSEric W. Biederman err = dst_output(net, sk, skb); 122c439cb2eSHerbert Xu 123c439cb2eSHerbert Xu return err; 124c439cb2eSHerbert Xu } 125e2cb77dbSEric W. Biederman EXPORT_SYMBOL_GPL(ip_local_out); 126c439cb2eSHerbert Xu 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) 132323e126fSDavid S. Miller ttl = ip4_dst_hoplimit(dst); 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 */ 140cfe673b0SEric Dumazet int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, 141f6d8bd05SEric Dumazet __be32 saddr, __be32 daddr, struct ip_options_rcu *opt) 1421da177e4SLinus Torvalds { 1431da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 144511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 14577589ce0SEric W. Biederman struct net *net = sock_net(sk); 1461da177e4SLinus Torvalds struct iphdr *iph; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Build the IP header. */ 149f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0)); 1508856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 151eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1521da177e4SLinus Torvalds iph->version = 4; 1531da177e4SLinus Torvalds iph->ihl = 5; 1541da177e4SLinus Torvalds iph->tos = inet->tos; 155d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 156dd927a26SDavid S. Miller iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); 157dd927a26SDavid S. Miller iph->saddr = saddr; 1581da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 159cfe673b0SEric Dumazet if (ip_dont_fragment(sk, &rt->dst)) { 160cfe673b0SEric Dumazet iph->frag_off = htons(IP_DF); 161cfe673b0SEric Dumazet iph->id = 0; 162cfe673b0SEric Dumazet } else { 163cfe673b0SEric Dumazet iph->frag_off = 0; 16477589ce0SEric W. Biederman __ip_select_ident(net, iph, 1); 165cfe673b0SEric Dumazet } 1661da177e4SLinus Torvalds 167f6d8bd05SEric Dumazet if (opt && opt->opt.optlen) { 168f6d8bd05SEric Dumazet iph->ihl += opt->opt.optlen>>2; 169f6d8bd05SEric Dumazet ip_options_build(skb, &opt->opt, daddr, rt, 0); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds skb->priority = sk->sk_priority; 1734a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* Send it out. */ 17633224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 1771da177e4SLinus Torvalds } 178d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 179d8c97a94SArnaldo Carvalho de Melo 180694869b3SEric W. Biederman static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 1811da177e4SLinus Torvalds { 182adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 18380787ebcSMitsuru Chinen struct rtable *rt = (struct rtable *)dst; 1841da177e4SLinus Torvalds struct net_device *dev = dst->dev; 185c2636b4dSChuck Lever unsigned int hh_len = LL_RESERVED_SPACE(dev); 186f6b72b62SDavid S. Miller struct neighbour *neigh; 187a263b309SDavid S. Miller u32 nexthop; 1881da177e4SLinus Torvalds 189edf391ffSNeil Horman if (rt->rt_type == RTN_MULTICAST) { 1904ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len); 191edf391ffSNeil Horman } else if (rt->rt_type == RTN_BROADCAST) 1924ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len); 19380787ebcSMitsuru Chinen 1941da177e4SLinus Torvalds /* Be paranoid, rather than too clever. */ 1953b04dddeSStephen Hemminger if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 1961da177e4SLinus Torvalds struct sk_buff *skb2; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); 19951456b29SIan Morris if (!skb2) { 2001da177e4SLinus Torvalds kfree_skb(skb); 2011da177e4SLinus Torvalds return -ENOMEM; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds if (skb->sk) 2041da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 2055d0ba55bSEric Dumazet consume_skb(skb); 2061da177e4SLinus Torvalds skb = skb2; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 20914972cbdSRoopa Prabhu if (lwtunnel_xmit_redirect(dst->lwtstate)) { 21014972cbdSRoopa Prabhu int res = lwtunnel_xmit(skb); 21114972cbdSRoopa Prabhu 21214972cbdSRoopa Prabhu if (res < 0 || res == LWTUNNEL_XMIT_DONE) 21314972cbdSRoopa Prabhu return res; 21414972cbdSRoopa Prabhu } 21514972cbdSRoopa Prabhu 216a263b309SDavid S. Miller rcu_read_lock_bh(); 217155e8336SJulian Anastasov nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr); 218a263b309SDavid S. Miller neigh = __ipv4_neigh_lookup_noref(dev, nexthop); 219a263b309SDavid S. Miller if (unlikely(!neigh)) 220a263b309SDavid S. Miller neigh = __neigh_create(&arp_tbl, &nexthop, dev, false); 2219871f1adSVasiliy Kulikov if (!IS_ERR(neigh)) { 2225110effeSDavid S. Miller int res = dst_neigh_output(dst, neigh, skb); 223f2c31e32SEric Dumazet 224a263b309SDavid S. Miller rcu_read_unlock_bh(); 225f2c31e32SEric Dumazet return res; 226f2c31e32SEric Dumazet } 227a263b309SDavid S. Miller rcu_read_unlock_bh(); 22805e3aa09SDavid S. Miller 229e87cc472SJoe Perches net_dbg_ratelimited("%s: No header cache and no neighbour!\n", 230e87cc472SJoe Perches __func__); 2311da177e4SLinus Torvalds kfree_skb(skb); 2321da177e4SLinus Torvalds return -EINVAL; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 235694869b3SEric W. Biederman static int ip_finish_output_gso(struct net *net, struct sock *sk, 236694869b3SEric W. Biederman struct sk_buff *skb, unsigned int mtu) 237c7ba65d7SFlorian Westphal { 238c7ba65d7SFlorian Westphal netdev_features_t features; 239c7ba65d7SFlorian Westphal struct sk_buff *segs; 240c7ba65d7SFlorian Westphal int ret = 0; 241c7ba65d7SFlorian Westphal 242*9ee6c5dcSLance Richardson /* common case: seglen is <= mtu 243359ebda2SShmulik Ladkani */ 244*9ee6c5dcSLance Richardson if (skb_gso_validate_mtu(skb, mtu)) 245694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 246c7ba65d7SFlorian Westphal 247c7ba65d7SFlorian Westphal /* Slowpath - GSO segment length is exceeding the dst MTU. 248c7ba65d7SFlorian Westphal * 249c7ba65d7SFlorian Westphal * This can happen in two cases: 250c7ba65d7SFlorian Westphal * 1) TCP GRO packet, DF bit not set 251c7ba65d7SFlorian Westphal * 2) skb arrived via virtio-net, we thus get TSO/GSO skbs directly 252c7ba65d7SFlorian Westphal * from host network stack. 253c7ba65d7SFlorian Westphal */ 254c7ba65d7SFlorian Westphal features = netif_skb_features(skb); 2559207f9d4SKonstantin Khlebnikov BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET); 256c7ba65d7SFlorian Westphal segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 257330966e5SFlorian Westphal if (IS_ERR_OR_NULL(segs)) { 258c7ba65d7SFlorian Westphal kfree_skb(skb); 259c7ba65d7SFlorian Westphal return -ENOMEM; 260c7ba65d7SFlorian Westphal } 261c7ba65d7SFlorian Westphal 262c7ba65d7SFlorian Westphal consume_skb(skb); 263c7ba65d7SFlorian Westphal 264c7ba65d7SFlorian Westphal do { 265c7ba65d7SFlorian Westphal struct sk_buff *nskb = segs->next; 266c7ba65d7SFlorian Westphal int err; 267c7ba65d7SFlorian Westphal 268c7ba65d7SFlorian Westphal segs->next = NULL; 269694869b3SEric W. Biederman err = ip_fragment(net, sk, segs, mtu, ip_finish_output2); 270c7ba65d7SFlorian Westphal 271c7ba65d7SFlorian Westphal if (err && ret == 0) 272c7ba65d7SFlorian Westphal ret = err; 273c7ba65d7SFlorian Westphal segs = nskb; 274c7ba65d7SFlorian Westphal } while (segs); 275c7ba65d7SFlorian Westphal 276c7ba65d7SFlorian Westphal return ret; 277c7ba65d7SFlorian Westphal } 278c7ba65d7SFlorian Westphal 2790c4b51f0SEric W. Biederman static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 2801da177e4SLinus Torvalds { 281c5501eb3SFlorian Westphal unsigned int mtu; 282c5501eb3SFlorian Westphal 2835c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 2845c901daaSPatrick McHardy /* Policy lookup after SNAT yielded a new policy */ 28500db4124SIan Morris if (skb_dst(skb)->xfrm) { 28648d5cad8SPatrick McHardy IPCB(skb)->flags |= IPSKB_REROUTED; 28713206b6bSEric W. Biederman return dst_output(net, sk, skb); 28848d5cad8SPatrick McHardy } 2895c901daaSPatrick McHardy #endif 290fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 291c7ba65d7SFlorian Westphal if (skb_is_gso(skb)) 292694869b3SEric W. Biederman return ip_finish_output_gso(net, sk, skb, mtu); 293c7ba65d7SFlorian Westphal 294d6b915e2SFlorian Westphal if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU)) 295694869b3SEric W. Biederman return ip_fragment(net, sk, skb, mtu, ip_finish_output2); 296c7ba65d7SFlorian Westphal 297694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 300ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb) 3011da177e4SLinus Torvalds { 302511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 303d8d1f30bSChangli Gao struct net_device *dev = rt->dst.dev; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* 3061da177e4SLinus Torvalds * If the indicated interface is up and running, send the packet. 3071da177e4SLinus Torvalds */ 30888f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds skb->dev = dev; 3111da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * Multicasts are looped back for other local users 3151da177e4SLinus Torvalds */ 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds if (rt->rt_flags&RTCF_MULTICAST) { 3187ad6848cSOctavian Purdila if (sk_mc_loop(sk) 3191da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 3201da177e4SLinus Torvalds /* Small optimization: do not loopback not local frames, 3211da177e4SLinus Torvalds which returned after forwarding; they will be dropped 3221da177e4SLinus Torvalds by ip_mr_input in any case. 3231da177e4SLinus Torvalds Note, that local frames are looped back to be delivered 3241da177e4SLinus Torvalds to local recipients. 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds This check is duplicated in ip_mr_input at the moment. 3271da177e4SLinus Torvalds */ 3289d4fb27dSJoe Perches && 3299d4fb27dSJoe Perches ((rt->rt_flags & RTCF_LOCAL) || 3309d4fb27dSJoe Perches !(IPCB(skb)->flags & IPSKB_FORWARDED)) 3311da177e4SLinus Torvalds #endif 3321da177e4SLinus Torvalds ) { 3331da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 3341da177e4SLinus Torvalds if (newskb) 3359bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 33629a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 33795603e22SMichel Machado dev_loopback_xmit); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* Multicasts with ttl 0 must not go beyond the host */ 3411da177e4SLinus Torvalds 342eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl == 0) { 3431da177e4SLinus Torvalds kfree_skb(skb); 3441da177e4SLinus Torvalds return 0; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds if (rt->rt_flags&RTCF_BROADCAST) { 3491da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 3501da177e4SLinus Torvalds if (newskb) 35129a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 35229a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 35329a26a56SEric W. Biederman dev_loopback_xmit); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 35629a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 35729a26a56SEric W. Biederman net, sk, skb, NULL, skb->dev, 35829a26a56SEric W. Biederman ip_finish_output, 35948d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 362ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb) 3631da177e4SLinus Torvalds { 364adf30907SEric Dumazet struct net_device *dev = skb_dst(skb)->dev; 3651bd9bef6SPatrick McHardy 36688f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 3671da177e4SLinus Torvalds 3681bd9bef6SPatrick McHardy skb->dev = dev; 3691bd9bef6SPatrick McHardy skb->protocol = htons(ETH_P_IP); 3701bd9bef6SPatrick McHardy 37129a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 37229a26a56SEric W. Biederman net, sk, skb, NULL, dev, 37348d5cad8SPatrick McHardy ip_finish_output, 37448d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 37784f9307cSEric Dumazet /* 37884f9307cSEric Dumazet * copy saddr and daddr, possibly using 64bit load/stores 37984f9307cSEric Dumazet * Equivalent to : 38084f9307cSEric Dumazet * iph->saddr = fl4->saddr; 38184f9307cSEric Dumazet * iph->daddr = fl4->daddr; 38284f9307cSEric Dumazet */ 38384f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4) 38484f9307cSEric Dumazet { 38584f9307cSEric Dumazet BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) != 38684f9307cSEric Dumazet offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr)); 38784f9307cSEric Dumazet memcpy(&iph->saddr, &fl4->saddr, 38884f9307cSEric Dumazet sizeof(fl4->saddr) + sizeof(fl4->daddr)); 38984f9307cSEric Dumazet } 39084f9307cSEric Dumazet 391b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */ 392b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 39577589ce0SEric W. Biederman struct net *net = sock_net(sk); 396f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 397b57ae01aSDavid S. Miller struct flowi4 *fl4; 3981da177e4SLinus Torvalds struct rtable *rt; 3991da177e4SLinus Torvalds struct iphdr *iph; 400ab6e3febSEric Dumazet int res; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* Skip all of this if the packet is already routed, 4031da177e4SLinus Torvalds * f.e. by something like SCTP. 4041da177e4SLinus Torvalds */ 405ab6e3febSEric Dumazet rcu_read_lock(); 406f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 407ea4fc0d6SDavid S. Miller fl4 = &fl->u.ip4; 408511c3f92SEric Dumazet rt = skb_rtable(skb); 40900db4124SIan Morris if (rt) 4101da177e4SLinus Torvalds goto packet_routed; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* Make sure we can route this packet. */ 4131da177e4SLinus Torvalds rt = (struct rtable *)__sk_dst_check(sk, 0); 41451456b29SIan Morris if (!rt) { 4153ca3c68eSAl Viro __be32 daddr; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* Use correct destination address if we have options. */ 418c720c7e8SEric Dumazet daddr = inet->inet_daddr; 419f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 420f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds /* If this fails, retransmit mechanism of transport layer will 4231da177e4SLinus Torvalds * keep trying until route appears or the connection times 4241da177e4SLinus Torvalds * itself out. 4251da177e4SLinus Torvalds */ 42677589ce0SEric W. Biederman rt = ip_route_output_ports(net, fl4, sk, 42778fbfd8aSDavid S. Miller daddr, inet->inet_saddr, 42878fbfd8aSDavid S. Miller inet->inet_dport, 42978fbfd8aSDavid S. Miller inet->inet_sport, 43078fbfd8aSDavid S. Miller sk->sk_protocol, 43178fbfd8aSDavid S. Miller RT_CONN_FLAGS(sk), 43278fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 433b23dd4feSDavid S. Miller if (IS_ERR(rt)) 4341da177e4SLinus Torvalds goto no_route; 435d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 4361da177e4SLinus Torvalds } 437d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds packet_routed: 440155e8336SJulian Anastasov if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway) 4411da177e4SLinus Torvalds goto no_route; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* OK, we know where to send it, allocate and build IP header. */ 444f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0)); 4458856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 446eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 447714e85beSAl Viro *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff)); 44860ff7467SWANG Cong if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df) 4491da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 4501da177e4SLinus Torvalds else 4511da177e4SLinus Torvalds iph->frag_off = 0; 452d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 4531da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 45484f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 45584f9307cSEric Dumazet 4561da177e4SLinus Torvalds /* Transport layer set skb->h.foo itself. */ 4571da177e4SLinus Torvalds 458f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.optlen) { 459f6d8bd05SEric Dumazet iph->ihl += inet_opt->opt.optlen >> 2; 460f6d8bd05SEric Dumazet ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 46377589ce0SEric W. Biederman ip_select_ident_segs(net, skb, sk, 464b6a7719aSHannes Frederic Sowa skb_shinfo(skb)->gso_segs ?: 1); 4651da177e4SLinus Torvalds 466b0270e91SEric Dumazet /* TODO : should we use skb->sk here instead of sk ? */ 4671da177e4SLinus Torvalds skb->priority = sk->sk_priority; 4684a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 4691da177e4SLinus Torvalds 47033224b16SEric W. Biederman res = ip_local_out(net, sk, skb); 471ab6e3febSEric Dumazet rcu_read_unlock(); 472ab6e3febSEric Dumazet return res; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds no_route: 475ab6e3febSEric Dumazet rcu_read_unlock(); 47677589ce0SEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 4771da177e4SLinus Torvalds kfree_skb(skb); 4781da177e4SLinus Torvalds return -EHOSTUNREACH; 4791da177e4SLinus Torvalds } 4804bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 4851da177e4SLinus Torvalds to->priority = from->priority; 4861da177e4SLinus Torvalds to->protocol = from->protocol; 487adf30907SEric Dumazet skb_dst_drop(to); 488fe76cda3SEric Dumazet skb_dst_copy(to, from); 4891da177e4SLinus Torvalds to->dev = from->dev; 49082e91ffeSThomas Graf to->mark = from->mark; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* Copy the flags to each fragment. */ 4931da177e4SLinus Torvalds IPCB(to)->flags = IPCB(from)->flags; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4961da177e4SLinus Torvalds to->tc_index = from->tc_index; 4971da177e4SLinus Torvalds #endif 498e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 4996ca40d4eSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IP_VS) 500c98d80edSJulian Anastasov to->ipvs_property = from->ipvs_property; 501c98d80edSJulian Anastasov #endif 502984bc16cSJames Morris skb_copy_secmark(to, from); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 505694869b3SEric W. Biederman static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 506c5501eb3SFlorian Westphal unsigned int mtu, 507694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 50849d16b23SAndy Zhou { 50949d16b23SAndy Zhou struct iphdr *iph = ip_hdr(skb); 51049d16b23SAndy Zhou 511d6b915e2SFlorian Westphal if ((iph->frag_off & htons(IP_DF)) == 0) 512694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 513d6b915e2SFlorian Westphal 514d6b915e2SFlorian Westphal if (unlikely(!skb->ignore_df || 51549d16b23SAndy Zhou (IPCB(skb)->frag_max_size && 51649d16b23SAndy Zhou IPCB(skb)->frag_max_size > mtu))) { 5179479b0afSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 51849d16b23SAndy Zhou icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 51949d16b23SAndy Zhou htonl(mtu)); 52049d16b23SAndy Zhou kfree_skb(skb); 52149d16b23SAndy Zhou return -EMSGSIZE; 52249d16b23SAndy Zhou } 52349d16b23SAndy Zhou 524694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 52549d16b23SAndy Zhou } 52649d16b23SAndy Zhou 5271da177e4SLinus Torvalds /* 5281da177e4SLinus Torvalds * This IP datagram is too large to be sent in one piece. Break it up into 5291da177e4SLinus Torvalds * smaller pieces (each of size equal to IP header plus 5301da177e4SLinus Torvalds * a block of the data of the original IP data part) that will yet fit in a 5311da177e4SLinus Torvalds * single device frame, and queue such a frame for sending. 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds 534694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 535694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 5361da177e4SLinus Torvalds { 5371da177e4SLinus Torvalds struct iphdr *iph; 5381da177e4SLinus Torvalds int ptr; 5391da177e4SLinus Torvalds struct sk_buff *skb2; 540c893b806SChangli Gao unsigned int mtu, hlen, left, len, ll_rs; 5411da177e4SLinus Torvalds int offset; 54276ab608dSAlexey Dobriyan __be16 not_last_frag; 543511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 5441da177e4SLinus Torvalds int err = 0; 5451da177e4SLinus Torvalds 546dbd3393cSHannes Frederic Sowa /* for offloaded checksums cleanup checksum before fragmentation */ 547dbd3393cSHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 548dbd3393cSHannes Frederic Sowa (err = skb_checksum_help(skb))) 549dbd3393cSHannes Frederic Sowa goto fail; 550dbd3393cSHannes Frederic Sowa 5511da177e4SLinus Torvalds /* 5521da177e4SLinus Torvalds * Point into the IP datagram header. 5531da177e4SLinus Torvalds */ 5541da177e4SLinus Torvalds 555eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 5561da177e4SLinus Torvalds 557fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 558d6b915e2SFlorian Westphal if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu) 559d6b915e2SFlorian Westphal mtu = IPCB(skb)->frag_max_size; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /* 5621da177e4SLinus Torvalds * Setup starting values. 5631da177e4SLinus Torvalds */ 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds hlen = iph->ihl * 4; 566f87c10a8SHannes Frederic Sowa mtu = mtu - hlen; /* Size of data space */ 56789cee8b1SHerbert Xu IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds /* When frag_list is given, use it. First, check its validity: 5701da177e4SLinus Torvalds * some transformers could create wrong frag_list or break existing 5711da177e4SLinus Torvalds * one, it is not prohibited. In this case fall back to copying. 5721da177e4SLinus Torvalds * 5731da177e4SLinus Torvalds * LATER: this step can be merged to real generation of fragments, 5741da177e4SLinus Torvalds * we can switch to copy when see the first bad fragment. 5751da177e4SLinus Torvalds */ 57621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 5773d13008eSEric Dumazet struct sk_buff *frag, *frag2; 5781da177e4SLinus Torvalds int first_len = skb_pagelen(skb); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds if (first_len - hlen > mtu || 5811da177e4SLinus Torvalds ((first_len - hlen) & 7) || 58256f8a75cSPaul Gortmaker ip_is_fragment(iph) || 5831da177e4SLinus Torvalds skb_cloned(skb)) 5841da177e4SLinus Torvalds goto slow_path; 5851da177e4SLinus Torvalds 586d7fcf1a5SDavid S. Miller skb_walk_frags(skb, frag) { 5871da177e4SLinus Torvalds /* Correct geometry. */ 5881da177e4SLinus Torvalds if (frag->len > mtu || 5891da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 5901da177e4SLinus Torvalds skb_headroom(frag) < hlen) 5913d13008eSEric Dumazet goto slow_path_clean; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* Partially cloned skb? */ 5941da177e4SLinus Torvalds if (skb_shared(frag)) 5953d13008eSEric Dumazet goto slow_path_clean; 5962fdba6b0SHerbert Xu 5972fdba6b0SHerbert Xu BUG_ON(frag->sk); 5982fdba6b0SHerbert Xu if (skb->sk) { 5992fdba6b0SHerbert Xu frag->sk = skb->sk; 6002fdba6b0SHerbert Xu frag->destructor = sock_wfree; 6012fdba6b0SHerbert Xu } 6023d13008eSEric Dumazet skb->truesize -= frag->truesize; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* Everything is OK. Generate! */ 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds err = 0; 6081da177e4SLinus Torvalds offset = 0; 6091da177e4SLinus Torvalds frag = skb_shinfo(skb)->frag_list; 610d7fcf1a5SDavid S. Miller skb_frag_list_init(skb); 6111da177e4SLinus Torvalds skb->data_len = first_len - skb_headlen(skb); 6121da177e4SLinus Torvalds skb->len = first_len; 6131da177e4SLinus Torvalds iph->tot_len = htons(first_len); 6141da177e4SLinus Torvalds iph->frag_off = htons(IP_MF); 6151da177e4SLinus Torvalds ip_send_check(iph); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds for (;;) { 6181da177e4SLinus Torvalds /* Prepare header of the next frame, 6191da177e4SLinus Torvalds * before previous one went down. */ 6201da177e4SLinus Torvalds if (frag) { 6211da177e4SLinus Torvalds frag->ip_summed = CHECKSUM_NONE; 622badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(frag); 623e2d1bca7SArnaldo Carvalho de Melo __skb_push(frag, hlen); 624e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(frag); 625d56f90a7SArnaldo Carvalho de Melo memcpy(skb_network_header(frag), iph, hlen); 626eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(frag); 6271da177e4SLinus Torvalds iph->tot_len = htons(frag->len); 6281da177e4SLinus Torvalds ip_copy_metadata(frag, skb); 6291da177e4SLinus Torvalds if (offset == 0) 6301da177e4SLinus Torvalds ip_options_fragment(frag); 6311da177e4SLinus Torvalds offset += skb->len - hlen; 6321da177e4SLinus Torvalds iph->frag_off = htons(offset>>3); 63300db4124SIan Morris if (frag->next) 6341da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 6351da177e4SLinus Torvalds /* Ready, complete checksum */ 6361da177e4SLinus Torvalds ip_send_check(iph); 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 639694869b3SEric W. Biederman err = output(net, sk, skb); 6401da177e4SLinus Torvalds 641dafee490SWei Dong if (!err) 64226a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 6431da177e4SLinus Torvalds if (err || !frag) 6441da177e4SLinus Torvalds break; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds skb = frag; 6471da177e4SLinus Torvalds frag = skb->next; 6481da177e4SLinus Torvalds skb->next = NULL; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (err == 0) { 65226a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 6531da177e4SLinus Torvalds return 0; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds while (frag) { 6571da177e4SLinus Torvalds skb = frag->next; 6581da177e4SLinus Torvalds kfree_skb(frag); 6591da177e4SLinus Torvalds frag = skb; 6601da177e4SLinus Torvalds } 66126a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 6621da177e4SLinus Torvalds return err; 6633d13008eSEric Dumazet 6643d13008eSEric Dumazet slow_path_clean: 6653d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 6663d13008eSEric Dumazet if (frag2 == frag) 6673d13008eSEric Dumazet break; 6683d13008eSEric Dumazet frag2->sk = NULL; 6693d13008eSEric Dumazet frag2->destructor = NULL; 6703d13008eSEric Dumazet skb->truesize += frag2->truesize; 6713d13008eSEric Dumazet } 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds slow_path: 675c9af6db4SPravin B Shelar iph = ip_hdr(skb); 676fc70fb64SAlexander Duyck 6771da177e4SLinus Torvalds left = skb->len - hlen; /* Space per frame */ 67849085bd7SGeorge Kadianakis ptr = hlen; /* Where to start from */ 6791da177e4SLinus Torvalds 6808d045163SFlorian Westphal ll_rs = LL_RESERVED_SPACE(rt->dst.dev); 6819bcfcaf5SStephen Hemminger 6821da177e4SLinus Torvalds /* 6831da177e4SLinus Torvalds * Fragment the datagram. 6841da177e4SLinus Torvalds */ 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; 6871da177e4SLinus Torvalds not_last_frag = iph->frag_off & htons(IP_MF); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds /* 6901da177e4SLinus Torvalds * Keep copying data until we run out. 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds while (left > 0) { 6941da177e4SLinus Torvalds len = left; 6951da177e4SLinus Torvalds /* IF: it doesn't fit, use 'mtu' - the data space left */ 6961da177e4SLinus Torvalds if (len > mtu) 6971da177e4SLinus Torvalds len = mtu; 6981da177e4SLinus Torvalds /* IF: we are not sending up to and including the packet end 6991da177e4SLinus Torvalds then align the next start on an eight byte boundary */ 7001da177e4SLinus Torvalds if (len < left) { 7011da177e4SLinus Torvalds len &= ~7; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 704cbffccc9SJoe Perches /* Allocate buffer */ 705cbffccc9SJoe Perches skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC); 706cbffccc9SJoe Perches if (!skb2) { 7071da177e4SLinus Torvalds err = -ENOMEM; 7081da177e4SLinus Torvalds goto fail; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /* 7121da177e4SLinus Torvalds * Set up data on packet 7131da177e4SLinus Torvalds */ 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds ip_copy_metadata(skb2, skb); 7161da177e4SLinus Torvalds skb_reserve(skb2, ll_rs); 7171da177e4SLinus Torvalds skb_put(skb2, len + hlen); 718c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb2); 719b0e380b1SArnaldo Carvalho de Melo skb2->transport_header = skb2->network_header + hlen; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* 7221da177e4SLinus Torvalds * Charge the memory for the fragment to any owner 7231da177e4SLinus Torvalds * it might possess 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds if (skb->sk) 7271da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* 7301da177e4SLinus Torvalds * Copy the packet header into the new buffer. 7311da177e4SLinus Torvalds */ 7321da177e4SLinus Torvalds 733d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /* 7361da177e4SLinus Torvalds * Copy a block of the IP datagram. 7371da177e4SLinus Torvalds */ 738bff9b61cSArnaldo Carvalho de Melo if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len)) 7391da177e4SLinus Torvalds BUG(); 7401da177e4SLinus Torvalds left -= len; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /* 7431da177e4SLinus Torvalds * Fill in the new header fields. 7441da177e4SLinus Torvalds */ 745eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb2); 7461da177e4SLinus Torvalds iph->frag_off = htons((offset >> 3)); 7471da177e4SLinus Torvalds 748d6b915e2SFlorian Westphal if (IPCB(skb)->flags & IPSKB_FRAG_PMTU) 749d6b915e2SFlorian Westphal iph->frag_off |= htons(IP_DF); 750d6b915e2SFlorian Westphal 7511da177e4SLinus Torvalds /* ANK: dirty, but effective trick. Upgrade options only if 7521da177e4SLinus Torvalds * the segment to be fragmented was THE FIRST (otherwise, 7531da177e4SLinus Torvalds * options are already fixed) and make it ONCE 7541da177e4SLinus Torvalds * on the initial skb, so that all the following fragments 7551da177e4SLinus Torvalds * will inherit fixed options. 7561da177e4SLinus Torvalds */ 7571da177e4SLinus Torvalds if (offset == 0) 7581da177e4SLinus Torvalds ip_options_fragment(skb); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* 7611da177e4SLinus Torvalds * Added AC : If we are fragmenting a fragment that's not the 7621da177e4SLinus Torvalds * last fragment then keep MF on each bit 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds if (left > 0 || not_last_frag) 7651da177e4SLinus Torvalds iph->frag_off |= htons(IP_MF); 7661da177e4SLinus Torvalds ptr += len; 7671da177e4SLinus Torvalds offset += len; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * Put this fragment into the sending queue. 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds iph->tot_len = htons(len + hlen); 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds ip_send_check(iph); 7751da177e4SLinus Torvalds 776694869b3SEric W. Biederman err = output(net, sk, skb2); 7771da177e4SLinus Torvalds if (err) 7781da177e4SLinus Torvalds goto fail; 779dafee490SWei Dong 78026a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 7811da177e4SLinus Torvalds } 7825d0ba55bSEric Dumazet consume_skb(skb); 78326a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 7841da177e4SLinus Torvalds return err; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds fail: 7871da177e4SLinus Torvalds kfree_skb(skb); 78826a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 7891da177e4SLinus Torvalds return err; 7901da177e4SLinus Torvalds } 79149d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment); 7922e2f7aefSPatrick McHardy 7931da177e4SLinus Torvalds int 7941da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 7951da177e4SLinus Torvalds { 796f69e6d13SAl Viro struct msghdr *msg = from; 7971da177e4SLinus Torvalds 79884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 79921226abbSAl Viro if (copy_from_iter(to, len, &msg->msg_iter) != len) 8001da177e4SLinus Torvalds return -EFAULT; 8011da177e4SLinus Torvalds } else { 80244bb9363SAl Viro __wsum csum = 0; 80321226abbSAl Viro if (csum_and_copy_from_iter(to, len, &csum, &msg->msg_iter) != len) 8041da177e4SLinus Torvalds return -EFAULT; 8051da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds return 0; 8081da177e4SLinus Torvalds } 8094bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag); 8101da177e4SLinus Torvalds 81144bb9363SAl Viro static inline __wsum 8121da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy) 8131da177e4SLinus Torvalds { 8141da177e4SLinus Torvalds char *kaddr; 81544bb9363SAl Viro __wsum csum; 8161da177e4SLinus Torvalds kaddr = kmap(page); 8171da177e4SLinus Torvalds csum = csum_partial(kaddr + offset, copy, 0); 8181da177e4SLinus Torvalds kunmap(page); 8191da177e4SLinus Torvalds return csum; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8224b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk, 8231470ddf7SHerbert Xu struct sk_buff_head *queue, 824e89e9cf5SAnanda Raju int getfrag(void *from, char *to, int offset, int len, 825e89e9cf5SAnanda Raju int odd, struct sk_buff *skb), 826e89e9cf5SAnanda Raju void *from, int length, int hh_len, int fragheaderlen, 827d9be4f7aSBill Sommerfeld int transhdrlen, int maxfraglen, unsigned int flags) 828e89e9cf5SAnanda Raju { 829e89e9cf5SAnanda Raju struct sk_buff *skb; 830e89e9cf5SAnanda Raju int err; 831e89e9cf5SAnanda Raju 832e89e9cf5SAnanda Raju /* There is support for UDP fragmentation offload by network 833e89e9cf5SAnanda Raju * device, so create one single skb packet containing complete 834e89e9cf5SAnanda Raju * udp datagram 835e89e9cf5SAnanda Raju */ 83651456b29SIan Morris skb = skb_peek_tail(queue); 83751456b29SIan Morris if (!skb) { 838e89e9cf5SAnanda Raju skb = sock_alloc_send_skb(sk, 839e89e9cf5SAnanda Raju hh_len + fragheaderlen + transhdrlen + 20, 840e89e9cf5SAnanda Raju (flags & MSG_DONTWAIT), &err); 841e89e9cf5SAnanda Raju 84251456b29SIan Morris if (!skb) 843e89e9cf5SAnanda Raju return err; 844e89e9cf5SAnanda Raju 845e89e9cf5SAnanda Raju /* reserve space for Hardware header */ 846e89e9cf5SAnanda Raju skb_reserve(skb, hh_len); 847e89e9cf5SAnanda Raju 848e89e9cf5SAnanda Raju /* create space for UDP/IP header */ 849e89e9cf5SAnanda Raju skb_put(skb, fragheaderlen + transhdrlen); 850e89e9cf5SAnanda Raju 851e89e9cf5SAnanda Raju /* initialize network header pointer */ 852c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 853e89e9cf5SAnanda Raju 854e89e9cf5SAnanda Raju /* initialize protocol header pointer */ 855b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + fragheaderlen; 856e89e9cf5SAnanda Raju 857e89e9cf5SAnanda Raju skb->csum = 0; 858e89e9cf5SAnanda Raju 859e93b7d74SJiri Pirko __skb_queue_tail(queue, skb); 860e93b7d74SJiri Pirko } else if (skb_is_gso(skb)) { 861e93b7d74SJiri Pirko goto append; 862e93b7d74SJiri Pirko } 863e93b7d74SJiri Pirko 864e93b7d74SJiri Pirko skb->ip_summed = CHECKSUM_PARTIAL; 865e89e9cf5SAnanda Raju /* specify the length of each IP datagram fragment */ 866d9be4f7aSBill Sommerfeld skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen; 867f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 868be9164e7SKostya B 869e93b7d74SJiri Pirko append: 870be9164e7SKostya B return skb_append_datato_frags(sk, skb, getfrag, from, 871be9164e7SKostya B (length - transhdrlen)); 872e89e9cf5SAnanda Raju } 873e89e9cf5SAnanda Raju 874f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk, 875f5fca608SDavid S. Miller struct flowi4 *fl4, 876f5fca608SDavid S. Miller struct sk_buff_head *queue, 8771470ddf7SHerbert Xu struct inet_cork *cork, 8785640f768SEric Dumazet struct page_frag *pfrag, 8791470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, 8801470ddf7SHerbert Xu int len, int odd, struct sk_buff *skb), 8811da177e4SLinus Torvalds void *from, int length, int transhdrlen, 8821da177e4SLinus Torvalds unsigned int flags) 8831da177e4SLinus Torvalds { 8841da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 8851da177e4SLinus Torvalds struct sk_buff *skb; 8861da177e4SLinus Torvalds 88707df5294SHerbert Xu struct ip_options *opt = cork->opt; 8881da177e4SLinus Torvalds int hh_len; 8891da177e4SLinus Torvalds int exthdrlen; 8901da177e4SLinus Torvalds int mtu; 8911da177e4SLinus Torvalds int copy; 8921da177e4SLinus Torvalds int err; 8931da177e4SLinus Torvalds int offset = 0; 894daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, maxnonfragsize; 8951da177e4SLinus Torvalds int csummode = CHECKSUM_NONE; 8961470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 89709c2d251SWillem de Bruijn u32 tskey = 0; 8981da177e4SLinus Torvalds 89996d7303eSSteffen Klassert skb = skb_peek_tail(queue); 90096d7303eSSteffen Klassert 90196d7303eSSteffen Klassert exthdrlen = !skb ? rt->dst.header_len : 0; 90207df5294SHerbert Xu mtu = cork->fragsize; 90309c2d251SWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 90409c2d251SWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 90509c2d251SWillem de Bruijn tskey = sk->sk_tskey++; 9061470ddf7SHerbert Xu 907d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 9101da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 91160ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 9121da177e4SLinus Torvalds 913daba287bSHannes Frederic Sowa if (cork->length + length > maxnonfragsize - fragheaderlen) { 914f5fca608SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 91561e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 9161da177e4SLinus Torvalds return -EMSGSIZE; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds /* 9201da177e4SLinus Torvalds * transhdrlen > 0 means that this is the first fragment and we wish 9211da177e4SLinus Torvalds * it won't be fragmented in the future. 9221da177e4SLinus Torvalds */ 9231da177e4SLinus Torvalds if (transhdrlen && 9241da177e4SLinus Torvalds length + fragheaderlen <= mtu && 925c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) && 926d749c9cbSHannes Frederic Sowa !(flags & MSG_MORE) && 9271da177e4SLinus Torvalds !exthdrlen) 92884fa7933SPatrick McHardy csummode = CHECKSUM_PARTIAL; 9291da177e4SLinus Torvalds 9301470ddf7SHerbert Xu cork->length += length; 93126cde9f7SHerbert Xu if (((length > mtu) || (skb && skb_is_gso(skb))) && 932be9164e7SKostya B (sk->sk_protocol == IPPROTO_UDP) && 933acf8dd0aSMichal Kubeček (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len && 93440ba3302SMichal Kubeček (sk->sk_type == SOCK_DGRAM) && !sk->sk_no_check_tx) { 9351470ddf7SHerbert Xu err = ip_ufo_append_data(sk, queue, getfrag, from, length, 9361470ddf7SHerbert Xu hh_len, fragheaderlen, transhdrlen, 937d9be4f7aSBill Sommerfeld maxfraglen, flags); 938baa829d8SPatrick McHardy if (err) 939e89e9cf5SAnanda Raju goto error; 940e89e9cf5SAnanda Raju return 0; 941e89e9cf5SAnanda Raju } 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* So, what's going on in the loop below? 9441da177e4SLinus Torvalds * 9451da177e4SLinus Torvalds * We use calculated fragment length to generate chained skb, 9461da177e4SLinus Torvalds * each of segments is IP fragment ready for sending to network after 9471da177e4SLinus Torvalds * adding appropriate IP header. 9481da177e4SLinus Torvalds */ 9491da177e4SLinus Torvalds 95026cde9f7SHerbert Xu if (!skb) 9511da177e4SLinus Torvalds goto alloc_new_skb; 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds while (length > 0) { 9541da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 9551da177e4SLinus Torvalds copy = mtu - skb->len; 9561da177e4SLinus Torvalds if (copy < length) 9571da177e4SLinus Torvalds copy = maxfraglen - skb->len; 9581da177e4SLinus Torvalds if (copy <= 0) { 9591da177e4SLinus Torvalds char *data; 9601da177e4SLinus Torvalds unsigned int datalen; 9611da177e4SLinus Torvalds unsigned int fraglen; 9621da177e4SLinus Torvalds unsigned int fraggap; 9631da177e4SLinus Torvalds unsigned int alloclen; 9641da177e4SLinus Torvalds struct sk_buff *skb_prev; 9651da177e4SLinus Torvalds alloc_new_skb: 9661da177e4SLinus Torvalds skb_prev = skb; 9671da177e4SLinus Torvalds if (skb_prev) 9681da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 9691da177e4SLinus Torvalds else 9701da177e4SLinus Torvalds fraggap = 0; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * If remaining data exceeds the mtu, 9741da177e4SLinus Torvalds * we know we need more fragment(s). 9751da177e4SLinus Torvalds */ 9761da177e4SLinus Torvalds datalen = length + fraggap; 9771da177e4SLinus Torvalds if (datalen > mtu - fragheaderlen) 9781da177e4SLinus Torvalds datalen = maxfraglen - fragheaderlen; 9791da177e4SLinus Torvalds fraglen = datalen + fragheaderlen; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds if ((flags & MSG_MORE) && 982d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 9831da177e4SLinus Torvalds alloclen = mtu; 9841da177e4SLinus Torvalds else 98559104f06SEric Dumazet alloclen = fraglen; 9861da177e4SLinus Torvalds 987353e5c9aSSteffen Klassert alloclen += exthdrlen; 988353e5c9aSSteffen Klassert 9891da177e4SLinus Torvalds /* The last fragment gets additional space at tail. 9901da177e4SLinus Torvalds * Note, with MSG_MORE we overallocate on fragments, 9911da177e4SLinus Torvalds * because we have no idea what fragment will be 9921da177e4SLinus Torvalds * the last. 9931da177e4SLinus Torvalds */ 99433f99dc7SSteffen Klassert if (datalen == length + fraggap) 995d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 99633f99dc7SSteffen Klassert 9971da177e4SLinus Torvalds if (transhdrlen) { 9981da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 9991da177e4SLinus Torvalds alloclen + hh_len + 15, 10001da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 10011da177e4SLinus Torvalds } else { 10021da177e4SLinus Torvalds skb = NULL; 10031da177e4SLinus Torvalds if (atomic_read(&sk->sk_wmem_alloc) <= 10041da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 10051da177e4SLinus Torvalds skb = sock_wmalloc(sk, 10061da177e4SLinus Torvalds alloclen + hh_len + 15, 1, 10071da177e4SLinus Torvalds sk->sk_allocation); 100851456b29SIan Morris if (unlikely(!skb)) 10091da177e4SLinus Torvalds err = -ENOBUFS; 10101da177e4SLinus Torvalds } 101151456b29SIan Morris if (!skb) 10121da177e4SLinus Torvalds goto error; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds /* 10151da177e4SLinus Torvalds * Fill in the control structures 10161da177e4SLinus Torvalds */ 10171da177e4SLinus Torvalds skb->ip_summed = csummode; 10181da177e4SLinus Torvalds skb->csum = 0; 10191da177e4SLinus Torvalds skb_reserve(skb, hh_len); 102011878b40SWillem de Bruijn 102111878b40SWillem de Bruijn /* only the initial fragment is time stamped */ 10221470ddf7SHerbert Xu skb_shinfo(skb)->tx_flags = cork->tx_flags; 102311878b40SWillem de Bruijn cork->tx_flags = 0; 102409c2d251SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 102509c2d251SWillem de Bruijn tskey = 0; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds /* 10281da177e4SLinus Torvalds * Find where to start putting bytes. 10291da177e4SLinus Torvalds */ 1030353e5c9aSSteffen Klassert data = skb_put(skb, fraglen + exthdrlen); 1031c14d2450SArnaldo Carvalho de Melo skb_set_network_header(skb, exthdrlen); 1032b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1033b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 1034353e5c9aSSteffen Klassert data += fragheaderlen + exthdrlen; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds if (fraggap) { 10371da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 10381da177e4SLinus Torvalds skb_prev, maxfraglen, 10391da177e4SLinus Torvalds data + transhdrlen, fraggap, 0); 10401da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 10411da177e4SLinus Torvalds skb->csum); 10421da177e4SLinus Torvalds data += fraggap; 1043e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds copy = datalen - transhdrlen - fraggap; 10471da177e4SLinus Torvalds if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { 10481da177e4SLinus Torvalds err = -EFAULT; 10491da177e4SLinus Torvalds kfree_skb(skb); 10501da177e4SLinus Torvalds goto error; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds offset += copy; 10541da177e4SLinus Torvalds length -= datalen - fraggap; 10551da177e4SLinus Torvalds transhdrlen = 0; 10561da177e4SLinus Torvalds exthdrlen = 0; 10571da177e4SLinus Torvalds csummode = CHECKSUM_NONE; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* 10601da177e4SLinus Torvalds * Put the packet on the pending queue. 10611da177e4SLinus Torvalds */ 10621470ddf7SHerbert Xu __skb_queue_tail(queue, skb); 10631da177e4SLinus Torvalds continue; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds if (copy > length) 10671da177e4SLinus Torvalds copy = length; 10681da177e4SLinus Torvalds 1069d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) { 10701da177e4SLinus Torvalds unsigned int off; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds off = skb->len; 10731da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 10741da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 10751da177e4SLinus Torvalds __skb_trim(skb, off); 10761da177e4SLinus Torvalds err = -EFAULT; 10771da177e4SLinus Torvalds goto error; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds } else { 10801da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds err = -ENOMEM; 10835640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 10841da177e4SLinus Torvalds goto error; 10851da177e4SLinus Torvalds 10865640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 10875640f768SEric Dumazet pfrag->offset)) { 10881da177e4SLinus Torvalds err = -EMSGSIZE; 10895640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 10901da177e4SLinus Torvalds goto error; 10915640f768SEric Dumazet 10925640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 10935640f768SEric Dumazet pfrag->offset, 0); 10945640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 10955640f768SEric Dumazet get_page(pfrag->page); 10961da177e4SLinus Torvalds } 10975640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 10985640f768SEric Dumazet if (getfrag(from, 10995640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 11005640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 11015640f768SEric Dumazet goto error_efault; 11025640f768SEric Dumazet 11035640f768SEric Dumazet pfrag->offset += copy; 11045640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 11051da177e4SLinus Torvalds skb->len += copy; 11061da177e4SLinus Torvalds skb->data_len += copy; 1107f945fa7aSHerbert Xu skb->truesize += copy; 1108f945fa7aSHerbert Xu atomic_add(copy, &sk->sk_wmem_alloc); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds offset += copy; 11111da177e4SLinus Torvalds length -= copy; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds return 0; 11151da177e4SLinus Torvalds 11165640f768SEric Dumazet error_efault: 11175640f768SEric Dumazet err = -EFAULT; 11181da177e4SLinus Torvalds error: 11191470ddf7SHerbert Xu cork->length -= length; 11205e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 11211da177e4SLinus Torvalds return err; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, 11251470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp) 11261470ddf7SHerbert Xu { 1127f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 11281470ddf7SHerbert Xu struct rtable *rt; 11291470ddf7SHerbert Xu 11301470ddf7SHerbert Xu /* 11311470ddf7SHerbert Xu * setup for corking. 11321470ddf7SHerbert Xu */ 11331470ddf7SHerbert Xu opt = ipc->opt; 11341470ddf7SHerbert Xu if (opt) { 113551456b29SIan Morris if (!cork->opt) { 11361470ddf7SHerbert Xu cork->opt = kmalloc(sizeof(struct ip_options) + 40, 11371470ddf7SHerbert Xu sk->sk_allocation); 113851456b29SIan Morris if (unlikely(!cork->opt)) 11391470ddf7SHerbert Xu return -ENOBUFS; 11401470ddf7SHerbert Xu } 1141f6d8bd05SEric Dumazet memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); 11421470ddf7SHerbert Xu cork->flags |= IPCORK_OPT; 11431470ddf7SHerbert Xu cork->addr = ipc->addr; 11441470ddf7SHerbert Xu } 11451470ddf7SHerbert Xu rt = *rtp; 11461470ddf7SHerbert Xu if (unlikely(!rt)) 11471470ddf7SHerbert Xu return -EFAULT; 11481470ddf7SHerbert Xu /* 11491470ddf7SHerbert Xu * We steal reference to this route, caller should not release it 11501470ddf7SHerbert Xu */ 11511470ddf7SHerbert Xu *rtp = NULL; 1152482fc609SHannes Frederic Sowa cork->fragsize = ip_sk_use_pmtu(sk) ? 1153482fc609SHannes Frederic Sowa dst_mtu(&rt->dst) : rt->dst.dev->mtu; 11541470ddf7SHerbert Xu cork->dst = &rt->dst; 11551470ddf7SHerbert Xu cork->length = 0; 1156aa661581SFrancesco Fusco cork->ttl = ipc->ttl; 1157aa661581SFrancesco Fusco cork->tos = ipc->tos; 1158aa661581SFrancesco Fusco cork->priority = ipc->priority; 11591470ddf7SHerbert Xu cork->tx_flags = ipc->tx_flags; 11601470ddf7SHerbert Xu 11611470ddf7SHerbert Xu return 0; 11621470ddf7SHerbert Xu } 11631470ddf7SHerbert Xu 11641470ddf7SHerbert Xu /* 11651470ddf7SHerbert Xu * ip_append_data() and ip_append_page() can make one large IP datagram 11661470ddf7SHerbert Xu * from many pieces of data. Each pieces will be holded on the socket 11671470ddf7SHerbert Xu * until ip_push_pending_frames() is called. Each piece can be a page 11681470ddf7SHerbert Xu * or non-page data. 11691470ddf7SHerbert Xu * 11701470ddf7SHerbert Xu * Not only UDP, other transport protocols - e.g. raw sockets - can use 11711470ddf7SHerbert Xu * this interface potentially. 11721470ddf7SHerbert Xu * 11731470ddf7SHerbert Xu * LATER: length must be adjusted by pad at tail, when it is required. 11741470ddf7SHerbert Xu */ 1175f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4, 11761470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, int len, 11771470ddf7SHerbert Xu int odd, struct sk_buff *skb), 11781470ddf7SHerbert Xu void *from, int length, int transhdrlen, 11791470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 11801470ddf7SHerbert Xu unsigned int flags) 11811470ddf7SHerbert Xu { 11821470ddf7SHerbert Xu struct inet_sock *inet = inet_sk(sk); 11831470ddf7SHerbert Xu int err; 11841470ddf7SHerbert Xu 11851470ddf7SHerbert Xu if (flags&MSG_PROBE) 11861470ddf7SHerbert Xu return 0; 11871470ddf7SHerbert Xu 11881470ddf7SHerbert Xu if (skb_queue_empty(&sk->sk_write_queue)) { 1189bdc712b4SDavid S. Miller err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp); 11901470ddf7SHerbert Xu if (err) 11911470ddf7SHerbert Xu return err; 11921470ddf7SHerbert Xu } else { 11931470ddf7SHerbert Xu transhdrlen = 0; 11941470ddf7SHerbert Xu } 11951470ddf7SHerbert Xu 11965640f768SEric Dumazet return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base, 11975640f768SEric Dumazet sk_page_frag(sk), getfrag, 11981470ddf7SHerbert Xu from, length, transhdrlen, flags); 11991470ddf7SHerbert Xu } 12001470ddf7SHerbert Xu 1201f5fca608SDavid S. Miller ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 12021da177e4SLinus Torvalds int offset, size_t size, int flags) 12031da177e4SLinus Torvalds { 12041da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 12051da177e4SLinus Torvalds struct sk_buff *skb; 12061da177e4SLinus Torvalds struct rtable *rt; 12071da177e4SLinus Torvalds struct ip_options *opt = NULL; 1208bdc712b4SDavid S. Miller struct inet_cork *cork; 12091da177e4SLinus Torvalds int hh_len; 12101da177e4SLinus Torvalds int mtu; 12111da177e4SLinus Torvalds int len; 12121da177e4SLinus Torvalds int err; 1213daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds if (inet->hdrincl) 12161da177e4SLinus Torvalds return -EPERM; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (flags&MSG_PROBE) 12191da177e4SLinus Torvalds return 0; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) 12221da177e4SLinus Torvalds return -EINVAL; 12231da177e4SLinus Torvalds 1224bdc712b4SDavid S. Miller cork = &inet->cork.base; 1225bdc712b4SDavid S. Miller rt = (struct rtable *)cork->dst; 1226bdc712b4SDavid S. Miller if (cork->flags & IPCORK_OPT) 1227bdc712b4SDavid S. Miller opt = cork->opt; 12281da177e4SLinus Torvalds 1229d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) 12301da177e4SLinus Torvalds return -EOPNOTSUPP; 12311da177e4SLinus Torvalds 1232d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 1233bdc712b4SDavid S. Miller mtu = cork->fragsize; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 12361da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 123760ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 12381da177e4SLinus Torvalds 1239daba287bSHannes Frederic Sowa if (cork->length + size > maxnonfragsize - fragheaderlen) { 124061e7f09dSHannes Frederic Sowa ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 124161e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 12421da177e4SLinus Torvalds return -EMSGSIZE; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 124551456b29SIan Morris skb = skb_peek_tail(&sk->sk_write_queue); 124651456b29SIan Morris if (!skb) 12471da177e4SLinus Torvalds return -EINVAL; 12481da177e4SLinus Torvalds 124926cde9f7SHerbert Xu if ((size + skb->len > mtu) && 125026cde9f7SHerbert Xu (sk->sk_protocol == IPPROTO_UDP) && 1251d8d1f30bSChangli Gao (rt->dst.dev->features & NETIF_F_UFO)) { 1252a8c4a252SHannes Frederic Sowa if (skb->ip_summed != CHECKSUM_PARTIAL) 1253a8c4a252SHannes Frederic Sowa return -EOPNOTSUPP; 1254a8c4a252SHannes Frederic Sowa 12557967168cSHerbert Xu skb_shinfo(skb)->gso_size = mtu - fragheaderlen; 1256f83ef8c0SHerbert Xu skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 12577967168cSHerbert Xu } 1258a8c4a252SHannes Frederic Sowa cork->length += size; 1259e89e9cf5SAnanda Raju 12601da177e4SLinus Torvalds while (size > 0) { 1261be12a1feSHannes Frederic Sowa if (skb_is_gso(skb)) { 1262e89e9cf5SAnanda Raju len = size; 1263be12a1feSHannes Frederic Sowa } else { 1264e89e9cf5SAnanda Raju 12651da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 12661da177e4SLinus Torvalds len = mtu - skb->len; 12671da177e4SLinus Torvalds if (len < size) 12681da177e4SLinus Torvalds len = maxfraglen - skb->len; 1269e89e9cf5SAnanda Raju } 12701da177e4SLinus Torvalds if (len <= 0) { 12711da177e4SLinus Torvalds struct sk_buff *skb_prev; 12721da177e4SLinus Torvalds int alloclen; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds skb_prev = skb; 12751da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds alloclen = fragheaderlen + hh_len + fraggap + 15; 12781da177e4SLinus Torvalds skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); 12791da177e4SLinus Torvalds if (unlikely(!skb)) { 12801da177e4SLinus Torvalds err = -ENOBUFS; 12811da177e4SLinus Torvalds goto error; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds /* 12851da177e4SLinus Torvalds * Fill in the control structures 12861da177e4SLinus Torvalds */ 12871da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 12881da177e4SLinus Torvalds skb->csum = 0; 12891da177e4SLinus Torvalds skb_reserve(skb, hh_len); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds /* 12921da177e4SLinus Torvalds * Find where to start putting bytes. 12931da177e4SLinus Torvalds */ 1294967b05f6SArnaldo Carvalho de Melo skb_put(skb, fragheaderlen + fraggap); 12952ca9e6f2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1296b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1297b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 12981da177e4SLinus Torvalds if (fraggap) { 1299967b05f6SArnaldo Carvalho de Melo skb->csum = skb_copy_and_csum_bits(skb_prev, 1300967b05f6SArnaldo Carvalho de Melo maxfraglen, 13019c70220bSArnaldo Carvalho de Melo skb_transport_header(skb), 1302967b05f6SArnaldo Carvalho de Melo fraggap, 0); 13031da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 13041da177e4SLinus Torvalds skb->csum); 1305e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds /* 13091da177e4SLinus Torvalds * Put the packet on the pending queue. 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 13121da177e4SLinus Torvalds continue; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds if (len > size) 13161da177e4SLinus Torvalds len = size; 1317be12a1feSHannes Frederic Sowa 1318be12a1feSHannes Frederic Sowa if (skb_append_pagefrags(skb, page, offset, len)) { 13191da177e4SLinus Torvalds err = -EMSGSIZE; 13201da177e4SLinus Torvalds goto error; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds if (skb->ip_summed == CHECKSUM_NONE) { 132444bb9363SAl Viro __wsum csum; 13251da177e4SLinus Torvalds csum = csum_page(page, offset, len); 13261da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, skb->len); 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds skb->len += len; 13301da177e4SLinus Torvalds skb->data_len += len; 13311e34a11dSDavid S. Miller skb->truesize += len; 13321e34a11dSDavid S. Miller atomic_add(len, &sk->sk_wmem_alloc); 13331da177e4SLinus Torvalds offset += len; 13341da177e4SLinus Torvalds size -= len; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds return 0; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds error: 1339bdc712b4SDavid S. Miller cork->length -= size; 13405e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 13411da177e4SLinus Torvalds return err; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork) 1345429f08e9SPavel Emelyanov { 13461470ddf7SHerbert Xu cork->flags &= ~IPCORK_OPT; 13471470ddf7SHerbert Xu kfree(cork->opt); 13481470ddf7SHerbert Xu cork->opt = NULL; 13491470ddf7SHerbert Xu dst_release(cork->dst); 13501470ddf7SHerbert Xu cork->dst = NULL; 1351429f08e9SPavel Emelyanov } 1352429f08e9SPavel Emelyanov 13531da177e4SLinus Torvalds /* 13541da177e4SLinus Torvalds * Combined all pending IP fragments on the socket as one IP datagram 13551da177e4SLinus Torvalds * and push them out. 13561da177e4SLinus Torvalds */ 13571c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk, 135877968b78SDavid S. Miller struct flowi4 *fl4, 13591470ddf7SHerbert Xu struct sk_buff_head *queue, 13601470ddf7SHerbert Xu struct inet_cork *cork) 13611da177e4SLinus Torvalds { 13621da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 13631da177e4SLinus Torvalds struct sk_buff **tail_skb; 13641da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 13650388b004SPavel Emelyanov struct net *net = sock_net(sk); 13661da177e4SLinus Torvalds struct ip_options *opt = NULL; 13671470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 13681da177e4SLinus Torvalds struct iphdr *iph; 136976ab608dSAlexey Dobriyan __be16 df = 0; 13701da177e4SLinus Torvalds __u8 ttl; 13711da177e4SLinus Torvalds 137251456b29SIan Morris skb = __skb_dequeue(queue); 137351456b29SIan Morris if (!skb) 13741da177e4SLinus Torvalds goto out; 13751da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1378d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1379bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 13801470ddf7SHerbert Xu while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1381cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 13821da177e4SLinus Torvalds *tail_skb = tmp_skb; 13831da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 13841da177e4SLinus Torvalds skb->len += tmp_skb->len; 13851da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 13861da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 13871da177e4SLinus Torvalds tmp_skb->destructor = NULL; 13881da177e4SLinus Torvalds tmp_skb->sk = NULL; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow 13921da177e4SLinus Torvalds * to fragment the frame generated here. No matter, what transforms 13931da177e4SLinus Torvalds * how transforms change size of the packet, it will come out. 13941da177e4SLinus Torvalds */ 139560ff7467SWANG Cong skb->ignore_df = ip_sk_ignore_df(sk); 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds /* DF bit is set when we want to see DF on outgoing frames. 139860ff7467SWANG Cong * If ignore_df is set too, we still allow to fragment this frame 13991da177e4SLinus Torvalds * locally. */ 1400482fc609SHannes Frederic Sowa if (inet->pmtudisc == IP_PMTUDISC_DO || 1401482fc609SHannes Frederic Sowa inet->pmtudisc == IP_PMTUDISC_PROBE || 1402d8d1f30bSChangli Gao (skb->len <= dst_mtu(&rt->dst) && 1403d8d1f30bSChangli Gao ip_dont_fragment(sk, &rt->dst))) 14041da177e4SLinus Torvalds df = htons(IP_DF); 14051da177e4SLinus Torvalds 14061470ddf7SHerbert Xu if (cork->flags & IPCORK_OPT) 14071470ddf7SHerbert Xu opt = cork->opt; 14081da177e4SLinus Torvalds 1409aa661581SFrancesco Fusco if (cork->ttl != 0) 1410aa661581SFrancesco Fusco ttl = cork->ttl; 1411aa661581SFrancesco Fusco else if (rt->rt_type == RTN_MULTICAST) 14121da177e4SLinus Torvalds ttl = inet->mc_ttl; 14131da177e4SLinus Torvalds else 1414d8d1f30bSChangli Gao ttl = ip_select_ttl(inet, &rt->dst); 14151da177e4SLinus Torvalds 1416749154aaSAnsis Atteka iph = ip_hdr(skb); 14171da177e4SLinus Torvalds iph->version = 4; 14181da177e4SLinus Torvalds iph->ihl = 5; 1419aa661581SFrancesco Fusco iph->tos = (cork->tos != -1) ? cork->tos : inet->tos; 14201da177e4SLinus Torvalds iph->frag_off = df; 14211da177e4SLinus Torvalds iph->ttl = ttl; 14221da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 142384f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 1424b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, sk); 14251da177e4SLinus Torvalds 142622f728f8SDavid S. Miller if (opt) { 142722f728f8SDavid S. Miller iph->ihl += opt->optlen>>2; 142822f728f8SDavid S. Miller ip_options_build(skb, opt, cork->addr, rt, 0); 142922f728f8SDavid S. Miller } 143022f728f8SDavid S. Miller 1431aa661581SFrancesco Fusco skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority; 14324a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1433a21bba94SEric Dumazet /* 1434a21bba94SEric Dumazet * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1435a21bba94SEric Dumazet * on dst refcount 1436a21bba94SEric Dumazet */ 14371470ddf7SHerbert Xu cork->dst = NULL; 1438d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 14391da177e4SLinus Torvalds 144096793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 14410388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 144296793b48SDavid L Stevens skb_transport_header(skb))->type); 144396793b48SDavid L Stevens 14441c32c5adSHerbert Xu ip_cork_release(cork); 14451c32c5adSHerbert Xu out: 14461c32c5adSHerbert Xu return skb; 14471c32c5adSHerbert Xu } 14481c32c5adSHerbert Xu 1449b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb) 14501c32c5adSHerbert Xu { 14511c32c5adSHerbert Xu int err; 14521c32c5adSHerbert Xu 145333224b16SEric W. Biederman err = ip_local_out(net, skb->sk, skb); 14541da177e4SLinus Torvalds if (err) { 14551da177e4SLinus Torvalds if (err > 0) 14566ce9e7b5SEric Dumazet err = net_xmit_errno(err); 14571da177e4SLinus Torvalds if (err) 14581c32c5adSHerbert Xu IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds return err; 14621da177e4SLinus Torvalds } 14631da177e4SLinus Torvalds 146477968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4) 14651470ddf7SHerbert Xu { 14661c32c5adSHerbert Xu struct sk_buff *skb; 14671c32c5adSHerbert Xu 146877968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 14691c32c5adSHerbert Xu if (!skb) 14701c32c5adSHerbert Xu return 0; 14711c32c5adSHerbert Xu 14721c32c5adSHerbert Xu /* Netfilter gets whole the not fragmented skb. */ 1473b5ec8eeaSEric Dumazet return ip_send_skb(sock_net(sk), skb); 14741470ddf7SHerbert Xu } 14751470ddf7SHerbert Xu 14761da177e4SLinus Torvalds /* 14771da177e4SLinus Torvalds * Throw away all pending data on the socket. 14781da177e4SLinus Torvalds */ 14791470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk, 14801470ddf7SHerbert Xu struct sk_buff_head *queue, 14811470ddf7SHerbert Xu struct inet_cork *cork) 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds struct sk_buff *skb; 14841da177e4SLinus Torvalds 14851470ddf7SHerbert Xu while ((skb = __skb_dequeue_tail(queue)) != NULL) 14861da177e4SLinus Torvalds kfree_skb(skb); 14871da177e4SLinus Torvalds 14881470ddf7SHerbert Xu ip_cork_release(cork); 14891470ddf7SHerbert Xu } 14901470ddf7SHerbert Xu 14911470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk) 14921470ddf7SHerbert Xu { 1493bdc712b4SDavid S. Miller __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 14961c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk, 149777968b78SDavid S. Miller struct flowi4 *fl4, 14981c32c5adSHerbert Xu int getfrag(void *from, char *to, int offset, 14991c32c5adSHerbert Xu int len, int odd, struct sk_buff *skb), 15001c32c5adSHerbert Xu void *from, int length, int transhdrlen, 15011c32c5adSHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 15021c32c5adSHerbert Xu unsigned int flags) 15031c32c5adSHerbert Xu { 1504b80d7226SDavid S. Miller struct inet_cork cork; 15051c32c5adSHerbert Xu struct sk_buff_head queue; 15061c32c5adSHerbert Xu int err; 15071c32c5adSHerbert Xu 15081c32c5adSHerbert Xu if (flags & MSG_PROBE) 15091c32c5adSHerbert Xu return NULL; 15101c32c5adSHerbert Xu 15111c32c5adSHerbert Xu __skb_queue_head_init(&queue); 15121c32c5adSHerbert Xu 1513b80d7226SDavid S. Miller cork.flags = 0; 1514b80d7226SDavid S. Miller cork.addr = 0; 151570652728SDavid S. Miller cork.opt = NULL; 15161c32c5adSHerbert Xu err = ip_setup_cork(sk, &cork, ipc, rtp); 15171c32c5adSHerbert Xu if (err) 15181c32c5adSHerbert Xu return ERR_PTR(err); 15191c32c5adSHerbert Xu 15205640f768SEric Dumazet err = __ip_append_data(sk, fl4, &queue, &cork, 15215640f768SEric Dumazet ¤t->task_frag, getfrag, 15221c32c5adSHerbert Xu from, length, transhdrlen, flags); 15231c32c5adSHerbert Xu if (err) { 15241c32c5adSHerbert Xu __ip_flush_pending_frames(sk, &queue, &cork); 15251c32c5adSHerbert Xu return ERR_PTR(err); 15261c32c5adSHerbert Xu } 15271c32c5adSHerbert Xu 152877968b78SDavid S. Miller return __ip_make_skb(sk, fl4, &queue, &cork); 15291c32c5adSHerbert Xu } 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /* 15321da177e4SLinus Torvalds * Fetch data from kernel space and fill in checksum if needed. 15331da177e4SLinus Torvalds */ 15341da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset, 15351da177e4SLinus Torvalds int len, int odd, struct sk_buff *skb) 15361da177e4SLinus Torvalds { 15375084205fSAl Viro __wsum csum; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0); 15401da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 15411da177e4SLinus Torvalds return 0; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds /* 15451da177e4SLinus Torvalds * Generic function to send a packet as reply to another packet. 1546be9f4a44SEric Dumazet * Used to send some TCP resets/acks so far. 15471da177e4SLinus Torvalds */ 1548bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, 154924a2d43dSEric Dumazet const struct ip_options *sopt, 155024a2d43dSEric Dumazet __be32 daddr, __be32 saddr, 155124a2d43dSEric Dumazet const struct ip_reply_arg *arg, 155270e73416SDavid S. Miller unsigned int len) 15531da177e4SLinus Torvalds { 1554f6d8bd05SEric Dumazet struct ip_options_data replyopts; 15551da177e4SLinus Torvalds struct ipcm_cookie ipc; 155677968b78SDavid S. Miller struct flowi4 fl4; 1557511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1558bdbbb852SEric Dumazet struct net *net = sock_net(sk); 1559be9f4a44SEric Dumazet struct sk_buff *nskb; 15604062090eSVasily Averin int err; 1561f7ba868bSDavid Ahern int oif; 15621da177e4SLinus Torvalds 156324a2d43dSEric Dumazet if (__ip_options_echo(&replyopts.opt.opt, skb, sopt)) 15641da177e4SLinus Torvalds return; 15651da177e4SLinus Torvalds 15660a5ebb80SDavid S. Miller ipc.addr = daddr; 15671da177e4SLinus Torvalds ipc.opt = NULL; 15682244d07bSOliver Hartkopp ipc.tx_flags = 0; 1569aa661581SFrancesco Fusco ipc.ttl = 0; 1570aa661581SFrancesco Fusco ipc.tos = -1; 15711da177e4SLinus Torvalds 1572f6d8bd05SEric Dumazet if (replyopts.opt.opt.optlen) { 15731da177e4SLinus Torvalds ipc.opt = &replyopts.opt; 15741da177e4SLinus Torvalds 1575f6d8bd05SEric Dumazet if (replyopts.opt.opt.srr) 1576f6d8bd05SEric Dumazet daddr = replyopts.opt.opt.faddr; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 1579f7ba868bSDavid Ahern oif = arg->bound_dev_if; 1580e0d56fddSDavid Ahern oif = oif ? : skb->skb_iif; 1581f7ba868bSDavid Ahern 1582f7ba868bSDavid Ahern flowi4_init_output(&fl4, oif, 1583e110861fSLorenzo Colitti IP4_REPLY_MARK(net, skb->mark), 158466b13d99SEric Dumazet RT_TOS(arg->tos), 1585be9f4a44SEric Dumazet RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol, 1586538de0e0SDavid S. Miller ip_reply_arg_flowi_flags(arg), 158770e73416SDavid S. Miller daddr, saddr, 1588538de0e0SDavid S. Miller tcp_hdr(skb)->source, tcp_hdr(skb)->dest); 15899d6ec938SDavid S. Miller security_skb_classify_flow(skb, flowi4_to_flowi(&fl4)); 1590be9f4a44SEric Dumazet rt = ip_route_output_key(net, &fl4); 1591b23dd4feSDavid S. Miller if (IS_ERR(rt)) 15921da177e4SLinus Torvalds return; 15931da177e4SLinus Torvalds 1594bdbbb852SEric Dumazet inet_sk(sk)->tos = arg->tos; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds sk->sk_priority = skb->priority; 1597eddc9ec5SArnaldo Carvalho de Melo sk->sk_protocol = ip_hdr(skb)->protocol; 1598f0e48dbfSPatrick McHardy sk->sk_bound_dev_if = arg->bound_dev_if; 1599be9f4a44SEric Dumazet sk->sk_sndbuf = sysctl_wmem_default; 16004062090eSVasily Averin err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, 16014062090eSVasily Averin len, 0, &ipc, &rt, MSG_DONTWAIT); 16024062090eSVasily Averin if (unlikely(err)) { 16034062090eSVasily Averin ip_flush_pending_frames(sk); 16044062090eSVasily Averin goto out; 16054062090eSVasily Averin } 16064062090eSVasily Averin 1607be9f4a44SEric Dumazet nskb = skb_peek(&sk->sk_write_queue); 1608be9f4a44SEric Dumazet if (nskb) { 16091da177e4SLinus Torvalds if (arg->csumoffset >= 0) 1610be9f4a44SEric Dumazet *((__sum16 *)skb_transport_header(nskb) + 1611be9f4a44SEric Dumazet arg->csumoffset) = csum_fold(csum_add(nskb->csum, 16129c70220bSArnaldo Carvalho de Melo arg->csum)); 1613be9f4a44SEric Dumazet nskb->ip_summed = CHECKSUM_NONE; 161477968b78SDavid S. Miller ip_push_pending_frames(sk, &fl4); 16151da177e4SLinus Torvalds } 16164062090eSVasily Averin out: 16171da177e4SLinus Torvalds ip_rt_put(rt); 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds void __init ip_init(void) 16211da177e4SLinus Torvalds { 16221da177e4SLinus Torvalds ip_rt_init(); 16231da177e4SLinus Torvalds inet_initpeers(); 16241da177e4SLinus Torvalds 162572c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST) 162672c1d3bdSWANG Cong igmp_mc_init(); 16271da177e4SLinus Torvalds #endif 16281da177e4SLinus Torvalds } 1629