xref: /openbmc/linux/net/ipv4/ip_output.c (revision 49d16b23cd1e61c028ee088c5a64e9ac6a9c6147)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		The Internet Protocol (IP) output module.
71da177e4SLinus Torvalds  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *		Donald Becker, <becker@super.org>
111da177e4SLinus Torvalds  *		Alan Cox, <Alan.Cox@linux.org>
121da177e4SLinus Torvalds  *		Richard Underwood
131da177e4SLinus Torvalds  *		Stefan Becker, <stefanb@yello.ping.de>
141da177e4SLinus Torvalds  *		Jorge Cwik, <jorge@laser.satlink.net>
151da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
161da177e4SLinus Torvalds  *		Hirokazu Takahashi, <taka@valinux.co.jp>
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	See ip_input.c for original log
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	Fixes:
211da177e4SLinus Torvalds  *		Alan Cox	:	Missing nonblock feature in ip_build_xmit.
221da177e4SLinus Torvalds  *		Mike Kilburn	:	htons() missing in ip_build_xmit.
231da177e4SLinus Torvalds  *		Bradford Johnson:	Fix faulty handling of some frames when
241da177e4SLinus Torvalds  *					no route is found.
251da177e4SLinus Torvalds  *		Alexander Demenshin:	Missing sk/skb free in ip_queue_xmit
261da177e4SLinus Torvalds  *					(in case if packet not accepted by
271da177e4SLinus Torvalds  *					output firewall rules)
281da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
291da177e4SLinus Torvalds  *		Alexey Kuznetsov:	use new route cache
301da177e4SLinus Torvalds  *		Andi Kleen:		Fix broken PMTU recovery and remove
311da177e4SLinus Torvalds  *					some redundant tests.
321da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
331da177e4SLinus Torvalds  *		Andi Kleen	: 	Replace ip_reply with ip_send_reply.
341da177e4SLinus Torvalds  *		Andi Kleen	:	Split fast and slow ip_build_xmit path
351da177e4SLinus Torvalds  *					for decreased register pressure on x86
361da177e4SLinus Torvalds  *					and more readibility.
371da177e4SLinus Torvalds  *		Marc Boucher	:	When call_out_firewall returns FW_QUEUE,
381da177e4SLinus Torvalds  *					silently drop skb instead of failing with -EPERM.
391da177e4SLinus Torvalds  *		Detlev Wengorz	:	Copy protocol for fragments.
401da177e4SLinus Torvalds  *		Hirokazu Takahashi:	HW checksumming for outgoing UDP
411da177e4SLinus Torvalds  *					datagrams.
421da177e4SLinus Torvalds  *		Hirokazu Takahashi:	sendfile() on UDP works now.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <asm/uaccess.h>
461da177e4SLinus Torvalds #include <linux/module.h>
471da177e4SLinus Torvalds #include <linux/types.h>
481da177e4SLinus Torvalds #include <linux/kernel.h>
491da177e4SLinus Torvalds #include <linux/mm.h>
501da177e4SLinus Torvalds #include <linux/string.h>
511da177e4SLinus Torvalds #include <linux/errno.h>
52a1f8e7f7SAl Viro #include <linux/highmem.h>
535a0e3ad6STejun Heo #include <linux/slab.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #include <linux/socket.h>
561da177e4SLinus Torvalds #include <linux/sockios.h>
571da177e4SLinus Torvalds #include <linux/in.h>
581da177e4SLinus Torvalds #include <linux/inet.h>
591da177e4SLinus Torvalds #include <linux/netdevice.h>
601da177e4SLinus Torvalds #include <linux/etherdevice.h>
611da177e4SLinus Torvalds #include <linux/proc_fs.h>
621da177e4SLinus Torvalds #include <linux/stat.h>
631da177e4SLinus Torvalds #include <linux/init.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include <net/snmp.h>
661da177e4SLinus Torvalds #include <net/ip.h>
671da177e4SLinus Torvalds #include <net/protocol.h>
681da177e4SLinus Torvalds #include <net/route.h>
69cfacb057SPatrick McHardy #include <net/xfrm.h>
701da177e4SLinus Torvalds #include <linux/skbuff.h>
711da177e4SLinus Torvalds #include <net/sock.h>
721da177e4SLinus Torvalds #include <net/arp.h>
731da177e4SLinus Torvalds #include <net/icmp.h>
741da177e4SLinus Torvalds #include <net/checksum.h>
751da177e4SLinus Torvalds #include <net/inetpeer.h>
761da177e4SLinus Torvalds #include <linux/igmp.h>
771da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
781da177e4SLinus Torvalds #include <linux/netfilter_bridge.h>
791da177e4SLinus Torvalds #include <linux/mroute.h>
801da177e4SLinus Torvalds #include <linux/netlink.h>
816cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h>
821da177e4SLinus Torvalds 
83ab32ea5dSBrian Haley int sysctl_ip_default_ttl __read_mostly = IPDEFTTL;
84323e126fSDavid S. Miller EXPORT_SYMBOL(sysctl_ip_default_ttl);
851da177e4SLinus Torvalds 
86*49d16b23SAndy Zhou static int ip_fragment(struct sock *sk, struct sk_buff *skb,
87*49d16b23SAndy Zhou 		       int (*output)(struct sock *, struct sk_buff *));
88*49d16b23SAndy Zhou 
891da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */
902fbd9679SDenis Efremov void ip_send_check(struct iphdr *iph)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	iph->check = 0;
931da177e4SLinus Torvalds 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
941da177e4SLinus Torvalds }
954bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check);
961da177e4SLinus Torvalds 
977d771aaaSEric Dumazet static int __ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
98c439cb2eSHerbert Xu {
99c439cb2eSHerbert Xu 	struct iphdr *iph = ip_hdr(skb);
100c439cb2eSHerbert Xu 
101c439cb2eSHerbert Xu 	iph->tot_len = htons(skb->len);
102c439cb2eSHerbert Xu 	ip_send_check(iph);
1037026b1ddSDavid Miller 	return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, sk, skb, NULL,
1047026b1ddSDavid Miller 		       skb_dst(skb)->dev, dst_output_sk);
1057026b1ddSDavid Miller }
1067026b1ddSDavid Miller 
1077026b1ddSDavid Miller int __ip_local_out(struct sk_buff *skb)
1087026b1ddSDavid Miller {
1097026b1ddSDavid Miller 	return __ip_local_out_sk(skb->sk, skb);
110c439cb2eSHerbert Xu }
111c439cb2eSHerbert Xu 
112aad88724SEric Dumazet int ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
113c439cb2eSHerbert Xu {
114c439cb2eSHerbert Xu 	int err;
115c439cb2eSHerbert Xu 
116c439cb2eSHerbert Xu 	err = __ip_local_out(skb);
117c439cb2eSHerbert Xu 	if (likely(err == 1))
118aad88724SEric Dumazet 		err = dst_output_sk(sk, skb);
119c439cb2eSHerbert Xu 
120c439cb2eSHerbert Xu 	return err;
121c439cb2eSHerbert Xu }
122aad88724SEric Dumazet EXPORT_SYMBOL_GPL(ip_local_out_sk);
123c439cb2eSHerbert Xu 
1241da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	int ttl = inet->uc_ttl;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	if (ttl < 0)
129323e126fSDavid S. Miller 		ttl = ip4_dst_hoplimit(dst);
1301da177e4SLinus Torvalds 	return ttl;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
1341da177e4SLinus Torvalds  *		Add an ip header to a skbuff and send it out.
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
138f6d8bd05SEric Dumazet 			  __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
141511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
1421da177e4SLinus Torvalds 	struct iphdr *iph;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/* Build the IP header. */
145f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
1468856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
147eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
1481da177e4SLinus Torvalds 	iph->version  = 4;
1491da177e4SLinus Torvalds 	iph->ihl      = 5;
1501da177e4SLinus Torvalds 	iph->tos      = inet->tos;
151d8d1f30bSChangli Gao 	if (ip_dont_fragment(sk, &rt->dst))
1521da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
1531da177e4SLinus Torvalds 	else
1541da177e4SLinus Torvalds 		iph->frag_off = 0;
155d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
156dd927a26SDavid S. Miller 	iph->daddr    = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
157dd927a26SDavid S. Miller 	iph->saddr    = saddr;
1581da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
159b6a7719aSHannes Frederic Sowa 	ip_select_ident(sock_net(sk), skb, sk);
1601da177e4SLinus Torvalds 
161f6d8bd05SEric Dumazet 	if (opt && opt->opt.optlen) {
162f6d8bd05SEric Dumazet 		iph->ihl += opt->opt.optlen>>2;
163f6d8bd05SEric Dumazet 		ip_options_build(skb, &opt->opt, daddr, rt, 0);
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
1674a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Send it out. */
170c439cb2eSHerbert Xu 	return ip_local_out(skb);
1711da177e4SLinus Torvalds }
172d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
173d8c97a94SArnaldo Carvalho de Melo 
1747026b1ddSDavid Miller static inline int ip_finish_output2(struct sock *sk, struct sk_buff *skb)
1751da177e4SLinus Torvalds {
176adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
17780787ebcSMitsuru Chinen 	struct rtable *rt = (struct rtable *)dst;
1781da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
179c2636b4dSChuck Lever 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
180f6b72b62SDavid S. Miller 	struct neighbour *neigh;
181a263b309SDavid S. Miller 	u32 nexthop;
1821da177e4SLinus Torvalds 
183edf391ffSNeil Horman 	if (rt->rt_type == RTN_MULTICAST) {
184edf391ffSNeil Horman 		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len);
185edf391ffSNeil Horman 	} else if (rt->rt_type == RTN_BROADCAST)
186edf391ffSNeil Horman 		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len);
18780787ebcSMitsuru Chinen 
1881da177e4SLinus Torvalds 	/* Be paranoid, rather than too clever. */
1893b04dddeSStephen Hemminger 	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
1901da177e4SLinus Torvalds 		struct sk_buff *skb2;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
19351456b29SIan Morris 		if (!skb2) {
1941da177e4SLinus Torvalds 			kfree_skb(skb);
1951da177e4SLinus Torvalds 			return -ENOMEM;
1961da177e4SLinus Torvalds 		}
1971da177e4SLinus Torvalds 		if (skb->sk)
1981da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
1995d0ba55bSEric Dumazet 		consume_skb(skb);
2001da177e4SLinus Torvalds 		skb = skb2;
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 
203a263b309SDavid S. Miller 	rcu_read_lock_bh();
204155e8336SJulian Anastasov 	nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
205a263b309SDavid S. Miller 	neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
206a263b309SDavid S. Miller 	if (unlikely(!neigh))
207a263b309SDavid S. Miller 		neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
2089871f1adSVasiliy Kulikov 	if (!IS_ERR(neigh)) {
2095110effeSDavid S. Miller 		int res = dst_neigh_output(dst, neigh, skb);
210f2c31e32SEric Dumazet 
211a263b309SDavid S. Miller 		rcu_read_unlock_bh();
212f2c31e32SEric Dumazet 		return res;
213f2c31e32SEric Dumazet 	}
214a263b309SDavid S. Miller 	rcu_read_unlock_bh();
21505e3aa09SDavid S. Miller 
216e87cc472SJoe Perches 	net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
217e87cc472SJoe Perches 			    __func__);
2181da177e4SLinus Torvalds 	kfree_skb(skb);
2191da177e4SLinus Torvalds 	return -EINVAL;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2227026b1ddSDavid Miller static int ip_finish_output_gso(struct sock *sk, struct sk_buff *skb)
223c7ba65d7SFlorian Westphal {
224c7ba65d7SFlorian Westphal 	netdev_features_t features;
225c7ba65d7SFlorian Westphal 	struct sk_buff *segs;
226c7ba65d7SFlorian Westphal 	int ret = 0;
227c7ba65d7SFlorian Westphal 
228c7ba65d7SFlorian Westphal 	/* common case: locally created skb or seglen is <= mtu */
229c7ba65d7SFlorian Westphal 	if (((IPCB(skb)->flags & IPSKB_FORWARDED) == 0) ||
230c7ba65d7SFlorian Westphal 	      skb_gso_network_seglen(skb) <= ip_skb_dst_mtu(skb))
2317026b1ddSDavid Miller 		return ip_finish_output2(sk, skb);
232c7ba65d7SFlorian Westphal 
233c7ba65d7SFlorian Westphal 	/* Slowpath -  GSO segment length is exceeding the dst MTU.
234c7ba65d7SFlorian Westphal 	 *
235c7ba65d7SFlorian Westphal 	 * This can happen in two cases:
236c7ba65d7SFlorian Westphal 	 * 1) TCP GRO packet, DF bit not set
237c7ba65d7SFlorian Westphal 	 * 2) skb arrived via virtio-net, we thus get TSO/GSO skbs directly
238c7ba65d7SFlorian Westphal 	 * from host network stack.
239c7ba65d7SFlorian Westphal 	 */
240c7ba65d7SFlorian Westphal 	features = netif_skb_features(skb);
241c7ba65d7SFlorian Westphal 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
242330966e5SFlorian Westphal 	if (IS_ERR_OR_NULL(segs)) {
243c7ba65d7SFlorian Westphal 		kfree_skb(skb);
244c7ba65d7SFlorian Westphal 		return -ENOMEM;
245c7ba65d7SFlorian Westphal 	}
246c7ba65d7SFlorian Westphal 
247c7ba65d7SFlorian Westphal 	consume_skb(skb);
248c7ba65d7SFlorian Westphal 
249c7ba65d7SFlorian Westphal 	do {
250c7ba65d7SFlorian Westphal 		struct sk_buff *nskb = segs->next;
251c7ba65d7SFlorian Westphal 		int err;
252c7ba65d7SFlorian Westphal 
253c7ba65d7SFlorian Westphal 		segs->next = NULL;
2547026b1ddSDavid Miller 		err = ip_fragment(sk, segs, ip_finish_output2);
255c7ba65d7SFlorian Westphal 
256c7ba65d7SFlorian Westphal 		if (err && ret == 0)
257c7ba65d7SFlorian Westphal 			ret = err;
258c7ba65d7SFlorian Westphal 		segs = nskb;
259c7ba65d7SFlorian Westphal 	} while (segs);
260c7ba65d7SFlorian Westphal 
261c7ba65d7SFlorian Westphal 	return ret;
262c7ba65d7SFlorian Westphal }
263c7ba65d7SFlorian Westphal 
2647026b1ddSDavid Miller static int ip_finish_output(struct sock *sk, struct sk_buff *skb)
2651da177e4SLinus Torvalds {
2665c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
2675c901daaSPatrick McHardy 	/* Policy lookup after SNAT yielded a new policy */
26800db4124SIan Morris 	if (skb_dst(skb)->xfrm) {
26948d5cad8SPatrick McHardy 		IPCB(skb)->flags |= IPSKB_REROUTED;
2707026b1ddSDavid Miller 		return dst_output_sk(sk, skb);
27148d5cad8SPatrick McHardy 	}
2725c901daaSPatrick McHardy #endif
273c7ba65d7SFlorian Westphal 	if (skb_is_gso(skb))
2747026b1ddSDavid Miller 		return ip_finish_output_gso(sk, skb);
275c7ba65d7SFlorian Westphal 
276c7ba65d7SFlorian Westphal 	if (skb->len > ip_skb_dst_mtu(skb))
2777026b1ddSDavid Miller 		return ip_fragment(sk, skb, ip_finish_output2);
278c7ba65d7SFlorian Westphal 
2797026b1ddSDavid Miller 	return ip_finish_output2(sk, skb);
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
282aad88724SEric Dumazet int ip_mc_output(struct sock *sk, struct sk_buff *skb)
2831da177e4SLinus Torvalds {
284511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
285d8d1f30bSChangli Gao 	struct net_device *dev = rt->dst.dev;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	/*
2881da177e4SLinus Torvalds 	 *	If the indicated interface is up and running, send the packet.
2891da177e4SLinus Torvalds 	 */
290edf391ffSNeil Horman 	IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	skb->dev = dev;
2931da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/*
2961da177e4SLinus Torvalds 	 *	Multicasts are looped back for other local users
2971da177e4SLinus Torvalds 	 */
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_MULTICAST) {
3007ad6848cSOctavian Purdila 		if (sk_mc_loop(sk)
3011da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
3021da177e4SLinus Torvalds 		/* Small optimization: do not loopback not local frames,
3031da177e4SLinus Torvalds 		   which returned after forwarding; they will be  dropped
3041da177e4SLinus Torvalds 		   by ip_mr_input in any case.
3051da177e4SLinus Torvalds 		   Note, that local frames are looped back to be delivered
3061da177e4SLinus Torvalds 		   to local recipients.
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 		   This check is duplicated in ip_mr_input at the moment.
3091da177e4SLinus Torvalds 		 */
3109d4fb27dSJoe Perches 		    &&
3119d4fb27dSJoe Perches 		    ((rt->rt_flags & RTCF_LOCAL) ||
3129d4fb27dSJoe Perches 		     !(IPCB(skb)->flags & IPSKB_FORWARDED))
3131da177e4SLinus Torvalds #endif
3141da177e4SLinus Torvalds 		   ) {
3151da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
3161da177e4SLinus Torvalds 			if (newskb)
3179bbc768aSJan Engelhardt 				NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
3187026b1ddSDavid Miller 					sk, newskb, NULL, newskb->dev,
31995603e22SMichel Machado 					dev_loopback_xmit);
3201da177e4SLinus Torvalds 		}
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 		/* Multicasts with ttl 0 must not go beyond the host */
3231da177e4SLinus Torvalds 
324eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->ttl == 0) {
3251da177e4SLinus Torvalds 			kfree_skb(skb);
3261da177e4SLinus Torvalds 			return 0;
3271da177e4SLinus Torvalds 		}
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_BROADCAST) {
3311da177e4SLinus Torvalds 		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
3321da177e4SLinus Torvalds 		if (newskb)
3337026b1ddSDavid Miller 			NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, newskb,
33495603e22SMichel Machado 				NULL, newskb->dev, dev_loopback_xmit);
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
3377026b1ddSDavid Miller 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb, NULL,
3389bbc768aSJan Engelhardt 			    skb->dev, ip_finish_output,
33948d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
342aad88724SEric Dumazet int ip_output(struct sock *sk, struct sk_buff *skb)
3431da177e4SLinus Torvalds {
344adf30907SEric Dumazet 	struct net_device *dev = skb_dst(skb)->dev;
3451bd9bef6SPatrick McHardy 
346edf391ffSNeil Horman 	IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
3471da177e4SLinus Torvalds 
3481bd9bef6SPatrick McHardy 	skb->dev = dev;
3491bd9bef6SPatrick McHardy 	skb->protocol = htons(ETH_P_IP);
3501bd9bef6SPatrick McHardy 
3517026b1ddSDavid Miller 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb,
3527026b1ddSDavid Miller 			    NULL, dev,
35348d5cad8SPatrick McHardy 			    ip_finish_output,
35448d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
35784f9307cSEric Dumazet /*
35884f9307cSEric Dumazet  * copy saddr and daddr, possibly using 64bit load/stores
35984f9307cSEric Dumazet  * Equivalent to :
36084f9307cSEric Dumazet  *   iph->saddr = fl4->saddr;
36184f9307cSEric Dumazet  *   iph->daddr = fl4->daddr;
36284f9307cSEric Dumazet  */
36384f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
36484f9307cSEric Dumazet {
36584f9307cSEric Dumazet 	BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
36684f9307cSEric Dumazet 		     offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
36784f9307cSEric Dumazet 	memcpy(&iph->saddr, &fl4->saddr,
36884f9307cSEric Dumazet 	       sizeof(fl4->saddr) + sizeof(fl4->daddr));
36984f9307cSEric Dumazet }
37084f9307cSEric Dumazet 
371b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */
372b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
375f6d8bd05SEric Dumazet 	struct ip_options_rcu *inet_opt;
376b57ae01aSDavid S. Miller 	struct flowi4 *fl4;
3771da177e4SLinus Torvalds 	struct rtable *rt;
3781da177e4SLinus Torvalds 	struct iphdr *iph;
379ab6e3febSEric Dumazet 	int res;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	/* Skip all of this if the packet is already routed,
3821da177e4SLinus Torvalds 	 * f.e. by something like SCTP.
3831da177e4SLinus Torvalds 	 */
384ab6e3febSEric Dumazet 	rcu_read_lock();
385f6d8bd05SEric Dumazet 	inet_opt = rcu_dereference(inet->inet_opt);
386ea4fc0d6SDavid S. Miller 	fl4 = &fl->u.ip4;
387511c3f92SEric Dumazet 	rt = skb_rtable(skb);
38800db4124SIan Morris 	if (rt)
3891da177e4SLinus Torvalds 		goto packet_routed;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	/* Make sure we can route this packet. */
3921da177e4SLinus Torvalds 	rt = (struct rtable *)__sk_dst_check(sk, 0);
39351456b29SIan Morris 	if (!rt) {
3943ca3c68eSAl Viro 		__be32 daddr;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		/* Use correct destination address if we have options. */
397c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
398f6d8bd05SEric Dumazet 		if (inet_opt && inet_opt->opt.srr)
399f6d8bd05SEric Dumazet 			daddr = inet_opt->opt.faddr;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		/* If this fails, retransmit mechanism of transport layer will
4021da177e4SLinus Torvalds 		 * keep trying until route appears or the connection times
4031da177e4SLinus Torvalds 		 * itself out.
4041da177e4SLinus Torvalds 		 */
405b57ae01aSDavid S. Miller 		rt = ip_route_output_ports(sock_net(sk), fl4, sk,
40678fbfd8aSDavid S. Miller 					   daddr, inet->inet_saddr,
40778fbfd8aSDavid S. Miller 					   inet->inet_dport,
40878fbfd8aSDavid S. Miller 					   inet->inet_sport,
40978fbfd8aSDavid S. Miller 					   sk->sk_protocol,
41078fbfd8aSDavid S. Miller 					   RT_CONN_FLAGS(sk),
41178fbfd8aSDavid S. Miller 					   sk->sk_bound_dev_if);
412b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
4131da177e4SLinus Torvalds 			goto no_route;
414d8d1f30bSChangli Gao 		sk_setup_caps(sk, &rt->dst);
4151da177e4SLinus Torvalds 	}
416d8d1f30bSChangli Gao 	skb_dst_set_noref(skb, &rt->dst);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds packet_routed:
419155e8336SJulian Anastasov 	if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
4201da177e4SLinus Torvalds 		goto no_route;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	/* OK, we know where to send it, allocate and build IP header. */
423f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
4248856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
425eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
426714e85beSAl Viro 	*((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
42760ff7467SWANG Cong 	if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
4281da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
4291da177e4SLinus Torvalds 	else
4301da177e4SLinus Torvalds 		iph->frag_off = 0;
431d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
4321da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
43384f9307cSEric Dumazet 	ip_copy_addrs(iph, fl4);
43484f9307cSEric Dumazet 
4351da177e4SLinus Torvalds 	/* Transport layer set skb->h.foo itself. */
4361da177e4SLinus Torvalds 
437f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.optlen) {
438f6d8bd05SEric Dumazet 		iph->ihl += inet_opt->opt.optlen >> 2;
439f6d8bd05SEric Dumazet 		ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
442b6a7719aSHannes Frederic Sowa 	ip_select_ident_segs(sock_net(sk), skb, sk,
443b6a7719aSHannes Frederic Sowa 			     skb_shinfo(skb)->gso_segs ?: 1);
4441da177e4SLinus Torvalds 
445b0270e91SEric Dumazet 	/* TODO : should we use skb->sk here instead of sk ? */
4461da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
4474a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
4481da177e4SLinus Torvalds 
449ab6e3febSEric Dumazet 	res = ip_local_out(skb);
450ab6e3febSEric Dumazet 	rcu_read_unlock();
451ab6e3febSEric Dumazet 	return res;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds no_route:
454ab6e3febSEric Dumazet 	rcu_read_unlock();
4555e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
4561da177e4SLinus Torvalds 	kfree_skb(skb);
4571da177e4SLinus Torvalds 	return -EHOSTUNREACH;
4581da177e4SLinus Torvalds }
4594bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
4641da177e4SLinus Torvalds 	to->priority = from->priority;
4651da177e4SLinus Torvalds 	to->protocol = from->protocol;
466adf30907SEric Dumazet 	skb_dst_drop(to);
467fe76cda3SEric Dumazet 	skb_dst_copy(to, from);
4681da177e4SLinus Torvalds 	to->dev = from->dev;
46982e91ffeSThomas Graf 	to->mark = from->mark;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	/* Copy the flags to each fragment. */
4721da177e4SLinus Torvalds 	IPCB(to)->flags = IPCB(from)->flags;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4751da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
4761da177e4SLinus Torvalds #endif
477e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
478c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
479c98d80edSJulian Anastasov 	to->ipvs_property = from->ipvs_property;
480c98d80edSJulian Anastasov #endif
481984bc16cSJames Morris 	skb_copy_secmark(to, from);
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484*49d16b23SAndy Zhou static int ip_fragment(struct sock *sk, struct sk_buff *skb,
485*49d16b23SAndy Zhou 		       int (*output)(struct sock *, struct sk_buff *))
486*49d16b23SAndy Zhou {
487*49d16b23SAndy Zhou 	struct iphdr *iph = ip_hdr(skb);
488*49d16b23SAndy Zhou 	unsigned int mtu = ip_skb_dst_mtu(skb);
489*49d16b23SAndy Zhou 
490*49d16b23SAndy Zhou 	if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
491*49d16b23SAndy Zhou 		     (IPCB(skb)->frag_max_size &&
492*49d16b23SAndy Zhou 		      IPCB(skb)->frag_max_size > mtu))) {
493*49d16b23SAndy Zhou 		struct rtable *rt = skb_rtable(skb);
494*49d16b23SAndy Zhou 		struct net_device *dev = rt->dst.dev;
495*49d16b23SAndy Zhou 
496*49d16b23SAndy Zhou 		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
497*49d16b23SAndy Zhou 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
498*49d16b23SAndy Zhou 			  htonl(mtu));
499*49d16b23SAndy Zhou 		kfree_skb(skb);
500*49d16b23SAndy Zhou 		return -EMSGSIZE;
501*49d16b23SAndy Zhou 	}
502*49d16b23SAndy Zhou 
503*49d16b23SAndy Zhou 	return ip_do_fragment(sk, skb, output);
504*49d16b23SAndy Zhou }
505*49d16b23SAndy Zhou 
5061da177e4SLinus Torvalds /*
5071da177e4SLinus Torvalds  *	This IP datagram is too large to be sent in one piece.  Break it up into
5081da177e4SLinus Torvalds  *	smaller pieces (each of size equal to IP header plus
5091da177e4SLinus Torvalds  *	a block of the data of the original IP data part) that will yet fit in a
5101da177e4SLinus Torvalds  *	single device frame, and queue such a frame for sending.
5111da177e4SLinus Torvalds  */
5121da177e4SLinus Torvalds 
513*49d16b23SAndy Zhou int ip_do_fragment(struct sock *sk, struct sk_buff *skb,
5147026b1ddSDavid Miller 		   int (*output)(struct sock *, struct sk_buff *))
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	struct iphdr *iph;
5171da177e4SLinus Torvalds 	int ptr;
5181da177e4SLinus Torvalds 	struct net_device *dev;
5191da177e4SLinus Torvalds 	struct sk_buff *skb2;
520c893b806SChangli Gao 	unsigned int mtu, hlen, left, len, ll_rs;
5211da177e4SLinus Torvalds 	int offset;
52276ab608dSAlexey Dobriyan 	__be16 not_last_frag;
523511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
5241da177e4SLinus Torvalds 	int err = 0;
5251da177e4SLinus Torvalds 
526d8d1f30bSChangli Gao 	dev = rt->dst.dev;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/*
5291da177e4SLinus Torvalds 	 *	Point into the IP datagram header.
5301da177e4SLinus Torvalds 	 */
5311da177e4SLinus Torvalds 
532eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
5331da177e4SLinus Torvalds 
53469647ce4SHannes Frederic Sowa 	mtu = ip_skb_dst_mtu(skb);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	/*
5371da177e4SLinus Torvalds 	 *	Setup starting values.
5381da177e4SLinus Torvalds 	 */
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	hlen = iph->ihl * 4;
541f87c10a8SHannes Frederic Sowa 	mtu = mtu - hlen;	/* Size of data space */
5421109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
5436c79bf0fSBart De Schuymer 	if (skb->nf_bridge)
5446c79bf0fSBart De Schuymer 		mtu -= nf_bridge_mtu_reduction(skb);
5456c79bf0fSBart De Schuymer #endif
54689cee8b1SHerbert Xu 	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	/* When frag_list is given, use it. First, check its validity:
5491da177e4SLinus Torvalds 	 * some transformers could create wrong frag_list or break existing
5501da177e4SLinus Torvalds 	 * one, it is not prohibited. In this case fall back to copying.
5511da177e4SLinus Torvalds 	 *
5521da177e4SLinus Torvalds 	 * LATER: this step can be merged to real generation of fragments,
5531da177e4SLinus Torvalds 	 * we can switch to copy when see the first bad fragment.
5541da177e4SLinus Torvalds 	 */
55521dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
5563d13008eSEric Dumazet 		struct sk_buff *frag, *frag2;
5571da177e4SLinus Torvalds 		int first_len = skb_pagelen(skb);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
5601da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
56156f8a75cSPaul Gortmaker 		    ip_is_fragment(iph) ||
5621da177e4SLinus Torvalds 		    skb_cloned(skb))
5631da177e4SLinus Torvalds 			goto slow_path;
5641da177e4SLinus Torvalds 
565d7fcf1a5SDavid S. Miller 		skb_walk_frags(skb, frag) {
5661da177e4SLinus Torvalds 			/* Correct geometry. */
5671da177e4SLinus Torvalds 			if (frag->len > mtu ||
5681da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
5691da177e4SLinus Torvalds 			    skb_headroom(frag) < hlen)
5703d13008eSEric Dumazet 				goto slow_path_clean;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 			/* Partially cloned skb? */
5731da177e4SLinus Torvalds 			if (skb_shared(frag))
5743d13008eSEric Dumazet 				goto slow_path_clean;
5752fdba6b0SHerbert Xu 
5762fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
5772fdba6b0SHerbert Xu 			if (skb->sk) {
5782fdba6b0SHerbert Xu 				frag->sk = skb->sk;
5792fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
5802fdba6b0SHerbert Xu 			}
5813d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
5821da177e4SLinus Torvalds 		}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 		/* Everything is OK. Generate! */
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 		err = 0;
5871da177e4SLinus Torvalds 		offset = 0;
5881da177e4SLinus Torvalds 		frag = skb_shinfo(skb)->frag_list;
589d7fcf1a5SDavid S. Miller 		skb_frag_list_init(skb);
5901da177e4SLinus Torvalds 		skb->data_len = first_len - skb_headlen(skb);
5911da177e4SLinus Torvalds 		skb->len = first_len;
5921da177e4SLinus Torvalds 		iph->tot_len = htons(first_len);
5931da177e4SLinus Torvalds 		iph->frag_off = htons(IP_MF);
5941da177e4SLinus Torvalds 		ip_send_check(iph);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 		for (;;) {
5971da177e4SLinus Torvalds 			/* Prepare header of the next frame,
5981da177e4SLinus Torvalds 			 * before previous one went down. */
5991da177e4SLinus Torvalds 			if (frag) {
6001da177e4SLinus Torvalds 				frag->ip_summed = CHECKSUM_NONE;
601badff6d0SArnaldo Carvalho de Melo 				skb_reset_transport_header(frag);
602e2d1bca7SArnaldo Carvalho de Melo 				__skb_push(frag, hlen);
603e2d1bca7SArnaldo Carvalho de Melo 				skb_reset_network_header(frag);
604d56f90a7SArnaldo Carvalho de Melo 				memcpy(skb_network_header(frag), iph, hlen);
605eddc9ec5SArnaldo Carvalho de Melo 				iph = ip_hdr(frag);
6061da177e4SLinus Torvalds 				iph->tot_len = htons(frag->len);
6071da177e4SLinus Torvalds 				ip_copy_metadata(frag, skb);
6081da177e4SLinus Torvalds 				if (offset == 0)
6091da177e4SLinus Torvalds 					ip_options_fragment(frag);
6101da177e4SLinus Torvalds 				offset += skb->len - hlen;
6111da177e4SLinus Torvalds 				iph->frag_off = htons(offset>>3);
61200db4124SIan Morris 				if (frag->next)
6131da177e4SLinus Torvalds 					iph->frag_off |= htons(IP_MF);
6141da177e4SLinus Torvalds 				/* Ready, complete checksum */
6151da177e4SLinus Torvalds 				ip_send_check(iph);
6161da177e4SLinus Torvalds 			}
6171da177e4SLinus Torvalds 
6187026b1ddSDavid Miller 			err = output(sk, skb);
6191da177e4SLinus Torvalds 
620dafee490SWei Dong 			if (!err)
6215e38e270SPavel Emelyanov 				IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
6221da177e4SLinus Torvalds 			if (err || !frag)
6231da177e4SLinus Torvalds 				break;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 			skb = frag;
6261da177e4SLinus Torvalds 			frag = skb->next;
6271da177e4SLinus Torvalds 			skb->next = NULL;
6281da177e4SLinus Torvalds 		}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 		if (err == 0) {
6315e38e270SPavel Emelyanov 			IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
6321da177e4SLinus Torvalds 			return 0;
6331da177e4SLinus Torvalds 		}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		while (frag) {
6361da177e4SLinus Torvalds 			skb = frag->next;
6371da177e4SLinus Torvalds 			kfree_skb(frag);
6381da177e4SLinus Torvalds 			frag = skb;
6391da177e4SLinus Torvalds 		}
6405e38e270SPavel Emelyanov 		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
6411da177e4SLinus Torvalds 		return err;
6423d13008eSEric Dumazet 
6433d13008eSEric Dumazet slow_path_clean:
6443d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
6453d13008eSEric Dumazet 			if (frag2 == frag)
6463d13008eSEric Dumazet 				break;
6473d13008eSEric Dumazet 			frag2->sk = NULL;
6483d13008eSEric Dumazet 			frag2->destructor = NULL;
6493d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
6503d13008eSEric Dumazet 		}
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds slow_path:
654fc70fb64SAlexander Duyck 	/* for offloaded checksums cleanup checksum before fragmentation */
655fc70fb64SAlexander Duyck 	if ((skb->ip_summed == CHECKSUM_PARTIAL) && skb_checksum_help(skb))
656fc70fb64SAlexander Duyck 		goto fail;
657c9af6db4SPravin B Shelar 	iph = ip_hdr(skb);
658fc70fb64SAlexander Duyck 
6591da177e4SLinus Torvalds 	left = skb->len - hlen;		/* Space per frame */
66049085bd7SGeorge Kadianakis 	ptr = hlen;		/* Where to start from */
6611da177e4SLinus Torvalds 
6628d045163SFlorian Westphal 	ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
6639bcfcaf5SStephen Hemminger 
6641da177e4SLinus Torvalds 	/*
6651da177e4SLinus Torvalds 	 *	Fragment the datagram.
6661da177e4SLinus Torvalds 	 */
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
6691da177e4SLinus Torvalds 	not_last_frag = iph->frag_off & htons(IP_MF);
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	/*
6721da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
6731da177e4SLinus Torvalds 	 */
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	while (left > 0) {
6761da177e4SLinus Torvalds 		len = left;
6771da177e4SLinus Torvalds 		/* IF: it doesn't fit, use 'mtu' - the data space left */
6781da177e4SLinus Torvalds 		if (len > mtu)
6791da177e4SLinus Torvalds 			len = mtu;
6801da177e4SLinus Torvalds 		/* IF: we are not sending up to and including the packet end
6811da177e4SLinus Torvalds 		   then align the next start on an eight byte boundary */
6821da177e4SLinus Torvalds 		if (len < left)	{
6831da177e4SLinus Torvalds 			len &= ~7;
6841da177e4SLinus Torvalds 		}
6851da177e4SLinus Torvalds 
686cbffccc9SJoe Perches 		/* Allocate buffer */
687cbffccc9SJoe Perches 		skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
688cbffccc9SJoe Perches 		if (!skb2) {
6891da177e4SLinus Torvalds 			err = -ENOMEM;
6901da177e4SLinus Torvalds 			goto fail;
6911da177e4SLinus Torvalds 		}
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 		/*
6941da177e4SLinus Torvalds 		 *	Set up data on packet
6951da177e4SLinus Torvalds 		 */
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		ip_copy_metadata(skb2, skb);
6981da177e4SLinus Torvalds 		skb_reserve(skb2, ll_rs);
6991da177e4SLinus Torvalds 		skb_put(skb2, len + hlen);
700c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
701b0e380b1SArnaldo Carvalho de Melo 		skb2->transport_header = skb2->network_header + hlen;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 		/*
7041da177e4SLinus Torvalds 		 *	Charge the memory for the fragment to any owner
7051da177e4SLinus Torvalds 		 *	it might possess
7061da177e4SLinus Torvalds 		 */
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 		if (skb->sk)
7091da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 		/*
7121da177e4SLinus Torvalds 		 *	Copy the packet header into the new buffer.
7131da177e4SLinus Torvalds 		 */
7141da177e4SLinus Torvalds 
715d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 		/*
7181da177e4SLinus Torvalds 		 *	Copy a block of the IP datagram.
7191da177e4SLinus Torvalds 		 */
720bff9b61cSArnaldo Carvalho de Melo 		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
7211da177e4SLinus Torvalds 			BUG();
7221da177e4SLinus Torvalds 		left -= len;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 		/*
7251da177e4SLinus Torvalds 		 *	Fill in the new header fields.
7261da177e4SLinus Torvalds 		 */
727eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
7281da177e4SLinus Torvalds 		iph->frag_off = htons((offset >> 3));
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		/* ANK: dirty, but effective trick. Upgrade options only if
7311da177e4SLinus Torvalds 		 * the segment to be fragmented was THE FIRST (otherwise,
7321da177e4SLinus Torvalds 		 * options are already fixed) and make it ONCE
7331da177e4SLinus Torvalds 		 * on the initial skb, so that all the following fragments
7341da177e4SLinus Torvalds 		 * will inherit fixed options.
7351da177e4SLinus Torvalds 		 */
7361da177e4SLinus Torvalds 		if (offset == 0)
7371da177e4SLinus Torvalds 			ip_options_fragment(skb);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		/*
7401da177e4SLinus Torvalds 		 *	Added AC : If we are fragmenting a fragment that's not the
7411da177e4SLinus Torvalds 		 *		   last fragment then keep MF on each bit
7421da177e4SLinus Torvalds 		 */
7431da177e4SLinus Torvalds 		if (left > 0 || not_last_frag)
7441da177e4SLinus Torvalds 			iph->frag_off |= htons(IP_MF);
7451da177e4SLinus Torvalds 		ptr += len;
7461da177e4SLinus Torvalds 		offset += len;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 		/*
7491da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
7501da177e4SLinus Torvalds 		 */
7511da177e4SLinus Torvalds 		iph->tot_len = htons(len + hlen);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 		ip_send_check(iph);
7541da177e4SLinus Torvalds 
7557026b1ddSDavid Miller 		err = output(sk, skb2);
7561da177e4SLinus Torvalds 		if (err)
7571da177e4SLinus Torvalds 			goto fail;
758dafee490SWei Dong 
7595e38e270SPavel Emelyanov 		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
7601da177e4SLinus Torvalds 	}
7615d0ba55bSEric Dumazet 	consume_skb(skb);
7625e38e270SPavel Emelyanov 	IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
7631da177e4SLinus Torvalds 	return err;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds fail:
7661da177e4SLinus Torvalds 	kfree_skb(skb);
7675e38e270SPavel Emelyanov 	IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
7681da177e4SLinus Torvalds 	return err;
7691da177e4SLinus Torvalds }
770*49d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment);
7712e2f7aefSPatrick McHardy 
7721da177e4SLinus Torvalds int
7731da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
7741da177e4SLinus Torvalds {
775f69e6d13SAl Viro 	struct msghdr *msg = from;
7761da177e4SLinus Torvalds 
77784fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
77821226abbSAl Viro 		if (copy_from_iter(to, len, &msg->msg_iter) != len)
7791da177e4SLinus Torvalds 			return -EFAULT;
7801da177e4SLinus Torvalds 	} else {
78144bb9363SAl Viro 		__wsum csum = 0;
78221226abbSAl Viro 		if (csum_and_copy_from_iter(to, len, &csum, &msg->msg_iter) != len)
7831da177e4SLinus Torvalds 			return -EFAULT;
7841da177e4SLinus Torvalds 		skb->csum = csum_block_add(skb->csum, csum, odd);
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	return 0;
7871da177e4SLinus Torvalds }
7884bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag);
7891da177e4SLinus Torvalds 
79044bb9363SAl Viro static inline __wsum
7911da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	char *kaddr;
79444bb9363SAl Viro 	__wsum csum;
7951da177e4SLinus Torvalds 	kaddr = kmap(page);
7961da177e4SLinus Torvalds 	csum = csum_partial(kaddr + offset, copy, 0);
7971da177e4SLinus Torvalds 	kunmap(page);
7981da177e4SLinus Torvalds 	return csum;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8014b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk,
8021470ddf7SHerbert Xu 			struct sk_buff_head *queue,
803e89e9cf5SAnanda Raju 			int getfrag(void *from, char *to, int offset, int len,
804e89e9cf5SAnanda Raju 			       int odd, struct sk_buff *skb),
805e89e9cf5SAnanda Raju 			void *from, int length, int hh_len, int fragheaderlen,
806d9be4f7aSBill Sommerfeld 			int transhdrlen, int maxfraglen, unsigned int flags)
807e89e9cf5SAnanda Raju {
808e89e9cf5SAnanda Raju 	struct sk_buff *skb;
809e89e9cf5SAnanda Raju 	int err;
810e89e9cf5SAnanda Raju 
811e89e9cf5SAnanda Raju 	/* There is support for UDP fragmentation offload by network
812e89e9cf5SAnanda Raju 	 * device, so create one single skb packet containing complete
813e89e9cf5SAnanda Raju 	 * udp datagram
814e89e9cf5SAnanda Raju 	 */
81551456b29SIan Morris 	skb = skb_peek_tail(queue);
81651456b29SIan Morris 	if (!skb) {
817e89e9cf5SAnanda Raju 		skb = sock_alloc_send_skb(sk,
818e89e9cf5SAnanda Raju 			hh_len + fragheaderlen + transhdrlen + 20,
819e89e9cf5SAnanda Raju 			(flags & MSG_DONTWAIT), &err);
820e89e9cf5SAnanda Raju 
82151456b29SIan Morris 		if (!skb)
822e89e9cf5SAnanda Raju 			return err;
823e89e9cf5SAnanda Raju 
824e89e9cf5SAnanda Raju 		/* reserve space for Hardware header */
825e89e9cf5SAnanda Raju 		skb_reserve(skb, hh_len);
826e89e9cf5SAnanda Raju 
827e89e9cf5SAnanda Raju 		/* create space for UDP/IP header */
828e89e9cf5SAnanda Raju 		skb_put(skb, fragheaderlen + transhdrlen);
829e89e9cf5SAnanda Raju 
830e89e9cf5SAnanda Raju 		/* initialize network header pointer */
831c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
832e89e9cf5SAnanda Raju 
833e89e9cf5SAnanda Raju 		/* initialize protocol header pointer */
834b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header = skb->network_header + fragheaderlen;
835e89e9cf5SAnanda Raju 
836e89e9cf5SAnanda Raju 		skb->csum = 0;
837e89e9cf5SAnanda Raju 
838e93b7d74SJiri Pirko 		__skb_queue_tail(queue, skb);
839e93b7d74SJiri Pirko 	} else if (skb_is_gso(skb)) {
840e93b7d74SJiri Pirko 		goto append;
841e93b7d74SJiri Pirko 	}
842e93b7d74SJiri Pirko 
843e93b7d74SJiri Pirko 	skb->ip_summed = CHECKSUM_PARTIAL;
844e89e9cf5SAnanda Raju 	/* specify the length of each IP datagram fragment */
845d9be4f7aSBill Sommerfeld 	skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen;
846f83ef8c0SHerbert Xu 	skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
847be9164e7SKostya B 
848e93b7d74SJiri Pirko append:
849be9164e7SKostya B 	return skb_append_datato_frags(sk, skb, getfrag, from,
850be9164e7SKostya B 				       (length - transhdrlen));
851e89e9cf5SAnanda Raju }
852e89e9cf5SAnanda Raju 
853f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk,
854f5fca608SDavid S. Miller 			    struct flowi4 *fl4,
855f5fca608SDavid S. Miller 			    struct sk_buff_head *queue,
8561470ddf7SHerbert Xu 			    struct inet_cork *cork,
8575640f768SEric Dumazet 			    struct page_frag *pfrag,
8581470ddf7SHerbert Xu 			    int getfrag(void *from, char *to, int offset,
8591470ddf7SHerbert Xu 					int len, int odd, struct sk_buff *skb),
8601da177e4SLinus Torvalds 			    void *from, int length, int transhdrlen,
8611da177e4SLinus Torvalds 			    unsigned int flags)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
8641da177e4SLinus Torvalds 	struct sk_buff *skb;
8651da177e4SLinus Torvalds 
86607df5294SHerbert Xu 	struct ip_options *opt = cork->opt;
8671da177e4SLinus Torvalds 	int hh_len;
8681da177e4SLinus Torvalds 	int exthdrlen;
8691da177e4SLinus Torvalds 	int mtu;
8701da177e4SLinus Torvalds 	int copy;
8711da177e4SLinus Torvalds 	int err;
8721da177e4SLinus Torvalds 	int offset = 0;
873daba287bSHannes Frederic Sowa 	unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
8741da177e4SLinus Torvalds 	int csummode = CHECKSUM_NONE;
8751470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
87609c2d251SWillem de Bruijn 	u32 tskey = 0;
8771da177e4SLinus Torvalds 
87896d7303eSSteffen Klassert 	skb = skb_peek_tail(queue);
87996d7303eSSteffen Klassert 
88096d7303eSSteffen Klassert 	exthdrlen = !skb ? rt->dst.header_len : 0;
88107df5294SHerbert Xu 	mtu = cork->fragsize;
88209c2d251SWillem de Bruijn 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
88309c2d251SWillem de Bruijn 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
88409c2d251SWillem de Bruijn 		tskey = sk->sk_tskey++;
8851470ddf7SHerbert Xu 
886d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
8891da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
89060ff7467SWANG Cong 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
8911da177e4SLinus Torvalds 
892daba287bSHannes Frederic Sowa 	if (cork->length + length > maxnonfragsize - fragheaderlen) {
893f5fca608SDavid S. Miller 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
89461e7f09dSHannes Frederic Sowa 			       mtu - (opt ? opt->optlen : 0));
8951da177e4SLinus Torvalds 		return -EMSGSIZE;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/*
8991da177e4SLinus Torvalds 	 * transhdrlen > 0 means that this is the first fragment and we wish
9001da177e4SLinus Torvalds 	 * it won't be fragmented in the future.
9011da177e4SLinus Torvalds 	 */
9021da177e4SLinus Torvalds 	if (transhdrlen &&
9031da177e4SLinus Torvalds 	    length + fragheaderlen <= mtu &&
904d8d1f30bSChangli Gao 	    rt->dst.dev->features & NETIF_F_V4_CSUM &&
9051da177e4SLinus Torvalds 	    !exthdrlen)
90684fa7933SPatrick McHardy 		csummode = CHECKSUM_PARTIAL;
9071da177e4SLinus Torvalds 
9081470ddf7SHerbert Xu 	cork->length += length;
90926cde9f7SHerbert Xu 	if (((length > mtu) || (skb && skb_is_gso(skb))) &&
910be9164e7SKostya B 	    (sk->sk_protocol == IPPROTO_UDP) &&
911acf8dd0aSMichal Kubeček 	    (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len &&
912acf8dd0aSMichal Kubeček 	    (sk->sk_type == SOCK_DGRAM)) {
9131470ddf7SHerbert Xu 		err = ip_ufo_append_data(sk, queue, getfrag, from, length,
9141470ddf7SHerbert Xu 					 hh_len, fragheaderlen, transhdrlen,
915d9be4f7aSBill Sommerfeld 					 maxfraglen, flags);
916baa829d8SPatrick McHardy 		if (err)
917e89e9cf5SAnanda Raju 			goto error;
918e89e9cf5SAnanda Raju 		return 0;
919e89e9cf5SAnanda Raju 	}
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	/* So, what's going on in the loop below?
9221da177e4SLinus Torvalds 	 *
9231da177e4SLinus Torvalds 	 * We use calculated fragment length to generate chained skb,
9241da177e4SLinus Torvalds 	 * each of segments is IP fragment ready for sending to network after
9251da177e4SLinus Torvalds 	 * adding appropriate IP header.
9261da177e4SLinus Torvalds 	 */
9271da177e4SLinus Torvalds 
92826cde9f7SHerbert Xu 	if (!skb)
9291da177e4SLinus Torvalds 		goto alloc_new_skb;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	while (length > 0) {
9321da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
9331da177e4SLinus Torvalds 		copy = mtu - skb->len;
9341da177e4SLinus Torvalds 		if (copy < length)
9351da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
9361da177e4SLinus Torvalds 		if (copy <= 0) {
9371da177e4SLinus Torvalds 			char *data;
9381da177e4SLinus Torvalds 			unsigned int datalen;
9391da177e4SLinus Torvalds 			unsigned int fraglen;
9401da177e4SLinus Torvalds 			unsigned int fraggap;
9411da177e4SLinus Torvalds 			unsigned int alloclen;
9421da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
9431da177e4SLinus Torvalds alloc_new_skb:
9441da177e4SLinus Torvalds 			skb_prev = skb;
9451da177e4SLinus Torvalds 			if (skb_prev)
9461da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
9471da177e4SLinus Torvalds 			else
9481da177e4SLinus Torvalds 				fraggap = 0;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 			/*
9511da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
9521da177e4SLinus Torvalds 			 * we know we need more fragment(s).
9531da177e4SLinus Torvalds 			 */
9541da177e4SLinus Torvalds 			datalen = length + fraggap;
9551da177e4SLinus Torvalds 			if (datalen > mtu - fragheaderlen)
9561da177e4SLinus Torvalds 				datalen = maxfraglen - fragheaderlen;
9571da177e4SLinus Torvalds 			fraglen = datalen + fragheaderlen;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
960d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
9611da177e4SLinus Torvalds 				alloclen = mtu;
9621da177e4SLinus Torvalds 			else
96359104f06SEric Dumazet 				alloclen = fraglen;
9641da177e4SLinus Torvalds 
965353e5c9aSSteffen Klassert 			alloclen += exthdrlen;
966353e5c9aSSteffen Klassert 
9671da177e4SLinus Torvalds 			/* The last fragment gets additional space at tail.
9681da177e4SLinus Torvalds 			 * Note, with MSG_MORE we overallocate on fragments,
9691da177e4SLinus Torvalds 			 * because we have no idea what fragment will be
9701da177e4SLinus Torvalds 			 * the last.
9711da177e4SLinus Torvalds 			 */
97233f99dc7SSteffen Klassert 			if (datalen == length + fraggap)
973d8d1f30bSChangli Gao 				alloclen += rt->dst.trailer_len;
97433f99dc7SSteffen Klassert 
9751da177e4SLinus Torvalds 			if (transhdrlen) {
9761da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
9771da177e4SLinus Torvalds 						alloclen + hh_len + 15,
9781da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
9791da177e4SLinus Torvalds 			} else {
9801da177e4SLinus Torvalds 				skb = NULL;
9811da177e4SLinus Torvalds 				if (atomic_read(&sk->sk_wmem_alloc) <=
9821da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
9831da177e4SLinus Torvalds 					skb = sock_wmalloc(sk,
9841da177e4SLinus Torvalds 							   alloclen + hh_len + 15, 1,
9851da177e4SLinus Torvalds 							   sk->sk_allocation);
98651456b29SIan Morris 				if (unlikely(!skb))
9871da177e4SLinus Torvalds 					err = -ENOBUFS;
9881da177e4SLinus Torvalds 			}
98951456b29SIan Morris 			if (!skb)
9901da177e4SLinus Torvalds 				goto error;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 			/*
9931da177e4SLinus Torvalds 			 *	Fill in the control structures
9941da177e4SLinus Torvalds 			 */
9951da177e4SLinus Torvalds 			skb->ip_summed = csummode;
9961da177e4SLinus Torvalds 			skb->csum = 0;
9971da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
99811878b40SWillem de Bruijn 
99911878b40SWillem de Bruijn 			/* only the initial fragment is time stamped */
10001470ddf7SHerbert Xu 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
100111878b40SWillem de Bruijn 			cork->tx_flags = 0;
100209c2d251SWillem de Bruijn 			skb_shinfo(skb)->tskey = tskey;
100309c2d251SWillem de Bruijn 			tskey = 0;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 			/*
10061da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
10071da177e4SLinus Torvalds 			 */
1008353e5c9aSSteffen Klassert 			data = skb_put(skb, fraglen + exthdrlen);
1009c14d2450SArnaldo Carvalho de Melo 			skb_set_network_header(skb, exthdrlen);
1010b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1011b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
1012353e5c9aSSteffen Klassert 			data += fragheaderlen + exthdrlen;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 			if (fraggap) {
10151da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
10161da177e4SLinus Torvalds 					skb_prev, maxfraglen,
10171da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
10181da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
10191da177e4SLinus Torvalds 							  skb->csum);
10201da177e4SLinus Torvalds 				data += fraggap;
1021e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
10221da177e4SLinus Torvalds 			}
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 			copy = datalen - transhdrlen - fraggap;
10251da177e4SLinus Torvalds 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
10261da177e4SLinus Torvalds 				err = -EFAULT;
10271da177e4SLinus Torvalds 				kfree_skb(skb);
10281da177e4SLinus Torvalds 				goto error;
10291da177e4SLinus Torvalds 			}
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 			offset += copy;
10321da177e4SLinus Torvalds 			length -= datalen - fraggap;
10331da177e4SLinus Torvalds 			transhdrlen = 0;
10341da177e4SLinus Torvalds 			exthdrlen = 0;
10351da177e4SLinus Torvalds 			csummode = CHECKSUM_NONE;
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 			/*
10381da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
10391da177e4SLinus Torvalds 			 */
10401470ddf7SHerbert Xu 			__skb_queue_tail(queue, skb);
10411da177e4SLinus Torvalds 			continue;
10421da177e4SLinus Torvalds 		}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 		if (copy > length)
10451da177e4SLinus Torvalds 			copy = length;
10461da177e4SLinus Torvalds 
1047d8d1f30bSChangli Gao 		if (!(rt->dst.dev->features&NETIF_F_SG)) {
10481da177e4SLinus Torvalds 			unsigned int off;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 			off = skb->len;
10511da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
10521da177e4SLinus Torvalds 					offset, copy, off, skb) < 0) {
10531da177e4SLinus Torvalds 				__skb_trim(skb, off);
10541da177e4SLinus Torvalds 				err = -EFAULT;
10551da177e4SLinus Torvalds 				goto error;
10561da177e4SLinus Torvalds 			}
10571da177e4SLinus Torvalds 		} else {
10581da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 			err = -ENOMEM;
10615640f768SEric Dumazet 			if (!sk_page_frag_refill(sk, pfrag))
10621da177e4SLinus Torvalds 				goto error;
10631da177e4SLinus Torvalds 
10645640f768SEric Dumazet 			if (!skb_can_coalesce(skb, i, pfrag->page,
10655640f768SEric Dumazet 					      pfrag->offset)) {
10661da177e4SLinus Torvalds 				err = -EMSGSIZE;
10675640f768SEric Dumazet 				if (i == MAX_SKB_FRAGS)
10681da177e4SLinus Torvalds 					goto error;
10695640f768SEric Dumazet 
10705640f768SEric Dumazet 				__skb_fill_page_desc(skb, i, pfrag->page,
10715640f768SEric Dumazet 						     pfrag->offset, 0);
10725640f768SEric Dumazet 				skb_shinfo(skb)->nr_frags = ++i;
10735640f768SEric Dumazet 				get_page(pfrag->page);
10741da177e4SLinus Torvalds 			}
10755640f768SEric Dumazet 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
10765640f768SEric Dumazet 			if (getfrag(from,
10775640f768SEric Dumazet 				    page_address(pfrag->page) + pfrag->offset,
10785640f768SEric Dumazet 				    offset, copy, skb->len, skb) < 0)
10795640f768SEric Dumazet 				goto error_efault;
10805640f768SEric Dumazet 
10815640f768SEric Dumazet 			pfrag->offset += copy;
10825640f768SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
10831da177e4SLinus Torvalds 			skb->len += copy;
10841da177e4SLinus Torvalds 			skb->data_len += copy;
1085f945fa7aSHerbert Xu 			skb->truesize += copy;
1086f945fa7aSHerbert Xu 			atomic_add(copy, &sk->sk_wmem_alloc);
10871da177e4SLinus Torvalds 		}
10881da177e4SLinus Torvalds 		offset += copy;
10891da177e4SLinus Torvalds 		length -= copy;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	return 0;
10931da177e4SLinus Torvalds 
10945640f768SEric Dumazet error_efault:
10955640f768SEric Dumazet 	err = -EFAULT;
10961da177e4SLinus Torvalds error:
10971470ddf7SHerbert Xu 	cork->length -= length;
10985e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
10991da177e4SLinus Torvalds 	return err;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
11031470ddf7SHerbert Xu 			 struct ipcm_cookie *ipc, struct rtable **rtp)
11041470ddf7SHerbert Xu {
1105f6d8bd05SEric Dumazet 	struct ip_options_rcu *opt;
11061470ddf7SHerbert Xu 	struct rtable *rt;
11071470ddf7SHerbert Xu 
11081470ddf7SHerbert Xu 	/*
11091470ddf7SHerbert Xu 	 * setup for corking.
11101470ddf7SHerbert Xu 	 */
11111470ddf7SHerbert Xu 	opt = ipc->opt;
11121470ddf7SHerbert Xu 	if (opt) {
111351456b29SIan Morris 		if (!cork->opt) {
11141470ddf7SHerbert Xu 			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
11151470ddf7SHerbert Xu 					    sk->sk_allocation);
111651456b29SIan Morris 			if (unlikely(!cork->opt))
11171470ddf7SHerbert Xu 				return -ENOBUFS;
11181470ddf7SHerbert Xu 		}
1119f6d8bd05SEric Dumazet 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
11201470ddf7SHerbert Xu 		cork->flags |= IPCORK_OPT;
11211470ddf7SHerbert Xu 		cork->addr = ipc->addr;
11221470ddf7SHerbert Xu 	}
11231470ddf7SHerbert Xu 	rt = *rtp;
11241470ddf7SHerbert Xu 	if (unlikely(!rt))
11251470ddf7SHerbert Xu 		return -EFAULT;
11261470ddf7SHerbert Xu 	/*
11271470ddf7SHerbert Xu 	 * We steal reference to this route, caller should not release it
11281470ddf7SHerbert Xu 	 */
11291470ddf7SHerbert Xu 	*rtp = NULL;
1130482fc609SHannes Frederic Sowa 	cork->fragsize = ip_sk_use_pmtu(sk) ?
1131482fc609SHannes Frederic Sowa 			 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
11321470ddf7SHerbert Xu 	cork->dst = &rt->dst;
11331470ddf7SHerbert Xu 	cork->length = 0;
1134aa661581SFrancesco Fusco 	cork->ttl = ipc->ttl;
1135aa661581SFrancesco Fusco 	cork->tos = ipc->tos;
1136aa661581SFrancesco Fusco 	cork->priority = ipc->priority;
11371470ddf7SHerbert Xu 	cork->tx_flags = ipc->tx_flags;
11381470ddf7SHerbert Xu 
11391470ddf7SHerbert Xu 	return 0;
11401470ddf7SHerbert Xu }
11411470ddf7SHerbert Xu 
11421470ddf7SHerbert Xu /*
11431470ddf7SHerbert Xu  *	ip_append_data() and ip_append_page() can make one large IP datagram
11441470ddf7SHerbert Xu  *	from many pieces of data. Each pieces will be holded on the socket
11451470ddf7SHerbert Xu  *	until ip_push_pending_frames() is called. Each piece can be a page
11461470ddf7SHerbert Xu  *	or non-page data.
11471470ddf7SHerbert Xu  *
11481470ddf7SHerbert Xu  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
11491470ddf7SHerbert Xu  *	this interface potentially.
11501470ddf7SHerbert Xu  *
11511470ddf7SHerbert Xu  *	LATER: length must be adjusted by pad at tail, when it is required.
11521470ddf7SHerbert Xu  */
1153f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4,
11541470ddf7SHerbert Xu 		   int getfrag(void *from, char *to, int offset, int len,
11551470ddf7SHerbert Xu 			       int odd, struct sk_buff *skb),
11561470ddf7SHerbert Xu 		   void *from, int length, int transhdrlen,
11571470ddf7SHerbert Xu 		   struct ipcm_cookie *ipc, struct rtable **rtp,
11581470ddf7SHerbert Xu 		   unsigned int flags)
11591470ddf7SHerbert Xu {
11601470ddf7SHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
11611470ddf7SHerbert Xu 	int err;
11621470ddf7SHerbert Xu 
11631470ddf7SHerbert Xu 	if (flags&MSG_PROBE)
11641470ddf7SHerbert Xu 		return 0;
11651470ddf7SHerbert Xu 
11661470ddf7SHerbert Xu 	if (skb_queue_empty(&sk->sk_write_queue)) {
1167bdc712b4SDavid S. Miller 		err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
11681470ddf7SHerbert Xu 		if (err)
11691470ddf7SHerbert Xu 			return err;
11701470ddf7SHerbert Xu 	} else {
11711470ddf7SHerbert Xu 		transhdrlen = 0;
11721470ddf7SHerbert Xu 	}
11731470ddf7SHerbert Xu 
11745640f768SEric Dumazet 	return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
11755640f768SEric Dumazet 				sk_page_frag(sk), getfrag,
11761470ddf7SHerbert Xu 				from, length, transhdrlen, flags);
11771470ddf7SHerbert Xu }
11781470ddf7SHerbert Xu 
1179f5fca608SDavid S. Miller ssize_t	ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
11801da177e4SLinus Torvalds 		       int offset, size_t size, int flags)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11831da177e4SLinus Torvalds 	struct sk_buff *skb;
11841da177e4SLinus Torvalds 	struct rtable *rt;
11851da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
1186bdc712b4SDavid S. Miller 	struct inet_cork *cork;
11871da177e4SLinus Torvalds 	int hh_len;
11881da177e4SLinus Torvalds 	int mtu;
11891da177e4SLinus Torvalds 	int len;
11901da177e4SLinus Torvalds 	int err;
1191daba287bSHannes Frederic Sowa 	unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	if (inet->hdrincl)
11941da177e4SLinus Torvalds 		return -EPERM;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
11971da177e4SLinus Torvalds 		return 0;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue))
12001da177e4SLinus Torvalds 		return -EINVAL;
12011da177e4SLinus Torvalds 
1202bdc712b4SDavid S. Miller 	cork = &inet->cork.base;
1203bdc712b4SDavid S. Miller 	rt = (struct rtable *)cork->dst;
1204bdc712b4SDavid S. Miller 	if (cork->flags & IPCORK_OPT)
1205bdc712b4SDavid S. Miller 		opt = cork->opt;
12061da177e4SLinus Torvalds 
1207d8d1f30bSChangli Gao 	if (!(rt->dst.dev->features&NETIF_F_SG))
12081da177e4SLinus Torvalds 		return -EOPNOTSUPP;
12091da177e4SLinus Torvalds 
1210d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1211bdc712b4SDavid S. Miller 	mtu = cork->fragsize;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
12141da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
121560ff7467SWANG Cong 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
12161da177e4SLinus Torvalds 
1217daba287bSHannes Frederic Sowa 	if (cork->length + size > maxnonfragsize - fragheaderlen) {
121861e7f09dSHannes Frederic Sowa 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
121961e7f09dSHannes Frederic Sowa 			       mtu - (opt ? opt->optlen : 0));
12201da177e4SLinus Torvalds 		return -EMSGSIZE;
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
122351456b29SIan Morris 	skb = skb_peek_tail(&sk->sk_write_queue);
122451456b29SIan Morris 	if (!skb)
12251da177e4SLinus Torvalds 		return -EINVAL;
12261da177e4SLinus Torvalds 
1227bdc712b4SDavid S. Miller 	cork->length += size;
122826cde9f7SHerbert Xu 	if ((size + skb->len > mtu) &&
122926cde9f7SHerbert Xu 	    (sk->sk_protocol == IPPROTO_UDP) &&
1230d8d1f30bSChangli Gao 	    (rt->dst.dev->features & NETIF_F_UFO)) {
12317967168cSHerbert Xu 		skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
1232f83ef8c0SHerbert Xu 		skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
12337967168cSHerbert Xu 	}
1234e89e9cf5SAnanda Raju 
12351da177e4SLinus Torvalds 	while (size > 0) {
12361da177e4SLinus Torvalds 		int i;
12371da177e4SLinus Torvalds 
123889114afdSHerbert Xu 		if (skb_is_gso(skb))
1239e89e9cf5SAnanda Raju 			len = size;
1240e89e9cf5SAnanda Raju 		else {
1241e89e9cf5SAnanda Raju 
12421da177e4SLinus Torvalds 			/* Check if the remaining data fits into current packet. */
12431da177e4SLinus Torvalds 			len = mtu - skb->len;
12441da177e4SLinus Torvalds 			if (len < size)
12451da177e4SLinus Torvalds 				len = maxfraglen - skb->len;
1246e89e9cf5SAnanda Raju 		}
12471da177e4SLinus Torvalds 		if (len <= 0) {
12481da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
12491da177e4SLinus Torvalds 			int alloclen;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 			skb_prev = skb;
12521da177e4SLinus Torvalds 			fraggap = skb_prev->len - maxfraglen;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 			alloclen = fragheaderlen + hh_len + fraggap + 15;
12551da177e4SLinus Torvalds 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
12561da177e4SLinus Torvalds 			if (unlikely(!skb)) {
12571da177e4SLinus Torvalds 				err = -ENOBUFS;
12581da177e4SLinus Torvalds 				goto error;
12591da177e4SLinus Torvalds 			}
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 			/*
12621da177e4SLinus Torvalds 			 *	Fill in the control structures
12631da177e4SLinus Torvalds 			 */
12641da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_NONE;
12651da177e4SLinus Torvalds 			skb->csum = 0;
12661da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 			/*
12691da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
12701da177e4SLinus Torvalds 			 */
1271967b05f6SArnaldo Carvalho de Melo 			skb_put(skb, fragheaderlen + fraggap);
12722ca9e6f2SArnaldo Carvalho de Melo 			skb_reset_network_header(skb);
1273b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1274b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
12751da177e4SLinus Torvalds 			if (fraggap) {
1276967b05f6SArnaldo Carvalho de Melo 				skb->csum = skb_copy_and_csum_bits(skb_prev,
1277967b05f6SArnaldo Carvalho de Melo 								   maxfraglen,
12789c70220bSArnaldo Carvalho de Melo 						    skb_transport_header(skb),
1279967b05f6SArnaldo Carvalho de Melo 								   fraggap, 0);
12801da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
12811da177e4SLinus Torvalds 							  skb->csum);
1282e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
12831da177e4SLinus Torvalds 			}
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 			/*
12861da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
12871da177e4SLinus Torvalds 			 */
12881da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
12891da177e4SLinus Torvalds 			continue;
12901da177e4SLinus Torvalds 		}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 		i = skb_shinfo(skb)->nr_frags;
12931da177e4SLinus Torvalds 		if (len > size)
12941da177e4SLinus Torvalds 			len = size;
12951da177e4SLinus Torvalds 		if (skb_can_coalesce(skb, i, page, offset)) {
12969e903e08SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i-1], len);
12971da177e4SLinus Torvalds 		} else if (i < MAX_SKB_FRAGS) {
12981da177e4SLinus Torvalds 			get_page(page);
12991da177e4SLinus Torvalds 			skb_fill_page_desc(skb, i, page, offset, len);
13001da177e4SLinus Torvalds 		} else {
13011da177e4SLinus Torvalds 			err = -EMSGSIZE;
13021da177e4SLinus Torvalds 			goto error;
13031da177e4SLinus Torvalds 		}
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 		if (skb->ip_summed == CHECKSUM_NONE) {
130644bb9363SAl Viro 			__wsum csum;
13071da177e4SLinus Torvalds 			csum = csum_page(page, offset, len);
13081da177e4SLinus Torvalds 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
13091da177e4SLinus Torvalds 		}
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		skb->len += len;
13121da177e4SLinus Torvalds 		skb->data_len += len;
13131e34a11dSDavid S. Miller 		skb->truesize += len;
13141e34a11dSDavid S. Miller 		atomic_add(len, &sk->sk_wmem_alloc);
13151da177e4SLinus Torvalds 		offset += len;
13161da177e4SLinus Torvalds 		size -= len;
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 	return 0;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds error:
1321bdc712b4SDavid S. Miller 	cork->length -= size;
13225e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
13231da177e4SLinus Torvalds 	return err;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork)
1327429f08e9SPavel Emelyanov {
13281470ddf7SHerbert Xu 	cork->flags &= ~IPCORK_OPT;
13291470ddf7SHerbert Xu 	kfree(cork->opt);
13301470ddf7SHerbert Xu 	cork->opt = NULL;
13311470ddf7SHerbert Xu 	dst_release(cork->dst);
13321470ddf7SHerbert Xu 	cork->dst = NULL;
1333429f08e9SPavel Emelyanov }
1334429f08e9SPavel Emelyanov 
13351da177e4SLinus Torvalds /*
13361da177e4SLinus Torvalds  *	Combined all pending IP fragments on the socket as one IP datagram
13371da177e4SLinus Torvalds  *	and push them out.
13381da177e4SLinus Torvalds  */
13391c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk,
134077968b78SDavid S. Miller 			      struct flowi4 *fl4,
13411470ddf7SHerbert Xu 			      struct sk_buff_head *queue,
13421470ddf7SHerbert Xu 			      struct inet_cork *cork)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
13451da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
13461da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
13470388b004SPavel Emelyanov 	struct net *net = sock_net(sk);
13481da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
13491470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
13501da177e4SLinus Torvalds 	struct iphdr *iph;
135176ab608dSAlexey Dobriyan 	__be16 df = 0;
13521da177e4SLinus Torvalds 	__u8 ttl;
13531da177e4SLinus Torvalds 
135451456b29SIan Morris 	skb = __skb_dequeue(queue);
135551456b29SIan Morris 	if (!skb)
13561da177e4SLinus Torvalds 		goto out;
13571da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1360d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1361bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
13621470ddf7SHerbert Xu 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1363cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
13641da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
13651da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
13661da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
13671da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
13681da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
13691da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
13701da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
13741da177e4SLinus Torvalds 	 * to fragment the frame generated here. No matter, what transforms
13751da177e4SLinus Torvalds 	 * how transforms change size of the packet, it will come out.
13761da177e4SLinus Torvalds 	 */
137760ff7467SWANG Cong 	skb->ignore_df = ip_sk_ignore_df(sk);
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	/* DF bit is set when we want to see DF on outgoing frames.
138060ff7467SWANG Cong 	 * If ignore_df is set too, we still allow to fragment this frame
13811da177e4SLinus Torvalds 	 * locally. */
1382482fc609SHannes Frederic Sowa 	if (inet->pmtudisc == IP_PMTUDISC_DO ||
1383482fc609SHannes Frederic Sowa 	    inet->pmtudisc == IP_PMTUDISC_PROBE ||
1384d8d1f30bSChangli Gao 	    (skb->len <= dst_mtu(&rt->dst) &&
1385d8d1f30bSChangli Gao 	     ip_dont_fragment(sk, &rt->dst)))
13861da177e4SLinus Torvalds 		df = htons(IP_DF);
13871da177e4SLinus Torvalds 
13881470ddf7SHerbert Xu 	if (cork->flags & IPCORK_OPT)
13891470ddf7SHerbert Xu 		opt = cork->opt;
13901da177e4SLinus Torvalds 
1391aa661581SFrancesco Fusco 	if (cork->ttl != 0)
1392aa661581SFrancesco Fusco 		ttl = cork->ttl;
1393aa661581SFrancesco Fusco 	else if (rt->rt_type == RTN_MULTICAST)
13941da177e4SLinus Torvalds 		ttl = inet->mc_ttl;
13951da177e4SLinus Torvalds 	else
1396d8d1f30bSChangli Gao 		ttl = ip_select_ttl(inet, &rt->dst);
13971da177e4SLinus Torvalds 
1398749154aaSAnsis Atteka 	iph = ip_hdr(skb);
13991da177e4SLinus Torvalds 	iph->version = 4;
14001da177e4SLinus Torvalds 	iph->ihl = 5;
1401aa661581SFrancesco Fusco 	iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
14021da177e4SLinus Torvalds 	iph->frag_off = df;
14031da177e4SLinus Torvalds 	iph->ttl = ttl;
14041da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
140584f9307cSEric Dumazet 	ip_copy_addrs(iph, fl4);
1406b6a7719aSHannes Frederic Sowa 	ip_select_ident(net, skb, sk);
14071da177e4SLinus Torvalds 
140822f728f8SDavid S. Miller 	if (opt) {
140922f728f8SDavid S. Miller 		iph->ihl += opt->optlen>>2;
141022f728f8SDavid S. Miller 		ip_options_build(skb, opt, cork->addr, rt, 0);
141122f728f8SDavid S. Miller 	}
141222f728f8SDavid S. Miller 
1413aa661581SFrancesco Fusco 	skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
14144a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1415a21bba94SEric Dumazet 	/*
1416a21bba94SEric Dumazet 	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1417a21bba94SEric Dumazet 	 * on dst refcount
1418a21bba94SEric Dumazet 	 */
14191470ddf7SHerbert Xu 	cork->dst = NULL;
1420d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
14211da177e4SLinus Torvalds 
142296793b48SDavid L Stevens 	if (iph->protocol == IPPROTO_ICMP)
14230388b004SPavel Emelyanov 		icmp_out_count(net, ((struct icmphdr *)
142496793b48SDavid L Stevens 			skb_transport_header(skb))->type);
142596793b48SDavid L Stevens 
14261c32c5adSHerbert Xu 	ip_cork_release(cork);
14271c32c5adSHerbert Xu out:
14281c32c5adSHerbert Xu 	return skb;
14291c32c5adSHerbert Xu }
14301c32c5adSHerbert Xu 
1431b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb)
14321c32c5adSHerbert Xu {
14331c32c5adSHerbert Xu 	int err;
14341c32c5adSHerbert Xu 
1435c439cb2eSHerbert Xu 	err = ip_local_out(skb);
14361da177e4SLinus Torvalds 	if (err) {
14371da177e4SLinus Torvalds 		if (err > 0)
14386ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
14391da177e4SLinus Torvalds 		if (err)
14401c32c5adSHerbert Xu 			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	return err;
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
144677968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
14471470ddf7SHerbert Xu {
14481c32c5adSHerbert Xu 	struct sk_buff *skb;
14491c32c5adSHerbert Xu 
145077968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
14511c32c5adSHerbert Xu 	if (!skb)
14521c32c5adSHerbert Xu 		return 0;
14531c32c5adSHerbert Xu 
14541c32c5adSHerbert Xu 	/* Netfilter gets whole the not fragmented skb. */
1455b5ec8eeaSEric Dumazet 	return ip_send_skb(sock_net(sk), skb);
14561470ddf7SHerbert Xu }
14571470ddf7SHerbert Xu 
14581da177e4SLinus Torvalds /*
14591da177e4SLinus Torvalds  *	Throw away all pending data on the socket.
14601da177e4SLinus Torvalds  */
14611470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk,
14621470ddf7SHerbert Xu 				      struct sk_buff_head *queue,
14631470ddf7SHerbert Xu 				      struct inet_cork *cork)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds 	struct sk_buff *skb;
14661da177e4SLinus Torvalds 
14671470ddf7SHerbert Xu 	while ((skb = __skb_dequeue_tail(queue)) != NULL)
14681da177e4SLinus Torvalds 		kfree_skb(skb);
14691da177e4SLinus Torvalds 
14701470ddf7SHerbert Xu 	ip_cork_release(cork);
14711470ddf7SHerbert Xu }
14721470ddf7SHerbert Xu 
14731470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk)
14741470ddf7SHerbert Xu {
1475bdc712b4SDavid S. Miller 	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
14781c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk,
147977968b78SDavid S. Miller 			    struct flowi4 *fl4,
14801c32c5adSHerbert Xu 			    int getfrag(void *from, char *to, int offset,
14811c32c5adSHerbert Xu 					int len, int odd, struct sk_buff *skb),
14821c32c5adSHerbert Xu 			    void *from, int length, int transhdrlen,
14831c32c5adSHerbert Xu 			    struct ipcm_cookie *ipc, struct rtable **rtp,
14841c32c5adSHerbert Xu 			    unsigned int flags)
14851c32c5adSHerbert Xu {
1486b80d7226SDavid S. Miller 	struct inet_cork cork;
14871c32c5adSHerbert Xu 	struct sk_buff_head queue;
14881c32c5adSHerbert Xu 	int err;
14891c32c5adSHerbert Xu 
14901c32c5adSHerbert Xu 	if (flags & MSG_PROBE)
14911c32c5adSHerbert Xu 		return NULL;
14921c32c5adSHerbert Xu 
14931c32c5adSHerbert Xu 	__skb_queue_head_init(&queue);
14941c32c5adSHerbert Xu 
1495b80d7226SDavid S. Miller 	cork.flags = 0;
1496b80d7226SDavid S. Miller 	cork.addr = 0;
149770652728SDavid S. Miller 	cork.opt = NULL;
14981c32c5adSHerbert Xu 	err = ip_setup_cork(sk, &cork, ipc, rtp);
14991c32c5adSHerbert Xu 	if (err)
15001c32c5adSHerbert Xu 		return ERR_PTR(err);
15011c32c5adSHerbert Xu 
15025640f768SEric Dumazet 	err = __ip_append_data(sk, fl4, &queue, &cork,
15035640f768SEric Dumazet 			       &current->task_frag, getfrag,
15041c32c5adSHerbert Xu 			       from, length, transhdrlen, flags);
15051c32c5adSHerbert Xu 	if (err) {
15061c32c5adSHerbert Xu 		__ip_flush_pending_frames(sk, &queue, &cork);
15071c32c5adSHerbert Xu 		return ERR_PTR(err);
15081c32c5adSHerbert Xu 	}
15091c32c5adSHerbert Xu 
151077968b78SDavid S. Miller 	return __ip_make_skb(sk, fl4, &queue, &cork);
15111c32c5adSHerbert Xu }
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds /*
15141da177e4SLinus Torvalds  *	Fetch data from kernel space and fill in checksum if needed.
15151da177e4SLinus Torvalds  */
15161da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset,
15171da177e4SLinus Torvalds 			      int len, int odd, struct sk_buff *skb)
15181da177e4SLinus Torvalds {
15195084205fSAl Viro 	__wsum csum;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
15221da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
15231da177e4SLinus Torvalds 	return 0;
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds /*
15271da177e4SLinus Torvalds  *	Generic function to send a packet as reply to another packet.
1528be9f4a44SEric Dumazet  *	Used to send some TCP resets/acks so far.
15291da177e4SLinus Torvalds  */
1530bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
153124a2d43dSEric Dumazet 			   const struct ip_options *sopt,
153224a2d43dSEric Dumazet 			   __be32 daddr, __be32 saddr,
153324a2d43dSEric Dumazet 			   const struct ip_reply_arg *arg,
153470e73416SDavid S. Miller 			   unsigned int len)
15351da177e4SLinus Torvalds {
1536f6d8bd05SEric Dumazet 	struct ip_options_data replyopts;
15371da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
153877968b78SDavid S. Miller 	struct flowi4 fl4;
1539511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
1540bdbbb852SEric Dumazet 	struct net *net = sock_net(sk);
1541be9f4a44SEric Dumazet 	struct sk_buff *nskb;
15424062090eSVasily Averin 	int err;
15431da177e4SLinus Torvalds 
154424a2d43dSEric Dumazet 	if (__ip_options_echo(&replyopts.opt.opt, skb, sopt))
15451da177e4SLinus Torvalds 		return;
15461da177e4SLinus Torvalds 
15470a5ebb80SDavid S. Miller 	ipc.addr = daddr;
15481da177e4SLinus Torvalds 	ipc.opt = NULL;
15492244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
1550aa661581SFrancesco Fusco 	ipc.ttl = 0;
1551aa661581SFrancesco Fusco 	ipc.tos = -1;
15521da177e4SLinus Torvalds 
1553f6d8bd05SEric Dumazet 	if (replyopts.opt.opt.optlen) {
15541da177e4SLinus Torvalds 		ipc.opt = &replyopts.opt;
15551da177e4SLinus Torvalds 
1556f6d8bd05SEric Dumazet 		if (replyopts.opt.opt.srr)
1557f6d8bd05SEric Dumazet 			daddr = replyopts.opt.opt.faddr;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 
1560e110861fSLorenzo Colitti 	flowi4_init_output(&fl4, arg->bound_dev_if,
1561e110861fSLorenzo Colitti 			   IP4_REPLY_MARK(net, skb->mark),
156266b13d99SEric Dumazet 			   RT_TOS(arg->tos),
1563be9f4a44SEric Dumazet 			   RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1564538de0e0SDavid S. Miller 			   ip_reply_arg_flowi_flags(arg),
156570e73416SDavid S. Miller 			   daddr, saddr,
1566538de0e0SDavid S. Miller 			   tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
15679d6ec938SDavid S. Miller 	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1568be9f4a44SEric Dumazet 	rt = ip_route_output_key(net, &fl4);
1569b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
15701da177e4SLinus Torvalds 		return;
15711da177e4SLinus Torvalds 
1572bdbbb852SEric Dumazet 	inet_sk(sk)->tos = arg->tos;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	sk->sk_priority = skb->priority;
1575eddc9ec5SArnaldo Carvalho de Melo 	sk->sk_protocol = ip_hdr(skb)->protocol;
1576f0e48dbfSPatrick McHardy 	sk->sk_bound_dev_if = arg->bound_dev_if;
1577be9f4a44SEric Dumazet 	sk->sk_sndbuf = sysctl_wmem_default;
15784062090eSVasily Averin 	err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
15794062090eSVasily Averin 			     len, 0, &ipc, &rt, MSG_DONTWAIT);
15804062090eSVasily Averin 	if (unlikely(err)) {
15814062090eSVasily Averin 		ip_flush_pending_frames(sk);
15824062090eSVasily Averin 		goto out;
15834062090eSVasily Averin 	}
15844062090eSVasily Averin 
1585be9f4a44SEric Dumazet 	nskb = skb_peek(&sk->sk_write_queue);
1586be9f4a44SEric Dumazet 	if (nskb) {
15871da177e4SLinus Torvalds 		if (arg->csumoffset >= 0)
1588be9f4a44SEric Dumazet 			*((__sum16 *)skb_transport_header(nskb) +
1589be9f4a44SEric Dumazet 			  arg->csumoffset) = csum_fold(csum_add(nskb->csum,
15909c70220bSArnaldo Carvalho de Melo 								arg->csum));
1591be9f4a44SEric Dumazet 		nskb->ip_summed = CHECKSUM_NONE;
1592be9f4a44SEric Dumazet 		skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb));
159377968b78SDavid S. Miller 		ip_push_pending_frames(sk, &fl4);
15941da177e4SLinus Torvalds 	}
15954062090eSVasily Averin out:
15961da177e4SLinus Torvalds 	ip_rt_put(rt);
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds void __init ip_init(void)
16001da177e4SLinus Torvalds {
16011da177e4SLinus Torvalds 	ip_rt_init();
16021da177e4SLinus Torvalds 	inet_initpeers();
16031da177e4SLinus Torvalds 
160472c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST)
160572c1d3bdSWANG Cong 	igmp_mc_init();
16061da177e4SLinus Torvalds #endif
16071da177e4SLinus Torvalds }
1608