xref: /openbmc/linux/net/ipv4/ip_output.c (revision 3557b3fd)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		The Internet Protocol (IP) output module.
7  *
8  * Authors:	Ross Biro
9  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10  *		Donald Becker, <becker@super.org>
11  *		Alan Cox, <Alan.Cox@linux.org>
12  *		Richard Underwood
13  *		Stefan Becker, <stefanb@yello.ping.de>
14  *		Jorge Cwik, <jorge@laser.satlink.net>
15  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
16  *		Hirokazu Takahashi, <taka@valinux.co.jp>
17  *
18  *	See ip_input.c for original log
19  *
20  *	Fixes:
21  *		Alan Cox	:	Missing nonblock feature in ip_build_xmit.
22  *		Mike Kilburn	:	htons() missing in ip_build_xmit.
23  *		Bradford Johnson:	Fix faulty handling of some frames when
24  *					no route is found.
25  *		Alexander Demenshin:	Missing sk/skb free in ip_queue_xmit
26  *					(in case if packet not accepted by
27  *					output firewall rules)
28  *		Mike McLagan	:	Routing by source
29  *		Alexey Kuznetsov:	use new route cache
30  *		Andi Kleen:		Fix broken PMTU recovery and remove
31  *					some redundant tests.
32  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
33  *		Andi Kleen	: 	Replace ip_reply with ip_send_reply.
34  *		Andi Kleen	:	Split fast and slow ip_build_xmit path
35  *					for decreased register pressure on x86
36  *					and more readibility.
37  *		Marc Boucher	:	When call_out_firewall returns FW_QUEUE,
38  *					silently drop skb instead of failing with -EPERM.
39  *		Detlev Wengorz	:	Copy protocol for fragments.
40  *		Hirokazu Takahashi:	HW checksumming for outgoing UDP
41  *					datagrams.
42  *		Hirokazu Takahashi:	sendfile() on UDP works now.
43  */
44 
45 #include <linux/uaccess.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/kernel.h>
49 #include <linux/mm.h>
50 #include <linux/string.h>
51 #include <linux/errno.h>
52 #include <linux/highmem.h>
53 #include <linux/slab.h>
54 
55 #include <linux/socket.h>
56 #include <linux/sockios.h>
57 #include <linux/in.h>
58 #include <linux/inet.h>
59 #include <linux/netdevice.h>
60 #include <linux/etherdevice.h>
61 #include <linux/proc_fs.h>
62 #include <linux/stat.h>
63 #include <linux/init.h>
64 
65 #include <net/snmp.h>
66 #include <net/ip.h>
67 #include <net/protocol.h>
68 #include <net/route.h>
69 #include <net/xfrm.h>
70 #include <linux/skbuff.h>
71 #include <net/sock.h>
72 #include <net/arp.h>
73 #include <net/icmp.h>
74 #include <net/checksum.h>
75 #include <net/inetpeer.h>
76 #include <net/lwtunnel.h>
77 #include <linux/bpf-cgroup.h>
78 #include <linux/igmp.h>
79 #include <linux/netfilter_ipv4.h>
80 #include <linux/netfilter_bridge.h>
81 #include <linux/netlink.h>
82 #include <linux/tcp.h>
83 
84 static int
85 ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
86 	    unsigned int mtu,
87 	    int (*output)(struct net *, struct sock *, struct sk_buff *));
88 
89 /* Generate a checksum for an outgoing IP datagram. */
90 void ip_send_check(struct iphdr *iph)
91 {
92 	iph->check = 0;
93 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
94 }
95 EXPORT_SYMBOL(ip_send_check);
96 
97 int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
98 {
99 	struct iphdr *iph = ip_hdr(skb);
100 
101 	iph->tot_len = htons(skb->len);
102 	ip_send_check(iph);
103 
104 	/* if egress device is enslaved to an L3 master device pass the
105 	 * skb to its handler for processing
106 	 */
107 	skb = l3mdev_ip_out(sk, skb);
108 	if (unlikely(!skb))
109 		return 0;
110 
111 	skb->protocol = htons(ETH_P_IP);
112 
113 	return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
114 		       net, sk, skb, NULL, skb_dst(skb)->dev,
115 		       dst_output);
116 }
117 
118 int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
119 {
120 	int err;
121 
122 	err = __ip_local_out(net, sk, skb);
123 	if (likely(err == 1))
124 		err = dst_output(net, sk, skb);
125 
126 	return err;
127 }
128 EXPORT_SYMBOL_GPL(ip_local_out);
129 
130 static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
131 {
132 	int ttl = inet->uc_ttl;
133 
134 	if (ttl < 0)
135 		ttl = ip4_dst_hoplimit(dst);
136 	return ttl;
137 }
138 
139 /*
140  *		Add an ip header to a skbuff and send it out.
141  *
142  */
143 int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
144 			  __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
145 {
146 	struct inet_sock *inet = inet_sk(sk);
147 	struct rtable *rt = skb_rtable(skb);
148 	struct net *net = sock_net(sk);
149 	struct iphdr *iph;
150 
151 	/* Build the IP header. */
152 	skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
153 	skb_reset_network_header(skb);
154 	iph = ip_hdr(skb);
155 	iph->version  = 4;
156 	iph->ihl      = 5;
157 	iph->tos      = inet->tos;
158 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
159 	iph->daddr    = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
160 	iph->saddr    = saddr;
161 	iph->protocol = sk->sk_protocol;
162 	if (ip_dont_fragment(sk, &rt->dst)) {
163 		iph->frag_off = htons(IP_DF);
164 		iph->id = 0;
165 	} else {
166 		iph->frag_off = 0;
167 		__ip_select_ident(net, iph, 1);
168 	}
169 
170 	if (opt && opt->opt.optlen) {
171 		iph->ihl += opt->opt.optlen>>2;
172 		ip_options_build(skb, &opt->opt, daddr, rt, 0);
173 	}
174 
175 	skb->priority = sk->sk_priority;
176 	if (!skb->mark)
177 		skb->mark = sk->sk_mark;
178 
179 	/* Send it out. */
180 	return ip_local_out(net, skb->sk, skb);
181 }
182 EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
183 
184 static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
185 {
186 	struct dst_entry *dst = skb_dst(skb);
187 	struct rtable *rt = (struct rtable *)dst;
188 	struct net_device *dev = dst->dev;
189 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
190 	struct neighbour *neigh;
191 	bool is_v6gw = false;
192 
193 	if (rt->rt_type == RTN_MULTICAST) {
194 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
195 	} else if (rt->rt_type == RTN_BROADCAST)
196 		IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
197 
198 	/* Be paranoid, rather than too clever. */
199 	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
200 		struct sk_buff *skb2;
201 
202 		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
203 		if (!skb2) {
204 			kfree_skb(skb);
205 			return -ENOMEM;
206 		}
207 		if (skb->sk)
208 			skb_set_owner_w(skb2, skb->sk);
209 		consume_skb(skb);
210 		skb = skb2;
211 	}
212 
213 	if (lwtunnel_xmit_redirect(dst->lwtstate)) {
214 		int res = lwtunnel_xmit(skb);
215 
216 		if (res < 0 || res == LWTUNNEL_XMIT_DONE)
217 			return res;
218 	}
219 
220 	rcu_read_lock_bh();
221 	neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
222 	if (!IS_ERR(neigh)) {
223 		int res;
224 
225 		sock_confirm_neigh(skb, neigh);
226 		/* if crossing protocols, can not use the cached header */
227 		res = neigh_output(neigh, skb, is_v6gw);
228 		rcu_read_unlock_bh();
229 		return res;
230 	}
231 	rcu_read_unlock_bh();
232 
233 	net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
234 			    __func__);
235 	kfree_skb(skb);
236 	return -EINVAL;
237 }
238 
239 static int ip_finish_output_gso(struct net *net, struct sock *sk,
240 				struct sk_buff *skb, unsigned int mtu)
241 {
242 	netdev_features_t features;
243 	struct sk_buff *segs;
244 	int ret = 0;
245 
246 	/* common case: seglen is <= mtu
247 	 */
248 	if (skb_gso_validate_network_len(skb, mtu))
249 		return ip_finish_output2(net, sk, skb);
250 
251 	/* Slowpath -  GSO segment length exceeds the egress MTU.
252 	 *
253 	 * This can happen in several cases:
254 	 *  - Forwarding of a TCP GRO skb, when DF flag is not set.
255 	 *  - Forwarding of an skb that arrived on a virtualization interface
256 	 *    (virtio-net/vhost/tap) with TSO/GSO size set by other network
257 	 *    stack.
258 	 *  - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an
259 	 *    interface with a smaller MTU.
260 	 *  - Arriving GRO skb (or GSO skb in a virtualized environment) that is
261 	 *    bridged to a NETIF_F_TSO tunnel stacked over an interface with an
262 	 *    insufficent MTU.
263 	 */
264 	features = netif_skb_features(skb);
265 	BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
266 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
267 	if (IS_ERR_OR_NULL(segs)) {
268 		kfree_skb(skb);
269 		return -ENOMEM;
270 	}
271 
272 	consume_skb(skb);
273 
274 	do {
275 		struct sk_buff *nskb = segs->next;
276 		int err;
277 
278 		skb_mark_not_on_list(segs);
279 		err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
280 
281 		if (err && ret == 0)
282 			ret = err;
283 		segs = nskb;
284 	} while (segs);
285 
286 	return ret;
287 }
288 
289 static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
290 {
291 	unsigned int mtu;
292 	int ret;
293 
294 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
295 	if (ret) {
296 		kfree_skb(skb);
297 		return ret;
298 	}
299 
300 #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
301 	/* Policy lookup after SNAT yielded a new policy */
302 	if (skb_dst(skb)->xfrm) {
303 		IPCB(skb)->flags |= IPSKB_REROUTED;
304 		return dst_output(net, sk, skb);
305 	}
306 #endif
307 	mtu = ip_skb_dst_mtu(sk, skb);
308 	if (skb_is_gso(skb))
309 		return ip_finish_output_gso(net, sk, skb, mtu);
310 
311 	if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU))
312 		return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
313 
314 	return ip_finish_output2(net, sk, skb);
315 }
316 
317 static int ip_mc_finish_output(struct net *net, struct sock *sk,
318 			       struct sk_buff *skb)
319 {
320 	int ret;
321 
322 	ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
323 	if (ret) {
324 		kfree_skb(skb);
325 		return ret;
326 	}
327 
328 	return dev_loopback_xmit(net, sk, skb);
329 }
330 
331 int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
332 {
333 	struct rtable *rt = skb_rtable(skb);
334 	struct net_device *dev = rt->dst.dev;
335 
336 	/*
337 	 *	If the indicated interface is up and running, send the packet.
338 	 */
339 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
340 
341 	skb->dev = dev;
342 	skb->protocol = htons(ETH_P_IP);
343 
344 	/*
345 	 *	Multicasts are looped back for other local users
346 	 */
347 
348 	if (rt->rt_flags&RTCF_MULTICAST) {
349 		if (sk_mc_loop(sk)
350 #ifdef CONFIG_IP_MROUTE
351 		/* Small optimization: do not loopback not local frames,
352 		   which returned after forwarding; they will be  dropped
353 		   by ip_mr_input in any case.
354 		   Note, that local frames are looped back to be delivered
355 		   to local recipients.
356 
357 		   This check is duplicated in ip_mr_input at the moment.
358 		 */
359 		    &&
360 		    ((rt->rt_flags & RTCF_LOCAL) ||
361 		     !(IPCB(skb)->flags & IPSKB_FORWARDED))
362 #endif
363 		   ) {
364 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
365 			if (newskb)
366 				NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
367 					net, sk, newskb, NULL, newskb->dev,
368 					ip_mc_finish_output);
369 		}
370 
371 		/* Multicasts with ttl 0 must not go beyond the host */
372 
373 		if (ip_hdr(skb)->ttl == 0) {
374 			kfree_skb(skb);
375 			return 0;
376 		}
377 	}
378 
379 	if (rt->rt_flags&RTCF_BROADCAST) {
380 		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
381 		if (newskb)
382 			NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
383 				net, sk, newskb, NULL, newskb->dev,
384 				ip_mc_finish_output);
385 	}
386 
387 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
388 			    net, sk, skb, NULL, skb->dev,
389 			    ip_finish_output,
390 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
391 }
392 
393 int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
394 {
395 	struct net_device *dev = skb_dst(skb)->dev;
396 
397 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
398 
399 	skb->dev = dev;
400 	skb->protocol = htons(ETH_P_IP);
401 
402 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
403 			    net, sk, skb, NULL, dev,
404 			    ip_finish_output,
405 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
406 }
407 
408 /*
409  * copy saddr and daddr, possibly using 64bit load/stores
410  * Equivalent to :
411  *   iph->saddr = fl4->saddr;
412  *   iph->daddr = fl4->daddr;
413  */
414 static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
415 {
416 	BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
417 		     offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
418 	memcpy(&iph->saddr, &fl4->saddr,
419 	       sizeof(fl4->saddr) + sizeof(fl4->daddr));
420 }
421 
422 /* Note: skb->sk can be different from sk, in case of tunnels */
423 int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
424 		    __u8 tos)
425 {
426 	struct inet_sock *inet = inet_sk(sk);
427 	struct net *net = sock_net(sk);
428 	struct ip_options_rcu *inet_opt;
429 	struct flowi4 *fl4;
430 	struct rtable *rt;
431 	struct iphdr *iph;
432 	int res;
433 
434 	/* Skip all of this if the packet is already routed,
435 	 * f.e. by something like SCTP.
436 	 */
437 	rcu_read_lock();
438 	inet_opt = rcu_dereference(inet->inet_opt);
439 	fl4 = &fl->u.ip4;
440 	rt = skb_rtable(skb);
441 	if (rt)
442 		goto packet_routed;
443 
444 	/* Make sure we can route this packet. */
445 	rt = (struct rtable *)__sk_dst_check(sk, 0);
446 	if (!rt) {
447 		__be32 daddr;
448 
449 		/* Use correct destination address if we have options. */
450 		daddr = inet->inet_daddr;
451 		if (inet_opt && inet_opt->opt.srr)
452 			daddr = inet_opt->opt.faddr;
453 
454 		/* If this fails, retransmit mechanism of transport layer will
455 		 * keep trying until route appears or the connection times
456 		 * itself out.
457 		 */
458 		rt = ip_route_output_ports(net, fl4, sk,
459 					   daddr, inet->inet_saddr,
460 					   inet->inet_dport,
461 					   inet->inet_sport,
462 					   sk->sk_protocol,
463 					   RT_CONN_FLAGS_TOS(sk, tos),
464 					   sk->sk_bound_dev_if);
465 		if (IS_ERR(rt))
466 			goto no_route;
467 		sk_setup_caps(sk, &rt->dst);
468 	}
469 	skb_dst_set_noref(skb, &rt->dst);
470 
471 packet_routed:
472 	if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_gw_family)
473 		goto no_route;
474 
475 	/* OK, we know where to send it, allocate and build IP header. */
476 	skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
477 	skb_reset_network_header(skb);
478 	iph = ip_hdr(skb);
479 	*((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (tos & 0xff));
480 	if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
481 		iph->frag_off = htons(IP_DF);
482 	else
483 		iph->frag_off = 0;
484 	iph->ttl      = ip_select_ttl(inet, &rt->dst);
485 	iph->protocol = sk->sk_protocol;
486 	ip_copy_addrs(iph, fl4);
487 
488 	/* Transport layer set skb->h.foo itself. */
489 
490 	if (inet_opt && inet_opt->opt.optlen) {
491 		iph->ihl += inet_opt->opt.optlen >> 2;
492 		ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
493 	}
494 
495 	ip_select_ident_segs(net, skb, sk,
496 			     skb_shinfo(skb)->gso_segs ?: 1);
497 
498 	/* TODO : should we use skb->sk here instead of sk ? */
499 	skb->priority = sk->sk_priority;
500 	skb->mark = sk->sk_mark;
501 
502 	res = ip_local_out(net, sk, skb);
503 	rcu_read_unlock();
504 	return res;
505 
506 no_route:
507 	rcu_read_unlock();
508 	IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
509 	kfree_skb(skb);
510 	return -EHOSTUNREACH;
511 }
512 EXPORT_SYMBOL(__ip_queue_xmit);
513 
514 static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
515 {
516 	to->pkt_type = from->pkt_type;
517 	to->priority = from->priority;
518 	to->protocol = from->protocol;
519 	skb_dst_drop(to);
520 	skb_dst_copy(to, from);
521 	to->dev = from->dev;
522 	to->mark = from->mark;
523 
524 	skb_copy_hash(to, from);
525 
526 	/* Copy the flags to each fragment. */
527 	IPCB(to)->flags = IPCB(from)->flags;
528 
529 #ifdef CONFIG_NET_SCHED
530 	to->tc_index = from->tc_index;
531 #endif
532 	nf_copy(to, from);
533 	skb_ext_copy(to, from);
534 #if IS_ENABLED(CONFIG_IP_VS)
535 	to->ipvs_property = from->ipvs_property;
536 #endif
537 	skb_copy_secmark(to, from);
538 }
539 
540 static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
541 		       unsigned int mtu,
542 		       int (*output)(struct net *, struct sock *, struct sk_buff *))
543 {
544 	struct iphdr *iph = ip_hdr(skb);
545 
546 	if ((iph->frag_off & htons(IP_DF)) == 0)
547 		return ip_do_fragment(net, sk, skb, output);
548 
549 	if (unlikely(!skb->ignore_df ||
550 		     (IPCB(skb)->frag_max_size &&
551 		      IPCB(skb)->frag_max_size > mtu))) {
552 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
553 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
554 			  htonl(mtu));
555 		kfree_skb(skb);
556 		return -EMSGSIZE;
557 	}
558 
559 	return ip_do_fragment(net, sk, skb, output);
560 }
561 
562 /*
563  *	This IP datagram is too large to be sent in one piece.  Break it up into
564  *	smaller pieces (each of size equal to IP header plus
565  *	a block of the data of the original IP data part) that will yet fit in a
566  *	single device frame, and queue such a frame for sending.
567  */
568 
569 int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
570 		   int (*output)(struct net *, struct sock *, struct sk_buff *))
571 {
572 	struct iphdr *iph;
573 	int ptr;
574 	struct sk_buff *skb2;
575 	unsigned int mtu, hlen, left, len, ll_rs;
576 	int offset;
577 	__be16 not_last_frag;
578 	struct rtable *rt = skb_rtable(skb);
579 	int err = 0;
580 
581 	/* for offloaded checksums cleanup checksum before fragmentation */
582 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
583 	    (err = skb_checksum_help(skb)))
584 		goto fail;
585 
586 	/*
587 	 *	Point into the IP datagram header.
588 	 */
589 
590 	iph = ip_hdr(skb);
591 
592 	mtu = ip_skb_dst_mtu(sk, skb);
593 	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
594 		mtu = IPCB(skb)->frag_max_size;
595 
596 	/*
597 	 *	Setup starting values.
598 	 */
599 
600 	hlen = iph->ihl * 4;
601 	mtu = mtu - hlen;	/* Size of data space */
602 	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
603 	ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
604 
605 	/* When frag_list is given, use it. First, check its validity:
606 	 * some transformers could create wrong frag_list or break existing
607 	 * one, it is not prohibited. In this case fall back to copying.
608 	 *
609 	 * LATER: this step can be merged to real generation of fragments,
610 	 * we can switch to copy when see the first bad fragment.
611 	 */
612 	if (skb_has_frag_list(skb)) {
613 		struct sk_buff *frag, *frag2;
614 		unsigned int first_len = skb_pagelen(skb);
615 
616 		if (first_len - hlen > mtu ||
617 		    ((first_len - hlen) & 7) ||
618 		    ip_is_fragment(iph) ||
619 		    skb_cloned(skb) ||
620 		    skb_headroom(skb) < ll_rs)
621 			goto slow_path;
622 
623 		skb_walk_frags(skb, frag) {
624 			/* Correct geometry. */
625 			if (frag->len > mtu ||
626 			    ((frag->len & 7) && frag->next) ||
627 			    skb_headroom(frag) < hlen + ll_rs)
628 				goto slow_path_clean;
629 
630 			/* Partially cloned skb? */
631 			if (skb_shared(frag))
632 				goto slow_path_clean;
633 
634 			BUG_ON(frag->sk);
635 			if (skb->sk) {
636 				frag->sk = skb->sk;
637 				frag->destructor = sock_wfree;
638 			}
639 			skb->truesize -= frag->truesize;
640 		}
641 
642 		/* Everything is OK. Generate! */
643 
644 		err = 0;
645 		offset = 0;
646 		frag = skb_shinfo(skb)->frag_list;
647 		skb_frag_list_init(skb);
648 		skb->data_len = first_len - skb_headlen(skb);
649 		skb->len = first_len;
650 		iph->tot_len = htons(first_len);
651 		iph->frag_off = htons(IP_MF);
652 		ip_send_check(iph);
653 
654 		for (;;) {
655 			/* Prepare header of the next frame,
656 			 * before previous one went down. */
657 			if (frag) {
658 				frag->ip_summed = CHECKSUM_NONE;
659 				skb_reset_transport_header(frag);
660 				__skb_push(frag, hlen);
661 				skb_reset_network_header(frag);
662 				memcpy(skb_network_header(frag), iph, hlen);
663 				iph = ip_hdr(frag);
664 				iph->tot_len = htons(frag->len);
665 				ip_copy_metadata(frag, skb);
666 				if (offset == 0)
667 					ip_options_fragment(frag);
668 				offset += skb->len - hlen;
669 				iph->frag_off = htons(offset>>3);
670 				if (frag->next)
671 					iph->frag_off |= htons(IP_MF);
672 				/* Ready, complete checksum */
673 				ip_send_check(iph);
674 			}
675 
676 			err = output(net, sk, skb);
677 
678 			if (!err)
679 				IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
680 			if (err || !frag)
681 				break;
682 
683 			skb = frag;
684 			frag = skb->next;
685 			skb_mark_not_on_list(skb);
686 		}
687 
688 		if (err == 0) {
689 			IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
690 			return 0;
691 		}
692 
693 		kfree_skb_list(frag);
694 
695 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
696 		return err;
697 
698 slow_path_clean:
699 		skb_walk_frags(skb, frag2) {
700 			if (frag2 == frag)
701 				break;
702 			frag2->sk = NULL;
703 			frag2->destructor = NULL;
704 			skb->truesize += frag2->truesize;
705 		}
706 	}
707 
708 slow_path:
709 	iph = ip_hdr(skb);
710 
711 	left = skb->len - hlen;		/* Space per frame */
712 	ptr = hlen;		/* Where to start from */
713 
714 	/*
715 	 *	Fragment the datagram.
716 	 */
717 
718 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
719 	not_last_frag = iph->frag_off & htons(IP_MF);
720 
721 	/*
722 	 *	Keep copying data until we run out.
723 	 */
724 
725 	while (left > 0) {
726 		len = left;
727 		/* IF: it doesn't fit, use 'mtu' - the data space left */
728 		if (len > mtu)
729 			len = mtu;
730 		/* IF: we are not sending up to and including the packet end
731 		   then align the next start on an eight byte boundary */
732 		if (len < left)	{
733 			len &= ~7;
734 		}
735 
736 		/* Allocate buffer */
737 		skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
738 		if (!skb2) {
739 			err = -ENOMEM;
740 			goto fail;
741 		}
742 
743 		/*
744 		 *	Set up data on packet
745 		 */
746 
747 		ip_copy_metadata(skb2, skb);
748 		skb_reserve(skb2, ll_rs);
749 		skb_put(skb2, len + hlen);
750 		skb_reset_network_header(skb2);
751 		skb2->transport_header = skb2->network_header + hlen;
752 
753 		/*
754 		 *	Charge the memory for the fragment to any owner
755 		 *	it might possess
756 		 */
757 
758 		if (skb->sk)
759 			skb_set_owner_w(skb2, skb->sk);
760 
761 		/*
762 		 *	Copy the packet header into the new buffer.
763 		 */
764 
765 		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
766 
767 		/*
768 		 *	Copy a block of the IP datagram.
769 		 */
770 		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
771 			BUG();
772 		left -= len;
773 
774 		/*
775 		 *	Fill in the new header fields.
776 		 */
777 		iph = ip_hdr(skb2);
778 		iph->frag_off = htons((offset >> 3));
779 
780 		if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
781 			iph->frag_off |= htons(IP_DF);
782 
783 		/* ANK: dirty, but effective trick. Upgrade options only if
784 		 * the segment to be fragmented was THE FIRST (otherwise,
785 		 * options are already fixed) and make it ONCE
786 		 * on the initial skb, so that all the following fragments
787 		 * will inherit fixed options.
788 		 */
789 		if (offset == 0)
790 			ip_options_fragment(skb);
791 
792 		/*
793 		 *	Added AC : If we are fragmenting a fragment that's not the
794 		 *		   last fragment then keep MF on each bit
795 		 */
796 		if (left > 0 || not_last_frag)
797 			iph->frag_off |= htons(IP_MF);
798 		ptr += len;
799 		offset += len;
800 
801 		/*
802 		 *	Put this fragment into the sending queue.
803 		 */
804 		iph->tot_len = htons(len + hlen);
805 
806 		ip_send_check(iph);
807 
808 		err = output(net, sk, skb2);
809 		if (err)
810 			goto fail;
811 
812 		IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
813 	}
814 	consume_skb(skb);
815 	IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
816 	return err;
817 
818 fail:
819 	kfree_skb(skb);
820 	IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
821 	return err;
822 }
823 EXPORT_SYMBOL(ip_do_fragment);
824 
825 int
826 ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
827 {
828 	struct msghdr *msg = from;
829 
830 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
831 		if (!copy_from_iter_full(to, len, &msg->msg_iter))
832 			return -EFAULT;
833 	} else {
834 		__wsum csum = 0;
835 		if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
836 			return -EFAULT;
837 		skb->csum = csum_block_add(skb->csum, csum, odd);
838 	}
839 	return 0;
840 }
841 EXPORT_SYMBOL(ip_generic_getfrag);
842 
843 static inline __wsum
844 csum_page(struct page *page, int offset, int copy)
845 {
846 	char *kaddr;
847 	__wsum csum;
848 	kaddr = kmap(page);
849 	csum = csum_partial(kaddr + offset, copy, 0);
850 	kunmap(page);
851 	return csum;
852 }
853 
854 static int __ip_append_data(struct sock *sk,
855 			    struct flowi4 *fl4,
856 			    struct sk_buff_head *queue,
857 			    struct inet_cork *cork,
858 			    struct page_frag *pfrag,
859 			    int getfrag(void *from, char *to, int offset,
860 					int len, int odd, struct sk_buff *skb),
861 			    void *from, int length, int transhdrlen,
862 			    unsigned int flags)
863 {
864 	struct inet_sock *inet = inet_sk(sk);
865 	struct ubuf_info *uarg = NULL;
866 	struct sk_buff *skb;
867 
868 	struct ip_options *opt = cork->opt;
869 	int hh_len;
870 	int exthdrlen;
871 	int mtu;
872 	int copy;
873 	int err;
874 	int offset = 0;
875 	unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
876 	int csummode = CHECKSUM_NONE;
877 	struct rtable *rt = (struct rtable *)cork->dst;
878 	unsigned int wmem_alloc_delta = 0;
879 	bool paged, extra_uref;
880 	u32 tskey = 0;
881 
882 	skb = skb_peek_tail(queue);
883 
884 	exthdrlen = !skb ? rt->dst.header_len : 0;
885 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
886 	paged = !!cork->gso_size;
887 
888 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
889 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
890 		tskey = sk->sk_tskey++;
891 
892 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
893 
894 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
895 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
896 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
897 
898 	if (cork->length + length > maxnonfragsize - fragheaderlen) {
899 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
900 			       mtu - (opt ? opt->optlen : 0));
901 		return -EMSGSIZE;
902 	}
903 
904 	/*
905 	 * transhdrlen > 0 means that this is the first fragment and we wish
906 	 * it won't be fragmented in the future.
907 	 */
908 	if (transhdrlen &&
909 	    length + fragheaderlen <= mtu &&
910 	    rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
911 	    (!(flags & MSG_MORE) || cork->gso_size) &&
912 	    (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM)))
913 		csummode = CHECKSUM_PARTIAL;
914 
915 	if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) {
916 		uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb));
917 		if (!uarg)
918 			return -ENOBUFS;
919 		extra_uref = true;
920 		if (rt->dst.dev->features & NETIF_F_SG &&
921 		    csummode == CHECKSUM_PARTIAL) {
922 			paged = true;
923 		} else {
924 			uarg->zerocopy = 0;
925 			skb_zcopy_set(skb, uarg, &extra_uref);
926 		}
927 	}
928 
929 	cork->length += length;
930 
931 	/* So, what's going on in the loop below?
932 	 *
933 	 * We use calculated fragment length to generate chained skb,
934 	 * each of segments is IP fragment ready for sending to network after
935 	 * adding appropriate IP header.
936 	 */
937 
938 	if (!skb)
939 		goto alloc_new_skb;
940 
941 	while (length > 0) {
942 		/* Check if the remaining data fits into current packet. */
943 		copy = mtu - skb->len;
944 		if (copy < length)
945 			copy = maxfraglen - skb->len;
946 		if (copy <= 0) {
947 			char *data;
948 			unsigned int datalen;
949 			unsigned int fraglen;
950 			unsigned int fraggap;
951 			unsigned int alloclen;
952 			unsigned int pagedlen;
953 			struct sk_buff *skb_prev;
954 alloc_new_skb:
955 			skb_prev = skb;
956 			if (skb_prev)
957 				fraggap = skb_prev->len - maxfraglen;
958 			else
959 				fraggap = 0;
960 
961 			/*
962 			 * If remaining data exceeds the mtu,
963 			 * we know we need more fragment(s).
964 			 */
965 			datalen = length + fraggap;
966 			if (datalen > mtu - fragheaderlen)
967 				datalen = maxfraglen - fragheaderlen;
968 			fraglen = datalen + fragheaderlen;
969 			pagedlen = 0;
970 
971 			if ((flags & MSG_MORE) &&
972 			    !(rt->dst.dev->features&NETIF_F_SG))
973 				alloclen = mtu;
974 			else if (!paged)
975 				alloclen = fraglen;
976 			else {
977 				alloclen = min_t(int, fraglen, MAX_HEADER);
978 				pagedlen = fraglen - alloclen;
979 			}
980 
981 			alloclen += exthdrlen;
982 
983 			/* The last fragment gets additional space at tail.
984 			 * Note, with MSG_MORE we overallocate on fragments,
985 			 * because we have no idea what fragment will be
986 			 * the last.
987 			 */
988 			if (datalen == length + fraggap)
989 				alloclen += rt->dst.trailer_len;
990 
991 			if (transhdrlen) {
992 				skb = sock_alloc_send_skb(sk,
993 						alloclen + hh_len + 15,
994 						(flags & MSG_DONTWAIT), &err);
995 			} else {
996 				skb = NULL;
997 				if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
998 				    2 * sk->sk_sndbuf)
999 					skb = alloc_skb(alloclen + hh_len + 15,
1000 							sk->sk_allocation);
1001 				if (unlikely(!skb))
1002 					err = -ENOBUFS;
1003 			}
1004 			if (!skb)
1005 				goto error;
1006 
1007 			/*
1008 			 *	Fill in the control structures
1009 			 */
1010 			skb->ip_summed = csummode;
1011 			skb->csum = 0;
1012 			skb_reserve(skb, hh_len);
1013 
1014 			/*
1015 			 *	Find where to start putting bytes.
1016 			 */
1017 			data = skb_put(skb, fraglen + exthdrlen - pagedlen);
1018 			skb_set_network_header(skb, exthdrlen);
1019 			skb->transport_header = (skb->network_header +
1020 						 fragheaderlen);
1021 			data += fragheaderlen + exthdrlen;
1022 
1023 			if (fraggap) {
1024 				skb->csum = skb_copy_and_csum_bits(
1025 					skb_prev, maxfraglen,
1026 					data + transhdrlen, fraggap, 0);
1027 				skb_prev->csum = csum_sub(skb_prev->csum,
1028 							  skb->csum);
1029 				data += fraggap;
1030 				pskb_trim_unique(skb_prev, maxfraglen);
1031 			}
1032 
1033 			copy = datalen - transhdrlen - fraggap - pagedlen;
1034 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1035 				err = -EFAULT;
1036 				kfree_skb(skb);
1037 				goto error;
1038 			}
1039 
1040 			offset += copy;
1041 			length -= copy + transhdrlen;
1042 			transhdrlen = 0;
1043 			exthdrlen = 0;
1044 			csummode = CHECKSUM_NONE;
1045 
1046 			/* only the initial fragment is time stamped */
1047 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
1048 			cork->tx_flags = 0;
1049 			skb_shinfo(skb)->tskey = tskey;
1050 			tskey = 0;
1051 			skb_zcopy_set(skb, uarg, &extra_uref);
1052 
1053 			if ((flags & MSG_CONFIRM) && !skb_prev)
1054 				skb_set_dst_pending_confirm(skb, 1);
1055 
1056 			/*
1057 			 * Put the packet on the pending queue.
1058 			 */
1059 			if (!skb->destructor) {
1060 				skb->destructor = sock_wfree;
1061 				skb->sk = sk;
1062 				wmem_alloc_delta += skb->truesize;
1063 			}
1064 			__skb_queue_tail(queue, skb);
1065 			continue;
1066 		}
1067 
1068 		if (copy > length)
1069 			copy = length;
1070 
1071 		if (!(rt->dst.dev->features&NETIF_F_SG) &&
1072 		    skb_tailroom(skb) >= copy) {
1073 			unsigned int off;
1074 
1075 			off = skb->len;
1076 			if (getfrag(from, skb_put(skb, copy),
1077 					offset, copy, off, skb) < 0) {
1078 				__skb_trim(skb, off);
1079 				err = -EFAULT;
1080 				goto error;
1081 			}
1082 		} else if (!uarg || !uarg->zerocopy) {
1083 			int i = skb_shinfo(skb)->nr_frags;
1084 
1085 			err = -ENOMEM;
1086 			if (!sk_page_frag_refill(sk, pfrag))
1087 				goto error;
1088 
1089 			if (!skb_can_coalesce(skb, i, pfrag->page,
1090 					      pfrag->offset)) {
1091 				err = -EMSGSIZE;
1092 				if (i == MAX_SKB_FRAGS)
1093 					goto error;
1094 
1095 				__skb_fill_page_desc(skb, i, pfrag->page,
1096 						     pfrag->offset, 0);
1097 				skb_shinfo(skb)->nr_frags = ++i;
1098 				get_page(pfrag->page);
1099 			}
1100 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
1101 			if (getfrag(from,
1102 				    page_address(pfrag->page) + pfrag->offset,
1103 				    offset, copy, skb->len, skb) < 0)
1104 				goto error_efault;
1105 
1106 			pfrag->offset += copy;
1107 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1108 			skb->len += copy;
1109 			skb->data_len += copy;
1110 			skb->truesize += copy;
1111 			wmem_alloc_delta += copy;
1112 		} else {
1113 			err = skb_zerocopy_iter_dgram(skb, from, copy);
1114 			if (err < 0)
1115 				goto error;
1116 		}
1117 		offset += copy;
1118 		length -= copy;
1119 	}
1120 
1121 	if (wmem_alloc_delta)
1122 		refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
1123 	return 0;
1124 
1125 error_efault:
1126 	err = -EFAULT;
1127 error:
1128 	if (uarg)
1129 		sock_zerocopy_put_abort(uarg, extra_uref);
1130 	cork->length -= length;
1131 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1132 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
1133 	return err;
1134 }
1135 
1136 static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1137 			 struct ipcm_cookie *ipc, struct rtable **rtp)
1138 {
1139 	struct ip_options_rcu *opt;
1140 	struct rtable *rt;
1141 
1142 	rt = *rtp;
1143 	if (unlikely(!rt))
1144 		return -EFAULT;
1145 
1146 	/*
1147 	 * setup for corking.
1148 	 */
1149 	opt = ipc->opt;
1150 	if (opt) {
1151 		if (!cork->opt) {
1152 			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1153 					    sk->sk_allocation);
1154 			if (unlikely(!cork->opt))
1155 				return -ENOBUFS;
1156 		}
1157 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
1158 		cork->flags |= IPCORK_OPT;
1159 		cork->addr = ipc->addr;
1160 	}
1161 
1162 	/*
1163 	 * We steal reference to this route, caller should not release it
1164 	 */
1165 	*rtp = NULL;
1166 	cork->fragsize = ip_sk_use_pmtu(sk) ?
1167 			 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
1168 
1169 	cork->gso_size = ipc->gso_size;
1170 	cork->dst = &rt->dst;
1171 	cork->length = 0;
1172 	cork->ttl = ipc->ttl;
1173 	cork->tos = ipc->tos;
1174 	cork->priority = ipc->priority;
1175 	cork->transmit_time = ipc->sockc.transmit_time;
1176 	cork->tx_flags = 0;
1177 	sock_tx_timestamp(sk, ipc->sockc.tsflags, &cork->tx_flags);
1178 
1179 	return 0;
1180 }
1181 
1182 /*
1183  *	ip_append_data() and ip_append_page() can make one large IP datagram
1184  *	from many pieces of data. Each pieces will be holded on the socket
1185  *	until ip_push_pending_frames() is called. Each piece can be a page
1186  *	or non-page data.
1187  *
1188  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
1189  *	this interface potentially.
1190  *
1191  *	LATER: length must be adjusted by pad at tail, when it is required.
1192  */
1193 int ip_append_data(struct sock *sk, struct flowi4 *fl4,
1194 		   int getfrag(void *from, char *to, int offset, int len,
1195 			       int odd, struct sk_buff *skb),
1196 		   void *from, int length, int transhdrlen,
1197 		   struct ipcm_cookie *ipc, struct rtable **rtp,
1198 		   unsigned int flags)
1199 {
1200 	struct inet_sock *inet = inet_sk(sk);
1201 	int err;
1202 
1203 	if (flags&MSG_PROBE)
1204 		return 0;
1205 
1206 	if (skb_queue_empty(&sk->sk_write_queue)) {
1207 		err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
1208 		if (err)
1209 			return err;
1210 	} else {
1211 		transhdrlen = 0;
1212 	}
1213 
1214 	return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1215 				sk_page_frag(sk), getfrag,
1216 				from, length, transhdrlen, flags);
1217 }
1218 
1219 ssize_t	ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1220 		       int offset, size_t size, int flags)
1221 {
1222 	struct inet_sock *inet = inet_sk(sk);
1223 	struct sk_buff *skb;
1224 	struct rtable *rt;
1225 	struct ip_options *opt = NULL;
1226 	struct inet_cork *cork;
1227 	int hh_len;
1228 	int mtu;
1229 	int len;
1230 	int err;
1231 	unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
1232 
1233 	if (inet->hdrincl)
1234 		return -EPERM;
1235 
1236 	if (flags&MSG_PROBE)
1237 		return 0;
1238 
1239 	if (skb_queue_empty(&sk->sk_write_queue))
1240 		return -EINVAL;
1241 
1242 	cork = &inet->cork.base;
1243 	rt = (struct rtable *)cork->dst;
1244 	if (cork->flags & IPCORK_OPT)
1245 		opt = cork->opt;
1246 
1247 	if (!(rt->dst.dev->features&NETIF_F_SG))
1248 		return -EOPNOTSUPP;
1249 
1250 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1251 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
1252 
1253 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1254 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
1255 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
1256 
1257 	if (cork->length + size > maxnonfragsize - fragheaderlen) {
1258 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1259 			       mtu - (opt ? opt->optlen : 0));
1260 		return -EMSGSIZE;
1261 	}
1262 
1263 	skb = skb_peek_tail(&sk->sk_write_queue);
1264 	if (!skb)
1265 		return -EINVAL;
1266 
1267 	cork->length += size;
1268 
1269 	while (size > 0) {
1270 		/* Check if the remaining data fits into current packet. */
1271 		len = mtu - skb->len;
1272 		if (len < size)
1273 			len = maxfraglen - skb->len;
1274 
1275 		if (len <= 0) {
1276 			struct sk_buff *skb_prev;
1277 			int alloclen;
1278 
1279 			skb_prev = skb;
1280 			fraggap = skb_prev->len - maxfraglen;
1281 
1282 			alloclen = fragheaderlen + hh_len + fraggap + 15;
1283 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1284 			if (unlikely(!skb)) {
1285 				err = -ENOBUFS;
1286 				goto error;
1287 			}
1288 
1289 			/*
1290 			 *	Fill in the control structures
1291 			 */
1292 			skb->ip_summed = CHECKSUM_NONE;
1293 			skb->csum = 0;
1294 			skb_reserve(skb, hh_len);
1295 
1296 			/*
1297 			 *	Find where to start putting bytes.
1298 			 */
1299 			skb_put(skb, fragheaderlen + fraggap);
1300 			skb_reset_network_header(skb);
1301 			skb->transport_header = (skb->network_header +
1302 						 fragheaderlen);
1303 			if (fraggap) {
1304 				skb->csum = skb_copy_and_csum_bits(skb_prev,
1305 								   maxfraglen,
1306 						    skb_transport_header(skb),
1307 								   fraggap, 0);
1308 				skb_prev->csum = csum_sub(skb_prev->csum,
1309 							  skb->csum);
1310 				pskb_trim_unique(skb_prev, maxfraglen);
1311 			}
1312 
1313 			/*
1314 			 * Put the packet on the pending queue.
1315 			 */
1316 			__skb_queue_tail(&sk->sk_write_queue, skb);
1317 			continue;
1318 		}
1319 
1320 		if (len > size)
1321 			len = size;
1322 
1323 		if (skb_append_pagefrags(skb, page, offset, len)) {
1324 			err = -EMSGSIZE;
1325 			goto error;
1326 		}
1327 
1328 		if (skb->ip_summed == CHECKSUM_NONE) {
1329 			__wsum csum;
1330 			csum = csum_page(page, offset, len);
1331 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
1332 		}
1333 
1334 		skb->len += len;
1335 		skb->data_len += len;
1336 		skb->truesize += len;
1337 		refcount_add(len, &sk->sk_wmem_alloc);
1338 		offset += len;
1339 		size -= len;
1340 	}
1341 	return 0;
1342 
1343 error:
1344 	cork->length -= size;
1345 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1346 	return err;
1347 }
1348 
1349 static void ip_cork_release(struct inet_cork *cork)
1350 {
1351 	cork->flags &= ~IPCORK_OPT;
1352 	kfree(cork->opt);
1353 	cork->opt = NULL;
1354 	dst_release(cork->dst);
1355 	cork->dst = NULL;
1356 }
1357 
1358 /*
1359  *	Combined all pending IP fragments on the socket as one IP datagram
1360  *	and push them out.
1361  */
1362 struct sk_buff *__ip_make_skb(struct sock *sk,
1363 			      struct flowi4 *fl4,
1364 			      struct sk_buff_head *queue,
1365 			      struct inet_cork *cork)
1366 {
1367 	struct sk_buff *skb, *tmp_skb;
1368 	struct sk_buff **tail_skb;
1369 	struct inet_sock *inet = inet_sk(sk);
1370 	struct net *net = sock_net(sk);
1371 	struct ip_options *opt = NULL;
1372 	struct rtable *rt = (struct rtable *)cork->dst;
1373 	struct iphdr *iph;
1374 	__be16 df = 0;
1375 	__u8 ttl;
1376 
1377 	skb = __skb_dequeue(queue);
1378 	if (!skb)
1379 		goto out;
1380 	tail_skb = &(skb_shinfo(skb)->frag_list);
1381 
1382 	/* move skb->data to ip header from ext header */
1383 	if (skb->data < skb_network_header(skb))
1384 		__skb_pull(skb, skb_network_offset(skb));
1385 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1386 		__skb_pull(tmp_skb, skb_network_header_len(skb));
1387 		*tail_skb = tmp_skb;
1388 		tail_skb = &(tmp_skb->next);
1389 		skb->len += tmp_skb->len;
1390 		skb->data_len += tmp_skb->len;
1391 		skb->truesize += tmp_skb->truesize;
1392 		tmp_skb->destructor = NULL;
1393 		tmp_skb->sk = NULL;
1394 	}
1395 
1396 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1397 	 * to fragment the frame generated here. No matter, what transforms
1398 	 * how transforms change size of the packet, it will come out.
1399 	 */
1400 	skb->ignore_df = ip_sk_ignore_df(sk);
1401 
1402 	/* DF bit is set when we want to see DF on outgoing frames.
1403 	 * If ignore_df is set too, we still allow to fragment this frame
1404 	 * locally. */
1405 	if (inet->pmtudisc == IP_PMTUDISC_DO ||
1406 	    inet->pmtudisc == IP_PMTUDISC_PROBE ||
1407 	    (skb->len <= dst_mtu(&rt->dst) &&
1408 	     ip_dont_fragment(sk, &rt->dst)))
1409 		df = htons(IP_DF);
1410 
1411 	if (cork->flags & IPCORK_OPT)
1412 		opt = cork->opt;
1413 
1414 	if (cork->ttl != 0)
1415 		ttl = cork->ttl;
1416 	else if (rt->rt_type == RTN_MULTICAST)
1417 		ttl = inet->mc_ttl;
1418 	else
1419 		ttl = ip_select_ttl(inet, &rt->dst);
1420 
1421 	iph = ip_hdr(skb);
1422 	iph->version = 4;
1423 	iph->ihl = 5;
1424 	iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1425 	iph->frag_off = df;
1426 	iph->ttl = ttl;
1427 	iph->protocol = sk->sk_protocol;
1428 	ip_copy_addrs(iph, fl4);
1429 	ip_select_ident(net, skb, sk);
1430 
1431 	if (opt) {
1432 		iph->ihl += opt->optlen>>2;
1433 		ip_options_build(skb, opt, cork->addr, rt, 0);
1434 	}
1435 
1436 	skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1437 	skb->mark = sk->sk_mark;
1438 	skb->tstamp = cork->transmit_time;
1439 	/*
1440 	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1441 	 * on dst refcount
1442 	 */
1443 	cork->dst = NULL;
1444 	skb_dst_set(skb, &rt->dst);
1445 
1446 	if (iph->protocol == IPPROTO_ICMP)
1447 		icmp_out_count(net, ((struct icmphdr *)
1448 			skb_transport_header(skb))->type);
1449 
1450 	ip_cork_release(cork);
1451 out:
1452 	return skb;
1453 }
1454 
1455 int ip_send_skb(struct net *net, struct sk_buff *skb)
1456 {
1457 	int err;
1458 
1459 	err = ip_local_out(net, skb->sk, skb);
1460 	if (err) {
1461 		if (err > 0)
1462 			err = net_xmit_errno(err);
1463 		if (err)
1464 			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
1465 	}
1466 
1467 	return err;
1468 }
1469 
1470 int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
1471 {
1472 	struct sk_buff *skb;
1473 
1474 	skb = ip_finish_skb(sk, fl4);
1475 	if (!skb)
1476 		return 0;
1477 
1478 	/* Netfilter gets whole the not fragmented skb. */
1479 	return ip_send_skb(sock_net(sk), skb);
1480 }
1481 
1482 /*
1483  *	Throw away all pending data on the socket.
1484  */
1485 static void __ip_flush_pending_frames(struct sock *sk,
1486 				      struct sk_buff_head *queue,
1487 				      struct inet_cork *cork)
1488 {
1489 	struct sk_buff *skb;
1490 
1491 	while ((skb = __skb_dequeue_tail(queue)) != NULL)
1492 		kfree_skb(skb);
1493 
1494 	ip_cork_release(cork);
1495 }
1496 
1497 void ip_flush_pending_frames(struct sock *sk)
1498 {
1499 	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
1500 }
1501 
1502 struct sk_buff *ip_make_skb(struct sock *sk,
1503 			    struct flowi4 *fl4,
1504 			    int getfrag(void *from, char *to, int offset,
1505 					int len, int odd, struct sk_buff *skb),
1506 			    void *from, int length, int transhdrlen,
1507 			    struct ipcm_cookie *ipc, struct rtable **rtp,
1508 			    struct inet_cork *cork, unsigned int flags)
1509 {
1510 	struct sk_buff_head queue;
1511 	int err;
1512 
1513 	if (flags & MSG_PROBE)
1514 		return NULL;
1515 
1516 	__skb_queue_head_init(&queue);
1517 
1518 	cork->flags = 0;
1519 	cork->addr = 0;
1520 	cork->opt = NULL;
1521 	err = ip_setup_cork(sk, cork, ipc, rtp);
1522 	if (err)
1523 		return ERR_PTR(err);
1524 
1525 	err = __ip_append_data(sk, fl4, &queue, cork,
1526 			       &current->task_frag, getfrag,
1527 			       from, length, transhdrlen, flags);
1528 	if (err) {
1529 		__ip_flush_pending_frames(sk, &queue, cork);
1530 		return ERR_PTR(err);
1531 	}
1532 
1533 	return __ip_make_skb(sk, fl4, &queue, cork);
1534 }
1535 
1536 /*
1537  *	Fetch data from kernel space and fill in checksum if needed.
1538  */
1539 static int ip_reply_glue_bits(void *dptr, char *to, int offset,
1540 			      int len, int odd, struct sk_buff *skb)
1541 {
1542 	__wsum csum;
1543 
1544 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
1545 	skb->csum = csum_block_add(skb->csum, csum, odd);
1546 	return 0;
1547 }
1548 
1549 /*
1550  *	Generic function to send a packet as reply to another packet.
1551  *	Used to send some TCP resets/acks so far.
1552  */
1553 void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
1554 			   const struct ip_options *sopt,
1555 			   __be32 daddr, __be32 saddr,
1556 			   const struct ip_reply_arg *arg,
1557 			   unsigned int len)
1558 {
1559 	struct ip_options_data replyopts;
1560 	struct ipcm_cookie ipc;
1561 	struct flowi4 fl4;
1562 	struct rtable *rt = skb_rtable(skb);
1563 	struct net *net = sock_net(sk);
1564 	struct sk_buff *nskb;
1565 	int err;
1566 	int oif;
1567 
1568 	if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt))
1569 		return;
1570 
1571 	ipcm_init(&ipc);
1572 	ipc.addr = daddr;
1573 
1574 	if (replyopts.opt.opt.optlen) {
1575 		ipc.opt = &replyopts.opt;
1576 
1577 		if (replyopts.opt.opt.srr)
1578 			daddr = replyopts.opt.opt.faddr;
1579 	}
1580 
1581 	oif = arg->bound_dev_if;
1582 	if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
1583 		oif = skb->skb_iif;
1584 
1585 	flowi4_init_output(&fl4, oif,
1586 			   IP4_REPLY_MARK(net, skb->mark) ?: sk->sk_mark,
1587 			   RT_TOS(arg->tos),
1588 			   RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1589 			   ip_reply_arg_flowi_flags(arg),
1590 			   daddr, saddr,
1591 			   tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1592 			   arg->uid);
1593 	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1594 	rt = ip_route_output_key(net, &fl4);
1595 	if (IS_ERR(rt))
1596 		return;
1597 
1598 	inet_sk(sk)->tos = arg->tos;
1599 
1600 	sk->sk_priority = skb->priority;
1601 	sk->sk_protocol = ip_hdr(skb)->protocol;
1602 	sk->sk_bound_dev_if = arg->bound_dev_if;
1603 	sk->sk_sndbuf = sysctl_wmem_default;
1604 	sk->sk_mark = fl4.flowi4_mark;
1605 	err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
1606 			     len, 0, &ipc, &rt, MSG_DONTWAIT);
1607 	if (unlikely(err)) {
1608 		ip_flush_pending_frames(sk);
1609 		goto out;
1610 	}
1611 
1612 	nskb = skb_peek(&sk->sk_write_queue);
1613 	if (nskb) {
1614 		if (arg->csumoffset >= 0)
1615 			*((__sum16 *)skb_transport_header(nskb) +
1616 			  arg->csumoffset) = csum_fold(csum_add(nskb->csum,
1617 								arg->csum));
1618 		nskb->ip_summed = CHECKSUM_NONE;
1619 		ip_push_pending_frames(sk, &fl4);
1620 	}
1621 out:
1622 	ip_rt_put(rt);
1623 }
1624 
1625 void __init ip_init(void)
1626 {
1627 	ip_rt_init();
1628 	inet_initpeers();
1629 
1630 #if defined(CONFIG_IP_MULTICAST)
1631 	igmp_mc_init();
1632 #endif
1633 }
1634