xref: /openbmc/linux/net/ipv4/ip_output.c (revision 7c0f6ba682b9c7632072ffbedf8d328c8f3c42ba)
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 
45*7c0f6ba6SLinus Torvalds #include <linux/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>
7614972cbdSRoopa Prabhu #include <net/lwtunnel.h>
7733b48679SDaniel Mack #include <linux/bpf-cgroup.h>
781da177e4SLinus Torvalds #include <linux/igmp.h>
791da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
801da177e4SLinus Torvalds #include <linux/netfilter_bridge.h>
811da177e4SLinus Torvalds #include <linux/netlink.h>
826cbb0df7SArnaldo Carvalho de Melo #include <linux/tcp.h>
831da177e4SLinus Torvalds 
84694869b3SEric W. Biederman static int
85694869b3SEric W. Biederman ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
86c5501eb3SFlorian Westphal 	    unsigned int mtu,
87694869b3SEric W. Biederman 	    int (*output)(struct net *, struct sock *, struct sk_buff *));
8849d16b23SAndy 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 
97cf91a99dSEric W. Biederman int __ip_local_out(struct net *net, 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);
103a8e3e1a9SDavid Ahern 
104a8e3e1a9SDavid Ahern 	/* if egress device is enslaved to an L3 master device pass the
105a8e3e1a9SDavid Ahern 	 * skb to its handler for processing
106a8e3e1a9SDavid Ahern 	 */
107a8e3e1a9SDavid Ahern 	skb = l3mdev_ip_out(sk, skb);
108a8e3e1a9SDavid Ahern 	if (unlikely(!skb))
109a8e3e1a9SDavid Ahern 		return 0;
110a8e3e1a9SDavid Ahern 
111f4180439SEli Cooper 	skb->protocol = htons(ETH_P_IP);
112f4180439SEli Cooper 
11329a26a56SEric W. Biederman 	return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
11429a26a56SEric W. Biederman 		       net, sk, skb, NULL, skb_dst(skb)->dev,
11513206b6bSEric W. Biederman 		       dst_output);
1167026b1ddSDavid Miller }
1177026b1ddSDavid Miller 
11833224b16SEric W. Biederman int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
119c439cb2eSHerbert Xu {
120c439cb2eSHerbert Xu 	int err;
121c439cb2eSHerbert Xu 
122cf91a99dSEric W. Biederman 	err = __ip_local_out(net, sk, skb);
123c439cb2eSHerbert Xu 	if (likely(err == 1))
12413206b6bSEric W. Biederman 		err = dst_output(net, sk, skb);
125c439cb2eSHerbert Xu 
126c439cb2eSHerbert Xu 	return err;
127c439cb2eSHerbert Xu }
128e2cb77dbSEric W. Biederman EXPORT_SYMBOL_GPL(ip_local_out);
129c439cb2eSHerbert Xu 
1301da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int ttl = inet->uc_ttl;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if (ttl < 0)
135323e126fSDavid S. Miller 		ttl = ip4_dst_hoplimit(dst);
1361da177e4SLinus Torvalds 	return ttl;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  *		Add an ip header to a skbuff and send it out.
1411da177e4SLinus Torvalds  *
1421da177e4SLinus Torvalds  */
143cfe673b0SEric Dumazet int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
144f6d8bd05SEric Dumazet 			  __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
147511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
14877589ce0SEric W. Biederman 	struct net *net = sock_net(sk);
1491da177e4SLinus Torvalds 	struct iphdr *iph;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	/* Build the IP header. */
152f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
1538856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
154eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
1551da177e4SLinus Torvalds 	iph->version  = 4;
1561da177e4SLinus Torvalds 	iph->ihl      = 5;
1571da177e4SLinus Torvalds 	iph->tos      = inet->tos;
158d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
159dd927a26SDavid S. Miller 	iph->daddr    = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
160dd927a26SDavid S. Miller 	iph->saddr    = saddr;
1611da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
162cfe673b0SEric Dumazet 	if (ip_dont_fragment(sk, &rt->dst)) {
163cfe673b0SEric Dumazet 		iph->frag_off = htons(IP_DF);
164cfe673b0SEric Dumazet 		iph->id = 0;
165cfe673b0SEric Dumazet 	} else {
166cfe673b0SEric Dumazet 		iph->frag_off = 0;
16777589ce0SEric W. Biederman 		__ip_select_ident(net, iph, 1);
168cfe673b0SEric Dumazet 	}
1691da177e4SLinus Torvalds 
170f6d8bd05SEric Dumazet 	if (opt && opt->opt.optlen) {
171f6d8bd05SEric Dumazet 		iph->ihl += opt->opt.optlen>>2;
172f6d8bd05SEric Dumazet 		ip_options_build(skb, &opt->opt, daddr, rt, 0);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
1764a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/* Send it out. */
17933224b16SEric W. Biederman 	return ip_local_out(net, skb->sk, skb);
1801da177e4SLinus Torvalds }
181d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
182d8c97a94SArnaldo Carvalho de Melo 
183694869b3SEric W. Biederman static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
1841da177e4SLinus Torvalds {
185adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
18680787ebcSMitsuru Chinen 	struct rtable *rt = (struct rtable *)dst;
1871da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
188c2636b4dSChuck Lever 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
189f6b72b62SDavid S. Miller 	struct neighbour *neigh;
190a263b309SDavid S. Miller 	u32 nexthop;
1911da177e4SLinus Torvalds 
192edf391ffSNeil Horman 	if (rt->rt_type == RTN_MULTICAST) {
1934ba1bf42SEric W. Biederman 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
194edf391ffSNeil Horman 	} else if (rt->rt_type == RTN_BROADCAST)
1954ba1bf42SEric W. Biederman 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
19680787ebcSMitsuru Chinen 
1971da177e4SLinus Torvalds 	/* Be paranoid, rather than too clever. */
1983b04dddeSStephen Hemminger 	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
1991da177e4SLinus Torvalds 		struct sk_buff *skb2;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
20251456b29SIan Morris 		if (!skb2) {
2031da177e4SLinus Torvalds 			kfree_skb(skb);
2041da177e4SLinus Torvalds 			return -ENOMEM;
2051da177e4SLinus Torvalds 		}
2061da177e4SLinus Torvalds 		if (skb->sk)
2071da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
2085d0ba55bSEric Dumazet 		consume_skb(skb);
2091da177e4SLinus Torvalds 		skb = skb2;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
21214972cbdSRoopa Prabhu 	if (lwtunnel_xmit_redirect(dst->lwtstate)) {
21314972cbdSRoopa Prabhu 		int res = lwtunnel_xmit(skb);
21414972cbdSRoopa Prabhu 
21514972cbdSRoopa Prabhu 		if (res < 0 || res == LWTUNNEL_XMIT_DONE)
21614972cbdSRoopa Prabhu 			return res;
21714972cbdSRoopa Prabhu 	}
21814972cbdSRoopa Prabhu 
219a263b309SDavid S. Miller 	rcu_read_lock_bh();
220155e8336SJulian Anastasov 	nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
221a263b309SDavid S. Miller 	neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
222a263b309SDavid S. Miller 	if (unlikely(!neigh))
223a263b309SDavid S. Miller 		neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
2249871f1adSVasiliy Kulikov 	if (!IS_ERR(neigh)) {
2255110effeSDavid S. Miller 		int res = dst_neigh_output(dst, neigh, skb);
226f2c31e32SEric Dumazet 
227a263b309SDavid S. Miller 		rcu_read_unlock_bh();
228f2c31e32SEric Dumazet 		return res;
229f2c31e32SEric Dumazet 	}
230a263b309SDavid S. Miller 	rcu_read_unlock_bh();
23105e3aa09SDavid S. Miller 
232e87cc472SJoe Perches 	net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
233e87cc472SJoe Perches 			    __func__);
2341da177e4SLinus Torvalds 	kfree_skb(skb);
2351da177e4SLinus Torvalds 	return -EINVAL;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238694869b3SEric W. Biederman static int ip_finish_output_gso(struct net *net, struct sock *sk,
239694869b3SEric W. Biederman 				struct sk_buff *skb, unsigned int mtu)
240c7ba65d7SFlorian Westphal {
241c7ba65d7SFlorian Westphal 	netdev_features_t features;
242c7ba65d7SFlorian Westphal 	struct sk_buff *segs;
243c7ba65d7SFlorian Westphal 	int ret = 0;
244c7ba65d7SFlorian Westphal 
2459ee6c5dcSLance Richardson 	/* common case: seglen is <= mtu
246359ebda2SShmulik Ladkani 	 */
2479ee6c5dcSLance Richardson 	if (skb_gso_validate_mtu(skb, mtu))
248694869b3SEric W. Biederman 		return ip_finish_output2(net, sk, skb);
249c7ba65d7SFlorian Westphal 
2500ace81ecSLance Richardson 	/* Slowpath -  GSO segment length exceeds the egress MTU.
251c7ba65d7SFlorian Westphal 	 *
2520ace81ecSLance Richardson 	 * This can happen in several cases:
2530ace81ecSLance Richardson 	 *  - Forwarding of a TCP GRO skb, when DF flag is not set.
2540ace81ecSLance Richardson 	 *  - Forwarding of an skb that arrived on a virtualization interface
2550ace81ecSLance Richardson 	 *    (virtio-net/vhost/tap) with TSO/GSO size set by other network
2560ace81ecSLance Richardson 	 *    stack.
2570ace81ecSLance Richardson 	 *  - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an
2580ace81ecSLance Richardson 	 *    interface with a smaller MTU.
2590ace81ecSLance Richardson 	 *  - Arriving GRO skb (or GSO skb in a virtualized environment) that is
2600ace81ecSLance Richardson 	 *    bridged to a NETIF_F_TSO tunnel stacked over an interface with an
2610ace81ecSLance Richardson 	 *    insufficent MTU.
262c7ba65d7SFlorian Westphal 	 */
263c7ba65d7SFlorian Westphal 	features = netif_skb_features(skb);
2649207f9d4SKonstantin Khlebnikov 	BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
265c7ba65d7SFlorian Westphal 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
266330966e5SFlorian Westphal 	if (IS_ERR_OR_NULL(segs)) {
267c7ba65d7SFlorian Westphal 		kfree_skb(skb);
268c7ba65d7SFlorian Westphal 		return -ENOMEM;
269c7ba65d7SFlorian Westphal 	}
270c7ba65d7SFlorian Westphal 
271c7ba65d7SFlorian Westphal 	consume_skb(skb);
272c7ba65d7SFlorian Westphal 
273c7ba65d7SFlorian Westphal 	do {
274c7ba65d7SFlorian Westphal 		struct sk_buff *nskb = segs->next;
275c7ba65d7SFlorian Westphal 		int err;
276c7ba65d7SFlorian Westphal 
277c7ba65d7SFlorian Westphal 		segs->next = NULL;
278694869b3SEric W. Biederman 		err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
279c7ba65d7SFlorian Westphal 
280c7ba65d7SFlorian Westphal 		if (err && ret == 0)
281c7ba65d7SFlorian Westphal 			ret = err;
282c7ba65d7SFlorian Westphal 		segs = nskb;
283c7ba65d7SFlorian Westphal 	} while (segs);
284c7ba65d7SFlorian Westphal 
285c7ba65d7SFlorian Westphal 	return ret;
286c7ba65d7SFlorian Westphal }
287c7ba65d7SFlorian Westphal 
2880c4b51f0SEric W. Biederman static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
2891da177e4SLinus Torvalds {
290c5501eb3SFlorian Westphal 	unsigned int mtu;
29133b48679SDaniel Mack 	int ret;
29233b48679SDaniel Mack 
29333b48679SDaniel Mack 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
29433b48679SDaniel Mack 	if (ret) {
29533b48679SDaniel Mack 		kfree_skb(skb);
29633b48679SDaniel Mack 		return ret;
29733b48679SDaniel Mack 	}
298c5501eb3SFlorian Westphal 
2995c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
3005c901daaSPatrick McHardy 	/* Policy lookup after SNAT yielded a new policy */
30100db4124SIan Morris 	if (skb_dst(skb)->xfrm) {
30248d5cad8SPatrick McHardy 		IPCB(skb)->flags |= IPSKB_REROUTED;
30313206b6bSEric W. Biederman 		return dst_output(net, sk, skb);
30448d5cad8SPatrick McHardy 	}
3055c901daaSPatrick McHardy #endif
306fedbb6b4SShmulik Ladkani 	mtu = ip_skb_dst_mtu(sk, skb);
307c7ba65d7SFlorian Westphal 	if (skb_is_gso(skb))
308694869b3SEric W. Biederman 		return ip_finish_output_gso(net, sk, skb, mtu);
309c7ba65d7SFlorian Westphal 
310d6b915e2SFlorian Westphal 	if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU))
311694869b3SEric W. Biederman 		return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
312c7ba65d7SFlorian Westphal 
313694869b3SEric W. Biederman 	return ip_finish_output2(net, sk, skb);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
31633b48679SDaniel Mack static int ip_mc_finish_output(struct net *net, struct sock *sk,
31733b48679SDaniel Mack 			       struct sk_buff *skb)
31833b48679SDaniel Mack {
31933b48679SDaniel Mack 	int ret;
32033b48679SDaniel Mack 
32133b48679SDaniel Mack 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
32233b48679SDaniel Mack 	if (ret) {
32333b48679SDaniel Mack 		kfree_skb(skb);
32433b48679SDaniel Mack 		return ret;
32533b48679SDaniel Mack 	}
32633b48679SDaniel Mack 
32733b48679SDaniel Mack 	return dev_loopback_xmit(net, sk, skb);
32833b48679SDaniel Mack }
32933b48679SDaniel Mack 
330ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
3311da177e4SLinus Torvalds {
332511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
333d8d1f30bSChangli Gao 	struct net_device *dev = rt->dst.dev;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/*
3361da177e4SLinus Torvalds 	 *	If the indicated interface is up and running, send the packet.
3371da177e4SLinus Torvalds 	 */
33888f5cc24SEric W. Biederman 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	skb->dev = dev;
3411da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	/*
3441da177e4SLinus Torvalds 	 *	Multicasts are looped back for other local users
3451da177e4SLinus Torvalds 	 */
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_MULTICAST) {
3487ad6848cSOctavian Purdila 		if (sk_mc_loop(sk)
3491da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
3501da177e4SLinus Torvalds 		/* Small optimization: do not loopback not local frames,
3511da177e4SLinus Torvalds 		   which returned after forwarding; they will be  dropped
3521da177e4SLinus Torvalds 		   by ip_mr_input in any case.
3531da177e4SLinus Torvalds 		   Note, that local frames are looped back to be delivered
3541da177e4SLinus Torvalds 		   to local recipients.
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		   This check is duplicated in ip_mr_input at the moment.
3571da177e4SLinus Torvalds 		 */
3589d4fb27dSJoe Perches 		    &&
3599d4fb27dSJoe Perches 		    ((rt->rt_flags & RTCF_LOCAL) ||
3609d4fb27dSJoe Perches 		     !(IPCB(skb)->flags & IPSKB_FORWARDED))
3611da177e4SLinus Torvalds #endif
3621da177e4SLinus Torvalds 		   ) {
3631da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
3641da177e4SLinus Torvalds 			if (newskb)
3659bbc768aSJan Engelhardt 				NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
36629a26a56SEric W. Biederman 					net, sk, newskb, NULL, newskb->dev,
36733b48679SDaniel Mack 					ip_mc_finish_output);
3681da177e4SLinus Torvalds 		}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		/* Multicasts with ttl 0 must not go beyond the host */
3711da177e4SLinus Torvalds 
372eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->ttl == 0) {
3731da177e4SLinus Torvalds 			kfree_skb(skb);
3741da177e4SLinus Torvalds 			return 0;
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_BROADCAST) {
3791da177e4SLinus Torvalds 		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
3801da177e4SLinus Torvalds 		if (newskb)
38129a26a56SEric W. Biederman 			NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
38229a26a56SEric W. Biederman 				net, sk, newskb, NULL, newskb->dev,
38333b48679SDaniel Mack 				ip_mc_finish_output);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 
38629a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
38729a26a56SEric W. Biederman 			    net, sk, skb, NULL, skb->dev,
38829a26a56SEric W. Biederman 			    ip_finish_output,
38948d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
392ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
3931da177e4SLinus Torvalds {
394adf30907SEric Dumazet 	struct net_device *dev = skb_dst(skb)->dev;
3951bd9bef6SPatrick McHardy 
39688f5cc24SEric W. Biederman 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
3971da177e4SLinus Torvalds 
3981bd9bef6SPatrick McHardy 	skb->dev = dev;
3991bd9bef6SPatrick McHardy 	skb->protocol = htons(ETH_P_IP);
4001bd9bef6SPatrick McHardy 
40129a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
40229a26a56SEric W. Biederman 			    net, sk, skb, NULL, dev,
40348d5cad8SPatrick McHardy 			    ip_finish_output,
40448d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
40784f9307cSEric Dumazet /*
40884f9307cSEric Dumazet  * copy saddr and daddr, possibly using 64bit load/stores
40984f9307cSEric Dumazet  * Equivalent to :
41084f9307cSEric Dumazet  *   iph->saddr = fl4->saddr;
41184f9307cSEric Dumazet  *   iph->daddr = fl4->daddr;
41284f9307cSEric Dumazet  */
41384f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
41484f9307cSEric Dumazet {
41584f9307cSEric Dumazet 	BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
41684f9307cSEric Dumazet 		     offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
41784f9307cSEric Dumazet 	memcpy(&iph->saddr, &fl4->saddr,
41884f9307cSEric Dumazet 	       sizeof(fl4->saddr) + sizeof(fl4->daddr));
41984f9307cSEric Dumazet }
42084f9307cSEric Dumazet 
421b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */
422b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
42577589ce0SEric W. Biederman 	struct net *net = sock_net(sk);
426f6d8bd05SEric Dumazet 	struct ip_options_rcu *inet_opt;
427b57ae01aSDavid S. Miller 	struct flowi4 *fl4;
4281da177e4SLinus Torvalds 	struct rtable *rt;
4291da177e4SLinus Torvalds 	struct iphdr *iph;
430ab6e3febSEric Dumazet 	int res;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/* Skip all of this if the packet is already routed,
4331da177e4SLinus Torvalds 	 * f.e. by something like SCTP.
4341da177e4SLinus Torvalds 	 */
435ab6e3febSEric Dumazet 	rcu_read_lock();
436f6d8bd05SEric Dumazet 	inet_opt = rcu_dereference(inet->inet_opt);
437ea4fc0d6SDavid S. Miller 	fl4 = &fl->u.ip4;
438511c3f92SEric Dumazet 	rt = skb_rtable(skb);
43900db4124SIan Morris 	if (rt)
4401da177e4SLinus Torvalds 		goto packet_routed;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	/* Make sure we can route this packet. */
4431da177e4SLinus Torvalds 	rt = (struct rtable *)__sk_dst_check(sk, 0);
44451456b29SIan Morris 	if (!rt) {
4453ca3c68eSAl Viro 		__be32 daddr;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 		/* Use correct destination address if we have options. */
448c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
449f6d8bd05SEric Dumazet 		if (inet_opt && inet_opt->opt.srr)
450f6d8bd05SEric Dumazet 			daddr = inet_opt->opt.faddr;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 		/* If this fails, retransmit mechanism of transport layer will
4531da177e4SLinus Torvalds 		 * keep trying until route appears or the connection times
4541da177e4SLinus Torvalds 		 * itself out.
4551da177e4SLinus Torvalds 		 */
45677589ce0SEric W. Biederman 		rt = ip_route_output_ports(net, fl4, sk,
45778fbfd8aSDavid S. Miller 					   daddr, inet->inet_saddr,
45878fbfd8aSDavid S. Miller 					   inet->inet_dport,
45978fbfd8aSDavid S. Miller 					   inet->inet_sport,
46078fbfd8aSDavid S. Miller 					   sk->sk_protocol,
46178fbfd8aSDavid S. Miller 					   RT_CONN_FLAGS(sk),
46278fbfd8aSDavid S. Miller 					   sk->sk_bound_dev_if);
463b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
4641da177e4SLinus Torvalds 			goto no_route;
465d8d1f30bSChangli Gao 		sk_setup_caps(sk, &rt->dst);
4661da177e4SLinus Torvalds 	}
467d8d1f30bSChangli Gao 	skb_dst_set_noref(skb, &rt->dst);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds packet_routed:
470155e8336SJulian Anastasov 	if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
4711da177e4SLinus Torvalds 		goto no_route;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* OK, we know where to send it, allocate and build IP header. */
474f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
4758856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
476eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
477714e85beSAl Viro 	*((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
47860ff7467SWANG Cong 	if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
4791da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
4801da177e4SLinus Torvalds 	else
4811da177e4SLinus Torvalds 		iph->frag_off = 0;
482d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
4831da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
48484f9307cSEric Dumazet 	ip_copy_addrs(iph, fl4);
48584f9307cSEric Dumazet 
4861da177e4SLinus Torvalds 	/* Transport layer set skb->h.foo itself. */
4871da177e4SLinus Torvalds 
488f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.optlen) {
489f6d8bd05SEric Dumazet 		iph->ihl += inet_opt->opt.optlen >> 2;
490f6d8bd05SEric Dumazet 		ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 
49377589ce0SEric W. Biederman 	ip_select_ident_segs(net, skb, sk,
494b6a7719aSHannes Frederic Sowa 			     skb_shinfo(skb)->gso_segs ?: 1);
4951da177e4SLinus Torvalds 
496b0270e91SEric Dumazet 	/* TODO : should we use skb->sk here instead of sk ? */
4971da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
4984a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
4991da177e4SLinus Torvalds 
50033224b16SEric W. Biederman 	res = ip_local_out(net, sk, skb);
501ab6e3febSEric Dumazet 	rcu_read_unlock();
502ab6e3febSEric Dumazet 	return res;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds no_route:
505ab6e3febSEric Dumazet 	rcu_read_unlock();
50677589ce0SEric W. Biederman 	IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
5071da177e4SLinus Torvalds 	kfree_skb(skb);
5081da177e4SLinus Torvalds 	return -EHOSTUNREACH;
5091da177e4SLinus Torvalds }
5104bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
5151da177e4SLinus Torvalds 	to->priority = from->priority;
5161da177e4SLinus Torvalds 	to->protocol = from->protocol;
517adf30907SEric Dumazet 	skb_dst_drop(to);
518fe76cda3SEric Dumazet 	skb_dst_copy(to, from);
5191da177e4SLinus Torvalds 	to->dev = from->dev;
52082e91ffeSThomas Graf 	to->mark = from->mark;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	/* Copy the flags to each fragment. */
5231da177e4SLinus Torvalds 	IPCB(to)->flags = IPCB(from)->flags;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
5261da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
5271da177e4SLinus Torvalds #endif
528e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
5296ca40d4eSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IP_VS)
530c98d80edSJulian Anastasov 	to->ipvs_property = from->ipvs_property;
531c98d80edSJulian Anastasov #endif
532984bc16cSJames Morris 	skb_copy_secmark(to, from);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
535694869b3SEric W. Biederman static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
536c5501eb3SFlorian Westphal 		       unsigned int mtu,
537694869b3SEric W. Biederman 		       int (*output)(struct net *, struct sock *, struct sk_buff *))
53849d16b23SAndy Zhou {
53949d16b23SAndy Zhou 	struct iphdr *iph = ip_hdr(skb);
54049d16b23SAndy Zhou 
541d6b915e2SFlorian Westphal 	if ((iph->frag_off & htons(IP_DF)) == 0)
542694869b3SEric W. Biederman 		return ip_do_fragment(net, sk, skb, output);
543d6b915e2SFlorian Westphal 
544d6b915e2SFlorian Westphal 	if (unlikely(!skb->ignore_df ||
54549d16b23SAndy Zhou 		     (IPCB(skb)->frag_max_size &&
54649d16b23SAndy Zhou 		      IPCB(skb)->frag_max_size > mtu))) {
5479479b0afSEric W. Biederman 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
54849d16b23SAndy Zhou 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
54949d16b23SAndy Zhou 			  htonl(mtu));
55049d16b23SAndy Zhou 		kfree_skb(skb);
55149d16b23SAndy Zhou 		return -EMSGSIZE;
55249d16b23SAndy Zhou 	}
55349d16b23SAndy Zhou 
554694869b3SEric W. Biederman 	return ip_do_fragment(net, sk, skb, output);
55549d16b23SAndy Zhou }
55649d16b23SAndy Zhou 
5571da177e4SLinus Torvalds /*
5581da177e4SLinus Torvalds  *	This IP datagram is too large to be sent in one piece.  Break it up into
5591da177e4SLinus Torvalds  *	smaller pieces (each of size equal to IP header plus
5601da177e4SLinus Torvalds  *	a block of the data of the original IP data part) that will yet fit in a
5611da177e4SLinus Torvalds  *	single device frame, and queue such a frame for sending.
5621da177e4SLinus Torvalds  */
5631da177e4SLinus Torvalds 
564694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
565694869b3SEric W. Biederman 		   int (*output)(struct net *, struct sock *, struct sk_buff *))
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	struct iphdr *iph;
5681da177e4SLinus Torvalds 	int ptr;
5691da177e4SLinus Torvalds 	struct sk_buff *skb2;
570c893b806SChangli Gao 	unsigned int mtu, hlen, left, len, ll_rs;
5711da177e4SLinus Torvalds 	int offset;
57276ab608dSAlexey Dobriyan 	__be16 not_last_frag;
573511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
5741da177e4SLinus Torvalds 	int err = 0;
5751da177e4SLinus Torvalds 
576dbd3393cSHannes Frederic Sowa 	/* for offloaded checksums cleanup checksum before fragmentation */
577dbd3393cSHannes Frederic Sowa 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
578dbd3393cSHannes Frederic Sowa 	    (err = skb_checksum_help(skb)))
579dbd3393cSHannes Frederic Sowa 		goto fail;
580dbd3393cSHannes Frederic Sowa 
5811da177e4SLinus Torvalds 	/*
5821da177e4SLinus Torvalds 	 *	Point into the IP datagram header.
5831da177e4SLinus Torvalds 	 */
5841da177e4SLinus Torvalds 
585eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
5861da177e4SLinus Torvalds 
587fedbb6b4SShmulik Ladkani 	mtu = ip_skb_dst_mtu(sk, skb);
588d6b915e2SFlorian Westphal 	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
589d6b915e2SFlorian Westphal 		mtu = IPCB(skb)->frag_max_size;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	/*
5921da177e4SLinus Torvalds 	 *	Setup starting values.
5931da177e4SLinus Torvalds 	 */
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	hlen = iph->ihl * 4;
596f87c10a8SHannes Frederic Sowa 	mtu = mtu - hlen;	/* Size of data space */
59789cee8b1SHerbert Xu 	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/* When frag_list is given, use it. First, check its validity:
6001da177e4SLinus Torvalds 	 * some transformers could create wrong frag_list or break existing
6011da177e4SLinus Torvalds 	 * one, it is not prohibited. In this case fall back to copying.
6021da177e4SLinus Torvalds 	 *
6031da177e4SLinus Torvalds 	 * LATER: this step can be merged to real generation of fragments,
6041da177e4SLinus Torvalds 	 * we can switch to copy when see the first bad fragment.
6051da177e4SLinus Torvalds 	 */
60621dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
6073d13008eSEric Dumazet 		struct sk_buff *frag, *frag2;
608c72d8cdaSAlexey Dobriyan 		unsigned int first_len = skb_pagelen(skb);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
6111da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
61256f8a75cSPaul Gortmaker 		    ip_is_fragment(iph) ||
6131da177e4SLinus Torvalds 		    skb_cloned(skb))
6141da177e4SLinus Torvalds 			goto slow_path;
6151da177e4SLinus Torvalds 
616d7fcf1a5SDavid S. Miller 		skb_walk_frags(skb, frag) {
6171da177e4SLinus Torvalds 			/* Correct geometry. */
6181da177e4SLinus Torvalds 			if (frag->len > mtu ||
6191da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
6201da177e4SLinus Torvalds 			    skb_headroom(frag) < hlen)
6213d13008eSEric Dumazet 				goto slow_path_clean;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 			/* Partially cloned skb? */
6241da177e4SLinus Torvalds 			if (skb_shared(frag))
6253d13008eSEric Dumazet 				goto slow_path_clean;
6262fdba6b0SHerbert Xu 
6272fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
6282fdba6b0SHerbert Xu 			if (skb->sk) {
6292fdba6b0SHerbert Xu 				frag->sk = skb->sk;
6302fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
6312fdba6b0SHerbert Xu 			}
6323d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
6331da177e4SLinus Torvalds 		}
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 		/* Everything is OK. Generate! */
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 		err = 0;
6381da177e4SLinus Torvalds 		offset = 0;
6391da177e4SLinus Torvalds 		frag = skb_shinfo(skb)->frag_list;
640d7fcf1a5SDavid S. Miller 		skb_frag_list_init(skb);
6411da177e4SLinus Torvalds 		skb->data_len = first_len - skb_headlen(skb);
6421da177e4SLinus Torvalds 		skb->len = first_len;
6431da177e4SLinus Torvalds 		iph->tot_len = htons(first_len);
6441da177e4SLinus Torvalds 		iph->frag_off = htons(IP_MF);
6451da177e4SLinus Torvalds 		ip_send_check(iph);
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 		for (;;) {
6481da177e4SLinus Torvalds 			/* Prepare header of the next frame,
6491da177e4SLinus Torvalds 			 * before previous one went down. */
6501da177e4SLinus Torvalds 			if (frag) {
6511da177e4SLinus Torvalds 				frag->ip_summed = CHECKSUM_NONE;
652badff6d0SArnaldo Carvalho de Melo 				skb_reset_transport_header(frag);
653e2d1bca7SArnaldo Carvalho de Melo 				__skb_push(frag, hlen);
654e2d1bca7SArnaldo Carvalho de Melo 				skb_reset_network_header(frag);
655d56f90a7SArnaldo Carvalho de Melo 				memcpy(skb_network_header(frag), iph, hlen);
656eddc9ec5SArnaldo Carvalho de Melo 				iph = ip_hdr(frag);
6571da177e4SLinus Torvalds 				iph->tot_len = htons(frag->len);
6581da177e4SLinus Torvalds 				ip_copy_metadata(frag, skb);
6591da177e4SLinus Torvalds 				if (offset == 0)
6601da177e4SLinus Torvalds 					ip_options_fragment(frag);
6611da177e4SLinus Torvalds 				offset += skb->len - hlen;
6621da177e4SLinus Torvalds 				iph->frag_off = htons(offset>>3);
66300db4124SIan Morris 				if (frag->next)
6641da177e4SLinus Torvalds 					iph->frag_off |= htons(IP_MF);
6651da177e4SLinus Torvalds 				/* Ready, complete checksum */
6661da177e4SLinus Torvalds 				ip_send_check(iph);
6671da177e4SLinus Torvalds 			}
6681da177e4SLinus Torvalds 
669694869b3SEric W. Biederman 			err = output(net, sk, skb);
6701da177e4SLinus Torvalds 
671dafee490SWei Dong 			if (!err)
67226a949dbSEric W. Biederman 				IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
6731da177e4SLinus Torvalds 			if (err || !frag)
6741da177e4SLinus Torvalds 				break;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 			skb = frag;
6771da177e4SLinus Torvalds 			frag = skb->next;
6781da177e4SLinus Torvalds 			skb->next = NULL;
6791da177e4SLinus Torvalds 		}
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 		if (err == 0) {
68226a949dbSEric W. Biederman 			IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
6831da177e4SLinus Torvalds 			return 0;
6841da177e4SLinus Torvalds 		}
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 		while (frag) {
6871da177e4SLinus Torvalds 			skb = frag->next;
6881da177e4SLinus Torvalds 			kfree_skb(frag);
6891da177e4SLinus Torvalds 			frag = skb;
6901da177e4SLinus Torvalds 		}
69126a949dbSEric W. Biederman 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
6921da177e4SLinus Torvalds 		return err;
6933d13008eSEric Dumazet 
6943d13008eSEric Dumazet slow_path_clean:
6953d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
6963d13008eSEric Dumazet 			if (frag2 == frag)
6973d13008eSEric Dumazet 				break;
6983d13008eSEric Dumazet 			frag2->sk = NULL;
6993d13008eSEric Dumazet 			frag2->destructor = NULL;
7003d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
7013d13008eSEric Dumazet 		}
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds slow_path:
705c9af6db4SPravin B Shelar 	iph = ip_hdr(skb);
706fc70fb64SAlexander Duyck 
7071da177e4SLinus Torvalds 	left = skb->len - hlen;		/* Space per frame */
70849085bd7SGeorge Kadianakis 	ptr = hlen;		/* Where to start from */
7091da177e4SLinus Torvalds 
7108d045163SFlorian Westphal 	ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
7119bcfcaf5SStephen Hemminger 
7121da177e4SLinus Torvalds 	/*
7131da177e4SLinus Torvalds 	 *	Fragment the datagram.
7141da177e4SLinus Torvalds 	 */
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
7171da177e4SLinus Torvalds 	not_last_frag = iph->frag_off & htons(IP_MF);
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	/*
7201da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
7211da177e4SLinus Torvalds 	 */
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	while (left > 0) {
7241da177e4SLinus Torvalds 		len = left;
7251da177e4SLinus Torvalds 		/* IF: it doesn't fit, use 'mtu' - the data space left */
7261da177e4SLinus Torvalds 		if (len > mtu)
7271da177e4SLinus Torvalds 			len = mtu;
7281da177e4SLinus Torvalds 		/* IF: we are not sending up to and including the packet end
7291da177e4SLinus Torvalds 		   then align the next start on an eight byte boundary */
7301da177e4SLinus Torvalds 		if (len < left)	{
7311da177e4SLinus Torvalds 			len &= ~7;
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 
734cbffccc9SJoe Perches 		/* Allocate buffer */
735cbffccc9SJoe Perches 		skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
736cbffccc9SJoe Perches 		if (!skb2) {
7371da177e4SLinus Torvalds 			err = -ENOMEM;
7381da177e4SLinus Torvalds 			goto fail;
7391da177e4SLinus Torvalds 		}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 		/*
7421da177e4SLinus Torvalds 		 *	Set up data on packet
7431da177e4SLinus Torvalds 		 */
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 		ip_copy_metadata(skb2, skb);
7461da177e4SLinus Torvalds 		skb_reserve(skb2, ll_rs);
7471da177e4SLinus Torvalds 		skb_put(skb2, len + hlen);
748c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
749b0e380b1SArnaldo Carvalho de Melo 		skb2->transport_header = skb2->network_header + hlen;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 		/*
7521da177e4SLinus Torvalds 		 *	Charge the memory for the fragment to any owner
7531da177e4SLinus Torvalds 		 *	it might possess
7541da177e4SLinus Torvalds 		 */
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 		if (skb->sk)
7571da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 		/*
7601da177e4SLinus Torvalds 		 *	Copy the packet header into the new buffer.
7611da177e4SLinus Torvalds 		 */
7621da177e4SLinus Torvalds 
763d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 		/*
7661da177e4SLinus Torvalds 		 *	Copy a block of the IP datagram.
7671da177e4SLinus Torvalds 		 */
768bff9b61cSArnaldo Carvalho de Melo 		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
7691da177e4SLinus Torvalds 			BUG();
7701da177e4SLinus Torvalds 		left -= len;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		/*
7731da177e4SLinus Torvalds 		 *	Fill in the new header fields.
7741da177e4SLinus Torvalds 		 */
775eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
7761da177e4SLinus Torvalds 		iph->frag_off = htons((offset >> 3));
7771da177e4SLinus Torvalds 
778d6b915e2SFlorian Westphal 		if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
779d6b915e2SFlorian Westphal 			iph->frag_off |= htons(IP_DF);
780d6b915e2SFlorian Westphal 
7811da177e4SLinus Torvalds 		/* ANK: dirty, but effective trick. Upgrade options only if
7821da177e4SLinus Torvalds 		 * the segment to be fragmented was THE FIRST (otherwise,
7831da177e4SLinus Torvalds 		 * options are already fixed) and make it ONCE
7841da177e4SLinus Torvalds 		 * on the initial skb, so that all the following fragments
7851da177e4SLinus Torvalds 		 * will inherit fixed options.
7861da177e4SLinus Torvalds 		 */
7871da177e4SLinus Torvalds 		if (offset == 0)
7881da177e4SLinus Torvalds 			ip_options_fragment(skb);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 		/*
7911da177e4SLinus Torvalds 		 *	Added AC : If we are fragmenting a fragment that's not the
7921da177e4SLinus Torvalds 		 *		   last fragment then keep MF on each bit
7931da177e4SLinus Torvalds 		 */
7941da177e4SLinus Torvalds 		if (left > 0 || not_last_frag)
7951da177e4SLinus Torvalds 			iph->frag_off |= htons(IP_MF);
7961da177e4SLinus Torvalds 		ptr += len;
7971da177e4SLinus Torvalds 		offset += len;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		/*
8001da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
8011da177e4SLinus Torvalds 		 */
8021da177e4SLinus Torvalds 		iph->tot_len = htons(len + hlen);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 		ip_send_check(iph);
8051da177e4SLinus Torvalds 
806694869b3SEric W. Biederman 		err = output(net, sk, skb2);
8071da177e4SLinus Torvalds 		if (err)
8081da177e4SLinus Torvalds 			goto fail;
809dafee490SWei Dong 
81026a949dbSEric W. Biederman 		IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
8111da177e4SLinus Torvalds 	}
8125d0ba55bSEric Dumazet 	consume_skb(skb);
81326a949dbSEric W. Biederman 	IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
8141da177e4SLinus Torvalds 	return err;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds fail:
8171da177e4SLinus Torvalds 	kfree_skb(skb);
81826a949dbSEric W. Biederman 	IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
8191da177e4SLinus Torvalds 	return err;
8201da177e4SLinus Torvalds }
82149d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment);
8222e2f7aefSPatrick McHardy 
8231da177e4SLinus Torvalds int
8241da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
8251da177e4SLinus Torvalds {
826f69e6d13SAl Viro 	struct msghdr *msg = from;
8271da177e4SLinus Torvalds 
82884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
8290b62fca2SAl Viro 		if (!copy_from_iter_full(to, len, &msg->msg_iter))
8301da177e4SLinus Torvalds 			return -EFAULT;
8311da177e4SLinus Torvalds 	} else {
83244bb9363SAl Viro 		__wsum csum = 0;
8330b62fca2SAl Viro 		if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
8341da177e4SLinus Torvalds 			return -EFAULT;
8351da177e4SLinus Torvalds 		skb->csum = csum_block_add(skb->csum, csum, odd);
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 	return 0;
8381da177e4SLinus Torvalds }
8394bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag);
8401da177e4SLinus Torvalds 
84144bb9363SAl Viro static inline __wsum
8421da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	char *kaddr;
84544bb9363SAl Viro 	__wsum csum;
8461da177e4SLinus Torvalds 	kaddr = kmap(page);
8471da177e4SLinus Torvalds 	csum = csum_partial(kaddr + offset, copy, 0);
8481da177e4SLinus Torvalds 	kunmap(page);
8491da177e4SLinus Torvalds 	return csum;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8524b30b1c6SAdrian Bunk static inline int ip_ufo_append_data(struct sock *sk,
8531470ddf7SHerbert Xu 			struct sk_buff_head *queue,
854e89e9cf5SAnanda Raju 			int getfrag(void *from, char *to, int offset, int len,
855e89e9cf5SAnanda Raju 			       int odd, struct sk_buff *skb),
856e89e9cf5SAnanda Raju 			void *from, int length, int hh_len, int fragheaderlen,
857d9be4f7aSBill Sommerfeld 			int transhdrlen, int maxfraglen, unsigned int flags)
858e89e9cf5SAnanda Raju {
859e89e9cf5SAnanda Raju 	struct sk_buff *skb;
860e89e9cf5SAnanda Raju 	int err;
861e89e9cf5SAnanda Raju 
862e89e9cf5SAnanda Raju 	/* There is support for UDP fragmentation offload by network
863e89e9cf5SAnanda Raju 	 * device, so create one single skb packet containing complete
864e89e9cf5SAnanda Raju 	 * udp datagram
865e89e9cf5SAnanda Raju 	 */
86651456b29SIan Morris 	skb = skb_peek_tail(queue);
86751456b29SIan Morris 	if (!skb) {
868e89e9cf5SAnanda Raju 		skb = sock_alloc_send_skb(sk,
869e89e9cf5SAnanda Raju 			hh_len + fragheaderlen + transhdrlen + 20,
870e89e9cf5SAnanda Raju 			(flags & MSG_DONTWAIT), &err);
871e89e9cf5SAnanda Raju 
87251456b29SIan Morris 		if (!skb)
873e89e9cf5SAnanda Raju 			return err;
874e89e9cf5SAnanda Raju 
875e89e9cf5SAnanda Raju 		/* reserve space for Hardware header */
876e89e9cf5SAnanda Raju 		skb_reserve(skb, hh_len);
877e89e9cf5SAnanda Raju 
878e89e9cf5SAnanda Raju 		/* create space for UDP/IP header */
879e89e9cf5SAnanda Raju 		skb_put(skb, fragheaderlen + transhdrlen);
880e89e9cf5SAnanda Raju 
881e89e9cf5SAnanda Raju 		/* initialize network header pointer */
882c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb);
883e89e9cf5SAnanda Raju 
884e89e9cf5SAnanda Raju 		/* initialize protocol header pointer */
885b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header = skb->network_header + fragheaderlen;
886e89e9cf5SAnanda Raju 
887e89e9cf5SAnanda Raju 		skb->csum = 0;
888e89e9cf5SAnanda Raju 
889e93b7d74SJiri Pirko 		__skb_queue_tail(queue, skb);
890e93b7d74SJiri Pirko 	} else if (skb_is_gso(skb)) {
891e93b7d74SJiri Pirko 		goto append;
892e93b7d74SJiri Pirko 	}
893e93b7d74SJiri Pirko 
894e93b7d74SJiri Pirko 	skb->ip_summed = CHECKSUM_PARTIAL;
895e89e9cf5SAnanda Raju 	/* specify the length of each IP datagram fragment */
896d9be4f7aSBill Sommerfeld 	skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen;
897f83ef8c0SHerbert Xu 	skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
898be9164e7SKostya B 
899e93b7d74SJiri Pirko append:
900be9164e7SKostya B 	return skb_append_datato_frags(sk, skb, getfrag, from,
901be9164e7SKostya B 				       (length - transhdrlen));
902e89e9cf5SAnanda Raju }
903e89e9cf5SAnanda Raju 
904f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk,
905f5fca608SDavid S. Miller 			    struct flowi4 *fl4,
906f5fca608SDavid S. Miller 			    struct sk_buff_head *queue,
9071470ddf7SHerbert Xu 			    struct inet_cork *cork,
9085640f768SEric Dumazet 			    struct page_frag *pfrag,
9091470ddf7SHerbert Xu 			    int getfrag(void *from, char *to, int offset,
9101470ddf7SHerbert Xu 					int len, int odd, struct sk_buff *skb),
9111da177e4SLinus Torvalds 			    void *from, int length, int transhdrlen,
9121da177e4SLinus Torvalds 			    unsigned int flags)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
9151da177e4SLinus Torvalds 	struct sk_buff *skb;
9161da177e4SLinus Torvalds 
91707df5294SHerbert Xu 	struct ip_options *opt = cork->opt;
9181da177e4SLinus Torvalds 	int hh_len;
9191da177e4SLinus Torvalds 	int exthdrlen;
9201da177e4SLinus Torvalds 	int mtu;
9211da177e4SLinus Torvalds 	int copy;
9221da177e4SLinus Torvalds 	int err;
9231da177e4SLinus Torvalds 	int offset = 0;
924daba287bSHannes Frederic Sowa 	unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
9251da177e4SLinus Torvalds 	int csummode = CHECKSUM_NONE;
9261470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
92709c2d251SWillem de Bruijn 	u32 tskey = 0;
9281da177e4SLinus Torvalds 
92996d7303eSSteffen Klassert 	skb = skb_peek_tail(queue);
93096d7303eSSteffen Klassert 
93196d7303eSSteffen Klassert 	exthdrlen = !skb ? rt->dst.header_len : 0;
93207df5294SHerbert Xu 	mtu = cork->fragsize;
93309c2d251SWillem de Bruijn 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
93409c2d251SWillem de Bruijn 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
93509c2d251SWillem de Bruijn 		tskey = sk->sk_tskey++;
9361470ddf7SHerbert Xu 
937d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
9401da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
94160ff7467SWANG Cong 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
9421da177e4SLinus Torvalds 
943daba287bSHannes Frederic Sowa 	if (cork->length + length > maxnonfragsize - fragheaderlen) {
944f5fca608SDavid S. Miller 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
94561e7f09dSHannes Frederic Sowa 			       mtu - (opt ? opt->optlen : 0));
9461da177e4SLinus Torvalds 		return -EMSGSIZE;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	/*
9501da177e4SLinus Torvalds 	 * transhdrlen > 0 means that this is the first fragment and we wish
9511da177e4SLinus Torvalds 	 * it won't be fragmented in the future.
9521da177e4SLinus Torvalds 	 */
9531da177e4SLinus Torvalds 	if (transhdrlen &&
9541da177e4SLinus Torvalds 	    length + fragheaderlen <= mtu &&
955c8cd0989STom Herbert 	    rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
956d749c9cbSHannes Frederic Sowa 	    !(flags & MSG_MORE) &&
9571da177e4SLinus Torvalds 	    !exthdrlen)
95884fa7933SPatrick McHardy 		csummode = CHECKSUM_PARTIAL;
9591da177e4SLinus Torvalds 
9601470ddf7SHerbert Xu 	cork->length += length;
9610a28cfd5Szheng li 	if ((((length + fragheaderlen) > mtu) || (skb && skb_is_gso(skb))) &&
962be9164e7SKostya B 	    (sk->sk_protocol == IPPROTO_UDP) &&
963acf8dd0aSMichal Kubeček 	    (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len &&
96440ba3302SMichal Kubeček 	    (sk->sk_type == SOCK_DGRAM) && !sk->sk_no_check_tx) {
9651470ddf7SHerbert Xu 		err = ip_ufo_append_data(sk, queue, getfrag, from, length,
9661470ddf7SHerbert Xu 					 hh_len, fragheaderlen, transhdrlen,
967d9be4f7aSBill Sommerfeld 					 maxfraglen, flags);
968baa829d8SPatrick McHardy 		if (err)
969e89e9cf5SAnanda Raju 			goto error;
970e89e9cf5SAnanda Raju 		return 0;
971e89e9cf5SAnanda Raju 	}
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/* So, what's going on in the loop below?
9741da177e4SLinus Torvalds 	 *
9751da177e4SLinus Torvalds 	 * We use calculated fragment length to generate chained skb,
9761da177e4SLinus Torvalds 	 * each of segments is IP fragment ready for sending to network after
9771da177e4SLinus Torvalds 	 * adding appropriate IP header.
9781da177e4SLinus Torvalds 	 */
9791da177e4SLinus Torvalds 
98026cde9f7SHerbert Xu 	if (!skb)
9811da177e4SLinus Torvalds 		goto alloc_new_skb;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	while (length > 0) {
9841da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
9851da177e4SLinus Torvalds 		copy = mtu - skb->len;
9861da177e4SLinus Torvalds 		if (copy < length)
9871da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
9881da177e4SLinus Torvalds 		if (copy <= 0) {
9891da177e4SLinus Torvalds 			char *data;
9901da177e4SLinus Torvalds 			unsigned int datalen;
9911da177e4SLinus Torvalds 			unsigned int fraglen;
9921da177e4SLinus Torvalds 			unsigned int fraggap;
9931da177e4SLinus Torvalds 			unsigned int alloclen;
9941da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
9951da177e4SLinus Torvalds alloc_new_skb:
9961da177e4SLinus Torvalds 			skb_prev = skb;
9971da177e4SLinus Torvalds 			if (skb_prev)
9981da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
9991da177e4SLinus Torvalds 			else
10001da177e4SLinus Torvalds 				fraggap = 0;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 			/*
10031da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
10041da177e4SLinus Torvalds 			 * we know we need more fragment(s).
10051da177e4SLinus Torvalds 			 */
10061da177e4SLinus Torvalds 			datalen = length + fraggap;
10071da177e4SLinus Torvalds 			if (datalen > mtu - fragheaderlen)
10081da177e4SLinus Torvalds 				datalen = maxfraglen - fragheaderlen;
10091da177e4SLinus Torvalds 			fraglen = datalen + fragheaderlen;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
1012d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
10131da177e4SLinus Torvalds 				alloclen = mtu;
10141da177e4SLinus Torvalds 			else
101559104f06SEric Dumazet 				alloclen = fraglen;
10161da177e4SLinus Torvalds 
1017353e5c9aSSteffen Klassert 			alloclen += exthdrlen;
1018353e5c9aSSteffen Klassert 
10191da177e4SLinus Torvalds 			/* The last fragment gets additional space at tail.
10201da177e4SLinus Torvalds 			 * Note, with MSG_MORE we overallocate on fragments,
10211da177e4SLinus Torvalds 			 * because we have no idea what fragment will be
10221da177e4SLinus Torvalds 			 * the last.
10231da177e4SLinus Torvalds 			 */
102433f99dc7SSteffen Klassert 			if (datalen == length + fraggap)
1025d8d1f30bSChangli Gao 				alloclen += rt->dst.trailer_len;
102633f99dc7SSteffen Klassert 
10271da177e4SLinus Torvalds 			if (transhdrlen) {
10281da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
10291da177e4SLinus Torvalds 						alloclen + hh_len + 15,
10301da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
10311da177e4SLinus Torvalds 			} else {
10321da177e4SLinus Torvalds 				skb = NULL;
10331da177e4SLinus Torvalds 				if (atomic_read(&sk->sk_wmem_alloc) <=
10341da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
10351da177e4SLinus Torvalds 					skb = sock_wmalloc(sk,
10361da177e4SLinus Torvalds 							   alloclen + hh_len + 15, 1,
10371da177e4SLinus Torvalds 							   sk->sk_allocation);
103851456b29SIan Morris 				if (unlikely(!skb))
10391da177e4SLinus Torvalds 					err = -ENOBUFS;
10401da177e4SLinus Torvalds 			}
104151456b29SIan Morris 			if (!skb)
10421da177e4SLinus Torvalds 				goto error;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 			/*
10451da177e4SLinus Torvalds 			 *	Fill in the control structures
10461da177e4SLinus Torvalds 			 */
10471da177e4SLinus Torvalds 			skb->ip_summed = csummode;
10481da177e4SLinus Torvalds 			skb->csum = 0;
10491da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
105011878b40SWillem de Bruijn 
105111878b40SWillem de Bruijn 			/* only the initial fragment is time stamped */
10521470ddf7SHerbert Xu 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
105311878b40SWillem de Bruijn 			cork->tx_flags = 0;
105409c2d251SWillem de Bruijn 			skb_shinfo(skb)->tskey = tskey;
105509c2d251SWillem de Bruijn 			tskey = 0;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 			/*
10581da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
10591da177e4SLinus Torvalds 			 */
1060353e5c9aSSteffen Klassert 			data = skb_put(skb, fraglen + exthdrlen);
1061c14d2450SArnaldo Carvalho de Melo 			skb_set_network_header(skb, exthdrlen);
1062b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1063b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
1064353e5c9aSSteffen Klassert 			data += fragheaderlen + exthdrlen;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 			if (fraggap) {
10671da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
10681da177e4SLinus Torvalds 					skb_prev, maxfraglen,
10691da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
10701da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
10711da177e4SLinus Torvalds 							  skb->csum);
10721da177e4SLinus Torvalds 				data += fraggap;
1073e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
10741da177e4SLinus Torvalds 			}
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 			copy = datalen - transhdrlen - fraggap;
10771da177e4SLinus Torvalds 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
10781da177e4SLinus Torvalds 				err = -EFAULT;
10791da177e4SLinus Torvalds 				kfree_skb(skb);
10801da177e4SLinus Torvalds 				goto error;
10811da177e4SLinus Torvalds 			}
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 			offset += copy;
10841da177e4SLinus Torvalds 			length -= datalen - fraggap;
10851da177e4SLinus Torvalds 			transhdrlen = 0;
10861da177e4SLinus Torvalds 			exthdrlen = 0;
10871da177e4SLinus Torvalds 			csummode = CHECKSUM_NONE;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 			/*
10901da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
10911da177e4SLinus Torvalds 			 */
10921470ddf7SHerbert Xu 			__skb_queue_tail(queue, skb);
10931da177e4SLinus Torvalds 			continue;
10941da177e4SLinus Torvalds 		}
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 		if (copy > length)
10971da177e4SLinus Torvalds 			copy = length;
10981da177e4SLinus Torvalds 
1099d8d1f30bSChangli Gao 		if (!(rt->dst.dev->features&NETIF_F_SG)) {
11001da177e4SLinus Torvalds 			unsigned int off;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 			off = skb->len;
11031da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
11041da177e4SLinus Torvalds 					offset, copy, off, skb) < 0) {
11051da177e4SLinus Torvalds 				__skb_trim(skb, off);
11061da177e4SLinus Torvalds 				err = -EFAULT;
11071da177e4SLinus Torvalds 				goto error;
11081da177e4SLinus Torvalds 			}
11091da177e4SLinus Torvalds 		} else {
11101da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 			err = -ENOMEM;
11135640f768SEric Dumazet 			if (!sk_page_frag_refill(sk, pfrag))
11141da177e4SLinus Torvalds 				goto error;
11151da177e4SLinus Torvalds 
11165640f768SEric Dumazet 			if (!skb_can_coalesce(skb, i, pfrag->page,
11175640f768SEric Dumazet 					      pfrag->offset)) {
11181da177e4SLinus Torvalds 				err = -EMSGSIZE;
11195640f768SEric Dumazet 				if (i == MAX_SKB_FRAGS)
11201da177e4SLinus Torvalds 					goto error;
11215640f768SEric Dumazet 
11225640f768SEric Dumazet 				__skb_fill_page_desc(skb, i, pfrag->page,
11235640f768SEric Dumazet 						     pfrag->offset, 0);
11245640f768SEric Dumazet 				skb_shinfo(skb)->nr_frags = ++i;
11255640f768SEric Dumazet 				get_page(pfrag->page);
11261da177e4SLinus Torvalds 			}
11275640f768SEric Dumazet 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
11285640f768SEric Dumazet 			if (getfrag(from,
11295640f768SEric Dumazet 				    page_address(pfrag->page) + pfrag->offset,
11305640f768SEric Dumazet 				    offset, copy, skb->len, skb) < 0)
11315640f768SEric Dumazet 				goto error_efault;
11325640f768SEric Dumazet 
11335640f768SEric Dumazet 			pfrag->offset += copy;
11345640f768SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
11351da177e4SLinus Torvalds 			skb->len += copy;
11361da177e4SLinus Torvalds 			skb->data_len += copy;
1137f945fa7aSHerbert Xu 			skb->truesize += copy;
1138f945fa7aSHerbert Xu 			atomic_add(copy, &sk->sk_wmem_alloc);
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 		offset += copy;
11411da177e4SLinus Torvalds 		length -= copy;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	return 0;
11451da177e4SLinus Torvalds 
11465640f768SEric Dumazet error_efault:
11475640f768SEric Dumazet 	err = -EFAULT;
11481da177e4SLinus Torvalds error:
11491470ddf7SHerbert Xu 	cork->length -= length;
11505e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
11511da177e4SLinus Torvalds 	return err;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
11551470ddf7SHerbert Xu 			 struct ipcm_cookie *ipc, struct rtable **rtp)
11561470ddf7SHerbert Xu {
1157f6d8bd05SEric Dumazet 	struct ip_options_rcu *opt;
11581470ddf7SHerbert Xu 	struct rtable *rt;
11591470ddf7SHerbert Xu 
11601470ddf7SHerbert Xu 	/*
11611470ddf7SHerbert Xu 	 * setup for corking.
11621470ddf7SHerbert Xu 	 */
11631470ddf7SHerbert Xu 	opt = ipc->opt;
11641470ddf7SHerbert Xu 	if (opt) {
116551456b29SIan Morris 		if (!cork->opt) {
11661470ddf7SHerbert Xu 			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
11671470ddf7SHerbert Xu 					    sk->sk_allocation);
116851456b29SIan Morris 			if (unlikely(!cork->opt))
11691470ddf7SHerbert Xu 				return -ENOBUFS;
11701470ddf7SHerbert Xu 		}
1171f6d8bd05SEric Dumazet 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
11721470ddf7SHerbert Xu 		cork->flags |= IPCORK_OPT;
11731470ddf7SHerbert Xu 		cork->addr = ipc->addr;
11741470ddf7SHerbert Xu 	}
11751470ddf7SHerbert Xu 	rt = *rtp;
11761470ddf7SHerbert Xu 	if (unlikely(!rt))
11771470ddf7SHerbert Xu 		return -EFAULT;
11781470ddf7SHerbert Xu 	/*
11791470ddf7SHerbert Xu 	 * We steal reference to this route, caller should not release it
11801470ddf7SHerbert Xu 	 */
11811470ddf7SHerbert Xu 	*rtp = NULL;
1182482fc609SHannes Frederic Sowa 	cork->fragsize = ip_sk_use_pmtu(sk) ?
1183482fc609SHannes Frederic Sowa 			 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
11841470ddf7SHerbert Xu 	cork->dst = &rt->dst;
11851470ddf7SHerbert Xu 	cork->length = 0;
1186aa661581SFrancesco Fusco 	cork->ttl = ipc->ttl;
1187aa661581SFrancesco Fusco 	cork->tos = ipc->tos;
1188aa661581SFrancesco Fusco 	cork->priority = ipc->priority;
11891470ddf7SHerbert Xu 	cork->tx_flags = ipc->tx_flags;
11901470ddf7SHerbert Xu 
11911470ddf7SHerbert Xu 	return 0;
11921470ddf7SHerbert Xu }
11931470ddf7SHerbert Xu 
11941470ddf7SHerbert Xu /*
11951470ddf7SHerbert Xu  *	ip_append_data() and ip_append_page() can make one large IP datagram
11961470ddf7SHerbert Xu  *	from many pieces of data. Each pieces will be holded on the socket
11971470ddf7SHerbert Xu  *	until ip_push_pending_frames() is called. Each piece can be a page
11981470ddf7SHerbert Xu  *	or non-page data.
11991470ddf7SHerbert Xu  *
12001470ddf7SHerbert Xu  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
12011470ddf7SHerbert Xu  *	this interface potentially.
12021470ddf7SHerbert Xu  *
12031470ddf7SHerbert Xu  *	LATER: length must be adjusted by pad at tail, when it is required.
12041470ddf7SHerbert Xu  */
1205f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4,
12061470ddf7SHerbert Xu 		   int getfrag(void *from, char *to, int offset, int len,
12071470ddf7SHerbert Xu 			       int odd, struct sk_buff *skb),
12081470ddf7SHerbert Xu 		   void *from, int length, int transhdrlen,
12091470ddf7SHerbert Xu 		   struct ipcm_cookie *ipc, struct rtable **rtp,
12101470ddf7SHerbert Xu 		   unsigned int flags)
12111470ddf7SHerbert Xu {
12121470ddf7SHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
12131470ddf7SHerbert Xu 	int err;
12141470ddf7SHerbert Xu 
12151470ddf7SHerbert Xu 	if (flags&MSG_PROBE)
12161470ddf7SHerbert Xu 		return 0;
12171470ddf7SHerbert Xu 
12181470ddf7SHerbert Xu 	if (skb_queue_empty(&sk->sk_write_queue)) {
1219bdc712b4SDavid S. Miller 		err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
12201470ddf7SHerbert Xu 		if (err)
12211470ddf7SHerbert Xu 			return err;
12221470ddf7SHerbert Xu 	} else {
12231470ddf7SHerbert Xu 		transhdrlen = 0;
12241470ddf7SHerbert Xu 	}
12251470ddf7SHerbert Xu 
12265640f768SEric Dumazet 	return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
12275640f768SEric Dumazet 				sk_page_frag(sk), getfrag,
12281470ddf7SHerbert Xu 				from, length, transhdrlen, flags);
12291470ddf7SHerbert Xu }
12301470ddf7SHerbert Xu 
1231f5fca608SDavid S. Miller ssize_t	ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
12321da177e4SLinus Torvalds 		       int offset, size_t size, int flags)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
12351da177e4SLinus Torvalds 	struct sk_buff *skb;
12361da177e4SLinus Torvalds 	struct rtable *rt;
12371da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
1238bdc712b4SDavid S. Miller 	struct inet_cork *cork;
12391da177e4SLinus Torvalds 	int hh_len;
12401da177e4SLinus Torvalds 	int mtu;
12411da177e4SLinus Torvalds 	int len;
12421da177e4SLinus Torvalds 	int err;
1243daba287bSHannes Frederic Sowa 	unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	if (inet->hdrincl)
12461da177e4SLinus Torvalds 		return -EPERM;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
12491da177e4SLinus Torvalds 		return 0;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue))
12521da177e4SLinus Torvalds 		return -EINVAL;
12531da177e4SLinus Torvalds 
1254bdc712b4SDavid S. Miller 	cork = &inet->cork.base;
1255bdc712b4SDavid S. Miller 	rt = (struct rtable *)cork->dst;
1256bdc712b4SDavid S. Miller 	if (cork->flags & IPCORK_OPT)
1257bdc712b4SDavid S. Miller 		opt = cork->opt;
12581da177e4SLinus Torvalds 
1259d8d1f30bSChangli Gao 	if (!(rt->dst.dev->features&NETIF_F_SG))
12601da177e4SLinus Torvalds 		return -EOPNOTSUPP;
12611da177e4SLinus Torvalds 
1262d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1263bdc712b4SDavid S. Miller 	mtu = cork->fragsize;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
12661da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
126760ff7467SWANG Cong 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
12681da177e4SLinus Torvalds 
1269daba287bSHannes Frederic Sowa 	if (cork->length + size > maxnonfragsize - fragheaderlen) {
127061e7f09dSHannes Frederic Sowa 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
127161e7f09dSHannes Frederic Sowa 			       mtu - (opt ? opt->optlen : 0));
12721da177e4SLinus Torvalds 		return -EMSGSIZE;
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
127551456b29SIan Morris 	skb = skb_peek_tail(&sk->sk_write_queue);
127651456b29SIan Morris 	if (!skb)
12771da177e4SLinus Torvalds 		return -EINVAL;
12781da177e4SLinus Torvalds 
127926cde9f7SHerbert Xu 	if ((size + skb->len > mtu) &&
128026cde9f7SHerbert Xu 	    (sk->sk_protocol == IPPROTO_UDP) &&
1281d8d1f30bSChangli Gao 	    (rt->dst.dev->features & NETIF_F_UFO)) {
1282a8c4a252SHannes Frederic Sowa 		if (skb->ip_summed != CHECKSUM_PARTIAL)
1283a8c4a252SHannes Frederic Sowa 			return -EOPNOTSUPP;
1284a8c4a252SHannes Frederic Sowa 
12857967168cSHerbert Xu 		skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
1286f83ef8c0SHerbert Xu 		skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
12877967168cSHerbert Xu 	}
1288a8c4a252SHannes Frederic Sowa 	cork->length += size;
1289e89e9cf5SAnanda Raju 
12901da177e4SLinus Torvalds 	while (size > 0) {
1291be12a1feSHannes Frederic Sowa 		if (skb_is_gso(skb)) {
1292e89e9cf5SAnanda Raju 			len = size;
1293be12a1feSHannes Frederic Sowa 		} else {
1294e89e9cf5SAnanda Raju 
12951da177e4SLinus Torvalds 			/* Check if the remaining data fits into current packet. */
12961da177e4SLinus Torvalds 			len = mtu - skb->len;
12971da177e4SLinus Torvalds 			if (len < size)
12981da177e4SLinus Torvalds 				len = maxfraglen - skb->len;
1299e89e9cf5SAnanda Raju 		}
13001da177e4SLinus Torvalds 		if (len <= 0) {
13011da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
13021da177e4SLinus Torvalds 			int alloclen;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 			skb_prev = skb;
13051da177e4SLinus Torvalds 			fraggap = skb_prev->len - maxfraglen;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 			alloclen = fragheaderlen + hh_len + fraggap + 15;
13081da177e4SLinus Torvalds 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
13091da177e4SLinus Torvalds 			if (unlikely(!skb)) {
13101da177e4SLinus Torvalds 				err = -ENOBUFS;
13111da177e4SLinus Torvalds 				goto error;
13121da177e4SLinus Torvalds 			}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 			/*
13151da177e4SLinus Torvalds 			 *	Fill in the control structures
13161da177e4SLinus Torvalds 			 */
13171da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_NONE;
13181da177e4SLinus Torvalds 			skb->csum = 0;
13191da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 			/*
13221da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
13231da177e4SLinus Torvalds 			 */
1324967b05f6SArnaldo Carvalho de Melo 			skb_put(skb, fragheaderlen + fraggap);
13252ca9e6f2SArnaldo Carvalho de Melo 			skb_reset_network_header(skb);
1326b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1327b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
13281da177e4SLinus Torvalds 			if (fraggap) {
1329967b05f6SArnaldo Carvalho de Melo 				skb->csum = skb_copy_and_csum_bits(skb_prev,
1330967b05f6SArnaldo Carvalho de Melo 								   maxfraglen,
13319c70220bSArnaldo Carvalho de Melo 						    skb_transport_header(skb),
1332967b05f6SArnaldo Carvalho de Melo 								   fraggap, 0);
13331da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
13341da177e4SLinus Torvalds 							  skb->csum);
1335e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
13361da177e4SLinus Torvalds 			}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 			/*
13391da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
13401da177e4SLinus Torvalds 			 */
13411da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
13421da177e4SLinus Torvalds 			continue;
13431da177e4SLinus Torvalds 		}
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 		if (len > size)
13461da177e4SLinus Torvalds 			len = size;
1347be12a1feSHannes Frederic Sowa 
1348be12a1feSHannes Frederic Sowa 		if (skb_append_pagefrags(skb, page, offset, len)) {
13491da177e4SLinus Torvalds 			err = -EMSGSIZE;
13501da177e4SLinus Torvalds 			goto error;
13511da177e4SLinus Torvalds 		}
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 		if (skb->ip_summed == CHECKSUM_NONE) {
135444bb9363SAl Viro 			__wsum csum;
13551da177e4SLinus Torvalds 			csum = csum_page(page, offset, len);
13561da177e4SLinus Torvalds 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
13571da177e4SLinus Torvalds 		}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 		skb->len += len;
13601da177e4SLinus Torvalds 		skb->data_len += len;
13611e34a11dSDavid S. Miller 		skb->truesize += len;
13621e34a11dSDavid S. Miller 		atomic_add(len, &sk->sk_wmem_alloc);
13631da177e4SLinus Torvalds 		offset += len;
13641da177e4SLinus Torvalds 		size -= len;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 	return 0;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds error:
1369bdc712b4SDavid S. Miller 	cork->length -= size;
13705e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
13711da177e4SLinus Torvalds 	return err;
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13741470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork)
1375429f08e9SPavel Emelyanov {
13761470ddf7SHerbert Xu 	cork->flags &= ~IPCORK_OPT;
13771470ddf7SHerbert Xu 	kfree(cork->opt);
13781470ddf7SHerbert Xu 	cork->opt = NULL;
13791470ddf7SHerbert Xu 	dst_release(cork->dst);
13801470ddf7SHerbert Xu 	cork->dst = NULL;
1381429f08e9SPavel Emelyanov }
1382429f08e9SPavel Emelyanov 
13831da177e4SLinus Torvalds /*
13841da177e4SLinus Torvalds  *	Combined all pending IP fragments on the socket as one IP datagram
13851da177e4SLinus Torvalds  *	and push them out.
13861da177e4SLinus Torvalds  */
13871c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk,
138877968b78SDavid S. Miller 			      struct flowi4 *fl4,
13891470ddf7SHerbert Xu 			      struct sk_buff_head *queue,
13901470ddf7SHerbert Xu 			      struct inet_cork *cork)
13911da177e4SLinus Torvalds {
13921da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
13931da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
13941da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
13950388b004SPavel Emelyanov 	struct net *net = sock_net(sk);
13961da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
13971470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
13981da177e4SLinus Torvalds 	struct iphdr *iph;
139976ab608dSAlexey Dobriyan 	__be16 df = 0;
14001da177e4SLinus Torvalds 	__u8 ttl;
14011da177e4SLinus Torvalds 
140251456b29SIan Morris 	skb = __skb_dequeue(queue);
140351456b29SIan Morris 	if (!skb)
14041da177e4SLinus Torvalds 		goto out;
14051da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1408d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1409bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
14101470ddf7SHerbert Xu 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1411cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
14121da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
14131da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
14141da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
14151da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
14161da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
14171da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
14181da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
14221da177e4SLinus Torvalds 	 * to fragment the frame generated here. No matter, what transforms
14231da177e4SLinus Torvalds 	 * how transforms change size of the packet, it will come out.
14241da177e4SLinus Torvalds 	 */
142560ff7467SWANG Cong 	skb->ignore_df = ip_sk_ignore_df(sk);
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	/* DF bit is set when we want to see DF on outgoing frames.
142860ff7467SWANG Cong 	 * If ignore_df is set too, we still allow to fragment this frame
14291da177e4SLinus Torvalds 	 * locally. */
1430482fc609SHannes Frederic Sowa 	if (inet->pmtudisc == IP_PMTUDISC_DO ||
1431482fc609SHannes Frederic Sowa 	    inet->pmtudisc == IP_PMTUDISC_PROBE ||
1432d8d1f30bSChangli Gao 	    (skb->len <= dst_mtu(&rt->dst) &&
1433d8d1f30bSChangli Gao 	     ip_dont_fragment(sk, &rt->dst)))
14341da177e4SLinus Torvalds 		df = htons(IP_DF);
14351da177e4SLinus Torvalds 
14361470ddf7SHerbert Xu 	if (cork->flags & IPCORK_OPT)
14371470ddf7SHerbert Xu 		opt = cork->opt;
14381da177e4SLinus Torvalds 
1439aa661581SFrancesco Fusco 	if (cork->ttl != 0)
1440aa661581SFrancesco Fusco 		ttl = cork->ttl;
1441aa661581SFrancesco Fusco 	else if (rt->rt_type == RTN_MULTICAST)
14421da177e4SLinus Torvalds 		ttl = inet->mc_ttl;
14431da177e4SLinus Torvalds 	else
1444d8d1f30bSChangli Gao 		ttl = ip_select_ttl(inet, &rt->dst);
14451da177e4SLinus Torvalds 
1446749154aaSAnsis Atteka 	iph = ip_hdr(skb);
14471da177e4SLinus Torvalds 	iph->version = 4;
14481da177e4SLinus Torvalds 	iph->ihl = 5;
1449aa661581SFrancesco Fusco 	iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
14501da177e4SLinus Torvalds 	iph->frag_off = df;
14511da177e4SLinus Torvalds 	iph->ttl = ttl;
14521da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
145384f9307cSEric Dumazet 	ip_copy_addrs(iph, fl4);
1454b6a7719aSHannes Frederic Sowa 	ip_select_ident(net, skb, sk);
14551da177e4SLinus Torvalds 
145622f728f8SDavid S. Miller 	if (opt) {
145722f728f8SDavid S. Miller 		iph->ihl += opt->optlen>>2;
145822f728f8SDavid S. Miller 		ip_options_build(skb, opt, cork->addr, rt, 0);
145922f728f8SDavid S. Miller 	}
146022f728f8SDavid S. Miller 
1461aa661581SFrancesco Fusco 	skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
14624a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1463a21bba94SEric Dumazet 	/*
1464a21bba94SEric Dumazet 	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1465a21bba94SEric Dumazet 	 * on dst refcount
1466a21bba94SEric Dumazet 	 */
14671470ddf7SHerbert Xu 	cork->dst = NULL;
1468d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
14691da177e4SLinus Torvalds 
147096793b48SDavid L Stevens 	if (iph->protocol == IPPROTO_ICMP)
14710388b004SPavel Emelyanov 		icmp_out_count(net, ((struct icmphdr *)
147296793b48SDavid L Stevens 			skb_transport_header(skb))->type);
147396793b48SDavid L Stevens 
14741c32c5adSHerbert Xu 	ip_cork_release(cork);
14751c32c5adSHerbert Xu out:
14761c32c5adSHerbert Xu 	return skb;
14771c32c5adSHerbert Xu }
14781c32c5adSHerbert Xu 
1479b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb)
14801c32c5adSHerbert Xu {
14811c32c5adSHerbert Xu 	int err;
14821c32c5adSHerbert Xu 
148333224b16SEric W. Biederman 	err = ip_local_out(net, skb->sk, skb);
14841da177e4SLinus Torvalds 	if (err) {
14851da177e4SLinus Torvalds 		if (err > 0)
14866ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
14871da177e4SLinus Torvalds 		if (err)
14881c32c5adSHerbert Xu 			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	return err;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
149477968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
14951470ddf7SHerbert Xu {
14961c32c5adSHerbert Xu 	struct sk_buff *skb;
14971c32c5adSHerbert Xu 
149877968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
14991c32c5adSHerbert Xu 	if (!skb)
15001c32c5adSHerbert Xu 		return 0;
15011c32c5adSHerbert Xu 
15021c32c5adSHerbert Xu 	/* Netfilter gets whole the not fragmented skb. */
1503b5ec8eeaSEric Dumazet 	return ip_send_skb(sock_net(sk), skb);
15041470ddf7SHerbert Xu }
15051470ddf7SHerbert Xu 
15061da177e4SLinus Torvalds /*
15071da177e4SLinus Torvalds  *	Throw away all pending data on the socket.
15081da177e4SLinus Torvalds  */
15091470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk,
15101470ddf7SHerbert Xu 				      struct sk_buff_head *queue,
15111470ddf7SHerbert Xu 				      struct inet_cork *cork)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	struct sk_buff *skb;
15141da177e4SLinus Torvalds 
15151470ddf7SHerbert Xu 	while ((skb = __skb_dequeue_tail(queue)) != NULL)
15161da177e4SLinus Torvalds 		kfree_skb(skb);
15171da177e4SLinus Torvalds 
15181470ddf7SHerbert Xu 	ip_cork_release(cork);
15191470ddf7SHerbert Xu }
15201470ddf7SHerbert Xu 
15211470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk)
15221470ddf7SHerbert Xu {
1523bdc712b4SDavid S. Miller 	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
15261c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk,
152777968b78SDavid S. Miller 			    struct flowi4 *fl4,
15281c32c5adSHerbert Xu 			    int getfrag(void *from, char *to, int offset,
15291c32c5adSHerbert Xu 					int len, int odd, struct sk_buff *skb),
15301c32c5adSHerbert Xu 			    void *from, int length, int transhdrlen,
15311c32c5adSHerbert Xu 			    struct ipcm_cookie *ipc, struct rtable **rtp,
15321c32c5adSHerbert Xu 			    unsigned int flags)
15331c32c5adSHerbert Xu {
1534b80d7226SDavid S. Miller 	struct inet_cork cork;
15351c32c5adSHerbert Xu 	struct sk_buff_head queue;
15361c32c5adSHerbert Xu 	int err;
15371c32c5adSHerbert Xu 
15381c32c5adSHerbert Xu 	if (flags & MSG_PROBE)
15391c32c5adSHerbert Xu 		return NULL;
15401c32c5adSHerbert Xu 
15411c32c5adSHerbert Xu 	__skb_queue_head_init(&queue);
15421c32c5adSHerbert Xu 
1543b80d7226SDavid S. Miller 	cork.flags = 0;
1544b80d7226SDavid S. Miller 	cork.addr = 0;
154570652728SDavid S. Miller 	cork.opt = NULL;
15461c32c5adSHerbert Xu 	err = ip_setup_cork(sk, &cork, ipc, rtp);
15471c32c5adSHerbert Xu 	if (err)
15481c32c5adSHerbert Xu 		return ERR_PTR(err);
15491c32c5adSHerbert Xu 
15505640f768SEric Dumazet 	err = __ip_append_data(sk, fl4, &queue, &cork,
15515640f768SEric Dumazet 			       &current->task_frag, getfrag,
15521c32c5adSHerbert Xu 			       from, length, transhdrlen, flags);
15531c32c5adSHerbert Xu 	if (err) {
15541c32c5adSHerbert Xu 		__ip_flush_pending_frames(sk, &queue, &cork);
15551c32c5adSHerbert Xu 		return ERR_PTR(err);
15561c32c5adSHerbert Xu 	}
15571c32c5adSHerbert Xu 
155877968b78SDavid S. Miller 	return __ip_make_skb(sk, fl4, &queue, &cork);
15591c32c5adSHerbert Xu }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds /*
15621da177e4SLinus Torvalds  *	Fetch data from kernel space and fill in checksum if needed.
15631da177e4SLinus Torvalds  */
15641da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset,
15651da177e4SLinus Torvalds 			      int len, int odd, struct sk_buff *skb)
15661da177e4SLinus Torvalds {
15675084205fSAl Viro 	__wsum csum;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
15701da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
15711da177e4SLinus Torvalds 	return 0;
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds /*
15751da177e4SLinus Torvalds  *	Generic function to send a packet as reply to another packet.
1576be9f4a44SEric Dumazet  *	Used to send some TCP resets/acks so far.
15771da177e4SLinus Torvalds  */
1578bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
157924a2d43dSEric Dumazet 			   const struct ip_options *sopt,
158024a2d43dSEric Dumazet 			   __be32 daddr, __be32 saddr,
158124a2d43dSEric Dumazet 			   const struct ip_reply_arg *arg,
158270e73416SDavid S. Miller 			   unsigned int len)
15831da177e4SLinus Torvalds {
1584f6d8bd05SEric Dumazet 	struct ip_options_data replyopts;
15851da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
158677968b78SDavid S. Miller 	struct flowi4 fl4;
1587511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
1588bdbbb852SEric Dumazet 	struct net *net = sock_net(sk);
1589be9f4a44SEric Dumazet 	struct sk_buff *nskb;
15904062090eSVasily Averin 	int err;
1591f7ba868bSDavid Ahern 	int oif;
15921da177e4SLinus Torvalds 
159324a2d43dSEric Dumazet 	if (__ip_options_echo(&replyopts.opt.opt, skb, sopt))
15941da177e4SLinus Torvalds 		return;
15951da177e4SLinus Torvalds 
15960a5ebb80SDavid S. Miller 	ipc.addr = daddr;
15971da177e4SLinus Torvalds 	ipc.opt = NULL;
15982244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
1599aa661581SFrancesco Fusco 	ipc.ttl = 0;
1600aa661581SFrancesco Fusco 	ipc.tos = -1;
16011da177e4SLinus Torvalds 
1602f6d8bd05SEric Dumazet 	if (replyopts.opt.opt.optlen) {
16031da177e4SLinus Torvalds 		ipc.opt = &replyopts.opt;
16041da177e4SLinus Torvalds 
1605f6d8bd05SEric Dumazet 		if (replyopts.opt.opt.srr)
1606f6d8bd05SEric Dumazet 			daddr = replyopts.opt.opt.faddr;
16071da177e4SLinus Torvalds 	}
16081da177e4SLinus Torvalds 
1609f7ba868bSDavid Ahern 	oif = arg->bound_dev_if;
16109b6c14d5SDavid Ahern 	if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
16119b6c14d5SDavid Ahern 		oif = skb->skb_iif;
1612f7ba868bSDavid Ahern 
1613f7ba868bSDavid Ahern 	flowi4_init_output(&fl4, oif,
1614e110861fSLorenzo Colitti 			   IP4_REPLY_MARK(net, skb->mark),
161566b13d99SEric Dumazet 			   RT_TOS(arg->tos),
1616be9f4a44SEric Dumazet 			   RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1617538de0e0SDavid S. Miller 			   ip_reply_arg_flowi_flags(arg),
161870e73416SDavid S. Miller 			   daddr, saddr,
1619e2d118a1SLorenzo Colitti 			   tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1620e2d118a1SLorenzo Colitti 			   arg->uid);
16219d6ec938SDavid S. Miller 	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1622be9f4a44SEric Dumazet 	rt = ip_route_output_key(net, &fl4);
1623b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
16241da177e4SLinus Torvalds 		return;
16251da177e4SLinus Torvalds 
1626bdbbb852SEric Dumazet 	inet_sk(sk)->tos = arg->tos;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	sk->sk_priority = skb->priority;
1629eddc9ec5SArnaldo Carvalho de Melo 	sk->sk_protocol = ip_hdr(skb)->protocol;
1630f0e48dbfSPatrick McHardy 	sk->sk_bound_dev_if = arg->bound_dev_if;
1631be9f4a44SEric Dumazet 	sk->sk_sndbuf = sysctl_wmem_default;
16324062090eSVasily Averin 	err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
16334062090eSVasily Averin 			     len, 0, &ipc, &rt, MSG_DONTWAIT);
16344062090eSVasily Averin 	if (unlikely(err)) {
16354062090eSVasily Averin 		ip_flush_pending_frames(sk);
16364062090eSVasily Averin 		goto out;
16374062090eSVasily Averin 	}
16384062090eSVasily Averin 
1639be9f4a44SEric Dumazet 	nskb = skb_peek(&sk->sk_write_queue);
1640be9f4a44SEric Dumazet 	if (nskb) {
16411da177e4SLinus Torvalds 		if (arg->csumoffset >= 0)
1642be9f4a44SEric Dumazet 			*((__sum16 *)skb_transport_header(nskb) +
1643be9f4a44SEric Dumazet 			  arg->csumoffset) = csum_fold(csum_add(nskb->csum,
16449c70220bSArnaldo Carvalho de Melo 								arg->csum));
1645be9f4a44SEric Dumazet 		nskb->ip_summed = CHECKSUM_NONE;
164677968b78SDavid S. Miller 		ip_push_pending_frames(sk, &fl4);
16471da177e4SLinus Torvalds 	}
16484062090eSVasily Averin out:
16491da177e4SLinus Torvalds 	ip_rt_put(rt);
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds void __init ip_init(void)
16531da177e4SLinus Torvalds {
16541da177e4SLinus Torvalds 	ip_rt_init();
16551da177e4SLinus Torvalds 	inet_initpeers();
16561da177e4SLinus Torvalds 
165772c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST)
165872c1d3bdSWANG Cong 	igmp_mc_init();
16591da177e4SLinus Torvalds #endif
16601da177e4SLinus Torvalds }
1661