xref: /linux/net/ipv4/ip_output.c (revision f6d8bd051c391c1c0458a30b2a7abcd939329259)
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 <asm/system.h>
471da177e4SLinus Torvalds #include <linux/module.h>
481da177e4SLinus Torvalds #include <linux/types.h>
491da177e4SLinus Torvalds #include <linux/kernel.h>
501da177e4SLinus Torvalds #include <linux/mm.h>
511da177e4SLinus Torvalds #include <linux/string.h>
521da177e4SLinus Torvalds #include <linux/errno.h>
53a1f8e7f7SAl Viro #include <linux/highmem.h>
545a0e3ad6STejun Heo #include <linux/slab.h>
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include <linux/socket.h>
571da177e4SLinus Torvalds #include <linux/sockios.h>
581da177e4SLinus Torvalds #include <linux/in.h>
591da177e4SLinus Torvalds #include <linux/inet.h>
601da177e4SLinus Torvalds #include <linux/netdevice.h>
611da177e4SLinus Torvalds #include <linux/etherdevice.h>
621da177e4SLinus Torvalds #include <linux/proc_fs.h>
631da177e4SLinus Torvalds #include <linux/stat.h>
641da177e4SLinus Torvalds #include <linux/init.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <net/snmp.h>
671da177e4SLinus Torvalds #include <net/ip.h>
681da177e4SLinus Torvalds #include <net/protocol.h>
691da177e4SLinus Torvalds #include <net/route.h>
70cfacb057SPatrick McHardy #include <net/xfrm.h>
711da177e4SLinus Torvalds #include <linux/skbuff.h>
721da177e4SLinus Torvalds #include <net/sock.h>
731da177e4SLinus Torvalds #include <net/arp.h>
741da177e4SLinus Torvalds #include <net/icmp.h>
751da177e4SLinus Torvalds #include <net/checksum.h>
761da177e4SLinus Torvalds #include <net/inetpeer.h>
771da177e4SLinus Torvalds #include <linux/igmp.h>
781da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
791da177e4SLinus Torvalds #include <linux/netfilter_bridge.h>
801da177e4SLinus Torvalds #include <linux/mroute.h>
811da177e4SLinus Torvalds #include <linux/netlink.h>
826cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h>
831da177e4SLinus Torvalds 
84ab32ea5dSBrian Haley int sysctl_ip_default_ttl __read_mostly = IPDEFTTL;
85323e126fSDavid S. Miller EXPORT_SYMBOL(sysctl_ip_default_ttl);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */
881da177e4SLinus Torvalds __inline__ void ip_send_check(struct iphdr *iph)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	iph->check = 0;
911da177e4SLinus Torvalds 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
921da177e4SLinus Torvalds }
934bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_send_check);
941da177e4SLinus Torvalds 
95c439cb2eSHerbert Xu int __ip_local_out(struct sk_buff *skb)
96c439cb2eSHerbert Xu {
97c439cb2eSHerbert Xu 	struct iphdr *iph = ip_hdr(skb);
98c439cb2eSHerbert Xu 
99c439cb2eSHerbert Xu 	iph->tot_len = htons(skb->len);
100c439cb2eSHerbert Xu 	ip_send_check(iph);
1019bbc768aSJan Engelhardt 	return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
1029bbc768aSJan Engelhardt 		       skb_dst(skb)->dev, dst_output);
103c439cb2eSHerbert Xu }
104c439cb2eSHerbert Xu 
105c439cb2eSHerbert Xu int ip_local_out(struct sk_buff *skb)
106c439cb2eSHerbert Xu {
107c439cb2eSHerbert Xu 	int err;
108c439cb2eSHerbert Xu 
109c439cb2eSHerbert Xu 	err = __ip_local_out(skb);
110c439cb2eSHerbert Xu 	if (likely(err == 1))
111c439cb2eSHerbert Xu 		err = dst_output(skb);
112c439cb2eSHerbert Xu 
113c439cb2eSHerbert Xu 	return err;
114c439cb2eSHerbert Xu }
115c439cb2eSHerbert Xu EXPORT_SYMBOL_GPL(ip_local_out);
116c439cb2eSHerbert Xu 
1171da177e4SLinus Torvalds /* dev_loopback_xmit for use with netfilter. */
1181da177e4SLinus Torvalds static int ip_dev_loopback_xmit(struct sk_buff *newskb)
1191da177e4SLinus Torvalds {
120459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(newskb);
121bbe735e4SArnaldo Carvalho de Melo 	__skb_pull(newskb, skb_network_offset(newskb));
1221da177e4SLinus Torvalds 	newskb->pkt_type = PACKET_LOOPBACK;
1231da177e4SLinus Torvalds 	newskb->ip_summed = CHECKSUM_UNNECESSARY;
124adf30907SEric Dumazet 	WARN_ON(!skb_dst(newskb));
125e30b38c2SEric Dumazet 	netif_rx_ni(newskb);
1261da177e4SLinus Torvalds 	return 0;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	int ttl = inet->uc_ttl;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	if (ttl < 0)
134323e126fSDavid S. Miller 		ttl = ip4_dst_hoplimit(dst);
1351da177e4SLinus Torvalds 	return ttl;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  *		Add an ip header to a skbuff and send it out.
1401da177e4SLinus Torvalds  *
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
143*f6d8bd05SEric Dumazet 			  __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
146511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
1471da177e4SLinus Torvalds 	struct iphdr *iph;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	/* Build the IP header. */
150*f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
1518856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
152eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
1531da177e4SLinus Torvalds 	iph->version  = 4;
1541da177e4SLinus Torvalds 	iph->ihl      = 5;
1551da177e4SLinus Torvalds 	iph->tos      = inet->tos;
156d8d1f30bSChangli Gao 	if (ip_dont_fragment(sk, &rt->dst))
1571da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
1581da177e4SLinus Torvalds 	else
1591da177e4SLinus Torvalds 		iph->frag_off = 0;
160d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
1611da177e4SLinus Torvalds 	iph->daddr    = rt->rt_dst;
1621da177e4SLinus Torvalds 	iph->saddr    = rt->rt_src;
1631da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
164d8d1f30bSChangli Gao 	ip_select_ident(iph, &rt->dst, sk);
1651da177e4SLinus Torvalds 
166*f6d8bd05SEric Dumazet 	if (opt && opt->opt.optlen) {
167*f6d8bd05SEric Dumazet 		iph->ihl += opt->opt.optlen>>2;
168*f6d8bd05SEric Dumazet 		ip_options_build(skb, &opt->opt, daddr, rt, 0);
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
1724a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/* Send it out. */
175c439cb2eSHerbert Xu 	return ip_local_out(skb);
1761da177e4SLinus Torvalds }
177d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
178d8c97a94SArnaldo Carvalho de Melo 
1791da177e4SLinus Torvalds static inline int ip_finish_output2(struct sk_buff *skb)
1801da177e4SLinus Torvalds {
181adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
18280787ebcSMitsuru Chinen 	struct rtable *rt = (struct rtable *)dst;
1831da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
184c2636b4dSChuck Lever 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
1851da177e4SLinus Torvalds 
186edf391ffSNeil Horman 	if (rt->rt_type == RTN_MULTICAST) {
187edf391ffSNeil Horman 		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len);
188edf391ffSNeil Horman 	} else if (rt->rt_type == RTN_BROADCAST)
189edf391ffSNeil Horman 		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len);
19080787ebcSMitsuru Chinen 
1911da177e4SLinus Torvalds 	/* Be paranoid, rather than too clever. */
1923b04dddeSStephen Hemminger 	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
1931da177e4SLinus Torvalds 		struct sk_buff *skb2;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
1961da177e4SLinus Torvalds 		if (skb2 == NULL) {
1971da177e4SLinus Torvalds 			kfree_skb(skb);
1981da177e4SLinus Torvalds 			return -ENOMEM;
1991da177e4SLinus Torvalds 		}
2001da177e4SLinus Torvalds 		if (skb->sk)
2011da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
2021da177e4SLinus Torvalds 		kfree_skb(skb);
2031da177e4SLinus Torvalds 		skb = skb2;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2063644f0ceSStephen Hemminger 	if (dst->hh)
2073644f0ceSStephen Hemminger 		return neigh_hh_output(dst->hh, skb);
2083644f0ceSStephen Hemminger 	else if (dst->neighbour)
2091da177e4SLinus Torvalds 		return dst->neighbour->output(skb);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (net_ratelimit())
2121da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip_finish_output2: No header cache and no neighbour!\n");
2131da177e4SLinus Torvalds 	kfree_skb(skb);
2141da177e4SLinus Torvalds 	return -EINVAL;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
217628a5c56SJohn Heffner static inline int ip_skb_dst_mtu(struct sk_buff *skb)
218628a5c56SJohn Heffner {
219628a5c56SJohn Heffner 	struct inet_sock *inet = skb->sk ? inet_sk(skb->sk) : NULL;
220628a5c56SJohn Heffner 
221628a5c56SJohn Heffner 	return (inet && inet->pmtudisc == IP_PMTUDISC_PROBE) ?
222adf30907SEric Dumazet 	       skb_dst(skb)->dev->mtu : dst_mtu(skb_dst(skb));
223628a5c56SJohn Heffner }
224628a5c56SJohn Heffner 
225861d0486SPatrick McHardy static int ip_finish_output(struct sk_buff *skb)
2261da177e4SLinus Torvalds {
2275c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
2285c901daaSPatrick McHardy 	/* Policy lookup after SNAT yielded a new policy */
229adf30907SEric Dumazet 	if (skb_dst(skb)->xfrm != NULL) {
23048d5cad8SPatrick McHardy 		IPCB(skb)->flags |= IPSKB_REROUTED;
23148d5cad8SPatrick McHardy 		return dst_output(skb);
23248d5cad8SPatrick McHardy 	}
2335c901daaSPatrick McHardy #endif
234628a5c56SJohn Heffner 	if (skb->len > ip_skb_dst_mtu(skb) && !skb_is_gso(skb))
2351bd9bef6SPatrick McHardy 		return ip_fragment(skb, ip_finish_output2);
2361bd9bef6SPatrick McHardy 	else
2371bd9bef6SPatrick McHardy 		return ip_finish_output2(skb);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds int ip_mc_output(struct sk_buff *skb)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct sock *sk = skb->sk;
243511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
244d8d1f30bSChangli Gao 	struct net_device *dev = rt->dst.dev;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/*
2471da177e4SLinus Torvalds 	 *	If the indicated interface is up and running, send the packet.
2481da177e4SLinus Torvalds 	 */
249edf391ffSNeil Horman 	IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	skb->dev = dev;
2521da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/*
2551da177e4SLinus Torvalds 	 *	Multicasts are looped back for other local users
2561da177e4SLinus Torvalds 	 */
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_MULTICAST) {
2597ad6848cSOctavian Purdila 		if (sk_mc_loop(sk)
2601da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2611da177e4SLinus Torvalds 		/* Small optimization: do not loopback not local frames,
2621da177e4SLinus Torvalds 		   which returned after forwarding; they will be  dropped
2631da177e4SLinus Torvalds 		   by ip_mr_input in any case.
2641da177e4SLinus Torvalds 		   Note, that local frames are looped back to be delivered
2651da177e4SLinus Torvalds 		   to local recipients.
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		   This check is duplicated in ip_mr_input at the moment.
2681da177e4SLinus Torvalds 		 */
2699d4fb27dSJoe Perches 		    &&
2709d4fb27dSJoe Perches 		    ((rt->rt_flags & RTCF_LOCAL) ||
2719d4fb27dSJoe Perches 		     !(IPCB(skb)->flags & IPSKB_FORWARDED))
2721da177e4SLinus Torvalds #endif
2731da177e4SLinus Torvalds 		   ) {
2741da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
2751da177e4SLinus Torvalds 			if (newskb)
2769bbc768aSJan Engelhardt 				NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
2779bbc768aSJan Engelhardt 					newskb, NULL, newskb->dev,
2781da177e4SLinus Torvalds 					ip_dev_loopback_xmit);
2791da177e4SLinus Torvalds 		}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 		/* Multicasts with ttl 0 must not go beyond the host */
2821da177e4SLinus Torvalds 
283eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->ttl == 0) {
2841da177e4SLinus Torvalds 			kfree_skb(skb);
2851da177e4SLinus Torvalds 			return 0;
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_BROADCAST) {
2901da177e4SLinus Torvalds 		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
2911da177e4SLinus Torvalds 		if (newskb)
2929bbc768aSJan Engelhardt 			NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, newskb,
2939bbc768aSJan Engelhardt 				NULL, newskb->dev, ip_dev_loopback_xmit);
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 
2969bbc768aSJan Engelhardt 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL,
2979bbc768aSJan Engelhardt 			    skb->dev, ip_finish_output,
29848d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds int ip_output(struct sk_buff *skb)
3021da177e4SLinus Torvalds {
303adf30907SEric Dumazet 	struct net_device *dev = skb_dst(skb)->dev;
3041bd9bef6SPatrick McHardy 
305edf391ffSNeil Horman 	IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
3061da177e4SLinus Torvalds 
3071bd9bef6SPatrick McHardy 	skb->dev = dev;
3081bd9bef6SPatrick McHardy 	skb->protocol = htons(ETH_P_IP);
3091bd9bef6SPatrick McHardy 
3109bbc768aSJan Engelhardt 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, dev,
31148d5cad8SPatrick McHardy 			    ip_finish_output,
31248d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3154e15ed4dSShan Wei int ip_queue_xmit(struct sk_buff *skb)
3161da177e4SLinus Torvalds {
317e89862f4SDavid S. Miller 	struct sock *sk = skb->sk;
3181da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
319*f6d8bd05SEric Dumazet 	struct ip_options_rcu *inet_opt;
3201da177e4SLinus Torvalds 	struct rtable *rt;
3211da177e4SLinus Torvalds 	struct iphdr *iph;
322ab6e3febSEric Dumazet 	int res;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* Skip all of this if the packet is already routed,
3251da177e4SLinus Torvalds 	 * f.e. by something like SCTP.
3261da177e4SLinus Torvalds 	 */
327ab6e3febSEric Dumazet 	rcu_read_lock();
328*f6d8bd05SEric Dumazet 	inet_opt = rcu_dereference(inet->inet_opt);
329511c3f92SEric Dumazet 	rt = skb_rtable(skb);
3301da177e4SLinus Torvalds 	if (rt != NULL)
3311da177e4SLinus Torvalds 		goto packet_routed;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/* Make sure we can route this packet. */
3341da177e4SLinus Torvalds 	rt = (struct rtable *)__sk_dst_check(sk, 0);
3351da177e4SLinus Torvalds 	if (rt == NULL) {
3363ca3c68eSAl Viro 		__be32 daddr;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		/* Use correct destination address if we have options. */
339c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
340*f6d8bd05SEric Dumazet 		if (inet_opt && inet_opt->opt.srr)
341*f6d8bd05SEric Dumazet 			daddr = inet_opt->opt.faddr;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 		/* If this fails, retransmit mechanism of transport layer will
3441da177e4SLinus Torvalds 		 * keep trying until route appears or the connection times
3451da177e4SLinus Torvalds 		 * itself out.
3461da177e4SLinus Torvalds 		 */
34778fbfd8aSDavid S. Miller 		rt = ip_route_output_ports(sock_net(sk), sk,
34878fbfd8aSDavid S. Miller 					   daddr, inet->inet_saddr,
34978fbfd8aSDavid S. Miller 					   inet->inet_dport,
35078fbfd8aSDavid S. Miller 					   inet->inet_sport,
35178fbfd8aSDavid S. Miller 					   sk->sk_protocol,
35278fbfd8aSDavid S. Miller 					   RT_CONN_FLAGS(sk),
35378fbfd8aSDavid S. Miller 					   sk->sk_bound_dev_if);
354b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
3551da177e4SLinus Torvalds 			goto no_route;
356d8d1f30bSChangli Gao 		sk_setup_caps(sk, &rt->dst);
3571da177e4SLinus Torvalds 	}
358d8d1f30bSChangli Gao 	skb_dst_set_noref(skb, &rt->dst);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds packet_routed:
361*f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_dst != rt->rt_gateway)
3621da177e4SLinus Torvalds 		goto no_route;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* OK, we know where to send it, allocate and build IP header. */
365*f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
3668856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
367eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
368714e85beSAl Viro 	*((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
369d8d1f30bSChangli Gao 	if (ip_dont_fragment(sk, &rt->dst) && !skb->local_df)
3701da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
3711da177e4SLinus Torvalds 	else
3721da177e4SLinus Torvalds 		iph->frag_off = 0;
373d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
3741da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
3751da177e4SLinus Torvalds 	iph->saddr    = rt->rt_src;
3761da177e4SLinus Torvalds 	iph->daddr    = rt->rt_dst;
3771da177e4SLinus Torvalds 	/* Transport layer set skb->h.foo itself. */
3781da177e4SLinus Torvalds 
379*f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.optlen) {
380*f6d8bd05SEric Dumazet 		iph->ihl += inet_opt->opt.optlen >> 2;
381*f6d8bd05SEric Dumazet 		ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
384d8d1f30bSChangli Gao 	ip_select_ident_more(iph, &rt->dst, sk,
3857967168cSHerbert Xu 			     (skb_shinfo(skb)->gso_segs ?: 1) - 1);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
3884a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
3891da177e4SLinus Torvalds 
390ab6e3febSEric Dumazet 	res = ip_local_out(skb);
391ab6e3febSEric Dumazet 	rcu_read_unlock();
392ab6e3febSEric Dumazet 	return res;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds no_route:
395ab6e3febSEric Dumazet 	rcu_read_unlock();
3965e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
3971da177e4SLinus Torvalds 	kfree_skb(skb);
3981da177e4SLinus Torvalds 	return -EHOSTUNREACH;
3991da177e4SLinus Torvalds }
4004bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
4061da177e4SLinus Torvalds 	to->priority = from->priority;
4071da177e4SLinus Torvalds 	to->protocol = from->protocol;
408adf30907SEric Dumazet 	skb_dst_drop(to);
409fe76cda3SEric Dumazet 	skb_dst_copy(to, from);
4101da177e4SLinus Torvalds 	to->dev = from->dev;
41182e91ffeSThomas Graf 	to->mark = from->mark;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	/* Copy the flags to each fragment. */
4141da177e4SLinus Torvalds 	IPCB(to)->flags = IPCB(from)->flags;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4171da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
4181da177e4SLinus Torvalds #endif
419e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
420ba9dda3aSJozsef Kadlecsik #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
421ba9dda3aSJozsef Kadlecsik     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
422ba9dda3aSJozsef Kadlecsik 	to->nf_trace = from->nf_trace;
423ba9dda3aSJozsef Kadlecsik #endif
424c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
425c98d80edSJulian Anastasov 	to->ipvs_property = from->ipvs_property;
426c98d80edSJulian Anastasov #endif
427984bc16cSJames Morris 	skb_copy_secmark(to, from);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  *	This IP datagram is too large to be sent in one piece.  Break it up into
4321da177e4SLinus Torvalds  *	smaller pieces (each of size equal to IP header plus
4331da177e4SLinus Torvalds  *	a block of the data of the original IP data part) that will yet fit in a
4341da177e4SLinus Torvalds  *	single device frame, and queue such a frame for sending.
4351da177e4SLinus Torvalds  */
4361da177e4SLinus Torvalds 
4372e2f7aefSPatrick McHardy int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	struct iphdr *iph;
4401da177e4SLinus Torvalds 	int ptr;
4411da177e4SLinus Torvalds 	struct net_device *dev;
4421da177e4SLinus Torvalds 	struct sk_buff *skb2;
443c893b806SChangli Gao 	unsigned int mtu, hlen, left, len, ll_rs;
4441da177e4SLinus Torvalds 	int offset;
44576ab608dSAlexey Dobriyan 	__be16 not_last_frag;
446511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
4471da177e4SLinus Torvalds 	int err = 0;
4481da177e4SLinus Torvalds 
449d8d1f30bSChangli Gao 	dev = rt->dst.dev;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/*
4521da177e4SLinus Torvalds 	 *	Point into the IP datagram header.
4531da177e4SLinus Torvalds 	 */
4541da177e4SLinus Torvalds 
455eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	if (unlikely((iph->frag_off & htons(IP_DF)) && !skb->local_df)) {
4585e38e270SPavel Emelyanov 		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
4591da177e4SLinus Torvalds 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
460628a5c56SJohn Heffner 			  htonl(ip_skb_dst_mtu(skb)));
4611da177e4SLinus Torvalds 		kfree_skb(skb);
4621da177e4SLinus Torvalds 		return -EMSGSIZE;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	/*
4661da177e4SLinus Torvalds 	 *	Setup starting values.
4671da177e4SLinus Torvalds 	 */
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	hlen = iph->ihl * 4;
470d8d1f30bSChangli Gao 	mtu = dst_mtu(&rt->dst) - hlen;	/* Size of data space */
4716c79bf0fSBart De Schuymer #ifdef CONFIG_BRIDGE_NETFILTER
4726c79bf0fSBart De Schuymer 	if (skb->nf_bridge)
4736c79bf0fSBart De Schuymer 		mtu -= nf_bridge_mtu_reduction(skb);
4746c79bf0fSBart De Schuymer #endif
47589cee8b1SHerbert Xu 	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* When frag_list is given, use it. First, check its validity:
4781da177e4SLinus Torvalds 	 * some transformers could create wrong frag_list or break existing
4791da177e4SLinus Torvalds 	 * one, it is not prohibited. In this case fall back to copying.
4801da177e4SLinus Torvalds 	 *
4811da177e4SLinus Torvalds 	 * LATER: this step can be merged to real generation of fragments,
4821da177e4SLinus Torvalds 	 * we can switch to copy when see the first bad fragment.
4831da177e4SLinus Torvalds 	 */
48421dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
4853d13008eSEric Dumazet 		struct sk_buff *frag, *frag2;
4861da177e4SLinus Torvalds 		int first_len = skb_pagelen(skb);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
4891da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
4901da177e4SLinus Torvalds 		    (iph->frag_off & htons(IP_MF|IP_OFFSET)) ||
4911da177e4SLinus Torvalds 		    skb_cloned(skb))
4921da177e4SLinus Torvalds 			goto slow_path;
4931da177e4SLinus Torvalds 
494d7fcf1a5SDavid S. Miller 		skb_walk_frags(skb, frag) {
4951da177e4SLinus Torvalds 			/* Correct geometry. */
4961da177e4SLinus Torvalds 			if (frag->len > mtu ||
4971da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
4981da177e4SLinus Torvalds 			    skb_headroom(frag) < hlen)
4993d13008eSEric Dumazet 				goto slow_path_clean;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 			/* Partially cloned skb? */
5021da177e4SLinus Torvalds 			if (skb_shared(frag))
5033d13008eSEric Dumazet 				goto slow_path_clean;
5042fdba6b0SHerbert Xu 
5052fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
5062fdba6b0SHerbert Xu 			if (skb->sk) {
5072fdba6b0SHerbert Xu 				frag->sk = skb->sk;
5082fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
5092fdba6b0SHerbert Xu 			}
5103d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 		/* Everything is OK. Generate! */
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 		err = 0;
5161da177e4SLinus Torvalds 		offset = 0;
5171da177e4SLinus Torvalds 		frag = skb_shinfo(skb)->frag_list;
518d7fcf1a5SDavid S. Miller 		skb_frag_list_init(skb);
5191da177e4SLinus Torvalds 		skb->data_len = first_len - skb_headlen(skb);
5201da177e4SLinus Torvalds 		skb->len = first_len;
5211da177e4SLinus Torvalds 		iph->tot_len = htons(first_len);
5221da177e4SLinus Torvalds 		iph->frag_off = htons(IP_MF);
5231da177e4SLinus Torvalds 		ip_send_check(iph);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		for (;;) {
5261da177e4SLinus Torvalds 			/* Prepare header of the next frame,
5271da177e4SLinus Torvalds 			 * before previous one went down. */
5281da177e4SLinus Torvalds 			if (frag) {
5291da177e4SLinus Torvalds 				frag->ip_summed = CHECKSUM_NONE;
530badff6d0SArnaldo Carvalho de Melo 				skb_reset_transport_header(frag);
531e2d1bca7SArnaldo Carvalho de Melo 				__skb_push(frag, hlen);
532e2d1bca7SArnaldo Carvalho de Melo 				skb_reset_network_header(frag);
533d56f90a7SArnaldo Carvalho de Melo 				memcpy(skb_network_header(frag), iph, hlen);
534eddc9ec5SArnaldo Carvalho de Melo 				iph = ip_hdr(frag);
5351da177e4SLinus Torvalds 				iph->tot_len = htons(frag->len);
5361da177e4SLinus Torvalds 				ip_copy_metadata(frag, skb);
5371da177e4SLinus Torvalds 				if (offset == 0)
5381da177e4SLinus Torvalds 					ip_options_fragment(frag);
5391da177e4SLinus Torvalds 				offset += skb->len - hlen;
5401da177e4SLinus Torvalds 				iph->frag_off = htons(offset>>3);
5411da177e4SLinus Torvalds 				if (frag->next != NULL)
5421da177e4SLinus Torvalds 					iph->frag_off |= htons(IP_MF);
5431da177e4SLinus Torvalds 				/* Ready, complete checksum */
5441da177e4SLinus Torvalds 				ip_send_check(iph);
5451da177e4SLinus Torvalds 			}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 			err = output(skb);
5481da177e4SLinus Torvalds 
549dafee490SWei Dong 			if (!err)
5505e38e270SPavel Emelyanov 				IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
5511da177e4SLinus Torvalds 			if (err || !frag)
5521da177e4SLinus Torvalds 				break;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 			skb = frag;
5551da177e4SLinus Torvalds 			frag = skb->next;
5561da177e4SLinus Torvalds 			skb->next = NULL;
5571da177e4SLinus Torvalds 		}
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 		if (err == 0) {
5605e38e270SPavel Emelyanov 			IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
5611da177e4SLinus Torvalds 			return 0;
5621da177e4SLinus Torvalds 		}
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 		while (frag) {
5651da177e4SLinus Torvalds 			skb = frag->next;
5661da177e4SLinus Torvalds 			kfree_skb(frag);
5671da177e4SLinus Torvalds 			frag = skb;
5681da177e4SLinus Torvalds 		}
5695e38e270SPavel Emelyanov 		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
5701da177e4SLinus Torvalds 		return err;
5713d13008eSEric Dumazet 
5723d13008eSEric Dumazet slow_path_clean:
5733d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
5743d13008eSEric Dumazet 			if (frag2 == frag)
5753d13008eSEric Dumazet 				break;
5763d13008eSEric Dumazet 			frag2->sk = NULL;
5773d13008eSEric Dumazet 			frag2->destructor = NULL;
5783d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
5793d13008eSEric Dumazet 		}
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds slow_path:
5831da177e4SLinus Torvalds 	left = skb->len - hlen;		/* Space per frame */
58449085bd7SGeorge Kadianakis 	ptr = hlen;		/* Where to start from */
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	/* for bridged IP traffic encapsulated inside f.e. a vlan header,
5879bcfcaf5SStephen Hemminger 	 * we need to make room for the encapsulating header
5889bcfcaf5SStephen Hemminger 	 */
589c893b806SChangli Gao 	ll_rs = LL_RESERVED_SPACE_EXTRA(rt->dst.dev, nf_bridge_pad(skb));
5909bcfcaf5SStephen Hemminger 
5911da177e4SLinus Torvalds 	/*
5921da177e4SLinus Torvalds 	 *	Fragment the datagram.
5931da177e4SLinus Torvalds 	 */
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
5961da177e4SLinus Torvalds 	not_last_frag = iph->frag_off & htons(IP_MF);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/*
5991da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
6001da177e4SLinus Torvalds 	 */
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	while (left > 0) {
6031da177e4SLinus Torvalds 		len = left;
6041da177e4SLinus Torvalds 		/* IF: it doesn't fit, use 'mtu' - the data space left */
6051da177e4SLinus Torvalds 		if (len > mtu)
6061da177e4SLinus Torvalds 			len = mtu;
6071da177e4SLinus Torvalds 		/* IF: we are not sending up to and including the packet end
6081da177e4SLinus Torvalds 		   then align the next start on an eight byte boundary */
6091da177e4SLinus Torvalds 		if (len < left)	{
6101da177e4SLinus Torvalds 			len &= ~7;
6111da177e4SLinus Torvalds 		}
6121da177e4SLinus Torvalds 		/*
6131da177e4SLinus Torvalds 		 *	Allocate buffer.
6141da177e4SLinus Torvalds 		 */
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 		if ((skb2 = alloc_skb(len+hlen+ll_rs, GFP_ATOMIC)) == NULL) {
61764ce2073SPatrick McHardy 			NETDEBUG(KERN_INFO "IP: frag: no memory for new fragment!\n");
6181da177e4SLinus Torvalds 			err = -ENOMEM;
6191da177e4SLinus Torvalds 			goto fail;
6201da177e4SLinus Torvalds 		}
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 		/*
6231da177e4SLinus Torvalds 		 *	Set up data on packet
6241da177e4SLinus Torvalds 		 */
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		ip_copy_metadata(skb2, skb);
6271da177e4SLinus Torvalds 		skb_reserve(skb2, ll_rs);
6281da177e4SLinus Torvalds 		skb_put(skb2, len + hlen);
629c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
630b0e380b1SArnaldo Carvalho de Melo 		skb2->transport_header = skb2->network_header + hlen;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 		/*
6331da177e4SLinus Torvalds 		 *	Charge the memory for the fragment to any owner
6341da177e4SLinus Torvalds 		 *	it might possess
6351da177e4SLinus Torvalds 		 */
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 		if (skb->sk)
6381da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 		/*
6411da177e4SLinus Torvalds 		 *	Copy the packet header into the new buffer.
6421da177e4SLinus Torvalds 		 */
6431da177e4SLinus Torvalds 
644d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 		/*
6471da177e4SLinus Torvalds 		 *	Copy a block of the IP datagram.
6481da177e4SLinus Torvalds 		 */
649bff9b61cSArnaldo Carvalho de Melo 		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
6501da177e4SLinus Torvalds 			BUG();
6511da177e4SLinus Torvalds 		left -= len;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 		/*
6541da177e4SLinus Torvalds 		 *	Fill in the new header fields.
6551da177e4SLinus Torvalds 		 */
656eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
6571da177e4SLinus Torvalds 		iph->frag_off = htons((offset >> 3));
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		/* ANK: dirty, but effective trick. Upgrade options only if
6601da177e4SLinus Torvalds 		 * the segment to be fragmented was THE FIRST (otherwise,
6611da177e4SLinus Torvalds 		 * options are already fixed) and make it ONCE
6621da177e4SLinus Torvalds 		 * on the initial skb, so that all the following fragments
6631da177e4SLinus Torvalds 		 * will inherit fixed options.
6641da177e4SLinus Torvalds 		 */
6651da177e4SLinus Torvalds 		if (offset == 0)
6661da177e4SLinus Torvalds 			ip_options_fragment(skb);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 		/*
6691da177e4SLinus Torvalds 		 *	Added AC : If we are fragmenting a fragment that's not the
6701da177e4SLinus Torvalds 		 *		   last fragment then keep MF on each bit
6711da177e4SLinus Torvalds 		 */
6721da177e4SLinus Torvalds 		if (left > 0 || not_last_frag)
6731da177e4SLinus Torvalds 			iph->frag_off |= htons(IP_MF);
6741da177e4SLinus Torvalds 		ptr += len;
6751da177e4SLinus Torvalds 		offset += len;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		/*
6781da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
6791da177e4SLinus Torvalds 		 */
6801da177e4SLinus Torvalds 		iph->tot_len = htons(len + hlen);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 		ip_send_check(iph);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 		err = output(skb2);
6851da177e4SLinus Torvalds 		if (err)
6861da177e4SLinus Torvalds 			goto fail;
687dafee490SWei Dong 
6885e38e270SPavel Emelyanov 		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 	kfree_skb(skb);
6915e38e270SPavel Emelyanov 	IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
6921da177e4SLinus Torvalds 	return err;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds fail:
6951da177e4SLinus Torvalds 	kfree_skb(skb);
6965e38e270SPavel Emelyanov 	IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
6971da177e4SLinus Torvalds 	return err;
6981da177e4SLinus Torvalds }
6992e2f7aefSPatrick McHardy EXPORT_SYMBOL(ip_fragment);
7002e2f7aefSPatrick McHardy 
7011da177e4SLinus Torvalds int
7021da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	struct iovec *iov = from;
7051da177e4SLinus Torvalds 
70684fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
7071da177e4SLinus Torvalds 		if (memcpy_fromiovecend(to, iov, offset, len) < 0)
7081da177e4SLinus Torvalds 			return -EFAULT;
7091da177e4SLinus Torvalds 	} else {
71044bb9363SAl Viro 		__wsum csum = 0;
7111da177e4SLinus Torvalds 		if (csum_partial_copy_fromiovecend(to, iov, offset, len, &csum) < 0)
7121da177e4SLinus Torvalds 			return -EFAULT;
7131da177e4SLinus Torvalds 		skb->csum = csum_block_add(skb->csum, csum, odd);
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 	return 0;
7161da177e4SLinus Torvalds }
7174bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag);
7181da177e4SLinus Torvalds 
71944bb9363SAl Viro static inline __wsum
7201da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	char *kaddr;
72344bb9363SAl Viro 	__wsum csum;
7241da177e4SLinus Torvalds 	kaddr = kmap(page);
7251da177e4SLinus Torvalds 	csum = csum_partial(kaddr + offset, copy, 0);
7261da177e4SLinus Torvalds 	kunmap(page);
7271da177e4SLinus Torvalds 	return csum;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7304b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk,
7311470ddf7SHerbert Xu 			struct sk_buff_head *queue,
732e89e9cf5SAnanda Raju 			int getfrag(void *from, char *to, int offset, int len,
733e89e9cf5SAnanda Raju 			       int odd, struct sk_buff *skb),
734e89e9cf5SAnanda Raju 			void *from, int length, int hh_len, int fragheaderlen,
735e89e9cf5SAnanda Raju 			int transhdrlen, int mtu, unsigned int flags)
736e89e9cf5SAnanda Raju {
737e89e9cf5SAnanda Raju 	struct sk_buff *skb;
738e89e9cf5SAnanda Raju 	int err;
739e89e9cf5SAnanda Raju 
740e89e9cf5SAnanda Raju 	/* There is support for UDP fragmentation offload by network
741e89e9cf5SAnanda Raju 	 * device, so create one single skb packet containing complete
742e89e9cf5SAnanda Raju 	 * udp datagram
743e89e9cf5SAnanda Raju 	 */
7441470ddf7SHerbert Xu 	if ((skb = skb_peek_tail(queue)) == NULL) {
745e89e9cf5SAnanda Raju 		skb = sock_alloc_send_skb(sk,
746e89e9cf5SAnanda Raju 			hh_len + fragheaderlen + transhdrlen + 20,
747e89e9cf5SAnanda Raju 			(flags & MSG_DONTWAIT), &err);
748e89e9cf5SAnanda Raju 
749e89e9cf5SAnanda Raju 		if (skb == NULL)
750e89e9cf5SAnanda Raju 			return err;
751e89e9cf5SAnanda Raju 
752e89e9cf5SAnanda Raju 		/* reserve space for Hardware header */
753e89e9cf5SAnanda Raju 		skb_reserve(skb, hh_len);
754e89e9cf5SAnanda Raju 
755e89e9cf5SAnanda Raju 		/* create space for UDP/IP header */
756e89e9cf5SAnanda Raju 		skb_put(skb, fragheaderlen + transhdrlen);
757e89e9cf5SAnanda Raju 
758e89e9cf5SAnanda Raju 		/* initialize network header pointer */
759c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
760e89e9cf5SAnanda Raju 
761e89e9cf5SAnanda Raju 		/* initialize protocol header pointer */
762b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header = skb->network_header + fragheaderlen;
763e89e9cf5SAnanda Raju 
76484fa7933SPatrick McHardy 		skb->ip_summed = CHECKSUM_PARTIAL;
765e89e9cf5SAnanda Raju 		skb->csum = 0;
766e89e9cf5SAnanda Raju 
767e89e9cf5SAnanda Raju 		/* specify the length of each IP datagram fragment */
7687967168cSHerbert Xu 		skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
769f83ef8c0SHerbert Xu 		skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
7701470ddf7SHerbert Xu 		__skb_queue_tail(queue, skb);
771e89e9cf5SAnanda Raju 	}
772be9164e7SKostya B 
773be9164e7SKostya B 	return skb_append_datato_frags(sk, skb, getfrag, from,
774be9164e7SKostya B 				       (length - transhdrlen));
775e89e9cf5SAnanda Raju }
776e89e9cf5SAnanda Raju 
7771470ddf7SHerbert Xu static int __ip_append_data(struct sock *sk, struct sk_buff_head *queue,
7781470ddf7SHerbert Xu 			    struct inet_cork *cork,
7791470ddf7SHerbert Xu 			    int getfrag(void *from, char *to, int offset,
7801470ddf7SHerbert Xu 					int len, int odd, struct sk_buff *skb),
7811da177e4SLinus Torvalds 			    void *from, int length, int transhdrlen,
7821da177e4SLinus Torvalds 			    unsigned int flags)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7851da177e4SLinus Torvalds 	struct sk_buff *skb;
7861da177e4SLinus Torvalds 
78707df5294SHerbert Xu 	struct ip_options *opt = cork->opt;
7881da177e4SLinus Torvalds 	int hh_len;
7891da177e4SLinus Torvalds 	int exthdrlen;
7901da177e4SLinus Torvalds 	int mtu;
7911da177e4SLinus Torvalds 	int copy;
7921da177e4SLinus Torvalds 	int err;
7931da177e4SLinus Torvalds 	int offset = 0;
7941da177e4SLinus Torvalds 	unsigned int maxfraglen, fragheaderlen;
7951da177e4SLinus Torvalds 	int csummode = CHECKSUM_NONE;
7961470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
7971da177e4SLinus Torvalds 
7981470ddf7SHerbert Xu 	exthdrlen = transhdrlen ? rt->dst.header_len : 0;
7991da177e4SLinus Torvalds 	length += exthdrlen;
8001da177e4SLinus Torvalds 	transhdrlen += exthdrlen;
80107df5294SHerbert Xu 	mtu = cork->fragsize;
8021470ddf7SHerbert Xu 
803d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
8061da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
8071da177e4SLinus Torvalds 
8081470ddf7SHerbert Xu 	if (cork->length + length > 0xFFFF - fragheaderlen) {
809c720c7e8SEric Dumazet 		ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->inet_dport,
810c720c7e8SEric Dumazet 			       mtu-exthdrlen);
8111da177e4SLinus Torvalds 		return -EMSGSIZE;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	/*
8151da177e4SLinus Torvalds 	 * transhdrlen > 0 means that this is the first fragment and we wish
8161da177e4SLinus Torvalds 	 * it won't be fragmented in the future.
8171da177e4SLinus Torvalds 	 */
8181da177e4SLinus Torvalds 	if (transhdrlen &&
8191da177e4SLinus Torvalds 	    length + fragheaderlen <= mtu &&
820d8d1f30bSChangli Gao 	    rt->dst.dev->features & NETIF_F_V4_CSUM &&
8211da177e4SLinus Torvalds 	    !exthdrlen)
82284fa7933SPatrick McHardy 		csummode = CHECKSUM_PARTIAL;
8231da177e4SLinus Torvalds 
8241470ddf7SHerbert Xu 	skb = skb_peek_tail(queue);
82526cde9f7SHerbert Xu 
8261470ddf7SHerbert Xu 	cork->length += length;
82726cde9f7SHerbert Xu 	if (((length > mtu) || (skb && skb_is_gso(skb))) &&
828be9164e7SKostya B 	    (sk->sk_protocol == IPPROTO_UDP) &&
829d8d1f30bSChangli Gao 	    (rt->dst.dev->features & NETIF_F_UFO)) {
8301470ddf7SHerbert Xu 		err = ip_ufo_append_data(sk, queue, getfrag, from, length,
8311470ddf7SHerbert Xu 					 hh_len, fragheaderlen, transhdrlen,
8321470ddf7SHerbert Xu 					 mtu, flags);
833baa829d8SPatrick McHardy 		if (err)
834e89e9cf5SAnanda Raju 			goto error;
835e89e9cf5SAnanda Raju 		return 0;
836e89e9cf5SAnanda Raju 	}
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	/* So, what's going on in the loop below?
8391da177e4SLinus Torvalds 	 *
8401da177e4SLinus Torvalds 	 * We use calculated fragment length to generate chained skb,
8411da177e4SLinus Torvalds 	 * each of segments is IP fragment ready for sending to network after
8421da177e4SLinus Torvalds 	 * adding appropriate IP header.
8431da177e4SLinus Torvalds 	 */
8441da177e4SLinus Torvalds 
84526cde9f7SHerbert Xu 	if (!skb)
8461da177e4SLinus Torvalds 		goto alloc_new_skb;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	while (length > 0) {
8491da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
8501da177e4SLinus Torvalds 		copy = mtu - skb->len;
8511da177e4SLinus Torvalds 		if (copy < length)
8521da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
8531da177e4SLinus Torvalds 		if (copy <= 0) {
8541da177e4SLinus Torvalds 			char *data;
8551da177e4SLinus Torvalds 			unsigned int datalen;
8561da177e4SLinus Torvalds 			unsigned int fraglen;
8571da177e4SLinus Torvalds 			unsigned int fraggap;
8581da177e4SLinus Torvalds 			unsigned int alloclen;
8591da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
8601da177e4SLinus Torvalds alloc_new_skb:
8611da177e4SLinus Torvalds 			skb_prev = skb;
8621da177e4SLinus Torvalds 			if (skb_prev)
8631da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
8641da177e4SLinus Torvalds 			else
8651da177e4SLinus Torvalds 				fraggap = 0;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 			/*
8681da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
8691da177e4SLinus Torvalds 			 * we know we need more fragment(s).
8701da177e4SLinus Torvalds 			 */
8711da177e4SLinus Torvalds 			datalen = length + fraggap;
8721da177e4SLinus Torvalds 			if (datalen > mtu - fragheaderlen)
8731da177e4SLinus Torvalds 				datalen = maxfraglen - fragheaderlen;
8741da177e4SLinus Torvalds 			fraglen = datalen + fragheaderlen;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
877d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
8781da177e4SLinus Torvalds 				alloclen = mtu;
8791da177e4SLinus Torvalds 			else
88059104f06SEric Dumazet 				alloclen = fraglen;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 			/* The last fragment gets additional space at tail.
8831da177e4SLinus Torvalds 			 * Note, with MSG_MORE we overallocate on fragments,
8841da177e4SLinus Torvalds 			 * because we have no idea what fragment will be
8851da177e4SLinus Torvalds 			 * the last.
8861da177e4SLinus Torvalds 			 */
88759104f06SEric Dumazet 			if (datalen == length + fraggap) {
888d8d1f30bSChangli Gao 				alloclen += rt->dst.trailer_len;
88959104f06SEric Dumazet 				/* make sure mtu is not reached */
89059104f06SEric Dumazet 				if (datalen > mtu - fragheaderlen - rt->dst.trailer_len)
89159104f06SEric Dumazet 					datalen -= ALIGN(rt->dst.trailer_len, 8);
89259104f06SEric Dumazet 			}
8931da177e4SLinus Torvalds 			if (transhdrlen) {
8941da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
8951da177e4SLinus Torvalds 						alloclen + hh_len + 15,
8961da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
8971da177e4SLinus Torvalds 			} else {
8981da177e4SLinus Torvalds 				skb = NULL;
8991da177e4SLinus Torvalds 				if (atomic_read(&sk->sk_wmem_alloc) <=
9001da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
9011da177e4SLinus Torvalds 					skb = sock_wmalloc(sk,
9021da177e4SLinus Torvalds 							   alloclen + hh_len + 15, 1,
9031da177e4SLinus Torvalds 							   sk->sk_allocation);
9041da177e4SLinus Torvalds 				if (unlikely(skb == NULL))
9051da177e4SLinus Torvalds 					err = -ENOBUFS;
90651f31cabSPatrick Ohly 				else
90751f31cabSPatrick Ohly 					/* only the initial fragment is
90851f31cabSPatrick Ohly 					   time stamped */
9091470ddf7SHerbert Xu 					cork->tx_flags = 0;
9101da177e4SLinus Torvalds 			}
9111da177e4SLinus Torvalds 			if (skb == NULL)
9121da177e4SLinus Torvalds 				goto error;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 			/*
9151da177e4SLinus Torvalds 			 *	Fill in the control structures
9161da177e4SLinus Torvalds 			 */
9171da177e4SLinus Torvalds 			skb->ip_summed = csummode;
9181da177e4SLinus Torvalds 			skb->csum = 0;
9191da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
9201470ddf7SHerbert Xu 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 			/*
9231da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
9241da177e4SLinus Torvalds 			 */
9251da177e4SLinus Torvalds 			data = skb_put(skb, fraglen);
926c14d2450SArnaldo Carvalho de Melo 			skb_set_network_header(skb, exthdrlen);
927b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
928b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
9291da177e4SLinus Torvalds 			data += fragheaderlen;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 			if (fraggap) {
9321da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
9331da177e4SLinus Torvalds 					skb_prev, maxfraglen,
9341da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
9351da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
9361da177e4SLinus Torvalds 							  skb->csum);
9371da177e4SLinus Torvalds 				data += fraggap;
938e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
9391da177e4SLinus Torvalds 			}
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 			copy = datalen - transhdrlen - fraggap;
9421da177e4SLinus Torvalds 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
9431da177e4SLinus Torvalds 				err = -EFAULT;
9441da177e4SLinus Torvalds 				kfree_skb(skb);
9451da177e4SLinus Torvalds 				goto error;
9461da177e4SLinus Torvalds 			}
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 			offset += copy;
9491da177e4SLinus Torvalds 			length -= datalen - fraggap;
9501da177e4SLinus Torvalds 			transhdrlen = 0;
9511da177e4SLinus Torvalds 			exthdrlen = 0;
9521da177e4SLinus Torvalds 			csummode = CHECKSUM_NONE;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 			/*
9551da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
9561da177e4SLinus Torvalds 			 */
9571470ddf7SHerbert Xu 			__skb_queue_tail(queue, skb);
9581da177e4SLinus Torvalds 			continue;
9591da177e4SLinus Torvalds 		}
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 		if (copy > length)
9621da177e4SLinus Torvalds 			copy = length;
9631da177e4SLinus Torvalds 
964d8d1f30bSChangli Gao 		if (!(rt->dst.dev->features&NETIF_F_SG)) {
9651da177e4SLinus Torvalds 			unsigned int off;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 			off = skb->len;
9681da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
9691da177e4SLinus Torvalds 					offset, copy, off, skb) < 0) {
9701da177e4SLinus Torvalds 				__skb_trim(skb, off);
9711da177e4SLinus Torvalds 				err = -EFAULT;
9721da177e4SLinus Torvalds 				goto error;
9731da177e4SLinus Torvalds 			}
9741da177e4SLinus Torvalds 		} else {
9751da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
9761da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
9771470ddf7SHerbert Xu 			struct page *page = cork->page;
9781470ddf7SHerbert Xu 			int off = cork->off;
9791da177e4SLinus Torvalds 			unsigned int left;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 			if (page && (left = PAGE_SIZE - off) > 0) {
9821da177e4SLinus Torvalds 				if (copy >= left)
9831da177e4SLinus Torvalds 					copy = left;
9841da177e4SLinus Torvalds 				if (page != frag->page) {
9851da177e4SLinus Torvalds 					if (i == MAX_SKB_FRAGS) {
9861da177e4SLinus Torvalds 						err = -EMSGSIZE;
9871da177e4SLinus Torvalds 						goto error;
9881da177e4SLinus Torvalds 					}
9891da177e4SLinus Torvalds 					get_page(page);
9901470ddf7SHerbert Xu 					skb_fill_page_desc(skb, i, page, off, 0);
9911da177e4SLinus Torvalds 					frag = &skb_shinfo(skb)->frags[i];
9921da177e4SLinus Torvalds 				}
9931da177e4SLinus Torvalds 			} else if (i < MAX_SKB_FRAGS) {
9941da177e4SLinus Torvalds 				if (copy > PAGE_SIZE)
9951da177e4SLinus Torvalds 					copy = PAGE_SIZE;
9961da177e4SLinus Torvalds 				page = alloc_pages(sk->sk_allocation, 0);
9971da177e4SLinus Torvalds 				if (page == NULL)  {
9981da177e4SLinus Torvalds 					err = -ENOMEM;
9991da177e4SLinus Torvalds 					goto error;
10001da177e4SLinus Torvalds 				}
10011470ddf7SHerbert Xu 				cork->page = page;
10021470ddf7SHerbert Xu 				cork->off = 0;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 				skb_fill_page_desc(skb, i, page, 0, 0);
10051da177e4SLinus Torvalds 				frag = &skb_shinfo(skb)->frags[i];
10061da177e4SLinus Torvalds 			} else {
10071da177e4SLinus Torvalds 				err = -EMSGSIZE;
10081da177e4SLinus Torvalds 				goto error;
10091da177e4SLinus Torvalds 			}
10101da177e4SLinus Torvalds 			if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
10111da177e4SLinus Torvalds 				err = -EFAULT;
10121da177e4SLinus Torvalds 				goto error;
10131da177e4SLinus Torvalds 			}
10141470ddf7SHerbert Xu 			cork->off += copy;
10151da177e4SLinus Torvalds 			frag->size += copy;
10161da177e4SLinus Torvalds 			skb->len += copy;
10171da177e4SLinus Torvalds 			skb->data_len += copy;
1018f945fa7aSHerbert Xu 			skb->truesize += copy;
1019f945fa7aSHerbert Xu 			atomic_add(copy, &sk->sk_wmem_alloc);
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 		offset += copy;
10221da177e4SLinus Torvalds 		length -= copy;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	return 0;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds error:
10281470ddf7SHerbert Xu 	cork->length -= length;
10295e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
10301da177e4SLinus Torvalds 	return err;
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
10341470ddf7SHerbert Xu 			 struct ipcm_cookie *ipc, struct rtable **rtp)
10351470ddf7SHerbert Xu {
10361470ddf7SHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
1037*f6d8bd05SEric Dumazet 	struct ip_options_rcu *opt;
10381470ddf7SHerbert Xu 	struct rtable *rt;
10391470ddf7SHerbert Xu 
10401470ddf7SHerbert Xu 	/*
10411470ddf7SHerbert Xu 	 * setup for corking.
10421470ddf7SHerbert Xu 	 */
10431470ddf7SHerbert Xu 	opt = ipc->opt;
10441470ddf7SHerbert Xu 	if (opt) {
10451470ddf7SHerbert Xu 		if (cork->opt == NULL) {
10461470ddf7SHerbert Xu 			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
10471470ddf7SHerbert Xu 					    sk->sk_allocation);
10481470ddf7SHerbert Xu 			if (unlikely(cork->opt == NULL))
10491470ddf7SHerbert Xu 				return -ENOBUFS;
10501470ddf7SHerbert Xu 		}
1051*f6d8bd05SEric Dumazet 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
10521470ddf7SHerbert Xu 		cork->flags |= IPCORK_OPT;
10531470ddf7SHerbert Xu 		cork->addr = ipc->addr;
10541470ddf7SHerbert Xu 	}
10551470ddf7SHerbert Xu 	rt = *rtp;
10561470ddf7SHerbert Xu 	if (unlikely(!rt))
10571470ddf7SHerbert Xu 		return -EFAULT;
10581470ddf7SHerbert Xu 	/*
10591470ddf7SHerbert Xu 	 * We steal reference to this route, caller should not release it
10601470ddf7SHerbert Xu 	 */
10611470ddf7SHerbert Xu 	*rtp = NULL;
10621470ddf7SHerbert Xu 	cork->fragsize = inet->pmtudisc == IP_PMTUDISC_PROBE ?
10631470ddf7SHerbert Xu 			 rt->dst.dev->mtu : dst_mtu(rt->dst.path);
10641470ddf7SHerbert Xu 	cork->dst = &rt->dst;
10651470ddf7SHerbert Xu 	cork->length = 0;
10661470ddf7SHerbert Xu 	cork->tx_flags = ipc->tx_flags;
10671470ddf7SHerbert Xu 	cork->page = NULL;
10681470ddf7SHerbert Xu 	cork->off = 0;
10691470ddf7SHerbert Xu 
10701470ddf7SHerbert Xu 	return 0;
10711470ddf7SHerbert Xu }
10721470ddf7SHerbert Xu 
10731470ddf7SHerbert Xu /*
10741470ddf7SHerbert Xu  *	ip_append_data() and ip_append_page() can make one large IP datagram
10751470ddf7SHerbert Xu  *	from many pieces of data. Each pieces will be holded on the socket
10761470ddf7SHerbert Xu  *	until ip_push_pending_frames() is called. Each piece can be a page
10771470ddf7SHerbert Xu  *	or non-page data.
10781470ddf7SHerbert Xu  *
10791470ddf7SHerbert Xu  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
10801470ddf7SHerbert Xu  *	this interface potentially.
10811470ddf7SHerbert Xu  *
10821470ddf7SHerbert Xu  *	LATER: length must be adjusted by pad at tail, when it is required.
10831470ddf7SHerbert Xu  */
10841470ddf7SHerbert Xu int ip_append_data(struct sock *sk,
10851470ddf7SHerbert Xu 		   int getfrag(void *from, char *to, int offset, int len,
10861470ddf7SHerbert Xu 			       int odd, struct sk_buff *skb),
10871470ddf7SHerbert Xu 		   void *from, int length, int transhdrlen,
10881470ddf7SHerbert Xu 		   struct ipcm_cookie *ipc, struct rtable **rtp,
10891470ddf7SHerbert Xu 		   unsigned int flags)
10901470ddf7SHerbert Xu {
10911470ddf7SHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
10921470ddf7SHerbert Xu 	int err;
10931470ddf7SHerbert Xu 
10941470ddf7SHerbert Xu 	if (flags&MSG_PROBE)
10951470ddf7SHerbert Xu 		return 0;
10961470ddf7SHerbert Xu 
10971470ddf7SHerbert Xu 	if (skb_queue_empty(&sk->sk_write_queue)) {
10981470ddf7SHerbert Xu 		err = ip_setup_cork(sk, &inet->cork, ipc, rtp);
10991470ddf7SHerbert Xu 		if (err)
11001470ddf7SHerbert Xu 			return err;
11011470ddf7SHerbert Xu 	} else {
11021470ddf7SHerbert Xu 		transhdrlen = 0;
11031470ddf7SHerbert Xu 	}
11041470ddf7SHerbert Xu 
11051470ddf7SHerbert Xu 	return __ip_append_data(sk, &sk->sk_write_queue, &inet->cork, getfrag,
11061470ddf7SHerbert Xu 				from, length, transhdrlen, flags);
11071470ddf7SHerbert Xu }
11081470ddf7SHerbert Xu 
11091da177e4SLinus Torvalds ssize_t	ip_append_page(struct sock *sk, struct page *page,
11101da177e4SLinus Torvalds 		       int offset, size_t size, int flags)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11131da177e4SLinus Torvalds 	struct sk_buff *skb;
11141da177e4SLinus Torvalds 	struct rtable *rt;
11151da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
11161da177e4SLinus Torvalds 	int hh_len;
11171da177e4SLinus Torvalds 	int mtu;
11181da177e4SLinus Torvalds 	int len;
11191da177e4SLinus Torvalds 	int err;
11201da177e4SLinus Torvalds 	unsigned int maxfraglen, fragheaderlen, fraggap;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	if (inet->hdrincl)
11231da177e4SLinus Torvalds 		return -EPERM;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
11261da177e4SLinus Torvalds 		return 0;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue))
11291da177e4SLinus Torvalds 		return -EINVAL;
11301da177e4SLinus Torvalds 
1131c8cdaf99SYOSHIFUJI Hideaki 	rt = (struct rtable *)inet->cork.dst;
11321da177e4SLinus Torvalds 	if (inet->cork.flags & IPCORK_OPT)
11331da177e4SLinus Torvalds 		opt = inet->cork.opt;
11341da177e4SLinus Torvalds 
1135d8d1f30bSChangli Gao 	if (!(rt->dst.dev->features&NETIF_F_SG))
11361da177e4SLinus Torvalds 		return -EOPNOTSUPP;
11371da177e4SLinus Torvalds 
1138d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
11391da177e4SLinus Torvalds 	mtu = inet->cork.fragsize;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
11421da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if (inet->cork.length + size > 0xFFFF - fragheaderlen) {
1145c720c7e8SEric Dumazet 		ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->inet_dport, mtu);
11461da177e4SLinus Torvalds 		return -EMSGSIZE;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
11501da177e4SLinus Torvalds 		return -EINVAL;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	inet->cork.length += size;
115326cde9f7SHerbert Xu 	if ((size + skb->len > mtu) &&
115426cde9f7SHerbert Xu 	    (sk->sk_protocol == IPPROTO_UDP) &&
1155d8d1f30bSChangli Gao 	    (rt->dst.dev->features & NETIF_F_UFO)) {
11567967168cSHerbert Xu 		skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
1157f83ef8c0SHerbert Xu 		skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
11587967168cSHerbert Xu 	}
1159e89e9cf5SAnanda Raju 
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	while (size > 0) {
11621da177e4SLinus Torvalds 		int i;
11631da177e4SLinus Torvalds 
116489114afdSHerbert Xu 		if (skb_is_gso(skb))
1165e89e9cf5SAnanda Raju 			len = size;
1166e89e9cf5SAnanda Raju 		else {
1167e89e9cf5SAnanda Raju 
11681da177e4SLinus Torvalds 			/* Check if the remaining data fits into current packet. */
11691da177e4SLinus Torvalds 			len = mtu - skb->len;
11701da177e4SLinus Torvalds 			if (len < size)
11711da177e4SLinus Torvalds 				len = maxfraglen - skb->len;
1172e89e9cf5SAnanda Raju 		}
11731da177e4SLinus Torvalds 		if (len <= 0) {
11741da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
11751da177e4SLinus Torvalds 			int alloclen;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 			skb_prev = skb;
11781da177e4SLinus Torvalds 			fraggap = skb_prev->len - maxfraglen;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 			alloclen = fragheaderlen + hh_len + fraggap + 15;
11811da177e4SLinus Torvalds 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
11821da177e4SLinus Torvalds 			if (unlikely(!skb)) {
11831da177e4SLinus Torvalds 				err = -ENOBUFS;
11841da177e4SLinus Torvalds 				goto error;
11851da177e4SLinus Torvalds 			}
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 			/*
11881da177e4SLinus Torvalds 			 *	Fill in the control structures
11891da177e4SLinus Torvalds 			 */
11901da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_NONE;
11911da177e4SLinus Torvalds 			skb->csum = 0;
11921da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 			/*
11951da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
11961da177e4SLinus Torvalds 			 */
1197967b05f6SArnaldo Carvalho de Melo 			skb_put(skb, fragheaderlen + fraggap);
11982ca9e6f2SArnaldo Carvalho de Melo 			skb_reset_network_header(skb);
1199b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1200b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
12011da177e4SLinus Torvalds 			if (fraggap) {
1202967b05f6SArnaldo Carvalho de Melo 				skb->csum = skb_copy_and_csum_bits(skb_prev,
1203967b05f6SArnaldo Carvalho de Melo 								   maxfraglen,
12049c70220bSArnaldo Carvalho de Melo 						    skb_transport_header(skb),
1205967b05f6SArnaldo Carvalho de Melo 								   fraggap, 0);
12061da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
12071da177e4SLinus Torvalds 							  skb->csum);
1208e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
12091da177e4SLinus Torvalds 			}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 			/*
12121da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
12131da177e4SLinus Torvalds 			 */
12141da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
12151da177e4SLinus Torvalds 			continue;
12161da177e4SLinus Torvalds 		}
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		i = skb_shinfo(skb)->nr_frags;
12191da177e4SLinus Torvalds 		if (len > size)
12201da177e4SLinus Torvalds 			len = size;
12211da177e4SLinus Torvalds 		if (skb_can_coalesce(skb, i, page, offset)) {
12221da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i-1].size += len;
12231da177e4SLinus Torvalds 		} else if (i < MAX_SKB_FRAGS) {
12241da177e4SLinus Torvalds 			get_page(page);
12251da177e4SLinus Torvalds 			skb_fill_page_desc(skb, i, page, offset, len);
12261da177e4SLinus Torvalds 		} else {
12271da177e4SLinus Torvalds 			err = -EMSGSIZE;
12281da177e4SLinus Torvalds 			goto error;
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		if (skb->ip_summed == CHECKSUM_NONE) {
123244bb9363SAl Viro 			__wsum csum;
12331da177e4SLinus Torvalds 			csum = csum_page(page, offset, len);
12341da177e4SLinus Torvalds 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
12351da177e4SLinus Torvalds 		}
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 		skb->len += len;
12381da177e4SLinus Torvalds 		skb->data_len += len;
12391e34a11dSDavid S. Miller 		skb->truesize += len;
12401e34a11dSDavid S. Miller 		atomic_add(len, &sk->sk_wmem_alloc);
12411da177e4SLinus Torvalds 		offset += len;
12421da177e4SLinus Torvalds 		size -= len;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 	return 0;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds error:
12471da177e4SLinus Torvalds 	inet->cork.length -= size;
12485e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
12491da177e4SLinus Torvalds 	return err;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork)
1253429f08e9SPavel Emelyanov {
12541470ddf7SHerbert Xu 	cork->flags &= ~IPCORK_OPT;
12551470ddf7SHerbert Xu 	kfree(cork->opt);
12561470ddf7SHerbert Xu 	cork->opt = NULL;
12571470ddf7SHerbert Xu 	dst_release(cork->dst);
12581470ddf7SHerbert Xu 	cork->dst = NULL;
1259429f08e9SPavel Emelyanov }
1260429f08e9SPavel Emelyanov 
12611da177e4SLinus Torvalds /*
12621da177e4SLinus Torvalds  *	Combined all pending IP fragments on the socket as one IP datagram
12631da177e4SLinus Torvalds  *	and push them out.
12641da177e4SLinus Torvalds  */
12651c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk,
12661470ddf7SHerbert Xu 			      struct sk_buff_head *queue,
12671470ddf7SHerbert Xu 			      struct inet_cork *cork)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
12701da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
12711da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
12720388b004SPavel Emelyanov 	struct net *net = sock_net(sk);
12731da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
12741470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
12751da177e4SLinus Torvalds 	struct iphdr *iph;
127676ab608dSAlexey Dobriyan 	__be16 df = 0;
12771da177e4SLinus Torvalds 	__u8 ttl;
12781da177e4SLinus Torvalds 
12791470ddf7SHerbert Xu 	if ((skb = __skb_dequeue(queue)) == NULL)
12801da177e4SLinus Torvalds 		goto out;
12811da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1284d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1285bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
12861470ddf7SHerbert Xu 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1287cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
12881da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
12891da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
12901da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
12911da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
12921da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
12931da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
12941da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
12981da177e4SLinus Torvalds 	 * to fragment the frame generated here. No matter, what transforms
12991da177e4SLinus Torvalds 	 * how transforms change size of the packet, it will come out.
13001da177e4SLinus Torvalds 	 */
1301628a5c56SJohn Heffner 	if (inet->pmtudisc < IP_PMTUDISC_DO)
13021da177e4SLinus Torvalds 		skb->local_df = 1;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	/* DF bit is set when we want to see DF on outgoing frames.
13051da177e4SLinus Torvalds 	 * If local_df is set too, we still allow to fragment this frame
13061da177e4SLinus Torvalds 	 * locally. */
1307628a5c56SJohn Heffner 	if (inet->pmtudisc >= IP_PMTUDISC_DO ||
1308d8d1f30bSChangli Gao 	    (skb->len <= dst_mtu(&rt->dst) &&
1309d8d1f30bSChangli Gao 	     ip_dont_fragment(sk, &rt->dst)))
13101da177e4SLinus Torvalds 		df = htons(IP_DF);
13111da177e4SLinus Torvalds 
13121470ddf7SHerbert Xu 	if (cork->flags & IPCORK_OPT)
13131470ddf7SHerbert Xu 		opt = cork->opt;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	if (rt->rt_type == RTN_MULTICAST)
13161da177e4SLinus Torvalds 		ttl = inet->mc_ttl;
13171da177e4SLinus Torvalds 	else
1318d8d1f30bSChangli Gao 		ttl = ip_select_ttl(inet, &rt->dst);
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	iph = (struct iphdr *)skb->data;
13211da177e4SLinus Torvalds 	iph->version = 4;
13221da177e4SLinus Torvalds 	iph->ihl = 5;
13231da177e4SLinus Torvalds 	if (opt) {
13241da177e4SLinus Torvalds 		iph->ihl += opt->optlen>>2;
13251470ddf7SHerbert Xu 		ip_options_build(skb, opt, cork->addr, rt, 0);
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 	iph->tos = inet->tos;
13281da177e4SLinus Torvalds 	iph->frag_off = df;
1329d8d1f30bSChangli Gao 	ip_select_ident(iph, &rt->dst, sk);
13301da177e4SLinus Torvalds 	iph->ttl = ttl;
13311da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
13321da177e4SLinus Torvalds 	iph->saddr = rt->rt_src;
13331da177e4SLinus Torvalds 	iph->daddr = rt->rt_dst;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
13364a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1337a21bba94SEric Dumazet 	/*
1338a21bba94SEric Dumazet 	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1339a21bba94SEric Dumazet 	 * on dst refcount
1340a21bba94SEric Dumazet 	 */
13411470ddf7SHerbert Xu 	cork->dst = NULL;
1342d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
13431da177e4SLinus Torvalds 
134496793b48SDavid L Stevens 	if (iph->protocol == IPPROTO_ICMP)
13450388b004SPavel Emelyanov 		icmp_out_count(net, ((struct icmphdr *)
134696793b48SDavid L Stevens 			skb_transport_header(skb))->type);
134796793b48SDavid L Stevens 
13481c32c5adSHerbert Xu 	ip_cork_release(cork);
13491c32c5adSHerbert Xu out:
13501c32c5adSHerbert Xu 	return skb;
13511c32c5adSHerbert Xu }
13521c32c5adSHerbert Xu 
13531c32c5adSHerbert Xu int ip_send_skb(struct sk_buff *skb)
13541c32c5adSHerbert Xu {
13551c32c5adSHerbert Xu 	struct net *net = sock_net(skb->sk);
13561c32c5adSHerbert Xu 	int err;
13571c32c5adSHerbert Xu 
1358c439cb2eSHerbert Xu 	err = ip_local_out(skb);
13591da177e4SLinus Torvalds 	if (err) {
13601da177e4SLinus Torvalds 		if (err > 0)
13616ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
13621da177e4SLinus Torvalds 		if (err)
13631c32c5adSHerbert Xu 			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	return err;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691470ddf7SHerbert Xu int ip_push_pending_frames(struct sock *sk)
13701470ddf7SHerbert Xu {
13711c32c5adSHerbert Xu 	struct sk_buff *skb;
13721c32c5adSHerbert Xu 
13731c32c5adSHerbert Xu 	skb = ip_finish_skb(sk);
13741c32c5adSHerbert Xu 	if (!skb)
13751c32c5adSHerbert Xu 		return 0;
13761c32c5adSHerbert Xu 
13771c32c5adSHerbert Xu 	/* Netfilter gets whole the not fragmented skb. */
13781c32c5adSHerbert Xu 	return ip_send_skb(skb);
13791470ddf7SHerbert Xu }
13801470ddf7SHerbert Xu 
13811da177e4SLinus Torvalds /*
13821da177e4SLinus Torvalds  *	Throw away all pending data on the socket.
13831da177e4SLinus Torvalds  */
13841470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk,
13851470ddf7SHerbert Xu 				      struct sk_buff_head *queue,
13861470ddf7SHerbert Xu 				      struct inet_cork *cork)
13871da177e4SLinus Torvalds {
13881da177e4SLinus Torvalds 	struct sk_buff *skb;
13891da177e4SLinus Torvalds 
13901470ddf7SHerbert Xu 	while ((skb = __skb_dequeue_tail(queue)) != NULL)
13911da177e4SLinus Torvalds 		kfree_skb(skb);
13921da177e4SLinus Torvalds 
13931470ddf7SHerbert Xu 	ip_cork_release(cork);
13941470ddf7SHerbert Xu }
13951470ddf7SHerbert Xu 
13961470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk)
13971470ddf7SHerbert Xu {
13981470ddf7SHerbert Xu 	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork);
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk,
14021c32c5adSHerbert Xu 			    int getfrag(void *from, char *to, int offset,
14031c32c5adSHerbert Xu 					int len, int odd, struct sk_buff *skb),
14041c32c5adSHerbert Xu 			    void *from, int length, int transhdrlen,
14051c32c5adSHerbert Xu 			    struct ipcm_cookie *ipc, struct rtable **rtp,
14061c32c5adSHerbert Xu 			    unsigned int flags)
14071c32c5adSHerbert Xu {
14081c32c5adSHerbert Xu 	struct inet_cork cork = {};
14091c32c5adSHerbert Xu 	struct sk_buff_head queue;
14101c32c5adSHerbert Xu 	int err;
14111c32c5adSHerbert Xu 
14121c32c5adSHerbert Xu 	if (flags & MSG_PROBE)
14131c32c5adSHerbert Xu 		return NULL;
14141c32c5adSHerbert Xu 
14151c32c5adSHerbert Xu 	__skb_queue_head_init(&queue);
14161c32c5adSHerbert Xu 
14171c32c5adSHerbert Xu 	err = ip_setup_cork(sk, &cork, ipc, rtp);
14181c32c5adSHerbert Xu 	if (err)
14191c32c5adSHerbert Xu 		return ERR_PTR(err);
14201c32c5adSHerbert Xu 
14211c32c5adSHerbert Xu 	err = __ip_append_data(sk, &queue, &cork, getfrag,
14221c32c5adSHerbert Xu 			       from, length, transhdrlen, flags);
14231c32c5adSHerbert Xu 	if (err) {
14241c32c5adSHerbert Xu 		__ip_flush_pending_frames(sk, &queue, &cork);
14251c32c5adSHerbert Xu 		return ERR_PTR(err);
14261c32c5adSHerbert Xu 	}
14271c32c5adSHerbert Xu 
14281c32c5adSHerbert Xu 	return __ip_make_skb(sk, &queue, &cork);
14291c32c5adSHerbert Xu }
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds /*
14321da177e4SLinus Torvalds  *	Fetch data from kernel space and fill in checksum if needed.
14331da177e4SLinus Torvalds  */
14341da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset,
14351da177e4SLinus Torvalds 			      int len, int odd, struct sk_buff *skb)
14361da177e4SLinus Torvalds {
14375084205fSAl Viro 	__wsum csum;
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
14401da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
14411da177e4SLinus Torvalds 	return 0;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds /*
14451da177e4SLinus Torvalds  *	Generic function to send a packet as reply to another packet.
14461da177e4SLinus Torvalds  *	Used to send TCP resets so far. ICMP should use this function too.
14471da177e4SLinus Torvalds  *
14481da177e4SLinus Torvalds  *	Should run single threaded per socket because it uses the sock
14491da177e4SLinus Torvalds  *     	structure to pass arguments.
14501da177e4SLinus Torvalds  */
14511da177e4SLinus Torvalds void ip_send_reply(struct sock *sk, struct sk_buff *skb, struct ip_reply_arg *arg,
14521da177e4SLinus Torvalds 		   unsigned int len)
14531da177e4SLinus Torvalds {
14541da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1455*f6d8bd05SEric Dumazet 	struct ip_options_data replyopts;
14561da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
14573ca3c68eSAl Viro 	__be32 daddr;
1458511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
14591da177e4SLinus Torvalds 
1460*f6d8bd05SEric Dumazet 	if (ip_options_echo(&replyopts.opt.opt, skb))
14611da177e4SLinus Torvalds 		return;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	daddr = ipc.addr = rt->rt_src;
14641da177e4SLinus Torvalds 	ipc.opt = NULL;
14652244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
14661da177e4SLinus Torvalds 
1467*f6d8bd05SEric Dumazet 	if (replyopts.opt.opt.optlen) {
14681da177e4SLinus Torvalds 		ipc.opt = &replyopts.opt;
14691da177e4SLinus Torvalds 
1470*f6d8bd05SEric Dumazet 		if (replyopts.opt.opt.srr)
1471*f6d8bd05SEric Dumazet 			daddr = replyopts.opt.opt.faddr;
14721da177e4SLinus Torvalds 	}
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	{
1475538de0e0SDavid S. Miller 		struct flowi4 fl4;
1476538de0e0SDavid S. Miller 
1477538de0e0SDavid S. Miller 		flowi4_init_output(&fl4, arg->bound_dev_if, 0,
1478538de0e0SDavid S. Miller 				   RT_TOS(ip_hdr(skb)->tos),
1479538de0e0SDavid S. Miller 				   RT_SCOPE_UNIVERSE, sk->sk_protocol,
1480538de0e0SDavid S. Miller 				   ip_reply_arg_flowi_flags(arg),
1481538de0e0SDavid S. Miller 				   daddr, rt->rt_spec_dst,
1482538de0e0SDavid S. Miller 				   tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
14839d6ec938SDavid S. Miller 		security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
14849d6ec938SDavid S. Miller 		rt = ip_route_output_key(sock_net(sk), &fl4);
1485b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
14861da177e4SLinus Torvalds 			return;
14871da177e4SLinus Torvalds 	}
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	/* And let IP do all the hard work.
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	   This chunk is not reenterable, hence spinlock.
14921da177e4SLinus Torvalds 	   Note that it uses the fact, that this function is called
14931da177e4SLinus Torvalds 	   with locally disabled BH and that sk cannot be already spinlocked.
14941da177e4SLinus Torvalds 	 */
14951da177e4SLinus Torvalds 	bh_lock_sock(sk);
1496eddc9ec5SArnaldo Carvalho de Melo 	inet->tos = ip_hdr(skb)->tos;
14971da177e4SLinus Torvalds 	sk->sk_priority = skb->priority;
1498eddc9ec5SArnaldo Carvalho de Melo 	sk->sk_protocol = ip_hdr(skb)->protocol;
1499f0e48dbfSPatrick McHardy 	sk->sk_bound_dev_if = arg->bound_dev_if;
15001da177e4SLinus Torvalds 	ip_append_data(sk, ip_reply_glue_bits, arg->iov->iov_base, len, 0,
15012e77d89bSEric Dumazet 		       &ipc, &rt, MSG_DONTWAIT);
15021da177e4SLinus Torvalds 	if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
15031da177e4SLinus Torvalds 		if (arg->csumoffset >= 0)
15049c70220bSArnaldo Carvalho de Melo 			*((__sum16 *)skb_transport_header(skb) +
15059c70220bSArnaldo Carvalho de Melo 			  arg->csumoffset) = csum_fold(csum_add(skb->csum,
15069c70220bSArnaldo Carvalho de Melo 								arg->csum));
15071da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_NONE;
15081da177e4SLinus Torvalds 		ip_push_pending_frames(sk);
15091da177e4SLinus Torvalds 	}
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	bh_unlock_sock(sk);
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	ip_rt_put(rt);
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds void __init ip_init(void)
15171da177e4SLinus Torvalds {
15181da177e4SLinus Torvalds 	ip_rt_init();
15191da177e4SLinus Torvalds 	inet_initpeers();
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds #if defined(CONFIG_IP_MULTICAST) && defined(CONFIG_PROC_FS)
15221da177e4SLinus Torvalds 	igmp_mc_proc_init();
15231da177e4SLinus Torvalds #endif
15241da177e4SLinus Torvalds }
1525