12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IPv6 output functions 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Based on linux/net/ipv4/ip_output.c 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Changes: 121da177e4SLinus Torvalds * A.N.Kuznetsov : airthmetics in fragmentation. 131da177e4SLinus Torvalds * extension headers are implemented. 141da177e4SLinus Torvalds * route changes now work. 151da177e4SLinus Torvalds * ip6_forward does not confuse sniffers. 161da177e4SLinus Torvalds * etc. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * H. von Brand : Added missing #include <linux/string.h> 191da177e4SLinus Torvalds * Imran Patel : frag id should be in NBO 201da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI 211da177e4SLinus Torvalds * : add ip6_append_data and related functions 221da177e4SLinus Torvalds * for datagram xmit 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <linux/errno.h> 26ef76bc23SHerbert Xu #include <linux/kernel.h> 271da177e4SLinus Torvalds #include <linux/string.h> 281da177e4SLinus Torvalds #include <linux/socket.h> 291da177e4SLinus Torvalds #include <linux/net.h> 301da177e4SLinus Torvalds #include <linux/netdevice.h> 311da177e4SLinus Torvalds #include <linux/if_arp.h> 321da177e4SLinus Torvalds #include <linux/in6.h> 331da177e4SLinus Torvalds #include <linux/tcp.h> 341da177e4SLinus Torvalds #include <linux/route.h> 35b59f45d0SHerbert Xu #include <linux/module.h> 365a0e3ad6STejun Heo #include <linux/slab.h> 371da177e4SLinus Torvalds 3833b48679SDaniel Mack #include <linux/bpf-cgroup.h> 391da177e4SLinus Torvalds #include <linux/netfilter.h> 401da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <net/sock.h> 431da177e4SLinus Torvalds #include <net/snmp.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <net/ipv6.h> 461da177e4SLinus Torvalds #include <net/ndisc.h> 471da177e4SLinus Torvalds #include <net/protocol.h> 481da177e4SLinus Torvalds #include <net/ip6_route.h> 491da177e4SLinus Torvalds #include <net/addrconf.h> 501da177e4SLinus Torvalds #include <net/rawv6.h> 511da177e4SLinus Torvalds #include <net/icmp.h> 521da177e4SLinus Torvalds #include <net/xfrm.h> 531da177e4SLinus Torvalds #include <net/checksum.h> 547bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 55ca254490SDavid Ahern #include <net/l3mdev.h> 5614972cbdSRoopa Prabhu #include <net/lwtunnel.h> 57571912c6SMartin Varghese #include <net/ip_tunnels.h> 581da177e4SLinus Torvalds 597d8c6e39SEric W. Biederman static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) 601da177e4SLinus Torvalds { 61adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 621da177e4SLinus Torvalds struct net_device *dev = dst->dev; 63*e415ed3aSVasily Averin struct inet6_dev *idev = ip6_dst_idev(dst); 645796015fSVasily Averin unsigned int hh_len = LL_RESERVED_SPACE(dev); 65*e415ed3aSVasily Averin const struct in6_addr *daddr, *nexthop; 66*e415ed3aSVasily Averin struct ipv6hdr *hdr; 67f6b72b62SDavid S. Miller struct neighbour *neigh; 686fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 int ret; 691da177e4SLinus Torvalds 705796015fSVasily Averin /* Be paranoid, rather than too clever. */ 71*e415ed3aSVasily Averin if (unlikely(hh_len > skb_headroom(skb)) && dev->header_ops) { 72*e415ed3aSVasily Averin skb = skb_expand_head(skb, hh_len); 735796015fSVasily Averin if (!skb) { 74*e415ed3aSVasily Averin IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS); 755796015fSVasily Averin return -ENOMEM; 765796015fSVasily Averin } 775796015fSVasily Averin } 785796015fSVasily Averin 79*e415ed3aSVasily Averin hdr = ipv6_hdr(skb); 80*e415ed3aSVasily Averin daddr = &hdr->daddr; 81*e415ed3aSVasily Averin if (ipv6_addr_is_multicast(daddr)) { 827026b1ddSDavid Miller if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) && 838571ab47SYuval Mintz ((mroute6_is_socket(net, skb) && 84bd91b8bfSBenjamin Thery !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) || 85*e415ed3aSVasily Averin ipv6_chk_mcast_addr(dev, daddr, &hdr->saddr))) { 861da177e4SLinus Torvalds struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* Do not check for IFF_ALLMULTI; multicast routing 891da177e4SLinus Torvalds is not supported in any case. 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds if (newskb) 92b2e0b385SJan Engelhardt NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, 9329a26a56SEric W. Biederman net, sk, newskb, NULL, newskb->dev, 9495603e22SMichel Machado dev_loopback_xmit); 951da177e4SLinus Torvalds 96*e415ed3aSVasily Averin if (hdr->hop_limit == 0) { 9778126c41SEric W. Biederman IP6_INC_STATS(net, idev, 983bd653c8SDenis V. Lunev IPSTATS_MIB_OUTDISCARDS); 991da177e4SLinus Torvalds kfree_skb(skb); 1001da177e4SLinus Torvalds return 0; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 10478126c41SEric W. Biederman IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len); 105*e415ed3aSVasily Averin if (IPV6_ADDR_MC_SCOPE(daddr) <= IPV6_ADDR_SCOPE_NODELOCAL && 106dd408515SHannes Frederic Sowa !(dev->flags & IFF_LOOPBACK)) { 107dd408515SHannes Frederic Sowa kfree_skb(skb); 108dd408515SHannes Frederic Sowa return 0; 109dd408515SHannes Frederic Sowa } 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 11214972cbdSRoopa Prabhu if (lwtunnel_xmit_redirect(dst->lwtstate)) { 11314972cbdSRoopa Prabhu int res = lwtunnel_xmit(skb); 11414972cbdSRoopa Prabhu 11514972cbdSRoopa Prabhu if (res < 0 || res == LWTUNNEL_XMIT_DONE) 11614972cbdSRoopa Prabhu return res; 11714972cbdSRoopa Prabhu } 11814972cbdSRoopa Prabhu 1196fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 120*e415ed3aSVasily Averin nexthop = rt6_nexthop((struct rt6_info *)dst, daddr); 121*e415ed3aSVasily Averin neigh = __ipv6_neigh_lookup_noref(dev, nexthop); 1226fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(!neigh)) 123*e415ed3aSVasily Averin neigh = __neigh_create(&nd_tbl, nexthop, dev, false); 1246fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 if (!IS_ERR(neigh)) { 1254ff06203SJulian Anastasov sock_confirm_neigh(skb, neigh); 1260353f282SDavid Ahern ret = neigh_output(neigh, skb, false); 1276fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 1286fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 return ret; 1296fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 } 1306fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 13105e3aa09SDavid S. Miller 132*e415ed3aSVasily Averin IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTNOROUTES); 1339e508490SJan Engelhardt kfree_skb(skb); 1349e508490SJan Engelhardt return -EINVAL; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 137b210de4fSAya Levin static int 138b210de4fSAya Levin ip6_finish_output_gso_slowpath_drop(struct net *net, struct sock *sk, 139b210de4fSAya Levin struct sk_buff *skb, unsigned int mtu) 140b210de4fSAya Levin { 141b210de4fSAya Levin struct sk_buff *segs, *nskb; 142b210de4fSAya Levin netdev_features_t features; 143b210de4fSAya Levin int ret = 0; 144b210de4fSAya Levin 145b210de4fSAya Levin /* Please see corresponding comment in ip_finish_output_gso 146b210de4fSAya Levin * describing the cases where GSO segment length exceeds the 147b210de4fSAya Levin * egress MTU. 148b210de4fSAya Levin */ 149b210de4fSAya Levin features = netif_skb_features(skb); 150b210de4fSAya Levin segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 151b210de4fSAya Levin if (IS_ERR_OR_NULL(segs)) { 152b210de4fSAya Levin kfree_skb(skb); 153b210de4fSAya Levin return -ENOMEM; 154b210de4fSAya Levin } 155b210de4fSAya Levin 156b210de4fSAya Levin consume_skb(skb); 157b210de4fSAya Levin 158b210de4fSAya Levin skb_list_walk_safe(segs, segs, nskb) { 159b210de4fSAya Levin int err; 160b210de4fSAya Levin 161b210de4fSAya Levin skb_mark_not_on_list(segs); 162b210de4fSAya Levin err = ip6_fragment(net, sk, segs, ip6_finish_output2); 163b210de4fSAya Levin if (err && ret == 0) 164b210de4fSAya Levin ret = err; 165b210de4fSAya Levin } 166b210de4fSAya Levin 167b210de4fSAya Levin return ret; 168b210de4fSAya Levin } 169b210de4fSAya Levin 170956fe219Sbrakmo static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 1719e508490SJan Engelhardt { 172b210de4fSAya Levin unsigned int mtu; 173b210de4fSAya Levin 17409ee9dbaSTobias Brunner #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 17509ee9dbaSTobias Brunner /* Policy lookup after SNAT yielded a new policy */ 17609ee9dbaSTobias Brunner if (skb_dst(skb)->xfrm) { 17709ee9dbaSTobias Brunner IPCB(skb)->flags |= IPSKB_REROUTED; 17809ee9dbaSTobias Brunner return dst_output(net, sk, skb); 17909ee9dbaSTobias Brunner } 18009ee9dbaSTobias Brunner #endif 18109ee9dbaSTobias Brunner 182b210de4fSAya Levin mtu = ip6_skb_dst_mtu(skb); 183b210de4fSAya Levin if (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu)) 184b210de4fSAya Levin return ip6_finish_output_gso_slowpath_drop(net, sk, skb, mtu); 185b210de4fSAya Levin 186b210de4fSAya Levin if ((skb->len > mtu && !skb_is_gso(skb)) || 1879037c357SJiri Pirko dst_allfrag(skb_dst(skb)) || 1889037c357SJiri Pirko (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size)) 1897d8c6e39SEric W. Biederman return ip6_fragment(net, sk, skb, ip6_finish_output2); 1909e508490SJan Engelhardt else 1917d8c6e39SEric W. Biederman return ip6_finish_output2(net, sk, skb); 1929e508490SJan Engelhardt } 1939e508490SJan Engelhardt 194956fe219Sbrakmo static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 195956fe219Sbrakmo { 196956fe219Sbrakmo int ret; 197956fe219Sbrakmo 198956fe219Sbrakmo ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); 199956fe219Sbrakmo switch (ret) { 200956fe219Sbrakmo case NET_XMIT_SUCCESS: 201956fe219Sbrakmo return __ip6_finish_output(net, sk, skb); 202956fe219Sbrakmo case NET_XMIT_CN: 203956fe219Sbrakmo return __ip6_finish_output(net, sk, skb) ? : ret; 204956fe219Sbrakmo default: 205956fe219Sbrakmo kfree_skb(skb); 206956fe219Sbrakmo return ret; 207956fe219Sbrakmo } 208956fe219Sbrakmo } 209956fe219Sbrakmo 210ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb) 2111da177e4SLinus Torvalds { 21228f8bfd1SPhil Sutter struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev; 213adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 214be10de0aSEric W. Biederman 21597a7a37aSChenbo Feng skb->protocol = htons(ETH_P_IPV6); 21697a7a37aSChenbo Feng skb->dev = dev; 21797a7a37aSChenbo Feng 218778d80beSYOSHIFUJI Hideaki if (unlikely(idev->cnf.disable_ipv6)) { 21919a0644cSEric W. Biederman IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS); 220778d80beSYOSHIFUJI Hideaki kfree_skb(skb); 221778d80beSYOSHIFUJI Hideaki return 0; 222778d80beSYOSHIFUJI Hideaki } 223778d80beSYOSHIFUJI Hideaki 22429a26a56SEric W. Biederman return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, 22528f8bfd1SPhil Sutter net, sk, skb, indev, dev, 2269c6eb28aSJan Engelhardt ip6_finish_output, 2279c6eb28aSJan Engelhardt !(IP6CB(skb)->flags & IP6SKB_REROUTED)); 2281da177e4SLinus Torvalds } 2296585d7dcSBrian Vazquez EXPORT_SYMBOL(ip6_output); 2301da177e4SLinus Torvalds 231e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np) 232513674b5SShaohua Li { 233513674b5SShaohua Li if (!np->autoflowlabel_set) 234513674b5SShaohua Li return ip6_default_np_autolabel(net); 235513674b5SShaohua Li else 236513674b5SShaohua Li return np->autoflowlabel; 237513674b5SShaohua Li } 238513674b5SShaohua Li 2391da177e4SLinus Torvalds /* 240b5d43998SShan Wei * xmit an sk_buff (used by TCP, SCTP and DCCP) 2411c1e9d2bSEric Dumazet * Note : socket lock is not held for SYNACK packets, but might be modified 2421c1e9d2bSEric Dumazet * by calls to skb_set_owner_w() and ipv6_local_error(), 2431c1e9d2bSEric Dumazet * which are using proper atomic operations or spinlocks. 2441da177e4SLinus Torvalds */ 2451c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, 2464f6570d7SEric Dumazet __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority) 2471da177e4SLinus Torvalds { 2483bd653c8SDenis V. Lunev struct net *net = sock_net(sk); 2491c1e9d2bSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 2504c9483b2SDavid S. Miller struct in6_addr *first_hop = &fl6->daddr; 251adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 25266033f47SStefano Brivio unsigned int head_room; 2531da177e4SLinus Torvalds struct ipv6hdr *hdr; 2544c9483b2SDavid S. Miller u8 proto = fl6->flowi6_proto; 2551da177e4SLinus Torvalds int seg_len = skb->len; 256e651f03aSGerrit Renker int hlimit = -1; 2571da177e4SLinus Torvalds u32 mtu; 2581da177e4SLinus Torvalds 25966033f47SStefano Brivio head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev); 26066033f47SStefano Brivio if (opt) 26166033f47SStefano Brivio head_room += opt->opt_nflen + opt->opt_flen; 2621da177e4SLinus Torvalds 26366033f47SStefano Brivio if (unlikely(skb_headroom(skb) < head_room)) { 2641da177e4SLinus Torvalds struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room); 26563159f29SIan Morris if (!skb2) { 266adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 267a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTDISCARDS); 2681da177e4SLinus Torvalds kfree_skb(skb); 2691da177e4SLinus Torvalds return -ENOBUFS; 2701da177e4SLinus Torvalds } 271bbd6528dSEric Dumazet if (skb->sk) 272bbd6528dSEric Dumazet skb_set_owner_w(skb2, skb->sk); 273808db80aSEric Dumazet consume_skb(skb); 274a11d206dSYOSHIFUJI Hideaki skb = skb2; 2751da177e4SLinus Torvalds } 27666033f47SStefano Brivio 27766033f47SStefano Brivio if (opt) { 27866033f47SStefano Brivio seg_len += opt->opt_nflen + opt->opt_flen; 27966033f47SStefano Brivio 2801da177e4SLinus Torvalds if (opt->opt_flen) 2811da177e4SLinus Torvalds ipv6_push_frag_opts(skb, opt, &proto); 28266033f47SStefano Brivio 2831da177e4SLinus Torvalds if (opt->opt_nflen) 284613fa3caSDavid Lebrun ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop, 285613fa3caSDavid Lebrun &fl6->saddr); 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct ipv6hdr)); 289e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 2900660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* 2931da177e4SLinus Torvalds * Fill in the IPv6 header 2941da177e4SLinus Torvalds */ 295b903d324SEric Dumazet if (np) 2961da177e4SLinus Torvalds hlimit = np->hop_limit; 2971da177e4SLinus Torvalds if (hlimit < 0) 2986b75d090SYOSHIFUJI Hideaki hlimit = ip6_dst_hoplimit(dst); 2991da177e4SLinus Torvalds 300cb1ce2efSTom Herbert ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel, 301513674b5SShaohua Li ip6_autoflowlabel(net, np), fl6)); 30241a1f8eaSYOSHIFUJI Hideaki 3031da177e4SLinus Torvalds hdr->payload_len = htons(seg_len); 3041da177e4SLinus Torvalds hdr->nexthdr = proto; 3051da177e4SLinus Torvalds hdr->hop_limit = hlimit; 3061da177e4SLinus Torvalds 3074e3fd7a0SAlexey Dobriyan hdr->saddr = fl6->saddr; 3084e3fd7a0SAlexey Dobriyan hdr->daddr = *first_hop; 3091da177e4SLinus Torvalds 3109c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 3114f6570d7SEric Dumazet skb->priority = priority; 31292e55f41SPablo Neira skb->mark = mark; 313a2c2064fSPatrick McHardy 3141da177e4SLinus Torvalds mtu = dst_mtu(dst); 31560ff7467SWANG Cong if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) { 316adf30907SEric Dumazet IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)), 317edf391ffSNeil Horman IPSTATS_MIB_OUT, skb->len); 318a8e3e1a9SDavid Ahern 319a8e3e1a9SDavid Ahern /* if egress device is enslaved to an L3 master device pass the 320a8e3e1a9SDavid Ahern * skb to its handler for processing 321a8e3e1a9SDavid Ahern */ 322a8e3e1a9SDavid Ahern skb = l3mdev_ip6_out((struct sock *)sk, skb); 323a8e3e1a9SDavid Ahern if (unlikely(!skb)) 324a8e3e1a9SDavid Ahern return 0; 325a8e3e1a9SDavid Ahern 3261c1e9d2bSEric Dumazet /* hooks should never assume socket lock is held. 3271c1e9d2bSEric Dumazet * we promote our socket to non const 3281c1e9d2bSEric Dumazet */ 32929a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, 3301c1e9d2bSEric Dumazet net, (struct sock *)sk, skb, NULL, dst->dev, 33113206b6bSEric W. Biederman dst_output); 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds skb->dev = dst->dev; 3351c1e9d2bSEric Dumazet /* ipv6_local_error() does not require socket lock, 3361c1e9d2bSEric Dumazet * we promote our socket to non const 3371c1e9d2bSEric Dumazet */ 3381c1e9d2bSEric Dumazet ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu); 3391c1e9d2bSEric Dumazet 340adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS); 3411da177e4SLinus Torvalds kfree_skb(skb); 3421da177e4SLinus Torvalds return -EMSGSIZE; 3431da177e4SLinus Torvalds } 3447159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit); 3457159039aSYOSHIFUJI Hideaki 3461da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds struct ip6_ra_chain *ra; 3491da177e4SLinus Torvalds struct sock *last = NULL; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds read_lock(&ip6_ra_lock); 3521da177e4SLinus Torvalds for (ra = ip6_ra_chain; ra; ra = ra->next) { 3531da177e4SLinus Torvalds struct sock *sk = ra->sk; 3540bd1b59bSAndrew McDonald if (sk && ra->sel == sel && 3550bd1b59bSAndrew McDonald (!sk->sk_bound_dev_if || 3560bd1b59bSAndrew McDonald sk->sk_bound_dev_if == skb->dev->ifindex)) { 3579036b2feSFrancesco Ruggeri struct ipv6_pinfo *np = inet6_sk(sk); 3589036b2feSFrancesco Ruggeri 3599036b2feSFrancesco Ruggeri if (np && np->rtalert_isolate && 3609036b2feSFrancesco Ruggeri !net_eq(sock_net(sk), dev_net(skb->dev))) { 3619036b2feSFrancesco Ruggeri continue; 3629036b2feSFrancesco Ruggeri } 3631da177e4SLinus Torvalds if (last) { 3641da177e4SLinus Torvalds struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 3651da177e4SLinus Torvalds if (skb2) 3661da177e4SLinus Torvalds rawv6_rcv(last, skb2); 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds last = sk; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds if (last) { 3731da177e4SLinus Torvalds rawv6_rcv(last, skb); 3741da177e4SLinus Torvalds read_unlock(&ip6_ra_lock); 3751da177e4SLinus Torvalds return 1; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds read_unlock(&ip6_ra_lock); 3781da177e4SLinus Torvalds return 0; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 381e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb) 382e21e0b5fSVille Nuorvala { 3830660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 384e21e0b5fSVille Nuorvala u8 nexthdr = hdr->nexthdr; 38575f2811cSJesse Gross __be16 frag_off; 386e21e0b5fSVille Nuorvala int offset; 387e21e0b5fSVille Nuorvala 388e21e0b5fSVille Nuorvala if (ipv6_ext_hdr(nexthdr)) { 38975f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off); 390e21e0b5fSVille Nuorvala if (offset < 0) 391e21e0b5fSVille Nuorvala return 0; 392e21e0b5fSVille Nuorvala } else 393e21e0b5fSVille Nuorvala offset = sizeof(struct ipv6hdr); 394e21e0b5fSVille Nuorvala 395e21e0b5fSVille Nuorvala if (nexthdr == IPPROTO_ICMPV6) { 396e21e0b5fSVille Nuorvala struct icmp6hdr *icmp6; 397e21e0b5fSVille Nuorvala 398d56f90a7SArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_network_header(skb) + 399d56f90a7SArnaldo Carvalho de Melo offset + 1 - skb->data))) 400e21e0b5fSVille Nuorvala return 0; 401e21e0b5fSVille Nuorvala 402d56f90a7SArnaldo Carvalho de Melo icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset); 403e21e0b5fSVille Nuorvala 404e21e0b5fSVille Nuorvala switch (icmp6->icmp6_type) { 405e21e0b5fSVille Nuorvala case NDISC_ROUTER_SOLICITATION: 406e21e0b5fSVille Nuorvala case NDISC_ROUTER_ADVERTISEMENT: 407e21e0b5fSVille Nuorvala case NDISC_NEIGHBOUR_SOLICITATION: 408e21e0b5fSVille Nuorvala case NDISC_NEIGHBOUR_ADVERTISEMENT: 409e21e0b5fSVille Nuorvala case NDISC_REDIRECT: 410e21e0b5fSVille Nuorvala /* For reaction involving unicast neighbor discovery 411e21e0b5fSVille Nuorvala * message destined to the proxied address, pass it to 412e21e0b5fSVille Nuorvala * input function. 413e21e0b5fSVille Nuorvala */ 414e21e0b5fSVille Nuorvala return 1; 415e21e0b5fSVille Nuorvala default: 416e21e0b5fSVille Nuorvala break; 417e21e0b5fSVille Nuorvala } 418e21e0b5fSVille Nuorvala } 419e21e0b5fSVille Nuorvala 42074553b09SVille Nuorvala /* 42174553b09SVille Nuorvala * The proxying router can't forward traffic sent to a link-local 42274553b09SVille Nuorvala * address, so signal the sender and discard the packet. This 42374553b09SVille Nuorvala * behavior is clarified by the MIPv6 specification. 42474553b09SVille Nuorvala */ 42574553b09SVille Nuorvala if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) { 42674553b09SVille Nuorvala dst_link_failure(skb); 42774553b09SVille Nuorvala return -1; 42874553b09SVille Nuorvala } 42974553b09SVille Nuorvala 430e21e0b5fSVille Nuorvala return 0; 431e21e0b5fSVille Nuorvala } 432e21e0b5fSVille Nuorvala 4330c4b51f0SEric W. Biederman static inline int ip6_forward_finish(struct net *net, struct sock *sk, 4340c4b51f0SEric W. Biederman struct sk_buff *skb) 4351da177e4SLinus Torvalds { 43671a1c915SJeff Barnhill struct dst_entry *dst = skb_dst(skb); 43771a1c915SJeff Barnhill 43871a1c915SJeff Barnhill __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS); 43971a1c915SJeff Barnhill __IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len); 44071a1c915SJeff Barnhill 441f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV 442f839a6c9SIdo Schimmel if (skb->offload_l3_fwd_mark) { 443f839a6c9SIdo Schimmel consume_skb(skb); 444f839a6c9SIdo Schimmel return 0; 445f839a6c9SIdo Schimmel } 446f839a6c9SIdo Schimmel #endif 447f839a6c9SIdo Schimmel 4488203e2d8SEric Dumazet skb->tstamp = 0; 44913206b6bSEric W. Biederman return dst_output(net, sk, skb); 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 452fe6cc55fSFlorian Westphal static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu) 453fe6cc55fSFlorian Westphal { 454418a3156SFlorian Westphal if (skb->len <= mtu) 455fe6cc55fSFlorian Westphal return false; 456fe6cc55fSFlorian Westphal 45760ff7467SWANG Cong /* ipv6 conntrack defrag sets max_frag_size + ignore_df */ 458fe6cc55fSFlorian Westphal if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu) 459fe6cc55fSFlorian Westphal return true; 460fe6cc55fSFlorian Westphal 46160ff7467SWANG Cong if (skb->ignore_df) 462418a3156SFlorian Westphal return false; 463418a3156SFlorian Westphal 464779b7931SDaniel Axtens if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) 465fe6cc55fSFlorian Westphal return false; 466fe6cc55fSFlorian Westphal 467fe6cc55fSFlorian Westphal return true; 468fe6cc55fSFlorian Westphal } 469fe6cc55fSFlorian Westphal 4701da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb) 4711da177e4SLinus Torvalds { 472bdb7cc64SStephen Suryaputra struct inet6_dev *idev = __in6_dev_get_safely(skb->dev); 473adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 4740660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 4751da177e4SLinus Torvalds struct inet6_skb_parm *opt = IP6CB(skb); 476c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dst->dev); 47714f3ad6fSUlrich Weber u32 mtu; 4781da177e4SLinus Torvalds 47953b7997fSYOSHIFUJI Hideaki if (net->ipv6.devconf_all->forwarding == 0) 4801da177e4SLinus Torvalds goto error; 4811da177e4SLinus Torvalds 482090f1166SLi RongQing if (skb->pkt_type != PACKET_HOST) 483090f1166SLi RongQing goto drop; 484090f1166SLi RongQing 4859ef2e965SHannes Frederic Sowa if (unlikely(skb->sk)) 4869ef2e965SHannes Frederic Sowa goto drop; 4879ef2e965SHannes Frederic Sowa 4884497b076SBen Hutchings if (skb_warn_if_lro(skb)) 4894497b076SBen Hutchings goto drop; 4904497b076SBen Hutchings 491ccd27f05SNicolas Dichtel if (!net->ipv6.devconf_all->disable_policy && 492ccd27f05SNicolas Dichtel !idev->cnf.disable_policy && 493ccd27f05SNicolas Dichtel !xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) { 494bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 4951da177e4SLinus Torvalds goto drop; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 49835fc92a9SHerbert Xu skb_forward_csum(skb); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* 5011da177e4SLinus Torvalds * We DO NOT make any processing on 5021da177e4SLinus Torvalds * RA packets, pushing them to user level AS IS 5031da177e4SLinus Torvalds * without ane WARRANTY that application will be able 5041da177e4SLinus Torvalds * to interpret them. The reason is that we 5051da177e4SLinus Torvalds * cannot make anything clever here. 5061da177e4SLinus Torvalds * 5071da177e4SLinus Torvalds * We are not end-node, so that if packet contains 5081da177e4SLinus Torvalds * AH/ESP, we cannot make anything. 5091da177e4SLinus Torvalds * Defragmentation also would be mistake, RA packets 5101da177e4SLinus Torvalds * cannot be fragmented, because there is no warranty 5111da177e4SLinus Torvalds * that different fragments will go along one path. --ANK 5121da177e4SLinus Torvalds */ 513ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { 514ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 if (ip6_call_ra_chain(skb, ntohs(opt->ra))) 5151da177e4SLinus Torvalds return 0; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /* 5191da177e4SLinus Torvalds * check and decrement ttl 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds if (hdr->hop_limit <= 1) { 5223ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0); 523bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds kfree_skb(skb); 5261da177e4SLinus Torvalds return -ETIMEDOUT; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529fbea49e1SYOSHIFUJI Hideaki /* XXX: idev->cnf.proxy_ndp? */ 53053b7997fSYOSHIFUJI Hideaki if (net->ipv6.devconf_all->proxy_ndp && 5318a3edd80SDaniel Lezcano pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) { 53274553b09SVille Nuorvala int proxied = ip6_forward_proxy_check(skb); 53346c7655fSKangmin Park if (proxied > 0) { 53446c7655fSKangmin Park hdr->hop_limit--; 535e21e0b5fSVille Nuorvala return ip6_input(skb); 53646c7655fSKangmin Park } else if (proxied < 0) { 537bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 53874553b09SVille Nuorvala goto drop; 53974553b09SVille Nuorvala } 540e21e0b5fSVille Nuorvala } 541e21e0b5fSVille Nuorvala 5421da177e4SLinus Torvalds if (!xfrm6_route_forward(skb)) { 543bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS); 5441da177e4SLinus Torvalds goto drop; 5451da177e4SLinus Torvalds } 546adf30907SEric Dumazet dst = skb_dst(skb); 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* IPv6 specs say nothing about it, but it is clear that we cannot 5491da177e4SLinus Torvalds send redirects to source routed frames. 5501e5dc146SMasahide NAKAMURA We don't send redirects to frames decapsulated from IPsec. 5511da177e4SLinus Torvalds */ 5522f17becfSStephen Suryaputra if (IP6CB(skb)->iif == dst->dev->ifindex && 5532f17becfSStephen Suryaputra opt->srcrt == 0 && !skb_sec_path(skb)) { 5541da177e4SLinus Torvalds struct in6_addr *target = NULL; 555fbfe95a4SDavid S. Miller struct inet_peer *peer; 5561da177e4SLinus Torvalds struct rt6_info *rt; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* 5591da177e4SLinus Torvalds * incoming and outgoing devices are the same 5601da177e4SLinus Torvalds * send a redirect. 5611da177e4SLinus Torvalds */ 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 564c45a3dfbSDavid S. Miller if (rt->rt6i_flags & RTF_GATEWAY) 565c45a3dfbSDavid S. Miller target = &rt->rt6i_gateway; 5661da177e4SLinus Torvalds else 5671da177e4SLinus Torvalds target = &hdr->daddr; 5681da177e4SLinus Torvalds 569fd0273d7SMartin KaFai Lau peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1); 57092d86829SDavid S. Miller 5711da177e4SLinus Torvalds /* Limit redirects both by destination (here) 5721da177e4SLinus Torvalds and by source (inside ndisc_send_redirect) 5731da177e4SLinus Torvalds */ 574fbfe95a4SDavid S. Miller if (inet_peer_xrlim_allow(peer, 1*HZ)) 5754991969aSDavid S. Miller ndisc_send_redirect(skb, target); 5761d861aa4SDavid S. Miller if (peer) 5771d861aa4SDavid S. Miller inet_putpeer(peer); 5785bb1ab09SDavid L Stevens } else { 5795bb1ab09SDavid L Stevens int addrtype = ipv6_addr_type(&hdr->saddr); 5805bb1ab09SDavid L Stevens 5811da177e4SLinus Torvalds /* This check is security critical. */ 582f81b2e7dSYOSHIFUJI Hideaki if (addrtype == IPV6_ADDR_ANY || 583f81b2e7dSYOSHIFUJI Hideaki addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK)) 5841da177e4SLinus Torvalds goto error; 5855bb1ab09SDavid L Stevens if (addrtype & IPV6_ADDR_LINKLOCAL) { 5865bb1ab09SDavid L Stevens icmpv6_send(skb, ICMPV6_DEST_UNREACH, 5873ffe533cSAlexey Dobriyan ICMPV6_NOT_NEIGHBOUR, 0); 5885bb1ab09SDavid L Stevens goto error; 5895bb1ab09SDavid L Stevens } 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592427faee1SVadim Fedorenko mtu = ip6_dst_mtu_maybe_forward(dst, true); 59314f3ad6fSUlrich Weber if (mtu < IPV6_MIN_MTU) 59414f3ad6fSUlrich Weber mtu = IPV6_MIN_MTU; 59514f3ad6fSUlrich Weber 596fe6cc55fSFlorian Westphal if (ip6_pkt_too_big(skb, mtu)) { 5971da177e4SLinus Torvalds /* Again, force OUTPUT device used as source address */ 5981da177e4SLinus Torvalds skb->dev = dst->dev; 59914f3ad6fSUlrich Weber icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 600bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS); 6011d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(dst), 60215c77d8bSEric Dumazet IPSTATS_MIB_FRAGFAILS); 6031da177e4SLinus Torvalds kfree_skb(skb); 6041da177e4SLinus Torvalds return -EMSGSIZE; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds if (skb_cow(skb, dst->dev->hard_header_len)) { 6081d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(dst), 60915c77d8bSEric Dumazet IPSTATS_MIB_OUTDISCARDS); 6101da177e4SLinus Torvalds goto drop; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6130660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds /* Mangling hops number delayed to point after skb COW */ 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds hdr->hop_limit--; 6181da177e4SLinus Torvalds 61929a26a56SEric W. Biederman return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, 62029a26a56SEric W. Biederman net, NULL, skb, skb->dev, dst->dev, 6216e23ae2aSPatrick McHardy ip6_forward_finish); 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds error: 624bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS); 6251da177e4SLinus Torvalds drop: 6261da177e4SLinus Torvalds kfree_skb(skb); 6271da177e4SLinus Torvalds return -EINVAL; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds to->pkt_type = from->pkt_type; 6331da177e4SLinus Torvalds to->priority = from->priority; 6341da177e4SLinus Torvalds to->protocol = from->protocol; 635adf30907SEric Dumazet skb_dst_drop(to); 636adf30907SEric Dumazet skb_dst_set(to, dst_clone(skb_dst(from))); 6371da177e4SLinus Torvalds to->dev = from->dev; 63882e91ffeSThomas Graf to->mark = from->mark; 6391da177e4SLinus Torvalds 6403dd1c9a1SPaolo Abeni skb_copy_hash(to, from); 6413dd1c9a1SPaolo Abeni 6421da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 6431da177e4SLinus Torvalds to->tc_index = from->tc_index; 6441da177e4SLinus Torvalds #endif 645e7ac05f3SYasuyuki Kozakai nf_copy(to, from); 646df5042f4SFlorian Westphal skb_ext_copy(to, from); 647984bc16cSJames Morris skb_copy_secmark(to, from); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6500feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr, 6510feca619SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, 6520feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter) 6530feca619SPablo Neira Ayuso { 6540feca619SPablo Neira Ayuso unsigned int first_len; 6550feca619SPablo Neira Ayuso struct frag_hdr *fh; 6560feca619SPablo Neira Ayuso 6570feca619SPablo Neira Ayuso /* BUILD HEADER */ 6580feca619SPablo Neira Ayuso *prevhdr = NEXTHDR_FRAGMENT; 6590feca619SPablo Neira Ayuso iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC); 6600feca619SPablo Neira Ayuso if (!iter->tmp_hdr) 6610feca619SPablo Neira Ayuso return -ENOMEM; 6620feca619SPablo Neira Ayuso 663b7034146SEric Dumazet iter->frag = skb_shinfo(skb)->frag_list; 6640feca619SPablo Neira Ayuso skb_frag_list_init(skb); 6650feca619SPablo Neira Ayuso 6660feca619SPablo Neira Ayuso iter->offset = 0; 6670feca619SPablo Neira Ayuso iter->hlen = hlen; 6680feca619SPablo Neira Ayuso iter->frag_id = frag_id; 6690feca619SPablo Neira Ayuso iter->nexthdr = nexthdr; 6700feca619SPablo Neira Ayuso 6710feca619SPablo Neira Ayuso __skb_pull(skb, hlen); 6720feca619SPablo Neira Ayuso fh = __skb_push(skb, sizeof(struct frag_hdr)); 6730feca619SPablo Neira Ayuso __skb_push(skb, hlen); 6740feca619SPablo Neira Ayuso skb_reset_network_header(skb); 6750feca619SPablo Neira Ayuso memcpy(skb_network_header(skb), iter->tmp_hdr, hlen); 6760feca619SPablo Neira Ayuso 6770feca619SPablo Neira Ayuso fh->nexthdr = nexthdr; 6780feca619SPablo Neira Ayuso fh->reserved = 0; 6790feca619SPablo Neira Ayuso fh->frag_off = htons(IP6_MF); 6800feca619SPablo Neira Ayuso fh->identification = frag_id; 6810feca619SPablo Neira Ayuso 6820feca619SPablo Neira Ayuso first_len = skb_pagelen(skb); 6830feca619SPablo Neira Ayuso skb->data_len = first_len - skb_headlen(skb); 6840feca619SPablo Neira Ayuso skb->len = first_len; 6850feca619SPablo Neira Ayuso ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr)); 6860feca619SPablo Neira Ayuso 6870feca619SPablo Neira Ayuso return 0; 6880feca619SPablo Neira Ayuso } 6890feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_init); 6900feca619SPablo Neira Ayuso 6910feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb, 6920feca619SPablo Neira Ayuso struct ip6_fraglist_iter *iter) 6930feca619SPablo Neira Ayuso { 6940feca619SPablo Neira Ayuso struct sk_buff *frag = iter->frag; 6950feca619SPablo Neira Ayuso unsigned int hlen = iter->hlen; 6960feca619SPablo Neira Ayuso struct frag_hdr *fh; 6970feca619SPablo Neira Ayuso 6980feca619SPablo Neira Ayuso frag->ip_summed = CHECKSUM_NONE; 6990feca619SPablo Neira Ayuso skb_reset_transport_header(frag); 7000feca619SPablo Neira Ayuso fh = __skb_push(frag, sizeof(struct frag_hdr)); 7010feca619SPablo Neira Ayuso __skb_push(frag, hlen); 7020feca619SPablo Neira Ayuso skb_reset_network_header(frag); 7030feca619SPablo Neira Ayuso memcpy(skb_network_header(frag), iter->tmp_hdr, hlen); 7040feca619SPablo Neira Ayuso iter->offset += skb->len - hlen - sizeof(struct frag_hdr); 7050feca619SPablo Neira Ayuso fh->nexthdr = iter->nexthdr; 7060feca619SPablo Neira Ayuso fh->reserved = 0; 7070feca619SPablo Neira Ayuso fh->frag_off = htons(iter->offset); 7080feca619SPablo Neira Ayuso if (frag->next) 7090feca619SPablo Neira Ayuso fh->frag_off |= htons(IP6_MF); 7100feca619SPablo Neira Ayuso fh->identification = iter->frag_id; 7110feca619SPablo Neira Ayuso ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr)); 7120feca619SPablo Neira Ayuso ip6_copy_metadata(frag, skb); 7130feca619SPablo Neira Ayuso } 7140feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_prepare); 7150feca619SPablo Neira Ayuso 7168a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu, 7178a6a1f17SPablo Neira Ayuso unsigned short needed_tailroom, int hdr_room, u8 *prevhdr, 7188a6a1f17SPablo Neira Ayuso u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state) 7198a6a1f17SPablo Neira Ayuso { 7208a6a1f17SPablo Neira Ayuso state->prevhdr = prevhdr; 7218a6a1f17SPablo Neira Ayuso state->nexthdr = nexthdr; 7228a6a1f17SPablo Neira Ayuso state->frag_id = frag_id; 7238a6a1f17SPablo Neira Ayuso 7248a6a1f17SPablo Neira Ayuso state->hlen = hlen; 7258a6a1f17SPablo Neira Ayuso state->mtu = mtu; 7268a6a1f17SPablo Neira Ayuso 7278a6a1f17SPablo Neira Ayuso state->left = skb->len - hlen; /* Space per frame */ 7288a6a1f17SPablo Neira Ayuso state->ptr = hlen; /* Where to start from */ 7298a6a1f17SPablo Neira Ayuso 7308a6a1f17SPablo Neira Ayuso state->hroom = hdr_room; 7318a6a1f17SPablo Neira Ayuso state->troom = needed_tailroom; 7328a6a1f17SPablo Neira Ayuso 7338a6a1f17SPablo Neira Ayuso state->offset = 0; 7348a6a1f17SPablo Neira Ayuso } 7358a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_init); 7368a6a1f17SPablo Neira Ayuso 7378a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state) 7388a6a1f17SPablo Neira Ayuso { 7398a6a1f17SPablo Neira Ayuso u8 *prevhdr = state->prevhdr, *fragnexthdr_offset; 7408a6a1f17SPablo Neira Ayuso struct sk_buff *frag; 7418a6a1f17SPablo Neira Ayuso struct frag_hdr *fh; 7428a6a1f17SPablo Neira Ayuso unsigned int len; 7438a6a1f17SPablo Neira Ayuso 7448a6a1f17SPablo Neira Ayuso len = state->left; 7458a6a1f17SPablo Neira Ayuso /* IF: it doesn't fit, use 'mtu' - the data space left */ 7468a6a1f17SPablo Neira Ayuso if (len > state->mtu) 7478a6a1f17SPablo Neira Ayuso len = state->mtu; 7488a6a1f17SPablo Neira Ayuso /* IF: we are not sending up to and including the packet end 7498a6a1f17SPablo Neira Ayuso then align the next start on an eight byte boundary */ 7508a6a1f17SPablo Neira Ayuso if (len < state->left) 7518a6a1f17SPablo Neira Ayuso len &= ~7; 7528a6a1f17SPablo Neira Ayuso 7538a6a1f17SPablo Neira Ayuso /* Allocate buffer */ 7548a6a1f17SPablo Neira Ayuso frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) + 7558a6a1f17SPablo Neira Ayuso state->hroom + state->troom, GFP_ATOMIC); 7568a6a1f17SPablo Neira Ayuso if (!frag) 7578a6a1f17SPablo Neira Ayuso return ERR_PTR(-ENOMEM); 7588a6a1f17SPablo Neira Ayuso 7598a6a1f17SPablo Neira Ayuso /* 7608a6a1f17SPablo Neira Ayuso * Set up data on packet 7618a6a1f17SPablo Neira Ayuso */ 7628a6a1f17SPablo Neira Ayuso 7638a6a1f17SPablo Neira Ayuso ip6_copy_metadata(frag, skb); 7648a6a1f17SPablo Neira Ayuso skb_reserve(frag, state->hroom); 7658a6a1f17SPablo Neira Ayuso skb_put(frag, len + state->hlen + sizeof(struct frag_hdr)); 7668a6a1f17SPablo Neira Ayuso skb_reset_network_header(frag); 7678a6a1f17SPablo Neira Ayuso fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen); 7688a6a1f17SPablo Neira Ayuso frag->transport_header = (frag->network_header + state->hlen + 7698a6a1f17SPablo Neira Ayuso sizeof(struct frag_hdr)); 7708a6a1f17SPablo Neira Ayuso 7718a6a1f17SPablo Neira Ayuso /* 7728a6a1f17SPablo Neira Ayuso * Charge the memory for the fragment to any owner 7738a6a1f17SPablo Neira Ayuso * it might possess 7748a6a1f17SPablo Neira Ayuso */ 7758a6a1f17SPablo Neira Ayuso if (skb->sk) 7768a6a1f17SPablo Neira Ayuso skb_set_owner_w(frag, skb->sk); 7778a6a1f17SPablo Neira Ayuso 7788a6a1f17SPablo Neira Ayuso /* 7798a6a1f17SPablo Neira Ayuso * Copy the packet header into the new buffer. 7808a6a1f17SPablo Neira Ayuso */ 7818a6a1f17SPablo Neira Ayuso skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen); 7828a6a1f17SPablo Neira Ayuso 7838a6a1f17SPablo Neira Ayuso fragnexthdr_offset = skb_network_header(frag); 7848a6a1f17SPablo Neira Ayuso fragnexthdr_offset += prevhdr - skb_network_header(skb); 7858a6a1f17SPablo Neira Ayuso *fragnexthdr_offset = NEXTHDR_FRAGMENT; 7868a6a1f17SPablo Neira Ayuso 7878a6a1f17SPablo Neira Ayuso /* 7888a6a1f17SPablo Neira Ayuso * Build fragment header. 7898a6a1f17SPablo Neira Ayuso */ 7908a6a1f17SPablo Neira Ayuso fh->nexthdr = state->nexthdr; 7918a6a1f17SPablo Neira Ayuso fh->reserved = 0; 7928a6a1f17SPablo Neira Ayuso fh->identification = state->frag_id; 7938a6a1f17SPablo Neira Ayuso 7948a6a1f17SPablo Neira Ayuso /* 7958a6a1f17SPablo Neira Ayuso * Copy a block of the IP datagram. 7968a6a1f17SPablo Neira Ayuso */ 7978a6a1f17SPablo Neira Ayuso BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag), 7988a6a1f17SPablo Neira Ayuso len)); 7998a6a1f17SPablo Neira Ayuso state->left -= len; 8008a6a1f17SPablo Neira Ayuso 8018a6a1f17SPablo Neira Ayuso fh->frag_off = htons(state->offset); 8028a6a1f17SPablo Neira Ayuso if (state->left > 0) 8038a6a1f17SPablo Neira Ayuso fh->frag_off |= htons(IP6_MF); 8048a6a1f17SPablo Neira Ayuso ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr)); 8058a6a1f17SPablo Neira Ayuso 8068a6a1f17SPablo Neira Ayuso state->ptr += len; 8078a6a1f17SPablo Neira Ayuso state->offset += len; 8088a6a1f17SPablo Neira Ayuso 8098a6a1f17SPablo Neira Ayuso return frag; 8108a6a1f17SPablo Neira Ayuso } 8118a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_next); 8128a6a1f17SPablo Neira Ayuso 8137d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, 8147d8c6e39SEric W. Biederman int (*output)(struct net *, struct sock *, struct sk_buff *)) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds struct sk_buff *frag; 817adf30907SEric Dumazet struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 818f60e5990Shannes@stressinduktion.org struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ? 819f60e5990Shannes@stressinduktion.org inet6_sk(skb->sk) : NULL; 8208a6a1f17SPablo Neira Ayuso struct ip6_frag_state state; 8218a6a1f17SPablo Neira Ayuso unsigned int mtu, hlen, nexthdr_offset; 8229669fffcSEric Dumazet ktime_t tstamp = skb->tstamp; 8238a6a1f17SPablo Neira Ayuso int hroom, err = 0; 824286c2349SMartin KaFai Lau __be32 frag_id; 8251da177e4SLinus Torvalds u8 *prevhdr, nexthdr = 0; 8261da177e4SLinus Torvalds 8277dd7eb95SDavid S. Miller err = ip6_find_1stfragopt(skb, &prevhdr); 8287dd7eb95SDavid S. Miller if (err < 0) 8292423496aSCraig Gallek goto fail; 8307dd7eb95SDavid S. Miller hlen = err; 8311da177e4SLinus Torvalds nexthdr = *prevhdr; 832ef0efcd3SJunwei Hu nexthdr_offset = prevhdr - skb_network_header(skb); 8331da177e4SLinus Torvalds 834628a5c56SJohn Heffner mtu = ip6_skb_dst_mtu(skb); 835b881ef76SJohn Heffner 836b881ef76SJohn Heffner /* We must not fragment if the socket is set to force MTU discovery 83714f3ad6fSUlrich Weber * or if the skb it not generated by a local socket. 838b881ef76SJohn Heffner */ 839485fca66SFlorian Westphal if (unlikely(!skb->ignore_df && skb->len > mtu)) 840485fca66SFlorian Westphal goto fail_toobig; 841a34a101eSEric Dumazet 842485fca66SFlorian Westphal if (IP6CB(skb)->frag_max_size) { 843485fca66SFlorian Westphal if (IP6CB(skb)->frag_max_size > mtu) 844485fca66SFlorian Westphal goto fail_toobig; 845485fca66SFlorian Westphal 846485fca66SFlorian Westphal /* don't send fragments larger than what we received */ 847485fca66SFlorian Westphal mtu = IP6CB(skb)->frag_max_size; 848485fca66SFlorian Westphal if (mtu < IPV6_MIN_MTU) 849485fca66SFlorian Westphal mtu = IPV6_MIN_MTU; 850b881ef76SJohn Heffner } 851b881ef76SJohn Heffner 852d91675f9SYOSHIFUJI Hideaki if (np && np->frag_size < mtu) { 853d91675f9SYOSHIFUJI Hideaki if (np->frag_size) 854d91675f9SYOSHIFUJI Hideaki mtu = np->frag_size; 855d91675f9SYOSHIFUJI Hideaki } 85689bc7848SHannes Frederic Sowa if (mtu < hlen + sizeof(struct frag_hdr) + 8) 857b72a2b01SHannes Frederic Sowa goto fail_toobig; 8581e0d69a9SHannes Frederic Sowa mtu -= hlen + sizeof(struct frag_hdr); 8591da177e4SLinus Torvalds 860fd0273d7SMartin KaFai Lau frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr, 861fd0273d7SMartin KaFai Lau &ipv6_hdr(skb)->saddr); 862286c2349SMartin KaFai Lau 863405c92f7SHannes Frederic Sowa if (skb->ip_summed == CHECKSUM_PARTIAL && 864405c92f7SHannes Frederic Sowa (err = skb_checksum_help(skb))) 865405c92f7SHannes Frederic Sowa goto fail; 866405c92f7SHannes Frederic Sowa 867ef0efcd3SJunwei Hu prevhdr = skb_network_header(skb) + nexthdr_offset; 8681d325d21SFlorian Westphal hroom = LL_RESERVED_SPACE(rt->dst.dev); 86921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 870c72d8cdaSAlexey Dobriyan unsigned int first_len = skb_pagelen(skb); 8710feca619SPablo Neira Ayuso struct ip6_fraglist_iter iter; 8723d13008eSEric Dumazet struct sk_buff *frag2; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds if (first_len - hlen > mtu || 8751da177e4SLinus Torvalds ((first_len - hlen) & 7) || 8761d325d21SFlorian Westphal skb_cloned(skb) || 8771d325d21SFlorian Westphal skb_headroom(skb) < (hroom + sizeof(struct frag_hdr))) 8781da177e4SLinus Torvalds goto slow_path; 8791da177e4SLinus Torvalds 8804d9092bbSDavid S. Miller skb_walk_frags(skb, frag) { 8811da177e4SLinus Torvalds /* Correct geometry. */ 8821da177e4SLinus Torvalds if (frag->len > mtu || 8831da177e4SLinus Torvalds ((frag->len & 7) && frag->next) || 8841d325d21SFlorian Westphal skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr))) 8853d13008eSEric Dumazet goto slow_path_clean; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds /* Partially cloned skb? */ 8881da177e4SLinus Torvalds if (skb_shared(frag)) 8893d13008eSEric Dumazet goto slow_path_clean; 8902fdba6b0SHerbert Xu 8912fdba6b0SHerbert Xu BUG_ON(frag->sk); 8922fdba6b0SHerbert Xu if (skb->sk) { 8932fdba6b0SHerbert Xu frag->sk = skb->sk; 8942fdba6b0SHerbert Xu frag->destructor = sock_wfree; 8952fdba6b0SHerbert Xu } 8963d13008eSEric Dumazet skb->truesize -= frag->truesize; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8990feca619SPablo Neira Ayuso err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id, 9000feca619SPablo Neira Ayuso &iter); 9010feca619SPablo Neira Ayuso if (err < 0) 9021d325d21SFlorian Westphal goto fail; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds for (;;) { 9051da177e4SLinus Torvalds /* Prepare header of the next frame, 9061da177e4SLinus Torvalds * before previous one went down. */ 9070feca619SPablo Neira Ayuso if (iter.frag) 9080feca619SPablo Neira Ayuso ip6_fraglist_prepare(skb, &iter); 9091da177e4SLinus Torvalds 9109669fffcSEric Dumazet skb->tstamp = tstamp; 9117d8c6e39SEric W. Biederman err = output(net, sk, skb); 912dafee490SWei Dong if (!err) 913d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 9143bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGCREATES); 915dafee490SWei Dong 9160feca619SPablo Neira Ayuso if (err || !iter.frag) 9171da177e4SLinus Torvalds break; 9181da177e4SLinus Torvalds 9190feca619SPablo Neira Ayuso skb = ip6_fraglist_next(&iter); 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 9220feca619SPablo Neira Ayuso kfree(iter.tmp_hdr); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if (err == 0) { 925d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 9263bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGOKS); 9271da177e4SLinus Torvalds return 0; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 930b7034146SEric Dumazet kfree_skb_list(iter.frag); 9311da177e4SLinus Torvalds 932d8d1f30bSChangli Gao IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 9333bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGFAILS); 9341da177e4SLinus Torvalds return err; 9353d13008eSEric Dumazet 9363d13008eSEric Dumazet slow_path_clean: 9373d13008eSEric Dumazet skb_walk_frags(skb, frag2) { 9383d13008eSEric Dumazet if (frag2 == frag) 9393d13008eSEric Dumazet break; 9403d13008eSEric Dumazet frag2->sk = NULL; 9413d13008eSEric Dumazet frag2->destructor = NULL; 9423d13008eSEric Dumazet skb->truesize += frag2->truesize; 9433d13008eSEric Dumazet } 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds slow_path: 9471da177e4SLinus Torvalds /* 9481da177e4SLinus Torvalds * Fragment the datagram. 9491da177e4SLinus Torvalds */ 9501da177e4SLinus Torvalds 9518a6a1f17SPablo Neira Ayuso ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom, 9528a6a1f17SPablo Neira Ayuso LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id, 9538a6a1f17SPablo Neira Ayuso &state); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* 9561da177e4SLinus Torvalds * Keep copying data until we run out. 9571da177e4SLinus Torvalds */ 95879e49503SFlorian Westphal 9598a6a1f17SPablo Neira Ayuso while (state.left > 0) { 9608a6a1f17SPablo Neira Ayuso frag = ip6_frag_next(skb, &state); 9618a6a1f17SPablo Neira Ayuso if (IS_ERR(frag)) { 9628a6a1f17SPablo Neira Ayuso err = PTR_ERR(frag); 9631da177e4SLinus Torvalds goto fail; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds /* 9671da177e4SLinus Torvalds * Put this fragment into the sending queue. 9681da177e4SLinus Torvalds */ 9699669fffcSEric Dumazet frag->tstamp = tstamp; 9707d8c6e39SEric W. Biederman err = output(net, sk, frag); 9711da177e4SLinus Torvalds if (err) 9721da177e4SLinus Torvalds goto fail; 973dafee490SWei Dong 974adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 9753bd653c8SDenis V. Lunev IPSTATS_MIB_FRAGCREATES); 9761da177e4SLinus Torvalds } 977adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 978a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_FRAGOKS); 979808db80aSEric Dumazet consume_skb(skb); 9801da177e4SLinus Torvalds return err; 9811da177e4SLinus Torvalds 982485fca66SFlorian Westphal fail_toobig: 983485fca66SFlorian Westphal if (skb->sk && dst_allfrag(skb_dst(skb))) 984485fca66SFlorian Westphal sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK); 985485fca66SFlorian Westphal 986485fca66SFlorian Westphal icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 987485fca66SFlorian Westphal err = -EMSGSIZE; 988485fca66SFlorian Westphal 9891da177e4SLinus Torvalds fail: 990adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 991a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_FRAGFAILS); 9921da177e4SLinus Torvalds kfree_skb(skb); 9931da177e4SLinus Torvalds return err; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 996b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key, 997b71d1d42SEric Dumazet const struct in6_addr *fl_addr, 998b71d1d42SEric Dumazet const struct in6_addr *addr_cache) 999cf6b1982SYOSHIFUJI Hideaki { 1000a02cec21SEric Dumazet return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) && 100163159f29SIan Morris (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache)); 1002cf6b1982SYOSHIFUJI Hideaki } 1003cf6b1982SYOSHIFUJI Hideaki 1004497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk, 1005497c615aSHerbert Xu struct dst_entry *dst, 1006b71d1d42SEric Dumazet const struct flowi6 *fl6) 10071da177e4SLinus Torvalds { 10081da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 1009a963a37dSEric Dumazet struct rt6_info *rt; 10101da177e4SLinus Torvalds 1011497c615aSHerbert Xu if (!dst) 1012497c615aSHerbert Xu goto out; 10131da177e4SLinus Torvalds 1014a963a37dSEric Dumazet if (dst->ops->family != AF_INET6) { 1015a963a37dSEric Dumazet dst_release(dst); 1016a963a37dSEric Dumazet return NULL; 1017a963a37dSEric Dumazet } 1018a963a37dSEric Dumazet 1019a963a37dSEric Dumazet rt = (struct rt6_info *)dst; 10201da177e4SLinus Torvalds /* Yes, checking route validity in not connected 1021d76e60a5SDavid S. Miller * case is not very simple. Take into account, 1022d76e60a5SDavid S. Miller * that we do not support routing by source, TOS, 1023d76e60a5SDavid S. Miller * and MSG_DONTROUTE --ANK (980726) 1024d76e60a5SDavid S. Miller * 1025cf6b1982SYOSHIFUJI Hideaki * 1. ip6_rt_check(): If route was host route, 1026cf6b1982SYOSHIFUJI Hideaki * check that cached destination is current. 1027d76e60a5SDavid S. Miller * If it is network route, we still may 1028d76e60a5SDavid S. Miller * check its validity using saved pointer 1029d76e60a5SDavid S. Miller * to the last used address: daddr_cache. 1030d76e60a5SDavid S. Miller * We do not want to save whole address now, 1031d76e60a5SDavid S. Miller * (because main consumer of this service 1032d76e60a5SDavid S. Miller * is tcp, which has not this problem), 1033d76e60a5SDavid S. Miller * so that the last trick works only on connected 1034d76e60a5SDavid S. Miller * sockets. 1035d76e60a5SDavid S. Miller * 2. oif also should be the same. 10361da177e4SLinus Torvalds */ 10374c9483b2SDavid S. Miller if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) || 10388e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES 10394c9483b2SDavid S. Miller ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) || 10408e1ef0a9SYOSHIFUJI Hideaki #endif 1041ca254490SDavid Ahern (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) && 1042ca254490SDavid Ahern (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) { 1043497c615aSHerbert Xu dst_release(dst); 1044497c615aSHerbert Xu dst = NULL; 10451da177e4SLinus Torvalds } 1046497c615aSHerbert Xu 1047497c615aSHerbert Xu out: 1048497c615aSHerbert Xu return dst; 10491da177e4SLinus Torvalds } 1050497c615aSHerbert Xu 10513aef934fSEric Dumazet static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk, 10524c9483b2SDavid S. Miller struct dst_entry **dst, struct flowi6 *fl6) 1053497c615aSHerbert Xu { 105469cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 105569cce1d1SDavid S. Miller struct neighbour *n; 105697cac082SDavid S. Miller struct rt6_info *rt; 105769cce1d1SDavid S. Miller #endif 105869cce1d1SDavid S. Miller int err; 10596f21c96aSPaolo Abeni int flags = 0; 10601da177e4SLinus Torvalds 1061e16e888bSMarkus Stenberg /* The correct way to handle this would be to do 1062e16e888bSMarkus Stenberg * ip6_route_get_saddr, and then ip6_route_output; however, 1063e16e888bSMarkus Stenberg * the route-specific preferred source forces the 1064e16e888bSMarkus Stenberg * ip6_route_output call _before_ ip6_route_get_saddr. 1065e16e888bSMarkus Stenberg * 1066e16e888bSMarkus Stenberg * In source specific routing (no src=any default route), 1067e16e888bSMarkus Stenberg * ip6_route_output will fail given src=any saddr, though, so 1068e16e888bSMarkus Stenberg * that's why we try it again later. 1069e16e888bSMarkus Stenberg */ 1070c305b9e6Szhang kai if (ipv6_addr_any(&fl6->saddr)) { 1071a68886a6SDavid Ahern struct fib6_info *from; 1072e16e888bSMarkus Stenberg struct rt6_info *rt; 1073e16e888bSMarkus Stenberg 1074e16e888bSMarkus Stenberg *dst = ip6_route_output(net, sk, fl6); 1075e16e888bSMarkus Stenberg rt = (*dst)->error ? NULL : (struct rt6_info *)*dst; 1076a68886a6SDavid Ahern 1077a68886a6SDavid Ahern rcu_read_lock(); 1078a68886a6SDavid Ahern from = rt ? rcu_dereference(rt->from) : NULL; 1079a68886a6SDavid Ahern err = ip6_route_get_saddr(net, from, &fl6->daddr, 1080e16e888bSMarkus Stenberg sk ? inet6_sk(sk)->srcprefs : 0, 1081e16e888bSMarkus Stenberg &fl6->saddr); 1082a68886a6SDavid Ahern rcu_read_unlock(); 1083a68886a6SDavid Ahern 1084e16e888bSMarkus Stenberg if (err) 1085e16e888bSMarkus Stenberg goto out_err_release; 1086e16e888bSMarkus Stenberg 1087e16e888bSMarkus Stenberg /* If we had an erroneous initial result, pretend it 1088e16e888bSMarkus Stenberg * never existed and let the SA-enabled version take 1089e16e888bSMarkus Stenberg * over. 1090e16e888bSMarkus Stenberg */ 1091c305b9e6Szhang kai if ((*dst)->error) { 1092e16e888bSMarkus Stenberg dst_release(*dst); 1093e16e888bSMarkus Stenberg *dst = NULL; 1094e16e888bSMarkus Stenberg } 10956f21c96aSPaolo Abeni 10966f21c96aSPaolo Abeni if (fl6->flowi6_oif) 10976f21c96aSPaolo Abeni flags |= RT6_LOOKUP_F_IFACE; 1098e16e888bSMarkus Stenberg } 1099e16e888bSMarkus Stenberg 110063159f29SIan Morris if (!*dst) 11016f21c96aSPaolo Abeni *dst = ip6_route_output_flags(net, sk, fl6, flags); 11021da177e4SLinus Torvalds 1103e5d08d71SIan Morris err = (*dst)->error; 1104e5d08d71SIan Morris if (err) 11051da177e4SLinus Torvalds goto out_err_release; 11061da177e4SLinus Torvalds 110795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 110895c385b4SNeil Horman /* 110995c385b4SNeil Horman * Here if the dst entry we've looked up 111095c385b4SNeil Horman * has a neighbour entry that is in the INCOMPLETE 111195c385b4SNeil Horman * state and the src address from the flow is 111295c385b4SNeil Horman * marked as OPTIMISTIC, we release the found 111395c385b4SNeil Horman * dst entry and replace it instead with the 111495c385b4SNeil Horman * dst entry of the nexthop router 111595c385b4SNeil Horman */ 1116c56bf6feSEric Dumazet rt = (struct rt6_info *) *dst; 1117707be1ffSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 11182647a9b0SMartin KaFai Lau n = __ipv6_neigh_lookup_noref(rt->dst.dev, 11192647a9b0SMartin KaFai Lau rt6_nexthop(rt, &fl6->daddr)); 1120707be1ffSYOSHIFUJI Hideaki / 吉藤英明 err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0; 1121707be1ffSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 1122707be1ffSYOSHIFUJI Hideaki / 吉藤英明 1123707be1ffSYOSHIFUJI Hideaki / 吉藤英明 if (err) { 112495c385b4SNeil Horman struct inet6_ifaddr *ifp; 11254c9483b2SDavid S. Miller struct flowi6 fl_gw6; 112695c385b4SNeil Horman int redirect; 112795c385b4SNeil Horman 11284c9483b2SDavid S. Miller ifp = ipv6_get_ifaddr(net, &fl6->saddr, 11291cab3da6SDaniel Lezcano (*dst)->dev, 1); 113095c385b4SNeil Horman 113195c385b4SNeil Horman redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC); 113295c385b4SNeil Horman if (ifp) 113395c385b4SNeil Horman in6_ifa_put(ifp); 113495c385b4SNeil Horman 113595c385b4SNeil Horman if (redirect) { 113695c385b4SNeil Horman /* 113795c385b4SNeil Horman * We need to get the dst entry for the 113895c385b4SNeil Horman * default router instead 113995c385b4SNeil Horman */ 114095c385b4SNeil Horman dst_release(*dst); 11414c9483b2SDavid S. Miller memcpy(&fl_gw6, fl6, sizeof(struct flowi6)); 11424c9483b2SDavid S. Miller memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr)); 11434c9483b2SDavid S. Miller *dst = ip6_route_output(net, sk, &fl_gw6); 1144e5d08d71SIan Morris err = (*dst)->error; 1145e5d08d71SIan Morris if (err) 114695c385b4SNeil Horman goto out_err_release; 114795c385b4SNeil Horman } 114895c385b4SNeil Horman } 114995c385b4SNeil Horman #endif 1150ec5e3b0aSJonathan T. Leighton if (ipv6_addr_v4mapped(&fl6->saddr) && 115100ea1ceeSWillem de Bruijn !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) { 115200ea1ceeSWillem de Bruijn err = -EAFNOSUPPORT; 115300ea1ceeSWillem de Bruijn goto out_err_release; 115400ea1ceeSWillem de Bruijn } 115595c385b4SNeil Horman 11561da177e4SLinus Torvalds return 0; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds out_err_release: 11591da177e4SLinus Torvalds dst_release(*dst); 11601da177e4SLinus Torvalds *dst = NULL; 11618a966fc0SDavid Ahern 11620d240e78SDavid Ahern if (err == -ENETUNREACH) 11630d240e78SDavid Ahern IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES); 11641da177e4SLinus Torvalds return err; 11651da177e4SLinus Torvalds } 116634a0b3cdSAdrian Bunk 1167497c615aSHerbert Xu /** 1168497c615aSHerbert Xu * ip6_dst_lookup - perform route lookup on flow 1169b51cd7c8SAndrew Lunn * @net: Network namespace to perform lookup in 1170497c615aSHerbert Xu * @sk: socket which provides route info 1171497c615aSHerbert Xu * @dst: pointer to dst_entry * for result 11724c9483b2SDavid S. Miller * @fl6: flow to lookup 1173497c615aSHerbert Xu * 1174497c615aSHerbert Xu * This function performs a route lookup on the given flow. 1175497c615aSHerbert Xu * 1176497c615aSHerbert Xu * It returns zero on success, or a standard errno code on error. 1177497c615aSHerbert Xu */ 1178343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst, 1179343d60aaSRoopa Prabhu struct flowi6 *fl6) 1180497c615aSHerbert Xu { 1181497c615aSHerbert Xu *dst = NULL; 1182343d60aaSRoopa Prabhu return ip6_dst_lookup_tail(net, sk, dst, fl6); 1183497c615aSHerbert Xu } 11843cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip6_dst_lookup); 11853cf3dc6cSArnaldo Carvalho de Melo 1186497c615aSHerbert Xu /** 118768d0c6d3SDavid S. Miller * ip6_dst_lookup_flow - perform route lookup on flow with ipsec 1188b51cd7c8SAndrew Lunn * @net: Network namespace to perform lookup in 118968d0c6d3SDavid S. Miller * @sk: socket which provides route info 11904c9483b2SDavid S. Miller * @fl6: flow to lookup 119168d0c6d3SDavid S. Miller * @final_dst: final destination address for ipsec lookup 119268d0c6d3SDavid S. Miller * 119368d0c6d3SDavid S. Miller * This function performs a route lookup on the given flow. 119468d0c6d3SDavid S. Miller * 119568d0c6d3SDavid S. Miller * It returns a valid dst pointer on success, or a pointer encoded 119668d0c6d3SDavid S. Miller * error code. 119768d0c6d3SDavid S. Miller */ 1198c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6, 11990e0d44abSSteffen Klassert const struct in6_addr *final_dst) 120068d0c6d3SDavid S. Miller { 120168d0c6d3SDavid S. Miller struct dst_entry *dst = NULL; 120268d0c6d3SDavid S. Miller int err; 120368d0c6d3SDavid S. Miller 1204c4e85f73SSabrina Dubroca err = ip6_dst_lookup_tail(net, sk, &dst, fl6); 120568d0c6d3SDavid S. Miller if (err) 120668d0c6d3SDavid S. Miller return ERR_PTR(err); 120768d0c6d3SDavid S. Miller if (final_dst) 12084e3fd7a0SAlexey Dobriyan fl6->daddr = *final_dst; 12092774c131SDavid S. Miller 1210c4e85f73SSabrina Dubroca return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0); 121168d0c6d3SDavid S. Miller } 121268d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow); 121368d0c6d3SDavid S. Miller 121468d0c6d3SDavid S. Miller /** 121568d0c6d3SDavid S. Miller * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow 121668d0c6d3SDavid S. Miller * @sk: socket which provides the dst cache and route info 12174c9483b2SDavid S. Miller * @fl6: flow to lookup 121868d0c6d3SDavid S. Miller * @final_dst: final destination address for ipsec lookup 121996818159SAlexey Kodanev * @connected: whether @sk is connected or not 1220497c615aSHerbert Xu * 1221497c615aSHerbert Xu * This function performs a route lookup on the given flow with the 1222497c615aSHerbert Xu * possibility of using the cached route in the socket if it is valid. 1223497c615aSHerbert Xu * It will take the socket dst lock when operating on the dst cache. 1224497c615aSHerbert Xu * As a result, this function can only be used in process context. 1225497c615aSHerbert Xu * 122696818159SAlexey Kodanev * In addition, for a connected socket, cache the dst in the socket 122796818159SAlexey Kodanev * if the current cache is not valid. 122896818159SAlexey Kodanev * 122968d0c6d3SDavid S. Miller * It returns a valid dst pointer on success, or a pointer encoded 123068d0c6d3SDavid S. Miller * error code. 1231497c615aSHerbert Xu */ 12324c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, 123396818159SAlexey Kodanev const struct in6_addr *final_dst, 123496818159SAlexey Kodanev bool connected) 1235497c615aSHerbert Xu { 123668d0c6d3SDavid S. Miller struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie); 1237497c615aSHerbert Xu 12384c9483b2SDavid S. Miller dst = ip6_sk_dst_check(sk, dst, fl6); 123996818159SAlexey Kodanev if (dst) 124096818159SAlexey Kodanev return dst; 124196818159SAlexey Kodanev 1242c4e85f73SSabrina Dubroca dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst); 124396818159SAlexey Kodanev if (connected && !IS_ERR(dst)) 124496818159SAlexey Kodanev ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6); 124568d0c6d3SDavid S. Miller 124600bc0ef5SJakub Sitnicki return dst; 124768d0c6d3SDavid S. Miller } 124868d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow); 1249497c615aSHerbert Xu 1250571912c6SMartin Varghese /** 1251571912c6SMartin Varghese * ip6_dst_lookup_tunnel - perform route lookup on tunnel 1252571912c6SMartin Varghese * @skb: Packet for which lookup is done 1253571912c6SMartin Varghese * @dev: Tunnel device 1254571912c6SMartin Varghese * @net: Network namespace of tunnel device 1255b51cd7c8SAndrew Lunn * @sock: Socket which provides route info 1256571912c6SMartin Varghese * @saddr: Memory to store the src ip address 1257571912c6SMartin Varghese * @info: Tunnel information 1258571912c6SMartin Varghese * @protocol: IP protocol 1259b51cd7c8SAndrew Lunn * @use_cache: Flag to enable cache usage 1260571912c6SMartin Varghese * This function performs a route lookup on a tunnel 1261571912c6SMartin Varghese * 1262571912c6SMartin Varghese * It returns a valid dst pointer and stores src address to be used in 1263571912c6SMartin Varghese * tunnel in param saddr on success, else a pointer encoded error code. 1264571912c6SMartin Varghese */ 1265571912c6SMartin Varghese 1266571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb, 1267571912c6SMartin Varghese struct net_device *dev, 1268571912c6SMartin Varghese struct net *net, 1269571912c6SMartin Varghese struct socket *sock, 1270571912c6SMartin Varghese struct in6_addr *saddr, 1271571912c6SMartin Varghese const struct ip_tunnel_info *info, 1272571912c6SMartin Varghese u8 protocol, 1273571912c6SMartin Varghese bool use_cache) 1274571912c6SMartin Varghese { 1275571912c6SMartin Varghese struct dst_entry *dst = NULL; 1276571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE 1277571912c6SMartin Varghese struct dst_cache *dst_cache; 1278571912c6SMartin Varghese #endif 1279571912c6SMartin Varghese struct flowi6 fl6; 1280571912c6SMartin Varghese __u8 prio; 1281571912c6SMartin Varghese 1282571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE 1283571912c6SMartin Varghese dst_cache = (struct dst_cache *)&info->dst_cache; 1284571912c6SMartin Varghese if (use_cache) { 1285571912c6SMartin Varghese dst = dst_cache_get_ip6(dst_cache, saddr); 1286571912c6SMartin Varghese if (dst) 1287571912c6SMartin Varghese return dst; 1288571912c6SMartin Varghese } 1289571912c6SMartin Varghese #endif 1290571912c6SMartin Varghese memset(&fl6, 0, sizeof(fl6)); 1291571912c6SMartin Varghese fl6.flowi6_mark = skb->mark; 1292571912c6SMartin Varghese fl6.flowi6_proto = protocol; 1293571912c6SMartin Varghese fl6.daddr = info->key.u.ipv6.dst; 1294571912c6SMartin Varghese fl6.saddr = info->key.u.ipv6.src; 1295571912c6SMartin Varghese prio = info->key.tos; 1296571912c6SMartin Varghese fl6.flowlabel = ip6_make_flowinfo(RT_TOS(prio), 1297571912c6SMartin Varghese info->key.label); 1298571912c6SMartin Varghese 1299571912c6SMartin Varghese dst = ipv6_stub->ipv6_dst_lookup_flow(net, sock->sk, &fl6, 1300571912c6SMartin Varghese NULL); 1301571912c6SMartin Varghese if (IS_ERR(dst)) { 1302571912c6SMartin Varghese netdev_dbg(dev, "no route to %pI6\n", &fl6.daddr); 1303571912c6SMartin Varghese return ERR_PTR(-ENETUNREACH); 1304571912c6SMartin Varghese } 1305571912c6SMartin Varghese if (dst->dev == dev) { /* is this necessary? */ 1306571912c6SMartin Varghese netdev_dbg(dev, "circular route to %pI6\n", &fl6.daddr); 1307571912c6SMartin Varghese dst_release(dst); 1308571912c6SMartin Varghese return ERR_PTR(-ELOOP); 1309571912c6SMartin Varghese } 1310571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE 1311571912c6SMartin Varghese if (use_cache) 1312571912c6SMartin Varghese dst_cache_set_ip6(dst_cache, dst, &fl6.saddr); 1313571912c6SMartin Varghese #endif 1314571912c6SMartin Varghese *saddr = fl6.saddr; 1315571912c6SMartin Varghese return dst; 1316571912c6SMartin Varghese } 1317571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip6_dst_lookup_tunnel); 1318571912c6SMartin Varghese 13190178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src, 13200178b695SHerbert Xu gfp_t gfp) 13210178b695SHerbert Xu { 13220178b695SHerbert Xu return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; 13230178b695SHerbert Xu } 13240178b695SHerbert Xu 13250178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src, 13260178b695SHerbert Xu gfp_t gfp) 13270178b695SHerbert Xu { 13280178b695SHerbert Xu return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; 13290178b695SHerbert Xu } 13300178b695SHerbert Xu 133175a493e6SHannes Frederic Sowa static void ip6_append_data_mtu(unsigned int *mtu, 13320c183379SGao feng int *maxfraglen, 13330c183379SGao feng unsigned int fragheaderlen, 13340c183379SGao feng struct sk_buff *skb, 133575a493e6SHannes Frederic Sowa struct rt6_info *rt, 1336e367c2d0Slucien unsigned int orig_mtu) 13370c183379SGao feng { 13380c183379SGao feng if (!(rt->dst.flags & DST_XFRM_TUNNEL)) { 133963159f29SIan Morris if (!skb) { 13400c183379SGao feng /* first fragment, reserve header_len */ 1341e367c2d0Slucien *mtu = orig_mtu - rt->dst.header_len; 13420c183379SGao feng 13430c183379SGao feng } else { 13440c183379SGao feng /* 13450c183379SGao feng * this fragment is not first, the headers 13460c183379SGao feng * space is regarded as data space. 13470c183379SGao feng */ 1348e367c2d0Slucien *mtu = orig_mtu; 13490c183379SGao feng } 13500c183379SGao feng *maxfraglen = ((*mtu - fragheaderlen) & ~7) 13510c183379SGao feng + fragheaderlen - sizeof(struct frag_hdr); 13520c183379SGao feng } 13530c183379SGao feng } 13540c183379SGao feng 1355366e41d9SVlad Yasevich static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork, 135626879da5SWei Wang struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6, 13575fdaa88dSWillem de Bruijn struct rt6_info *rt, struct flowi6 *fl6) 1358366e41d9SVlad Yasevich { 1359366e41d9SVlad Yasevich struct ipv6_pinfo *np = inet6_sk(sk); 1360366e41d9SVlad Yasevich unsigned int mtu; 136126879da5SWei Wang struct ipv6_txoptions *opt = ipc6->opt; 1362366e41d9SVlad Yasevich 1363366e41d9SVlad Yasevich /* 1364366e41d9SVlad Yasevich * setup for corking 1365366e41d9SVlad Yasevich */ 1366366e41d9SVlad Yasevich if (opt) { 1367366e41d9SVlad Yasevich if (WARN_ON(v6_cork->opt)) 1368366e41d9SVlad Yasevich return -EINVAL; 1369366e41d9SVlad Yasevich 1370864e2a1fSEric Dumazet v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation); 137163159f29SIan Morris if (unlikely(!v6_cork->opt)) 1372366e41d9SVlad Yasevich return -ENOBUFS; 1373366e41d9SVlad Yasevich 1374864e2a1fSEric Dumazet v6_cork->opt->tot_len = sizeof(*opt); 1375366e41d9SVlad Yasevich v6_cork->opt->opt_flen = opt->opt_flen; 1376366e41d9SVlad Yasevich v6_cork->opt->opt_nflen = opt->opt_nflen; 1377366e41d9SVlad Yasevich 1378366e41d9SVlad Yasevich v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt, 1379366e41d9SVlad Yasevich sk->sk_allocation); 1380366e41d9SVlad Yasevich if (opt->dst0opt && !v6_cork->opt->dst0opt) 1381366e41d9SVlad Yasevich return -ENOBUFS; 1382366e41d9SVlad Yasevich 1383366e41d9SVlad Yasevich v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt, 1384366e41d9SVlad Yasevich sk->sk_allocation); 1385366e41d9SVlad Yasevich if (opt->dst1opt && !v6_cork->opt->dst1opt) 1386366e41d9SVlad Yasevich return -ENOBUFS; 1387366e41d9SVlad Yasevich 1388366e41d9SVlad Yasevich v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt, 1389366e41d9SVlad Yasevich sk->sk_allocation); 1390366e41d9SVlad Yasevich if (opt->hopopt && !v6_cork->opt->hopopt) 1391366e41d9SVlad Yasevich return -ENOBUFS; 1392366e41d9SVlad Yasevich 1393366e41d9SVlad Yasevich v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt, 1394366e41d9SVlad Yasevich sk->sk_allocation); 1395366e41d9SVlad Yasevich if (opt->srcrt && !v6_cork->opt->srcrt) 1396366e41d9SVlad Yasevich return -ENOBUFS; 1397366e41d9SVlad Yasevich 1398366e41d9SVlad Yasevich /* need source address above miyazawa*/ 1399366e41d9SVlad Yasevich } 1400366e41d9SVlad Yasevich dst_hold(&rt->dst); 1401366e41d9SVlad Yasevich cork->base.dst = &rt->dst; 1402366e41d9SVlad Yasevich cork->fl.u.ip6 = *fl6; 140326879da5SWei Wang v6_cork->hop_limit = ipc6->hlimit; 140426879da5SWei Wang v6_cork->tclass = ipc6->tclass; 1405366e41d9SVlad Yasevich if (rt->dst.flags & DST_XFRM_TUNNEL) 1406366e41d9SVlad Yasevich mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ? 1407749439bfSMike Maloney READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst); 1408366e41d9SVlad Yasevich else 1409366e41d9SVlad Yasevich mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ? 1410c02b3741SDavid S. Miller READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst)); 1411366e41d9SVlad Yasevich if (np->frag_size < mtu) { 1412366e41d9SVlad Yasevich if (np->frag_size) 1413366e41d9SVlad Yasevich mtu = np->frag_size; 1414366e41d9SVlad Yasevich } 1415749439bfSMike Maloney if (mtu < IPV6_MIN_MTU) 1416749439bfSMike Maloney return -EINVAL; 1417366e41d9SVlad Yasevich cork->base.fragsize = mtu; 1418fbf47813SWillem de Bruijn cork->base.gso_size = ipc6->gso_size; 1419678ca42dSWillem de Bruijn cork->base.tx_flags = 0; 1420c6af0c22SWillem de Bruijn cork->base.mark = ipc6->sockc.mark; 1421678ca42dSWillem de Bruijn sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags); 1422bec1f6f6SWillem de Bruijn 14230f6c480fSDavid Miller if (dst_allfrag(xfrm_dst_path(&rt->dst))) 1424366e41d9SVlad Yasevich cork->base.flags |= IPCORK_ALLFRAG; 1425366e41d9SVlad Yasevich cork->base.length = 0; 1426366e41d9SVlad Yasevich 14275fdaa88dSWillem de Bruijn cork->base.transmit_time = ipc6->sockc.transmit_time; 1428a818f75eSJesus Sanchez-Palencia 1429366e41d9SVlad Yasevich return 0; 1430366e41d9SVlad Yasevich } 1431366e41d9SVlad Yasevich 14320bbe84a6SVlad Yasevich static int __ip6_append_data(struct sock *sk, 14330bbe84a6SVlad Yasevich struct flowi6 *fl6, 14340bbe84a6SVlad Yasevich struct sk_buff_head *queue, 14350bbe84a6SVlad Yasevich struct inet_cork *cork, 14360bbe84a6SVlad Yasevich struct inet6_cork *v6_cork, 14370bbe84a6SVlad Yasevich struct page_frag *pfrag, 14380bbe84a6SVlad Yasevich int getfrag(void *from, char *to, int offset, 14390bbe84a6SVlad Yasevich int len, int odd, struct sk_buff *skb), 14401da177e4SLinus Torvalds void *from, int length, int transhdrlen, 14415fdaa88dSWillem de Bruijn unsigned int flags, struct ipcm6_cookie *ipc6) 14421da177e4SLinus Torvalds { 14430c183379SGao feng struct sk_buff *skb, *skb_prev = NULL; 144410b8a3deSPaolo Abeni unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu; 1445b5947e5dSWillem de Bruijn struct ubuf_info *uarg = NULL; 14460bbe84a6SVlad Yasevich int exthdrlen = 0; 14470bbe84a6SVlad Yasevich int dst_exthdrlen = 0; 14481da177e4SLinus Torvalds int hh_len; 14491da177e4SLinus Torvalds int copy; 14501da177e4SLinus Torvalds int err; 14511da177e4SLinus Torvalds int offset = 0; 145209c2d251SWillem de Bruijn u32 tskey = 0; 14530bbe84a6SVlad Yasevich struct rt6_info *rt = (struct rt6_info *)cork->dst; 14540bbe84a6SVlad Yasevich struct ipv6_txoptions *opt = v6_cork->opt; 145532dce968SVlad Yasevich int csummode = CHECKSUM_NONE; 1456682b1a9dSHannes Frederic Sowa unsigned int maxnonfragsize, headersize; 14571f4c6eb2SEric Dumazet unsigned int wmem_alloc_delta = 0; 1458100f6d8eSWillem de Bruijn bool paged, extra_uref = false; 14591da177e4SLinus Torvalds 14600bbe84a6SVlad Yasevich skb = skb_peek_tail(queue); 14610bbe84a6SVlad Yasevich if (!skb) { 14620bbe84a6SVlad Yasevich exthdrlen = opt ? opt->opt_flen : 0; 14637efdba5bSRomain KUNTZ dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len; 14641da177e4SLinus Torvalds } 14650bbe84a6SVlad Yasevich 146615e36f5bSWillem de Bruijn paged = !!cork->gso_size; 1467bec1f6f6SWillem de Bruijn mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize; 1468e367c2d0Slucien orig_mtu = mtu; 14691da177e4SLinus Torvalds 1470678ca42dSWillem de Bruijn if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && 1471678ca42dSWillem de Bruijn sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) 1472678ca42dSWillem de Bruijn tskey = sk->sk_tskey++; 1473678ca42dSWillem de Bruijn 1474d8d1f30bSChangli Gao hh_len = LL_RESERVED_SPACE(rt->dst.dev); 14751da177e4SLinus Torvalds 1476a1b05140SMasahide NAKAMURA fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len + 1477b4ce9277SHerbert Xu (opt ? opt->opt_nflen : 0); 14784df98e76SHannes Frederic Sowa maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - 14794df98e76SHannes Frederic Sowa sizeof(struct frag_hdr); 14801da177e4SLinus Torvalds 14814df98e76SHannes Frederic Sowa headersize = sizeof(struct ipv6hdr) + 14823a1cebe7SHannes Frederic Sowa (opt ? opt->opt_flen + opt->opt_nflen : 0) + 14834df98e76SHannes Frederic Sowa (dst_allfrag(&rt->dst) ? 14844df98e76SHannes Frederic Sowa sizeof(struct frag_hdr) : 0) + 14854df98e76SHannes Frederic Sowa rt->rt6i_nfheader_len; 14864df98e76SHannes Frederic Sowa 148710b8a3deSPaolo Abeni /* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit 148810b8a3deSPaolo Abeni * the first fragment 148910b8a3deSPaolo Abeni */ 149010b8a3deSPaolo Abeni if (headersize + transhdrlen > mtu) 149110b8a3deSPaolo Abeni goto emsgsize; 149210b8a3deSPaolo Abeni 149326879da5SWei Wang if (cork->length + length > mtu - headersize && ipc6->dontfrag && 14944df98e76SHannes Frederic Sowa (sk->sk_protocol == IPPROTO_UDP || 14954df98e76SHannes Frederic Sowa sk->sk_protocol == IPPROTO_RAW)) { 14964df98e76SHannes Frederic Sowa ipv6_local_rxpmtu(sk, fl6, mtu - headersize + 14974df98e76SHannes Frederic Sowa sizeof(struct ipv6hdr)); 14984df98e76SHannes Frederic Sowa goto emsgsize; 14994df98e76SHannes Frederic Sowa } 15004df98e76SHannes Frederic Sowa 1501682b1a9dSHannes Frederic Sowa if (ip6_sk_ignore_df(sk)) 1502682b1a9dSHannes Frederic Sowa maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN; 1503682b1a9dSHannes Frederic Sowa else 1504682b1a9dSHannes Frederic Sowa maxnonfragsize = mtu; 1505682b1a9dSHannes Frederic Sowa 15064df98e76SHannes Frederic Sowa if (cork->length + length > maxnonfragsize - headersize) { 15074df98e76SHannes Frederic Sowa emsgsize: 150810b8a3deSPaolo Abeni pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0); 150910b8a3deSPaolo Abeni ipv6_local_error(sk, EMSGSIZE, fl6, pmtu); 15101da177e4SLinus Torvalds return -EMSGSIZE; 15111da177e4SLinus Torvalds } 1512682b1a9dSHannes Frederic Sowa 1513682b1a9dSHannes Frederic Sowa /* CHECKSUM_PARTIAL only with no extension headers and when 1514682b1a9dSHannes Frederic Sowa * we are not going to fragment 1515682b1a9dSHannes Frederic Sowa */ 1516682b1a9dSHannes Frederic Sowa if (transhdrlen && sk->sk_protocol == IPPROTO_UDP && 1517682b1a9dSHannes Frederic Sowa headersize == sizeof(struct ipv6hdr) && 15182b89ed65SVlad Yasevich length <= mtu - headersize && 1519bec1f6f6SWillem de Bruijn (!(flags & MSG_MORE) || cork->gso_size) && 1520c8cd0989STom Herbert rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM)) 1521682b1a9dSHannes Frederic Sowa csummode = CHECKSUM_PARTIAL; 15221da177e4SLinus Torvalds 1523b5947e5dSWillem de Bruijn if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) { 15248c793822SJonathan Lemon uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb)); 1525b5947e5dSWillem de Bruijn if (!uarg) 1526b5947e5dSWillem de Bruijn return -ENOBUFS; 1527522924b5SWillem de Bruijn extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ 1528b5947e5dSWillem de Bruijn if (rt->dst.dev->features & NETIF_F_SG && 1529b5947e5dSWillem de Bruijn csummode == CHECKSUM_PARTIAL) { 1530b5947e5dSWillem de Bruijn paged = true; 1531b5947e5dSWillem de Bruijn } else { 1532b5947e5dSWillem de Bruijn uarg->zerocopy = 0; 153352900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 1534b5947e5dSWillem de Bruijn } 1535b5947e5dSWillem de Bruijn } 1536b5947e5dSWillem de Bruijn 15371da177e4SLinus Torvalds /* 15381da177e4SLinus Torvalds * Let's try using as much space as possible. 15391da177e4SLinus Torvalds * Use MTU if total length of the message fits into the MTU. 15401da177e4SLinus Torvalds * Otherwise, we need to reserve fragment header and 15411da177e4SLinus Torvalds * fragment alignment (= 8-15 octects, in total). 15421da177e4SLinus Torvalds * 1543634a63e7SRandy Dunlap * Note that we may need to "move" the data from the tail 15441da177e4SLinus Torvalds * of the buffer to the new fragment when we split 15451da177e4SLinus Torvalds * the message. 15461da177e4SLinus Torvalds * 15471da177e4SLinus Torvalds * FIXME: It may be fragmented into multiple chunks 15481da177e4SLinus Torvalds * at once if non-fragmentable extension headers 15491da177e4SLinus Torvalds * are too large. 15501da177e4SLinus Torvalds * --yoshfuji 15511da177e4SLinus Torvalds */ 15521da177e4SLinus Torvalds 15532811ebacSHannes Frederic Sowa cork->length += length; 15542811ebacSHannes Frederic Sowa if (!skb) 15551da177e4SLinus Torvalds goto alloc_new_skb; 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds while (length > 0) { 15581da177e4SLinus Torvalds /* Check if the remaining data fits into current packet. */ 1559bdc712b4SDavid S. Miller copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len; 15601da177e4SLinus Torvalds if (copy < length) 15611da177e4SLinus Torvalds copy = maxfraglen - skb->len; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds if (copy <= 0) { 15641da177e4SLinus Torvalds char *data; 15651da177e4SLinus Torvalds unsigned int datalen; 15661da177e4SLinus Torvalds unsigned int fraglen; 15671da177e4SLinus Torvalds unsigned int fraggap; 15686d123b81SJakub Kicinski unsigned int alloclen, alloc_extra; 1569aba36930SWillem de Bruijn unsigned int pagedlen; 15701da177e4SLinus Torvalds alloc_new_skb: 15711da177e4SLinus Torvalds /* There's no room in the current skb */ 15720c183379SGao feng if (skb) 15730c183379SGao feng fraggap = skb->len - maxfraglen; 15741da177e4SLinus Torvalds else 15751da177e4SLinus Torvalds fraggap = 0; 15760c183379SGao feng /* update mtu and maxfraglen if necessary */ 157763159f29SIan Morris if (!skb || !skb_prev) 15780c183379SGao feng ip6_append_data_mtu(&mtu, &maxfraglen, 157975a493e6SHannes Frederic Sowa fragheaderlen, skb, rt, 1580e367c2d0Slucien orig_mtu); 15810c183379SGao feng 15820c183379SGao feng skb_prev = skb; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* 15851da177e4SLinus Torvalds * If remaining data exceeds the mtu, 15861da177e4SLinus Torvalds * we know we need more fragment(s). 15871da177e4SLinus Torvalds */ 15881da177e4SLinus Torvalds datalen = length + fraggap; 15891da177e4SLinus Torvalds 15900c183379SGao feng if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen) 15910c183379SGao feng datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len; 159215e36f5bSWillem de Bruijn fraglen = datalen + fragheaderlen; 1593aba36930SWillem de Bruijn pagedlen = 0; 159415e36f5bSWillem de Bruijn 15956d123b81SJakub Kicinski alloc_extra = hh_len; 15966d123b81SJakub Kicinski alloc_extra += dst_exthdrlen; 15976d123b81SJakub Kicinski alloc_extra += rt->dst.trailer_len; 15986d123b81SJakub Kicinski 15996d123b81SJakub Kicinski /* We just reserve space for fragment header. 16006d123b81SJakub Kicinski * Note: this may be overallocation if the message 16016d123b81SJakub Kicinski * (without MSG_MORE) fits into the MTU. 16026d123b81SJakub Kicinski */ 16036d123b81SJakub Kicinski alloc_extra += sizeof(struct frag_hdr); 16046d123b81SJakub Kicinski 16051da177e4SLinus Torvalds if ((flags & MSG_MORE) && 1606d8d1f30bSChangli Gao !(rt->dst.dev->features&NETIF_F_SG)) 16071da177e4SLinus Torvalds alloclen = mtu; 16086d123b81SJakub Kicinski else if (!paged && 16096d123b81SJakub Kicinski (fraglen + alloc_extra < SKB_MAX_ALLOC || 16106d123b81SJakub Kicinski !(rt->dst.dev->features & NETIF_F_SG))) 161115e36f5bSWillem de Bruijn alloclen = fraglen; 161215e36f5bSWillem de Bruijn else { 161315e36f5bSWillem de Bruijn alloclen = min_t(int, fraglen, MAX_HEADER); 161415e36f5bSWillem de Bruijn pagedlen = fraglen - alloclen; 161515e36f5bSWillem de Bruijn } 16166d123b81SJakub Kicinski alloclen += alloc_extra; 1617299b0767SSteffen Klassert 16180c183379SGao feng if (datalen != length + fraggap) { 16191da177e4SLinus Torvalds /* 16200c183379SGao feng * this is not the last fragment, the trailer 16210c183379SGao feng * space is regarded as data space. 16221da177e4SLinus Torvalds */ 16230c183379SGao feng datalen += rt->dst.trailer_len; 16240c183379SGao feng } 16250c183379SGao feng 16260c183379SGao feng fraglen = datalen + fragheaderlen; 16271da177e4SLinus Torvalds 162815e36f5bSWillem de Bruijn copy = datalen - transhdrlen - fraggap - pagedlen; 1629232cd35dSEric Dumazet if (copy < 0) { 1630232cd35dSEric Dumazet err = -EINVAL; 1631232cd35dSEric Dumazet goto error; 1632232cd35dSEric Dumazet } 16331da177e4SLinus Torvalds if (transhdrlen) { 16346d123b81SJakub Kicinski skb = sock_alloc_send_skb(sk, alloclen, 16351da177e4SLinus Torvalds (flags & MSG_DONTWAIT), &err); 16361da177e4SLinus Torvalds } else { 16371da177e4SLinus Torvalds skb = NULL; 16381f4c6eb2SEric Dumazet if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 16391da177e4SLinus Torvalds 2 * sk->sk_sndbuf) 16406d123b81SJakub Kicinski skb = alloc_skb(alloclen, 16411da177e4SLinus Torvalds sk->sk_allocation); 164263159f29SIan Morris if (unlikely(!skb)) 16431da177e4SLinus Torvalds err = -ENOBUFS; 16441da177e4SLinus Torvalds } 164563159f29SIan Morris if (!skb) 16461da177e4SLinus Torvalds goto error; 16471da177e4SLinus Torvalds /* 16481da177e4SLinus Torvalds * Fill in the control structures 16491da177e4SLinus Torvalds */ 16509c9c9ad5SHannes Frederic Sowa skb->protocol = htons(ETH_P_IPV6); 165132dce968SVlad Yasevich skb->ip_summed = csummode; 16521da177e4SLinus Torvalds skb->csum = 0; 16531f85851eSGao feng /* reserve for fragmentation and ipsec header */ 16541f85851eSGao feng skb_reserve(skb, hh_len + sizeof(struct frag_hdr) + 16551f85851eSGao feng dst_exthdrlen); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds /* 16581da177e4SLinus Torvalds * Find where to start putting bytes 16591da177e4SLinus Torvalds */ 166015e36f5bSWillem de Bruijn data = skb_put(skb, fraglen - pagedlen); 16611f85851eSGao feng skb_set_network_header(skb, exthdrlen); 16621f85851eSGao feng data += fragheaderlen; 1663b0e380b1SArnaldo Carvalho de Melo skb->transport_header = (skb->network_header + 1664b0e380b1SArnaldo Carvalho de Melo fragheaderlen); 16651da177e4SLinus Torvalds if (fraggap) { 16661da177e4SLinus Torvalds skb->csum = skb_copy_and_csum_bits( 16671da177e4SLinus Torvalds skb_prev, maxfraglen, 16688d5930dfSAl Viro data + transhdrlen, fraggap); 16691da177e4SLinus Torvalds skb_prev->csum = csum_sub(skb_prev->csum, 16701da177e4SLinus Torvalds skb->csum); 16711da177e4SLinus Torvalds data += fraggap; 1672e9fa4f7bSHerbert Xu pskb_trim_unique(skb_prev, maxfraglen); 16731da177e4SLinus Torvalds } 1674232cd35dSEric Dumazet if (copy > 0 && 1675232cd35dSEric Dumazet getfrag(from, data + transhdrlen, offset, 1676232cd35dSEric Dumazet copy, fraggap, skb) < 0) { 16771da177e4SLinus Torvalds err = -EFAULT; 16781da177e4SLinus Torvalds kfree_skb(skb); 16791da177e4SLinus Torvalds goto error; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds offset += copy; 168315e36f5bSWillem de Bruijn length -= copy + transhdrlen; 16841da177e4SLinus Torvalds transhdrlen = 0; 16851da177e4SLinus Torvalds exthdrlen = 0; 1686299b0767SSteffen Klassert dst_exthdrlen = 0; 16871da177e4SLinus Torvalds 168852900d22SWillem de Bruijn /* Only the initial fragment is time stamped */ 168952900d22SWillem de Bruijn skb_shinfo(skb)->tx_flags = cork->tx_flags; 169052900d22SWillem de Bruijn cork->tx_flags = 0; 169152900d22SWillem de Bruijn skb_shinfo(skb)->tskey = tskey; 169252900d22SWillem de Bruijn tskey = 0; 169352900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, &extra_uref); 169452900d22SWillem de Bruijn 16950dec879fSJulian Anastasov if ((flags & MSG_CONFIRM) && !skb_prev) 16960dec879fSJulian Anastasov skb_set_dst_pending_confirm(skb, 1); 16970dec879fSJulian Anastasov 16981da177e4SLinus Torvalds /* 16991da177e4SLinus Torvalds * Put the packet on the pending queue 17001da177e4SLinus Torvalds */ 17011f4c6eb2SEric Dumazet if (!skb->destructor) { 17021f4c6eb2SEric Dumazet skb->destructor = sock_wfree; 17031f4c6eb2SEric Dumazet skb->sk = sk; 17041f4c6eb2SEric Dumazet wmem_alloc_delta += skb->truesize; 17051f4c6eb2SEric Dumazet } 17060bbe84a6SVlad Yasevich __skb_queue_tail(queue, skb); 17071da177e4SLinus Torvalds continue; 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds if (copy > length) 17111da177e4SLinus Torvalds copy = length; 17121da177e4SLinus Torvalds 1713113f99c3SWillem de Bruijn if (!(rt->dst.dev->features&NETIF_F_SG) && 1714113f99c3SWillem de Bruijn skb_tailroom(skb) >= copy) { 17151da177e4SLinus Torvalds unsigned int off; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds off = skb->len; 17181da177e4SLinus Torvalds if (getfrag(from, skb_put(skb, copy), 17191da177e4SLinus Torvalds offset, copy, off, skb) < 0) { 17201da177e4SLinus Torvalds __skb_trim(skb, off); 17211da177e4SLinus Torvalds err = -EFAULT; 17221da177e4SLinus Torvalds goto error; 17231da177e4SLinus Torvalds } 1724b5947e5dSWillem de Bruijn } else if (!uarg || !uarg->zerocopy) { 17251da177e4SLinus Torvalds int i = skb_shinfo(skb)->nr_frags; 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds err = -ENOMEM; 17285640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 17291da177e4SLinus Torvalds goto error; 17301da177e4SLinus Torvalds 17315640f768SEric Dumazet if (!skb_can_coalesce(skb, i, pfrag->page, 17325640f768SEric Dumazet pfrag->offset)) { 17331da177e4SLinus Torvalds err = -EMSGSIZE; 17345640f768SEric Dumazet if (i == MAX_SKB_FRAGS) 17351da177e4SLinus Torvalds goto error; 17365640f768SEric Dumazet 17375640f768SEric Dumazet __skb_fill_page_desc(skb, i, pfrag->page, 17385640f768SEric Dumazet pfrag->offset, 0); 17395640f768SEric Dumazet skb_shinfo(skb)->nr_frags = ++i; 17405640f768SEric Dumazet get_page(pfrag->page); 17411da177e4SLinus Torvalds } 17425640f768SEric Dumazet copy = min_t(int, copy, pfrag->size - pfrag->offset); 17439e903e08SEric Dumazet if (getfrag(from, 17445640f768SEric Dumazet page_address(pfrag->page) + pfrag->offset, 17455640f768SEric Dumazet offset, copy, skb->len, skb) < 0) 17465640f768SEric Dumazet goto error_efault; 17475640f768SEric Dumazet 17485640f768SEric Dumazet pfrag->offset += copy; 17495640f768SEric Dumazet skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); 17501da177e4SLinus Torvalds skb->len += copy; 17511da177e4SLinus Torvalds skb->data_len += copy; 1752f945fa7aSHerbert Xu skb->truesize += copy; 17531f4c6eb2SEric Dumazet wmem_alloc_delta += copy; 1754b5947e5dSWillem de Bruijn } else { 1755b5947e5dSWillem de Bruijn err = skb_zerocopy_iter_dgram(skb, from, copy); 1756b5947e5dSWillem de Bruijn if (err < 0) 1757b5947e5dSWillem de Bruijn goto error; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds offset += copy; 17601da177e4SLinus Torvalds length -= copy; 17611da177e4SLinus Torvalds } 17625640f768SEric Dumazet 17639e8445a5SPaolo Abeni if (wmem_alloc_delta) 17641f4c6eb2SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 17651da177e4SLinus Torvalds return 0; 17665640f768SEric Dumazet 17675640f768SEric Dumazet error_efault: 17685640f768SEric Dumazet err = -EFAULT; 17691da177e4SLinus Torvalds error: 17708e044917SJonathan Lemon net_zcopy_put_abort(uarg, extra_uref); 1771bdc712b4SDavid S. Miller cork->length -= length; 17723bd653c8SDenis V. Lunev IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); 17731f4c6eb2SEric Dumazet refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); 17741da177e4SLinus Torvalds return err; 17751da177e4SLinus Torvalds } 17760bbe84a6SVlad Yasevich 17770bbe84a6SVlad Yasevich int ip6_append_data(struct sock *sk, 17780bbe84a6SVlad Yasevich int getfrag(void *from, char *to, int offset, int len, 17790bbe84a6SVlad Yasevich int odd, struct sk_buff *skb), 178026879da5SWei Wang void *from, int length, int transhdrlen, 178126879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 17825fdaa88dSWillem de Bruijn struct rt6_info *rt, unsigned int flags) 17830bbe84a6SVlad Yasevich { 17840bbe84a6SVlad Yasevich struct inet_sock *inet = inet_sk(sk); 17850bbe84a6SVlad Yasevich struct ipv6_pinfo *np = inet6_sk(sk); 17860bbe84a6SVlad Yasevich int exthdrlen; 17870bbe84a6SVlad Yasevich int err; 17880bbe84a6SVlad Yasevich 17890bbe84a6SVlad Yasevich if (flags&MSG_PROBE) 17900bbe84a6SVlad Yasevich return 0; 17910bbe84a6SVlad Yasevich if (skb_queue_empty(&sk->sk_write_queue)) { 17920bbe84a6SVlad Yasevich /* 17930bbe84a6SVlad Yasevich * setup for corking 17940bbe84a6SVlad Yasevich */ 179526879da5SWei Wang err = ip6_setup_cork(sk, &inet->cork, &np->cork, 17965fdaa88dSWillem de Bruijn ipc6, rt, fl6); 17970bbe84a6SVlad Yasevich if (err) 17980bbe84a6SVlad Yasevich return err; 17990bbe84a6SVlad Yasevich 180026879da5SWei Wang exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0); 18010bbe84a6SVlad Yasevich length += exthdrlen; 18020bbe84a6SVlad Yasevich transhdrlen += exthdrlen; 18030bbe84a6SVlad Yasevich } else { 18040bbe84a6SVlad Yasevich fl6 = &inet->cork.fl.u.ip6; 18050bbe84a6SVlad Yasevich transhdrlen = 0; 18060bbe84a6SVlad Yasevich } 18070bbe84a6SVlad Yasevich 18080bbe84a6SVlad Yasevich return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base, 18090bbe84a6SVlad Yasevich &np->cork, sk_page_frag(sk), getfrag, 18105fdaa88dSWillem de Bruijn from, length, transhdrlen, flags, ipc6); 18110bbe84a6SVlad Yasevich } 1812a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data); 18131da177e4SLinus Torvalds 1814366e41d9SVlad Yasevich static void ip6_cork_release(struct inet_cork_full *cork, 1815366e41d9SVlad Yasevich struct inet6_cork *v6_cork) 1816bf138862SPavel Emelyanov { 1817366e41d9SVlad Yasevich if (v6_cork->opt) { 1818366e41d9SVlad Yasevich kfree(v6_cork->opt->dst0opt); 1819366e41d9SVlad Yasevich kfree(v6_cork->opt->dst1opt); 1820366e41d9SVlad Yasevich kfree(v6_cork->opt->hopopt); 1821366e41d9SVlad Yasevich kfree(v6_cork->opt->srcrt); 1822366e41d9SVlad Yasevich kfree(v6_cork->opt); 1823366e41d9SVlad Yasevich v6_cork->opt = NULL; 18240178b695SHerbert Xu } 18250178b695SHerbert Xu 1826366e41d9SVlad Yasevich if (cork->base.dst) { 1827366e41d9SVlad Yasevich dst_release(cork->base.dst); 1828366e41d9SVlad Yasevich cork->base.dst = NULL; 1829366e41d9SVlad Yasevich cork->base.flags &= ~IPCORK_ALLFRAG; 1830bf138862SPavel Emelyanov } 1831366e41d9SVlad Yasevich memset(&cork->fl, 0, sizeof(cork->fl)); 1832bf138862SPavel Emelyanov } 1833bf138862SPavel Emelyanov 18346422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk, 18356422398cSVlad Yasevich struct sk_buff_head *queue, 18366422398cSVlad Yasevich struct inet_cork_full *cork, 18376422398cSVlad Yasevich struct inet6_cork *v6_cork) 18381da177e4SLinus Torvalds { 18391da177e4SLinus Torvalds struct sk_buff *skb, *tmp_skb; 18401da177e4SLinus Torvalds struct sk_buff **tail_skb; 18411da177e4SLinus Torvalds struct in6_addr final_dst_buf, *final_dst = &final_dst_buf; 18421da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 18433bd653c8SDenis V. Lunev struct net *net = sock_net(sk); 18441da177e4SLinus Torvalds struct ipv6hdr *hdr; 18456422398cSVlad Yasevich struct ipv6_txoptions *opt = v6_cork->opt; 18466422398cSVlad Yasevich struct rt6_info *rt = (struct rt6_info *)cork->base.dst; 18476422398cSVlad Yasevich struct flowi6 *fl6 = &cork->fl.u.ip6; 18484c9483b2SDavid S. Miller unsigned char proto = fl6->flowi6_proto; 18491da177e4SLinus Torvalds 18506422398cSVlad Yasevich skb = __skb_dequeue(queue); 185163159f29SIan Morris if (!skb) 18521da177e4SLinus Torvalds goto out; 18531da177e4SLinus Torvalds tail_skb = &(skb_shinfo(skb)->frag_list); 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds /* move skb->data to ip header from ext header */ 1856d56f90a7SArnaldo Carvalho de Melo if (skb->data < skb_network_header(skb)) 1857bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 18586422398cSVlad Yasevich while ((tmp_skb = __skb_dequeue(queue)) != NULL) { 1859cfe1fc77SArnaldo Carvalho de Melo __skb_pull(tmp_skb, skb_network_header_len(skb)); 18601da177e4SLinus Torvalds *tail_skb = tmp_skb; 18611da177e4SLinus Torvalds tail_skb = &(tmp_skb->next); 18621da177e4SLinus Torvalds skb->len += tmp_skb->len; 18631da177e4SLinus Torvalds skb->data_len += tmp_skb->len; 18641da177e4SLinus Torvalds skb->truesize += tmp_skb->truesize; 18651da177e4SLinus Torvalds tmp_skb->destructor = NULL; 18661da177e4SLinus Torvalds tmp_skb->sk = NULL; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 186928a89453SHerbert Xu /* Allow local fragmentation. */ 187060ff7467SWANG Cong skb->ignore_df = ip6_sk_ignore_df(sk); 187128a89453SHerbert Xu 18724e3fd7a0SAlexey Dobriyan *final_dst = fl6->daddr; 1873cfe1fc77SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_header_len(skb)); 18741da177e4SLinus Torvalds if (opt && opt->opt_flen) 18751da177e4SLinus Torvalds ipv6_push_frag_opts(skb, opt, &proto); 18761da177e4SLinus Torvalds if (opt && opt->opt_nflen) 1877613fa3caSDavid Lebrun ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr); 18781da177e4SLinus Torvalds 1879e2d1bca7SArnaldo Carvalho de Melo skb_push(skb, sizeof(struct ipv6hdr)); 1880e2d1bca7SArnaldo Carvalho de Melo skb_reset_network_header(skb); 18810660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 18821da177e4SLinus Torvalds 18836422398cSVlad Yasevich ip6_flow_hdr(hdr, v6_cork->tclass, 1884cb1ce2efSTom Herbert ip6_make_flowlabel(net, skb, fl6->flowlabel, 1885513674b5SShaohua Li ip6_autoflowlabel(net, np), fl6)); 18866422398cSVlad Yasevich hdr->hop_limit = v6_cork->hop_limit; 18871da177e4SLinus Torvalds hdr->nexthdr = proto; 18884e3fd7a0SAlexey Dobriyan hdr->saddr = fl6->saddr; 18894e3fd7a0SAlexey Dobriyan hdr->daddr = *final_dst; 18901da177e4SLinus Torvalds 1891a2c2064fSPatrick McHardy skb->priority = sk->sk_priority; 1892c6af0c22SWillem de Bruijn skb->mark = cork->base.mark; 1893a2c2064fSPatrick McHardy 1894a818f75eSJesus Sanchez-Palencia skb->tstamp = cork->base.transmit_time; 1895a818f75eSJesus Sanchez-Palencia 1896d8d1f30bSChangli Gao skb_dst_set(skb, dst_clone(&rt->dst)); 1897edf391ffSNeil Horman IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 189814878f75SDavid L Stevens if (proto == IPPROTO_ICMPV6) { 1899adf30907SEric Dumazet struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 190014878f75SDavid L Stevens 190143a43b60SHannes Frederic Sowa ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type); 190243a43b60SHannes Frederic Sowa ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); 190314878f75SDavid L Stevens } 190414878f75SDavid L Stevens 19056422398cSVlad Yasevich ip6_cork_release(cork, v6_cork); 19066422398cSVlad Yasevich out: 19076422398cSVlad Yasevich return skb; 19086422398cSVlad Yasevich } 19096422398cSVlad Yasevich 19106422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb) 19116422398cSVlad Yasevich { 19126422398cSVlad Yasevich struct net *net = sock_net(skb->sk); 19136422398cSVlad Yasevich struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 19146422398cSVlad Yasevich int err; 19156422398cSVlad Yasevich 191633224b16SEric W. Biederman err = ip6_local_out(net, skb->sk, skb); 19171da177e4SLinus Torvalds if (err) { 19181da177e4SLinus Torvalds if (err > 0) 19196ce9e7b5SEric Dumazet err = net_xmit_errno(err); 19201da177e4SLinus Torvalds if (err) 19216422398cSVlad Yasevich IP6_INC_STATS(net, rt->rt6i_idev, 19226422398cSVlad Yasevich IPSTATS_MIB_OUTDISCARDS); 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds return err; 19266422398cSVlad Yasevich } 19276422398cSVlad Yasevich 19286422398cSVlad Yasevich int ip6_push_pending_frames(struct sock *sk) 19296422398cSVlad Yasevich { 19306422398cSVlad Yasevich struct sk_buff *skb; 19316422398cSVlad Yasevich 19326422398cSVlad Yasevich skb = ip6_finish_skb(sk); 19336422398cSVlad Yasevich if (!skb) 19346422398cSVlad Yasevich return 0; 19356422398cSVlad Yasevich 19366422398cSVlad Yasevich return ip6_send_skb(skb); 19371da177e4SLinus Torvalds } 1938a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames); 19391da177e4SLinus Torvalds 19400bbe84a6SVlad Yasevich static void __ip6_flush_pending_frames(struct sock *sk, 19416422398cSVlad Yasevich struct sk_buff_head *queue, 19426422398cSVlad Yasevich struct inet_cork_full *cork, 19436422398cSVlad Yasevich struct inet6_cork *v6_cork) 19441da177e4SLinus Torvalds { 19451da177e4SLinus Torvalds struct sk_buff *skb; 19461da177e4SLinus Torvalds 19470bbe84a6SVlad Yasevich while ((skb = __skb_dequeue_tail(queue)) != NULL) { 1948adf30907SEric Dumazet if (skb_dst(skb)) 1949adf30907SEric Dumazet IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)), 1950a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_OUTDISCARDS); 19511da177e4SLinus Torvalds kfree_skb(skb); 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19546422398cSVlad Yasevich ip6_cork_release(cork, v6_cork); 19551da177e4SLinus Torvalds } 19560bbe84a6SVlad Yasevich 19570bbe84a6SVlad Yasevich void ip6_flush_pending_frames(struct sock *sk) 19580bbe84a6SVlad Yasevich { 19596422398cSVlad Yasevich __ip6_flush_pending_frames(sk, &sk->sk_write_queue, 19606422398cSVlad Yasevich &inet_sk(sk)->cork, &inet6_sk(sk)->cork); 19610bbe84a6SVlad Yasevich } 1962a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames); 19636422398cSVlad Yasevich 19646422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk, 19656422398cSVlad Yasevich int getfrag(void *from, char *to, int offset, 19666422398cSVlad Yasevich int len, int odd, struct sk_buff *skb), 19676422398cSVlad Yasevich void *from, int length, int transhdrlen, 196826879da5SWei Wang struct ipcm6_cookie *ipc6, struct flowi6 *fl6, 19696422398cSVlad Yasevich struct rt6_info *rt, unsigned int flags, 19705fdaa88dSWillem de Bruijn struct inet_cork_full *cork) 19716422398cSVlad Yasevich { 19726422398cSVlad Yasevich struct inet6_cork v6_cork; 19736422398cSVlad Yasevich struct sk_buff_head queue; 197426879da5SWei Wang int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0); 19756422398cSVlad Yasevich int err; 19766422398cSVlad Yasevich 19776422398cSVlad Yasevich if (flags & MSG_PROBE) 19786422398cSVlad Yasevich return NULL; 19796422398cSVlad Yasevich 19806422398cSVlad Yasevich __skb_queue_head_init(&queue); 19816422398cSVlad Yasevich 19821cd7884dSWillem de Bruijn cork->base.flags = 0; 19831cd7884dSWillem de Bruijn cork->base.addr = 0; 19841cd7884dSWillem de Bruijn cork->base.opt = NULL; 19851cd7884dSWillem de Bruijn cork->base.dst = NULL; 19866422398cSVlad Yasevich v6_cork.opt = NULL; 19875fdaa88dSWillem de Bruijn err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt, fl6); 1988862c03eeSEric Dumazet if (err) { 19891cd7884dSWillem de Bruijn ip6_cork_release(cork, &v6_cork); 19906422398cSVlad Yasevich return ERR_PTR(err); 1991862c03eeSEric Dumazet } 199226879da5SWei Wang if (ipc6->dontfrag < 0) 199326879da5SWei Wang ipc6->dontfrag = inet6_sk(sk)->dontfrag; 19946422398cSVlad Yasevich 19951cd7884dSWillem de Bruijn err = __ip6_append_data(sk, fl6, &queue, &cork->base, &v6_cork, 19966422398cSVlad Yasevich ¤t->task_frag, getfrag, from, 19976422398cSVlad Yasevich length + exthdrlen, transhdrlen + exthdrlen, 19985fdaa88dSWillem de Bruijn flags, ipc6); 19996422398cSVlad Yasevich if (err) { 20001cd7884dSWillem de Bruijn __ip6_flush_pending_frames(sk, &queue, cork, &v6_cork); 20016422398cSVlad Yasevich return ERR_PTR(err); 20026422398cSVlad Yasevich } 20036422398cSVlad Yasevich 20041cd7884dSWillem de Bruijn return __ip6_make_skb(sk, &queue, cork, &v6_cork); 20056422398cSVlad Yasevich } 2006