xref: /openbmc/linux/net/ipv6/ip6_output.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
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 
45d457a0e3SEric Dumazet #include <net/gso.h>
461da177e4SLinus Torvalds #include <net/ipv6.h>
471da177e4SLinus Torvalds #include <net/ndisc.h>
481da177e4SLinus Torvalds #include <net/protocol.h>
491da177e4SLinus Torvalds #include <net/ip6_route.h>
501da177e4SLinus Torvalds #include <net/addrconf.h>
511da177e4SLinus Torvalds #include <net/rawv6.h>
521da177e4SLinus Torvalds #include <net/icmp.h>
531da177e4SLinus Torvalds #include <net/xfrm.h>
541da177e4SLinus Torvalds #include <net/checksum.h>
557bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
56ca254490SDavid Ahern #include <net/l3mdev.h>
5714972cbdSRoopa Prabhu #include <net/lwtunnel.h>
58571912c6SMartin Varghese #include <net/ip_tunnels.h>
591da177e4SLinus Torvalds 
ip6_finish_output2(struct net * net,struct sock * sk,struct sk_buff * skb)607d8c6e39SEric W. Biederman static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
611da177e4SLinus Torvalds {
62adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
631da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
64e415ed3aSVasily Averin 	struct inet6_dev *idev = ip6_dst_idev(dst);
655796015fSVasily Averin 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
66e415ed3aSVasily Averin 	const struct in6_addr *daddr, *nexthop;
67e415ed3aSVasily Averin 	struct ipv6hdr *hdr;
68f6b72b62SDavid S. Miller 	struct neighbour *neigh;
696fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	int ret;
701da177e4SLinus Torvalds 
715796015fSVasily Averin 	/* Be paranoid, rather than too clever. */
72e415ed3aSVasily Averin 	if (unlikely(hh_len > skb_headroom(skb)) && dev->header_ops) {
736ab6bf73SEric Dumazet 		/* Make sure idev stays alive */
746ab6bf73SEric Dumazet 		rcu_read_lock();
75e415ed3aSVasily Averin 		skb = skb_expand_head(skb, hh_len);
765796015fSVasily Averin 		if (!skb) {
77e415ed3aSVasily Averin 			IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
786ab6bf73SEric Dumazet 			rcu_read_unlock();
795796015fSVasily Averin 			return -ENOMEM;
805796015fSVasily Averin 		}
816ab6bf73SEric Dumazet 		rcu_read_unlock();
825796015fSVasily Averin 	}
835796015fSVasily Averin 
84e415ed3aSVasily Averin 	hdr = ipv6_hdr(skb);
85e415ed3aSVasily Averin 	daddr = &hdr->daddr;
86e415ed3aSVasily Averin 	if (ipv6_addr_is_multicast(daddr)) {
877026b1ddSDavid Miller 		if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
888571ab47SYuval Mintz 		    ((mroute6_is_socket(net, skb) &&
89bd91b8bfSBenjamin Thery 		     !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
90e415ed3aSVasily Averin 		     ipv6_chk_mcast_addr(dev, daddr, &hdr->saddr))) {
911da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 			/* Do not check for IFF_ALLMULTI; multicast routing
941da177e4SLinus Torvalds 			   is not supported in any case.
951da177e4SLinus Torvalds 			 */
961da177e4SLinus Torvalds 			if (newskb)
97b2e0b385SJan Engelhardt 				NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
9829a26a56SEric W. Biederman 					net, sk, newskb, NULL, newskb->dev,
9995603e22SMichel Machado 					dev_loopback_xmit);
1001da177e4SLinus Torvalds 
101e415ed3aSVasily Averin 			if (hdr->hop_limit == 0) {
10278126c41SEric W. Biederman 				IP6_INC_STATS(net, idev,
1033bd653c8SDenis V. Lunev 					      IPSTATS_MIB_OUTDISCARDS);
1041da177e4SLinus Torvalds 				kfree_skb(skb);
1051da177e4SLinus Torvalds 				return 0;
1061da177e4SLinus Torvalds 			}
1071da177e4SLinus Torvalds 		}
1081da177e4SLinus Torvalds 
10978126c41SEric W. Biederman 		IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
110e415ed3aSVasily Averin 		if (IPV6_ADDR_MC_SCOPE(daddr) <= IPV6_ADDR_SCOPE_NODELOCAL &&
111dd408515SHannes Frederic Sowa 		    !(dev->flags & IFF_LOOPBACK)) {
112dd408515SHannes Frederic Sowa 			kfree_skb(skb);
113dd408515SHannes Frederic Sowa 			return 0;
114dd408515SHannes Frederic Sowa 		}
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
11714972cbdSRoopa Prabhu 	if (lwtunnel_xmit_redirect(dst->lwtstate)) {
11814972cbdSRoopa Prabhu 		int res = lwtunnel_xmit(skb);
11914972cbdSRoopa Prabhu 
120a171fbecSYan Zhai 		if (res != LWTUNNEL_XMIT_CONTINUE)
12114972cbdSRoopa Prabhu 			return res;
12214972cbdSRoopa Prabhu 	}
12314972cbdSRoopa Prabhu 
12456712f74SHeng Guo 	IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
12556712f74SHeng Guo 
12609eed119SEric Dumazet 	rcu_read_lock();
127*797a4c1fSEric Dumazet 	nexthop = rt6_nexthop(dst_rt6_info(dst), daddr);
128e415ed3aSVasily Averin 	neigh = __ipv6_neigh_lookup_noref(dev, nexthop);
12958f71be5SPavel Begunkov 
13058f71be5SPavel Begunkov 	if (unlikely(IS_ERR_OR_NULL(neigh))) {
1316fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		if (unlikely(!neigh))
132e415ed3aSVasily Averin 			neigh = __neigh_create(&nd_tbl, nexthop, dev, false);
13358f71be5SPavel Begunkov 		if (IS_ERR(neigh)) {
13409eed119SEric Dumazet 			rcu_read_unlock();
13558f71be5SPavel Begunkov 			IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTNOROUTES);
13658f71be5SPavel Begunkov 			kfree_skb_reason(skb, SKB_DROP_REASON_NEIGH_CREATEFAIL);
13758f71be5SPavel Begunkov 			return -EINVAL;
13858f71be5SPavel Begunkov 		}
13958f71be5SPavel Begunkov 	}
1404ff06203SJulian Anastasov 	sock_confirm_neigh(skb, neigh);
1410353f282SDavid Ahern 	ret = neigh_output(neigh, skb, false);
14209eed119SEric Dumazet 	rcu_read_unlock();
1436fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	return ret;
1446fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 }
1451da177e4SLinus Torvalds 
146b210de4fSAya Levin static int
ip6_finish_output_gso_slowpath_drop(struct net * net,struct sock * sk,struct sk_buff * skb,unsigned int mtu)147b210de4fSAya Levin ip6_finish_output_gso_slowpath_drop(struct net *net, struct sock *sk,
148b210de4fSAya Levin 				    struct sk_buff *skb, unsigned int mtu)
149b210de4fSAya Levin {
150b210de4fSAya Levin 	struct sk_buff *segs, *nskb;
151b210de4fSAya Levin 	netdev_features_t features;
152b210de4fSAya Levin 	int ret = 0;
153b210de4fSAya Levin 
154b210de4fSAya Levin 	/* Please see corresponding comment in ip_finish_output_gso
155b210de4fSAya Levin 	 * describing the cases where GSO segment length exceeds the
156b210de4fSAya Levin 	 * egress MTU.
157b210de4fSAya Levin 	 */
158b210de4fSAya Levin 	features = netif_skb_features(skb);
159b210de4fSAya Levin 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
160b210de4fSAya Levin 	if (IS_ERR_OR_NULL(segs)) {
161b210de4fSAya Levin 		kfree_skb(skb);
162b210de4fSAya Levin 		return -ENOMEM;
163b210de4fSAya Levin 	}
164b210de4fSAya Levin 
165b210de4fSAya Levin 	consume_skb(skb);
166b210de4fSAya Levin 
167b210de4fSAya Levin 	skb_list_walk_safe(segs, segs, nskb) {
168b210de4fSAya Levin 		int err;
169b210de4fSAya Levin 
170b210de4fSAya Levin 		skb_mark_not_on_list(segs);
171ee1c730fSYan Zhai 		/* Last GSO segment can be smaller than gso_size (and MTU).
172ee1c730fSYan Zhai 		 * Adding a fragment header would produce an "atomic fragment",
173ee1c730fSYan Zhai 		 * which is considered harmful (RFC-8021). Avoid that.
174ee1c730fSYan Zhai 		 */
175ee1c730fSYan Zhai 		err = segs->len > mtu ?
176ee1c730fSYan Zhai 			ip6_fragment(net, sk, segs, ip6_finish_output2) :
177ee1c730fSYan Zhai 			ip6_finish_output2(net, sk, segs);
178b210de4fSAya Levin 		if (err && ret == 0)
179b210de4fSAya Levin 			ret = err;
180b210de4fSAya Levin 	}
181b210de4fSAya Levin 
182b210de4fSAya Levin 	return ret;
183b210de4fSAya Levin }
184b210de4fSAya Levin 
__ip6_finish_output(struct net * net,struct sock * sk,struct sk_buff * skb)185956fe219Sbrakmo static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1869e508490SJan Engelhardt {
187b210de4fSAya Levin 	unsigned int mtu;
188b210de4fSAya Levin 
18909ee9dbaSTobias Brunner #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
19009ee9dbaSTobias Brunner 	/* Policy lookup after SNAT yielded a new policy */
19109ee9dbaSTobias Brunner 	if (skb_dst(skb)->xfrm) {
19219d36c5fSEric Dumazet 		IP6CB(skb)->flags |= IP6SKB_REROUTED;
19309ee9dbaSTobias Brunner 		return dst_output(net, sk, skb);
19409ee9dbaSTobias Brunner 	}
19509ee9dbaSTobias Brunner #endif
19609ee9dbaSTobias Brunner 
197b210de4fSAya Levin 	mtu = ip6_skb_dst_mtu(skb);
19880e425b6SCoco Li 	if (skb_is_gso(skb) &&
19980e425b6SCoco Li 	    !(IP6CB(skb)->flags & IP6SKB_FAKEJUMBO) &&
20080e425b6SCoco Li 	    !skb_gso_validate_network_len(skb, mtu))
201b210de4fSAya Levin 		return ip6_finish_output_gso_slowpath_drop(net, sk, skb, mtu);
202b210de4fSAya Levin 
203b210de4fSAya Levin 	if ((skb->len > mtu && !skb_is_gso(skb)) ||
2049037c357SJiri Pirko 	    dst_allfrag(skb_dst(skb)) ||
2059037c357SJiri Pirko 	    (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
2067d8c6e39SEric W. Biederman 		return ip6_fragment(net, sk, skb, ip6_finish_output2);
2079e508490SJan Engelhardt 	else
2087d8c6e39SEric W. Biederman 		return ip6_finish_output2(net, sk, skb);
2099e508490SJan Engelhardt }
2109e508490SJan Engelhardt 
ip6_finish_output(struct net * net,struct sock * sk,struct sk_buff * skb)211956fe219Sbrakmo static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
212956fe219Sbrakmo {
213956fe219Sbrakmo 	int ret;
214956fe219Sbrakmo 
215956fe219Sbrakmo 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
216956fe219Sbrakmo 	switch (ret) {
217956fe219Sbrakmo 	case NET_XMIT_SUCCESS:
218956fe219Sbrakmo 	case NET_XMIT_CN:
219956fe219Sbrakmo 		return __ip6_finish_output(net, sk, skb) ? : ret;
220956fe219Sbrakmo 	default:
2215e187189SMenglong Dong 		kfree_skb_reason(skb, SKB_DROP_REASON_BPF_CGROUP_EGRESS);
222956fe219Sbrakmo 		return ret;
223956fe219Sbrakmo 	}
224956fe219Sbrakmo }
225956fe219Sbrakmo 
ip6_output(struct net * net,struct sock * sk,struct sk_buff * skb)226ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
2271da177e4SLinus Torvalds {
22828f8bfd1SPhil Sutter 	struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev;
229adf30907SEric Dumazet 	struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
230be10de0aSEric W. Biederman 
23197a7a37aSChenbo Feng 	skb->protocol = htons(ETH_P_IPV6);
23297a7a37aSChenbo Feng 	skb->dev = dev;
23397a7a37aSChenbo Feng 
234e31b25ccSEric Dumazet 	if (unlikely(!idev || READ_ONCE(idev->cnf.disable_ipv6))) {
23519a0644cSEric W. Biederman 		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
2365e187189SMenglong Dong 		kfree_skb_reason(skb, SKB_DROP_REASON_IPV6DISABLED);
237778d80beSYOSHIFUJI Hideaki 		return 0;
238778d80beSYOSHIFUJI Hideaki 	}
239778d80beSYOSHIFUJI Hideaki 
24029a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
24128f8bfd1SPhil Sutter 			    net, sk, skb, indev, dev,
2429c6eb28aSJan Engelhardt 			    ip6_finish_output,
2439c6eb28aSJan Engelhardt 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
2441da177e4SLinus Torvalds }
2456585d7dcSBrian Vazquez EXPORT_SYMBOL(ip6_output);
2461da177e4SLinus Torvalds 
ip6_autoflowlabel(struct net * net,const struct ipv6_pinfo * np)247e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np)
248513674b5SShaohua Li {
249513674b5SShaohua Li 	if (!np->autoflowlabel_set)
250513674b5SShaohua Li 		return ip6_default_np_autolabel(net);
251513674b5SShaohua Li 	else
252513674b5SShaohua Li 		return np->autoflowlabel;
253513674b5SShaohua Li }
254513674b5SShaohua Li 
2551da177e4SLinus Torvalds /*
256b5d43998SShan Wei  * xmit an sk_buff (used by TCP, SCTP and DCCP)
2571c1e9d2bSEric Dumazet  * Note : socket lock is not held for SYNACK packets, but might be modified
2581c1e9d2bSEric Dumazet  * by calls to skb_set_owner_w() and ipv6_local_error(),
2591c1e9d2bSEric Dumazet  * which are using proper atomic operations or spinlocks.
2601da177e4SLinus Torvalds  */
ip6_xmit(const struct sock * sk,struct sk_buff * skb,struct flowi6 * fl6,__u32 mark,struct ipv6_txoptions * opt,int tclass,u32 priority)2611c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
2624f6570d7SEric Dumazet 	     __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority)
2631da177e4SLinus Torvalds {
2643bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
2651c1e9d2bSEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
2664c9483b2SDavid S. Miller 	struct in6_addr *first_hop = &fl6->daddr;
267adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2680c9f227bSVasily Averin 	struct net_device *dev = dst->dev;
2690c9f227bSVasily Averin 	struct inet6_dev *idev = ip6_dst_idev(dst);
27080e425b6SCoco Li 	struct hop_jumbo_hdr *hop_jumbo;
27180e425b6SCoco Li 	int hoplen = sizeof(*hop_jumbo);
27266033f47SStefano Brivio 	unsigned int head_room;
2731da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
2744c9483b2SDavid S. Miller 	u8  proto = fl6->flowi6_proto;
2751da177e4SLinus Torvalds 	int seg_len = skb->len;
276e651f03aSGerrit Renker 	int hlimit = -1;
2771da177e4SLinus Torvalds 	u32 mtu;
2781da177e4SLinus Torvalds 
27980e425b6SCoco Li 	head_room = sizeof(struct ipv6hdr) + hoplen + LL_RESERVED_SPACE(dev);
28066033f47SStefano Brivio 	if (opt)
28166033f47SStefano Brivio 		head_room += opt->opt_nflen + opt->opt_flen;
2821da177e4SLinus Torvalds 
2830c9f227bSVasily Averin 	if (unlikely(head_room > skb_headroom(skb))) {
284124b428fSEric Dumazet 		/* Make sure idev stays alive */
285124b428fSEric Dumazet 		rcu_read_lock();
2860c9f227bSVasily Averin 		skb = skb_expand_head(skb, head_room);
2870c9f227bSVasily Averin 		if (!skb) {
2880c9f227bSVasily Averin 			IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
289124b428fSEric Dumazet 			rcu_read_unlock();
2901da177e4SLinus Torvalds 			return -ENOBUFS;
2911da177e4SLinus Torvalds 		}
292124b428fSEric Dumazet 		rcu_read_unlock();
2931da177e4SLinus Torvalds 	}
29466033f47SStefano Brivio 
29566033f47SStefano Brivio 	if (opt) {
29666033f47SStefano Brivio 		seg_len += opt->opt_nflen + opt->opt_flen;
29766033f47SStefano Brivio 
2981da177e4SLinus Torvalds 		if (opt->opt_flen)
2991da177e4SLinus Torvalds 			ipv6_push_frag_opts(skb, opt, &proto);
30066033f47SStefano Brivio 
3011da177e4SLinus Torvalds 		if (opt->opt_nflen)
302613fa3caSDavid Lebrun 			ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop,
303613fa3caSDavid Lebrun 					     &fl6->saddr);
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 
30680e425b6SCoco Li 	if (unlikely(seg_len > IPV6_MAXPLEN)) {
30780e425b6SCoco Li 		hop_jumbo = skb_push(skb, hoplen);
30880e425b6SCoco Li 
30980e425b6SCoco Li 		hop_jumbo->nexthdr = proto;
31080e425b6SCoco Li 		hop_jumbo->hdrlen = 0;
31180e425b6SCoco Li 		hop_jumbo->tlv_type = IPV6_TLV_JUMBO;
31280e425b6SCoco Li 		hop_jumbo->tlv_len = 4;
31380e425b6SCoco Li 		hop_jumbo->jumbo_payload_len = htonl(seg_len + hoplen);
31480e425b6SCoco Li 
31580e425b6SCoco Li 		proto = IPPROTO_HOPOPTS;
31680e425b6SCoco Li 		seg_len = 0;
31780e425b6SCoco Li 		IP6CB(skb)->flags |= IP6SKB_FAKEJUMBO;
31880e425b6SCoco Li 	}
31980e425b6SCoco Li 
320e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
321e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
3220660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/*
3251da177e4SLinus Torvalds 	 *	Fill in the IPv6 header
3261da177e4SLinus Torvalds 	 */
327b903d324SEric Dumazet 	if (np)
3281da177e4SLinus Torvalds 		hlimit = np->hop_limit;
3291da177e4SLinus Torvalds 	if (hlimit < 0)
3306b75d090SYOSHIFUJI Hideaki 		hlimit = ip6_dst_hoplimit(dst);
3311da177e4SLinus Torvalds 
332cb1ce2efSTom Herbert 	ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
333513674b5SShaohua Li 				ip6_autoflowlabel(net, np), fl6));
33441a1f8eaSYOSHIFUJI Hideaki 
3351da177e4SLinus Torvalds 	hdr->payload_len = htons(seg_len);
3361da177e4SLinus Torvalds 	hdr->nexthdr = proto;
3371da177e4SLinus Torvalds 	hdr->hop_limit = hlimit;
3381da177e4SLinus Torvalds 
3394e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
3404e3fd7a0SAlexey Dobriyan 	hdr->daddr = *first_hop;
3411da177e4SLinus Torvalds 
3429c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
3434f6570d7SEric Dumazet 	skb->priority = priority;
34492e55f41SPablo Neira 	skb->mark = mark;
345a2c2064fSPatrick McHardy 
3461da177e4SLinus Torvalds 	mtu = dst_mtu(dst);
34760ff7467SWANG Cong 	if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
34856712f74SHeng Guo 		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
349a8e3e1a9SDavid Ahern 
350a8e3e1a9SDavid Ahern 		/* if egress device is enslaved to an L3 master device pass the
351a8e3e1a9SDavid Ahern 		 * skb to its handler for processing
352a8e3e1a9SDavid Ahern 		 */
353a8e3e1a9SDavid Ahern 		skb = l3mdev_ip6_out((struct sock *)sk, skb);
354a8e3e1a9SDavid Ahern 		if (unlikely(!skb))
355a8e3e1a9SDavid Ahern 			return 0;
356a8e3e1a9SDavid Ahern 
3571c1e9d2bSEric Dumazet 		/* hooks should never assume socket lock is held.
3581c1e9d2bSEric Dumazet 		 * we promote our socket to non const
3591c1e9d2bSEric Dumazet 		 */
36029a26a56SEric W. Biederman 		return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
3610c9f227bSVasily Averin 			       net, (struct sock *)sk, skb, NULL, dev,
36213206b6bSEric W. Biederman 			       dst_output);
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3650c9f227bSVasily Averin 	skb->dev = dev;
3661c1e9d2bSEric Dumazet 	/* ipv6_local_error() does not require socket lock,
3671c1e9d2bSEric Dumazet 	 * we promote our socket to non const
3681c1e9d2bSEric Dumazet 	 */
3691c1e9d2bSEric Dumazet 	ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
3701c1e9d2bSEric Dumazet 
3710c9f227bSVasily Averin 	IP6_INC_STATS(net, idev, IPSTATS_MIB_FRAGFAILS);
3721da177e4SLinus Torvalds 	kfree_skb(skb);
3731da177e4SLinus Torvalds 	return -EMSGSIZE;
3741da177e4SLinus Torvalds }
3757159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit);
3767159039aSYOSHIFUJI Hideaki 
ip6_call_ra_chain(struct sk_buff * skb,int sel)3771da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	struct ip6_ra_chain *ra;
3801da177e4SLinus Torvalds 	struct sock *last = NULL;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	read_lock(&ip6_ra_lock);
3831da177e4SLinus Torvalds 	for (ra = ip6_ra_chain; ra; ra = ra->next) {
3841da177e4SLinus Torvalds 		struct sock *sk = ra->sk;
3850bd1b59bSAndrew McDonald 		if (sk && ra->sel == sel &&
3860bd1b59bSAndrew McDonald 		    (!sk->sk_bound_dev_if ||
3870bd1b59bSAndrew McDonald 		     sk->sk_bound_dev_if == skb->dev->ifindex)) {
3889036b2feSFrancesco Ruggeri 			struct ipv6_pinfo *np = inet6_sk(sk);
3899036b2feSFrancesco Ruggeri 
3909036b2feSFrancesco Ruggeri 			if (np && np->rtalert_isolate &&
3919036b2feSFrancesco Ruggeri 			    !net_eq(sock_net(sk), dev_net(skb->dev))) {
3929036b2feSFrancesco Ruggeri 				continue;
3939036b2feSFrancesco Ruggeri 			}
3941da177e4SLinus Torvalds 			if (last) {
3951da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
3961da177e4SLinus Torvalds 				if (skb2)
3971da177e4SLinus Torvalds 					rawv6_rcv(last, skb2);
3981da177e4SLinus Torvalds 			}
3991da177e4SLinus Torvalds 			last = sk;
4001da177e4SLinus Torvalds 		}
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	if (last) {
4041da177e4SLinus Torvalds 		rawv6_rcv(last, skb);
4051da177e4SLinus Torvalds 		read_unlock(&ip6_ra_lock);
4061da177e4SLinus Torvalds 		return 1;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 	read_unlock(&ip6_ra_lock);
4091da177e4SLinus Torvalds 	return 0;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
ip6_forward_proxy_check(struct sk_buff * skb)412e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb)
413e21e0b5fSVille Nuorvala {
4140660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
415e21e0b5fSVille Nuorvala 	u8 nexthdr = hdr->nexthdr;
41675f2811cSJesse Gross 	__be16 frag_off;
417e21e0b5fSVille Nuorvala 	int offset;
418e21e0b5fSVille Nuorvala 
419e21e0b5fSVille Nuorvala 	if (ipv6_ext_hdr(nexthdr)) {
42075f2811cSJesse Gross 		offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
421e21e0b5fSVille Nuorvala 		if (offset < 0)
422e21e0b5fSVille Nuorvala 			return 0;
423e21e0b5fSVille Nuorvala 	} else
424e21e0b5fSVille Nuorvala 		offset = sizeof(struct ipv6hdr);
425e21e0b5fSVille Nuorvala 
426e21e0b5fSVille Nuorvala 	if (nexthdr == IPPROTO_ICMPV6) {
427e21e0b5fSVille Nuorvala 		struct icmp6hdr *icmp6;
428e21e0b5fSVille Nuorvala 
429d56f90a7SArnaldo Carvalho de Melo 		if (!pskb_may_pull(skb, (skb_network_header(skb) +
430d56f90a7SArnaldo Carvalho de Melo 					 offset + 1 - skb->data)))
431e21e0b5fSVille Nuorvala 			return 0;
432e21e0b5fSVille Nuorvala 
433d56f90a7SArnaldo Carvalho de Melo 		icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
434e21e0b5fSVille Nuorvala 
435e21e0b5fSVille Nuorvala 		switch (icmp6->icmp6_type) {
436e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_SOLICITATION:
437e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_ADVERTISEMENT:
438e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_SOLICITATION:
439e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_ADVERTISEMENT:
440e21e0b5fSVille Nuorvala 		case NDISC_REDIRECT:
441e21e0b5fSVille Nuorvala 			/* For reaction involving unicast neighbor discovery
442e21e0b5fSVille Nuorvala 			 * message destined to the proxied address, pass it to
443e21e0b5fSVille Nuorvala 			 * input function.
444e21e0b5fSVille Nuorvala 			 */
445e21e0b5fSVille Nuorvala 			return 1;
446e21e0b5fSVille Nuorvala 		default:
447e21e0b5fSVille Nuorvala 			break;
448e21e0b5fSVille Nuorvala 		}
449e21e0b5fSVille Nuorvala 	}
450e21e0b5fSVille Nuorvala 
45174553b09SVille Nuorvala 	/*
45274553b09SVille Nuorvala 	 * The proxying router can't forward traffic sent to a link-local
45374553b09SVille Nuorvala 	 * address, so signal the sender and discard the packet. This
45474553b09SVille Nuorvala 	 * behavior is clarified by the MIPv6 specification.
45574553b09SVille Nuorvala 	 */
45674553b09SVille Nuorvala 	if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
45774553b09SVille Nuorvala 		dst_link_failure(skb);
45874553b09SVille Nuorvala 		return -1;
45974553b09SVille Nuorvala 	}
46074553b09SVille Nuorvala 
461e21e0b5fSVille Nuorvala 	return 0;
462e21e0b5fSVille Nuorvala }
463e21e0b5fSVille Nuorvala 
ip6_forward_finish(struct net * net,struct sock * sk,struct sk_buff * skb)4640c4b51f0SEric W. Biederman static inline int ip6_forward_finish(struct net *net, struct sock *sk,
4650c4b51f0SEric W. Biederman 				     struct sk_buff *skb)
4661da177e4SLinus Torvalds {
46771a1c915SJeff Barnhill 	struct dst_entry *dst = skb_dst(skb);
46871a1c915SJeff Barnhill 
46971a1c915SJeff Barnhill 	__IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
47071a1c915SJeff Barnhill 
471f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV
472f839a6c9SIdo Schimmel 	if (skb->offload_l3_fwd_mark) {
473f839a6c9SIdo Schimmel 		consume_skb(skb);
474f839a6c9SIdo Schimmel 		return 0;
475f839a6c9SIdo Schimmel 	}
476f839a6c9SIdo Schimmel #endif
477f839a6c9SIdo Schimmel 
478de799101SMartin KaFai Lau 	skb_clear_tstamp(skb);
47913206b6bSEric W. Biederman 	return dst_output(net, sk, skb);
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
ip6_pkt_too_big(const struct sk_buff * skb,unsigned int mtu)482fe6cc55fSFlorian Westphal static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
483fe6cc55fSFlorian Westphal {
484418a3156SFlorian Westphal 	if (skb->len <= mtu)
485fe6cc55fSFlorian Westphal 		return false;
486fe6cc55fSFlorian Westphal 
48760ff7467SWANG Cong 	/* ipv6 conntrack defrag sets max_frag_size + ignore_df */
488fe6cc55fSFlorian Westphal 	if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
489fe6cc55fSFlorian Westphal 		return true;
490fe6cc55fSFlorian Westphal 
49160ff7467SWANG Cong 	if (skb->ignore_df)
492418a3156SFlorian Westphal 		return false;
493418a3156SFlorian Westphal 
494779b7931SDaniel Axtens 	if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
495fe6cc55fSFlorian Westphal 		return false;
496fe6cc55fSFlorian Westphal 
497fe6cc55fSFlorian Westphal 	return true;
498fe6cc55fSFlorian Westphal }
499fe6cc55fSFlorian Westphal 
ip6_forward(struct sk_buff * skb)5001da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb)
5011da177e4SLinus Torvalds {
502adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
5030660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
5041da177e4SLinus Torvalds 	struct inet6_skb_parm *opt = IP6CB(skb);
505c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dst->dev);
5060857d6f8SStephen Suryaputra 	struct inet6_dev *idev;
5072edc1a38SMenglong Dong 	SKB_DR(reason);
50814f3ad6fSUlrich Weber 	u32 mtu;
5091da177e4SLinus Torvalds 
5100857d6f8SStephen Suryaputra 	idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
51153b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->forwarding == 0)
5121da177e4SLinus Torvalds 		goto error;
5131da177e4SLinus Torvalds 
514090f1166SLi RongQing 	if (skb->pkt_type != PACKET_HOST)
515090f1166SLi RongQing 		goto drop;
516090f1166SLi RongQing 
5179ef2e965SHannes Frederic Sowa 	if (unlikely(skb->sk))
5189ef2e965SHannes Frederic Sowa 		goto drop;
5199ef2e965SHannes Frederic Sowa 
5204497b076SBen Hutchings 	if (skb_warn_if_lro(skb))
5214497b076SBen Hutchings 		goto drop;
5224497b076SBen Hutchings 
523ccd27f05SNicolas Dichtel 	if (!net->ipv6.devconf_all->disable_policy &&
524e3fa461dSNicolas Dichtel 	    (!idev || !idev->cnf.disable_policy) &&
525ccd27f05SNicolas Dichtel 	    !xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
526bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
5271da177e4SLinus Torvalds 		goto drop;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 
53035fc92a9SHerbert Xu 	skb_forward_csum(skb);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/*
5331da177e4SLinus Torvalds 	 *	We DO NOT make any processing on
5341da177e4SLinus Torvalds 	 *	RA packets, pushing them to user level AS IS
5351da177e4SLinus Torvalds 	 *	without ane WARRANTY that application will be able
5361da177e4SLinus Torvalds 	 *	to interpret them. The reason is that we
5371da177e4SLinus Torvalds 	 *	cannot make anything clever here.
5381da177e4SLinus Torvalds 	 *
5391da177e4SLinus Torvalds 	 *	We are not end-node, so that if packet contains
5401da177e4SLinus Torvalds 	 *	AH/ESP, we cannot make anything.
5411da177e4SLinus Torvalds 	 *	Defragmentation also would be mistake, RA packets
5421da177e4SLinus Torvalds 	 *	cannot be fragmented, because there is no warranty
5431da177e4SLinus Torvalds 	 *	that different fragments will go along one path. --ANK
5441da177e4SLinus Torvalds 	 */
545ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 	if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
546ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 		if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
5471da177e4SLinus Torvalds 			return 0;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	/*
5511da177e4SLinus Torvalds 	 *	check and decrement ttl
5521da177e4SLinus Torvalds 	 */
5531da177e4SLinus Torvalds 	if (hdr->hop_limit <= 1) {
5543ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
555bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
5561da177e4SLinus Torvalds 
5572edc1a38SMenglong Dong 		kfree_skb_reason(skb, SKB_DROP_REASON_IP_INHDR);
5581da177e4SLinus Torvalds 		return -ETIMEDOUT;
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 
561fbea49e1SYOSHIFUJI Hideaki 	/* XXX: idev->cnf.proxy_ndp? */
56253b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->proxy_ndp &&
5638a3edd80SDaniel Lezcano 	    pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
56474553b09SVille Nuorvala 		int proxied = ip6_forward_proxy_check(skb);
56546c7655fSKangmin Park 		if (proxied > 0) {
5669f535c87SGergely Risko 			/* It's tempting to decrease the hop limit
5679f535c87SGergely Risko 			 * here by 1, as we do at the end of the
5689f535c87SGergely Risko 			 * function too.
5699f535c87SGergely Risko 			 *
5709f535c87SGergely Risko 			 * But that would be incorrect, as proxying is
5719f535c87SGergely Risko 			 * not forwarding.  The ip6_input function
5729f535c87SGergely Risko 			 * will handle this packet locally, and it
5739f535c87SGergely Risko 			 * depends on the hop limit being unchanged.
5749f535c87SGergely Risko 			 *
5759f535c87SGergely Risko 			 * One example is the NDP hop limit, that
5769f535c87SGergely Risko 			 * always has to stay 255, but other would be
5779f535c87SGergely Risko 			 * similar checks around RA packets, where the
5789f535c87SGergely Risko 			 * user can even change the desired limit.
5799f535c87SGergely Risko 			 */
580e21e0b5fSVille Nuorvala 			return ip6_input(skb);
58146c7655fSKangmin Park 		} else if (proxied < 0) {
582bdb7cc64SStephen Suryaputra 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
58374553b09SVille Nuorvala 			goto drop;
58474553b09SVille Nuorvala 		}
585e21e0b5fSVille Nuorvala 	}
586e21e0b5fSVille Nuorvala 
5871da177e4SLinus Torvalds 	if (!xfrm6_route_forward(skb)) {
588bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
5892edc1a38SMenglong Dong 		SKB_DR_SET(reason, XFRM_POLICY);
5901da177e4SLinus Torvalds 		goto drop;
5911da177e4SLinus Torvalds 	}
592adf30907SEric Dumazet 	dst = skb_dst(skb);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/* IPv6 specs say nothing about it, but it is clear that we cannot
5951da177e4SLinus Torvalds 	   send redirects to source routed frames.
5961e5dc146SMasahide NAKAMURA 	   We don't send redirects to frames decapsulated from IPsec.
5971da177e4SLinus Torvalds 	 */
5982f17becfSStephen Suryaputra 	if (IP6CB(skb)->iif == dst->dev->ifindex &&
5992f17becfSStephen Suryaputra 	    opt->srcrt == 0 && !skb_sec_path(skb)) {
6001da177e4SLinus Torvalds 		struct in6_addr *target = NULL;
601fbfe95a4SDavid S. Miller 		struct inet_peer *peer;
6021da177e4SLinus Torvalds 		struct rt6_info *rt;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 		/*
6051da177e4SLinus Torvalds 		 *	incoming and outgoing devices are the same
6061da177e4SLinus Torvalds 		 *	send a redirect.
6071da177e4SLinus Torvalds 		 */
6081da177e4SLinus Torvalds 
609*797a4c1fSEric Dumazet 		rt = dst_rt6_info(dst);
610c45a3dfbSDavid S. Miller 		if (rt->rt6i_flags & RTF_GATEWAY)
611c45a3dfbSDavid S. Miller 			target = &rt->rt6i_gateway;
6121da177e4SLinus Torvalds 		else
6131da177e4SLinus Torvalds 			target = &hdr->daddr;
6141da177e4SLinus Torvalds 
615fd0273d7SMartin KaFai Lau 		peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1);
61692d86829SDavid S. Miller 
6171da177e4SLinus Torvalds 		/* Limit redirects both by destination (here)
6181da177e4SLinus Torvalds 		   and by source (inside ndisc_send_redirect)
6191da177e4SLinus Torvalds 		 */
620fbfe95a4SDavid S. Miller 		if (inet_peer_xrlim_allow(peer, 1*HZ))
6214991969aSDavid S. Miller 			ndisc_send_redirect(skb, target);
6221d861aa4SDavid S. Miller 		if (peer)
6231d861aa4SDavid S. Miller 			inet_putpeer(peer);
6245bb1ab09SDavid L Stevens 	} else {
6255bb1ab09SDavid L Stevens 		int addrtype = ipv6_addr_type(&hdr->saddr);
6265bb1ab09SDavid L Stevens 
6271da177e4SLinus Torvalds 		/* This check is security critical. */
628f81b2e7dSYOSHIFUJI Hideaki 		if (addrtype == IPV6_ADDR_ANY ||
629f81b2e7dSYOSHIFUJI Hideaki 		    addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
6301da177e4SLinus Torvalds 			goto error;
6315bb1ab09SDavid L Stevens 		if (addrtype & IPV6_ADDR_LINKLOCAL) {
6325bb1ab09SDavid L Stevens 			icmpv6_send(skb, ICMPV6_DEST_UNREACH,
6333ffe533cSAlexey Dobriyan 				    ICMPV6_NOT_NEIGHBOUR, 0);
6345bb1ab09SDavid L Stevens 			goto error;
6355bb1ab09SDavid L Stevens 		}
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 
638427faee1SVadim Fedorenko 	mtu = ip6_dst_mtu_maybe_forward(dst, true);
63914f3ad6fSUlrich Weber 	if (mtu < IPV6_MIN_MTU)
64014f3ad6fSUlrich Weber 		mtu = IPV6_MIN_MTU;
64114f3ad6fSUlrich Weber 
642fe6cc55fSFlorian Westphal 	if (ip6_pkt_too_big(skb, mtu)) {
6431da177e4SLinus Torvalds 		/* Again, force OUTPUT device used as source address */
6441da177e4SLinus Torvalds 		skb->dev = dst->dev;
64514f3ad6fSUlrich Weber 		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
646bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS);
6471d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(dst),
64815c77d8bSEric Dumazet 				IPSTATS_MIB_FRAGFAILS);
6492edc1a38SMenglong Dong 		kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
6501da177e4SLinus Torvalds 		return -EMSGSIZE;
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (skb_cow(skb, dst->dev->hard_header_len)) {
6541d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(dst),
65515c77d8bSEric Dumazet 				IPSTATS_MIB_OUTDISCARDS);
6561da177e4SLinus Torvalds 		goto drop;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
6590660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/* Mangling hops number delayed to point after skb COW */
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	hdr->hop_limit--;
6641da177e4SLinus Torvalds 
66529a26a56SEric W. Biederman 	return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
66629a26a56SEric W. Biederman 		       net, NULL, skb, skb->dev, dst->dev,
6676e23ae2aSPatrick McHardy 		       ip6_forward_finish);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds error:
670bdb7cc64SStephen Suryaputra 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
6712edc1a38SMenglong Dong 	SKB_DR_SET(reason, IP_INADDRERRORS);
6721da177e4SLinus Torvalds drop:
6732edc1a38SMenglong Dong 	kfree_skb_reason(skb, reason);
6741da177e4SLinus Torvalds 	return -EINVAL;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
ip6_copy_metadata(struct sk_buff * to,struct sk_buff * from)6771da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
6801da177e4SLinus Torvalds 	to->priority = from->priority;
6811da177e4SLinus Torvalds 	to->protocol = from->protocol;
682adf30907SEric Dumazet 	skb_dst_drop(to);
683adf30907SEric Dumazet 	skb_dst_set(to, dst_clone(skb_dst(from)));
6841da177e4SLinus Torvalds 	to->dev = from->dev;
68582e91ffeSThomas Graf 	to->mark = from->mark;
6861da177e4SLinus Torvalds 
6873dd1c9a1SPaolo Abeni 	skb_copy_hash(to, from);
6883dd1c9a1SPaolo Abeni 
6891da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
6901da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
6911da177e4SLinus Torvalds #endif
692e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
693df5042f4SFlorian Westphal 	skb_ext_copy(to, from);
694984bc16cSJames Morris 	skb_copy_secmark(to, from);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
ip6_fraglist_init(struct sk_buff * skb,unsigned int hlen,u8 * prevhdr,u8 nexthdr,__be32 frag_id,struct ip6_fraglist_iter * iter)6970feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
6980feca619SPablo Neira Ayuso 		      u8 nexthdr, __be32 frag_id,
6990feca619SPablo Neira Ayuso 		      struct ip6_fraglist_iter *iter)
7000feca619SPablo Neira Ayuso {
7010feca619SPablo Neira Ayuso 	unsigned int first_len;
7020feca619SPablo Neira Ayuso 	struct frag_hdr *fh;
7030feca619SPablo Neira Ayuso 
7040feca619SPablo Neira Ayuso 	/* BUILD HEADER */
7050feca619SPablo Neira Ayuso 	*prevhdr = NEXTHDR_FRAGMENT;
7060feca619SPablo Neira Ayuso 	iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
7070feca619SPablo Neira Ayuso 	if (!iter->tmp_hdr)
7080feca619SPablo Neira Ayuso 		return -ENOMEM;
7090feca619SPablo Neira Ayuso 
710b7034146SEric Dumazet 	iter->frag = skb_shinfo(skb)->frag_list;
7110feca619SPablo Neira Ayuso 	skb_frag_list_init(skb);
7120feca619SPablo Neira Ayuso 
7130feca619SPablo Neira Ayuso 	iter->offset = 0;
7140feca619SPablo Neira Ayuso 	iter->hlen = hlen;
7150feca619SPablo Neira Ayuso 	iter->frag_id = frag_id;
7160feca619SPablo Neira Ayuso 	iter->nexthdr = nexthdr;
7170feca619SPablo Neira Ayuso 
7180feca619SPablo Neira Ayuso 	__skb_pull(skb, hlen);
7190feca619SPablo Neira Ayuso 	fh = __skb_push(skb, sizeof(struct frag_hdr));
7200feca619SPablo Neira Ayuso 	__skb_push(skb, hlen);
7210feca619SPablo Neira Ayuso 	skb_reset_network_header(skb);
7220feca619SPablo Neira Ayuso 	memcpy(skb_network_header(skb), iter->tmp_hdr, hlen);
7230feca619SPablo Neira Ayuso 
7240feca619SPablo Neira Ayuso 	fh->nexthdr = nexthdr;
7250feca619SPablo Neira Ayuso 	fh->reserved = 0;
7260feca619SPablo Neira Ayuso 	fh->frag_off = htons(IP6_MF);
7270feca619SPablo Neira Ayuso 	fh->identification = frag_id;
7280feca619SPablo Neira Ayuso 
7290feca619SPablo Neira Ayuso 	first_len = skb_pagelen(skb);
7300feca619SPablo Neira Ayuso 	skb->data_len = first_len - skb_headlen(skb);
7310feca619SPablo Neira Ayuso 	skb->len = first_len;
7320feca619SPablo Neira Ayuso 	ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr));
7330feca619SPablo Neira Ayuso 
7340feca619SPablo Neira Ayuso 	return 0;
7350feca619SPablo Neira Ayuso }
7360feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_init);
7370feca619SPablo Neira Ayuso 
ip6_fraglist_prepare(struct sk_buff * skb,struct ip6_fraglist_iter * iter)7380feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb,
7390feca619SPablo Neira Ayuso 			  struct ip6_fraglist_iter *iter)
7400feca619SPablo Neira Ayuso {
7410feca619SPablo Neira Ayuso 	struct sk_buff *frag = iter->frag;
7420feca619SPablo Neira Ayuso 	unsigned int hlen = iter->hlen;
7430feca619SPablo Neira Ayuso 	struct frag_hdr *fh;
7440feca619SPablo Neira Ayuso 
7450feca619SPablo Neira Ayuso 	frag->ip_summed = CHECKSUM_NONE;
7460feca619SPablo Neira Ayuso 	skb_reset_transport_header(frag);
7470feca619SPablo Neira Ayuso 	fh = __skb_push(frag, sizeof(struct frag_hdr));
7480feca619SPablo Neira Ayuso 	__skb_push(frag, hlen);
7490feca619SPablo Neira Ayuso 	skb_reset_network_header(frag);
7500feca619SPablo Neira Ayuso 	memcpy(skb_network_header(frag), iter->tmp_hdr, hlen);
7510feca619SPablo Neira Ayuso 	iter->offset += skb->len - hlen - sizeof(struct frag_hdr);
7520feca619SPablo Neira Ayuso 	fh->nexthdr = iter->nexthdr;
7530feca619SPablo Neira Ayuso 	fh->reserved = 0;
7540feca619SPablo Neira Ayuso 	fh->frag_off = htons(iter->offset);
7550feca619SPablo Neira Ayuso 	if (frag->next)
7560feca619SPablo Neira Ayuso 		fh->frag_off |= htons(IP6_MF);
7570feca619SPablo Neira Ayuso 	fh->identification = iter->frag_id;
7580feca619SPablo Neira Ayuso 	ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
7590feca619SPablo Neira Ayuso 	ip6_copy_metadata(frag, skb);
7600feca619SPablo Neira Ayuso }
7610feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_prepare);
7620feca619SPablo Neira Ayuso 
ip6_frag_init(struct sk_buff * skb,unsigned int hlen,unsigned int mtu,unsigned short needed_tailroom,int hdr_room,u8 * prevhdr,u8 nexthdr,__be32 frag_id,struct ip6_frag_state * state)7638a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
7648a6a1f17SPablo Neira Ayuso 		   unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
7658a6a1f17SPablo Neira Ayuso 		   u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state)
7668a6a1f17SPablo Neira Ayuso {
7678a6a1f17SPablo Neira Ayuso 	state->prevhdr = prevhdr;
7688a6a1f17SPablo Neira Ayuso 	state->nexthdr = nexthdr;
7698a6a1f17SPablo Neira Ayuso 	state->frag_id = frag_id;
7708a6a1f17SPablo Neira Ayuso 
7718a6a1f17SPablo Neira Ayuso 	state->hlen = hlen;
7728a6a1f17SPablo Neira Ayuso 	state->mtu = mtu;
7738a6a1f17SPablo Neira Ayuso 
7748a6a1f17SPablo Neira Ayuso 	state->left = skb->len - hlen;	/* Space per frame */
7758a6a1f17SPablo Neira Ayuso 	state->ptr = hlen;		/* Where to start from */
7768a6a1f17SPablo Neira Ayuso 
7778a6a1f17SPablo Neira Ayuso 	state->hroom = hdr_room;
7788a6a1f17SPablo Neira Ayuso 	state->troom = needed_tailroom;
7798a6a1f17SPablo Neira Ayuso 
7808a6a1f17SPablo Neira Ayuso 	state->offset = 0;
7818a6a1f17SPablo Neira Ayuso }
7828a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_init);
7838a6a1f17SPablo Neira Ayuso 
ip6_frag_next(struct sk_buff * skb,struct ip6_frag_state * state)7848a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state)
7858a6a1f17SPablo Neira Ayuso {
7868a6a1f17SPablo Neira Ayuso 	u8 *prevhdr = state->prevhdr, *fragnexthdr_offset;
7878a6a1f17SPablo Neira Ayuso 	struct sk_buff *frag;
7888a6a1f17SPablo Neira Ayuso 	struct frag_hdr *fh;
7898a6a1f17SPablo Neira Ayuso 	unsigned int len;
7908a6a1f17SPablo Neira Ayuso 
7918a6a1f17SPablo Neira Ayuso 	len = state->left;
7928a6a1f17SPablo Neira Ayuso 	/* IF: it doesn't fit, use 'mtu' - the data space left */
7938a6a1f17SPablo Neira Ayuso 	if (len > state->mtu)
7948a6a1f17SPablo Neira Ayuso 		len = state->mtu;
7958a6a1f17SPablo Neira Ayuso 	/* IF: we are not sending up to and including the packet end
7968a6a1f17SPablo Neira Ayuso 	   then align the next start on an eight byte boundary */
7978a6a1f17SPablo Neira Ayuso 	if (len < state->left)
7988a6a1f17SPablo Neira Ayuso 		len &= ~7;
7998a6a1f17SPablo Neira Ayuso 
8008a6a1f17SPablo Neira Ayuso 	/* Allocate buffer */
8018a6a1f17SPablo Neira Ayuso 	frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) +
8028a6a1f17SPablo Neira Ayuso 			 state->hroom + state->troom, GFP_ATOMIC);
8038a6a1f17SPablo Neira Ayuso 	if (!frag)
8048a6a1f17SPablo Neira Ayuso 		return ERR_PTR(-ENOMEM);
8058a6a1f17SPablo Neira Ayuso 
8068a6a1f17SPablo Neira Ayuso 	/*
8078a6a1f17SPablo Neira Ayuso 	 *	Set up data on packet
8088a6a1f17SPablo Neira Ayuso 	 */
8098a6a1f17SPablo Neira Ayuso 
8108a6a1f17SPablo Neira Ayuso 	ip6_copy_metadata(frag, skb);
8118a6a1f17SPablo Neira Ayuso 	skb_reserve(frag, state->hroom);
8128a6a1f17SPablo Neira Ayuso 	skb_put(frag, len + state->hlen + sizeof(struct frag_hdr));
8138a6a1f17SPablo Neira Ayuso 	skb_reset_network_header(frag);
8148a6a1f17SPablo Neira Ayuso 	fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen);
8158a6a1f17SPablo Neira Ayuso 	frag->transport_header = (frag->network_header + state->hlen +
8168a6a1f17SPablo Neira Ayuso 				  sizeof(struct frag_hdr));
8178a6a1f17SPablo Neira Ayuso 
8188a6a1f17SPablo Neira Ayuso 	/*
8198a6a1f17SPablo Neira Ayuso 	 *	Charge the memory for the fragment to any owner
8208a6a1f17SPablo Neira Ayuso 	 *	it might possess
8218a6a1f17SPablo Neira Ayuso 	 */
8228a6a1f17SPablo Neira Ayuso 	if (skb->sk)
8238a6a1f17SPablo Neira Ayuso 		skb_set_owner_w(frag, skb->sk);
8248a6a1f17SPablo Neira Ayuso 
8258a6a1f17SPablo Neira Ayuso 	/*
8268a6a1f17SPablo Neira Ayuso 	 *	Copy the packet header into the new buffer.
8278a6a1f17SPablo Neira Ayuso 	 */
8288a6a1f17SPablo Neira Ayuso 	skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen);
8298a6a1f17SPablo Neira Ayuso 
8308a6a1f17SPablo Neira Ayuso 	fragnexthdr_offset = skb_network_header(frag);
8318a6a1f17SPablo Neira Ayuso 	fragnexthdr_offset += prevhdr - skb_network_header(skb);
8328a6a1f17SPablo Neira Ayuso 	*fragnexthdr_offset = NEXTHDR_FRAGMENT;
8338a6a1f17SPablo Neira Ayuso 
8348a6a1f17SPablo Neira Ayuso 	/*
8358a6a1f17SPablo Neira Ayuso 	 *	Build fragment header.
8368a6a1f17SPablo Neira Ayuso 	 */
8378a6a1f17SPablo Neira Ayuso 	fh->nexthdr = state->nexthdr;
8388a6a1f17SPablo Neira Ayuso 	fh->reserved = 0;
8398a6a1f17SPablo Neira Ayuso 	fh->identification = state->frag_id;
8408a6a1f17SPablo Neira Ayuso 
8418a6a1f17SPablo Neira Ayuso 	/*
8428a6a1f17SPablo Neira Ayuso 	 *	Copy a block of the IP datagram.
8438a6a1f17SPablo Neira Ayuso 	 */
8448a6a1f17SPablo Neira Ayuso 	BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag),
8458a6a1f17SPablo Neira Ayuso 			     len));
8468a6a1f17SPablo Neira Ayuso 	state->left -= len;
8478a6a1f17SPablo Neira Ayuso 
8488a6a1f17SPablo Neira Ayuso 	fh->frag_off = htons(state->offset);
8498a6a1f17SPablo Neira Ayuso 	if (state->left > 0)
8508a6a1f17SPablo Neira Ayuso 		fh->frag_off |= htons(IP6_MF);
8518a6a1f17SPablo Neira Ayuso 	ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
8528a6a1f17SPablo Neira Ayuso 
8538a6a1f17SPablo Neira Ayuso 	state->ptr += len;
8548a6a1f17SPablo Neira Ayuso 	state->offset += len;
8558a6a1f17SPablo Neira Ayuso 
8568a6a1f17SPablo Neira Ayuso 	return frag;
8578a6a1f17SPablo Neira Ayuso }
8588a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_next);
8598a6a1f17SPablo Neira Ayuso 
ip6_fragment(struct net * net,struct sock * sk,struct sk_buff * skb,int (* output)(struct net *,struct sock *,struct sk_buff *))8607d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
8617d8c6e39SEric W. Biederman 		 int (*output)(struct net *, struct sock *, struct sk_buff *))
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct sk_buff *frag;
864*797a4c1fSEric Dumazet 	struct rt6_info *rt = dst_rt6_info(skb_dst(skb));
865f60e5990Shannes@stressinduktion.org 	struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
866f60e5990Shannes@stressinduktion.org 				inet6_sk(skb->sk) : NULL;
867a1ac9c8aSMartin KaFai Lau 	bool mono_delivery_time = skb->mono_delivery_time;
8688a6a1f17SPablo Neira Ayuso 	struct ip6_frag_state state;
8698a6a1f17SPablo Neira Ayuso 	unsigned int mtu, hlen, nexthdr_offset;
8709669fffcSEric Dumazet 	ktime_t tstamp = skb->tstamp;
8718a6a1f17SPablo Neira Ayuso 	int hroom, err = 0;
872286c2349SMartin KaFai Lau 	__be32 frag_id;
8731da177e4SLinus Torvalds 	u8 *prevhdr, nexthdr = 0;
8741da177e4SLinus Torvalds 
8757dd7eb95SDavid S. Miller 	err = ip6_find_1stfragopt(skb, &prevhdr);
8767dd7eb95SDavid S. Miller 	if (err < 0)
8772423496aSCraig Gallek 		goto fail;
8787dd7eb95SDavid S. Miller 	hlen = err;
8791da177e4SLinus Torvalds 	nexthdr = *prevhdr;
880ef0efcd3SJunwei Hu 	nexthdr_offset = prevhdr - skb_network_header(skb);
8811da177e4SLinus Torvalds 
882628a5c56SJohn Heffner 	mtu = ip6_skb_dst_mtu(skb);
883b881ef76SJohn Heffner 
884b881ef76SJohn Heffner 	/* We must not fragment if the socket is set to force MTU discovery
88514f3ad6fSUlrich Weber 	 * or if the skb it not generated by a local socket.
886b881ef76SJohn Heffner 	 */
887485fca66SFlorian Westphal 	if (unlikely(!skb->ignore_df && skb->len > mtu))
888485fca66SFlorian Westphal 		goto fail_toobig;
889a34a101eSEric Dumazet 
890485fca66SFlorian Westphal 	if (IP6CB(skb)->frag_max_size) {
891485fca66SFlorian Westphal 		if (IP6CB(skb)->frag_max_size > mtu)
892485fca66SFlorian Westphal 			goto fail_toobig;
893485fca66SFlorian Westphal 
894485fca66SFlorian Westphal 		/* don't send fragments larger than what we received */
895485fca66SFlorian Westphal 		mtu = IP6CB(skb)->frag_max_size;
896485fca66SFlorian Westphal 		if (mtu < IPV6_MIN_MTU)
897485fca66SFlorian Westphal 			mtu = IPV6_MIN_MTU;
898b881ef76SJohn Heffner 	}
899b881ef76SJohn Heffner 
900d91675f9SYOSHIFUJI Hideaki 	if (np && np->frag_size < mtu) {
901d91675f9SYOSHIFUJI Hideaki 		if (np->frag_size)
902d91675f9SYOSHIFUJI Hideaki 			mtu = np->frag_size;
903d91675f9SYOSHIFUJI Hideaki 	}
90489bc7848SHannes Frederic Sowa 	if (mtu < hlen + sizeof(struct frag_hdr) + 8)
905b72a2b01SHannes Frederic Sowa 		goto fail_toobig;
9061e0d69a9SHannes Frederic Sowa 	mtu -= hlen + sizeof(struct frag_hdr);
9071da177e4SLinus Torvalds 
908fd0273d7SMartin KaFai Lau 	frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
909fd0273d7SMartin KaFai Lau 				    &ipv6_hdr(skb)->saddr);
910286c2349SMartin KaFai Lau 
911405c92f7SHannes Frederic Sowa 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
912405c92f7SHannes Frederic Sowa 	    (err = skb_checksum_help(skb)))
913405c92f7SHannes Frederic Sowa 		goto fail;
914405c92f7SHannes Frederic Sowa 
915ef0efcd3SJunwei Hu 	prevhdr = skb_network_header(skb) + nexthdr_offset;
9161d325d21SFlorian Westphal 	hroom = LL_RESERVED_SPACE(rt->dst.dev);
91721dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
918c72d8cdaSAlexey Dobriyan 		unsigned int first_len = skb_pagelen(skb);
9190feca619SPablo Neira Ayuso 		struct ip6_fraglist_iter iter;
9203d13008eSEric Dumazet 		struct sk_buff *frag2;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
9231da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
9241d325d21SFlorian Westphal 		    skb_cloned(skb) ||
9251d325d21SFlorian Westphal 		    skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
9261da177e4SLinus Torvalds 			goto slow_path;
9271da177e4SLinus Torvalds 
9284d9092bbSDavid S. Miller 		skb_walk_frags(skb, frag) {
9291da177e4SLinus Torvalds 			/* Correct geometry. */
9301da177e4SLinus Torvalds 			if (frag->len > mtu ||
9311da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
9321d325d21SFlorian Westphal 			    skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
9333d13008eSEric Dumazet 				goto slow_path_clean;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 			/* Partially cloned skb? */
9361da177e4SLinus Torvalds 			if (skb_shared(frag))
9373d13008eSEric Dumazet 				goto slow_path_clean;
9382fdba6b0SHerbert Xu 
9392fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
9402fdba6b0SHerbert Xu 			if (skb->sk) {
9412fdba6b0SHerbert Xu 				frag->sk = skb->sk;
9422fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
9432fdba6b0SHerbert Xu 			}
9443d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
9451da177e4SLinus Torvalds 		}
9461da177e4SLinus Torvalds 
9470feca619SPablo Neira Ayuso 		err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id,
9480feca619SPablo Neira Ayuso 					&iter);
9490feca619SPablo Neira Ayuso 		if (err < 0)
9501d325d21SFlorian Westphal 			goto fail;
9511da177e4SLinus Torvalds 
952803e8486SEric Dumazet 		/* We prevent @rt from being freed. */
953803e8486SEric Dumazet 		rcu_read_lock();
954803e8486SEric Dumazet 
9551da177e4SLinus Torvalds 		for (;;) {
9561da177e4SLinus Torvalds 			/* Prepare header of the next frame,
9571da177e4SLinus Torvalds 			 * before previous one went down. */
9580feca619SPablo Neira Ayuso 			if (iter.frag)
9590feca619SPablo Neira Ayuso 				ip6_fraglist_prepare(skb, &iter);
9601da177e4SLinus Torvalds 
961a1ac9c8aSMartin KaFai Lau 			skb_set_delivery_time(skb, tstamp, mono_delivery_time);
9627d8c6e39SEric W. Biederman 			err = output(net, sk, skb);
963dafee490SWei Dong 			if (!err)
964d8d1f30bSChangli Gao 				IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
9653bd653c8SDenis V. Lunev 					      IPSTATS_MIB_FRAGCREATES);
966dafee490SWei Dong 
9670feca619SPablo Neira Ayuso 			if (err || !iter.frag)
9681da177e4SLinus Torvalds 				break;
9691da177e4SLinus Torvalds 
9700feca619SPablo Neira Ayuso 			skb = ip6_fraglist_next(&iter);
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 
9730feca619SPablo Neira Ayuso 		kfree(iter.tmp_hdr);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 		if (err == 0) {
976d8d1f30bSChangli Gao 			IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
9773bd653c8SDenis V. Lunev 				      IPSTATS_MIB_FRAGOKS);
978803e8486SEric Dumazet 			rcu_read_unlock();
9791da177e4SLinus Torvalds 			return 0;
9801da177e4SLinus Torvalds 		}
9811da177e4SLinus Torvalds 
982b7034146SEric Dumazet 		kfree_skb_list(iter.frag);
9831da177e4SLinus Torvalds 
984d8d1f30bSChangli Gao 		IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
9853bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGFAILS);
986803e8486SEric Dumazet 		rcu_read_unlock();
9871da177e4SLinus Torvalds 		return err;
9883d13008eSEric Dumazet 
9893d13008eSEric Dumazet slow_path_clean:
9903d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
9913d13008eSEric Dumazet 			if (frag2 == frag)
9923d13008eSEric Dumazet 				break;
9933d13008eSEric Dumazet 			frag2->sk = NULL;
9943d13008eSEric Dumazet 			frag2->destructor = NULL;
9953d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
9963d13008eSEric Dumazet 		}
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds slow_path:
10001da177e4SLinus Torvalds 	/*
10011da177e4SLinus Torvalds 	 *	Fragment the datagram.
10021da177e4SLinus Torvalds 	 */
10031da177e4SLinus Torvalds 
10048a6a1f17SPablo Neira Ayuso 	ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom,
10058a6a1f17SPablo Neira Ayuso 		      LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id,
10068a6a1f17SPablo Neira Ayuso 		      &state);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	/*
10091da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
10101da177e4SLinus Torvalds 	 */
101179e49503SFlorian Westphal 
10128a6a1f17SPablo Neira Ayuso 	while (state.left > 0) {
10138a6a1f17SPablo Neira Ayuso 		frag = ip6_frag_next(skb, &state);
10148a6a1f17SPablo Neira Ayuso 		if (IS_ERR(frag)) {
10158a6a1f17SPablo Neira Ayuso 			err = PTR_ERR(frag);
10161da177e4SLinus Torvalds 			goto fail;
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 		/*
10201da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
10211da177e4SLinus Torvalds 		 */
1022a1ac9c8aSMartin KaFai Lau 		skb_set_delivery_time(frag, tstamp, mono_delivery_time);
10237d8c6e39SEric W. Biederman 		err = output(net, sk, frag);
10241da177e4SLinus Torvalds 		if (err)
10251da177e4SLinus Torvalds 			goto fail;
1026dafee490SWei Dong 
1027adf30907SEric Dumazet 		IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
10283bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGCREATES);
10291da177e4SLinus Torvalds 	}
1030adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
1031a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGOKS);
1032808db80aSEric Dumazet 	consume_skb(skb);
10331da177e4SLinus Torvalds 	return err;
10341da177e4SLinus Torvalds 
1035485fca66SFlorian Westphal fail_toobig:
1036485fca66SFlorian Westphal 	if (skb->sk && dst_allfrag(skb_dst(skb)))
1037aba54656SEric Dumazet 		sk_gso_disable(skb->sk);
1038485fca66SFlorian Westphal 
1039485fca66SFlorian Westphal 	icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1040485fca66SFlorian Westphal 	err = -EMSGSIZE;
1041485fca66SFlorian Westphal 
10421da177e4SLinus Torvalds fail:
1043adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
1044a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGFAILS);
10451da177e4SLinus Torvalds 	kfree_skb(skb);
10461da177e4SLinus Torvalds 	return err;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
ip6_rt_check(const struct rt6key * rt_key,const struct in6_addr * fl_addr,const struct in6_addr * addr_cache)1049b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key,
1050b71d1d42SEric Dumazet 			       const struct in6_addr *fl_addr,
1051b71d1d42SEric Dumazet 			       const struct in6_addr *addr_cache)
1052cf6b1982SYOSHIFUJI Hideaki {
1053a02cec21SEric Dumazet 	return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
105463159f29SIan Morris 		(!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
1055cf6b1982SYOSHIFUJI Hideaki }
1056cf6b1982SYOSHIFUJI Hideaki 
ip6_sk_dst_check(struct sock * sk,struct dst_entry * dst,const struct flowi6 * fl6)1057497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
1058497c615aSHerbert Xu 					  struct dst_entry *dst,
1059b71d1d42SEric Dumazet 					  const struct flowi6 *fl6)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
1062a963a37dSEric Dumazet 	struct rt6_info *rt;
10631da177e4SLinus Torvalds 
1064497c615aSHerbert Xu 	if (!dst)
1065497c615aSHerbert Xu 		goto out;
10661da177e4SLinus Torvalds 
1067a963a37dSEric Dumazet 	if (dst->ops->family != AF_INET6) {
1068a963a37dSEric Dumazet 		dst_release(dst);
1069a963a37dSEric Dumazet 		return NULL;
1070a963a37dSEric Dumazet 	}
1071a963a37dSEric Dumazet 
1072*797a4c1fSEric Dumazet 	rt = dst_rt6_info(dst);
10731da177e4SLinus Torvalds 	/* Yes, checking route validity in not connected
1074d76e60a5SDavid S. Miller 	 * case is not very simple. Take into account,
1075d76e60a5SDavid S. Miller 	 * that we do not support routing by source, TOS,
1076d76e60a5SDavid S. Miller 	 * and MSG_DONTROUTE		--ANK (980726)
1077d76e60a5SDavid S. Miller 	 *
1078cf6b1982SYOSHIFUJI Hideaki 	 * 1. ip6_rt_check(): If route was host route,
1079cf6b1982SYOSHIFUJI Hideaki 	 *    check that cached destination is current.
1080d76e60a5SDavid S. Miller 	 *    If it is network route, we still may
1081d76e60a5SDavid S. Miller 	 *    check its validity using saved pointer
1082d76e60a5SDavid S. Miller 	 *    to the last used address: daddr_cache.
1083d76e60a5SDavid S. Miller 	 *    We do not want to save whole address now,
1084d76e60a5SDavid S. Miller 	 *    (because main consumer of this service
1085d76e60a5SDavid S. Miller 	 *    is tcp, which has not this problem),
1086d76e60a5SDavid S. Miller 	 *    so that the last trick works only on connected
1087d76e60a5SDavid S. Miller 	 *    sockets.
1088d76e60a5SDavid S. Miller 	 * 2. oif also should be the same.
10891da177e4SLinus Torvalds 	 */
10904c9483b2SDavid S. Miller 	if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
10918e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES
10924c9483b2SDavid S. Miller 	    ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
10938e1ef0a9SYOSHIFUJI Hideaki #endif
109440867d74SDavid Ahern 	   (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
1095497c615aSHerbert Xu 		dst_release(dst);
1096497c615aSHerbert Xu 		dst = NULL;
10971da177e4SLinus Torvalds 	}
1098497c615aSHerbert Xu 
1099497c615aSHerbert Xu out:
1100497c615aSHerbert Xu 	return dst;
11011da177e4SLinus Torvalds }
1102497c615aSHerbert Xu 
ip6_dst_lookup_tail(struct net * net,const struct sock * sk,struct dst_entry ** dst,struct flowi6 * fl6)11033aef934fSEric Dumazet static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
11044c9483b2SDavid S. Miller 			       struct dst_entry **dst, struct flowi6 *fl6)
1105497c615aSHerbert Xu {
110669cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
110769cce1d1SDavid S. Miller 	struct neighbour *n;
110897cac082SDavid S. Miller 	struct rt6_info *rt;
110969cce1d1SDavid S. Miller #endif
111069cce1d1SDavid S. Miller 	int err;
11116f21c96aSPaolo Abeni 	int flags = 0;
11121da177e4SLinus Torvalds 
1113e16e888bSMarkus Stenberg 	/* The correct way to handle this would be to do
1114e16e888bSMarkus Stenberg 	 * ip6_route_get_saddr, and then ip6_route_output; however,
1115e16e888bSMarkus Stenberg 	 * the route-specific preferred source forces the
1116e16e888bSMarkus Stenberg 	 * ip6_route_output call _before_ ip6_route_get_saddr.
1117e16e888bSMarkus Stenberg 	 *
1118e16e888bSMarkus Stenberg 	 * In source specific routing (no src=any default route),
1119e16e888bSMarkus Stenberg 	 * ip6_route_output will fail given src=any saddr, though, so
1120e16e888bSMarkus Stenberg 	 * that's why we try it again later.
1121e16e888bSMarkus Stenberg 	 */
1122c305b9e6Szhang kai 	if (ipv6_addr_any(&fl6->saddr)) {
1123a68886a6SDavid Ahern 		struct fib6_info *from;
1124e16e888bSMarkus Stenberg 		struct rt6_info *rt;
1125e16e888bSMarkus Stenberg 
1126e16e888bSMarkus Stenberg 		*dst = ip6_route_output(net, sk, fl6);
1127*797a4c1fSEric Dumazet 		rt = (*dst)->error ? NULL : dst_rt6_info(*dst);
1128a68886a6SDavid Ahern 
1129a68886a6SDavid Ahern 		rcu_read_lock();
1130a68886a6SDavid Ahern 		from = rt ? rcu_dereference(rt->from) : NULL;
1131a68886a6SDavid Ahern 		err = ip6_route_get_saddr(net, from, &fl6->daddr,
1132e16e888bSMarkus Stenberg 					  sk ? inet6_sk(sk)->srcprefs : 0,
1133e7f3e5fbSNicolas Dichtel 					  fl6->flowi6_l3mdev,
1134e16e888bSMarkus Stenberg 					  &fl6->saddr);
1135a68886a6SDavid Ahern 		rcu_read_unlock();
1136a68886a6SDavid Ahern 
1137e16e888bSMarkus Stenberg 		if (err)
1138e16e888bSMarkus Stenberg 			goto out_err_release;
1139e16e888bSMarkus Stenberg 
1140e16e888bSMarkus Stenberg 		/* If we had an erroneous initial result, pretend it
1141e16e888bSMarkus Stenberg 		 * never existed and let the SA-enabled version take
1142e16e888bSMarkus Stenberg 		 * over.
1143e16e888bSMarkus Stenberg 		 */
1144c305b9e6Szhang kai 		if ((*dst)->error) {
1145e16e888bSMarkus Stenberg 			dst_release(*dst);
1146e16e888bSMarkus Stenberg 			*dst = NULL;
1147e16e888bSMarkus Stenberg 		}
11486f21c96aSPaolo Abeni 
11496f21c96aSPaolo Abeni 		if (fl6->flowi6_oif)
11506f21c96aSPaolo Abeni 			flags |= RT6_LOOKUP_F_IFACE;
1151e16e888bSMarkus Stenberg 	}
1152e16e888bSMarkus Stenberg 
115363159f29SIan Morris 	if (!*dst)
11546f21c96aSPaolo Abeni 		*dst = ip6_route_output_flags(net, sk, fl6, flags);
11551da177e4SLinus Torvalds 
1156e5d08d71SIan Morris 	err = (*dst)->error;
1157e5d08d71SIan Morris 	if (err)
11581da177e4SLinus Torvalds 		goto out_err_release;
11591da177e4SLinus Torvalds 
116095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
116195c385b4SNeil Horman 	/*
116295c385b4SNeil Horman 	 * Here if the dst entry we've looked up
116395c385b4SNeil Horman 	 * has a neighbour entry that is in the INCOMPLETE
116495c385b4SNeil Horman 	 * state and the src address from the flow is
116595c385b4SNeil Horman 	 * marked as OPTIMISTIC, we release the found
116695c385b4SNeil Horman 	 * dst entry and replace it instead with the
116795c385b4SNeil Horman 	 * dst entry of the nexthop router
116895c385b4SNeil Horman 	 */
1169*797a4c1fSEric Dumazet 	rt = dst_rt6_info(*dst);
117009eed119SEric Dumazet 	rcu_read_lock();
11712647a9b0SMartin KaFai Lau 	n = __ipv6_neigh_lookup_noref(rt->dst.dev,
11722647a9b0SMartin KaFai Lau 				      rt6_nexthop(rt, &fl6->daddr));
1173b071af52SEric Dumazet 	err = n && !(READ_ONCE(n->nud_state) & NUD_VALID) ? -EINVAL : 0;
117409eed119SEric Dumazet 	rcu_read_unlock();
1175707be1ffSYOSHIFUJI Hideaki / 吉藤英明 
1176707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	if (err) {
117795c385b4SNeil Horman 		struct inet6_ifaddr *ifp;
11784c9483b2SDavid S. Miller 		struct flowi6 fl_gw6;
117995c385b4SNeil Horman 		int redirect;
118095c385b4SNeil Horman 
11814c9483b2SDavid S. Miller 		ifp = ipv6_get_ifaddr(net, &fl6->saddr,
11821cab3da6SDaniel Lezcano 				      (*dst)->dev, 1);
118395c385b4SNeil Horman 
118495c385b4SNeil Horman 		redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
118595c385b4SNeil Horman 		if (ifp)
118695c385b4SNeil Horman 			in6_ifa_put(ifp);
118795c385b4SNeil Horman 
118895c385b4SNeil Horman 		if (redirect) {
118995c385b4SNeil Horman 			/*
119095c385b4SNeil Horman 			 * We need to get the dst entry for the
119195c385b4SNeil Horman 			 * default router instead
119295c385b4SNeil Horman 			 */
119395c385b4SNeil Horman 			dst_release(*dst);
11944c9483b2SDavid S. Miller 			memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
11954c9483b2SDavid S. Miller 			memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
11964c9483b2SDavid S. Miller 			*dst = ip6_route_output(net, sk, &fl_gw6);
1197e5d08d71SIan Morris 			err = (*dst)->error;
1198e5d08d71SIan Morris 			if (err)
119995c385b4SNeil Horman 				goto out_err_release;
120095c385b4SNeil Horman 		}
120195c385b4SNeil Horman 	}
120295c385b4SNeil Horman #endif
1203ec5e3b0aSJonathan T. Leighton 	if (ipv6_addr_v4mapped(&fl6->saddr) &&
120400ea1ceeSWillem de Bruijn 	    !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) {
120500ea1ceeSWillem de Bruijn 		err = -EAFNOSUPPORT;
120600ea1ceeSWillem de Bruijn 		goto out_err_release;
120700ea1ceeSWillem de Bruijn 	}
120895c385b4SNeil Horman 
12091da177e4SLinus Torvalds 	return 0;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds out_err_release:
12121da177e4SLinus Torvalds 	dst_release(*dst);
12131da177e4SLinus Torvalds 	*dst = NULL;
12148a966fc0SDavid Ahern 
12150d240e78SDavid Ahern 	if (err == -ENETUNREACH)
12160d240e78SDavid Ahern 		IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
12171da177e4SLinus Torvalds 	return err;
12181da177e4SLinus Torvalds }
121934a0b3cdSAdrian Bunk 
1220497c615aSHerbert Xu /**
1221497c615aSHerbert Xu  *	ip6_dst_lookup - perform route lookup on flow
1222b51cd7c8SAndrew Lunn  *	@net: Network namespace to perform lookup in
1223497c615aSHerbert Xu  *	@sk: socket which provides route info
1224497c615aSHerbert Xu  *	@dst: pointer to dst_entry * for result
12254c9483b2SDavid S. Miller  *	@fl6: flow to lookup
1226497c615aSHerbert Xu  *
1227497c615aSHerbert Xu  *	This function performs a route lookup on the given flow.
1228497c615aSHerbert Xu  *
1229497c615aSHerbert Xu  *	It returns zero on success, or a standard errno code on error.
1230497c615aSHerbert Xu  */
ip6_dst_lookup(struct net * net,struct sock * sk,struct dst_entry ** dst,struct flowi6 * fl6)1231343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1232343d60aaSRoopa Prabhu 		   struct flowi6 *fl6)
1233497c615aSHerbert Xu {
1234497c615aSHerbert Xu 	*dst = NULL;
1235343d60aaSRoopa Prabhu 	return ip6_dst_lookup_tail(net, sk, dst, fl6);
1236497c615aSHerbert Xu }
12373cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip6_dst_lookup);
12383cf3dc6cSArnaldo Carvalho de Melo 
1239497c615aSHerbert Xu /**
124068d0c6d3SDavid S. Miller  *	ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1241b51cd7c8SAndrew Lunn  *	@net: Network namespace to perform lookup in
124268d0c6d3SDavid S. Miller  *	@sk: socket which provides route info
12434c9483b2SDavid S. Miller  *	@fl6: flow to lookup
124468d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
124568d0c6d3SDavid S. Miller  *
124668d0c6d3SDavid S. Miller  *	This function performs a route lookup on the given flow.
124768d0c6d3SDavid S. Miller  *
124868d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
124968d0c6d3SDavid S. Miller  *	error code.
125068d0c6d3SDavid S. Miller  */
ip6_dst_lookup_flow(struct net * net,const struct sock * sk,struct flowi6 * fl6,const struct in6_addr * final_dst)1251c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
12520e0d44abSSteffen Klassert 				      const struct in6_addr *final_dst)
125368d0c6d3SDavid S. Miller {
125468d0c6d3SDavid S. Miller 	struct dst_entry *dst = NULL;
125568d0c6d3SDavid S. Miller 	int err;
125668d0c6d3SDavid S. Miller 
1257c4e85f73SSabrina Dubroca 	err = ip6_dst_lookup_tail(net, sk, &dst, fl6);
125868d0c6d3SDavid S. Miller 	if (err)
125968d0c6d3SDavid S. Miller 		return ERR_PTR(err);
126068d0c6d3SDavid S. Miller 	if (final_dst)
12614e3fd7a0SAlexey Dobriyan 		fl6->daddr = *final_dst;
12622774c131SDavid S. Miller 
1263c4e85f73SSabrina Dubroca 	return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0);
126468d0c6d3SDavid S. Miller }
126568d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
126668d0c6d3SDavid S. Miller 
126768d0c6d3SDavid S. Miller /**
126868d0c6d3SDavid S. Miller  *	ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
126968d0c6d3SDavid S. Miller  *	@sk: socket which provides the dst cache and route info
12704c9483b2SDavid S. Miller  *	@fl6: flow to lookup
127168d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
127296818159SAlexey Kodanev  *	@connected: whether @sk is connected or not
1273497c615aSHerbert Xu  *
1274497c615aSHerbert Xu  *	This function performs a route lookup on the given flow with the
1275497c615aSHerbert Xu  *	possibility of using the cached route in the socket if it is valid.
1276497c615aSHerbert Xu  *	It will take the socket dst lock when operating on the dst cache.
1277497c615aSHerbert Xu  *	As a result, this function can only be used in process context.
1278497c615aSHerbert Xu  *
127996818159SAlexey Kodanev  *	In addition, for a connected socket, cache the dst in the socket
128096818159SAlexey Kodanev  *	if the current cache is not valid.
128196818159SAlexey Kodanev  *
128268d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
128368d0c6d3SDavid S. Miller  *	error code.
1284497c615aSHerbert Xu  */
ip6_sk_dst_lookup_flow(struct sock * sk,struct flowi6 * fl6,const struct in6_addr * final_dst,bool connected)12854c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
128696818159SAlexey Kodanev 					 const struct in6_addr *final_dst,
128796818159SAlexey Kodanev 					 bool connected)
1288497c615aSHerbert Xu {
128968d0c6d3SDavid S. Miller 	struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
1290497c615aSHerbert Xu 
12914c9483b2SDavid S. Miller 	dst = ip6_sk_dst_check(sk, dst, fl6);
129296818159SAlexey Kodanev 	if (dst)
129396818159SAlexey Kodanev 		return dst;
129496818159SAlexey Kodanev 
1295c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst);
129696818159SAlexey Kodanev 	if (connected && !IS_ERR(dst))
129796818159SAlexey Kodanev 		ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6);
129868d0c6d3SDavid S. Miller 
129900bc0ef5SJakub Sitnicki 	return dst;
130068d0c6d3SDavid S. Miller }
130168d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
1302497c615aSHerbert Xu 
1303571912c6SMartin Varghese /**
1304571912c6SMartin Varghese  *      ip6_dst_lookup_tunnel - perform route lookup on tunnel
1305571912c6SMartin Varghese  *      @skb: Packet for which lookup is done
1306571912c6SMartin Varghese  *      @dev: Tunnel device
1307571912c6SMartin Varghese  *      @net: Network namespace of tunnel device
1308b51cd7c8SAndrew Lunn  *      @sock: Socket which provides route info
1309571912c6SMartin Varghese  *      @saddr: Memory to store the src ip address
1310571912c6SMartin Varghese  *      @info: Tunnel information
1311571912c6SMartin Varghese  *      @protocol: IP protocol
1312b51cd7c8SAndrew Lunn  *      @use_cache: Flag to enable cache usage
1313571912c6SMartin Varghese  *      This function performs a route lookup on a tunnel
1314571912c6SMartin Varghese  *
1315571912c6SMartin Varghese  *      It returns a valid dst pointer and stores src address to be used in
1316571912c6SMartin Varghese  *      tunnel in param saddr on success, else a pointer encoded error code.
1317571912c6SMartin Varghese  */
1318571912c6SMartin Varghese 
ip6_dst_lookup_tunnel(struct sk_buff * skb,struct net_device * dev,struct net * net,struct socket * sock,struct in6_addr * saddr,const struct ip_tunnel_info * info,u8 protocol,bool use_cache)1319571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb,
1320571912c6SMartin Varghese 					struct net_device *dev,
1321571912c6SMartin Varghese 					struct net *net,
1322571912c6SMartin Varghese 					struct socket *sock,
1323571912c6SMartin Varghese 					struct in6_addr *saddr,
1324571912c6SMartin Varghese 					const struct ip_tunnel_info *info,
1325571912c6SMartin Varghese 					u8 protocol,
1326571912c6SMartin Varghese 					bool use_cache)
1327571912c6SMartin Varghese {
1328571912c6SMartin Varghese 	struct dst_entry *dst = NULL;
1329571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1330571912c6SMartin Varghese 	struct dst_cache *dst_cache;
1331571912c6SMartin Varghese #endif
1332571912c6SMartin Varghese 	struct flowi6 fl6;
1333571912c6SMartin Varghese 	__u8 prio;
1334571912c6SMartin Varghese 
1335571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1336571912c6SMartin Varghese 	dst_cache = (struct dst_cache *)&info->dst_cache;
1337571912c6SMartin Varghese 	if (use_cache) {
1338571912c6SMartin Varghese 		dst = dst_cache_get_ip6(dst_cache, saddr);
1339571912c6SMartin Varghese 		if (dst)
1340571912c6SMartin Varghese 			return dst;
1341571912c6SMartin Varghese 	}
1342571912c6SMartin Varghese #endif
1343571912c6SMartin Varghese 	memset(&fl6, 0, sizeof(fl6));
1344571912c6SMartin Varghese 	fl6.flowi6_mark = skb->mark;
1345571912c6SMartin Varghese 	fl6.flowi6_proto = protocol;
1346571912c6SMartin Varghese 	fl6.daddr = info->key.u.ipv6.dst;
1347571912c6SMartin Varghese 	fl6.saddr = info->key.u.ipv6.src;
1348571912c6SMartin Varghese 	prio = info->key.tos;
1349ab7e2e0dSMatthias May 	fl6.flowlabel = ip6_make_flowinfo(prio, info->key.label);
1350571912c6SMartin Varghese 
1351571912c6SMartin Varghese 	dst = ipv6_stub->ipv6_dst_lookup_flow(net, sock->sk, &fl6,
1352571912c6SMartin Varghese 					      NULL);
1353571912c6SMartin Varghese 	if (IS_ERR(dst)) {
1354571912c6SMartin Varghese 		netdev_dbg(dev, "no route to %pI6\n", &fl6.daddr);
1355571912c6SMartin Varghese 		return ERR_PTR(-ENETUNREACH);
1356571912c6SMartin Varghese 	}
1357571912c6SMartin Varghese 	if (dst->dev == dev) { /* is this necessary? */
1358571912c6SMartin Varghese 		netdev_dbg(dev, "circular route to %pI6\n", &fl6.daddr);
1359571912c6SMartin Varghese 		dst_release(dst);
1360571912c6SMartin Varghese 		return ERR_PTR(-ELOOP);
1361571912c6SMartin Varghese 	}
1362571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1363571912c6SMartin Varghese 	if (use_cache)
1364571912c6SMartin Varghese 		dst_cache_set_ip6(dst_cache, dst, &fl6.saddr);
1365571912c6SMartin Varghese #endif
1366571912c6SMartin Varghese 	*saddr = fl6.saddr;
1367571912c6SMartin Varghese 	return dst;
1368571912c6SMartin Varghese }
1369571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip6_dst_lookup_tunnel);
1370571912c6SMartin Varghese 
ip6_opt_dup(struct ipv6_opt_hdr * src,gfp_t gfp)13710178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
13720178b695SHerbert Xu 					       gfp_t gfp)
13730178b695SHerbert Xu {
13740178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
13750178b695SHerbert Xu }
13760178b695SHerbert Xu 
ip6_rthdr_dup(struct ipv6_rt_hdr * src,gfp_t gfp)13770178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
13780178b695SHerbert Xu 						gfp_t gfp)
13790178b695SHerbert Xu {
13800178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
13810178b695SHerbert Xu }
13820178b695SHerbert Xu 
ip6_append_data_mtu(unsigned int * mtu,int * maxfraglen,unsigned int fragheaderlen,struct sk_buff * skb,struct rt6_info * rt,unsigned int orig_mtu)138375a493e6SHannes Frederic Sowa static void ip6_append_data_mtu(unsigned int *mtu,
13840c183379SGao feng 				int *maxfraglen,
13850c183379SGao feng 				unsigned int fragheaderlen,
13860c183379SGao feng 				struct sk_buff *skb,
138775a493e6SHannes Frederic Sowa 				struct rt6_info *rt,
1388e367c2d0Slucien 				unsigned int orig_mtu)
13890c183379SGao feng {
13900c183379SGao feng 	if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
139163159f29SIan Morris 		if (!skb) {
13920c183379SGao feng 			/* first fragment, reserve header_len */
1393e367c2d0Slucien 			*mtu = orig_mtu - rt->dst.header_len;
13940c183379SGao feng 
13950c183379SGao feng 		} else {
13960c183379SGao feng 			/*
13970c183379SGao feng 			 * this fragment is not first, the headers
13980c183379SGao feng 			 * space is regarded as data space.
13990c183379SGao feng 			 */
1400e367c2d0Slucien 			*mtu = orig_mtu;
14010c183379SGao feng 		}
14020c183379SGao feng 		*maxfraglen = ((*mtu - fragheaderlen) & ~7)
14030c183379SGao feng 			      + fragheaderlen - sizeof(struct frag_hdr);
14040c183379SGao feng 	}
14050c183379SGao feng }
14060c183379SGao feng 
ip6_setup_cork(struct sock * sk,struct inet_cork_full * cork,struct inet6_cork * v6_cork,struct ipcm6_cookie * ipc6,struct rt6_info * rt)1407366e41d9SVlad Yasevich static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
140826879da5SWei Wang 			  struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
1409f37a4cc6SPavel Begunkov 			  struct rt6_info *rt)
1410366e41d9SVlad Yasevich {
1411366e41d9SVlad Yasevich 	struct ipv6_pinfo *np = inet6_sk(sk);
1412366e41d9SVlad Yasevich 	unsigned int mtu;
1413d656b2eaSPavel Begunkov 	struct ipv6_txoptions *nopt, *opt = ipc6->opt;
1414366e41d9SVlad Yasevich 
141540ac240cSPavel Begunkov 	/* callers pass dst together with a reference, set it first so
141640ac240cSPavel Begunkov 	 * ip6_cork_release() can put it down even in case of an error.
141740ac240cSPavel Begunkov 	 */
141840ac240cSPavel Begunkov 	cork->base.dst = &rt->dst;
141940ac240cSPavel Begunkov 
1420366e41d9SVlad Yasevich 	/*
1421366e41d9SVlad Yasevich 	 * setup for corking
1422366e41d9SVlad Yasevich 	 */
1423366e41d9SVlad Yasevich 	if (opt) {
1424366e41d9SVlad Yasevich 		if (WARN_ON(v6_cork->opt))
1425366e41d9SVlad Yasevich 			return -EINVAL;
1426366e41d9SVlad Yasevich 
1427d656b2eaSPavel Begunkov 		nopt = v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation);
1428d656b2eaSPavel Begunkov 		if (unlikely(!nopt))
1429366e41d9SVlad Yasevich 			return -ENOBUFS;
1430366e41d9SVlad Yasevich 
1431d656b2eaSPavel Begunkov 		nopt->tot_len = sizeof(*opt);
1432d656b2eaSPavel Begunkov 		nopt->opt_flen = opt->opt_flen;
1433d656b2eaSPavel Begunkov 		nopt->opt_nflen = opt->opt_nflen;
1434366e41d9SVlad Yasevich 
1435d656b2eaSPavel Begunkov 		nopt->dst0opt = ip6_opt_dup(opt->dst0opt, sk->sk_allocation);
1436d656b2eaSPavel Begunkov 		if (opt->dst0opt && !nopt->dst0opt)
1437366e41d9SVlad Yasevich 			return -ENOBUFS;
1438366e41d9SVlad Yasevich 
1439d656b2eaSPavel Begunkov 		nopt->dst1opt = ip6_opt_dup(opt->dst1opt, sk->sk_allocation);
1440d656b2eaSPavel Begunkov 		if (opt->dst1opt && !nopt->dst1opt)
1441366e41d9SVlad Yasevich 			return -ENOBUFS;
1442366e41d9SVlad Yasevich 
1443d656b2eaSPavel Begunkov 		nopt->hopopt = ip6_opt_dup(opt->hopopt, sk->sk_allocation);
1444d656b2eaSPavel Begunkov 		if (opt->hopopt && !nopt->hopopt)
1445366e41d9SVlad Yasevich 			return -ENOBUFS;
1446366e41d9SVlad Yasevich 
1447d656b2eaSPavel Begunkov 		nopt->srcrt = ip6_rthdr_dup(opt->srcrt, sk->sk_allocation);
1448d656b2eaSPavel Begunkov 		if (opt->srcrt && !nopt->srcrt)
1449366e41d9SVlad Yasevich 			return -ENOBUFS;
1450366e41d9SVlad Yasevich 
1451366e41d9SVlad Yasevich 		/* need source address above miyazawa*/
1452366e41d9SVlad Yasevich 	}
145326879da5SWei Wang 	v6_cork->hop_limit = ipc6->hlimit;
145426879da5SWei Wang 	v6_cork->tclass = ipc6->tclass;
1455366e41d9SVlad Yasevich 	if (rt->dst.flags & DST_XFRM_TUNNEL)
1456366e41d9SVlad Yasevich 		mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1457749439bfSMike Maloney 		      READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst);
1458366e41d9SVlad Yasevich 	else
1459366e41d9SVlad Yasevich 		mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1460c02b3741SDavid S. Miller 			READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst));
1461366e41d9SVlad Yasevich 	if (np->frag_size < mtu) {
1462366e41d9SVlad Yasevich 		if (np->frag_size)
1463366e41d9SVlad Yasevich 			mtu = np->frag_size;
1464366e41d9SVlad Yasevich 	}
1465366e41d9SVlad Yasevich 	cork->base.fragsize = mtu;
1466fbf47813SWillem de Bruijn 	cork->base.gso_size = ipc6->gso_size;
1467678ca42dSWillem de Bruijn 	cork->base.tx_flags = 0;
1468c6af0c22SWillem de Bruijn 	cork->base.mark = ipc6->sockc.mark;
1469678ca42dSWillem de Bruijn 	sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags);
1470bec1f6f6SWillem de Bruijn 
14710f6c480fSDavid Miller 	if (dst_allfrag(xfrm_dst_path(&rt->dst)))
1472366e41d9SVlad Yasevich 		cork->base.flags |= IPCORK_ALLFRAG;
1473366e41d9SVlad Yasevich 	cork->base.length = 0;
1474366e41d9SVlad Yasevich 
14755fdaa88dSWillem de Bruijn 	cork->base.transmit_time = ipc6->sockc.transmit_time;
1476a818f75eSJesus Sanchez-Palencia 
1477366e41d9SVlad Yasevich 	return 0;
1478366e41d9SVlad Yasevich }
1479366e41d9SVlad Yasevich 
__ip6_append_data(struct sock * sk,struct sk_buff_head * queue,struct inet_cork_full * cork_full,struct inet6_cork * v6_cork,struct page_frag * pfrag,int getfrag (void * from,char * to,int offset,int len,int odd,struct sk_buff * skb),void * from,size_t length,int transhdrlen,unsigned int flags,struct ipcm6_cookie * ipc6)14800bbe84a6SVlad Yasevich static int __ip6_append_data(struct sock *sk,
14810bbe84a6SVlad Yasevich 			     struct sk_buff_head *queue,
1482f3b46a3eSPavel Begunkov 			     struct inet_cork_full *cork_full,
14830bbe84a6SVlad Yasevich 			     struct inet6_cork *v6_cork,
14840bbe84a6SVlad Yasevich 			     struct page_frag *pfrag,
14850bbe84a6SVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
14860bbe84a6SVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
1487f93431c8SWang Yufen 			     void *from, size_t length, int transhdrlen,
14885fdaa88dSWillem de Bruijn 			     unsigned int flags, struct ipcm6_cookie *ipc6)
14891da177e4SLinus Torvalds {
14900c183379SGao feng 	struct sk_buff *skb, *skb_prev = NULL;
1491f3b46a3eSPavel Begunkov 	struct inet_cork *cork = &cork_full->base;
1492f37a4cc6SPavel Begunkov 	struct flowi6 *fl6 = &cork_full->fl.u.ip6;
149310b8a3deSPaolo Abeni 	unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
1494b5947e5dSWillem de Bruijn 	struct ubuf_info *uarg = NULL;
14950bbe84a6SVlad Yasevich 	int exthdrlen = 0;
14960bbe84a6SVlad Yasevich 	int dst_exthdrlen = 0;
14971da177e4SLinus Torvalds 	int hh_len;
14981da177e4SLinus Torvalds 	int copy;
14991da177e4SLinus Torvalds 	int err;
15001da177e4SLinus Torvalds 	int offset = 0;
1501773ba4feSPavel Begunkov 	bool zc = false;
150209c2d251SWillem de Bruijn 	u32 tskey = 0;
1503*797a4c1fSEric Dumazet 	struct rt6_info *rt = dst_rt6_info(cork->dst);
150419a788bdSVadim Fedorenko 	bool paged, hold_tskey, extra_uref = false;
15050bbe84a6SVlad Yasevich 	struct ipv6_txoptions *opt = v6_cork->opt;
150632dce968SVlad Yasevich 	int csummode = CHECKSUM_NONE;
1507682b1a9dSHannes Frederic Sowa 	unsigned int maxnonfragsize, headersize;
15081f4c6eb2SEric Dumazet 	unsigned int wmem_alloc_delta = 0;
15091da177e4SLinus Torvalds 
15100bbe84a6SVlad Yasevich 	skb = skb_peek_tail(queue);
15110bbe84a6SVlad Yasevich 	if (!skb) {
15120bbe84a6SVlad Yasevich 		exthdrlen = opt ? opt->opt_flen : 0;
15137efdba5bSRomain KUNTZ 		dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
15141da177e4SLinus Torvalds 	}
15150bbe84a6SVlad Yasevich 
151615e36f5bSWillem de Bruijn 	paged = !!cork->gso_size;
1517bec1f6f6SWillem de Bruijn 	mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize;
1518e367c2d0Slucien 	orig_mtu = mtu;
15191da177e4SLinus Torvalds 
1520d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
15211da177e4SLinus Torvalds 
1522a1b05140SMasahide NAKAMURA 	fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
1523b4ce9277SHerbert Xu 			(opt ? opt->opt_nflen : 0);
15241da177e4SLinus Torvalds 
15254df98e76SHannes Frederic Sowa 	headersize = sizeof(struct ipv6hdr) +
15263a1cebe7SHannes Frederic Sowa 		     (opt ? opt->opt_flen + opt->opt_nflen : 0) +
15274df98e76SHannes Frederic Sowa 		     (dst_allfrag(&rt->dst) ?
15284df98e76SHannes Frederic Sowa 		      sizeof(struct frag_hdr) : 0) +
15294df98e76SHannes Frederic Sowa 		     rt->rt6i_nfheader_len;
15304df98e76SHannes Frederic Sowa 
15315e34af41STadeusz Struk 	if (mtu <= fragheaderlen ||
15325e34af41STadeusz Struk 	    ((mtu - fragheaderlen) & ~7) + fragheaderlen <= sizeof(struct frag_hdr))
15336596a022SJiri Bohac 		goto emsgsize;
15346596a022SJiri Bohac 
15356596a022SJiri Bohac 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
15366596a022SJiri Bohac 		     sizeof(struct frag_hdr);
15376596a022SJiri Bohac 
153810b8a3deSPaolo Abeni 	/* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit
153910b8a3deSPaolo Abeni 	 * the first fragment
154010b8a3deSPaolo Abeni 	 */
154110b8a3deSPaolo Abeni 	if (headersize + transhdrlen > mtu)
154210b8a3deSPaolo Abeni 		goto emsgsize;
154310b8a3deSPaolo Abeni 
154426879da5SWei Wang 	if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
15454df98e76SHannes Frederic Sowa 	    (sk->sk_protocol == IPPROTO_UDP ||
154613651224SJakub Kicinski 	     sk->sk_protocol == IPPROTO_ICMPV6 ||
15474df98e76SHannes Frederic Sowa 	     sk->sk_protocol == IPPROTO_RAW)) {
15484df98e76SHannes Frederic Sowa 		ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
15494df98e76SHannes Frederic Sowa 				sizeof(struct ipv6hdr));
15504df98e76SHannes Frederic Sowa 		goto emsgsize;
15514df98e76SHannes Frederic Sowa 	}
15524df98e76SHannes Frederic Sowa 
1553682b1a9dSHannes Frederic Sowa 	if (ip6_sk_ignore_df(sk))
1554682b1a9dSHannes Frederic Sowa 		maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1555682b1a9dSHannes Frederic Sowa 	else
1556682b1a9dSHannes Frederic Sowa 		maxnonfragsize = mtu;
1557682b1a9dSHannes Frederic Sowa 
15584df98e76SHannes Frederic Sowa 	if (cork->length + length > maxnonfragsize - headersize) {
15594df98e76SHannes Frederic Sowa emsgsize:
156010b8a3deSPaolo Abeni 		pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
156110b8a3deSPaolo Abeni 		ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
15621da177e4SLinus Torvalds 		return -EMSGSIZE;
15631da177e4SLinus Torvalds 	}
1564682b1a9dSHannes Frederic Sowa 
1565682b1a9dSHannes Frederic Sowa 	/* CHECKSUM_PARTIAL only with no extension headers and when
1566682b1a9dSHannes Frederic Sowa 	 * we are not going to fragment
1567682b1a9dSHannes Frederic Sowa 	 */
1568682b1a9dSHannes Frederic Sowa 	if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
1569682b1a9dSHannes Frederic Sowa 	    headersize == sizeof(struct ipv6hdr) &&
15702b89ed65SVlad Yasevich 	    length <= mtu - headersize &&
1571bec1f6f6SWillem de Bruijn 	    (!(flags & MSG_MORE) || cork->gso_size) &&
1572c8cd0989STom Herbert 	    rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1573682b1a9dSHannes Frederic Sowa 		csummode = CHECKSUM_PARTIAL;
15741da177e4SLinus Torvalds 
15751fd3ae8cSPavel Begunkov 	if ((flags & MSG_ZEROCOPY) && length) {
15761fd3ae8cSPavel Begunkov 		struct msghdr *msg = from;
15771fd3ae8cSPavel Begunkov 
15781fd3ae8cSPavel Begunkov 		if (getfrag == ip_generic_getfrag && msg->msg_ubuf) {
15791fd3ae8cSPavel Begunkov 			if (skb_zcopy(skb) && msg->msg_ubuf != skb_zcopy(skb))
15801fd3ae8cSPavel Begunkov 				return -EINVAL;
15811fd3ae8cSPavel Begunkov 
15821fd3ae8cSPavel Begunkov 			/* Leave uarg NULL if can't zerocopy, callers should
15831fd3ae8cSPavel Begunkov 			 * be able to handle it.
15841fd3ae8cSPavel Begunkov 			 */
15851fd3ae8cSPavel Begunkov 			if ((rt->dst.dev->features & NETIF_F_SG) &&
15861fd3ae8cSPavel Begunkov 			    csummode == CHECKSUM_PARTIAL) {
15871fd3ae8cSPavel Begunkov 				paged = true;
15881fd3ae8cSPavel Begunkov 				zc = true;
15891fd3ae8cSPavel Begunkov 				uarg = msg->msg_ubuf;
15901fd3ae8cSPavel Begunkov 			}
15911fd3ae8cSPavel Begunkov 		} else if (sock_flag(sk, SOCK_ZEROCOPY)) {
15928c793822SJonathan Lemon 			uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb));
1593b5947e5dSWillem de Bruijn 			if (!uarg)
1594b5947e5dSWillem de Bruijn 				return -ENOBUFS;
1595522924b5SWillem de Bruijn 			extra_uref = !skb_zcopy(skb);	/* only ref on new uarg */
1596b5947e5dSWillem de Bruijn 			if (rt->dst.dev->features & NETIF_F_SG &&
1597b5947e5dSWillem de Bruijn 			    csummode == CHECKSUM_PARTIAL) {
1598b5947e5dSWillem de Bruijn 				paged = true;
1599773ba4feSPavel Begunkov 				zc = true;
1600b5947e5dSWillem de Bruijn 			} else {
1601e7d2b510SPavel Begunkov 				uarg_to_msgzc(uarg)->zerocopy = 0;
160252900d22SWillem de Bruijn 				skb_zcopy_set(skb, uarg, &extra_uref);
1603b5947e5dSWillem de Bruijn 			}
1604b5947e5dSWillem de Bruijn 		}
16056d8192bdSDavid Howells 	} else if ((flags & MSG_SPLICE_PAGES) && length) {
1606cafbe182SEric Dumazet 		if (inet_test_bit(HDRINCL, sk))
16076d8192bdSDavid Howells 			return -EPERM;
16085a6f6873SDavid Howells 		if (rt->dst.dev->features & NETIF_F_SG &&
16095a6f6873SDavid Howells 		    getfrag == ip_generic_getfrag)
16106d8192bdSDavid Howells 			/* We need an empty buffer to attach stuff to */
16116d8192bdSDavid Howells 			paged = true;
16126d8192bdSDavid Howells 		else
16136d8192bdSDavid Howells 			flags &= ~MSG_SPLICE_PAGES;
16141fd3ae8cSPavel Begunkov 	}
1615b5947e5dSWillem de Bruijn 
161619a788bdSVadim Fedorenko 	hold_tskey = cork->tx_flags & SKBTX_ANY_TSTAMP &&
161719a788bdSVadim Fedorenko 		     READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID;
161819a788bdSVadim Fedorenko 	if (hold_tskey)
161919a788bdSVadim Fedorenko 		tskey = atomic_inc_return(&sk->sk_tskey) - 1;
162019a788bdSVadim Fedorenko 
16211da177e4SLinus Torvalds 	/*
16221da177e4SLinus Torvalds 	 * Let's try using as much space as possible.
16231da177e4SLinus Torvalds 	 * Use MTU if total length of the message fits into the MTU.
16241da177e4SLinus Torvalds 	 * Otherwise, we need to reserve fragment header and
16251da177e4SLinus Torvalds 	 * fragment alignment (= 8-15 octects, in total).
16261da177e4SLinus Torvalds 	 *
1627634a63e7SRandy Dunlap 	 * Note that we may need to "move" the data from the tail
16281da177e4SLinus Torvalds 	 * of the buffer to the new fragment when we split
16291da177e4SLinus Torvalds 	 * the message.
16301da177e4SLinus Torvalds 	 *
16311da177e4SLinus Torvalds 	 * FIXME: It may be fragmented into multiple chunks
16321da177e4SLinus Torvalds 	 *        at once if non-fragmentable extension headers
16331da177e4SLinus Torvalds 	 *        are too large.
16341da177e4SLinus Torvalds 	 * --yoshfuji
16351da177e4SLinus Torvalds 	 */
16361da177e4SLinus Torvalds 
16372811ebacSHannes Frederic Sowa 	cork->length += length;
16382811ebacSHannes Frederic Sowa 	if (!skb)
16391da177e4SLinus Torvalds 		goto alloc_new_skb;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	while (length > 0) {
16421da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
1643bdc712b4SDavid S. Miller 		copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
16441da177e4SLinus Torvalds 		if (copy < length)
16451da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 		if (copy <= 0) {
16481da177e4SLinus Torvalds 			char *data;
16491da177e4SLinus Torvalds 			unsigned int datalen;
16501da177e4SLinus Torvalds 			unsigned int fraglen;
16511da177e4SLinus Torvalds 			unsigned int fraggap;
16526d123b81SJakub Kicinski 			unsigned int alloclen, alloc_extra;
1653aba36930SWillem de Bruijn 			unsigned int pagedlen;
16541da177e4SLinus Torvalds alloc_new_skb:
16551da177e4SLinus Torvalds 			/* There's no room in the current skb */
16560c183379SGao feng 			if (skb)
16570c183379SGao feng 				fraggap = skb->len - maxfraglen;
16581da177e4SLinus Torvalds 			else
16591da177e4SLinus Torvalds 				fraggap = 0;
16600c183379SGao feng 			/* update mtu and maxfraglen if necessary */
166163159f29SIan Morris 			if (!skb || !skb_prev)
16620c183379SGao feng 				ip6_append_data_mtu(&mtu, &maxfraglen,
166375a493e6SHannes Frederic Sowa 						    fragheaderlen, skb, rt,
1664e367c2d0Slucien 						    orig_mtu);
16650c183379SGao feng 
16660c183379SGao feng 			skb_prev = skb;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 			/*
16691da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
16701da177e4SLinus Torvalds 			 * we know we need more fragment(s).
16711da177e4SLinus Torvalds 			 */
16721da177e4SLinus Torvalds 			datalen = length + fraggap;
16731da177e4SLinus Torvalds 
16740c183379SGao feng 			if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
16750c183379SGao feng 				datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
167615e36f5bSWillem de Bruijn 			fraglen = datalen + fragheaderlen;
1677aba36930SWillem de Bruijn 			pagedlen = 0;
167815e36f5bSWillem de Bruijn 
16796d123b81SJakub Kicinski 			alloc_extra = hh_len;
16806d123b81SJakub Kicinski 			alloc_extra += dst_exthdrlen;
16816d123b81SJakub Kicinski 			alloc_extra += rt->dst.trailer_len;
16826d123b81SJakub Kicinski 
16836d123b81SJakub Kicinski 			/* We just reserve space for fragment header.
16846d123b81SJakub Kicinski 			 * Note: this may be overallocation if the message
16856d123b81SJakub Kicinski 			 * (without MSG_MORE) fits into the MTU.
16866d123b81SJakub Kicinski 			 */
16876d123b81SJakub Kicinski 			alloc_extra += sizeof(struct frag_hdr);
16886d123b81SJakub Kicinski 
16891da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
1690d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
16911da177e4SLinus Torvalds 				alloclen = mtu;
16926d123b81SJakub Kicinski 			else if (!paged &&
16936d123b81SJakub Kicinski 				 (fraglen + alloc_extra < SKB_MAX_ALLOC ||
16946d123b81SJakub Kicinski 				  !(rt->dst.dev->features & NETIF_F_SG)))
169515e36f5bSWillem de Bruijn 				alloclen = fraglen;
169647cf8899SPavel Begunkov 			else {
1697773ba4feSPavel Begunkov 				alloclen = fragheaderlen + transhdrlen;
1698773ba4feSPavel Begunkov 				pagedlen = datalen - transhdrlen;
169915e36f5bSWillem de Bruijn 			}
17006d123b81SJakub Kicinski 			alloclen += alloc_extra;
1701299b0767SSteffen Klassert 
17020c183379SGao feng 			if (datalen != length + fraggap) {
17031da177e4SLinus Torvalds 				/*
17040c183379SGao feng 				 * this is not the last fragment, the trailer
17050c183379SGao feng 				 * space is regarded as data space.
17061da177e4SLinus Torvalds 				 */
17070c183379SGao feng 				datalen += rt->dst.trailer_len;
17080c183379SGao feng 			}
17090c183379SGao feng 
17100c183379SGao feng 			fraglen = datalen + fragheaderlen;
17111da177e4SLinus Torvalds 
171215e36f5bSWillem de Bruijn 			copy = datalen - transhdrlen - fraggap - pagedlen;
1713ce650a16SDavid Howells 			/* [!] NOTE: copy may be negative if pagedlen>0
1714ce650a16SDavid Howells 			 * because then the equation may reduces to -fraggap.
1715ce650a16SDavid Howells 			 */
1716ce650a16SDavid Howells 			if (copy < 0 && !(flags & MSG_SPLICE_PAGES)) {
1717232cd35dSEric Dumazet 				err = -EINVAL;
1718232cd35dSEric Dumazet 				goto error;
1719232cd35dSEric Dumazet 			}
17201da177e4SLinus Torvalds 			if (transhdrlen) {
17216d123b81SJakub Kicinski 				skb = sock_alloc_send_skb(sk, alloclen,
17221da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
17231da177e4SLinus Torvalds 			} else {
17241da177e4SLinus Torvalds 				skb = NULL;
17251f4c6eb2SEric Dumazet 				if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
17261da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
17276d123b81SJakub Kicinski 					skb = alloc_skb(alloclen,
17281da177e4SLinus Torvalds 							sk->sk_allocation);
172963159f29SIan Morris 				if (unlikely(!skb))
17301da177e4SLinus Torvalds 					err = -ENOBUFS;
17311da177e4SLinus Torvalds 			}
173263159f29SIan Morris 			if (!skb)
17331da177e4SLinus Torvalds 				goto error;
17341da177e4SLinus Torvalds 			/*
17351da177e4SLinus Torvalds 			 *	Fill in the control structures
17361da177e4SLinus Torvalds 			 */
17379c9c9ad5SHannes Frederic Sowa 			skb->protocol = htons(ETH_P_IPV6);
173832dce968SVlad Yasevich 			skb->ip_summed = csummode;
17391da177e4SLinus Torvalds 			skb->csum = 0;
17401f85851eSGao feng 			/* reserve for fragmentation and ipsec header */
17411f85851eSGao feng 			skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
17421f85851eSGao feng 				    dst_exthdrlen);
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 			/*
17451da177e4SLinus Torvalds 			 *	Find where to start putting bytes
17461da177e4SLinus Torvalds 			 */
174715e36f5bSWillem de Bruijn 			data = skb_put(skb, fraglen - pagedlen);
17481f85851eSGao feng 			skb_set_network_header(skb, exthdrlen);
17491f85851eSGao feng 			data += fragheaderlen;
1750b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1751b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
17521da177e4SLinus Torvalds 			if (fraggap) {
17531da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
17541da177e4SLinus Torvalds 					skb_prev, maxfraglen,
17558d5930dfSAl Viro 					data + transhdrlen, fraggap);
17561da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
17571da177e4SLinus Torvalds 							  skb->csum);
17581da177e4SLinus Torvalds 				data += fraggap;
1759e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
17601da177e4SLinus Torvalds 			}
1761232cd35dSEric Dumazet 			if (copy > 0 &&
1762232cd35dSEric Dumazet 			    getfrag(from, data + transhdrlen, offset,
1763232cd35dSEric Dumazet 				    copy, fraggap, skb) < 0) {
17641da177e4SLinus Torvalds 				err = -EFAULT;
17651da177e4SLinus Torvalds 				kfree_skb(skb);
17661da177e4SLinus Torvalds 				goto error;
1767ce650a16SDavid Howells 			} else if (flags & MSG_SPLICE_PAGES) {
1768ce650a16SDavid Howells 				copy = 0;
17691da177e4SLinus Torvalds 			}
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 			offset += copy;
177215e36f5bSWillem de Bruijn 			length -= copy + transhdrlen;
17731da177e4SLinus Torvalds 			transhdrlen = 0;
17741da177e4SLinus Torvalds 			exthdrlen = 0;
1775299b0767SSteffen Klassert 			dst_exthdrlen = 0;
17761da177e4SLinus Torvalds 
177752900d22SWillem de Bruijn 			/* Only the initial fragment is time stamped */
177852900d22SWillem de Bruijn 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
177952900d22SWillem de Bruijn 			cork->tx_flags = 0;
178052900d22SWillem de Bruijn 			skb_shinfo(skb)->tskey = tskey;
178152900d22SWillem de Bruijn 			tskey = 0;
178252900d22SWillem de Bruijn 			skb_zcopy_set(skb, uarg, &extra_uref);
178352900d22SWillem de Bruijn 
17840dec879fSJulian Anastasov 			if ((flags & MSG_CONFIRM) && !skb_prev)
17850dec879fSJulian Anastasov 				skb_set_dst_pending_confirm(skb, 1);
17860dec879fSJulian Anastasov 
17871da177e4SLinus Torvalds 			/*
17881da177e4SLinus Torvalds 			 * Put the packet on the pending queue
17891da177e4SLinus Torvalds 			 */
17901f4c6eb2SEric Dumazet 			if (!skb->destructor) {
17911f4c6eb2SEric Dumazet 				skb->destructor = sock_wfree;
17921f4c6eb2SEric Dumazet 				skb->sk = sk;
17931f4c6eb2SEric Dumazet 				wmem_alloc_delta += skb->truesize;
17941f4c6eb2SEric Dumazet 			}
17950bbe84a6SVlad Yasevich 			__skb_queue_tail(queue, skb);
17961da177e4SLinus Torvalds 			continue;
17971da177e4SLinus Torvalds 		}
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 		if (copy > length)
18001da177e4SLinus Torvalds 			copy = length;
18011da177e4SLinus Torvalds 
1802113f99c3SWillem de Bruijn 		if (!(rt->dst.dev->features&NETIF_F_SG) &&
1803113f99c3SWillem de Bruijn 		    skb_tailroom(skb) >= copy) {
18041da177e4SLinus Torvalds 			unsigned int off;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 			off = skb->len;
18071da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
18081da177e4SLinus Torvalds 						offset, copy, off, skb) < 0) {
18091da177e4SLinus Torvalds 				__skb_trim(skb, off);
18101da177e4SLinus Torvalds 				err = -EFAULT;
18111da177e4SLinus Torvalds 				goto error;
18121da177e4SLinus Torvalds 			}
18136d8192bdSDavid Howells 		} else if (flags & MSG_SPLICE_PAGES) {
18146d8192bdSDavid Howells 			struct msghdr *msg = from;
18156d8192bdSDavid Howells 
1816ce650a16SDavid Howells 			err = -EIO;
1817ce650a16SDavid Howells 			if (WARN_ON_ONCE(copy > msg->msg_iter.count))
1818ce650a16SDavid Howells 				goto error;
1819ce650a16SDavid Howells 
18206d8192bdSDavid Howells 			err = skb_splice_from_iter(skb, &msg->msg_iter, copy,
18216d8192bdSDavid Howells 						   sk->sk_allocation);
18226d8192bdSDavid Howells 			if (err < 0)
18236d8192bdSDavid Howells 				goto error;
18246d8192bdSDavid Howells 			copy = err;
18256d8192bdSDavid Howells 			wmem_alloc_delta += copy;
18261fd3ae8cSPavel Begunkov 		} else if (!zc) {
18271da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 			err = -ENOMEM;
18305640f768SEric Dumazet 			if (!sk_page_frag_refill(sk, pfrag))
18311da177e4SLinus Torvalds 				goto error;
18321da177e4SLinus Torvalds 
18331fd3ae8cSPavel Begunkov 			skb_zcopy_downgrade_managed(skb);
18345640f768SEric Dumazet 			if (!skb_can_coalesce(skb, i, pfrag->page,
18355640f768SEric Dumazet 					      pfrag->offset)) {
18361da177e4SLinus Torvalds 				err = -EMSGSIZE;
18375640f768SEric Dumazet 				if (i == MAX_SKB_FRAGS)
18381da177e4SLinus Torvalds 					goto error;
18395640f768SEric Dumazet 
18405640f768SEric Dumazet 				__skb_fill_page_desc(skb, i, pfrag->page,
18415640f768SEric Dumazet 						     pfrag->offset, 0);
18425640f768SEric Dumazet 				skb_shinfo(skb)->nr_frags = ++i;
18435640f768SEric Dumazet 				get_page(pfrag->page);
18441da177e4SLinus Torvalds 			}
18455640f768SEric Dumazet 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
18469e903e08SEric Dumazet 			if (getfrag(from,
18475640f768SEric Dumazet 				    page_address(pfrag->page) + pfrag->offset,
18485640f768SEric Dumazet 				    offset, copy, skb->len, skb) < 0)
18495640f768SEric Dumazet 				goto error_efault;
18505640f768SEric Dumazet 
18515640f768SEric Dumazet 			pfrag->offset += copy;
18525640f768SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
18531da177e4SLinus Torvalds 			skb->len += copy;
18541da177e4SLinus Torvalds 			skb->data_len += copy;
1855f945fa7aSHerbert Xu 			skb->truesize += copy;
18561f4c6eb2SEric Dumazet 			wmem_alloc_delta += copy;
1857b5947e5dSWillem de Bruijn 		} else {
1858b5947e5dSWillem de Bruijn 			err = skb_zerocopy_iter_dgram(skb, from, copy);
1859b5947e5dSWillem de Bruijn 			if (err < 0)
1860b5947e5dSWillem de Bruijn 				goto error;
18611da177e4SLinus Torvalds 		}
18621da177e4SLinus Torvalds 		offset += copy;
18631da177e4SLinus Torvalds 		length -= copy;
18641da177e4SLinus Torvalds 	}
18655640f768SEric Dumazet 
18669e8445a5SPaolo Abeni 	if (wmem_alloc_delta)
18671f4c6eb2SEric Dumazet 		refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
18681da177e4SLinus Torvalds 	return 0;
18695640f768SEric Dumazet 
18705640f768SEric Dumazet error_efault:
18715640f768SEric Dumazet 	err = -EFAULT;
18721da177e4SLinus Torvalds error:
18738e044917SJonathan Lemon 	net_zcopy_put_abort(uarg, extra_uref);
1874bdc712b4SDavid S. Miller 	cork->length -= length;
18753bd653c8SDenis V. Lunev 	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
18761f4c6eb2SEric Dumazet 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
187719a788bdSVadim Fedorenko 	if (hold_tskey)
187819a788bdSVadim Fedorenko 		atomic_dec(&sk->sk_tskey);
18791da177e4SLinus Torvalds 	return err;
18801da177e4SLinus Torvalds }
18810bbe84a6SVlad Yasevich 
ip6_append_data(struct sock * sk,int getfrag (void * from,char * to,int offset,int len,int odd,struct sk_buff * skb),void * from,size_t length,int transhdrlen,struct ipcm6_cookie * ipc6,struct flowi6 * fl6,struct rt6_info * rt,unsigned int flags)18820bbe84a6SVlad Yasevich int ip6_append_data(struct sock *sk,
18830bbe84a6SVlad Yasevich 		    int getfrag(void *from, char *to, int offset, int len,
18840bbe84a6SVlad Yasevich 				int odd, struct sk_buff *skb),
1885f93431c8SWang Yufen 		    void *from, size_t length, int transhdrlen,
188626879da5SWei Wang 		    struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
18875fdaa88dSWillem de Bruijn 		    struct rt6_info *rt, unsigned int flags)
18880bbe84a6SVlad Yasevich {
18890bbe84a6SVlad Yasevich 	struct inet_sock *inet = inet_sk(sk);
18900bbe84a6SVlad Yasevich 	struct ipv6_pinfo *np = inet6_sk(sk);
18910bbe84a6SVlad Yasevich 	int exthdrlen;
18920bbe84a6SVlad Yasevich 	int err;
18930bbe84a6SVlad Yasevich 
18940bbe84a6SVlad Yasevich 	if (flags&MSG_PROBE)
18950bbe84a6SVlad Yasevich 		return 0;
18960bbe84a6SVlad Yasevich 	if (skb_queue_empty(&sk->sk_write_queue)) {
18970bbe84a6SVlad Yasevich 		/*
18980bbe84a6SVlad Yasevich 		 * setup for corking
18990bbe84a6SVlad Yasevich 		 */
190040ac240cSPavel Begunkov 		dst_hold(&rt->dst);
190126879da5SWei Wang 		err = ip6_setup_cork(sk, &inet->cork, &np->cork,
1902f37a4cc6SPavel Begunkov 				     ipc6, rt);
19030bbe84a6SVlad Yasevich 		if (err)
19040bbe84a6SVlad Yasevich 			return err;
19050bbe84a6SVlad Yasevich 
1906f37a4cc6SPavel Begunkov 		inet->cork.fl.u.ip6 = *fl6;
190726879da5SWei Wang 		exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
19080bbe84a6SVlad Yasevich 		length += exthdrlen;
19090bbe84a6SVlad Yasevich 		transhdrlen += exthdrlen;
19100bbe84a6SVlad Yasevich 	} else {
19110bbe84a6SVlad Yasevich 		transhdrlen = 0;
19120bbe84a6SVlad Yasevich 	}
19130bbe84a6SVlad Yasevich 
1914f37a4cc6SPavel Begunkov 	return __ip6_append_data(sk, &sk->sk_write_queue, &inet->cork,
19150bbe84a6SVlad Yasevich 				 &np->cork, sk_page_frag(sk), getfrag,
19165fdaa88dSWillem de Bruijn 				 from, length, transhdrlen, flags, ipc6);
19170bbe84a6SVlad Yasevich }
1918a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data);
19191da177e4SLinus Torvalds 
ip6_cork_steal_dst(struct sk_buff * skb,struct inet_cork_full * cork)1920cd3c7480SPavel Begunkov static void ip6_cork_steal_dst(struct sk_buff *skb, struct inet_cork_full *cork)
1921cd3c7480SPavel Begunkov {
1922cd3c7480SPavel Begunkov 	struct dst_entry *dst = cork->base.dst;
1923cd3c7480SPavel Begunkov 
1924cd3c7480SPavel Begunkov 	cork->base.dst = NULL;
1925cd3c7480SPavel Begunkov 	cork->base.flags &= ~IPCORK_ALLFRAG;
1926cd3c7480SPavel Begunkov 	skb_dst_set(skb, dst);
1927cd3c7480SPavel Begunkov }
1928cd3c7480SPavel Begunkov 
ip6_cork_release(struct inet_cork_full * cork,struct inet6_cork * v6_cork)1929366e41d9SVlad Yasevich static void ip6_cork_release(struct inet_cork_full *cork,
1930366e41d9SVlad Yasevich 			     struct inet6_cork *v6_cork)
1931bf138862SPavel Emelyanov {
1932366e41d9SVlad Yasevich 	if (v6_cork->opt) {
1933d656b2eaSPavel Begunkov 		struct ipv6_txoptions *opt = v6_cork->opt;
1934d656b2eaSPavel Begunkov 
1935d656b2eaSPavel Begunkov 		kfree(opt->dst0opt);
1936d656b2eaSPavel Begunkov 		kfree(opt->dst1opt);
1937d656b2eaSPavel Begunkov 		kfree(opt->hopopt);
1938d656b2eaSPavel Begunkov 		kfree(opt->srcrt);
1939d656b2eaSPavel Begunkov 		kfree(opt);
1940366e41d9SVlad Yasevich 		v6_cork->opt = NULL;
19410178b695SHerbert Xu 	}
19420178b695SHerbert Xu 
1943366e41d9SVlad Yasevich 	if (cork->base.dst) {
1944366e41d9SVlad Yasevich 		dst_release(cork->base.dst);
1945366e41d9SVlad Yasevich 		cork->base.dst = NULL;
1946366e41d9SVlad Yasevich 		cork->base.flags &= ~IPCORK_ALLFRAG;
1947bf138862SPavel Emelyanov 	}
1948bf138862SPavel Emelyanov }
1949bf138862SPavel Emelyanov 
__ip6_make_skb(struct sock * sk,struct sk_buff_head * queue,struct inet_cork_full * cork,struct inet6_cork * v6_cork)19506422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk,
19516422398cSVlad Yasevich 			       struct sk_buff_head *queue,
19526422398cSVlad Yasevich 			       struct inet_cork_full *cork,
19536422398cSVlad Yasevich 			       struct inet6_cork *v6_cork)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
19561da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
1957b60d4e58SPavel Begunkov 	struct in6_addr *final_dst;
19581da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
19593bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
19601da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
19616422398cSVlad Yasevich 	struct ipv6_txoptions *opt = v6_cork->opt;
1962*797a4c1fSEric Dumazet 	struct rt6_info *rt = dst_rt6_info(cork->base.dst);
19636422398cSVlad Yasevich 	struct flowi6 *fl6 = &cork->fl.u.ip6;
19644c9483b2SDavid S. Miller 	unsigned char proto = fl6->flowi6_proto;
19651da177e4SLinus Torvalds 
19666422398cSVlad Yasevich 	skb = __skb_dequeue(queue);
196763159f29SIan Morris 	if (!skb)
19681da177e4SLinus Torvalds 		goto out;
19691da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1972d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1973bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
19746422398cSVlad Yasevich 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1975cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
19761da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
19771da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
19781da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
19791da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
19801da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
19811da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
19821da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
19831da177e4SLinus Torvalds 	}
19841da177e4SLinus Torvalds 
198528a89453SHerbert Xu 	/* Allow local fragmentation. */
198660ff7467SWANG Cong 	skb->ignore_df = ip6_sk_ignore_df(sk);
1987cfe1fc77SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_header_len(skb));
1988b60d4e58SPavel Begunkov 
1989b60d4e58SPavel Begunkov 	final_dst = &fl6->daddr;
19901da177e4SLinus Torvalds 	if (opt && opt->opt_flen)
19911da177e4SLinus Torvalds 		ipv6_push_frag_opts(skb, opt, &proto);
19921da177e4SLinus Torvalds 	if (opt && opt->opt_nflen)
1993613fa3caSDavid Lebrun 		ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr);
19941da177e4SLinus Torvalds 
1995e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
1996e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
19970660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
19981da177e4SLinus Torvalds 
19996422398cSVlad Yasevich 	ip6_flow_hdr(hdr, v6_cork->tclass,
2000cb1ce2efSTom Herbert 		     ip6_make_flowlabel(net, skb, fl6->flowlabel,
2001513674b5SShaohua Li 					ip6_autoflowlabel(net, np), fl6));
20026422398cSVlad Yasevich 	hdr->hop_limit = v6_cork->hop_limit;
20031da177e4SLinus Torvalds 	hdr->nexthdr = proto;
20044e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
20054e3fd7a0SAlexey Dobriyan 	hdr->daddr = *final_dst;
20061da177e4SLinus Torvalds 
2007a2c2064fSPatrick McHardy 	skb->priority = sk->sk_priority;
2008c6af0c22SWillem de Bruijn 	skb->mark = cork->base.mark;
2009a818f75eSJesus Sanchez-Palencia 	skb->tstamp = cork->base.transmit_time;
2010a818f75eSJesus Sanchez-Palencia 
2011cd3c7480SPavel Begunkov 	ip6_cork_steal_dst(skb, cork);
201256712f74SHeng Guo 	IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
201314878f75SDavid L Stevens 	if (proto == IPPROTO_ICMPV6) {
2014adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
2015ea30388bSZiyang Xuan 		u8 icmp6_type;
201614878f75SDavid L Stevens 
2017cafbe182SEric Dumazet 		if (sk->sk_socket->type == SOCK_RAW &&
201868c8ba16SShigeru Yoshida 		   !(fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH))
2019ea30388bSZiyang Xuan 			icmp6_type = fl6->fl6_icmp_type;
2020ea30388bSZiyang Xuan 		else
2021ea30388bSZiyang Xuan 			icmp6_type = icmp6_hdr(skb)->icmp6_type;
2022ea30388bSZiyang Xuan 		ICMP6MSGOUT_INC_STATS(net, idev, icmp6_type);
202343a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
202414878f75SDavid L Stevens 	}
202514878f75SDavid L Stevens 
20266422398cSVlad Yasevich 	ip6_cork_release(cork, v6_cork);
20276422398cSVlad Yasevich out:
20286422398cSVlad Yasevich 	return skb;
20296422398cSVlad Yasevich }
20306422398cSVlad Yasevich 
ip6_send_skb(struct sk_buff * skb)20316422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb)
20326422398cSVlad Yasevich {
20336422398cSVlad Yasevich 	struct net *net = sock_net(skb->sk);
2034*797a4c1fSEric Dumazet 	struct rt6_info *rt = dst_rt6_info(skb_dst(skb));
20356422398cSVlad Yasevich 	int err;
20366422398cSVlad Yasevich 
2037af1dde07SEric Dumazet 	rcu_read_lock();
203833224b16SEric W. Biederman 	err = ip6_local_out(net, skb->sk, skb);
20391da177e4SLinus Torvalds 	if (err) {
20401da177e4SLinus Torvalds 		if (err > 0)
20416ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
20421da177e4SLinus Torvalds 		if (err)
20436422398cSVlad Yasevich 			IP6_INC_STATS(net, rt->rt6i_idev,
20446422398cSVlad Yasevich 				      IPSTATS_MIB_OUTDISCARDS);
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 
2047af1dde07SEric Dumazet 	rcu_read_unlock();
20481da177e4SLinus Torvalds 	return err;
20496422398cSVlad Yasevich }
20506422398cSVlad Yasevich 
ip6_push_pending_frames(struct sock * sk)20516422398cSVlad Yasevich int ip6_push_pending_frames(struct sock *sk)
20526422398cSVlad Yasevich {
20536422398cSVlad Yasevich 	struct sk_buff *skb;
20546422398cSVlad Yasevich 
20556422398cSVlad Yasevich 	skb = ip6_finish_skb(sk);
20566422398cSVlad Yasevich 	if (!skb)
20576422398cSVlad Yasevich 		return 0;
20586422398cSVlad Yasevich 
20596422398cSVlad Yasevich 	return ip6_send_skb(skb);
20601da177e4SLinus Torvalds }
2061a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
20621da177e4SLinus Torvalds 
__ip6_flush_pending_frames(struct sock * sk,struct sk_buff_head * queue,struct inet_cork_full * cork,struct inet6_cork * v6_cork)20630bbe84a6SVlad Yasevich static void __ip6_flush_pending_frames(struct sock *sk,
20646422398cSVlad Yasevich 				       struct sk_buff_head *queue,
20656422398cSVlad Yasevich 				       struct inet_cork_full *cork,
20666422398cSVlad Yasevich 				       struct inet6_cork *v6_cork)
20671da177e4SLinus Torvalds {
20681da177e4SLinus Torvalds 	struct sk_buff *skb;
20691da177e4SLinus Torvalds 
20700bbe84a6SVlad Yasevich 	while ((skb = __skb_dequeue_tail(queue)) != NULL) {
2071adf30907SEric Dumazet 		if (skb_dst(skb))
2072adf30907SEric Dumazet 			IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
2073a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_OUTDISCARDS);
20741da177e4SLinus Torvalds 		kfree_skb(skb);
20751da177e4SLinus Torvalds 	}
20761da177e4SLinus Torvalds 
20776422398cSVlad Yasevich 	ip6_cork_release(cork, v6_cork);
20781da177e4SLinus Torvalds }
20790bbe84a6SVlad Yasevich 
ip6_flush_pending_frames(struct sock * sk)20800bbe84a6SVlad Yasevich void ip6_flush_pending_frames(struct sock *sk)
20810bbe84a6SVlad Yasevich {
20826422398cSVlad Yasevich 	__ip6_flush_pending_frames(sk, &sk->sk_write_queue,
20836422398cSVlad Yasevich 				   &inet_sk(sk)->cork, &inet6_sk(sk)->cork);
20840bbe84a6SVlad Yasevich }
2085a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
20866422398cSVlad Yasevich 
ip6_make_skb(struct sock * sk,int getfrag (void * from,char * to,int offset,int len,int odd,struct sk_buff * skb),void * from,size_t length,int transhdrlen,struct ipcm6_cookie * ipc6,struct rt6_info * rt,unsigned int flags,struct inet_cork_full * cork)20876422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk,
20886422398cSVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
20896422398cSVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
2090f93431c8SWang Yufen 			     void *from, size_t length, int transhdrlen,
2091f37a4cc6SPavel Begunkov 			     struct ipcm6_cookie *ipc6, struct rt6_info *rt,
2092f37a4cc6SPavel Begunkov 			     unsigned int flags, struct inet_cork_full *cork)
20936422398cSVlad Yasevich {
20946422398cSVlad Yasevich 	struct inet6_cork v6_cork;
20956422398cSVlad Yasevich 	struct sk_buff_head queue;
209626879da5SWei Wang 	int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
20976422398cSVlad Yasevich 	int err;
20986422398cSVlad Yasevich 
209940ac240cSPavel Begunkov 	if (flags & MSG_PROBE) {
210040ac240cSPavel Begunkov 		dst_release(&rt->dst);
21016422398cSVlad Yasevich 		return NULL;
210240ac240cSPavel Begunkov 	}
21036422398cSVlad Yasevich 
21046422398cSVlad Yasevich 	__skb_queue_head_init(&queue);
21056422398cSVlad Yasevich 
21061cd7884dSWillem de Bruijn 	cork->base.flags = 0;
21071cd7884dSWillem de Bruijn 	cork->base.addr = 0;
21081cd7884dSWillem de Bruijn 	cork->base.opt = NULL;
21096422398cSVlad Yasevich 	v6_cork.opt = NULL;
2110f37a4cc6SPavel Begunkov 	err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt);
2111862c03eeSEric Dumazet 	if (err) {
21121cd7884dSWillem de Bruijn 		ip6_cork_release(cork, &v6_cork);
21136422398cSVlad Yasevich 		return ERR_PTR(err);
2114862c03eeSEric Dumazet 	}
211526879da5SWei Wang 	if (ipc6->dontfrag < 0)
211626879da5SWei Wang 		ipc6->dontfrag = inet6_sk(sk)->dontfrag;
21176422398cSVlad Yasevich 
2118f37a4cc6SPavel Begunkov 	err = __ip6_append_data(sk, &queue, cork, &v6_cork,
21196422398cSVlad Yasevich 				&current->task_frag, getfrag, from,
21206422398cSVlad Yasevich 				length + exthdrlen, transhdrlen + exthdrlen,
21215fdaa88dSWillem de Bruijn 				flags, ipc6);
21226422398cSVlad Yasevich 	if (err) {
21231cd7884dSWillem de Bruijn 		__ip6_flush_pending_frames(sk, &queue, cork, &v6_cork);
21246422398cSVlad Yasevich 		return ERR_PTR(err);
21256422398cSVlad Yasevich 	}
21266422398cSVlad Yasevich 
21271cd7884dSWillem de Bruijn 	return __ip6_make_skb(sk, &queue, cork, &v6_cork);
21286422398cSVlad Yasevich }
2129