xref: /linux/net/ipv4/ip_output.c (revision 15e36f5b8e982debe43e425d2e12d34e022d51e9)
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 
457c0f6ba6SLinus 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;
176e05a90ecSJamal Hadi Salim 	if (!skb->mark)
1774a19ec58SLaszlo Attila Toth 		skb->mark = sk->sk_mark;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* Send it out. */
18033224b16SEric W. Biederman 	return ip_local_out(net, skb->sk, skb);
1811da177e4SLinus Torvalds }
182d8c97a94SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
183d8c97a94SArnaldo Carvalho de Melo 
184694869b3SEric W. Biederman static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
1851da177e4SLinus Torvalds {
186adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
18780787ebcSMitsuru Chinen 	struct rtable *rt = (struct rtable *)dst;
1881da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
189c2636b4dSChuck Lever 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
190f6b72b62SDavid S. Miller 	struct neighbour *neigh;
191a263b309SDavid S. Miller 	u32 nexthop;
1921da177e4SLinus Torvalds 
193edf391ffSNeil Horman 	if (rt->rt_type == RTN_MULTICAST) {
1944ba1bf42SEric W. Biederman 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
195edf391ffSNeil Horman 	} else if (rt->rt_type == RTN_BROADCAST)
1964ba1bf42SEric W. Biederman 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
19780787ebcSMitsuru Chinen 
1981da177e4SLinus Torvalds 	/* Be paranoid, rather than too clever. */
1993b04dddeSStephen Hemminger 	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2001da177e4SLinus Torvalds 		struct sk_buff *skb2;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
20351456b29SIan Morris 		if (!skb2) {
2041da177e4SLinus Torvalds 			kfree_skb(skb);
2051da177e4SLinus Torvalds 			return -ENOMEM;
2061da177e4SLinus Torvalds 		}
2071da177e4SLinus Torvalds 		if (skb->sk)
2081da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
2095d0ba55bSEric Dumazet 		consume_skb(skb);
2101da177e4SLinus Torvalds 		skb = skb2;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
21314972cbdSRoopa Prabhu 	if (lwtunnel_xmit_redirect(dst->lwtstate)) {
21414972cbdSRoopa Prabhu 		int res = lwtunnel_xmit(skb);
21514972cbdSRoopa Prabhu 
21614972cbdSRoopa Prabhu 		if (res < 0 || res == LWTUNNEL_XMIT_DONE)
21714972cbdSRoopa Prabhu 			return res;
21814972cbdSRoopa Prabhu 	}
21914972cbdSRoopa Prabhu 
220a263b309SDavid S. Miller 	rcu_read_lock_bh();
221155e8336SJulian Anastasov 	nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
222a263b309SDavid S. Miller 	neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
223a263b309SDavid S. Miller 	if (unlikely(!neigh))
224a263b309SDavid S. Miller 		neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
2259871f1adSVasiliy Kulikov 	if (!IS_ERR(neigh)) {
2264ff06203SJulian Anastasov 		int res;
2274ff06203SJulian Anastasov 
2284ff06203SJulian Anastasov 		sock_confirm_neigh(skb, neigh);
229c16ec185SJulian Anastasov 		res = neigh_output(neigh, skb);
230f2c31e32SEric Dumazet 
231a263b309SDavid S. Miller 		rcu_read_unlock_bh();
232f2c31e32SEric Dumazet 		return res;
233f2c31e32SEric Dumazet 	}
234a263b309SDavid S. Miller 	rcu_read_unlock_bh();
23505e3aa09SDavid S. Miller 
236e87cc472SJoe Perches 	net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
237e87cc472SJoe Perches 			    __func__);
2381da177e4SLinus Torvalds 	kfree_skb(skb);
2391da177e4SLinus Torvalds 	return -EINVAL;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
242694869b3SEric W. Biederman static int ip_finish_output_gso(struct net *net, struct sock *sk,
243694869b3SEric W. Biederman 				struct sk_buff *skb, unsigned int mtu)
244c7ba65d7SFlorian Westphal {
245c7ba65d7SFlorian Westphal 	netdev_features_t features;
246c7ba65d7SFlorian Westphal 	struct sk_buff *segs;
247c7ba65d7SFlorian Westphal 	int ret = 0;
248c7ba65d7SFlorian Westphal 
2499ee6c5dcSLance Richardson 	/* common case: seglen is <= mtu
250359ebda2SShmulik Ladkani 	 */
251779b7931SDaniel Axtens 	if (skb_gso_validate_network_len(skb, mtu))
252694869b3SEric W. Biederman 		return ip_finish_output2(net, sk, skb);
253c7ba65d7SFlorian Westphal 
2540ace81ecSLance Richardson 	/* Slowpath -  GSO segment length exceeds the egress MTU.
255c7ba65d7SFlorian Westphal 	 *
2560ace81ecSLance Richardson 	 * This can happen in several cases:
2570ace81ecSLance Richardson 	 *  - Forwarding of a TCP GRO skb, when DF flag is not set.
2580ace81ecSLance Richardson 	 *  - Forwarding of an skb that arrived on a virtualization interface
2590ace81ecSLance Richardson 	 *    (virtio-net/vhost/tap) with TSO/GSO size set by other network
2600ace81ecSLance Richardson 	 *    stack.
2610ace81ecSLance Richardson 	 *  - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an
2620ace81ecSLance Richardson 	 *    interface with a smaller MTU.
2630ace81ecSLance Richardson 	 *  - Arriving GRO skb (or GSO skb in a virtualized environment) that is
2640ace81ecSLance Richardson 	 *    bridged to a NETIF_F_TSO tunnel stacked over an interface with an
2650ace81ecSLance Richardson 	 *    insufficent MTU.
266c7ba65d7SFlorian Westphal 	 */
267c7ba65d7SFlorian Westphal 	features = netif_skb_features(skb);
2689207f9d4SKonstantin Khlebnikov 	BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
269c7ba65d7SFlorian Westphal 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
270330966e5SFlorian Westphal 	if (IS_ERR_OR_NULL(segs)) {
271c7ba65d7SFlorian Westphal 		kfree_skb(skb);
272c7ba65d7SFlorian Westphal 		return -ENOMEM;
273c7ba65d7SFlorian Westphal 	}
274c7ba65d7SFlorian Westphal 
275c7ba65d7SFlorian Westphal 	consume_skb(skb);
276c7ba65d7SFlorian Westphal 
277c7ba65d7SFlorian Westphal 	do {
278c7ba65d7SFlorian Westphal 		struct sk_buff *nskb = segs->next;
279c7ba65d7SFlorian Westphal 		int err;
280c7ba65d7SFlorian Westphal 
281c7ba65d7SFlorian Westphal 		segs->next = NULL;
282694869b3SEric W. Biederman 		err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
283c7ba65d7SFlorian Westphal 
284c7ba65d7SFlorian Westphal 		if (err && ret == 0)
285c7ba65d7SFlorian Westphal 			ret = err;
286c7ba65d7SFlorian Westphal 		segs = nskb;
287c7ba65d7SFlorian Westphal 	} while (segs);
288c7ba65d7SFlorian Westphal 
289c7ba65d7SFlorian Westphal 	return ret;
290c7ba65d7SFlorian Westphal }
291c7ba65d7SFlorian Westphal 
2920c4b51f0SEric W. Biederman static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
2931da177e4SLinus Torvalds {
294c5501eb3SFlorian Westphal 	unsigned int mtu;
29533b48679SDaniel Mack 	int ret;
29633b48679SDaniel Mack 
29733b48679SDaniel Mack 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
29833b48679SDaniel Mack 	if (ret) {
29933b48679SDaniel Mack 		kfree_skb(skb);
30033b48679SDaniel Mack 		return ret;
30133b48679SDaniel Mack 	}
302c5501eb3SFlorian Westphal 
3035c901daaSPatrick McHardy #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
3045c901daaSPatrick McHardy 	/* Policy lookup after SNAT yielded a new policy */
30500db4124SIan Morris 	if (skb_dst(skb)->xfrm) {
30648d5cad8SPatrick McHardy 		IPCB(skb)->flags |= IPSKB_REROUTED;
30713206b6bSEric W. Biederman 		return dst_output(net, sk, skb);
30848d5cad8SPatrick McHardy 	}
3095c901daaSPatrick McHardy #endif
310fedbb6b4SShmulik Ladkani 	mtu = ip_skb_dst_mtu(sk, skb);
311c7ba65d7SFlorian Westphal 	if (skb_is_gso(skb))
312694869b3SEric W. Biederman 		return ip_finish_output_gso(net, sk, skb, mtu);
313c7ba65d7SFlorian Westphal 
314d6b915e2SFlorian Westphal 	if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU))
315694869b3SEric W. Biederman 		return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
316c7ba65d7SFlorian Westphal 
317694869b3SEric W. Biederman 	return ip_finish_output2(net, sk, skb);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
32033b48679SDaniel Mack static int ip_mc_finish_output(struct net *net, struct sock *sk,
32133b48679SDaniel Mack 			       struct sk_buff *skb)
32233b48679SDaniel Mack {
32333b48679SDaniel Mack 	int ret;
32433b48679SDaniel Mack 
32533b48679SDaniel Mack 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
32633b48679SDaniel Mack 	if (ret) {
32733b48679SDaniel Mack 		kfree_skb(skb);
32833b48679SDaniel Mack 		return ret;
32933b48679SDaniel Mack 	}
33033b48679SDaniel Mack 
33133b48679SDaniel Mack 	return dev_loopback_xmit(net, sk, skb);
33233b48679SDaniel Mack }
33333b48679SDaniel Mack 
334ede2059dSEric W. Biederman int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
3351da177e4SLinus Torvalds {
336511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
337d8d1f30bSChangli Gao 	struct net_device *dev = rt->dst.dev;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/*
3401da177e4SLinus Torvalds 	 *	If the indicated interface is up and running, send the packet.
3411da177e4SLinus Torvalds 	 */
34288f5cc24SEric W. Biederman 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	skb->dev = dev;
3451da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	/*
3481da177e4SLinus Torvalds 	 *	Multicasts are looped back for other local users
3491da177e4SLinus Torvalds 	 */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_MULTICAST) {
3527ad6848cSOctavian Purdila 		if (sk_mc_loop(sk)
3531da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
3541da177e4SLinus Torvalds 		/* Small optimization: do not loopback not local frames,
3551da177e4SLinus Torvalds 		   which returned after forwarding; they will be  dropped
3561da177e4SLinus Torvalds 		   by ip_mr_input in any case.
3571da177e4SLinus Torvalds 		   Note, that local frames are looped back to be delivered
3581da177e4SLinus Torvalds 		   to local recipients.
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 		   This check is duplicated in ip_mr_input at the moment.
3611da177e4SLinus Torvalds 		 */
3629d4fb27dSJoe Perches 		    &&
3639d4fb27dSJoe Perches 		    ((rt->rt_flags & RTCF_LOCAL) ||
3649d4fb27dSJoe Perches 		     !(IPCB(skb)->flags & IPSKB_FORWARDED))
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 		   ) {
3671da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
3681da177e4SLinus Torvalds 			if (newskb)
3699bbc768aSJan Engelhardt 				NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
37029a26a56SEric W. Biederman 					net, sk, newskb, NULL, newskb->dev,
37133b48679SDaniel Mack 					ip_mc_finish_output);
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		/* Multicasts with ttl 0 must not go beyond the host */
3751da177e4SLinus Torvalds 
376eddc9ec5SArnaldo Carvalho de Melo 		if (ip_hdr(skb)->ttl == 0) {
3771da177e4SLinus Torvalds 			kfree_skb(skb);
3781da177e4SLinus Torvalds 			return 0;
3791da177e4SLinus Torvalds 		}
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_BROADCAST) {
3831da177e4SLinus Torvalds 		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
3841da177e4SLinus Torvalds 		if (newskb)
38529a26a56SEric W. Biederman 			NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
38629a26a56SEric W. Biederman 				net, sk, newskb, NULL, newskb->dev,
38733b48679SDaniel Mack 				ip_mc_finish_output);
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
39029a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
39129a26a56SEric W. Biederman 			    net, sk, skb, NULL, skb->dev,
39229a26a56SEric W. Biederman 			    ip_finish_output,
39348d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
396ede2059dSEric W. Biederman int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
3971da177e4SLinus Torvalds {
398adf30907SEric Dumazet 	struct net_device *dev = skb_dst(skb)->dev;
3991bd9bef6SPatrick McHardy 
40088f5cc24SEric W. Biederman 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
4011da177e4SLinus Torvalds 
4021bd9bef6SPatrick McHardy 	skb->dev = dev;
4031bd9bef6SPatrick McHardy 	skb->protocol = htons(ETH_P_IP);
4041bd9bef6SPatrick McHardy 
40529a26a56SEric W. Biederman 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
40629a26a56SEric W. Biederman 			    net, sk, skb, NULL, dev,
40748d5cad8SPatrick McHardy 			    ip_finish_output,
40848d5cad8SPatrick McHardy 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
41184f9307cSEric Dumazet /*
41284f9307cSEric Dumazet  * copy saddr and daddr, possibly using 64bit load/stores
41384f9307cSEric Dumazet  * Equivalent to :
41484f9307cSEric Dumazet  *   iph->saddr = fl4->saddr;
41584f9307cSEric Dumazet  *   iph->daddr = fl4->daddr;
41684f9307cSEric Dumazet  */
41784f9307cSEric Dumazet static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
41884f9307cSEric Dumazet {
41984f9307cSEric Dumazet 	BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
42084f9307cSEric Dumazet 		     offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
42184f9307cSEric Dumazet 	memcpy(&iph->saddr, &fl4->saddr,
42284f9307cSEric Dumazet 	       sizeof(fl4->saddr) + sizeof(fl4->daddr));
42384f9307cSEric Dumazet }
42484f9307cSEric Dumazet 
425b0270e91SEric Dumazet /* Note: skb->sk can be different from sk, in case of tunnels */
426b0270e91SEric Dumazet int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
42977589ce0SEric W. Biederman 	struct net *net = sock_net(sk);
430f6d8bd05SEric Dumazet 	struct ip_options_rcu *inet_opt;
431b57ae01aSDavid S. Miller 	struct flowi4 *fl4;
4321da177e4SLinus Torvalds 	struct rtable *rt;
4331da177e4SLinus Torvalds 	struct iphdr *iph;
434ab6e3febSEric Dumazet 	int res;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* Skip all of this if the packet is already routed,
4371da177e4SLinus Torvalds 	 * f.e. by something like SCTP.
4381da177e4SLinus Torvalds 	 */
439ab6e3febSEric Dumazet 	rcu_read_lock();
440f6d8bd05SEric Dumazet 	inet_opt = rcu_dereference(inet->inet_opt);
441ea4fc0d6SDavid S. Miller 	fl4 = &fl->u.ip4;
442511c3f92SEric Dumazet 	rt = skb_rtable(skb);
44300db4124SIan Morris 	if (rt)
4441da177e4SLinus Torvalds 		goto packet_routed;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/* Make sure we can route this packet. */
4471da177e4SLinus Torvalds 	rt = (struct rtable *)__sk_dst_check(sk, 0);
44851456b29SIan Morris 	if (!rt) {
4493ca3c68eSAl Viro 		__be32 daddr;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		/* Use correct destination address if we have options. */
452c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
453f6d8bd05SEric Dumazet 		if (inet_opt && inet_opt->opt.srr)
454f6d8bd05SEric Dumazet 			daddr = inet_opt->opt.faddr;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 		/* If this fails, retransmit mechanism of transport layer will
4571da177e4SLinus Torvalds 		 * keep trying until route appears or the connection times
4581da177e4SLinus Torvalds 		 * itself out.
4591da177e4SLinus Torvalds 		 */
46077589ce0SEric W. Biederman 		rt = ip_route_output_ports(net, fl4, sk,
46178fbfd8aSDavid S. Miller 					   daddr, inet->inet_saddr,
46278fbfd8aSDavid S. Miller 					   inet->inet_dport,
46378fbfd8aSDavid S. Miller 					   inet->inet_sport,
46478fbfd8aSDavid S. Miller 					   sk->sk_protocol,
46578fbfd8aSDavid S. Miller 					   RT_CONN_FLAGS(sk),
46678fbfd8aSDavid S. Miller 					   sk->sk_bound_dev_if);
467b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
4681da177e4SLinus Torvalds 			goto no_route;
469d8d1f30bSChangli Gao 		sk_setup_caps(sk, &rt->dst);
4701da177e4SLinus Torvalds 	}
471d8d1f30bSChangli Gao 	skb_dst_set_noref(skb, &rt->dst);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds packet_routed:
474155e8336SJulian Anastasov 	if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
4751da177e4SLinus Torvalds 		goto no_route;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* OK, we know where to send it, allocate and build IP header. */
478f6d8bd05SEric Dumazet 	skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
4798856dfa3SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
480eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
481714e85beSAl Viro 	*((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
48260ff7467SWANG Cong 	if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
4831da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
4841da177e4SLinus Torvalds 	else
4851da177e4SLinus Torvalds 		iph->frag_off = 0;
486d8d1f30bSChangli Gao 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
4871da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
48884f9307cSEric Dumazet 	ip_copy_addrs(iph, fl4);
48984f9307cSEric Dumazet 
4901da177e4SLinus Torvalds 	/* Transport layer set skb->h.foo itself. */
4911da177e4SLinus Torvalds 
492f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.optlen) {
493f6d8bd05SEric Dumazet 		iph->ihl += inet_opt->opt.optlen >> 2;
494f6d8bd05SEric Dumazet 		ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 
49777589ce0SEric W. Biederman 	ip_select_ident_segs(net, skb, sk,
498b6a7719aSHannes Frederic Sowa 			     skb_shinfo(skb)->gso_segs ?: 1);
4991da177e4SLinus Torvalds 
500b0270e91SEric Dumazet 	/* TODO : should we use skb->sk here instead of sk ? */
5011da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
5024a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
5031da177e4SLinus Torvalds 
50433224b16SEric W. Biederman 	res = ip_local_out(net, sk, skb);
505ab6e3febSEric Dumazet 	rcu_read_unlock();
506ab6e3febSEric Dumazet 	return res;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds no_route:
509ab6e3febSEric Dumazet 	rcu_read_unlock();
51077589ce0SEric W. Biederman 	IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
5111da177e4SLinus Torvalds 	kfree_skb(skb);
5121da177e4SLinus Torvalds 	return -EHOSTUNREACH;
5131da177e4SLinus Torvalds }
5144bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_queue_xmit);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
5191da177e4SLinus Torvalds 	to->priority = from->priority;
5201da177e4SLinus Torvalds 	to->protocol = from->protocol;
521adf30907SEric Dumazet 	skb_dst_drop(to);
522fe76cda3SEric Dumazet 	skb_dst_copy(to, from);
5231da177e4SLinus Torvalds 	to->dev = from->dev;
52482e91ffeSThomas Graf 	to->mark = from->mark;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	/* Copy the flags to each fragment. */
5271da177e4SLinus Torvalds 	IPCB(to)->flags = IPCB(from)->flags;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
5301da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
5311da177e4SLinus Torvalds #endif
532e7ac05f3SYasuyuki Kozakai 	nf_copy(to, from);
5336ca40d4eSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IP_VS)
534c98d80edSJulian Anastasov 	to->ipvs_property = from->ipvs_property;
535c98d80edSJulian Anastasov #endif
536984bc16cSJames Morris 	skb_copy_secmark(to, from);
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
539694869b3SEric W. Biederman static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
540c5501eb3SFlorian Westphal 		       unsigned int mtu,
541694869b3SEric W. Biederman 		       int (*output)(struct net *, struct sock *, struct sk_buff *))
54249d16b23SAndy Zhou {
54349d16b23SAndy Zhou 	struct iphdr *iph = ip_hdr(skb);
54449d16b23SAndy Zhou 
545d6b915e2SFlorian Westphal 	if ((iph->frag_off & htons(IP_DF)) == 0)
546694869b3SEric W. Biederman 		return ip_do_fragment(net, sk, skb, output);
547d6b915e2SFlorian Westphal 
548d6b915e2SFlorian Westphal 	if (unlikely(!skb->ignore_df ||
54949d16b23SAndy Zhou 		     (IPCB(skb)->frag_max_size &&
55049d16b23SAndy Zhou 		      IPCB(skb)->frag_max_size > mtu))) {
5519479b0afSEric W. Biederman 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
55249d16b23SAndy Zhou 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
55349d16b23SAndy Zhou 			  htonl(mtu));
55449d16b23SAndy Zhou 		kfree_skb(skb);
55549d16b23SAndy Zhou 		return -EMSGSIZE;
55649d16b23SAndy Zhou 	}
55749d16b23SAndy Zhou 
558694869b3SEric W. Biederman 	return ip_do_fragment(net, sk, skb, output);
55949d16b23SAndy Zhou }
56049d16b23SAndy Zhou 
5611da177e4SLinus Torvalds /*
5621da177e4SLinus Torvalds  *	This IP datagram is too large to be sent in one piece.  Break it up into
5631da177e4SLinus Torvalds  *	smaller pieces (each of size equal to IP header plus
5641da177e4SLinus Torvalds  *	a block of the data of the original IP data part) that will yet fit in a
5651da177e4SLinus Torvalds  *	single device frame, and queue such a frame for sending.
5661da177e4SLinus Torvalds  */
5671da177e4SLinus Torvalds 
568694869b3SEric W. Biederman int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
569694869b3SEric W. Biederman 		   int (*output)(struct net *, struct sock *, struct sk_buff *))
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	struct iphdr *iph;
5721da177e4SLinus Torvalds 	int ptr;
5731da177e4SLinus Torvalds 	struct sk_buff *skb2;
574c893b806SChangli Gao 	unsigned int mtu, hlen, left, len, ll_rs;
5751da177e4SLinus Torvalds 	int offset;
57676ab608dSAlexey Dobriyan 	__be16 not_last_frag;
577511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
5781da177e4SLinus Torvalds 	int err = 0;
5791da177e4SLinus Torvalds 
580dbd3393cSHannes Frederic Sowa 	/* for offloaded checksums cleanup checksum before fragmentation */
581dbd3393cSHannes Frederic Sowa 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
582dbd3393cSHannes Frederic Sowa 	    (err = skb_checksum_help(skb)))
583dbd3393cSHannes Frederic Sowa 		goto fail;
584dbd3393cSHannes Frederic Sowa 
5851da177e4SLinus Torvalds 	/*
5861da177e4SLinus Torvalds 	 *	Point into the IP datagram header.
5871da177e4SLinus Torvalds 	 */
5881da177e4SLinus Torvalds 
589eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
5901da177e4SLinus Torvalds 
591fedbb6b4SShmulik Ladkani 	mtu = ip_skb_dst_mtu(sk, skb);
592d6b915e2SFlorian Westphal 	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
593d6b915e2SFlorian Westphal 		mtu = IPCB(skb)->frag_max_size;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	/*
5961da177e4SLinus Torvalds 	 *	Setup starting values.
5971da177e4SLinus Torvalds 	 */
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	hlen = iph->ihl * 4;
600f87c10a8SHannes Frederic Sowa 	mtu = mtu - hlen;	/* Size of data space */
60189cee8b1SHerbert Xu 	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
602254d900bSVasily Averin 	ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* When frag_list is given, use it. First, check its validity:
6051da177e4SLinus Torvalds 	 * some transformers could create wrong frag_list or break existing
6061da177e4SLinus Torvalds 	 * one, it is not prohibited. In this case fall back to copying.
6071da177e4SLinus Torvalds 	 *
6081da177e4SLinus Torvalds 	 * LATER: this step can be merged to real generation of fragments,
6091da177e4SLinus Torvalds 	 * we can switch to copy when see the first bad fragment.
6101da177e4SLinus Torvalds 	 */
61121dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
6123d13008eSEric Dumazet 		struct sk_buff *frag, *frag2;
613c72d8cdaSAlexey Dobriyan 		unsigned int first_len = skb_pagelen(skb);
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
6161da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
61756f8a75cSPaul Gortmaker 		    ip_is_fragment(iph) ||
618254d900bSVasily Averin 		    skb_cloned(skb) ||
619254d900bSVasily Averin 		    skb_headroom(skb) < ll_rs)
6201da177e4SLinus Torvalds 			goto slow_path;
6211da177e4SLinus Torvalds 
622d7fcf1a5SDavid S. Miller 		skb_walk_frags(skb, frag) {
6231da177e4SLinus Torvalds 			/* Correct geometry. */
6241da177e4SLinus Torvalds 			if (frag->len > mtu ||
6251da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
626254d900bSVasily Averin 			    skb_headroom(frag) < hlen + ll_rs)
6273d13008eSEric Dumazet 				goto slow_path_clean;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 			/* Partially cloned skb? */
6301da177e4SLinus Torvalds 			if (skb_shared(frag))
6313d13008eSEric Dumazet 				goto slow_path_clean;
6322fdba6b0SHerbert Xu 
6332fdba6b0SHerbert Xu 			BUG_ON(frag->sk);
6342fdba6b0SHerbert Xu 			if (skb->sk) {
6352fdba6b0SHerbert Xu 				frag->sk = skb->sk;
6362fdba6b0SHerbert Xu 				frag->destructor = sock_wfree;
6372fdba6b0SHerbert Xu 			}
6383d13008eSEric Dumazet 			skb->truesize -= frag->truesize;
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		/* Everything is OK. Generate! */
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 		err = 0;
6441da177e4SLinus Torvalds 		offset = 0;
6451da177e4SLinus Torvalds 		frag = skb_shinfo(skb)->frag_list;
646d7fcf1a5SDavid S. Miller 		skb_frag_list_init(skb);
6471da177e4SLinus Torvalds 		skb->data_len = first_len - skb_headlen(skb);
6481da177e4SLinus Torvalds 		skb->len = first_len;
6491da177e4SLinus Torvalds 		iph->tot_len = htons(first_len);
6501da177e4SLinus Torvalds 		iph->frag_off = htons(IP_MF);
6511da177e4SLinus Torvalds 		ip_send_check(iph);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 		for (;;) {
6541da177e4SLinus Torvalds 			/* Prepare header of the next frame,
6551da177e4SLinus Torvalds 			 * before previous one went down. */
6561da177e4SLinus Torvalds 			if (frag) {
6571da177e4SLinus Torvalds 				frag->ip_summed = CHECKSUM_NONE;
658badff6d0SArnaldo Carvalho de Melo 				skb_reset_transport_header(frag);
659e2d1bca7SArnaldo Carvalho de Melo 				__skb_push(frag, hlen);
660e2d1bca7SArnaldo Carvalho de Melo 				skb_reset_network_header(frag);
661d56f90a7SArnaldo Carvalho de Melo 				memcpy(skb_network_header(frag), iph, hlen);
662eddc9ec5SArnaldo Carvalho de Melo 				iph = ip_hdr(frag);
6631da177e4SLinus Torvalds 				iph->tot_len = htons(frag->len);
6641da177e4SLinus Torvalds 				ip_copy_metadata(frag, skb);
6651da177e4SLinus Torvalds 				if (offset == 0)
6661da177e4SLinus Torvalds 					ip_options_fragment(frag);
6671da177e4SLinus Torvalds 				offset += skb->len - hlen;
6681da177e4SLinus Torvalds 				iph->frag_off = htons(offset>>3);
66900db4124SIan Morris 				if (frag->next)
6701da177e4SLinus Torvalds 					iph->frag_off |= htons(IP_MF);
6711da177e4SLinus Torvalds 				/* Ready, complete checksum */
6721da177e4SLinus Torvalds 				ip_send_check(iph);
6731da177e4SLinus Torvalds 			}
6741da177e4SLinus Torvalds 
675694869b3SEric W. Biederman 			err = output(net, sk, skb);
6761da177e4SLinus Torvalds 
677dafee490SWei Dong 			if (!err)
67826a949dbSEric W. Biederman 				IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
6791da177e4SLinus Torvalds 			if (err || !frag)
6801da177e4SLinus Torvalds 				break;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 			skb = frag;
6831da177e4SLinus Torvalds 			frag = skb->next;
6841da177e4SLinus Torvalds 			skb->next = NULL;
6851da177e4SLinus Torvalds 		}
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 		if (err == 0) {
68826a949dbSEric W. Biederman 			IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
6891da177e4SLinus Torvalds 			return 0;
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 		while (frag) {
6931da177e4SLinus Torvalds 			skb = frag->next;
6941da177e4SLinus Torvalds 			kfree_skb(frag);
6951da177e4SLinus Torvalds 			frag = skb;
6961da177e4SLinus Torvalds 		}
69726a949dbSEric W. Biederman 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
6981da177e4SLinus Torvalds 		return err;
6993d13008eSEric Dumazet 
7003d13008eSEric Dumazet slow_path_clean:
7013d13008eSEric Dumazet 		skb_walk_frags(skb, frag2) {
7023d13008eSEric Dumazet 			if (frag2 == frag)
7033d13008eSEric Dumazet 				break;
7043d13008eSEric Dumazet 			frag2->sk = NULL;
7053d13008eSEric Dumazet 			frag2->destructor = NULL;
7063d13008eSEric Dumazet 			skb->truesize += frag2->truesize;
7073d13008eSEric Dumazet 		}
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds slow_path:
711c9af6db4SPravin B Shelar 	iph = ip_hdr(skb);
712fc70fb64SAlexander Duyck 
7131da177e4SLinus Torvalds 	left = skb->len - hlen;		/* Space per frame */
71449085bd7SGeorge Kadianakis 	ptr = hlen;		/* Where to start from */
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/*
7171da177e4SLinus Torvalds 	 *	Fragment the datagram.
7181da177e4SLinus Torvalds 	 */
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
7211da177e4SLinus Torvalds 	not_last_frag = iph->frag_off & htons(IP_MF);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	/*
7241da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
7251da177e4SLinus Torvalds 	 */
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	while (left > 0) {
7281da177e4SLinus Torvalds 		len = left;
7291da177e4SLinus Torvalds 		/* IF: it doesn't fit, use 'mtu' - the data space left */
7301da177e4SLinus Torvalds 		if (len > mtu)
7311da177e4SLinus Torvalds 			len = mtu;
7321da177e4SLinus Torvalds 		/* IF: we are not sending up to and including the packet end
7331da177e4SLinus Torvalds 		   then align the next start on an eight byte boundary */
7341da177e4SLinus Torvalds 		if (len < left)	{
7351da177e4SLinus Torvalds 			len &= ~7;
7361da177e4SLinus Torvalds 		}
7371da177e4SLinus Torvalds 
738cbffccc9SJoe Perches 		/* Allocate buffer */
739cbffccc9SJoe Perches 		skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
740cbffccc9SJoe Perches 		if (!skb2) {
7411da177e4SLinus Torvalds 			err = -ENOMEM;
7421da177e4SLinus Torvalds 			goto fail;
7431da177e4SLinus Torvalds 		}
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 		/*
7461da177e4SLinus Torvalds 		 *	Set up data on packet
7471da177e4SLinus Torvalds 		 */
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 		ip_copy_metadata(skb2, skb);
7501da177e4SLinus Torvalds 		skb_reserve(skb2, ll_rs);
7511da177e4SLinus Torvalds 		skb_put(skb2, len + hlen);
752c1d2bbe1SArnaldo Carvalho de Melo 		skb_reset_network_header(skb2);
753b0e380b1SArnaldo Carvalho de Melo 		skb2->transport_header = skb2->network_header + hlen;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		/*
7561da177e4SLinus Torvalds 		 *	Charge the memory for the fragment to any owner
7571da177e4SLinus Torvalds 		 *	it might possess
7581da177e4SLinus Torvalds 		 */
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		if (skb->sk)
7611da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 		/*
7641da177e4SLinus Torvalds 		 *	Copy the packet header into the new buffer.
7651da177e4SLinus Torvalds 		 */
7661da177e4SLinus Torvalds 
767d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 		/*
7701da177e4SLinus Torvalds 		 *	Copy a block of the IP datagram.
7711da177e4SLinus Torvalds 		 */
772bff9b61cSArnaldo Carvalho de Melo 		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
7731da177e4SLinus Torvalds 			BUG();
7741da177e4SLinus Torvalds 		left -= len;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		/*
7771da177e4SLinus Torvalds 		 *	Fill in the new header fields.
7781da177e4SLinus Torvalds 		 */
779eddc9ec5SArnaldo Carvalho de Melo 		iph = ip_hdr(skb2);
7801da177e4SLinus Torvalds 		iph->frag_off = htons((offset >> 3));
7811da177e4SLinus Torvalds 
782d6b915e2SFlorian Westphal 		if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
783d6b915e2SFlorian Westphal 			iph->frag_off |= htons(IP_DF);
784d6b915e2SFlorian Westphal 
7851da177e4SLinus Torvalds 		/* ANK: dirty, but effective trick. Upgrade options only if
7861da177e4SLinus Torvalds 		 * the segment to be fragmented was THE FIRST (otherwise,
7871da177e4SLinus Torvalds 		 * options are already fixed) and make it ONCE
7881da177e4SLinus Torvalds 		 * on the initial skb, so that all the following fragments
7891da177e4SLinus Torvalds 		 * will inherit fixed options.
7901da177e4SLinus Torvalds 		 */
7911da177e4SLinus Torvalds 		if (offset == 0)
7921da177e4SLinus Torvalds 			ip_options_fragment(skb);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 		/*
7951da177e4SLinus Torvalds 		 *	Added AC : If we are fragmenting a fragment that's not the
7961da177e4SLinus Torvalds 		 *		   last fragment then keep MF on each bit
7971da177e4SLinus Torvalds 		 */
7981da177e4SLinus Torvalds 		if (left > 0 || not_last_frag)
7991da177e4SLinus Torvalds 			iph->frag_off |= htons(IP_MF);
8001da177e4SLinus Torvalds 		ptr += len;
8011da177e4SLinus Torvalds 		offset += len;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		/*
8041da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
8051da177e4SLinus Torvalds 		 */
8061da177e4SLinus Torvalds 		iph->tot_len = htons(len + hlen);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 		ip_send_check(iph);
8091da177e4SLinus Torvalds 
810694869b3SEric W. Biederman 		err = output(net, sk, skb2);
8111da177e4SLinus Torvalds 		if (err)
8121da177e4SLinus Torvalds 			goto fail;
813dafee490SWei Dong 
81426a949dbSEric W. Biederman 		IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
8151da177e4SLinus Torvalds 	}
8165d0ba55bSEric Dumazet 	consume_skb(skb);
81726a949dbSEric W. Biederman 	IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
8181da177e4SLinus Torvalds 	return err;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds fail:
8211da177e4SLinus Torvalds 	kfree_skb(skb);
82226a949dbSEric W. Biederman 	IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
8231da177e4SLinus Torvalds 	return err;
8241da177e4SLinus Torvalds }
82549d16b23SAndy Zhou EXPORT_SYMBOL(ip_do_fragment);
8262e2f7aefSPatrick McHardy 
8271da177e4SLinus Torvalds int
8281da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
8291da177e4SLinus Torvalds {
830f69e6d13SAl Viro 	struct msghdr *msg = from;
8311da177e4SLinus Torvalds 
83284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
8330b62fca2SAl Viro 		if (!copy_from_iter_full(to, len, &msg->msg_iter))
8341da177e4SLinus Torvalds 			return -EFAULT;
8351da177e4SLinus Torvalds 	} else {
83644bb9363SAl Viro 		__wsum csum = 0;
8370b62fca2SAl Viro 		if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
8381da177e4SLinus Torvalds 			return -EFAULT;
8391da177e4SLinus Torvalds 		skb->csum = csum_block_add(skb->csum, csum, odd);
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 	return 0;
8421da177e4SLinus Torvalds }
8434bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_generic_getfrag);
8441da177e4SLinus Torvalds 
84544bb9363SAl Viro static inline __wsum
8461da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	char *kaddr;
84944bb9363SAl Viro 	__wsum csum;
8501da177e4SLinus Torvalds 	kaddr = kmap(page);
8511da177e4SLinus Torvalds 	csum = csum_partial(kaddr + offset, copy, 0);
8521da177e4SLinus Torvalds 	kunmap(page);
8531da177e4SLinus Torvalds 	return csum;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
856f5fca608SDavid S. Miller static int __ip_append_data(struct sock *sk,
857f5fca608SDavid S. Miller 			    struct flowi4 *fl4,
858f5fca608SDavid S. Miller 			    struct sk_buff_head *queue,
8591470ddf7SHerbert Xu 			    struct inet_cork *cork,
8605640f768SEric Dumazet 			    struct page_frag *pfrag,
8611470ddf7SHerbert Xu 			    int getfrag(void *from, char *to, int offset,
8621470ddf7SHerbert Xu 					int len, int odd, struct sk_buff *skb),
8631da177e4SLinus Torvalds 			    void *from, int length, int transhdrlen,
8641da177e4SLinus Torvalds 			    unsigned int flags)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
8671da177e4SLinus Torvalds 	struct sk_buff *skb;
8681da177e4SLinus Torvalds 
86907df5294SHerbert Xu 	struct ip_options *opt = cork->opt;
8701da177e4SLinus Torvalds 	int hh_len;
8711da177e4SLinus Torvalds 	int exthdrlen;
8721da177e4SLinus Torvalds 	int mtu;
8731da177e4SLinus Torvalds 	int copy;
8741da177e4SLinus Torvalds 	int err;
8751da177e4SLinus Torvalds 	int offset = 0;
876daba287bSHannes Frederic Sowa 	unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
8771da177e4SLinus Torvalds 	int csummode = CHECKSUM_NONE;
8781470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
879694aba69SEric Dumazet 	unsigned int wmem_alloc_delta = 0;
88009c2d251SWillem de Bruijn 	u32 tskey = 0;
881*15e36f5bSWillem de Bruijn 	bool paged;
8821da177e4SLinus Torvalds 
88396d7303eSSteffen Klassert 	skb = skb_peek_tail(queue);
88496d7303eSSteffen Klassert 
88596d7303eSSteffen Klassert 	exthdrlen = !skb ? rt->dst.header_len : 0;
886bec1f6f6SWillem de Bruijn 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
887*15e36f5bSWillem de Bruijn 	paged = !!cork->gso_size;
888bec1f6f6SWillem de Bruijn 
88909c2d251SWillem de Bruijn 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
89009c2d251SWillem de Bruijn 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
89109c2d251SWillem de Bruijn 		tskey = sk->sk_tskey++;
8921470ddf7SHerbert Xu 
893d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
8961da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
89760ff7467SWANG Cong 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
8981da177e4SLinus Torvalds 
899daba287bSHannes Frederic Sowa 	if (cork->length + length > maxnonfragsize - fragheaderlen) {
900f5fca608SDavid S. Miller 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
90161e7f09dSHannes Frederic Sowa 			       mtu - (opt ? opt->optlen : 0));
9021da177e4SLinus Torvalds 		return -EMSGSIZE;
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	/*
9061da177e4SLinus Torvalds 	 * transhdrlen > 0 means that this is the first fragment and we wish
9071da177e4SLinus Torvalds 	 * it won't be fragmented in the future.
9081da177e4SLinus Torvalds 	 */
9091da177e4SLinus Torvalds 	if (transhdrlen &&
9101da177e4SLinus Torvalds 	    length + fragheaderlen <= mtu &&
911c8cd0989STom Herbert 	    rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
912bec1f6f6SWillem de Bruijn 	    (!(flags & MSG_MORE) || cork->gso_size) &&
9131da177e4SLinus Torvalds 	    !exthdrlen)
91484fa7933SPatrick McHardy 		csummode = CHECKSUM_PARTIAL;
9151da177e4SLinus Torvalds 
9161470ddf7SHerbert Xu 	cork->length += length;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/* So, what's going on in the loop below?
9191da177e4SLinus Torvalds 	 *
9201da177e4SLinus Torvalds 	 * We use calculated fragment length to generate chained skb,
9211da177e4SLinus Torvalds 	 * each of segments is IP fragment ready for sending to network after
9221da177e4SLinus Torvalds 	 * adding appropriate IP header.
9231da177e4SLinus Torvalds 	 */
9241da177e4SLinus Torvalds 
92526cde9f7SHerbert Xu 	if (!skb)
9261da177e4SLinus Torvalds 		goto alloc_new_skb;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	while (length > 0) {
9291da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
9301da177e4SLinus Torvalds 		copy = mtu - skb->len;
9311da177e4SLinus Torvalds 		if (copy < length)
9321da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
9331da177e4SLinus Torvalds 		if (copy <= 0) {
9341da177e4SLinus Torvalds 			char *data;
9351da177e4SLinus Torvalds 			unsigned int datalen;
9361da177e4SLinus Torvalds 			unsigned int fraglen;
9371da177e4SLinus Torvalds 			unsigned int fraggap;
9381da177e4SLinus Torvalds 			unsigned int alloclen;
939*15e36f5bSWillem de Bruijn 			unsigned int pagedlen = 0;
9401da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
9411da177e4SLinus Torvalds alloc_new_skb:
9421da177e4SLinus Torvalds 			skb_prev = skb;
9431da177e4SLinus Torvalds 			if (skb_prev)
9441da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
9451da177e4SLinus Torvalds 			else
9461da177e4SLinus Torvalds 				fraggap = 0;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 			/*
9491da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
9501da177e4SLinus Torvalds 			 * we know we need more fragment(s).
9511da177e4SLinus Torvalds 			 */
9521da177e4SLinus Torvalds 			datalen = length + fraggap;
9531da177e4SLinus Torvalds 			if (datalen > mtu - fragheaderlen)
9541da177e4SLinus Torvalds 				datalen = maxfraglen - fragheaderlen;
9551da177e4SLinus Torvalds 			fraglen = datalen + fragheaderlen;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
958d8d1f30bSChangli Gao 			    !(rt->dst.dev->features&NETIF_F_SG))
9591da177e4SLinus Torvalds 				alloclen = mtu;
960*15e36f5bSWillem de Bruijn 			else if (!paged)
96159104f06SEric Dumazet 				alloclen = fraglen;
962*15e36f5bSWillem de Bruijn 			else {
963*15e36f5bSWillem de Bruijn 				alloclen = min_t(int, fraglen, MAX_HEADER);
964*15e36f5bSWillem de Bruijn 				pagedlen = fraglen - alloclen;
965*15e36f5bSWillem de Bruijn 			}
9661da177e4SLinus Torvalds 
967353e5c9aSSteffen Klassert 			alloclen += exthdrlen;
968353e5c9aSSteffen Klassert 
9691da177e4SLinus Torvalds 			/* The last fragment gets additional space at tail.
9701da177e4SLinus Torvalds 			 * Note, with MSG_MORE we overallocate on fragments,
9711da177e4SLinus Torvalds 			 * because we have no idea what fragment will be
9721da177e4SLinus Torvalds 			 * the last.
9731da177e4SLinus Torvalds 			 */
97433f99dc7SSteffen Klassert 			if (datalen == length + fraggap)
975d8d1f30bSChangli Gao 				alloclen += rt->dst.trailer_len;
97633f99dc7SSteffen Klassert 
9771da177e4SLinus Torvalds 			if (transhdrlen) {
9781da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
9791da177e4SLinus Torvalds 						alloclen + hh_len + 15,
9801da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
9811da177e4SLinus Torvalds 			} else {
9821da177e4SLinus Torvalds 				skb = NULL;
983694aba69SEric Dumazet 				if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
9841da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
985694aba69SEric Dumazet 					skb = alloc_skb(alloclen + hh_len + 15,
9861da177e4SLinus Torvalds 							sk->sk_allocation);
98751456b29SIan Morris 				if (unlikely(!skb))
9881da177e4SLinus Torvalds 					err = -ENOBUFS;
9891da177e4SLinus Torvalds 			}
99051456b29SIan Morris 			if (!skb)
9911da177e4SLinus Torvalds 				goto error;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 			/*
9941da177e4SLinus Torvalds 			 *	Fill in the control structures
9951da177e4SLinus Torvalds 			 */
9961da177e4SLinus Torvalds 			skb->ip_summed = csummode;
9971da177e4SLinus Torvalds 			skb->csum = 0;
9981da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
99911878b40SWillem de Bruijn 
100011878b40SWillem de Bruijn 			/* only the initial fragment is time stamped */
10011470ddf7SHerbert Xu 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
100211878b40SWillem de Bruijn 			cork->tx_flags = 0;
100309c2d251SWillem de Bruijn 			skb_shinfo(skb)->tskey = tskey;
100409c2d251SWillem de Bruijn 			tskey = 0;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 			/*
10071da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
10081da177e4SLinus Torvalds 			 */
1009*15e36f5bSWillem de Bruijn 			data = skb_put(skb, fraglen + exthdrlen - pagedlen);
1010c14d2450SArnaldo Carvalho de Melo 			skb_set_network_header(skb, exthdrlen);
1011b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1012b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
1013353e5c9aSSteffen Klassert 			data += fragheaderlen + exthdrlen;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 			if (fraggap) {
10161da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
10171da177e4SLinus Torvalds 					skb_prev, maxfraglen,
10181da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
10191da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
10201da177e4SLinus Torvalds 							  skb->csum);
10211da177e4SLinus Torvalds 				data += fraggap;
1022e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
10231da177e4SLinus Torvalds 			}
10241da177e4SLinus Torvalds 
1025*15e36f5bSWillem de Bruijn 			copy = datalen - transhdrlen - fraggap - pagedlen;
10261da177e4SLinus Torvalds 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
10271da177e4SLinus Torvalds 				err = -EFAULT;
10281da177e4SLinus Torvalds 				kfree_skb(skb);
10291da177e4SLinus Torvalds 				goto error;
10301da177e4SLinus Torvalds 			}
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 			offset += copy;
1033*15e36f5bSWillem de Bruijn 			length -= copy + transhdrlen;
10341da177e4SLinus Torvalds 			transhdrlen = 0;
10351da177e4SLinus Torvalds 			exthdrlen = 0;
10361da177e4SLinus Torvalds 			csummode = CHECKSUM_NONE;
10371da177e4SLinus Torvalds 
10380dec879fSJulian Anastasov 			if ((flags & MSG_CONFIRM) && !skb_prev)
10390dec879fSJulian Anastasov 				skb_set_dst_pending_confirm(skb, 1);
10400dec879fSJulian Anastasov 
10411da177e4SLinus Torvalds 			/*
10421da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
10431da177e4SLinus Torvalds 			 */
1044694aba69SEric Dumazet 			if (!skb->destructor) {
1045694aba69SEric Dumazet 				skb->destructor = sock_wfree;
1046694aba69SEric Dumazet 				skb->sk = sk;
1047694aba69SEric Dumazet 				wmem_alloc_delta += skb->truesize;
1048694aba69SEric Dumazet 			}
10491470ddf7SHerbert Xu 			__skb_queue_tail(queue, skb);
10501da177e4SLinus Torvalds 			continue;
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		if (copy > length)
10541da177e4SLinus Torvalds 			copy = length;
10551da177e4SLinus Torvalds 
1056d8d1f30bSChangli Gao 		if (!(rt->dst.dev->features&NETIF_F_SG)) {
10571da177e4SLinus Torvalds 			unsigned int off;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 			off = skb->len;
10601da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
10611da177e4SLinus Torvalds 					offset, copy, off, skb) < 0) {
10621da177e4SLinus Torvalds 				__skb_trim(skb, off);
10631da177e4SLinus Torvalds 				err = -EFAULT;
10641da177e4SLinus Torvalds 				goto error;
10651da177e4SLinus Torvalds 			}
10661da177e4SLinus Torvalds 		} else {
10671da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 			err = -ENOMEM;
10705640f768SEric Dumazet 			if (!sk_page_frag_refill(sk, pfrag))
10711da177e4SLinus Torvalds 				goto error;
10721da177e4SLinus Torvalds 
10735640f768SEric Dumazet 			if (!skb_can_coalesce(skb, i, pfrag->page,
10745640f768SEric Dumazet 					      pfrag->offset)) {
10751da177e4SLinus Torvalds 				err = -EMSGSIZE;
10765640f768SEric Dumazet 				if (i == MAX_SKB_FRAGS)
10771da177e4SLinus Torvalds 					goto error;
10785640f768SEric Dumazet 
10795640f768SEric Dumazet 				__skb_fill_page_desc(skb, i, pfrag->page,
10805640f768SEric Dumazet 						     pfrag->offset, 0);
10815640f768SEric Dumazet 				skb_shinfo(skb)->nr_frags = ++i;
10825640f768SEric Dumazet 				get_page(pfrag->page);
10831da177e4SLinus Torvalds 			}
10845640f768SEric Dumazet 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
10855640f768SEric Dumazet 			if (getfrag(from,
10865640f768SEric Dumazet 				    page_address(pfrag->page) + pfrag->offset,
10875640f768SEric Dumazet 				    offset, copy, skb->len, skb) < 0)
10885640f768SEric Dumazet 				goto error_efault;
10895640f768SEric Dumazet 
10905640f768SEric Dumazet 			pfrag->offset += copy;
10915640f768SEric Dumazet 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
10921da177e4SLinus Torvalds 			skb->len += copy;
10931da177e4SLinus Torvalds 			skb->data_len += copy;
1094f945fa7aSHerbert Xu 			skb->truesize += copy;
1095694aba69SEric Dumazet 			wmem_alloc_delta += copy;
10961da177e4SLinus Torvalds 		}
10971da177e4SLinus Torvalds 		offset += copy;
10981da177e4SLinus Torvalds 		length -= copy;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 
11019e8445a5SPaolo Abeni 	if (wmem_alloc_delta)
1102694aba69SEric Dumazet 		refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
11031da177e4SLinus Torvalds 	return 0;
11041da177e4SLinus Torvalds 
11055640f768SEric Dumazet error_efault:
11065640f768SEric Dumazet 	err = -EFAULT;
11071da177e4SLinus Torvalds error:
11081470ddf7SHerbert Xu 	cork->length -= length;
11095e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1110694aba69SEric Dumazet 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
11111da177e4SLinus Torvalds 	return err;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
11141470ddf7SHerbert Xu static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
11151470ddf7SHerbert Xu 			 struct ipcm_cookie *ipc, struct rtable **rtp)
11161470ddf7SHerbert Xu {
1117f6d8bd05SEric Dumazet 	struct ip_options_rcu *opt;
11181470ddf7SHerbert Xu 	struct rtable *rt;
11191470ddf7SHerbert Xu 
11209783ccd0SGao Feng 	rt = *rtp;
11219783ccd0SGao Feng 	if (unlikely(!rt))
11229783ccd0SGao Feng 		return -EFAULT;
11239783ccd0SGao Feng 
11241470ddf7SHerbert Xu 	/*
11251470ddf7SHerbert Xu 	 * setup for corking.
11261470ddf7SHerbert Xu 	 */
11271470ddf7SHerbert Xu 	opt = ipc->opt;
11281470ddf7SHerbert Xu 	if (opt) {
112951456b29SIan Morris 		if (!cork->opt) {
11301470ddf7SHerbert Xu 			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
11311470ddf7SHerbert Xu 					    sk->sk_allocation);
113251456b29SIan Morris 			if (unlikely(!cork->opt))
11331470ddf7SHerbert Xu 				return -ENOBUFS;
11341470ddf7SHerbert Xu 		}
1135f6d8bd05SEric Dumazet 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
11361470ddf7SHerbert Xu 		cork->flags |= IPCORK_OPT;
11371470ddf7SHerbert Xu 		cork->addr = ipc->addr;
11381470ddf7SHerbert Xu 	}
11399783ccd0SGao Feng 
11401470ddf7SHerbert Xu 	/*
11411470ddf7SHerbert Xu 	 * We steal reference to this route, caller should not release it
11421470ddf7SHerbert Xu 	 */
11431470ddf7SHerbert Xu 	*rtp = NULL;
1144482fc609SHannes Frederic Sowa 	cork->fragsize = ip_sk_use_pmtu(sk) ?
1145482fc609SHannes Frederic Sowa 			 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
1146bec1f6f6SWillem de Bruijn 
1147bec1f6f6SWillem de Bruijn 	cork->gso_size = sk->sk_type == SOCK_DGRAM ? ipc->gso_size : 0;
11481470ddf7SHerbert Xu 	cork->dst = &rt->dst;
11491470ddf7SHerbert Xu 	cork->length = 0;
1150aa661581SFrancesco Fusco 	cork->ttl = ipc->ttl;
1151aa661581SFrancesco Fusco 	cork->tos = ipc->tos;
1152aa661581SFrancesco Fusco 	cork->priority = ipc->priority;
11531470ddf7SHerbert Xu 	cork->tx_flags = ipc->tx_flags;
11541470ddf7SHerbert Xu 
11551470ddf7SHerbert Xu 	return 0;
11561470ddf7SHerbert Xu }
11571470ddf7SHerbert Xu 
11581470ddf7SHerbert Xu /*
11591470ddf7SHerbert Xu  *	ip_append_data() and ip_append_page() can make one large IP datagram
11601470ddf7SHerbert Xu  *	from many pieces of data. Each pieces will be holded on the socket
11611470ddf7SHerbert Xu  *	until ip_push_pending_frames() is called. Each piece can be a page
11621470ddf7SHerbert Xu  *	or non-page data.
11631470ddf7SHerbert Xu  *
11641470ddf7SHerbert Xu  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
11651470ddf7SHerbert Xu  *	this interface potentially.
11661470ddf7SHerbert Xu  *
11671470ddf7SHerbert Xu  *	LATER: length must be adjusted by pad at tail, when it is required.
11681470ddf7SHerbert Xu  */
1169f5fca608SDavid S. Miller int ip_append_data(struct sock *sk, struct flowi4 *fl4,
11701470ddf7SHerbert Xu 		   int getfrag(void *from, char *to, int offset, int len,
11711470ddf7SHerbert Xu 			       int odd, struct sk_buff *skb),
11721470ddf7SHerbert Xu 		   void *from, int length, int transhdrlen,
11731470ddf7SHerbert Xu 		   struct ipcm_cookie *ipc, struct rtable **rtp,
11741470ddf7SHerbert Xu 		   unsigned int flags)
11751470ddf7SHerbert Xu {
11761470ddf7SHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
11771470ddf7SHerbert Xu 	int err;
11781470ddf7SHerbert Xu 
11791470ddf7SHerbert Xu 	if (flags&MSG_PROBE)
11801470ddf7SHerbert Xu 		return 0;
11811470ddf7SHerbert Xu 
11821470ddf7SHerbert Xu 	if (skb_queue_empty(&sk->sk_write_queue)) {
1183bdc712b4SDavid S. Miller 		err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
11841470ddf7SHerbert Xu 		if (err)
11851470ddf7SHerbert Xu 			return err;
11861470ddf7SHerbert Xu 	} else {
11871470ddf7SHerbert Xu 		transhdrlen = 0;
11881470ddf7SHerbert Xu 	}
11891470ddf7SHerbert Xu 
11905640f768SEric Dumazet 	return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
11915640f768SEric Dumazet 				sk_page_frag(sk), getfrag,
11921470ddf7SHerbert Xu 				from, length, transhdrlen, flags);
11931470ddf7SHerbert Xu }
11941470ddf7SHerbert Xu 
1195f5fca608SDavid S. Miller ssize_t	ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
11961da177e4SLinus Torvalds 		       int offset, size_t size, int flags)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11991da177e4SLinus Torvalds 	struct sk_buff *skb;
12001da177e4SLinus Torvalds 	struct rtable *rt;
12011da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
1202bdc712b4SDavid S. Miller 	struct inet_cork *cork;
12031da177e4SLinus Torvalds 	int hh_len;
12041da177e4SLinus Torvalds 	int mtu;
12051da177e4SLinus Torvalds 	int len;
12061da177e4SLinus Torvalds 	int err;
1207daba287bSHannes Frederic Sowa 	unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	if (inet->hdrincl)
12101da177e4SLinus Torvalds 		return -EPERM;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
12131da177e4SLinus Torvalds 		return 0;
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue))
12161da177e4SLinus Torvalds 		return -EINVAL;
12171da177e4SLinus Torvalds 
1218bdc712b4SDavid S. Miller 	cork = &inet->cork.base;
1219bdc712b4SDavid S. Miller 	rt = (struct rtable *)cork->dst;
1220bdc712b4SDavid S. Miller 	if (cork->flags & IPCORK_OPT)
1221bdc712b4SDavid S. Miller 		opt = cork->opt;
12221da177e4SLinus Torvalds 
1223d8d1f30bSChangli Gao 	if (!(rt->dst.dev->features&NETIF_F_SG))
12241da177e4SLinus Torvalds 		return -EOPNOTSUPP;
12251da177e4SLinus Torvalds 
1226d8d1f30bSChangli Gao 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1227bec1f6f6SWillem de Bruijn 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
12301da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
123160ff7467SWANG Cong 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
12321da177e4SLinus Torvalds 
1233daba287bSHannes Frederic Sowa 	if (cork->length + size > maxnonfragsize - fragheaderlen) {
123461e7f09dSHannes Frederic Sowa 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
123561e7f09dSHannes Frederic Sowa 			       mtu - (opt ? opt->optlen : 0));
12361da177e4SLinus Torvalds 		return -EMSGSIZE;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 
123951456b29SIan Morris 	skb = skb_peek_tail(&sk->sk_write_queue);
124051456b29SIan Morris 	if (!skb)
12411da177e4SLinus Torvalds 		return -EINVAL;
12421da177e4SLinus Torvalds 
1243a8c4a252SHannes Frederic Sowa 	cork->length += size;
1244e89e9cf5SAnanda Raju 
12451da177e4SLinus Torvalds 	while (size > 0) {
12461da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
12471da177e4SLinus Torvalds 		len = mtu - skb->len;
12481da177e4SLinus Torvalds 		if (len < size)
12491da177e4SLinus Torvalds 			len = maxfraglen - skb->len;
1250ab2fb7e3SWillem de Bruijn 
12511da177e4SLinus Torvalds 		if (len <= 0) {
12521da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
12531da177e4SLinus Torvalds 			int alloclen;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 			skb_prev = skb;
12561da177e4SLinus Torvalds 			fraggap = skb_prev->len - maxfraglen;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 			alloclen = fragheaderlen + hh_len + fraggap + 15;
12591da177e4SLinus Torvalds 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
12601da177e4SLinus Torvalds 			if (unlikely(!skb)) {
12611da177e4SLinus Torvalds 				err = -ENOBUFS;
12621da177e4SLinus Torvalds 				goto error;
12631da177e4SLinus Torvalds 			}
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 			/*
12661da177e4SLinus Torvalds 			 *	Fill in the control structures
12671da177e4SLinus Torvalds 			 */
12681da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_NONE;
12691da177e4SLinus Torvalds 			skb->csum = 0;
12701da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 			/*
12731da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
12741da177e4SLinus Torvalds 			 */
1275967b05f6SArnaldo Carvalho de Melo 			skb_put(skb, fragheaderlen + fraggap);
12762ca9e6f2SArnaldo Carvalho de Melo 			skb_reset_network_header(skb);
1277b0e380b1SArnaldo Carvalho de Melo 			skb->transport_header = (skb->network_header +
1278b0e380b1SArnaldo Carvalho de Melo 						 fragheaderlen);
12791da177e4SLinus Torvalds 			if (fraggap) {
1280967b05f6SArnaldo Carvalho de Melo 				skb->csum = skb_copy_and_csum_bits(skb_prev,
1281967b05f6SArnaldo Carvalho de Melo 								   maxfraglen,
12829c70220bSArnaldo Carvalho de Melo 						    skb_transport_header(skb),
1283967b05f6SArnaldo Carvalho de Melo 								   fraggap, 0);
12841da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
12851da177e4SLinus Torvalds 							  skb->csum);
1286e9fa4f7bSHerbert Xu 				pskb_trim_unique(skb_prev, maxfraglen);
12871da177e4SLinus Torvalds 			}
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 			/*
12901da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
12911da177e4SLinus Torvalds 			 */
12921da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
12931da177e4SLinus Torvalds 			continue;
12941da177e4SLinus Torvalds 		}
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 		if (len > size)
12971da177e4SLinus Torvalds 			len = size;
1298be12a1feSHannes Frederic Sowa 
1299be12a1feSHannes Frederic Sowa 		if (skb_append_pagefrags(skb, page, offset, len)) {
13001da177e4SLinus Torvalds 			err = -EMSGSIZE;
13011da177e4SLinus Torvalds 			goto error;
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		if (skb->ip_summed == CHECKSUM_NONE) {
130544bb9363SAl Viro 			__wsum csum;
13061da177e4SLinus Torvalds 			csum = csum_page(page, offset, len);
13071da177e4SLinus Torvalds 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
13081da177e4SLinus Torvalds 		}
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 		skb->len += len;
13111da177e4SLinus Torvalds 		skb->data_len += len;
13121e34a11dSDavid S. Miller 		skb->truesize += len;
131314afee4bSReshetova, Elena 		refcount_add(len, &sk->sk_wmem_alloc);
13141da177e4SLinus Torvalds 		offset += len;
13151da177e4SLinus Torvalds 		size -= len;
13161da177e4SLinus Torvalds 	}
13171da177e4SLinus Torvalds 	return 0;
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds error:
1320bdc712b4SDavid S. Miller 	cork->length -= size;
13215e38e270SPavel Emelyanov 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
13221da177e4SLinus Torvalds 	return err;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13251470ddf7SHerbert Xu static void ip_cork_release(struct inet_cork *cork)
1326429f08e9SPavel Emelyanov {
13271470ddf7SHerbert Xu 	cork->flags &= ~IPCORK_OPT;
13281470ddf7SHerbert Xu 	kfree(cork->opt);
13291470ddf7SHerbert Xu 	cork->opt = NULL;
13301470ddf7SHerbert Xu 	dst_release(cork->dst);
13311470ddf7SHerbert Xu 	cork->dst = NULL;
1332429f08e9SPavel Emelyanov }
1333429f08e9SPavel Emelyanov 
13341da177e4SLinus Torvalds /*
13351da177e4SLinus Torvalds  *	Combined all pending IP fragments on the socket as one IP datagram
13361da177e4SLinus Torvalds  *	and push them out.
13371da177e4SLinus Torvalds  */
13381c32c5adSHerbert Xu struct sk_buff *__ip_make_skb(struct sock *sk,
133977968b78SDavid S. Miller 			      struct flowi4 *fl4,
13401470ddf7SHerbert Xu 			      struct sk_buff_head *queue,
13411470ddf7SHerbert Xu 			      struct inet_cork *cork)
13421da177e4SLinus Torvalds {
13431da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
13441da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
13451da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
13460388b004SPavel Emelyanov 	struct net *net = sock_net(sk);
13471da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
13481470ddf7SHerbert Xu 	struct rtable *rt = (struct rtable *)cork->dst;
13491da177e4SLinus Torvalds 	struct iphdr *iph;
135076ab608dSAlexey Dobriyan 	__be16 df = 0;
13511da177e4SLinus Torvalds 	__u8 ttl;
13521da177e4SLinus Torvalds 
135351456b29SIan Morris 	skb = __skb_dequeue(queue);
135451456b29SIan Morris 	if (!skb)
13551da177e4SLinus Torvalds 		goto out;
13561da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
1359d56f90a7SArnaldo Carvalho de Melo 	if (skb->data < skb_network_header(skb))
1360bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
13611470ddf7SHerbert Xu 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1362cfe1fc77SArnaldo Carvalho de Melo 		__skb_pull(tmp_skb, skb_network_header_len(skb));
13631da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
13641da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
13651da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
13661da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
13671da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
13681da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
13691da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
13731da177e4SLinus Torvalds 	 * to fragment the frame generated here. No matter, what transforms
13741da177e4SLinus Torvalds 	 * how transforms change size of the packet, it will come out.
13751da177e4SLinus Torvalds 	 */
137660ff7467SWANG Cong 	skb->ignore_df = ip_sk_ignore_df(sk);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	/* DF bit is set when we want to see DF on outgoing frames.
137960ff7467SWANG Cong 	 * If ignore_df is set too, we still allow to fragment this frame
13801da177e4SLinus Torvalds 	 * locally. */
1381482fc609SHannes Frederic Sowa 	if (inet->pmtudisc == IP_PMTUDISC_DO ||
1382482fc609SHannes Frederic Sowa 	    inet->pmtudisc == IP_PMTUDISC_PROBE ||
1383d8d1f30bSChangli Gao 	    (skb->len <= dst_mtu(&rt->dst) &&
1384d8d1f30bSChangli Gao 	     ip_dont_fragment(sk, &rt->dst)))
13851da177e4SLinus Torvalds 		df = htons(IP_DF);
13861da177e4SLinus Torvalds 
13871470ddf7SHerbert Xu 	if (cork->flags & IPCORK_OPT)
13881470ddf7SHerbert Xu 		opt = cork->opt;
13891da177e4SLinus Torvalds 
1390aa661581SFrancesco Fusco 	if (cork->ttl != 0)
1391aa661581SFrancesco Fusco 		ttl = cork->ttl;
1392aa661581SFrancesco Fusco 	else if (rt->rt_type == RTN_MULTICAST)
13931da177e4SLinus Torvalds 		ttl = inet->mc_ttl;
13941da177e4SLinus Torvalds 	else
1395d8d1f30bSChangli Gao 		ttl = ip_select_ttl(inet, &rt->dst);
13961da177e4SLinus Torvalds 
1397749154aaSAnsis Atteka 	iph = ip_hdr(skb);
13981da177e4SLinus Torvalds 	iph->version = 4;
13991da177e4SLinus Torvalds 	iph->ihl = 5;
1400aa661581SFrancesco Fusco 	iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
14011da177e4SLinus Torvalds 	iph->frag_off = df;
14021da177e4SLinus Torvalds 	iph->ttl = ttl;
14031da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
140484f9307cSEric Dumazet 	ip_copy_addrs(iph, fl4);
1405b6a7719aSHannes Frederic Sowa 	ip_select_ident(net, skb, sk);
14061da177e4SLinus Torvalds 
140722f728f8SDavid S. Miller 	if (opt) {
140822f728f8SDavid S. Miller 		iph->ihl += opt->optlen>>2;
140922f728f8SDavid S. Miller 		ip_options_build(skb, opt, cork->addr, rt, 0);
141022f728f8SDavid S. Miller 	}
141122f728f8SDavid S. Miller 
1412aa661581SFrancesco Fusco 	skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
14134a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
1414a21bba94SEric Dumazet 	/*
1415a21bba94SEric Dumazet 	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1416a21bba94SEric Dumazet 	 * on dst refcount
1417a21bba94SEric Dumazet 	 */
14181470ddf7SHerbert Xu 	cork->dst = NULL;
1419d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
14201da177e4SLinus Torvalds 
142196793b48SDavid L Stevens 	if (iph->protocol == IPPROTO_ICMP)
14220388b004SPavel Emelyanov 		icmp_out_count(net, ((struct icmphdr *)
142396793b48SDavid L Stevens 			skb_transport_header(skb))->type);
142496793b48SDavid L Stevens 
14251c32c5adSHerbert Xu 	ip_cork_release(cork);
14261c32c5adSHerbert Xu out:
14271c32c5adSHerbert Xu 	return skb;
14281c32c5adSHerbert Xu }
14291c32c5adSHerbert Xu 
1430b5ec8eeaSEric Dumazet int ip_send_skb(struct net *net, struct sk_buff *skb)
14311c32c5adSHerbert Xu {
14321c32c5adSHerbert Xu 	int err;
14331c32c5adSHerbert Xu 
143433224b16SEric W. Biederman 	err = ip_local_out(net, skb->sk, skb);
14351da177e4SLinus Torvalds 	if (err) {
14361da177e4SLinus Torvalds 		if (err > 0)
14376ce9e7b5SEric Dumazet 			err = net_xmit_errno(err);
14381da177e4SLinus Torvalds 		if (err)
14391c32c5adSHerbert Xu 			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
14401da177e4SLinus Torvalds 	}
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	return err;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
144577968b78SDavid S. Miller int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
14461470ddf7SHerbert Xu {
14471c32c5adSHerbert Xu 	struct sk_buff *skb;
14481c32c5adSHerbert Xu 
144977968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
14501c32c5adSHerbert Xu 	if (!skb)
14511c32c5adSHerbert Xu 		return 0;
14521c32c5adSHerbert Xu 
14531c32c5adSHerbert Xu 	/* Netfilter gets whole the not fragmented skb. */
1454b5ec8eeaSEric Dumazet 	return ip_send_skb(sock_net(sk), skb);
14551470ddf7SHerbert Xu }
14561470ddf7SHerbert Xu 
14571da177e4SLinus Torvalds /*
14581da177e4SLinus Torvalds  *	Throw away all pending data on the socket.
14591da177e4SLinus Torvalds  */
14601470ddf7SHerbert Xu static void __ip_flush_pending_frames(struct sock *sk,
14611470ddf7SHerbert Xu 				      struct sk_buff_head *queue,
14621470ddf7SHerbert Xu 				      struct inet_cork *cork)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	struct sk_buff *skb;
14651da177e4SLinus Torvalds 
14661470ddf7SHerbert Xu 	while ((skb = __skb_dequeue_tail(queue)) != NULL)
14671da177e4SLinus Torvalds 		kfree_skb(skb);
14681da177e4SLinus Torvalds 
14691470ddf7SHerbert Xu 	ip_cork_release(cork);
14701470ddf7SHerbert Xu }
14711470ddf7SHerbert Xu 
14721470ddf7SHerbert Xu void ip_flush_pending_frames(struct sock *sk)
14731470ddf7SHerbert Xu {
1474bdc712b4SDavid S. Miller 	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771c32c5adSHerbert Xu struct sk_buff *ip_make_skb(struct sock *sk,
147877968b78SDavid S. Miller 			    struct flowi4 *fl4,
14791c32c5adSHerbert Xu 			    int getfrag(void *from, char *to, int offset,
14801c32c5adSHerbert Xu 					int len, int odd, struct sk_buff *skb),
14811c32c5adSHerbert Xu 			    void *from, int length, int transhdrlen,
14821c32c5adSHerbert Xu 			    struct ipcm_cookie *ipc, struct rtable **rtp,
14831cd7884dSWillem de Bruijn 			    struct inet_cork *cork, unsigned int flags)
14841c32c5adSHerbert Xu {
14851c32c5adSHerbert Xu 	struct sk_buff_head queue;
14861c32c5adSHerbert Xu 	int err;
14871c32c5adSHerbert Xu 
14881c32c5adSHerbert Xu 	if (flags & MSG_PROBE)
14891c32c5adSHerbert Xu 		return NULL;
14901c32c5adSHerbert Xu 
14911c32c5adSHerbert Xu 	__skb_queue_head_init(&queue);
14921c32c5adSHerbert Xu 
14931cd7884dSWillem de Bruijn 	cork->flags = 0;
14941cd7884dSWillem de Bruijn 	cork->addr = 0;
14951cd7884dSWillem de Bruijn 	cork->opt = NULL;
14961cd7884dSWillem de Bruijn 	err = ip_setup_cork(sk, cork, ipc, rtp);
14971c32c5adSHerbert Xu 	if (err)
14981c32c5adSHerbert Xu 		return ERR_PTR(err);
14991c32c5adSHerbert Xu 
15001cd7884dSWillem de Bruijn 	err = __ip_append_data(sk, fl4, &queue, cork,
15015640f768SEric Dumazet 			       &current->task_frag, getfrag,
15021c32c5adSHerbert Xu 			       from, length, transhdrlen, flags);
15031c32c5adSHerbert Xu 	if (err) {
15041cd7884dSWillem de Bruijn 		__ip_flush_pending_frames(sk, &queue, cork);
15051c32c5adSHerbert Xu 		return ERR_PTR(err);
15061c32c5adSHerbert Xu 	}
15071c32c5adSHerbert Xu 
15081cd7884dSWillem de Bruijn 	return __ip_make_skb(sk, fl4, &queue, cork);
15091c32c5adSHerbert Xu }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds /*
15121da177e4SLinus Torvalds  *	Fetch data from kernel space and fill in checksum if needed.
15131da177e4SLinus Torvalds  */
15141da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset,
15151da177e4SLinus Torvalds 			      int len, int odd, struct sk_buff *skb)
15161da177e4SLinus Torvalds {
15175084205fSAl Viro 	__wsum csum;
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
15201da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
15211da177e4SLinus Torvalds 	return 0;
15221da177e4SLinus Torvalds }
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds /*
15251da177e4SLinus Torvalds  *	Generic function to send a packet as reply to another packet.
1526be9f4a44SEric Dumazet  *	Used to send some TCP resets/acks so far.
15271da177e4SLinus Torvalds  */
1528bdbbb852SEric Dumazet void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
152924a2d43dSEric Dumazet 			   const struct ip_options *sopt,
153024a2d43dSEric Dumazet 			   __be32 daddr, __be32 saddr,
153124a2d43dSEric Dumazet 			   const struct ip_reply_arg *arg,
153270e73416SDavid S. Miller 			   unsigned int len)
15331da177e4SLinus Torvalds {
1534f6d8bd05SEric Dumazet 	struct ip_options_data replyopts;
15351da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
153677968b78SDavid S. Miller 	struct flowi4 fl4;
1537511c3f92SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
1538bdbbb852SEric Dumazet 	struct net *net = sock_net(sk);
1539be9f4a44SEric Dumazet 	struct sk_buff *nskb;
15404062090eSVasily Averin 	int err;
1541f7ba868bSDavid Ahern 	int oif;
15421da177e4SLinus Torvalds 
154391ed1e66SPaolo Abeni 	if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt))
15441da177e4SLinus Torvalds 		return;
15451da177e4SLinus Torvalds 
15460a5ebb80SDavid S. Miller 	ipc.addr = daddr;
15471da177e4SLinus Torvalds 	ipc.opt = NULL;
15482244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
1549aa661581SFrancesco Fusco 	ipc.ttl = 0;
1550aa661581SFrancesco Fusco 	ipc.tos = -1;
15511da177e4SLinus Torvalds 
1552f6d8bd05SEric Dumazet 	if (replyopts.opt.opt.optlen) {
15531da177e4SLinus Torvalds 		ipc.opt = &replyopts.opt;
15541da177e4SLinus Torvalds 
1555f6d8bd05SEric Dumazet 		if (replyopts.opt.opt.srr)
1556f6d8bd05SEric Dumazet 			daddr = replyopts.opt.opt.faddr;
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 
1559f7ba868bSDavid Ahern 	oif = arg->bound_dev_if;
15609b6c14d5SDavid Ahern 	if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
15619b6c14d5SDavid Ahern 		oif = skb->skb_iif;
1562f7ba868bSDavid Ahern 
1563f7ba868bSDavid Ahern 	flowi4_init_output(&fl4, oif,
1564e110861fSLorenzo Colitti 			   IP4_REPLY_MARK(net, skb->mark),
156566b13d99SEric Dumazet 			   RT_TOS(arg->tos),
1566be9f4a44SEric Dumazet 			   RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1567538de0e0SDavid S. Miller 			   ip_reply_arg_flowi_flags(arg),
156870e73416SDavid S. Miller 			   daddr, saddr,
1569e2d118a1SLorenzo Colitti 			   tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1570e2d118a1SLorenzo Colitti 			   arg->uid);
15719d6ec938SDavid S. Miller 	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1572be9f4a44SEric Dumazet 	rt = ip_route_output_key(net, &fl4);
1573b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
15741da177e4SLinus Torvalds 		return;
15751da177e4SLinus Torvalds 
1576bdbbb852SEric Dumazet 	inet_sk(sk)->tos = arg->tos;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	sk->sk_priority = skb->priority;
1579eddc9ec5SArnaldo Carvalho de Melo 	sk->sk_protocol = ip_hdr(skb)->protocol;
1580f0e48dbfSPatrick McHardy 	sk->sk_bound_dev_if = arg->bound_dev_if;
1581be9f4a44SEric Dumazet 	sk->sk_sndbuf = sysctl_wmem_default;
1582bf99b4deSPau Espin Pedrol 	sk->sk_mark = fl4.flowi4_mark;
15834062090eSVasily Averin 	err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
15844062090eSVasily Averin 			     len, 0, &ipc, &rt, MSG_DONTWAIT);
15854062090eSVasily Averin 	if (unlikely(err)) {
15864062090eSVasily Averin 		ip_flush_pending_frames(sk);
15874062090eSVasily Averin 		goto out;
15884062090eSVasily Averin 	}
15894062090eSVasily Averin 
1590be9f4a44SEric Dumazet 	nskb = skb_peek(&sk->sk_write_queue);
1591be9f4a44SEric Dumazet 	if (nskb) {
15921da177e4SLinus Torvalds 		if (arg->csumoffset >= 0)
1593be9f4a44SEric Dumazet 			*((__sum16 *)skb_transport_header(nskb) +
1594be9f4a44SEric Dumazet 			  arg->csumoffset) = csum_fold(csum_add(nskb->csum,
15959c70220bSArnaldo Carvalho de Melo 								arg->csum));
1596be9f4a44SEric Dumazet 		nskb->ip_summed = CHECKSUM_NONE;
159777968b78SDavid S. Miller 		ip_push_pending_frames(sk, &fl4);
15981da177e4SLinus Torvalds 	}
15994062090eSVasily Averin out:
16001da177e4SLinus Torvalds 	ip_rt_put(rt);
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds void __init ip_init(void)
16041da177e4SLinus Torvalds {
16051da177e4SLinus Torvalds 	ip_rt_init();
16061da177e4SLinus Torvalds 	inet_initpeers();
16071da177e4SLinus Torvalds 
160872c1d3bdSWANG Cong #if defined(CONFIG_IP_MULTICAST)
160972c1d3bdSWANG Cong 	igmp_mc_init();
16101da177e4SLinus Torvalds #endif
16111da177e4SLinus Torvalds }
1612