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> 571da177e4SLinus Torvalds 587d8c6e39SEric W. Biederman static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 591da177e4SLinus Torvalds { 60adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 611da177e4SLinus Torvalds struct net_device *dev = dst->dev; 629b1c1ef1SNicolas Dichtel const struct in6_addr *nexthop; 63f6b72b62SDavid S. Miller struct neighbour *neigh; 646fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 int ret; 651da177e4SLinus Torvalds 660660e03fSArnaldo Carvalho de Melo if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) { 67adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 681da177e4SLinus Torvalds 697026b1ddSDavid Miller if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) && 708571ab47SYuval Mintz ((mroute6_is_socket(net, skb) && 71bd91b8bfSBenjamin Thery !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) || 720660e03fSArnaldo Carvalho de Melo ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr, 737bc570c8SYOSHIFUJI Hideaki &ipv6_hdr(skb)->saddr))) { 741da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* Do not check for IFF_ALLMULTI; multicast routing 771da177e4SLinus Torvalds is not supported in any case. 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds if (newskb) 80b2e0b385SJan Engelhardt NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, 8129a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 8295603e22SMichel Machado dev_loopback_xmit); 831da177e4SLinus Torvalds 840660e03fSArnaldo Carvalho de Melo if (ipv6_hdr(skb)->hop_limit == 0) { 8578126c41SEric W. Biederman IP6_INC_STATS(net, idev, 863bd653c8SDenis V. Lunev IPSTATS_MIB_OUTDISCARDS); 871da177e4SLinus Torvalds kfree_skb(skb); 881da177e4SLinus Torvalds return 0; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 9278126c41SEric W. Biederman IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len); 93dd408515SHannes Frederic Sowa 94dd408515SHannes Frederic Sowa if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <= 95dd408515SHannes Frederic Sowa IPV6_ADDR_SCOPE_NODELOCAL && 96dd408515SHannes Frederic Sowa !(dev->flags & IFF_LOOPBACK)) { 97dd408515SHannes Frederic Sowa kfree_skb(skb); 98dd408515SHannes Frederic Sowa return 0; 99dd408515SHannes Frederic Sowa } 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 10214972cbdSRoopa Prabhu if (lwtunnel_xmit_redirect(dst->lwtstate)) { 10314972cbdSRoopa Prabhu int res = lwtunnel_xmit(skb); 10414972cbdSRoopa Prabhu 10514972cbdSRoopa Prabhu if (res < 0 || res == LWTUNNEL_XMIT_DONE) 10614972cbdSRoopa Prabhu return res; 10714972cbdSRoopa Prabhu } 10814972cbdSRoopa Prabhu 1096fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 1102647a9b0SMartin KaFai Lau nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr); 1116fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop); 1126fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(!neigh)) 1136fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false); 1146fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR(neigh)) { 1154ff06203SJulian Anastasov sock_confirm_neigh(skb, neigh); 1160353f282SDavid Ahern ret = neigh_output(neigh, skb, false); 1176fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 1186fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 return ret; 1196fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 } 1206fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 12105e3aa09SDavid S. Miller 12278126c41SEric W. Biederman IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); 1239e508490SJan Engelhardt kfree_skb(skb); 1249e508490SJan Engelhardt return -EINVAL; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127956fe219Sbrakmo static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 1289e508490SJan Engelhardt { 12909ee9dbaSTobias Brunner #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 13009ee9dbaSTobias Brunner /* Policy lookup after SNAT yielded a new policy */ 13109ee9dbaSTobias Brunner if (skb_dst(skb)->xfrm) { 13209ee9dbaSTobias Brunner IPCB(skb)->flags |= IPSKB_REROUTED; 13309ee9dbaSTobias Brunner return dst_output(net, sk, skb); 13409ee9dbaSTobias Brunner } 13509ee9dbaSTobias Brunner #endif 13609ee9dbaSTobias Brunner 1379e508490SJan Engelhardt if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) || 1389037c357SJiri Pirko dst_allfrag(skb_dst(skb)) || 1399037c357SJiri Pirko (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size)) 1407d8c6e39SEric W. Biederman return ip6_fragment(net, sk, skb, ip6_finish_output2); 1419e508490SJan Engelhardt else 1427d8c6e39SEric W. Biederman return ip6_finish_output2(net, sk, skb); 1439e508490SJan Engelhardt } 1449e508490SJan Engelhardt 145956fe219Sbrakmo static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 146956fe219Sbrakmo { 147956fe219Sbrakmo int ret; 148956fe219Sbrakmo 149956fe219Sbrakmo ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 150956fe219Sbrakmo switch (ret) { 151956fe219Sbrakmo case NET_XMIT_SUCCESS: 152956fe219Sbrakmo return __ip6_finish_output(net, sk, skb); 153956fe219Sbrakmo case NET_XMIT_CN: 154956fe219Sbrakmo return __ip6_finish_output(net, sk, skb) ? : ret; 155956fe219Sbrakmo default: 156956fe219Sbrakmo kfree_skb(skb); 157956fe219Sbrakmo return ret; 158956fe219Sbrakmo } 159956fe219Sbrakmo } 160956fe219Sbrakmo 161ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb) 1621da177e4SLinus Torvalds { 163*28f8bfd1SPhil Sutter struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev; 164adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 165be10de0aSEric W. Biederman 16697a7a37aSChenbo Feng skb->protocol = htons(ETH_P_IPV6); 16797a7a37aSChenbo Feng skb->dev = dev; 16897a7a37aSChenbo Feng 169778d80beSYOSHIFUJI Hideaki if (unlikely(idev->cnf.disable_ipv6)) { 17019a0644cSEric W. Biederman IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS); 171778d80beSYOSHIFUJI Hideaki kfree_skb(skb); 172778d80beSYOSHIFUJI Hideaki return 0; 173778d80beSYOSHIFUJI Hideaki } 174778d80beSYOSHIFUJI Hideaki 17529a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, 176*28f8bfd1SPhil Sutter net, sk, skb, indev, dev, 1779c6eb28aSJan Engelhardt ip6_finish_output, 1789c6eb28aSJan Engelhardt !(IP6CB(skb)->flags & IP6SKB_REROUTED)); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 181e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np) 182513674b5SShaohua Li { 183513674b5SShaohua Li if (!np->autoflowlabel_set) 184513674b5SShaohua Li return ip6_default_np_autolabel(net); 185513674b5SShaohua Li else 186513674b5SShaohua Li return np->autoflowlabel; 187513674b5SShaohua Li } 188513674b5SShaohua Li 1891da177e4SLinus Torvalds /* 190b5d43998SShan Wei * xmit an sk_buff (used by TCP, SCTP and DCCP) 1911c1e9d2bSEric Dumazet * Note : socket lock is not held for SYNACK packets, but might be modified 1921c1e9d2bSEric Dumazet * by calls to skb_set_owner_w() and ipv6_local_error(), 1931c1e9d2bSEric Dumazet * which are using proper atomic operations or spinlocks. 1941da177e4SLinus Torvalds */ 1951c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, 1964f6570d7SEric Dumazet __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority) 1971da177e4SLinus Torvalds { 1983bd653c8SDenis V. Lunev struct net *net = sock_net(sk); 1991c1e9d2bSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 2004c9483b2SDavid S. Miller struct in6_addr *first_hop = &fl6->daddr; 201adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 20266033f47SStefano Brivio unsigned int head_room; 2031da177e4SLinus Torvalds struct ipv6hdr *hdr; 2044c9483b2SDavid S. Miller u8 proto = fl6->flowi6_proto; 2051da177e4SLinus Torvalds int seg_len = skb->len; 206e651f03aSGerrit Renker int hlimit = -1; 2071da177e4SLinus Torvalds u32 mtu; 2081da177e4SLinus Torvalds 20966033f47SStefano Brivio head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev); 21066033f47SStefano Brivio if (opt) 21166033f47SStefano Brivio head_room += opt->opt_nflen + opt->opt_flen; 2121da177e4SLinus Torvalds 21366033f47SStefano Brivio if (unlikely(skb_headroom(skb) < head_room)) { 2141da177e4SLinus Torvalds struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room); 21563159f29SIan Morris if (!skb2) { 216adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 217a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTDISCARDS); 2181da177e4SLinus Torvalds kfree_skb(skb); 2191da177e4SLinus Torvalds return -ENOBUFS; 2201da177e4SLinus Torvalds } 221bbd6528dSEric Dumazet if (skb->sk) 222bbd6528dSEric Dumazet skb_set_owner_w(skb2, skb->sk); 223808db80aSEric Dumazet consume_skb(skb); 224a11d206dSYOSHIFUJI Hideaki skb = skb2; 2251da177e4SLinus Torvalds } 22666033f47SStefano Brivio 22766033f47SStefano Brivio if (opt) { 22866033f47SStefano Brivio seg_len += opt->opt_nflen + opt->opt_flen; 22966033f47SStefano Brivio 2301da177e4SLinus Torvalds if (opt->opt_flen) 2311da177e4SLinus Torvalds ipv6_push_frag_opts(skb, opt, &proto); 23266033f47SStefano Brivio 2331da177e4SLinus Torvalds if (opt->opt_nflen) 234613fa3caSDavid Lebrun ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop, 235613fa3caSDavid Lebrun &fl6->saddr); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 238e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct ipv6hdr)); 239e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2400660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds /* 2431da177e4SLinus Torvalds * Fill in the IPv6 header 2441da177e4SLinus Torvalds */ 245b903d324SEric Dumazet if (np) 2461da177e4SLinus Torvalds hlimit = np->hop_limit; 2471da177e4SLinus Torvalds if (hlimit < 0) 2486b75d090SYOSHIFUJI Hideaki hlimit = ip6_dst_hoplimit(dst); 2491da177e4SLinus Torvalds 250cb1ce2efSTom Herbert ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel, 251513674b5SShaohua Li ip6_autoflowlabel(net, np), fl6)); 25241a1f8eaSYOSHIFUJI Hideaki 2531da177e4SLinus Torvalds hdr->payload_len = htons(seg_len); 2541da177e4SLinus Torvalds hdr->nexthdr = proto; 2551da177e4SLinus Torvalds hdr->hop_limit = hlimit; 2561da177e4SLinus Torvalds 2574e3fd7a0SAlexey Dobriyan hdr->saddr = fl6->saddr; 2584e3fd7a0SAlexey Dobriyan hdr->daddr = *first_hop; 2591da177e4SLinus Torvalds 2609c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 2614f6570d7SEric Dumazet skb->priority = priority; 26292e55f41SPablo Neira skb->mark = mark; 263a2c2064fSPatrick McHardy 2641da177e4SLinus Torvalds mtu = dst_mtu(dst); 26560ff7467SWANG Cong if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) { 266adf30907SEric Dumazet IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)), 267edf391ffSNeil Horman IPSTATS_MIB_OUT, skb->len); 268a8e3e1a9SDavid Ahern 269a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 270a8e3e1a9SDavid Ahern * skb to its handler for processing 271a8e3e1a9SDavid Ahern */ 272a8e3e1a9SDavid Ahern skb = l3mdev_ip6_out((struct sock *)sk, skb); 273a8e3e1a9SDavid Ahern if (unlikely(!skb)) 274a8e3e1a9SDavid Ahern return 0; 275a8e3e1a9SDavid Ahern 2761c1e9d2bSEric Dumazet /* hooks should never assume socket lock is held. 2771c1e9d2bSEric Dumazet * we promote our socket to non const 2781c1e9d2bSEric Dumazet */ 27929a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, 2801c1e9d2bSEric Dumazet net, (struct sock *)sk, skb, NULL, dst->dev, 28113206b6bSEric W. Biederman dst_output); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds skb->dev = dst->dev; 2851c1e9d2bSEric Dumazet /* ipv6_local_error() does not require socket lock, 2861c1e9d2bSEric Dumazet * we promote our socket to non const 2871c1e9d2bSEric Dumazet */ 2881c1e9d2bSEric Dumazet ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu); 2891c1e9d2bSEric Dumazet 290adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS); 2911da177e4SLinus Torvalds kfree_skb(skb); 2921da177e4SLinus Torvalds return -EMSGSIZE; 2931da177e4SLinus Torvalds } 2947159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit); 2957159039aSYOSHIFUJI Hideaki 2961da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds struct ip6_ra_chain *ra; 2991da177e4SLinus Torvalds struct sock *last = NULL; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds read_lock(&ip6_ra_lock); 3021da177e4SLinus Torvalds for (ra = ip6_ra_chain; ra; ra = ra->next) { 3031da177e4SLinus Torvalds struct sock *sk = ra->sk; 3040bd1b59bSAndrew McDonald if (sk && ra->sel == sel && 3050bd1b59bSAndrew McDonald (!sk->sk_bound_dev_if || 3060bd1b59bSAndrew McDonald sk->sk_bound_dev_if == skb->dev->ifindex)) { 3079036b2feSFrancesco Ruggeri struct ipv6_pinfo *np = inet6_sk(sk); 3089036b2feSFrancesco Ruggeri 3099036b2feSFrancesco Ruggeri if (np && np->rtalert_isolate && 3109036b2feSFrancesco Ruggeri !net_eq(sock_net(sk), dev_net(skb->dev))) { 3119036b2feSFrancesco Ruggeri continue; 3129036b2feSFrancesco Ruggeri } 3131da177e4SLinus Torvalds if (last) { 3141da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 3151da177e4SLinus Torvalds if (skb2) 3161da177e4SLinus Torvalds rawv6_rcv(last, skb2); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds last = sk; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds if (last) { 3231da177e4SLinus Torvalds rawv6_rcv(last, skb); 3241da177e4SLinus Torvalds read_unlock(&ip6_ra_lock); 3251da177e4SLinus Torvalds return 1; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds read_unlock(&ip6_ra_lock); 3281da177e4SLinus Torvalds return 0; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 331e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb) 332e21e0b5fSVille Nuorvala { 3330660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 334e21e0b5fSVille Nuorvala u8 nexthdr = hdr->nexthdr; 33575f2811cSJesse Gross __be16 frag_off; 336e21e0b5fSVille Nuorvala int offset; 337e21e0b5fSVille Nuorvala 338e21e0b5fSVille Nuorvala if (ipv6_ext_hdr(nexthdr)) { 33975f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off); 340e21e0b5fSVille Nuorvala if (offset < 0) 341e21e0b5fSVille Nuorvala return 0; 342e21e0b5fSVille Nuorvala } else 343e21e0b5fSVille Nuorvala offset = sizeof(struct ipv6hdr); 344e21e0b5fSVille Nuorvala 345e21e0b5fSVille Nuorvala if (nexthdr == IPPROTO_ICMPV6) { 346e21e0b5fSVille Nuorvala struct icmp6hdr *icmp6; 347e21e0b5fSVille Nuorvala 348d56f90a7SArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_network_header(skb) + 349d56f90a7SArnaldo Carvalho de Melo offset + 1 - skb->data))) 350e21e0b5fSVille Nuorvala return 0; 351e21e0b5fSVille Nuorvala 352d56f90a7SArnaldo Carvalho de Melo icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset); 353e21e0b5fSVille Nuorvala 354e21e0b5fSVille Nuorvala switch (icmp6->icmp6_type) { 355e21e0b5fSVille Nuorvala case NDISC_ROUTER_SOLICITATION: 356e21e0b5fSVille Nuorvala case NDISC_ROUTER_ADVERTISEMENT: 357e21e0b5fSVille Nuorvala case NDISC_NEIGHBOUR_SOLICITATION: 358e21e0b5fSVille Nuorvala case NDISC_NEIGHBOUR_ADVERTISEMENT: 359e21e0b5fSVille Nuorvala case NDISC_REDIRECT: 360e21e0b5fSVille Nuorvala /* For reaction involving unicast neighbor discovery 361e21e0b5fSVille Nuorvala * message destined to the proxied address, pass it to 362e21e0b5fSVille Nuorvala * input function. 363e21e0b5fSVille Nuorvala */ 364e21e0b5fSVille Nuorvala return 1; 365e21e0b5fSVille Nuorvala default: 366e21e0b5fSVille Nuorvala break; 367e21e0b5fSVille Nuorvala } 368e21e0b5fSVille Nuorvala } 369e21e0b5fSVille Nuorvala 37074553b09SVille Nuorvala /* 37174553b09SVille Nuorvala * The proxying router can't forward traffic sent to a link-local 37274553b09SVille Nuorvala * address, so signal the sender and discard the packet. This 37374553b09SVille Nuorvala * behavior is clarified by the MIPv6 specification. 37474553b09SVille Nuorvala */ 37574553b09SVille Nuorvala if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) { 37674553b09SVille Nuorvala dst_link_failure(skb); 37774553b09SVille Nuorvala return -1; 37874553b09SVille Nuorvala } 37974553b09SVille Nuorvala 380e21e0b5fSVille Nuorvala return 0; 381e21e0b5fSVille Nuorvala } 382e21e0b5fSVille Nuorvala 3830c4b51f0SEric W. Biederman static inline int ip6_forward_finish(struct net *net, struct sock *sk, 3840c4b51f0SEric W. Biederman struct sk_buff *skb) 3851da177e4SLinus Torvalds { 38671a1c915SJeff Barnhill struct dst_entry *dst = skb_dst(skb); 38771a1c915SJeff Barnhill 38871a1c915SJeff Barnhill __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS); 38971a1c915SJeff Barnhill __IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len); 39071a1c915SJeff Barnhill 391f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV 392f839a6c9SIdo Schimmel if (skb->offload_l3_fwd_mark) { 393f839a6c9SIdo Schimmel consume_skb(skb); 394f839a6c9SIdo Schimmel return 0; 395f839a6c9SIdo Schimmel } 396f839a6c9SIdo Schimmel #endif 397f839a6c9SIdo Schimmel 3988203e2d8SEric Dumazet skb->tstamp = 0; 39913206b6bSEric W. Biederman return dst_output(net, sk, skb); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 402fe6cc55fSFlorian Westphal static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) 403fe6cc55fSFlorian Westphal { 404418a3156SFlorian Westphal if (skb->len <= mtu) 405fe6cc55fSFlorian Westphal return false; 406fe6cc55fSFlorian Westphal 40760ff7467SWANG Cong /* ipv6 conntrack defrag sets max_frag_size + ignore_df */ 408fe6cc55fSFlorian Westphal if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu) 409fe6cc55fSFlorian Westphal return true; 410fe6cc55fSFlorian Westphal 41160ff7467SWANG Cong if (skb->ignore_df) 412418a3156SFlorian Westphal return false; 413418a3156SFlorian Westphal 414779b7931SDaniel Axtens if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) 415fe6cc55fSFlorian Westphal return false; 416fe6cc55fSFlorian Westphal 417fe6cc55fSFlorian Westphal return true; 418fe6cc55fSFlorian Westphal } 419fe6cc55fSFlorian Westphal 4201da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb) 4211da177e4SLinus Torvalds { 422bdb7cc64SStephen Suryaputra struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); 423adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 4240660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 4251da177e4SLinus Torvalds struct inet6_skb_parm *opt = IP6CB(skb); 426c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dst->dev); 42714f3ad6fSUlrich Weber u32 mtu; 4281da177e4SLinus Torvalds 42953b7997fSYOSHIFUJI Hideaki if (net->ipv6.devconf_all->forwarding == 0) 4301da177e4SLinus Torvalds goto error; 4311da177e4SLinus Torvalds 432090f1166SLi RongQing if (skb->pkt_type != PACKET_HOST) 433090f1166SLi RongQing goto drop; 434090f1166SLi RongQing 4359ef2e965SHannes Frederic Sowa if (unlikely(skb->sk)) 4369ef2e965SHannes Frederic Sowa goto drop; 4379ef2e965SHannes Frederic Sowa 4384497b076SBen Hutchings if (skb_warn_if_lro(skb)) 4394497b076SBen Hutchings goto drop; 4404497b076SBen Hutchings 4411da177e4SLinus Torvalds if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) { 442bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4431da177e4SLinus Torvalds goto drop; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 44635fc92a9SHerbert Xu skb_forward_csum(skb); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* 4491da177e4SLinus Torvalds * We DO NOT make any processing on 4501da177e4SLinus Torvalds * RA packets, pushing them to user level AS IS 4511da177e4SLinus Torvalds * without ane WARRANTY that application will be able 4521da177e4SLinus Torvalds * to interpret them. The reason is that we 4531da177e4SLinus Torvalds * cannot make anything clever here. 4541da177e4SLinus Torvalds * 4551da177e4SLinus Torvalds * We are not end-node, so that if packet contains 4561da177e4SLinus Torvalds * AH/ESP, we cannot make anything. 4571da177e4SLinus Torvalds * Defragmentation also would be mistake, RA packets 4581da177e4SLinus Torvalds * cannot be fragmented, because there is no warranty 4591da177e4SLinus Torvalds * that different fragments will go along one path. --ANK 4601da177e4SLinus Torvalds */ 461ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 462ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 if (ip6_call_ra_chain(skb, ntohs(opt->ra))) 4631da177e4SLinus Torvalds return 0; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /* 4671da177e4SLinus Torvalds * check and decrement ttl 4681da177e4SLinus Torvalds */ 4691da177e4SLinus Torvalds if (hdr->hop_limit <= 1) { 4701da177e4SLinus Torvalds /* Force OUTPUT device used as source address */ 4711da177e4SLinus Torvalds skb->dev = dst->dev; 4723ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0); 473bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds kfree_skb(skb); 4761da177e4SLinus Torvalds return -ETIMEDOUT; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 479fbea49e1SYOSHIFUJI Hideaki /* XXX: idev->cnf.proxy_ndp? */ 48053b7997fSYOSHIFUJI Hideaki if (net->ipv6.devconf_all->proxy_ndp && 4818a3edd80SDaniel Lezcano pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) { 48274553b09SVille Nuorvala int proxied = ip6_forward_proxy_check(skb); 48374553b09SVille Nuorvala if (proxied > 0) 484e21e0b5fSVille Nuorvala return ip6_input(skb); 48574553b09SVille Nuorvala else if (proxied < 0) { 486bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 48774553b09SVille Nuorvala goto drop; 48874553b09SVille Nuorvala } 489e21e0b5fSVille Nuorvala } 490e21e0b5fSVille Nuorvala 4911da177e4SLinus Torvalds if (!xfrm6_route_forward(skb)) { 492bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4931da177e4SLinus Torvalds goto drop; 4941da177e4SLinus Torvalds } 495adf30907SEric Dumazet dst = skb_dst(skb); 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* IPv6 specs say nothing about it, but it is clear that we cannot 4981da177e4SLinus Torvalds send redirects to source routed frames. 4991e5dc146SMasahide NAKAMURA We don't send redirects to frames decapsulated from IPsec. 5001da177e4SLinus Torvalds */ 5012f17becfSStephen Suryaputra if (IP6CB(skb)->iif == dst->dev->ifindex && 5022f17becfSStephen Suryaputra opt->srcrt == 0 && !skb_sec_path(skb)) { 5031da177e4SLinus Torvalds struct in6_addr *target = NULL; 504fbfe95a4SDavid S. Miller struct inet_peer *peer; 5051da177e4SLinus Torvalds struct rt6_info *rt; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /* 5081da177e4SLinus Torvalds * incoming and outgoing devices are the same 5091da177e4SLinus Torvalds * send a redirect. 5101da177e4SLinus Torvalds */ 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 513c45a3dfbSDavid S. Miller if (rt->rt6i_flags & RTF_GATEWAY) 514c45a3dfbSDavid S. Miller target = &rt->rt6i_gateway; 5151da177e4SLinus Torvalds else 5161da177e4SLinus Torvalds target = &hdr->daddr; 5171da177e4SLinus Torvalds 518fd0273d7SMartin KaFai Lau peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1); 51992d86829SDavid S. Miller 5201da177e4SLinus Torvalds /* Limit redirects both by destination (here) 5211da177e4SLinus Torvalds and by source (inside ndisc_send_redirect) 5221da177e4SLinus Torvalds */ 523fbfe95a4SDavid S. Miller if (inet_peer_xrlim_allow(peer, 1*HZ)) 5244991969aSDavid S. Miller ndisc_send_redirect(skb, target); 5251d861aa4SDavid S. Miller if (peer) 5261d861aa4SDavid S. Miller inet_putpeer(peer); 5275bb1ab09SDavid L Stevens } else { 5285bb1ab09SDavid L Stevens int addrtype = ipv6_addr_type(&hdr->saddr); 5295bb1ab09SDavid L Stevens 5301da177e4SLinus Torvalds /* This check is security critical. */ 531f81b2e7dSYOSHIFUJI Hideaki if (addrtype == IPV6_ADDR_ANY || 532f81b2e7dSYOSHIFUJI Hideaki addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK)) 5331da177e4SLinus Torvalds goto error; 5345bb1ab09SDavid L Stevens if (addrtype & IPV6_ADDR_LINKLOCAL) { 5355bb1ab09SDavid L Stevens icmpv6_send(skb, ICMPV6_DEST_UNREACH, 5363ffe533cSAlexey Dobriyan ICMPV6_NOT_NEIGHBOUR, 0); 5375bb1ab09SDavid L Stevens goto error; 5385bb1ab09SDavid L Stevens } 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5410954cf9cSHannes Frederic Sowa mtu = ip6_dst_mtu_forward(dst); 54214f3ad6fSUlrich Weber if (mtu < IPV6_MIN_MTU) 54314f3ad6fSUlrich Weber mtu = IPV6_MIN_MTU; 54414f3ad6fSUlrich Weber 545fe6cc55fSFlorian Westphal if (ip6_pkt_too_big(skb, mtu)) { 5461da177e4SLinus Torvalds /* Again, force OUTPUT device used as source address */ 5471da177e4SLinus Torvalds skb->dev = dst->dev; 54814f3ad6fSUlrich Weber icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 549bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS); 5501d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(dst), 55115c77d8bSEric Dumazet IPSTATS_MIB_FRAGFAILS); 5521da177e4SLinus Torvalds kfree_skb(skb); 5531da177e4SLinus Torvalds return -EMSGSIZE; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds if (skb_cow(skb, dst->dev->hard_header_len)) { 5571d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(dst), 55815c77d8bSEric Dumazet IPSTATS_MIB_OUTDISCARDS); 5591da177e4SLinus Torvalds goto drop; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5620660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* Mangling hops number delayed to point after skb COW */ 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds hdr->hop_limit--; 5671da177e4SLinus Torvalds 56829a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, 56929a26a56SEric W. Biederman net, NULL, skb, skb->dev, dst->dev, 5706e23ae2aSPatrick McHardy ip6_forward_finish); 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds error: 573bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 5741da177e4SLinus Torvalds drop: 5751da177e4SLinus Torvalds kfree_skb(skb); 5761da177e4SLinus Torvalds return -EINVAL; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 5821da177e4SLinus Torvalds to->priority = from->priority; 5831da177e4SLinus Torvalds to->protocol = from->protocol; 584adf30907SEric Dumazet skb_dst_drop(to); 585adf30907SEric Dumazet skb_dst_set(to, dst_clone(skb_dst(from))); 5861da177e4SLinus Torvalds to->dev = from->dev; 58782e91ffeSThomas Graf to->mark = from->mark; 5881da177e4SLinus Torvalds 5893dd1c9a1SPaolo Abeni skb_copy_hash(to, from); 5903dd1c9a1SPaolo Abeni 5911da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5921da177e4SLinus Torvalds to->tc_index = from->tc_index; 5931da177e4SLinus Torvalds #endif 594e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 595df5042f4SFlorian Westphal skb_ext_copy(to, from); 596984bc16cSJames Morris skb_copy_secmark(to, from); 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5990feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr, 6000feca619SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, 6010feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter) 6020feca619SPablo Neira Ayuso { 6030feca619SPablo Neira Ayuso unsigned int first_len; 6040feca619SPablo Neira Ayuso struct frag_hdr *fh; 6050feca619SPablo Neira Ayuso 6060feca619SPablo Neira Ayuso /* BUILD HEADER */ 6070feca619SPablo Neira Ayuso *prevhdr = NEXTHDR_FRAGMENT; 6080feca619SPablo Neira Ayuso iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC); 6090feca619SPablo Neira Ayuso if (!iter->tmp_hdr) 6100feca619SPablo Neira Ayuso return -ENOMEM; 6110feca619SPablo Neira Ayuso 612b7034146SEric Dumazet iter->frag = skb_shinfo(skb)->frag_list; 6130feca619SPablo Neira Ayuso skb_frag_list_init(skb); 6140feca619SPablo Neira Ayuso 6150feca619SPablo Neira Ayuso iter->offset = 0; 6160feca619SPablo Neira Ayuso iter->hlen = hlen; 6170feca619SPablo Neira Ayuso iter->frag_id = frag_id; 6180feca619SPablo Neira Ayuso iter->nexthdr = nexthdr; 6190feca619SPablo Neira Ayuso 6200feca619SPablo Neira Ayuso __skb_pull(skb, hlen); 6210feca619SPablo Neira Ayuso fh = __skb_push(skb, sizeof(struct frag_hdr)); 6220feca619SPablo Neira Ayuso __skb_push(skb, hlen); 6230feca619SPablo Neira Ayuso skb_reset_network_header(skb); 6240feca619SPablo Neira Ayuso memcpy(skb_network_header(skb), iter->tmp_hdr, hlen); 6250feca619SPablo Neira Ayuso 6260feca619SPablo Neira Ayuso fh->nexthdr = nexthdr; 6270feca619SPablo Neira Ayuso fh->reserved = 0; 6280feca619SPablo Neira Ayuso fh->frag_off = htons(IP6_MF); 6290feca619SPablo Neira Ayuso fh->identification = frag_id; 6300feca619SPablo Neira Ayuso 6310feca619SPablo Neira Ayuso first_len = skb_pagelen(skb); 6320feca619SPablo Neira Ayuso skb->data_len = first_len - skb_headlen(skb); 6330feca619SPablo Neira Ayuso skb->len = first_len; 6340feca619SPablo Neira Ayuso ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr)); 6350feca619SPablo Neira Ayuso 6360feca619SPablo Neira Ayuso return 0; 6370feca619SPablo Neira Ayuso } 6380feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_init); 6390feca619SPablo Neira Ayuso 6400feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb, 6410feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter) 6420feca619SPablo Neira Ayuso { 6430feca619SPablo Neira Ayuso struct sk_buff *frag = iter->frag; 6440feca619SPablo Neira Ayuso unsigned int hlen = iter->hlen; 6450feca619SPablo Neira Ayuso struct frag_hdr *fh; 6460feca619SPablo Neira Ayuso 6470feca619SPablo Neira Ayuso frag->ip_summed = CHECKSUM_NONE; 6480feca619SPablo Neira Ayuso skb_reset_transport_header(frag); 6490feca619SPablo Neira Ayuso fh = __skb_push(frag, sizeof(struct frag_hdr)); 6500feca619SPablo Neira Ayuso __skb_push(frag, hlen); 6510feca619SPablo Neira Ayuso skb_reset_network_header(frag); 6520feca619SPablo Neira Ayuso memcpy(skb_network_header(frag), iter->tmp_hdr, hlen); 6530feca619SPablo Neira Ayuso iter->offset += skb->len - hlen - sizeof(struct frag_hdr); 6540feca619SPablo Neira Ayuso fh->nexthdr = iter->nexthdr; 6550feca619SPablo Neira Ayuso fh->reserved = 0; 6560feca619SPablo Neira Ayuso fh->frag_off = htons(iter->offset); 6570feca619SPablo Neira Ayuso if (frag->next) 6580feca619SPablo Neira Ayuso fh->frag_off |= htons(IP6_MF); 6590feca619SPablo Neira Ayuso fh->identification = iter->frag_id; 6600feca619SPablo Neira Ayuso ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr)); 6610feca619SPablo Neira Ayuso ip6_copy_metadata(frag, skb); 6620feca619SPablo Neira Ayuso } 6630feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_prepare); 6640feca619SPablo Neira Ayuso 6658a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu, 6668a6a1f17SPablo Neira Ayuso unsigned short needed_tailroom, int hdr_room, u8 *prevhdr, 6678a6a1f17SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state) 6688a6a1f17SPablo Neira Ayuso { 6698a6a1f17SPablo Neira Ayuso state->prevhdr = prevhdr; 6708a6a1f17SPablo Neira Ayuso state->nexthdr = nexthdr; 6718a6a1f17SPablo Neira Ayuso state->frag_id = frag_id; 6728a6a1f17SPablo Neira Ayuso 6738a6a1f17SPablo Neira Ayuso state->hlen = hlen; 6748a6a1f17SPablo Neira Ayuso state->mtu = mtu; 6758a6a1f17SPablo Neira Ayuso 6768a6a1f17SPablo Neira Ayuso state->left = skb->len - hlen; /* Space per frame */ 6778a6a1f17SPablo Neira Ayuso state->ptr = hlen; /* Where to start from */ 6788a6a1f17SPablo Neira Ayuso 6798a6a1f17SPablo Neira Ayuso state->hroom = hdr_room; 6808a6a1f17SPablo Neira Ayuso state->troom = needed_tailroom; 6818a6a1f17SPablo Neira Ayuso 6828a6a1f17SPablo Neira Ayuso state->offset = 0; 6838a6a1f17SPablo Neira Ayuso } 6848a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_init); 6858a6a1f17SPablo Neira Ayuso 6868a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state) 6878a6a1f17SPablo Neira Ayuso { 6888a6a1f17SPablo Neira Ayuso u8 *prevhdr = state->prevhdr, *fragnexthdr_offset; 6898a6a1f17SPablo Neira Ayuso struct sk_buff *frag; 6908a6a1f17SPablo Neira Ayuso struct frag_hdr *fh; 6918a6a1f17SPablo Neira Ayuso unsigned int len; 6928a6a1f17SPablo Neira Ayuso 6938a6a1f17SPablo Neira Ayuso len = state->left; 6948a6a1f17SPablo Neira Ayuso /* IF: it doesn't fit, use 'mtu' - the data space left */ 6958a6a1f17SPablo Neira Ayuso if (len > state->mtu) 6968a6a1f17SPablo Neira Ayuso len = state->mtu; 6978a6a1f17SPablo Neira Ayuso /* IF: we are not sending up to and including the packet end 6988a6a1f17SPablo Neira Ayuso then align the next start on an eight byte boundary */ 6998a6a1f17SPablo Neira Ayuso if (len < state->left) 7008a6a1f17SPablo Neira Ayuso len &= ~7; 7018a6a1f17SPablo Neira Ayuso 7028a6a1f17SPablo Neira Ayuso /* Allocate buffer */ 7038a6a1f17SPablo Neira Ayuso frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) + 7048a6a1f17SPablo Neira Ayuso state->hroom + state->troom, GFP_ATOMIC); 7058a6a1f17SPablo Neira Ayuso if (!frag) 7068a6a1f17SPablo Neira Ayuso return ERR_PTR(-ENOMEM); 7078a6a1f17SPablo Neira Ayuso 7088a6a1f17SPablo Neira Ayuso /* 7098a6a1f17SPablo Neira Ayuso * Set up data on packet 7108a6a1f17SPablo Neira Ayuso */ 7118a6a1f17SPablo Neira Ayuso 7128a6a1f17SPablo Neira Ayuso ip6_copy_metadata(frag, skb); 7138a6a1f17SPablo Neira Ayuso skb_reserve(frag, state->hroom); 7148a6a1f17SPablo Neira Ayuso skb_put(frag, len + state->hlen + sizeof(struct frag_hdr)); 7158a6a1f17SPablo Neira Ayuso skb_reset_network_header(frag); 7168a6a1f17SPablo Neira Ayuso fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen); 7178a6a1f17SPablo Neira Ayuso frag->transport_header = (frag->network_header + state->hlen + 7188a6a1f17SPablo Neira Ayuso sizeof(struct frag_hdr)); 7198a6a1f17SPablo Neira Ayuso 7208a6a1f17SPablo Neira Ayuso /* 7218a6a1f17SPablo Neira Ayuso * Charge the memory for the fragment to any owner 7228a6a1f17SPablo Neira Ayuso * it might possess 7238a6a1f17SPablo Neira Ayuso */ 7248a6a1f17SPablo Neira Ayuso if (skb->sk) 7258a6a1f17SPablo Neira Ayuso skb_set_owner_w(frag, skb->sk); 7268a6a1f17SPablo Neira Ayuso 7278a6a1f17SPablo Neira Ayuso /* 7288a6a1f17SPablo Neira Ayuso * Copy the packet header into the new buffer. 7298a6a1f17SPablo Neira Ayuso */ 7308a6a1f17SPablo Neira Ayuso skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen); 7318a6a1f17SPablo Neira Ayuso 7328a6a1f17SPablo Neira Ayuso fragnexthdr_offset = skb_network_header(frag); 7338a6a1f17SPablo Neira Ayuso fragnexthdr_offset += prevhdr - skb_network_header(skb); 7348a6a1f17SPablo Neira Ayuso *fragnexthdr_offset = NEXTHDR_FRAGMENT; 7358a6a1f17SPablo Neira Ayuso 7368a6a1f17SPablo Neira Ayuso /* 7378a6a1f17SPablo Neira Ayuso * Build fragment header. 7388a6a1f17SPablo Neira Ayuso */ 7398a6a1f17SPablo Neira Ayuso fh->nexthdr = state->nexthdr; 7408a6a1f17SPablo Neira Ayuso fh->reserved = 0; 7418a6a1f17SPablo Neira Ayuso fh->identification = state->frag_id; 7428a6a1f17SPablo Neira Ayuso 7438a6a1f17SPablo Neira Ayuso /* 7448a6a1f17SPablo Neira Ayuso * Copy a block of the IP datagram. 7458a6a1f17SPablo Neira Ayuso */ 7468a6a1f17SPablo Neira Ayuso BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag), 7478a6a1f17SPablo Neira Ayuso len)); 7488a6a1f17SPablo Neira Ayuso state->left -= len; 7498a6a1f17SPablo Neira Ayuso 7508a6a1f17SPablo Neira Ayuso fh->frag_off = htons(state->offset); 7518a6a1f17SPablo Neira Ayuso if (state->left > 0) 7528a6a1f17SPablo Neira Ayuso fh->frag_off |= htons(IP6_MF); 7538a6a1f17SPablo Neira Ayuso ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr)); 7548a6a1f17SPablo Neira Ayuso 7558a6a1f17SPablo Neira Ayuso state->ptr += len; 7568a6a1f17SPablo Neira Ayuso state->offset += len; 7578a6a1f17SPablo Neira Ayuso 7588a6a1f17SPablo Neira Ayuso return frag; 7598a6a1f17SPablo Neira Ayuso } 7608a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_next); 7618a6a1f17SPablo Neira Ayuso 7627d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 7637d8c6e39SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds struct sk_buff *frag; 766adf30907SEric Dumazet struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 767f60e5990Shannes@stressinduktion.org struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ? 768f60e5990Shannes@stressinduktion.org inet6_sk(skb->sk) : NULL; 7698a6a1f17SPablo Neira Ayuso struct ip6_frag_state state; 7708a6a1f17SPablo Neira Ayuso unsigned int mtu, hlen, nexthdr_offset; 7719669fffcSEric Dumazet ktime_t tstamp = skb->tstamp; 7728a6a1f17SPablo Neira Ayuso int hroom, err = 0; 773286c2349SMartin KaFai Lau __be32 frag_id; 7741da177e4SLinus Torvalds u8 *prevhdr, nexthdr = 0; 7751da177e4SLinus Torvalds 7767dd7eb95SDavid S. Miller err = ip6_find_1stfragopt(skb, &prevhdr); 7777dd7eb95SDavid S. Miller if (err < 0) 7782423496aSCraig Gallek goto fail; 7797dd7eb95SDavid S. Miller hlen = err; 7801da177e4SLinus Torvalds nexthdr = *prevhdr; 781ef0efcd3SJunwei Hu nexthdr_offset = prevhdr - skb_network_header(skb); 7821da177e4SLinus Torvalds 783628a5c56SJohn Heffner mtu = ip6_skb_dst_mtu(skb); 784b881ef76SJohn Heffner 785b881ef76SJohn Heffner /* We must not fragment if the socket is set to force MTU discovery 78614f3ad6fSUlrich Weber * or if the skb it not generated by a local socket. 787b881ef76SJohn Heffner */ 788485fca66SFlorian Westphal if (unlikely(!skb->ignore_df && skb->len > mtu)) 789485fca66SFlorian Westphal goto fail_toobig; 790a34a101eSEric Dumazet 791485fca66SFlorian Westphal if (IP6CB(skb)->frag_max_size) { 792485fca66SFlorian Westphal if (IP6CB(skb)->frag_max_size > mtu) 793485fca66SFlorian Westphal goto fail_toobig; 794485fca66SFlorian Westphal 795485fca66SFlorian Westphal /* don't send fragments larger than what we received */ 796485fca66SFlorian Westphal mtu = IP6CB(skb)->frag_max_size; 797485fca66SFlorian Westphal if (mtu < IPV6_MIN_MTU) 798485fca66SFlorian Westphal mtu = IPV6_MIN_MTU; 799b881ef76SJohn Heffner } 800b881ef76SJohn Heffner 801d91675f9SYOSHIFUJI Hideaki if (np && np->frag_size < mtu) { 802d91675f9SYOSHIFUJI Hideaki if (np->frag_size) 803d91675f9SYOSHIFUJI Hideaki mtu = np->frag_size; 804d91675f9SYOSHIFUJI Hideaki } 80589bc7848SHannes Frederic Sowa if (mtu < hlen + sizeof(struct frag_hdr) + 8) 806b72a2b01SHannes Frederic Sowa goto fail_toobig; 8071e0d69a9SHannes Frederic Sowa mtu -= hlen + sizeof(struct frag_hdr); 8081da177e4SLinus Torvalds 809fd0273d7SMartin KaFai Lau frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr, 810fd0273d7SMartin KaFai Lau &ipv6_hdr(skb)->saddr); 811286c2349SMartin KaFai Lau 812405c92f7SHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 813405c92f7SHannes Frederic Sowa (err = skb_checksum_help(skb))) 814405c92f7SHannes Frederic Sowa goto fail; 815405c92f7SHannes Frederic Sowa 816ef0efcd3SJunwei Hu prevhdr = skb_network_header(skb) + nexthdr_offset; 8171d325d21SFlorian Westphal hroom = LL_RESERVED_SPACE(rt->dst.dev); 81821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 819c72d8cdaSAlexey Dobriyan unsigned int first_len = skb_pagelen(skb); 8200feca619SPablo Neira Ayuso struct ip6_fraglist_iter iter; 8213d13008eSEric Dumazet struct sk_buff *frag2; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds if (first_len - hlen > mtu || 8241da177e4SLinus Torvalds ((first_len - hlen) & 7) || 8251d325d21SFlorian Westphal skb_cloned(skb) || 8261d325d21SFlorian Westphal skb_headroom(skb) < (hroom + sizeof(struct frag_hdr))) 8271da177e4SLinus Torvalds goto slow_path; 8281da177e4SLinus Torvalds 8294d9092bbSDavid S. Miller skb_walk_frags(skb, frag) { 8301da177e4SLinus Torvalds /* Correct geometry. */ 8311da177e4SLinus Torvalds if (frag->len > mtu || 8321da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 8331d325d21SFlorian Westphal skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr))) 8343d13008eSEric Dumazet goto slow_path_clean; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* Partially cloned skb? */ 8371da177e4SLinus Torvalds if (skb_shared(frag)) 8383d13008eSEric Dumazet goto slow_path_clean; 8392fdba6b0SHerbert Xu 8402fdba6b0SHerbert Xu BUG_ON(frag->sk); 8412fdba6b0SHerbert Xu if (skb->sk) { 8422fdba6b0SHerbert Xu frag->sk = skb->sk; 8432fdba6b0SHerbert Xu frag->destructor = sock_wfree; 8442fdba6b0SHerbert Xu } 8453d13008eSEric Dumazet skb->truesize -= frag->truesize; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 8480feca619SPablo Neira Ayuso err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id, 8490feca619SPablo Neira Ayuso &iter); 8500feca619SPablo Neira Ayuso if (err < 0) 8511d325d21SFlorian Westphal goto fail; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds for (;;) { 8541da177e4SLinus Torvalds /* Prepare header of the next frame, 8551da177e4SLinus Torvalds * before previous one went down. */ 8560feca619SPablo Neira Ayuso if (iter.frag) 8570feca619SPablo Neira Ayuso ip6_fraglist_prepare(skb, &iter); 8581da177e4SLinus Torvalds 8599669fffcSEric Dumazet skb->tstamp = tstamp; 8607d8c6e39SEric W. Biederman err = output(net, sk, skb); 861dafee490SWei Dong if (!err) 862d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 8633bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGCREATES); 864dafee490SWei Dong 8650feca619SPablo Neira Ayuso if (err || !iter.frag) 8661da177e4SLinus Torvalds break; 8671da177e4SLinus Torvalds 8680feca619SPablo Neira Ayuso skb = ip6_fraglist_next(&iter); 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds 8710feca619SPablo Neira Ayuso kfree(iter.tmp_hdr); 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (err == 0) { 874d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 8753bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGOKS); 8761da177e4SLinus Torvalds return 0; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879b7034146SEric Dumazet kfree_skb_list(iter.frag); 8801da177e4SLinus Torvalds 881d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 8823bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGFAILS); 8831da177e4SLinus Torvalds return err; 8843d13008eSEric Dumazet 8853d13008eSEric Dumazet slow_path_clean: 8863d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 8873d13008eSEric Dumazet if (frag2 == frag) 8883d13008eSEric Dumazet break; 8893d13008eSEric Dumazet frag2->sk = NULL; 8903d13008eSEric Dumazet frag2->destructor = NULL; 8913d13008eSEric Dumazet skb->truesize += frag2->truesize; 8923d13008eSEric Dumazet } 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds slow_path: 8961da177e4SLinus Torvalds /* 8971da177e4SLinus Torvalds * Fragment the datagram. 8981da177e4SLinus Torvalds */ 8991da177e4SLinus Torvalds 9008a6a1f17SPablo Neira Ayuso ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom, 9018a6a1f17SPablo Neira Ayuso LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id, 9028a6a1f17SPablo Neira Ayuso &state); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* 9051da177e4SLinus Torvalds * Keep copying data until we run out. 9061da177e4SLinus Torvalds */ 90779e49503SFlorian Westphal 9088a6a1f17SPablo Neira Ayuso while (state.left > 0) { 9098a6a1f17SPablo Neira Ayuso frag = ip6_frag_next(skb, &state); 9108a6a1f17SPablo Neira Ayuso if (IS_ERR(frag)) { 9118a6a1f17SPablo Neira Ayuso err = PTR_ERR(frag); 9121da177e4SLinus Torvalds goto fail; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* 9161da177e4SLinus Torvalds * Put this fragment into the sending queue. 9171da177e4SLinus Torvalds */ 9189669fffcSEric Dumazet frag->tstamp = tstamp; 9197d8c6e39SEric W. Biederman err = output(net, sk, frag); 9201da177e4SLinus Torvalds if (err) 9211da177e4SLinus Torvalds goto fail; 922dafee490SWei Dong 923adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 9243bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGCREATES); 9251da177e4SLinus Torvalds } 926adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 927a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_FRAGOKS); 928808db80aSEric Dumazet consume_skb(skb); 9291da177e4SLinus Torvalds return err; 9301da177e4SLinus Torvalds 931485fca66SFlorian Westphal fail_toobig: 932485fca66SFlorian Westphal if (skb->sk && dst_allfrag(skb_dst(skb))) 933485fca66SFlorian Westphal sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK); 934485fca66SFlorian Westphal 935485fca66SFlorian Westphal icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 936485fca66SFlorian Westphal err = -EMSGSIZE; 937485fca66SFlorian Westphal 9381da177e4SLinus Torvalds fail: 939adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 940a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_FRAGFAILS); 9411da177e4SLinus Torvalds kfree_skb(skb); 9421da177e4SLinus Torvalds return err; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 945b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key, 946b71d1d42SEric Dumazet const struct in6_addr *fl_addr, 947b71d1d42SEric Dumazet const struct in6_addr *addr_cache) 948cf6b1982SYOSHIFUJI Hideaki { 949a02cec21SEric Dumazet return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) && 95063159f29SIan Morris (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache)); 951cf6b1982SYOSHIFUJI Hideaki } 952cf6b1982SYOSHIFUJI Hideaki 953497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk, 954497c615aSHerbert Xu struct dst_entry *dst, 955b71d1d42SEric Dumazet const struct flowi6 *fl6) 9561da177e4SLinus Torvalds { 9571da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 958a963a37dSEric Dumazet struct rt6_info *rt; 9591da177e4SLinus Torvalds 960497c615aSHerbert Xu if (!dst) 961497c615aSHerbert Xu goto out; 9621da177e4SLinus Torvalds 963a963a37dSEric Dumazet if (dst->ops->family != AF_INET6) { 964a963a37dSEric Dumazet dst_release(dst); 965a963a37dSEric Dumazet return NULL; 966a963a37dSEric Dumazet } 967a963a37dSEric Dumazet 968a963a37dSEric Dumazet rt = (struct rt6_info *)dst; 9691da177e4SLinus Torvalds /* Yes, checking route validity in not connected 970d76e60a5SDavid S. Miller * case is not very simple. Take into account, 971d76e60a5SDavid S. Miller * that we do not support routing by source, TOS, 972d76e60a5SDavid S. Miller * and MSG_DONTROUTE --ANK (980726) 973d76e60a5SDavid S. Miller * 974cf6b1982SYOSHIFUJI Hideaki * 1. ip6_rt_check(): If route was host route, 975cf6b1982SYOSHIFUJI Hideaki * check that cached destination is current. 976d76e60a5SDavid S. Miller * If it is network route, we still may 977d76e60a5SDavid S. Miller * check its validity using saved pointer 978d76e60a5SDavid S. Miller * to the last used address: daddr_cache. 979d76e60a5SDavid S. Miller * We do not want to save whole address now, 980d76e60a5SDavid S. Miller * (because main consumer of this service 981d76e60a5SDavid S. Miller * is tcp, which has not this problem), 982d76e60a5SDavid S. Miller * so that the last trick works only on connected 983d76e60a5SDavid S. Miller * sockets. 984d76e60a5SDavid S. Miller * 2. oif also should be the same. 9851da177e4SLinus Torvalds */ 9864c9483b2SDavid S. Miller if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) || 9878e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES 9884c9483b2SDavid S. Miller ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) || 9898e1ef0a9SYOSHIFUJI Hideaki #endif 990ca254490SDavid Ahern (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) && 991ca254490SDavid Ahern (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) { 992497c615aSHerbert Xu dst_release(dst); 993497c615aSHerbert Xu dst = NULL; 9941da177e4SLinus Torvalds } 995497c615aSHerbert Xu 996497c615aSHerbert Xu out: 997497c615aSHerbert Xu return dst; 9981da177e4SLinus Torvalds } 999497c615aSHerbert Xu 10003aef934fSEric Dumazet static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk, 10014c9483b2SDavid S. Miller struct dst_entry **dst, struct flowi6 *fl6) 1002497c615aSHerbert Xu { 100369cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 100469cce1d1SDavid S. Miller struct neighbour *n; 100597cac082SDavid S. Miller struct rt6_info *rt; 100669cce1d1SDavid S. Miller #endif 100769cce1d1SDavid S. Miller int err; 10086f21c96aSPaolo Abeni int flags = 0; 10091da177e4SLinus Torvalds 1010e16e888bSMarkus Stenberg /* The correct way to handle this would be to do 1011e16e888bSMarkus Stenberg * ip6_route_get_saddr, and then ip6_route_output; however, 1012e16e888bSMarkus Stenberg * the route-specific preferred source forces the 1013e16e888bSMarkus Stenberg * ip6_route_output call _before_ ip6_route_get_saddr. 1014e16e888bSMarkus Stenberg * 1015e16e888bSMarkus Stenberg * In source specific routing (no src=any default route), 1016e16e888bSMarkus Stenberg * ip6_route_output will fail given src=any saddr, though, so 1017e16e888bSMarkus Stenberg * that's why we try it again later. 1018e16e888bSMarkus Stenberg */ 1019e16e888bSMarkus Stenberg if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) { 1020a68886a6SDavid Ahern struct fib6_info *from; 1021e16e888bSMarkus Stenberg struct rt6_info *rt; 1022e16e888bSMarkus Stenberg bool had_dst = *dst != NULL; 1023e16e888bSMarkus Stenberg 1024e16e888bSMarkus Stenberg if (!had_dst) 1025e16e888bSMarkus Stenberg *dst = ip6_route_output(net, sk, fl6); 1026e16e888bSMarkus Stenberg rt = (*dst)->error ? NULL : (struct rt6_info *)*dst; 1027a68886a6SDavid Ahern 1028a68886a6SDavid Ahern rcu_read_lock(); 1029a68886a6SDavid Ahern from = rt ? rcu_dereference(rt->from) : NULL; 1030a68886a6SDavid Ahern err = ip6_route_get_saddr(net, from, &fl6->daddr, 1031e16e888bSMarkus Stenberg sk ? inet6_sk(sk)->srcprefs : 0, 1032e16e888bSMarkus Stenberg &fl6->saddr); 1033a68886a6SDavid Ahern rcu_read_unlock(); 1034a68886a6SDavid Ahern 1035e16e888bSMarkus Stenberg if (err) 1036e16e888bSMarkus Stenberg goto out_err_release; 1037e16e888bSMarkus Stenberg 1038e16e888bSMarkus Stenberg /* If we had an erroneous initial result, pretend it 1039e16e888bSMarkus Stenberg * never existed and let the SA-enabled version take 1040e16e888bSMarkus Stenberg * over. 1041e16e888bSMarkus Stenberg */ 1042e16e888bSMarkus Stenberg if (!had_dst && (*dst)->error) { 1043e16e888bSMarkus Stenberg dst_release(*dst); 1044e16e888bSMarkus Stenberg *dst = NULL; 1045e16e888bSMarkus Stenberg } 10466f21c96aSPaolo Abeni 10476f21c96aSPaolo Abeni if (fl6->flowi6_oif) 10486f21c96aSPaolo Abeni flags |= RT6_LOOKUP_F_IFACE; 1049e16e888bSMarkus Stenberg } 1050e16e888bSMarkus Stenberg 105163159f29SIan Morris if (!*dst) 10526f21c96aSPaolo Abeni *dst = ip6_route_output_flags(net, sk, fl6, flags); 10531da177e4SLinus Torvalds 1054e5d08d71SIan Morris err = (*dst)->error; 1055e5d08d71SIan Morris if (err) 10561da177e4SLinus Torvalds goto out_err_release; 10571da177e4SLinus Torvalds 105895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 105995c385b4SNeil Horman /* 106095c385b4SNeil Horman * Here if the dst entry we've looked up 106195c385b4SNeil Horman * has a neighbour entry that is in the INCOMPLETE 106295c385b4SNeil Horman * state and the src address from the flow is 106395c385b4SNeil Horman * marked as OPTIMISTIC, we release the found 106495c385b4SNeil Horman * dst entry and replace it instead with the 106595c385b4SNeil Horman * dst entry of the nexthop router 106695c385b4SNeil Horman */ 1067c56bf6feSEric Dumazet rt = (struct rt6_info *) *dst; 1068707be1ffSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 10692647a9b0SMartin KaFai Lau n = __ipv6_neigh_lookup_noref(rt->dst.dev, 10702647a9b0SMartin KaFai Lau rt6_nexthop(rt, &fl6->daddr)); 1071707be1ffSYOSHIFUJI Hideaki / 吉藤英明 err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0; 1072707be1ffSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 1073707be1ffSYOSHIFUJI Hideaki / 吉藤英明 1074707be1ffSYOSHIFUJI Hideaki / 吉藤英明 if (err) { 107595c385b4SNeil Horman struct inet6_ifaddr *ifp; 10764c9483b2SDavid S. Miller struct flowi6 fl_gw6; 107795c385b4SNeil Horman int redirect; 107895c385b4SNeil Horman 10794c9483b2SDavid S. Miller ifp = ipv6_get_ifaddr(net, &fl6->saddr, 10801cab3da6SDaniel Lezcano (*dst)->dev, 1); 108195c385b4SNeil Horman 108295c385b4SNeil Horman redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC); 108395c385b4SNeil Horman if (ifp) 108495c385b4SNeil Horman in6_ifa_put(ifp); 108595c385b4SNeil Horman 108695c385b4SNeil Horman if (redirect) { 108795c385b4SNeil Horman /* 108895c385b4SNeil Horman * We need to get the dst entry for the 108995c385b4SNeil Horman * default router instead 109095c385b4SNeil Horman */ 109195c385b4SNeil Horman dst_release(*dst); 10924c9483b2SDavid S. Miller memcpy(&fl_gw6, fl6, sizeof(struct flowi6)); 10934c9483b2SDavid S. Miller memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr)); 10944c9483b2SDavid S. Miller *dst = ip6_route_output(net, sk, &fl_gw6); 1095e5d08d71SIan Morris err = (*dst)->error; 1096e5d08d71SIan Morris if (err) 109795c385b4SNeil Horman goto out_err_release; 109895c385b4SNeil Horman } 109995c385b4SNeil Horman } 110095c385b4SNeil Horman #endif 1101ec5e3b0aSJonathan T. Leighton if (ipv6_addr_v4mapped(&fl6->saddr) && 110200ea1ceeSWillem de Bruijn !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) { 110300ea1ceeSWillem de Bruijn err = -EAFNOSUPPORT; 110400ea1ceeSWillem de Bruijn goto out_err_release; 110500ea1ceeSWillem de Bruijn } 110695c385b4SNeil Horman 11071da177e4SLinus Torvalds return 0; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds out_err_release: 11101da177e4SLinus Torvalds dst_release(*dst); 11111da177e4SLinus Torvalds *dst = NULL; 11128a966fc0SDavid Ahern 11130d240e78SDavid Ahern if (err == -ENETUNREACH) 11140d240e78SDavid Ahern IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES); 11151da177e4SLinus Torvalds return err; 11161da177e4SLinus Torvalds } 111734a0b3cdSAdrian Bunk 1118497c615aSHerbert Xu /** 1119497c615aSHerbert Xu * ip6_dst_lookup - perform route lookup on flow 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 113868d0c6d3SDavid S. Miller * @sk: socket which provides route info 11394c9483b2SDavid S. Miller * @fl6: flow to lookup 114068d0c6d3SDavid S. Miller * @final_dst: final destination address for ipsec lookup 114168d0c6d3SDavid S. Miller * 114268d0c6d3SDavid S. Miller * This function performs a route lookup on the given flow. 114368d0c6d3SDavid S. Miller * 114468d0c6d3SDavid S. Miller * It returns a valid dst pointer on success, or a pointer encoded 114568d0c6d3SDavid S. Miller * error code. 114668d0c6d3SDavid S. Miller */ 11473aef934fSEric Dumazet struct dst_entry *ip6_dst_lookup_flow(const struct sock *sk, struct flowi6 *fl6, 11480e0d44abSSteffen Klassert const struct in6_addr *final_dst) 114968d0c6d3SDavid S. Miller { 115068d0c6d3SDavid S. Miller struct dst_entry *dst = NULL; 115168d0c6d3SDavid S. Miller int err; 115268d0c6d3SDavid S. Miller 1153343d60aaSRoopa Prabhu err = ip6_dst_lookup_tail(sock_net(sk), sk, &dst, fl6); 115468d0c6d3SDavid S. Miller if (err) 115568d0c6d3SDavid S. Miller return ERR_PTR(err); 115668d0c6d3SDavid S. Miller if (final_dst) 11574e3fd7a0SAlexey Dobriyan fl6->daddr = *final_dst; 11582774c131SDavid S. Miller 1159f92ee619SSteffen Klassert return xfrm_lookup_route(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0); 116068d0c6d3SDavid S. Miller } 116168d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow); 116268d0c6d3SDavid S. Miller 116368d0c6d3SDavid S. Miller /** 116468d0c6d3SDavid S. Miller * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow 116568d0c6d3SDavid S. Miller * @sk: socket which provides the dst cache and route info 11664c9483b2SDavid S. Miller * @fl6: flow to lookup 116768d0c6d3SDavid S. Miller * @final_dst: final destination address for ipsec lookup 116896818159SAlexey Kodanev * @connected: whether @sk is connected or not 1169497c615aSHerbert Xu * 1170497c615aSHerbert Xu * This function performs a route lookup on the given flow with the 1171497c615aSHerbert Xu * possibility of using the cached route in the socket if it is valid. 1172497c615aSHerbert Xu * It will take the socket dst lock when operating on the dst cache. 1173497c615aSHerbert Xu * As a result, this function can only be used in process context. 1174497c615aSHerbert Xu * 117596818159SAlexey Kodanev * In addition, for a connected socket, cache the dst in the socket 117696818159SAlexey Kodanev * if the current cache is not valid. 117796818159SAlexey Kodanev * 117868d0c6d3SDavid S. Miller * It returns a valid dst pointer on success, or a pointer encoded 117968d0c6d3SDavid S. Miller * error code. 1180497c615aSHerbert Xu */ 11814c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, 118296818159SAlexey Kodanev const struct in6_addr *final_dst, 118396818159SAlexey Kodanev bool connected) 1184497c615aSHerbert Xu { 118568d0c6d3SDavid S. Miller struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie); 1186497c615aSHerbert Xu 11874c9483b2SDavid S. Miller dst = ip6_sk_dst_check(sk, dst, fl6); 118896818159SAlexey Kodanev if (dst) 118996818159SAlexey Kodanev return dst; 119096818159SAlexey Kodanev 119100bc0ef5SJakub Sitnicki dst = ip6_dst_lookup_flow(sk, fl6, final_dst); 119296818159SAlexey Kodanev if (connected && !IS_ERR(dst)) 119396818159SAlexey Kodanev ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6); 119468d0c6d3SDavid S. Miller 119500bc0ef5SJakub Sitnicki return dst; 119668d0c6d3SDavid S. Miller } 119768d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow); 1198497c615aSHerbert Xu 11990178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src, 12000178b695SHerbert Xu gfp_t gfp) 12010178b695SHerbert Xu { 12020178b695SHerbert Xu return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; 12030178b695SHerbert Xu } 12040178b695SHerbert Xu 12050178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src, 12060178b695SHerbert Xu gfp_t gfp) 12070178b695SHerbert Xu { 12080178b695SHerbert Xu return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; 12090178b695SHerbert Xu } 12100178b695SHerbert Xu 121175a493e6SHannes Frederic Sowa static void ip6_append_data_mtu(unsigned int *mtu, 12120c183379SGao feng int *maxfraglen, 12130c183379SGao feng unsigned int fragheaderlen, 12140c183379SGao feng struct sk_buff *skb, 121575a493e6SHannes Frederic Sowa struct rt6_info *rt, 1216e367c2d0Slucien unsigned int orig_mtu) 12170c183379SGao feng { 12180c183379SGao feng if (!(rt->dst.flags & DST_XFRM_TUNNEL)) { 121963159f29SIan Morris if (!skb) { 12200c183379SGao feng /* first fragment, reserve header_len */ 1221e367c2d0Slucien *mtu = orig_mtu - rt->dst.header_len; 12220c183379SGao feng 12230c183379SGao feng } else { 12240c183379SGao feng /* 12250c183379SGao feng * this fragment is not first, the headers 12260c183379SGao feng * space is regarded as data space. 12270c183379SGao feng */ 1228e367c2d0Slucien *mtu = orig_mtu; 12290c183379SGao feng } 12300c183379SGao feng *maxfraglen = ((*mtu - fragheaderlen) & ~7) 12310c183379SGao feng + fragheaderlen - sizeof(struct frag_hdr); 12320c183379SGao feng } 12330c183379SGao feng } 12340c183379SGao feng 1235366e41d9SVlad Yasevich static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork, 123626879da5SWei Wang struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6, 12375fdaa88dSWillem de Bruijn struct rt6_info *rt, struct flowi6 *fl6) 1238366e41d9SVlad Yasevich { 1239366e41d9SVlad Yasevich struct ipv6_pinfo *np = inet6_sk(sk); 1240366e41d9SVlad Yasevich unsigned int mtu; 124126879da5SWei Wang struct ipv6_txoptions *opt = ipc6->opt; 1242366e41d9SVlad Yasevich 1243366e41d9SVlad Yasevich /* 1244366e41d9SVlad Yasevich * setup for corking 1245366e41d9SVlad Yasevich */ 1246366e41d9SVlad Yasevich if (opt) { 1247366e41d9SVlad Yasevich if (WARN_ON(v6_cork->opt)) 1248366e41d9SVlad Yasevich return -EINVAL; 1249366e41d9SVlad Yasevich 1250864e2a1fSEric Dumazet v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation); 125163159f29SIan Morris if (unlikely(!v6_cork->opt)) 1252366e41d9SVlad Yasevich return -ENOBUFS; 1253366e41d9SVlad Yasevich 1254864e2a1fSEric Dumazet v6_cork->opt->tot_len = sizeof(*opt); 1255366e41d9SVlad Yasevich v6_cork->opt->opt_flen = opt->opt_flen; 1256366e41d9SVlad Yasevich v6_cork->opt->opt_nflen = opt->opt_nflen; 1257366e41d9SVlad Yasevich 1258366e41d9SVlad Yasevich v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt, 1259366e41d9SVlad Yasevich sk->sk_allocation); 1260366e41d9SVlad Yasevich if (opt->dst0opt && !v6_cork->opt->dst0opt) 1261366e41d9SVlad Yasevich return -ENOBUFS; 1262366e41d9SVlad Yasevich 1263366e41d9SVlad Yasevich v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt, 1264366e41d9SVlad Yasevich sk->sk_allocation); 1265366e41d9SVlad Yasevich if (opt->dst1opt && !v6_cork->opt->dst1opt) 1266366e41d9SVlad Yasevich return -ENOBUFS; 1267366e41d9SVlad Yasevich 1268366e41d9SVlad Yasevich v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt, 1269366e41d9SVlad Yasevich sk->sk_allocation); 1270366e41d9SVlad Yasevich if (opt->hopopt && !v6_cork->opt->hopopt) 1271366e41d9SVlad Yasevich return -ENOBUFS; 1272366e41d9SVlad Yasevich 1273366e41d9SVlad Yasevich v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt, 1274366e41d9SVlad Yasevich sk->sk_allocation); 1275366e41d9SVlad Yasevich if (opt->srcrt && !v6_cork->opt->srcrt) 1276366e41d9SVlad Yasevich return -ENOBUFS; 1277366e41d9SVlad Yasevich 1278366e41d9SVlad Yasevich /* need source address above miyazawa*/ 1279366e41d9SVlad Yasevich } 1280366e41d9SVlad Yasevich dst_hold(&rt->dst); 1281366e41d9SVlad Yasevich cork->base.dst = &rt->dst; 1282366e41d9SVlad Yasevich cork->fl.u.ip6 = *fl6; 128326879da5SWei Wang v6_cork->hop_limit = ipc6->hlimit; 128426879da5SWei Wang v6_cork->tclass = ipc6->tclass; 1285366e41d9SVlad Yasevich if (rt->dst.flags & DST_XFRM_TUNNEL) 1286366e41d9SVlad Yasevich mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ? 1287749439bfSMike Maloney READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst); 1288366e41d9SVlad Yasevich else 1289366e41d9SVlad Yasevich mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ? 1290c02b3741SDavid S. Miller READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst)); 1291366e41d9SVlad Yasevich if (np->frag_size < mtu) { 1292366e41d9SVlad Yasevich if (np->frag_size) 1293366e41d9SVlad Yasevich mtu = np->frag_size; 1294366e41d9SVlad Yasevich } 1295749439bfSMike Maloney if (mtu < IPV6_MIN_MTU) 1296749439bfSMike Maloney return -EINVAL; 1297366e41d9SVlad Yasevich cork->base.fragsize = mtu; 1298fbf47813SWillem de Bruijn cork->base.gso_size = ipc6->gso_size; 1299678ca42dSWillem de Bruijn cork->base.tx_flags = 0; 1300c6af0c22SWillem de Bruijn cork->base.mark = ipc6->sockc.mark; 1301678ca42dSWillem de Bruijn sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags); 1302bec1f6f6SWillem de Bruijn 13030f6c480fSDavid Miller if (dst_allfrag(xfrm_dst_path(&rt->dst))) 1304366e41d9SVlad Yasevich cork->base.flags |= IPCORK_ALLFRAG; 1305366e41d9SVlad Yasevich cork->base.length = 0; 1306366e41d9SVlad Yasevich 13075fdaa88dSWillem de Bruijn cork->base.transmit_time = ipc6->sockc.transmit_time; 1308a818f75eSJesus Sanchez-Palencia 1309366e41d9SVlad Yasevich return 0; 1310366e41d9SVlad Yasevich } 1311366e41d9SVlad Yasevich 13120bbe84a6SVlad Yasevich static int __ip6_append_data(struct sock *sk, 13130bbe84a6SVlad Yasevich struct flowi6 *fl6, 13140bbe84a6SVlad Yasevich struct sk_buff_head *queue, 13150bbe84a6SVlad Yasevich struct inet_cork *cork, 13160bbe84a6SVlad Yasevich struct inet6_cork *v6_cork, 13170bbe84a6SVlad Yasevich struct page_frag *pfrag, 13180bbe84a6SVlad Yasevich int getfrag(void *from, char *to, int offset, 13190bbe84a6SVlad Yasevich int len, int odd, struct sk_buff *skb), 13201da177e4SLinus Torvalds void *from, int length, int transhdrlen, 13215fdaa88dSWillem de Bruijn unsigned int flags, struct ipcm6_cookie *ipc6) 13221da177e4SLinus Torvalds { 13230c183379SGao feng struct sk_buff *skb, *skb_prev = NULL; 132410b8a3deSPaolo Abeni unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu; 1325b5947e5dSWillem de Bruijn struct ubuf_info *uarg = NULL; 13260bbe84a6SVlad Yasevich int exthdrlen = 0; 13270bbe84a6SVlad Yasevich int dst_exthdrlen = 0; 13281da177e4SLinus Torvalds int hh_len; 13291da177e4SLinus Torvalds int copy; 13301da177e4SLinus Torvalds int err; 13311da177e4SLinus Torvalds int offset = 0; 133209c2d251SWillem de Bruijn u32 tskey = 0; 13330bbe84a6SVlad Yasevich struct rt6_info *rt = (struct rt6_info *)cork->dst; 13340bbe84a6SVlad Yasevich struct ipv6_txoptions *opt = v6_cork->opt; 133532dce968SVlad Yasevich int csummode = CHECKSUM_NONE; 1336682b1a9dSHannes Frederic Sowa unsigned int maxnonfragsize, headersize; 13371f4c6eb2SEric Dumazet unsigned int wmem_alloc_delta = 0; 1338100f6d8eSWillem de Bruijn bool paged, extra_uref = false; 13391da177e4SLinus Torvalds 13400bbe84a6SVlad Yasevich skb = skb_peek_tail(queue); 13410bbe84a6SVlad Yasevich if (!skb) { 13420bbe84a6SVlad Yasevich exthdrlen = opt ? opt->opt_flen : 0; 13437efdba5bSRomain KUNTZ dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len; 13441da177e4SLinus Torvalds } 13450bbe84a6SVlad Yasevich 134615e36f5bSWillem de Bruijn paged = !!cork->gso_size; 1347bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize; 1348e367c2d0Slucien orig_mtu = mtu; 13491da177e4SLinus Torvalds 1350678ca42dSWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 1351678ca42dSWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 1352678ca42dSWillem de Bruijn tskey = sk->sk_tskey++; 1353678ca42dSWillem de Bruijn 1354d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 13551da177e4SLinus Torvalds 1356a1b05140SMasahide NAKAMURA fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len + 1357b4ce9277SHerbert Xu (opt ? opt->opt_nflen : 0); 13584df98e76SHannes Frederic Sowa maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - 13594df98e76SHannes Frederic Sowa sizeof(struct frag_hdr); 13601da177e4SLinus Torvalds 13614df98e76SHannes Frederic Sowa headersize = sizeof(struct ipv6hdr) + 13623a1cebe7SHannes Frederic Sowa (opt ? opt->opt_flen + opt->opt_nflen : 0) + 13634df98e76SHannes Frederic Sowa (dst_allfrag(&rt->dst) ? 13644df98e76SHannes Frederic Sowa sizeof(struct frag_hdr) : 0) + 13654df98e76SHannes Frederic Sowa rt->rt6i_nfheader_len; 13664df98e76SHannes Frederic Sowa 136710b8a3deSPaolo Abeni /* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit 136810b8a3deSPaolo Abeni * the first fragment 136910b8a3deSPaolo Abeni */ 137010b8a3deSPaolo Abeni if (headersize + transhdrlen > mtu) 137110b8a3deSPaolo Abeni goto emsgsize; 137210b8a3deSPaolo Abeni 137326879da5SWei Wang if (cork->length + length > mtu - headersize && ipc6->dontfrag && 13744df98e76SHannes Frederic Sowa (sk->sk_protocol == IPPROTO_UDP || 13754df98e76SHannes Frederic Sowa sk->sk_protocol == IPPROTO_RAW)) { 13764df98e76SHannes Frederic Sowa ipv6_local_rxpmtu(sk, fl6, mtu - headersize + 13774df98e76SHannes Frederic Sowa sizeof(struct ipv6hdr)); 13784df98e76SHannes Frederic Sowa goto emsgsize; 13794df98e76SHannes Frederic Sowa } 13804df98e76SHannes Frederic Sowa 1381682b1a9dSHannes Frederic Sowa if (ip6_sk_ignore_df(sk)) 1382682b1a9dSHannes Frederic Sowa maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN; 1383682b1a9dSHannes Frederic Sowa else 1384682b1a9dSHannes Frederic Sowa maxnonfragsize = mtu; 1385682b1a9dSHannes Frederic Sowa 13864df98e76SHannes Frederic Sowa if (cork->length + length > maxnonfragsize - headersize) { 13874df98e76SHannes Frederic Sowa emsgsize: 138810b8a3deSPaolo Abeni pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0); 138910b8a3deSPaolo Abeni ipv6_local_error(sk, EMSGSIZE, fl6, pmtu); 13901da177e4SLinus Torvalds return -EMSGSIZE; 13911da177e4SLinus Torvalds } 1392682b1a9dSHannes Frederic Sowa 1393682b1a9dSHannes Frederic Sowa /* CHECKSUM_PARTIAL only with no extension headers and when 1394682b1a9dSHannes Frederic Sowa * we are not going to fragment 1395682b1a9dSHannes Frederic Sowa */ 1396682b1a9dSHannes Frederic Sowa if (transhdrlen && sk->sk_protocol == IPPROTO_UDP && 1397682b1a9dSHannes Frederic Sowa headersize == sizeof(struct ipv6hdr) && 13982b89ed65SVlad Yasevich length <= mtu - headersize && 1399bec1f6f6SWillem de Bruijn (!(flags & MSG_MORE) || cork->gso_size) && 1400c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM)) 1401682b1a9dSHannes Frederic Sowa csummode = CHECKSUM_PARTIAL; 14021da177e4SLinus Torvalds 1403b5947e5dSWillem de Bruijn if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) { 1404b5947e5dSWillem de Bruijn uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb)); 1405b5947e5dSWillem de Bruijn if (!uarg) 1406b5947e5dSWillem de Bruijn return -ENOBUFS; 1407522924b5SWillem de Bruijn extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ 1408b5947e5dSWillem de Bruijn if (rt->dst.dev->features & NETIF_F_SG && 1409b5947e5dSWillem de Bruijn csummode == CHECKSUM_PARTIAL) { 1410b5947e5dSWillem de Bruijn paged = true; 1411b5947e5dSWillem de Bruijn } else { 1412b5947e5dSWillem de Bruijn uarg->zerocopy = 0; 141352900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 1414b5947e5dSWillem de Bruijn } 1415b5947e5dSWillem de Bruijn } 1416b5947e5dSWillem de Bruijn 14171da177e4SLinus Torvalds /* 14181da177e4SLinus Torvalds * Let's try using as much space as possible. 14191da177e4SLinus Torvalds * Use MTU if total length of the message fits into the MTU. 14201da177e4SLinus Torvalds * Otherwise, we need to reserve fragment header and 14211da177e4SLinus Torvalds * fragment alignment (= 8-15 octects, in total). 14221da177e4SLinus Torvalds * 14231da177e4SLinus Torvalds * Note that we may need to "move" the data from the tail of 14241da177e4SLinus Torvalds * of the buffer to the new fragment when we split 14251da177e4SLinus Torvalds * the message. 14261da177e4SLinus Torvalds * 14271da177e4SLinus Torvalds * FIXME: It may be fragmented into multiple chunks 14281da177e4SLinus Torvalds * at once if non-fragmentable extension headers 14291da177e4SLinus Torvalds * are too large. 14301da177e4SLinus Torvalds * --yoshfuji 14311da177e4SLinus Torvalds */ 14321da177e4SLinus Torvalds 14332811ebacSHannes Frederic Sowa cork->length += length; 14342811ebacSHannes Frederic Sowa if (!skb) 14351da177e4SLinus Torvalds goto alloc_new_skb; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds while (length > 0) { 14381da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 1439bdc712b4SDavid S. Miller copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len; 14401da177e4SLinus Torvalds if (copy < length) 14411da177e4SLinus Torvalds copy = maxfraglen - skb->len; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds if (copy <= 0) { 14441da177e4SLinus Torvalds char *data; 14451da177e4SLinus Torvalds unsigned int datalen; 14461da177e4SLinus Torvalds unsigned int fraglen; 14471da177e4SLinus Torvalds unsigned int fraggap; 14481da177e4SLinus Torvalds unsigned int alloclen; 1449aba36930SWillem de Bruijn unsigned int pagedlen; 14501da177e4SLinus Torvalds alloc_new_skb: 14511da177e4SLinus Torvalds /* There's no room in the current skb */ 14520c183379SGao feng if (skb) 14530c183379SGao feng fraggap = skb->len - maxfraglen; 14541da177e4SLinus Torvalds else 14551da177e4SLinus Torvalds fraggap = 0; 14560c183379SGao feng /* update mtu and maxfraglen if necessary */ 145763159f29SIan Morris if (!skb || !skb_prev) 14580c183379SGao feng ip6_append_data_mtu(&mtu, &maxfraglen, 145975a493e6SHannes Frederic Sowa fragheaderlen, skb, rt, 1460e367c2d0Slucien orig_mtu); 14610c183379SGao feng 14620c183379SGao feng skb_prev = skb; 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds /* 14651da177e4SLinus Torvalds * If remaining data exceeds the mtu, 14661da177e4SLinus Torvalds * we know we need more fragment(s). 14671da177e4SLinus Torvalds */ 14681da177e4SLinus Torvalds datalen = length + fraggap; 14691da177e4SLinus Torvalds 14700c183379SGao feng if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen) 14710c183379SGao feng datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len; 147215e36f5bSWillem de Bruijn fraglen = datalen + fragheaderlen; 1473aba36930SWillem de Bruijn pagedlen = 0; 147415e36f5bSWillem de Bruijn 14751da177e4SLinus Torvalds if ((flags & MSG_MORE) && 1476d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 14771da177e4SLinus Torvalds alloclen = mtu; 147815e36f5bSWillem de Bruijn else if (!paged) 147915e36f5bSWillem de Bruijn alloclen = fraglen; 148015e36f5bSWillem de Bruijn else { 148115e36f5bSWillem de Bruijn alloclen = min_t(int, fraglen, MAX_HEADER); 148215e36f5bSWillem de Bruijn pagedlen = fraglen - alloclen; 148315e36f5bSWillem de Bruijn } 14841da177e4SLinus Torvalds 1485299b0767SSteffen Klassert alloclen += dst_exthdrlen; 1486299b0767SSteffen Klassert 14870c183379SGao feng if (datalen != length + fraggap) { 14881da177e4SLinus Torvalds /* 14890c183379SGao feng * this is not the last fragment, the trailer 14900c183379SGao feng * space is regarded as data space. 14911da177e4SLinus Torvalds */ 14920c183379SGao feng datalen += rt->dst.trailer_len; 14930c183379SGao feng } 14940c183379SGao feng 1495d8d1f30bSChangli Gao alloclen += rt->dst.trailer_len; 14960c183379SGao feng fraglen = datalen + fragheaderlen; 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds /* 14991da177e4SLinus Torvalds * We just reserve space for fragment header. 15001da177e4SLinus Torvalds * Note: this may be overallocation if the message 15011da177e4SLinus Torvalds * (without MSG_MORE) fits into the MTU. 15021da177e4SLinus Torvalds */ 15031da177e4SLinus Torvalds alloclen += sizeof(struct frag_hdr); 15041da177e4SLinus Torvalds 150515e36f5bSWillem de Bruijn copy = datalen - transhdrlen - fraggap - pagedlen; 1506232cd35dSEric Dumazet if (copy < 0) { 1507232cd35dSEric Dumazet err = -EINVAL; 1508232cd35dSEric Dumazet goto error; 1509232cd35dSEric Dumazet } 15101da177e4SLinus Torvalds if (transhdrlen) { 15111da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, 15121da177e4SLinus Torvalds alloclen + hh_len, 15131da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 15141da177e4SLinus Torvalds } else { 15151da177e4SLinus Torvalds skb = NULL; 15161f4c6eb2SEric Dumazet if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 15171da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 15181f4c6eb2SEric Dumazet skb = alloc_skb(alloclen + hh_len, 15191da177e4SLinus Torvalds sk->sk_allocation); 152063159f29SIan Morris if (unlikely(!skb)) 15211da177e4SLinus Torvalds err = -ENOBUFS; 15221da177e4SLinus Torvalds } 152363159f29SIan Morris if (!skb) 15241da177e4SLinus Torvalds goto error; 15251da177e4SLinus Torvalds /* 15261da177e4SLinus Torvalds * Fill in the control structures 15271da177e4SLinus Torvalds */ 15289c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 152932dce968SVlad Yasevich skb->ip_summed = csummode; 15301da177e4SLinus Torvalds skb->csum = 0; 15311f85851eSGao feng /* reserve for fragmentation and ipsec header */ 15321f85851eSGao feng skb_reserve(skb, hh_len + sizeof(struct frag_hdr) + 15331f85851eSGao feng dst_exthdrlen); 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds /* 15361da177e4SLinus Torvalds * Find where to start putting bytes 15371da177e4SLinus Torvalds */ 153815e36f5bSWillem de Bruijn data = skb_put(skb, fraglen - pagedlen); 15391f85851eSGao feng skb_set_network_header(skb, exthdrlen); 15401f85851eSGao feng data += fragheaderlen; 1541b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1542b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 15431da177e4SLinus Torvalds if (fraggap) { 15441da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 15451da177e4SLinus Torvalds skb_prev, maxfraglen, 15461da177e4SLinus Torvalds data + transhdrlen, fraggap, 0); 15471da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 15481da177e4SLinus Torvalds skb->csum); 15491da177e4SLinus Torvalds data += fraggap; 1550e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 15511da177e4SLinus Torvalds } 1552232cd35dSEric Dumazet if (copy > 0 && 1553232cd35dSEric Dumazet getfrag(from, data + transhdrlen, offset, 1554232cd35dSEric Dumazet copy, fraggap, skb) < 0) { 15551da177e4SLinus Torvalds err = -EFAULT; 15561da177e4SLinus Torvalds kfree_skb(skb); 15571da177e4SLinus Torvalds goto error; 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds offset += copy; 156115e36f5bSWillem de Bruijn length -= copy + transhdrlen; 15621da177e4SLinus Torvalds transhdrlen = 0; 15631da177e4SLinus Torvalds exthdrlen = 0; 1564299b0767SSteffen Klassert dst_exthdrlen = 0; 15651da177e4SLinus Torvalds 156652900d22SWillem de Bruijn /* Only the initial fragment is time stamped */ 156752900d22SWillem de Bruijn skb_shinfo(skb)->tx_flags = cork->tx_flags; 156852900d22SWillem de Bruijn cork->tx_flags = 0; 156952900d22SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 157052900d22SWillem de Bruijn tskey = 0; 157152900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 157252900d22SWillem de Bruijn 15730dec879fSJulian Anastasov if ((flags & MSG_CONFIRM) && !skb_prev) 15740dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 15750dec879fSJulian Anastasov 15761da177e4SLinus Torvalds /* 15771da177e4SLinus Torvalds * Put the packet on the pending queue 15781da177e4SLinus Torvalds */ 15791f4c6eb2SEric Dumazet if (!skb->destructor) { 15801f4c6eb2SEric Dumazet skb->destructor = sock_wfree; 15811f4c6eb2SEric Dumazet skb->sk = sk; 15821f4c6eb2SEric Dumazet wmem_alloc_delta += skb->truesize; 15831f4c6eb2SEric Dumazet } 15840bbe84a6SVlad Yasevich __skb_queue_tail(queue, skb); 15851da177e4SLinus Torvalds continue; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds if (copy > length) 15891da177e4SLinus Torvalds copy = length; 15901da177e4SLinus Torvalds 1591113f99c3SWillem de Bruijn if (!(rt->dst.dev->features&NETIF_F_SG) && 1592113f99c3SWillem de Bruijn skb_tailroom(skb) >= copy) { 15931da177e4SLinus Torvalds unsigned int off; 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds off = skb->len; 15961da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 15971da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 15981da177e4SLinus Torvalds __skb_trim(skb, off); 15991da177e4SLinus Torvalds err = -EFAULT; 16001da177e4SLinus Torvalds goto error; 16011da177e4SLinus Torvalds } 1602b5947e5dSWillem de Bruijn } else if (!uarg || !uarg->zerocopy) { 16031da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds err = -ENOMEM; 16065640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 16071da177e4SLinus Torvalds goto error; 16081da177e4SLinus Torvalds 16095640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 16105640f768SEric Dumazet pfrag->offset)) { 16111da177e4SLinus Torvalds err = -EMSGSIZE; 16125640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 16131da177e4SLinus Torvalds goto error; 16145640f768SEric Dumazet 16155640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 16165640f768SEric Dumazet pfrag->offset, 0); 16175640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 16185640f768SEric Dumazet get_page(pfrag->page); 16191da177e4SLinus Torvalds } 16205640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 16219e903e08SEric Dumazet if (getfrag(from, 16225640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 16235640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 16245640f768SEric Dumazet goto error_efault; 16255640f768SEric Dumazet 16265640f768SEric Dumazet pfrag->offset += copy; 16275640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 16281da177e4SLinus Torvalds skb->len += copy; 16291da177e4SLinus Torvalds skb->data_len += copy; 1630f945fa7aSHerbert Xu skb->truesize += copy; 16311f4c6eb2SEric Dumazet wmem_alloc_delta += copy; 1632b5947e5dSWillem de Bruijn } else { 1633b5947e5dSWillem de Bruijn err = skb_zerocopy_iter_dgram(skb, from, copy); 1634b5947e5dSWillem de Bruijn if (err < 0) 1635b5947e5dSWillem de Bruijn goto error; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds offset += copy; 16381da177e4SLinus Torvalds length -= copy; 16391da177e4SLinus Torvalds } 16405640f768SEric Dumazet 16419e8445a5SPaolo Abeni if (wmem_alloc_delta) 16421f4c6eb2SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 16431da177e4SLinus Torvalds return 0; 16445640f768SEric Dumazet 16455640f768SEric Dumazet error_efault: 16465640f768SEric Dumazet err = -EFAULT; 16471da177e4SLinus Torvalds error: 164897ef7b4cSWillem de Bruijn if (uarg) 164952900d22SWillem de Bruijn sock_zerocopy_put_abort(uarg, extra_uref); 1650bdc712b4SDavid S. Miller cork->length -= length; 16513bd653c8SDenis V. Lunev IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 16521f4c6eb2SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 16531da177e4SLinus Torvalds return err; 16541da177e4SLinus Torvalds } 16550bbe84a6SVlad Yasevich 16560bbe84a6SVlad Yasevich int ip6_append_data(struct sock *sk, 16570bbe84a6SVlad Yasevich int getfrag(void *from, char *to, int offset, int len, 16580bbe84a6SVlad Yasevich int odd, struct sk_buff *skb), 165926879da5SWei Wang void *from, int length, int transhdrlen, 166026879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 16615fdaa88dSWillem de Bruijn struct rt6_info *rt, unsigned int flags) 16620bbe84a6SVlad Yasevich { 16630bbe84a6SVlad Yasevich struct inet_sock *inet = inet_sk(sk); 16640bbe84a6SVlad Yasevich struct ipv6_pinfo *np = inet6_sk(sk); 16650bbe84a6SVlad Yasevich int exthdrlen; 16660bbe84a6SVlad Yasevich int err; 16670bbe84a6SVlad Yasevich 16680bbe84a6SVlad Yasevich if (flags&MSG_PROBE) 16690bbe84a6SVlad Yasevich return 0; 16700bbe84a6SVlad Yasevich if (skb_queue_empty(&sk->sk_write_queue)) { 16710bbe84a6SVlad Yasevich /* 16720bbe84a6SVlad Yasevich * setup for corking 16730bbe84a6SVlad Yasevich */ 167426879da5SWei Wang err = ip6_setup_cork(sk, &inet->cork, &np->cork, 16755fdaa88dSWillem de Bruijn ipc6, rt, fl6); 16760bbe84a6SVlad Yasevich if (err) 16770bbe84a6SVlad Yasevich return err; 16780bbe84a6SVlad Yasevich 167926879da5SWei Wang exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0); 16800bbe84a6SVlad Yasevich length += exthdrlen; 16810bbe84a6SVlad Yasevich transhdrlen += exthdrlen; 16820bbe84a6SVlad Yasevich } else { 16830bbe84a6SVlad Yasevich fl6 = &inet->cork.fl.u.ip6; 16840bbe84a6SVlad Yasevich transhdrlen = 0; 16850bbe84a6SVlad Yasevich } 16860bbe84a6SVlad Yasevich 16870bbe84a6SVlad Yasevich return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base, 16880bbe84a6SVlad Yasevich &np->cork, sk_page_frag(sk), getfrag, 16895fdaa88dSWillem de Bruijn from, length, transhdrlen, flags, ipc6); 16900bbe84a6SVlad Yasevich } 1691a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data); 16921da177e4SLinus Torvalds 1693366e41d9SVlad Yasevich static void ip6_cork_release(struct inet_cork_full *cork, 1694366e41d9SVlad Yasevich struct inet6_cork *v6_cork) 1695bf138862SPavel Emelyanov { 1696366e41d9SVlad Yasevich if (v6_cork->opt) { 1697366e41d9SVlad Yasevich kfree(v6_cork->opt->dst0opt); 1698366e41d9SVlad Yasevich kfree(v6_cork->opt->dst1opt); 1699366e41d9SVlad Yasevich kfree(v6_cork->opt->hopopt); 1700366e41d9SVlad Yasevich kfree(v6_cork->opt->srcrt); 1701366e41d9SVlad Yasevich kfree(v6_cork->opt); 1702366e41d9SVlad Yasevich v6_cork->opt = NULL; 17030178b695SHerbert Xu } 17040178b695SHerbert Xu 1705366e41d9SVlad Yasevich if (cork->base.dst) { 1706366e41d9SVlad Yasevich dst_release(cork->base.dst); 1707366e41d9SVlad Yasevich cork->base.dst = NULL; 1708366e41d9SVlad Yasevich cork->base.flags &= ~IPCORK_ALLFRAG; 1709bf138862SPavel Emelyanov } 1710366e41d9SVlad Yasevich memset(&cork->fl, 0, sizeof(cork->fl)); 1711bf138862SPavel Emelyanov } 1712bf138862SPavel Emelyanov 17136422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk, 17146422398cSVlad Yasevich struct sk_buff_head *queue, 17156422398cSVlad Yasevich struct inet_cork_full *cork, 17166422398cSVlad Yasevich struct inet6_cork *v6_cork) 17171da177e4SLinus Torvalds { 17181da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 17191da177e4SLinus Torvalds struct sk_buff **tail_skb; 17201da177e4SLinus Torvalds struct in6_addr final_dst_buf, *final_dst = &final_dst_buf; 17211da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 17223bd653c8SDenis V. Lunev struct net *net = sock_net(sk); 17231da177e4SLinus Torvalds struct ipv6hdr *hdr; 17246422398cSVlad Yasevich struct ipv6_txoptions *opt = v6_cork->opt; 17256422398cSVlad Yasevich struct rt6_info *rt = (struct rt6_info *)cork->base.dst; 17266422398cSVlad Yasevich struct flowi6 *fl6 = &cork->fl.u.ip6; 17274c9483b2SDavid S. Miller unsigned char proto = fl6->flowi6_proto; 17281da177e4SLinus Torvalds 17296422398cSVlad Yasevich skb = __skb_dequeue(queue); 173063159f29SIan Morris if (!skb) 17311da177e4SLinus Torvalds goto out; 17321da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1735d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1736bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 17376422398cSVlad Yasevich while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1738cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 17391da177e4SLinus Torvalds *tail_skb = tmp_skb; 17401da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 17411da177e4SLinus Torvalds skb->len += tmp_skb->len; 17421da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 17431da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 17441da177e4SLinus Torvalds tmp_skb->destructor = NULL; 17451da177e4SLinus Torvalds tmp_skb->sk = NULL; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 174828a89453SHerbert Xu /* Allow local fragmentation. */ 174960ff7467SWANG Cong skb->ignore_df = ip6_sk_ignore_df(sk); 175028a89453SHerbert Xu 17514e3fd7a0SAlexey Dobriyan *final_dst = fl6->daddr; 1752cfe1fc77SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_header_len(skb)); 17531da177e4SLinus Torvalds if (opt && opt->opt_flen) 17541da177e4SLinus Torvalds ipv6_push_frag_opts(skb, opt, &proto); 17551da177e4SLinus Torvalds if (opt && opt->opt_nflen) 1756613fa3caSDavid Lebrun ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr); 17571da177e4SLinus Torvalds 1758e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct ipv6hdr)); 1759e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 17600660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 17611da177e4SLinus Torvalds 17626422398cSVlad Yasevich ip6_flow_hdr(hdr, v6_cork->tclass, 1763cb1ce2efSTom Herbert ip6_make_flowlabel(net, skb, fl6->flowlabel, 1764513674b5SShaohua Li ip6_autoflowlabel(net, np), fl6)); 17656422398cSVlad Yasevich hdr->hop_limit = v6_cork->hop_limit; 17661da177e4SLinus Torvalds hdr->nexthdr = proto; 17674e3fd7a0SAlexey Dobriyan hdr->saddr = fl6->saddr; 17684e3fd7a0SAlexey Dobriyan hdr->daddr = *final_dst; 17691da177e4SLinus Torvalds 1770a2c2064fSPatrick McHardy skb->priority = sk->sk_priority; 1771c6af0c22SWillem de Bruijn skb->mark = cork->base.mark; 1772a2c2064fSPatrick McHardy 1773a818f75eSJesus Sanchez-Palencia skb->tstamp = cork->base.transmit_time; 1774a818f75eSJesus Sanchez-Palencia 1775d8d1f30bSChangli Gao skb_dst_set(skb, dst_clone(&rt->dst)); 1776edf391ffSNeil Horman IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 177714878f75SDavid L Stevens if (proto == IPPROTO_ICMPV6) { 1778adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 177914878f75SDavid L Stevens 178043a43b60SHannes Frederic Sowa ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type); 178143a43b60SHannes Frederic Sowa ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); 178214878f75SDavid L Stevens } 178314878f75SDavid L Stevens 17846422398cSVlad Yasevich ip6_cork_release(cork, v6_cork); 17856422398cSVlad Yasevich out: 17866422398cSVlad Yasevich return skb; 17876422398cSVlad Yasevich } 17886422398cSVlad Yasevich 17896422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb) 17906422398cSVlad Yasevich { 17916422398cSVlad Yasevich struct net *net = sock_net(skb->sk); 17926422398cSVlad Yasevich struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 17936422398cSVlad Yasevich int err; 17946422398cSVlad Yasevich 179533224b16SEric W. Biederman err = ip6_local_out(net, skb->sk, skb); 17961da177e4SLinus Torvalds if (err) { 17971da177e4SLinus Torvalds if (err > 0) 17986ce9e7b5SEric Dumazet err = net_xmit_errno(err); 17991da177e4SLinus Torvalds if (err) 18006422398cSVlad Yasevich IP6_INC_STATS(net, rt->rt6i_idev, 18016422398cSVlad Yasevich IPSTATS_MIB_OUTDISCARDS); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds return err; 18056422398cSVlad Yasevich } 18066422398cSVlad Yasevich 18076422398cSVlad Yasevich int ip6_push_pending_frames(struct sock *sk) 18086422398cSVlad Yasevich { 18096422398cSVlad Yasevich struct sk_buff *skb; 18106422398cSVlad Yasevich 18116422398cSVlad Yasevich skb = ip6_finish_skb(sk); 18126422398cSVlad Yasevich if (!skb) 18136422398cSVlad Yasevich return 0; 18146422398cSVlad Yasevich 18156422398cSVlad Yasevich return ip6_send_skb(skb); 18161da177e4SLinus Torvalds } 1817a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames); 18181da177e4SLinus Torvalds 18190bbe84a6SVlad Yasevich static void __ip6_flush_pending_frames(struct sock *sk, 18206422398cSVlad Yasevich struct sk_buff_head *queue, 18216422398cSVlad Yasevich struct inet_cork_full *cork, 18226422398cSVlad Yasevich struct inet6_cork *v6_cork) 18231da177e4SLinus Torvalds { 18241da177e4SLinus Torvalds struct sk_buff *skb; 18251da177e4SLinus Torvalds 18260bbe84a6SVlad Yasevich while ((skb = __skb_dequeue_tail(queue)) != NULL) { 1827adf30907SEric Dumazet if (skb_dst(skb)) 1828adf30907SEric Dumazet IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)), 1829a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTDISCARDS); 18301da177e4SLinus Torvalds kfree_skb(skb); 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds 18336422398cSVlad Yasevich ip6_cork_release(cork, v6_cork); 18341da177e4SLinus Torvalds } 18350bbe84a6SVlad Yasevich 18360bbe84a6SVlad Yasevich void ip6_flush_pending_frames(struct sock *sk) 18370bbe84a6SVlad Yasevich { 18386422398cSVlad Yasevich __ip6_flush_pending_frames(sk, &sk->sk_write_queue, 18396422398cSVlad Yasevich &inet_sk(sk)->cork, &inet6_sk(sk)->cork); 18400bbe84a6SVlad Yasevich } 1841a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames); 18426422398cSVlad Yasevich 18436422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk, 18446422398cSVlad Yasevich int getfrag(void *from, char *to, int offset, 18456422398cSVlad Yasevich int len, int odd, struct sk_buff *skb), 18466422398cSVlad Yasevich void *from, int length, int transhdrlen, 184726879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 18486422398cSVlad Yasevich struct rt6_info *rt, unsigned int flags, 18495fdaa88dSWillem de Bruijn struct inet_cork_full *cork) 18506422398cSVlad Yasevich { 18516422398cSVlad Yasevich struct inet6_cork v6_cork; 18526422398cSVlad Yasevich struct sk_buff_head queue; 185326879da5SWei Wang int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0); 18546422398cSVlad Yasevich int err; 18556422398cSVlad Yasevich 18566422398cSVlad Yasevich if (flags & MSG_PROBE) 18576422398cSVlad Yasevich return NULL; 18586422398cSVlad Yasevich 18596422398cSVlad Yasevich __skb_queue_head_init(&queue); 18606422398cSVlad Yasevich 18611cd7884dSWillem de Bruijn cork->base.flags = 0; 18621cd7884dSWillem de Bruijn cork->base.addr = 0; 18631cd7884dSWillem de Bruijn cork->base.opt = NULL; 18641cd7884dSWillem de Bruijn cork->base.dst = NULL; 18656422398cSVlad Yasevich v6_cork.opt = NULL; 18665fdaa88dSWillem de Bruijn err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt, fl6); 1867862c03eeSEric Dumazet if (err) { 18681cd7884dSWillem de Bruijn ip6_cork_release(cork, &v6_cork); 18696422398cSVlad Yasevich return ERR_PTR(err); 1870862c03eeSEric Dumazet } 187126879da5SWei Wang if (ipc6->dontfrag < 0) 187226879da5SWei Wang ipc6->dontfrag = inet6_sk(sk)->dontfrag; 18736422398cSVlad Yasevich 18741cd7884dSWillem de Bruijn err = __ip6_append_data(sk, fl6, &queue, &cork->base, &v6_cork, 18756422398cSVlad Yasevich ¤t->task_frag, getfrag, from, 18766422398cSVlad Yasevich length + exthdrlen, transhdrlen + exthdrlen, 18775fdaa88dSWillem de Bruijn flags, ipc6); 18786422398cSVlad Yasevich if (err) { 18791cd7884dSWillem de Bruijn __ip6_flush_pending_frames(sk, &queue, cork, &v6_cork); 18806422398cSVlad Yasevich return ERR_PTR(err); 18816422398cSVlad Yasevich } 18826422398cSVlad Yasevich 18831cd7884dSWillem de Bruijn return __ip6_make_skb(sk, &queue, cork, &v6_cork); 18846422398cSVlad Yasevich } 1885