1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The Internet Protocol (IP) output module. 81da177e4SLinus Torvalds * 902c30a84SJesper Juhl * Authors: Ross Biro 101da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 111da177e4SLinus Torvalds * Donald Becker, <becker@super.org> 121da177e4SLinus Torvalds * Alan Cox, <Alan.Cox@linux.org> 131da177e4SLinus Torvalds * Richard Underwood 141da177e4SLinus Torvalds * Stefan Becker, <stefanb@yello.ping.de> 151da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 161da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 171da177e4SLinus Torvalds * Hirokazu Takahashi, <taka@valinux.co.jp> 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * See ip_input.c for original log 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Fixes: 221da177e4SLinus Torvalds * Alan Cox : Missing nonblock feature in ip_build_xmit. 231da177e4SLinus Torvalds * Mike Kilburn : htons() missing in ip_build_xmit. 241da177e4SLinus Torvalds * Bradford Johnson: Fix faulty handling of some frames when 251da177e4SLinus Torvalds * no route is found. 261da177e4SLinus Torvalds * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit 271da177e4SLinus Torvalds * (in case if packet not accepted by 281da177e4SLinus Torvalds * output firewall rules) 291da177e4SLinus Torvalds * Mike McLagan : Routing by source 301da177e4SLinus Torvalds * Alexey Kuznetsov: use new route cache 311da177e4SLinus Torvalds * Andi Kleen: Fix broken PMTU recovery and remove 321da177e4SLinus Torvalds * some redundant tests. 331da177e4SLinus Torvalds * Vitaly E. Lavrov : Transparent proxy revived after year coma. 341da177e4SLinus Torvalds * Andi Kleen : Replace ip_reply with ip_send_reply. 351da177e4SLinus Torvalds * Andi Kleen : Split fast and slow ip_build_xmit path 361da177e4SLinus Torvalds * for decreased register pressure on x86 37a66e04ceSBhaskar Chowdhury * and more readability. 381da177e4SLinus Torvalds * Marc Boucher : When call_out_firewall returns FW_QUEUE, 391da177e4SLinus Torvalds * silently drop skb instead of failing with -EPERM. 401da177e4SLinus Torvalds * Detlev Wengorz : Copy protocol for fragments. 411da177e4SLinus Torvalds * Hirokazu Takahashi: HW checksumming for outgoing UDP 421da177e4SLinus Torvalds * datagrams. 431da177e4SLinus Torvalds * Hirokazu Takahashi: sendfile() on UDP works now. 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds 467c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 471da177e4SLinus Torvalds #include <linux/module.h> 481da177e4SLinus Torvalds #include <linux/types.h> 491da177e4SLinus Torvalds #include <linux/kernel.h> 501da177e4SLinus Torvalds #include <linux/mm.h> 511da177e4SLinus Torvalds #include <linux/string.h> 521da177e4SLinus Torvalds #include <linux/errno.h> 53a1f8e7f7SAl Viro #include <linux/highmem.h> 545a0e3ad6STejun Heo #include <linux/slab.h> 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include <linux/socket.h> 571da177e4SLinus Torvalds #include <linux/sockios.h> 581da177e4SLinus Torvalds #include <linux/in.h> 591da177e4SLinus Torvalds #include <linux/inet.h> 601da177e4SLinus Torvalds #include <linux/netdevice.h> 611da177e4SLinus Torvalds #include <linux/etherdevice.h> 621da177e4SLinus Torvalds #include <linux/proc_fs.h> 631da177e4SLinus Torvalds #include <linux/stat.h> 641da177e4SLinus Torvalds #include <linux/init.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #include <net/snmp.h> 671da177e4SLinus Torvalds #include <net/ip.h> 681da177e4SLinus Torvalds #include <net/protocol.h> 691da177e4SLinus Torvalds #include <net/route.h> 70cfacb057SPatrick McHardy #include <net/xfrm.h> 711da177e4SLinus Torvalds #include <linux/skbuff.h> 721da177e4SLinus Torvalds #include <net/sock.h> 731da177e4SLinus Torvalds #include <net/arp.h> 741da177e4SLinus Torvalds #include <net/icmp.h> 751da177e4SLinus Torvalds #include <net/checksum.h> 761da177e4SLinus Torvalds #include <net/inetpeer.h> 77ba9e04a7SWei Wang #include <net/inet_ecn.h> 7814972cbdSRoopa Prabhu #include <net/lwtunnel.h> 7933b48679SDaniel Mack #include <linux/bpf-cgroup.h> 801da177e4SLinus Torvalds #include <linux/igmp.h> 811da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 821da177e4SLinus Torvalds #include <linux/netfilter_bridge.h> 831da177e4SLinus Torvalds #include <linux/netlink.h> 846cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h> 851da177e4SLinus Torvalds 86694869b3SEric W. Biederman static int 87694869b3SEric W. Biederman ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 88c5501eb3SFlorian Westphal unsigned int mtu, 89694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)); 9049d16b23SAndy Zhou 911da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */ 922fbd9679SDenis Efremov void ip_send_check(struct iphdr *iph) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds iph->check = 0; 951da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 961da177e4SLinus Torvalds } 974bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check); 981da177e4SLinus Torvalds 99cf91a99dSEric W. Biederman int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 100c439cb2eSHerbert Xu { 101c439cb2eSHerbert Xu struct iphdr *iph = ip_hdr(skb); 102c439cb2eSHerbert Xu 103c439cb2eSHerbert Xu iph->tot_len = htons(skb->len); 104c439cb2eSHerbert Xu ip_send_check(iph); 105a8e3e1a9SDavid Ahern 106a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 107a8e3e1a9SDavid Ahern * skb to its handler for processing 108a8e3e1a9SDavid Ahern */ 109a8e3e1a9SDavid Ahern skb = l3mdev_ip_out(sk, skb); 110a8e3e1a9SDavid Ahern if (unlikely(!skb)) 111a8e3e1a9SDavid Ahern return 0; 112a8e3e1a9SDavid Ahern 113f4180439SEli Cooper skb->protocol = htons(ETH_P_IP); 114f4180439SEli Cooper 11529a26a56SEric W. Biederman return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, 11629a26a56SEric W. Biederman net, sk, skb, NULL, skb_dst(skb)->dev, 11713206b6bSEric W. Biederman dst_output); 1187026b1ddSDavid Miller } 1197026b1ddSDavid Miller 12033224b16SEric W. Biederman int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 121c439cb2eSHerbert Xu { 122c439cb2eSHerbert Xu int err; 123c439cb2eSHerbert Xu 124cf91a99dSEric W. Biederman err = __ip_local_out(net, sk, skb); 125c439cb2eSHerbert Xu if (likely(err == 1)) 12613206b6bSEric W. Biederman err = dst_output(net, sk, skb); 127c439cb2eSHerbert Xu 128c439cb2eSHerbert Xu return err; 129c439cb2eSHerbert Xu } 130e2cb77dbSEric W. Biederman EXPORT_SYMBOL_GPL(ip_local_out); 131c439cb2eSHerbert Xu 1321da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds int ttl = inet->uc_ttl; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds if (ttl < 0) 137323e126fSDavid S. Miller ttl = ip4_dst_hoplimit(dst); 1381da177e4SLinus Torvalds return ttl; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * Add an ip header to a skbuff and send it out. 1431da177e4SLinus Torvalds * 1441da177e4SLinus Torvalds */ 145cfe673b0SEric Dumazet int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, 146de033b7dSWei Wang __be32 saddr, __be32 daddr, struct ip_options_rcu *opt, 147de033b7dSWei Wang u8 tos) 1481da177e4SLinus Torvalds { 1491da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 150511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 15177589ce0SEric W. Biederman struct net *net = sock_net(sk); 1521da177e4SLinus Torvalds struct iphdr *iph; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* Build the IP header. */ 155f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0)); 1568856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 157eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1581da177e4SLinus Torvalds iph->version = 4; 1591da177e4SLinus Torvalds iph->ihl = 5; 160de033b7dSWei Wang iph->tos = tos; 161d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 162dd927a26SDavid S. Miller iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); 163dd927a26SDavid S. Miller iph->saddr = saddr; 1641da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 165cfe673b0SEric Dumazet if (ip_dont_fragment(sk, &rt->dst)) { 166cfe673b0SEric Dumazet iph->frag_off = htons(IP_DF); 167cfe673b0SEric Dumazet iph->id = 0; 168cfe673b0SEric Dumazet } else { 169cfe673b0SEric Dumazet iph->frag_off = 0; 17077589ce0SEric W. Biederman __ip_select_ident(net, iph, 1); 171cfe673b0SEric Dumazet } 1721da177e4SLinus Torvalds 173f6d8bd05SEric Dumazet if (opt && opt->opt.optlen) { 174f6d8bd05SEric Dumazet iph->ihl += opt->opt.optlen>>2; 175f6d8bd05SEric Dumazet ip_options_build(skb, &opt->opt, daddr, rt, 0); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds skb->priority = sk->sk_priority; 179e05a90ecSJamal Hadi Salim if (!skb->mark) 1804a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* Send it out. */ 18333224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 1841da177e4SLinus Torvalds } 185d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 186d8c97a94SArnaldo Carvalho de Melo 187694869b3SEric W. Biederman static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 1881da177e4SLinus Torvalds { 189adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 19080787ebcSMitsuru Chinen struct rtable *rt = (struct rtable *)dst; 1911da177e4SLinus Torvalds struct net_device *dev = dst->dev; 192c2636b4dSChuck Lever unsigned int hh_len = LL_RESERVED_SPACE(dev); 193f6b72b62SDavid S. Miller struct neighbour *neigh; 1945c9f7c1dSDavid Ahern bool is_v6gw = false; 1951da177e4SLinus Torvalds 196edf391ffSNeil Horman if (rt->rt_type == RTN_MULTICAST) { 1974ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len); 198edf391ffSNeil Horman } else if (rt->rt_type == RTN_BROADCAST) 1994ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len); 20080787ebcSMitsuru Chinen 2013b04dddeSStephen Hemminger if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 2025678a595SVasily Averin skb = skb_expand_head(skb, hh_len); 2035678a595SVasily Averin if (!skb) 2041da177e4SLinus Torvalds return -ENOMEM; 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 20714972cbdSRoopa Prabhu if (lwtunnel_xmit_redirect(dst->lwtstate)) { 20814972cbdSRoopa Prabhu int res = lwtunnel_xmit(skb); 20914972cbdSRoopa Prabhu 21014972cbdSRoopa Prabhu if (res < 0 || res == LWTUNNEL_XMIT_DONE) 21114972cbdSRoopa Prabhu return res; 21214972cbdSRoopa Prabhu } 21314972cbdSRoopa Prabhu 214a263b309SDavid S. Miller rcu_read_lock_bh(); 2155c9f7c1dSDavid Ahern neigh = ip_neigh_for_gw(rt, skb, &is_v6gw); 2169871f1adSVasiliy Kulikov if (!IS_ERR(neigh)) { 2174ff06203SJulian Anastasov int res; 2184ff06203SJulian Anastasov 2194ff06203SJulian Anastasov sock_confirm_neigh(skb, neigh); 2205c9f7c1dSDavid Ahern /* if crossing protocols, can not use the cached header */ 2215c9f7c1dSDavid Ahern res = neigh_output(neigh, skb, is_v6gw); 222a263b309SDavid S. Miller rcu_read_unlock_bh(); 223f2c31e32SEric Dumazet return res; 224f2c31e32SEric Dumazet } 225a263b309SDavid S. Miller rcu_read_unlock_bh(); 22605e3aa09SDavid S. Miller 227e87cc472SJoe Perches net_dbg_ratelimited("%s: No header cache and no neighbour!\n", 228e87cc472SJoe Perches __func__); 2291da177e4SLinus Torvalds kfree_skb(skb); 2301da177e4SLinus Torvalds return -EINVAL; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 233694869b3SEric W. Biederman static int ip_finish_output_gso(struct net *net, struct sock *sk, 234694869b3SEric W. Biederman struct sk_buff *skb, unsigned int mtu) 235c7ba65d7SFlorian Westphal { 23688bebdf5SJason A. Donenfeld struct sk_buff *segs, *nskb; 237c7ba65d7SFlorian Westphal netdev_features_t features; 238c7ba65d7SFlorian Westphal int ret = 0; 239c7ba65d7SFlorian Westphal 2409ee6c5dcSLance Richardson /* common case: seglen is <= mtu 241359ebda2SShmulik Ladkani */ 242779b7931SDaniel Axtens if (skb_gso_validate_network_len(skb, mtu)) 243694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 244c7ba65d7SFlorian Westphal 2450ace81ecSLance Richardson /* Slowpath - GSO segment length exceeds the egress MTU. 246c7ba65d7SFlorian Westphal * 2470ace81ecSLance Richardson * This can happen in several cases: 2480ace81ecSLance Richardson * - Forwarding of a TCP GRO skb, when DF flag is not set. 2490ace81ecSLance Richardson * - Forwarding of an skb that arrived on a virtualization interface 2500ace81ecSLance Richardson * (virtio-net/vhost/tap) with TSO/GSO size set by other network 2510ace81ecSLance Richardson * stack. 2520ace81ecSLance Richardson * - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an 2530ace81ecSLance Richardson * interface with a smaller MTU. 2540ace81ecSLance Richardson * - Arriving GRO skb (or GSO skb in a virtualized environment) that is 2550ace81ecSLance Richardson * bridged to a NETIF_F_TSO tunnel stacked over an interface with an 256a66e04ceSBhaskar Chowdhury * insufficient MTU. 257c7ba65d7SFlorian Westphal */ 258c7ba65d7SFlorian Westphal features = netif_skb_features(skb); 259a08e7fd9SCambda Zhu BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET); 260c7ba65d7SFlorian Westphal segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 261330966e5SFlorian Westphal if (IS_ERR_OR_NULL(segs)) { 262c7ba65d7SFlorian Westphal kfree_skb(skb); 263c7ba65d7SFlorian Westphal return -ENOMEM; 264c7ba65d7SFlorian Westphal } 265c7ba65d7SFlorian Westphal 266c7ba65d7SFlorian Westphal consume_skb(skb); 267c7ba65d7SFlorian Westphal 26888bebdf5SJason A. Donenfeld skb_list_walk_safe(segs, segs, nskb) { 269c7ba65d7SFlorian Westphal int err; 270c7ba65d7SFlorian Westphal 271a8305bffSDavid S. Miller skb_mark_not_on_list(segs); 272694869b3SEric W. Biederman err = ip_fragment(net, sk, segs, mtu, ip_finish_output2); 273c7ba65d7SFlorian Westphal 274c7ba65d7SFlorian Westphal if (err && ret == 0) 275c7ba65d7SFlorian Westphal ret = err; 27688bebdf5SJason A. Donenfeld } 277c7ba65d7SFlorian Westphal 278c7ba65d7SFlorian Westphal return ret; 279c7ba65d7SFlorian Westphal } 280c7ba65d7SFlorian Westphal 281956fe219Sbrakmo static int __ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 2821da177e4SLinus Torvalds { 283c5501eb3SFlorian Westphal unsigned int mtu; 284c5501eb3SFlorian Westphal 2855c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 2865c901daaSPatrick McHardy /* Policy lookup after SNAT yielded a new policy */ 28700db4124SIan Morris if (skb_dst(skb)->xfrm) { 28848d5cad8SPatrick McHardy IPCB(skb)->flags |= IPSKB_REROUTED; 28913206b6bSEric W. Biederman return dst_output(net, sk, skb); 29048d5cad8SPatrick McHardy } 2915c901daaSPatrick McHardy #endif 292fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 293c7ba65d7SFlorian Westphal if (skb_is_gso(skb)) 294694869b3SEric W. Biederman return ip_finish_output_gso(net, sk, skb, mtu); 295c7ba65d7SFlorian Westphal 296bb4cc1a1SFlorian Westphal if (skb->len > mtu || IPCB(skb)->frag_max_size) 297694869b3SEric W. Biederman return ip_fragment(net, sk, skb, mtu, ip_finish_output2); 298c7ba65d7SFlorian Westphal 299694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 302956fe219Sbrakmo static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 303956fe219Sbrakmo { 304956fe219Sbrakmo int ret; 305956fe219Sbrakmo 306956fe219Sbrakmo ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 307956fe219Sbrakmo switch (ret) { 308956fe219Sbrakmo case NET_XMIT_SUCCESS: 309956fe219Sbrakmo return __ip_finish_output(net, sk, skb); 310956fe219Sbrakmo case NET_XMIT_CN: 311956fe219Sbrakmo return __ip_finish_output(net, sk, skb) ? : ret; 312956fe219Sbrakmo default: 313956fe219Sbrakmo kfree_skb(skb); 314956fe219Sbrakmo return ret; 315956fe219Sbrakmo } 316956fe219Sbrakmo } 317956fe219Sbrakmo 31833b48679SDaniel Mack static int ip_mc_finish_output(struct net *net, struct sock *sk, 31933b48679SDaniel Mack struct sk_buff *skb) 32033b48679SDaniel Mack { 3215b18f128SStephen Suryaputra struct rtable *new_rt; 322d96ff269SDavid S. Miller bool do_cn = false; 323d96ff269SDavid S. Miller int ret, err; 32433b48679SDaniel Mack 32533b48679SDaniel Mack ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 326956fe219Sbrakmo switch (ret) { 327956fe219Sbrakmo case NET_XMIT_CN: 328d96ff269SDavid S. Miller do_cn = true; 329a8eceea8SJoe Perches fallthrough; 330d96ff269SDavid S. Miller case NET_XMIT_SUCCESS: 331d96ff269SDavid S. Miller break; 332956fe219Sbrakmo default: 33333b48679SDaniel Mack kfree_skb(skb); 33433b48679SDaniel Mack return ret; 33533b48679SDaniel Mack } 33633b48679SDaniel Mack 3375b18f128SStephen Suryaputra /* Reset rt_iif so that inet_iif() will return skb->skb_iif. Setting 3385b18f128SStephen Suryaputra * this to non-zero causes ipi_ifindex in in_pktinfo to be overwritten, 3395b18f128SStephen Suryaputra * see ipv4_pktinfo_prepare(). 3405b18f128SStephen Suryaputra */ 3415b18f128SStephen Suryaputra new_rt = rt_dst_clone(net->loopback_dev, skb_rtable(skb)); 3425b18f128SStephen Suryaputra if (new_rt) { 3435b18f128SStephen Suryaputra new_rt->rt_iif = 0; 3445b18f128SStephen Suryaputra skb_dst_drop(skb); 3455b18f128SStephen Suryaputra skb_dst_set(skb, &new_rt->dst); 3465b18f128SStephen Suryaputra } 3475b18f128SStephen Suryaputra 348d96ff269SDavid S. Miller err = dev_loopback_xmit(net, sk, skb); 349d96ff269SDavid S. Miller return (do_cn && err) ? ret : err; 35033b48679SDaniel Mack } 35133b48679SDaniel Mack 352ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb) 3531da177e4SLinus Torvalds { 354511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 355d8d1f30bSChangli Gao struct net_device *dev = rt->dst.dev; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * If the indicated interface is up and running, send the packet. 3591da177e4SLinus Torvalds */ 36088f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds skb->dev = dev; 3631da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /* 3661da177e4SLinus Torvalds * Multicasts are looped back for other local users 3671da177e4SLinus Torvalds */ 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (rt->rt_flags&RTCF_MULTICAST) { 3707ad6848cSOctavian Purdila if (sk_mc_loop(sk) 3711da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 3721da177e4SLinus Torvalds /* Small optimization: do not loopback not local frames, 3731da177e4SLinus Torvalds which returned after forwarding; they will be dropped 3741da177e4SLinus Torvalds by ip_mr_input in any case. 3751da177e4SLinus Torvalds Note, that local frames are looped back to be delivered 3761da177e4SLinus Torvalds to local recipients. 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds This check is duplicated in ip_mr_input at the moment. 3791da177e4SLinus Torvalds */ 3809d4fb27dSJoe Perches && 3819d4fb27dSJoe Perches ((rt->rt_flags & RTCF_LOCAL) || 3829d4fb27dSJoe Perches !(IPCB(skb)->flags & IPSKB_FORWARDED)) 3831da177e4SLinus Torvalds #endif 3841da177e4SLinus Torvalds ) { 3851da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 3861da177e4SLinus Torvalds if (newskb) 3879bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 38829a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 38933b48679SDaniel Mack ip_mc_finish_output); 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /* Multicasts with ttl 0 must not go beyond the host */ 3931da177e4SLinus Torvalds 394eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl == 0) { 3951da177e4SLinus Torvalds kfree_skb(skb); 3961da177e4SLinus Torvalds return 0; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds if (rt->rt_flags&RTCF_BROADCAST) { 4011da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 4021da177e4SLinus Torvalds if (newskb) 40329a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 40429a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 40533b48679SDaniel Mack ip_mc_finish_output); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 40829a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 40929a26a56SEric W. Biederman net, sk, skb, NULL, skb->dev, 41029a26a56SEric W. Biederman ip_finish_output, 41148d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 414ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb) 4151da177e4SLinus Torvalds { 41628f8bfd1SPhil Sutter struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev; 4171bd9bef6SPatrick McHardy 41888f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 4191da177e4SLinus Torvalds 4201bd9bef6SPatrick McHardy skb->dev = dev; 4211bd9bef6SPatrick McHardy skb->protocol = htons(ETH_P_IP); 4221bd9bef6SPatrick McHardy 42329a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 42428f8bfd1SPhil Sutter net, sk, skb, indev, dev, 42548d5cad8SPatrick McHardy ip_finish_output, 42648d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 4271da177e4SLinus Torvalds } 4286585d7dcSBrian Vazquez EXPORT_SYMBOL(ip_output); 4291da177e4SLinus Torvalds 43084f9307cSEric Dumazet /* 43184f9307cSEric Dumazet * copy saddr and daddr, possibly using 64bit load/stores 43284f9307cSEric Dumazet * Equivalent to : 43384f9307cSEric Dumazet * iph->saddr = fl4->saddr; 43484f9307cSEric Dumazet * iph->daddr = fl4->daddr; 43584f9307cSEric Dumazet */ 43684f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4) 43784f9307cSEric Dumazet { 43884f9307cSEric Dumazet BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) != 43984f9307cSEric Dumazet offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr)); 4406321c7acSGustavo A. R. Silva 4416321c7acSGustavo A. R. Silva iph->saddr = fl4->saddr; 4426321c7acSGustavo A. R. Silva iph->daddr = fl4->daddr; 44384f9307cSEric Dumazet } 44484f9307cSEric Dumazet 445b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */ 44669b9e1e0SXin Long int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl, 44769b9e1e0SXin Long __u8 tos) 4481da177e4SLinus Torvalds { 4491da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 45077589ce0SEric W. Biederman struct net *net = sock_net(sk); 451f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 452b57ae01aSDavid S. Miller struct flowi4 *fl4; 4531da177e4SLinus Torvalds struct rtable *rt; 4541da177e4SLinus Torvalds struct iphdr *iph; 455ab6e3febSEric Dumazet int res; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* Skip all of this if the packet is already routed, 4581da177e4SLinus Torvalds * f.e. by something like SCTP. 4591da177e4SLinus Torvalds */ 460ab6e3febSEric Dumazet rcu_read_lock(); 461f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 462ea4fc0d6SDavid S. Miller fl4 = &fl->u.ip4; 463511c3f92SEric Dumazet rt = skb_rtable(skb); 46400db4124SIan Morris if (rt) 4651da177e4SLinus Torvalds goto packet_routed; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /* Make sure we can route this packet. */ 4681da177e4SLinus Torvalds rt = (struct rtable *)__sk_dst_check(sk, 0); 46951456b29SIan Morris if (!rt) { 4703ca3c68eSAl Viro __be32 daddr; 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /* Use correct destination address if we have options. */ 473c720c7e8SEric Dumazet daddr = inet->inet_daddr; 474f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 475f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds /* If this fails, retransmit mechanism of transport layer will 4781da177e4SLinus Torvalds * keep trying until route appears or the connection times 4791da177e4SLinus Torvalds * itself out. 4801da177e4SLinus Torvalds */ 48177589ce0SEric W. Biederman rt = ip_route_output_ports(net, fl4, sk, 48278fbfd8aSDavid S. Miller daddr, inet->inet_saddr, 48378fbfd8aSDavid S. Miller inet->inet_dport, 48478fbfd8aSDavid S. Miller inet->inet_sport, 48578fbfd8aSDavid S. Miller sk->sk_protocol, 48669b9e1e0SXin Long RT_CONN_FLAGS_TOS(sk, tos), 48778fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 488b23dd4feSDavid S. Miller if (IS_ERR(rt)) 4891da177e4SLinus Torvalds goto no_route; 490d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 4911da177e4SLinus Torvalds } 492d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds packet_routed: 49577d5bc7eSDavid Ahern if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway) 4961da177e4SLinus Torvalds goto no_route; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* OK, we know where to send it, allocate and build IP header. */ 499f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0)); 5008856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 501eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 50269b9e1e0SXin Long *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (tos & 0xff)); 50360ff7467SWANG Cong if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df) 5041da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 5051da177e4SLinus Torvalds else 5061da177e4SLinus Torvalds iph->frag_off = 0; 507d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 5081da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 50984f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 51084f9307cSEric Dumazet 5111da177e4SLinus Torvalds /* Transport layer set skb->h.foo itself. */ 5121da177e4SLinus Torvalds 513f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.optlen) { 514f6d8bd05SEric Dumazet iph->ihl += inet_opt->opt.optlen >> 2; 515f6d8bd05SEric Dumazet ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 51877589ce0SEric W. Biederman ip_select_ident_segs(net, skb, sk, 519b6a7719aSHannes Frederic Sowa skb_shinfo(skb)->gso_segs ?: 1); 5201da177e4SLinus Torvalds 521b0270e91SEric Dumazet /* TODO : should we use skb->sk here instead of sk ? */ 5221da177e4SLinus Torvalds skb->priority = sk->sk_priority; 5234a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 5241da177e4SLinus Torvalds 52533224b16SEric W. Biederman res = ip_local_out(net, sk, skb); 526ab6e3febSEric Dumazet rcu_read_unlock(); 527ab6e3febSEric Dumazet return res; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds no_route: 530ab6e3febSEric Dumazet rcu_read_unlock(); 53177589ce0SEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 5321da177e4SLinus Torvalds kfree_skb(skb); 5331da177e4SLinus Torvalds return -EHOSTUNREACH; 5341da177e4SLinus Torvalds } 53569b9e1e0SXin Long EXPORT_SYMBOL(__ip_queue_xmit); 5361da177e4SLinus Torvalds 53705e22e83SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl) 53805e22e83SEric Dumazet { 53905e22e83SEric Dumazet return __ip_queue_xmit(sk, skb, fl, inet_sk(sk)->tos); 54005e22e83SEric Dumazet } 54105e22e83SEric Dumazet EXPORT_SYMBOL(ip_queue_xmit); 54205e22e83SEric Dumazet 5431da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 5441da177e4SLinus Torvalds { 5451da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 5461da177e4SLinus Torvalds to->priority = from->priority; 5471da177e4SLinus Torvalds to->protocol = from->protocol; 548d2f0c961SShmulik Ladkani to->skb_iif = from->skb_iif; 549adf30907SEric Dumazet skb_dst_drop(to); 550fe76cda3SEric Dumazet skb_dst_copy(to, from); 5511da177e4SLinus Torvalds to->dev = from->dev; 55282e91ffeSThomas Graf to->mark = from->mark; 5531da177e4SLinus Torvalds 5543dd1c9a1SPaolo Abeni skb_copy_hash(to, from); 5553dd1c9a1SPaolo Abeni 5561da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5571da177e4SLinus Torvalds to->tc_index = from->tc_index; 5581da177e4SLinus Torvalds #endif 559e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 560df5042f4SFlorian Westphal skb_ext_copy(to, from); 5616ca40d4eSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IP_VS) 562c98d80edSJulian Anastasov to->ipvs_property = from->ipvs_property; 563c98d80edSJulian Anastasov #endif 564984bc16cSJames Morris skb_copy_secmark(to, from); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567694869b3SEric W. Biederman static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 568c5501eb3SFlorian Westphal unsigned int mtu, 569694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 57049d16b23SAndy Zhou { 57149d16b23SAndy Zhou struct iphdr *iph = ip_hdr(skb); 57249d16b23SAndy Zhou 573d6b915e2SFlorian Westphal if ((iph->frag_off & htons(IP_DF)) == 0) 574694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 575d6b915e2SFlorian Westphal 576d6b915e2SFlorian Westphal if (unlikely(!skb->ignore_df || 57749d16b23SAndy Zhou (IPCB(skb)->frag_max_size && 57849d16b23SAndy Zhou IPCB(skb)->frag_max_size > mtu))) { 5799479b0afSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 58049d16b23SAndy Zhou icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 58149d16b23SAndy Zhou htonl(mtu)); 58249d16b23SAndy Zhou kfree_skb(skb); 58349d16b23SAndy Zhou return -EMSGSIZE; 58449d16b23SAndy Zhou } 58549d16b23SAndy Zhou 586694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 58749d16b23SAndy Zhou } 58849d16b23SAndy Zhou 589c8b17be0SPablo Neira Ayuso void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph, 590c8b17be0SPablo Neira Ayuso unsigned int hlen, struct ip_fraglist_iter *iter) 591c8b17be0SPablo Neira Ayuso { 592c8b17be0SPablo Neira Ayuso unsigned int first_len = skb_pagelen(skb); 593c8b17be0SPablo Neira Ayuso 594b7034146SEric Dumazet iter->frag = skb_shinfo(skb)->frag_list; 595c8b17be0SPablo Neira Ayuso skb_frag_list_init(skb); 596c8b17be0SPablo Neira Ayuso 597c8b17be0SPablo Neira Ayuso iter->offset = 0; 598c8b17be0SPablo Neira Ayuso iter->iph = iph; 599c8b17be0SPablo Neira Ayuso iter->hlen = hlen; 600c8b17be0SPablo Neira Ayuso 601c8b17be0SPablo Neira Ayuso skb->data_len = first_len - skb_headlen(skb); 602c8b17be0SPablo Neira Ayuso skb->len = first_len; 603c8b17be0SPablo Neira Ayuso iph->tot_len = htons(first_len); 604c8b17be0SPablo Neira Ayuso iph->frag_off = htons(IP_MF); 605c8b17be0SPablo Neira Ayuso ip_send_check(iph); 606c8b17be0SPablo Neira Ayuso } 607c8b17be0SPablo Neira Ayuso EXPORT_SYMBOL(ip_fraglist_init); 608c8b17be0SPablo Neira Ayuso 609c8b17be0SPablo Neira Ayuso void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter) 610c8b17be0SPablo Neira Ayuso { 611c8b17be0SPablo Neira Ayuso unsigned int hlen = iter->hlen; 612c8b17be0SPablo Neira Ayuso struct iphdr *iph = iter->iph; 613c8b17be0SPablo Neira Ayuso struct sk_buff *frag; 614c8b17be0SPablo Neira Ayuso 615c8b17be0SPablo Neira Ayuso frag = iter->frag; 616c8b17be0SPablo Neira Ayuso frag->ip_summed = CHECKSUM_NONE; 617c8b17be0SPablo Neira Ayuso skb_reset_transport_header(frag); 618c8b17be0SPablo Neira Ayuso __skb_push(frag, hlen); 619c8b17be0SPablo Neira Ayuso skb_reset_network_header(frag); 620c8b17be0SPablo Neira Ayuso memcpy(skb_network_header(frag), iph, hlen); 621c8b17be0SPablo Neira Ayuso iter->iph = ip_hdr(frag); 622c8b17be0SPablo Neira Ayuso iph = iter->iph; 623c8b17be0SPablo Neira Ayuso iph->tot_len = htons(frag->len); 624c8b17be0SPablo Neira Ayuso ip_copy_metadata(frag, skb); 625c8b17be0SPablo Neira Ayuso iter->offset += skb->len - hlen; 626c8b17be0SPablo Neira Ayuso iph->frag_off = htons(iter->offset >> 3); 627c8b17be0SPablo Neira Ayuso if (frag->next) 628c8b17be0SPablo Neira Ayuso iph->frag_off |= htons(IP_MF); 629c8b17be0SPablo Neira Ayuso /* Ready, complete checksum */ 630c8b17be0SPablo Neira Ayuso ip_send_check(iph); 631c8b17be0SPablo Neira Ayuso } 632c8b17be0SPablo Neira Ayuso EXPORT_SYMBOL(ip_fraglist_prepare); 633c8b17be0SPablo Neira Ayuso 634065ff79fSPablo Neira Ayuso void ip_frag_init(struct sk_buff *skb, unsigned int hlen, 635e7a409c3SEric Dumazet unsigned int ll_rs, unsigned int mtu, bool DF, 636065ff79fSPablo Neira Ayuso struct ip_frag_state *state) 637065ff79fSPablo Neira Ayuso { 638065ff79fSPablo Neira Ayuso struct iphdr *iph = ip_hdr(skb); 639065ff79fSPablo Neira Ayuso 640e7a409c3SEric Dumazet state->DF = DF; 641065ff79fSPablo Neira Ayuso state->hlen = hlen; 642065ff79fSPablo Neira Ayuso state->ll_rs = ll_rs; 643065ff79fSPablo Neira Ayuso state->mtu = mtu; 644065ff79fSPablo Neira Ayuso 645065ff79fSPablo Neira Ayuso state->left = skb->len - hlen; /* Space per frame */ 646065ff79fSPablo Neira Ayuso state->ptr = hlen; /* Where to start from */ 647065ff79fSPablo Neira Ayuso 648065ff79fSPablo Neira Ayuso state->offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; 649065ff79fSPablo Neira Ayuso state->not_last_frag = iph->frag_off & htons(IP_MF); 650065ff79fSPablo Neira Ayuso } 651065ff79fSPablo Neira Ayuso EXPORT_SYMBOL(ip_frag_init); 652065ff79fSPablo Neira Ayuso 65319c3401aSPablo Neira Ayuso static void ip_frag_ipcb(struct sk_buff *from, struct sk_buff *to, 654faf482caSYajun Deng bool first_frag) 65519c3401aSPablo Neira Ayuso { 65619c3401aSPablo Neira Ayuso /* Copy the flags to each fragment. */ 65719c3401aSPablo Neira Ayuso IPCB(to)->flags = IPCB(from)->flags; 65819c3401aSPablo Neira Ayuso 65919c3401aSPablo Neira Ayuso /* ANK: dirty, but effective trick. Upgrade options only if 66019c3401aSPablo Neira Ayuso * the segment to be fragmented was THE FIRST (otherwise, 66119c3401aSPablo Neira Ayuso * options are already fixed) and make it ONCE 66219c3401aSPablo Neira Ayuso * on the initial skb, so that all the following fragments 66319c3401aSPablo Neira Ayuso * will inherit fixed options. 66419c3401aSPablo Neira Ayuso */ 66519c3401aSPablo Neira Ayuso if (first_frag) 66619c3401aSPablo Neira Ayuso ip_options_fragment(from); 66719c3401aSPablo Neira Ayuso } 66819c3401aSPablo Neira Ayuso 669065ff79fSPablo Neira Ayuso struct sk_buff *ip_frag_next(struct sk_buff *skb, struct ip_frag_state *state) 670065ff79fSPablo Neira Ayuso { 671065ff79fSPablo Neira Ayuso unsigned int len = state->left; 672065ff79fSPablo Neira Ayuso struct sk_buff *skb2; 673065ff79fSPablo Neira Ayuso struct iphdr *iph; 674065ff79fSPablo Neira Ayuso 675065ff79fSPablo Neira Ayuso /* IF: it doesn't fit, use 'mtu' - the data space left */ 676065ff79fSPablo Neira Ayuso if (len > state->mtu) 677065ff79fSPablo Neira Ayuso len = state->mtu; 678065ff79fSPablo Neira Ayuso /* IF: we are not sending up to and including the packet end 679065ff79fSPablo Neira Ayuso then align the next start on an eight byte boundary */ 680065ff79fSPablo Neira Ayuso if (len < state->left) { 681065ff79fSPablo Neira Ayuso len &= ~7; 682065ff79fSPablo Neira Ayuso } 683065ff79fSPablo Neira Ayuso 684065ff79fSPablo Neira Ayuso /* Allocate buffer */ 685065ff79fSPablo Neira Ayuso skb2 = alloc_skb(len + state->hlen + state->ll_rs, GFP_ATOMIC); 686065ff79fSPablo Neira Ayuso if (!skb2) 687065ff79fSPablo Neira Ayuso return ERR_PTR(-ENOMEM); 688065ff79fSPablo Neira Ayuso 689065ff79fSPablo Neira Ayuso /* 690065ff79fSPablo Neira Ayuso * Set up data on packet 691065ff79fSPablo Neira Ayuso */ 692065ff79fSPablo Neira Ayuso 693065ff79fSPablo Neira Ayuso ip_copy_metadata(skb2, skb); 694065ff79fSPablo Neira Ayuso skb_reserve(skb2, state->ll_rs); 695065ff79fSPablo Neira Ayuso skb_put(skb2, len + state->hlen); 696065ff79fSPablo Neira Ayuso skb_reset_network_header(skb2); 697065ff79fSPablo Neira Ayuso skb2->transport_header = skb2->network_header + state->hlen; 698065ff79fSPablo Neira Ayuso 699065ff79fSPablo Neira Ayuso /* 700065ff79fSPablo Neira Ayuso * Charge the memory for the fragment to any owner 701065ff79fSPablo Neira Ayuso * it might possess 702065ff79fSPablo Neira Ayuso */ 703065ff79fSPablo Neira Ayuso 704065ff79fSPablo Neira Ayuso if (skb->sk) 705065ff79fSPablo Neira Ayuso skb_set_owner_w(skb2, skb->sk); 706065ff79fSPablo Neira Ayuso 707065ff79fSPablo Neira Ayuso /* 708065ff79fSPablo Neira Ayuso * Copy the packet header into the new buffer. 709065ff79fSPablo Neira Ayuso */ 710065ff79fSPablo Neira Ayuso 711065ff79fSPablo Neira Ayuso skb_copy_from_linear_data(skb, skb_network_header(skb2), state->hlen); 712065ff79fSPablo Neira Ayuso 713065ff79fSPablo Neira Ayuso /* 714065ff79fSPablo Neira Ayuso * Copy a block of the IP datagram. 715065ff79fSPablo Neira Ayuso */ 716065ff79fSPablo Neira Ayuso if (skb_copy_bits(skb, state->ptr, skb_transport_header(skb2), len)) 717065ff79fSPablo Neira Ayuso BUG(); 718065ff79fSPablo Neira Ayuso state->left -= len; 719065ff79fSPablo Neira Ayuso 720065ff79fSPablo Neira Ayuso /* 721065ff79fSPablo Neira Ayuso * Fill in the new header fields. 722065ff79fSPablo Neira Ayuso */ 723065ff79fSPablo Neira Ayuso iph = ip_hdr(skb2); 724065ff79fSPablo Neira Ayuso iph->frag_off = htons((state->offset >> 3)); 725e7a409c3SEric Dumazet if (state->DF) 726e7a409c3SEric Dumazet iph->frag_off |= htons(IP_DF); 727065ff79fSPablo Neira Ayuso 728065ff79fSPablo Neira Ayuso /* 729065ff79fSPablo Neira Ayuso * Added AC : If we are fragmenting a fragment that's not the 730065ff79fSPablo Neira Ayuso * last fragment then keep MF on each bit 731065ff79fSPablo Neira Ayuso */ 732065ff79fSPablo Neira Ayuso if (state->left > 0 || state->not_last_frag) 733065ff79fSPablo Neira Ayuso iph->frag_off |= htons(IP_MF); 734065ff79fSPablo Neira Ayuso state->ptr += len; 735065ff79fSPablo Neira Ayuso state->offset += len; 736065ff79fSPablo Neira Ayuso 737065ff79fSPablo Neira Ayuso iph->tot_len = htons(len + state->hlen); 738065ff79fSPablo Neira Ayuso 739065ff79fSPablo Neira Ayuso ip_send_check(iph); 740065ff79fSPablo Neira Ayuso 741065ff79fSPablo Neira Ayuso return skb2; 742065ff79fSPablo Neira Ayuso } 743065ff79fSPablo Neira Ayuso EXPORT_SYMBOL(ip_frag_next); 744065ff79fSPablo Neira Ayuso 7451da177e4SLinus Torvalds /* 7461da177e4SLinus Torvalds * This IP datagram is too large to be sent in one piece. Break it up into 7471da177e4SLinus Torvalds * smaller pieces (each of size equal to IP header plus 7481da177e4SLinus Torvalds * a block of the data of the original IP data part) that will yet fit in a 7491da177e4SLinus Torvalds * single device frame, and queue such a frame for sending. 7501da177e4SLinus Torvalds */ 7511da177e4SLinus Torvalds 752694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 753694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds struct iphdr *iph; 7561da177e4SLinus Torvalds struct sk_buff *skb2; 757511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 758065ff79fSPablo Neira Ayuso unsigned int mtu, hlen, ll_rs; 759c8b17be0SPablo Neira Ayuso struct ip_fraglist_iter iter; 7609669fffcSEric Dumazet ktime_t tstamp = skb->tstamp; 761065ff79fSPablo Neira Ayuso struct ip_frag_state state; 7621da177e4SLinus Torvalds int err = 0; 7631da177e4SLinus Torvalds 764dbd3393cSHannes Frederic Sowa /* for offloaded checksums cleanup checksum before fragmentation */ 765dbd3393cSHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 766dbd3393cSHannes Frederic Sowa (err = skb_checksum_help(skb))) 767dbd3393cSHannes Frederic Sowa goto fail; 768dbd3393cSHannes Frederic Sowa 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * Point into the IP datagram header. 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds 773eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7741da177e4SLinus Torvalds 775fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 776d6b915e2SFlorian Westphal if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu) 777d6b915e2SFlorian Westphal mtu = IPCB(skb)->frag_max_size; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* 7801da177e4SLinus Torvalds * Setup starting values. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds hlen = iph->ihl * 4; 784f87c10a8SHannes Frederic Sowa mtu = mtu - hlen; /* Size of data space */ 78589cee8b1SHerbert Xu IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; 786254d900bSVasily Averin ll_rs = LL_RESERVED_SPACE(rt->dst.dev); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* When frag_list is given, use it. First, check its validity: 7891da177e4SLinus Torvalds * some transformers could create wrong frag_list or break existing 7901da177e4SLinus Torvalds * one, it is not prohibited. In this case fall back to copying. 7911da177e4SLinus Torvalds * 7921da177e4SLinus Torvalds * LATER: this step can be merged to real generation of fragments, 7931da177e4SLinus Torvalds * we can switch to copy when see the first bad fragment. 7941da177e4SLinus Torvalds */ 79521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 7963d13008eSEric Dumazet struct sk_buff *frag, *frag2; 797c72d8cdaSAlexey Dobriyan unsigned int first_len = skb_pagelen(skb); 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds if (first_len - hlen > mtu || 8001da177e4SLinus Torvalds ((first_len - hlen) & 7) || 80156f8a75cSPaul Gortmaker ip_is_fragment(iph) || 802254d900bSVasily Averin skb_cloned(skb) || 803254d900bSVasily Averin skb_headroom(skb) < ll_rs) 8041da177e4SLinus Torvalds goto slow_path; 8051da177e4SLinus Torvalds 806d7fcf1a5SDavid S. Miller skb_walk_frags(skb, frag) { 8071da177e4SLinus Torvalds /* Correct geometry. */ 8081da177e4SLinus Torvalds if (frag->len > mtu || 8091da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 810254d900bSVasily Averin skb_headroom(frag) < hlen + ll_rs) 8113d13008eSEric Dumazet goto slow_path_clean; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* Partially cloned skb? */ 8141da177e4SLinus Torvalds if (skb_shared(frag)) 8153d13008eSEric Dumazet goto slow_path_clean; 8162fdba6b0SHerbert Xu 8172fdba6b0SHerbert Xu BUG_ON(frag->sk); 8182fdba6b0SHerbert Xu if (skb->sk) { 8192fdba6b0SHerbert Xu frag->sk = skb->sk; 8202fdba6b0SHerbert Xu frag->destructor = sock_wfree; 8212fdba6b0SHerbert Xu } 8223d13008eSEric Dumazet skb->truesize -= frag->truesize; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* Everything is OK. Generate! */ 826c8b17be0SPablo Neira Ayuso ip_fraglist_init(skb, iph, hlen, &iter); 8271b9fbe81SYajun Deng 8281da177e4SLinus Torvalds for (;;) { 8291da177e4SLinus Torvalds /* Prepare header of the next frame, 8301da177e4SLinus Torvalds * before previous one went down. */ 83119c3401aSPablo Neira Ayuso if (iter.frag) { 832*27a8caa5SJakub Kicinski bool first_frag = (iter.offset == 0); 833*27a8caa5SJakub Kicinski 834faf482caSYajun Deng IPCB(iter.frag)->flags = IPCB(skb)->flags; 835c8b17be0SPablo Neira Ayuso ip_fraglist_prepare(skb, &iter); 836*27a8caa5SJakub Kicinski if (first_frag && IPCB(skb)->opt.optlen) { 837*27a8caa5SJakub Kicinski /* ipcb->opt is not populated for frags 838*27a8caa5SJakub Kicinski * coming from __ip_make_skb(), 839*27a8caa5SJakub Kicinski * ip_options_fragment() needs optlen 840*27a8caa5SJakub Kicinski */ 841*27a8caa5SJakub Kicinski IPCB(iter.frag)->opt.optlen = 842*27a8caa5SJakub Kicinski IPCB(skb)->opt.optlen; 843*27a8caa5SJakub Kicinski ip_options_fragment(iter.frag); 844*27a8caa5SJakub Kicinski ip_send_check(iter.iph); 845*27a8caa5SJakub Kicinski } 84619c3401aSPablo Neira Ayuso } 8471da177e4SLinus Torvalds 8489669fffcSEric Dumazet skb->tstamp = tstamp; 849694869b3SEric W. Biederman err = output(net, sk, skb); 8501da177e4SLinus Torvalds 851dafee490SWei Dong if (!err) 85226a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 853c8b17be0SPablo Neira Ayuso if (err || !iter.frag) 8541da177e4SLinus Torvalds break; 8551da177e4SLinus Torvalds 856c8b17be0SPablo Neira Ayuso skb = ip_fraglist_next(&iter); 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds if (err == 0) { 86026a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 8611da177e4SLinus Torvalds return 0; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 864b7034146SEric Dumazet kfree_skb_list(iter.frag); 865942f146aSPablo Neira Ayuso 86626a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 8671da177e4SLinus Torvalds return err; 8683d13008eSEric Dumazet 8693d13008eSEric Dumazet slow_path_clean: 8703d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 8713d13008eSEric Dumazet if (frag2 == frag) 8723d13008eSEric Dumazet break; 8733d13008eSEric Dumazet frag2->sk = NULL; 8743d13008eSEric Dumazet frag2->destructor = NULL; 8753d13008eSEric Dumazet skb->truesize += frag2->truesize; 8763d13008eSEric Dumazet } 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds slow_path: 8801da177e4SLinus Torvalds /* 8811da177e4SLinus Torvalds * Fragment the datagram. 8821da177e4SLinus Torvalds */ 8831da177e4SLinus Torvalds 884e7a409c3SEric Dumazet ip_frag_init(skb, hlen, ll_rs, mtu, IPCB(skb)->flags & IPSKB_FRAG_PMTU, 885e7a409c3SEric Dumazet &state); 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* 8881da177e4SLinus Torvalds * Keep copying data until we run out. 8891da177e4SLinus Torvalds */ 8901da177e4SLinus Torvalds 891065ff79fSPablo Neira Ayuso while (state.left > 0) { 89219c3401aSPablo Neira Ayuso bool first_frag = (state.offset == 0); 89319c3401aSPablo Neira Ayuso 894065ff79fSPablo Neira Ayuso skb2 = ip_frag_next(skb, &state); 895065ff79fSPablo Neira Ayuso if (IS_ERR(skb2)) { 896065ff79fSPablo Neira Ayuso err = PTR_ERR(skb2); 8971da177e4SLinus Torvalds goto fail; 8981da177e4SLinus Torvalds } 899faf482caSYajun Deng ip_frag_ipcb(skb, skb2, first_frag); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* 9021da177e4SLinus Torvalds * Put this fragment into the sending queue. 9031da177e4SLinus Torvalds */ 9049669fffcSEric Dumazet skb2->tstamp = tstamp; 905694869b3SEric W. Biederman err = output(net, sk, skb2); 9061da177e4SLinus Torvalds if (err) 9071da177e4SLinus Torvalds goto fail; 908dafee490SWei Dong 90926a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 9101da177e4SLinus Torvalds } 9115d0ba55bSEric Dumazet consume_skb(skb); 91226a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 9131da177e4SLinus Torvalds return err; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds fail: 9161da177e4SLinus Torvalds kfree_skb(skb); 91726a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 9181da177e4SLinus Torvalds return err; 9191da177e4SLinus Torvalds } 92049d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment); 9212e2f7aefSPatrick McHardy 9221da177e4SLinus Torvalds int 9231da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 9241da177e4SLinus Torvalds { 925f69e6d13SAl Viro struct msghdr *msg = from; 9261da177e4SLinus Torvalds 92784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 9280b62fca2SAl Viro if (!copy_from_iter_full(to, len, &msg->msg_iter)) 9291da177e4SLinus Torvalds return -EFAULT; 9301da177e4SLinus Torvalds } else { 93144bb9363SAl Viro __wsum csum = 0; 9320b62fca2SAl Viro if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter)) 9331da177e4SLinus Torvalds return -EFAULT; 9341da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds return 0; 9371da177e4SLinus Torvalds } 9384bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag); 9391da177e4SLinus Torvalds 94044bb9363SAl Viro static inline __wsum 9411da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy) 9421da177e4SLinus Torvalds { 9431da177e4SLinus Torvalds char *kaddr; 94444bb9363SAl Viro __wsum csum; 9451da177e4SLinus Torvalds kaddr = kmap(page); 9461da177e4SLinus Torvalds csum = csum_partial(kaddr + offset, copy, 0); 9471da177e4SLinus Torvalds kunmap(page); 9481da177e4SLinus Torvalds return csum; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk, 952f5fca608SDavid S. Miller struct flowi4 *fl4, 953f5fca608SDavid S. Miller struct sk_buff_head *queue, 9541470ddf7SHerbert Xu struct inet_cork *cork, 9555640f768SEric Dumazet struct page_frag *pfrag, 9561470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, 9571470ddf7SHerbert Xu int len, int odd, struct sk_buff *skb), 9581da177e4SLinus Torvalds void *from, int length, int transhdrlen, 9591da177e4SLinus Torvalds unsigned int flags) 9601da177e4SLinus Torvalds { 9611da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 962b5947e5dSWillem de Bruijn struct ubuf_info *uarg = NULL; 9631da177e4SLinus Torvalds struct sk_buff *skb; 9641da177e4SLinus Torvalds 96507df5294SHerbert Xu struct ip_options *opt = cork->opt; 9661da177e4SLinus Torvalds int hh_len; 9671da177e4SLinus Torvalds int exthdrlen; 9681da177e4SLinus Torvalds int mtu; 9691da177e4SLinus Torvalds int copy; 9701da177e4SLinus Torvalds int err; 9711da177e4SLinus Torvalds int offset = 0; 972daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, maxnonfragsize; 9731da177e4SLinus Torvalds int csummode = CHECKSUM_NONE; 9741470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 975694aba69SEric Dumazet unsigned int wmem_alloc_delta = 0; 976100f6d8eSWillem de Bruijn bool paged, extra_uref = false; 97709c2d251SWillem de Bruijn u32 tskey = 0; 9781da177e4SLinus Torvalds 97996d7303eSSteffen Klassert skb = skb_peek_tail(queue); 98096d7303eSSteffen Klassert 98196d7303eSSteffen Klassert exthdrlen = !skb ? rt->dst.header_len : 0; 982bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize; 98315e36f5bSWillem de Bruijn paged = !!cork->gso_size; 984bec1f6f6SWillem de Bruijn 98509c2d251SWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 98609c2d251SWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 98709c2d251SWillem de Bruijn tskey = sk->sk_tskey++; 9881470ddf7SHerbert Xu 989d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 9921da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 993cbc08a33SMiaohe Lin maxnonfragsize = ip_sk_ignore_df(sk) ? IP_MAX_MTU : mtu; 9941da177e4SLinus Torvalds 995daba287bSHannes Frederic Sowa if (cork->length + length > maxnonfragsize - fragheaderlen) { 996f5fca608SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 99761e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 9981da177e4SLinus Torvalds return -EMSGSIZE; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 10021da177e4SLinus Torvalds * transhdrlen > 0 means that this is the first fragment and we wish 10031da177e4SLinus Torvalds * it won't be fragmented in the future. 10041da177e4SLinus Torvalds */ 10051da177e4SLinus Torvalds if (transhdrlen && 10061da177e4SLinus Torvalds length + fragheaderlen <= mtu && 1007c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) && 1008bec1f6f6SWillem de Bruijn (!(flags & MSG_MORE) || cork->gso_size) && 1009cd027a54SJacek Kalwas (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM))) 101084fa7933SPatrick McHardy csummode = CHECKSUM_PARTIAL; 10111da177e4SLinus Torvalds 1012b5947e5dSWillem de Bruijn if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) { 10138c793822SJonathan Lemon uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb)); 1014b5947e5dSWillem de Bruijn if (!uarg) 1015b5947e5dSWillem de Bruijn return -ENOBUFS; 1016522924b5SWillem de Bruijn extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ 1017b5947e5dSWillem de Bruijn if (rt->dst.dev->features & NETIF_F_SG && 1018b5947e5dSWillem de Bruijn csummode == CHECKSUM_PARTIAL) { 1019b5947e5dSWillem de Bruijn paged = true; 1020b5947e5dSWillem de Bruijn } else { 1021b5947e5dSWillem de Bruijn uarg->zerocopy = 0; 102252900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 1023b5947e5dSWillem de Bruijn } 1024b5947e5dSWillem de Bruijn } 1025b5947e5dSWillem de Bruijn 10261470ddf7SHerbert Xu cork->length += length; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds /* So, what's going on in the loop below? 10291da177e4SLinus Torvalds * 10301da177e4SLinus Torvalds * We use calculated fragment length to generate chained skb, 10311da177e4SLinus Torvalds * each of segments is IP fragment ready for sending to network after 10321da177e4SLinus Torvalds * adding appropriate IP header. 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds 103526cde9f7SHerbert Xu if (!skb) 10361da177e4SLinus Torvalds goto alloc_new_skb; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds while (length > 0) { 10391da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 10401da177e4SLinus Torvalds copy = mtu - skb->len; 10411da177e4SLinus Torvalds if (copy < length) 10421da177e4SLinus Torvalds copy = maxfraglen - skb->len; 10431da177e4SLinus Torvalds if (copy <= 0) { 10441da177e4SLinus Torvalds char *data; 10451da177e4SLinus Torvalds unsigned int datalen; 10461da177e4SLinus Torvalds unsigned int fraglen; 10471da177e4SLinus Torvalds unsigned int fraggap; 10486d123b81SJakub Kicinski unsigned int alloclen, alloc_extra; 1049aba36930SWillem de Bruijn unsigned int pagedlen; 10501da177e4SLinus Torvalds struct sk_buff *skb_prev; 10511da177e4SLinus Torvalds alloc_new_skb: 10521da177e4SLinus Torvalds skb_prev = skb; 10531da177e4SLinus Torvalds if (skb_prev) 10541da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 10551da177e4SLinus Torvalds else 10561da177e4SLinus Torvalds fraggap = 0; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds /* 10591da177e4SLinus Torvalds * If remaining data exceeds the mtu, 10601da177e4SLinus Torvalds * we know we need more fragment(s). 10611da177e4SLinus Torvalds */ 10621da177e4SLinus Torvalds datalen = length + fraggap; 10631da177e4SLinus Torvalds if (datalen > mtu - fragheaderlen) 10641da177e4SLinus Torvalds datalen = maxfraglen - fragheaderlen; 10651da177e4SLinus Torvalds fraglen = datalen + fragheaderlen; 1066aba36930SWillem de Bruijn pagedlen = 0; 10671da177e4SLinus Torvalds 10686d123b81SJakub Kicinski alloc_extra = hh_len + 15; 10696d123b81SJakub Kicinski alloc_extra += exthdrlen; 1070353e5c9aSSteffen Klassert 10711da177e4SLinus Torvalds /* The last fragment gets additional space at tail. 10721da177e4SLinus Torvalds * Note, with MSG_MORE we overallocate on fragments, 10731da177e4SLinus Torvalds * because we have no idea what fragment will be 10741da177e4SLinus Torvalds * the last. 10751da177e4SLinus Torvalds */ 107633f99dc7SSteffen Klassert if (datalen == length + fraggap) 10776d123b81SJakub Kicinski alloc_extra += rt->dst.trailer_len; 10786d123b81SJakub Kicinski 10796d123b81SJakub Kicinski if ((flags & MSG_MORE) && 10806d123b81SJakub Kicinski !(rt->dst.dev->features&NETIF_F_SG)) 10816d123b81SJakub Kicinski alloclen = mtu; 10826d123b81SJakub Kicinski else if (!paged && 10836d123b81SJakub Kicinski (fraglen + alloc_extra < SKB_MAX_ALLOC || 10846d123b81SJakub Kicinski !(rt->dst.dev->features & NETIF_F_SG))) 10856d123b81SJakub Kicinski alloclen = fraglen; 10866d123b81SJakub Kicinski else { 10876d123b81SJakub Kicinski alloclen = min_t(int, fraglen, MAX_HEADER); 10886d123b81SJakub Kicinski pagedlen = fraglen - alloclen; 10896d123b81SJakub Kicinski } 10906d123b81SJakub Kicinski 10916d123b81SJakub Kicinski alloclen += alloc_extra; 109233f99dc7SSteffen Klassert 10931da177e4SLinus Torvalds if (transhdrlen) { 10946d123b81SJakub Kicinski skb = sock_alloc_send_skb(sk, alloclen, 10951da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 10961da177e4SLinus Torvalds } else { 10971da177e4SLinus Torvalds skb = NULL; 1098694aba69SEric Dumazet if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 10991da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 11006d123b81SJakub Kicinski skb = alloc_skb(alloclen, 11011da177e4SLinus Torvalds sk->sk_allocation); 110251456b29SIan Morris if (unlikely(!skb)) 11031da177e4SLinus Torvalds err = -ENOBUFS; 11041da177e4SLinus Torvalds } 110551456b29SIan Morris if (!skb) 11061da177e4SLinus Torvalds goto error; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds /* 11091da177e4SLinus Torvalds * Fill in the control structures 11101da177e4SLinus Torvalds */ 11111da177e4SLinus Torvalds skb->ip_summed = csummode; 11121da177e4SLinus Torvalds skb->csum = 0; 11131da177e4SLinus Torvalds skb_reserve(skb, hh_len); 111411878b40SWillem de Bruijn 11151da177e4SLinus Torvalds /* 11161da177e4SLinus Torvalds * Find where to start putting bytes. 11171da177e4SLinus Torvalds */ 111815e36f5bSWillem de Bruijn data = skb_put(skb, fraglen + exthdrlen - pagedlen); 1119c14d2450SArnaldo Carvalho de Melo skb_set_network_header(skb, exthdrlen); 1120b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1121b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 1122353e5c9aSSteffen Klassert data += fragheaderlen + exthdrlen; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds if (fraggap) { 11251da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 11261da177e4SLinus Torvalds skb_prev, maxfraglen, 11278d5930dfSAl Viro data + transhdrlen, fraggap); 11281da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 11291da177e4SLinus Torvalds skb->csum); 11301da177e4SLinus Torvalds data += fraggap; 1131e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 113415e36f5bSWillem de Bruijn copy = datalen - transhdrlen - fraggap - pagedlen; 11351da177e4SLinus Torvalds if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { 11361da177e4SLinus Torvalds err = -EFAULT; 11371da177e4SLinus Torvalds kfree_skb(skb); 11381da177e4SLinus Torvalds goto error; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds offset += copy; 114215e36f5bSWillem de Bruijn length -= copy + transhdrlen; 11431da177e4SLinus Torvalds transhdrlen = 0; 11441da177e4SLinus Torvalds exthdrlen = 0; 11451da177e4SLinus Torvalds csummode = CHECKSUM_NONE; 11461da177e4SLinus Torvalds 114752900d22SWillem de Bruijn /* only the initial fragment is time stamped */ 114852900d22SWillem de Bruijn skb_shinfo(skb)->tx_flags = cork->tx_flags; 114952900d22SWillem de Bruijn cork->tx_flags = 0; 115052900d22SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 115152900d22SWillem de Bruijn tskey = 0; 115252900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 115352900d22SWillem de Bruijn 11540dec879fSJulian Anastasov if ((flags & MSG_CONFIRM) && !skb_prev) 11550dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 11560dec879fSJulian Anastasov 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * Put the packet on the pending queue. 11591da177e4SLinus Torvalds */ 1160694aba69SEric Dumazet if (!skb->destructor) { 1161694aba69SEric Dumazet skb->destructor = sock_wfree; 1162694aba69SEric Dumazet skb->sk = sk; 1163694aba69SEric Dumazet wmem_alloc_delta += skb->truesize; 1164694aba69SEric Dumazet } 11651470ddf7SHerbert Xu __skb_queue_tail(queue, skb); 11661da177e4SLinus Torvalds continue; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds if (copy > length) 11701da177e4SLinus Torvalds copy = length; 11711da177e4SLinus Torvalds 1172113f99c3SWillem de Bruijn if (!(rt->dst.dev->features&NETIF_F_SG) && 1173113f99c3SWillem de Bruijn skb_tailroom(skb) >= copy) { 11741da177e4SLinus Torvalds unsigned int off; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds off = skb->len; 11771da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 11781da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 11791da177e4SLinus Torvalds __skb_trim(skb, off); 11801da177e4SLinus Torvalds err = -EFAULT; 11811da177e4SLinus Torvalds goto error; 11821da177e4SLinus Torvalds } 1183b5947e5dSWillem de Bruijn } else if (!uarg || !uarg->zerocopy) { 11841da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds err = -ENOMEM; 11875640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 11881da177e4SLinus Torvalds goto error; 11891da177e4SLinus Torvalds 11905640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 11915640f768SEric Dumazet pfrag->offset)) { 11921da177e4SLinus Torvalds err = -EMSGSIZE; 11935640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 11941da177e4SLinus Torvalds goto error; 11955640f768SEric Dumazet 11965640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 11975640f768SEric Dumazet pfrag->offset, 0); 11985640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 11995640f768SEric Dumazet get_page(pfrag->page); 12001da177e4SLinus Torvalds } 12015640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 12025640f768SEric Dumazet if (getfrag(from, 12035640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 12045640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 12055640f768SEric Dumazet goto error_efault; 12065640f768SEric Dumazet 12075640f768SEric Dumazet pfrag->offset += copy; 12085640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 12091da177e4SLinus Torvalds skb->len += copy; 12101da177e4SLinus Torvalds skb->data_len += copy; 1211f945fa7aSHerbert Xu skb->truesize += copy; 1212694aba69SEric Dumazet wmem_alloc_delta += copy; 1213b5947e5dSWillem de Bruijn } else { 1214b5947e5dSWillem de Bruijn err = skb_zerocopy_iter_dgram(skb, from, copy); 1215b5947e5dSWillem de Bruijn if (err < 0) 1216b5947e5dSWillem de Bruijn goto error; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds offset += copy; 12191da177e4SLinus Torvalds length -= copy; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12229e8445a5SPaolo Abeni if (wmem_alloc_delta) 1223694aba69SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 12241da177e4SLinus Torvalds return 0; 12251da177e4SLinus Torvalds 12265640f768SEric Dumazet error_efault: 12275640f768SEric Dumazet err = -EFAULT; 12281da177e4SLinus Torvalds error: 12298e044917SJonathan Lemon net_zcopy_put_abort(uarg, extra_uref); 12301470ddf7SHerbert Xu cork->length -= length; 12315e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 1232694aba69SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 12331da177e4SLinus Torvalds return err; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, 12371470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp) 12381470ddf7SHerbert Xu { 1239f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 12401470ddf7SHerbert Xu struct rtable *rt; 12411470ddf7SHerbert Xu 12429783ccd0SGao Feng rt = *rtp; 12439783ccd0SGao Feng if (unlikely(!rt)) 12449783ccd0SGao Feng return -EFAULT; 12459783ccd0SGao Feng 12461470ddf7SHerbert Xu /* 12471470ddf7SHerbert Xu * setup for corking. 12481470ddf7SHerbert Xu */ 12491470ddf7SHerbert Xu opt = ipc->opt; 12501470ddf7SHerbert Xu if (opt) { 125151456b29SIan Morris if (!cork->opt) { 12521470ddf7SHerbert Xu cork->opt = kmalloc(sizeof(struct ip_options) + 40, 12531470ddf7SHerbert Xu sk->sk_allocation); 125451456b29SIan Morris if (unlikely(!cork->opt)) 12551470ddf7SHerbert Xu return -ENOBUFS; 12561470ddf7SHerbert Xu } 1257f6d8bd05SEric Dumazet memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); 12581470ddf7SHerbert Xu cork->flags |= IPCORK_OPT; 12591470ddf7SHerbert Xu cork->addr = ipc->addr; 12601470ddf7SHerbert Xu } 12619783ccd0SGao Feng 1262482fc609SHannes Frederic Sowa cork->fragsize = ip_sk_use_pmtu(sk) ? 1263501a90c9SEric Dumazet dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu); 1264501a90c9SEric Dumazet 1265501a90c9SEric Dumazet if (!inetdev_valid_mtu(cork->fragsize)) 1266501a90c9SEric Dumazet return -ENETUNREACH; 1267bec1f6f6SWillem de Bruijn 1268fbf47813SWillem de Bruijn cork->gso_size = ipc->gso_size; 1269501a90c9SEric Dumazet 12701470ddf7SHerbert Xu cork->dst = &rt->dst; 1271501a90c9SEric Dumazet /* We stole this route, caller should not release it. */ 1272501a90c9SEric Dumazet *rtp = NULL; 1273501a90c9SEric Dumazet 12741470ddf7SHerbert Xu cork->length = 0; 1275aa661581SFrancesco Fusco cork->ttl = ipc->ttl; 1276aa661581SFrancesco Fusco cork->tos = ipc->tos; 1277c6af0c22SWillem de Bruijn cork->mark = ipc->sockc.mark; 1278aa661581SFrancesco Fusco cork->priority = ipc->priority; 1279bc969a97SJesus Sanchez-Palencia cork->transmit_time = ipc->sockc.transmit_time; 1280678ca42dSWillem de Bruijn cork->tx_flags = 0; 1281678ca42dSWillem de Bruijn sock_tx_timestamp(sk, ipc->sockc.tsflags, &cork->tx_flags); 12821470ddf7SHerbert Xu 12831470ddf7SHerbert Xu return 0; 12841470ddf7SHerbert Xu } 12851470ddf7SHerbert Xu 12861470ddf7SHerbert Xu /* 12871470ddf7SHerbert Xu * ip_append_data() and ip_append_page() can make one large IP datagram 12881470ddf7SHerbert Xu * from many pieces of data. Each pieces will be holded on the socket 12891470ddf7SHerbert Xu * until ip_push_pending_frames() is called. Each piece can be a page 12901470ddf7SHerbert Xu * or non-page data. 12911470ddf7SHerbert Xu * 12921470ddf7SHerbert Xu * Not only UDP, other transport protocols - e.g. raw sockets - can use 12931470ddf7SHerbert Xu * this interface potentially. 12941470ddf7SHerbert Xu * 12951470ddf7SHerbert Xu * LATER: length must be adjusted by pad at tail, when it is required. 12961470ddf7SHerbert Xu */ 1297f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4, 12981470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, int len, 12991470ddf7SHerbert Xu int odd, struct sk_buff *skb), 13001470ddf7SHerbert Xu void *from, int length, int transhdrlen, 13011470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 13021470ddf7SHerbert Xu unsigned int flags) 13031470ddf7SHerbert Xu { 13041470ddf7SHerbert Xu struct inet_sock *inet = inet_sk(sk); 13051470ddf7SHerbert Xu int err; 13061470ddf7SHerbert Xu 13071470ddf7SHerbert Xu if (flags&MSG_PROBE) 13081470ddf7SHerbert Xu return 0; 13091470ddf7SHerbert Xu 13101470ddf7SHerbert Xu if (skb_queue_empty(&sk->sk_write_queue)) { 1311bdc712b4SDavid S. Miller err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp); 13121470ddf7SHerbert Xu if (err) 13131470ddf7SHerbert Xu return err; 13141470ddf7SHerbert Xu } else { 13151470ddf7SHerbert Xu transhdrlen = 0; 13161470ddf7SHerbert Xu } 13171470ddf7SHerbert Xu 13185640f768SEric Dumazet return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base, 13195640f768SEric Dumazet sk_page_frag(sk), getfrag, 13201470ddf7SHerbert Xu from, length, transhdrlen, flags); 13211470ddf7SHerbert Xu } 13221470ddf7SHerbert Xu 1323f5fca608SDavid S. Miller ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 13241da177e4SLinus Torvalds int offset, size_t size, int flags) 13251da177e4SLinus Torvalds { 13261da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 13271da177e4SLinus Torvalds struct sk_buff *skb; 13281da177e4SLinus Torvalds struct rtable *rt; 13291da177e4SLinus Torvalds struct ip_options *opt = NULL; 1330bdc712b4SDavid S. Miller struct inet_cork *cork; 13311da177e4SLinus Torvalds int hh_len; 13321da177e4SLinus Torvalds int mtu; 13331da177e4SLinus Torvalds int len; 13341da177e4SLinus Torvalds int err; 1335daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds if (inet->hdrincl) 13381da177e4SLinus Torvalds return -EPERM; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds if (flags&MSG_PROBE) 13411da177e4SLinus Torvalds return 0; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) 13441da177e4SLinus Torvalds return -EINVAL; 13451da177e4SLinus Torvalds 1346bdc712b4SDavid S. Miller cork = &inet->cork.base; 1347bdc712b4SDavid S. Miller rt = (struct rtable *)cork->dst; 1348bdc712b4SDavid S. Miller if (cork->flags & IPCORK_OPT) 1349bdc712b4SDavid S. Miller opt = cork->opt; 13501da177e4SLinus Torvalds 1351d8d1f30bSChangli Gao if (!(rt->dst.dev->features & NETIF_F_SG)) 13521da177e4SLinus Torvalds return -EOPNOTSUPP; 13531da177e4SLinus Torvalds 1354d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 1355bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 13581da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 135960ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 13601da177e4SLinus Torvalds 1361daba287bSHannes Frederic Sowa if (cork->length + size > maxnonfragsize - fragheaderlen) { 136261e7f09dSHannes Frederic Sowa ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 136361e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 13641da177e4SLinus Torvalds return -EMSGSIZE; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 136751456b29SIan Morris skb = skb_peek_tail(&sk->sk_write_queue); 136851456b29SIan Morris if (!skb) 13691da177e4SLinus Torvalds return -EINVAL; 13701da177e4SLinus Torvalds 1371a8c4a252SHannes Frederic Sowa cork->length += size; 1372e89e9cf5SAnanda Raju 13731da177e4SLinus Torvalds while (size > 0) { 13741da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 13751da177e4SLinus Torvalds len = mtu - skb->len; 13761da177e4SLinus Torvalds if (len < size) 13771da177e4SLinus Torvalds len = maxfraglen - skb->len; 1378ab2fb7e3SWillem de Bruijn 13791da177e4SLinus Torvalds if (len <= 0) { 13801da177e4SLinus Torvalds struct sk_buff *skb_prev; 13811da177e4SLinus Torvalds int alloclen; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds skb_prev = skb; 13841da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds alloclen = fragheaderlen + hh_len + fraggap + 15; 13871da177e4SLinus Torvalds skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); 13881da177e4SLinus Torvalds if (unlikely(!skb)) { 13891da177e4SLinus Torvalds err = -ENOBUFS; 13901da177e4SLinus Torvalds goto error; 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /* 13941da177e4SLinus Torvalds * Fill in the control structures 13951da177e4SLinus Torvalds */ 13961da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 13971da177e4SLinus Torvalds skb->csum = 0; 13981da177e4SLinus Torvalds skb_reserve(skb, hh_len); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds /* 14011da177e4SLinus Torvalds * Find where to start putting bytes. 14021da177e4SLinus Torvalds */ 1403967b05f6SArnaldo Carvalho de Melo skb_put(skb, fragheaderlen + fraggap); 14042ca9e6f2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1405b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1406b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 14071da177e4SLinus Torvalds if (fraggap) { 1408967b05f6SArnaldo Carvalho de Melo skb->csum = skb_copy_and_csum_bits(skb_prev, 1409967b05f6SArnaldo Carvalho de Melo maxfraglen, 14109c70220bSArnaldo Carvalho de Melo skb_transport_header(skb), 14118d5930dfSAl Viro fraggap); 14121da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 14131da177e4SLinus Torvalds skb->csum); 1414e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds /* 14181da177e4SLinus Torvalds * Put the packet on the pending queue. 14191da177e4SLinus Torvalds */ 14201da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 14211da177e4SLinus Torvalds continue; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds if (len > size) 14251da177e4SLinus Torvalds len = size; 1426be12a1feSHannes Frederic Sowa 1427be12a1feSHannes Frederic Sowa if (skb_append_pagefrags(skb, page, offset, len)) { 14281da177e4SLinus Torvalds err = -EMSGSIZE; 14291da177e4SLinus Torvalds goto error; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds if (skb->ip_summed == CHECKSUM_NONE) { 143344bb9363SAl Viro __wsum csum; 14341da177e4SLinus Torvalds csum = csum_page(page, offset, len); 14351da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, skb->len); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds skb->len += len; 14391da177e4SLinus Torvalds skb->data_len += len; 14401e34a11dSDavid S. Miller skb->truesize += len; 144114afee4bSReshetova, Elena refcount_add(len, &sk->sk_wmem_alloc); 14421da177e4SLinus Torvalds offset += len; 14431da177e4SLinus Torvalds size -= len; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds return 0; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds error: 1448bdc712b4SDavid S. Miller cork->length -= size; 14495e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 14501da177e4SLinus Torvalds return err; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork) 1454429f08e9SPavel Emelyanov { 14551470ddf7SHerbert Xu cork->flags &= ~IPCORK_OPT; 14561470ddf7SHerbert Xu kfree(cork->opt); 14571470ddf7SHerbert Xu cork->opt = NULL; 14581470ddf7SHerbert Xu dst_release(cork->dst); 14591470ddf7SHerbert Xu cork->dst = NULL; 1460429f08e9SPavel Emelyanov } 1461429f08e9SPavel Emelyanov 14621da177e4SLinus Torvalds /* 14631da177e4SLinus Torvalds * Combined all pending IP fragments on the socket as one IP datagram 14641da177e4SLinus Torvalds * and push them out. 14651da177e4SLinus Torvalds */ 14661c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk, 146777968b78SDavid S. Miller struct flowi4 *fl4, 14681470ddf7SHerbert Xu struct sk_buff_head *queue, 14691470ddf7SHerbert Xu struct inet_cork *cork) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 14721da177e4SLinus Torvalds struct sk_buff **tail_skb; 14731da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 14740388b004SPavel Emelyanov struct net *net = sock_net(sk); 14751da177e4SLinus Torvalds struct ip_options *opt = NULL; 14761470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 14771da177e4SLinus Torvalds struct iphdr *iph; 147876ab608dSAlexey Dobriyan __be16 df = 0; 14791da177e4SLinus Torvalds __u8 ttl; 14801da177e4SLinus Torvalds 148151456b29SIan Morris skb = __skb_dequeue(queue); 148251456b29SIan Morris if (!skb) 14831da177e4SLinus Torvalds goto out; 14841da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1487d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1488bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 14891470ddf7SHerbert Xu while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1490cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 14911da177e4SLinus Torvalds *tail_skb = tmp_skb; 14921da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 14931da177e4SLinus Torvalds skb->len += tmp_skb->len; 14941da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 14951da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 14961da177e4SLinus Torvalds tmp_skb->destructor = NULL; 14971da177e4SLinus Torvalds tmp_skb->sk = NULL; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow 15011da177e4SLinus Torvalds * to fragment the frame generated here. No matter, what transforms 15021da177e4SLinus Torvalds * how transforms change size of the packet, it will come out. 15031da177e4SLinus Torvalds */ 150460ff7467SWANG Cong skb->ignore_df = ip_sk_ignore_df(sk); 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /* DF bit is set when we want to see DF on outgoing frames. 150760ff7467SWANG Cong * If ignore_df is set too, we still allow to fragment this frame 15081da177e4SLinus Torvalds * locally. */ 1509482fc609SHannes Frederic Sowa if (inet->pmtudisc == IP_PMTUDISC_DO || 1510482fc609SHannes Frederic Sowa inet->pmtudisc == IP_PMTUDISC_PROBE || 1511d8d1f30bSChangli Gao (skb->len <= dst_mtu(&rt->dst) && 1512d8d1f30bSChangli Gao ip_dont_fragment(sk, &rt->dst))) 15131da177e4SLinus Torvalds df = htons(IP_DF); 15141da177e4SLinus Torvalds 15151470ddf7SHerbert Xu if (cork->flags & IPCORK_OPT) 15161470ddf7SHerbert Xu opt = cork->opt; 15171da177e4SLinus Torvalds 1518aa661581SFrancesco Fusco if (cork->ttl != 0) 1519aa661581SFrancesco Fusco ttl = cork->ttl; 1520aa661581SFrancesco Fusco else if (rt->rt_type == RTN_MULTICAST) 15211da177e4SLinus Torvalds ttl = inet->mc_ttl; 15221da177e4SLinus Torvalds else 1523d8d1f30bSChangli Gao ttl = ip_select_ttl(inet, &rt->dst); 15241da177e4SLinus Torvalds 1525749154aaSAnsis Atteka iph = ip_hdr(skb); 15261da177e4SLinus Torvalds iph->version = 4; 15271da177e4SLinus Torvalds iph->ihl = 5; 1528aa661581SFrancesco Fusco iph->tos = (cork->tos != -1) ? cork->tos : inet->tos; 15291da177e4SLinus Torvalds iph->frag_off = df; 15301da177e4SLinus Torvalds iph->ttl = ttl; 15311da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 153284f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 1533b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, sk); 15341da177e4SLinus Torvalds 153522f728f8SDavid S. Miller if (opt) { 153622f728f8SDavid S. Miller iph->ihl += opt->optlen >> 2; 153722f728f8SDavid S. Miller ip_options_build(skb, opt, cork->addr, rt, 0); 153822f728f8SDavid S. Miller } 153922f728f8SDavid S. Miller 1540aa661581SFrancesco Fusco skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority; 1541c6af0c22SWillem de Bruijn skb->mark = cork->mark; 1542bc969a97SJesus Sanchez-Palencia skb->tstamp = cork->transmit_time; 1543a21bba94SEric Dumazet /* 1544a21bba94SEric Dumazet * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1545a21bba94SEric Dumazet * on dst refcount 1546a21bba94SEric Dumazet */ 15471470ddf7SHerbert Xu cork->dst = NULL; 1548d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 15491da177e4SLinus Torvalds 155096793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 15510388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 155296793b48SDavid L Stevens skb_transport_header(skb))->type); 155396793b48SDavid L Stevens 15541c32c5adSHerbert Xu ip_cork_release(cork); 15551c32c5adSHerbert Xu out: 15561c32c5adSHerbert Xu return skb; 15571c32c5adSHerbert Xu } 15581c32c5adSHerbert Xu 1559b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb) 15601c32c5adSHerbert Xu { 15611c32c5adSHerbert Xu int err; 15621c32c5adSHerbert Xu 156333224b16SEric W. Biederman err = ip_local_out(net, skb->sk, skb); 15641da177e4SLinus Torvalds if (err) { 15651da177e4SLinus Torvalds if (err > 0) 15666ce9e7b5SEric Dumazet err = net_xmit_errno(err); 15671da177e4SLinus Torvalds if (err) 15681c32c5adSHerbert Xu IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds return err; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 157477968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4) 15751470ddf7SHerbert Xu { 15761c32c5adSHerbert Xu struct sk_buff *skb; 15771c32c5adSHerbert Xu 157877968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 15791c32c5adSHerbert Xu if (!skb) 15801c32c5adSHerbert Xu return 0; 15811c32c5adSHerbert Xu 15821c32c5adSHerbert Xu /* Netfilter gets whole the not fragmented skb. */ 1583b5ec8eeaSEric Dumazet return ip_send_skb(sock_net(sk), skb); 15841470ddf7SHerbert Xu } 15851470ddf7SHerbert Xu 15861da177e4SLinus Torvalds /* 15871da177e4SLinus Torvalds * Throw away all pending data on the socket. 15881da177e4SLinus Torvalds */ 15891470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk, 15901470ddf7SHerbert Xu struct sk_buff_head *queue, 15911470ddf7SHerbert Xu struct inet_cork *cork) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds struct sk_buff *skb; 15941da177e4SLinus Torvalds 15951470ddf7SHerbert Xu while ((skb = __skb_dequeue_tail(queue)) != NULL) 15961da177e4SLinus Torvalds kfree_skb(skb); 15971da177e4SLinus Torvalds 15981470ddf7SHerbert Xu ip_cork_release(cork); 15991470ddf7SHerbert Xu } 16001470ddf7SHerbert Xu 16011470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk) 16021470ddf7SHerbert Xu { 1603bdc712b4SDavid S. Miller __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 16061c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk, 160777968b78SDavid S. Miller struct flowi4 *fl4, 16081c32c5adSHerbert Xu int getfrag(void *from, char *to, int offset, 16091c32c5adSHerbert Xu int len, int odd, struct sk_buff *skb), 16101c32c5adSHerbert Xu void *from, int length, int transhdrlen, 16111c32c5adSHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 16121cd7884dSWillem de Bruijn struct inet_cork *cork, unsigned int flags) 16131c32c5adSHerbert Xu { 16141c32c5adSHerbert Xu struct sk_buff_head queue; 16151c32c5adSHerbert Xu int err; 16161c32c5adSHerbert Xu 16171c32c5adSHerbert Xu if (flags & MSG_PROBE) 16181c32c5adSHerbert Xu return NULL; 16191c32c5adSHerbert Xu 16201c32c5adSHerbert Xu __skb_queue_head_init(&queue); 16211c32c5adSHerbert Xu 16221cd7884dSWillem de Bruijn cork->flags = 0; 16231cd7884dSWillem de Bruijn cork->addr = 0; 16241cd7884dSWillem de Bruijn cork->opt = NULL; 16251cd7884dSWillem de Bruijn err = ip_setup_cork(sk, cork, ipc, rtp); 16261c32c5adSHerbert Xu if (err) 16271c32c5adSHerbert Xu return ERR_PTR(err); 16281c32c5adSHerbert Xu 16291cd7884dSWillem de Bruijn err = __ip_append_data(sk, fl4, &queue, cork, 16305640f768SEric Dumazet ¤t->task_frag, getfrag, 16311c32c5adSHerbert Xu from, length, transhdrlen, flags); 16321c32c5adSHerbert Xu if (err) { 16331cd7884dSWillem de Bruijn __ip_flush_pending_frames(sk, &queue, cork); 16341c32c5adSHerbert Xu return ERR_PTR(err); 16351c32c5adSHerbert Xu } 16361c32c5adSHerbert Xu 16371cd7884dSWillem de Bruijn return __ip_make_skb(sk, fl4, &queue, cork); 16381c32c5adSHerbert Xu } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* 16411da177e4SLinus Torvalds * Fetch data from kernel space and fill in checksum if needed. 16421da177e4SLinus Torvalds */ 16431da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset, 16441da177e4SLinus Torvalds int len, int odd, struct sk_buff *skb) 16451da177e4SLinus Torvalds { 16465084205fSAl Viro __wsum csum; 16471da177e4SLinus Torvalds 1648cc44c17bSAl Viro csum = csum_partial_copy_nocheck(dptr+offset, to, len); 16491da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 16501da177e4SLinus Torvalds return 0; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds /* 16541da177e4SLinus Torvalds * Generic function to send a packet as reply to another packet. 1655be9f4a44SEric Dumazet * Used to send some TCP resets/acks so far. 16561da177e4SLinus Torvalds */ 1657bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, 165824a2d43dSEric Dumazet const struct ip_options *sopt, 165924a2d43dSEric Dumazet __be32 daddr, __be32 saddr, 166024a2d43dSEric Dumazet const struct ip_reply_arg *arg, 1661d6fb396cSEric Dumazet unsigned int len, u64 transmit_time) 16621da177e4SLinus Torvalds { 1663f6d8bd05SEric Dumazet struct ip_options_data replyopts; 16641da177e4SLinus Torvalds struct ipcm_cookie ipc; 166577968b78SDavid S. Miller struct flowi4 fl4; 1666511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1667bdbbb852SEric Dumazet struct net *net = sock_net(sk); 1668be9f4a44SEric Dumazet struct sk_buff *nskb; 16694062090eSVasily Averin int err; 1670f7ba868bSDavid Ahern int oif; 16711da177e4SLinus Torvalds 167291ed1e66SPaolo Abeni if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt)) 16731da177e4SLinus Torvalds return; 16741da177e4SLinus Torvalds 167535178206SWillem de Bruijn ipcm_init(&ipc); 16760a5ebb80SDavid S. Miller ipc.addr = daddr; 1677d6fb396cSEric Dumazet ipc.sockc.transmit_time = transmit_time; 16781da177e4SLinus Torvalds 1679f6d8bd05SEric Dumazet if (replyopts.opt.opt.optlen) { 16801da177e4SLinus Torvalds ipc.opt = &replyopts.opt; 16811da177e4SLinus Torvalds 1682f6d8bd05SEric Dumazet if (replyopts.opt.opt.srr) 1683f6d8bd05SEric Dumazet daddr = replyopts.opt.opt.faddr; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 1686f7ba868bSDavid Ahern oif = arg->bound_dev_if; 16879b6c14d5SDavid Ahern if (!oif && netif_index_is_l3_master(net, skb->skb_iif)) 16889b6c14d5SDavid Ahern oif = skb->skb_iif; 1689f7ba868bSDavid Ahern 1690f7ba868bSDavid Ahern flowi4_init_output(&fl4, oif, 169100483690SJon Maxwell IP4_REPLY_MARK(net, skb->mark) ?: sk->sk_mark, 169266b13d99SEric Dumazet RT_TOS(arg->tos), 1693be9f4a44SEric Dumazet RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol, 1694538de0e0SDavid S. Miller ip_reply_arg_flowi_flags(arg), 169570e73416SDavid S. Miller daddr, saddr, 1696e2d118a1SLorenzo Colitti tcp_hdr(skb)->source, tcp_hdr(skb)->dest, 1697e2d118a1SLorenzo Colitti arg->uid); 16983df98d79SPaul Moore security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4)); 1699be9f4a44SEric Dumazet rt = ip_route_output_key(net, &fl4); 1700b23dd4feSDavid S. Miller if (IS_ERR(rt)) 17011da177e4SLinus Torvalds return; 17021da177e4SLinus Torvalds 1703ba9e04a7SWei Wang inet_sk(sk)->tos = arg->tos & ~INET_ECN_MASK; 17041da177e4SLinus Torvalds 1705eddc9ec5SArnaldo Carvalho de Melo sk->sk_protocol = ip_hdr(skb)->protocol; 1706f0e48dbfSPatrick McHardy sk->sk_bound_dev_if = arg->bound_dev_if; 1707be9f4a44SEric Dumazet sk->sk_sndbuf = sysctl_wmem_default; 17080da7536fSWillem de Bruijn ipc.sockc.mark = fl4.flowi4_mark; 17094062090eSVasily Averin err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, 17104062090eSVasily Averin len, 0, &ipc, &rt, MSG_DONTWAIT); 17114062090eSVasily Averin if (unlikely(err)) { 17124062090eSVasily Averin ip_flush_pending_frames(sk); 17134062090eSVasily Averin goto out; 17144062090eSVasily Averin } 17154062090eSVasily Averin 1716be9f4a44SEric Dumazet nskb = skb_peek(&sk->sk_write_queue); 1717be9f4a44SEric Dumazet if (nskb) { 17181da177e4SLinus Torvalds if (arg->csumoffset >= 0) 1719be9f4a44SEric Dumazet *((__sum16 *)skb_transport_header(nskb) + 1720be9f4a44SEric Dumazet arg->csumoffset) = csum_fold(csum_add(nskb->csum, 17219c70220bSArnaldo Carvalho de Melo arg->csum)); 1722be9f4a44SEric Dumazet nskb->ip_summed = CHECKSUM_NONE; 172377968b78SDavid S. Miller ip_push_pending_frames(sk, &fl4); 17241da177e4SLinus Torvalds } 17254062090eSVasily Averin out: 17261da177e4SLinus Torvalds ip_rt_put(rt); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds void __init ip_init(void) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds ip_rt_init(); 17321da177e4SLinus Torvalds inet_initpeers(); 17331da177e4SLinus Torvalds 173472c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST) 173572c1d3bdSWANG Cong igmp_mc_init(); 17361da177e4SLinus Torvalds #endif 17371da177e4SLinus Torvalds } 1738