12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IPv6 output functions 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Based on linux/net/ipv4/ip_output.c 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Changes: 121da177e4SLinus Torvalds * A.N.Kuznetsov : airthmetics in fragmentation. 131da177e4SLinus Torvalds * extension headers are implemented. 141da177e4SLinus Torvalds * route changes now work. 151da177e4SLinus Torvalds * ip6_forward does not confuse sniffers. 161da177e4SLinus Torvalds * etc. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * H. von Brand : Added missing #include <linux/string.h> 191da177e4SLinus Torvalds * Imran Patel : frag id should be in NBO 201da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI 211da177e4SLinus Torvalds * : add ip6_append_data and related functions 221da177e4SLinus Torvalds * for datagram xmit 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <linux/errno.h> 26ef76bc23SHerbert Xu #include <linux/kernel.h> 271da177e4SLinus Torvalds #include <linux/string.h> 281da177e4SLinus Torvalds #include <linux/socket.h> 291da177e4SLinus Torvalds #include <linux/net.h> 301da177e4SLinus Torvalds #include <linux/netdevice.h> 311da177e4SLinus Torvalds #include <linux/if_arp.h> 321da177e4SLinus Torvalds #include <linux/in6.h> 331da177e4SLinus Torvalds #include <linux/tcp.h> 341da177e4SLinus Torvalds #include <linux/route.h> 35b59f45d0SHerbert Xu #include <linux/module.h> 365a0e3ad6STejun Heo #include <linux/slab.h> 371da177e4SLinus Torvalds 3833b48679SDaniel Mack #include <linux/bpf-cgroup.h> 391da177e4SLinus Torvalds #include <linux/netfilter.h> 401da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <net/sock.h> 431da177e4SLinus Torvalds #include <net/snmp.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <net/ipv6.h> 461da177e4SLinus Torvalds #include <net/ndisc.h> 471da177e4SLinus Torvalds #include <net/protocol.h> 481da177e4SLinus Torvalds #include <net/ip6_route.h> 491da177e4SLinus Torvalds #include <net/addrconf.h> 501da177e4SLinus Torvalds #include <net/rawv6.h> 511da177e4SLinus Torvalds #include <net/icmp.h> 521da177e4SLinus Torvalds #include <net/xfrm.h> 531da177e4SLinus Torvalds #include <net/checksum.h> 547bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 55ca254490SDavid Ahern #include <net/l3mdev.h> 5614972cbdSRoopa Prabhu #include <net/lwtunnel.h> 57571912c6SMartin Varghese #include <net/ip_tunnels.h> 581da177e4SLinus Torvalds 597d8c6e39SEric W. Biederman static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 601da177e4SLinus Torvalds { 61adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 621da177e4SLinus Torvalds struct net_device *dev = dst->dev; 639b1c1ef1SNicolas Dichtel const struct in6_addr *nexthop; 64f6b72b62SDavid S. Miller struct neighbour *neigh; 656fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 int ret; 661da177e4SLinus Torvalds 670660e03fSArnaldo Carvalho de Melo if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) { 68adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 691da177e4SLinus Torvalds 707026b1ddSDavid Miller if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) && 718571ab47SYuval Mintz ((mroute6_is_socket(net, skb) && 72bd91b8bfSBenjamin Thery !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) || 730660e03fSArnaldo Carvalho de Melo ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr, 747bc570c8SYOSHIFUJI Hideaki &ipv6_hdr(skb)->saddr))) { 751da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* Do not check for IFF_ALLMULTI; multicast routing 781da177e4SLinus Torvalds is not supported in any case. 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds if (newskb) 81b2e0b385SJan Engelhardt NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, 8229a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 8395603e22SMichel Machado dev_loopback_xmit); 841da177e4SLinus Torvalds 850660e03fSArnaldo Carvalho de Melo if (ipv6_hdr(skb)->hop_limit == 0) { 8678126c41SEric W. Biederman IP6_INC_STATS(net, idev, 873bd653c8SDenis V. Lunev IPSTATS_MIB_OUTDISCARDS); 881da177e4SLinus Torvalds kfree_skb(skb); 891da177e4SLinus Torvalds return 0; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds 9378126c41SEric W. Biederman IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len); 94dd408515SHannes Frederic Sowa 95dd408515SHannes Frederic Sowa if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <= 96dd408515SHannes Frederic Sowa IPV6_ADDR_SCOPE_NODELOCAL && 97dd408515SHannes Frederic Sowa !(dev->flags & IFF_LOOPBACK)) { 98dd408515SHannes Frederic Sowa kfree_skb(skb); 99dd408515SHannes Frederic Sowa return 0; 100dd408515SHannes Frederic Sowa } 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 10314972cbdSRoopa Prabhu if (lwtunnel_xmit_redirect(dst->lwtstate)) { 10414972cbdSRoopa Prabhu int res = lwtunnel_xmit(skb); 10514972cbdSRoopa Prabhu 10614972cbdSRoopa Prabhu if (res < 0 || res == LWTUNNEL_XMIT_DONE) 10714972cbdSRoopa Prabhu return res; 10814972cbdSRoopa Prabhu } 10914972cbdSRoopa Prabhu 1106fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 1112647a9b0SMartin KaFai Lau nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr); 1126fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop); 1136fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(!neigh)) 1146fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false); 1156fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR(neigh)) { 1164ff06203SJulian Anastasov sock_confirm_neigh(skb, neigh); 1170353f282SDavid Ahern ret = neigh_output(neigh, skb, false); 1186fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 1196fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 return ret; 1206fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 } 1216fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 12205e3aa09SDavid S. Miller 12378126c41SEric W. Biederman IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); 1249e508490SJan Engelhardt kfree_skb(skb); 1259e508490SJan Engelhardt return -EINVAL; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 128956fe219Sbrakmo static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 1299e508490SJan Engelhardt { 13009ee9dbaSTobias Brunner #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 13109ee9dbaSTobias Brunner /* Policy lookup after SNAT yielded a new policy */ 13209ee9dbaSTobias Brunner if (skb_dst(skb)->xfrm) { 13309ee9dbaSTobias Brunner IPCB(skb)->flags |= IPSKB_REROUTED; 13409ee9dbaSTobias Brunner return dst_output(net, sk, skb); 13509ee9dbaSTobias Brunner } 13609ee9dbaSTobias Brunner #endif 13709ee9dbaSTobias Brunner 1389e508490SJan Engelhardt if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) || 1399037c357SJiri Pirko dst_allfrag(skb_dst(skb)) || 1409037c357SJiri Pirko (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size)) 1417d8c6e39SEric W. Biederman return ip6_fragment(net, sk, skb, ip6_finish_output2); 1429e508490SJan Engelhardt else 1437d8c6e39SEric W. Biederman return ip6_finish_output2(net, sk, skb); 1449e508490SJan Engelhardt } 1459e508490SJan Engelhardt 146956fe219Sbrakmo static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 147956fe219Sbrakmo { 148956fe219Sbrakmo int ret; 149956fe219Sbrakmo 150956fe219Sbrakmo ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 151956fe219Sbrakmo switch (ret) { 152956fe219Sbrakmo case NET_XMIT_SUCCESS: 153956fe219Sbrakmo return __ip6_finish_output(net, sk, skb); 154956fe219Sbrakmo case NET_XMIT_CN: 155956fe219Sbrakmo return __ip6_finish_output(net, sk, skb) ? : ret; 156956fe219Sbrakmo default: 157956fe219Sbrakmo kfree_skb(skb); 158956fe219Sbrakmo return ret; 159956fe219Sbrakmo } 160956fe219Sbrakmo } 161956fe219Sbrakmo 162ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb) 1631da177e4SLinus Torvalds { 16428f8bfd1SPhil Sutter struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev; 165adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 166be10de0aSEric W. Biederman 16797a7a37aSChenbo Feng skb->protocol = htons(ETH_P_IPV6); 16897a7a37aSChenbo Feng skb->dev = dev; 16997a7a37aSChenbo Feng 170778d80beSYOSHIFUJI Hideaki if (unlikely(idev->cnf.disable_ipv6)) { 17119a0644cSEric W. Biederman IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS); 172778d80beSYOSHIFUJI Hideaki kfree_skb(skb); 173778d80beSYOSHIFUJI Hideaki return 0; 174778d80beSYOSHIFUJI Hideaki } 175778d80beSYOSHIFUJI Hideaki 17629a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, 17728f8bfd1SPhil Sutter net, sk, skb, indev, dev, 1789c6eb28aSJan Engelhardt ip6_finish_output, 1799c6eb28aSJan Engelhardt !(IP6CB(skb)->flags & IP6SKB_REROUTED)); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 182e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np) 183513674b5SShaohua Li { 184513674b5SShaohua Li if (!np->autoflowlabel_set) 185513674b5SShaohua Li return ip6_default_np_autolabel(net); 186513674b5SShaohua Li else 187513674b5SShaohua Li return np->autoflowlabel; 188513674b5SShaohua Li } 189513674b5SShaohua Li 1901da177e4SLinus Torvalds /* 191b5d43998SShan Wei * xmit an sk_buff (used by TCP, SCTP and DCCP) 1921c1e9d2bSEric Dumazet * Note : socket lock is not held for SYNACK packets, but might be modified 1931c1e9d2bSEric Dumazet * by calls to skb_set_owner_w() and ipv6_local_error(), 1941c1e9d2bSEric Dumazet * which are using proper atomic operations or spinlocks. 1951da177e4SLinus Torvalds */ 1961c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, 1974f6570d7SEric Dumazet __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority) 1981da177e4SLinus Torvalds { 1993bd653c8SDenis V. Lunev struct net *net = sock_net(sk); 2001c1e9d2bSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 2014c9483b2SDavid S. Miller struct in6_addr *first_hop = &fl6->daddr; 202adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 20366033f47SStefano Brivio unsigned int head_room; 2041da177e4SLinus Torvalds struct ipv6hdr *hdr; 2054c9483b2SDavid S. Miller u8 proto = fl6->flowi6_proto; 2061da177e4SLinus Torvalds int seg_len = skb->len; 207e651f03aSGerrit Renker int hlimit = -1; 2081da177e4SLinus Torvalds u32 mtu; 2091da177e4SLinus Torvalds 21066033f47SStefano Brivio head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev); 21166033f47SStefano Brivio if (opt) 21266033f47SStefano Brivio head_room += opt->opt_nflen + opt->opt_flen; 2131da177e4SLinus Torvalds 21466033f47SStefano Brivio if (unlikely(skb_headroom(skb) < head_room)) { 2151da177e4SLinus Torvalds struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room); 21663159f29SIan Morris if (!skb2) { 217adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 218a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTDISCARDS); 2191da177e4SLinus Torvalds kfree_skb(skb); 2201da177e4SLinus Torvalds return -ENOBUFS; 2211da177e4SLinus Torvalds } 222bbd6528dSEric Dumazet if (skb->sk) 223bbd6528dSEric Dumazet skb_set_owner_w(skb2, skb->sk); 224808db80aSEric Dumazet consume_skb(skb); 225a11d206dSYOSHIFUJI Hideaki skb = skb2; 2261da177e4SLinus Torvalds } 22766033f47SStefano Brivio 22866033f47SStefano Brivio if (opt) { 22966033f47SStefano Brivio seg_len += opt->opt_nflen + opt->opt_flen; 23066033f47SStefano Brivio 2311da177e4SLinus Torvalds if (opt->opt_flen) 2321da177e4SLinus Torvalds ipv6_push_frag_opts(skb, opt, &proto); 23366033f47SStefano Brivio 2341da177e4SLinus Torvalds if (opt->opt_nflen) 235613fa3caSDavid Lebrun ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop, 236613fa3caSDavid Lebrun &fl6->saddr); 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 239e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct ipv6hdr)); 240e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2410660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * Fill in the IPv6 header 2451da177e4SLinus Torvalds */ 246b903d324SEric Dumazet if (np) 2471da177e4SLinus Torvalds hlimit = np->hop_limit; 2481da177e4SLinus Torvalds if (hlimit < 0) 2496b75d090SYOSHIFUJI Hideaki hlimit = ip6_dst_hoplimit(dst); 2501da177e4SLinus Torvalds 251cb1ce2efSTom Herbert ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel, 252513674b5SShaohua Li ip6_autoflowlabel(net, np), fl6)); 25341a1f8eaSYOSHIFUJI Hideaki 2541da177e4SLinus Torvalds hdr->payload_len = htons(seg_len); 2551da177e4SLinus Torvalds hdr->nexthdr = proto; 2561da177e4SLinus Torvalds hdr->hop_limit = hlimit; 2571da177e4SLinus Torvalds 2584e3fd7a0SAlexey Dobriyan hdr->saddr = fl6->saddr; 2594e3fd7a0SAlexey Dobriyan hdr->daddr = *first_hop; 2601da177e4SLinus Torvalds 2619c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 2624f6570d7SEric Dumazet skb->priority = priority; 26392e55f41SPablo Neira skb->mark = mark; 264a2c2064fSPatrick McHardy 2651da177e4SLinus Torvalds mtu = dst_mtu(dst); 26660ff7467SWANG Cong if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) { 267adf30907SEric Dumazet IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)), 268edf391ffSNeil Horman IPSTATS_MIB_OUT, skb->len); 269a8e3e1a9SDavid Ahern 270a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 271a8e3e1a9SDavid Ahern * skb to its handler for processing 272a8e3e1a9SDavid Ahern */ 273a8e3e1a9SDavid Ahern skb = l3mdev_ip6_out((struct sock *)sk, skb); 274a8e3e1a9SDavid Ahern if (unlikely(!skb)) 275a8e3e1a9SDavid Ahern return 0; 276a8e3e1a9SDavid Ahern 2771c1e9d2bSEric Dumazet /* hooks should never assume socket lock is held. 2781c1e9d2bSEric Dumazet * we promote our socket to non const 2791c1e9d2bSEric Dumazet */ 28029a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, 2811c1e9d2bSEric Dumazet net, (struct sock *)sk, skb, NULL, dst->dev, 28213206b6bSEric W. Biederman dst_output); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds skb->dev = dst->dev; 2861c1e9d2bSEric Dumazet /* ipv6_local_error() does not require socket lock, 2871c1e9d2bSEric Dumazet * we promote our socket to non const 2881c1e9d2bSEric Dumazet */ 2891c1e9d2bSEric Dumazet ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu); 2901c1e9d2bSEric Dumazet 291adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS); 2921da177e4SLinus Torvalds kfree_skb(skb); 2931da177e4SLinus Torvalds return -EMSGSIZE; 2941da177e4SLinus Torvalds } 2957159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit); 2967159039aSYOSHIFUJI Hideaki 2971da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct ip6_ra_chain *ra; 3001da177e4SLinus Torvalds struct sock *last = NULL; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds read_lock(&ip6_ra_lock); 3031da177e4SLinus Torvalds for (ra = ip6_ra_chain; ra; ra = ra->next) { 3041da177e4SLinus Torvalds struct sock *sk = ra->sk; 3050bd1b59bSAndrew McDonald if (sk && ra->sel == sel && 3060bd1b59bSAndrew McDonald (!sk->sk_bound_dev_if || 3070bd1b59bSAndrew McDonald sk->sk_bound_dev_if == skb->dev->ifindex)) { 3089036b2feSFrancesco Ruggeri struct ipv6_pinfo *np = inet6_sk(sk); 3099036b2feSFrancesco Ruggeri 3109036b2feSFrancesco Ruggeri if (np && np->rtalert_isolate && 3119036b2feSFrancesco Ruggeri !net_eq(sock_net(sk), dev_net(skb->dev))) { 3129036b2feSFrancesco Ruggeri continue; 3139036b2feSFrancesco Ruggeri } 3141da177e4SLinus Torvalds if (last) { 3151da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 3161da177e4SLinus Torvalds if (skb2) 3171da177e4SLinus Torvalds rawv6_rcv(last, skb2); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds last = sk; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds if (last) { 3241da177e4SLinus Torvalds rawv6_rcv(last, skb); 3251da177e4SLinus Torvalds read_unlock(&ip6_ra_lock); 3261da177e4SLinus Torvalds return 1; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds read_unlock(&ip6_ra_lock); 3291da177e4SLinus Torvalds return 0; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 332e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb) 333e21e0b5fSVille Nuorvala { 3340660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 335e21e0b5fSVille Nuorvala u8 nexthdr = hdr->nexthdr; 33675f2811cSJesse Gross __be16 frag_off; 337e21e0b5fSVille Nuorvala int offset; 338e21e0b5fSVille Nuorvala 339e21e0b5fSVille Nuorvala if (ipv6_ext_hdr(nexthdr)) { 34075f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off); 341e21e0b5fSVille Nuorvala if (offset < 0) 342e21e0b5fSVille Nuorvala return 0; 343e21e0b5fSVille Nuorvala } else 344e21e0b5fSVille Nuorvala offset = sizeof(struct ipv6hdr); 345e21e0b5fSVille Nuorvala 346e21e0b5fSVille Nuorvala if (nexthdr == IPPROTO_ICMPV6) { 347e21e0b5fSVille Nuorvala struct icmp6hdr *icmp6; 348e21e0b5fSVille Nuorvala 349d56f90a7SArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_network_header(skb) + 350d56f90a7SArnaldo Carvalho de Melo offset + 1 - skb->data))) 351e21e0b5fSVille Nuorvala return 0; 352e21e0b5fSVille Nuorvala 353d56f90a7SArnaldo Carvalho de Melo icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset); 354e21e0b5fSVille Nuorvala 355e21e0b5fSVille Nuorvala switch (icmp6->icmp6_type) { 356e21e0b5fSVille Nuorvala case NDISC_ROUTER_SOLICITATION: 357e21e0b5fSVille Nuorvala case NDISC_ROUTER_ADVERTISEMENT: 358e21e0b5fSVille Nuorvala case NDISC_NEIGHBOUR_SOLICITATION: 359e21e0b5fSVille Nuorvala case NDISC_NEIGHBOUR_ADVERTISEMENT: 360e21e0b5fSVille Nuorvala case NDISC_REDIRECT: 361e21e0b5fSVille Nuorvala /* For reaction involving unicast neighbor discovery 362e21e0b5fSVille Nuorvala * message destined to the proxied address, pass it to 363e21e0b5fSVille Nuorvala * input function. 364e21e0b5fSVille Nuorvala */ 365e21e0b5fSVille Nuorvala return 1; 366e21e0b5fSVille Nuorvala default: 367e21e0b5fSVille Nuorvala break; 368e21e0b5fSVille Nuorvala } 369e21e0b5fSVille Nuorvala } 370e21e0b5fSVille Nuorvala 37174553b09SVille Nuorvala /* 37274553b09SVille Nuorvala * The proxying router can't forward traffic sent to a link-local 37374553b09SVille Nuorvala * address, so signal the sender and discard the packet. This 37474553b09SVille Nuorvala * behavior is clarified by the MIPv6 specification. 37574553b09SVille Nuorvala */ 37674553b09SVille Nuorvala if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) { 37774553b09SVille Nuorvala dst_link_failure(skb); 37874553b09SVille Nuorvala return -1; 37974553b09SVille Nuorvala } 38074553b09SVille Nuorvala 381e21e0b5fSVille Nuorvala return 0; 382e21e0b5fSVille Nuorvala } 383e21e0b5fSVille Nuorvala 3840c4b51f0SEric W. Biederman static inline int ip6_forward_finish(struct net *net, struct sock *sk, 3850c4b51f0SEric W. Biederman struct sk_buff *skb) 3861da177e4SLinus Torvalds { 38771a1c915SJeff Barnhill struct dst_entry *dst = skb_dst(skb); 38871a1c915SJeff Barnhill 38971a1c915SJeff Barnhill __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS); 39071a1c915SJeff Barnhill __IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len); 39171a1c915SJeff Barnhill 392f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV 393f839a6c9SIdo Schimmel if (skb->offload_l3_fwd_mark) { 394f839a6c9SIdo Schimmel consume_skb(skb); 395f839a6c9SIdo Schimmel return 0; 396f839a6c9SIdo Schimmel } 397f839a6c9SIdo Schimmel #endif 398f839a6c9SIdo Schimmel 3998203e2d8SEric Dumazet skb->tstamp = 0; 40013206b6bSEric W. Biederman return dst_output(net, sk, skb); 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 403fe6cc55fSFlorian Westphal static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) 404fe6cc55fSFlorian Westphal { 405418a3156SFlorian Westphal if (skb->len <= mtu) 406fe6cc55fSFlorian Westphal return false; 407fe6cc55fSFlorian Westphal 40860ff7467SWANG Cong /* ipv6 conntrack defrag sets max_frag_size + ignore_df */ 409fe6cc55fSFlorian Westphal if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu) 410fe6cc55fSFlorian Westphal return true; 411fe6cc55fSFlorian Westphal 41260ff7467SWANG Cong if (skb->ignore_df) 413418a3156SFlorian Westphal return false; 414418a3156SFlorian Westphal 415779b7931SDaniel Axtens if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) 416fe6cc55fSFlorian Westphal return false; 417fe6cc55fSFlorian Westphal 418fe6cc55fSFlorian Westphal return true; 419fe6cc55fSFlorian Westphal } 420fe6cc55fSFlorian Westphal 4211da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb) 4221da177e4SLinus Torvalds { 423bdb7cc64SStephen Suryaputra struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); 424adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 4250660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 4261da177e4SLinus Torvalds struct inet6_skb_parm *opt = IP6CB(skb); 427c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dst->dev); 42814f3ad6fSUlrich Weber u32 mtu; 4291da177e4SLinus Torvalds 43053b7997fSYOSHIFUJI Hideaki if (net->ipv6.devconf_all->forwarding == 0) 4311da177e4SLinus Torvalds goto error; 4321da177e4SLinus Torvalds 433090f1166SLi RongQing if (skb->pkt_type != PACKET_HOST) 434090f1166SLi RongQing goto drop; 435090f1166SLi RongQing 4369ef2e965SHannes Frederic Sowa if (unlikely(skb->sk)) 4379ef2e965SHannes Frederic Sowa goto drop; 4389ef2e965SHannes Frederic Sowa 4394497b076SBen Hutchings if (skb_warn_if_lro(skb)) 4404497b076SBen Hutchings goto drop; 4414497b076SBen Hutchings 4421da177e4SLinus Torvalds if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) { 443bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4441da177e4SLinus Torvalds goto drop; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 44735fc92a9SHerbert Xu skb_forward_csum(skb); 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* 4501da177e4SLinus Torvalds * We DO NOT make any processing on 4511da177e4SLinus Torvalds * RA packets, pushing them to user level AS IS 4521da177e4SLinus Torvalds * without ane WARRANTY that application will be able 4531da177e4SLinus Torvalds * to interpret them. The reason is that we 4541da177e4SLinus Torvalds * cannot make anything clever here. 4551da177e4SLinus Torvalds * 4561da177e4SLinus Torvalds * We are not end-node, so that if packet contains 4571da177e4SLinus Torvalds * AH/ESP, we cannot make anything. 4581da177e4SLinus Torvalds * Defragmentation also would be mistake, RA packets 4591da177e4SLinus Torvalds * cannot be fragmented, because there is no warranty 4601da177e4SLinus Torvalds * that different fragments will go along one path. --ANK 4611da177e4SLinus Torvalds */ 462ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 463ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 if (ip6_call_ra_chain(skb, ntohs(opt->ra))) 4641da177e4SLinus Torvalds return 0; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /* 4681da177e4SLinus Torvalds * check and decrement ttl 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds if (hdr->hop_limit <= 1) { 4713ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0); 472bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds kfree_skb(skb); 4751da177e4SLinus Torvalds return -ETIMEDOUT; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 478fbea49e1SYOSHIFUJI Hideaki /* XXX: idev->cnf.proxy_ndp? */ 47953b7997fSYOSHIFUJI Hideaki if (net->ipv6.devconf_all->proxy_ndp && 4808a3edd80SDaniel Lezcano pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) { 48174553b09SVille Nuorvala int proxied = ip6_forward_proxy_check(skb); 48274553b09SVille Nuorvala if (proxied > 0) 483e21e0b5fSVille Nuorvala return ip6_input(skb); 48474553b09SVille Nuorvala else if (proxied < 0) { 485bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 48674553b09SVille Nuorvala goto drop; 48774553b09SVille Nuorvala } 488e21e0b5fSVille Nuorvala } 489e21e0b5fSVille Nuorvala 4901da177e4SLinus Torvalds if (!xfrm6_route_forward(skb)) { 491bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4921da177e4SLinus Torvalds goto drop; 4931da177e4SLinus Torvalds } 494adf30907SEric Dumazet dst = skb_dst(skb); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* IPv6 specs say nothing about it, but it is clear that we cannot 4971da177e4SLinus Torvalds send redirects to source routed frames. 4981e5dc146SMasahide NAKAMURA We don't send redirects to frames decapsulated from IPsec. 4991da177e4SLinus Torvalds */ 5002f17becfSStephen Suryaputra if (IP6CB(skb)->iif == dst->dev->ifindex && 5012f17becfSStephen Suryaputra opt->srcrt == 0 && !skb_sec_path(skb)) { 5021da177e4SLinus Torvalds struct in6_addr *target = NULL; 503fbfe95a4SDavid S. Miller struct inet_peer *peer; 5041da177e4SLinus Torvalds struct rt6_info *rt; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /* 5071da177e4SLinus Torvalds * incoming and outgoing devices are the same 5081da177e4SLinus Torvalds * send a redirect. 5091da177e4SLinus Torvalds */ 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 512c45a3dfbSDavid S. Miller if (rt->rt6i_flags & RTF_GATEWAY) 513c45a3dfbSDavid S. Miller target = &rt->rt6i_gateway; 5141da177e4SLinus Torvalds else 5151da177e4SLinus Torvalds target = &hdr->daddr; 5161da177e4SLinus Torvalds 517fd0273d7SMartin KaFai Lau peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1); 51892d86829SDavid S. Miller 5191da177e4SLinus Torvalds /* Limit redirects both by destination (here) 5201da177e4SLinus Torvalds and by source (inside ndisc_send_redirect) 5211da177e4SLinus Torvalds */ 522fbfe95a4SDavid S. Miller if (inet_peer_xrlim_allow(peer, 1*HZ)) 5234991969aSDavid S. Miller ndisc_send_redirect(skb, target); 5241d861aa4SDavid S. Miller if (peer) 5251d861aa4SDavid S. Miller inet_putpeer(peer); 5265bb1ab09SDavid L Stevens } else { 5275bb1ab09SDavid L Stevens int addrtype = ipv6_addr_type(&hdr->saddr); 5285bb1ab09SDavid L Stevens 5291da177e4SLinus Torvalds /* This check is security critical. */ 530f81b2e7dSYOSHIFUJI Hideaki if (addrtype == IPV6_ADDR_ANY || 531f81b2e7dSYOSHIFUJI Hideaki addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK)) 5321da177e4SLinus Torvalds goto error; 5335bb1ab09SDavid L Stevens if (addrtype & IPV6_ADDR_LINKLOCAL) { 5345bb1ab09SDavid L Stevens icmpv6_send(skb, ICMPV6_DEST_UNREACH, 5353ffe533cSAlexey Dobriyan ICMPV6_NOT_NEIGHBOUR, 0); 5365bb1ab09SDavid L Stevens goto error; 5375bb1ab09SDavid L Stevens } 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5400954cf9cSHannes Frederic Sowa mtu = ip6_dst_mtu_forward(dst); 54114f3ad6fSUlrich Weber if (mtu < IPV6_MIN_MTU) 54214f3ad6fSUlrich Weber mtu = IPV6_MIN_MTU; 54314f3ad6fSUlrich Weber 544fe6cc55fSFlorian Westphal if (ip6_pkt_too_big(skb, mtu)) { 5451da177e4SLinus Torvalds /* Again, force OUTPUT device used as source address */ 5461da177e4SLinus Torvalds skb->dev = dst->dev; 54714f3ad6fSUlrich Weber icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 548bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS); 5491d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(dst), 55015c77d8bSEric Dumazet IPSTATS_MIB_FRAGFAILS); 5511da177e4SLinus Torvalds kfree_skb(skb); 5521da177e4SLinus Torvalds return -EMSGSIZE; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds if (skb_cow(skb, dst->dev->hard_header_len)) { 5561d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(dst), 55715c77d8bSEric Dumazet IPSTATS_MIB_OUTDISCARDS); 5581da177e4SLinus Torvalds goto drop; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5610660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds /* Mangling hops number delayed to point after skb COW */ 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds hdr->hop_limit--; 5661da177e4SLinus Torvalds 56729a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, 56829a26a56SEric W. Biederman net, NULL, skb, skb->dev, dst->dev, 5696e23ae2aSPatrick McHardy ip6_forward_finish); 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds error: 572bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 5731da177e4SLinus Torvalds drop: 5741da177e4SLinus Torvalds kfree_skb(skb); 5751da177e4SLinus Torvalds return -EINVAL; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 5811da177e4SLinus Torvalds to->priority = from->priority; 5821da177e4SLinus Torvalds to->protocol = from->protocol; 583adf30907SEric Dumazet skb_dst_drop(to); 584adf30907SEric Dumazet skb_dst_set(to, dst_clone(skb_dst(from))); 5851da177e4SLinus Torvalds to->dev = from->dev; 58682e91ffeSThomas Graf to->mark = from->mark; 5871da177e4SLinus Torvalds 5883dd1c9a1SPaolo Abeni skb_copy_hash(to, from); 5893dd1c9a1SPaolo Abeni 5901da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5911da177e4SLinus Torvalds to->tc_index = from->tc_index; 5921da177e4SLinus Torvalds #endif 593e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 594df5042f4SFlorian Westphal skb_ext_copy(to, from); 595984bc16cSJames Morris skb_copy_secmark(to, from); 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5980feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr, 5990feca619SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, 6000feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter) 6010feca619SPablo Neira Ayuso { 6020feca619SPablo Neira Ayuso unsigned int first_len; 6030feca619SPablo Neira Ayuso struct frag_hdr *fh; 6040feca619SPablo Neira Ayuso 6050feca619SPablo Neira Ayuso /* BUILD HEADER */ 6060feca619SPablo Neira Ayuso *prevhdr = NEXTHDR_FRAGMENT; 6070feca619SPablo Neira Ayuso iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC); 6080feca619SPablo Neira Ayuso if (!iter->tmp_hdr) 6090feca619SPablo Neira Ayuso return -ENOMEM; 6100feca619SPablo Neira Ayuso 611b7034146SEric Dumazet iter->frag = skb_shinfo(skb)->frag_list; 6120feca619SPablo Neira Ayuso skb_frag_list_init(skb); 6130feca619SPablo Neira Ayuso 6140feca619SPablo Neira Ayuso iter->offset = 0; 6150feca619SPablo Neira Ayuso iter->hlen = hlen; 6160feca619SPablo Neira Ayuso iter->frag_id = frag_id; 6170feca619SPablo Neira Ayuso iter->nexthdr = nexthdr; 6180feca619SPablo Neira Ayuso 6190feca619SPablo Neira Ayuso __skb_pull(skb, hlen); 6200feca619SPablo Neira Ayuso fh = __skb_push(skb, sizeof(struct frag_hdr)); 6210feca619SPablo Neira Ayuso __skb_push(skb, hlen); 6220feca619SPablo Neira Ayuso skb_reset_network_header(skb); 6230feca619SPablo Neira Ayuso memcpy(skb_network_header(skb), iter->tmp_hdr, hlen); 6240feca619SPablo Neira Ayuso 6250feca619SPablo Neira Ayuso fh->nexthdr = nexthdr; 6260feca619SPablo Neira Ayuso fh->reserved = 0; 6270feca619SPablo Neira Ayuso fh->frag_off = htons(IP6_MF); 6280feca619SPablo Neira Ayuso fh->identification = frag_id; 6290feca619SPablo Neira Ayuso 6300feca619SPablo Neira Ayuso first_len = skb_pagelen(skb); 6310feca619SPablo Neira Ayuso skb->data_len = first_len - skb_headlen(skb); 6320feca619SPablo Neira Ayuso skb->len = first_len; 6330feca619SPablo Neira Ayuso ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr)); 6340feca619SPablo Neira Ayuso 6350feca619SPablo Neira Ayuso return 0; 6360feca619SPablo Neira Ayuso } 6370feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_init); 6380feca619SPablo Neira Ayuso 6390feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb, 6400feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter) 6410feca619SPablo Neira Ayuso { 6420feca619SPablo Neira Ayuso struct sk_buff *frag = iter->frag; 6430feca619SPablo Neira Ayuso unsigned int hlen = iter->hlen; 6440feca619SPablo Neira Ayuso struct frag_hdr *fh; 6450feca619SPablo Neira Ayuso 6460feca619SPablo Neira Ayuso frag->ip_summed = CHECKSUM_NONE; 6470feca619SPablo Neira Ayuso skb_reset_transport_header(frag); 6480feca619SPablo Neira Ayuso fh = __skb_push(frag, sizeof(struct frag_hdr)); 6490feca619SPablo Neira Ayuso __skb_push(frag, hlen); 6500feca619SPablo Neira Ayuso skb_reset_network_header(frag); 6510feca619SPablo Neira Ayuso memcpy(skb_network_header(frag), iter->tmp_hdr, hlen); 6520feca619SPablo Neira Ayuso iter->offset += skb->len - hlen - sizeof(struct frag_hdr); 6530feca619SPablo Neira Ayuso fh->nexthdr = iter->nexthdr; 6540feca619SPablo Neira Ayuso fh->reserved = 0; 6550feca619SPablo Neira Ayuso fh->frag_off = htons(iter->offset); 6560feca619SPablo Neira Ayuso if (frag->next) 6570feca619SPablo Neira Ayuso fh->frag_off |= htons(IP6_MF); 6580feca619SPablo Neira Ayuso fh->identification = iter->frag_id; 6590feca619SPablo Neira Ayuso ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr)); 6600feca619SPablo Neira Ayuso ip6_copy_metadata(frag, skb); 6610feca619SPablo Neira Ayuso } 6620feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_prepare); 6630feca619SPablo Neira Ayuso 6648a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu, 6658a6a1f17SPablo Neira Ayuso unsigned short needed_tailroom, int hdr_room, u8 *prevhdr, 6668a6a1f17SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state) 6678a6a1f17SPablo Neira Ayuso { 6688a6a1f17SPablo Neira Ayuso state->prevhdr = prevhdr; 6698a6a1f17SPablo Neira Ayuso state->nexthdr = nexthdr; 6708a6a1f17SPablo Neira Ayuso state->frag_id = frag_id; 6718a6a1f17SPablo Neira Ayuso 6728a6a1f17SPablo Neira Ayuso state->hlen = hlen; 6738a6a1f17SPablo Neira Ayuso state->mtu = mtu; 6748a6a1f17SPablo Neira Ayuso 6758a6a1f17SPablo Neira Ayuso state->left = skb->len - hlen; /* Space per frame */ 6768a6a1f17SPablo Neira Ayuso state->ptr = hlen; /* Where to start from */ 6778a6a1f17SPablo Neira Ayuso 6788a6a1f17SPablo Neira Ayuso state->hroom = hdr_room; 6798a6a1f17SPablo Neira Ayuso state->troom = needed_tailroom; 6808a6a1f17SPablo Neira Ayuso 6818a6a1f17SPablo Neira Ayuso state->offset = 0; 6828a6a1f17SPablo Neira Ayuso } 6838a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_init); 6848a6a1f17SPablo Neira Ayuso 6858a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state) 6868a6a1f17SPablo Neira Ayuso { 6878a6a1f17SPablo Neira Ayuso u8 *prevhdr = state->prevhdr, *fragnexthdr_offset; 6888a6a1f17SPablo Neira Ayuso struct sk_buff *frag; 6898a6a1f17SPablo Neira Ayuso struct frag_hdr *fh; 6908a6a1f17SPablo Neira Ayuso unsigned int len; 6918a6a1f17SPablo Neira Ayuso 6928a6a1f17SPablo Neira Ayuso len = state->left; 6938a6a1f17SPablo Neira Ayuso /* IF: it doesn't fit, use 'mtu' - the data space left */ 6948a6a1f17SPablo Neira Ayuso if (len > state->mtu) 6958a6a1f17SPablo Neira Ayuso len = state->mtu; 6968a6a1f17SPablo Neira Ayuso /* IF: we are not sending up to and including the packet end 6978a6a1f17SPablo Neira Ayuso then align the next start on an eight byte boundary */ 6988a6a1f17SPablo Neira Ayuso if (len < state->left) 6998a6a1f17SPablo Neira Ayuso len &= ~7; 7008a6a1f17SPablo Neira Ayuso 7018a6a1f17SPablo Neira Ayuso /* Allocate buffer */ 7028a6a1f17SPablo Neira Ayuso frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) + 7038a6a1f17SPablo Neira Ayuso state->hroom + state->troom, GFP_ATOMIC); 7048a6a1f17SPablo Neira Ayuso if (!frag) 7058a6a1f17SPablo Neira Ayuso return ERR_PTR(-ENOMEM); 7068a6a1f17SPablo Neira Ayuso 7078a6a1f17SPablo Neira Ayuso /* 7088a6a1f17SPablo Neira Ayuso * Set up data on packet 7098a6a1f17SPablo Neira Ayuso */ 7108a6a1f17SPablo Neira Ayuso 7118a6a1f17SPablo Neira Ayuso ip6_copy_metadata(frag, skb); 7128a6a1f17SPablo Neira Ayuso skb_reserve(frag, state->hroom); 7138a6a1f17SPablo Neira Ayuso skb_put(frag, len + state->hlen + sizeof(struct frag_hdr)); 7148a6a1f17SPablo Neira Ayuso skb_reset_network_header(frag); 7158a6a1f17SPablo Neira Ayuso fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen); 7168a6a1f17SPablo Neira Ayuso frag->transport_header = (frag->network_header + state->hlen + 7178a6a1f17SPablo Neira Ayuso sizeof(struct frag_hdr)); 7188a6a1f17SPablo Neira Ayuso 7198a6a1f17SPablo Neira Ayuso /* 7208a6a1f17SPablo Neira Ayuso * Charge the memory for the fragment to any owner 7218a6a1f17SPablo Neira Ayuso * it might possess 7228a6a1f17SPablo Neira Ayuso */ 7238a6a1f17SPablo Neira Ayuso if (skb->sk) 7248a6a1f17SPablo Neira Ayuso skb_set_owner_w(frag, skb->sk); 7258a6a1f17SPablo Neira Ayuso 7268a6a1f17SPablo Neira Ayuso /* 7278a6a1f17SPablo Neira Ayuso * Copy the packet header into the new buffer. 7288a6a1f17SPablo Neira Ayuso */ 7298a6a1f17SPablo Neira Ayuso skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen); 7308a6a1f17SPablo Neira Ayuso 7318a6a1f17SPablo Neira Ayuso fragnexthdr_offset = skb_network_header(frag); 7328a6a1f17SPablo Neira Ayuso fragnexthdr_offset += prevhdr - skb_network_header(skb); 7338a6a1f17SPablo Neira Ayuso *fragnexthdr_offset = NEXTHDR_FRAGMENT; 7348a6a1f17SPablo Neira Ayuso 7358a6a1f17SPablo Neira Ayuso /* 7368a6a1f17SPablo Neira Ayuso * Build fragment header. 7378a6a1f17SPablo Neira Ayuso */ 7388a6a1f17SPablo Neira Ayuso fh->nexthdr = state->nexthdr; 7398a6a1f17SPablo Neira Ayuso fh->reserved = 0; 7408a6a1f17SPablo Neira Ayuso fh->identification = state->frag_id; 7418a6a1f17SPablo Neira Ayuso 7428a6a1f17SPablo Neira Ayuso /* 7438a6a1f17SPablo Neira Ayuso * Copy a block of the IP datagram. 7448a6a1f17SPablo Neira Ayuso */ 7458a6a1f17SPablo Neira Ayuso BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag), 7468a6a1f17SPablo Neira Ayuso len)); 7478a6a1f17SPablo Neira Ayuso state->left -= len; 7488a6a1f17SPablo Neira Ayuso 7498a6a1f17SPablo Neira Ayuso fh->frag_off = htons(state->offset); 7508a6a1f17SPablo Neira Ayuso if (state->left > 0) 7518a6a1f17SPablo Neira Ayuso fh->frag_off |= htons(IP6_MF); 7528a6a1f17SPablo Neira Ayuso ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr)); 7538a6a1f17SPablo Neira Ayuso 7548a6a1f17SPablo Neira Ayuso state->ptr += len; 7558a6a1f17SPablo Neira Ayuso state->offset += len; 7568a6a1f17SPablo Neira Ayuso 7578a6a1f17SPablo Neira Ayuso return frag; 7588a6a1f17SPablo Neira Ayuso } 7598a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_next); 7608a6a1f17SPablo Neira Ayuso 7617d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 7627d8c6e39SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds struct sk_buff *frag; 765adf30907SEric Dumazet struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 766f60e5990Shannes@stressinduktion.org struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ? 767f60e5990Shannes@stressinduktion.org inet6_sk(skb->sk) : NULL; 7688a6a1f17SPablo Neira Ayuso struct ip6_frag_state state; 7698a6a1f17SPablo Neira Ayuso unsigned int mtu, hlen, nexthdr_offset; 7709669fffcSEric Dumazet ktime_t tstamp = skb->tstamp; 7718a6a1f17SPablo Neira Ayuso int hroom, err = 0; 772286c2349SMartin KaFai Lau __be32 frag_id; 7731da177e4SLinus Torvalds u8 *prevhdr, nexthdr = 0; 7741da177e4SLinus Torvalds 7757dd7eb95SDavid S. Miller err = ip6_find_1stfragopt(skb, &prevhdr); 7767dd7eb95SDavid S. Miller if (err < 0) 7772423496aSCraig Gallek goto fail; 7787dd7eb95SDavid S. Miller hlen = err; 7791da177e4SLinus Torvalds nexthdr = *prevhdr; 780ef0efcd3SJunwei Hu nexthdr_offset = prevhdr - skb_network_header(skb); 7811da177e4SLinus Torvalds 782628a5c56SJohn Heffner mtu = ip6_skb_dst_mtu(skb); 783b881ef76SJohn Heffner 784b881ef76SJohn Heffner /* We must not fragment if the socket is set to force MTU discovery 78514f3ad6fSUlrich Weber * or if the skb it not generated by a local socket. 786b881ef76SJohn Heffner */ 787485fca66SFlorian Westphal if (unlikely(!skb->ignore_df && skb->len > mtu)) 788485fca66SFlorian Westphal goto fail_toobig; 789a34a101eSEric Dumazet 790485fca66SFlorian Westphal if (IP6CB(skb)->frag_max_size) { 791485fca66SFlorian Westphal if (IP6CB(skb)->frag_max_size > mtu) 792485fca66SFlorian Westphal goto fail_toobig; 793485fca66SFlorian Westphal 794485fca66SFlorian Westphal /* don't send fragments larger than what we received */ 795485fca66SFlorian Westphal mtu = IP6CB(skb)->frag_max_size; 796485fca66SFlorian Westphal if (mtu < IPV6_MIN_MTU) 797485fca66SFlorian Westphal mtu = IPV6_MIN_MTU; 798b881ef76SJohn Heffner } 799b881ef76SJohn Heffner 800d91675f9SYOSHIFUJI Hideaki if (np && np->frag_size < mtu) { 801d91675f9SYOSHIFUJI Hideaki if (np->frag_size) 802d91675f9SYOSHIFUJI Hideaki mtu = np->frag_size; 803d91675f9SYOSHIFUJI Hideaki } 80489bc7848SHannes Frederic Sowa if (mtu < hlen + sizeof(struct frag_hdr) + 8) 805b72a2b01SHannes Frederic Sowa goto fail_toobig; 8061e0d69a9SHannes Frederic Sowa mtu -= hlen + sizeof(struct frag_hdr); 8071da177e4SLinus Torvalds 808fd0273d7SMartin KaFai Lau frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr, 809fd0273d7SMartin KaFai Lau &ipv6_hdr(skb)->saddr); 810286c2349SMartin KaFai Lau 811405c92f7SHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 812405c92f7SHannes Frederic Sowa (err = skb_checksum_help(skb))) 813405c92f7SHannes Frederic Sowa goto fail; 814405c92f7SHannes Frederic Sowa 815ef0efcd3SJunwei Hu prevhdr = skb_network_header(skb) + nexthdr_offset; 8161d325d21SFlorian Westphal hroom = LL_RESERVED_SPACE(rt->dst.dev); 81721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 818c72d8cdaSAlexey Dobriyan unsigned int first_len = skb_pagelen(skb); 8190feca619SPablo Neira Ayuso struct ip6_fraglist_iter iter; 8203d13008eSEric Dumazet struct sk_buff *frag2; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds if (first_len - hlen > mtu || 8231da177e4SLinus Torvalds ((first_len - hlen) & 7) || 8241d325d21SFlorian Westphal skb_cloned(skb) || 8251d325d21SFlorian Westphal skb_headroom(skb) < (hroom + sizeof(struct frag_hdr))) 8261da177e4SLinus Torvalds goto slow_path; 8271da177e4SLinus Torvalds 8284d9092bbSDavid S. Miller skb_walk_frags(skb, frag) { 8291da177e4SLinus Torvalds /* Correct geometry. */ 8301da177e4SLinus Torvalds if (frag->len > mtu || 8311da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 8321d325d21SFlorian Westphal skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr))) 8333d13008eSEric Dumazet goto slow_path_clean; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* Partially cloned skb? */ 8361da177e4SLinus Torvalds if (skb_shared(frag)) 8373d13008eSEric Dumazet goto slow_path_clean; 8382fdba6b0SHerbert Xu 8392fdba6b0SHerbert Xu BUG_ON(frag->sk); 8402fdba6b0SHerbert Xu if (skb->sk) { 8412fdba6b0SHerbert Xu frag->sk = skb->sk; 8422fdba6b0SHerbert Xu frag->destructor = sock_wfree; 8432fdba6b0SHerbert Xu } 8443d13008eSEric Dumazet skb->truesize -= frag->truesize; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 8470feca619SPablo Neira Ayuso err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id, 8480feca619SPablo Neira Ayuso &iter); 8490feca619SPablo Neira Ayuso if (err < 0) 8501d325d21SFlorian Westphal goto fail; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds for (;;) { 8531da177e4SLinus Torvalds /* Prepare header of the next frame, 8541da177e4SLinus Torvalds * before previous one went down. */ 8550feca619SPablo Neira Ayuso if (iter.frag) 8560feca619SPablo Neira Ayuso ip6_fraglist_prepare(skb, &iter); 8571da177e4SLinus Torvalds 8589669fffcSEric Dumazet skb->tstamp = tstamp; 8597d8c6e39SEric W. Biederman err = output(net, sk, skb); 860dafee490SWei Dong if (!err) 861d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 8623bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGCREATES); 863dafee490SWei Dong 8640feca619SPablo Neira Ayuso if (err || !iter.frag) 8651da177e4SLinus Torvalds break; 8661da177e4SLinus Torvalds 8670feca619SPablo Neira Ayuso skb = ip6_fraglist_next(&iter); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 8700feca619SPablo Neira Ayuso kfree(iter.tmp_hdr); 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds if (err == 0) { 873d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 8743bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGOKS); 8751da177e4SLinus Torvalds return 0; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 878b7034146SEric Dumazet kfree_skb_list(iter.frag); 8791da177e4SLinus Torvalds 880d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 8813bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGFAILS); 8821da177e4SLinus Torvalds return err; 8833d13008eSEric Dumazet 8843d13008eSEric Dumazet slow_path_clean: 8853d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 8863d13008eSEric Dumazet if (frag2 == frag) 8873d13008eSEric Dumazet break; 8883d13008eSEric Dumazet frag2->sk = NULL; 8893d13008eSEric Dumazet frag2->destructor = NULL; 8903d13008eSEric Dumazet skb->truesize += frag2->truesize; 8913d13008eSEric Dumazet } 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds slow_path: 8951da177e4SLinus Torvalds /* 8961da177e4SLinus Torvalds * Fragment the datagram. 8971da177e4SLinus Torvalds */ 8981da177e4SLinus Torvalds 8998a6a1f17SPablo Neira Ayuso ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom, 9008a6a1f17SPablo Neira Ayuso LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id, 9018a6a1f17SPablo Neira Ayuso &state); 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds /* 9041da177e4SLinus Torvalds * Keep copying data until we run out. 9051da177e4SLinus Torvalds */ 90679e49503SFlorian Westphal 9078a6a1f17SPablo Neira Ayuso while (state.left > 0) { 9088a6a1f17SPablo Neira Ayuso frag = ip6_frag_next(skb, &state); 9098a6a1f17SPablo Neira Ayuso if (IS_ERR(frag)) { 9108a6a1f17SPablo Neira Ayuso err = PTR_ERR(frag); 9111da177e4SLinus Torvalds goto fail; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* 9151da177e4SLinus Torvalds * Put this fragment into the sending queue. 9161da177e4SLinus Torvalds */ 9179669fffcSEric Dumazet frag->tstamp = tstamp; 9187d8c6e39SEric W. Biederman err = output(net, sk, frag); 9191da177e4SLinus Torvalds if (err) 9201da177e4SLinus Torvalds goto fail; 921dafee490SWei Dong 922adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 9233bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGCREATES); 9241da177e4SLinus Torvalds } 925adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 926a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_FRAGOKS); 927808db80aSEric Dumazet consume_skb(skb); 9281da177e4SLinus Torvalds return err; 9291da177e4SLinus Torvalds 930485fca66SFlorian Westphal fail_toobig: 931485fca66SFlorian Westphal if (skb->sk && dst_allfrag(skb_dst(skb))) 932485fca66SFlorian Westphal sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK); 933485fca66SFlorian Westphal 934485fca66SFlorian Westphal icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 935485fca66SFlorian Westphal err = -EMSGSIZE; 936485fca66SFlorian Westphal 9371da177e4SLinus Torvalds fail: 938adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 939a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_FRAGFAILS); 9401da177e4SLinus Torvalds kfree_skb(skb); 9411da177e4SLinus Torvalds return err; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds 944b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key, 945b71d1d42SEric Dumazet const struct in6_addr *fl_addr, 946b71d1d42SEric Dumazet const struct in6_addr *addr_cache) 947cf6b1982SYOSHIFUJI Hideaki { 948a02cec21SEric Dumazet return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) && 94963159f29SIan Morris (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache)); 950cf6b1982SYOSHIFUJI Hideaki } 951cf6b1982SYOSHIFUJI Hideaki 952497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk, 953497c615aSHerbert Xu struct dst_entry *dst, 954b71d1d42SEric Dumazet const struct flowi6 *fl6) 9551da177e4SLinus Torvalds { 9561da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 957a963a37dSEric Dumazet struct rt6_info *rt; 9581da177e4SLinus Torvalds 959497c615aSHerbert Xu if (!dst) 960497c615aSHerbert Xu goto out; 9611da177e4SLinus Torvalds 962a963a37dSEric Dumazet if (dst->ops->family != AF_INET6) { 963a963a37dSEric Dumazet dst_release(dst); 964a963a37dSEric Dumazet return NULL; 965a963a37dSEric Dumazet } 966a963a37dSEric Dumazet 967a963a37dSEric Dumazet rt = (struct rt6_info *)dst; 9681da177e4SLinus Torvalds /* Yes, checking route validity in not connected 969d76e60a5SDavid S. Miller * case is not very simple. Take into account, 970d76e60a5SDavid S. Miller * that we do not support routing by source, TOS, 971d76e60a5SDavid S. Miller * and MSG_DONTROUTE --ANK (980726) 972d76e60a5SDavid S. Miller * 973cf6b1982SYOSHIFUJI Hideaki * 1. ip6_rt_check(): If route was host route, 974cf6b1982SYOSHIFUJI Hideaki * check that cached destination is current. 975d76e60a5SDavid S. Miller * If it is network route, we still may 976d76e60a5SDavid S. Miller * check its validity using saved pointer 977d76e60a5SDavid S. Miller * to the last used address: daddr_cache. 978d76e60a5SDavid S. Miller * We do not want to save whole address now, 979d76e60a5SDavid S. Miller * (because main consumer of this service 980d76e60a5SDavid S. Miller * is tcp, which has not this problem), 981d76e60a5SDavid S. Miller * so that the last trick works only on connected 982d76e60a5SDavid S. Miller * sockets. 983d76e60a5SDavid S. Miller * 2. oif also should be the same. 9841da177e4SLinus Torvalds */ 9854c9483b2SDavid S. Miller if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) || 9868e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES 9874c9483b2SDavid S. Miller ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) || 9888e1ef0a9SYOSHIFUJI Hideaki #endif 989ca254490SDavid Ahern (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) && 990ca254490SDavid Ahern (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) { 991497c615aSHerbert Xu dst_release(dst); 992497c615aSHerbert Xu dst = NULL; 9931da177e4SLinus Torvalds } 994497c615aSHerbert Xu 995497c615aSHerbert Xu out: 996497c615aSHerbert Xu return dst; 9971da177e4SLinus Torvalds } 998497c615aSHerbert Xu 9993aef934fSEric Dumazet static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk, 10004c9483b2SDavid S. Miller struct dst_entry **dst, struct flowi6 *fl6) 1001497c615aSHerbert Xu { 100269cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 100369cce1d1SDavid S. Miller struct neighbour *n; 100497cac082SDavid S. Miller struct rt6_info *rt; 100569cce1d1SDavid S. Miller #endif 100669cce1d1SDavid S. Miller int err; 10076f21c96aSPaolo Abeni int flags = 0; 10081da177e4SLinus Torvalds 1009e16e888bSMarkus Stenberg /* The correct way to handle this would be to do 1010e16e888bSMarkus Stenberg * ip6_route_get_saddr, and then ip6_route_output; however, 1011e16e888bSMarkus Stenberg * the route-specific preferred source forces the 1012e16e888bSMarkus Stenberg * ip6_route_output call _before_ ip6_route_get_saddr. 1013e16e888bSMarkus Stenberg * 1014e16e888bSMarkus Stenberg * In source specific routing (no src=any default route), 1015e16e888bSMarkus Stenberg * ip6_route_output will fail given src=any saddr, though, so 1016e16e888bSMarkus Stenberg * that's why we try it again later. 1017e16e888bSMarkus Stenberg */ 1018e16e888bSMarkus Stenberg if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) { 1019a68886a6SDavid Ahern struct fib6_info *from; 1020e16e888bSMarkus Stenberg struct rt6_info *rt; 1021e16e888bSMarkus Stenberg bool had_dst = *dst != NULL; 1022e16e888bSMarkus Stenberg 1023e16e888bSMarkus Stenberg if (!had_dst) 1024e16e888bSMarkus Stenberg *dst = ip6_route_output(net, sk, fl6); 1025e16e888bSMarkus Stenberg rt = (*dst)->error ? NULL : (struct rt6_info *)*dst; 1026a68886a6SDavid Ahern 1027a68886a6SDavid Ahern rcu_read_lock(); 1028a68886a6SDavid Ahern from = rt ? rcu_dereference(rt->from) : NULL; 1029a68886a6SDavid Ahern err = ip6_route_get_saddr(net, from, &fl6->daddr, 1030e16e888bSMarkus Stenberg sk ? inet6_sk(sk)->srcprefs : 0, 1031e16e888bSMarkus Stenberg &fl6->saddr); 1032a68886a6SDavid Ahern rcu_read_unlock(); 1033a68886a6SDavid Ahern 1034e16e888bSMarkus Stenberg if (err) 1035e16e888bSMarkus Stenberg goto out_err_release; 1036e16e888bSMarkus Stenberg 1037e16e888bSMarkus Stenberg /* If we had an erroneous initial result, pretend it 1038e16e888bSMarkus Stenberg * never existed and let the SA-enabled version take 1039e16e888bSMarkus Stenberg * over. 1040e16e888bSMarkus Stenberg */ 1041e16e888bSMarkus Stenberg if (!had_dst && (*dst)->error) { 1042e16e888bSMarkus Stenberg dst_release(*dst); 1043e16e888bSMarkus Stenberg *dst = NULL; 1044e16e888bSMarkus Stenberg } 10456f21c96aSPaolo Abeni 10466f21c96aSPaolo Abeni if (fl6->flowi6_oif) 10476f21c96aSPaolo Abeni flags |= RT6_LOOKUP_F_IFACE; 1048e16e888bSMarkus Stenberg } 1049e16e888bSMarkus Stenberg 105063159f29SIan Morris if (!*dst) 10516f21c96aSPaolo Abeni *dst = ip6_route_output_flags(net, sk, fl6, flags); 10521da177e4SLinus Torvalds 1053e5d08d71SIan Morris err = (*dst)->error; 1054e5d08d71SIan Morris if (err) 10551da177e4SLinus Torvalds goto out_err_release; 10561da177e4SLinus Torvalds 105795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 105895c385b4SNeil Horman /* 105995c385b4SNeil Horman * Here if the dst entry we've looked up 106095c385b4SNeil Horman * has a neighbour entry that is in the INCOMPLETE 106195c385b4SNeil Horman * state and the src address from the flow is 106295c385b4SNeil Horman * marked as OPTIMISTIC, we release the found 106395c385b4SNeil Horman * dst entry and replace it instead with the 106495c385b4SNeil Horman * dst entry of the nexthop router 106595c385b4SNeil Horman */ 1066c56bf6feSEric Dumazet rt = (struct rt6_info *) *dst; 1067707be1ffSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 10682647a9b0SMartin KaFai Lau n = __ipv6_neigh_lookup_noref(rt->dst.dev, 10692647a9b0SMartin KaFai Lau rt6_nexthop(rt, &fl6->daddr)); 1070707be1ffSYOSHIFUJI Hideaki / 吉藤英明 err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0; 1071707be1ffSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 1072707be1ffSYOSHIFUJI Hideaki / 吉藤英明 1073707be1ffSYOSHIFUJI Hideaki / 吉藤英明 if (err) { 107495c385b4SNeil Horman struct inet6_ifaddr *ifp; 10754c9483b2SDavid S. Miller struct flowi6 fl_gw6; 107695c385b4SNeil Horman int redirect; 107795c385b4SNeil Horman 10784c9483b2SDavid S. Miller ifp = ipv6_get_ifaddr(net, &fl6->saddr, 10791cab3da6SDaniel Lezcano (*dst)->dev, 1); 108095c385b4SNeil Horman 108195c385b4SNeil Horman redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC); 108295c385b4SNeil Horman if (ifp) 108395c385b4SNeil Horman in6_ifa_put(ifp); 108495c385b4SNeil Horman 108595c385b4SNeil Horman if (redirect) { 108695c385b4SNeil Horman /* 108795c385b4SNeil Horman * We need to get the dst entry for the 108895c385b4SNeil Horman * default router instead 108995c385b4SNeil Horman */ 109095c385b4SNeil Horman dst_release(*dst); 10914c9483b2SDavid S. Miller memcpy(&fl_gw6, fl6, sizeof(struct flowi6)); 10924c9483b2SDavid S. Miller memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr)); 10934c9483b2SDavid S. Miller *dst = ip6_route_output(net, sk, &fl_gw6); 1094e5d08d71SIan Morris err = (*dst)->error; 1095e5d08d71SIan Morris if (err) 109695c385b4SNeil Horman goto out_err_release; 109795c385b4SNeil Horman } 109895c385b4SNeil Horman } 109995c385b4SNeil Horman #endif 1100ec5e3b0aSJonathan T. Leighton if (ipv6_addr_v4mapped(&fl6->saddr) && 110100ea1ceeSWillem de Bruijn !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) { 110200ea1ceeSWillem de Bruijn err = -EAFNOSUPPORT; 110300ea1ceeSWillem de Bruijn goto out_err_release; 110400ea1ceeSWillem de Bruijn } 110595c385b4SNeil Horman 11061da177e4SLinus Torvalds return 0; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds out_err_release: 11091da177e4SLinus Torvalds dst_release(*dst); 11101da177e4SLinus Torvalds *dst = NULL; 11118a966fc0SDavid Ahern 11120d240e78SDavid Ahern if (err == -ENETUNREACH) 11130d240e78SDavid Ahern IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES); 11141da177e4SLinus Torvalds return err; 11151da177e4SLinus Torvalds } 111634a0b3cdSAdrian Bunk 1117497c615aSHerbert Xu /** 1118497c615aSHerbert Xu * ip6_dst_lookup - perform route lookup on flow 1119b51cd7c8SAndrew Lunn * @net: Network namespace to perform lookup in 1120497c615aSHerbert Xu * @sk: socket which provides route info 1121497c615aSHerbert Xu * @dst: pointer to dst_entry * for result 11224c9483b2SDavid S. Miller * @fl6: flow to lookup 1123497c615aSHerbert Xu * 1124497c615aSHerbert Xu * This function performs a route lookup on the given flow. 1125497c615aSHerbert Xu * 1126497c615aSHerbert Xu * It returns zero on success, or a standard errno code on error. 1127497c615aSHerbert Xu */ 1128343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst, 1129343d60aaSRoopa Prabhu struct flowi6 *fl6) 1130497c615aSHerbert Xu { 1131497c615aSHerbert Xu *dst = NULL; 1132343d60aaSRoopa Prabhu return ip6_dst_lookup_tail(net, sk, dst, fl6); 1133497c615aSHerbert Xu } 11343cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip6_dst_lookup); 11353cf3dc6cSArnaldo Carvalho de Melo 1136497c615aSHerbert Xu /** 113768d0c6d3SDavid S. Miller * ip6_dst_lookup_flow - perform route lookup on flow with ipsec 1138b51cd7c8SAndrew Lunn * @net: Network namespace to perform lookup in 113968d0c6d3SDavid S. Miller * @sk: socket which provides route info 11404c9483b2SDavid S. Miller * @fl6: flow to lookup 114168d0c6d3SDavid S. Miller * @final_dst: final destination address for ipsec lookup 114268d0c6d3SDavid S. Miller * 114368d0c6d3SDavid S. Miller * This function performs a route lookup on the given flow. 114468d0c6d3SDavid S. Miller * 114568d0c6d3SDavid S. Miller * It returns a valid dst pointer on success, or a pointer encoded 114668d0c6d3SDavid S. Miller * error code. 114768d0c6d3SDavid S. Miller */ 1148c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6, 11490e0d44abSSteffen Klassert const struct in6_addr *final_dst) 115068d0c6d3SDavid S. Miller { 115168d0c6d3SDavid S. Miller struct dst_entry *dst = NULL; 115268d0c6d3SDavid S. Miller int err; 115368d0c6d3SDavid S. Miller 1154c4e85f73SSabrina Dubroca err = ip6_dst_lookup_tail(net, sk, &dst, fl6); 115568d0c6d3SDavid S. Miller if (err) 115668d0c6d3SDavid S. Miller return ERR_PTR(err); 115768d0c6d3SDavid S. Miller if (final_dst) 11584e3fd7a0SAlexey Dobriyan fl6->daddr = *final_dst; 11592774c131SDavid S. Miller 1160c4e85f73SSabrina Dubroca return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0); 116168d0c6d3SDavid S. Miller } 116268d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow); 116368d0c6d3SDavid S. Miller 116468d0c6d3SDavid S. Miller /** 116568d0c6d3SDavid S. Miller * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow 116668d0c6d3SDavid S. Miller * @sk: socket which provides the dst cache and route info 11674c9483b2SDavid S. Miller * @fl6: flow to lookup 116868d0c6d3SDavid S. Miller * @final_dst: final destination address for ipsec lookup 116996818159SAlexey Kodanev * @connected: whether @sk is connected or not 1170497c615aSHerbert Xu * 1171497c615aSHerbert Xu * This function performs a route lookup on the given flow with the 1172497c615aSHerbert Xu * possibility of using the cached route in the socket if it is valid. 1173497c615aSHerbert Xu * It will take the socket dst lock when operating on the dst cache. 1174497c615aSHerbert Xu * As a result, this function can only be used in process context. 1175497c615aSHerbert Xu * 117696818159SAlexey Kodanev * In addition, for a connected socket, cache the dst in the socket 117796818159SAlexey Kodanev * if the current cache is not valid. 117896818159SAlexey Kodanev * 117968d0c6d3SDavid S. Miller * It returns a valid dst pointer on success, or a pointer encoded 118068d0c6d3SDavid S. Miller * error code. 1181497c615aSHerbert Xu */ 11824c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, 118396818159SAlexey Kodanev const struct in6_addr *final_dst, 118496818159SAlexey Kodanev bool connected) 1185497c615aSHerbert Xu { 118668d0c6d3SDavid S. Miller struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie); 1187497c615aSHerbert Xu 11884c9483b2SDavid S. Miller dst = ip6_sk_dst_check(sk, dst, fl6); 118996818159SAlexey Kodanev if (dst) 119096818159SAlexey Kodanev return dst; 119196818159SAlexey Kodanev 1192c4e85f73SSabrina Dubroca dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst); 119396818159SAlexey Kodanev if (connected && !IS_ERR(dst)) 119496818159SAlexey Kodanev ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6); 119568d0c6d3SDavid S. Miller 119600bc0ef5SJakub Sitnicki return dst; 119768d0c6d3SDavid S. Miller } 119868d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow); 1199497c615aSHerbert Xu 1200571912c6SMartin Varghese /** 1201571912c6SMartin Varghese * ip6_dst_lookup_tunnel - perform route lookup on tunnel 1202571912c6SMartin Varghese * @skb: Packet for which lookup is done 1203571912c6SMartin Varghese * @dev: Tunnel device 1204571912c6SMartin Varghese * @net: Network namespace of tunnel device 1205b51cd7c8SAndrew Lunn * @sock: Socket which provides route info 1206571912c6SMartin Varghese * @saddr: Memory to store the src ip address 1207571912c6SMartin Varghese * @info: Tunnel information 1208571912c6SMartin Varghese * @protocol: IP protocol 1209b51cd7c8SAndrew Lunn * @use_cache: Flag to enable cache usage 1210571912c6SMartin Varghese * This function performs a route lookup on a tunnel 1211571912c6SMartin Varghese * 1212571912c6SMartin Varghese * It returns a valid dst pointer and stores src address to be used in 1213571912c6SMartin Varghese * tunnel in param saddr on success, else a pointer encoded error code. 1214571912c6SMartin Varghese */ 1215571912c6SMartin Varghese 1216571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb, 1217571912c6SMartin Varghese struct net_device *dev, 1218571912c6SMartin Varghese struct net *net, 1219571912c6SMartin Varghese struct socket *sock, 1220571912c6SMartin Varghese struct in6_addr *saddr, 1221571912c6SMartin Varghese const struct ip_tunnel_info *info, 1222571912c6SMartin Varghese u8 protocol, 1223571912c6SMartin Varghese bool use_cache) 1224571912c6SMartin Varghese { 1225571912c6SMartin Varghese struct dst_entry *dst = NULL; 1226571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE 1227571912c6SMartin Varghese struct dst_cache *dst_cache; 1228571912c6SMartin Varghese #endif 1229571912c6SMartin Varghese struct flowi6 fl6; 1230571912c6SMartin Varghese __u8 prio; 1231571912c6SMartin Varghese 1232571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE 1233571912c6SMartin Varghese dst_cache = (struct dst_cache *)&info->dst_cache; 1234571912c6SMartin Varghese if (use_cache) { 1235571912c6SMartin Varghese dst = dst_cache_get_ip6(dst_cache, saddr); 1236571912c6SMartin Varghese if (dst) 1237571912c6SMartin Varghese return dst; 1238571912c6SMartin Varghese } 1239571912c6SMartin Varghese #endif 1240571912c6SMartin Varghese memset(&fl6, 0, sizeof(fl6)); 1241571912c6SMartin Varghese fl6.flowi6_mark = skb->mark; 1242571912c6SMartin Varghese fl6.flowi6_proto = protocol; 1243571912c6SMartin Varghese fl6.daddr = info->key.u.ipv6.dst; 1244571912c6SMartin Varghese fl6.saddr = info->key.u.ipv6.src; 1245571912c6SMartin Varghese prio = info->key.tos; 1246571912c6SMartin Varghese fl6.flowlabel = ip6_make_flowinfo(RT_TOS(prio), 1247571912c6SMartin Varghese info->key.label); 1248571912c6SMartin Varghese 1249571912c6SMartin Varghese dst = ipv6_stub->ipv6_dst_lookup_flow(net, sock->sk, &fl6, 1250571912c6SMartin Varghese NULL); 1251571912c6SMartin Varghese if (IS_ERR(dst)) { 1252571912c6SMartin Varghese netdev_dbg(dev, "no route to %pI6\n", &fl6.daddr); 1253571912c6SMartin Varghese return ERR_PTR(-ENETUNREACH); 1254571912c6SMartin Varghese } 1255571912c6SMartin Varghese if (dst->dev == dev) { /* is this necessary? */ 1256571912c6SMartin Varghese netdev_dbg(dev, "circular route to %pI6\n", &fl6.daddr); 1257571912c6SMartin Varghese dst_release(dst); 1258571912c6SMartin Varghese return ERR_PTR(-ELOOP); 1259571912c6SMartin Varghese } 1260571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE 1261571912c6SMartin Varghese if (use_cache) 1262571912c6SMartin Varghese dst_cache_set_ip6(dst_cache, dst, &fl6.saddr); 1263571912c6SMartin Varghese #endif 1264571912c6SMartin Varghese *saddr = fl6.saddr; 1265571912c6SMartin Varghese return dst; 1266571912c6SMartin Varghese } 1267571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip6_dst_lookup_tunnel); 1268571912c6SMartin Varghese 12690178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src, 12700178b695SHerbert Xu gfp_t gfp) 12710178b695SHerbert Xu { 12720178b695SHerbert Xu return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; 12730178b695SHerbert Xu } 12740178b695SHerbert Xu 12750178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src, 12760178b695SHerbert Xu gfp_t gfp) 12770178b695SHerbert Xu { 12780178b695SHerbert Xu return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; 12790178b695SHerbert Xu } 12800178b695SHerbert Xu 128175a493e6SHannes Frederic Sowa static void ip6_append_data_mtu(unsigned int *mtu, 12820c183379SGao feng int *maxfraglen, 12830c183379SGao feng unsigned int fragheaderlen, 12840c183379SGao feng struct sk_buff *skb, 128575a493e6SHannes Frederic Sowa struct rt6_info *rt, 1286e367c2d0Slucien unsigned int orig_mtu) 12870c183379SGao feng { 12880c183379SGao feng if (!(rt->dst.flags & DST_XFRM_TUNNEL)) { 128963159f29SIan Morris if (!skb) { 12900c183379SGao feng /* first fragment, reserve header_len */ 1291e367c2d0Slucien *mtu = orig_mtu - rt->dst.header_len; 12920c183379SGao feng 12930c183379SGao feng } else { 12940c183379SGao feng /* 12950c183379SGao feng * this fragment is not first, the headers 12960c183379SGao feng * space is regarded as data space. 12970c183379SGao feng */ 1298e367c2d0Slucien *mtu = orig_mtu; 12990c183379SGao feng } 13000c183379SGao feng *maxfraglen = ((*mtu - fragheaderlen) & ~7) 13010c183379SGao feng + fragheaderlen - sizeof(struct frag_hdr); 13020c183379SGao feng } 13030c183379SGao feng } 13040c183379SGao feng 1305366e41d9SVlad Yasevich static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork, 130626879da5SWei Wang struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6, 13075fdaa88dSWillem de Bruijn struct rt6_info *rt, struct flowi6 *fl6) 1308366e41d9SVlad Yasevich { 1309366e41d9SVlad Yasevich struct ipv6_pinfo *np = inet6_sk(sk); 1310366e41d9SVlad Yasevich unsigned int mtu; 131126879da5SWei Wang struct ipv6_txoptions *opt = ipc6->opt; 1312366e41d9SVlad Yasevich 1313366e41d9SVlad Yasevich /* 1314366e41d9SVlad Yasevich * setup for corking 1315366e41d9SVlad Yasevich */ 1316366e41d9SVlad Yasevich if (opt) { 1317366e41d9SVlad Yasevich if (WARN_ON(v6_cork->opt)) 1318366e41d9SVlad Yasevich return -EINVAL; 1319366e41d9SVlad Yasevich 1320864e2a1fSEric Dumazet v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation); 132163159f29SIan Morris if (unlikely(!v6_cork->opt)) 1322366e41d9SVlad Yasevich return -ENOBUFS; 1323366e41d9SVlad Yasevich 1324864e2a1fSEric Dumazet v6_cork->opt->tot_len = sizeof(*opt); 1325366e41d9SVlad Yasevich v6_cork->opt->opt_flen = opt->opt_flen; 1326366e41d9SVlad Yasevich v6_cork->opt->opt_nflen = opt->opt_nflen; 1327366e41d9SVlad Yasevich 1328366e41d9SVlad Yasevich v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt, 1329366e41d9SVlad Yasevich sk->sk_allocation); 1330366e41d9SVlad Yasevich if (opt->dst0opt && !v6_cork->opt->dst0opt) 1331366e41d9SVlad Yasevich return -ENOBUFS; 1332366e41d9SVlad Yasevich 1333366e41d9SVlad Yasevich v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt, 1334366e41d9SVlad Yasevich sk->sk_allocation); 1335366e41d9SVlad Yasevich if (opt->dst1opt && !v6_cork->opt->dst1opt) 1336366e41d9SVlad Yasevich return -ENOBUFS; 1337366e41d9SVlad Yasevich 1338366e41d9SVlad Yasevich v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt, 1339366e41d9SVlad Yasevich sk->sk_allocation); 1340366e41d9SVlad Yasevich if (opt->hopopt && !v6_cork->opt->hopopt) 1341366e41d9SVlad Yasevich return -ENOBUFS; 1342366e41d9SVlad Yasevich 1343366e41d9SVlad Yasevich v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt, 1344366e41d9SVlad Yasevich sk->sk_allocation); 1345366e41d9SVlad Yasevich if (opt->srcrt && !v6_cork->opt->srcrt) 1346366e41d9SVlad Yasevich return -ENOBUFS; 1347366e41d9SVlad Yasevich 1348366e41d9SVlad Yasevich /* need source address above miyazawa*/ 1349366e41d9SVlad Yasevich } 1350366e41d9SVlad Yasevich dst_hold(&rt->dst); 1351366e41d9SVlad Yasevich cork->base.dst = &rt->dst; 1352366e41d9SVlad Yasevich cork->fl.u.ip6 = *fl6; 135326879da5SWei Wang v6_cork->hop_limit = ipc6->hlimit; 135426879da5SWei Wang v6_cork->tclass = ipc6->tclass; 1355366e41d9SVlad Yasevich if (rt->dst.flags & DST_XFRM_TUNNEL) 1356366e41d9SVlad Yasevich mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ? 1357749439bfSMike Maloney READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst); 1358366e41d9SVlad Yasevich else 1359366e41d9SVlad Yasevich mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ? 1360c02b3741SDavid S. Miller READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst)); 1361366e41d9SVlad Yasevich if (np->frag_size < mtu) { 1362366e41d9SVlad Yasevich if (np->frag_size) 1363366e41d9SVlad Yasevich mtu = np->frag_size; 1364366e41d9SVlad Yasevich } 1365749439bfSMike Maloney if (mtu < IPV6_MIN_MTU) 1366749439bfSMike Maloney return -EINVAL; 1367366e41d9SVlad Yasevich cork->base.fragsize = mtu; 1368fbf47813SWillem de Bruijn cork->base.gso_size = ipc6->gso_size; 1369678ca42dSWillem de Bruijn cork->base.tx_flags = 0; 1370c6af0c22SWillem de Bruijn cork->base.mark = ipc6->sockc.mark; 1371678ca42dSWillem de Bruijn sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags); 1372bec1f6f6SWillem de Bruijn 13730f6c480fSDavid Miller if (dst_allfrag(xfrm_dst_path(&rt->dst))) 1374366e41d9SVlad Yasevich cork->base.flags |= IPCORK_ALLFRAG; 1375366e41d9SVlad Yasevich cork->base.length = 0; 1376366e41d9SVlad Yasevich 13775fdaa88dSWillem de Bruijn cork->base.transmit_time = ipc6->sockc.transmit_time; 1378a818f75eSJesus Sanchez-Palencia 1379366e41d9SVlad Yasevich return 0; 1380366e41d9SVlad Yasevich } 1381366e41d9SVlad Yasevich 13820bbe84a6SVlad Yasevich static int __ip6_append_data(struct sock *sk, 13830bbe84a6SVlad Yasevich struct flowi6 *fl6, 13840bbe84a6SVlad Yasevich struct sk_buff_head *queue, 13850bbe84a6SVlad Yasevich struct inet_cork *cork, 13860bbe84a6SVlad Yasevich struct inet6_cork *v6_cork, 13870bbe84a6SVlad Yasevich struct page_frag *pfrag, 13880bbe84a6SVlad Yasevich int getfrag(void *from, char *to, int offset, 13890bbe84a6SVlad Yasevich int len, int odd, struct sk_buff *skb), 13901da177e4SLinus Torvalds void *from, int length, int transhdrlen, 13915fdaa88dSWillem de Bruijn unsigned int flags, struct ipcm6_cookie *ipc6) 13921da177e4SLinus Torvalds { 13930c183379SGao feng struct sk_buff *skb, *skb_prev = NULL; 139410b8a3deSPaolo Abeni unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu; 1395b5947e5dSWillem de Bruijn struct ubuf_info *uarg = NULL; 13960bbe84a6SVlad Yasevich int exthdrlen = 0; 13970bbe84a6SVlad Yasevich int dst_exthdrlen = 0; 13981da177e4SLinus Torvalds int hh_len; 13991da177e4SLinus Torvalds int copy; 14001da177e4SLinus Torvalds int err; 14011da177e4SLinus Torvalds int offset = 0; 140209c2d251SWillem de Bruijn u32 tskey = 0; 14030bbe84a6SVlad Yasevich struct rt6_info *rt = (struct rt6_info *)cork->dst; 14040bbe84a6SVlad Yasevich struct ipv6_txoptions *opt = v6_cork->opt; 140532dce968SVlad Yasevich int csummode = CHECKSUM_NONE; 1406682b1a9dSHannes Frederic Sowa unsigned int maxnonfragsize, headersize; 14071f4c6eb2SEric Dumazet unsigned int wmem_alloc_delta = 0; 1408100f6d8eSWillem de Bruijn bool paged, extra_uref = false; 14091da177e4SLinus Torvalds 14100bbe84a6SVlad Yasevich skb = skb_peek_tail(queue); 14110bbe84a6SVlad Yasevich if (!skb) { 14120bbe84a6SVlad Yasevich exthdrlen = opt ? opt->opt_flen : 0; 14137efdba5bSRomain KUNTZ dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len; 14141da177e4SLinus Torvalds } 14150bbe84a6SVlad Yasevich 141615e36f5bSWillem de Bruijn paged = !!cork->gso_size; 1417bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize; 1418e367c2d0Slucien orig_mtu = mtu; 14191da177e4SLinus Torvalds 1420678ca42dSWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 1421678ca42dSWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 1422678ca42dSWillem de Bruijn tskey = sk->sk_tskey++; 1423678ca42dSWillem de Bruijn 1424d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 14251da177e4SLinus Torvalds 1426a1b05140SMasahide NAKAMURA fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len + 1427b4ce9277SHerbert Xu (opt ? opt->opt_nflen : 0); 14284df98e76SHannes Frederic Sowa maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - 14294df98e76SHannes Frederic Sowa sizeof(struct frag_hdr); 14301da177e4SLinus Torvalds 14314df98e76SHannes Frederic Sowa headersize = sizeof(struct ipv6hdr) + 14323a1cebe7SHannes Frederic Sowa (opt ? opt->opt_flen + opt->opt_nflen : 0) + 14334df98e76SHannes Frederic Sowa (dst_allfrag(&rt->dst) ? 14344df98e76SHannes Frederic Sowa sizeof(struct frag_hdr) : 0) + 14354df98e76SHannes Frederic Sowa rt->rt6i_nfheader_len; 14364df98e76SHannes Frederic Sowa 143710b8a3deSPaolo Abeni /* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit 143810b8a3deSPaolo Abeni * the first fragment 143910b8a3deSPaolo Abeni */ 144010b8a3deSPaolo Abeni if (headersize + transhdrlen > mtu) 144110b8a3deSPaolo Abeni goto emsgsize; 144210b8a3deSPaolo Abeni 144326879da5SWei Wang if (cork->length + length > mtu - headersize && ipc6->dontfrag && 14444df98e76SHannes Frederic Sowa (sk->sk_protocol == IPPROTO_UDP || 14454df98e76SHannes Frederic Sowa sk->sk_protocol == IPPROTO_RAW)) { 14464df98e76SHannes Frederic Sowa ipv6_local_rxpmtu(sk, fl6, mtu - headersize + 14474df98e76SHannes Frederic Sowa sizeof(struct ipv6hdr)); 14484df98e76SHannes Frederic Sowa goto emsgsize; 14494df98e76SHannes Frederic Sowa } 14504df98e76SHannes Frederic Sowa 1451682b1a9dSHannes Frederic Sowa if (ip6_sk_ignore_df(sk)) 1452682b1a9dSHannes Frederic Sowa maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN; 1453682b1a9dSHannes Frederic Sowa else 1454682b1a9dSHannes Frederic Sowa maxnonfragsize = mtu; 1455682b1a9dSHannes Frederic Sowa 14564df98e76SHannes Frederic Sowa if (cork->length + length > maxnonfragsize - headersize) { 14574df98e76SHannes Frederic Sowa emsgsize: 145810b8a3deSPaolo Abeni pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0); 145910b8a3deSPaolo Abeni ipv6_local_error(sk, EMSGSIZE, fl6, pmtu); 14601da177e4SLinus Torvalds return -EMSGSIZE; 14611da177e4SLinus Torvalds } 1462682b1a9dSHannes Frederic Sowa 1463682b1a9dSHannes Frederic Sowa /* CHECKSUM_PARTIAL only with no extension headers and when 1464682b1a9dSHannes Frederic Sowa * we are not going to fragment 1465682b1a9dSHannes Frederic Sowa */ 1466682b1a9dSHannes Frederic Sowa if (transhdrlen && sk->sk_protocol == IPPROTO_UDP && 1467682b1a9dSHannes Frederic Sowa headersize == sizeof(struct ipv6hdr) && 14682b89ed65SVlad Yasevich length <= mtu - headersize && 1469bec1f6f6SWillem de Bruijn (!(flags & MSG_MORE) || cork->gso_size) && 1470c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM)) 1471682b1a9dSHannes Frederic Sowa csummode = CHECKSUM_PARTIAL; 14721da177e4SLinus Torvalds 1473b5947e5dSWillem de Bruijn if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) { 14748c793822SJonathan Lemon uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb)); 1475b5947e5dSWillem de Bruijn if (!uarg) 1476b5947e5dSWillem de Bruijn return -ENOBUFS; 1477522924b5SWillem de Bruijn extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ 1478b5947e5dSWillem de Bruijn if (rt->dst.dev->features & NETIF_F_SG && 1479b5947e5dSWillem de Bruijn csummode == CHECKSUM_PARTIAL) { 1480b5947e5dSWillem de Bruijn paged = true; 1481b5947e5dSWillem de Bruijn } else { 1482b5947e5dSWillem de Bruijn uarg->zerocopy = 0; 148352900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 1484b5947e5dSWillem de Bruijn } 1485b5947e5dSWillem de Bruijn } 1486b5947e5dSWillem de Bruijn 14871da177e4SLinus Torvalds /* 14881da177e4SLinus Torvalds * Let's try using as much space as possible. 14891da177e4SLinus Torvalds * Use MTU if total length of the message fits into the MTU. 14901da177e4SLinus Torvalds * Otherwise, we need to reserve fragment header and 14911da177e4SLinus Torvalds * fragment alignment (= 8-15 octects, in total). 14921da177e4SLinus Torvalds * 1493634a63e7SRandy Dunlap * Note that we may need to "move" the data from the tail 14941da177e4SLinus Torvalds * of the buffer to the new fragment when we split 14951da177e4SLinus Torvalds * the message. 14961da177e4SLinus Torvalds * 14971da177e4SLinus Torvalds * FIXME: It may be fragmented into multiple chunks 14981da177e4SLinus Torvalds * at once if non-fragmentable extension headers 14991da177e4SLinus Torvalds * are too large. 15001da177e4SLinus Torvalds * --yoshfuji 15011da177e4SLinus Torvalds */ 15021da177e4SLinus Torvalds 15032811ebacSHannes Frederic Sowa cork->length += length; 15042811ebacSHannes Frederic Sowa if (!skb) 15051da177e4SLinus Torvalds goto alloc_new_skb; 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds while (length > 0) { 15081da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 1509bdc712b4SDavid S. Miller copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len; 15101da177e4SLinus Torvalds if (copy < length) 15111da177e4SLinus Torvalds copy = maxfraglen - skb->len; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds if (copy <= 0) { 15141da177e4SLinus Torvalds char *data; 15151da177e4SLinus Torvalds unsigned int datalen; 15161da177e4SLinus Torvalds unsigned int fraglen; 15171da177e4SLinus Torvalds unsigned int fraggap; 15181da177e4SLinus Torvalds unsigned int alloclen; 1519aba36930SWillem de Bruijn unsigned int pagedlen; 15201da177e4SLinus Torvalds alloc_new_skb: 15211da177e4SLinus Torvalds /* There's no room in the current skb */ 15220c183379SGao feng if (skb) 15230c183379SGao feng fraggap = skb->len - maxfraglen; 15241da177e4SLinus Torvalds else 15251da177e4SLinus Torvalds fraggap = 0; 15260c183379SGao feng /* update mtu and maxfraglen if necessary */ 152763159f29SIan Morris if (!skb || !skb_prev) 15280c183379SGao feng ip6_append_data_mtu(&mtu, &maxfraglen, 152975a493e6SHannes Frederic Sowa fragheaderlen, skb, rt, 1530e367c2d0Slucien orig_mtu); 15310c183379SGao feng 15320c183379SGao feng skb_prev = skb; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds /* 15351da177e4SLinus Torvalds * If remaining data exceeds the mtu, 15361da177e4SLinus Torvalds * we know we need more fragment(s). 15371da177e4SLinus Torvalds */ 15381da177e4SLinus Torvalds datalen = length + fraggap; 15391da177e4SLinus Torvalds 15400c183379SGao feng if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen) 15410c183379SGao feng datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len; 154215e36f5bSWillem de Bruijn fraglen = datalen + fragheaderlen; 1543aba36930SWillem de Bruijn pagedlen = 0; 154415e36f5bSWillem de Bruijn 15451da177e4SLinus Torvalds if ((flags & MSG_MORE) && 1546d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 15471da177e4SLinus Torvalds alloclen = mtu; 154815e36f5bSWillem de Bruijn else if (!paged) 154915e36f5bSWillem de Bruijn alloclen = fraglen; 155015e36f5bSWillem de Bruijn else { 155115e36f5bSWillem de Bruijn alloclen = min_t(int, fraglen, MAX_HEADER); 155215e36f5bSWillem de Bruijn pagedlen = fraglen - alloclen; 155315e36f5bSWillem de Bruijn } 15541da177e4SLinus Torvalds 1555299b0767SSteffen Klassert alloclen += dst_exthdrlen; 1556299b0767SSteffen Klassert 15570c183379SGao feng if (datalen != length + fraggap) { 15581da177e4SLinus Torvalds /* 15590c183379SGao feng * this is not the last fragment, the trailer 15600c183379SGao feng * space is regarded as data space. 15611da177e4SLinus Torvalds */ 15620c183379SGao feng datalen += rt->dst.trailer_len; 15630c183379SGao feng } 15640c183379SGao feng 1565d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 15660c183379SGao feng fraglen = datalen + fragheaderlen; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds /* 15691da177e4SLinus Torvalds * We just reserve space for fragment header. 15701da177e4SLinus Torvalds * Note: this may be overallocation if the message 15711da177e4SLinus Torvalds * (without MSG_MORE) fits into the MTU. 15721da177e4SLinus Torvalds */ 15731da177e4SLinus Torvalds alloclen += sizeof(struct frag_hdr); 15741da177e4SLinus Torvalds 157515e36f5bSWillem de Bruijn copy = datalen - transhdrlen - fraggap - pagedlen; 1576232cd35dSEric Dumazet if (copy < 0) { 1577232cd35dSEric Dumazet err = -EINVAL; 1578232cd35dSEric Dumazet goto error; 1579232cd35dSEric Dumazet } 15801da177e4SLinus Torvalds if (transhdrlen) { 15811da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 15821da177e4SLinus Torvalds alloclen + hh_len, 15831da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 15841da177e4SLinus Torvalds } else { 15851da177e4SLinus Torvalds skb = NULL; 15861f4c6eb2SEric Dumazet if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 15871da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 15881f4c6eb2SEric Dumazet skb = alloc_skb(alloclen + hh_len, 15891da177e4SLinus Torvalds sk->sk_allocation); 159063159f29SIan Morris if (unlikely(!skb)) 15911da177e4SLinus Torvalds err = -ENOBUFS; 15921da177e4SLinus Torvalds } 159363159f29SIan Morris if (!skb) 15941da177e4SLinus Torvalds goto error; 15951da177e4SLinus Torvalds /* 15961da177e4SLinus Torvalds * Fill in the control structures 15971da177e4SLinus Torvalds */ 15989c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 159932dce968SVlad Yasevich skb->ip_summed = csummode; 16001da177e4SLinus Torvalds skb->csum = 0; 16011f85851eSGao feng /* reserve for fragmentation and ipsec header */ 16021f85851eSGao feng skb_reserve(skb, hh_len + sizeof(struct frag_hdr) + 16031f85851eSGao feng dst_exthdrlen); 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds /* 16061da177e4SLinus Torvalds * Find where to start putting bytes 16071da177e4SLinus Torvalds */ 160815e36f5bSWillem de Bruijn data = skb_put(skb, fraglen - pagedlen); 16091f85851eSGao feng skb_set_network_header(skb, exthdrlen); 16101f85851eSGao feng data += fragheaderlen; 1611b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1612b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 16131da177e4SLinus Torvalds if (fraggap) { 16141da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 16151da177e4SLinus Torvalds skb_prev, maxfraglen, 16168d5930dfSAl Viro data + transhdrlen, fraggap); 16171da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 16181da177e4SLinus Torvalds skb->csum); 16191da177e4SLinus Torvalds data += fraggap; 1620e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 16211da177e4SLinus Torvalds } 1622232cd35dSEric Dumazet if (copy > 0 && 1623232cd35dSEric Dumazet getfrag(from, data + transhdrlen, offset, 1624232cd35dSEric Dumazet copy, fraggap, skb) < 0) { 16251da177e4SLinus Torvalds err = -EFAULT; 16261da177e4SLinus Torvalds kfree_skb(skb); 16271da177e4SLinus Torvalds goto error; 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds offset += copy; 163115e36f5bSWillem de Bruijn length -= copy + transhdrlen; 16321da177e4SLinus Torvalds transhdrlen = 0; 16331da177e4SLinus Torvalds exthdrlen = 0; 1634299b0767SSteffen Klassert dst_exthdrlen = 0; 16351da177e4SLinus Torvalds 163652900d22SWillem de Bruijn /* Only the initial fragment is time stamped */ 163752900d22SWillem de Bruijn skb_shinfo(skb)->tx_flags = cork->tx_flags; 163852900d22SWillem de Bruijn cork->tx_flags = 0; 163952900d22SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 164052900d22SWillem de Bruijn tskey = 0; 164152900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 164252900d22SWillem de Bruijn 16430dec879fSJulian Anastasov if ((flags & MSG_CONFIRM) && !skb_prev) 16440dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 16450dec879fSJulian Anastasov 16461da177e4SLinus Torvalds /* 16471da177e4SLinus Torvalds * Put the packet on the pending queue 16481da177e4SLinus Torvalds */ 16491f4c6eb2SEric Dumazet if (!skb->destructor) { 16501f4c6eb2SEric Dumazet skb->destructor = sock_wfree; 16511f4c6eb2SEric Dumazet skb->sk = sk; 16521f4c6eb2SEric Dumazet wmem_alloc_delta += skb->truesize; 16531f4c6eb2SEric Dumazet } 16540bbe84a6SVlad Yasevich __skb_queue_tail(queue, skb); 16551da177e4SLinus Torvalds continue; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds if (copy > length) 16591da177e4SLinus Torvalds copy = length; 16601da177e4SLinus Torvalds 1661113f99c3SWillem de Bruijn if (!(rt->dst.dev->features&NETIF_F_SG) && 1662113f99c3SWillem de Bruijn skb_tailroom(skb) >= copy) { 16631da177e4SLinus Torvalds unsigned int off; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds off = skb->len; 16661da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 16671da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 16681da177e4SLinus Torvalds __skb_trim(skb, off); 16691da177e4SLinus Torvalds err = -EFAULT; 16701da177e4SLinus Torvalds goto error; 16711da177e4SLinus Torvalds } 1672b5947e5dSWillem de Bruijn } else if (!uarg || !uarg->zerocopy) { 16731da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds err = -ENOMEM; 16765640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 16771da177e4SLinus Torvalds goto error; 16781da177e4SLinus Torvalds 16795640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 16805640f768SEric Dumazet pfrag->offset)) { 16811da177e4SLinus Torvalds err = -EMSGSIZE; 16825640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 16831da177e4SLinus Torvalds goto error; 16845640f768SEric Dumazet 16855640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 16865640f768SEric Dumazet pfrag->offset, 0); 16875640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 16885640f768SEric Dumazet get_page(pfrag->page); 16891da177e4SLinus Torvalds } 16905640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 16919e903e08SEric Dumazet if (getfrag(from, 16925640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 16935640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 16945640f768SEric Dumazet goto error_efault; 16955640f768SEric Dumazet 16965640f768SEric Dumazet pfrag->offset += copy; 16975640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 16981da177e4SLinus Torvalds skb->len += copy; 16991da177e4SLinus Torvalds skb->data_len += copy; 1700f945fa7aSHerbert Xu skb->truesize += copy; 17011f4c6eb2SEric Dumazet wmem_alloc_delta += copy; 1702b5947e5dSWillem de Bruijn } else { 1703b5947e5dSWillem de Bruijn err = skb_zerocopy_iter_dgram(skb, from, copy); 1704b5947e5dSWillem de Bruijn if (err < 0) 1705b5947e5dSWillem de Bruijn goto error; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds offset += copy; 17081da177e4SLinus Torvalds length -= copy; 17091da177e4SLinus Torvalds } 17105640f768SEric Dumazet 17119e8445a5SPaolo Abeni if (wmem_alloc_delta) 17121f4c6eb2SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 17131da177e4SLinus Torvalds return 0; 17145640f768SEric Dumazet 17155640f768SEric Dumazet error_efault: 17165640f768SEric Dumazet err = -EFAULT; 17171da177e4SLinus Torvalds error: 1718*8e044917SJonathan Lemon net_zcopy_put_abort(uarg, extra_uref); 1719bdc712b4SDavid S. Miller cork->length -= length; 17203bd653c8SDenis V. Lunev IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 17211f4c6eb2SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 17221da177e4SLinus Torvalds return err; 17231da177e4SLinus Torvalds } 17240bbe84a6SVlad Yasevich 17250bbe84a6SVlad Yasevich int ip6_append_data(struct sock *sk, 17260bbe84a6SVlad Yasevich int getfrag(void *from, char *to, int offset, int len, 17270bbe84a6SVlad Yasevich int odd, struct sk_buff *skb), 172826879da5SWei Wang void *from, int length, int transhdrlen, 172926879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 17305fdaa88dSWillem de Bruijn struct rt6_info *rt, unsigned int flags) 17310bbe84a6SVlad Yasevich { 17320bbe84a6SVlad Yasevich struct inet_sock *inet = inet_sk(sk); 17330bbe84a6SVlad Yasevich struct ipv6_pinfo *np = inet6_sk(sk); 17340bbe84a6SVlad Yasevich int exthdrlen; 17350bbe84a6SVlad Yasevich int err; 17360bbe84a6SVlad Yasevich 17370bbe84a6SVlad Yasevich if (flags&MSG_PROBE) 17380bbe84a6SVlad Yasevich return 0; 17390bbe84a6SVlad Yasevich if (skb_queue_empty(&sk->sk_write_queue)) { 17400bbe84a6SVlad Yasevich /* 17410bbe84a6SVlad Yasevich * setup for corking 17420bbe84a6SVlad Yasevich */ 174326879da5SWei Wang err = ip6_setup_cork(sk, &inet->cork, &np->cork, 17445fdaa88dSWillem de Bruijn ipc6, rt, fl6); 17450bbe84a6SVlad Yasevich if (err) 17460bbe84a6SVlad Yasevich return err; 17470bbe84a6SVlad Yasevich 174826879da5SWei Wang exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0); 17490bbe84a6SVlad Yasevich length += exthdrlen; 17500bbe84a6SVlad Yasevich transhdrlen += exthdrlen; 17510bbe84a6SVlad Yasevich } else { 17520bbe84a6SVlad Yasevich fl6 = &inet->cork.fl.u.ip6; 17530bbe84a6SVlad Yasevich transhdrlen = 0; 17540bbe84a6SVlad Yasevich } 17550bbe84a6SVlad Yasevich 17560bbe84a6SVlad Yasevich return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base, 17570bbe84a6SVlad Yasevich &np->cork, sk_page_frag(sk), getfrag, 17585fdaa88dSWillem de Bruijn from, length, transhdrlen, flags, ipc6); 17590bbe84a6SVlad Yasevich } 1760a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data); 17611da177e4SLinus Torvalds 1762366e41d9SVlad Yasevich static void ip6_cork_release(struct inet_cork_full *cork, 1763366e41d9SVlad Yasevich struct inet6_cork *v6_cork) 1764bf138862SPavel Emelyanov { 1765366e41d9SVlad Yasevich if (v6_cork->opt) { 1766366e41d9SVlad Yasevich kfree(v6_cork->opt->dst0opt); 1767366e41d9SVlad Yasevich kfree(v6_cork->opt->dst1opt); 1768366e41d9SVlad Yasevich kfree(v6_cork->opt->hopopt); 1769366e41d9SVlad Yasevich kfree(v6_cork->opt->srcrt); 1770366e41d9SVlad Yasevich kfree(v6_cork->opt); 1771366e41d9SVlad Yasevich v6_cork->opt = NULL; 17720178b695SHerbert Xu } 17730178b695SHerbert Xu 1774366e41d9SVlad Yasevich if (cork->base.dst) { 1775366e41d9SVlad Yasevich dst_release(cork->base.dst); 1776366e41d9SVlad Yasevich cork->base.dst = NULL; 1777366e41d9SVlad Yasevich cork->base.flags &= ~IPCORK_ALLFRAG; 1778bf138862SPavel Emelyanov } 1779366e41d9SVlad Yasevich memset(&cork->fl, 0, sizeof(cork->fl)); 1780bf138862SPavel Emelyanov } 1781bf138862SPavel Emelyanov 17826422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk, 17836422398cSVlad Yasevich struct sk_buff_head *queue, 17846422398cSVlad Yasevich struct inet_cork_full *cork, 17856422398cSVlad Yasevich struct inet6_cork *v6_cork) 17861da177e4SLinus Torvalds { 17871da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 17881da177e4SLinus Torvalds struct sk_buff **tail_skb; 17891da177e4SLinus Torvalds struct in6_addr final_dst_buf, *final_dst = &final_dst_buf; 17901da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 17913bd653c8SDenis V. Lunev struct net *net = sock_net(sk); 17921da177e4SLinus Torvalds struct ipv6hdr *hdr; 17936422398cSVlad Yasevich struct ipv6_txoptions *opt = v6_cork->opt; 17946422398cSVlad Yasevich struct rt6_info *rt = (struct rt6_info *)cork->base.dst; 17956422398cSVlad Yasevich struct flowi6 *fl6 = &cork->fl.u.ip6; 17964c9483b2SDavid S. Miller unsigned char proto = fl6->flowi6_proto; 17971da177e4SLinus Torvalds 17986422398cSVlad Yasevich skb = __skb_dequeue(queue); 179963159f29SIan Morris if (!skb) 18001da177e4SLinus Torvalds goto out; 18011da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1804d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1805bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 18066422398cSVlad Yasevich while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1807cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 18081da177e4SLinus Torvalds *tail_skb = tmp_skb; 18091da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 18101da177e4SLinus Torvalds skb->len += tmp_skb->len; 18111da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 18121da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 18131da177e4SLinus Torvalds tmp_skb->destructor = NULL; 18141da177e4SLinus Torvalds tmp_skb->sk = NULL; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 181728a89453SHerbert Xu /* Allow local fragmentation. */ 181860ff7467SWANG Cong skb->ignore_df = ip6_sk_ignore_df(sk); 181928a89453SHerbert Xu 18204e3fd7a0SAlexey Dobriyan *final_dst = fl6->daddr; 1821cfe1fc77SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_header_len(skb)); 18221da177e4SLinus Torvalds if (opt && opt->opt_flen) 18231da177e4SLinus Torvalds ipv6_push_frag_opts(skb, opt, &proto); 18241da177e4SLinus Torvalds if (opt && opt->opt_nflen) 1825613fa3caSDavid Lebrun ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr); 18261da177e4SLinus Torvalds 1827e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct ipv6hdr)); 1828e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 18290660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 18301da177e4SLinus Torvalds 18316422398cSVlad Yasevich ip6_flow_hdr(hdr, v6_cork->tclass, 1832cb1ce2efSTom Herbert ip6_make_flowlabel(net, skb, fl6->flowlabel, 1833513674b5SShaohua Li ip6_autoflowlabel(net, np), fl6)); 18346422398cSVlad Yasevich hdr->hop_limit = v6_cork->hop_limit; 18351da177e4SLinus Torvalds hdr->nexthdr = proto; 18364e3fd7a0SAlexey Dobriyan hdr->saddr = fl6->saddr; 18374e3fd7a0SAlexey Dobriyan hdr->daddr = *final_dst; 18381da177e4SLinus Torvalds 1839a2c2064fSPatrick McHardy skb->priority = sk->sk_priority; 1840c6af0c22SWillem de Bruijn skb->mark = cork->base.mark; 1841a2c2064fSPatrick McHardy 1842a818f75eSJesus Sanchez-Palencia skb->tstamp = cork->base.transmit_time; 1843a818f75eSJesus Sanchez-Palencia 1844d8d1f30bSChangli Gao skb_dst_set(skb, dst_clone(&rt->dst)); 1845edf391ffSNeil Horman IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 184614878f75SDavid L Stevens if (proto == IPPROTO_ICMPV6) { 1847adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 184814878f75SDavid L Stevens 184943a43b60SHannes Frederic Sowa ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type); 185043a43b60SHannes Frederic Sowa ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); 185114878f75SDavid L Stevens } 185214878f75SDavid L Stevens 18536422398cSVlad Yasevich ip6_cork_release(cork, v6_cork); 18546422398cSVlad Yasevich out: 18556422398cSVlad Yasevich return skb; 18566422398cSVlad Yasevich } 18576422398cSVlad Yasevich 18586422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb) 18596422398cSVlad Yasevich { 18606422398cSVlad Yasevich struct net *net = sock_net(skb->sk); 18616422398cSVlad Yasevich struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 18626422398cSVlad Yasevich int err; 18636422398cSVlad Yasevich 186433224b16SEric W. Biederman err = ip6_local_out(net, skb->sk, skb); 18651da177e4SLinus Torvalds if (err) { 18661da177e4SLinus Torvalds if (err > 0) 18676ce9e7b5SEric Dumazet err = net_xmit_errno(err); 18681da177e4SLinus Torvalds if (err) 18696422398cSVlad Yasevich IP6_INC_STATS(net, rt->rt6i_idev, 18706422398cSVlad Yasevich IPSTATS_MIB_OUTDISCARDS); 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds return err; 18746422398cSVlad Yasevich } 18756422398cSVlad Yasevich 18766422398cSVlad Yasevich int ip6_push_pending_frames(struct sock *sk) 18776422398cSVlad Yasevich { 18786422398cSVlad Yasevich struct sk_buff *skb; 18796422398cSVlad Yasevich 18806422398cSVlad Yasevich skb = ip6_finish_skb(sk); 18816422398cSVlad Yasevich if (!skb) 18826422398cSVlad Yasevich return 0; 18836422398cSVlad Yasevich 18846422398cSVlad Yasevich return ip6_send_skb(skb); 18851da177e4SLinus Torvalds } 1886a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames); 18871da177e4SLinus Torvalds 18880bbe84a6SVlad Yasevich static void __ip6_flush_pending_frames(struct sock *sk, 18896422398cSVlad Yasevich struct sk_buff_head *queue, 18906422398cSVlad Yasevich struct inet_cork_full *cork, 18916422398cSVlad Yasevich struct inet6_cork *v6_cork) 18921da177e4SLinus Torvalds { 18931da177e4SLinus Torvalds struct sk_buff *skb; 18941da177e4SLinus Torvalds 18950bbe84a6SVlad Yasevich while ((skb = __skb_dequeue_tail(queue)) != NULL) { 1896adf30907SEric Dumazet if (skb_dst(skb)) 1897adf30907SEric Dumazet IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)), 1898a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTDISCARDS); 18991da177e4SLinus Torvalds kfree_skb(skb); 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19026422398cSVlad Yasevich ip6_cork_release(cork, v6_cork); 19031da177e4SLinus Torvalds } 19040bbe84a6SVlad Yasevich 19050bbe84a6SVlad Yasevich void ip6_flush_pending_frames(struct sock *sk) 19060bbe84a6SVlad Yasevich { 19076422398cSVlad Yasevich __ip6_flush_pending_frames(sk, &sk->sk_write_queue, 19086422398cSVlad Yasevich &inet_sk(sk)->cork, &inet6_sk(sk)->cork); 19090bbe84a6SVlad Yasevich } 1910a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames); 19116422398cSVlad Yasevich 19126422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk, 19136422398cSVlad Yasevich int getfrag(void *from, char *to, int offset, 19146422398cSVlad Yasevich int len, int odd, struct sk_buff *skb), 19156422398cSVlad Yasevich void *from, int length, int transhdrlen, 191626879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 19176422398cSVlad Yasevich struct rt6_info *rt, unsigned int flags, 19185fdaa88dSWillem de Bruijn struct inet_cork_full *cork) 19196422398cSVlad Yasevich { 19206422398cSVlad Yasevich struct inet6_cork v6_cork; 19216422398cSVlad Yasevich struct sk_buff_head queue; 192226879da5SWei Wang int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0); 19236422398cSVlad Yasevich int err; 19246422398cSVlad Yasevich 19256422398cSVlad Yasevich if (flags & MSG_PROBE) 19266422398cSVlad Yasevich return NULL; 19276422398cSVlad Yasevich 19286422398cSVlad Yasevich __skb_queue_head_init(&queue); 19296422398cSVlad Yasevich 19301cd7884dSWillem de Bruijn cork->base.flags = 0; 19311cd7884dSWillem de Bruijn cork->base.addr = 0; 19321cd7884dSWillem de Bruijn cork->base.opt = NULL; 19331cd7884dSWillem de Bruijn cork->base.dst = NULL; 19346422398cSVlad Yasevich v6_cork.opt = NULL; 19355fdaa88dSWillem de Bruijn err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt, fl6); 1936862c03eeSEric Dumazet if (err) { 19371cd7884dSWillem de Bruijn ip6_cork_release(cork, &v6_cork); 19386422398cSVlad Yasevich return ERR_PTR(err); 1939862c03eeSEric Dumazet } 194026879da5SWei Wang if (ipc6->dontfrag < 0) 194126879da5SWei Wang ipc6->dontfrag = inet6_sk(sk)->dontfrag; 19426422398cSVlad Yasevich 19431cd7884dSWillem de Bruijn err = __ip6_append_data(sk, fl6, &queue, &cork->base, &v6_cork, 19446422398cSVlad Yasevich ¤t->task_frag, getfrag, from, 19456422398cSVlad Yasevich length + exthdrlen, transhdrlen + exthdrlen, 19465fdaa88dSWillem de Bruijn flags, ipc6); 19476422398cSVlad Yasevich if (err) { 19481cd7884dSWillem de Bruijn __ip6_flush_pending_frames(sk, &queue, cork, &v6_cork); 19496422398cSVlad Yasevich return ERR_PTR(err); 19506422398cSVlad Yasevich } 19516422398cSVlad Yasevich 19521cd7884dSWillem de Bruijn return __ip6_make_skb(sk, &queue, cork, &v6_cork); 19536422398cSVlad Yasevich } 1954