xref: /openbmc/linux/net/ipv6/ip6_output.c (revision a34a101e1e6365638b02ea83a38d7a4cb228dc04)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 output functions
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	Based on linux/net/ipv4/ip_output.c
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Changes:
161da177e4SLinus Torvalds  *	A.N.Kuznetsov	:	airthmetics in fragmentation.
171da177e4SLinus Torvalds  *				extension headers are implemented.
181da177e4SLinus Torvalds  *				route changes now work.
191da177e4SLinus Torvalds  *				ip6_forward does not confuse sniffers.
201da177e4SLinus Torvalds  *				etc.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *      H. von Brand    :       Added missing #include <linux/string.h>
231da177e4SLinus Torvalds  *	Imran Patel	: 	frag id should be in NBO
241da177e4SLinus Torvalds  *      Kazunori MIYAZAWA @USAGI
251da177e4SLinus Torvalds  *			:       add ip6_append_data and related functions
261da177e4SLinus Torvalds  *				for datagram xmit
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/errno.h>
30ef76bc23SHerbert Xu #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/string.h>
321da177e4SLinus Torvalds #include <linux/socket.h>
331da177e4SLinus Torvalds #include <linux/net.h>
341da177e4SLinus Torvalds #include <linux/netdevice.h>
351da177e4SLinus Torvalds #include <linux/if_arp.h>
361da177e4SLinus Torvalds #include <linux/in6.h>
371da177e4SLinus Torvalds #include <linux/tcp.h>
381da177e4SLinus Torvalds #include <linux/route.h>
39b59f45d0SHerbert Xu #include <linux/module.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/netfilter.h>
431da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <net/sock.h>
461da177e4SLinus Torvalds #include <net/snmp.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #include <net/ipv6.h>
491da177e4SLinus Torvalds #include <net/ndisc.h>
501da177e4SLinus Torvalds #include <net/protocol.h>
511da177e4SLinus Torvalds #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/addrconf.h>
531da177e4SLinus Torvalds #include <net/rawv6.h>
541da177e4SLinus Torvalds #include <net/icmp.h>
551da177e4SLinus Torvalds #include <net/xfrm.h>
561da177e4SLinus Torvalds #include <net/checksum.h>
577bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
581da177e4SLinus Torvalds 
59ad0081e4SDavid Stevens int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
601da177e4SLinus Torvalds 
61ef76bc23SHerbert Xu int __ip6_local_out(struct sk_buff *skb)
62ef76bc23SHerbert Xu {
63ef76bc23SHerbert Xu 	int len;
64ef76bc23SHerbert Xu 
65ef76bc23SHerbert Xu 	len = skb->len - sizeof(struct ipv6hdr);
66ef76bc23SHerbert Xu 	if (len > IPV6_MAXPLEN)
67ef76bc23SHerbert Xu 		len = 0;
68ef76bc23SHerbert Xu 	ipv6_hdr(skb)->payload_len = htons(len);
69ef76bc23SHerbert Xu 
70b2e0b385SJan Engelhardt 	return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
71b2e0b385SJan Engelhardt 		       skb_dst(skb)->dev, dst_output);
72ef76bc23SHerbert Xu }
73ef76bc23SHerbert Xu 
74ef76bc23SHerbert Xu int ip6_local_out(struct sk_buff *skb)
75ef76bc23SHerbert Xu {
76ef76bc23SHerbert Xu 	int err;
77ef76bc23SHerbert Xu 
78ef76bc23SHerbert Xu 	err = __ip6_local_out(skb);
79ef76bc23SHerbert Xu 	if (likely(err == 1))
80ef76bc23SHerbert Xu 		err = dst_output(skb);
81ef76bc23SHerbert Xu 
82ef76bc23SHerbert Xu 	return err;
83ef76bc23SHerbert Xu }
84ef76bc23SHerbert Xu EXPORT_SYMBOL_GPL(ip6_local_out);
85ef76bc23SHerbert Xu 
861da177e4SLinus Torvalds /* dev_loopback_xmit for use with netfilter. */
871da177e4SLinus Torvalds static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
881da177e4SLinus Torvalds {
89459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(newskb);
90bbe735e4SArnaldo Carvalho de Melo 	__skb_pull(newskb, skb_network_offset(newskb));
911da177e4SLinus Torvalds 	newskb->pkt_type = PACKET_LOOPBACK;
921da177e4SLinus Torvalds 	newskb->ip_summed = CHECKSUM_UNNECESSARY;
93adf30907SEric Dumazet 	WARN_ON(!skb_dst(newskb));
941da177e4SLinus Torvalds 
95e30b38c2SEric Dumazet 	netif_rx_ni(newskb);
961da177e4SLinus Torvalds 	return 0;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
999e508490SJan Engelhardt static int ip6_finish_output2(struct sk_buff *skb)
1001da177e4SLinus Torvalds {
101adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1021da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
103f6b72b62SDavid S. Miller 	struct neighbour *neigh;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IPV6);
1061da177e4SLinus Torvalds 	skb->dev = dev;
1071da177e4SLinus Torvalds 
1080660e03fSArnaldo Carvalho de Melo 	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
109adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
1101da177e4SLinus Torvalds 
1117ad6848cSOctavian Purdila 		if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
112d1db275dSPatrick McHardy 		    ((mroute6_socket(dev_net(dev), skb) &&
113bd91b8bfSBenjamin Thery 		     !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
1140660e03fSArnaldo Carvalho de Melo 		     ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
1157bc570c8SYOSHIFUJI Hideaki 					 &ipv6_hdr(skb)->saddr))) {
1161da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 			/* Do not check for IFF_ALLMULTI; multicast routing
1191da177e4SLinus Torvalds 			   is not supported in any case.
1201da177e4SLinus Torvalds 			 */
1211da177e4SLinus Torvalds 			if (newskb)
122b2e0b385SJan Engelhardt 				NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
123b2e0b385SJan Engelhardt 					newskb, NULL, newskb->dev,
1241da177e4SLinus Torvalds 					ip6_dev_loopback_xmit);
1251da177e4SLinus Torvalds 
1260660e03fSArnaldo Carvalho de Melo 			if (ipv6_hdr(skb)->hop_limit == 0) {
1273bd653c8SDenis V. Lunev 				IP6_INC_STATS(dev_net(dev), idev,
1283bd653c8SDenis V. Lunev 					      IPSTATS_MIB_OUTDISCARDS);
1291da177e4SLinus Torvalds 				kfree_skb(skb);
1301da177e4SLinus Torvalds 				return 0;
1311da177e4SLinus Torvalds 			}
1321da177e4SLinus Torvalds 		}
1331da177e4SLinus Torvalds 
134edf391ffSNeil Horman 		IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
135edf391ffSNeil Horman 				skb->len);
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 
138f2c31e32SEric Dumazet 	rcu_read_lock();
13927217455SDavid Miller 	neigh = dst_get_neighbour_noref(dst);
140f2c31e32SEric Dumazet 	if (neigh) {
141f2c31e32SEric Dumazet 		int res = neigh_output(neigh, skb);
14205e3aa09SDavid S. Miller 
143f2c31e32SEric Dumazet 		rcu_read_unlock();
144f2c31e32SEric Dumazet 		return res;
145f2c31e32SEric Dumazet 	}
146f2c31e32SEric Dumazet 	rcu_read_unlock();
1479e508490SJan Engelhardt 	IP6_INC_STATS_BH(dev_net(dst->dev),
1489e508490SJan Engelhardt 			 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
1499e508490SJan Engelhardt 	kfree_skb(skb);
1509e508490SJan Engelhardt 	return -EINVAL;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1539e508490SJan Engelhardt static int ip6_finish_output(struct sk_buff *skb)
1549e508490SJan Engelhardt {
1559e508490SJan Engelhardt 	if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
1569e508490SJan Engelhardt 	    dst_allfrag(skb_dst(skb)))
1579e508490SJan Engelhardt 		return ip6_fragment(skb, ip6_finish_output2);
1589e508490SJan Engelhardt 	else
1599e508490SJan Engelhardt 		return ip6_finish_output2(skb);
1609e508490SJan Engelhardt }
1619e508490SJan Engelhardt 
1621da177e4SLinus Torvalds int ip6_output(struct sk_buff *skb)
1631da177e4SLinus Torvalds {
1649e508490SJan Engelhardt 	struct net_device *dev = skb_dst(skb)->dev;
165adf30907SEric Dumazet 	struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
166778d80beSYOSHIFUJI Hideaki 	if (unlikely(idev->cnf.disable_ipv6)) {
1679e508490SJan Engelhardt 		IP6_INC_STATS(dev_net(dev), idev,
1683bd653c8SDenis V. Lunev 			      IPSTATS_MIB_OUTDISCARDS);
169778d80beSYOSHIFUJI Hideaki 		kfree_skb(skb);
170778d80beSYOSHIFUJI Hideaki 		return 0;
171778d80beSYOSHIFUJI Hideaki 	}
172778d80beSYOSHIFUJI Hideaki 
1739c6eb28aSJan Engelhardt 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
1749c6eb28aSJan Engelhardt 			    ip6_finish_output,
1759c6eb28aSJan Engelhardt 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /*
179b5d43998SShan Wei  *	xmit an sk_buff (used by TCP, SCTP and DCCP)
1801da177e4SLinus Torvalds  */
1811da177e4SLinus Torvalds 
1824c9483b2SDavid S. Miller int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
183b903d324SEric Dumazet 	     struct ipv6_txoptions *opt, int tclass)
1841da177e4SLinus Torvalds {
1853bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
186b30bd282SPatrick McHardy 	struct ipv6_pinfo *np = inet6_sk(sk);
1874c9483b2SDavid S. Miller 	struct in6_addr *first_hop = &fl6->daddr;
188adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1891da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
1904c9483b2SDavid S. Miller 	u8  proto = fl6->flowi6_proto;
1911da177e4SLinus Torvalds 	int seg_len = skb->len;
192e651f03aSGerrit Renker 	int hlimit = -1;
1931da177e4SLinus Torvalds 	u32 mtu;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	if (opt) {
196c2636b4dSChuck Lever 		unsigned int head_room;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 		/* First: exthdrs may take lots of space (~8K for now)
1991da177e4SLinus Torvalds 		   MAX_HEADER is not enough.
2001da177e4SLinus Torvalds 		 */
2011da177e4SLinus Torvalds 		head_room = opt->opt_nflen + opt->opt_flen;
2021da177e4SLinus Torvalds 		seg_len += head_room;
2031da177e4SLinus Torvalds 		head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 		if (skb_headroom(skb) < head_room) {
2061da177e4SLinus Torvalds 			struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
207a11d206dSYOSHIFUJI Hideaki 			if (skb2 == NULL) {
208adf30907SEric Dumazet 				IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
209a11d206dSYOSHIFUJI Hideaki 					      IPSTATS_MIB_OUTDISCARDS);
2101da177e4SLinus Torvalds 				kfree_skb(skb);
2111da177e4SLinus Torvalds 				return -ENOBUFS;
2121da177e4SLinus Torvalds 			}
213808db80aSEric Dumazet 			consume_skb(skb);
214a11d206dSYOSHIFUJI Hideaki 			skb = skb2;
2151da177e4SLinus Torvalds 			skb_set_owner_w(skb, sk);
2161da177e4SLinus Torvalds 		}
2171da177e4SLinus Torvalds 		if (opt->opt_flen)
2181da177e4SLinus Torvalds 			ipv6_push_frag_opts(skb, opt, &proto);
2191da177e4SLinus Torvalds 		if (opt->opt_nflen)
2201da177e4SLinus Torvalds 			ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 
223e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
224e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
2250660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	/*
2281da177e4SLinus Torvalds 	 *	Fill in the IPv6 header
2291da177e4SLinus Torvalds 	 */
230b903d324SEric Dumazet 	if (np)
2311da177e4SLinus Torvalds 		hlimit = np->hop_limit;
2321da177e4SLinus Torvalds 	if (hlimit < 0)
2336b75d090SYOSHIFUJI Hideaki 		hlimit = ip6_dst_hoplimit(dst);
2341da177e4SLinus Torvalds 
2354c9483b2SDavid S. Miller 	*(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl6->flowlabel;
23641a1f8eaSYOSHIFUJI Hideaki 
2371da177e4SLinus Torvalds 	hdr->payload_len = htons(seg_len);
2381da177e4SLinus Torvalds 	hdr->nexthdr = proto;
2391da177e4SLinus Torvalds 	hdr->hop_limit = hlimit;
2401da177e4SLinus Torvalds 
2414e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
2424e3fd7a0SAlexey Dobriyan 	hdr->daddr = *first_hop;
2431da177e4SLinus Torvalds 
244a2c2064fSPatrick McHardy 	skb->priority = sk->sk_priority;
2454a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
246a2c2064fSPatrick McHardy 
2471da177e4SLinus Torvalds 	mtu = dst_mtu(dst);
248283d07acSWei Yongjun 	if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
249adf30907SEric Dumazet 		IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
250edf391ffSNeil Horman 			      IPSTATS_MIB_OUT, skb->len);
251b2e0b385SJan Engelhardt 		return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
252b2e0b385SJan Engelhardt 			       dst->dev, dst_output);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
255e87cc472SJoe Perches 	net_dbg_ratelimited("IPv6: sending pkt_too_big to self\n");
2561da177e4SLinus Torvalds 	skb->dev = dst->dev;
2573ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
258adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
2591da177e4SLinus Torvalds 	kfree_skb(skb);
2601da177e4SLinus Torvalds 	return -EMSGSIZE;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2637159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_xmit);
2647159039aSYOSHIFUJI Hideaki 
2651da177e4SLinus Torvalds /*
2661da177e4SLinus Torvalds  *	To avoid extra problems ND packets are send through this
2671da177e4SLinus Torvalds  *	routine. It's code duplication but I really want to avoid
2681da177e4SLinus Torvalds  *	extra checks since ipv6_build_header is used by TCP (which
2691da177e4SLinus Torvalds  *	is for us performance critical)
2701da177e4SLinus Torvalds  */
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
2739acd9f3aSYOSHIFUJI Hideaki 	       const struct in6_addr *saddr, const struct in6_addr *daddr,
2741da177e4SLinus Torvalds 	       int proto, int len)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2771da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IPV6);
2801da177e4SLinus Torvalds 	skb->dev = dev;
2811da177e4SLinus Torvalds 
28255f79cc0SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
28355f79cc0SArnaldo Carvalho de Melo 	skb_put(skb, sizeof(struct ipv6hdr));
2840660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
2851da177e4SLinus Torvalds 
286ae08e1f0SAl Viro 	*(__be32*)hdr = htonl(0x60000000);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	hdr->payload_len = htons(len);
2891da177e4SLinus Torvalds 	hdr->nexthdr = proto;
2901da177e4SLinus Torvalds 	hdr->hop_limit = np->hop_limit;
2911da177e4SLinus Torvalds 
2924e3fd7a0SAlexey Dobriyan 	hdr->saddr = *saddr;
2934e3fd7a0SAlexey Dobriyan 	hdr->daddr = *daddr;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct ip6_ra_chain *ra;
3011da177e4SLinus Torvalds 	struct sock *last = NULL;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	read_lock(&ip6_ra_lock);
3041da177e4SLinus Torvalds 	for (ra = ip6_ra_chain; ra; ra = ra->next) {
3051da177e4SLinus Torvalds 		struct sock *sk = ra->sk;
3060bd1b59bSAndrew McDonald 		if (sk && ra->sel == sel &&
3070bd1b59bSAndrew McDonald 		    (!sk->sk_bound_dev_if ||
3080bd1b59bSAndrew McDonald 		     sk->sk_bound_dev_if == skb->dev->ifindex)) {
3091da177e4SLinus Torvalds 			if (last) {
3101da177e4SLinus Torvalds 				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
3111da177e4SLinus Torvalds 				if (skb2)
3121da177e4SLinus Torvalds 					rawv6_rcv(last, skb2);
3131da177e4SLinus Torvalds 			}
3141da177e4SLinus Torvalds 			last = sk;
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	if (last) {
3191da177e4SLinus Torvalds 		rawv6_rcv(last, skb);
3201da177e4SLinus Torvalds 		read_unlock(&ip6_ra_lock);
3211da177e4SLinus Torvalds 		return 1;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 	read_unlock(&ip6_ra_lock);
3241da177e4SLinus Torvalds 	return 0;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
327e21e0b5fSVille Nuorvala static int ip6_forward_proxy_check(struct sk_buff *skb)
328e21e0b5fSVille Nuorvala {
3290660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
330e21e0b5fSVille Nuorvala 	u8 nexthdr = hdr->nexthdr;
33175f2811cSJesse Gross 	__be16 frag_off;
332e21e0b5fSVille Nuorvala 	int offset;
333e21e0b5fSVille Nuorvala 
334e21e0b5fSVille Nuorvala 	if (ipv6_ext_hdr(nexthdr)) {
33575f2811cSJesse Gross 		offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
336e21e0b5fSVille Nuorvala 		if (offset < 0)
337e21e0b5fSVille Nuorvala 			return 0;
338e21e0b5fSVille Nuorvala 	} else
339e21e0b5fSVille Nuorvala 		offset = sizeof(struct ipv6hdr);
340e21e0b5fSVille Nuorvala 
341e21e0b5fSVille Nuorvala 	if (nexthdr == IPPROTO_ICMPV6) {
342e21e0b5fSVille Nuorvala 		struct icmp6hdr *icmp6;
343e21e0b5fSVille Nuorvala 
344d56f90a7SArnaldo Carvalho de Melo 		if (!pskb_may_pull(skb, (skb_network_header(skb) +
345d56f90a7SArnaldo Carvalho de Melo 					 offset + 1 - skb->data)))
346e21e0b5fSVille Nuorvala 			return 0;
347e21e0b5fSVille Nuorvala 
348d56f90a7SArnaldo Carvalho de Melo 		icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
349e21e0b5fSVille Nuorvala 
350e21e0b5fSVille Nuorvala 		switch (icmp6->icmp6_type) {
351e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_SOLICITATION:
352e21e0b5fSVille Nuorvala 		case NDISC_ROUTER_ADVERTISEMENT:
353e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_SOLICITATION:
354e21e0b5fSVille Nuorvala 		case NDISC_NEIGHBOUR_ADVERTISEMENT:
355e21e0b5fSVille Nuorvala 		case NDISC_REDIRECT:
356e21e0b5fSVille Nuorvala 			/* For reaction involving unicast neighbor discovery
357e21e0b5fSVille Nuorvala 			 * message destined to the proxied address, pass it to
358e21e0b5fSVille Nuorvala 			 * input function.
359e21e0b5fSVille Nuorvala 			 */
360e21e0b5fSVille Nuorvala 			return 1;
361e21e0b5fSVille Nuorvala 		default:
362e21e0b5fSVille Nuorvala 			break;
363e21e0b5fSVille Nuorvala 		}
364e21e0b5fSVille Nuorvala 	}
365e21e0b5fSVille Nuorvala 
36674553b09SVille Nuorvala 	/*
36774553b09SVille Nuorvala 	 * The proxying router can't forward traffic sent to a link-local
36874553b09SVille Nuorvala 	 * address, so signal the sender and discard the packet. This
36974553b09SVille Nuorvala 	 * behavior is clarified by the MIPv6 specification.
37074553b09SVille Nuorvala 	 */
37174553b09SVille Nuorvala 	if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
37274553b09SVille Nuorvala 		dst_link_failure(skb);
37374553b09SVille Nuorvala 		return -1;
37474553b09SVille Nuorvala 	}
37574553b09SVille Nuorvala 
376e21e0b5fSVille Nuorvala 	return 0;
377e21e0b5fSVille Nuorvala }
378e21e0b5fSVille Nuorvala 
3791da177e4SLinus Torvalds static inline int ip6_forward_finish(struct sk_buff *skb)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	return dst_output(skb);
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds int ip6_forward(struct sk_buff *skb)
3851da177e4SLinus Torvalds {
386adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3870660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
3881da177e4SLinus Torvalds 	struct inet6_skb_parm *opt = IP6CB(skb);
389c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dst->dev);
39014f3ad6fSUlrich Weber 	u32 mtu;
3911da177e4SLinus Torvalds 
39253b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->forwarding == 0)
3931da177e4SLinus Torvalds 		goto error;
3941da177e4SLinus Torvalds 
3954497b076SBen Hutchings 	if (skb_warn_if_lro(skb))
3964497b076SBen Hutchings 		goto drop;
3974497b076SBen Hutchings 
3981da177e4SLinus Torvalds 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
3993bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
4001da177e4SLinus Torvalds 		goto drop;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
40372b43d08SAlexey Kuznetsov 	if (skb->pkt_type != PACKET_HOST)
40472b43d08SAlexey Kuznetsov 		goto drop;
40572b43d08SAlexey Kuznetsov 
40635fc92a9SHerbert Xu 	skb_forward_csum(skb);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	/*
4091da177e4SLinus Torvalds 	 *	We DO NOT make any processing on
4101da177e4SLinus Torvalds 	 *	RA packets, pushing them to user level AS IS
4111da177e4SLinus Torvalds 	 *	without ane WARRANTY that application will be able
4121da177e4SLinus Torvalds 	 *	to interpret them. The reason is that we
4131da177e4SLinus Torvalds 	 *	cannot make anything clever here.
4141da177e4SLinus Torvalds 	 *
4151da177e4SLinus Torvalds 	 *	We are not end-node, so that if packet contains
4161da177e4SLinus Torvalds 	 *	AH/ESP, we cannot make anything.
4171da177e4SLinus Torvalds 	 *	Defragmentation also would be mistake, RA packets
4181da177e4SLinus Torvalds 	 *	cannot be fragmented, because there is no warranty
4191da177e4SLinus Torvalds 	 *	that different fragments will go along one path. --ANK
4201da177e4SLinus Torvalds 	 */
4211da177e4SLinus Torvalds 	if (opt->ra) {
422d56f90a7SArnaldo Carvalho de Melo 		u8 *ptr = skb_network_header(skb) + opt->ra;
4231da177e4SLinus Torvalds 		if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
4241da177e4SLinus Torvalds 			return 0;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/*
4281da177e4SLinus Torvalds 	 *	check and decrement ttl
4291da177e4SLinus Torvalds 	 */
4301da177e4SLinus Torvalds 	if (hdr->hop_limit <= 1) {
4311da177e4SLinus Torvalds 		/* Force OUTPUT device used as source address */
4321da177e4SLinus Torvalds 		skb->dev = dst->dev;
4333ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
434483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net,
435483a47d2SDenis V. Lunev 				 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 		kfree_skb(skb);
4381da177e4SLinus Torvalds 		return -ETIMEDOUT;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
441fbea49e1SYOSHIFUJI Hideaki 	/* XXX: idev->cnf.proxy_ndp? */
44253b7997fSYOSHIFUJI Hideaki 	if (net->ipv6.devconf_all->proxy_ndp &&
4438a3edd80SDaniel Lezcano 	    pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
44474553b09SVille Nuorvala 		int proxied = ip6_forward_proxy_check(skb);
44574553b09SVille Nuorvala 		if (proxied > 0)
446e21e0b5fSVille Nuorvala 			return ip6_input(skb);
44774553b09SVille Nuorvala 		else if (proxied < 0) {
4483bd653c8SDenis V. Lunev 			IP6_INC_STATS(net, ip6_dst_idev(dst),
4493bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INDISCARDS);
45074553b09SVille Nuorvala 			goto drop;
45174553b09SVille Nuorvala 		}
452e21e0b5fSVille Nuorvala 	}
453e21e0b5fSVille Nuorvala 
4541da177e4SLinus Torvalds 	if (!xfrm6_route_forward(skb)) {
4553bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
4561da177e4SLinus Torvalds 		goto drop;
4571da177e4SLinus Torvalds 	}
458adf30907SEric Dumazet 	dst = skb_dst(skb);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	/* IPv6 specs say nothing about it, but it is clear that we cannot
4611da177e4SLinus Torvalds 	   send redirects to source routed frames.
4621e5dc146SMasahide NAKAMURA 	   We don't send redirects to frames decapsulated from IPsec.
4631da177e4SLinus Torvalds 	 */
464c45a3dfbSDavid S. Miller 	if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) {
4651da177e4SLinus Torvalds 		struct in6_addr *target = NULL;
4661da177e4SLinus Torvalds 		struct rt6_info *rt;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 		/*
4691da177e4SLinus Torvalds 		 *	incoming and outgoing devices are the same
4701da177e4SLinus Torvalds 		 *	send a redirect.
4711da177e4SLinus Torvalds 		 */
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 		rt = (struct rt6_info *) dst;
474c45a3dfbSDavid S. Miller 		if (rt->rt6i_flags & RTF_GATEWAY)
475c45a3dfbSDavid S. Miller 			target = &rt->rt6i_gateway;
4761da177e4SLinus Torvalds 		else
4771da177e4SLinus Torvalds 			target = &hdr->daddr;
4781da177e4SLinus Torvalds 
47992d86829SDavid S. Miller 		if (!rt->rt6i_peer)
48092d86829SDavid S. Miller 			rt6_bind_peer(rt, 1);
48192d86829SDavid S. Miller 
4821da177e4SLinus Torvalds 		/* Limit redirects both by destination (here)
4831da177e4SLinus Torvalds 		   and by source (inside ndisc_send_redirect)
4841da177e4SLinus Torvalds 		 */
48592d86829SDavid S. Miller 		if (inet_peer_xrlim_allow(rt->rt6i_peer, 1*HZ))
4864991969aSDavid S. Miller 			ndisc_send_redirect(skb, target);
4875bb1ab09SDavid L Stevens 	} else {
4885bb1ab09SDavid L Stevens 		int addrtype = ipv6_addr_type(&hdr->saddr);
4895bb1ab09SDavid L Stevens 
4901da177e4SLinus Torvalds 		/* This check is security critical. */
491f81b2e7dSYOSHIFUJI Hideaki 		if (addrtype == IPV6_ADDR_ANY ||
492f81b2e7dSYOSHIFUJI Hideaki 		    addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
4931da177e4SLinus Torvalds 			goto error;
4945bb1ab09SDavid L Stevens 		if (addrtype & IPV6_ADDR_LINKLOCAL) {
4955bb1ab09SDavid L Stevens 			icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4963ffe533cSAlexey Dobriyan 				    ICMPV6_NOT_NEIGHBOUR, 0);
4975bb1ab09SDavid L Stevens 			goto error;
4985bb1ab09SDavid L Stevens 		}
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 
50114f3ad6fSUlrich Weber 	mtu = dst_mtu(dst);
50214f3ad6fSUlrich Weber 	if (mtu < IPV6_MIN_MTU)
50314f3ad6fSUlrich Weber 		mtu = IPV6_MIN_MTU;
50414f3ad6fSUlrich Weber 
5050aa68271SHerbert Xu 	if (skb->len > mtu && !skb_is_gso(skb)) {
5061da177e4SLinus Torvalds 		/* Again, force OUTPUT device used as source address */
5071da177e4SLinus Torvalds 		skb->dev = dst->dev;
50814f3ad6fSUlrich Weber 		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
509483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net,
510483a47d2SDenis V. Lunev 				 ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
511483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net,
512483a47d2SDenis V. Lunev 				 ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
5131da177e4SLinus Torvalds 		kfree_skb(skb);
5141da177e4SLinus Torvalds 		return -EMSGSIZE;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (skb_cow(skb, dst->dev->hard_header_len)) {
5183bd653c8SDenis V. Lunev 		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
5191da177e4SLinus Torvalds 		goto drop;
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 
5220660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	/* Mangling hops number delayed to point after skb COW */
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	hdr->hop_limit--;
5271da177e4SLinus Torvalds 
528483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
529b2e0b385SJan Engelhardt 	return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
5306e23ae2aSPatrick McHardy 		       ip6_forward_finish);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds error:
533483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
5341da177e4SLinus Torvalds drop:
5351da177e4SLinus Torvalds 	kfree_skb(skb);
5361da177e4SLinus Torvalds 	return -EINVAL;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
5421da177e4SLinus Torvalds 	to->priority = from->priority;
5431da177e4SLinus Torvalds 	to->protocol = from->protocol;
544adf30907SEric Dumazet 	skb_dst_drop(to);
545adf30907SEric Dumazet 	skb_dst_set(to, dst_clone(skb_dst(from)));
5461da177e4SLinus Torvalds 	to->dev = from->dev;
54782e91ffeSThomas Graf 	to->mark = from->mark;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
5501da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
5511da177e4SLinus Torvalds #endif
552e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
553ba9dda3aSJozsef Kadlecsik #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
554ba9dda3aSJozsef Kadlecsik     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
555ba9dda3aSJozsef Kadlecsik 	to->nf_trace = from->nf_trace;
556ba9dda3aSJozsef Kadlecsik #endif
557984bc16cSJames Morris 	skb_copy_secmark(to, from);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	u16 offset = sizeof(struct ipv6hdr);
5630660e03fSArnaldo Carvalho de Melo 	struct ipv6_opt_hdr *exthdr =
5640660e03fSArnaldo Carvalho de Melo 				(struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
56527a884dcSArnaldo Carvalho de Melo 	unsigned int packet_len = skb->tail - skb->network_header;
5661da177e4SLinus Torvalds 	int found_rhdr = 0;
5670660e03fSArnaldo Carvalho de Melo 	*nexthdr = &ipv6_hdr(skb)->nexthdr;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	while (offset + 1 <= packet_len) {
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 		switch (**nexthdr) {
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 		case NEXTHDR_HOP:
57427637df9SMasahide NAKAMURA 			break;
5751da177e4SLinus Torvalds 		case NEXTHDR_ROUTING:
57627637df9SMasahide NAKAMURA 			found_rhdr = 1;
57727637df9SMasahide NAKAMURA 			break;
5781da177e4SLinus Torvalds 		case NEXTHDR_DEST:
57959fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
58027637df9SMasahide NAKAMURA 			if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
58127637df9SMasahide NAKAMURA 				break;
58227637df9SMasahide NAKAMURA #endif
58327637df9SMasahide NAKAMURA 			if (found_rhdr)
58427637df9SMasahide NAKAMURA 				return offset;
5851da177e4SLinus Torvalds 			break;
5861da177e4SLinus Torvalds 		default :
5871da177e4SLinus Torvalds 			return offset;
5881da177e4SLinus Torvalds 		}
58927637df9SMasahide NAKAMURA 
59027637df9SMasahide NAKAMURA 		offset += ipv6_optlen(exthdr);
59127637df9SMasahide NAKAMURA 		*nexthdr = &exthdr->nexthdr;
592d56f90a7SArnaldo Carvalho de Melo 		exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
593d56f90a7SArnaldo Carvalho de Melo 						 offset);
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	return offset;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
59987c48fa3SEric Dumazet void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
60087c48fa3SEric Dumazet {
60187c48fa3SEric Dumazet 	static atomic_t ipv6_fragmentation_id;
60287c48fa3SEric Dumazet 	int old, new;
60387c48fa3SEric Dumazet 
604e688a604SEric Dumazet 	if (rt && !(rt->dst.flags & DST_NOPEER)) {
60587c48fa3SEric Dumazet 		struct inet_peer *peer;
60687c48fa3SEric Dumazet 
60787c48fa3SEric Dumazet 		if (!rt->rt6i_peer)
60887c48fa3SEric Dumazet 			rt6_bind_peer(rt, 1);
60987c48fa3SEric Dumazet 		peer = rt->rt6i_peer;
61087c48fa3SEric Dumazet 		if (peer) {
61187c48fa3SEric Dumazet 			fhdr->identification = htonl(inet_getid(peer, 0));
61287c48fa3SEric Dumazet 			return;
61387c48fa3SEric Dumazet 		}
61487c48fa3SEric Dumazet 	}
61587c48fa3SEric Dumazet 	do {
61687c48fa3SEric Dumazet 		old = atomic_read(&ipv6_fragmentation_id);
61787c48fa3SEric Dumazet 		new = old + 1;
61887c48fa3SEric Dumazet 		if (!new)
61987c48fa3SEric Dumazet 			new = 1;
62087c48fa3SEric Dumazet 	} while (atomic_cmpxchg(&ipv6_fragmentation_id, old, new) != old);
62187c48fa3SEric Dumazet 	fhdr->identification = htonl(new);
62287c48fa3SEric Dumazet }
62387c48fa3SEric Dumazet 
624ad0081e4SDavid Stevens int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	struct sk_buff *frag;
627adf30907SEric Dumazet 	struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
628d91675f9SYOSHIFUJI Hideaki 	struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
6291da177e4SLinus Torvalds 	struct ipv6hdr *tmp_hdr;
6301da177e4SLinus Torvalds 	struct frag_hdr *fh;
6311da177e4SLinus Torvalds 	unsigned int mtu, hlen, left, len;
632a7ae1992SHerbert Xu 	int hroom, troom;
633ae08e1f0SAl Viro 	__be32 frag_id = 0;
6341da177e4SLinus Torvalds 	int ptr, offset = 0, err=0;
6351da177e4SLinus Torvalds 	u8 *prevhdr, nexthdr = 0;
636adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	hlen = ip6_find_1stfragopt(skb, &prevhdr);
6391da177e4SLinus Torvalds 	nexthdr = *prevhdr;
6401da177e4SLinus Torvalds 
641628a5c56SJohn Heffner 	mtu = ip6_skb_dst_mtu(skb);
642b881ef76SJohn Heffner 
643b881ef76SJohn Heffner 	/* We must not fragment if the socket is set to force MTU discovery
64414f3ad6fSUlrich Weber 	 * or if the skb it not generated by a local socket.
645b881ef76SJohn Heffner 	 */
646*a34a101eSEric Dumazet 	if (unlikely(!skb->local_df && skb->len > mtu)) {
647*a34a101eSEric Dumazet 		if (skb->sk && dst_allfrag(skb_dst(skb)))
648*a34a101eSEric Dumazet 			sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
649*a34a101eSEric Dumazet 
650adf30907SEric Dumazet 		skb->dev = skb_dst(skb)->dev;
6513ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
652adf30907SEric Dumazet 		IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
6533bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGFAILS);
654b881ef76SJohn Heffner 		kfree_skb(skb);
655b881ef76SJohn Heffner 		return -EMSGSIZE;
656b881ef76SJohn Heffner 	}
657b881ef76SJohn Heffner 
658d91675f9SYOSHIFUJI Hideaki 	if (np && np->frag_size < mtu) {
659d91675f9SYOSHIFUJI Hideaki 		if (np->frag_size)
660d91675f9SYOSHIFUJI Hideaki 			mtu = np->frag_size;
661d91675f9SYOSHIFUJI Hideaki 	}
662d91675f9SYOSHIFUJI Hideaki 	mtu -= hlen + sizeof(struct frag_hdr);
6631da177e4SLinus Torvalds 
66421dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
6651da177e4SLinus Torvalds 		int first_len = skb_pagelen(skb);
6663d13008eSEric Dumazet 		struct sk_buff *frag2;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
6691da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
6701da177e4SLinus Torvalds 		    skb_cloned(skb))
6711da177e4SLinus Torvalds 			goto slow_path;
6721da177e4SLinus Torvalds 
6734d9092bbSDavid S. Miller 		skb_walk_frags(skb, frag) {
6741da177e4SLinus Torvalds 			/* Correct geometry. */
6751da177e4SLinus Torvalds 			if (frag->len > mtu ||
6761da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
6771da177e4SLinus Torvalds 			    skb_headroom(frag) < hlen)
6783d13008eSEric Dumazet 				goto slow_path_clean;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 			/* Partially cloned skb? */
6811da177e4SLinus Torvalds 			if (skb_shared(frag))
6823d13008eSEric Dumazet 				goto slow_path_clean;
6832fdba6b0SHerbert Xu 
6842fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
6852fdba6b0SHerbert Xu 			if (skb->sk) {
6862fdba6b0SHerbert Xu 				frag->sk = skb->sk;
6872fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
6882fdba6b0SHerbert Xu 			}
6893d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 		err = 0;
6931da177e4SLinus Torvalds 		offset = 0;
6941da177e4SLinus Torvalds 		frag = skb_shinfo(skb)->frag_list;
6954d9092bbSDavid S. Miller 		skb_frag_list_init(skb);
6961da177e4SLinus Torvalds 		/* BUILD HEADER */
6971da177e4SLinus Torvalds 
6989a217a1cSYOSHIFUJI Hideaki 		*prevhdr = NEXTHDR_FRAGMENT;
699d56f90a7SArnaldo Carvalho de Melo 		tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
7001da177e4SLinus Torvalds 		if (!tmp_hdr) {
701adf30907SEric Dumazet 			IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
7023bd653c8SDenis V. Lunev 				      IPSTATS_MIB_FRAGFAILS);
7031da177e4SLinus Torvalds 			return -ENOMEM;
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 		__skb_pull(skb, hlen);
7071da177e4SLinus Torvalds 		fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
708e2d1bca7SArnaldo Carvalho de Melo 		__skb_push(skb, hlen);
709e2d1bca7SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
710d56f90a7SArnaldo Carvalho de Melo 		memcpy(skb_network_header(skb), tmp_hdr, hlen);
7111da177e4SLinus Torvalds 
71287c48fa3SEric Dumazet 		ipv6_select_ident(fh, rt);
7131da177e4SLinus Torvalds 		fh->nexthdr = nexthdr;
7141da177e4SLinus Torvalds 		fh->reserved = 0;
7151da177e4SLinus Torvalds 		fh->frag_off = htons(IP6_MF);
7161da177e4SLinus Torvalds 		frag_id = fh->identification;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 		first_len = skb_pagelen(skb);
7191da177e4SLinus Torvalds 		skb->data_len = first_len - skb_headlen(skb);
7201da177e4SLinus Torvalds 		skb->len = first_len;
7210660e03fSArnaldo Carvalho de Melo 		ipv6_hdr(skb)->payload_len = htons(first_len -
7220660e03fSArnaldo Carvalho de Melo 						   sizeof(struct ipv6hdr));
7231da177e4SLinus Torvalds 
724d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		for (;;) {
7271da177e4SLinus Torvalds 			/* Prepare header of the next frame,
7281da177e4SLinus Torvalds 			 * before previous one went down. */
7291da177e4SLinus Torvalds 			if (frag) {
7301da177e4SLinus Torvalds 				frag->ip_summed = CHECKSUM_NONE;
731badff6d0SArnaldo Carvalho de Melo 				skb_reset_transport_header(frag);
7321da177e4SLinus Torvalds 				fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
733e2d1bca7SArnaldo Carvalho de Melo 				__skb_push(frag, hlen);
734e2d1bca7SArnaldo Carvalho de Melo 				skb_reset_network_header(frag);
735d56f90a7SArnaldo Carvalho de Melo 				memcpy(skb_network_header(frag), tmp_hdr,
736d56f90a7SArnaldo Carvalho de Melo 				       hlen);
7371da177e4SLinus Torvalds 				offset += skb->len - hlen - sizeof(struct frag_hdr);
7381da177e4SLinus Torvalds 				fh->nexthdr = nexthdr;
7391da177e4SLinus Torvalds 				fh->reserved = 0;
7401da177e4SLinus Torvalds 				fh->frag_off = htons(offset);
7411da177e4SLinus Torvalds 				if (frag->next != NULL)
7421da177e4SLinus Torvalds 					fh->frag_off |= htons(IP6_MF);
7431da177e4SLinus Torvalds 				fh->identification = frag_id;
7440660e03fSArnaldo Carvalho de Melo 				ipv6_hdr(frag)->payload_len =
7450660e03fSArnaldo Carvalho de Melo 						htons(frag->len -
7460660e03fSArnaldo Carvalho de Melo 						      sizeof(struct ipv6hdr));
7471da177e4SLinus Torvalds 				ip6_copy_metadata(frag, skb);
7481da177e4SLinus Torvalds 			}
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 			err = output(skb);
751dafee490SWei Dong 			if(!err)
752d8d1f30bSChangli Gao 				IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
7533bd653c8SDenis V. Lunev 					      IPSTATS_MIB_FRAGCREATES);
754dafee490SWei Dong 
7551da177e4SLinus Torvalds 			if (err || !frag)
7561da177e4SLinus Torvalds 				break;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 			skb = frag;
7591da177e4SLinus Torvalds 			frag = skb->next;
7601da177e4SLinus Torvalds 			skb->next = NULL;
7611da177e4SLinus Torvalds 		}
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 		kfree(tmp_hdr);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		if (err == 0) {
766d8d1f30bSChangli Gao 			IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
7673bd653c8SDenis V. Lunev 				      IPSTATS_MIB_FRAGOKS);
768d8d1f30bSChangli Gao 			dst_release(&rt->dst);
7691da177e4SLinus Torvalds 			return 0;
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		while (frag) {
7731da177e4SLinus Torvalds 			skb = frag->next;
7741da177e4SLinus Torvalds 			kfree_skb(frag);
7751da177e4SLinus Torvalds 			frag = skb;
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 
778d8d1f30bSChangli Gao 		IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
7793bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGFAILS);
780d8d1f30bSChangli Gao 		dst_release(&rt->dst);
7811da177e4SLinus Torvalds 		return err;
7823d13008eSEric Dumazet 
7833d13008eSEric Dumazet slow_path_clean:
7843d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
7853d13008eSEric Dumazet 			if (frag2 == frag)
7863d13008eSEric Dumazet 				break;
7873d13008eSEric Dumazet 			frag2->sk = NULL;
7883d13008eSEric Dumazet 			frag2->destructor = NULL;
7893d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
7903d13008eSEric Dumazet 		}
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds slow_path:
79472e843bbSEric Dumazet 	if ((skb->ip_summed == CHECKSUM_PARTIAL) &&
79572e843bbSEric Dumazet 	    skb_checksum_help(skb))
79672e843bbSEric Dumazet 		goto fail;
79772e843bbSEric Dumazet 
7981da177e4SLinus Torvalds 	left = skb->len - hlen;		/* Space per frame */
7991da177e4SLinus Torvalds 	ptr = hlen;			/* Where to start from */
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/*
8021da177e4SLinus Torvalds 	 *	Fragment the datagram.
8031da177e4SLinus Torvalds 	 */
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	*prevhdr = NEXTHDR_FRAGMENT;
806a7ae1992SHerbert Xu 	hroom = LL_RESERVED_SPACE(rt->dst.dev);
807a7ae1992SHerbert Xu 	troom = rt->dst.dev->needed_tailroom;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/*
8101da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
8111da177e4SLinus Torvalds 	 */
8121da177e4SLinus Torvalds 	while(left > 0)	{
8131da177e4SLinus Torvalds 		len = left;
8141da177e4SLinus Torvalds 		/* IF: it doesn't fit, use 'mtu' - the data space left */
8151da177e4SLinus Torvalds 		if (len > mtu)
8161da177e4SLinus Torvalds 			len = mtu;
8171da177e4SLinus Torvalds 		/* IF: we are not sending up to and including the packet end
8181da177e4SLinus Torvalds 		   then align the next start on an eight byte boundary */
8191da177e4SLinus Torvalds 		if (len < left)	{
8201da177e4SLinus Torvalds 			len &= ~7;
8211da177e4SLinus Torvalds 		}
8221da177e4SLinus Torvalds 		/*
8231da177e4SLinus Torvalds 		 *	Allocate buffer.
8241da177e4SLinus Torvalds 		 */
8251da177e4SLinus Torvalds 
826a7ae1992SHerbert Xu 		if ((frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
827a7ae1992SHerbert Xu 				      hroom + troom, GFP_ATOMIC)) == NULL) {
82864ce2073SPatrick McHardy 			NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
829adf30907SEric Dumazet 			IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
830a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_FRAGFAILS);
8311da177e4SLinus Torvalds 			err = -ENOMEM;
8321da177e4SLinus Torvalds 			goto fail;
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 		/*
8361da177e4SLinus Torvalds 		 *	Set up data on packet
8371da177e4SLinus Torvalds 		 */
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 		ip6_copy_metadata(frag, skb);
840a7ae1992SHerbert Xu 		skb_reserve(frag, hroom);
8411da177e4SLinus Torvalds 		skb_put(frag, len + hlen + sizeof(struct frag_hdr));
842c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(frag);
843badff6d0SArnaldo Carvalho de Melo 		fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
844b0e380b1SArnaldo Carvalho de Melo 		frag->transport_header = (frag->network_header + hlen +
845b0e380b1SArnaldo Carvalho de Melo 					  sizeof(struct frag_hdr));
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 		/*
8481da177e4SLinus Torvalds 		 *	Charge the memory for the fragment to any owner
8491da177e4SLinus Torvalds 		 *	it might possess
8501da177e4SLinus Torvalds 		 */
8511da177e4SLinus Torvalds 		if (skb->sk)
8521da177e4SLinus Torvalds 			skb_set_owner_w(frag, skb->sk);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		/*
8551da177e4SLinus Torvalds 		 *	Copy the packet header into the new buffer.
8561da177e4SLinus Torvalds 		 */
857d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 		/*
8601da177e4SLinus Torvalds 		 *	Build fragment header.
8611da177e4SLinus Torvalds 		 */
8621da177e4SLinus Torvalds 		fh->nexthdr = nexthdr;
8631da177e4SLinus Torvalds 		fh->reserved = 0;
864f36d6ab1SYan Zheng 		if (!frag_id) {
86587c48fa3SEric Dumazet 			ipv6_select_ident(fh, rt);
8661da177e4SLinus Torvalds 			frag_id = fh->identification;
8671da177e4SLinus Torvalds 		} else
8681da177e4SLinus Torvalds 			fh->identification = frag_id;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 		/*
8711da177e4SLinus Torvalds 		 *	Copy a block of the IP datagram.
8721da177e4SLinus Torvalds 		 */
8738984e41dSWei Yongjun 		if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
8741da177e4SLinus Torvalds 			BUG();
8751da177e4SLinus Torvalds 		left -= len;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 		fh->frag_off = htons(offset);
8781da177e4SLinus Torvalds 		if (left > 0)
8791da177e4SLinus Torvalds 			fh->frag_off |= htons(IP6_MF);
8800660e03fSArnaldo Carvalho de Melo 		ipv6_hdr(frag)->payload_len = htons(frag->len -
8810660e03fSArnaldo Carvalho de Melo 						    sizeof(struct ipv6hdr));
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 		ptr += len;
8841da177e4SLinus Torvalds 		offset += len;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 		/*
8871da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
8881da177e4SLinus Torvalds 		 */
8891da177e4SLinus Torvalds 		err = output(frag);
8901da177e4SLinus Torvalds 		if (err)
8911da177e4SLinus Torvalds 			goto fail;
892dafee490SWei Dong 
893adf30907SEric Dumazet 		IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
8943bd653c8SDenis V. Lunev 			      IPSTATS_MIB_FRAGCREATES);
8951da177e4SLinus Torvalds 	}
896adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
897a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGOKS);
898808db80aSEric Dumazet 	consume_skb(skb);
8991da177e4SLinus Torvalds 	return err;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds fail:
902adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
903a11d206dSYOSHIFUJI Hideaki 		      IPSTATS_MIB_FRAGFAILS);
9041da177e4SLinus Torvalds 	kfree_skb(skb);
9051da177e4SLinus Torvalds 	return err;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
908b71d1d42SEric Dumazet static inline int ip6_rt_check(const struct rt6key *rt_key,
909b71d1d42SEric Dumazet 			       const struct in6_addr *fl_addr,
910b71d1d42SEric Dumazet 			       const struct in6_addr *addr_cache)
911cf6b1982SYOSHIFUJI Hideaki {
912a02cec21SEric Dumazet 	return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
913a02cec21SEric Dumazet 		(addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
914cf6b1982SYOSHIFUJI Hideaki }
915cf6b1982SYOSHIFUJI Hideaki 
916497c615aSHerbert Xu static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
917497c615aSHerbert Xu 					  struct dst_entry *dst,
918b71d1d42SEric Dumazet 					  const struct flowi6 *fl6)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
921497c615aSHerbert Xu 	struct rt6_info *rt = (struct rt6_info *)dst;
9221da177e4SLinus Torvalds 
923497c615aSHerbert Xu 	if (!dst)
924497c615aSHerbert Xu 		goto out;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	/* Yes, checking route validity in not connected
927d76e60a5SDavid S. Miller 	 * case is not very simple. Take into account,
928d76e60a5SDavid S. Miller 	 * that we do not support routing by source, TOS,
929d76e60a5SDavid S. Miller 	 * and MSG_DONTROUTE 		--ANK (980726)
930d76e60a5SDavid S. Miller 	 *
931cf6b1982SYOSHIFUJI Hideaki 	 * 1. ip6_rt_check(): If route was host route,
932cf6b1982SYOSHIFUJI Hideaki 	 *    check that cached destination is current.
933d76e60a5SDavid S. Miller 	 *    If it is network route, we still may
934d76e60a5SDavid S. Miller 	 *    check its validity using saved pointer
935d76e60a5SDavid S. Miller 	 *    to the last used address: daddr_cache.
936d76e60a5SDavid S. Miller 	 *    We do not want to save whole address now,
937d76e60a5SDavid S. Miller 	 *    (because main consumer of this service
938d76e60a5SDavid S. Miller 	 *    is tcp, which has not this problem),
939d76e60a5SDavid S. Miller 	 *    so that the last trick works only on connected
940d76e60a5SDavid S. Miller 	 *    sockets.
941d76e60a5SDavid S. Miller 	 * 2. oif also should be the same.
9421da177e4SLinus Torvalds 	 */
9434c9483b2SDavid S. Miller 	if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
9448e1ef0a9SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_SUBTREES
9454c9483b2SDavid S. Miller 	    ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
9468e1ef0a9SYOSHIFUJI Hideaki #endif
9474c9483b2SDavid S. Miller 	    (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
948497c615aSHerbert Xu 		dst_release(dst);
949497c615aSHerbert Xu 		dst = NULL;
9501da177e4SLinus Torvalds 	}
951497c615aSHerbert Xu 
952497c615aSHerbert Xu out:
953497c615aSHerbert Xu 	return dst;
9541da177e4SLinus Torvalds }
955497c615aSHerbert Xu 
956497c615aSHerbert Xu static int ip6_dst_lookup_tail(struct sock *sk,
9574c9483b2SDavid S. Miller 			       struct dst_entry **dst, struct flowi6 *fl6)
958497c615aSHerbert Xu {
9593b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
96069cce1d1SDavid S. Miller #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
96169cce1d1SDavid S. Miller 	struct neighbour *n;
96269cce1d1SDavid S. Miller #endif
96369cce1d1SDavid S. Miller 	int err;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if (*dst == NULL)
9664c9483b2SDavid S. Miller 		*dst = ip6_route_output(net, sk, fl6);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if ((err = (*dst)->error))
9691da177e4SLinus Torvalds 		goto out_err_release;
9701da177e4SLinus Torvalds 
9714c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6->saddr)) {
972c3968a85SDaniel Walter 		struct rt6_info *rt = (struct rt6_info *) *dst;
973c3968a85SDaniel Walter 		err = ip6_route_get_saddr(net, rt, &fl6->daddr,
9747cbca67cSYOSHIFUJI Hideaki 					  sk ? inet6_sk(sk)->srcprefs : 0,
9754c9483b2SDavid S. Miller 					  &fl6->saddr);
97644456d37SOlaf Hering 		if (err)
9771da177e4SLinus Torvalds 			goto out_err_release;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
98095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
98195c385b4SNeil Horman 	/*
98295c385b4SNeil Horman 	 * Here if the dst entry we've looked up
98395c385b4SNeil Horman 	 * has a neighbour entry that is in the INCOMPLETE
98495c385b4SNeil Horman 	 * state and the src address from the flow is
98595c385b4SNeil Horman 	 * marked as OPTIMISTIC, we release the found
98695c385b4SNeil Horman 	 * dst entry and replace it instead with the
98795c385b4SNeil Horman 	 * dst entry of the nexthop router
98895c385b4SNeil Horman 	 */
989f2c31e32SEric Dumazet 	rcu_read_lock();
99027217455SDavid Miller 	n = dst_get_neighbour_noref(*dst);
99169cce1d1SDavid S. Miller 	if (n && !(n->nud_state & NUD_VALID)) {
99295c385b4SNeil Horman 		struct inet6_ifaddr *ifp;
9934c9483b2SDavid S. Miller 		struct flowi6 fl_gw6;
99495c385b4SNeil Horman 		int redirect;
99595c385b4SNeil Horman 
996f2c31e32SEric Dumazet 		rcu_read_unlock();
9974c9483b2SDavid S. Miller 		ifp = ipv6_get_ifaddr(net, &fl6->saddr,
9981cab3da6SDaniel Lezcano 				      (*dst)->dev, 1);
99995c385b4SNeil Horman 
100095c385b4SNeil Horman 		redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
100195c385b4SNeil Horman 		if (ifp)
100295c385b4SNeil Horman 			in6_ifa_put(ifp);
100395c385b4SNeil Horman 
100495c385b4SNeil Horman 		if (redirect) {
100595c385b4SNeil Horman 			/*
100695c385b4SNeil Horman 			 * We need to get the dst entry for the
100795c385b4SNeil Horman 			 * default router instead
100895c385b4SNeil Horman 			 */
100995c385b4SNeil Horman 			dst_release(*dst);
10104c9483b2SDavid S. Miller 			memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
10114c9483b2SDavid S. Miller 			memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
10124c9483b2SDavid S. Miller 			*dst = ip6_route_output(net, sk, &fl_gw6);
101395c385b4SNeil Horman 			if ((err = (*dst)->error))
101495c385b4SNeil Horman 				goto out_err_release;
101595c385b4SNeil Horman 		}
1016f2c31e32SEric Dumazet 	} else {
1017f2c31e32SEric Dumazet 		rcu_read_unlock();
101895c385b4SNeil Horman 	}
101995c385b4SNeil Horman #endif
102095c385b4SNeil Horman 
10211da177e4SLinus Torvalds 	return 0;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds out_err_release:
1024ca46f9c8SMitsuru Chinen 	if (err == -ENETUNREACH)
1025483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
10261da177e4SLinus Torvalds 	dst_release(*dst);
10271da177e4SLinus Torvalds 	*dst = NULL;
10281da177e4SLinus Torvalds 	return err;
10291da177e4SLinus Torvalds }
103034a0b3cdSAdrian Bunk 
1031497c615aSHerbert Xu /**
1032497c615aSHerbert Xu  *	ip6_dst_lookup - perform route lookup on flow
1033497c615aSHerbert Xu  *	@sk: socket which provides route info
1034497c615aSHerbert Xu  *	@dst: pointer to dst_entry * for result
10354c9483b2SDavid S. Miller  *	@fl6: flow to lookup
1036497c615aSHerbert Xu  *
1037497c615aSHerbert Xu  *	This function performs a route lookup on the given flow.
1038497c615aSHerbert Xu  *
1039497c615aSHerbert Xu  *	It returns zero on success, or a standard errno code on error.
1040497c615aSHerbert Xu  */
10414c9483b2SDavid S. Miller int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
1042497c615aSHerbert Xu {
1043497c615aSHerbert Xu 	*dst = NULL;
10444c9483b2SDavid S. Miller 	return ip6_dst_lookup_tail(sk, dst, fl6);
1045497c615aSHerbert Xu }
10463cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip6_dst_lookup);
10473cf3dc6cSArnaldo Carvalho de Melo 
1048497c615aSHerbert Xu /**
104968d0c6d3SDavid S. Miller  *	ip6_dst_lookup_flow - perform route lookup on flow with ipsec
105068d0c6d3SDavid S. Miller  *	@sk: socket which provides route info
10514c9483b2SDavid S. Miller  *	@fl6: flow to lookup
105268d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
1053a1414715SDavid S. Miller  *	@can_sleep: we are in a sleepable context
105468d0c6d3SDavid S. Miller  *
105568d0c6d3SDavid S. Miller  *	This function performs a route lookup on the given flow.
105668d0c6d3SDavid S. Miller  *
105768d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
105868d0c6d3SDavid S. Miller  *	error code.
105968d0c6d3SDavid S. Miller  */
10604c9483b2SDavid S. Miller struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
106168d0c6d3SDavid S. Miller 				      const struct in6_addr *final_dst,
1062a1414715SDavid S. Miller 				      bool can_sleep)
106368d0c6d3SDavid S. Miller {
106468d0c6d3SDavid S. Miller 	struct dst_entry *dst = NULL;
106568d0c6d3SDavid S. Miller 	int err;
106668d0c6d3SDavid S. Miller 
10674c9483b2SDavid S. Miller 	err = ip6_dst_lookup_tail(sk, &dst, fl6);
106868d0c6d3SDavid S. Miller 	if (err)
106968d0c6d3SDavid S. Miller 		return ERR_PTR(err);
107068d0c6d3SDavid S. Miller 	if (final_dst)
10714e3fd7a0SAlexey Dobriyan 		fl6->daddr = *final_dst;
10722774c131SDavid S. Miller 	if (can_sleep)
10734c9483b2SDavid S. Miller 		fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
10742774c131SDavid S. Miller 
10754c9483b2SDavid S. Miller 	return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
107668d0c6d3SDavid S. Miller }
107768d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
107868d0c6d3SDavid S. Miller 
107968d0c6d3SDavid S. Miller /**
108068d0c6d3SDavid S. Miller  *	ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
108168d0c6d3SDavid S. Miller  *	@sk: socket which provides the dst cache and route info
10824c9483b2SDavid S. Miller  *	@fl6: flow to lookup
108368d0c6d3SDavid S. Miller  *	@final_dst: final destination address for ipsec lookup
1084a1414715SDavid S. Miller  *	@can_sleep: we are in a sleepable context
1085497c615aSHerbert Xu  *
1086497c615aSHerbert Xu  *	This function performs a route lookup on the given flow with the
1087497c615aSHerbert Xu  *	possibility of using the cached route in the socket if it is valid.
1088497c615aSHerbert Xu  *	It will take the socket dst lock when operating on the dst cache.
1089497c615aSHerbert Xu  *	As a result, this function can only be used in process context.
1090497c615aSHerbert Xu  *
109168d0c6d3SDavid S. Miller  *	It returns a valid dst pointer on success, or a pointer encoded
109268d0c6d3SDavid S. Miller  *	error code.
1093497c615aSHerbert Xu  */
10944c9483b2SDavid S. Miller struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
109568d0c6d3SDavid S. Miller 					 const struct in6_addr *final_dst,
1096a1414715SDavid S. Miller 					 bool can_sleep)
1097497c615aSHerbert Xu {
109868d0c6d3SDavid S. Miller 	struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
109968d0c6d3SDavid S. Miller 	int err;
1100497c615aSHerbert Xu 
11014c9483b2SDavid S. Miller 	dst = ip6_sk_dst_check(sk, dst, fl6);
110268d0c6d3SDavid S. Miller 
11034c9483b2SDavid S. Miller 	err = ip6_dst_lookup_tail(sk, &dst, fl6);
110468d0c6d3SDavid S. Miller 	if (err)
110568d0c6d3SDavid S. Miller 		return ERR_PTR(err);
110668d0c6d3SDavid S. Miller 	if (final_dst)
11074e3fd7a0SAlexey Dobriyan 		fl6->daddr = *final_dst;
11082774c131SDavid S. Miller 	if (can_sleep)
11094c9483b2SDavid S. Miller 		fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
11102774c131SDavid S. Miller 
11114c9483b2SDavid S. Miller 	return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
111268d0c6d3SDavid S. Miller }
111368d0c6d3SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
1114497c615aSHerbert Xu 
111534a0b3cdSAdrian Bunk static inline int ip6_ufo_append_data(struct sock *sk,
1116e89e9cf5SAnanda Raju 			int getfrag(void *from, char *to, int offset, int len,
1117e89e9cf5SAnanda Raju 			int odd, struct sk_buff *skb),
1118e89e9cf5SAnanda Raju 			void *from, int length, int hh_len, int fragheaderlen,
111987c48fa3SEric Dumazet 			int transhdrlen, int mtu,unsigned int flags,
112087c48fa3SEric Dumazet 			struct rt6_info *rt)
1121e89e9cf5SAnanda Raju 
1122e89e9cf5SAnanda Raju {
1123e89e9cf5SAnanda Raju 	struct sk_buff *skb;
1124e89e9cf5SAnanda Raju 	int err;
1125e89e9cf5SAnanda Raju 
1126e89e9cf5SAnanda Raju 	/* There is support for UDP large send offload by network
1127e89e9cf5SAnanda Raju 	 * device, so create one single skb packet containing complete
1128e89e9cf5SAnanda Raju 	 * udp datagram
1129e89e9cf5SAnanda Raju 	 */
1130e89e9cf5SAnanda Raju 	if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
1131e89e9cf5SAnanda Raju 		skb = sock_alloc_send_skb(sk,
1132e89e9cf5SAnanda Raju 			hh_len + fragheaderlen + transhdrlen + 20,
1133e89e9cf5SAnanda Raju 			(flags & MSG_DONTWAIT), &err);
1134e89e9cf5SAnanda Raju 		if (skb == NULL)
1135504744e4SZheng Yan 			return err;
1136e89e9cf5SAnanda Raju 
1137e89e9cf5SAnanda Raju 		/* reserve space for Hardware header */
1138e89e9cf5SAnanda Raju 		skb_reserve(skb, hh_len);
1139e89e9cf5SAnanda Raju 
1140e89e9cf5SAnanda Raju 		/* create space for UDP/IP header */
1141e89e9cf5SAnanda Raju 		skb_put(skb,fragheaderlen + transhdrlen);
1142e89e9cf5SAnanda Raju 
1143e89e9cf5SAnanda Raju 		/* initialize network header pointer */
1144c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
1145e89e9cf5SAnanda Raju 
1146e89e9cf5SAnanda Raju 		/* initialize protocol header pointer */
1147b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header = skb->network_header + fragheaderlen;
1148e89e9cf5SAnanda Raju 
114984fa7933SPatrick McHardy 		skb->ip_summed = CHECKSUM_PARTIAL;
1150e89e9cf5SAnanda Raju 		skb->csum = 0;
1151e89e9cf5SAnanda Raju 	}
1152e89e9cf5SAnanda Raju 
1153e89e9cf5SAnanda Raju 	err = skb_append_datato_frags(sk,skb, getfrag, from,
1154e89e9cf5SAnanda Raju 				      (length - transhdrlen));
1155e89e9cf5SAnanda Raju 	if (!err) {
1156e89e9cf5SAnanda Raju 		struct frag_hdr fhdr;
1157e89e9cf5SAnanda Raju 
1158c31d5326SSridhar Samudrala 		/* Specify the length of each IPv6 datagram fragment.
1159c31d5326SSridhar Samudrala 		 * It has to be a multiple of 8.
1160c31d5326SSridhar Samudrala 		 */
1161c31d5326SSridhar Samudrala 		skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1162c31d5326SSridhar Samudrala 					     sizeof(struct frag_hdr)) & ~7;
1163f83ef8c0SHerbert Xu 		skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
116487c48fa3SEric Dumazet 		ipv6_select_ident(&fhdr, rt);
1165e89e9cf5SAnanda Raju 		skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
1166e89e9cf5SAnanda Raju 		__skb_queue_tail(&sk->sk_write_queue, skb);
1167e89e9cf5SAnanda Raju 
1168e89e9cf5SAnanda Raju 		return 0;
1169e89e9cf5SAnanda Raju 	}
1170e89e9cf5SAnanda Raju 	/* There is not enough support do UPD LSO,
1171e89e9cf5SAnanda Raju 	 * so follow normal path
1172e89e9cf5SAnanda Raju 	 */
1173e89e9cf5SAnanda Raju 	kfree_skb(skb);
1174e89e9cf5SAnanda Raju 
1175e89e9cf5SAnanda Raju 	return err;
1176e89e9cf5SAnanda Raju }
11771da177e4SLinus Torvalds 
11780178b695SHerbert Xu static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
11790178b695SHerbert Xu 					       gfp_t gfp)
11800178b695SHerbert Xu {
11810178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
11820178b695SHerbert Xu }
11830178b695SHerbert Xu 
11840178b695SHerbert Xu static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
11850178b695SHerbert Xu 						gfp_t gfp)
11860178b695SHerbert Xu {
11870178b695SHerbert Xu 	return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
11880178b695SHerbert Xu }
11890178b695SHerbert Xu 
119041a1f8eaSYOSHIFUJI Hideaki int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
119141a1f8eaSYOSHIFUJI Hideaki 	int offset, int len, int odd, struct sk_buff *skb),
11921da177e4SLinus Torvalds 	void *from, int length, int transhdrlen,
11934c9483b2SDavid S. Miller 	int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
119413b52cd4SBrian Haley 	struct rt6_info *rt, unsigned int flags, int dontfrag)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11971da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
1198bdc712b4SDavid S. Miller 	struct inet_cork *cork;
11991da177e4SLinus Torvalds 	struct sk_buff *skb;
12001da177e4SLinus Torvalds 	unsigned int maxfraglen, fragheaderlen;
12011da177e4SLinus Torvalds 	int exthdrlen;
1202299b0767SSteffen Klassert 	int dst_exthdrlen;
12031da177e4SLinus Torvalds 	int hh_len;
12041da177e4SLinus Torvalds 	int mtu;
12051da177e4SLinus Torvalds 	int copy;
12061da177e4SLinus Torvalds 	int err;
12071da177e4SLinus Torvalds 	int offset = 0;
1208a693e698SAnders Berggren 	__u8 tx_flags = 0;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
12111da177e4SLinus Torvalds 		return 0;
1212bdc712b4SDavid S. Miller 	cork = &inet->cork.base;
12131da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue)) {
12141da177e4SLinus Torvalds 		/*
12151da177e4SLinus Torvalds 		 * setup for corking
12161da177e4SLinus Torvalds 		 */
12171da177e4SLinus Torvalds 		if (opt) {
12180178b695SHerbert Xu 			if (WARN_ON(np->cork.opt))
12190178b695SHerbert Xu 				return -EINVAL;
12200178b695SHerbert Xu 
12210178b695SHerbert Xu 			np->cork.opt = kmalloc(opt->tot_len, sk->sk_allocation);
12221da177e4SLinus Torvalds 			if (unlikely(np->cork.opt == NULL))
12231da177e4SLinus Torvalds 				return -ENOBUFS;
12240178b695SHerbert Xu 
12250178b695SHerbert Xu 			np->cork.opt->tot_len = opt->tot_len;
12260178b695SHerbert Xu 			np->cork.opt->opt_flen = opt->opt_flen;
12270178b695SHerbert Xu 			np->cork.opt->opt_nflen = opt->opt_nflen;
12280178b695SHerbert Xu 
12290178b695SHerbert Xu 			np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
12300178b695SHerbert Xu 							    sk->sk_allocation);
12310178b695SHerbert Xu 			if (opt->dst0opt && !np->cork.opt->dst0opt)
12320178b695SHerbert Xu 				return -ENOBUFS;
12330178b695SHerbert Xu 
12340178b695SHerbert Xu 			np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
12350178b695SHerbert Xu 							    sk->sk_allocation);
12360178b695SHerbert Xu 			if (opt->dst1opt && !np->cork.opt->dst1opt)
12370178b695SHerbert Xu 				return -ENOBUFS;
12380178b695SHerbert Xu 
12390178b695SHerbert Xu 			np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
12400178b695SHerbert Xu 							   sk->sk_allocation);
12410178b695SHerbert Xu 			if (opt->hopopt && !np->cork.opt->hopopt)
12420178b695SHerbert Xu 				return -ENOBUFS;
12430178b695SHerbert Xu 
12440178b695SHerbert Xu 			np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
12450178b695SHerbert Xu 							    sk->sk_allocation);
12460178b695SHerbert Xu 			if (opt->srcrt && !np->cork.opt->srcrt)
12470178b695SHerbert Xu 				return -ENOBUFS;
12480178b695SHerbert Xu 
12491da177e4SLinus Torvalds 			/* need source address above miyazawa*/
12501da177e4SLinus Torvalds 		}
1251d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1252bdc712b4SDavid S. Miller 		cork->dst = &rt->dst;
12534c9483b2SDavid S. Miller 		inet->cork.fl.u.ip6 = *fl6;
12541da177e4SLinus Torvalds 		np->cork.hop_limit = hlimit;
125541a1f8eaSYOSHIFUJI Hideaki 		np->cork.tclass = tclass;
1256628a5c56SJohn Heffner 		mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
1257299b0767SSteffen Klassert 		      rt->dst.dev->mtu : dst_mtu(&rt->dst);
1258c7503609SDave Jones 		if (np->frag_size < mtu) {
1259d91675f9SYOSHIFUJI Hideaki 			if (np->frag_size)
1260d91675f9SYOSHIFUJI Hideaki 				mtu = np->frag_size;
1261d91675f9SYOSHIFUJI Hideaki 		}
1262bdc712b4SDavid S. Miller 		cork->fragsize = mtu;
1263d8d1f30bSChangli Gao 		if (dst_allfrag(rt->dst.path))
1264bdc712b4SDavid S. Miller 			cork->flags |= IPCORK_ALLFRAG;
1265bdc712b4SDavid S. Miller 		cork->length = 0;
12661da177e4SLinus Torvalds 		sk->sk_sndmsg_page = NULL;
12671da177e4SLinus Torvalds 		sk->sk_sndmsg_off = 0;
1268299b0767SSteffen Klassert 		exthdrlen = (opt ? opt->opt_flen : 0) - rt->rt6i_nfheader_len;
12691da177e4SLinus Torvalds 		length += exthdrlen;
12701da177e4SLinus Torvalds 		transhdrlen += exthdrlen;
1271299b0767SSteffen Klassert 		dst_exthdrlen = rt->dst.header_len;
12721da177e4SLinus Torvalds 	} else {
1273bdc712b4SDavid S. Miller 		rt = (struct rt6_info *)cork->dst;
12744c9483b2SDavid S. Miller 		fl6 = &inet->cork.fl.u.ip6;
12751da177e4SLinus Torvalds 		opt = np->cork.opt;
12761da177e4SLinus Torvalds 		transhdrlen = 0;
12771da177e4SLinus Torvalds 		exthdrlen = 0;
1278299b0767SSteffen Klassert 		dst_exthdrlen = 0;
1279bdc712b4SDavid S. Miller 		mtu = cork->fragsize;
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
1282d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
12831da177e4SLinus Torvalds 
1284a1b05140SMasahide NAKAMURA 	fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
1285b4ce9277SHerbert Xu 			(opt ? opt->opt_nflen : 0);
12861da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
1289bdc712b4SDavid S. Miller 		if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
12904c9483b2SDavid S. Miller 			ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
12911da177e4SLinus Torvalds 			return -EMSGSIZE;
12921da177e4SLinus Torvalds 		}
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds 
1295a693e698SAnders Berggren 	/* For UDP, check if TX timestamp is enabled */
1296a693e698SAnders Berggren 	if (sk->sk_type == SOCK_DGRAM) {
1297a693e698SAnders Berggren 		err = sock_tx_timestamp(sk, &tx_flags);
1298a693e698SAnders Berggren 		if (err)
1299a693e698SAnders Berggren 			goto error;
1300a693e698SAnders Berggren 	}
1301a693e698SAnders Berggren 
13021da177e4SLinus Torvalds 	/*
13031da177e4SLinus Torvalds 	 * Let's try using as much space as possible.
13041da177e4SLinus Torvalds 	 * Use MTU if total length of the message fits into the MTU.
13051da177e4SLinus Torvalds 	 * Otherwise, we need to reserve fragment header and
13061da177e4SLinus Torvalds 	 * fragment alignment (= 8-15 octects, in total).
13071da177e4SLinus Torvalds 	 *
13081da177e4SLinus Torvalds 	 * Note that we may need to "move" the data from the tail of
13091da177e4SLinus Torvalds 	 * of the buffer to the new fragment when we split
13101da177e4SLinus Torvalds 	 * the message.
13111da177e4SLinus Torvalds 	 *
13121da177e4SLinus Torvalds 	 * FIXME: It may be fragmented into multiple chunks
13131da177e4SLinus Torvalds 	 *        at once if non-fragmentable extension headers
13141da177e4SLinus Torvalds 	 *        are too large.
13151da177e4SLinus Torvalds 	 * --yoshfuji
13161da177e4SLinus Torvalds 	 */
13171da177e4SLinus Torvalds 
1318bdc712b4SDavid S. Miller 	cork->length += length;
13194b340ae2SBrian Haley 	if (length > mtu) {
13204b340ae2SBrian Haley 		int proto = sk->sk_protocol;
13214b340ae2SBrian Haley 		if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
13224c9483b2SDavid S. Miller 			ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
13234b340ae2SBrian Haley 			return -EMSGSIZE;
13244b340ae2SBrian Haley 		}
13254b340ae2SBrian Haley 
13264b340ae2SBrian Haley 		if (proto == IPPROTO_UDP &&
1327d8d1f30bSChangli Gao 		    (rt->dst.dev->features & NETIF_F_UFO)) {
1328e89e9cf5SAnanda Raju 
13294b340ae2SBrian Haley 			err = ip6_ufo_append_data(sk, getfrag, from, length,
13304b340ae2SBrian Haley 						  hh_len, fragheaderlen,
133187c48fa3SEric Dumazet 						  transhdrlen, mtu, flags, rt);
1332baa829d8SPatrick McHardy 			if (err)
1333e89e9cf5SAnanda Raju 				goto error;
1334e89e9cf5SAnanda Raju 			return 0;
1335e89e9cf5SAnanda Raju 		}
13364b340ae2SBrian Haley 	}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
13391da177e4SLinus Torvalds 		goto alloc_new_skb;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	while (length > 0) {
13421da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
1343bdc712b4SDavid S. Miller 		copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
13441da177e4SLinus Torvalds 		if (copy < length)
13451da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 		if (copy <= 0) {
13481da177e4SLinus Torvalds 			char *data;
13491da177e4SLinus Torvalds 			unsigned int datalen;
13501da177e4SLinus Torvalds 			unsigned int fraglen;
13511da177e4SLinus Torvalds 			unsigned int fraggap;
13521da177e4SLinus Torvalds 			unsigned int alloclen;
13531da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
13541da177e4SLinus Torvalds alloc_new_skb:
13551da177e4SLinus Torvalds 			skb_prev = skb;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 			/* There's no room in the current skb */
13581da177e4SLinus Torvalds 			if (skb_prev)
13591da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
13601da177e4SLinus Torvalds 			else
13611da177e4SLinus Torvalds 				fraggap = 0;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 			/*
13641da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
13651da177e4SLinus Torvalds 			 * we know we need more fragment(s).
13661da177e4SLinus Torvalds 			 */
13671da177e4SLinus Torvalds 			datalen = length + fraggap;
1368bdc712b4SDavid S. Miller 			if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
13691da177e4SLinus Torvalds 				datalen = maxfraglen - fragheaderlen;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 			fraglen = datalen + fragheaderlen;
13721da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
1373d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
13741da177e4SLinus Torvalds 				alloclen = mtu;
13751da177e4SLinus Torvalds 			else
13761da177e4SLinus Torvalds 				alloclen = datalen + fragheaderlen;
13771da177e4SLinus Torvalds 
1378299b0767SSteffen Klassert 			alloclen += dst_exthdrlen;
1379299b0767SSteffen Klassert 
13801da177e4SLinus Torvalds 			/*
13811da177e4SLinus Torvalds 			 * The last fragment gets additional space at tail.
13821da177e4SLinus Torvalds 			 * Note: we overallocate on fragments with MSG_MODE
13831da177e4SLinus Torvalds 			 * because we have no idea if we're the last one.
13841da177e4SLinus Torvalds 			 */
13851da177e4SLinus Torvalds 			if (datalen == length + fraggap)
1386d8d1f30bSChangli Gao 				alloclen += rt->dst.trailer_len;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 			/*
13891da177e4SLinus Torvalds 			 * We just reserve space for fragment header.
13901da177e4SLinus Torvalds 			 * Note: this may be overallocation if the message
13911da177e4SLinus Torvalds 			 * (without MSG_MORE) fits into the MTU.
13921da177e4SLinus Torvalds 			 */
13931da177e4SLinus Torvalds 			alloclen += sizeof(struct frag_hdr);
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 			if (transhdrlen) {
13961da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
13971da177e4SLinus Torvalds 						alloclen + hh_len,
13981da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
13991da177e4SLinus Torvalds 			} else {
14001da177e4SLinus Torvalds 				skb = NULL;
14011da177e4SLinus Torvalds 				if (atomic_read(&sk->sk_wmem_alloc) <=
14021da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
14031da177e4SLinus Torvalds 					skb = sock_wmalloc(sk,
14041da177e4SLinus Torvalds 							   alloclen + hh_len, 1,
14051da177e4SLinus Torvalds 							   sk->sk_allocation);
14061da177e4SLinus Torvalds 				if (unlikely(skb == NULL))
14071da177e4SLinus Torvalds 					err = -ENOBUFS;
1408a693e698SAnders Berggren 				else {
1409a693e698SAnders Berggren 					/* Only the initial fragment
1410a693e698SAnders Berggren 					 * is time stamped.
1411a693e698SAnders Berggren 					 */
1412a693e698SAnders Berggren 					tx_flags = 0;
1413a693e698SAnders Berggren 				}
14141da177e4SLinus Torvalds 			}
14151da177e4SLinus Torvalds 			if (skb == NULL)
14161da177e4SLinus Torvalds 				goto error;
14171da177e4SLinus Torvalds 			/*
14181da177e4SLinus Torvalds 			 *	Fill in the control structures
14191da177e4SLinus Torvalds 			 */
1420d7f7c0acSEric Dumazet 			skb->ip_summed = CHECKSUM_NONE;
14211da177e4SLinus Torvalds 			skb->csum = 0;
14221f85851eSGao feng 			/* reserve for fragmentation and ipsec header */
14231f85851eSGao feng 			skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
14241f85851eSGao feng 				    dst_exthdrlen);
14251da177e4SLinus Torvalds 
1426a693e698SAnders Berggren 			if (sk->sk_type == SOCK_DGRAM)
1427a693e698SAnders Berggren 				skb_shinfo(skb)->tx_flags = tx_flags;
1428a693e698SAnders Berggren 
14291da177e4SLinus Torvalds 			/*
14301da177e4SLinus Torvalds 			 *	Find where to start putting bytes
14311da177e4SLinus Torvalds 			 */
14321f85851eSGao feng 			data = skb_put(skb, fraglen);
14331f85851eSGao feng 			skb_set_network_header(skb, exthdrlen);
14341f85851eSGao feng 			data += fragheaderlen;
1435b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1436b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
14371da177e4SLinus Torvalds 			if (fraggap) {
14381da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
14391da177e4SLinus Torvalds 					skb_prev, maxfraglen,
14401da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
14411da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
14421da177e4SLinus Torvalds 							  skb->csum);
14431da177e4SLinus Torvalds 				data += fraggap;
1444e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
14451da177e4SLinus Torvalds 			}
14461da177e4SLinus Torvalds 			copy = datalen - transhdrlen - fraggap;
1447299b0767SSteffen Klassert 
14481da177e4SLinus Torvalds 			if (copy < 0) {
14491da177e4SLinus Torvalds 				err = -EINVAL;
14501da177e4SLinus Torvalds 				kfree_skb(skb);
14511da177e4SLinus Torvalds 				goto error;
14521da177e4SLinus Torvalds 			} else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
14531da177e4SLinus Torvalds 				err = -EFAULT;
14541da177e4SLinus Torvalds 				kfree_skb(skb);
14551da177e4SLinus Torvalds 				goto error;
14561da177e4SLinus Torvalds 			}
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 			offset += copy;
14591da177e4SLinus Torvalds 			length -= datalen - fraggap;
14601da177e4SLinus Torvalds 			transhdrlen = 0;
14611da177e4SLinus Torvalds 			exthdrlen = 0;
1462299b0767SSteffen Klassert 			dst_exthdrlen = 0;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 			/*
14651da177e4SLinus Torvalds 			 * Put the packet on the pending queue
14661da177e4SLinus Torvalds 			 */
14671da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
14681da177e4SLinus Torvalds 			continue;
14691da177e4SLinus Torvalds 		}
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 		if (copy > length)
14721da177e4SLinus Torvalds 			copy = length;
14731da177e4SLinus Torvalds 
1474d8d1f30bSChangli Gao 		if (!(rt->dst.dev->features&NETIF_F_SG)) {
14751da177e4SLinus Torvalds 			unsigned int off;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 			off = skb->len;
14781da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
14791da177e4SLinus Torvalds 						offset, copy, off, skb) < 0) {
14801da177e4SLinus Torvalds 				__skb_trim(skb, off);
14811da177e4SLinus Torvalds 				err = -EFAULT;
14821da177e4SLinus Torvalds 				goto error;
14831da177e4SLinus Torvalds 			}
14841da177e4SLinus Torvalds 		} else {
14851da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
14861da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
14871da177e4SLinus Torvalds 			struct page *page = sk->sk_sndmsg_page;
14881da177e4SLinus Torvalds 			int off = sk->sk_sndmsg_off;
14891da177e4SLinus Torvalds 			unsigned int left;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 			if (page && (left = PAGE_SIZE - off) > 0) {
14921da177e4SLinus Torvalds 				if (copy >= left)
14931da177e4SLinus Torvalds 					copy = left;
1494408dadf0SIan Campbell 				if (page != skb_frag_page(frag)) {
14951da177e4SLinus Torvalds 					if (i == MAX_SKB_FRAGS) {
14961da177e4SLinus Torvalds 						err = -EMSGSIZE;
14971da177e4SLinus Torvalds 						goto error;
14981da177e4SLinus Torvalds 					}
14991da177e4SLinus Torvalds 					skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
1500408dadf0SIan Campbell 					skb_frag_ref(skb, i);
15011da177e4SLinus Torvalds 					frag = &skb_shinfo(skb)->frags[i];
15021da177e4SLinus Torvalds 				}
15031da177e4SLinus Torvalds 			} else if(i < MAX_SKB_FRAGS) {
15041da177e4SLinus Torvalds 				if (copy > PAGE_SIZE)
15051da177e4SLinus Torvalds 					copy = PAGE_SIZE;
15061da177e4SLinus Torvalds 				page = alloc_pages(sk->sk_allocation, 0);
15071da177e4SLinus Torvalds 				if (page == NULL) {
15081da177e4SLinus Torvalds 					err = -ENOMEM;
15091da177e4SLinus Torvalds 					goto error;
15101da177e4SLinus Torvalds 				}
15111da177e4SLinus Torvalds 				sk->sk_sndmsg_page = page;
15121da177e4SLinus Torvalds 				sk->sk_sndmsg_off = 0;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 				skb_fill_page_desc(skb, i, page, 0, 0);
15151da177e4SLinus Torvalds 				frag = &skb_shinfo(skb)->frags[i];
15161da177e4SLinus Torvalds 			} else {
15171da177e4SLinus Torvalds 				err = -EMSGSIZE;
15181da177e4SLinus Torvalds 				goto error;
15191da177e4SLinus Torvalds 			}
15209e903e08SEric Dumazet 			if (getfrag(from,
15219e903e08SEric Dumazet 				    skb_frag_address(frag) + skb_frag_size(frag),
1522408dadf0SIan Campbell 				    offset, copy, skb->len, skb) < 0) {
15231da177e4SLinus Torvalds 				err = -EFAULT;
15241da177e4SLinus Torvalds 				goto error;
15251da177e4SLinus Torvalds 			}
15261da177e4SLinus Torvalds 			sk->sk_sndmsg_off += copy;
15279e903e08SEric Dumazet 			skb_frag_size_add(frag, copy);
15281da177e4SLinus Torvalds 			skb->len += copy;
15291da177e4SLinus Torvalds 			skb->data_len += copy;
1530f945fa7aSHerbert Xu 			skb->truesize += copy;
1531f945fa7aSHerbert Xu 			atomic_add(copy, &sk->sk_wmem_alloc);
15321da177e4SLinus Torvalds 		}
15331da177e4SLinus Torvalds 		offset += copy;
15341da177e4SLinus Torvalds 		length -= copy;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 	return 0;
15371da177e4SLinus Torvalds error:
1538bdc712b4SDavid S. Miller 	cork->length -= length;
15393bd653c8SDenis V. Lunev 	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
15401da177e4SLinus Torvalds 	return err;
15411da177e4SLinus Torvalds }
1542a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_append_data);
15431da177e4SLinus Torvalds 
1544bf138862SPavel Emelyanov static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
1545bf138862SPavel Emelyanov {
15460178b695SHerbert Xu 	if (np->cork.opt) {
15470178b695SHerbert Xu 		kfree(np->cork.opt->dst0opt);
15480178b695SHerbert Xu 		kfree(np->cork.opt->dst1opt);
15490178b695SHerbert Xu 		kfree(np->cork.opt->hopopt);
15500178b695SHerbert Xu 		kfree(np->cork.opt->srcrt);
1551bf138862SPavel Emelyanov 		kfree(np->cork.opt);
1552bf138862SPavel Emelyanov 		np->cork.opt = NULL;
15530178b695SHerbert Xu 	}
15540178b695SHerbert Xu 
1555bdc712b4SDavid S. Miller 	if (inet->cork.base.dst) {
1556bdc712b4SDavid S. Miller 		dst_release(inet->cork.base.dst);
1557bdc712b4SDavid S. Miller 		inet->cork.base.dst = NULL;
1558bdc712b4SDavid S. Miller 		inet->cork.base.flags &= ~IPCORK_ALLFRAG;
1559bf138862SPavel Emelyanov 	}
1560bf138862SPavel Emelyanov 	memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1561bf138862SPavel Emelyanov }
1562bf138862SPavel Emelyanov 
15631da177e4SLinus Torvalds int ip6_push_pending_frames(struct sock *sk)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
15661da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
15671da177e4SLinus Torvalds 	struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
15681da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
15691da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
15703bd653c8SDenis V. Lunev 	struct net *net = sock_net(sk);
15711da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
15721da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = np->cork.opt;
1573bdc712b4SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
15744c9483b2SDavid S. Miller 	struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
15754c9483b2SDavid S. Miller 	unsigned char proto = fl6->flowi6_proto;
15761da177e4SLinus Torvalds 	int err = 0;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
15791da177e4SLinus Torvalds 		goto out;
15801da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1583d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1584bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
15851da177e4SLinus Torvalds 	while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
1586cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
15871da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
15881da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
15891da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
15901da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
15911da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
15921da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
15931da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
15941da177e4SLinus Torvalds 	}
15951da177e4SLinus Torvalds 
159628a89453SHerbert Xu 	/* Allow local fragmentation. */
1597b5c15fc0SHerbert Xu 	if (np->pmtudisc < IPV6_PMTUDISC_DO)
159828a89453SHerbert Xu 		skb->local_df = 1;
159928a89453SHerbert Xu 
16004e3fd7a0SAlexey Dobriyan 	*final_dst = fl6->daddr;
1601cfe1fc77SArnaldo Carvalho de Melo 	__skb_pull(skb, skb_network_header_len(skb));
16021da177e4SLinus Torvalds 	if (opt && opt->opt_flen)
16031da177e4SLinus Torvalds 		ipv6_push_frag_opts(skb, opt, &proto);
16041da177e4SLinus Torvalds 	if (opt && opt->opt_nflen)
16051da177e4SLinus Torvalds 		ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
16061da177e4SLinus Torvalds 
1607e2d1bca7SArnaldo Carvalho de Melo 	skb_push(skb, sizeof(struct ipv6hdr));
1608e2d1bca7SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
16090660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
16101da177e4SLinus Torvalds 
16114c9483b2SDavid S. Miller 	*(__be32*)hdr = fl6->flowlabel |
161241a1f8eaSYOSHIFUJI Hideaki 		     htonl(0x60000000 | ((int)np->cork.tclass << 20));
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	hdr->hop_limit = np->cork.hop_limit;
16151da177e4SLinus Torvalds 	hdr->nexthdr = proto;
16164e3fd7a0SAlexey Dobriyan 	hdr->saddr = fl6->saddr;
16174e3fd7a0SAlexey Dobriyan 	hdr->daddr = *final_dst;
16181da177e4SLinus Torvalds 
1619a2c2064fSPatrick McHardy 	skb->priority = sk->sk_priority;
16204a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1621a2c2064fSPatrick McHardy 
1622d8d1f30bSChangli Gao 	skb_dst_set(skb, dst_clone(&rt->dst));
1623edf391ffSNeil Horman 	IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
162414878f75SDavid L Stevens 	if (proto == IPPROTO_ICMPV6) {
1625adf30907SEric Dumazet 		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
162614878f75SDavid L Stevens 
16275a57d4c7SDenis V. Lunev 		ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
1628e41b5368SDenis V. Lunev 		ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
162914878f75SDavid L Stevens 	}
163014878f75SDavid L Stevens 
1631ef76bc23SHerbert Xu 	err = ip6_local_out(skb);
16321da177e4SLinus Torvalds 	if (err) {
16331da177e4SLinus Torvalds 		if (err > 0)
16346ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
16351da177e4SLinus Torvalds 		if (err)
16361da177e4SLinus Torvalds 			goto error;
16371da177e4SLinus Torvalds 	}
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds out:
1640bf138862SPavel Emelyanov 	ip6_cork_release(inet, np);
16411da177e4SLinus Torvalds 	return err;
16421da177e4SLinus Torvalds error:
164306254914SEric Dumazet 	IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
16441da177e4SLinus Torvalds 	goto out;
16451da177e4SLinus Torvalds }
1646a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds void ip6_flush_pending_frames(struct sock *sk)
16491da177e4SLinus Torvalds {
16501da177e4SLinus Torvalds 	struct sk_buff *skb;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
1653adf30907SEric Dumazet 		if (skb_dst(skb))
1654adf30907SEric Dumazet 			IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
1655a11d206dSYOSHIFUJI Hideaki 				      IPSTATS_MIB_OUTDISCARDS);
16561da177e4SLinus Torvalds 		kfree_skb(skb);
16571da177e4SLinus Torvalds 	}
16581da177e4SLinus Torvalds 
1659bf138862SPavel Emelyanov 	ip6_cork_release(inet_sk(sk), inet6_sk(sk));
16601da177e4SLinus Torvalds }
1661a495f836SChris Elston EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
1662