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