xref: /openbmc/linux/net/ipv6/ip6_output.c (revision 634a63e73f059439bb44d1c26ba742ee6311957e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	IPv6 output functions
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Based on linux/net/ipv4/ip_output.c
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Changes:
121da177e4SLinus Torvalds  *	A.N.Kuznetsov	:	airthmetics in fragmentation.
131da177e4SLinus Torvalds  *				extension headers are implemented.
141da177e4SLinus Torvalds  *				route changes now work.
151da177e4SLinus Torvalds  *				ip6_forward does not confuse sniffers.
161da177e4SLinus Torvalds  *				etc.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *      H. von Brand    :       Added missing #include <linux/string.h>
191da177e4SLinus Torvalds  *	Imran Patel	:	frag id should be in NBO
201da177e4SLinus Torvalds  *      Kazunori MIYAZAWA @USAGI
211da177e4SLinus Torvalds  *			:       add ip6_append_data and related functions
221da177e4SLinus Torvalds  *				for datagram xmit
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <linux/errno.h>
26ef76bc23SHerbert Xu #include <linux/kernel.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/socket.h>
291da177e4SLinus Torvalds #include <linux/net.h>
301da177e4SLinus Torvalds #include <linux/netdevice.h>
311da177e4SLinus Torvalds #include <linux/if_arp.h>
321da177e4SLinus Torvalds #include <linux/in6.h>
331da177e4SLinus Torvalds #include <linux/tcp.h>
341da177e4SLinus Torvalds #include <linux/route.h>
35b59f45d0SHerbert Xu #include <linux/module.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
371da177e4SLinus Torvalds 
3833b48679SDaniel Mack #include <linux/bpf-cgroup.h>
391da177e4SLinus Torvalds #include <linux/netfilter.h>
401da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <net/sock.h>
431da177e4SLinus Torvalds #include <net/snmp.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <net/ipv6.h>
461da177e4SLinus Torvalds #include <net/ndisc.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/ip6_route.h>
491da177e4SLinus Torvalds #include <net/addrconf.h>
501da177e4SLinus Torvalds #include <net/rawv6.h>
511da177e4SLinus Torvalds #include <net/icmp.h>
521da177e4SLinus Torvalds #include <net/xfrm.h>
531da177e4SLinus Torvalds #include <net/checksum.h>
547bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
55ca254490SDavid Ahern #include <net/l3mdev.h>
5614972cbdSRoopa Prabhu #include <net/lwtunnel.h>
57571912c6SMartin Varghese #include <net/ip_tunnels.h>
581da177e4SLinus Torvalds 
597d8c6e39SEric W. Biederman static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
601da177e4SLinus Torvalds {
61adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
621da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
639b1c1ef1SNicolas Dichtel 	const struct in6_addr *nexthop;
64f6b72b62SDavid S. Miller 	struct neighbour *neigh;
656fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	int ret;
661da177e4SLinus Torvalds 
670660e03fSArnaldo Carvalho de Melo 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
68adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
691da177e4SLinus Torvalds 
707026b1ddSDavid Miller 		if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
718571ab47SYuval Mintz 		    ((mroute6_is_socket(net, skb) &&
72bd91b8bfSBenjamin Thery 		     !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
730660e03fSArnaldo Carvalho de Melo 		     ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
747bc570c8SYOSHIFUJI Hideaki 					 &ipv6_hdr(skb)->saddr))) {
751da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 			/* Do not check for IFF_ALLMULTI; multicast routing
781da177e4SLinus Torvalds 			   is not supported in any case.
791da177e4SLinus Torvalds 			 */
801da177e4SLinus Torvalds 			if (newskb)
81b2e0b385SJan Engelhardt 				NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
8229a26a56SEric W. Biederman 					net, sk, newskb, NULL, newskb->dev,
8395603e22SMichel Machado 					dev_loopback_xmit);
841da177e4SLinus Torvalds 
850660e03fSArnaldo Carvalho de Melo 			if (ipv6_hdr(skb)->hop_limit == 0) {
8678126c41SEric W. Biederman 				IP6_INC_STATS(net, idev,
873bd653c8SDenis V. Lunev 					      IPSTATS_MIB_OUTDISCARDS);
881da177e4SLinus Torvalds 				kfree_skb(skb);
891da177e4SLinus Torvalds 				return 0;
901da177e4SLinus Torvalds 			}
911da177e4SLinus Torvalds 		}
921da177e4SLinus Torvalds 
9378126c41SEric W. Biederman 		IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
94dd408515SHannes Frederic Sowa 
95dd408515SHannes Frederic Sowa 		if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
96dd408515SHannes Frederic Sowa 		    IPV6_ADDR_SCOPE_NODELOCAL &&
97dd408515SHannes Frederic Sowa 		    !(dev->flags & IFF_LOOPBACK)) {
98dd408515SHannes Frederic Sowa 			kfree_skb(skb);
99dd408515SHannes Frederic Sowa 			return 0;
100dd408515SHannes Frederic Sowa 		}
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 
10314972cbdSRoopa Prabhu 	if (lwtunnel_xmit_redirect(dst->lwtstate)) {
10414972cbdSRoopa Prabhu 		int res = lwtunnel_xmit(skb);
10514972cbdSRoopa Prabhu 
10614972cbdSRoopa Prabhu 		if (res < 0 || res == LWTUNNEL_XMIT_DONE)
10714972cbdSRoopa Prabhu 			return res;
10814972cbdSRoopa Prabhu 	}
10914972cbdSRoopa Prabhu 
1106fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
1112647a9b0SMartin KaFai Lau 	nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
1126fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
1136fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	if (unlikely(!neigh))
1146fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
1156fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	if (!IS_ERR(neigh)) {
1164ff06203SJulian Anastasov 		sock_confirm_neigh(skb, neigh);
1170353f282SDavid Ahern 		ret = neigh_output(neigh, skb, false);
1186fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		rcu_read_unlock_bh();
1196fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 		return ret;
1206fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	}
1216fd6ce20SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
12205e3aa09SDavid S. Miller 
12378126c41SEric W. Biederman 	IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
1249e508490SJan Engelhardt 	kfree_skb(skb);
1259e508490SJan Engelhardt 	return -EINVAL;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128956fe219Sbrakmo static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1299e508490SJan Engelhardt {
13009ee9dbaSTobias Brunner #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
13109ee9dbaSTobias Brunner 	/* Policy lookup after SNAT yielded a new policy */
13209ee9dbaSTobias Brunner 	if (skb_dst(skb)->xfrm) {
13309ee9dbaSTobias Brunner 		IPCB(skb)->flags |= IPSKB_REROUTED;
13409ee9dbaSTobias Brunner 		return dst_output(net, sk, skb);
13509ee9dbaSTobias Brunner 	}
13609ee9dbaSTobias Brunner #endif
13709ee9dbaSTobias Brunner 
1389e508490SJan Engelhardt 	if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
1399037c357SJiri Pirko 	    dst_allfrag(skb_dst(skb)) ||
1409037c357SJiri Pirko 	    (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
1417d8c6e39SEric W. Biederman 		return ip6_fragment(net, sk, skb, ip6_finish_output2);
1429e508490SJan Engelhardt 	else
1437d8c6e39SEric W. Biederman 		return ip6_finish_output2(net, sk, skb);
1449e508490SJan Engelhardt }
1459e508490SJan Engelhardt 
146956fe219Sbrakmo static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
147956fe219Sbrakmo {
148956fe219Sbrakmo 	int ret;
149956fe219Sbrakmo 
150956fe219Sbrakmo 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
151956fe219Sbrakmo 	switch (ret) {
152956fe219Sbrakmo 	case NET_XMIT_SUCCESS:
153956fe219Sbrakmo 		return __ip6_finish_output(net, sk, skb);
154956fe219Sbrakmo 	case NET_XMIT_CN:
155956fe219Sbrakmo 		return __ip6_finish_output(net, sk, skb) ? : ret;
156956fe219Sbrakmo 	default:
157956fe219Sbrakmo 		kfree_skb(skb);
158956fe219Sbrakmo 		return ret;
159956fe219Sbrakmo 	}
160956fe219Sbrakmo }
161956fe219Sbrakmo 
162ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
1631da177e4SLinus Torvalds {
16428f8bfd1SPhil Sutter 	struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev;
165adf30907SEric Dumazet 	struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
166be10de0aSEric W. Biederman 
16797a7a37aSChenbo Feng 	skb->protocol = htons(ETH_P_IPV6);
16897a7a37aSChenbo Feng 	skb->dev = dev;
16997a7a37aSChenbo Feng 
170778d80beSYOSHIFUJI Hideaki 	if (unlikely(idev->cnf.disable_ipv6)) {
17119a0644cSEric W. Biederman 		IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
172778d80beSYOSHIFUJI Hideaki 		kfree_skb(skb);
173778d80beSYOSHIFUJI Hideaki 		return 0;
174778d80beSYOSHIFUJI Hideaki 	}
175778d80beSYOSHIFUJI Hideaki 
17629a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
17728f8bfd1SPhil Sutter 			    net, sk, skb, indev, dev,
1789c6eb28aSJan Engelhardt 			    ip6_finish_output,
1799c6eb28aSJan Engelhardt 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np)
183513674b5SShaohua Li {
184513674b5SShaohua Li 	if (!np->autoflowlabel_set)
185513674b5SShaohua Li 		return ip6_default_np_autolabel(net);
186513674b5SShaohua Li 	else
187513674b5SShaohua Li 		return np->autoflowlabel;
188513674b5SShaohua Li }
189513674b5SShaohua Li 
1901da177e4SLinus Torvalds /*
191b5d43998SShan Wei  * xmit an sk_buff (used by TCP, SCTP and DCCP)
1921c1e9d2bSEric Dumazet  * Note : socket lock is not held for SYNACK packets, but might be modified
1931c1e9d2bSEric Dumazet  * by calls to skb_set_owner_w() and ipv6_local_error(),
1941c1e9d2bSEric Dumazet  * which are using proper atomic operations or spinlocks.
1951da177e4SLinus Torvalds  */
1961c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
1974f6570d7SEric Dumazet 	     __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority)
1981da177e4SLinus Torvalds {
1993bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
2001c1e9d2bSEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
2014c9483b2SDavid S. Miller 	struct in6_addr *first_hop = &fl6->daddr;
202adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
20366033f47SStefano Brivio 	unsigned int head_room;
2041da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
2054c9483b2SDavid S. Miller 	u8  proto = fl6->flowi6_proto;
2061da177e4SLinus Torvalds 	int seg_len = skb->len;
207e651f03aSGerrit Renker 	int hlimit = -1;
2081da177e4SLinus Torvalds 	u32 mtu;
2091da177e4SLinus Torvalds 
21066033f47SStefano Brivio 	head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
21166033f47SStefano Brivio 	if (opt)
21266033f47SStefano Brivio 		head_room += opt->opt_nflen + opt->opt_flen;
2131da177e4SLinus Torvalds 
21466033f47SStefano Brivio 	if (unlikely(skb_headroom(skb) < head_room)) {
2151da177e4SLinus Torvalds 		struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
21663159f29SIan Morris 		if (!skb2) {
217adf30907SEric Dumazet 			IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
218a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_OUTDISCARDS);
2191da177e4SLinus Torvalds 			kfree_skb(skb);
2201da177e4SLinus Torvalds 			return -ENOBUFS;
2211da177e4SLinus Torvalds 		}
222bbd6528dSEric Dumazet 		if (skb->sk)
223bbd6528dSEric Dumazet 			skb_set_owner_w(skb2, skb->sk);
224808db80aSEric Dumazet 		consume_skb(skb);
225a11d206dSYOSHIFUJI Hideaki 		skb = skb2;
2261da177e4SLinus Torvalds 	}
22766033f47SStefano Brivio 
22866033f47SStefano Brivio 	if (opt) {
22966033f47SStefano Brivio 		seg_len += opt->opt_nflen + opt->opt_flen;
23066033f47SStefano Brivio 
2311da177e4SLinus Torvalds 		if (opt->opt_flen)
2321da177e4SLinus Torvalds 			ipv6_push_frag_opts(skb, opt, &proto);
23366033f47SStefano Brivio 
2341da177e4SLinus Torvalds 		if (opt->opt_nflen)
235613fa3caSDavid Lebrun 			ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop,
236613fa3caSDavid Lebrun 					     &fl6->saddr);
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 
239e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
240e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
2410660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/*
2441da177e4SLinus Torvalds 	 *	Fill in the IPv6 header
2451da177e4SLinus Torvalds 	 */
246b903d324SEric Dumazet 	if (np)
2471da177e4SLinus Torvalds 		hlimit = np->hop_limit;
2481da177e4SLinus Torvalds 	if (hlimit < 0)
2496b75d090SYOSHIFUJI Hideaki 		hlimit = ip6_dst_hoplimit(dst);
2501da177e4SLinus Torvalds 
251cb1ce2efSTom Herbert 	ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
252513674b5SShaohua Li 				ip6_autoflowlabel(net, np), fl6));
25341a1f8eaSYOSHIFUJI Hideaki 
2541da177e4SLinus Torvalds 	hdr->payload_len = htons(seg_len);
2551da177e4SLinus Torvalds 	hdr->nexthdr = proto;
2561da177e4SLinus Torvalds 	hdr->hop_limit = hlimit;
2571da177e4SLinus Torvalds 
2584e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
2594e3fd7a0SAlexey Dobriyan 	hdr->daddr = *first_hop;
2601da177e4SLinus Torvalds 
2619c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
2624f6570d7SEric Dumazet 	skb->priority = priority;
26392e55f41SPablo Neira 	skb->mark = mark;
264a2c2064fSPatrick McHardy 
2651da177e4SLinus Torvalds 	mtu = dst_mtu(dst);
26660ff7467SWANG Cong 	if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
267adf30907SEric Dumazet 		IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
268edf391ffSNeil Horman 			      IPSTATS_MIB_OUT, skb->len);
269a8e3e1a9SDavid Ahern 
270a8e3e1a9SDavid Ahern 		/* if egress device is enslaved to an L3 master device pass the
271a8e3e1a9SDavid Ahern 		 * skb to its handler for processing
272a8e3e1a9SDavid Ahern 		 */
273a8e3e1a9SDavid Ahern 		skb = l3mdev_ip6_out((struct sock *)sk, skb);
274a8e3e1a9SDavid Ahern 		if (unlikely(!skb))
275a8e3e1a9SDavid Ahern 			return 0;
276a8e3e1a9SDavid Ahern 
2771c1e9d2bSEric Dumazet 		/* hooks should never assume socket lock is held.
2781c1e9d2bSEric Dumazet 		 * we promote our socket to non const
2791c1e9d2bSEric Dumazet 		 */
28029a26a56SEric W. Biederman 		return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
2811c1e9d2bSEric Dumazet 			       net, (struct sock *)sk, skb, NULL, dst->dev,
28213206b6bSEric W. Biederman 			       dst_output);
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	skb->dev = dst->dev;
2861c1e9d2bSEric Dumazet 	/* ipv6_local_error() does not require socket lock,
2871c1e9d2bSEric Dumazet 	 * we promote our socket to non const
2881c1e9d2bSEric Dumazet 	 */
2891c1e9d2bSEric Dumazet 	ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
2901c1e9d2bSEric Dumazet 
291adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
2921da177e4SLinus Torvalds 	kfree_skb(skb);
2931da177e4SLinus Torvalds 	return -EMSGSIZE;
2941da177e4SLinus Torvalds }
2957159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit);
2967159039aSYOSHIFUJI Hideaki 
2971da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	struct ip6_ra_chain *ra;
3001da177e4SLinus Torvalds 	struct sock *last = NULL;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	read_lock(&ip6_ra_lock);
3031da177e4SLinus Torvalds 	for (ra = ip6_ra_chain; ra; ra = ra->next) {
3041da177e4SLinus Torvalds 		struct sock *sk = ra->sk;
3050bd1b59bSAndrew McDonald 		if (sk && ra->sel == sel &&
3060bd1b59bSAndrew McDonald 		    (!sk->sk_bound_dev_if ||
3070bd1b59bSAndrew McDonald 		     sk->sk_bound_dev_if == skb->dev->ifindex)) {
3089036b2feSFrancesco Ruggeri 			struct ipv6_pinfo *np = inet6_sk(sk);
3099036b2feSFrancesco Ruggeri 
3109036b2feSFrancesco Ruggeri 			if (np && np->rtalert_isolate &&
3119036b2feSFrancesco Ruggeri 			    !net_eq(sock_net(sk), dev_net(skb->dev))) {
3129036b2feSFrancesco Ruggeri 				continue;
3139036b2feSFrancesco Ruggeri 			}
3141da177e4SLinus Torvalds 			if (last) {
3151da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
3161da177e4SLinus Torvalds 				if (skb2)
3171da177e4SLinus Torvalds 					rawv6_rcv(last, skb2);
3181da177e4SLinus Torvalds 			}
3191da177e4SLinus Torvalds 			last = sk;
3201da177e4SLinus Torvalds 		}
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	if (last) {
3241da177e4SLinus Torvalds 		rawv6_rcv(last, skb);
3251da177e4SLinus Torvalds 		read_unlock(&ip6_ra_lock);
3261da177e4SLinus Torvalds 		return 1;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 	read_unlock(&ip6_ra_lock);
3291da177e4SLinus Torvalds 	return 0;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
332e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb)
333e21e0b5fSVille Nuorvala {
3340660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
335e21e0b5fSVille Nuorvala 	u8 nexthdr = hdr->nexthdr;
33675f2811cSJesse Gross 	__be16 frag_off;
337e21e0b5fSVille Nuorvala 	int offset;
338e21e0b5fSVille Nuorvala 
339e21e0b5fSVille Nuorvala 	if (ipv6_ext_hdr(nexthdr)) {
34075f2811cSJesse Gross 		offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
341e21e0b5fSVille Nuorvala 		if (offset < 0)
342e21e0b5fSVille Nuorvala 			return 0;
343e21e0b5fSVille Nuorvala 	} else
344e21e0b5fSVille Nuorvala 		offset = sizeof(struct ipv6hdr);
345e21e0b5fSVille Nuorvala 
346e21e0b5fSVille Nuorvala 	if (nexthdr == IPPROTO_ICMPV6) {
347e21e0b5fSVille Nuorvala 		struct icmp6hdr *icmp6;
348e21e0b5fSVille Nuorvala 
349d56f90a7SArnaldo Carvalho de Melo 		if (!pskb_may_pull(skb, (skb_network_header(skb) +
350d56f90a7SArnaldo Carvalho de Melo 					 offset + 1 - skb->data)))
351e21e0b5fSVille Nuorvala 			return 0;
352e21e0b5fSVille Nuorvala 
353d56f90a7SArnaldo Carvalho de Melo 		icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
354e21e0b5fSVille Nuorvala 
355e21e0b5fSVille Nuorvala 		switch (icmp6->icmp6_type) {
356e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_SOLICITATION:
357e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_ADVERTISEMENT:
358e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_SOLICITATION:
359e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_ADVERTISEMENT:
360e21e0b5fSVille Nuorvala 		case NDISC_REDIRECT:
361e21e0b5fSVille Nuorvala 			/* For reaction involving unicast neighbor discovery
362e21e0b5fSVille Nuorvala 			 * message destined to the proxied address, pass it to
363e21e0b5fSVille Nuorvala 			 * input function.
364e21e0b5fSVille Nuorvala 			 */
365e21e0b5fSVille Nuorvala 			return 1;
366e21e0b5fSVille Nuorvala 		default:
367e21e0b5fSVille Nuorvala 			break;
368e21e0b5fSVille Nuorvala 		}
369e21e0b5fSVille Nuorvala 	}
370e21e0b5fSVille Nuorvala 
37174553b09SVille Nuorvala 	/*
37274553b09SVille Nuorvala 	 * The proxying router can't forward traffic sent to a link-local
37374553b09SVille Nuorvala 	 * address, so signal the sender and discard the packet. This
37474553b09SVille Nuorvala 	 * behavior is clarified by the MIPv6 specification.
37574553b09SVille Nuorvala 	 */
37674553b09SVille Nuorvala 	if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
37774553b09SVille Nuorvala 		dst_link_failure(skb);
37874553b09SVille Nuorvala 		return -1;
37974553b09SVille Nuorvala 	}
38074553b09SVille Nuorvala 
381e21e0b5fSVille Nuorvala 	return 0;
382e21e0b5fSVille Nuorvala }
383e21e0b5fSVille Nuorvala 
3840c4b51f0SEric W. Biederman static inline int ip6_forward_finish(struct net *net, struct sock *sk,
3850c4b51f0SEric W. Biederman 				     struct sk_buff *skb)
3861da177e4SLinus Torvalds {
38771a1c915SJeff Barnhill 	struct dst_entry *dst = skb_dst(skb);
38871a1c915SJeff Barnhill 
38971a1c915SJeff Barnhill 	__IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
39071a1c915SJeff Barnhill 	__IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
39171a1c915SJeff Barnhill 
392f839a6c9SIdo Schimmel #ifdef CONFIG_NET_SWITCHDEV
393f839a6c9SIdo Schimmel 	if (skb->offload_l3_fwd_mark) {
394f839a6c9SIdo Schimmel 		consume_skb(skb);
395f839a6c9SIdo Schimmel 		return 0;
396f839a6c9SIdo Schimmel 	}
397f839a6c9SIdo Schimmel #endif
398f839a6c9SIdo Schimmel 
3998203e2d8SEric Dumazet 	skb->tstamp = 0;
40013206b6bSEric W. Biederman 	return dst_output(net, sk, skb);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
403fe6cc55fSFlorian Westphal static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
404fe6cc55fSFlorian Westphal {
405418a3156SFlorian Westphal 	if (skb->len <= mtu)
406fe6cc55fSFlorian Westphal 		return false;
407fe6cc55fSFlorian Westphal 
40860ff7467SWANG Cong 	/* ipv6 conntrack defrag sets max_frag_size + ignore_df */
409fe6cc55fSFlorian Westphal 	if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
410fe6cc55fSFlorian Westphal 		return true;
411fe6cc55fSFlorian Westphal 
41260ff7467SWANG Cong 	if (skb->ignore_df)
413418a3156SFlorian Westphal 		return false;
414418a3156SFlorian Westphal 
415779b7931SDaniel Axtens 	if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
416fe6cc55fSFlorian Westphal 		return false;
417fe6cc55fSFlorian Westphal 
418fe6cc55fSFlorian Westphal 	return true;
419fe6cc55fSFlorian Westphal }
420fe6cc55fSFlorian Westphal 
4211da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb)
4221da177e4SLinus Torvalds {
423bdb7cc64SStephen Suryaputra 	struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
424adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
4250660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
4261da177e4SLinus Torvalds 	struct inet6_skb_parm *opt = IP6CB(skb);
427c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dst->dev);
42814f3ad6fSUlrich Weber 	u32 mtu;
4291da177e4SLinus Torvalds 
43053b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->forwarding == 0)
4311da177e4SLinus Torvalds 		goto error;
4321da177e4SLinus Torvalds 
433090f1166SLi RongQing 	if (skb->pkt_type != PACKET_HOST)
434090f1166SLi RongQing 		goto drop;
435090f1166SLi RongQing 
4369ef2e965SHannes Frederic Sowa 	if (unlikely(skb->sk))
4379ef2e965SHannes Frederic Sowa 		goto drop;
4389ef2e965SHannes Frederic Sowa 
4394497b076SBen Hutchings 	if (skb_warn_if_lro(skb))
4404497b076SBen Hutchings 		goto drop;
4414497b076SBen Hutchings 
4421da177e4SLinus Torvalds 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
443bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
4441da177e4SLinus Torvalds 		goto drop;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
44735fc92a9SHerbert Xu 	skb_forward_csum(skb);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	/*
4501da177e4SLinus Torvalds 	 *	We DO NOT make any processing on
4511da177e4SLinus Torvalds 	 *	RA packets, pushing them to user level AS IS
4521da177e4SLinus Torvalds 	 *	without ane WARRANTY that application will be able
4531da177e4SLinus Torvalds 	 *	to interpret them. The reason is that we
4541da177e4SLinus Torvalds 	 *	cannot make anything clever here.
4551da177e4SLinus Torvalds 	 *
4561da177e4SLinus Torvalds 	 *	We are not end-node, so that if packet contains
4571da177e4SLinus Torvalds 	 *	AH/ESP, we cannot make anything.
4581da177e4SLinus Torvalds 	 *	Defragmentation also would be mistake, RA packets
4591da177e4SLinus Torvalds 	 *	cannot be fragmented, because there is no warranty
4601da177e4SLinus Torvalds 	 *	that different fragments will go along one path. --ANK
4611da177e4SLinus Torvalds 	 */
462ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 	if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
463ab4eb353SYOSHIFUJI Hideaki / 吉藤英明 		if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
4641da177e4SLinus Torvalds 			return 0;
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/*
4681da177e4SLinus Torvalds 	 *	check and decrement ttl
4691da177e4SLinus Torvalds 	 */
4701da177e4SLinus Torvalds 	if (hdr->hop_limit <= 1) {
4711da177e4SLinus Torvalds 		/* Force OUTPUT device used as source address */
4721da177e4SLinus Torvalds 		skb->dev = dst->dev;
4733ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
474bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		kfree_skb(skb);
4771da177e4SLinus Torvalds 		return -ETIMEDOUT;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 
480fbea49e1SYOSHIFUJI Hideaki 	/* XXX: idev->cnf.proxy_ndp? */
48153b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->proxy_ndp &&
4828a3edd80SDaniel Lezcano 	    pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
48374553b09SVille Nuorvala 		int proxied = ip6_forward_proxy_check(skb);
48474553b09SVille Nuorvala 		if (proxied > 0)
485e21e0b5fSVille Nuorvala 			return ip6_input(skb);
48674553b09SVille Nuorvala 		else if (proxied < 0) {
487bdb7cc64SStephen Suryaputra 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
48874553b09SVille Nuorvala 			goto drop;
48974553b09SVille Nuorvala 		}
490e21e0b5fSVille Nuorvala 	}
491e21e0b5fSVille Nuorvala 
4921da177e4SLinus Torvalds 	if (!xfrm6_route_forward(skb)) {
493bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
4941da177e4SLinus Torvalds 		goto drop;
4951da177e4SLinus Torvalds 	}
496adf30907SEric Dumazet 	dst = skb_dst(skb);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* IPv6 specs say nothing about it, but it is clear that we cannot
4991da177e4SLinus Torvalds 	   send redirects to source routed frames.
5001e5dc146SMasahide NAKAMURA 	   We don't send redirects to frames decapsulated from IPsec.
5011da177e4SLinus Torvalds 	 */
5022f17becfSStephen Suryaputra 	if (IP6CB(skb)->iif == dst->dev->ifindex &&
5032f17becfSStephen Suryaputra 	    opt->srcrt == 0 && !skb_sec_path(skb)) {
5041da177e4SLinus Torvalds 		struct in6_addr *target = NULL;
505fbfe95a4SDavid S. Miller 		struct inet_peer *peer;
5061da177e4SLinus Torvalds 		struct rt6_info *rt;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 		/*
5091da177e4SLinus Torvalds 		 *	incoming and outgoing devices are the same
5101da177e4SLinus Torvalds 		 *	send a redirect.
5111da177e4SLinus Torvalds 		 */
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		rt = (struct rt6_info *) dst;
514c45a3dfbSDavid S. Miller 		if (rt->rt6i_flags & RTF_GATEWAY)
515c45a3dfbSDavid S. Miller 			target = &rt->rt6i_gateway;
5161da177e4SLinus Torvalds 		else
5171da177e4SLinus Torvalds 			target = &hdr->daddr;
5181da177e4SLinus Torvalds 
519fd0273d7SMartin KaFai Lau 		peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1);
52092d86829SDavid S. Miller 
5211da177e4SLinus Torvalds 		/* Limit redirects both by destination (here)
5221da177e4SLinus Torvalds 		   and by source (inside ndisc_send_redirect)
5231da177e4SLinus Torvalds 		 */
524fbfe95a4SDavid S. Miller 		if (inet_peer_xrlim_allow(peer, 1*HZ))
5254991969aSDavid S. Miller 			ndisc_send_redirect(skb, target);
5261d861aa4SDavid S. Miller 		if (peer)
5271d861aa4SDavid S. Miller 			inet_putpeer(peer);
5285bb1ab09SDavid L Stevens 	} else {
5295bb1ab09SDavid L Stevens 		int addrtype = ipv6_addr_type(&hdr->saddr);
5305bb1ab09SDavid L Stevens 
5311da177e4SLinus Torvalds 		/* This check is security critical. */
532f81b2e7dSYOSHIFUJI Hideaki 		if (addrtype == IPV6_ADDR_ANY ||
533f81b2e7dSYOSHIFUJI Hideaki 		    addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
5341da177e4SLinus Torvalds 			goto error;
5355bb1ab09SDavid L Stevens 		if (addrtype & IPV6_ADDR_LINKLOCAL) {
5365bb1ab09SDavid L Stevens 			icmpv6_send(skb, ICMPV6_DEST_UNREACH,
5373ffe533cSAlexey Dobriyan 				    ICMPV6_NOT_NEIGHBOUR, 0);
5385bb1ab09SDavid L Stevens 			goto error;
5395bb1ab09SDavid L Stevens 		}
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
5420954cf9cSHannes Frederic Sowa 	mtu = ip6_dst_mtu_forward(dst);
54314f3ad6fSUlrich Weber 	if (mtu < IPV6_MIN_MTU)
54414f3ad6fSUlrich Weber 		mtu = IPV6_MIN_MTU;
54514f3ad6fSUlrich Weber 
546fe6cc55fSFlorian Westphal 	if (ip6_pkt_too_big(skb, mtu)) {
5471da177e4SLinus Torvalds 		/* Again, force OUTPUT device used as source address */
5481da177e4SLinus Torvalds 		skb->dev = dst->dev;
54914f3ad6fSUlrich Weber 		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
550bdb7cc64SStephen Suryaputra 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS);
5511d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(dst),
55215c77d8bSEric Dumazet 				IPSTATS_MIB_FRAGFAILS);
5531da177e4SLinus Torvalds 		kfree_skb(skb);
5541da177e4SLinus Torvalds 		return -EMSGSIZE;
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	if (skb_cow(skb, dst->dev->hard_header_len)) {
5581d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(dst),
55915c77d8bSEric Dumazet 				IPSTATS_MIB_OUTDISCARDS);
5601da177e4SLinus Torvalds 		goto drop;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5630660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	/* Mangling hops number delayed to point after skb COW */
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	hdr->hop_limit--;
5681da177e4SLinus Torvalds 
56929a26a56SEric W. Biederman 	return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
57029a26a56SEric W. Biederman 		       net, NULL, skb, skb->dev, dst->dev,
5716e23ae2aSPatrick McHardy 		       ip6_forward_finish);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds error:
574bdb7cc64SStephen Suryaputra 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
5751da177e4SLinus Torvalds drop:
5761da177e4SLinus Torvalds 	kfree_skb(skb);
5771da177e4SLinus Torvalds 	return -EINVAL;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
5831da177e4SLinus Torvalds 	to->priority = from->priority;
5841da177e4SLinus Torvalds 	to->protocol = from->protocol;
585adf30907SEric Dumazet 	skb_dst_drop(to);
586adf30907SEric Dumazet 	skb_dst_set(to, dst_clone(skb_dst(from)));
5871da177e4SLinus Torvalds 	to->dev = from->dev;
58882e91ffeSThomas Graf 	to->mark = from->mark;
5891da177e4SLinus Torvalds 
5903dd1c9a1SPaolo Abeni 	skb_copy_hash(to, from);
5913dd1c9a1SPaolo Abeni 
5921da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
5931da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
5941da177e4SLinus Torvalds #endif
595e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
596df5042f4SFlorian Westphal 	skb_ext_copy(to, from);
597984bc16cSJames Morris 	skb_copy_secmark(to, from);
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6000feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
6010feca619SPablo Neira Ayuso 		      u8 nexthdr, __be32 frag_id,
6020feca619SPablo Neira Ayuso 		      struct ip6_fraglist_iter *iter)
6030feca619SPablo Neira Ayuso {
6040feca619SPablo Neira Ayuso 	unsigned int first_len;
6050feca619SPablo Neira Ayuso 	struct frag_hdr *fh;
6060feca619SPablo Neira Ayuso 
6070feca619SPablo Neira Ayuso 	/* BUILD HEADER */
6080feca619SPablo Neira Ayuso 	*prevhdr = NEXTHDR_FRAGMENT;
6090feca619SPablo Neira Ayuso 	iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
6100feca619SPablo Neira Ayuso 	if (!iter->tmp_hdr)
6110feca619SPablo Neira Ayuso 		return -ENOMEM;
6120feca619SPablo Neira Ayuso 
613b7034146SEric Dumazet 	iter->frag = skb_shinfo(skb)->frag_list;
6140feca619SPablo Neira Ayuso 	skb_frag_list_init(skb);
6150feca619SPablo Neira Ayuso 
6160feca619SPablo Neira Ayuso 	iter->offset = 0;
6170feca619SPablo Neira Ayuso 	iter->hlen = hlen;
6180feca619SPablo Neira Ayuso 	iter->frag_id = frag_id;
6190feca619SPablo Neira Ayuso 	iter->nexthdr = nexthdr;
6200feca619SPablo Neira Ayuso 
6210feca619SPablo Neira Ayuso 	__skb_pull(skb, hlen);
6220feca619SPablo Neira Ayuso 	fh = __skb_push(skb, sizeof(struct frag_hdr));
6230feca619SPablo Neira Ayuso 	__skb_push(skb, hlen);
6240feca619SPablo Neira Ayuso 	skb_reset_network_header(skb);
6250feca619SPablo Neira Ayuso 	memcpy(skb_network_header(skb), iter->tmp_hdr, hlen);
6260feca619SPablo Neira Ayuso 
6270feca619SPablo Neira Ayuso 	fh->nexthdr = nexthdr;
6280feca619SPablo Neira Ayuso 	fh->reserved = 0;
6290feca619SPablo Neira Ayuso 	fh->frag_off = htons(IP6_MF);
6300feca619SPablo Neira Ayuso 	fh->identification = frag_id;
6310feca619SPablo Neira Ayuso 
6320feca619SPablo Neira Ayuso 	first_len = skb_pagelen(skb);
6330feca619SPablo Neira Ayuso 	skb->data_len = first_len - skb_headlen(skb);
6340feca619SPablo Neira Ayuso 	skb->len = first_len;
6350feca619SPablo Neira Ayuso 	ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr));
6360feca619SPablo Neira Ayuso 
6370feca619SPablo Neira Ayuso 	return 0;
6380feca619SPablo Neira Ayuso }
6390feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_init);
6400feca619SPablo Neira Ayuso 
6410feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb,
6420feca619SPablo Neira Ayuso 			  struct ip6_fraglist_iter *iter)
6430feca619SPablo Neira Ayuso {
6440feca619SPablo Neira Ayuso 	struct sk_buff *frag = iter->frag;
6450feca619SPablo Neira Ayuso 	unsigned int hlen = iter->hlen;
6460feca619SPablo Neira Ayuso 	struct frag_hdr *fh;
6470feca619SPablo Neira Ayuso 
6480feca619SPablo Neira Ayuso 	frag->ip_summed = CHECKSUM_NONE;
6490feca619SPablo Neira Ayuso 	skb_reset_transport_header(frag);
6500feca619SPablo Neira Ayuso 	fh = __skb_push(frag, sizeof(struct frag_hdr));
6510feca619SPablo Neira Ayuso 	__skb_push(frag, hlen);
6520feca619SPablo Neira Ayuso 	skb_reset_network_header(frag);
6530feca619SPablo Neira Ayuso 	memcpy(skb_network_header(frag), iter->tmp_hdr, hlen);
6540feca619SPablo Neira Ayuso 	iter->offset += skb->len - hlen - sizeof(struct frag_hdr);
6550feca619SPablo Neira Ayuso 	fh->nexthdr = iter->nexthdr;
6560feca619SPablo Neira Ayuso 	fh->reserved = 0;
6570feca619SPablo Neira Ayuso 	fh->frag_off = htons(iter->offset);
6580feca619SPablo Neira Ayuso 	if (frag->next)
6590feca619SPablo Neira Ayuso 		fh->frag_off |= htons(IP6_MF);
6600feca619SPablo Neira Ayuso 	fh->identification = iter->frag_id;
6610feca619SPablo Neira Ayuso 	ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
6620feca619SPablo Neira Ayuso 	ip6_copy_metadata(frag, skb);
6630feca619SPablo Neira Ayuso }
6640feca619SPablo Neira Ayuso EXPORT_SYMBOL(ip6_fraglist_prepare);
6650feca619SPablo Neira Ayuso 
6668a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
6678a6a1f17SPablo Neira Ayuso 		   unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
6688a6a1f17SPablo Neira Ayuso 		   u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state)
6698a6a1f17SPablo Neira Ayuso {
6708a6a1f17SPablo Neira Ayuso 	state->prevhdr = prevhdr;
6718a6a1f17SPablo Neira Ayuso 	state->nexthdr = nexthdr;
6728a6a1f17SPablo Neira Ayuso 	state->frag_id = frag_id;
6738a6a1f17SPablo Neira Ayuso 
6748a6a1f17SPablo Neira Ayuso 	state->hlen = hlen;
6758a6a1f17SPablo Neira Ayuso 	state->mtu = mtu;
6768a6a1f17SPablo Neira Ayuso 
6778a6a1f17SPablo Neira Ayuso 	state->left = skb->len - hlen;	/* Space per frame */
6788a6a1f17SPablo Neira Ayuso 	state->ptr = hlen;		/* Where to start from */
6798a6a1f17SPablo Neira Ayuso 
6808a6a1f17SPablo Neira Ayuso 	state->hroom = hdr_room;
6818a6a1f17SPablo Neira Ayuso 	state->troom = needed_tailroom;
6828a6a1f17SPablo Neira Ayuso 
6838a6a1f17SPablo Neira Ayuso 	state->offset = 0;
6848a6a1f17SPablo Neira Ayuso }
6858a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_init);
6868a6a1f17SPablo Neira Ayuso 
6878a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state)
6888a6a1f17SPablo Neira Ayuso {
6898a6a1f17SPablo Neira Ayuso 	u8 *prevhdr = state->prevhdr, *fragnexthdr_offset;
6908a6a1f17SPablo Neira Ayuso 	struct sk_buff *frag;
6918a6a1f17SPablo Neira Ayuso 	struct frag_hdr *fh;
6928a6a1f17SPablo Neira Ayuso 	unsigned int len;
6938a6a1f17SPablo Neira Ayuso 
6948a6a1f17SPablo Neira Ayuso 	len = state->left;
6958a6a1f17SPablo Neira Ayuso 	/* IF: it doesn't fit, use 'mtu' - the data space left */
6968a6a1f17SPablo Neira Ayuso 	if (len > state->mtu)
6978a6a1f17SPablo Neira Ayuso 		len = state->mtu;
6988a6a1f17SPablo Neira Ayuso 	/* IF: we are not sending up to and including the packet end
6998a6a1f17SPablo Neira Ayuso 	   then align the next start on an eight byte boundary */
7008a6a1f17SPablo Neira Ayuso 	if (len < state->left)
7018a6a1f17SPablo Neira Ayuso 		len &= ~7;
7028a6a1f17SPablo Neira Ayuso 
7038a6a1f17SPablo Neira Ayuso 	/* Allocate buffer */
7048a6a1f17SPablo Neira Ayuso 	frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) +
7058a6a1f17SPablo Neira Ayuso 			 state->hroom + state->troom, GFP_ATOMIC);
7068a6a1f17SPablo Neira Ayuso 	if (!frag)
7078a6a1f17SPablo Neira Ayuso 		return ERR_PTR(-ENOMEM);
7088a6a1f17SPablo Neira Ayuso 
7098a6a1f17SPablo Neira Ayuso 	/*
7108a6a1f17SPablo Neira Ayuso 	 *	Set up data on packet
7118a6a1f17SPablo Neira Ayuso 	 */
7128a6a1f17SPablo Neira Ayuso 
7138a6a1f17SPablo Neira Ayuso 	ip6_copy_metadata(frag, skb);
7148a6a1f17SPablo Neira Ayuso 	skb_reserve(frag, state->hroom);
7158a6a1f17SPablo Neira Ayuso 	skb_put(frag, len + state->hlen + sizeof(struct frag_hdr));
7168a6a1f17SPablo Neira Ayuso 	skb_reset_network_header(frag);
7178a6a1f17SPablo Neira Ayuso 	fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen);
7188a6a1f17SPablo Neira Ayuso 	frag->transport_header = (frag->network_header + state->hlen +
7198a6a1f17SPablo Neira Ayuso 				  sizeof(struct frag_hdr));
7208a6a1f17SPablo Neira Ayuso 
7218a6a1f17SPablo Neira Ayuso 	/*
7228a6a1f17SPablo Neira Ayuso 	 *	Charge the memory for the fragment to any owner
7238a6a1f17SPablo Neira Ayuso 	 *	it might possess
7248a6a1f17SPablo Neira Ayuso 	 */
7258a6a1f17SPablo Neira Ayuso 	if (skb->sk)
7268a6a1f17SPablo Neira Ayuso 		skb_set_owner_w(frag, skb->sk);
7278a6a1f17SPablo Neira Ayuso 
7288a6a1f17SPablo Neira Ayuso 	/*
7298a6a1f17SPablo Neira Ayuso 	 *	Copy the packet header into the new buffer.
7308a6a1f17SPablo Neira Ayuso 	 */
7318a6a1f17SPablo Neira Ayuso 	skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen);
7328a6a1f17SPablo Neira Ayuso 
7338a6a1f17SPablo Neira Ayuso 	fragnexthdr_offset = skb_network_header(frag);
7348a6a1f17SPablo Neira Ayuso 	fragnexthdr_offset += prevhdr - skb_network_header(skb);
7358a6a1f17SPablo Neira Ayuso 	*fragnexthdr_offset = NEXTHDR_FRAGMENT;
7368a6a1f17SPablo Neira Ayuso 
7378a6a1f17SPablo Neira Ayuso 	/*
7388a6a1f17SPablo Neira Ayuso 	 *	Build fragment header.
7398a6a1f17SPablo Neira Ayuso 	 */
7408a6a1f17SPablo Neira Ayuso 	fh->nexthdr = state->nexthdr;
7418a6a1f17SPablo Neira Ayuso 	fh->reserved = 0;
7428a6a1f17SPablo Neira Ayuso 	fh->identification = state->frag_id;
7438a6a1f17SPablo Neira Ayuso 
7448a6a1f17SPablo Neira Ayuso 	/*
7458a6a1f17SPablo Neira Ayuso 	 *	Copy a block of the IP datagram.
7468a6a1f17SPablo Neira Ayuso 	 */
7478a6a1f17SPablo Neira Ayuso 	BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag),
7488a6a1f17SPablo Neira Ayuso 			     len));
7498a6a1f17SPablo Neira Ayuso 	state->left -= len;
7508a6a1f17SPablo Neira Ayuso 
7518a6a1f17SPablo Neira Ayuso 	fh->frag_off = htons(state->offset);
7528a6a1f17SPablo Neira Ayuso 	if (state->left > 0)
7538a6a1f17SPablo Neira Ayuso 		fh->frag_off |= htons(IP6_MF);
7548a6a1f17SPablo Neira Ayuso 	ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
7558a6a1f17SPablo Neira Ayuso 
7568a6a1f17SPablo Neira Ayuso 	state->ptr += len;
7578a6a1f17SPablo Neira Ayuso 	state->offset += len;
7588a6a1f17SPablo Neira Ayuso 
7598a6a1f17SPablo Neira Ayuso 	return frag;
7608a6a1f17SPablo Neira Ayuso }
7618a6a1f17SPablo Neira Ayuso EXPORT_SYMBOL(ip6_frag_next);
7628a6a1f17SPablo Neira Ayuso 
7637d8c6e39SEric W. Biederman int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
7647d8c6e39SEric W. Biederman 		 int (*output)(struct net *, struct sock *, struct sk_buff *))
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	struct sk_buff *frag;
767adf30907SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
768f60e5990Shannes@stressinduktion.org 	struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
769f60e5990Shannes@stressinduktion.org 				inet6_sk(skb->sk) : NULL;
7708a6a1f17SPablo Neira Ayuso 	struct ip6_frag_state state;
7718a6a1f17SPablo Neira Ayuso 	unsigned int mtu, hlen, nexthdr_offset;
7729669fffcSEric Dumazet 	ktime_t tstamp = skb->tstamp;
7738a6a1f17SPablo Neira Ayuso 	int hroom, err = 0;
774286c2349SMartin KaFai Lau 	__be32 frag_id;
7751da177e4SLinus Torvalds 	u8 *prevhdr, nexthdr = 0;
7761da177e4SLinus Torvalds 
7777dd7eb95SDavid S. Miller 	err = ip6_find_1stfragopt(skb, &prevhdr);
7787dd7eb95SDavid S. Miller 	if (err < 0)
7792423496aSCraig Gallek 		goto fail;
7807dd7eb95SDavid S. Miller 	hlen = err;
7811da177e4SLinus Torvalds 	nexthdr = *prevhdr;
782ef0efcd3SJunwei Hu 	nexthdr_offset = prevhdr - skb_network_header(skb);
7831da177e4SLinus Torvalds 
784628a5c56SJohn Heffner 	mtu = ip6_skb_dst_mtu(skb);
785b881ef76SJohn Heffner 
786b881ef76SJohn Heffner 	/* We must not fragment if the socket is set to force MTU discovery
78714f3ad6fSUlrich Weber 	 * or if the skb it not generated by a local socket.
788b881ef76SJohn Heffner 	 */
789485fca66SFlorian Westphal 	if (unlikely(!skb->ignore_df && skb->len > mtu))
790485fca66SFlorian Westphal 		goto fail_toobig;
791a34a101eSEric Dumazet 
792485fca66SFlorian Westphal 	if (IP6CB(skb)->frag_max_size) {
793485fca66SFlorian Westphal 		if (IP6CB(skb)->frag_max_size > mtu)
794485fca66SFlorian Westphal 			goto fail_toobig;
795485fca66SFlorian Westphal 
796485fca66SFlorian Westphal 		/* don't send fragments larger than what we received */
797485fca66SFlorian Westphal 		mtu = IP6CB(skb)->frag_max_size;
798485fca66SFlorian Westphal 		if (mtu < IPV6_MIN_MTU)
799485fca66SFlorian Westphal 			mtu = IPV6_MIN_MTU;
800b881ef76SJohn Heffner 	}
801b881ef76SJohn Heffner 
802d91675f9SYOSHIFUJI Hideaki 	if (np && np->frag_size < mtu) {
803d91675f9SYOSHIFUJI Hideaki 		if (np->frag_size)
804d91675f9SYOSHIFUJI Hideaki 			mtu = np->frag_size;
805d91675f9SYOSHIFUJI Hideaki 	}
80689bc7848SHannes Frederic Sowa 	if (mtu < hlen + sizeof(struct frag_hdr) + 8)
807b72a2b01SHannes Frederic Sowa 		goto fail_toobig;
8081e0d69a9SHannes Frederic Sowa 	mtu -= hlen + sizeof(struct frag_hdr);
8091da177e4SLinus Torvalds 
810fd0273d7SMartin KaFai Lau 	frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
811fd0273d7SMartin KaFai Lau 				    &ipv6_hdr(skb)->saddr);
812286c2349SMartin KaFai Lau 
813405c92f7SHannes Frederic Sowa 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
814405c92f7SHannes Frederic Sowa 	    (err = skb_checksum_help(skb)))
815405c92f7SHannes Frederic Sowa 		goto fail;
816405c92f7SHannes Frederic Sowa 
817ef0efcd3SJunwei Hu 	prevhdr = skb_network_header(skb) + nexthdr_offset;
8181d325d21SFlorian Westphal 	hroom = LL_RESERVED_SPACE(rt->dst.dev);
81921dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
820c72d8cdaSAlexey Dobriyan 		unsigned int first_len = skb_pagelen(skb);
8210feca619SPablo Neira Ayuso 		struct ip6_fraglist_iter iter;
8223d13008eSEric Dumazet 		struct sk_buff *frag2;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
8251da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
8261d325d21SFlorian Westphal 		    skb_cloned(skb) ||
8271d325d21SFlorian Westphal 		    skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
8281da177e4SLinus Torvalds 			goto slow_path;
8291da177e4SLinus Torvalds 
8304d9092bbSDavid S. Miller 		skb_walk_frags(skb, frag) {
8311da177e4SLinus Torvalds 			/* Correct geometry. */
8321da177e4SLinus Torvalds 			if (frag->len > mtu ||
8331da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
8341d325d21SFlorian Westphal 			    skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
8353d13008eSEric Dumazet 				goto slow_path_clean;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 			/* Partially cloned skb? */
8381da177e4SLinus Torvalds 			if (skb_shared(frag))
8393d13008eSEric Dumazet 				goto slow_path_clean;
8402fdba6b0SHerbert Xu 
8412fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
8422fdba6b0SHerbert Xu 			if (skb->sk) {
8432fdba6b0SHerbert Xu 				frag->sk = skb->sk;
8442fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
8452fdba6b0SHerbert Xu 			}
8463d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
8471da177e4SLinus Torvalds 		}
8481da177e4SLinus Torvalds 
8490feca619SPablo Neira Ayuso 		err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id,
8500feca619SPablo Neira Ayuso 					&iter);
8510feca619SPablo Neira Ayuso 		if (err < 0)
8521d325d21SFlorian Westphal 			goto fail;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		for (;;) {
8551da177e4SLinus Torvalds 			/* Prepare header of the next frame,
8561da177e4SLinus Torvalds 			 * before previous one went down. */
8570feca619SPablo Neira Ayuso 			if (iter.frag)
8580feca619SPablo Neira Ayuso 				ip6_fraglist_prepare(skb, &iter);
8591da177e4SLinus Torvalds 
8609669fffcSEric Dumazet 			skb->tstamp = tstamp;
8617d8c6e39SEric W. Biederman 			err = output(net, sk, skb);
862dafee490SWei Dong 			if (!err)
863d8d1f30bSChangli Gao 				IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
8643bd653c8SDenis V. Lunev 					      IPSTATS_MIB_FRAGCREATES);
865dafee490SWei Dong 
8660feca619SPablo Neira Ayuso 			if (err || !iter.frag)
8671da177e4SLinus Torvalds 				break;
8681da177e4SLinus Torvalds 
8690feca619SPablo Neira Ayuso 			skb = ip6_fraglist_next(&iter);
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 
8720feca619SPablo Neira Ayuso 		kfree(iter.tmp_hdr);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 		if (err == 0) {
875d8d1f30bSChangli Gao 			IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
8763bd653c8SDenis V. Lunev 				      IPSTATS_MIB_FRAGOKS);
8771da177e4SLinus Torvalds 			return 0;
8781da177e4SLinus Torvalds 		}
8791da177e4SLinus Torvalds 
880b7034146SEric Dumazet 		kfree_skb_list(iter.frag);
8811da177e4SLinus Torvalds 
882d8d1f30bSChangli Gao 		IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
8833bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGFAILS);
8841da177e4SLinus Torvalds 		return err;
8853d13008eSEric Dumazet 
8863d13008eSEric Dumazet slow_path_clean:
8873d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
8883d13008eSEric Dumazet 			if (frag2 == frag)
8893d13008eSEric Dumazet 				break;
8903d13008eSEric Dumazet 			frag2->sk = NULL;
8913d13008eSEric Dumazet 			frag2->destructor = NULL;
8923d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
8933d13008eSEric Dumazet 		}
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds slow_path:
8971da177e4SLinus Torvalds 	/*
8981da177e4SLinus Torvalds 	 *	Fragment the datagram.
8991da177e4SLinus Torvalds 	 */
9001da177e4SLinus Torvalds 
9018a6a1f17SPablo Neira Ayuso 	ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom,
9028a6a1f17SPablo Neira Ayuso 		      LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id,
9038a6a1f17SPablo Neira Ayuso 		      &state);
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	/*
9061da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
9071da177e4SLinus Torvalds 	 */
90879e49503SFlorian Westphal 
9098a6a1f17SPablo Neira Ayuso 	while (state.left > 0) {
9108a6a1f17SPablo Neira Ayuso 		frag = ip6_frag_next(skb, &state);
9118a6a1f17SPablo Neira Ayuso 		if (IS_ERR(frag)) {
9128a6a1f17SPablo Neira Ayuso 			err = PTR_ERR(frag);
9131da177e4SLinus Torvalds 			goto fail;
9141da177e4SLinus Torvalds 		}
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 		/*
9171da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
9181da177e4SLinus Torvalds 		 */
9199669fffcSEric Dumazet 		frag->tstamp = tstamp;
9207d8c6e39SEric W. Biederman 		err = output(net, sk, frag);
9211da177e4SLinus Torvalds 		if (err)
9221da177e4SLinus Torvalds 			goto fail;
923dafee490SWei Dong 
924adf30907SEric Dumazet 		IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
9253bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGCREATES);
9261da177e4SLinus Torvalds 	}
927adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
928a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGOKS);
929808db80aSEric Dumazet 	consume_skb(skb);
9301da177e4SLinus Torvalds 	return err;
9311da177e4SLinus Torvalds 
932485fca66SFlorian Westphal fail_toobig:
933485fca66SFlorian Westphal 	if (skb->sk && dst_allfrag(skb_dst(skb)))
934485fca66SFlorian Westphal 		sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
935485fca66SFlorian Westphal 
936485fca66SFlorian Westphal 	icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
937485fca66SFlorian Westphal 	err = -EMSGSIZE;
938485fca66SFlorian Westphal 
9391da177e4SLinus Torvalds fail:
940adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
941a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGFAILS);
9421da177e4SLinus Torvalds 	kfree_skb(skb);
9431da177e4SLinus Torvalds 	return err;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
946b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key,
947b71d1d42SEric Dumazet 			       const struct in6_addr *fl_addr,
948b71d1d42SEric Dumazet 			       const struct in6_addr *addr_cache)
949cf6b1982SYOSHIFUJI Hideaki {
950a02cec21SEric Dumazet 	return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
95163159f29SIan Morris 		(!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
952cf6b1982SYOSHIFUJI Hideaki }
953cf6b1982SYOSHIFUJI Hideaki 
954497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
955497c615aSHerbert Xu 					  struct dst_entry *dst,
956b71d1d42SEric Dumazet 					  const struct flowi6 *fl6)
9571da177e4SLinus Torvalds {
9581da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
959a963a37dSEric Dumazet 	struct rt6_info *rt;
9601da177e4SLinus Torvalds 
961497c615aSHerbert Xu 	if (!dst)
962497c615aSHerbert Xu 		goto out;
9631da177e4SLinus Torvalds 
964a963a37dSEric Dumazet 	if (dst->ops->family != AF_INET6) {
965a963a37dSEric Dumazet 		dst_release(dst);
966a963a37dSEric Dumazet 		return NULL;
967a963a37dSEric Dumazet 	}
968a963a37dSEric Dumazet 
969a963a37dSEric Dumazet 	rt = (struct rt6_info *)dst;
9701da177e4SLinus Torvalds 	/* Yes, checking route validity in not connected
971d76e60a5SDavid S. Miller 	 * case is not very simple. Take into account,
972d76e60a5SDavid S. Miller 	 * that we do not support routing by source, TOS,
973d76e60a5SDavid S. Miller 	 * and MSG_DONTROUTE		--ANK (980726)
974d76e60a5SDavid S. Miller 	 *
975cf6b1982SYOSHIFUJI Hideaki 	 * 1. ip6_rt_check(): If route was host route,
976cf6b1982SYOSHIFUJI Hideaki 	 *    check that cached destination is current.
977d76e60a5SDavid S. Miller 	 *    If it is network route, we still may
978d76e60a5SDavid S. Miller 	 *    check its validity using saved pointer
979d76e60a5SDavid S. Miller 	 *    to the last used address: daddr_cache.
980d76e60a5SDavid S. Miller 	 *    We do not want to save whole address now,
981d76e60a5SDavid S. Miller 	 *    (because main consumer of this service
982d76e60a5SDavid S. Miller 	 *    is tcp, which has not this problem),
983d76e60a5SDavid S. Miller 	 *    so that the last trick works only on connected
984d76e60a5SDavid S. Miller 	 *    sockets.
985d76e60a5SDavid S. Miller 	 * 2. oif also should be the same.
9861da177e4SLinus Torvalds 	 */
9874c9483b2SDavid S. Miller 	if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
9888e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES
9894c9483b2SDavid S. Miller 	    ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
9908e1ef0a9SYOSHIFUJI Hideaki #endif
991ca254490SDavid Ahern 	   (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) &&
992ca254490SDavid Ahern 	      (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) {
993497c615aSHerbert Xu 		dst_release(dst);
994497c615aSHerbert Xu 		dst = NULL;
9951da177e4SLinus Torvalds 	}
996497c615aSHerbert Xu 
997497c615aSHerbert Xu out:
998497c615aSHerbert Xu 	return dst;
9991da177e4SLinus Torvalds }
1000497c615aSHerbert Xu 
10013aef934fSEric Dumazet static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
10024c9483b2SDavid S. Miller 			       struct dst_entry **dst, struct flowi6 *fl6)
1003497c615aSHerbert Xu {
100469cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
100569cce1d1SDavid S. Miller 	struct neighbour *n;
100697cac082SDavid S. Miller 	struct rt6_info *rt;
100769cce1d1SDavid S. Miller #endif
100869cce1d1SDavid S. Miller 	int err;
10096f21c96aSPaolo Abeni 	int flags = 0;
10101da177e4SLinus Torvalds 
1011e16e888bSMarkus Stenberg 	/* The correct way to handle this would be to do
1012e16e888bSMarkus Stenberg 	 * ip6_route_get_saddr, and then ip6_route_output; however,
1013e16e888bSMarkus Stenberg 	 * the route-specific preferred source forces the
1014e16e888bSMarkus Stenberg 	 * ip6_route_output call _before_ ip6_route_get_saddr.
1015e16e888bSMarkus Stenberg 	 *
1016e16e888bSMarkus Stenberg 	 * In source specific routing (no src=any default route),
1017e16e888bSMarkus Stenberg 	 * ip6_route_output will fail given src=any saddr, though, so
1018e16e888bSMarkus Stenberg 	 * that's why we try it again later.
1019e16e888bSMarkus Stenberg 	 */
1020e16e888bSMarkus Stenberg 	if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) {
1021a68886a6SDavid Ahern 		struct fib6_info *from;
1022e16e888bSMarkus Stenberg 		struct rt6_info *rt;
1023e16e888bSMarkus Stenberg 		bool had_dst = *dst != NULL;
1024e16e888bSMarkus Stenberg 
1025e16e888bSMarkus Stenberg 		if (!had_dst)
1026e16e888bSMarkus Stenberg 			*dst = ip6_route_output(net, sk, fl6);
1027e16e888bSMarkus Stenberg 		rt = (*dst)->error ? NULL : (struct rt6_info *)*dst;
1028a68886a6SDavid Ahern 
1029a68886a6SDavid Ahern 		rcu_read_lock();
1030a68886a6SDavid Ahern 		from = rt ? rcu_dereference(rt->from) : NULL;
1031a68886a6SDavid Ahern 		err = ip6_route_get_saddr(net, from, &fl6->daddr,
1032e16e888bSMarkus Stenberg 					  sk ? inet6_sk(sk)->srcprefs : 0,
1033e16e888bSMarkus Stenberg 					  &fl6->saddr);
1034a68886a6SDavid Ahern 		rcu_read_unlock();
1035a68886a6SDavid Ahern 
1036e16e888bSMarkus Stenberg 		if (err)
1037e16e888bSMarkus Stenberg 			goto out_err_release;
1038e16e888bSMarkus Stenberg 
1039e16e888bSMarkus Stenberg 		/* If we had an erroneous initial result, pretend it
1040e16e888bSMarkus Stenberg 		 * never existed and let the SA-enabled version take
1041e16e888bSMarkus Stenberg 		 * over.
1042e16e888bSMarkus Stenberg 		 */
1043e16e888bSMarkus Stenberg 		if (!had_dst && (*dst)->error) {
1044e16e888bSMarkus Stenberg 			dst_release(*dst);
1045e16e888bSMarkus Stenberg 			*dst = NULL;
1046e16e888bSMarkus Stenberg 		}
10476f21c96aSPaolo Abeni 
10486f21c96aSPaolo Abeni 		if (fl6->flowi6_oif)
10496f21c96aSPaolo Abeni 			flags |= RT6_LOOKUP_F_IFACE;
1050e16e888bSMarkus Stenberg 	}
1051e16e888bSMarkus Stenberg 
105263159f29SIan Morris 	if (!*dst)
10536f21c96aSPaolo Abeni 		*dst = ip6_route_output_flags(net, sk, fl6, flags);
10541da177e4SLinus Torvalds 
1055e5d08d71SIan Morris 	err = (*dst)->error;
1056e5d08d71SIan Morris 	if (err)
10571da177e4SLinus Torvalds 		goto out_err_release;
10581da177e4SLinus Torvalds 
105995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
106095c385b4SNeil Horman 	/*
106195c385b4SNeil Horman 	 * Here if the dst entry we've looked up
106295c385b4SNeil Horman 	 * has a neighbour entry that is in the INCOMPLETE
106395c385b4SNeil Horman 	 * state and the src address from the flow is
106495c385b4SNeil Horman 	 * marked as OPTIMISTIC, we release the found
106595c385b4SNeil Horman 	 * dst entry and replace it instead with the
106695c385b4SNeil Horman 	 * dst entry of the nexthop router
106795c385b4SNeil Horman 	 */
1068c56bf6feSEric Dumazet 	rt = (struct rt6_info *) *dst;
1069707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
10702647a9b0SMartin KaFai Lau 	n = __ipv6_neigh_lookup_noref(rt->dst.dev,
10712647a9b0SMartin KaFai Lau 				      rt6_nexthop(rt, &fl6->daddr));
1072707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
1073707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
1074707be1ffSYOSHIFUJI Hideaki / 吉藤英明 
1075707be1ffSYOSHIFUJI Hideaki / 吉藤英明 	if (err) {
107695c385b4SNeil Horman 		struct inet6_ifaddr *ifp;
10774c9483b2SDavid S. Miller 		struct flowi6 fl_gw6;
107895c385b4SNeil Horman 		int redirect;
107995c385b4SNeil Horman 
10804c9483b2SDavid S. Miller 		ifp = ipv6_get_ifaddr(net, &fl6->saddr,
10811cab3da6SDaniel Lezcano 				      (*dst)->dev, 1);
108295c385b4SNeil Horman 
108395c385b4SNeil Horman 		redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
108495c385b4SNeil Horman 		if (ifp)
108595c385b4SNeil Horman 			in6_ifa_put(ifp);
108695c385b4SNeil Horman 
108795c385b4SNeil Horman 		if (redirect) {
108895c385b4SNeil Horman 			/*
108995c385b4SNeil Horman 			 * We need to get the dst entry for the
109095c385b4SNeil Horman 			 * default router instead
109195c385b4SNeil Horman 			 */
109295c385b4SNeil Horman 			dst_release(*dst);
10934c9483b2SDavid S. Miller 			memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
10944c9483b2SDavid S. Miller 			memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
10954c9483b2SDavid S. Miller 			*dst = ip6_route_output(net, sk, &fl_gw6);
1096e5d08d71SIan Morris 			err = (*dst)->error;
1097e5d08d71SIan Morris 			if (err)
109895c385b4SNeil Horman 				goto out_err_release;
109995c385b4SNeil Horman 		}
110095c385b4SNeil Horman 	}
110195c385b4SNeil Horman #endif
1102ec5e3b0aSJonathan T. Leighton 	if (ipv6_addr_v4mapped(&fl6->saddr) &&
110300ea1ceeSWillem de Bruijn 	    !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) {
110400ea1ceeSWillem de Bruijn 		err = -EAFNOSUPPORT;
110500ea1ceeSWillem de Bruijn 		goto out_err_release;
110600ea1ceeSWillem de Bruijn 	}
110795c385b4SNeil Horman 
11081da177e4SLinus Torvalds 	return 0;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds out_err_release:
11111da177e4SLinus Torvalds 	dst_release(*dst);
11121da177e4SLinus Torvalds 	*dst = NULL;
11138a966fc0SDavid Ahern 
11140d240e78SDavid Ahern 	if (err == -ENETUNREACH)
11150d240e78SDavid Ahern 		IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
11161da177e4SLinus Torvalds 	return err;
11171da177e4SLinus Torvalds }
111834a0b3cdSAdrian Bunk 
1119497c615aSHerbert Xu /**
1120497c615aSHerbert Xu  *	ip6_dst_lookup - perform route lookup on flow
1121b51cd7c8SAndrew Lunn  *	@net: Network namespace to perform lookup in
1122497c615aSHerbert Xu  *	@sk: socket which provides route info
1123497c615aSHerbert Xu  *	@dst: pointer to dst_entry * for result
11244c9483b2SDavid S. Miller  *	@fl6: flow to lookup
1125497c615aSHerbert Xu  *
1126497c615aSHerbert Xu  *	This function performs a route lookup on the given flow.
1127497c615aSHerbert Xu  *
1128497c615aSHerbert Xu  *	It returns zero on success, or a standard errno code on error.
1129497c615aSHerbert Xu  */
1130343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1131343d60aaSRoopa Prabhu 		   struct flowi6 *fl6)
1132497c615aSHerbert Xu {
1133497c615aSHerbert Xu 	*dst = NULL;
1134343d60aaSRoopa Prabhu 	return ip6_dst_lookup_tail(net, sk, dst, fl6);
1135497c615aSHerbert Xu }
11363cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip6_dst_lookup);
11373cf3dc6cSArnaldo Carvalho de Melo 
1138497c615aSHerbert Xu /**
113968d0c6d3SDavid S. Miller  *	ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1140b51cd7c8SAndrew Lunn  *	@net: Network namespace to perform lookup in
114168d0c6d3SDavid S. Miller  *	@sk: socket which provides route info
11424c9483b2SDavid S. Miller  *	@fl6: flow to lookup
114368d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
114468d0c6d3SDavid S. Miller  *
114568d0c6d3SDavid S. Miller  *	This function performs a route lookup on the given flow.
114668d0c6d3SDavid S. Miller  *
114768d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
114868d0c6d3SDavid S. Miller  *	error code.
114968d0c6d3SDavid S. Miller  */
1150c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
11510e0d44abSSteffen Klassert 				      const struct in6_addr *final_dst)
115268d0c6d3SDavid S. Miller {
115368d0c6d3SDavid S. Miller 	struct dst_entry *dst = NULL;
115468d0c6d3SDavid S. Miller 	int err;
115568d0c6d3SDavid S. Miller 
1156c4e85f73SSabrina Dubroca 	err = ip6_dst_lookup_tail(net, sk, &dst, fl6);
115768d0c6d3SDavid S. Miller 	if (err)
115868d0c6d3SDavid S. Miller 		return ERR_PTR(err);
115968d0c6d3SDavid S. Miller 	if (final_dst)
11604e3fd7a0SAlexey Dobriyan 		fl6->daddr = *final_dst;
11612774c131SDavid S. Miller 
1162c4e85f73SSabrina Dubroca 	return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0);
116368d0c6d3SDavid S. Miller }
116468d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
116568d0c6d3SDavid S. Miller 
116668d0c6d3SDavid S. Miller /**
116768d0c6d3SDavid S. Miller  *	ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
116868d0c6d3SDavid S. Miller  *	@sk: socket which provides the dst cache and route info
11694c9483b2SDavid S. Miller  *	@fl6: flow to lookup
117068d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
117196818159SAlexey Kodanev  *	@connected: whether @sk is connected or not
1172497c615aSHerbert Xu  *
1173497c615aSHerbert Xu  *	This function performs a route lookup on the given flow with the
1174497c615aSHerbert Xu  *	possibility of using the cached route in the socket if it is valid.
1175497c615aSHerbert Xu  *	It will take the socket dst lock when operating on the dst cache.
1176497c615aSHerbert Xu  *	As a result, this function can only be used in process context.
1177497c615aSHerbert Xu  *
117896818159SAlexey Kodanev  *	In addition, for a connected socket, cache the dst in the socket
117996818159SAlexey Kodanev  *	if the current cache is not valid.
118096818159SAlexey Kodanev  *
118168d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
118268d0c6d3SDavid S. Miller  *	error code.
1183497c615aSHerbert Xu  */
11844c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
118596818159SAlexey Kodanev 					 const struct in6_addr *final_dst,
118696818159SAlexey Kodanev 					 bool connected)
1187497c615aSHerbert Xu {
118868d0c6d3SDavid S. Miller 	struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
1189497c615aSHerbert Xu 
11904c9483b2SDavid S. Miller 	dst = ip6_sk_dst_check(sk, dst, fl6);
119196818159SAlexey Kodanev 	if (dst)
119296818159SAlexey Kodanev 		return dst;
119396818159SAlexey Kodanev 
1194c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst);
119596818159SAlexey Kodanev 	if (connected && !IS_ERR(dst))
119696818159SAlexey Kodanev 		ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6);
119768d0c6d3SDavid S. Miller 
119800bc0ef5SJakub Sitnicki 	return dst;
119968d0c6d3SDavid S. Miller }
120068d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
1201497c615aSHerbert Xu 
1202571912c6SMartin Varghese /**
1203571912c6SMartin Varghese  *      ip6_dst_lookup_tunnel - perform route lookup on tunnel
1204571912c6SMartin Varghese  *      @skb: Packet for which lookup is done
1205571912c6SMartin Varghese  *      @dev: Tunnel device
1206571912c6SMartin Varghese  *      @net: Network namespace of tunnel device
1207b51cd7c8SAndrew Lunn  *      @sock: Socket which provides route info
1208571912c6SMartin Varghese  *      @saddr: Memory to store the src ip address
1209571912c6SMartin Varghese  *      @info: Tunnel information
1210571912c6SMartin Varghese  *      @protocol: IP protocol
1211b51cd7c8SAndrew Lunn  *      @use_cache: Flag to enable cache usage
1212571912c6SMartin Varghese  *      This function performs a route lookup on a tunnel
1213571912c6SMartin Varghese  *
1214571912c6SMartin Varghese  *      It returns a valid dst pointer and stores src address to be used in
1215571912c6SMartin Varghese  *      tunnel in param saddr on success, else a pointer encoded error code.
1216571912c6SMartin Varghese  */
1217571912c6SMartin Varghese 
1218571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb,
1219571912c6SMartin Varghese 					struct net_device *dev,
1220571912c6SMartin Varghese 					struct net *net,
1221571912c6SMartin Varghese 					struct socket *sock,
1222571912c6SMartin Varghese 					struct in6_addr *saddr,
1223571912c6SMartin Varghese 					const struct ip_tunnel_info *info,
1224571912c6SMartin Varghese 					u8 protocol,
1225571912c6SMartin Varghese 					bool use_cache)
1226571912c6SMartin Varghese {
1227571912c6SMartin Varghese 	struct dst_entry *dst = NULL;
1228571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1229571912c6SMartin Varghese 	struct dst_cache *dst_cache;
1230571912c6SMartin Varghese #endif
1231571912c6SMartin Varghese 	struct flowi6 fl6;
1232571912c6SMartin Varghese 	__u8 prio;
1233571912c6SMartin Varghese 
1234571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1235571912c6SMartin Varghese 	dst_cache = (struct dst_cache *)&info->dst_cache;
1236571912c6SMartin Varghese 	if (use_cache) {
1237571912c6SMartin Varghese 		dst = dst_cache_get_ip6(dst_cache, saddr);
1238571912c6SMartin Varghese 		if (dst)
1239571912c6SMartin Varghese 			return dst;
1240571912c6SMartin Varghese 	}
1241571912c6SMartin Varghese #endif
1242571912c6SMartin Varghese 	memset(&fl6, 0, sizeof(fl6));
1243571912c6SMartin Varghese 	fl6.flowi6_mark = skb->mark;
1244571912c6SMartin Varghese 	fl6.flowi6_proto = protocol;
1245571912c6SMartin Varghese 	fl6.daddr = info->key.u.ipv6.dst;
1246571912c6SMartin Varghese 	fl6.saddr = info->key.u.ipv6.src;
1247571912c6SMartin Varghese 	prio = info->key.tos;
1248571912c6SMartin Varghese 	fl6.flowlabel = ip6_make_flowinfo(RT_TOS(prio),
1249571912c6SMartin Varghese 					  info->key.label);
1250571912c6SMartin Varghese 
1251571912c6SMartin Varghese 	dst = ipv6_stub->ipv6_dst_lookup_flow(net, sock->sk, &fl6,
1252571912c6SMartin Varghese 					      NULL);
1253571912c6SMartin Varghese 	if (IS_ERR(dst)) {
1254571912c6SMartin Varghese 		netdev_dbg(dev, "no route to %pI6\n", &fl6.daddr);
1255571912c6SMartin Varghese 		return ERR_PTR(-ENETUNREACH);
1256571912c6SMartin Varghese 	}
1257571912c6SMartin Varghese 	if (dst->dev == dev) { /* is this necessary? */
1258571912c6SMartin Varghese 		netdev_dbg(dev, "circular route to %pI6\n", &fl6.daddr);
1259571912c6SMartin Varghese 		dst_release(dst);
1260571912c6SMartin Varghese 		return ERR_PTR(-ELOOP);
1261571912c6SMartin Varghese 	}
1262571912c6SMartin Varghese #ifdef CONFIG_DST_CACHE
1263571912c6SMartin Varghese 	if (use_cache)
1264571912c6SMartin Varghese 		dst_cache_set_ip6(dst_cache, dst, &fl6.saddr);
1265571912c6SMartin Varghese #endif
1266571912c6SMartin Varghese 	*saddr = fl6.saddr;
1267571912c6SMartin Varghese 	return dst;
1268571912c6SMartin Varghese }
1269571912c6SMartin Varghese EXPORT_SYMBOL_GPL(ip6_dst_lookup_tunnel);
1270571912c6SMartin Varghese 
12710178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
12720178b695SHerbert Xu 					       gfp_t gfp)
12730178b695SHerbert Xu {
12740178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
12750178b695SHerbert Xu }
12760178b695SHerbert Xu 
12770178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
12780178b695SHerbert Xu 						gfp_t gfp)
12790178b695SHerbert Xu {
12800178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
12810178b695SHerbert Xu }
12820178b695SHerbert Xu 
128375a493e6SHannes Frederic Sowa static void ip6_append_data_mtu(unsigned int *mtu,
12840c183379SGao feng 				int *maxfraglen,
12850c183379SGao feng 				unsigned int fragheaderlen,
12860c183379SGao feng 				struct sk_buff *skb,
128775a493e6SHannes Frederic Sowa 				struct rt6_info *rt,
1288e367c2d0Slucien 				unsigned int orig_mtu)
12890c183379SGao feng {
12900c183379SGao feng 	if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
129163159f29SIan Morris 		if (!skb) {
12920c183379SGao feng 			/* first fragment, reserve header_len */
1293e367c2d0Slucien 			*mtu = orig_mtu - rt->dst.header_len;
12940c183379SGao feng 
12950c183379SGao feng 		} else {
12960c183379SGao feng 			/*
12970c183379SGao feng 			 * this fragment is not first, the headers
12980c183379SGao feng 			 * space is regarded as data space.
12990c183379SGao feng 			 */
1300e367c2d0Slucien 			*mtu = orig_mtu;
13010c183379SGao feng 		}
13020c183379SGao feng 		*maxfraglen = ((*mtu - fragheaderlen) & ~7)
13030c183379SGao feng 			      + fragheaderlen - sizeof(struct frag_hdr);
13040c183379SGao feng 	}
13050c183379SGao feng }
13060c183379SGao feng 
1307366e41d9SVlad Yasevich static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
130826879da5SWei Wang 			  struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
13095fdaa88dSWillem de Bruijn 			  struct rt6_info *rt, struct flowi6 *fl6)
1310366e41d9SVlad Yasevich {
1311366e41d9SVlad Yasevich 	struct ipv6_pinfo *np = inet6_sk(sk);
1312366e41d9SVlad Yasevich 	unsigned int mtu;
131326879da5SWei Wang 	struct ipv6_txoptions *opt = ipc6->opt;
1314366e41d9SVlad Yasevich 
1315366e41d9SVlad Yasevich 	/*
1316366e41d9SVlad Yasevich 	 * setup for corking
1317366e41d9SVlad Yasevich 	 */
1318366e41d9SVlad Yasevich 	if (opt) {
1319366e41d9SVlad Yasevich 		if (WARN_ON(v6_cork->opt))
1320366e41d9SVlad Yasevich 			return -EINVAL;
1321366e41d9SVlad Yasevich 
1322864e2a1fSEric Dumazet 		v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation);
132363159f29SIan Morris 		if (unlikely(!v6_cork->opt))
1324366e41d9SVlad Yasevich 			return -ENOBUFS;
1325366e41d9SVlad Yasevich 
1326864e2a1fSEric Dumazet 		v6_cork->opt->tot_len = sizeof(*opt);
1327366e41d9SVlad Yasevich 		v6_cork->opt->opt_flen = opt->opt_flen;
1328366e41d9SVlad Yasevich 		v6_cork->opt->opt_nflen = opt->opt_nflen;
1329366e41d9SVlad Yasevich 
1330366e41d9SVlad Yasevich 		v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1331366e41d9SVlad Yasevich 						    sk->sk_allocation);
1332366e41d9SVlad Yasevich 		if (opt->dst0opt && !v6_cork->opt->dst0opt)
1333366e41d9SVlad Yasevich 			return -ENOBUFS;
1334366e41d9SVlad Yasevich 
1335366e41d9SVlad Yasevich 		v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1336366e41d9SVlad Yasevich 						    sk->sk_allocation);
1337366e41d9SVlad Yasevich 		if (opt->dst1opt && !v6_cork->opt->dst1opt)
1338366e41d9SVlad Yasevich 			return -ENOBUFS;
1339366e41d9SVlad Yasevich 
1340366e41d9SVlad Yasevich 		v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt,
1341366e41d9SVlad Yasevich 						   sk->sk_allocation);
1342366e41d9SVlad Yasevich 		if (opt->hopopt && !v6_cork->opt->hopopt)
1343366e41d9SVlad Yasevich 			return -ENOBUFS;
1344366e41d9SVlad Yasevich 
1345366e41d9SVlad Yasevich 		v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1346366e41d9SVlad Yasevich 						    sk->sk_allocation);
1347366e41d9SVlad Yasevich 		if (opt->srcrt && !v6_cork->opt->srcrt)
1348366e41d9SVlad Yasevich 			return -ENOBUFS;
1349366e41d9SVlad Yasevich 
1350366e41d9SVlad Yasevich 		/* need source address above miyazawa*/
1351366e41d9SVlad Yasevich 	}
1352366e41d9SVlad Yasevich 	dst_hold(&rt->dst);
1353366e41d9SVlad Yasevich 	cork->base.dst = &rt->dst;
1354366e41d9SVlad Yasevich 	cork->fl.u.ip6 = *fl6;
135526879da5SWei Wang 	v6_cork->hop_limit = ipc6->hlimit;
135626879da5SWei Wang 	v6_cork->tclass = ipc6->tclass;
1357366e41d9SVlad Yasevich 	if (rt->dst.flags & DST_XFRM_TUNNEL)
1358366e41d9SVlad Yasevich 		mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1359749439bfSMike Maloney 		      READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst);
1360366e41d9SVlad Yasevich 	else
1361366e41d9SVlad Yasevich 		mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1362c02b3741SDavid S. Miller 			READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst));
1363366e41d9SVlad Yasevich 	if (np->frag_size < mtu) {
1364366e41d9SVlad Yasevich 		if (np->frag_size)
1365366e41d9SVlad Yasevich 			mtu = np->frag_size;
1366366e41d9SVlad Yasevich 	}
1367749439bfSMike Maloney 	if (mtu < IPV6_MIN_MTU)
1368749439bfSMike Maloney 		return -EINVAL;
1369366e41d9SVlad Yasevich 	cork->base.fragsize = mtu;
1370fbf47813SWillem de Bruijn 	cork->base.gso_size = ipc6->gso_size;
1371678ca42dSWillem de Bruijn 	cork->base.tx_flags = 0;
1372c6af0c22SWillem de Bruijn 	cork->base.mark = ipc6->sockc.mark;
1373678ca42dSWillem de Bruijn 	sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags);
1374bec1f6f6SWillem de Bruijn 
13750f6c480fSDavid Miller 	if (dst_allfrag(xfrm_dst_path(&rt->dst)))
1376366e41d9SVlad Yasevich 		cork->base.flags |= IPCORK_ALLFRAG;
1377366e41d9SVlad Yasevich 	cork->base.length = 0;
1378366e41d9SVlad Yasevich 
13795fdaa88dSWillem de Bruijn 	cork->base.transmit_time = ipc6->sockc.transmit_time;
1380a818f75eSJesus Sanchez-Palencia 
1381366e41d9SVlad Yasevich 	return 0;
1382366e41d9SVlad Yasevich }
1383366e41d9SVlad Yasevich 
13840bbe84a6SVlad Yasevich static int __ip6_append_data(struct sock *sk,
13850bbe84a6SVlad Yasevich 			     struct flowi6 *fl6,
13860bbe84a6SVlad Yasevich 			     struct sk_buff_head *queue,
13870bbe84a6SVlad Yasevich 			     struct inet_cork *cork,
13880bbe84a6SVlad Yasevich 			     struct inet6_cork *v6_cork,
13890bbe84a6SVlad Yasevich 			     struct page_frag *pfrag,
13900bbe84a6SVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
13910bbe84a6SVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
13921da177e4SLinus Torvalds 			     void *from, int length, int transhdrlen,
13935fdaa88dSWillem de Bruijn 			     unsigned int flags, struct ipcm6_cookie *ipc6)
13941da177e4SLinus Torvalds {
13950c183379SGao feng 	struct sk_buff *skb, *skb_prev = NULL;
139610b8a3deSPaolo Abeni 	unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
1397b5947e5dSWillem de Bruijn 	struct ubuf_info *uarg = NULL;
13980bbe84a6SVlad Yasevich 	int exthdrlen = 0;
13990bbe84a6SVlad Yasevich 	int dst_exthdrlen = 0;
14001da177e4SLinus Torvalds 	int hh_len;
14011da177e4SLinus Torvalds 	int copy;
14021da177e4SLinus Torvalds 	int err;
14031da177e4SLinus Torvalds 	int offset = 0;
140409c2d251SWillem de Bruijn 	u32 tskey = 0;
14050bbe84a6SVlad Yasevich 	struct rt6_info *rt = (struct rt6_info *)cork->dst;
14060bbe84a6SVlad Yasevich 	struct ipv6_txoptions *opt = v6_cork->opt;
140732dce968SVlad Yasevich 	int csummode = CHECKSUM_NONE;
1408682b1a9dSHannes Frederic Sowa 	unsigned int maxnonfragsize, headersize;
14091f4c6eb2SEric Dumazet 	unsigned int wmem_alloc_delta = 0;
1410100f6d8eSWillem de Bruijn 	bool paged, extra_uref = false;
14111da177e4SLinus Torvalds 
14120bbe84a6SVlad Yasevich 	skb = skb_peek_tail(queue);
14130bbe84a6SVlad Yasevich 	if (!skb) {
14140bbe84a6SVlad Yasevich 		exthdrlen = opt ? opt->opt_flen : 0;
14157efdba5bSRomain KUNTZ 		dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
14161da177e4SLinus Torvalds 	}
14170bbe84a6SVlad Yasevich 
141815e36f5bSWillem de Bruijn 	paged = !!cork->gso_size;
1419bec1f6f6SWillem de Bruijn 	mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize;
1420e367c2d0Slucien 	orig_mtu = mtu;
14211da177e4SLinus Torvalds 
1422678ca42dSWillem de Bruijn 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
1423678ca42dSWillem de Bruijn 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
1424678ca42dSWillem de Bruijn 		tskey = sk->sk_tskey++;
1425678ca42dSWillem de Bruijn 
1426d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
14271da177e4SLinus Torvalds 
1428a1b05140SMasahide NAKAMURA 	fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
1429b4ce9277SHerbert Xu 			(opt ? opt->opt_nflen : 0);
14304df98e76SHannes Frederic Sowa 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
14314df98e76SHannes Frederic Sowa 		     sizeof(struct frag_hdr);
14321da177e4SLinus Torvalds 
14334df98e76SHannes Frederic Sowa 	headersize = sizeof(struct ipv6hdr) +
14343a1cebe7SHannes Frederic Sowa 		     (opt ? opt->opt_flen + opt->opt_nflen : 0) +
14354df98e76SHannes Frederic Sowa 		     (dst_allfrag(&rt->dst) ?
14364df98e76SHannes Frederic Sowa 		      sizeof(struct frag_hdr) : 0) +
14374df98e76SHannes Frederic Sowa 		     rt->rt6i_nfheader_len;
14384df98e76SHannes Frederic Sowa 
143910b8a3deSPaolo Abeni 	/* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit
144010b8a3deSPaolo Abeni 	 * the first fragment
144110b8a3deSPaolo Abeni 	 */
144210b8a3deSPaolo Abeni 	if (headersize + transhdrlen > mtu)
144310b8a3deSPaolo Abeni 		goto emsgsize;
144410b8a3deSPaolo Abeni 
144526879da5SWei Wang 	if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
14464df98e76SHannes Frederic Sowa 	    (sk->sk_protocol == IPPROTO_UDP ||
14474df98e76SHannes Frederic Sowa 	     sk->sk_protocol == IPPROTO_RAW)) {
14484df98e76SHannes Frederic Sowa 		ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
14494df98e76SHannes Frederic Sowa 				sizeof(struct ipv6hdr));
14504df98e76SHannes Frederic Sowa 		goto emsgsize;
14514df98e76SHannes Frederic Sowa 	}
14524df98e76SHannes Frederic Sowa 
1453682b1a9dSHannes Frederic Sowa 	if (ip6_sk_ignore_df(sk))
1454682b1a9dSHannes Frederic Sowa 		maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1455682b1a9dSHannes Frederic Sowa 	else
1456682b1a9dSHannes Frederic Sowa 		maxnonfragsize = mtu;
1457682b1a9dSHannes Frederic Sowa 
14584df98e76SHannes Frederic Sowa 	if (cork->length + length > maxnonfragsize - headersize) {
14594df98e76SHannes Frederic Sowa emsgsize:
146010b8a3deSPaolo Abeni 		pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
146110b8a3deSPaolo Abeni 		ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
14621da177e4SLinus Torvalds 		return -EMSGSIZE;
14631da177e4SLinus Torvalds 	}
1464682b1a9dSHannes Frederic Sowa 
1465682b1a9dSHannes Frederic Sowa 	/* CHECKSUM_PARTIAL only with no extension headers and when
1466682b1a9dSHannes Frederic Sowa 	 * we are not going to fragment
1467682b1a9dSHannes Frederic Sowa 	 */
1468682b1a9dSHannes Frederic Sowa 	if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
1469682b1a9dSHannes Frederic Sowa 	    headersize == sizeof(struct ipv6hdr) &&
14702b89ed65SVlad Yasevich 	    length <= mtu - headersize &&
1471bec1f6f6SWillem de Bruijn 	    (!(flags & MSG_MORE) || cork->gso_size) &&
1472c8cd0989STom Herbert 	    rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1473682b1a9dSHannes Frederic Sowa 		csummode = CHECKSUM_PARTIAL;
14741da177e4SLinus Torvalds 
1475b5947e5dSWillem de Bruijn 	if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) {
1476b5947e5dSWillem de Bruijn 		uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb));
1477b5947e5dSWillem de Bruijn 		if (!uarg)
1478b5947e5dSWillem de Bruijn 			return -ENOBUFS;
1479522924b5SWillem de Bruijn 		extra_uref = !skb_zcopy(skb);	/* only ref on new uarg */
1480b5947e5dSWillem de Bruijn 		if (rt->dst.dev->features & NETIF_F_SG &&
1481b5947e5dSWillem de Bruijn 		    csummode == CHECKSUM_PARTIAL) {
1482b5947e5dSWillem de Bruijn 			paged = true;
1483b5947e5dSWillem de Bruijn 		} else {
1484b5947e5dSWillem de Bruijn 			uarg->zerocopy = 0;
148552900d22SWillem de Bruijn 			skb_zcopy_set(skb, uarg, &extra_uref);
1486b5947e5dSWillem de Bruijn 		}
1487b5947e5dSWillem de Bruijn 	}
1488b5947e5dSWillem de Bruijn 
14891da177e4SLinus Torvalds 	/*
14901da177e4SLinus Torvalds 	 * Let's try using as much space as possible.
14911da177e4SLinus Torvalds 	 * Use MTU if total length of the message fits into the MTU.
14921da177e4SLinus Torvalds 	 * Otherwise, we need to reserve fragment header and
14931da177e4SLinus Torvalds 	 * fragment alignment (= 8-15 octects, in total).
14941da177e4SLinus Torvalds 	 *
1495*634a63e7SRandy Dunlap 	 * Note that we may need to "move" the data from the tail
14961da177e4SLinus Torvalds 	 * of the buffer to the new fragment when we split
14971da177e4SLinus Torvalds 	 * the message.
14981da177e4SLinus Torvalds 	 *
14991da177e4SLinus Torvalds 	 * FIXME: It may be fragmented into multiple chunks
15001da177e4SLinus Torvalds 	 *        at once if non-fragmentable extension headers
15011da177e4SLinus Torvalds 	 *        are too large.
15021da177e4SLinus Torvalds 	 * --yoshfuji
15031da177e4SLinus Torvalds 	 */
15041da177e4SLinus Torvalds 
15052811ebacSHannes Frederic Sowa 	cork->length += length;
15062811ebacSHannes Frederic Sowa 	if (!skb)
15071da177e4SLinus Torvalds 		goto alloc_new_skb;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	while (length > 0) {
15101da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
1511bdc712b4SDavid S. Miller 		copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
15121da177e4SLinus Torvalds 		if (copy < length)
15131da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 		if (copy <= 0) {
15161da177e4SLinus Torvalds 			char *data;
15171da177e4SLinus Torvalds 			unsigned int datalen;
15181da177e4SLinus Torvalds 			unsigned int fraglen;
15191da177e4SLinus Torvalds 			unsigned int fraggap;
15201da177e4SLinus Torvalds 			unsigned int alloclen;
1521aba36930SWillem de Bruijn 			unsigned int pagedlen;
15221da177e4SLinus Torvalds alloc_new_skb:
15231da177e4SLinus Torvalds 			/* There's no room in the current skb */
15240c183379SGao feng 			if (skb)
15250c183379SGao feng 				fraggap = skb->len - maxfraglen;
15261da177e4SLinus Torvalds 			else
15271da177e4SLinus Torvalds 				fraggap = 0;
15280c183379SGao feng 			/* update mtu and maxfraglen if necessary */
152963159f29SIan Morris 			if (!skb || !skb_prev)
15300c183379SGao feng 				ip6_append_data_mtu(&mtu, &maxfraglen,
153175a493e6SHannes Frederic Sowa 						    fragheaderlen, skb, rt,
1532e367c2d0Slucien 						    orig_mtu);
15330c183379SGao feng 
15340c183379SGao feng 			skb_prev = skb;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 			/*
15371da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
15381da177e4SLinus Torvalds 			 * we know we need more fragment(s).
15391da177e4SLinus Torvalds 			 */
15401da177e4SLinus Torvalds 			datalen = length + fraggap;
15411da177e4SLinus Torvalds 
15420c183379SGao feng 			if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
15430c183379SGao feng 				datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
154415e36f5bSWillem de Bruijn 			fraglen = datalen + fragheaderlen;
1545aba36930SWillem de Bruijn 			pagedlen = 0;
154615e36f5bSWillem de Bruijn 
15471da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
1548d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
15491da177e4SLinus Torvalds 				alloclen = mtu;
155015e36f5bSWillem de Bruijn 			else if (!paged)
155115e36f5bSWillem de Bruijn 				alloclen = fraglen;
155215e36f5bSWillem de Bruijn 			else {
155315e36f5bSWillem de Bruijn 				alloclen = min_t(int, fraglen, MAX_HEADER);
155415e36f5bSWillem de Bruijn 				pagedlen = fraglen - alloclen;
155515e36f5bSWillem de Bruijn 			}
15561da177e4SLinus Torvalds 
1557299b0767SSteffen Klassert 			alloclen += dst_exthdrlen;
1558299b0767SSteffen Klassert 
15590c183379SGao feng 			if (datalen != length + fraggap) {
15601da177e4SLinus Torvalds 				/*
15610c183379SGao feng 				 * this is not the last fragment, the trailer
15620c183379SGao feng 				 * space is regarded as data space.
15631da177e4SLinus Torvalds 				 */
15640c183379SGao feng 				datalen += rt->dst.trailer_len;
15650c183379SGao feng 			}
15660c183379SGao feng 
1567d8d1f30bSChangli Gao 			alloclen += rt->dst.trailer_len;
15680c183379SGao feng 			fraglen = datalen + fragheaderlen;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 			/*
15711da177e4SLinus Torvalds 			 * We just reserve space for fragment header.
15721da177e4SLinus Torvalds 			 * Note: this may be overallocation if the message
15731da177e4SLinus Torvalds 			 * (without MSG_MORE) fits into the MTU.
15741da177e4SLinus Torvalds 			 */
15751da177e4SLinus Torvalds 			alloclen += sizeof(struct frag_hdr);
15761da177e4SLinus Torvalds 
157715e36f5bSWillem de Bruijn 			copy = datalen - transhdrlen - fraggap - pagedlen;
1578232cd35dSEric Dumazet 			if (copy < 0) {
1579232cd35dSEric Dumazet 				err = -EINVAL;
1580232cd35dSEric Dumazet 				goto error;
1581232cd35dSEric Dumazet 			}
15821da177e4SLinus Torvalds 			if (transhdrlen) {
15831da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
15841da177e4SLinus Torvalds 						alloclen + hh_len,
15851da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
15861da177e4SLinus Torvalds 			} else {
15871da177e4SLinus Torvalds 				skb = NULL;
15881f4c6eb2SEric Dumazet 				if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
15891da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
15901f4c6eb2SEric Dumazet 					skb = alloc_skb(alloclen + hh_len,
15911da177e4SLinus Torvalds 							sk->sk_allocation);
159263159f29SIan Morris 				if (unlikely(!skb))
15931da177e4SLinus Torvalds 					err = -ENOBUFS;
15941da177e4SLinus Torvalds 			}
159563159f29SIan Morris 			if (!skb)
15961da177e4SLinus Torvalds 				goto error;
15971da177e4SLinus Torvalds 			/*
15981da177e4SLinus Torvalds 			 *	Fill in the control structures
15991da177e4SLinus Torvalds 			 */
16009c9c9ad5SHannes Frederic Sowa 			skb->protocol = htons(ETH_P_IPV6);
160132dce968SVlad Yasevich 			skb->ip_summed = csummode;
16021da177e4SLinus Torvalds 			skb->csum = 0;
16031f85851eSGao feng 			/* reserve for fragmentation and ipsec header */
16041f85851eSGao feng 			skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
16051f85851eSGao feng 				    dst_exthdrlen);
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 			/*
16081da177e4SLinus Torvalds 			 *	Find where to start putting bytes
16091da177e4SLinus Torvalds 			 */
161015e36f5bSWillem de Bruijn 			data = skb_put(skb, fraglen - pagedlen);
16111f85851eSGao feng 			skb_set_network_header(skb, exthdrlen);
16121f85851eSGao feng 			data += fragheaderlen;
1613b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1614b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
16151da177e4SLinus Torvalds 			if (fraggap) {
16161da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
16171da177e4SLinus Torvalds 					skb_prev, maxfraglen,
16181da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
16191da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
16201da177e4SLinus Torvalds 							  skb->csum);
16211da177e4SLinus Torvalds 				data += fraggap;
1622e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
16231da177e4SLinus Torvalds 			}
1624232cd35dSEric Dumazet 			if (copy > 0 &&
1625232cd35dSEric Dumazet 			    getfrag(from, data + transhdrlen, offset,
1626232cd35dSEric Dumazet 				    copy, fraggap, skb) < 0) {
16271da177e4SLinus Torvalds 				err = -EFAULT;
16281da177e4SLinus Torvalds 				kfree_skb(skb);
16291da177e4SLinus Torvalds 				goto error;
16301da177e4SLinus Torvalds 			}
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 			offset += copy;
163315e36f5bSWillem de Bruijn 			length -= copy + transhdrlen;
16341da177e4SLinus Torvalds 			transhdrlen = 0;
16351da177e4SLinus Torvalds 			exthdrlen = 0;
1636299b0767SSteffen Klassert 			dst_exthdrlen = 0;
16371da177e4SLinus Torvalds 
163852900d22SWillem de Bruijn 			/* Only the initial fragment is time stamped */
163952900d22SWillem de Bruijn 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
164052900d22SWillem de Bruijn 			cork->tx_flags = 0;
164152900d22SWillem de Bruijn 			skb_shinfo(skb)->tskey = tskey;
164252900d22SWillem de Bruijn 			tskey = 0;
164352900d22SWillem de Bruijn 			skb_zcopy_set(skb, uarg, &extra_uref);
164452900d22SWillem de Bruijn 
16450dec879fSJulian Anastasov 			if ((flags & MSG_CONFIRM) && !skb_prev)
16460dec879fSJulian Anastasov 				skb_set_dst_pending_confirm(skb, 1);
16470dec879fSJulian Anastasov 
16481da177e4SLinus Torvalds 			/*
16491da177e4SLinus Torvalds 			 * Put the packet on the pending queue
16501da177e4SLinus Torvalds 			 */
16511f4c6eb2SEric Dumazet 			if (!skb->destructor) {
16521f4c6eb2SEric Dumazet 				skb->destructor = sock_wfree;
16531f4c6eb2SEric Dumazet 				skb->sk = sk;
16541f4c6eb2SEric Dumazet 				wmem_alloc_delta += skb->truesize;
16551f4c6eb2SEric Dumazet 			}
16560bbe84a6SVlad Yasevich 			__skb_queue_tail(queue, skb);
16571da177e4SLinus Torvalds 			continue;
16581da177e4SLinus Torvalds 		}
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 		if (copy > length)
16611da177e4SLinus Torvalds 			copy = length;
16621da177e4SLinus Torvalds 
1663113f99c3SWillem de Bruijn 		if (!(rt->dst.dev->features&NETIF_F_SG) &&
1664113f99c3SWillem de Bruijn 		    skb_tailroom(skb) >= copy) {
16651da177e4SLinus Torvalds 			unsigned int off;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 			off = skb->len;
16681da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
16691da177e4SLinus Torvalds 						offset, copy, off, skb) < 0) {
16701da177e4SLinus Torvalds 				__skb_trim(skb, off);
16711da177e4SLinus Torvalds 				err = -EFAULT;
16721da177e4SLinus Torvalds 				goto error;
16731da177e4SLinus Torvalds 			}
1674b5947e5dSWillem de Bruijn 		} else if (!uarg || !uarg->zerocopy) {
16751da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 			err = -ENOMEM;
16785640f768SEric Dumazet 			if (!sk_page_frag_refill(sk, pfrag))
16791da177e4SLinus Torvalds 				goto error;
16801da177e4SLinus Torvalds 
16815640f768SEric Dumazet 			if (!skb_can_coalesce(skb, i, pfrag->page,
16825640f768SEric Dumazet 					      pfrag->offset)) {
16831da177e4SLinus Torvalds 				err = -EMSGSIZE;
16845640f768SEric Dumazet 				if (i == MAX_SKB_FRAGS)
16851da177e4SLinus Torvalds 					goto error;
16865640f768SEric Dumazet 
16875640f768SEric Dumazet 				__skb_fill_page_desc(skb, i, pfrag->page,
16885640f768SEric Dumazet 						     pfrag->offset, 0);
16895640f768SEric Dumazet 				skb_shinfo(skb)->nr_frags = ++i;
16905640f768SEric Dumazet 				get_page(pfrag->page);
16911da177e4SLinus Torvalds 			}
16925640f768SEric Dumazet 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
16939e903e08SEric Dumazet 			if (getfrag(from,
16945640f768SEric Dumazet 				    page_address(pfrag->page) + pfrag->offset,
16955640f768SEric Dumazet 				    offset, copy, skb->len, skb) < 0)
16965640f768SEric Dumazet 				goto error_efault;
16975640f768SEric Dumazet 
16985640f768SEric Dumazet 			pfrag->offset += copy;
16995640f768SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
17001da177e4SLinus Torvalds 			skb->len += copy;
17011da177e4SLinus Torvalds 			skb->data_len += copy;
1702f945fa7aSHerbert Xu 			skb->truesize += copy;
17031f4c6eb2SEric Dumazet 			wmem_alloc_delta += copy;
1704b5947e5dSWillem de Bruijn 		} else {
1705b5947e5dSWillem de Bruijn 			err = skb_zerocopy_iter_dgram(skb, from, copy);
1706b5947e5dSWillem de Bruijn 			if (err < 0)
1707b5947e5dSWillem de Bruijn 				goto error;
17081da177e4SLinus Torvalds 		}
17091da177e4SLinus Torvalds 		offset += copy;
17101da177e4SLinus Torvalds 		length -= copy;
17111da177e4SLinus Torvalds 	}
17125640f768SEric Dumazet 
17139e8445a5SPaolo Abeni 	if (wmem_alloc_delta)
17141f4c6eb2SEric Dumazet 		refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
17151da177e4SLinus Torvalds 	return 0;
17165640f768SEric Dumazet 
17175640f768SEric Dumazet error_efault:
17185640f768SEric Dumazet 	err = -EFAULT;
17191da177e4SLinus Torvalds error:
172097ef7b4cSWillem de Bruijn 	if (uarg)
172152900d22SWillem de Bruijn 		sock_zerocopy_put_abort(uarg, extra_uref);
1722bdc712b4SDavid S. Miller 	cork->length -= length;
17233bd653c8SDenis V. Lunev 	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
17241f4c6eb2SEric Dumazet 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
17251da177e4SLinus Torvalds 	return err;
17261da177e4SLinus Torvalds }
17270bbe84a6SVlad Yasevich 
17280bbe84a6SVlad Yasevich int ip6_append_data(struct sock *sk,
17290bbe84a6SVlad Yasevich 		    int getfrag(void *from, char *to, int offset, int len,
17300bbe84a6SVlad Yasevich 				int odd, struct sk_buff *skb),
173126879da5SWei Wang 		    void *from, int length, int transhdrlen,
173226879da5SWei Wang 		    struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
17335fdaa88dSWillem de Bruijn 		    struct rt6_info *rt, unsigned int flags)
17340bbe84a6SVlad Yasevich {
17350bbe84a6SVlad Yasevich 	struct inet_sock *inet = inet_sk(sk);
17360bbe84a6SVlad Yasevich 	struct ipv6_pinfo *np = inet6_sk(sk);
17370bbe84a6SVlad Yasevich 	int exthdrlen;
17380bbe84a6SVlad Yasevich 	int err;
17390bbe84a6SVlad Yasevich 
17400bbe84a6SVlad Yasevich 	if (flags&MSG_PROBE)
17410bbe84a6SVlad Yasevich 		return 0;
17420bbe84a6SVlad Yasevich 	if (skb_queue_empty(&sk->sk_write_queue)) {
17430bbe84a6SVlad Yasevich 		/*
17440bbe84a6SVlad Yasevich 		 * setup for corking
17450bbe84a6SVlad Yasevich 		 */
174626879da5SWei Wang 		err = ip6_setup_cork(sk, &inet->cork, &np->cork,
17475fdaa88dSWillem de Bruijn 				     ipc6, rt, fl6);
17480bbe84a6SVlad Yasevich 		if (err)
17490bbe84a6SVlad Yasevich 			return err;
17500bbe84a6SVlad Yasevich 
175126879da5SWei Wang 		exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
17520bbe84a6SVlad Yasevich 		length += exthdrlen;
17530bbe84a6SVlad Yasevich 		transhdrlen += exthdrlen;
17540bbe84a6SVlad Yasevich 	} else {
17550bbe84a6SVlad Yasevich 		fl6 = &inet->cork.fl.u.ip6;
17560bbe84a6SVlad Yasevich 		transhdrlen = 0;
17570bbe84a6SVlad Yasevich 	}
17580bbe84a6SVlad Yasevich 
17590bbe84a6SVlad Yasevich 	return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
17600bbe84a6SVlad Yasevich 				 &np->cork, sk_page_frag(sk), getfrag,
17615fdaa88dSWillem de Bruijn 				 from, length, transhdrlen, flags, ipc6);
17620bbe84a6SVlad Yasevich }
1763a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data);
17641da177e4SLinus Torvalds 
1765366e41d9SVlad Yasevich static void ip6_cork_release(struct inet_cork_full *cork,
1766366e41d9SVlad Yasevich 			     struct inet6_cork *v6_cork)
1767bf138862SPavel Emelyanov {
1768366e41d9SVlad Yasevich 	if (v6_cork->opt) {
1769366e41d9SVlad Yasevich 		kfree(v6_cork->opt->dst0opt);
1770366e41d9SVlad Yasevich 		kfree(v6_cork->opt->dst1opt);
1771366e41d9SVlad Yasevich 		kfree(v6_cork->opt->hopopt);
1772366e41d9SVlad Yasevich 		kfree(v6_cork->opt->srcrt);
1773366e41d9SVlad Yasevich 		kfree(v6_cork->opt);
1774366e41d9SVlad Yasevich 		v6_cork->opt = NULL;
17750178b695SHerbert Xu 	}
17760178b695SHerbert Xu 
1777366e41d9SVlad Yasevich 	if (cork->base.dst) {
1778366e41d9SVlad Yasevich 		dst_release(cork->base.dst);
1779366e41d9SVlad Yasevich 		cork->base.dst = NULL;
1780366e41d9SVlad Yasevich 		cork->base.flags &= ~IPCORK_ALLFRAG;
1781bf138862SPavel Emelyanov 	}
1782366e41d9SVlad Yasevich 	memset(&cork->fl, 0, sizeof(cork->fl));
1783bf138862SPavel Emelyanov }
1784bf138862SPavel Emelyanov 
17856422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk,
17866422398cSVlad Yasevich 			       struct sk_buff_head *queue,
17876422398cSVlad Yasevich 			       struct inet_cork_full *cork,
17886422398cSVlad Yasevich 			       struct inet6_cork *v6_cork)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
17911da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
17921da177e4SLinus Torvalds 	struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
17931da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
17943bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
17951da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
17966422398cSVlad Yasevich 	struct ipv6_txoptions *opt = v6_cork->opt;
17976422398cSVlad Yasevich 	struct rt6_info *rt = (struct rt6_info *)cork->base.dst;
17986422398cSVlad Yasevich 	struct flowi6 *fl6 = &cork->fl.u.ip6;
17994c9483b2SDavid S. Miller 	unsigned char proto = fl6->flowi6_proto;
18001da177e4SLinus Torvalds 
18016422398cSVlad Yasevich 	skb = __skb_dequeue(queue);
180263159f29SIan Morris 	if (!skb)
18031da177e4SLinus Torvalds 		goto out;
18041da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1807d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1808bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
18096422398cSVlad Yasevich 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1810cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
18111da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
18121da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
18131da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
18141da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
18151da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
18161da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
18171da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
18181da177e4SLinus Torvalds 	}
18191da177e4SLinus Torvalds 
182028a89453SHerbert Xu 	/* Allow local fragmentation. */
182160ff7467SWANG Cong 	skb->ignore_df = ip6_sk_ignore_df(sk);
182228a89453SHerbert Xu 
18234e3fd7a0SAlexey Dobriyan 	*final_dst = fl6->daddr;
1824cfe1fc77SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_header_len(skb));
18251da177e4SLinus Torvalds 	if (opt && opt->opt_flen)
18261da177e4SLinus Torvalds 		ipv6_push_frag_opts(skb, opt, &proto);
18271da177e4SLinus Torvalds 	if (opt && opt->opt_nflen)
1828613fa3caSDavid Lebrun 		ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr);
18291da177e4SLinus Torvalds 
1830e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
1831e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
18320660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
18331da177e4SLinus Torvalds 
18346422398cSVlad Yasevich 	ip6_flow_hdr(hdr, v6_cork->tclass,
1835cb1ce2efSTom Herbert 		     ip6_make_flowlabel(net, skb, fl6->flowlabel,
1836513674b5SShaohua Li 					ip6_autoflowlabel(net, np), fl6));
18376422398cSVlad Yasevich 	hdr->hop_limit = v6_cork->hop_limit;
18381da177e4SLinus Torvalds 	hdr->nexthdr = proto;
18394e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
18404e3fd7a0SAlexey Dobriyan 	hdr->daddr = *final_dst;
18411da177e4SLinus Torvalds 
1842a2c2064fSPatrick McHardy 	skb->priority = sk->sk_priority;
1843c6af0c22SWillem de Bruijn 	skb->mark = cork->base.mark;
1844a2c2064fSPatrick McHardy 
1845a818f75eSJesus Sanchez-Palencia 	skb->tstamp = cork->base.transmit_time;
1846a818f75eSJesus Sanchez-Palencia 
1847d8d1f30bSChangli Gao 	skb_dst_set(skb, dst_clone(&rt->dst));
1848edf391ffSNeil Horman 	IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
184914878f75SDavid L Stevens 	if (proto == IPPROTO_ICMPV6) {
1850adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
185114878f75SDavid L Stevens 
185243a43b60SHannes Frederic Sowa 		ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type);
185343a43b60SHannes Frederic Sowa 		ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
185414878f75SDavid L Stevens 	}
185514878f75SDavid L Stevens 
18566422398cSVlad Yasevich 	ip6_cork_release(cork, v6_cork);
18576422398cSVlad Yasevich out:
18586422398cSVlad Yasevich 	return skb;
18596422398cSVlad Yasevich }
18606422398cSVlad Yasevich 
18616422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb)
18626422398cSVlad Yasevich {
18636422398cSVlad Yasevich 	struct net *net = sock_net(skb->sk);
18646422398cSVlad Yasevich 	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
18656422398cSVlad Yasevich 	int err;
18666422398cSVlad Yasevich 
186733224b16SEric W. Biederman 	err = ip6_local_out(net, skb->sk, skb);
18681da177e4SLinus Torvalds 	if (err) {
18691da177e4SLinus Torvalds 		if (err > 0)
18706ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
18711da177e4SLinus Torvalds 		if (err)
18726422398cSVlad Yasevich 			IP6_INC_STATS(net, rt->rt6i_idev,
18736422398cSVlad Yasevich 				      IPSTATS_MIB_OUTDISCARDS);
18741da177e4SLinus Torvalds 	}
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	return err;
18776422398cSVlad Yasevich }
18786422398cSVlad Yasevich 
18796422398cSVlad Yasevich int ip6_push_pending_frames(struct sock *sk)
18806422398cSVlad Yasevich {
18816422398cSVlad Yasevich 	struct sk_buff *skb;
18826422398cSVlad Yasevich 
18836422398cSVlad Yasevich 	skb = ip6_finish_skb(sk);
18846422398cSVlad Yasevich 	if (!skb)
18856422398cSVlad Yasevich 		return 0;
18866422398cSVlad Yasevich 
18876422398cSVlad Yasevich 	return ip6_send_skb(skb);
18881da177e4SLinus Torvalds }
1889a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
18901da177e4SLinus Torvalds 
18910bbe84a6SVlad Yasevich static void __ip6_flush_pending_frames(struct sock *sk,
18926422398cSVlad Yasevich 				       struct sk_buff_head *queue,
18936422398cSVlad Yasevich 				       struct inet_cork_full *cork,
18946422398cSVlad Yasevich 				       struct inet6_cork *v6_cork)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	struct sk_buff *skb;
18971da177e4SLinus Torvalds 
18980bbe84a6SVlad Yasevich 	while ((skb = __skb_dequeue_tail(queue)) != NULL) {
1899adf30907SEric Dumazet 		if (skb_dst(skb))
1900adf30907SEric Dumazet 			IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
1901a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_OUTDISCARDS);
19021da177e4SLinus Torvalds 		kfree_skb(skb);
19031da177e4SLinus Torvalds 	}
19041da177e4SLinus Torvalds 
19056422398cSVlad Yasevich 	ip6_cork_release(cork, v6_cork);
19061da177e4SLinus Torvalds }
19070bbe84a6SVlad Yasevich 
19080bbe84a6SVlad Yasevich void ip6_flush_pending_frames(struct sock *sk)
19090bbe84a6SVlad Yasevich {
19106422398cSVlad Yasevich 	__ip6_flush_pending_frames(sk, &sk->sk_write_queue,
19116422398cSVlad Yasevich 				   &inet_sk(sk)->cork, &inet6_sk(sk)->cork);
19120bbe84a6SVlad Yasevich }
1913a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
19146422398cSVlad Yasevich 
19156422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk,
19166422398cSVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
19176422398cSVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
19186422398cSVlad Yasevich 			     void *from, int length, int transhdrlen,
191926879da5SWei Wang 			     struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
19206422398cSVlad Yasevich 			     struct rt6_info *rt, unsigned int flags,
19215fdaa88dSWillem de Bruijn 			     struct inet_cork_full *cork)
19226422398cSVlad Yasevich {
19236422398cSVlad Yasevich 	struct inet6_cork v6_cork;
19246422398cSVlad Yasevich 	struct sk_buff_head queue;
192526879da5SWei Wang 	int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
19266422398cSVlad Yasevich 	int err;
19276422398cSVlad Yasevich 
19286422398cSVlad Yasevich 	if (flags & MSG_PROBE)
19296422398cSVlad Yasevich 		return NULL;
19306422398cSVlad Yasevich 
19316422398cSVlad Yasevich 	__skb_queue_head_init(&queue);
19326422398cSVlad Yasevich 
19331cd7884dSWillem de Bruijn 	cork->base.flags = 0;
19341cd7884dSWillem de Bruijn 	cork->base.addr = 0;
19351cd7884dSWillem de Bruijn 	cork->base.opt = NULL;
19361cd7884dSWillem de Bruijn 	cork->base.dst = NULL;
19376422398cSVlad Yasevich 	v6_cork.opt = NULL;
19385fdaa88dSWillem de Bruijn 	err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt, fl6);
1939862c03eeSEric Dumazet 	if (err) {
19401cd7884dSWillem de Bruijn 		ip6_cork_release(cork, &v6_cork);
19416422398cSVlad Yasevich 		return ERR_PTR(err);
1942862c03eeSEric Dumazet 	}
194326879da5SWei Wang 	if (ipc6->dontfrag < 0)
194426879da5SWei Wang 		ipc6->dontfrag = inet6_sk(sk)->dontfrag;
19456422398cSVlad Yasevich 
19461cd7884dSWillem de Bruijn 	err = __ip6_append_data(sk, fl6, &queue, &cork->base, &v6_cork,
19476422398cSVlad Yasevich 				&current->task_frag, getfrag, from,
19486422398cSVlad Yasevich 				length + exthdrlen, transhdrlen + exthdrlen,
19495fdaa88dSWillem de Bruijn 				flags, ipc6);
19506422398cSVlad Yasevich 	if (err) {
19511cd7884dSWillem de Bruijn 		__ip6_flush_pending_frames(sk, &queue, cork, &v6_cork);
19526422398cSVlad Yasevich 		return ERR_PTR(err);
19536422398cSVlad Yasevich 	}
19546422398cSVlad Yasevich 
19551cd7884dSWillem de Bruijn 	return __ip6_make_skb(sk, &queue, cork, &v6_cork);
19566422398cSVlad Yasevich }
1957