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 371da177e4SLinus Torvalds * and more readibility. 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> 7714972cbdSRoopa Prabhu #include <net/lwtunnel.h> 7833b48679SDaniel Mack #include <linux/bpf-cgroup.h> 791da177e4SLinus Torvalds #include <linux/igmp.h> 801da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 811da177e4SLinus Torvalds #include <linux/netfilter_bridge.h> 821da177e4SLinus Torvalds #include <linux/netlink.h> 836cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h> 841da177e4SLinus Torvalds 85694869b3SEric W. Biederman static int 86694869b3SEric W. Biederman ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 87c5501eb3SFlorian Westphal unsigned int mtu, 88694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)); 8949d16b23SAndy Zhou 901da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */ 912fbd9679SDenis Efremov void ip_send_check(struct iphdr *iph) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds iph->check = 0; 941da177e4SLinus Torvalds iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 951da177e4SLinus Torvalds } 964bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check); 971da177e4SLinus Torvalds 98cf91a99dSEric W. Biederman int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 99c439cb2eSHerbert Xu { 100c439cb2eSHerbert Xu struct iphdr *iph = ip_hdr(skb); 101c439cb2eSHerbert Xu 102c439cb2eSHerbert Xu iph->tot_len = htons(skb->len); 103c439cb2eSHerbert Xu ip_send_check(iph); 104a8e3e1a9SDavid Ahern 105a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 106a8e3e1a9SDavid Ahern * skb to its handler for processing 107a8e3e1a9SDavid Ahern */ 108a8e3e1a9SDavid Ahern skb = l3mdev_ip_out(sk, skb); 109a8e3e1a9SDavid Ahern if (unlikely(!skb)) 110a8e3e1a9SDavid Ahern return 0; 111a8e3e1a9SDavid Ahern 112f4180439SEli Cooper skb->protocol = htons(ETH_P_IP); 113f4180439SEli Cooper 11429a26a56SEric W. Biederman return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, 11529a26a56SEric W. Biederman net, sk, skb, NULL, skb_dst(skb)->dev, 11613206b6bSEric W. Biederman dst_output); 1177026b1ddSDavid Miller } 1187026b1ddSDavid Miller 11933224b16SEric W. Biederman int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) 120c439cb2eSHerbert Xu { 121c439cb2eSHerbert Xu int err; 122c439cb2eSHerbert Xu 123cf91a99dSEric W. Biederman err = __ip_local_out(net, sk, skb); 124c439cb2eSHerbert Xu if (likely(err == 1)) 12513206b6bSEric W. Biederman err = dst_output(net, sk, skb); 126c439cb2eSHerbert Xu 127c439cb2eSHerbert Xu return err; 128c439cb2eSHerbert Xu } 129e2cb77dbSEric W. Biederman EXPORT_SYMBOL_GPL(ip_local_out); 130c439cb2eSHerbert Xu 1311da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds int ttl = inet->uc_ttl; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds if (ttl < 0) 136323e126fSDavid S. Miller ttl = ip4_dst_hoplimit(dst); 1371da177e4SLinus Torvalds return ttl; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* 1411da177e4SLinus Torvalds * Add an ip header to a skbuff and send it out. 1421da177e4SLinus Torvalds * 1431da177e4SLinus Torvalds */ 144cfe673b0SEric Dumazet int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, 145f6d8bd05SEric Dumazet __be32 saddr, __be32 daddr, struct ip_options_rcu *opt) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 148511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 14977589ce0SEric W. Biederman struct net *net = sock_net(sk); 1501da177e4SLinus Torvalds struct iphdr *iph; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* Build the IP header. */ 153f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0)); 1548856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 155eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 1561da177e4SLinus Torvalds iph->version = 4; 1571da177e4SLinus Torvalds iph->ihl = 5; 1581da177e4SLinus Torvalds iph->tos = inet->tos; 159d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 160dd927a26SDavid S. Miller iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); 161dd927a26SDavid S. Miller iph->saddr = saddr; 1621da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 163cfe673b0SEric Dumazet if (ip_dont_fragment(sk, &rt->dst)) { 164cfe673b0SEric Dumazet iph->frag_off = htons(IP_DF); 165cfe673b0SEric Dumazet iph->id = 0; 166cfe673b0SEric Dumazet } else { 167cfe673b0SEric Dumazet iph->frag_off = 0; 16877589ce0SEric W. Biederman __ip_select_ident(net, iph, 1); 169cfe673b0SEric Dumazet } 1701da177e4SLinus Torvalds 171f6d8bd05SEric Dumazet if (opt && opt->opt.optlen) { 172f6d8bd05SEric Dumazet iph->ihl += opt->opt.optlen>>2; 173f6d8bd05SEric Dumazet ip_options_build(skb, &opt->opt, daddr, rt, 0); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds skb->priority = sk->sk_priority; 177e05a90ecSJamal Hadi Salim if (!skb->mark) 1784a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* Send it out. */ 18133224b16SEric W. Biederman return ip_local_out(net, skb->sk, skb); 1821da177e4SLinus Torvalds } 183d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 184d8c97a94SArnaldo Carvalho de Melo 185694869b3SEric W. Biederman static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 1861da177e4SLinus Torvalds { 187adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 18880787ebcSMitsuru Chinen struct rtable *rt = (struct rtable *)dst; 1891da177e4SLinus Torvalds struct net_device *dev = dst->dev; 190c2636b4dSChuck Lever unsigned int hh_len = LL_RESERVED_SPACE(dev); 191f6b72b62SDavid S. Miller struct neighbour *neigh; 1925c9f7c1dSDavid Ahern bool is_v6gw = false; 1931da177e4SLinus Torvalds 194edf391ffSNeil Horman if (rt->rt_type == RTN_MULTICAST) { 1954ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len); 196edf391ffSNeil Horman } else if (rt->rt_type == RTN_BROADCAST) 1974ba1bf42SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len); 19880787ebcSMitsuru Chinen 1991da177e4SLinus Torvalds /* Be paranoid, rather than too clever. */ 2003b04dddeSStephen Hemminger if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 2011da177e4SLinus Torvalds struct sk_buff *skb2; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); 20451456b29SIan Morris if (!skb2) { 2051da177e4SLinus Torvalds kfree_skb(skb); 2061da177e4SLinus Torvalds return -ENOMEM; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds if (skb->sk) 2091da177e4SLinus Torvalds skb_set_owner_w(skb2, skb->sk); 2105d0ba55bSEric Dumazet consume_skb(skb); 2111da177e4SLinus Torvalds skb = skb2; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 21414972cbdSRoopa Prabhu if (lwtunnel_xmit_redirect(dst->lwtstate)) { 21514972cbdSRoopa Prabhu int res = lwtunnel_xmit(skb); 21614972cbdSRoopa Prabhu 21714972cbdSRoopa Prabhu if (res < 0 || res == LWTUNNEL_XMIT_DONE) 21814972cbdSRoopa Prabhu return res; 21914972cbdSRoopa Prabhu } 22014972cbdSRoopa Prabhu 221a263b309SDavid S. Miller rcu_read_lock_bh(); 2225c9f7c1dSDavid Ahern neigh = ip_neigh_for_gw(rt, skb, &is_v6gw); 2239871f1adSVasiliy Kulikov if (!IS_ERR(neigh)) { 2244ff06203SJulian Anastasov int res; 2254ff06203SJulian Anastasov 2264ff06203SJulian Anastasov sock_confirm_neigh(skb, neigh); 2275c9f7c1dSDavid Ahern /* if crossing protocols, can not use the cached header */ 2285c9f7c1dSDavid Ahern res = neigh_output(neigh, skb, is_v6gw); 229a263b309SDavid S. Miller rcu_read_unlock_bh(); 230f2c31e32SEric Dumazet return res; 231f2c31e32SEric Dumazet } 232a263b309SDavid S. Miller rcu_read_unlock_bh(); 23305e3aa09SDavid S. Miller 234e87cc472SJoe Perches net_dbg_ratelimited("%s: No header cache and no neighbour!\n", 235e87cc472SJoe Perches __func__); 2361da177e4SLinus Torvalds kfree_skb(skb); 2371da177e4SLinus Torvalds return -EINVAL; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240694869b3SEric W. Biederman static int ip_finish_output_gso(struct net *net, struct sock *sk, 241694869b3SEric W. Biederman struct sk_buff *skb, unsigned int mtu) 242c7ba65d7SFlorian Westphal { 243c7ba65d7SFlorian Westphal netdev_features_t features; 244c7ba65d7SFlorian Westphal struct sk_buff *segs; 245c7ba65d7SFlorian Westphal int ret = 0; 246c7ba65d7SFlorian Westphal 2479ee6c5dcSLance Richardson /* common case: seglen is <= mtu 248359ebda2SShmulik Ladkani */ 249779b7931SDaniel Axtens if (skb_gso_validate_network_len(skb, mtu)) 250694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 251c7ba65d7SFlorian Westphal 2520ace81ecSLance Richardson /* Slowpath - GSO segment length exceeds the egress MTU. 253c7ba65d7SFlorian Westphal * 2540ace81ecSLance Richardson * This can happen in several cases: 2550ace81ecSLance Richardson * - Forwarding of a TCP GRO skb, when DF flag is not set. 2560ace81ecSLance Richardson * - Forwarding of an skb that arrived on a virtualization interface 2570ace81ecSLance Richardson * (virtio-net/vhost/tap) with TSO/GSO size set by other network 2580ace81ecSLance Richardson * stack. 2590ace81ecSLance Richardson * - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an 2600ace81ecSLance Richardson * interface with a smaller MTU. 2610ace81ecSLance Richardson * - Arriving GRO skb (or GSO skb in a virtualized environment) that is 2620ace81ecSLance Richardson * bridged to a NETIF_F_TSO tunnel stacked over an interface with an 2630ace81ecSLance Richardson * insufficent MTU. 264c7ba65d7SFlorian Westphal */ 265c7ba65d7SFlorian Westphal features = netif_skb_features(skb); 2669207f9d4SKonstantin Khlebnikov BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET); 267c7ba65d7SFlorian Westphal segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 268330966e5SFlorian Westphal if (IS_ERR_OR_NULL(segs)) { 269c7ba65d7SFlorian Westphal kfree_skb(skb); 270c7ba65d7SFlorian Westphal return -ENOMEM; 271c7ba65d7SFlorian Westphal } 272c7ba65d7SFlorian Westphal 273c7ba65d7SFlorian Westphal consume_skb(skb); 274c7ba65d7SFlorian Westphal 275c7ba65d7SFlorian Westphal do { 276c7ba65d7SFlorian Westphal struct sk_buff *nskb = segs->next; 277c7ba65d7SFlorian Westphal int err; 278c7ba65d7SFlorian Westphal 279a8305bffSDavid S. Miller skb_mark_not_on_list(segs); 280694869b3SEric W. Biederman err = ip_fragment(net, sk, segs, mtu, ip_finish_output2); 281c7ba65d7SFlorian Westphal 282c7ba65d7SFlorian Westphal if (err && ret == 0) 283c7ba65d7SFlorian Westphal ret = err; 284c7ba65d7SFlorian Westphal segs = nskb; 285c7ba65d7SFlorian Westphal } while (segs); 286c7ba65d7SFlorian Westphal 287c7ba65d7SFlorian Westphal return ret; 288c7ba65d7SFlorian Westphal } 289c7ba65d7SFlorian Westphal 290956fe219Sbrakmo static int __ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 2911da177e4SLinus Torvalds { 292c5501eb3SFlorian Westphal unsigned int mtu; 293c5501eb3SFlorian Westphal 2945c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 2955c901daaSPatrick McHardy /* Policy lookup after SNAT yielded a new policy */ 29600db4124SIan Morris if (skb_dst(skb)->xfrm) { 29748d5cad8SPatrick McHardy IPCB(skb)->flags |= IPSKB_REROUTED; 29813206b6bSEric W. Biederman return dst_output(net, sk, skb); 29948d5cad8SPatrick McHardy } 3005c901daaSPatrick McHardy #endif 301fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 302c7ba65d7SFlorian Westphal if (skb_is_gso(skb)) 303694869b3SEric W. Biederman return ip_finish_output_gso(net, sk, skb, mtu); 304c7ba65d7SFlorian Westphal 305d6b915e2SFlorian Westphal if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU)) 306694869b3SEric W. Biederman return ip_fragment(net, sk, skb, mtu, ip_finish_output2); 307c7ba65d7SFlorian Westphal 308694869b3SEric W. Biederman return ip_finish_output2(net, sk, skb); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 311956fe219Sbrakmo static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 312956fe219Sbrakmo { 313956fe219Sbrakmo int ret; 314956fe219Sbrakmo 315956fe219Sbrakmo ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 316956fe219Sbrakmo switch (ret) { 317956fe219Sbrakmo case NET_XMIT_SUCCESS: 318956fe219Sbrakmo return __ip_finish_output(net, sk, skb); 319956fe219Sbrakmo case NET_XMIT_CN: 320956fe219Sbrakmo return __ip_finish_output(net, sk, skb) ? : ret; 321956fe219Sbrakmo default: 322956fe219Sbrakmo kfree_skb(skb); 323956fe219Sbrakmo return ret; 324956fe219Sbrakmo } 325956fe219Sbrakmo } 326956fe219Sbrakmo 32733b48679SDaniel Mack static int ip_mc_finish_output(struct net *net, struct sock *sk, 32833b48679SDaniel Mack struct sk_buff *skb) 32933b48679SDaniel Mack { 3305b18f128SStephen Suryaputra struct rtable *new_rt; 331d96ff269SDavid S. Miller bool do_cn = false; 332d96ff269SDavid S. Miller int ret, err; 33333b48679SDaniel Mack 33433b48679SDaniel Mack ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 335956fe219Sbrakmo switch (ret) { 336956fe219Sbrakmo case NET_XMIT_CN: 337d96ff269SDavid S. Miller do_cn = true; 338d96ff269SDavid S. Miller /* fall through */ 339d96ff269SDavid S. Miller case NET_XMIT_SUCCESS: 340d96ff269SDavid S. Miller break; 341956fe219Sbrakmo default: 34233b48679SDaniel Mack kfree_skb(skb); 34333b48679SDaniel Mack return ret; 34433b48679SDaniel Mack } 34533b48679SDaniel Mack 3465b18f128SStephen Suryaputra /* Reset rt_iif so that inet_iif() will return skb->skb_iif. Setting 3475b18f128SStephen Suryaputra * this to non-zero causes ipi_ifindex in in_pktinfo to be overwritten, 3485b18f128SStephen Suryaputra * see ipv4_pktinfo_prepare(). 3495b18f128SStephen Suryaputra */ 3505b18f128SStephen Suryaputra new_rt = rt_dst_clone(net->loopback_dev, skb_rtable(skb)); 3515b18f128SStephen Suryaputra if (new_rt) { 3525b18f128SStephen Suryaputra new_rt->rt_iif = 0; 3535b18f128SStephen Suryaputra skb_dst_drop(skb); 3545b18f128SStephen Suryaputra skb_dst_set(skb, &new_rt->dst); 3555b18f128SStephen Suryaputra } 3565b18f128SStephen Suryaputra 357d96ff269SDavid S. Miller err = dev_loopback_xmit(net, sk, skb); 358d96ff269SDavid S. Miller return (do_cn && err) ? ret : err; 35933b48679SDaniel Mack } 36033b48679SDaniel Mack 361ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb) 3621da177e4SLinus Torvalds { 363511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 364d8d1f30bSChangli Gao struct net_device *dev = rt->dst.dev; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* 3671da177e4SLinus Torvalds * If the indicated interface is up and running, send the packet. 3681da177e4SLinus Torvalds */ 36988f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds skb->dev = dev; 3721da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds /* 3751da177e4SLinus Torvalds * Multicasts are looped back for other local users 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds if (rt->rt_flags&RTCF_MULTICAST) { 3797ad6848cSOctavian Purdila if (sk_mc_loop(sk) 3801da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE 3811da177e4SLinus Torvalds /* Small optimization: do not loopback not local frames, 3821da177e4SLinus Torvalds which returned after forwarding; they will be dropped 3831da177e4SLinus Torvalds by ip_mr_input in any case. 3841da177e4SLinus Torvalds Note, that local frames are looped back to be delivered 3851da177e4SLinus Torvalds to local recipients. 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds This check is duplicated in ip_mr_input at the moment. 3881da177e4SLinus Torvalds */ 3899d4fb27dSJoe Perches && 3909d4fb27dSJoe Perches ((rt->rt_flags & RTCF_LOCAL) || 3919d4fb27dSJoe Perches !(IPCB(skb)->flags & IPSKB_FORWARDED)) 3921da177e4SLinus Torvalds #endif 3931da177e4SLinus Torvalds ) { 3941da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 3951da177e4SLinus Torvalds if (newskb) 3969bbc768aSJan Engelhardt NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 39729a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 39833b48679SDaniel Mack ip_mc_finish_output); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* Multicasts with ttl 0 must not go beyond the host */ 4021da177e4SLinus Torvalds 403eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->ttl == 0) { 4041da177e4SLinus Torvalds kfree_skb(skb); 4051da177e4SLinus Torvalds return 0; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (rt->rt_flags&RTCF_BROADCAST) { 4101da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 4111da177e4SLinus Torvalds if (newskb) 41229a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 41329a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 41433b48679SDaniel Mack ip_mc_finish_output); 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 41729a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 41829a26a56SEric W. Biederman net, sk, skb, NULL, skb->dev, 41929a26a56SEric W. Biederman ip_finish_output, 42048d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 423ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb) 4241da177e4SLinus Torvalds { 425*28f8bfd1SPhil Sutter struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev; 4261bd9bef6SPatrick McHardy 42788f5cc24SEric W. Biederman IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 4281da177e4SLinus Torvalds 4291bd9bef6SPatrick McHardy skb->dev = dev; 4301bd9bef6SPatrick McHardy skb->protocol = htons(ETH_P_IP); 4311bd9bef6SPatrick McHardy 43229a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 433*28f8bfd1SPhil Sutter net, sk, skb, indev, dev, 43448d5cad8SPatrick McHardy ip_finish_output, 43548d5cad8SPatrick McHardy !(IPCB(skb)->flags & IPSKB_REROUTED)); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 43884f9307cSEric Dumazet /* 43984f9307cSEric Dumazet * copy saddr and daddr, possibly using 64bit load/stores 44084f9307cSEric Dumazet * Equivalent to : 44184f9307cSEric Dumazet * iph->saddr = fl4->saddr; 44284f9307cSEric Dumazet * iph->daddr = fl4->daddr; 44384f9307cSEric Dumazet */ 44484f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4) 44584f9307cSEric Dumazet { 44684f9307cSEric Dumazet BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) != 44784f9307cSEric Dumazet offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr)); 44884f9307cSEric Dumazet memcpy(&iph->saddr, &fl4->saddr, 44984f9307cSEric Dumazet sizeof(fl4->saddr) + sizeof(fl4->daddr)); 45084f9307cSEric Dumazet } 45184f9307cSEric Dumazet 452b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */ 45369b9e1e0SXin Long int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl, 45469b9e1e0SXin Long __u8 tos) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 45777589ce0SEric W. Biederman struct net *net = sock_net(sk); 458f6d8bd05SEric Dumazet struct ip_options_rcu *inet_opt; 459b57ae01aSDavid S. Miller struct flowi4 *fl4; 4601da177e4SLinus Torvalds struct rtable *rt; 4611da177e4SLinus Torvalds struct iphdr *iph; 462ab6e3febSEric Dumazet int res; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* Skip all of this if the packet is already routed, 4651da177e4SLinus Torvalds * f.e. by something like SCTP. 4661da177e4SLinus Torvalds */ 467ab6e3febSEric Dumazet rcu_read_lock(); 468f6d8bd05SEric Dumazet inet_opt = rcu_dereference(inet->inet_opt); 469ea4fc0d6SDavid S. Miller fl4 = &fl->u.ip4; 470511c3f92SEric Dumazet rt = skb_rtable(skb); 47100db4124SIan Morris if (rt) 4721da177e4SLinus Torvalds goto packet_routed; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* Make sure we can route this packet. */ 4751da177e4SLinus Torvalds rt = (struct rtable *)__sk_dst_check(sk, 0); 47651456b29SIan Morris if (!rt) { 4773ca3c68eSAl Viro __be32 daddr; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* Use correct destination address if we have options. */ 480c720c7e8SEric Dumazet daddr = inet->inet_daddr; 481f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.srr) 482f6d8bd05SEric Dumazet daddr = inet_opt->opt.faddr; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* If this fails, retransmit mechanism of transport layer will 4851da177e4SLinus Torvalds * keep trying until route appears or the connection times 4861da177e4SLinus Torvalds * itself out. 4871da177e4SLinus Torvalds */ 48877589ce0SEric W. Biederman rt = ip_route_output_ports(net, fl4, sk, 48978fbfd8aSDavid S. Miller daddr, inet->inet_saddr, 49078fbfd8aSDavid S. Miller inet->inet_dport, 49178fbfd8aSDavid S. Miller inet->inet_sport, 49278fbfd8aSDavid S. Miller sk->sk_protocol, 49369b9e1e0SXin Long RT_CONN_FLAGS_TOS(sk, tos), 49478fbfd8aSDavid S. Miller sk->sk_bound_dev_if); 495b23dd4feSDavid S. Miller if (IS_ERR(rt)) 4961da177e4SLinus Torvalds goto no_route; 497d8d1f30bSChangli Gao sk_setup_caps(sk, &rt->dst); 4981da177e4SLinus Torvalds } 499d8d1f30bSChangli Gao skb_dst_set_noref(skb, &rt->dst); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds packet_routed: 50277d5bc7eSDavid Ahern if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway) 5031da177e4SLinus Torvalds goto no_route; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* OK, we know where to send it, allocate and build IP header. */ 506f6d8bd05SEric Dumazet skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0)); 5078856dfa3SArnaldo Carvalho de Melo skb_reset_network_header(skb); 508eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 50969b9e1e0SXin Long *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (tos & 0xff)); 51060ff7467SWANG Cong if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df) 5111da177e4SLinus Torvalds iph->frag_off = htons(IP_DF); 5121da177e4SLinus Torvalds else 5131da177e4SLinus Torvalds iph->frag_off = 0; 514d8d1f30bSChangli Gao iph->ttl = ip_select_ttl(inet, &rt->dst); 5151da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 51684f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 51784f9307cSEric Dumazet 5181da177e4SLinus Torvalds /* Transport layer set skb->h.foo itself. */ 5191da177e4SLinus Torvalds 520f6d8bd05SEric Dumazet if (inet_opt && inet_opt->opt.optlen) { 521f6d8bd05SEric Dumazet iph->ihl += inet_opt->opt.optlen >> 2; 522f6d8bd05SEric Dumazet ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 52577589ce0SEric W. Biederman ip_select_ident_segs(net, skb, sk, 526b6a7719aSHannes Frederic Sowa skb_shinfo(skb)->gso_segs ?: 1); 5271da177e4SLinus Torvalds 528b0270e91SEric Dumazet /* TODO : should we use skb->sk here instead of sk ? */ 5291da177e4SLinus Torvalds skb->priority = sk->sk_priority; 5304a19ec58SLaszlo Attila Toth skb->mark = sk->sk_mark; 5311da177e4SLinus Torvalds 53233224b16SEric W. Biederman res = ip_local_out(net, sk, skb); 533ab6e3febSEric Dumazet rcu_read_unlock(); 534ab6e3febSEric Dumazet return res; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds no_route: 537ab6e3febSEric Dumazet rcu_read_unlock(); 53877589ce0SEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); 5391da177e4SLinus Torvalds kfree_skb(skb); 5401da177e4SLinus Torvalds return -EHOSTUNREACH; 5411da177e4SLinus Torvalds } 54269b9e1e0SXin Long EXPORT_SYMBOL(__ip_queue_xmit); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 5451da177e4SLinus Torvalds { 5461da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 5471da177e4SLinus Torvalds to->priority = from->priority; 5481da177e4SLinus Torvalds to->protocol = from->protocol; 549d2f0c961SShmulik Ladkani to->skb_iif = from->skb_iif; 550adf30907SEric Dumazet skb_dst_drop(to); 551fe76cda3SEric Dumazet skb_dst_copy(to, from); 5521da177e4SLinus Torvalds to->dev = from->dev; 55382e91ffeSThomas Graf to->mark = from->mark; 5541da177e4SLinus Torvalds 5553dd1c9a1SPaolo Abeni skb_copy_hash(to, from); 5563dd1c9a1SPaolo Abeni 5571da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5581da177e4SLinus Torvalds to->tc_index = from->tc_index; 5591da177e4SLinus Torvalds #endif 560e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 561df5042f4SFlorian Westphal skb_ext_copy(to, from); 5626ca40d4eSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IP_VS) 563c98d80edSJulian Anastasov to->ipvs_property = from->ipvs_property; 564c98d80edSJulian Anastasov #endif 565984bc16cSJames Morris skb_copy_secmark(to, from); 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 568694869b3SEric W. Biederman static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 569c5501eb3SFlorian Westphal unsigned int mtu, 570694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 57149d16b23SAndy Zhou { 57249d16b23SAndy Zhou struct iphdr *iph = ip_hdr(skb); 57349d16b23SAndy Zhou 574d6b915e2SFlorian Westphal if ((iph->frag_off & htons(IP_DF)) == 0) 575694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 576d6b915e2SFlorian Westphal 577d6b915e2SFlorian Westphal if (unlikely(!skb->ignore_df || 57849d16b23SAndy Zhou (IPCB(skb)->frag_max_size && 57949d16b23SAndy Zhou IPCB(skb)->frag_max_size > mtu))) { 5809479b0afSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 58149d16b23SAndy Zhou icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, 58249d16b23SAndy Zhou htonl(mtu)); 58349d16b23SAndy Zhou kfree_skb(skb); 58449d16b23SAndy Zhou return -EMSGSIZE; 58549d16b23SAndy Zhou } 58649d16b23SAndy Zhou 587694869b3SEric W. Biederman return ip_do_fragment(net, sk, skb, output); 58849d16b23SAndy Zhou } 58949d16b23SAndy Zhou 590c8b17be0SPablo Neira Ayuso void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph, 591c8b17be0SPablo Neira Ayuso unsigned int hlen, struct ip_fraglist_iter *iter) 592c8b17be0SPablo Neira Ayuso { 593c8b17be0SPablo Neira Ayuso unsigned int first_len = skb_pagelen(skb); 594c8b17be0SPablo Neira Ayuso 595b7034146SEric Dumazet iter->frag = skb_shinfo(skb)->frag_list; 596c8b17be0SPablo Neira Ayuso skb_frag_list_init(skb); 597c8b17be0SPablo Neira Ayuso 598c8b17be0SPablo Neira Ayuso iter->offset = 0; 599c8b17be0SPablo Neira Ayuso iter->iph = iph; 600c8b17be0SPablo Neira Ayuso iter->hlen = hlen; 601c8b17be0SPablo Neira Ayuso 602c8b17be0SPablo Neira Ayuso skb->data_len = first_len - skb_headlen(skb); 603c8b17be0SPablo Neira Ayuso skb->len = first_len; 604c8b17be0SPablo Neira Ayuso iph->tot_len = htons(first_len); 605c8b17be0SPablo Neira Ayuso iph->frag_off = htons(IP_MF); 606c8b17be0SPablo Neira Ayuso ip_send_check(iph); 607c8b17be0SPablo Neira Ayuso } 608c8b17be0SPablo Neira Ayuso EXPORT_SYMBOL(ip_fraglist_init); 609c8b17be0SPablo Neira Ayuso 61019c3401aSPablo Neira Ayuso static void ip_fraglist_ipcb_prepare(struct sk_buff *skb, 61119c3401aSPablo Neira Ayuso struct ip_fraglist_iter *iter) 61219c3401aSPablo Neira Ayuso { 61319c3401aSPablo Neira Ayuso struct sk_buff *to = iter->frag; 61419c3401aSPablo Neira Ayuso 61519c3401aSPablo Neira Ayuso /* Copy the flags to each fragment. */ 61619c3401aSPablo Neira Ayuso IPCB(to)->flags = IPCB(skb)->flags; 61719c3401aSPablo Neira Ayuso 61819c3401aSPablo Neira Ayuso if (iter->offset == 0) 61919c3401aSPablo Neira Ayuso ip_options_fragment(to); 62019c3401aSPablo Neira Ayuso } 62119c3401aSPablo Neira Ayuso 622c8b17be0SPablo Neira Ayuso void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter) 623c8b17be0SPablo Neira Ayuso { 624c8b17be0SPablo Neira Ayuso unsigned int hlen = iter->hlen; 625c8b17be0SPablo Neira Ayuso struct iphdr *iph = iter->iph; 626c8b17be0SPablo Neira Ayuso struct sk_buff *frag; 627c8b17be0SPablo Neira Ayuso 628c8b17be0SPablo Neira Ayuso frag = iter->frag; 629c8b17be0SPablo Neira Ayuso frag->ip_summed = CHECKSUM_NONE; 630c8b17be0SPablo Neira Ayuso skb_reset_transport_header(frag); 631c8b17be0SPablo Neira Ayuso __skb_push(frag, hlen); 632c8b17be0SPablo Neira Ayuso skb_reset_network_header(frag); 633c8b17be0SPablo Neira Ayuso memcpy(skb_network_header(frag), iph, hlen); 634c8b17be0SPablo Neira Ayuso iter->iph = ip_hdr(frag); 635c8b17be0SPablo Neira Ayuso iph = iter->iph; 636c8b17be0SPablo Neira Ayuso iph->tot_len = htons(frag->len); 637c8b17be0SPablo Neira Ayuso ip_copy_metadata(frag, skb); 638c8b17be0SPablo Neira Ayuso iter->offset += skb->len - hlen; 639c8b17be0SPablo Neira Ayuso iph->frag_off = htons(iter->offset >> 3); 640c8b17be0SPablo Neira Ayuso if (frag->next) 641c8b17be0SPablo Neira Ayuso iph->frag_off |= htons(IP_MF); 642c8b17be0SPablo Neira Ayuso /* Ready, complete checksum */ 643c8b17be0SPablo Neira Ayuso ip_send_check(iph); 644c8b17be0SPablo Neira Ayuso } 645c8b17be0SPablo Neira Ayuso EXPORT_SYMBOL(ip_fraglist_prepare); 646c8b17be0SPablo Neira Ayuso 647065ff79fSPablo Neira Ayuso void ip_frag_init(struct sk_buff *skb, unsigned int hlen, 648e7a409c3SEric Dumazet unsigned int ll_rs, unsigned int mtu, bool DF, 649065ff79fSPablo Neira Ayuso struct ip_frag_state *state) 650065ff79fSPablo Neira Ayuso { 651065ff79fSPablo Neira Ayuso struct iphdr *iph = ip_hdr(skb); 652065ff79fSPablo Neira Ayuso 653e7a409c3SEric Dumazet state->DF = DF; 654065ff79fSPablo Neira Ayuso state->hlen = hlen; 655065ff79fSPablo Neira Ayuso state->ll_rs = ll_rs; 656065ff79fSPablo Neira Ayuso state->mtu = mtu; 657065ff79fSPablo Neira Ayuso 658065ff79fSPablo Neira Ayuso state->left = skb->len - hlen; /* Space per frame */ 659065ff79fSPablo Neira Ayuso state->ptr = hlen; /* Where to start from */ 660065ff79fSPablo Neira Ayuso 661065ff79fSPablo Neira Ayuso state->offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; 662065ff79fSPablo Neira Ayuso state->not_last_frag = iph->frag_off & htons(IP_MF); 663065ff79fSPablo Neira Ayuso } 664065ff79fSPablo Neira Ayuso EXPORT_SYMBOL(ip_frag_init); 665065ff79fSPablo Neira Ayuso 66619c3401aSPablo Neira Ayuso static void ip_frag_ipcb(struct sk_buff *from, struct sk_buff *to, 66719c3401aSPablo Neira Ayuso bool first_frag, struct ip_frag_state *state) 66819c3401aSPablo Neira Ayuso { 66919c3401aSPablo Neira Ayuso /* Copy the flags to each fragment. */ 67019c3401aSPablo Neira Ayuso IPCB(to)->flags = IPCB(from)->flags; 67119c3401aSPablo Neira Ayuso 67219c3401aSPablo Neira Ayuso /* ANK: dirty, but effective trick. Upgrade options only if 67319c3401aSPablo Neira Ayuso * the segment to be fragmented was THE FIRST (otherwise, 67419c3401aSPablo Neira Ayuso * options are already fixed) and make it ONCE 67519c3401aSPablo Neira Ayuso * on the initial skb, so that all the following fragments 67619c3401aSPablo Neira Ayuso * will inherit fixed options. 67719c3401aSPablo Neira Ayuso */ 67819c3401aSPablo Neira Ayuso if (first_frag) 67919c3401aSPablo Neira Ayuso ip_options_fragment(from); 68019c3401aSPablo Neira Ayuso } 68119c3401aSPablo Neira Ayuso 682065ff79fSPablo Neira Ayuso struct sk_buff *ip_frag_next(struct sk_buff *skb, struct ip_frag_state *state) 683065ff79fSPablo Neira Ayuso { 684065ff79fSPablo Neira Ayuso unsigned int len = state->left; 685065ff79fSPablo Neira Ayuso struct sk_buff *skb2; 686065ff79fSPablo Neira Ayuso struct iphdr *iph; 687065ff79fSPablo Neira Ayuso 688065ff79fSPablo Neira Ayuso len = state->left; 689065ff79fSPablo Neira Ayuso /* IF: it doesn't fit, use 'mtu' - the data space left */ 690065ff79fSPablo Neira Ayuso if (len > state->mtu) 691065ff79fSPablo Neira Ayuso len = state->mtu; 692065ff79fSPablo Neira Ayuso /* IF: we are not sending up to and including the packet end 693065ff79fSPablo Neira Ayuso then align the next start on an eight byte boundary */ 694065ff79fSPablo Neira Ayuso if (len < state->left) { 695065ff79fSPablo Neira Ayuso len &= ~7; 696065ff79fSPablo Neira Ayuso } 697065ff79fSPablo Neira Ayuso 698065ff79fSPablo Neira Ayuso /* Allocate buffer */ 699065ff79fSPablo Neira Ayuso skb2 = alloc_skb(len + state->hlen + state->ll_rs, GFP_ATOMIC); 700065ff79fSPablo Neira Ayuso if (!skb2) 701065ff79fSPablo Neira Ayuso return ERR_PTR(-ENOMEM); 702065ff79fSPablo Neira Ayuso 703065ff79fSPablo Neira Ayuso /* 704065ff79fSPablo Neira Ayuso * Set up data on packet 705065ff79fSPablo Neira Ayuso */ 706065ff79fSPablo Neira Ayuso 707065ff79fSPablo Neira Ayuso ip_copy_metadata(skb2, skb); 708065ff79fSPablo Neira Ayuso skb_reserve(skb2, state->ll_rs); 709065ff79fSPablo Neira Ayuso skb_put(skb2, len + state->hlen); 710065ff79fSPablo Neira Ayuso skb_reset_network_header(skb2); 711065ff79fSPablo Neira Ayuso skb2->transport_header = skb2->network_header + state->hlen; 712065ff79fSPablo Neira Ayuso 713065ff79fSPablo Neira Ayuso /* 714065ff79fSPablo Neira Ayuso * Charge the memory for the fragment to any owner 715065ff79fSPablo Neira Ayuso * it might possess 716065ff79fSPablo Neira Ayuso */ 717065ff79fSPablo Neira Ayuso 718065ff79fSPablo Neira Ayuso if (skb->sk) 719065ff79fSPablo Neira Ayuso skb_set_owner_w(skb2, skb->sk); 720065ff79fSPablo Neira Ayuso 721065ff79fSPablo Neira Ayuso /* 722065ff79fSPablo Neira Ayuso * Copy the packet header into the new buffer. 723065ff79fSPablo Neira Ayuso */ 724065ff79fSPablo Neira Ayuso 725065ff79fSPablo Neira Ayuso skb_copy_from_linear_data(skb, skb_network_header(skb2), state->hlen); 726065ff79fSPablo Neira Ayuso 727065ff79fSPablo Neira Ayuso /* 728065ff79fSPablo Neira Ayuso * Copy a block of the IP datagram. 729065ff79fSPablo Neira Ayuso */ 730065ff79fSPablo Neira Ayuso if (skb_copy_bits(skb, state->ptr, skb_transport_header(skb2), len)) 731065ff79fSPablo Neira Ayuso BUG(); 732065ff79fSPablo Neira Ayuso state->left -= len; 733065ff79fSPablo Neira Ayuso 734065ff79fSPablo Neira Ayuso /* 735065ff79fSPablo Neira Ayuso * Fill in the new header fields. 736065ff79fSPablo Neira Ayuso */ 737065ff79fSPablo Neira Ayuso iph = ip_hdr(skb2); 738065ff79fSPablo Neira Ayuso iph->frag_off = htons((state->offset >> 3)); 739e7a409c3SEric Dumazet if (state->DF) 740e7a409c3SEric Dumazet iph->frag_off |= htons(IP_DF); 741065ff79fSPablo Neira Ayuso 742065ff79fSPablo Neira Ayuso /* 743065ff79fSPablo Neira Ayuso * Added AC : If we are fragmenting a fragment that's not the 744065ff79fSPablo Neira Ayuso * last fragment then keep MF on each bit 745065ff79fSPablo Neira Ayuso */ 746065ff79fSPablo Neira Ayuso if (state->left > 0 || state->not_last_frag) 747065ff79fSPablo Neira Ayuso iph->frag_off |= htons(IP_MF); 748065ff79fSPablo Neira Ayuso state->ptr += len; 749065ff79fSPablo Neira Ayuso state->offset += len; 750065ff79fSPablo Neira Ayuso 751065ff79fSPablo Neira Ayuso iph->tot_len = htons(len + state->hlen); 752065ff79fSPablo Neira Ayuso 753065ff79fSPablo Neira Ayuso ip_send_check(iph); 754065ff79fSPablo Neira Ayuso 755065ff79fSPablo Neira Ayuso return skb2; 756065ff79fSPablo Neira Ayuso } 757065ff79fSPablo Neira Ayuso EXPORT_SYMBOL(ip_frag_next); 758065ff79fSPablo Neira Ayuso 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * This IP datagram is too large to be sent in one piece. Break it up into 7611da177e4SLinus Torvalds * smaller pieces (each of size equal to IP header plus 7621da177e4SLinus Torvalds * a block of the data of the original IP data part) that will yet fit in a 7631da177e4SLinus Torvalds * single device frame, and queue such a frame for sending. 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds 766694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 767694869b3SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds struct iphdr *iph; 7701da177e4SLinus Torvalds struct sk_buff *skb2; 771511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 772065ff79fSPablo Neira Ayuso unsigned int mtu, hlen, ll_rs; 773c8b17be0SPablo Neira Ayuso struct ip_fraglist_iter iter; 7749669fffcSEric Dumazet ktime_t tstamp = skb->tstamp; 775065ff79fSPablo Neira Ayuso struct ip_frag_state state; 7761da177e4SLinus Torvalds int err = 0; 7771da177e4SLinus Torvalds 778dbd3393cSHannes Frederic Sowa /* for offloaded checksums cleanup checksum before fragmentation */ 779dbd3393cSHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 780dbd3393cSHannes Frederic Sowa (err = skb_checksum_help(skb))) 781dbd3393cSHannes Frederic Sowa goto fail; 782dbd3393cSHannes Frederic Sowa 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * Point into the IP datagram header. 7851da177e4SLinus Torvalds */ 7861da177e4SLinus Torvalds 787eddc9ec5SArnaldo Carvalho de Melo iph = ip_hdr(skb); 7881da177e4SLinus Torvalds 789fedbb6b4SShmulik Ladkani mtu = ip_skb_dst_mtu(sk, skb); 790d6b915e2SFlorian Westphal if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu) 791d6b915e2SFlorian Westphal mtu = IPCB(skb)->frag_max_size; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * Setup starting values. 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds hlen = iph->ihl * 4; 798f87c10a8SHannes Frederic Sowa mtu = mtu - hlen; /* Size of data space */ 79989cee8b1SHerbert Xu IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; 800254d900bSVasily Averin ll_rs = LL_RESERVED_SPACE(rt->dst.dev); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /* When frag_list is given, use it. First, check its validity: 8031da177e4SLinus Torvalds * some transformers could create wrong frag_list or break existing 8041da177e4SLinus Torvalds * one, it is not prohibited. In this case fall back to copying. 8051da177e4SLinus Torvalds * 8061da177e4SLinus Torvalds * LATER: this step can be merged to real generation of fragments, 8071da177e4SLinus Torvalds * we can switch to copy when see the first bad fragment. 8081da177e4SLinus Torvalds */ 80921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 8103d13008eSEric Dumazet struct sk_buff *frag, *frag2; 811c72d8cdaSAlexey Dobriyan unsigned int first_len = skb_pagelen(skb); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds if (first_len - hlen > mtu || 8141da177e4SLinus Torvalds ((first_len - hlen) & 7) || 81556f8a75cSPaul Gortmaker ip_is_fragment(iph) || 816254d900bSVasily Averin skb_cloned(skb) || 817254d900bSVasily Averin skb_headroom(skb) < ll_rs) 8181da177e4SLinus Torvalds goto slow_path; 8191da177e4SLinus Torvalds 820d7fcf1a5SDavid S. Miller skb_walk_frags(skb, frag) { 8211da177e4SLinus Torvalds /* Correct geometry. */ 8221da177e4SLinus Torvalds if (frag->len > mtu || 8231da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 824254d900bSVasily Averin skb_headroom(frag) < hlen + ll_rs) 8253d13008eSEric Dumazet goto slow_path_clean; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* Partially cloned skb? */ 8281da177e4SLinus Torvalds if (skb_shared(frag)) 8293d13008eSEric Dumazet goto slow_path_clean; 8302fdba6b0SHerbert Xu 8312fdba6b0SHerbert Xu BUG_ON(frag->sk); 8322fdba6b0SHerbert Xu if (skb->sk) { 8332fdba6b0SHerbert Xu frag->sk = skb->sk; 8342fdba6b0SHerbert Xu frag->destructor = sock_wfree; 8352fdba6b0SHerbert Xu } 8363d13008eSEric Dumazet skb->truesize -= frag->truesize; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* Everything is OK. Generate! */ 840c8b17be0SPablo Neira Ayuso ip_fraglist_init(skb, iph, hlen, &iter); 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds for (;;) { 8431da177e4SLinus Torvalds /* Prepare header of the next frame, 8441da177e4SLinus Torvalds * before previous one went down. */ 84519c3401aSPablo Neira Ayuso if (iter.frag) { 84619c3401aSPablo Neira Ayuso ip_fraglist_ipcb_prepare(skb, &iter); 847c8b17be0SPablo Neira Ayuso ip_fraglist_prepare(skb, &iter); 84819c3401aSPablo Neira Ayuso } 8491da177e4SLinus Torvalds 8509669fffcSEric Dumazet skb->tstamp = tstamp; 851694869b3SEric W. Biederman err = output(net, sk, skb); 8521da177e4SLinus Torvalds 853dafee490SWei Dong if (!err) 85426a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 855c8b17be0SPablo Neira Ayuso if (err || !iter.frag) 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds 858c8b17be0SPablo Neira Ayuso skb = ip_fraglist_next(&iter); 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds if (err == 0) { 86226a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 8631da177e4SLinus Torvalds return 0; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 866b7034146SEric Dumazet kfree_skb_list(iter.frag); 867942f146aSPablo Neira Ayuso 86826a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 8691da177e4SLinus Torvalds return err; 8703d13008eSEric Dumazet 8713d13008eSEric Dumazet slow_path_clean: 8723d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 8733d13008eSEric Dumazet if (frag2 == frag) 8743d13008eSEric Dumazet break; 8753d13008eSEric Dumazet frag2->sk = NULL; 8763d13008eSEric Dumazet frag2->destructor = NULL; 8773d13008eSEric Dumazet skb->truesize += frag2->truesize; 8783d13008eSEric Dumazet } 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds slow_path: 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * Fragment the datagram. 8841da177e4SLinus Torvalds */ 8851da177e4SLinus Torvalds 886e7a409c3SEric Dumazet ip_frag_init(skb, hlen, ll_rs, mtu, IPCB(skb)->flags & IPSKB_FRAG_PMTU, 887e7a409c3SEric Dumazet &state); 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds /* 8901da177e4SLinus Torvalds * Keep copying data until we run out. 8911da177e4SLinus Torvalds */ 8921da177e4SLinus Torvalds 893065ff79fSPablo Neira Ayuso while (state.left > 0) { 89419c3401aSPablo Neira Ayuso bool first_frag = (state.offset == 0); 89519c3401aSPablo Neira Ayuso 896065ff79fSPablo Neira Ayuso skb2 = ip_frag_next(skb, &state); 897065ff79fSPablo Neira Ayuso if (IS_ERR(skb2)) { 898065ff79fSPablo Neira Ayuso err = PTR_ERR(skb2); 8991da177e4SLinus Torvalds goto fail; 9001da177e4SLinus Torvalds } 90119c3401aSPablo Neira Ayuso ip_frag_ipcb(skb, skb2, first_frag, &state); 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds /* 9041da177e4SLinus Torvalds * Put this fragment into the sending queue. 9051da177e4SLinus Torvalds */ 9069669fffcSEric Dumazet skb2->tstamp = tstamp; 907694869b3SEric W. Biederman err = output(net, sk, skb2); 9081da177e4SLinus Torvalds if (err) 9091da177e4SLinus Torvalds goto fail; 910dafee490SWei Dong 91126a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); 9121da177e4SLinus Torvalds } 9135d0ba55bSEric Dumazet consume_skb(skb); 91426a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); 9151da177e4SLinus Torvalds return err; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds fail: 9181da177e4SLinus Torvalds kfree_skb(skb); 91926a949dbSEric W. Biederman IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 9201da177e4SLinus Torvalds return err; 9211da177e4SLinus Torvalds } 92249d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment); 9232e2f7aefSPatrick McHardy 9241da177e4SLinus Torvalds int 9251da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) 9261da177e4SLinus Torvalds { 927f69e6d13SAl Viro struct msghdr *msg = from; 9281da177e4SLinus Torvalds 92984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 9300b62fca2SAl Viro if (!copy_from_iter_full(to, len, &msg->msg_iter)) 9311da177e4SLinus Torvalds return -EFAULT; 9321da177e4SLinus Torvalds } else { 93344bb9363SAl Viro __wsum csum = 0; 9340b62fca2SAl Viro if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter)) 9351da177e4SLinus Torvalds return -EFAULT; 9361da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds return 0; 9391da177e4SLinus Torvalds } 9404bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag); 9411da177e4SLinus Torvalds 94244bb9363SAl Viro static inline __wsum 9431da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds char *kaddr; 94644bb9363SAl Viro __wsum csum; 9471da177e4SLinus Torvalds kaddr = kmap(page); 9481da177e4SLinus Torvalds csum = csum_partial(kaddr + offset, copy, 0); 9491da177e4SLinus Torvalds kunmap(page); 9501da177e4SLinus Torvalds return csum; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 953f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk, 954f5fca608SDavid S. Miller struct flowi4 *fl4, 955f5fca608SDavid S. Miller struct sk_buff_head *queue, 9561470ddf7SHerbert Xu struct inet_cork *cork, 9575640f768SEric Dumazet struct page_frag *pfrag, 9581470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, 9591470ddf7SHerbert Xu int len, int odd, struct sk_buff *skb), 9601da177e4SLinus Torvalds void *from, int length, int transhdrlen, 9611da177e4SLinus Torvalds unsigned int flags) 9621da177e4SLinus Torvalds { 9631da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 964b5947e5dSWillem de Bruijn struct ubuf_info *uarg = NULL; 9651da177e4SLinus Torvalds struct sk_buff *skb; 9661da177e4SLinus Torvalds 96707df5294SHerbert Xu struct ip_options *opt = cork->opt; 9681da177e4SLinus Torvalds int hh_len; 9691da177e4SLinus Torvalds int exthdrlen; 9701da177e4SLinus Torvalds int mtu; 9711da177e4SLinus Torvalds int copy; 9721da177e4SLinus Torvalds int err; 9731da177e4SLinus Torvalds int offset = 0; 974daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, maxnonfragsize; 9751da177e4SLinus Torvalds int csummode = CHECKSUM_NONE; 9761470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 977694aba69SEric Dumazet unsigned int wmem_alloc_delta = 0; 978100f6d8eSWillem de Bruijn bool paged, extra_uref = false; 97909c2d251SWillem de Bruijn u32 tskey = 0; 9801da177e4SLinus Torvalds 98196d7303eSSteffen Klassert skb = skb_peek_tail(queue); 98296d7303eSSteffen Klassert 98396d7303eSSteffen Klassert exthdrlen = !skb ? rt->dst.header_len : 0; 984bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize; 98515e36f5bSWillem de Bruijn paged = !!cork->gso_size; 986bec1f6f6SWillem de Bruijn 98709c2d251SWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 98809c2d251SWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 98909c2d251SWillem de Bruijn tskey = sk->sk_tskey++; 9901470ddf7SHerbert Xu 991d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 9941da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 99560ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 9961da177e4SLinus Torvalds 997daba287bSHannes Frederic Sowa if (cork->length + length > maxnonfragsize - fragheaderlen) { 998f5fca608SDavid S. Miller ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 99961e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 10001da177e4SLinus Torvalds return -EMSGSIZE; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* 10041da177e4SLinus Torvalds * transhdrlen > 0 means that this is the first fragment and we wish 10051da177e4SLinus Torvalds * it won't be fragmented in the future. 10061da177e4SLinus Torvalds */ 10071da177e4SLinus Torvalds if (transhdrlen && 10081da177e4SLinus Torvalds length + fragheaderlen <= mtu && 1009c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) && 1010bec1f6f6SWillem de Bruijn (!(flags & MSG_MORE) || cork->gso_size) && 1011cd027a54SJacek Kalwas (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM))) 101284fa7933SPatrick McHardy csummode = CHECKSUM_PARTIAL; 10131da177e4SLinus Torvalds 1014b5947e5dSWillem de Bruijn if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) { 1015b5947e5dSWillem de Bruijn uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb)); 1016b5947e5dSWillem de Bruijn if (!uarg) 1017b5947e5dSWillem de Bruijn return -ENOBUFS; 1018522924b5SWillem de Bruijn extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ 1019b5947e5dSWillem de Bruijn if (rt->dst.dev->features & NETIF_F_SG && 1020b5947e5dSWillem de Bruijn csummode == CHECKSUM_PARTIAL) { 1021b5947e5dSWillem de Bruijn paged = true; 1022b5947e5dSWillem de Bruijn } else { 1023b5947e5dSWillem de Bruijn uarg->zerocopy = 0; 102452900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 1025b5947e5dSWillem de Bruijn } 1026b5947e5dSWillem de Bruijn } 1027b5947e5dSWillem de Bruijn 10281470ddf7SHerbert Xu cork->length += length; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /* So, what's going on in the loop below? 10311da177e4SLinus Torvalds * 10321da177e4SLinus Torvalds * We use calculated fragment length to generate chained skb, 10331da177e4SLinus Torvalds * each of segments is IP fragment ready for sending to network after 10341da177e4SLinus Torvalds * adding appropriate IP header. 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds 103726cde9f7SHerbert Xu if (!skb) 10381da177e4SLinus Torvalds goto alloc_new_skb; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds while (length > 0) { 10411da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 10421da177e4SLinus Torvalds copy = mtu - skb->len; 10431da177e4SLinus Torvalds if (copy < length) 10441da177e4SLinus Torvalds copy = maxfraglen - skb->len; 10451da177e4SLinus Torvalds if (copy <= 0) { 10461da177e4SLinus Torvalds char *data; 10471da177e4SLinus Torvalds unsigned int datalen; 10481da177e4SLinus Torvalds unsigned int fraglen; 10491da177e4SLinus Torvalds unsigned int fraggap; 10501da177e4SLinus Torvalds unsigned int alloclen; 1051aba36930SWillem de Bruijn unsigned int pagedlen; 10521da177e4SLinus Torvalds struct sk_buff *skb_prev; 10531da177e4SLinus Torvalds alloc_new_skb: 10541da177e4SLinus Torvalds skb_prev = skb; 10551da177e4SLinus Torvalds if (skb_prev) 10561da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 10571da177e4SLinus Torvalds else 10581da177e4SLinus Torvalds fraggap = 0; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* 10611da177e4SLinus Torvalds * If remaining data exceeds the mtu, 10621da177e4SLinus Torvalds * we know we need more fragment(s). 10631da177e4SLinus Torvalds */ 10641da177e4SLinus Torvalds datalen = length + fraggap; 10651da177e4SLinus Torvalds if (datalen > mtu - fragheaderlen) 10661da177e4SLinus Torvalds datalen = maxfraglen - fragheaderlen; 10671da177e4SLinus Torvalds fraglen = datalen + fragheaderlen; 1068aba36930SWillem de Bruijn pagedlen = 0; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds if ((flags & MSG_MORE) && 1071d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 10721da177e4SLinus Torvalds alloclen = mtu; 107315e36f5bSWillem de Bruijn else if (!paged) 107459104f06SEric Dumazet alloclen = fraglen; 107515e36f5bSWillem de Bruijn else { 107615e36f5bSWillem de Bruijn alloclen = min_t(int, fraglen, MAX_HEADER); 107715e36f5bSWillem de Bruijn pagedlen = fraglen - alloclen; 107815e36f5bSWillem de Bruijn } 10791da177e4SLinus Torvalds 1080353e5c9aSSteffen Klassert alloclen += exthdrlen; 1081353e5c9aSSteffen Klassert 10821da177e4SLinus Torvalds /* The last fragment gets additional space at tail. 10831da177e4SLinus Torvalds * Note, with MSG_MORE we overallocate on fragments, 10841da177e4SLinus Torvalds * because we have no idea what fragment will be 10851da177e4SLinus Torvalds * the last. 10861da177e4SLinus Torvalds */ 108733f99dc7SSteffen Klassert if (datalen == length + fraggap) 1088d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 108933f99dc7SSteffen Klassert 10901da177e4SLinus Torvalds if (transhdrlen) { 10911da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 10921da177e4SLinus Torvalds alloclen + hh_len + 15, 10931da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 10941da177e4SLinus Torvalds } else { 10951da177e4SLinus Torvalds skb = NULL; 1096694aba69SEric Dumazet if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 10971da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 1098694aba69SEric Dumazet skb = alloc_skb(alloclen + hh_len + 15, 10991da177e4SLinus Torvalds sk->sk_allocation); 110051456b29SIan Morris if (unlikely(!skb)) 11011da177e4SLinus Torvalds err = -ENOBUFS; 11021da177e4SLinus Torvalds } 110351456b29SIan Morris if (!skb) 11041da177e4SLinus Torvalds goto error; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* 11071da177e4SLinus Torvalds * Fill in the control structures 11081da177e4SLinus Torvalds */ 11091da177e4SLinus Torvalds skb->ip_summed = csummode; 11101da177e4SLinus Torvalds skb->csum = 0; 11111da177e4SLinus Torvalds skb_reserve(skb, hh_len); 111211878b40SWillem de Bruijn 11131da177e4SLinus Torvalds /* 11141da177e4SLinus Torvalds * Find where to start putting bytes. 11151da177e4SLinus Torvalds */ 111615e36f5bSWillem de Bruijn data = skb_put(skb, fraglen + exthdrlen - pagedlen); 1117c14d2450SArnaldo Carvalho de Melo skb_set_network_header(skb, exthdrlen); 1118b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1119b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 1120353e5c9aSSteffen Klassert data += fragheaderlen + exthdrlen; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds if (fraggap) { 11231da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 11241da177e4SLinus Torvalds skb_prev, maxfraglen, 11251da177e4SLinus Torvalds data + transhdrlen, fraggap, 0); 11261da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 11271da177e4SLinus Torvalds skb->csum); 11281da177e4SLinus Torvalds data += fraggap; 1129e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 113215e36f5bSWillem de Bruijn copy = datalen - transhdrlen - fraggap - pagedlen; 11331da177e4SLinus Torvalds if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { 11341da177e4SLinus Torvalds err = -EFAULT; 11351da177e4SLinus Torvalds kfree_skb(skb); 11361da177e4SLinus Torvalds goto error; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds offset += copy; 114015e36f5bSWillem de Bruijn length -= copy + transhdrlen; 11411da177e4SLinus Torvalds transhdrlen = 0; 11421da177e4SLinus Torvalds exthdrlen = 0; 11431da177e4SLinus Torvalds csummode = CHECKSUM_NONE; 11441da177e4SLinus Torvalds 114552900d22SWillem de Bruijn /* only the initial fragment is time stamped */ 114652900d22SWillem de Bruijn skb_shinfo(skb)->tx_flags = cork->tx_flags; 114752900d22SWillem de Bruijn cork->tx_flags = 0; 114852900d22SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 114952900d22SWillem de Bruijn tskey = 0; 115052900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 115152900d22SWillem de Bruijn 11520dec879fSJulian Anastasov if ((flags & MSG_CONFIRM) && !skb_prev) 11530dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 11540dec879fSJulian Anastasov 11551da177e4SLinus Torvalds /* 11561da177e4SLinus Torvalds * Put the packet on the pending queue. 11571da177e4SLinus Torvalds */ 1158694aba69SEric Dumazet if (!skb->destructor) { 1159694aba69SEric Dumazet skb->destructor = sock_wfree; 1160694aba69SEric Dumazet skb->sk = sk; 1161694aba69SEric Dumazet wmem_alloc_delta += skb->truesize; 1162694aba69SEric Dumazet } 11631470ddf7SHerbert Xu __skb_queue_tail(queue, skb); 11641da177e4SLinus Torvalds continue; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds if (copy > length) 11681da177e4SLinus Torvalds copy = length; 11691da177e4SLinus Torvalds 1170113f99c3SWillem de Bruijn if (!(rt->dst.dev->features&NETIF_F_SG) && 1171113f99c3SWillem de Bruijn skb_tailroom(skb) >= copy) { 11721da177e4SLinus Torvalds unsigned int off; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds off = skb->len; 11751da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 11761da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 11771da177e4SLinus Torvalds __skb_trim(skb, off); 11781da177e4SLinus Torvalds err = -EFAULT; 11791da177e4SLinus Torvalds goto error; 11801da177e4SLinus Torvalds } 1181b5947e5dSWillem de Bruijn } else if (!uarg || !uarg->zerocopy) { 11821da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds err = -ENOMEM; 11855640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 11861da177e4SLinus Torvalds goto error; 11871da177e4SLinus Torvalds 11885640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 11895640f768SEric Dumazet pfrag->offset)) { 11901da177e4SLinus Torvalds err = -EMSGSIZE; 11915640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 11921da177e4SLinus Torvalds goto error; 11935640f768SEric Dumazet 11945640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 11955640f768SEric Dumazet pfrag->offset, 0); 11965640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 11975640f768SEric Dumazet get_page(pfrag->page); 11981da177e4SLinus Torvalds } 11995640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 12005640f768SEric Dumazet if (getfrag(from, 12015640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 12025640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 12035640f768SEric Dumazet goto error_efault; 12045640f768SEric Dumazet 12055640f768SEric Dumazet pfrag->offset += copy; 12065640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 12071da177e4SLinus Torvalds skb->len += copy; 12081da177e4SLinus Torvalds skb->data_len += copy; 1209f945fa7aSHerbert Xu skb->truesize += copy; 1210694aba69SEric Dumazet wmem_alloc_delta += copy; 1211b5947e5dSWillem de Bruijn } else { 1212b5947e5dSWillem de Bruijn err = skb_zerocopy_iter_dgram(skb, from, copy); 1213b5947e5dSWillem de Bruijn if (err < 0) 1214b5947e5dSWillem de Bruijn goto error; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds offset += copy; 12171da177e4SLinus Torvalds length -= copy; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12209e8445a5SPaolo Abeni if (wmem_alloc_delta) 1221694aba69SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 12221da177e4SLinus Torvalds return 0; 12231da177e4SLinus Torvalds 12245640f768SEric Dumazet error_efault: 12255640f768SEric Dumazet err = -EFAULT; 12261da177e4SLinus Torvalds error: 122797ef7b4cSWillem de Bruijn if (uarg) 122852900d22SWillem de Bruijn sock_zerocopy_put_abort(uarg, extra_uref); 12291470ddf7SHerbert Xu cork->length -= length; 12305e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 1231694aba69SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 12321da177e4SLinus Torvalds return err; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, 12361470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp) 12371470ddf7SHerbert Xu { 1238f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 12391470ddf7SHerbert Xu struct rtable *rt; 12401470ddf7SHerbert Xu 12419783ccd0SGao Feng rt = *rtp; 12429783ccd0SGao Feng if (unlikely(!rt)) 12439783ccd0SGao Feng return -EFAULT; 12449783ccd0SGao Feng 12451470ddf7SHerbert Xu /* 12461470ddf7SHerbert Xu * setup for corking. 12471470ddf7SHerbert Xu */ 12481470ddf7SHerbert Xu opt = ipc->opt; 12491470ddf7SHerbert Xu if (opt) { 125051456b29SIan Morris if (!cork->opt) { 12511470ddf7SHerbert Xu cork->opt = kmalloc(sizeof(struct ip_options) + 40, 12521470ddf7SHerbert Xu sk->sk_allocation); 125351456b29SIan Morris if (unlikely(!cork->opt)) 12541470ddf7SHerbert Xu return -ENOBUFS; 12551470ddf7SHerbert Xu } 1256f6d8bd05SEric Dumazet memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); 12571470ddf7SHerbert Xu cork->flags |= IPCORK_OPT; 12581470ddf7SHerbert Xu cork->addr = ipc->addr; 12591470ddf7SHerbert Xu } 12609783ccd0SGao Feng 12611470ddf7SHerbert Xu /* 12621470ddf7SHerbert Xu * We steal reference to this route, caller should not release it 12631470ddf7SHerbert Xu */ 12641470ddf7SHerbert Xu *rtp = NULL; 1265482fc609SHannes Frederic Sowa cork->fragsize = ip_sk_use_pmtu(sk) ? 1266482fc609SHannes Frederic Sowa dst_mtu(&rt->dst) : rt->dst.dev->mtu; 1267bec1f6f6SWillem de Bruijn 1268fbf47813SWillem de Bruijn cork->gso_size = ipc->gso_size; 12691470ddf7SHerbert Xu cork->dst = &rt->dst; 12701470ddf7SHerbert Xu cork->length = 0; 1271aa661581SFrancesco Fusco cork->ttl = ipc->ttl; 1272aa661581SFrancesco Fusco cork->tos = ipc->tos; 1273c6af0c22SWillem de Bruijn cork->mark = ipc->sockc.mark; 1274aa661581SFrancesco Fusco cork->priority = ipc->priority; 1275bc969a97SJesus Sanchez-Palencia cork->transmit_time = ipc->sockc.transmit_time; 1276678ca42dSWillem de Bruijn cork->tx_flags = 0; 1277678ca42dSWillem de Bruijn sock_tx_timestamp(sk, ipc->sockc.tsflags, &cork->tx_flags); 12781470ddf7SHerbert Xu 12791470ddf7SHerbert Xu return 0; 12801470ddf7SHerbert Xu } 12811470ddf7SHerbert Xu 12821470ddf7SHerbert Xu /* 12831470ddf7SHerbert Xu * ip_append_data() and ip_append_page() can make one large IP datagram 12841470ddf7SHerbert Xu * from many pieces of data. Each pieces will be holded on the socket 12851470ddf7SHerbert Xu * until ip_push_pending_frames() is called. Each piece can be a page 12861470ddf7SHerbert Xu * or non-page data. 12871470ddf7SHerbert Xu * 12881470ddf7SHerbert Xu * Not only UDP, other transport protocols - e.g. raw sockets - can use 12891470ddf7SHerbert Xu * this interface potentially. 12901470ddf7SHerbert Xu * 12911470ddf7SHerbert Xu * LATER: length must be adjusted by pad at tail, when it is required. 12921470ddf7SHerbert Xu */ 1293f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4, 12941470ddf7SHerbert Xu int getfrag(void *from, char *to, int offset, int len, 12951470ddf7SHerbert Xu int odd, struct sk_buff *skb), 12961470ddf7SHerbert Xu void *from, int length, int transhdrlen, 12971470ddf7SHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 12981470ddf7SHerbert Xu unsigned int flags) 12991470ddf7SHerbert Xu { 13001470ddf7SHerbert Xu struct inet_sock *inet = inet_sk(sk); 13011470ddf7SHerbert Xu int err; 13021470ddf7SHerbert Xu 13031470ddf7SHerbert Xu if (flags&MSG_PROBE) 13041470ddf7SHerbert Xu return 0; 13051470ddf7SHerbert Xu 13061470ddf7SHerbert Xu if (skb_queue_empty(&sk->sk_write_queue)) { 1307bdc712b4SDavid S. Miller err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp); 13081470ddf7SHerbert Xu if (err) 13091470ddf7SHerbert Xu return err; 13101470ddf7SHerbert Xu } else { 13111470ddf7SHerbert Xu transhdrlen = 0; 13121470ddf7SHerbert Xu } 13131470ddf7SHerbert Xu 13145640f768SEric Dumazet return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base, 13155640f768SEric Dumazet sk_page_frag(sk), getfrag, 13161470ddf7SHerbert Xu from, length, transhdrlen, flags); 13171470ddf7SHerbert Xu } 13181470ddf7SHerbert Xu 1319f5fca608SDavid S. Miller ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, 13201da177e4SLinus Torvalds int offset, size_t size, int flags) 13211da177e4SLinus Torvalds { 13221da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 13231da177e4SLinus Torvalds struct sk_buff *skb; 13241da177e4SLinus Torvalds struct rtable *rt; 13251da177e4SLinus Torvalds struct ip_options *opt = NULL; 1326bdc712b4SDavid S. Miller struct inet_cork *cork; 13271da177e4SLinus Torvalds int hh_len; 13281da177e4SLinus Torvalds int mtu; 13291da177e4SLinus Torvalds int len; 13301da177e4SLinus Torvalds int err; 1331daba287bSHannes Frederic Sowa unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize; 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds if (inet->hdrincl) 13341da177e4SLinus Torvalds return -EPERM; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (flags&MSG_PROBE) 13371da177e4SLinus Torvalds return 0; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_write_queue)) 13401da177e4SLinus Torvalds return -EINVAL; 13411da177e4SLinus Torvalds 1342bdc712b4SDavid S. Miller cork = &inet->cork.base; 1343bdc712b4SDavid S. Miller rt = (struct rtable *)cork->dst; 1344bdc712b4SDavid S. Miller if (cork->flags & IPCORK_OPT) 1345bdc712b4SDavid S. Miller opt = cork->opt; 13461da177e4SLinus Torvalds 1347d8d1f30bSChangli Gao if (!(rt->dst.dev->features&NETIF_F_SG)) 13481da177e4SLinus Torvalds return -EOPNOTSUPP; 13491da177e4SLinus Torvalds 1350d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 1351bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 13541da177e4SLinus Torvalds maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 135560ff7467SWANG Cong maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; 13561da177e4SLinus Torvalds 1357daba287bSHannes Frederic Sowa if (cork->length + size > maxnonfragsize - fragheaderlen) { 135861e7f09dSHannes Frederic Sowa ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 135961e7f09dSHannes Frederic Sowa mtu - (opt ? opt->optlen : 0)); 13601da177e4SLinus Torvalds return -EMSGSIZE; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 136351456b29SIan Morris skb = skb_peek_tail(&sk->sk_write_queue); 136451456b29SIan Morris if (!skb) 13651da177e4SLinus Torvalds return -EINVAL; 13661da177e4SLinus Torvalds 1367a8c4a252SHannes Frederic Sowa cork->length += size; 1368e89e9cf5SAnanda Raju 13691da177e4SLinus Torvalds while (size > 0) { 13701da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 13711da177e4SLinus Torvalds len = mtu - skb->len; 13721da177e4SLinus Torvalds if (len < size) 13731da177e4SLinus Torvalds len = maxfraglen - skb->len; 1374ab2fb7e3SWillem de Bruijn 13751da177e4SLinus Torvalds if (len <= 0) { 13761da177e4SLinus Torvalds struct sk_buff *skb_prev; 13771da177e4SLinus Torvalds int alloclen; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds skb_prev = skb; 13801da177e4SLinus Torvalds fraggap = skb_prev->len - maxfraglen; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds alloclen = fragheaderlen + hh_len + fraggap + 15; 13831da177e4SLinus Torvalds skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); 13841da177e4SLinus Torvalds if (unlikely(!skb)) { 13851da177e4SLinus Torvalds err = -ENOBUFS; 13861da177e4SLinus Torvalds goto error; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* 13901da177e4SLinus Torvalds * Fill in the control structures 13911da177e4SLinus Torvalds */ 13921da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 13931da177e4SLinus Torvalds skb->csum = 0; 13941da177e4SLinus Torvalds skb_reserve(skb, hh_len); 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds /* 13971da177e4SLinus Torvalds * Find where to start putting bytes. 13981da177e4SLinus Torvalds */ 1399967b05f6SArnaldo Carvalho de Melo skb_put(skb, fragheaderlen + fraggap); 14002ca9e6f2SArnaldo Carvalho de Melo skb_reset_network_header(skb); 1401b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1402b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 14031da177e4SLinus Torvalds if (fraggap) { 1404967b05f6SArnaldo Carvalho de Melo skb->csum = skb_copy_and_csum_bits(skb_prev, 1405967b05f6SArnaldo Carvalho de Melo maxfraglen, 14069c70220bSArnaldo Carvalho de Melo skb_transport_header(skb), 1407967b05f6SArnaldo Carvalho de Melo fraggap, 0); 14081da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 14091da177e4SLinus Torvalds skb->csum); 1410e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14141da177e4SLinus Torvalds * Put the packet on the pending queue. 14151da177e4SLinus Torvalds */ 14161da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_write_queue, skb); 14171da177e4SLinus Torvalds continue; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds if (len > size) 14211da177e4SLinus Torvalds len = size; 1422be12a1feSHannes Frederic Sowa 1423be12a1feSHannes Frederic Sowa if (skb_append_pagefrags(skb, page, offset, len)) { 14241da177e4SLinus Torvalds err = -EMSGSIZE; 14251da177e4SLinus Torvalds goto error; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds if (skb->ip_summed == CHECKSUM_NONE) { 142944bb9363SAl Viro __wsum csum; 14301da177e4SLinus Torvalds csum = csum_page(page, offset, len); 14311da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, skb->len); 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds skb->len += len; 14351da177e4SLinus Torvalds skb->data_len += len; 14361e34a11dSDavid S. Miller skb->truesize += len; 143714afee4bSReshetova, Elena refcount_add(len, &sk->sk_wmem_alloc); 14381da177e4SLinus Torvalds offset += len; 14391da177e4SLinus Torvalds size -= len; 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds return 0; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds error: 1444bdc712b4SDavid S. Miller cork->length -= size; 14455e38e270SPavel Emelyanov IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); 14461da177e4SLinus Torvalds return err; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork) 1450429f08e9SPavel Emelyanov { 14511470ddf7SHerbert Xu cork->flags &= ~IPCORK_OPT; 14521470ddf7SHerbert Xu kfree(cork->opt); 14531470ddf7SHerbert Xu cork->opt = NULL; 14541470ddf7SHerbert Xu dst_release(cork->dst); 14551470ddf7SHerbert Xu cork->dst = NULL; 1456429f08e9SPavel Emelyanov } 1457429f08e9SPavel Emelyanov 14581da177e4SLinus Torvalds /* 14591da177e4SLinus Torvalds * Combined all pending IP fragments on the socket as one IP datagram 14601da177e4SLinus Torvalds * and push them out. 14611da177e4SLinus Torvalds */ 14621c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk, 146377968b78SDavid S. Miller struct flowi4 *fl4, 14641470ddf7SHerbert Xu struct sk_buff_head *queue, 14651470ddf7SHerbert Xu struct inet_cork *cork) 14661da177e4SLinus Torvalds { 14671da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 14681da177e4SLinus Torvalds struct sk_buff **tail_skb; 14691da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 14700388b004SPavel Emelyanov struct net *net = sock_net(sk); 14711da177e4SLinus Torvalds struct ip_options *opt = NULL; 14721470ddf7SHerbert Xu struct rtable *rt = (struct rtable *)cork->dst; 14731da177e4SLinus Torvalds struct iphdr *iph; 147476ab608dSAlexey Dobriyan __be16 df = 0; 14751da177e4SLinus Torvalds __u8 ttl; 14761da177e4SLinus Torvalds 147751456b29SIan Morris skb = __skb_dequeue(queue); 147851456b29SIan Morris if (!skb) 14791da177e4SLinus Torvalds goto out; 14801da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1483d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1484bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 14851470ddf7SHerbert Xu while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1486cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 14871da177e4SLinus Torvalds *tail_skb = tmp_skb; 14881da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 14891da177e4SLinus Torvalds skb->len += tmp_skb->len; 14901da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 14911da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 14921da177e4SLinus Torvalds tmp_skb->destructor = NULL; 14931da177e4SLinus Torvalds tmp_skb->sk = NULL; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow 14971da177e4SLinus Torvalds * to fragment the frame generated here. No matter, what transforms 14981da177e4SLinus Torvalds * how transforms change size of the packet, it will come out. 14991da177e4SLinus Torvalds */ 150060ff7467SWANG Cong skb->ignore_df = ip_sk_ignore_df(sk); 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds /* DF bit is set when we want to see DF on outgoing frames. 150360ff7467SWANG Cong * If ignore_df is set too, we still allow to fragment this frame 15041da177e4SLinus Torvalds * locally. */ 1505482fc609SHannes Frederic Sowa if (inet->pmtudisc == IP_PMTUDISC_DO || 1506482fc609SHannes Frederic Sowa inet->pmtudisc == IP_PMTUDISC_PROBE || 1507d8d1f30bSChangli Gao (skb->len <= dst_mtu(&rt->dst) && 1508d8d1f30bSChangli Gao ip_dont_fragment(sk, &rt->dst))) 15091da177e4SLinus Torvalds df = htons(IP_DF); 15101da177e4SLinus Torvalds 15111470ddf7SHerbert Xu if (cork->flags & IPCORK_OPT) 15121470ddf7SHerbert Xu opt = cork->opt; 15131da177e4SLinus Torvalds 1514aa661581SFrancesco Fusco if (cork->ttl != 0) 1515aa661581SFrancesco Fusco ttl = cork->ttl; 1516aa661581SFrancesco Fusco else if (rt->rt_type == RTN_MULTICAST) 15171da177e4SLinus Torvalds ttl = inet->mc_ttl; 15181da177e4SLinus Torvalds else 1519d8d1f30bSChangli Gao ttl = ip_select_ttl(inet, &rt->dst); 15201da177e4SLinus Torvalds 1521749154aaSAnsis Atteka iph = ip_hdr(skb); 15221da177e4SLinus Torvalds iph->version = 4; 15231da177e4SLinus Torvalds iph->ihl = 5; 1524aa661581SFrancesco Fusco iph->tos = (cork->tos != -1) ? cork->tos : inet->tos; 15251da177e4SLinus Torvalds iph->frag_off = df; 15261da177e4SLinus Torvalds iph->ttl = ttl; 15271da177e4SLinus Torvalds iph->protocol = sk->sk_protocol; 152884f9307cSEric Dumazet ip_copy_addrs(iph, fl4); 1529b6a7719aSHannes Frederic Sowa ip_select_ident(net, skb, sk); 15301da177e4SLinus Torvalds 153122f728f8SDavid S. Miller if (opt) { 153222f728f8SDavid S. Miller iph->ihl += opt->optlen>>2; 153322f728f8SDavid S. Miller ip_options_build(skb, opt, cork->addr, rt, 0); 153422f728f8SDavid S. Miller } 153522f728f8SDavid S. Miller 1536aa661581SFrancesco Fusco skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority; 1537c6af0c22SWillem de Bruijn skb->mark = cork->mark; 1538bc969a97SJesus Sanchez-Palencia skb->tstamp = cork->transmit_time; 1539a21bba94SEric Dumazet /* 1540a21bba94SEric Dumazet * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1541a21bba94SEric Dumazet * on dst refcount 1542a21bba94SEric Dumazet */ 15431470ddf7SHerbert Xu cork->dst = NULL; 1544d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 15451da177e4SLinus Torvalds 154696793b48SDavid L Stevens if (iph->protocol == IPPROTO_ICMP) 15470388b004SPavel Emelyanov icmp_out_count(net, ((struct icmphdr *) 154896793b48SDavid L Stevens skb_transport_header(skb))->type); 154996793b48SDavid L Stevens 15501c32c5adSHerbert Xu ip_cork_release(cork); 15511c32c5adSHerbert Xu out: 15521c32c5adSHerbert Xu return skb; 15531c32c5adSHerbert Xu } 15541c32c5adSHerbert Xu 1555b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb) 15561c32c5adSHerbert Xu { 15571c32c5adSHerbert Xu int err; 15581c32c5adSHerbert Xu 155933224b16SEric W. Biederman err = ip_local_out(net, skb->sk, skb); 15601da177e4SLinus Torvalds if (err) { 15611da177e4SLinus Torvalds if (err > 0) 15626ce9e7b5SEric Dumazet err = net_xmit_errno(err); 15631da177e4SLinus Torvalds if (err) 15641c32c5adSHerbert Xu IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds return err; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 157077968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4) 15711470ddf7SHerbert Xu { 15721c32c5adSHerbert Xu struct sk_buff *skb; 15731c32c5adSHerbert Xu 157477968b78SDavid S. Miller skb = ip_finish_skb(sk, fl4); 15751c32c5adSHerbert Xu if (!skb) 15761c32c5adSHerbert Xu return 0; 15771c32c5adSHerbert Xu 15781c32c5adSHerbert Xu /* Netfilter gets whole the not fragmented skb. */ 1579b5ec8eeaSEric Dumazet return ip_send_skb(sock_net(sk), skb); 15801470ddf7SHerbert Xu } 15811470ddf7SHerbert Xu 15821da177e4SLinus Torvalds /* 15831da177e4SLinus Torvalds * Throw away all pending data on the socket. 15841da177e4SLinus Torvalds */ 15851470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk, 15861470ddf7SHerbert Xu struct sk_buff_head *queue, 15871470ddf7SHerbert Xu struct inet_cork *cork) 15881da177e4SLinus Torvalds { 15891da177e4SLinus Torvalds struct sk_buff *skb; 15901da177e4SLinus Torvalds 15911470ddf7SHerbert Xu while ((skb = __skb_dequeue_tail(queue)) != NULL) 15921da177e4SLinus Torvalds kfree_skb(skb); 15931da177e4SLinus Torvalds 15941470ddf7SHerbert Xu ip_cork_release(cork); 15951470ddf7SHerbert Xu } 15961470ddf7SHerbert Xu 15971470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk) 15981470ddf7SHerbert Xu { 1599bdc712b4SDavid S. Miller __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 16021c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk, 160377968b78SDavid S. Miller struct flowi4 *fl4, 16041c32c5adSHerbert Xu int getfrag(void *from, char *to, int offset, 16051c32c5adSHerbert Xu int len, int odd, struct sk_buff *skb), 16061c32c5adSHerbert Xu void *from, int length, int transhdrlen, 16071c32c5adSHerbert Xu struct ipcm_cookie *ipc, struct rtable **rtp, 16081cd7884dSWillem de Bruijn struct inet_cork *cork, unsigned int flags) 16091c32c5adSHerbert Xu { 16101c32c5adSHerbert Xu struct sk_buff_head queue; 16111c32c5adSHerbert Xu int err; 16121c32c5adSHerbert Xu 16131c32c5adSHerbert Xu if (flags & MSG_PROBE) 16141c32c5adSHerbert Xu return NULL; 16151c32c5adSHerbert Xu 16161c32c5adSHerbert Xu __skb_queue_head_init(&queue); 16171c32c5adSHerbert Xu 16181cd7884dSWillem de Bruijn cork->flags = 0; 16191cd7884dSWillem de Bruijn cork->addr = 0; 16201cd7884dSWillem de Bruijn cork->opt = NULL; 16211cd7884dSWillem de Bruijn err = ip_setup_cork(sk, cork, ipc, rtp); 16221c32c5adSHerbert Xu if (err) 16231c32c5adSHerbert Xu return ERR_PTR(err); 16241c32c5adSHerbert Xu 16251cd7884dSWillem de Bruijn err = __ip_append_data(sk, fl4, &queue, cork, 16265640f768SEric Dumazet ¤t->task_frag, getfrag, 16271c32c5adSHerbert Xu from, length, transhdrlen, flags); 16281c32c5adSHerbert Xu if (err) { 16291cd7884dSWillem de Bruijn __ip_flush_pending_frames(sk, &queue, cork); 16301c32c5adSHerbert Xu return ERR_PTR(err); 16311c32c5adSHerbert Xu } 16321c32c5adSHerbert Xu 16331cd7884dSWillem de Bruijn return __ip_make_skb(sk, fl4, &queue, cork); 16341c32c5adSHerbert Xu } 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds /* 16371da177e4SLinus Torvalds * Fetch data from kernel space and fill in checksum if needed. 16381da177e4SLinus Torvalds */ 16391da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset, 16401da177e4SLinus Torvalds int len, int odd, struct sk_buff *skb) 16411da177e4SLinus Torvalds { 16425084205fSAl Viro __wsum csum; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0); 16451da177e4SLinus Torvalds skb->csum = csum_block_add(skb->csum, csum, odd); 16461da177e4SLinus Torvalds return 0; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds /* 16501da177e4SLinus Torvalds * Generic function to send a packet as reply to another packet. 1651be9f4a44SEric Dumazet * Used to send some TCP resets/acks so far. 16521da177e4SLinus Torvalds */ 1653bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, 165424a2d43dSEric Dumazet const struct ip_options *sopt, 165524a2d43dSEric Dumazet __be32 daddr, __be32 saddr, 165624a2d43dSEric Dumazet const struct ip_reply_arg *arg, 1657d6fb396cSEric Dumazet unsigned int len, u64 transmit_time) 16581da177e4SLinus Torvalds { 1659f6d8bd05SEric Dumazet struct ip_options_data replyopts; 16601da177e4SLinus Torvalds struct ipcm_cookie ipc; 166177968b78SDavid S. Miller struct flowi4 fl4; 1662511c3f92SEric Dumazet struct rtable *rt = skb_rtable(skb); 1663bdbbb852SEric Dumazet struct net *net = sock_net(sk); 1664be9f4a44SEric Dumazet struct sk_buff *nskb; 16654062090eSVasily Averin int err; 1666f7ba868bSDavid Ahern int oif; 16671da177e4SLinus Torvalds 166891ed1e66SPaolo Abeni if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt)) 16691da177e4SLinus Torvalds return; 16701da177e4SLinus Torvalds 167135178206SWillem de Bruijn ipcm_init(&ipc); 16720a5ebb80SDavid S. Miller ipc.addr = daddr; 1673d6fb396cSEric Dumazet ipc.sockc.transmit_time = transmit_time; 16741da177e4SLinus Torvalds 1675f6d8bd05SEric Dumazet if (replyopts.opt.opt.optlen) { 16761da177e4SLinus Torvalds ipc.opt = &replyopts.opt; 16771da177e4SLinus Torvalds 1678f6d8bd05SEric Dumazet if (replyopts.opt.opt.srr) 1679f6d8bd05SEric Dumazet daddr = replyopts.opt.opt.faddr; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 1682f7ba868bSDavid Ahern oif = arg->bound_dev_if; 16839b6c14d5SDavid Ahern if (!oif && netif_index_is_l3_master(net, skb->skb_iif)) 16849b6c14d5SDavid Ahern oif = skb->skb_iif; 1685f7ba868bSDavid Ahern 1686f7ba868bSDavid Ahern flowi4_init_output(&fl4, oif, 168700483690SJon Maxwell IP4_REPLY_MARK(net, skb->mark) ?: sk->sk_mark, 168866b13d99SEric Dumazet RT_TOS(arg->tos), 1689be9f4a44SEric Dumazet RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol, 1690538de0e0SDavid S. Miller ip_reply_arg_flowi_flags(arg), 169170e73416SDavid S. Miller daddr, saddr, 1692e2d118a1SLorenzo Colitti tcp_hdr(skb)->source, tcp_hdr(skb)->dest, 1693e2d118a1SLorenzo Colitti arg->uid); 16949d6ec938SDavid S. Miller security_skb_classify_flow(skb, flowi4_to_flowi(&fl4)); 1695be9f4a44SEric Dumazet rt = ip_route_output_key(net, &fl4); 1696b23dd4feSDavid S. Miller if (IS_ERR(rt)) 16971da177e4SLinus Torvalds return; 16981da177e4SLinus Torvalds 1699bdbbb852SEric Dumazet inet_sk(sk)->tos = arg->tos; 17001da177e4SLinus Torvalds 1701eddc9ec5SArnaldo Carvalho de Melo sk->sk_protocol = ip_hdr(skb)->protocol; 1702f0e48dbfSPatrick McHardy sk->sk_bound_dev_if = arg->bound_dev_if; 1703be9f4a44SEric Dumazet sk->sk_sndbuf = sysctl_wmem_default; 1704bf99b4deSPau Espin Pedrol sk->sk_mark = fl4.flowi4_mark; 17054062090eSVasily Averin err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, 17064062090eSVasily Averin len, 0, &ipc, &rt, MSG_DONTWAIT); 17074062090eSVasily Averin if (unlikely(err)) { 17084062090eSVasily Averin ip_flush_pending_frames(sk); 17094062090eSVasily Averin goto out; 17104062090eSVasily Averin } 17114062090eSVasily Averin 1712be9f4a44SEric Dumazet nskb = skb_peek(&sk->sk_write_queue); 1713be9f4a44SEric Dumazet if (nskb) { 17141da177e4SLinus Torvalds if (arg->csumoffset >= 0) 1715be9f4a44SEric Dumazet *((__sum16 *)skb_transport_header(nskb) + 1716be9f4a44SEric Dumazet arg->csumoffset) = csum_fold(csum_add(nskb->csum, 17179c70220bSArnaldo Carvalho de Melo arg->csum)); 1718be9f4a44SEric Dumazet nskb->ip_summed = CHECKSUM_NONE; 171977968b78SDavid S. Miller ip_push_pending_frames(sk, &fl4); 17201da177e4SLinus Torvalds } 17214062090eSVasily Averin out: 17221da177e4SLinus Torvalds ip_rt_put(rt); 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds void __init ip_init(void) 17261da177e4SLinus Torvalds { 17271da177e4SLinus Torvalds ip_rt_init(); 17281da177e4SLinus Torvalds inet_initpeers(); 17291da177e4SLinus Torvalds 173072c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST) 173172c1d3bdSWANG Cong igmp_mc_init(); 17321da177e4SLinus Torvalds #endif 17331da177e4SLinus Torvalds } 1734