xref: /openbmc/linux/net/ipv4/ip_output.c (revision 74a22e8f)
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 	to->skb_iif = from->skb_iif;
520 	skb_dst_drop(to);
521 	skb_dst_copy(to, from);
522 	to->dev = from->dev;
523 	to->mark = from->mark;
524 
525 	skb_copy_hash(to, from);
526 
527 	/* Copy the flags to each fragment. */
528 	IPCB(to)->flags = IPCB(from)->flags;
529 
530 #ifdef CONFIG_NET_SCHED
531 	to->tc_index = from->tc_index;
532 #endif
533 	nf_copy(to, from);
534 	skb_ext_copy(to, from);
535 #if IS_ENABLED(CONFIG_IP_VS)
536 	to->ipvs_property = from->ipvs_property;
537 #endif
538 	skb_copy_secmark(to, from);
539 }
540 
541 static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
542 		       unsigned int mtu,
543 		       int (*output)(struct net *, struct sock *, struct sk_buff *))
544 {
545 	struct iphdr *iph = ip_hdr(skb);
546 
547 	if ((iph->frag_off & htons(IP_DF)) == 0)
548 		return ip_do_fragment(net, sk, skb, output);
549 
550 	if (unlikely(!skb->ignore_df ||
551 		     (IPCB(skb)->frag_max_size &&
552 		      IPCB(skb)->frag_max_size > mtu))) {
553 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
554 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
555 			  htonl(mtu));
556 		kfree_skb(skb);
557 		return -EMSGSIZE;
558 	}
559 
560 	return ip_do_fragment(net, sk, skb, output);
561 }
562 
563 /*
564  *	This IP datagram is too large to be sent in one piece.  Break it up into
565  *	smaller pieces (each of size equal to IP header plus
566  *	a block of the data of the original IP data part) that will yet fit in a
567  *	single device frame, and queue such a frame for sending.
568  */
569 
570 int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
571 		   int (*output)(struct net *, struct sock *, struct sk_buff *))
572 {
573 	struct iphdr *iph;
574 	int ptr;
575 	struct sk_buff *skb2;
576 	unsigned int mtu, hlen, left, len, ll_rs;
577 	int offset;
578 	__be16 not_last_frag;
579 	struct rtable *rt = skb_rtable(skb);
580 	int err = 0;
581 
582 	/* for offloaded checksums cleanup checksum before fragmentation */
583 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
584 	    (err = skb_checksum_help(skb)))
585 		goto fail;
586 
587 	/*
588 	 *	Point into the IP datagram header.
589 	 */
590 
591 	iph = ip_hdr(skb);
592 
593 	mtu = ip_skb_dst_mtu(sk, skb);
594 	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
595 		mtu = IPCB(skb)->frag_max_size;
596 
597 	/*
598 	 *	Setup starting values.
599 	 */
600 
601 	hlen = iph->ihl * 4;
602 	mtu = mtu - hlen;	/* Size of data space */
603 	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
604 	ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
605 
606 	/* When frag_list is given, use it. First, check its validity:
607 	 * some transformers could create wrong frag_list or break existing
608 	 * one, it is not prohibited. In this case fall back to copying.
609 	 *
610 	 * LATER: this step can be merged to real generation of fragments,
611 	 * we can switch to copy when see the first bad fragment.
612 	 */
613 	if (skb_has_frag_list(skb)) {
614 		struct sk_buff *frag, *frag2;
615 		unsigned int first_len = skb_pagelen(skb);
616 
617 		if (first_len - hlen > mtu ||
618 		    ((first_len - hlen) & 7) ||
619 		    ip_is_fragment(iph) ||
620 		    skb_cloned(skb) ||
621 		    skb_headroom(skb) < ll_rs)
622 			goto slow_path;
623 
624 		skb_walk_frags(skb, frag) {
625 			/* Correct geometry. */
626 			if (frag->len > mtu ||
627 			    ((frag->len & 7) && frag->next) ||
628 			    skb_headroom(frag) < hlen + ll_rs)
629 				goto slow_path_clean;
630 
631 			/* Partially cloned skb? */
632 			if (skb_shared(frag))
633 				goto slow_path_clean;
634 
635 			BUG_ON(frag->sk);
636 			if (skb->sk) {
637 				frag->sk = skb->sk;
638 				frag->destructor = sock_wfree;
639 			}
640 			skb->truesize -= frag->truesize;
641 		}
642 
643 		/* Everything is OK. Generate! */
644 
645 		err = 0;
646 		offset = 0;
647 		frag = skb_shinfo(skb)->frag_list;
648 		skb_frag_list_init(skb);
649 		skb->data_len = first_len - skb_headlen(skb);
650 		skb->len = first_len;
651 		iph->tot_len = htons(first_len);
652 		iph->frag_off = htons(IP_MF);
653 		ip_send_check(iph);
654 
655 		for (;;) {
656 			/* Prepare header of the next frame,
657 			 * before previous one went down. */
658 			if (frag) {
659 				frag->ip_summed = CHECKSUM_NONE;
660 				skb_reset_transport_header(frag);
661 				__skb_push(frag, hlen);
662 				skb_reset_network_header(frag);
663 				memcpy(skb_network_header(frag), iph, hlen);
664 				iph = ip_hdr(frag);
665 				iph->tot_len = htons(frag->len);
666 				ip_copy_metadata(frag, skb);
667 				if (offset == 0)
668 					ip_options_fragment(frag);
669 				offset += skb->len - hlen;
670 				iph->frag_off = htons(offset>>3);
671 				if (frag->next)
672 					iph->frag_off |= htons(IP_MF);
673 				/* Ready, complete checksum */
674 				ip_send_check(iph);
675 			}
676 
677 			err = output(net, sk, skb);
678 
679 			if (!err)
680 				IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
681 			if (err || !frag)
682 				break;
683 
684 			skb = frag;
685 			frag = skb->next;
686 			skb_mark_not_on_list(skb);
687 		}
688 
689 		if (err == 0) {
690 			IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
691 			return 0;
692 		}
693 
694 		kfree_skb_list(frag);
695 
696 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
697 		return err;
698 
699 slow_path_clean:
700 		skb_walk_frags(skb, frag2) {
701 			if (frag2 == frag)
702 				break;
703 			frag2->sk = NULL;
704 			frag2->destructor = NULL;
705 			skb->truesize += frag2->truesize;
706 		}
707 	}
708 
709 slow_path:
710 	iph = ip_hdr(skb);
711 
712 	left = skb->len - hlen;		/* Space per frame */
713 	ptr = hlen;		/* Where to start from */
714 
715 	/*
716 	 *	Fragment the datagram.
717 	 */
718 
719 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
720 	not_last_frag = iph->frag_off & htons(IP_MF);
721 
722 	/*
723 	 *	Keep copying data until we run out.
724 	 */
725 
726 	while (left > 0) {
727 		len = left;
728 		/* IF: it doesn't fit, use 'mtu' - the data space left */
729 		if (len > mtu)
730 			len = mtu;
731 		/* IF: we are not sending up to and including the packet end
732 		   then align the next start on an eight byte boundary */
733 		if (len < left)	{
734 			len &= ~7;
735 		}
736 
737 		/* Allocate buffer */
738 		skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
739 		if (!skb2) {
740 			err = -ENOMEM;
741 			goto fail;
742 		}
743 
744 		/*
745 		 *	Set up data on packet
746 		 */
747 
748 		ip_copy_metadata(skb2, skb);
749 		skb_reserve(skb2, ll_rs);
750 		skb_put(skb2, len + hlen);
751 		skb_reset_network_header(skb2);
752 		skb2->transport_header = skb2->network_header + hlen;
753 
754 		/*
755 		 *	Charge the memory for the fragment to any owner
756 		 *	it might possess
757 		 */
758 
759 		if (skb->sk)
760 			skb_set_owner_w(skb2, skb->sk);
761 
762 		/*
763 		 *	Copy the packet header into the new buffer.
764 		 */
765 
766 		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
767 
768 		/*
769 		 *	Copy a block of the IP datagram.
770 		 */
771 		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
772 			BUG();
773 		left -= len;
774 
775 		/*
776 		 *	Fill in the new header fields.
777 		 */
778 		iph = ip_hdr(skb2);
779 		iph->frag_off = htons((offset >> 3));
780 
781 		if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
782 			iph->frag_off |= htons(IP_DF);
783 
784 		/* ANK: dirty, but effective trick. Upgrade options only if
785 		 * the segment to be fragmented was THE FIRST (otherwise,
786 		 * options are already fixed) and make it ONCE
787 		 * on the initial skb, so that all the following fragments
788 		 * will inherit fixed options.
789 		 */
790 		if (offset == 0)
791 			ip_options_fragment(skb);
792 
793 		/*
794 		 *	Added AC : If we are fragmenting a fragment that's not the
795 		 *		   last fragment then keep MF on each bit
796 		 */
797 		if (left > 0 || not_last_frag)
798 			iph->frag_off |= htons(IP_MF);
799 		ptr += len;
800 		offset += len;
801 
802 		/*
803 		 *	Put this fragment into the sending queue.
804 		 */
805 		iph->tot_len = htons(len + hlen);
806 
807 		ip_send_check(iph);
808 
809 		err = output(net, sk, skb2);
810 		if (err)
811 			goto fail;
812 
813 		IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
814 	}
815 	consume_skb(skb);
816 	IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
817 	return err;
818 
819 fail:
820 	kfree_skb(skb);
821 	IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
822 	return err;
823 }
824 EXPORT_SYMBOL(ip_do_fragment);
825 
826 int
827 ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
828 {
829 	struct msghdr *msg = from;
830 
831 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
832 		if (!copy_from_iter_full(to, len, &msg->msg_iter))
833 			return -EFAULT;
834 	} else {
835 		__wsum csum = 0;
836 		if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
837 			return -EFAULT;
838 		skb->csum = csum_block_add(skb->csum, csum, odd);
839 	}
840 	return 0;
841 }
842 EXPORT_SYMBOL(ip_generic_getfrag);
843 
844 static inline __wsum
845 csum_page(struct page *page, int offset, int copy)
846 {
847 	char *kaddr;
848 	__wsum csum;
849 	kaddr = kmap(page);
850 	csum = csum_partial(kaddr + offset, copy, 0);
851 	kunmap(page);
852 	return csum;
853 }
854 
855 static int __ip_append_data(struct sock *sk,
856 			    struct flowi4 *fl4,
857 			    struct sk_buff_head *queue,
858 			    struct inet_cork *cork,
859 			    struct page_frag *pfrag,
860 			    int getfrag(void *from, char *to, int offset,
861 					int len, int odd, struct sk_buff *skb),
862 			    void *from, int length, int transhdrlen,
863 			    unsigned int flags)
864 {
865 	struct inet_sock *inet = inet_sk(sk);
866 	struct ubuf_info *uarg = NULL;
867 	struct sk_buff *skb;
868 
869 	struct ip_options *opt = cork->opt;
870 	int hh_len;
871 	int exthdrlen;
872 	int mtu;
873 	int copy;
874 	int err;
875 	int offset = 0;
876 	unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
877 	int csummode = CHECKSUM_NONE;
878 	struct rtable *rt = (struct rtable *)cork->dst;
879 	unsigned int wmem_alloc_delta = 0;
880 	bool paged, extra_uref;
881 	u32 tskey = 0;
882 
883 	skb = skb_peek_tail(queue);
884 
885 	exthdrlen = !skb ? rt->dst.header_len : 0;
886 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
887 	paged = !!cork->gso_size;
888 
889 	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
890 	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
891 		tskey = sk->sk_tskey++;
892 
893 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
894 
895 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
896 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
897 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
898 
899 	if (cork->length + length > maxnonfragsize - fragheaderlen) {
900 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
901 			       mtu - (opt ? opt->optlen : 0));
902 		return -EMSGSIZE;
903 	}
904 
905 	/*
906 	 * transhdrlen > 0 means that this is the first fragment and we wish
907 	 * it won't be fragmented in the future.
908 	 */
909 	if (transhdrlen &&
910 	    length + fragheaderlen <= mtu &&
911 	    rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
912 	    (!(flags & MSG_MORE) || cork->gso_size) &&
913 	    (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM)))
914 		csummode = CHECKSUM_PARTIAL;
915 
916 	if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) {
917 		uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb));
918 		if (!uarg)
919 			return -ENOBUFS;
920 		extra_uref = true;
921 		if (rt->dst.dev->features & NETIF_F_SG &&
922 		    csummode == CHECKSUM_PARTIAL) {
923 			paged = true;
924 		} else {
925 			uarg->zerocopy = 0;
926 			skb_zcopy_set(skb, uarg, &extra_uref);
927 		}
928 	}
929 
930 	cork->length += length;
931 
932 	/* So, what's going on in the loop below?
933 	 *
934 	 * We use calculated fragment length to generate chained skb,
935 	 * each of segments is IP fragment ready for sending to network after
936 	 * adding appropriate IP header.
937 	 */
938 
939 	if (!skb)
940 		goto alloc_new_skb;
941 
942 	while (length > 0) {
943 		/* Check if the remaining data fits into current packet. */
944 		copy = mtu - skb->len;
945 		if (copy < length)
946 			copy = maxfraglen - skb->len;
947 		if (copy <= 0) {
948 			char *data;
949 			unsigned int datalen;
950 			unsigned int fraglen;
951 			unsigned int fraggap;
952 			unsigned int alloclen;
953 			unsigned int pagedlen;
954 			struct sk_buff *skb_prev;
955 alloc_new_skb:
956 			skb_prev = skb;
957 			if (skb_prev)
958 				fraggap = skb_prev->len - maxfraglen;
959 			else
960 				fraggap = 0;
961 
962 			/*
963 			 * If remaining data exceeds the mtu,
964 			 * we know we need more fragment(s).
965 			 */
966 			datalen = length + fraggap;
967 			if (datalen > mtu - fragheaderlen)
968 				datalen = maxfraglen - fragheaderlen;
969 			fraglen = datalen + fragheaderlen;
970 			pagedlen = 0;
971 
972 			if ((flags & MSG_MORE) &&
973 			    !(rt->dst.dev->features&NETIF_F_SG))
974 				alloclen = mtu;
975 			else if (!paged)
976 				alloclen = fraglen;
977 			else {
978 				alloclen = min_t(int, fraglen, MAX_HEADER);
979 				pagedlen = fraglen - alloclen;
980 			}
981 
982 			alloclen += exthdrlen;
983 
984 			/* The last fragment gets additional space at tail.
985 			 * Note, with MSG_MORE we overallocate on fragments,
986 			 * because we have no idea what fragment will be
987 			 * the last.
988 			 */
989 			if (datalen == length + fraggap)
990 				alloclen += rt->dst.trailer_len;
991 
992 			if (transhdrlen) {
993 				skb = sock_alloc_send_skb(sk,
994 						alloclen + hh_len + 15,
995 						(flags & MSG_DONTWAIT), &err);
996 			} else {
997 				skb = NULL;
998 				if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
999 				    2 * sk->sk_sndbuf)
1000 					skb = alloc_skb(alloclen + hh_len + 15,
1001 							sk->sk_allocation);
1002 				if (unlikely(!skb))
1003 					err = -ENOBUFS;
1004 			}
1005 			if (!skb)
1006 				goto error;
1007 
1008 			/*
1009 			 *	Fill in the control structures
1010 			 */
1011 			skb->ip_summed = csummode;
1012 			skb->csum = 0;
1013 			skb_reserve(skb, hh_len);
1014 
1015 			/*
1016 			 *	Find where to start putting bytes.
1017 			 */
1018 			data = skb_put(skb, fraglen + exthdrlen - pagedlen);
1019 			skb_set_network_header(skb, exthdrlen);
1020 			skb->transport_header = (skb->network_header +
1021 						 fragheaderlen);
1022 			data += fragheaderlen + exthdrlen;
1023 
1024 			if (fraggap) {
1025 				skb->csum = skb_copy_and_csum_bits(
1026 					skb_prev, maxfraglen,
1027 					data + transhdrlen, fraggap, 0);
1028 				skb_prev->csum = csum_sub(skb_prev->csum,
1029 							  skb->csum);
1030 				data += fraggap;
1031 				pskb_trim_unique(skb_prev, maxfraglen);
1032 			}
1033 
1034 			copy = datalen - transhdrlen - fraggap - pagedlen;
1035 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1036 				err = -EFAULT;
1037 				kfree_skb(skb);
1038 				goto error;
1039 			}
1040 
1041 			offset += copy;
1042 			length -= copy + transhdrlen;
1043 			transhdrlen = 0;
1044 			exthdrlen = 0;
1045 			csummode = CHECKSUM_NONE;
1046 
1047 			/* only the initial fragment is time stamped */
1048 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
1049 			cork->tx_flags = 0;
1050 			skb_shinfo(skb)->tskey = tskey;
1051 			tskey = 0;
1052 			skb_zcopy_set(skb, uarg, &extra_uref);
1053 
1054 			if ((flags & MSG_CONFIRM) && !skb_prev)
1055 				skb_set_dst_pending_confirm(skb, 1);
1056 
1057 			/*
1058 			 * Put the packet on the pending queue.
1059 			 */
1060 			if (!skb->destructor) {
1061 				skb->destructor = sock_wfree;
1062 				skb->sk = sk;
1063 				wmem_alloc_delta += skb->truesize;
1064 			}
1065 			__skb_queue_tail(queue, skb);
1066 			continue;
1067 		}
1068 
1069 		if (copy > length)
1070 			copy = length;
1071 
1072 		if (!(rt->dst.dev->features&NETIF_F_SG) &&
1073 		    skb_tailroom(skb) >= copy) {
1074 			unsigned int off;
1075 
1076 			off = skb->len;
1077 			if (getfrag(from, skb_put(skb, copy),
1078 					offset, copy, off, skb) < 0) {
1079 				__skb_trim(skb, off);
1080 				err = -EFAULT;
1081 				goto error;
1082 			}
1083 		} else if (!uarg || !uarg->zerocopy) {
1084 			int i = skb_shinfo(skb)->nr_frags;
1085 
1086 			err = -ENOMEM;
1087 			if (!sk_page_frag_refill(sk, pfrag))
1088 				goto error;
1089 
1090 			if (!skb_can_coalesce(skb, i, pfrag->page,
1091 					      pfrag->offset)) {
1092 				err = -EMSGSIZE;
1093 				if (i == MAX_SKB_FRAGS)
1094 					goto error;
1095 
1096 				__skb_fill_page_desc(skb, i, pfrag->page,
1097 						     pfrag->offset, 0);
1098 				skb_shinfo(skb)->nr_frags = ++i;
1099 				get_page(pfrag->page);
1100 			}
1101 			copy = min_t(int, copy, pfrag->size - pfrag->offset);
1102 			if (getfrag(from,
1103 				    page_address(pfrag->page) + pfrag->offset,
1104 				    offset, copy, skb->len, skb) < 0)
1105 				goto error_efault;
1106 
1107 			pfrag->offset += copy;
1108 			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1109 			skb->len += copy;
1110 			skb->data_len += copy;
1111 			skb->truesize += copy;
1112 			wmem_alloc_delta += copy;
1113 		} else {
1114 			err = skb_zerocopy_iter_dgram(skb, from, copy);
1115 			if (err < 0)
1116 				goto error;
1117 		}
1118 		offset += copy;
1119 		length -= copy;
1120 	}
1121 
1122 	if (wmem_alloc_delta)
1123 		refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
1124 	return 0;
1125 
1126 error_efault:
1127 	err = -EFAULT;
1128 error:
1129 	if (uarg)
1130 		sock_zerocopy_put_abort(uarg, extra_uref);
1131 	cork->length -= length;
1132 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1133 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
1134 	return err;
1135 }
1136 
1137 static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1138 			 struct ipcm_cookie *ipc, struct rtable **rtp)
1139 {
1140 	struct ip_options_rcu *opt;
1141 	struct rtable *rt;
1142 
1143 	rt = *rtp;
1144 	if (unlikely(!rt))
1145 		return -EFAULT;
1146 
1147 	/*
1148 	 * setup for corking.
1149 	 */
1150 	opt = ipc->opt;
1151 	if (opt) {
1152 		if (!cork->opt) {
1153 			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1154 					    sk->sk_allocation);
1155 			if (unlikely(!cork->opt))
1156 				return -ENOBUFS;
1157 		}
1158 		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
1159 		cork->flags |= IPCORK_OPT;
1160 		cork->addr = ipc->addr;
1161 	}
1162 
1163 	/*
1164 	 * We steal reference to this route, caller should not release it
1165 	 */
1166 	*rtp = NULL;
1167 	cork->fragsize = ip_sk_use_pmtu(sk) ?
1168 			 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
1169 
1170 	cork->gso_size = ipc->gso_size;
1171 	cork->dst = &rt->dst;
1172 	cork->length = 0;
1173 	cork->ttl = ipc->ttl;
1174 	cork->tos = ipc->tos;
1175 	cork->priority = ipc->priority;
1176 	cork->transmit_time = ipc->sockc.transmit_time;
1177 	cork->tx_flags = 0;
1178 	sock_tx_timestamp(sk, ipc->sockc.tsflags, &cork->tx_flags);
1179 
1180 	return 0;
1181 }
1182 
1183 /*
1184  *	ip_append_data() and ip_append_page() can make one large IP datagram
1185  *	from many pieces of data. Each pieces will be holded on the socket
1186  *	until ip_push_pending_frames() is called. Each piece can be a page
1187  *	or non-page data.
1188  *
1189  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
1190  *	this interface potentially.
1191  *
1192  *	LATER: length must be adjusted by pad at tail, when it is required.
1193  */
1194 int ip_append_data(struct sock *sk, struct flowi4 *fl4,
1195 		   int getfrag(void *from, char *to, int offset, int len,
1196 			       int odd, struct sk_buff *skb),
1197 		   void *from, int length, int transhdrlen,
1198 		   struct ipcm_cookie *ipc, struct rtable **rtp,
1199 		   unsigned int flags)
1200 {
1201 	struct inet_sock *inet = inet_sk(sk);
1202 	int err;
1203 
1204 	if (flags&MSG_PROBE)
1205 		return 0;
1206 
1207 	if (skb_queue_empty(&sk->sk_write_queue)) {
1208 		err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
1209 		if (err)
1210 			return err;
1211 	} else {
1212 		transhdrlen = 0;
1213 	}
1214 
1215 	return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1216 				sk_page_frag(sk), getfrag,
1217 				from, length, transhdrlen, flags);
1218 }
1219 
1220 ssize_t	ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1221 		       int offset, size_t size, int flags)
1222 {
1223 	struct inet_sock *inet = inet_sk(sk);
1224 	struct sk_buff *skb;
1225 	struct rtable *rt;
1226 	struct ip_options *opt = NULL;
1227 	struct inet_cork *cork;
1228 	int hh_len;
1229 	int mtu;
1230 	int len;
1231 	int err;
1232 	unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
1233 
1234 	if (inet->hdrincl)
1235 		return -EPERM;
1236 
1237 	if (flags&MSG_PROBE)
1238 		return 0;
1239 
1240 	if (skb_queue_empty(&sk->sk_write_queue))
1241 		return -EINVAL;
1242 
1243 	cork = &inet->cork.base;
1244 	rt = (struct rtable *)cork->dst;
1245 	if (cork->flags & IPCORK_OPT)
1246 		opt = cork->opt;
1247 
1248 	if (!(rt->dst.dev->features&NETIF_F_SG))
1249 		return -EOPNOTSUPP;
1250 
1251 	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1252 	mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
1253 
1254 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1255 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
1256 	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
1257 
1258 	if (cork->length + size > maxnonfragsize - fragheaderlen) {
1259 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1260 			       mtu - (opt ? opt->optlen : 0));
1261 		return -EMSGSIZE;
1262 	}
1263 
1264 	skb = skb_peek_tail(&sk->sk_write_queue);
1265 	if (!skb)
1266 		return -EINVAL;
1267 
1268 	cork->length += size;
1269 
1270 	while (size > 0) {
1271 		/* Check if the remaining data fits into current packet. */
1272 		len = mtu - skb->len;
1273 		if (len < size)
1274 			len = maxfraglen - skb->len;
1275 
1276 		if (len <= 0) {
1277 			struct sk_buff *skb_prev;
1278 			int alloclen;
1279 
1280 			skb_prev = skb;
1281 			fraggap = skb_prev->len - maxfraglen;
1282 
1283 			alloclen = fragheaderlen + hh_len + fraggap + 15;
1284 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1285 			if (unlikely(!skb)) {
1286 				err = -ENOBUFS;
1287 				goto error;
1288 			}
1289 
1290 			/*
1291 			 *	Fill in the control structures
1292 			 */
1293 			skb->ip_summed = CHECKSUM_NONE;
1294 			skb->csum = 0;
1295 			skb_reserve(skb, hh_len);
1296 
1297 			/*
1298 			 *	Find where to start putting bytes.
1299 			 */
1300 			skb_put(skb, fragheaderlen + fraggap);
1301 			skb_reset_network_header(skb);
1302 			skb->transport_header = (skb->network_header +
1303 						 fragheaderlen);
1304 			if (fraggap) {
1305 				skb->csum = skb_copy_and_csum_bits(skb_prev,
1306 								   maxfraglen,
1307 						    skb_transport_header(skb),
1308 								   fraggap, 0);
1309 				skb_prev->csum = csum_sub(skb_prev->csum,
1310 							  skb->csum);
1311 				pskb_trim_unique(skb_prev, maxfraglen);
1312 			}
1313 
1314 			/*
1315 			 * Put the packet on the pending queue.
1316 			 */
1317 			__skb_queue_tail(&sk->sk_write_queue, skb);
1318 			continue;
1319 		}
1320 
1321 		if (len > size)
1322 			len = size;
1323 
1324 		if (skb_append_pagefrags(skb, page, offset, len)) {
1325 			err = -EMSGSIZE;
1326 			goto error;
1327 		}
1328 
1329 		if (skb->ip_summed == CHECKSUM_NONE) {
1330 			__wsum csum;
1331 			csum = csum_page(page, offset, len);
1332 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
1333 		}
1334 
1335 		skb->len += len;
1336 		skb->data_len += len;
1337 		skb->truesize += len;
1338 		refcount_add(len, &sk->sk_wmem_alloc);
1339 		offset += len;
1340 		size -= len;
1341 	}
1342 	return 0;
1343 
1344 error:
1345 	cork->length -= size;
1346 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1347 	return err;
1348 }
1349 
1350 static void ip_cork_release(struct inet_cork *cork)
1351 {
1352 	cork->flags &= ~IPCORK_OPT;
1353 	kfree(cork->opt);
1354 	cork->opt = NULL;
1355 	dst_release(cork->dst);
1356 	cork->dst = NULL;
1357 }
1358 
1359 /*
1360  *	Combined all pending IP fragments on the socket as one IP datagram
1361  *	and push them out.
1362  */
1363 struct sk_buff *__ip_make_skb(struct sock *sk,
1364 			      struct flowi4 *fl4,
1365 			      struct sk_buff_head *queue,
1366 			      struct inet_cork *cork)
1367 {
1368 	struct sk_buff *skb, *tmp_skb;
1369 	struct sk_buff **tail_skb;
1370 	struct inet_sock *inet = inet_sk(sk);
1371 	struct net *net = sock_net(sk);
1372 	struct ip_options *opt = NULL;
1373 	struct rtable *rt = (struct rtable *)cork->dst;
1374 	struct iphdr *iph;
1375 	__be16 df = 0;
1376 	__u8 ttl;
1377 
1378 	skb = __skb_dequeue(queue);
1379 	if (!skb)
1380 		goto out;
1381 	tail_skb = &(skb_shinfo(skb)->frag_list);
1382 
1383 	/* move skb->data to ip header from ext header */
1384 	if (skb->data < skb_network_header(skb))
1385 		__skb_pull(skb, skb_network_offset(skb));
1386 	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1387 		__skb_pull(tmp_skb, skb_network_header_len(skb));
1388 		*tail_skb = tmp_skb;
1389 		tail_skb = &(tmp_skb->next);
1390 		skb->len += tmp_skb->len;
1391 		skb->data_len += tmp_skb->len;
1392 		skb->truesize += tmp_skb->truesize;
1393 		tmp_skb->destructor = NULL;
1394 		tmp_skb->sk = NULL;
1395 	}
1396 
1397 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1398 	 * to fragment the frame generated here. No matter, what transforms
1399 	 * how transforms change size of the packet, it will come out.
1400 	 */
1401 	skb->ignore_df = ip_sk_ignore_df(sk);
1402 
1403 	/* DF bit is set when we want to see DF on outgoing frames.
1404 	 * If ignore_df is set too, we still allow to fragment this frame
1405 	 * locally. */
1406 	if (inet->pmtudisc == IP_PMTUDISC_DO ||
1407 	    inet->pmtudisc == IP_PMTUDISC_PROBE ||
1408 	    (skb->len <= dst_mtu(&rt->dst) &&
1409 	     ip_dont_fragment(sk, &rt->dst)))
1410 		df = htons(IP_DF);
1411 
1412 	if (cork->flags & IPCORK_OPT)
1413 		opt = cork->opt;
1414 
1415 	if (cork->ttl != 0)
1416 		ttl = cork->ttl;
1417 	else if (rt->rt_type == RTN_MULTICAST)
1418 		ttl = inet->mc_ttl;
1419 	else
1420 		ttl = ip_select_ttl(inet, &rt->dst);
1421 
1422 	iph = ip_hdr(skb);
1423 	iph->version = 4;
1424 	iph->ihl = 5;
1425 	iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1426 	iph->frag_off = df;
1427 	iph->ttl = ttl;
1428 	iph->protocol = sk->sk_protocol;
1429 	ip_copy_addrs(iph, fl4);
1430 	ip_select_ident(net, skb, sk);
1431 
1432 	if (opt) {
1433 		iph->ihl += opt->optlen>>2;
1434 		ip_options_build(skb, opt, cork->addr, rt, 0);
1435 	}
1436 
1437 	skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1438 	skb->mark = sk->sk_mark;
1439 	skb->tstamp = cork->transmit_time;
1440 	/*
1441 	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1442 	 * on dst refcount
1443 	 */
1444 	cork->dst = NULL;
1445 	skb_dst_set(skb, &rt->dst);
1446 
1447 	if (iph->protocol == IPPROTO_ICMP)
1448 		icmp_out_count(net, ((struct icmphdr *)
1449 			skb_transport_header(skb))->type);
1450 
1451 	ip_cork_release(cork);
1452 out:
1453 	return skb;
1454 }
1455 
1456 int ip_send_skb(struct net *net, struct sk_buff *skb)
1457 {
1458 	int err;
1459 
1460 	err = ip_local_out(net, skb->sk, skb);
1461 	if (err) {
1462 		if (err > 0)
1463 			err = net_xmit_errno(err);
1464 		if (err)
1465 			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
1466 	}
1467 
1468 	return err;
1469 }
1470 
1471 int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
1472 {
1473 	struct sk_buff *skb;
1474 
1475 	skb = ip_finish_skb(sk, fl4);
1476 	if (!skb)
1477 		return 0;
1478 
1479 	/* Netfilter gets whole the not fragmented skb. */
1480 	return ip_send_skb(sock_net(sk), skb);
1481 }
1482 
1483 /*
1484  *	Throw away all pending data on the socket.
1485  */
1486 static void __ip_flush_pending_frames(struct sock *sk,
1487 				      struct sk_buff_head *queue,
1488 				      struct inet_cork *cork)
1489 {
1490 	struct sk_buff *skb;
1491 
1492 	while ((skb = __skb_dequeue_tail(queue)) != NULL)
1493 		kfree_skb(skb);
1494 
1495 	ip_cork_release(cork);
1496 }
1497 
1498 void ip_flush_pending_frames(struct sock *sk)
1499 {
1500 	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
1501 }
1502 
1503 struct sk_buff *ip_make_skb(struct sock *sk,
1504 			    struct flowi4 *fl4,
1505 			    int getfrag(void *from, char *to, int offset,
1506 					int len, int odd, struct sk_buff *skb),
1507 			    void *from, int length, int transhdrlen,
1508 			    struct ipcm_cookie *ipc, struct rtable **rtp,
1509 			    struct inet_cork *cork, unsigned int flags)
1510 {
1511 	struct sk_buff_head queue;
1512 	int err;
1513 
1514 	if (flags & MSG_PROBE)
1515 		return NULL;
1516 
1517 	__skb_queue_head_init(&queue);
1518 
1519 	cork->flags = 0;
1520 	cork->addr = 0;
1521 	cork->opt = NULL;
1522 	err = ip_setup_cork(sk, cork, ipc, rtp);
1523 	if (err)
1524 		return ERR_PTR(err);
1525 
1526 	err = __ip_append_data(sk, fl4, &queue, cork,
1527 			       &current->task_frag, getfrag,
1528 			       from, length, transhdrlen, flags);
1529 	if (err) {
1530 		__ip_flush_pending_frames(sk, &queue, cork);
1531 		return ERR_PTR(err);
1532 	}
1533 
1534 	return __ip_make_skb(sk, fl4, &queue, cork);
1535 }
1536 
1537 /*
1538  *	Fetch data from kernel space and fill in checksum if needed.
1539  */
1540 static int ip_reply_glue_bits(void *dptr, char *to, int offset,
1541 			      int len, int odd, struct sk_buff *skb)
1542 {
1543 	__wsum csum;
1544 
1545 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
1546 	skb->csum = csum_block_add(skb->csum, csum, odd);
1547 	return 0;
1548 }
1549 
1550 /*
1551  *	Generic function to send a packet as reply to another packet.
1552  *	Used to send some TCP resets/acks so far.
1553  */
1554 void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
1555 			   const struct ip_options *sopt,
1556 			   __be32 daddr, __be32 saddr,
1557 			   const struct ip_reply_arg *arg,
1558 			   unsigned int len)
1559 {
1560 	struct ip_options_data replyopts;
1561 	struct ipcm_cookie ipc;
1562 	struct flowi4 fl4;
1563 	struct rtable *rt = skb_rtable(skb);
1564 	struct net *net = sock_net(sk);
1565 	struct sk_buff *nskb;
1566 	int err;
1567 	int oif;
1568 
1569 	if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt))
1570 		return;
1571 
1572 	ipcm_init(&ipc);
1573 	ipc.addr = daddr;
1574 
1575 	if (replyopts.opt.opt.optlen) {
1576 		ipc.opt = &replyopts.opt;
1577 
1578 		if (replyopts.opt.opt.srr)
1579 			daddr = replyopts.opt.opt.faddr;
1580 	}
1581 
1582 	oif = arg->bound_dev_if;
1583 	if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
1584 		oif = skb->skb_iif;
1585 
1586 	flowi4_init_output(&fl4, oif,
1587 			   IP4_REPLY_MARK(net, skb->mark) ?: sk->sk_mark,
1588 			   RT_TOS(arg->tos),
1589 			   RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1590 			   ip_reply_arg_flowi_flags(arg),
1591 			   daddr, saddr,
1592 			   tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1593 			   arg->uid);
1594 	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1595 	rt = ip_route_output_key(net, &fl4);
1596 	if (IS_ERR(rt))
1597 		return;
1598 
1599 	inet_sk(sk)->tos = arg->tos;
1600 
1601 	sk->sk_priority = skb->priority;
1602 	sk->sk_protocol = ip_hdr(skb)->protocol;
1603 	sk->sk_bound_dev_if = arg->bound_dev_if;
1604 	sk->sk_sndbuf = sysctl_wmem_default;
1605 	sk->sk_mark = fl4.flowi4_mark;
1606 	err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
1607 			     len, 0, &ipc, &rt, MSG_DONTWAIT);
1608 	if (unlikely(err)) {
1609 		ip_flush_pending_frames(sk);
1610 		goto out;
1611 	}
1612 
1613 	nskb = skb_peek(&sk->sk_write_queue);
1614 	if (nskb) {
1615 		if (arg->csumoffset >= 0)
1616 			*((__sum16 *)skb_transport_header(nskb) +
1617 			  arg->csumoffset) = csum_fold(csum_add(nskb->csum,
1618 								arg->csum));
1619 		nskb->ip_summed = CHECKSUM_NONE;
1620 		ip_push_pending_frames(sk, &fl4);
1621 	}
1622 out:
1623 	ip_rt_put(rt);
1624 }
1625 
1626 void __init ip_init(void)
1627 {
1628 	ip_rt_init();
1629 	inet_initpeers();
1630 
1631 #if defined(CONFIG_IP_MULTICAST)
1632 	igmp_mc_init();
1633 #endif
1634 }
1635