xref: /openbmc/linux/net/ipv6/ip6_output.c (revision 427faee167bce4ccb0b22f6815a9ac509e31d4c3)
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;
635796015fSVasily Averin 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
645796015fSVasily Averin 	int delta = hh_len - skb_headroom(skb);
659b1c1ef1SNicolas Dichtel 	const struct in6_addr *nexthop;
66f6b72b62SDavid S. Miller 	struct neighbour *neigh;
676fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	int ret;
681da177e4SLinus Torvalds 
695796015fSVasily Averin 	/* Be paranoid, rather than too clever. */
705796015fSVasily Averin 	if (unlikely(delta > 0) && dev->header_ops) {
715796015fSVasily Averin 		/* pskb_expand_head() might crash, if skb is shared */
725796015fSVasily Averin 		if (skb_shared(skb)) {
735796015fSVasily Averin 			struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
745796015fSVasily Averin 
755796015fSVasily Averin 			if (likely(nskb)) {
765796015fSVasily Averin 				if (skb->sk)
775796015fSVasily Averin 					skb_set_owner_w(skb, skb->sk);
785796015fSVasily Averin 				consume_skb(skb);
795796015fSVasily Averin 			} else {
805796015fSVasily Averin 				kfree_skb(skb);
815796015fSVasily Averin 			}
825796015fSVasily Averin 			skb = nskb;
835796015fSVasily Averin 		}
845796015fSVasily Averin 		if (skb &&
855796015fSVasily Averin 		    pskb_expand_head(skb, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
865796015fSVasily Averin 			kfree_skb(skb);
875796015fSVasily Averin 			skb = NULL;
885796015fSVasily Averin 		}
895796015fSVasily Averin 		if (!skb) {
905796015fSVasily Averin 			IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
915796015fSVasily Averin 			return -ENOMEM;
925796015fSVasily Averin 		}
935796015fSVasily Averin 	}
945796015fSVasily Averin 
950660e03fSArnaldo Carvalho de Melo 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
96adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
971da177e4SLinus Torvalds 
987026b1ddSDavid Miller 		if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
998571ab47SYuval Mintz 		    ((mroute6_is_socket(net, skb) &&
100bd91b8bfSBenjamin Thery 		     !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
1010660e03fSArnaldo Carvalho de Melo 		     ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
1027bc570c8SYOSHIFUJI Hideaki 					 &ipv6_hdr(skb)->saddr))) {
1031da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 			/* Do not check for IFF_ALLMULTI; multicast routing
1061da177e4SLinus Torvalds 			   is not supported in any case.
1071da177e4SLinus Torvalds 			 */
1081da177e4SLinus Torvalds 			if (newskb)
109b2e0b385SJan Engelhardt 				NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
11029a26a56SEric W. Biederman 					net, sk, newskb, NULL, newskb->dev,
11195603e22SMichel Machado 					dev_loopback_xmit);
1121da177e4SLinus Torvalds 
1130660e03fSArnaldo Carvalho de Melo 			if (ipv6_hdr(skb)->hop_limit == 0) {
11478126c41SEric W. Biederman 				IP6_INC_STATS(net, idev,
1153bd653c8SDenis V. Lunev 					      IPSTATS_MIB_OUTDISCARDS);
1161da177e4SLinus Torvalds 				kfree_skb(skb);
1171da177e4SLinus Torvalds 				return 0;
1181da177e4SLinus Torvalds 			}
1191da177e4SLinus Torvalds 		}
1201da177e4SLinus Torvalds 
12178126c41SEric W. Biederman 		IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
122dd408515SHannes Frederic Sowa 
123dd408515SHannes Frederic Sowa 		if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
124dd408515SHannes Frederic Sowa 		    IPV6_ADDR_SCOPE_NODELOCAL &&
125dd408515SHannes Frederic Sowa 		    !(dev->flags & IFF_LOOPBACK)) {
126dd408515SHannes Frederic Sowa 			kfree_skb(skb);
127dd408515SHannes Frederic Sowa 			return 0;
128dd408515SHannes Frederic Sowa 		}
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
13114972cbdSRoopa Prabhu 	if (lwtunnel_xmit_redirect(dst->lwtstate)) {
13214972cbdSRoopa Prabhu 		int res = lwtunnel_xmit(skb);
13314972cbdSRoopa Prabhu 
13414972cbdSRoopa Prabhu 		if (res < 0 || res == LWTUNNEL_XMIT_DONE)
13514972cbdSRoopa Prabhu 			return res;
13614972cbdSRoopa Prabhu 	}
13714972cbdSRoopa Prabhu 
1386fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
1392647a9b0SMartin KaFai Lau 	nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
1406fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
1416fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	if (unlikely(!neigh))
1426fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
1436fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	if (!IS_ERR(neigh)) {
1444ff06203SJulian Anastasov 		sock_confirm_neigh(skb, neigh);
1450353f282SDavid Ahern 		ret = neigh_output(neigh, skb, false);
1466fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		rcu_read_unlock_bh();
1476fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		return ret;
1486fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	}
1496fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
15005e3aa09SDavid S. Miller 
15178126c41SEric W. Biederman 	IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
1529e508490SJan Engelhardt 	kfree_skb(skb);
1539e508490SJan Engelhardt 	return -EINVAL;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
156b210de4fSAya Levin static int
157b210de4fSAya Levin ip6_finish_output_gso_slowpath_drop(struct net *net, struct sock *sk,
158b210de4fSAya Levin 				    struct sk_buff *skb, unsigned int mtu)
159b210de4fSAya Levin {
160b210de4fSAya Levin 	struct sk_buff *segs, *nskb;
161b210de4fSAya Levin 	netdev_features_t features;
162b210de4fSAya Levin 	int ret = 0;
163b210de4fSAya Levin 
164b210de4fSAya Levin 	/* Please see corresponding comment in ip_finish_output_gso
165b210de4fSAya Levin 	 * describing the cases where GSO segment length exceeds the
166b210de4fSAya Levin 	 * egress MTU.
167b210de4fSAya Levin 	 */
168b210de4fSAya Levin 	features = netif_skb_features(skb);
169b210de4fSAya Levin 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
170b210de4fSAya Levin 	if (IS_ERR_OR_NULL(segs)) {
171b210de4fSAya Levin 		kfree_skb(skb);
172b210de4fSAya Levin 		return -ENOMEM;
173b210de4fSAya Levin 	}
174b210de4fSAya Levin 
175b210de4fSAya Levin 	consume_skb(skb);
176b210de4fSAya Levin 
177b210de4fSAya Levin 	skb_list_walk_safe(segs, segs, nskb) {
178b210de4fSAya Levin 		int err;
179b210de4fSAya Levin 
180b210de4fSAya Levin 		skb_mark_not_on_list(segs);
181b210de4fSAya Levin 		err = ip6_fragment(net, sk, segs, ip6_finish_output2);
182b210de4fSAya Levin 		if (err && ret == 0)
183b210de4fSAya Levin 			ret = err;
184b210de4fSAya Levin 	}
185b210de4fSAya Levin 
186b210de4fSAya Levin 	return ret;
187b210de4fSAya Levin }
188b210de4fSAya Levin 
189956fe219Sbrakmo static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1909e508490SJan Engelhardt {
191b210de4fSAya Levin 	unsigned int mtu;
192b210de4fSAya Levin 
19309ee9dbaSTobias Brunner #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
19409ee9dbaSTobias Brunner 	/* Policy lookup after SNAT yielded a new policy */
19509ee9dbaSTobias Brunner 	if (skb_dst(skb)->xfrm) {
19609ee9dbaSTobias Brunner 		IPCB(skb)->flags |= IPSKB_REROUTED;
19709ee9dbaSTobias Brunner 		return dst_output(net, sk, skb);
19809ee9dbaSTobias Brunner 	}
19909ee9dbaSTobias Brunner #endif
20009ee9dbaSTobias Brunner 
201b210de4fSAya Levin 	mtu = ip6_skb_dst_mtu(skb);
202b210de4fSAya Levin 	if (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu))
203b210de4fSAya Levin 		return ip6_finish_output_gso_slowpath_drop(net, sk, skb, mtu);
204b210de4fSAya Levin 
205b210de4fSAya Levin 	if ((skb->len > mtu && !skb_is_gso(skb)) ||
2069037c357SJiri Pirko 	    dst_allfrag(skb_dst(skb)) ||
2079037c357SJiri Pirko 	    (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
2087d8c6e39SEric W. Biederman 		return ip6_fragment(net, sk, skb, ip6_finish_output2);
2099e508490SJan Engelhardt 	else
2107d8c6e39SEric W. Biederman 		return ip6_finish_output2(net, sk, skb);
2119e508490SJan Engelhardt }
2129e508490SJan Engelhardt 
213956fe219Sbrakmo static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
214956fe219Sbrakmo {
215956fe219Sbrakmo 	int ret;
216956fe219Sbrakmo 
217956fe219Sbrakmo 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
218956fe219Sbrakmo 	switch (ret) {
219956fe219Sbrakmo 	case NET_XMIT_SUCCESS:
220956fe219Sbrakmo 		return __ip6_finish_output(net, sk, skb);
221956fe219Sbrakmo 	case NET_XMIT_CN:
222956fe219Sbrakmo 		return __ip6_finish_output(net, sk, skb) ? : ret;
223956fe219Sbrakmo 	default:
224956fe219Sbrakmo 		kfree_skb(skb);
225956fe219Sbrakmo 		return ret;
226956fe219Sbrakmo 	}
227956fe219Sbrakmo }
228956fe219Sbrakmo 
229ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
2301da177e4SLinus Torvalds {
23128f8bfd1SPhil Sutter 	struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev;
232adf30907SEric Dumazet 	struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
233be10de0aSEric W. Biederman 
23497a7a37aSChenbo Feng 	skb->protocol = htons(ETH_P_IPV6);
23597a7a37aSChenbo Feng 	skb->dev = dev;
23697a7a37aSChenbo Feng 
237778d80beSYOSHIFUJI Hideaki 	if (unlikely(idev->cnf.disable_ipv6)) {
23819a0644cSEric W. Biederman 		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
239778d80beSYOSHIFUJI Hideaki 		kfree_skb(skb);
240778d80beSYOSHIFUJI Hideaki 		return 0;
241778d80beSYOSHIFUJI Hideaki 	}
242778d80beSYOSHIFUJI Hideaki 
24329a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
24428f8bfd1SPhil Sutter 			    net, sk, skb, indev, dev,
2459c6eb28aSJan Engelhardt 			    ip6_finish_output,
2469c6eb28aSJan Engelhardt 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
2471da177e4SLinus Torvalds }
2486585d7dcSBrian Vazquez EXPORT_SYMBOL(ip6_output);
2491da177e4SLinus Torvalds 
250e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np)
251513674b5SShaohua Li {
252513674b5SShaohua Li 	if (!np->autoflowlabel_set)
253513674b5SShaohua Li 		return ip6_default_np_autolabel(net);
254513674b5SShaohua Li 	else
255513674b5SShaohua Li 		return np->autoflowlabel;
256513674b5SShaohua Li }
257513674b5SShaohua Li 
2581da177e4SLinus Torvalds /*
259b5d43998SShan Wei  * xmit an sk_buff (used by TCP, SCTP and DCCP)
2601c1e9d2bSEric Dumazet  * Note : socket lock is not held for SYNACK packets, but might be modified
2611c1e9d2bSEric Dumazet  * by calls to skb_set_owner_w() and ipv6_local_error(),
2621c1e9d2bSEric Dumazet  * which are using proper atomic operations or spinlocks.
2631da177e4SLinus Torvalds  */
2641c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
2654f6570d7SEric Dumazet 	     __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority)
2661da177e4SLinus Torvalds {
2673bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
2681c1e9d2bSEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
2694c9483b2SDavid S. Miller 	struct in6_addr *first_hop = &fl6->daddr;
270adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
27166033f47SStefano Brivio 	unsigned int head_room;
2721da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
2734c9483b2SDavid S. Miller 	u8  proto = fl6->flowi6_proto;
2741da177e4SLinus Torvalds 	int seg_len = skb->len;
275e651f03aSGerrit Renker 	int hlimit = -1;
2761da177e4SLinus Torvalds 	u32 mtu;
2771da177e4SLinus Torvalds 
27866033f47SStefano Brivio 	head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
27966033f47SStefano Brivio 	if (opt)
28066033f47SStefano Brivio 		head_room += opt->opt_nflen + opt->opt_flen;
2811da177e4SLinus Torvalds 
28266033f47SStefano Brivio 	if (unlikely(skb_headroom(skb) < head_room)) {
2831da177e4SLinus Torvalds 		struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
28463159f29SIan Morris 		if (!skb2) {
285adf30907SEric Dumazet 			IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
286a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_OUTDISCARDS);
2871da177e4SLinus Torvalds 			kfree_skb(skb);
2881da177e4SLinus Torvalds 			return -ENOBUFS;
2891da177e4SLinus Torvalds 		}
290bbd6528dSEric Dumazet 		if (skb->sk)
291bbd6528dSEric Dumazet 			skb_set_owner_w(skb2, skb->sk);
292808db80aSEric Dumazet 		consume_skb(skb);
293a11d206dSYOSHIFUJI Hideaki 		skb = skb2;
2941da177e4SLinus Torvalds 	}
29566033f47SStefano Brivio 
29666033f47SStefano Brivio 	if (opt) {
29766033f47SStefano Brivio 		seg_len += opt->opt_nflen + opt->opt_flen;
29866033f47SStefano Brivio 
2991da177e4SLinus Torvalds 		if (opt->opt_flen)
3001da177e4SLinus Torvalds 			ipv6_push_frag_opts(skb, opt, &proto);
30166033f47SStefano Brivio 
3021da177e4SLinus Torvalds 		if (opt->opt_nflen)
303613fa3caSDavid Lebrun 			ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop,
304613fa3caSDavid Lebrun 					     &fl6->saddr);
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
307e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
308e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
3090660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 *	Fill in the IPv6 header
3131da177e4SLinus Torvalds 	 */
314b903d324SEric Dumazet 	if (np)
3151da177e4SLinus Torvalds 		hlimit = np->hop_limit;
3161da177e4SLinus Torvalds 	if (hlimit < 0)
3176b75d090SYOSHIFUJI Hideaki 		hlimit = ip6_dst_hoplimit(dst);
3181da177e4SLinus Torvalds 
319cb1ce2efSTom Herbert 	ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
320513674b5SShaohua Li 				ip6_autoflowlabel(net, np), fl6));
32141a1f8eaSYOSHIFUJI Hideaki 
3221da177e4SLinus Torvalds 	hdr->payload_len = htons(seg_len);
3231da177e4SLinus Torvalds 	hdr->nexthdr = proto;
3241da177e4SLinus Torvalds 	hdr->hop_limit = hlimit;
3251da177e4SLinus Torvalds 
3264e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
3274e3fd7a0SAlexey Dobriyan 	hdr->daddr = *first_hop;
3281da177e4SLinus Torvalds 
3299c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
3304f6570d7SEric Dumazet 	skb->priority = priority;
33192e55f41SPablo Neira 	skb->mark = mark;
332a2c2064fSPatrick McHardy 
3331da177e4SLinus Torvalds 	mtu = dst_mtu(dst);
33460ff7467SWANG Cong 	if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
335adf30907SEric Dumazet 		IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
336edf391ffSNeil Horman 			      IPSTATS_MIB_OUT, skb->len);
337a8e3e1a9SDavid Ahern 
338a8e3e1a9SDavid Ahern 		/* if egress device is enslaved to an L3 master device pass the
339a8e3e1a9SDavid Ahern 		 * skb to its handler for processing
340a8e3e1a9SDavid Ahern 		 */
341a8e3e1a9SDavid Ahern 		skb = l3mdev_ip6_out((struct sock *)sk, skb);
342a8e3e1a9SDavid Ahern 		if (unlikely(!skb))
343a8e3e1a9SDavid Ahern 			return 0;
344a8e3e1a9SDavid Ahern 
3451c1e9d2bSEric Dumazet 		/* hooks should never assume socket lock is held.
3461c1e9d2bSEric Dumazet 		 * we promote our socket to non const
3471c1e9d2bSEric Dumazet 		 */
34829a26a56SEric W. Biederman 		return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
3491c1e9d2bSEric Dumazet 			       net, (struct sock *)sk, skb, NULL, dst->dev,
35013206b6bSEric W. Biederman 			       dst_output);
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	skb->dev = dst->dev;
3541c1e9d2bSEric Dumazet 	/* ipv6_local_error() does not require socket lock,
3551c1e9d2bSEric Dumazet 	 * we promote our socket to non const
3561c1e9d2bSEric Dumazet 	 */
3571c1e9d2bSEric Dumazet 	ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
3581c1e9d2bSEric Dumazet 
359adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
3601da177e4SLinus Torvalds 	kfree_skb(skb);
3611da177e4SLinus Torvalds 	return -EMSGSIZE;
3621da177e4SLinus Torvalds }
3637159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit);
3647159039aSYOSHIFUJI Hideaki 
3651da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct ip6_ra_chain *ra;
3681da177e4SLinus Torvalds 	struct sock *last = NULL;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	read_lock(&ip6_ra_lock);
3711da177e4SLinus Torvalds 	for (ra = ip6_ra_chain; ra; ra = ra->next) {
3721da177e4SLinus Torvalds 		struct sock *sk = ra->sk;
3730bd1b59bSAndrew McDonald 		if (sk && ra->sel == sel &&
3740bd1b59bSAndrew McDonald 		    (!sk->sk_bound_dev_if ||
3750bd1b59bSAndrew McDonald 		     sk->sk_bound_dev_if == skb->dev->ifindex)) {
3769036b2feSFrancesco Ruggeri 			struct ipv6_pinfo *np = inet6_sk(sk);
3779036b2feSFrancesco Ruggeri 
3789036b2feSFrancesco Ruggeri 			if (np && np->rtalert_isolate &&
3799036b2feSFrancesco Ruggeri 			    !net_eq(sock_net(sk), dev_net(skb->dev))) {
3809036b2feSFrancesco Ruggeri 				continue;
3819036b2feSFrancesco Ruggeri 			}
3821da177e4SLinus Torvalds 			if (last) {
3831da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
3841da177e4SLinus Torvalds 				if (skb2)
3851da177e4SLinus Torvalds 					rawv6_rcv(last, skb2);
3861da177e4SLinus Torvalds 			}
3871da177e4SLinus Torvalds 			last = sk;
3881da177e4SLinus Torvalds 		}
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if (last) {
3921da177e4SLinus Torvalds 		rawv6_rcv(last, skb);
3931da177e4SLinus Torvalds 		read_unlock(&ip6_ra_lock);
3941da177e4SLinus Torvalds 		return 1;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 	read_unlock(&ip6_ra_lock);
3971da177e4SLinus Torvalds 	return 0;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
400e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb)
401e21e0b5fSVille Nuorvala {
4020660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
403e21e0b5fSVille Nuorvala 	u8 nexthdr = hdr->nexthdr;
40475f2811cSJesse Gross 	__be16 frag_off;
405e21e0b5fSVille Nuorvala 	int offset;
406e21e0b5fSVille Nuorvala 
407e21e0b5fSVille Nuorvala 	if (ipv6_ext_hdr(nexthdr)) {
40875f2811cSJesse Gross 		offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
409e21e0b5fSVille Nuorvala 		if (offset < 0)
410e21e0b5fSVille Nuorvala 			return 0;
411e21e0b5fSVille Nuorvala 	} else
412e21e0b5fSVille Nuorvala 		offset = sizeof(struct ipv6hdr);
413e21e0b5fSVille Nuorvala 
414e21e0b5fSVille Nuorvala 	if (nexthdr == IPPROTO_ICMPV6) {
415e21e0b5fSVille Nuorvala 		struct icmp6hdr *icmp6;
416e21e0b5fSVille Nuorvala 
417d56f90a7SArnaldo Carvalho de Melo 		if (!pskb_may_pull(skb, (skb_network_header(skb) +
418d56f90a7SArnaldo Carvalho de Melo 					 offset + 1 - skb->data)))
419e21e0b5fSVille Nuorvala 			return 0;
420e21e0b5fSVille Nuorvala 
421d56f90a7SArnaldo Carvalho de Melo 		icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
422e21e0b5fSVille Nuorvala 
423e21e0b5fSVille Nuorvala 		switch (icmp6->icmp6_type) {
424e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_SOLICITATION:
425e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_ADVERTISEMENT:
426e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_SOLICITATION:
427e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_ADVERTISEMENT:
428e21e0b5fSVille Nuorvala 		case NDISC_REDIRECT:
429e21e0b5fSVille Nuorvala 			/* For reaction involving unicast neighbor discovery
430e21e0b5fSVille Nuorvala 			 * message destined to the proxied address, pass it to
431e21e0b5fSVille Nuorvala 			 * input function.
432e21e0b5fSVille Nuorvala 			 */
433e21e0b5fSVille Nuorvala 			return 1;
434e21e0b5fSVille Nuorvala 		default:
435e21e0b5fSVille Nuorvala 			break;
436e21e0b5fSVille Nuorvala 		}
437e21e0b5fSVille Nuorvala 	}
438e21e0b5fSVille Nuorvala 
43974553b09SVille Nuorvala 	/*
44074553b09SVille Nuorvala 	 * The proxying router can't forward traffic sent to a link-local
44174553b09SVille Nuorvala 	 * address, so signal the sender and discard the packet. This
44274553b09SVille Nuorvala 	 * behavior is clarified by the MIPv6 specification.
44374553b09SVille Nuorvala 	 */
44474553b09SVille Nuorvala 	if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
44574553b09SVille Nuorvala 		dst_link_failure(skb);
44674553b09SVille Nuorvala 		return -1;
44774553b09SVille Nuorvala 	}
44874553b09SVille Nuorvala 
449e21e0b5fSVille Nuorvala 	return 0;
450e21e0b5fSVille Nuorvala }
451e21e0b5fSVille Nuorvala 
4520c4b51f0SEric W. Biederman static inline int ip6_forward_finish(struct net *net, struct sock *sk,
4530c4b51f0SEric W. Biederman 				     struct sk_buff *skb)
4541da177e4SLinus Torvalds {
45571a1c915SJeff Barnhill 	struct dst_entry *dst = skb_dst(skb);
45671a1c915SJeff Barnhill 
45771a1c915SJeff Barnhill 	__IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
45871a1c915SJeff Barnhill 	__IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
45971a1c915SJeff Barnhill 
460f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV
461f839a6c9SIdo Schimmel 	if (skb->offload_l3_fwd_mark) {
462f839a6c9SIdo Schimmel 		consume_skb(skb);
463f839a6c9SIdo Schimmel 		return 0;
464f839a6c9SIdo Schimmel 	}
465f839a6c9SIdo Schimmel #endif
466f839a6c9SIdo Schimmel 
4678203e2d8SEric Dumazet 	skb->tstamp = 0;
46813206b6bSEric W. Biederman 	return dst_output(net, sk, skb);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
471fe6cc55fSFlorian Westphal static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
472fe6cc55fSFlorian Westphal {
473418a3156SFlorian Westphal 	if (skb->len <= mtu)
474fe6cc55fSFlorian Westphal 		return false;
475fe6cc55fSFlorian Westphal 
47660ff7467SWANG Cong 	/* ipv6 conntrack defrag sets max_frag_size + ignore_df */
477fe6cc55fSFlorian Westphal 	if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
478fe6cc55fSFlorian Westphal 		return true;
479fe6cc55fSFlorian Westphal 
48060ff7467SWANG Cong 	if (skb->ignore_df)
481418a3156SFlorian Westphal 		return false;
482418a3156SFlorian Westphal 
483779b7931SDaniel Axtens 	if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
484fe6cc55fSFlorian Westphal 		return false;
485fe6cc55fSFlorian Westphal 
486fe6cc55fSFlorian Westphal 	return true;
487fe6cc55fSFlorian Westphal }
488fe6cc55fSFlorian Westphal 
4891da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb)
4901da177e4SLinus Torvalds {
491bdb7cc64SStephen Suryaputra 	struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
492adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4930660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
4941da177e4SLinus Torvalds 	struct inet6_skb_parm *opt = IP6CB(skb);
495c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dst->dev);
49614f3ad6fSUlrich Weber 	u32 mtu;
4971da177e4SLinus Torvalds 
49853b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->forwarding == 0)
4991da177e4SLinus Torvalds 		goto error;
5001da177e4SLinus Torvalds 
501090f1166SLi RongQing 	if (skb->pkt_type != PACKET_HOST)
502090f1166SLi RongQing 		goto drop;
503090f1166SLi RongQing 
5049ef2e965SHannes Frederic Sowa 	if (unlikely(skb->sk))
5059ef2e965SHannes Frederic Sowa 		goto drop;
5069ef2e965SHannes Frederic Sowa 
5074497b076SBen Hutchings 	if (skb_warn_if_lro(skb))
5084497b076SBen Hutchings 		goto drop;
5094497b076SBen Hutchings 
510ccd27f05SNicolas Dichtel 	if (!net->ipv6.devconf_all->disable_policy &&
511ccd27f05SNicolas Dichtel 	    !idev->cnf.disable_policy &&
512ccd27f05SNicolas Dichtel 	    !xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
513bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
5141da177e4SLinus Torvalds 		goto drop;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
51735fc92a9SHerbert Xu 	skb_forward_csum(skb);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/*
5201da177e4SLinus Torvalds 	 *	We DO NOT make any processing on
5211da177e4SLinus Torvalds 	 *	RA packets, pushing them to user level AS IS
5221da177e4SLinus Torvalds 	 *	without ane WARRANTY that application will be able
5231da177e4SLinus Torvalds 	 *	to interpret them. The reason is that we
5241da177e4SLinus Torvalds 	 *	cannot make anything clever here.
5251da177e4SLinus Torvalds 	 *
5261da177e4SLinus Torvalds 	 *	We are not end-node, so that if packet contains
5271da177e4SLinus Torvalds 	 *	AH/ESP, we cannot make anything.
5281da177e4SLinus Torvalds 	 *	Defragmentation also would be mistake, RA packets
5291da177e4SLinus Torvalds 	 *	cannot be fragmented, because there is no warranty
5301da177e4SLinus Torvalds 	 *	that different fragments will go along one path. --ANK
5311da177e4SLinus Torvalds 	 */
532ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 	if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
533ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 		if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
5341da177e4SLinus Torvalds 			return 0;
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	/*
5381da177e4SLinus Torvalds 	 *	check and decrement ttl
5391da177e4SLinus Torvalds 	 */
5401da177e4SLinus Torvalds 	if (hdr->hop_limit <= 1) {
5413ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
542bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		kfree_skb(skb);
5451da177e4SLinus Torvalds 		return -ETIMEDOUT;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 
548fbea49e1SYOSHIFUJI Hideaki 	/* XXX: idev->cnf.proxy_ndp? */
54953b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->proxy_ndp &&
5508a3edd80SDaniel Lezcano 	    pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
55174553b09SVille Nuorvala 		int proxied = ip6_forward_proxy_check(skb);
55274553b09SVille Nuorvala 		if (proxied > 0)
553e21e0b5fSVille Nuorvala 			return ip6_input(skb);
55474553b09SVille Nuorvala 		else if (proxied < 0) {
555bdb7cc64SStephen Suryaputra 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
55674553b09SVille Nuorvala 			goto drop;
55774553b09SVille Nuorvala 		}
558e21e0b5fSVille Nuorvala 	}
559e21e0b5fSVille Nuorvala 
5601da177e4SLinus Torvalds 	if (!xfrm6_route_forward(skb)) {
561bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
5621da177e4SLinus Torvalds 		goto drop;
5631da177e4SLinus Torvalds 	}
564adf30907SEric Dumazet 	dst = skb_dst(skb);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* IPv6 specs say nothing about it, but it is clear that we cannot
5671da177e4SLinus Torvalds 	   send redirects to source routed frames.
5681e5dc146SMasahide NAKAMURA 	   We don't send redirects to frames decapsulated from IPsec.
5691da177e4SLinus Torvalds 	 */
5702f17becfSStephen Suryaputra 	if (IP6CB(skb)->iif == dst->dev->ifindex &&
5712f17becfSStephen Suryaputra 	    opt->srcrt == 0 && !skb_sec_path(skb)) {
5721da177e4SLinus Torvalds 		struct in6_addr *target = NULL;
573fbfe95a4SDavid S. Miller 		struct inet_peer *peer;
5741da177e4SLinus Torvalds 		struct rt6_info *rt;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 		/*
5771da177e4SLinus Torvalds 		 *	incoming and outgoing devices are the same
5781da177e4SLinus Torvalds 		 *	send a redirect.
5791da177e4SLinus Torvalds 		 */
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 		rt = (struct rt6_info *) dst;
582c45a3dfbSDavid S. Miller 		if (rt->rt6i_flags & RTF_GATEWAY)
583c45a3dfbSDavid S. Miller 			target = &rt->rt6i_gateway;
5841da177e4SLinus Torvalds 		else
5851da177e4SLinus Torvalds 			target = &hdr->daddr;
5861da177e4SLinus Torvalds 
587fd0273d7SMartin KaFai Lau 		peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1);
58892d86829SDavid S. Miller 
5891da177e4SLinus Torvalds 		/* Limit redirects both by destination (here)
5901da177e4SLinus Torvalds 		   and by source (inside ndisc_send_redirect)
5911da177e4SLinus Torvalds 		 */
592fbfe95a4SDavid S. Miller 		if (inet_peer_xrlim_allow(peer, 1*HZ))
5934991969aSDavid S. Miller 			ndisc_send_redirect(skb, target);
5941d861aa4SDavid S. Miller 		if (peer)
5951d861aa4SDavid S. Miller 			inet_putpeer(peer);
5965bb1ab09SDavid L Stevens 	} else {
5975bb1ab09SDavid L Stevens 		int addrtype = ipv6_addr_type(&hdr->saddr);
5985bb1ab09SDavid L Stevens 
5991da177e4SLinus Torvalds 		/* This check is security critical. */
600f81b2e7dSYOSHIFUJI Hideaki 		if (addrtype == IPV6_ADDR_ANY ||
601f81b2e7dSYOSHIFUJI Hideaki 		    addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
6021da177e4SLinus Torvalds 			goto error;
6035bb1ab09SDavid L Stevens 		if (addrtype & IPV6_ADDR_LINKLOCAL) {
6045bb1ab09SDavid L Stevens 			icmpv6_send(skb, ICMPV6_DEST_UNREACH,
6053ffe533cSAlexey Dobriyan 				    ICMPV6_NOT_NEIGHBOUR, 0);
6065bb1ab09SDavid L Stevens 			goto error;
6075bb1ab09SDavid L Stevens 		}
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
610*427faee1SVadim Fedorenko 	mtu = ip6_dst_mtu_maybe_forward(dst, true);
61114f3ad6fSUlrich Weber 	if (mtu < IPV6_MIN_MTU)
61214f3ad6fSUlrich Weber 		mtu = IPV6_MIN_MTU;
61314f3ad6fSUlrich Weber 
614fe6cc55fSFlorian Westphal 	if (ip6_pkt_too_big(skb, mtu)) {
6151da177e4SLinus Torvalds 		/* Again, force OUTPUT device used as source address */
6161da177e4SLinus Torvalds 		skb->dev = dst->dev;
61714f3ad6fSUlrich Weber 		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
618bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS);
6191d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(dst),
62015c77d8bSEric Dumazet 				IPSTATS_MIB_FRAGFAILS);
6211da177e4SLinus Torvalds 		kfree_skb(skb);
6221da177e4SLinus Torvalds 		return -EMSGSIZE;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (skb_cow(skb, dst->dev->hard_header_len)) {
6261d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(dst),
62715c77d8bSEric Dumazet 				IPSTATS_MIB_OUTDISCARDS);
6281da177e4SLinus Torvalds 		goto drop;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 
6310660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	/* Mangling hops number delayed to point after skb COW */
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	hdr->hop_limit--;
6361da177e4SLinus Torvalds 
63729a26a56SEric W. Biederman 	return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
63829a26a56SEric W. Biederman 		       net, NULL, skb, skb->dev, dst->dev,
6396e23ae2aSPatrick McHardy 		       ip6_forward_finish);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds error:
642bdb7cc64SStephen Suryaputra 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
6431da177e4SLinus Torvalds drop:
6441da177e4SLinus Torvalds 	kfree_skb(skb);
6451da177e4SLinus Torvalds 	return -EINVAL;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
6511da177e4SLinus Torvalds 	to->priority = from->priority;
6521da177e4SLinus Torvalds 	to->protocol = from->protocol;
653adf30907SEric Dumazet 	skb_dst_drop(to);
654adf30907SEric Dumazet 	skb_dst_set(to, dst_clone(skb_dst(from)));
6551da177e4SLinus Torvalds 	to->dev = from->dev;
65682e91ffeSThomas Graf 	to->mark = from->mark;
6571da177e4SLinus Torvalds 
6583dd1c9a1SPaolo Abeni 	skb_copy_hash(to, from);
6593dd1c9a1SPaolo Abeni 
6601da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
6611da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
6621da177e4SLinus Torvalds #endif
663e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
664df5042f4SFlorian Westphal 	skb_ext_copy(to, from);
665984bc16cSJames Morris 	skb_copy_secmark(to, from);
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
6680feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
6690feca619SPablo Neira Ayuso 		      u8 nexthdr, __be32 frag_id,
6700feca619SPablo Neira Ayuso 		      struct ip6_fraglist_iter *iter)
6710feca619SPablo Neira Ayuso {
6720feca619SPablo Neira Ayuso 	unsigned int first_len;
6730feca619SPablo Neira Ayuso 	struct frag_hdr *fh;
6740feca619SPablo Neira Ayuso 
6750feca619SPablo Neira Ayuso 	/* BUILD HEADER */
6760feca619SPablo Neira Ayuso 	*prevhdr = NEXTHDR_FRAGMENT;
6770feca619SPablo Neira Ayuso 	iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
6780feca619SPablo Neira Ayuso 	if (!iter->tmp_hdr)
6790feca619SPablo Neira Ayuso 		return -ENOMEM;
6800feca619SPablo Neira Ayuso 
681b7034146SEric Dumazet 	iter->frag = skb_shinfo(skb)->frag_list;
6820feca619SPablo Neira Ayuso 	skb_frag_list_init(skb);
6830feca619SPablo Neira Ayuso 
6840feca619SPablo Neira Ayuso 	iter->offset = 0;
6850feca619SPablo Neira Ayuso 	iter->hlen = hlen;
6860feca619SPablo Neira Ayuso 	iter->frag_id = frag_id;
6870feca619SPablo Neira Ayuso 	iter->nexthdr = nexthdr;
6880feca619SPablo Neira Ayuso 
6890feca619SPablo Neira Ayuso 	__skb_pull(skb, hlen);
6900feca619SPablo Neira Ayuso 	fh = __skb_push(skb, sizeof(struct frag_hdr));
6910feca619SPablo Neira Ayuso 	__skb_push(skb, hlen);
6920feca619SPablo Neira Ayuso 	skb_reset_network_header(skb);
6930feca619SPablo Neira Ayuso 	memcpy(skb_network_header(skb), iter->tmp_hdr, hlen);
6940feca619SPablo Neira Ayuso 
6950feca619SPablo Neira Ayuso 	fh->nexthdr = nexthdr;
6960feca619SPablo Neira Ayuso 	fh->reserved = 0;
6970feca619SPablo Neira Ayuso 	fh->frag_off = htons(IP6_MF);
6980feca619SPablo Neira Ayuso 	fh->identification = frag_id;
6990feca619SPablo Neira Ayuso 
7000feca619SPablo Neira Ayuso 	first_len = skb_pagelen(skb);
7010feca619SPablo Neira Ayuso 	skb->data_len = first_len - skb_headlen(skb);
7020feca619SPablo Neira Ayuso 	skb->len = first_len;
7030feca619SPablo Neira Ayuso 	ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr));
7040feca619SPablo Neira Ayuso 
7050feca619SPablo Neira Ayuso 	return 0;
7060feca619SPablo Neira Ayuso }
7070feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_init);
7080feca619SPablo Neira Ayuso 
7090feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb,
7100feca619SPablo Neira Ayuso 			  struct ip6_fraglist_iter *iter)
7110feca619SPablo Neira Ayuso {
7120feca619SPablo Neira Ayuso 	struct sk_buff *frag = iter->frag;
7130feca619SPablo Neira Ayuso 	unsigned int hlen = iter->hlen;
7140feca619SPablo Neira Ayuso 	struct frag_hdr *fh;
7150feca619SPablo Neira Ayuso 
7160feca619SPablo Neira Ayuso 	frag->ip_summed = CHECKSUM_NONE;
7170feca619SPablo Neira Ayuso 	skb_reset_transport_header(frag);
7180feca619SPablo Neira Ayuso 	fh = __skb_push(frag, sizeof(struct frag_hdr));
7190feca619SPablo Neira Ayuso 	__skb_push(frag, hlen);
7200feca619SPablo Neira Ayuso 	skb_reset_network_header(frag);
7210feca619SPablo Neira Ayuso 	memcpy(skb_network_header(frag), iter->tmp_hdr, hlen);
7220feca619SPablo Neira Ayuso 	iter->offset += skb->len - hlen - sizeof(struct frag_hdr);
7230feca619SPablo Neira Ayuso 	fh->nexthdr = iter->nexthdr;
7240feca619SPablo Neira Ayuso 	fh->reserved = 0;
7250feca619SPablo Neira Ayuso 	fh->frag_off = htons(iter->offset);
7260feca619SPablo Neira Ayuso 	if (frag->next)
7270feca619SPablo Neira Ayuso 		fh->frag_off |= htons(IP6_MF);
7280feca619SPablo Neira Ayuso 	fh->identification = iter->frag_id;
7290feca619SPablo Neira Ayuso 	ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
7300feca619SPablo Neira Ayuso 	ip6_copy_metadata(frag, skb);
7310feca619SPablo Neira Ayuso }
7320feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_prepare);
7330feca619SPablo Neira Ayuso 
7348a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
7358a6a1f17SPablo Neira Ayuso 		   unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
7368a6a1f17SPablo Neira Ayuso 		   u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state)
7378a6a1f17SPablo Neira Ayuso {
7388a6a1f17SPablo Neira Ayuso 	state->prevhdr = prevhdr;
7398a6a1f17SPablo Neira Ayuso 	state->nexthdr = nexthdr;
7408a6a1f17SPablo Neira Ayuso 	state->frag_id = frag_id;
7418a6a1f17SPablo Neira Ayuso 
7428a6a1f17SPablo Neira Ayuso 	state->hlen = hlen;
7438a6a1f17SPablo Neira Ayuso 	state->mtu = mtu;
7448a6a1f17SPablo Neira Ayuso 
7458a6a1f17SPablo Neira Ayuso 	state->left = skb->len - hlen;	/* Space per frame */
7468a6a1f17SPablo Neira Ayuso 	state->ptr = hlen;		/* Where to start from */
7478a6a1f17SPablo Neira Ayuso 
7488a6a1f17SPablo Neira Ayuso 	state->hroom = hdr_room;
7498a6a1f17SPablo Neira Ayuso 	state->troom = needed_tailroom;
7508a6a1f17SPablo Neira Ayuso 
7518a6a1f17SPablo Neira Ayuso 	state->offset = 0;
7528a6a1f17SPablo Neira Ayuso }
7538a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_init);
7548a6a1f17SPablo Neira Ayuso 
7558a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state)
7568a6a1f17SPablo Neira Ayuso {
7578a6a1f17SPablo Neira Ayuso 	u8 *prevhdr = state->prevhdr, *fragnexthdr_offset;
7588a6a1f17SPablo Neira Ayuso 	struct sk_buff *frag;
7598a6a1f17SPablo Neira Ayuso 	struct frag_hdr *fh;
7608a6a1f17SPablo Neira Ayuso 	unsigned int len;
7618a6a1f17SPablo Neira Ayuso 
7628a6a1f17SPablo Neira Ayuso 	len = state->left;
7638a6a1f17SPablo Neira Ayuso 	/* IF: it doesn't fit, use 'mtu' - the data space left */
7648a6a1f17SPablo Neira Ayuso 	if (len > state->mtu)
7658a6a1f17SPablo Neira Ayuso 		len = state->mtu;
7668a6a1f17SPablo Neira Ayuso 	/* IF: we are not sending up to and including the packet end
7678a6a1f17SPablo Neira Ayuso 	   then align the next start on an eight byte boundary */
7688a6a1f17SPablo Neira Ayuso 	if (len < state->left)
7698a6a1f17SPablo Neira Ayuso 		len &= ~7;
7708a6a1f17SPablo Neira Ayuso 
7718a6a1f17SPablo Neira Ayuso 	/* Allocate buffer */
7728a6a1f17SPablo Neira Ayuso 	frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) +
7738a6a1f17SPablo Neira Ayuso 			 state->hroom + state->troom, GFP_ATOMIC);
7748a6a1f17SPablo Neira Ayuso 	if (!frag)
7758a6a1f17SPablo Neira Ayuso 		return ERR_PTR(-ENOMEM);
7768a6a1f17SPablo Neira Ayuso 
7778a6a1f17SPablo Neira Ayuso 	/*
7788a6a1f17SPablo Neira Ayuso 	 *	Set up data on packet
7798a6a1f17SPablo Neira Ayuso 	 */
7808a6a1f17SPablo Neira Ayuso 
7818a6a1f17SPablo Neira Ayuso 	ip6_copy_metadata(frag, skb);
7828a6a1f17SPablo Neira Ayuso 	skb_reserve(frag, state->hroom);
7838a6a1f17SPablo Neira Ayuso 	skb_put(frag, len + state->hlen + sizeof(struct frag_hdr));
7848a6a1f17SPablo Neira Ayuso 	skb_reset_network_header(frag);
7858a6a1f17SPablo Neira Ayuso 	fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen);
7868a6a1f17SPablo Neira Ayuso 	frag->transport_header = (frag->network_header + state->hlen +
7878a6a1f17SPablo Neira Ayuso 				  sizeof(struct frag_hdr));
7888a6a1f17SPablo Neira Ayuso 
7898a6a1f17SPablo Neira Ayuso 	/*
7908a6a1f17SPablo Neira Ayuso 	 *	Charge the memory for the fragment to any owner
7918a6a1f17SPablo Neira Ayuso 	 *	it might possess
7928a6a1f17SPablo Neira Ayuso 	 */
7938a6a1f17SPablo Neira Ayuso 	if (skb->sk)
7948a6a1f17SPablo Neira Ayuso 		skb_set_owner_w(frag, skb->sk);
7958a6a1f17SPablo Neira Ayuso 
7968a6a1f17SPablo Neira Ayuso 	/*
7978a6a1f17SPablo Neira Ayuso 	 *	Copy the packet header into the new buffer.
7988a6a1f17SPablo Neira Ayuso 	 */
7998a6a1f17SPablo Neira Ayuso 	skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen);
8008a6a1f17SPablo Neira Ayuso 
8018a6a1f17SPablo Neira Ayuso 	fragnexthdr_offset = skb_network_header(frag);
8028a6a1f17SPablo Neira Ayuso 	fragnexthdr_offset += prevhdr - skb_network_header(skb);
8038a6a1f17SPablo Neira Ayuso 	*fragnexthdr_offset = NEXTHDR_FRAGMENT;
8048a6a1f17SPablo Neira Ayuso 
8058a6a1f17SPablo Neira Ayuso 	/*
8068a6a1f17SPablo Neira Ayuso 	 *	Build fragment header.
8078a6a1f17SPablo Neira Ayuso 	 */
8088a6a1f17SPablo Neira Ayuso 	fh->nexthdr = state->nexthdr;
8098a6a1f17SPablo Neira Ayuso 	fh->reserved = 0;
8108a6a1f17SPablo Neira Ayuso 	fh->identification = state->frag_id;
8118a6a1f17SPablo Neira Ayuso 
8128a6a1f17SPablo Neira Ayuso 	/*
8138a6a1f17SPablo Neira Ayuso 	 *	Copy a block of the IP datagram.
8148a6a1f17SPablo Neira Ayuso 	 */
8158a6a1f17SPablo Neira Ayuso 	BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag),
8168a6a1f17SPablo Neira Ayuso 			     len));
8178a6a1f17SPablo Neira Ayuso 	state->left -= len;
8188a6a1f17SPablo Neira Ayuso 
8198a6a1f17SPablo Neira Ayuso 	fh->frag_off = htons(state->offset);
8208a6a1f17SPablo Neira Ayuso 	if (state->left > 0)
8218a6a1f17SPablo Neira Ayuso 		fh->frag_off |= htons(IP6_MF);
8228a6a1f17SPablo Neira Ayuso 	ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
8238a6a1f17SPablo Neira Ayuso 
8248a6a1f17SPablo Neira Ayuso 	state->ptr += len;
8258a6a1f17SPablo Neira Ayuso 	state->offset += len;
8268a6a1f17SPablo Neira Ayuso 
8278a6a1f17SPablo Neira Ayuso 	return frag;
8288a6a1f17SPablo Neira Ayuso }
8298a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_next);
8308a6a1f17SPablo Neira Ayuso 
8317d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
8327d8c6e39SEric W. Biederman 		 int (*output)(struct net *, struct sock *, struct sk_buff *))
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	struct sk_buff *frag;
835adf30907SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
836f60e5990Shannes@stressinduktion.org 	struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
837f60e5990Shannes@stressinduktion.org 				inet6_sk(skb->sk) : NULL;
8388a6a1f17SPablo Neira Ayuso 	struct ip6_frag_state state;
8398a6a1f17SPablo Neira Ayuso 	unsigned int mtu, hlen, nexthdr_offset;
8409669fffcSEric Dumazet 	ktime_t tstamp = skb->tstamp;
8418a6a1f17SPablo Neira Ayuso 	int hroom, err = 0;
842286c2349SMartin KaFai Lau 	__be32 frag_id;
8431da177e4SLinus Torvalds 	u8 *prevhdr, nexthdr = 0;
8441da177e4SLinus Torvalds 
8457dd7eb95SDavid S. Miller 	err = ip6_find_1stfragopt(skb, &prevhdr);
8467dd7eb95SDavid S. Miller 	if (err < 0)
8472423496aSCraig Gallek 		goto fail;
8487dd7eb95SDavid S. Miller 	hlen = err;
8491da177e4SLinus Torvalds 	nexthdr = *prevhdr;
850ef0efcd3SJunwei Hu 	nexthdr_offset = prevhdr - skb_network_header(skb);
8511da177e4SLinus Torvalds 
852628a5c56SJohn Heffner 	mtu = ip6_skb_dst_mtu(skb);
853b881ef76SJohn Heffner 
854b881ef76SJohn Heffner 	/* We must not fragment if the socket is set to force MTU discovery
85514f3ad6fSUlrich Weber 	 * or if the skb it not generated by a local socket.
856b881ef76SJohn Heffner 	 */
857485fca66SFlorian Westphal 	if (unlikely(!skb->ignore_df && skb->len > mtu))
858485fca66SFlorian Westphal 		goto fail_toobig;
859a34a101eSEric Dumazet 
860485fca66SFlorian Westphal 	if (IP6CB(skb)->frag_max_size) {
861485fca66SFlorian Westphal 		if (IP6CB(skb)->frag_max_size > mtu)
862485fca66SFlorian Westphal 			goto fail_toobig;
863485fca66SFlorian Westphal 
864485fca66SFlorian Westphal 		/* don't send fragments larger than what we received */
865485fca66SFlorian Westphal 		mtu = IP6CB(skb)->frag_max_size;
866485fca66SFlorian Westphal 		if (mtu < IPV6_MIN_MTU)
867485fca66SFlorian Westphal 			mtu = IPV6_MIN_MTU;
868b881ef76SJohn Heffner 	}
869b881ef76SJohn Heffner 
870d91675f9SYOSHIFUJI Hideaki 	if (np && np->frag_size < mtu) {
871d91675f9SYOSHIFUJI Hideaki 		if (np->frag_size)
872d91675f9SYOSHIFUJI Hideaki 			mtu = np->frag_size;
873d91675f9SYOSHIFUJI Hideaki 	}
87489bc7848SHannes Frederic Sowa 	if (mtu < hlen + sizeof(struct frag_hdr) + 8)
875b72a2b01SHannes Frederic Sowa 		goto fail_toobig;
8761e0d69a9SHannes Frederic Sowa 	mtu -= hlen + sizeof(struct frag_hdr);
8771da177e4SLinus Torvalds 
878fd0273d7SMartin KaFai Lau 	frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
879fd0273d7SMartin KaFai Lau 				    &ipv6_hdr(skb)->saddr);
880286c2349SMartin KaFai Lau 
881405c92f7SHannes Frederic Sowa 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
882405c92f7SHannes Frederic Sowa 	    (err = skb_checksum_help(skb)))
883405c92f7SHannes Frederic Sowa 		goto fail;
884405c92f7SHannes Frederic Sowa 
885ef0efcd3SJunwei Hu 	prevhdr = skb_network_header(skb) + nexthdr_offset;
8861d325d21SFlorian Westphal 	hroom = LL_RESERVED_SPACE(rt->dst.dev);
88721dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
888c72d8cdaSAlexey Dobriyan 		unsigned int first_len = skb_pagelen(skb);
8890feca619SPablo Neira Ayuso 		struct ip6_fraglist_iter iter;
8903d13008eSEric Dumazet 		struct sk_buff *frag2;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
8931da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
8941d325d21SFlorian Westphal 		    skb_cloned(skb) ||
8951d325d21SFlorian Westphal 		    skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
8961da177e4SLinus Torvalds 			goto slow_path;
8971da177e4SLinus Torvalds 
8984d9092bbSDavid S. Miller 		skb_walk_frags(skb, frag) {
8991da177e4SLinus Torvalds 			/* Correct geometry. */
9001da177e4SLinus Torvalds 			if (frag->len > mtu ||
9011da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
9021d325d21SFlorian Westphal 			    skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
9033d13008eSEric Dumazet 				goto slow_path_clean;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 			/* Partially cloned skb? */
9061da177e4SLinus Torvalds 			if (skb_shared(frag))
9073d13008eSEric Dumazet 				goto slow_path_clean;
9082fdba6b0SHerbert Xu 
9092fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
9102fdba6b0SHerbert Xu 			if (skb->sk) {
9112fdba6b0SHerbert Xu 				frag->sk = skb->sk;
9122fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
9132fdba6b0SHerbert Xu 			}
9143d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 
9170feca619SPablo Neira Ayuso 		err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id,
9180feca619SPablo Neira Ayuso 					&iter);
9190feca619SPablo Neira Ayuso 		if (err < 0)
9201d325d21SFlorian Westphal 			goto fail;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		for (;;) {
9231da177e4SLinus Torvalds 			/* Prepare header of the next frame,
9241da177e4SLinus Torvalds 			 * before previous one went down. */
9250feca619SPablo Neira Ayuso 			if (iter.frag)
9260feca619SPablo Neira Ayuso 				ip6_fraglist_prepare(skb, &iter);
9271da177e4SLinus Torvalds 
9289669fffcSEric Dumazet 			skb->tstamp = tstamp;
9297d8c6e39SEric W. Biederman 			err = output(net, sk, skb);
930dafee490SWei Dong 			if (!err)
931d8d1f30bSChangli Gao 				IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
9323bd653c8SDenis V. Lunev 					      IPSTATS_MIB_FRAGCREATES);
933dafee490SWei Dong 
9340feca619SPablo Neira Ayuso 			if (err || !iter.frag)
9351da177e4SLinus Torvalds 				break;
9361da177e4SLinus Torvalds 
9370feca619SPablo Neira Ayuso 			skb = ip6_fraglist_next(&iter);
9381da177e4SLinus Torvalds 		}
9391da177e4SLinus Torvalds 
9400feca619SPablo Neira Ayuso 		kfree(iter.tmp_hdr);
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 		if (err == 0) {
943d8d1f30bSChangli Gao 			IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
9443bd653c8SDenis V. Lunev 				      IPSTATS_MIB_FRAGOKS);
9451da177e4SLinus Torvalds 			return 0;
9461da177e4SLinus Torvalds 		}
9471da177e4SLinus Torvalds 
948b7034146SEric Dumazet 		kfree_skb_list(iter.frag);
9491da177e4SLinus Torvalds 
950d8d1f30bSChangli Gao 		IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
9513bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGFAILS);
9521da177e4SLinus Torvalds 		return err;
9533d13008eSEric Dumazet 
9543d13008eSEric Dumazet slow_path_clean:
9553d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
9563d13008eSEric Dumazet 			if (frag2 == frag)
9573d13008eSEric Dumazet 				break;
9583d13008eSEric Dumazet 			frag2->sk = NULL;
9593d13008eSEric Dumazet 			frag2->destructor = NULL;
9603d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
9613d13008eSEric Dumazet 		}
9621da177e4SLinus Torvalds 	}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds slow_path:
9651da177e4SLinus Torvalds 	/*
9661da177e4SLinus Torvalds 	 *	Fragment the datagram.
9671da177e4SLinus Torvalds 	 */
9681da177e4SLinus Torvalds 
9698a6a1f17SPablo Neira Ayuso 	ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom,
9708a6a1f17SPablo Neira Ayuso 		      LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id,
9718a6a1f17SPablo Neira Ayuso 		      &state);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/*
9741da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
9751da177e4SLinus Torvalds 	 */
97679e49503SFlorian Westphal 
9778a6a1f17SPablo Neira Ayuso 	while (state.left > 0) {
9788a6a1f17SPablo Neira Ayuso 		frag = ip6_frag_next(skb, &state);
9798a6a1f17SPablo Neira Ayuso 		if (IS_ERR(frag)) {
9808a6a1f17SPablo Neira Ayuso 			err = PTR_ERR(frag);
9811da177e4SLinus Torvalds 			goto fail;
9821da177e4SLinus Torvalds 		}
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 		/*
9851da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
9861da177e4SLinus Torvalds 		 */
9879669fffcSEric Dumazet 		frag->tstamp = tstamp;
9887d8c6e39SEric W. Biederman 		err = output(net, sk, frag);
9891da177e4SLinus Torvalds 		if (err)
9901da177e4SLinus Torvalds 			goto fail;
991dafee490SWei Dong 
992adf30907SEric Dumazet 		IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
9933bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGCREATES);
9941da177e4SLinus Torvalds 	}
995adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
996a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGOKS);
997808db80aSEric Dumazet 	consume_skb(skb);
9981da177e4SLinus Torvalds 	return err;
9991da177e4SLinus Torvalds 
1000485fca66SFlorian Westphal fail_toobig:
1001485fca66SFlorian Westphal 	if (skb->sk && dst_allfrag(skb_dst(skb)))
1002485fca66SFlorian Westphal 		sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
1003485fca66SFlorian Westphal 
1004485fca66SFlorian Westphal 	icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1005485fca66SFlorian Westphal 	err = -EMSGSIZE;
1006485fca66SFlorian Westphal 
10071da177e4SLinus Torvalds fail:
1008adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
1009a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGFAILS);
10101da177e4SLinus Torvalds 	kfree_skb(skb);
10111da177e4SLinus Torvalds 	return err;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
1014b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key,
1015b71d1d42SEric Dumazet 			       const struct in6_addr *fl_addr,
1016b71d1d42SEric Dumazet 			       const struct in6_addr *addr_cache)
1017cf6b1982SYOSHIFUJI Hideaki {
1018a02cec21SEric Dumazet 	return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
101963159f29SIan Morris 		(!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
1020cf6b1982SYOSHIFUJI Hideaki }
1021cf6b1982SYOSHIFUJI Hideaki 
1022497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
1023497c615aSHerbert Xu 					  struct dst_entry *dst,
1024b71d1d42SEric Dumazet 					  const struct flowi6 *fl6)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
1027a963a37dSEric Dumazet 	struct rt6_info *rt;
10281da177e4SLinus Torvalds 
1029497c615aSHerbert Xu 	if (!dst)
1030497c615aSHerbert Xu 		goto out;
10311da177e4SLinus Torvalds 
1032a963a37dSEric Dumazet 	if (dst->ops->family != AF_INET6) {
1033a963a37dSEric Dumazet 		dst_release(dst);
1034a963a37dSEric Dumazet 		return NULL;
1035a963a37dSEric Dumazet 	}
1036a963a37dSEric Dumazet 
1037a963a37dSEric Dumazet 	rt = (struct rt6_info *)dst;
10381da177e4SLinus Torvalds 	/* Yes, checking route validity in not connected
1039d76e60a5SDavid S. Miller 	 * case is not very simple. Take into account,
1040d76e60a5SDavid S. Miller 	 * that we do not support routing by source, TOS,
1041d76e60a5SDavid S. Miller 	 * and MSG_DONTROUTE		--ANK (980726)
1042d76e60a5SDavid S. Miller 	 *
1043cf6b1982SYOSHIFUJI Hideaki 	 * 1. ip6_rt_check(): If route was host route,
1044cf6b1982SYOSHIFUJI Hideaki 	 *    check that cached destination is current.
1045d76e60a5SDavid S. Miller 	 *    If it is network route, we still may
1046d76e60a5SDavid S. Miller 	 *    check its validity using saved pointer
1047d76e60a5SDavid S. Miller 	 *    to the last used address: daddr_cache.
1048d76e60a5SDavid S. Miller 	 *    We do not want to save whole address now,
1049d76e60a5SDavid S. Miller 	 *    (because main consumer of this service
1050d76e60a5SDavid S. Miller 	 *    is tcp, which has not this problem),
1051d76e60a5SDavid S. Miller 	 *    so that the last trick works only on connected
1052d76e60a5SDavid S. Miller 	 *    sockets.
1053d76e60a5SDavid S. Miller 	 * 2. oif also should be the same.
10541da177e4SLinus Torvalds 	 */
10554c9483b2SDavid S. Miller 	if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
10568e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES
10574c9483b2SDavid S. Miller 	    ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
10588e1ef0a9SYOSHIFUJI Hideaki #endif
1059ca254490SDavid Ahern 	   (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) &&
1060ca254490SDavid Ahern 	      (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) {
1061497c615aSHerbert Xu 		dst_release(dst);
1062497c615aSHerbert Xu 		dst = NULL;
10631da177e4SLinus Torvalds 	}
1064497c615aSHerbert Xu 
1065497c615aSHerbert Xu out:
1066497c615aSHerbert Xu 	return dst;
10671da177e4SLinus Torvalds }
1068497c615aSHerbert Xu 
10693aef934fSEric Dumazet static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
10704c9483b2SDavid S. Miller 			       struct dst_entry **dst, struct flowi6 *fl6)
1071497c615aSHerbert Xu {
107269cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
107369cce1d1SDavid S. Miller 	struct neighbour *n;
107497cac082SDavid S. Miller 	struct rt6_info *rt;
107569cce1d1SDavid S. Miller #endif
107669cce1d1SDavid S. Miller 	int err;
10776f21c96aSPaolo Abeni 	int flags = 0;
10781da177e4SLinus Torvalds 
1079e16e888bSMarkus Stenberg 	/* The correct way to handle this would be to do
1080e16e888bSMarkus Stenberg 	 * ip6_route_get_saddr, and then ip6_route_output; however,
1081e16e888bSMarkus Stenberg 	 * the route-specific preferred source forces the
1082e16e888bSMarkus Stenberg 	 * ip6_route_output call _before_ ip6_route_get_saddr.
1083e16e888bSMarkus Stenberg 	 *
1084e16e888bSMarkus Stenberg 	 * In source specific routing (no src=any default route),
1085e16e888bSMarkus Stenberg 	 * ip6_route_output will fail given src=any saddr, though, so
1086e16e888bSMarkus Stenberg 	 * that's why we try it again later.
1087e16e888bSMarkus Stenberg 	 */
1088c305b9e6Szhang kai 	if (ipv6_addr_any(&fl6->saddr)) {
1089a68886a6SDavid Ahern 		struct fib6_info *from;
1090e16e888bSMarkus Stenberg 		struct rt6_info *rt;
1091e16e888bSMarkus Stenberg 
1092e16e888bSMarkus Stenberg 		*dst = ip6_route_output(net, sk, fl6);
1093e16e888bSMarkus Stenberg 		rt = (*dst)->error ? NULL : (struct rt6_info *)*dst;
1094a68886a6SDavid Ahern 
1095a68886a6SDavid Ahern 		rcu_read_lock();
1096a68886a6SDavid Ahern 		from = rt ? rcu_dereference(rt->from) : NULL;
1097a68886a6SDavid Ahern 		err = ip6_route_get_saddr(net, from, &fl6->daddr,
1098e16e888bSMarkus Stenberg 					  sk ? inet6_sk(sk)->srcprefs : 0,
1099e16e888bSMarkus Stenberg 					  &fl6->saddr);
1100a68886a6SDavid Ahern 		rcu_read_unlock();
1101a68886a6SDavid Ahern 
1102e16e888bSMarkus Stenberg 		if (err)
1103e16e888bSMarkus Stenberg 			goto out_err_release;
1104e16e888bSMarkus Stenberg 
1105e16e888bSMarkus Stenberg 		/* If we had an erroneous initial result, pretend it
1106e16e888bSMarkus Stenberg 		 * never existed and let the SA-enabled version take
1107e16e888bSMarkus Stenberg 		 * over.
1108e16e888bSMarkus Stenberg 		 */
1109c305b9e6Szhang kai 		if ((*dst)->error) {
1110e16e888bSMarkus Stenberg 			dst_release(*dst);
1111e16e888bSMarkus Stenberg 			*dst = NULL;
1112e16e888bSMarkus Stenberg 		}
11136f21c96aSPaolo Abeni 
11146f21c96aSPaolo Abeni 		if (fl6->flowi6_oif)
11156f21c96aSPaolo Abeni 			flags |= RT6_LOOKUP_F_IFACE;
1116e16e888bSMarkus Stenberg 	}
1117e16e888bSMarkus Stenberg 
111863159f29SIan Morris 	if (!*dst)
11196f21c96aSPaolo Abeni 		*dst = ip6_route_output_flags(net, sk, fl6, flags);
11201da177e4SLinus Torvalds 
1121e5d08d71SIan Morris 	err = (*dst)->error;
1122e5d08d71SIan Morris 	if (err)
11231da177e4SLinus Torvalds 		goto out_err_release;
11241da177e4SLinus Torvalds 
112595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
112695c385b4SNeil Horman 	/*
112795c385b4SNeil Horman 	 * Here if the dst entry we've looked up
112895c385b4SNeil Horman 	 * has a neighbour entry that is in the INCOMPLETE
112995c385b4SNeil Horman 	 * state and the src address from the flow is
113095c385b4SNeil Horman 	 * marked as OPTIMISTIC, we release the found
113195c385b4SNeil Horman 	 * dst entry and replace it instead with the
113295c385b4SNeil Horman 	 * dst entry of the nexthop router
113395c385b4SNeil Horman 	 */
1134c56bf6feSEric Dumazet 	rt = (struct rt6_info *) *dst;
1135707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
11362647a9b0SMartin KaFai Lau 	n = __ipv6_neigh_lookup_noref(rt->dst.dev,
11372647a9b0SMartin KaFai Lau 				      rt6_nexthop(rt, &fl6->daddr));
1138707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
1139707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
1140707be1ffSYOSHIFUJI Hideaki / 吉藤英明 
1141707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	if (err) {
114295c385b4SNeil Horman 		struct inet6_ifaddr *ifp;
11434c9483b2SDavid S. Miller 		struct flowi6 fl_gw6;
114495c385b4SNeil Horman 		int redirect;
114595c385b4SNeil Horman 
11464c9483b2SDavid S. Miller 		ifp = ipv6_get_ifaddr(net, &fl6->saddr,
11471cab3da6SDaniel Lezcano 				      (*dst)->dev, 1);
114895c385b4SNeil Horman 
114995c385b4SNeil Horman 		redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
115095c385b4SNeil Horman 		if (ifp)
115195c385b4SNeil Horman 			in6_ifa_put(ifp);
115295c385b4SNeil Horman 
115395c385b4SNeil Horman 		if (redirect) {
115495c385b4SNeil Horman 			/*
115595c385b4SNeil Horman 			 * We need to get the dst entry for the
115695c385b4SNeil Horman 			 * default router instead
115795c385b4SNeil Horman 			 */
115895c385b4SNeil Horman 			dst_release(*dst);
11594c9483b2SDavid S. Miller 			memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
11604c9483b2SDavid S. Miller 			memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
11614c9483b2SDavid S. Miller 			*dst = ip6_route_output(net, sk, &fl_gw6);
1162e5d08d71SIan Morris 			err = (*dst)->error;
1163e5d08d71SIan Morris 			if (err)
116495c385b4SNeil Horman 				goto out_err_release;
116595c385b4SNeil Horman 		}
116695c385b4SNeil Horman 	}
116795c385b4SNeil Horman #endif
1168ec5e3b0aSJonathan T. Leighton 	if (ipv6_addr_v4mapped(&fl6->saddr) &&
116900ea1ceeSWillem de Bruijn 	    !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) {
117000ea1ceeSWillem de Bruijn 		err = -EAFNOSUPPORT;
117100ea1ceeSWillem de Bruijn 		goto out_err_release;
117200ea1ceeSWillem de Bruijn 	}
117395c385b4SNeil Horman 
11741da177e4SLinus Torvalds 	return 0;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds out_err_release:
11771da177e4SLinus Torvalds 	dst_release(*dst);
11781da177e4SLinus Torvalds 	*dst = NULL;
11798a966fc0SDavid Ahern 
11800d240e78SDavid Ahern 	if (err == -ENETUNREACH)
11810d240e78SDavid Ahern 		IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
11821da177e4SLinus Torvalds 	return err;
11831da177e4SLinus Torvalds }
118434a0b3cdSAdrian Bunk 
1185497c615aSHerbert Xu /**
1186497c615aSHerbert Xu  *	ip6_dst_lookup - perform route lookup on flow
1187b51cd7c8SAndrew Lunn  *	@net: Network namespace to perform lookup in
1188497c615aSHerbert Xu  *	@sk: socket which provides route info
1189497c615aSHerbert Xu  *	@dst: pointer to dst_entry * for result
11904c9483b2SDavid S. Miller  *	@fl6: flow to lookup
1191497c615aSHerbert Xu  *
1192497c615aSHerbert Xu  *	This function performs a route lookup on the given flow.
1193497c615aSHerbert Xu  *
1194497c615aSHerbert Xu  *	It returns zero on success, or a standard errno code on error.
1195497c615aSHerbert Xu  */
1196343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1197343d60aaSRoopa Prabhu 		   struct flowi6 *fl6)
1198497c615aSHerbert Xu {
1199497c615aSHerbert Xu 	*dst = NULL;
1200343d60aaSRoopa Prabhu 	return ip6_dst_lookup_tail(net, sk, dst, fl6);
1201497c615aSHerbert Xu }
12023cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip6_dst_lookup);
12033cf3dc6cSArnaldo Carvalho de Melo 
1204497c615aSHerbert Xu /**
120568d0c6d3SDavid S. Miller  *	ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1206b51cd7c8SAndrew Lunn  *	@net: Network namespace to perform lookup in
120768d0c6d3SDavid S. Miller  *	@sk: socket which provides route info
12084c9483b2SDavid S. Miller  *	@fl6: flow to lookup
120968d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
121068d0c6d3SDavid S. Miller  *
121168d0c6d3SDavid S. Miller  *	This function performs a route lookup on the given flow.
121268d0c6d3SDavid S. Miller  *
121368d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
121468d0c6d3SDavid S. Miller  *	error code.
121568d0c6d3SDavid S. Miller  */
1216c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
12170e0d44abSSteffen Klassert 				      const struct in6_addr *final_dst)
121868d0c6d3SDavid S. Miller {
121968d0c6d3SDavid S. Miller 	struct dst_entry *dst = NULL;
122068d0c6d3SDavid S. Miller 	int err;
122168d0c6d3SDavid S. Miller 
1222c4e85f73SSabrina Dubroca 	err = ip6_dst_lookup_tail(net, sk, &dst, fl6);
122368d0c6d3SDavid S. Miller 	if (err)
122468d0c6d3SDavid S. Miller 		return ERR_PTR(err);
122568d0c6d3SDavid S. Miller 	if (final_dst)
12264e3fd7a0SAlexey Dobriyan 		fl6->daddr = *final_dst;
12272774c131SDavid S. Miller 
1228c4e85f73SSabrina Dubroca 	return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0);
122968d0c6d3SDavid S. Miller }
123068d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
123168d0c6d3SDavid S. Miller 
123268d0c6d3SDavid S. Miller /**
123368d0c6d3SDavid S. Miller  *	ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
123468d0c6d3SDavid S. Miller  *	@sk: socket which provides the dst cache and route info
12354c9483b2SDavid S. Miller  *	@fl6: flow to lookup
123668d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
123796818159SAlexey Kodanev  *	@connected: whether @sk is connected or not
1238497c615aSHerbert Xu  *
1239497c615aSHerbert Xu  *	This function performs a route lookup on the given flow with the
1240497c615aSHerbert Xu  *	possibility of using the cached route in the socket if it is valid.
1241497c615aSHerbert Xu  *	It will take the socket dst lock when operating on the dst cache.
1242497c615aSHerbert Xu  *	As a result, this function can only be used in process context.
1243497c615aSHerbert Xu  *
124496818159SAlexey Kodanev  *	In addition, for a connected socket, cache the dst in the socket
124596818159SAlexey Kodanev  *	if the current cache is not valid.
124696818159SAlexey Kodanev  *
124768d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
124868d0c6d3SDavid S. Miller  *	error code.
1249497c615aSHerbert Xu  */
12504c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
125196818159SAlexey Kodanev 					 const struct in6_addr *final_dst,
125296818159SAlexey Kodanev 					 bool connected)
1253497c615aSHerbert Xu {
125468d0c6d3SDavid S. Miller 	struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
1255497c615aSHerbert Xu 
12564c9483b2SDavid S. Miller 	dst = ip6_sk_dst_check(sk, dst, fl6);
125796818159SAlexey Kodanev 	if (dst)
125896818159SAlexey Kodanev 		return dst;
125996818159SAlexey Kodanev 
1260c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst);
126196818159SAlexey Kodanev 	if (connected && !IS_ERR(dst))
126296818159SAlexey Kodanev 		ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6);
126368d0c6d3SDavid S. Miller 
126400bc0ef5SJakub Sitnicki 	return dst;
126568d0c6d3SDavid S. Miller }
126668d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
1267497c615aSHerbert Xu 
1268571912c6SMartin Varghese /**
1269571912c6SMartin Varghese  *      ip6_dst_lookup_tunnel - perform route lookup on tunnel
1270571912c6SMartin Varghese  *      @skb: Packet for which lookup is done
1271571912c6SMartin Varghese  *      @dev: Tunnel device
1272571912c6SMartin Varghese  *      @net: Network namespace of tunnel device
1273b51cd7c8SAndrew Lunn  *      @sock: Socket which provides route info
1274571912c6SMartin Varghese  *      @saddr: Memory to store the src ip address
1275571912c6SMartin Varghese  *      @info: Tunnel information
1276571912c6SMartin Varghese  *      @protocol: IP protocol
1277b51cd7c8SAndrew Lunn  *      @use_cache: Flag to enable cache usage
1278571912c6SMartin Varghese  *      This function performs a route lookup on a tunnel
1279571912c6SMartin Varghese  *
1280571912c6SMartin Varghese  *      It returns a valid dst pointer and stores src address to be used in
1281571912c6SMartin Varghese  *      tunnel in param saddr on success, else a pointer encoded error code.
1282571912c6SMartin Varghese  */
1283571912c6SMartin Varghese 
1284571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb,
1285571912c6SMartin Varghese 					struct net_device *dev,
1286571912c6SMartin Varghese 					struct net *net,
1287571912c6SMartin Varghese 					struct socket *sock,
1288571912c6SMartin Varghese 					struct in6_addr *saddr,
1289571912c6SMartin Varghese 					const struct ip_tunnel_info *info,
1290571912c6SMartin Varghese 					u8 protocol,
1291571912c6SMartin Varghese 					bool use_cache)
1292571912c6SMartin Varghese {
1293571912c6SMartin Varghese 	struct dst_entry *dst = NULL;
1294571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1295571912c6SMartin Varghese 	struct dst_cache *dst_cache;
1296571912c6SMartin Varghese #endif
1297571912c6SMartin Varghese 	struct flowi6 fl6;
1298571912c6SMartin Varghese 	__u8 prio;
1299571912c6SMartin Varghese 
1300571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1301571912c6SMartin Varghese 	dst_cache = (struct dst_cache *)&info->dst_cache;
1302571912c6SMartin Varghese 	if (use_cache) {
1303571912c6SMartin Varghese 		dst = dst_cache_get_ip6(dst_cache, saddr);
1304571912c6SMartin Varghese 		if (dst)
1305571912c6SMartin Varghese 			return dst;
1306571912c6SMartin Varghese 	}
1307571912c6SMartin Varghese #endif
1308571912c6SMartin Varghese 	memset(&fl6, 0, sizeof(fl6));
1309571912c6SMartin Varghese 	fl6.flowi6_mark = skb->mark;
1310571912c6SMartin Varghese 	fl6.flowi6_proto = protocol;
1311571912c6SMartin Varghese 	fl6.daddr = info->key.u.ipv6.dst;
1312571912c6SMartin Varghese 	fl6.saddr = info->key.u.ipv6.src;
1313571912c6SMartin Varghese 	prio = info->key.tos;
1314571912c6SMartin Varghese 	fl6.flowlabel = ip6_make_flowinfo(RT_TOS(prio),
1315571912c6SMartin Varghese 					  info->key.label);
1316571912c6SMartin Varghese 
1317571912c6SMartin Varghese 	dst = ipv6_stub->ipv6_dst_lookup_flow(net, sock->sk, &fl6,
1318571912c6SMartin Varghese 					      NULL);
1319571912c6SMartin Varghese 	if (IS_ERR(dst)) {
1320571912c6SMartin Varghese 		netdev_dbg(dev, "no route to %pI6\n", &fl6.daddr);
1321571912c6SMartin Varghese 		return ERR_PTR(-ENETUNREACH);
1322571912c6SMartin Varghese 	}
1323571912c6SMartin Varghese 	if (dst->dev == dev) { /* is this necessary? */
1324571912c6SMartin Varghese 		netdev_dbg(dev, "circular route to %pI6\n", &fl6.daddr);
1325571912c6SMartin Varghese 		dst_release(dst);
1326571912c6SMartin Varghese 		return ERR_PTR(-ELOOP);
1327571912c6SMartin Varghese 	}
1328571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1329571912c6SMartin Varghese 	if (use_cache)
1330571912c6SMartin Varghese 		dst_cache_set_ip6(dst_cache, dst, &fl6.saddr);
1331571912c6SMartin Varghese #endif
1332571912c6SMartin Varghese 	*saddr = fl6.saddr;
1333571912c6SMartin Varghese 	return dst;
1334571912c6SMartin Varghese }
1335571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip6_dst_lookup_tunnel);
1336571912c6SMartin Varghese 
13370178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
13380178b695SHerbert Xu 					       gfp_t gfp)
13390178b695SHerbert Xu {
13400178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
13410178b695SHerbert Xu }
13420178b695SHerbert Xu 
13430178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
13440178b695SHerbert Xu 						gfp_t gfp)
13450178b695SHerbert Xu {
13460178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
13470178b695SHerbert Xu }
13480178b695SHerbert Xu 
134975a493e6SHannes Frederic Sowa static void ip6_append_data_mtu(unsigned int *mtu,
13500c183379SGao feng 				int *maxfraglen,
13510c183379SGao feng 				unsigned int fragheaderlen,
13520c183379SGao feng 				struct sk_buff *skb,
135375a493e6SHannes Frederic Sowa 				struct rt6_info *rt,
1354e367c2d0Slucien 				unsigned int orig_mtu)
13550c183379SGao feng {
13560c183379SGao feng 	if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
135763159f29SIan Morris 		if (!skb) {
13580c183379SGao feng 			/* first fragment, reserve header_len */
1359e367c2d0Slucien 			*mtu = orig_mtu - rt->dst.header_len;
13600c183379SGao feng 
13610c183379SGao feng 		} else {
13620c183379SGao feng 			/*
13630c183379SGao feng 			 * this fragment is not first, the headers
13640c183379SGao feng 			 * space is regarded as data space.
13650c183379SGao feng 			 */
1366e367c2d0Slucien 			*mtu = orig_mtu;
13670c183379SGao feng 		}
13680c183379SGao feng 		*maxfraglen = ((*mtu - fragheaderlen) & ~7)
13690c183379SGao feng 			      + fragheaderlen - sizeof(struct frag_hdr);
13700c183379SGao feng 	}
13710c183379SGao feng }
13720c183379SGao feng 
1373366e41d9SVlad Yasevich static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
137426879da5SWei Wang 			  struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
13755fdaa88dSWillem de Bruijn 			  struct rt6_info *rt, struct flowi6 *fl6)
1376366e41d9SVlad Yasevich {
1377366e41d9SVlad Yasevich 	struct ipv6_pinfo *np = inet6_sk(sk);
1378366e41d9SVlad Yasevich 	unsigned int mtu;
137926879da5SWei Wang 	struct ipv6_txoptions *opt = ipc6->opt;
1380366e41d9SVlad Yasevich 
1381366e41d9SVlad Yasevich 	/*
1382366e41d9SVlad Yasevich 	 * setup for corking
1383366e41d9SVlad Yasevich 	 */
1384366e41d9SVlad Yasevich 	if (opt) {
1385366e41d9SVlad Yasevich 		if (WARN_ON(v6_cork->opt))
1386366e41d9SVlad Yasevich 			return -EINVAL;
1387366e41d9SVlad Yasevich 
1388864e2a1fSEric Dumazet 		v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation);
138963159f29SIan Morris 		if (unlikely(!v6_cork->opt))
1390366e41d9SVlad Yasevich 			return -ENOBUFS;
1391366e41d9SVlad Yasevich 
1392864e2a1fSEric Dumazet 		v6_cork->opt->tot_len = sizeof(*opt);
1393366e41d9SVlad Yasevich 		v6_cork->opt->opt_flen = opt->opt_flen;
1394366e41d9SVlad Yasevich 		v6_cork->opt->opt_nflen = opt->opt_nflen;
1395366e41d9SVlad Yasevich 
1396366e41d9SVlad Yasevich 		v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1397366e41d9SVlad Yasevich 						    sk->sk_allocation);
1398366e41d9SVlad Yasevich 		if (opt->dst0opt && !v6_cork->opt->dst0opt)
1399366e41d9SVlad Yasevich 			return -ENOBUFS;
1400366e41d9SVlad Yasevich 
1401366e41d9SVlad Yasevich 		v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1402366e41d9SVlad Yasevich 						    sk->sk_allocation);
1403366e41d9SVlad Yasevich 		if (opt->dst1opt && !v6_cork->opt->dst1opt)
1404366e41d9SVlad Yasevich 			return -ENOBUFS;
1405366e41d9SVlad Yasevich 
1406366e41d9SVlad Yasevich 		v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt,
1407366e41d9SVlad Yasevich 						   sk->sk_allocation);
1408366e41d9SVlad Yasevich 		if (opt->hopopt && !v6_cork->opt->hopopt)
1409366e41d9SVlad Yasevich 			return -ENOBUFS;
1410366e41d9SVlad Yasevich 
1411366e41d9SVlad Yasevich 		v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1412366e41d9SVlad Yasevich 						    sk->sk_allocation);
1413366e41d9SVlad Yasevich 		if (opt->srcrt && !v6_cork->opt->srcrt)
1414366e41d9SVlad Yasevich 			return -ENOBUFS;
1415366e41d9SVlad Yasevich 
1416366e41d9SVlad Yasevich 		/* need source address above miyazawa*/
1417366e41d9SVlad Yasevich 	}
1418366e41d9SVlad Yasevich 	dst_hold(&rt->dst);
1419366e41d9SVlad Yasevich 	cork->base.dst = &rt->dst;
1420366e41d9SVlad Yasevich 	cork->fl.u.ip6 = *fl6;
142126879da5SWei Wang 	v6_cork->hop_limit = ipc6->hlimit;
142226879da5SWei Wang 	v6_cork->tclass = ipc6->tclass;
1423366e41d9SVlad Yasevich 	if (rt->dst.flags & DST_XFRM_TUNNEL)
1424366e41d9SVlad Yasevich 		mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1425749439bfSMike Maloney 		      READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst);
1426366e41d9SVlad Yasevich 	else
1427366e41d9SVlad Yasevich 		mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1428c02b3741SDavid S. Miller 			READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst));
1429366e41d9SVlad Yasevich 	if (np->frag_size < mtu) {
1430366e41d9SVlad Yasevich 		if (np->frag_size)
1431366e41d9SVlad Yasevich 			mtu = np->frag_size;
1432366e41d9SVlad Yasevich 	}
1433749439bfSMike Maloney 	if (mtu < IPV6_MIN_MTU)
1434749439bfSMike Maloney 		return -EINVAL;
1435366e41d9SVlad Yasevich 	cork->base.fragsize = mtu;
1436fbf47813SWillem de Bruijn 	cork->base.gso_size = ipc6->gso_size;
1437678ca42dSWillem de Bruijn 	cork->base.tx_flags = 0;
1438c6af0c22SWillem de Bruijn 	cork->base.mark = ipc6->sockc.mark;
1439678ca42dSWillem de Bruijn 	sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags);
1440bec1f6f6SWillem de Bruijn 
14410f6c480fSDavid Miller 	if (dst_allfrag(xfrm_dst_path(&rt->dst)))
1442366e41d9SVlad Yasevich 		cork->base.flags |= IPCORK_ALLFRAG;
1443366e41d9SVlad Yasevich 	cork->base.length = 0;
1444366e41d9SVlad Yasevich 
14455fdaa88dSWillem de Bruijn 	cork->base.transmit_time = ipc6->sockc.transmit_time;
1446a818f75eSJesus Sanchez-Palencia 
1447366e41d9SVlad Yasevich 	return 0;
1448366e41d9SVlad Yasevich }
1449366e41d9SVlad Yasevich 
14500bbe84a6SVlad Yasevich static int __ip6_append_data(struct sock *sk,
14510bbe84a6SVlad Yasevich 			     struct flowi6 *fl6,
14520bbe84a6SVlad Yasevich 			     struct sk_buff_head *queue,
14530bbe84a6SVlad Yasevich 			     struct inet_cork *cork,
14540bbe84a6SVlad Yasevich 			     struct inet6_cork *v6_cork,
14550bbe84a6SVlad Yasevich 			     struct page_frag *pfrag,
14560bbe84a6SVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
14570bbe84a6SVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
14581da177e4SLinus Torvalds 			     void *from, int length, int transhdrlen,
14595fdaa88dSWillem de Bruijn 			     unsigned int flags, struct ipcm6_cookie *ipc6)
14601da177e4SLinus Torvalds {
14610c183379SGao feng 	struct sk_buff *skb, *skb_prev = NULL;
146210b8a3deSPaolo Abeni 	unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
1463b5947e5dSWillem de Bruijn 	struct ubuf_info *uarg = NULL;
14640bbe84a6SVlad Yasevich 	int exthdrlen = 0;
14650bbe84a6SVlad Yasevich 	int dst_exthdrlen = 0;
14661da177e4SLinus Torvalds 	int hh_len;
14671da177e4SLinus Torvalds 	int copy;
14681da177e4SLinus Torvalds 	int err;
14691da177e4SLinus Torvalds 	int offset = 0;
147009c2d251SWillem de Bruijn 	u32 tskey = 0;
14710bbe84a6SVlad Yasevich 	struct rt6_info *rt = (struct rt6_info *)cork->dst;
14720bbe84a6SVlad Yasevich 	struct ipv6_txoptions *opt = v6_cork->opt;
147332dce968SVlad Yasevich 	int csummode = CHECKSUM_NONE;
1474682b1a9dSHannes Frederic Sowa 	unsigned int maxnonfragsize, headersize;
14751f4c6eb2SEric Dumazet 	unsigned int wmem_alloc_delta = 0;
1476100f6d8eSWillem de Bruijn 	bool paged, extra_uref = false;
14771da177e4SLinus Torvalds 
14780bbe84a6SVlad Yasevich 	skb = skb_peek_tail(queue);
14790bbe84a6SVlad Yasevich 	if (!skb) {
14800bbe84a6SVlad Yasevich 		exthdrlen = opt ? opt->opt_flen : 0;
14817efdba5bSRomain KUNTZ 		dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
14821da177e4SLinus Torvalds 	}
14830bbe84a6SVlad Yasevich 
148415e36f5bSWillem de Bruijn 	paged = !!cork->gso_size;
1485bec1f6f6SWillem de Bruijn 	mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize;
1486e367c2d0Slucien 	orig_mtu = mtu;
14871da177e4SLinus Torvalds 
1488678ca42dSWillem de Bruijn 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
1489678ca42dSWillem de Bruijn 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
1490678ca42dSWillem de Bruijn 		tskey = sk->sk_tskey++;
1491678ca42dSWillem de Bruijn 
1492d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
14931da177e4SLinus Torvalds 
1494a1b05140SMasahide NAKAMURA 	fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
1495b4ce9277SHerbert Xu 			(opt ? opt->opt_nflen : 0);
14964df98e76SHannes Frederic Sowa 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
14974df98e76SHannes Frederic Sowa 		     sizeof(struct frag_hdr);
14981da177e4SLinus Torvalds 
14994df98e76SHannes Frederic Sowa 	headersize = sizeof(struct ipv6hdr) +
15003a1cebe7SHannes Frederic Sowa 		     (opt ? opt->opt_flen + opt->opt_nflen : 0) +
15014df98e76SHannes Frederic Sowa 		     (dst_allfrag(&rt->dst) ?
15024df98e76SHannes Frederic Sowa 		      sizeof(struct frag_hdr) : 0) +
15034df98e76SHannes Frederic Sowa 		     rt->rt6i_nfheader_len;
15044df98e76SHannes Frederic Sowa 
150510b8a3deSPaolo Abeni 	/* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit
150610b8a3deSPaolo Abeni 	 * the first fragment
150710b8a3deSPaolo Abeni 	 */
150810b8a3deSPaolo Abeni 	if (headersize + transhdrlen > mtu)
150910b8a3deSPaolo Abeni 		goto emsgsize;
151010b8a3deSPaolo Abeni 
151126879da5SWei Wang 	if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
15124df98e76SHannes Frederic Sowa 	    (sk->sk_protocol == IPPROTO_UDP ||
15134df98e76SHannes Frederic Sowa 	     sk->sk_protocol == IPPROTO_RAW)) {
15144df98e76SHannes Frederic Sowa 		ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
15154df98e76SHannes Frederic Sowa 				sizeof(struct ipv6hdr));
15164df98e76SHannes Frederic Sowa 		goto emsgsize;
15174df98e76SHannes Frederic Sowa 	}
15184df98e76SHannes Frederic Sowa 
1519682b1a9dSHannes Frederic Sowa 	if (ip6_sk_ignore_df(sk))
1520682b1a9dSHannes Frederic Sowa 		maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1521682b1a9dSHannes Frederic Sowa 	else
1522682b1a9dSHannes Frederic Sowa 		maxnonfragsize = mtu;
1523682b1a9dSHannes Frederic Sowa 
15244df98e76SHannes Frederic Sowa 	if (cork->length + length > maxnonfragsize - headersize) {
15254df98e76SHannes Frederic Sowa emsgsize:
152610b8a3deSPaolo Abeni 		pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
152710b8a3deSPaolo Abeni 		ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
15281da177e4SLinus Torvalds 		return -EMSGSIZE;
15291da177e4SLinus Torvalds 	}
1530682b1a9dSHannes Frederic Sowa 
1531682b1a9dSHannes Frederic Sowa 	/* CHECKSUM_PARTIAL only with no extension headers and when
1532682b1a9dSHannes Frederic Sowa 	 * we are not going to fragment
1533682b1a9dSHannes Frederic Sowa 	 */
1534682b1a9dSHannes Frederic Sowa 	if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
1535682b1a9dSHannes Frederic Sowa 	    headersize == sizeof(struct ipv6hdr) &&
15362b89ed65SVlad Yasevich 	    length <= mtu - headersize &&
1537bec1f6f6SWillem de Bruijn 	    (!(flags & MSG_MORE) || cork->gso_size) &&
1538c8cd0989STom Herbert 	    rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1539682b1a9dSHannes Frederic Sowa 		csummode = CHECKSUM_PARTIAL;
15401da177e4SLinus Torvalds 
1541b5947e5dSWillem de Bruijn 	if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) {
15428c793822SJonathan Lemon 		uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb));
1543b5947e5dSWillem de Bruijn 		if (!uarg)
1544b5947e5dSWillem de Bruijn 			return -ENOBUFS;
1545522924b5SWillem de Bruijn 		extra_uref = !skb_zcopy(skb);	/* only ref on new uarg */
1546b5947e5dSWillem de Bruijn 		if (rt->dst.dev->features & NETIF_F_SG &&
1547b5947e5dSWillem de Bruijn 		    csummode == CHECKSUM_PARTIAL) {
1548b5947e5dSWillem de Bruijn 			paged = true;
1549b5947e5dSWillem de Bruijn 		} else {
1550b5947e5dSWillem de Bruijn 			uarg->zerocopy = 0;
155152900d22SWillem de Bruijn 			skb_zcopy_set(skb, uarg, &extra_uref);
1552b5947e5dSWillem de Bruijn 		}
1553b5947e5dSWillem de Bruijn 	}
1554b5947e5dSWillem de Bruijn 
15551da177e4SLinus Torvalds 	/*
15561da177e4SLinus Torvalds 	 * Let's try using as much space as possible.
15571da177e4SLinus Torvalds 	 * Use MTU if total length of the message fits into the MTU.
15581da177e4SLinus Torvalds 	 * Otherwise, we need to reserve fragment header and
15591da177e4SLinus Torvalds 	 * fragment alignment (= 8-15 octects, in total).
15601da177e4SLinus Torvalds 	 *
1561634a63e7SRandy Dunlap 	 * Note that we may need to "move" the data from the tail
15621da177e4SLinus Torvalds 	 * of the buffer to the new fragment when we split
15631da177e4SLinus Torvalds 	 * the message.
15641da177e4SLinus Torvalds 	 *
15651da177e4SLinus Torvalds 	 * FIXME: It may be fragmented into multiple chunks
15661da177e4SLinus Torvalds 	 *        at once if non-fragmentable extension headers
15671da177e4SLinus Torvalds 	 *        are too large.
15681da177e4SLinus Torvalds 	 * --yoshfuji
15691da177e4SLinus Torvalds 	 */
15701da177e4SLinus Torvalds 
15712811ebacSHannes Frederic Sowa 	cork->length += length;
15722811ebacSHannes Frederic Sowa 	if (!skb)
15731da177e4SLinus Torvalds 		goto alloc_new_skb;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	while (length > 0) {
15761da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
1577bdc712b4SDavid S. Miller 		copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
15781da177e4SLinus Torvalds 		if (copy < length)
15791da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 		if (copy <= 0) {
15821da177e4SLinus Torvalds 			char *data;
15831da177e4SLinus Torvalds 			unsigned int datalen;
15841da177e4SLinus Torvalds 			unsigned int fraglen;
15851da177e4SLinus Torvalds 			unsigned int fraggap;
15866d123b81SJakub Kicinski 			unsigned int alloclen, alloc_extra;
1587aba36930SWillem de Bruijn 			unsigned int pagedlen;
15881da177e4SLinus Torvalds alloc_new_skb:
15891da177e4SLinus Torvalds 			/* There's no room in the current skb */
15900c183379SGao feng 			if (skb)
15910c183379SGao feng 				fraggap = skb->len - maxfraglen;
15921da177e4SLinus Torvalds 			else
15931da177e4SLinus Torvalds 				fraggap = 0;
15940c183379SGao feng 			/* update mtu and maxfraglen if necessary */
159563159f29SIan Morris 			if (!skb || !skb_prev)
15960c183379SGao feng 				ip6_append_data_mtu(&mtu, &maxfraglen,
159775a493e6SHannes Frederic Sowa 						    fragheaderlen, skb, rt,
1598e367c2d0Slucien 						    orig_mtu);
15990c183379SGao feng 
16000c183379SGao feng 			skb_prev = skb;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 			/*
16031da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
16041da177e4SLinus Torvalds 			 * we know we need more fragment(s).
16051da177e4SLinus Torvalds 			 */
16061da177e4SLinus Torvalds 			datalen = length + fraggap;
16071da177e4SLinus Torvalds 
16080c183379SGao feng 			if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
16090c183379SGao feng 				datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
161015e36f5bSWillem de Bruijn 			fraglen = datalen + fragheaderlen;
1611aba36930SWillem de Bruijn 			pagedlen = 0;
161215e36f5bSWillem de Bruijn 
16136d123b81SJakub Kicinski 			alloc_extra = hh_len;
16146d123b81SJakub Kicinski 			alloc_extra += dst_exthdrlen;
16156d123b81SJakub Kicinski 			alloc_extra += rt->dst.trailer_len;
16166d123b81SJakub Kicinski 
16176d123b81SJakub Kicinski 			/* We just reserve space for fragment header.
16186d123b81SJakub Kicinski 			 * Note: this may be overallocation if the message
16196d123b81SJakub Kicinski 			 * (without MSG_MORE) fits into the MTU.
16206d123b81SJakub Kicinski 			 */
16216d123b81SJakub Kicinski 			alloc_extra += sizeof(struct frag_hdr);
16226d123b81SJakub Kicinski 
16231da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
1624d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
16251da177e4SLinus Torvalds 				alloclen = mtu;
16266d123b81SJakub Kicinski 			else if (!paged &&
16276d123b81SJakub Kicinski 				 (fraglen + alloc_extra < SKB_MAX_ALLOC ||
16286d123b81SJakub Kicinski 				  !(rt->dst.dev->features & NETIF_F_SG)))
162915e36f5bSWillem de Bruijn 				alloclen = fraglen;
163015e36f5bSWillem de Bruijn 			else {
163115e36f5bSWillem de Bruijn 				alloclen = min_t(int, fraglen, MAX_HEADER);
163215e36f5bSWillem de Bruijn 				pagedlen = fraglen - alloclen;
163315e36f5bSWillem de Bruijn 			}
16346d123b81SJakub Kicinski 			alloclen += alloc_extra;
1635299b0767SSteffen Klassert 
16360c183379SGao feng 			if (datalen != length + fraggap) {
16371da177e4SLinus Torvalds 				/*
16380c183379SGao feng 				 * this is not the last fragment, the trailer
16390c183379SGao feng 				 * space is regarded as data space.
16401da177e4SLinus Torvalds 				 */
16410c183379SGao feng 				datalen += rt->dst.trailer_len;
16420c183379SGao feng 			}
16430c183379SGao feng 
16440c183379SGao feng 			fraglen = datalen + fragheaderlen;
16451da177e4SLinus Torvalds 
164615e36f5bSWillem de Bruijn 			copy = datalen - transhdrlen - fraggap - pagedlen;
1647232cd35dSEric Dumazet 			if (copy < 0) {
1648232cd35dSEric Dumazet 				err = -EINVAL;
1649232cd35dSEric Dumazet 				goto error;
1650232cd35dSEric Dumazet 			}
16511da177e4SLinus Torvalds 			if (transhdrlen) {
16526d123b81SJakub Kicinski 				skb = sock_alloc_send_skb(sk, alloclen,
16531da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
16541da177e4SLinus Torvalds 			} else {
16551da177e4SLinus Torvalds 				skb = NULL;
16561f4c6eb2SEric Dumazet 				if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
16571da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
16586d123b81SJakub Kicinski 					skb = alloc_skb(alloclen,
16591da177e4SLinus Torvalds 							sk->sk_allocation);
166063159f29SIan Morris 				if (unlikely(!skb))
16611da177e4SLinus Torvalds 					err = -ENOBUFS;
16621da177e4SLinus Torvalds 			}
166363159f29SIan Morris 			if (!skb)
16641da177e4SLinus Torvalds 				goto error;
16651da177e4SLinus Torvalds 			/*
16661da177e4SLinus Torvalds 			 *	Fill in the control structures
16671da177e4SLinus Torvalds 			 */
16689c9c9ad5SHannes Frederic Sowa 			skb->protocol = htons(ETH_P_IPV6);
166932dce968SVlad Yasevich 			skb->ip_summed = csummode;
16701da177e4SLinus Torvalds 			skb->csum = 0;
16711f85851eSGao feng 			/* reserve for fragmentation and ipsec header */
16721f85851eSGao feng 			skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
16731f85851eSGao feng 				    dst_exthdrlen);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 			/*
16761da177e4SLinus Torvalds 			 *	Find where to start putting bytes
16771da177e4SLinus Torvalds 			 */
167815e36f5bSWillem de Bruijn 			data = skb_put(skb, fraglen - pagedlen);
16791f85851eSGao feng 			skb_set_network_header(skb, exthdrlen);
16801f85851eSGao feng 			data += fragheaderlen;
1681b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1682b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
16831da177e4SLinus Torvalds 			if (fraggap) {
16841da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
16851da177e4SLinus Torvalds 					skb_prev, maxfraglen,
16868d5930dfSAl Viro 					data + transhdrlen, fraggap);
16871da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
16881da177e4SLinus Torvalds 							  skb->csum);
16891da177e4SLinus Torvalds 				data += fraggap;
1690e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
16911da177e4SLinus Torvalds 			}
1692232cd35dSEric Dumazet 			if (copy > 0 &&
1693232cd35dSEric Dumazet 			    getfrag(from, data + transhdrlen, offset,
1694232cd35dSEric Dumazet 				    copy, fraggap, skb) < 0) {
16951da177e4SLinus Torvalds 				err = -EFAULT;
16961da177e4SLinus Torvalds 				kfree_skb(skb);
16971da177e4SLinus Torvalds 				goto error;
16981da177e4SLinus Torvalds 			}
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 			offset += copy;
170115e36f5bSWillem de Bruijn 			length -= copy + transhdrlen;
17021da177e4SLinus Torvalds 			transhdrlen = 0;
17031da177e4SLinus Torvalds 			exthdrlen = 0;
1704299b0767SSteffen Klassert 			dst_exthdrlen = 0;
17051da177e4SLinus Torvalds 
170652900d22SWillem de Bruijn 			/* Only the initial fragment is time stamped */
170752900d22SWillem de Bruijn 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
170852900d22SWillem de Bruijn 			cork->tx_flags = 0;
170952900d22SWillem de Bruijn 			skb_shinfo(skb)->tskey = tskey;
171052900d22SWillem de Bruijn 			tskey = 0;
171152900d22SWillem de Bruijn 			skb_zcopy_set(skb, uarg, &extra_uref);
171252900d22SWillem de Bruijn 
17130dec879fSJulian Anastasov 			if ((flags & MSG_CONFIRM) && !skb_prev)
17140dec879fSJulian Anastasov 				skb_set_dst_pending_confirm(skb, 1);
17150dec879fSJulian Anastasov 
17161da177e4SLinus Torvalds 			/*
17171da177e4SLinus Torvalds 			 * Put the packet on the pending queue
17181da177e4SLinus Torvalds 			 */
17191f4c6eb2SEric Dumazet 			if (!skb->destructor) {
17201f4c6eb2SEric Dumazet 				skb->destructor = sock_wfree;
17211f4c6eb2SEric Dumazet 				skb->sk = sk;
17221f4c6eb2SEric Dumazet 				wmem_alloc_delta += skb->truesize;
17231f4c6eb2SEric Dumazet 			}
17240bbe84a6SVlad Yasevich 			__skb_queue_tail(queue, skb);
17251da177e4SLinus Torvalds 			continue;
17261da177e4SLinus Torvalds 		}
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 		if (copy > length)
17291da177e4SLinus Torvalds 			copy = length;
17301da177e4SLinus Torvalds 
1731113f99c3SWillem de Bruijn 		if (!(rt->dst.dev->features&NETIF_F_SG) &&
1732113f99c3SWillem de Bruijn 		    skb_tailroom(skb) >= copy) {
17331da177e4SLinus Torvalds 			unsigned int off;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 			off = skb->len;
17361da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
17371da177e4SLinus Torvalds 						offset, copy, off, skb) < 0) {
17381da177e4SLinus Torvalds 				__skb_trim(skb, off);
17391da177e4SLinus Torvalds 				err = -EFAULT;
17401da177e4SLinus Torvalds 				goto error;
17411da177e4SLinus Torvalds 			}
1742b5947e5dSWillem de Bruijn 		} else if (!uarg || !uarg->zerocopy) {
17431da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 			err = -ENOMEM;
17465640f768SEric Dumazet 			if (!sk_page_frag_refill(sk, pfrag))
17471da177e4SLinus Torvalds 				goto error;
17481da177e4SLinus Torvalds 
17495640f768SEric Dumazet 			if (!skb_can_coalesce(skb, i, pfrag->page,
17505640f768SEric Dumazet 					      pfrag->offset)) {
17511da177e4SLinus Torvalds 				err = -EMSGSIZE;
17525640f768SEric Dumazet 				if (i == MAX_SKB_FRAGS)
17531da177e4SLinus Torvalds 					goto error;
17545640f768SEric Dumazet 
17555640f768SEric Dumazet 				__skb_fill_page_desc(skb, i, pfrag->page,
17565640f768SEric Dumazet 						     pfrag->offset, 0);
17575640f768SEric Dumazet 				skb_shinfo(skb)->nr_frags = ++i;
17585640f768SEric Dumazet 				get_page(pfrag->page);
17591da177e4SLinus Torvalds 			}
17605640f768SEric Dumazet 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
17619e903e08SEric Dumazet 			if (getfrag(from,
17625640f768SEric Dumazet 				    page_address(pfrag->page) + pfrag->offset,
17635640f768SEric Dumazet 				    offset, copy, skb->len, skb) < 0)
17645640f768SEric Dumazet 				goto error_efault;
17655640f768SEric Dumazet 
17665640f768SEric Dumazet 			pfrag->offset += copy;
17675640f768SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
17681da177e4SLinus Torvalds 			skb->len += copy;
17691da177e4SLinus Torvalds 			skb->data_len += copy;
1770f945fa7aSHerbert Xu 			skb->truesize += copy;
17711f4c6eb2SEric Dumazet 			wmem_alloc_delta += copy;
1772b5947e5dSWillem de Bruijn 		} else {
1773b5947e5dSWillem de Bruijn 			err = skb_zerocopy_iter_dgram(skb, from, copy);
1774b5947e5dSWillem de Bruijn 			if (err < 0)
1775b5947e5dSWillem de Bruijn 				goto error;
17761da177e4SLinus Torvalds 		}
17771da177e4SLinus Torvalds 		offset += copy;
17781da177e4SLinus Torvalds 		length -= copy;
17791da177e4SLinus Torvalds 	}
17805640f768SEric Dumazet 
17819e8445a5SPaolo Abeni 	if (wmem_alloc_delta)
17821f4c6eb2SEric Dumazet 		refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
17831da177e4SLinus Torvalds 	return 0;
17845640f768SEric Dumazet 
17855640f768SEric Dumazet error_efault:
17865640f768SEric Dumazet 	err = -EFAULT;
17871da177e4SLinus Torvalds error:
17888e044917SJonathan Lemon 	net_zcopy_put_abort(uarg, extra_uref);
1789bdc712b4SDavid S. Miller 	cork->length -= length;
17903bd653c8SDenis V. Lunev 	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
17911f4c6eb2SEric Dumazet 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
17921da177e4SLinus Torvalds 	return err;
17931da177e4SLinus Torvalds }
17940bbe84a6SVlad Yasevich 
17950bbe84a6SVlad Yasevich int ip6_append_data(struct sock *sk,
17960bbe84a6SVlad Yasevich 		    int getfrag(void *from, char *to, int offset, int len,
17970bbe84a6SVlad Yasevich 				int odd, struct sk_buff *skb),
179826879da5SWei Wang 		    void *from, int length, int transhdrlen,
179926879da5SWei Wang 		    struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
18005fdaa88dSWillem de Bruijn 		    struct rt6_info *rt, unsigned int flags)
18010bbe84a6SVlad Yasevich {
18020bbe84a6SVlad Yasevich 	struct inet_sock *inet = inet_sk(sk);
18030bbe84a6SVlad Yasevich 	struct ipv6_pinfo *np = inet6_sk(sk);
18040bbe84a6SVlad Yasevich 	int exthdrlen;
18050bbe84a6SVlad Yasevich 	int err;
18060bbe84a6SVlad Yasevich 
18070bbe84a6SVlad Yasevich 	if (flags&MSG_PROBE)
18080bbe84a6SVlad Yasevich 		return 0;
18090bbe84a6SVlad Yasevich 	if (skb_queue_empty(&sk->sk_write_queue)) {
18100bbe84a6SVlad Yasevich 		/*
18110bbe84a6SVlad Yasevich 		 * setup for corking
18120bbe84a6SVlad Yasevich 		 */
181326879da5SWei Wang 		err = ip6_setup_cork(sk, &inet->cork, &np->cork,
18145fdaa88dSWillem de Bruijn 				     ipc6, rt, fl6);
18150bbe84a6SVlad Yasevich 		if (err)
18160bbe84a6SVlad Yasevich 			return err;
18170bbe84a6SVlad Yasevich 
181826879da5SWei Wang 		exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
18190bbe84a6SVlad Yasevich 		length += exthdrlen;
18200bbe84a6SVlad Yasevich 		transhdrlen += exthdrlen;
18210bbe84a6SVlad Yasevich 	} else {
18220bbe84a6SVlad Yasevich 		fl6 = &inet->cork.fl.u.ip6;
18230bbe84a6SVlad Yasevich 		transhdrlen = 0;
18240bbe84a6SVlad Yasevich 	}
18250bbe84a6SVlad Yasevich 
18260bbe84a6SVlad Yasevich 	return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
18270bbe84a6SVlad Yasevich 				 &np->cork, sk_page_frag(sk), getfrag,
18285fdaa88dSWillem de Bruijn 				 from, length, transhdrlen, flags, ipc6);
18290bbe84a6SVlad Yasevich }
1830a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data);
18311da177e4SLinus Torvalds 
1832366e41d9SVlad Yasevich static void ip6_cork_release(struct inet_cork_full *cork,
1833366e41d9SVlad Yasevich 			     struct inet6_cork *v6_cork)
1834bf138862SPavel Emelyanov {
1835366e41d9SVlad Yasevich 	if (v6_cork->opt) {
1836366e41d9SVlad Yasevich 		kfree(v6_cork->opt->dst0opt);
1837366e41d9SVlad Yasevich 		kfree(v6_cork->opt->dst1opt);
1838366e41d9SVlad Yasevich 		kfree(v6_cork->opt->hopopt);
1839366e41d9SVlad Yasevich 		kfree(v6_cork->opt->srcrt);
1840366e41d9SVlad Yasevich 		kfree(v6_cork->opt);
1841366e41d9SVlad Yasevich 		v6_cork->opt = NULL;
18420178b695SHerbert Xu 	}
18430178b695SHerbert Xu 
1844366e41d9SVlad Yasevich 	if (cork->base.dst) {
1845366e41d9SVlad Yasevich 		dst_release(cork->base.dst);
1846366e41d9SVlad Yasevich 		cork->base.dst = NULL;
1847366e41d9SVlad Yasevich 		cork->base.flags &= ~IPCORK_ALLFRAG;
1848bf138862SPavel Emelyanov 	}
1849366e41d9SVlad Yasevich 	memset(&cork->fl, 0, sizeof(cork->fl));
1850bf138862SPavel Emelyanov }
1851bf138862SPavel Emelyanov 
18526422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk,
18536422398cSVlad Yasevich 			       struct sk_buff_head *queue,
18546422398cSVlad Yasevich 			       struct inet_cork_full *cork,
18556422398cSVlad Yasevich 			       struct inet6_cork *v6_cork)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
18581da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
18591da177e4SLinus Torvalds 	struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
18601da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
18613bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
18621da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
18636422398cSVlad Yasevich 	struct ipv6_txoptions *opt = v6_cork->opt;
18646422398cSVlad Yasevich 	struct rt6_info *rt = (struct rt6_info *)cork->base.dst;
18656422398cSVlad Yasevich 	struct flowi6 *fl6 = &cork->fl.u.ip6;
18664c9483b2SDavid S. Miller 	unsigned char proto = fl6->flowi6_proto;
18671da177e4SLinus Torvalds 
18686422398cSVlad Yasevich 	skb = __skb_dequeue(queue);
186963159f29SIan Morris 	if (!skb)
18701da177e4SLinus Torvalds 		goto out;
18711da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1874d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1875bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
18766422398cSVlad Yasevich 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1877cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
18781da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
18791da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
18801da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
18811da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
18821da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
18831da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
18841da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
18851da177e4SLinus Torvalds 	}
18861da177e4SLinus Torvalds 
188728a89453SHerbert Xu 	/* Allow local fragmentation. */
188860ff7467SWANG Cong 	skb->ignore_df = ip6_sk_ignore_df(sk);
188928a89453SHerbert Xu 
18904e3fd7a0SAlexey Dobriyan 	*final_dst = fl6->daddr;
1891cfe1fc77SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_header_len(skb));
18921da177e4SLinus Torvalds 	if (opt && opt->opt_flen)
18931da177e4SLinus Torvalds 		ipv6_push_frag_opts(skb, opt, &proto);
18941da177e4SLinus Torvalds 	if (opt && opt->opt_nflen)
1895613fa3caSDavid Lebrun 		ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr);
18961da177e4SLinus Torvalds 
1897e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
1898e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
18990660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
19001da177e4SLinus Torvalds 
19016422398cSVlad Yasevich 	ip6_flow_hdr(hdr, v6_cork->tclass,
1902cb1ce2efSTom Herbert 		     ip6_make_flowlabel(net, skb, fl6->flowlabel,
1903513674b5SShaohua Li 					ip6_autoflowlabel(net, np), fl6));
19046422398cSVlad Yasevich 	hdr->hop_limit = v6_cork->hop_limit;
19051da177e4SLinus Torvalds 	hdr->nexthdr = proto;
19064e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
19074e3fd7a0SAlexey Dobriyan 	hdr->daddr = *final_dst;
19081da177e4SLinus Torvalds 
1909a2c2064fSPatrick McHardy 	skb->priority = sk->sk_priority;
1910c6af0c22SWillem de Bruijn 	skb->mark = cork->base.mark;
1911a2c2064fSPatrick McHardy 
1912a818f75eSJesus Sanchez-Palencia 	skb->tstamp = cork->base.transmit_time;
1913a818f75eSJesus Sanchez-Palencia 
1914d8d1f30bSChangli Gao 	skb_dst_set(skb, dst_clone(&rt->dst));
1915edf391ffSNeil Horman 	IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
191614878f75SDavid L Stevens 	if (proto == IPPROTO_ICMPV6) {
1917adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
191814878f75SDavid L Stevens 
191943a43b60SHannes Frederic Sowa 		ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type);
192043a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
192114878f75SDavid L Stevens 	}
192214878f75SDavid L Stevens 
19236422398cSVlad Yasevich 	ip6_cork_release(cork, v6_cork);
19246422398cSVlad Yasevich out:
19256422398cSVlad Yasevich 	return skb;
19266422398cSVlad Yasevich }
19276422398cSVlad Yasevich 
19286422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb)
19296422398cSVlad Yasevich {
19306422398cSVlad Yasevich 	struct net *net = sock_net(skb->sk);
19316422398cSVlad Yasevich 	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
19326422398cSVlad Yasevich 	int err;
19336422398cSVlad Yasevich 
193433224b16SEric W. Biederman 	err = ip6_local_out(net, skb->sk, skb);
19351da177e4SLinus Torvalds 	if (err) {
19361da177e4SLinus Torvalds 		if (err > 0)
19376ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
19381da177e4SLinus Torvalds 		if (err)
19396422398cSVlad Yasevich 			IP6_INC_STATS(net, rt->rt6i_idev,
19406422398cSVlad Yasevich 				      IPSTATS_MIB_OUTDISCARDS);
19411da177e4SLinus Torvalds 	}
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	return err;
19446422398cSVlad Yasevich }
19456422398cSVlad Yasevich 
19466422398cSVlad Yasevich int ip6_push_pending_frames(struct sock *sk)
19476422398cSVlad Yasevich {
19486422398cSVlad Yasevich 	struct sk_buff *skb;
19496422398cSVlad Yasevich 
19506422398cSVlad Yasevich 	skb = ip6_finish_skb(sk);
19516422398cSVlad Yasevich 	if (!skb)
19526422398cSVlad Yasevich 		return 0;
19536422398cSVlad Yasevich 
19546422398cSVlad Yasevich 	return ip6_send_skb(skb);
19551da177e4SLinus Torvalds }
1956a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
19571da177e4SLinus Torvalds 
19580bbe84a6SVlad Yasevich static void __ip6_flush_pending_frames(struct sock *sk,
19596422398cSVlad Yasevich 				       struct sk_buff_head *queue,
19606422398cSVlad Yasevich 				       struct inet_cork_full *cork,
19616422398cSVlad Yasevich 				       struct inet6_cork *v6_cork)
19621da177e4SLinus Torvalds {
19631da177e4SLinus Torvalds 	struct sk_buff *skb;
19641da177e4SLinus Torvalds 
19650bbe84a6SVlad Yasevich 	while ((skb = __skb_dequeue_tail(queue)) != NULL) {
1966adf30907SEric Dumazet 		if (skb_dst(skb))
1967adf30907SEric Dumazet 			IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
1968a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_OUTDISCARDS);
19691da177e4SLinus Torvalds 		kfree_skb(skb);
19701da177e4SLinus Torvalds 	}
19711da177e4SLinus Torvalds 
19726422398cSVlad Yasevich 	ip6_cork_release(cork, v6_cork);
19731da177e4SLinus Torvalds }
19740bbe84a6SVlad Yasevich 
19750bbe84a6SVlad Yasevich void ip6_flush_pending_frames(struct sock *sk)
19760bbe84a6SVlad Yasevich {
19776422398cSVlad Yasevich 	__ip6_flush_pending_frames(sk, &sk->sk_write_queue,
19786422398cSVlad Yasevich 				   &inet_sk(sk)->cork, &inet6_sk(sk)->cork);
19790bbe84a6SVlad Yasevich }
1980a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
19816422398cSVlad Yasevich 
19826422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk,
19836422398cSVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
19846422398cSVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
19856422398cSVlad Yasevich 			     void *from, int length, int transhdrlen,
198626879da5SWei Wang 			     struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
19876422398cSVlad Yasevich 			     struct rt6_info *rt, unsigned int flags,
19885fdaa88dSWillem de Bruijn 			     struct inet_cork_full *cork)
19896422398cSVlad Yasevich {
19906422398cSVlad Yasevich 	struct inet6_cork v6_cork;
19916422398cSVlad Yasevich 	struct sk_buff_head queue;
199226879da5SWei Wang 	int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
19936422398cSVlad Yasevich 	int err;
19946422398cSVlad Yasevich 
19956422398cSVlad Yasevich 	if (flags & MSG_PROBE)
19966422398cSVlad Yasevich 		return NULL;
19976422398cSVlad Yasevich 
19986422398cSVlad Yasevich 	__skb_queue_head_init(&queue);
19996422398cSVlad Yasevich 
20001cd7884dSWillem de Bruijn 	cork->base.flags = 0;
20011cd7884dSWillem de Bruijn 	cork->base.addr = 0;
20021cd7884dSWillem de Bruijn 	cork->base.opt = NULL;
20031cd7884dSWillem de Bruijn 	cork->base.dst = NULL;
20046422398cSVlad Yasevich 	v6_cork.opt = NULL;
20055fdaa88dSWillem de Bruijn 	err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt, fl6);
2006862c03eeSEric Dumazet 	if (err) {
20071cd7884dSWillem de Bruijn 		ip6_cork_release(cork, &v6_cork);
20086422398cSVlad Yasevich 		return ERR_PTR(err);
2009862c03eeSEric Dumazet 	}
201026879da5SWei Wang 	if (ipc6->dontfrag < 0)
201126879da5SWei Wang 		ipc6->dontfrag = inet6_sk(sk)->dontfrag;
20126422398cSVlad Yasevich 
20131cd7884dSWillem de Bruijn 	err = __ip6_append_data(sk, fl6, &queue, &cork->base, &v6_cork,
20146422398cSVlad Yasevich 				&current->task_frag, getfrag, from,
20156422398cSVlad Yasevich 				length + exthdrlen, transhdrlen + exthdrlen,
20165fdaa88dSWillem de Bruijn 				flags, ipc6);
20176422398cSVlad Yasevich 	if (err) {
20181cd7884dSWillem de Bruijn 		__ip6_flush_pending_frames(sk, &queue, cork, &v6_cork);
20196422398cSVlad Yasevich 		return ERR_PTR(err);
20206422398cSVlad Yasevich 	}
20216422398cSVlad Yasevich 
20221cd7884dSWillem de Bruijn 	return __ip6_make_skb(sk, &queue, cork, &v6_cork);
20236422398cSVlad Yasevich }
2024