Blame view

net/ipv4/ip_output.c 39.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
  /*
   * INET		An implementation of the TCP/IP protocol suite for the LINUX
   *		operating system.  INET is implemented using the  BSD Socket
   *		interface as the means of communication with the user level.
   *
   *		The Internet Protocol (IP) output module.
   *
02c30a84e   Jesper Juhl   [PATCH] update Ro...
8
   * Authors:	Ross Biro
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
   *		Donald Becker, <becker@super.org>
   *		Alan Cox, <Alan.Cox@linux.org>
   *		Richard Underwood
   *		Stefan Becker, <stefanb@yello.ping.de>
   *		Jorge Cwik, <jorge@laser.satlink.net>
   *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
   *		Hirokazu Takahashi, <taka@valinux.co.jp>
   *
   *	See ip_input.c for original log
   *
   *	Fixes:
   *		Alan Cox	:	Missing nonblock feature in ip_build_xmit.
   *		Mike Kilburn	:	htons() missing in ip_build_xmit.
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
23
   *		Bradford Johnson:	Fix faulty handling of some frames when
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
27
28
29
30
31
32
33
   *					no route is found.
   *		Alexander Demenshin:	Missing sk/skb free in ip_queue_xmit
   *					(in case if packet not accepted by
   *					output firewall rules)
   *		Mike McLagan	:	Routing by source
   *		Alexey Kuznetsov:	use new route cache
   *		Andi Kleen:		Fix broken PMTU recovery and remove
   *					some redundant tests.
   *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
   *		Andi Kleen	: 	Replace ip_reply with ip_send_reply.
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
34
35
36
   *		Andi Kleen	:	Split fast and slow ip_build_xmit path
   *					for decreased register pressure on x86
   *					and more readibility.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
41
42
43
44
45
   *		Marc Boucher	:	When call_out_firewall returns FW_QUEUE,
   *					silently drop skb instead of failing with -EPERM.
   *		Detlev Wengorz	:	Copy protocol for fragments.
   *		Hirokazu Takahashi:	HW checksumming for outgoing UDP
   *					datagrams.
   *		Hirokazu Takahashi:	sendfile() on UDP works now.
   */
  
  #include <asm/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
  #include <linux/mm.h>
  #include <linux/string.h>
  #include <linux/errno.h>
a1f8e7f7f   Al Viro   [PATCH] severing ...
52
  #include <linux/highmem.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
53
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
  #include <linux/socket.h>
  #include <linux/sockios.h>
  #include <linux/in.h>
  #include <linux/inet.h>
  #include <linux/netdevice.h>
  #include <linux/etherdevice.h>
  #include <linux/proc_fs.h>
  #include <linux/stat.h>
  #include <linux/init.h>
  
  #include <net/snmp.h>
  #include <net/ip.h>
  #include <net/protocol.h>
  #include <net/route.h>
cfacb0577   Patrick McHardy   [IPV4]: ip_output...
69
  #include <net/xfrm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
73
  #include <linux/skbuff.h>
  #include <net/sock.h>
  #include <net/arp.h>
  #include <net/icmp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
  #include <net/checksum.h>
  #include <net/inetpeer.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
79
80
  #include <linux/igmp.h>
  #include <linux/netfilter_ipv4.h>
  #include <linux/netfilter_bridge.h>
  #include <linux/mroute.h>
  #include <linux/netlink.h>
6cbb0df78   Arnaldo Carvalho de Melo   [SOCK]: Introduce...
81
  #include <linux/tcp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82

ab32ea5d8   Brian Haley   [NET/IPV4/IPV6]: ...
83
  int sysctl_ip_default_ttl __read_mostly = IPDEFTTL;
323e126f0   David S. Miller   ipv4: Don't pre-s...
84
  EXPORT_SYMBOL(sysctl_ip_default_ttl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  
  /* Generate a checksum for an outgoing IP datagram. */
2fbd96797   Denis Efremov   ipv4: ip_output: ...
87
  void ip_send_check(struct iphdr *iph)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
  {
  	iph->check = 0;
  	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  }
4bc2f18ba   Eric Dumazet   net/ipv4: EXPORT_...
92
  EXPORT_SYMBOL(ip_send_check);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93

c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
94
95
96
97
98
99
  int __ip_local_out(struct sk_buff *skb)
  {
  	struct iphdr *iph = ip_hdr(skb);
  
  	iph->tot_len = htons(skb->len);
  	ip_send_check(iph);
9bbc768aa   Jan Engelhardt   netfilter: ipv4: ...
100
101
  	return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
  		       skb_dst(skb)->dev, dst_output);
c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
102
  }
aad88724c   Eric Dumazet   ipv4: add a sock ...
103
  int ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
104
105
106
107
108
  {
  	int err;
  
  	err = __ip_local_out(skb);
  	if (likely(err == 1))
aad88724c   Eric Dumazet   ipv4: add a sock ...
109
  		err = dst_output_sk(sk, skb);
c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
110
111
112
  
  	return err;
  }
aad88724c   Eric Dumazet   ipv4: add a sock ...
113
  EXPORT_SYMBOL_GPL(ip_local_out_sk);
c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
114

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
119
  static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
  {
  	int ttl = inet->uc_ttl;
  
  	if (ttl < 0)
323e126f0   David S. Miller   ipv4: Don't pre-s...
120
  		ttl = ip4_dst_hoplimit(dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
  	return ttl;
  }
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
123
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
   *		Add an ip header to a skbuff and send it out.
   *
   */
  int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
f6d8bd051   Eric Dumazet   inet: add RCU pro...
128
  			  __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
  {
  	struct inet_sock *inet = inet_sk(sk);
511c3f92a   Eric Dumazet   net: skb->rtable ...
131
  	struct rtable *rt = skb_rtable(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
134
  	struct iphdr *iph;
  
  	/* Build the IP header. */
f6d8bd051   Eric Dumazet   inet: add RCU pro...
135
  	skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
8856dfa3e   Arnaldo Carvalho de Melo   [SK_BUFF]: Use sk...
136
  	skb_reset_network_header(skb);
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
137
  	iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
  	iph->version  = 4;
  	iph->ihl      = 5;
  	iph->tos      = inet->tos;
d8d1f30b9   Changli Gao   net-next: remove ...
141
  	if (ip_dont_fragment(sk, &rt->dst))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
143
144
  		iph->frag_off = htons(IP_DF);
  	else
  		iph->frag_off = 0;
d8d1f30b9   Changli Gao   net-next: remove ...
145
  	iph->ttl      = ip_select_ttl(inet, &rt->dst);
dd927a269   David S. Miller   ipv4: In ip_build...
146
147
  	iph->daddr    = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
  	iph->saddr    = saddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
  	iph->protocol = sk->sk_protocol;
73f156a6e   Eric Dumazet   inetpeer: get rid...
149
  	ip_select_ident(skb, sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150

f6d8bd051   Eric Dumazet   inet: add RCU pro...
151
152
153
  	if (opt && opt->opt.optlen) {
  		iph->ihl += opt->opt.optlen>>2;
  		ip_options_build(skb, &opt->opt, daddr, rt, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  
  	skb->priority = sk->sk_priority;
4a19ec580   Laszlo Attila Toth   [NET]: Introducin...
157
  	skb->mark = sk->sk_mark;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
  
  	/* Send it out. */
c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
160
  	return ip_local_out(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  }
d8c97a945   Arnaldo Carvalho de Melo   [NET]: Export sym...
162
  EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
  static inline int ip_finish_output2(struct sk_buff *skb)
  {
adf30907d   Eric Dumazet   net: skb->dst acc...
165
  	struct dst_entry *dst = skb_dst(skb);
80787ebc2   Mitsuru Chinen   [IPV4] SNMP: Supp...
166
  	struct rtable *rt = (struct rtable *)dst;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
  	struct net_device *dev = dst->dev;
c2636b4d9   Chuck Lever   [NET]: Treat the ...
168
  	unsigned int hh_len = LL_RESERVED_SPACE(dev);
f6b72b621   David S. Miller   net: Embed hh_cac...
169
  	struct neighbour *neigh;
a263b3093   David S. Miller   ipv4: Make neigh ...
170
  	u32 nexthop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171

edf391ff1   Neil Horman   snmp: add missing...
172
173
174
175
  	if (rt->rt_type == RTN_MULTICAST) {
  		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len);
  	} else if (rt->rt_type == RTN_BROADCAST)
  		IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len);
80787ebc2   Mitsuru Chinen   [IPV4] SNMP: Supp...
176

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  	/* Be paranoid, rather than too clever. */
3b04ddde0   Stephen Hemminger   [NET]: Move hardw...
178
  	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
182
183
184
185
186
187
  		struct sk_buff *skb2;
  
  		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  		if (skb2 == NULL) {
  			kfree_skb(skb);
  			return -ENOMEM;
  		}
  		if (skb->sk)
  			skb_set_owner_w(skb2, skb->sk);
5d0ba55b6   Eric Dumazet   net: use consume_...
188
  		consume_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
  		skb = skb2;
  	}
a263b3093   David S. Miller   ipv4: Make neigh ...
191
  	rcu_read_lock_bh();
155e8336c   Julian Anastasov   ipv4: introduce r...
192
  	nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
a263b3093   David S. Miller   ipv4: Make neigh ...
193
194
195
  	neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  	if (unlikely(!neigh))
  		neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
9871f1ad6   Vasiliy Kulikov   ip: fix error han...
196
  	if (!IS_ERR(neigh)) {
5110effee   David S. Miller   net: Do delayed n...
197
  		int res = dst_neigh_output(dst, neigh, skb);
f2c31e32b   Eric Dumazet   net: fix NULL der...
198

a263b3093   David S. Miller   ipv4: Make neigh ...
199
  		rcu_read_unlock_bh();
f2c31e32b   Eric Dumazet   net: fix NULL der...
200
201
  		return res;
  	}
a263b3093   David S. Miller   ipv4: Make neigh ...
202
  	rcu_read_unlock_bh();
05e3aa094   David S. Miller   net: Create and u...
203

e87cc4728   Joe Perches   net: Convert net_...
204
205
206
  	net_dbg_ratelimited("%s: No header cache and no neighbour!
  ",
  			    __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
  	kfree_skb(skb);
  	return -EINVAL;
  }
c7ba65d7b   Florian Westphal   net: ip: push gso...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  static int ip_finish_output_gso(struct sk_buff *skb)
  {
  	netdev_features_t features;
  	struct sk_buff *segs;
  	int ret = 0;
  
  	/* common case: locally created skb or seglen is <= mtu */
  	if (((IPCB(skb)->flags & IPSKB_FORWARDED) == 0) ||
  	      skb_gso_network_seglen(skb) <= ip_skb_dst_mtu(skb))
  		return ip_finish_output2(skb);
  
  	/* Slowpath -  GSO segment length is exceeding the dst MTU.
  	 *
  	 * This can happen in two cases:
  	 * 1) TCP GRO packet, DF bit not set
  	 * 2) skb arrived via virtio-net, we thus get TSO/GSO skbs directly
  	 * from host network stack.
  	 */
  	features = netif_skb_features(skb);
  	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
330966e50   Florian Westphal   net: make skb_gso...
230
  	if (IS_ERR_OR_NULL(segs)) {
c7ba65d7b   Florian Westphal   net: ip: push gso...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  		kfree_skb(skb);
  		return -ENOMEM;
  	}
  
  	consume_skb(skb);
  
  	do {
  		struct sk_buff *nskb = segs->next;
  		int err;
  
  		segs->next = NULL;
  		err = ip_fragment(segs, ip_finish_output2);
  
  		if (err && ret == 0)
  			ret = err;
  		segs = nskb;
  	} while (segs);
  
  	return ret;
  }
861d04860   Patrick McHardy   [IPV4]: Uninline ...
251
  static int ip_finish_output(struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
  {
5c901daae   Patrick McHardy   [NETFILTER]: Redo...
253
254
  #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
  	/* Policy lookup after SNAT yielded a new policy */
adf30907d   Eric Dumazet   net: skb->dst acc...
255
  	if (skb_dst(skb)->xfrm != NULL) {
48d5cad87   Patrick McHardy   [XFRM]: Fix SNAT-...
256
257
258
  		IPCB(skb)->flags |= IPSKB_REROUTED;
  		return dst_output(skb);
  	}
5c901daae   Patrick McHardy   [NETFILTER]: Redo...
259
  #endif
c7ba65d7b   Florian Westphal   net: ip: push gso...
260
261
262
263
  	if (skb_is_gso(skb))
  		return ip_finish_output_gso(skb);
  
  	if (skb->len > ip_skb_dst_mtu(skb))
1bd9bef6f   Patrick McHardy   [NETFILTER]: Call...
264
  		return ip_fragment(skb, ip_finish_output2);
c7ba65d7b   Florian Westphal   net: ip: push gso...
265
266
  
  	return ip_finish_output2(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  }
aad88724c   Eric Dumazet   ipv4: add a sock ...
268
  int ip_mc_output(struct sock *sk, struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
  {
511c3f92a   Eric Dumazet   net: skb->rtable ...
270
  	struct rtable *rt = skb_rtable(skb);
d8d1f30b9   Changli Gao   net-next: remove ...
271
  	struct net_device *dev = rt->dst.dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
  
  	/*
  	 *	If the indicated interface is up and running, send the packet.
  	 */
edf391ff1   Neil Horman   snmp: add missing...
276
  	IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
278
279
280
281
282
283
284
285
  
  	skb->dev = dev;
  	skb->protocol = htons(ETH_P_IP);
  
  	/*
  	 *	Multicasts are looped back for other local users
  	 */
  
  	if (rt->rt_flags&RTCF_MULTICAST) {
7ad6848c7   Octavian Purdila   ip: fix mc_loop c...
286
  		if (sk_mc_loop(sk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
293
294
295
  #ifdef CONFIG_IP_MROUTE
  		/* Small optimization: do not loopback not local frames,
  		   which returned after forwarding; they will be  dropped
  		   by ip_mr_input in any case.
  		   Note, that local frames are looped back to be delivered
  		   to local recipients.
  
  		   This check is duplicated in ip_mr_input at the moment.
  		 */
9d4fb27db   Joe Perches   net/ipv4: Move &&...
296
297
298
  		    &&
  		    ((rt->rt_flags & RTCF_LOCAL) ||
  		     !(IPCB(skb)->flags & IPSKB_FORWARDED))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
  #endif
9d4fb27db   Joe Perches   net/ipv4: Move &&...
300
  		   ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
  			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  			if (newskb)
9bbc768aa   Jan Engelhardt   netfilter: ipv4: ...
303
304
  				NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  					newskb, NULL, newskb->dev,
95603e229   Michel Machado   net-next: add dev...
305
  					dev_loopback_xmit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
  		}
  
  		/* Multicasts with ttl 0 must not go beyond the host */
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
309
  		if (ip_hdr(skb)->ttl == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
311
312
313
314
315
316
317
  			kfree_skb(skb);
  			return 0;
  		}
  	}
  
  	if (rt->rt_flags&RTCF_BROADCAST) {
  		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  		if (newskb)
9bbc768aa   Jan Engelhardt   netfilter: ipv4: ...
318
  			NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, newskb,
95603e229   Michel Machado   net-next: add dev...
319
  				NULL, newskb->dev, dev_loopback_xmit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
  	}
9bbc768aa   Jan Engelhardt   netfilter: ipv4: ...
321
322
  	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL,
  			    skb->dev, ip_finish_output,
48d5cad87   Patrick McHardy   [XFRM]: Fix SNAT-...
323
  			    !(IPCB(skb)->flags & IPSKB_REROUTED));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  }
aad88724c   Eric Dumazet   ipv4: add a sock ...
325
  int ip_output(struct sock *sk, struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  {
adf30907d   Eric Dumazet   net: skb->dst acc...
327
  	struct net_device *dev = skb_dst(skb)->dev;
1bd9bef6f   Patrick McHardy   [NETFILTER]: Call...
328

edf391ff1   Neil Horman   snmp: add missing...
329
  	IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330

1bd9bef6f   Patrick McHardy   [NETFILTER]: Call...
331
332
  	skb->dev = dev;
  	skb->protocol = htons(ETH_P_IP);
9bbc768aa   Jan Engelhardt   netfilter: ipv4: ...
333
  	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, dev,
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
334
  			    ip_finish_output,
48d5cad87   Patrick McHardy   [XFRM]: Fix SNAT-...
335
  			    !(IPCB(skb)->flags & IPSKB_REROUTED));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
  }
84f9307c5   Eric Dumazet   ipv4: use a 64bit...
337
338
339
340
341
342
343
344
345
346
347
348
349
  /*
   * copy saddr and daddr, possibly using 64bit load/stores
   * Equivalent to :
   *   iph->saddr = fl4->saddr;
   *   iph->daddr = fl4->daddr;
   */
  static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
  {
  	BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
  		     offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
  	memcpy(&iph->saddr, &fl4->saddr,
  	       sizeof(fl4->saddr) + sizeof(fl4->daddr));
  }
b0270e910   Eric Dumazet   ipv4: add a sock ...
350
351
  /* Note: skb->sk can be different from sk, in case of tunnels */
  int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  	struct inet_sock *inet = inet_sk(sk);
f6d8bd051   Eric Dumazet   inet: add RCU pro...
354
  	struct ip_options_rcu *inet_opt;
b57ae01a8   David S. Miller   ipv4: Use cork fl...
355
  	struct flowi4 *fl4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
  	struct rtable *rt;
  	struct iphdr *iph;
ab6e3feba   Eric Dumazet   net: No dst refco...
358
  	int res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
362
  
  	/* Skip all of this if the packet is already routed,
  	 * f.e. by something like SCTP.
  	 */
ab6e3feba   Eric Dumazet   net: No dst refco...
363
  	rcu_read_lock();
f6d8bd051   Eric Dumazet   inet: add RCU pro...
364
  	inet_opt = rcu_dereference(inet->inet_opt);
ea4fc0d61   David S. Miller   ipv4: Don't use r...
365
  	fl4 = &fl->u.ip4;
511c3f92a   Eric Dumazet   net: skb->rtable ...
366
  	rt = skb_rtable(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
370
371
372
  	if (rt != NULL)
  		goto packet_routed;
  
  	/* Make sure we can route this packet. */
  	rt = (struct rtable *)__sk_dst_check(sk, 0);
  	if (rt == NULL) {
3ca3c68e7   Al Viro   [IPV4]: struct ip...
373
  		__be32 daddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
375
  
  		/* Use correct destination address if we have options. */
c720c7e83   Eric Dumazet   inet: rename some...
376
  		daddr = inet->inet_daddr;
f6d8bd051   Eric Dumazet   inet: add RCU pro...
377
378
  		if (inet_opt && inet_opt->opt.srr)
  			daddr = inet_opt->opt.faddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379

78fbfd8a6   David S. Miller   ipv4: Create and ...
380
381
382
383
  		/* If this fails, retransmit mechanism of transport layer will
  		 * keep trying until route appears or the connection times
  		 * itself out.
  		 */
b57ae01a8   David S. Miller   ipv4: Use cork fl...
384
  		rt = ip_route_output_ports(sock_net(sk), fl4, sk,
78fbfd8a6   David S. Miller   ipv4: Create and ...
385
386
387
388
389
390
391
392
  					   daddr, inet->inet_saddr,
  					   inet->inet_dport,
  					   inet->inet_sport,
  					   sk->sk_protocol,
  					   RT_CONN_FLAGS(sk),
  					   sk->sk_bound_dev_if);
  		if (IS_ERR(rt))
  			goto no_route;
d8d1f30b9   Changli Gao   net-next: remove ...
393
  		sk_setup_caps(sk, &rt->dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  	}
d8d1f30b9   Changli Gao   net-next: remove ...
395
  	skb_dst_set_noref(skb, &rt->dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
  
  packet_routed:
155e8336c   Julian Anastasov   ipv4: introduce r...
398
  	if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
400
401
  		goto no_route;
  
  	/* OK, we know where to send it, allocate and build IP header. */
f6d8bd051   Eric Dumazet   inet: add RCU pro...
402
  	skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
8856dfa3e   Arnaldo Carvalho de Melo   [SK_BUFF]: Use sk...
403
  	skb_reset_network_header(skb);
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
404
  	iph = ip_hdr(skb);
714e85be3   Al Viro   [IPV6]: Assorted ...
405
  	*((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
60ff74673   WANG Cong   net: rename local...
406
  	if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
  		iph->frag_off = htons(IP_DF);
  	else
  		iph->frag_off = 0;
d8d1f30b9   Changli Gao   net-next: remove ...
410
  	iph->ttl      = ip_select_ttl(inet, &rt->dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
  	iph->protocol = sk->sk_protocol;
84f9307c5   Eric Dumazet   ipv4: use a 64bit...
412
  	ip_copy_addrs(iph, fl4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
  	/* Transport layer set skb->h.foo itself. */
f6d8bd051   Eric Dumazet   inet: add RCU pro...
414
415
416
  	if (inet_opt && inet_opt->opt.optlen) {
  		iph->ihl += inet_opt->opt.optlen >> 2;
  		ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
  	}
73f156a6e   Eric Dumazet   inetpeer: get rid...
418
  	ip_select_ident_segs(skb, sk, skb_shinfo(skb)->gso_segs ?: 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419

b0270e910   Eric Dumazet   ipv4: add a sock ...
420
  	/* TODO : should we use skb->sk here instead of sk ? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
  	skb->priority = sk->sk_priority;
4a19ec580   Laszlo Attila Toth   [NET]: Introducin...
422
  	skb->mark = sk->sk_mark;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423

ab6e3feba   Eric Dumazet   net: No dst refco...
424
425
426
  	res = ip_local_out(skb);
  	rcu_read_unlock();
  	return res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
  
  no_route:
ab6e3feba   Eric Dumazet   net: No dst refco...
429
  	rcu_read_unlock();
5e38e2704   Pavel Emelyanov   mib: add net to I...
430
  	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
432
433
  	kfree_skb(skb);
  	return -EHOSTUNREACH;
  }
4bc2f18ba   Eric Dumazet   net/ipv4: EXPORT_...
434
  EXPORT_SYMBOL(ip_queue_xmit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
437
438
439
440
441
  
  
  static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  {
  	to->pkt_type = from->pkt_type;
  	to->priority = from->priority;
  	to->protocol = from->protocol;
adf30907d   Eric Dumazet   net: skb->dst acc...
442
  	skb_dst_drop(to);
fe76cda30   Eric Dumazet   ipv4: use skb_dst...
443
  	skb_dst_copy(to, from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
  	to->dev = from->dev;
82e91ffef   Thomas Graf   [NET]: Turn nfmar...
445
  	to->mark = from->mark;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
447
448
449
450
451
452
  
  	/* Copy the flags to each fragment. */
  	IPCB(to)->flags = IPCB(from)->flags;
  
  #ifdef CONFIG_NET_SCHED
  	to->tc_index = from->tc_index;
  #endif
e7ac05f34   Yasuyuki Kozakai   [NETFILTER]: nf_c...
453
  	nf_copy(to, from);
c98d80edc   Julian Anastasov   [SK_BUFF]: ipvs_p...
454
455
456
  #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  	to->ipvs_property = from->ipvs_property;
  #endif
984bc16cc   James Morris   [SECMARK]: Add se...
457
  	skb_copy_secmark(to, from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
461
462
463
464
465
  }
  
  /*
   *	This IP datagram is too large to be sent in one piece.  Break it up into
   *	smaller pieces (each of size equal to IP header plus
   *	a block of the data of the original IP data part) that will yet fit in a
   *	single device frame, and queue such a frame for sending.
   */
d9319100c   Jianjun Kong   net: clean up net...
466
  int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
  {
  	struct iphdr *iph;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
470
471
  	int ptr;
  	struct net_device *dev;
  	struct sk_buff *skb2;
c893b8066   Changli Gao   ip_fragment: fix ...
472
  	unsigned int mtu, hlen, left, len, ll_rs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
  	int offset;
76ab608d8   Alexey Dobriyan   [NET]: Endian-ann...
474
  	__be16 not_last_frag;
511c3f92a   Eric Dumazet   net: skb->rtable ...
475
  	struct rtable *rt = skb_rtable(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  	int err = 0;
d8d1f30b9   Changli Gao   net-next: remove ...
477
  	dev = rt->dst.dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
481
  
  	/*
  	 *	Point into the IP datagram header.
  	 */
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
482
  	iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483

69647ce46   Hannes Frederic Sowa   ipv4: use ip_skb_...
484
  	mtu = ip_skb_dst_mtu(skb);
60ff74673   WANG Cong   net: rename local...
485
  	if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
5f2d04f1f   Patrick McHardy   ipv4: fix path MT...
486
  		     (IPCB(skb)->frag_max_size &&
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
487
  		      IPCB(skb)->frag_max_size > mtu))) {
5e38e2704   Pavel Emelyanov   mib: add net to I...
488
  		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
  		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
490
  			  htonl(mtu));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
496
497
498
499
  		kfree_skb(skb);
  		return -EMSGSIZE;
  	}
  
  	/*
  	 *	Setup starting values.
  	 */
  
  	hlen = iph->ihl * 4;
f87c10a8a   Hannes Frederic Sowa   ipv4: introduce i...
500
  	mtu = mtu - hlen;	/* Size of data space */
1109a90c0   Pablo Neira Ayuso   netfilter: use IS...
501
  #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
6c79bf0f2   Bart De Schuymer   netfilter: bridge...
502
503
504
  	if (skb->nf_bridge)
  		mtu -= nf_bridge_mtu_reduction(skb);
  #endif
89cee8b1c   Herbert Xu   [IPV4]: Safer rea...
505
  	IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
508
509
510
511
512
513
  
  	/* When frag_list is given, use it. First, check its validity:
  	 * some transformers could create wrong frag_list or break existing
  	 * one, it is not prohibited. In this case fall back to copying.
  	 *
  	 * LATER: this step can be merged to real generation of fragments,
  	 * we can switch to copy when see the first bad fragment.
  	 */
21dc33015   David S. Miller   net: Rename skb_h...
514
  	if (skb_has_frag_list(skb)) {
3d13008e7   Eric Dumazet   ip: fix truesize ...
515
  		struct sk_buff *frag, *frag2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
519
  		int first_len = skb_pagelen(skb);
  
  		if (first_len - hlen > mtu ||
  		    ((first_len - hlen) & 7) ||
56f8a75c1   Paul Gortmaker   ip: introduce ip_...
520
  		    ip_is_fragment(iph) ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
522
  		    skb_cloned(skb))
  			goto slow_path;
d7fcf1a5c   David S. Miller   ipv4: Use frag li...
523
  		skb_walk_frags(skb, frag) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
525
526
527
  			/* Correct geometry. */
  			if (frag->len > mtu ||
  			    ((frag->len & 7) && frag->next) ||
  			    skb_headroom(frag) < hlen)
3d13008e7   Eric Dumazet   ip: fix truesize ...
528
  				goto slow_path_clean;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
  
  			/* Partially cloned skb? */
  			if (skb_shared(frag))
3d13008e7   Eric Dumazet   ip: fix truesize ...
532
  				goto slow_path_clean;
2fdba6b08   Herbert Xu   [IPV4/IPV6] Ensur...
533
534
535
  
  			BUG_ON(frag->sk);
  			if (skb->sk) {
2fdba6b08   Herbert Xu   [IPV4/IPV6] Ensur...
536
537
  				frag->sk = skb->sk;
  				frag->destructor = sock_wfree;
2fdba6b08   Herbert Xu   [IPV4/IPV6] Ensur...
538
  			}
3d13008e7   Eric Dumazet   ip: fix truesize ...
539
  			skb->truesize -= frag->truesize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
541
542
543
544
545
546
  		}
  
  		/* Everything is OK. Generate! */
  
  		err = 0;
  		offset = 0;
  		frag = skb_shinfo(skb)->frag_list;
d7fcf1a5c   David S. Miller   ipv4: Use frag li...
547
  		skb_frag_list_init(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
549
550
551
552
553
554
555
556
557
558
  		skb->data_len = first_len - skb_headlen(skb);
  		skb->len = first_len;
  		iph->tot_len = htons(first_len);
  		iph->frag_off = htons(IP_MF);
  		ip_send_check(iph);
  
  		for (;;) {
  			/* Prepare header of the next frame,
  			 * before previous one went down. */
  			if (frag) {
  				frag->ip_summed = CHECKSUM_NONE;
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
559
  				skb_reset_transport_header(frag);
e2d1bca7e   Arnaldo Carvalho de Melo   [SK_BUFF]: Use sk...
560
561
  				__skb_push(frag, hlen);
  				skb_reset_network_header(frag);
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
562
  				memcpy(skb_network_header(frag), iph, hlen);
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
563
  				iph = ip_hdr(frag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
566
567
568
569
570
571
572
573
574
575
576
  				iph->tot_len = htons(frag->len);
  				ip_copy_metadata(frag, skb);
  				if (offset == 0)
  					ip_options_fragment(frag);
  				offset += skb->len - hlen;
  				iph->frag_off = htons(offset>>3);
  				if (frag->next != NULL)
  					iph->frag_off |= htons(IP_MF);
  				/* Ready, complete checksum */
  				ip_send_check(iph);
  			}
  
  			err = output(skb);
dafee4908   Wei Dong   [IPV6]: SNMPv2 "i...
577
  			if (!err)
5e38e2704   Pavel Emelyanov   mib: add net to I...
578
  				IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
579
580
581
582
583
584
585
586
587
  			if (err || !frag)
  				break;
  
  			skb = frag;
  			frag = skb->next;
  			skb->next = NULL;
  		}
  
  		if (err == 0) {
5e38e2704   Pavel Emelyanov   mib: add net to I...
588
  			IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
590
591
592
593
594
595
596
  			return 0;
  		}
  
  		while (frag) {
  			skb = frag->next;
  			kfree_skb(frag);
  			frag = skb;
  		}
5e38e2704   Pavel Emelyanov   mib: add net to I...
597
  		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
598
  		return err;
3d13008e7   Eric Dumazet   ip: fix truesize ...
599
600
601
602
603
604
605
606
607
  
  slow_path_clean:
  		skb_walk_frags(skb, frag2) {
  			if (frag2 == frag)
  				break;
  			frag2->sk = NULL;
  			frag2->destructor = NULL;
  			skb->truesize += frag2->truesize;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
609
610
  	}
  
  slow_path:
fc70fb640   Alexander Duyck   net: Handle encap...
611
612
613
  	/* for offloaded checksums cleanup checksum before fragmentation */
  	if ((skb->ip_summed == CHECKSUM_PARTIAL) && skb_checksum_help(skb))
  		goto fail;
c9af6db4c   Pravin B Shelar   net: Fix possible...
614
  	iph = ip_hdr(skb);
fc70fb640   Alexander Duyck   net: Handle encap...
615

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
  	left = skb->len - hlen;		/* Space per frame */
49085bd7d   George Kadianakis   net/ipv4/ip_outpu...
617
  	ptr = hlen;		/* Where to start from */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
  	/* for bridged IP traffic encapsulated inside f.e. a vlan header,
9bcfcaf5e   Stephen Hemminger   [NETFILTER] bridg...
620
621
  	 * we need to make room for the encapsulating header
  	 */
c893b8066   Changli Gao   ip_fragment: fix ...
622
  	ll_rs = LL_RESERVED_SPACE_EXTRA(rt->dst.dev, nf_bridge_pad(skb));
9bcfcaf5e   Stephen Hemminger   [NETFILTER] bridg...
623

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
625
626
627
628
629
630
631
632
633
  	/*
  	 *	Fragment the datagram.
  	 */
  
  	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
  	not_last_frag = iph->frag_off & htons(IP_MF);
  
  	/*
  	 *	Keep copying data until we run out.
  	 */
132adf546   Stephen Hemminger   [IPV4]: cleanup
634
  	while (left > 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
636
637
638
  		len = left;
  		/* IF: it doesn't fit, use 'mtu' - the data space left */
  		if (len > mtu)
  			len = mtu;
25985edce   Lucas De Marchi   Fix common misspe...
639
  		/* IF: we are not sending up to and including the packet end
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
641
642
643
  		   then align the next start on an eight byte boundary */
  		if (len < left)	{
  			len &= ~7;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644

cbffccc97   Joe Perches   net; ipv[46] - Re...
645
646
647
  		/* Allocate buffer */
  		skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
  		if (!skb2) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
650
651
652
653
654
655
656
657
658
  			err = -ENOMEM;
  			goto fail;
  		}
  
  		/*
  		 *	Set up data on packet
  		 */
  
  		ip_copy_metadata(skb2, skb);
  		skb_reserve(skb2, ll_rs);
  		skb_put(skb2, len + hlen);
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
659
  		skb_reset_network_header(skb2);
b0e380b1d   Arnaldo Carvalho de Melo   [SK_BUFF]: unions...
660
  		skb2->transport_header = skb2->network_header + hlen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
662
663
664
665
666
667
668
669
670
671
672
  
  		/*
  		 *	Charge the memory for the fragment to any owner
  		 *	it might possess
  		 */
  
  		if (skb->sk)
  			skb_set_owner_w(skb2, skb->sk);
  
  		/*
  		 *	Copy the packet header into the new buffer.
  		 */
d626f62b1   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
673
  		skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
675
676
677
  
  		/*
  		 *	Copy a block of the IP datagram.
  		 */
bff9b61ce   Arnaldo Carvalho de Melo   [SK_BUFF]: Use th...
678
  		if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
682
683
684
  			BUG();
  		left -= len;
  
  		/*
  		 *	Fill in the new header fields.
  		 */
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
685
  		iph = ip_hdr(skb2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
  		iph->frag_off = htons((offset >> 3));
  
  		/* ANK: dirty, but effective trick. Upgrade options only if
  		 * the segment to be fragmented was THE FIRST (otherwise,
  		 * options are already fixed) and make it ONCE
  		 * on the initial skb, so that all the following fragments
  		 * will inherit fixed options.
  		 */
  		if (offset == 0)
  			ip_options_fragment(skb);
  
  		/*
  		 *	Added AC : If we are fragmenting a fragment that's not the
  		 *		   last fragment then keep MF on each bit
  		 */
  		if (left > 0 || not_last_frag)
  			iph->frag_off |= htons(IP_MF);
  		ptr += len;
  		offset += len;
  
  		/*
  		 *	Put this fragment into the sending queue.
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
712
713
714
715
  		iph->tot_len = htons(len + hlen);
  
  		ip_send_check(iph);
  
  		err = output(skb2);
  		if (err)
  			goto fail;
dafee4908   Wei Dong   [IPV6]: SNMPv2 "i...
716

5e38e2704   Pavel Emelyanov   mib: add net to I...
717
  		IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
  	}
5d0ba55b6   Eric Dumazet   net: use consume_...
719
  	consume_skb(skb);
5e38e2704   Pavel Emelyanov   mib: add net to I...
720
  	IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
722
723
  	return err;
  
  fail:
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
724
  	kfree_skb(skb);
5e38e2704   Pavel Emelyanov   mib: add net to I...
725
  	IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726
727
  	return err;
  }
2e2f7aefa   Patrick McHardy   [NETFILTER]: Fix ...
728
  EXPORT_SYMBOL(ip_fragment);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
730
731
  int
  ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
  {
f69e6d131   Al Viro   ip_generic_getfra...
732
  	struct msghdr *msg = from;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
733

84fa7933a   Patrick McHardy   [NET]: Replace CH...
734
  	if (skb->ip_summed == CHECKSUM_PARTIAL) {
c0371da60   Al Viro   put iov_iter into...
735
736
  		/* XXX: stripping const */
  		if (memcpy_fromiovecend(to, (struct iovec *)msg->msg_iter.iov, offset, len) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737
738
  			return -EFAULT;
  	} else {
44bb93633   Al Viro   [NET]: Annotate c...
739
  		__wsum csum = 0;
c0371da60   Al Viro   put iov_iter into...
740
741
  		/* XXX: stripping const */
  		if (csum_partial_copy_fromiovecend(to, (struct iovec *)msg->msg_iter.iov, offset, len, &csum) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
742
743
744
745
746
  			return -EFAULT;
  		skb->csum = csum_block_add(skb->csum, csum, odd);
  	}
  	return 0;
  }
4bc2f18ba   Eric Dumazet   net/ipv4: EXPORT_...
747
  EXPORT_SYMBOL(ip_generic_getfrag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748

44bb93633   Al Viro   [NET]: Annotate c...
749
  static inline __wsum
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
750
751
752
  csum_page(struct page *page, int offset, int copy)
  {
  	char *kaddr;
44bb93633   Al Viro   [NET]: Annotate c...
753
  	__wsum csum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
755
756
757
758
  	kaddr = kmap(page);
  	csum = csum_partial(kaddr + offset, copy, 0);
  	kunmap(page);
  	return csum;
  }
4b30b1c6a   Adrian Bunk   [IPV4]: make two ...
759
  static inline int ip_ufo_append_data(struct sock *sk,
1470ddf7f   Herbert Xu   inet: Remove expl...
760
  			struct sk_buff_head *queue,
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
761
762
763
  			int getfrag(void *from, char *to, int offset, int len,
  			       int odd, struct sk_buff *skb),
  			void *from, int length, int hh_len, int fragheaderlen,
d9be4f7a6   Bill Sommerfeld   ipv4: Constrain U...
764
  			int transhdrlen, int maxfraglen, unsigned int flags)
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
765
766
767
768
769
770
771
772
  {
  	struct sk_buff *skb;
  	int err;
  
  	/* There is support for UDP fragmentation offload by network
  	 * device, so create one single skb packet containing complete
  	 * udp datagram
  	 */
1470ddf7f   Herbert Xu   inet: Remove expl...
773
  	if ((skb = skb_peek_tail(queue)) == NULL) {
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
774
775
776
777
778
779
780
781
782
783
784
  		skb = sock_alloc_send_skb(sk,
  			hh_len + fragheaderlen + transhdrlen + 20,
  			(flags & MSG_DONTWAIT), &err);
  
  		if (skb == NULL)
  			return err;
  
  		/* reserve space for Hardware header */
  		skb_reserve(skb, hh_len);
  
  		/* create space for UDP/IP header */
d9319100c   Jianjun Kong   net: clean up net...
785
  		skb_put(skb, fragheaderlen + transhdrlen);
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
786
787
  
  		/* initialize network header pointer */
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
788
  		skb_reset_network_header(skb);
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
789
790
  
  		/* initialize protocol header pointer */
b0e380b1d   Arnaldo Carvalho de Melo   [SK_BUFF]: unions...
791
  		skb->transport_header = skb->network_header + fragheaderlen;
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
792

e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
793
  		skb->csum = 0;
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
794

e93b7d748   Jiri Pirko   ip_output: do skb...
795

1470ddf7f   Herbert Xu   inet: Remove expl...
796
  		__skb_queue_tail(queue, skb);
e93b7d748   Jiri Pirko   ip_output: do skb...
797
798
  	} else if (skb_is_gso(skb)) {
  		goto append;
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
799
  	}
be9164e76   Kostya B   [IPv4] UFO: preve...
800

e93b7d748   Jiri Pirko   ip_output: do skb...
801
802
803
804
805
806
  	skb->ip_summed = CHECKSUM_PARTIAL;
  	/* specify the length of each IP datagram fragment */
  	skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen;
  	skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  
  append:
be9164e76   Kostya B   [IPv4] UFO: preve...
807
808
  	return skb_append_datato_frags(sk, skb, getfrag, from,
  				       (length - transhdrlen));
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
809
  }
f5fca6086   David S. Miller   ipv4: Pass flow k...
810
811
812
  static int __ip_append_data(struct sock *sk,
  			    struct flowi4 *fl4,
  			    struct sk_buff_head *queue,
1470ddf7f   Herbert Xu   inet: Remove expl...
813
  			    struct inet_cork *cork,
5640f7685   Eric Dumazet   net: use a per ta...
814
  			    struct page_frag *pfrag,
1470ddf7f   Herbert Xu   inet: Remove expl...
815
816
817
818
  			    int getfrag(void *from, char *to, int offset,
  					int len, int odd, struct sk_buff *skb),
  			    void *from, int length, int transhdrlen,
  			    unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
820
821
  {
  	struct inet_sock *inet = inet_sk(sk);
  	struct sk_buff *skb;
07df5294a   Herbert Xu   inet: Replace lef...
822
  	struct ip_options *opt = cork->opt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
825
826
827
828
  	int hh_len;
  	int exthdrlen;
  	int mtu;
  	int copy;
  	int err;
  	int offset = 0;
daba287b2   Hannes Frederic Sowa   ipv4: fix DO and ...
829
  	unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830
  	int csummode = CHECKSUM_NONE;
1470ddf7f   Herbert Xu   inet: Remove expl...
831
  	struct rtable *rt = (struct rtable *)cork->dst;
09c2d251b   Willem de Bruijn   net-timestamp: ad...
832
  	u32 tskey = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833

96d7303e9   Steffen Klassert   ipv4: Fix packet ...
834
835
836
  	skb = skb_peek_tail(queue);
  
  	exthdrlen = !skb ? rt->dst.header_len : 0;
07df5294a   Herbert Xu   inet: Replace lef...
837
  	mtu = cork->fragsize;
09c2d251b   Willem de Bruijn   net-timestamp: ad...
838
839
840
  	if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
  	    sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
  		tskey = sk->sk_tskey++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841

d8d1f30b9   Changli Gao   net-next: remove ...
842
  	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
844
845
  
  	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
  	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
60ff74673   WANG Cong   net: rename local...
846
  	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847

daba287b2   Hannes Frederic Sowa   ipv4: fix DO and ...
848
  	if (cork->length + length > maxnonfragsize - fragheaderlen) {
f5fca6086   David S. Miller   ipv4: Pass flow k...
849
  		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
61e7f09d0   Hannes Frederic Sowa   ipv4: consistent ...
850
  			       mtu - (opt ? opt->optlen : 0));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
852
853
854
855
856
857
858
859
  		return -EMSGSIZE;
  	}
  
  	/*
  	 * transhdrlen > 0 means that this is the first fragment and we wish
  	 * it won't be fragmented in the future.
  	 */
  	if (transhdrlen &&
  	    length + fragheaderlen <= mtu &&
d8d1f30b9   Changli Gao   net-next: remove ...
860
  	    rt->dst.dev->features & NETIF_F_V4_CSUM &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
  	    !exthdrlen)
84fa7933a   Patrick McHardy   [NET]: Replace CH...
862
  		csummode = CHECKSUM_PARTIAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863

1470ddf7f   Herbert Xu   inet: Remove expl...
864
  	cork->length += length;
26cde9f7e   Herbert Xu   udp: Fix bogus UF...
865
  	if (((length > mtu) || (skb && skb_is_gso(skb))) &&
be9164e76   Kostya B   [IPv4] UFO: preve...
866
  	    (sk->sk_protocol == IPPROTO_UDP) &&
c146066ab   Steffen Klassert   ipv4: Don't use u...
867
  	    (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len) {
1470ddf7f   Herbert Xu   inet: Remove expl...
868
869
  		err = ip_ufo_append_data(sk, queue, getfrag, from, length,
  					 hh_len, fragheaderlen, transhdrlen,
d9be4f7a6   Bill Sommerfeld   ipv4: Constrain U...
870
  					 maxfraglen, flags);
baa829d89   Patrick McHardy   [IPV4/6]: Fix UFO...
871
  		if (err)
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
872
  			goto error;
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
873
874
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
876
877
878
879
880
881
  
  	/* So, what's going on in the loop below?
  	 *
  	 * We use calculated fragment length to generate chained skb,
  	 * each of segments is IP fragment ready for sending to network after
  	 * adding appropriate IP header.
  	 */
26cde9f7e   Herbert Xu   udp: Fix bogus UF...
882
  	if (!skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
  		goto alloc_new_skb;
  
  	while (length > 0) {
  		/* Check if the remaining data fits into current packet. */
  		copy = mtu - skb->len;
  		if (copy < length)
  			copy = maxfraglen - skb->len;
  		if (copy <= 0) {
  			char *data;
  			unsigned int datalen;
  			unsigned int fraglen;
  			unsigned int fraggap;
  			unsigned int alloclen;
  			struct sk_buff *skb_prev;
  alloc_new_skb:
  			skb_prev = skb;
  			if (skb_prev)
  				fraggap = skb_prev->len - maxfraglen;
  			else
  				fraggap = 0;
  
  			/*
  			 * If remaining data exceeds the mtu,
  			 * we know we need more fragment(s).
  			 */
  			datalen = length + fraggap;
  			if (datalen > mtu - fragheaderlen)
  				datalen = maxfraglen - fragheaderlen;
  			fraglen = datalen + fragheaderlen;
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
912
  			if ((flags & MSG_MORE) &&
d8d1f30b9   Changli Gao   net-next: remove ...
913
  			    !(rt->dst.dev->features&NETIF_F_SG))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
915
  				alloclen = mtu;
  			else
59104f062   Eric Dumazet   ip: take care of ...
916
  				alloclen = fraglen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917

353e5c9ab   Steffen Klassert   ipv4: Fix IPsec s...
918
  			alloclen += exthdrlen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
920
921
922
923
  			/* The last fragment gets additional space at tail.
  			 * Note, with MSG_MORE we overallocate on fragments,
  			 * because we have no idea what fragment will be
  			 * the last.
  			 */
33f99dc7f   Steffen Klassert   ipv4: Fix packet ...
924
  			if (datalen == length + fraggap)
d8d1f30b9   Changli Gao   net-next: remove ...
925
  				alloclen += rt->dst.trailer_len;
33f99dc7f   Steffen Klassert   ipv4: Fix packet ...
926

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
  			if (transhdrlen) {
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
928
  				skb = sock_alloc_send_skb(sk,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
929
930
931
932
933
934
  						alloclen + hh_len + 15,
  						(flags & MSG_DONTWAIT), &err);
  			} else {
  				skb = NULL;
  				if (atomic_read(&sk->sk_wmem_alloc) <=
  				    2 * sk->sk_sndbuf)
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
935
  					skb = sock_wmalloc(sk,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  							   alloclen + hh_len + 15, 1,
  							   sk->sk_allocation);
  				if (unlikely(skb == NULL))
  					err = -ENOBUFS;
  			}
  			if (skb == NULL)
  				goto error;
  
  			/*
  			 *	Fill in the control structures
  			 */
  			skb->ip_summed = csummode;
  			skb->csum = 0;
  			skb_reserve(skb, hh_len);
11878b40e   Willem de Bruijn   net-timestamp: SO...
950
951
  
  			/* only the initial fragment is time stamped */
1470ddf7f   Herbert Xu   inet: Remove expl...
952
  			skb_shinfo(skb)->tx_flags = cork->tx_flags;
11878b40e   Willem de Bruijn   net-timestamp: SO...
953
  			cork->tx_flags = 0;
09c2d251b   Willem de Bruijn   net-timestamp: ad...
954
955
  			skb_shinfo(skb)->tskey = tskey;
  			tskey = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
956
957
958
959
  
  			/*
  			 *	Find where to start putting bytes.
  			 */
353e5c9ab   Steffen Klassert   ipv4: Fix IPsec s...
960
  			data = skb_put(skb, fraglen + exthdrlen);
c14d2450c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
961
  			skb_set_network_header(skb, exthdrlen);
b0e380b1d   Arnaldo Carvalho de Melo   [SK_BUFF]: unions...
962
963
  			skb->transport_header = (skb->network_header +
  						 fragheaderlen);
353e5c9ab   Steffen Klassert   ipv4: Fix IPsec s...
964
  			data += fragheaderlen + exthdrlen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
965
966
967
968
969
970
971
972
  
  			if (fraggap) {
  				skb->csum = skb_copy_and_csum_bits(
  					skb_prev, maxfraglen,
  					data + transhdrlen, fraggap, 0);
  				skb_prev->csum = csum_sub(skb_prev->csum,
  							  skb->csum);
  				data += fraggap;
e9fa4f7bd   Herbert Xu   [INET]: Use pskb_...
973
  				pskb_trim_unique(skb_prev, maxfraglen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
  			}
  
  			copy = datalen - transhdrlen - fraggap;
  			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  				err = -EFAULT;
  				kfree_skb(skb);
  				goto error;
  			}
  
  			offset += copy;
  			length -= datalen - fraggap;
  			transhdrlen = 0;
  			exthdrlen = 0;
  			csummode = CHECKSUM_NONE;
  
  			/*
  			 * Put the packet on the pending queue.
  			 */
1470ddf7f   Herbert Xu   inet: Remove expl...
992
  			__skb_queue_tail(queue, skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
997
  			continue;
  		}
  
  		if (copy > length)
  			copy = length;
d8d1f30b9   Changli Gao   net-next: remove ...
998
  		if (!(rt->dst.dev->features&NETIF_F_SG)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
1001
  			unsigned int off;
  
  			off = skb->len;
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
1002
  			if (getfrag(from, skb_put(skb, copy),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003
1004
1005
1006
1007
1008
1009
  					offset, copy, off, skb) < 0) {
  				__skb_trim(skb, off);
  				err = -EFAULT;
  				goto error;
  			}
  		} else {
  			int i = skb_shinfo(skb)->nr_frags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010

5640f7685   Eric Dumazet   net: use a per ta...
1011
1012
  			err = -ENOMEM;
  			if (!sk_page_frag_refill(sk, pfrag))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
  				goto error;
5640f7685   Eric Dumazet   net: use a per ta...
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
  
  			if (!skb_can_coalesce(skb, i, pfrag->page,
  					      pfrag->offset)) {
  				err = -EMSGSIZE;
  				if (i == MAX_SKB_FRAGS)
  					goto error;
  
  				__skb_fill_page_desc(skb, i, pfrag->page,
  						     pfrag->offset, 0);
  				skb_shinfo(skb)->nr_frags = ++i;
  				get_page(pfrag->page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1025
  			}
5640f7685   Eric Dumazet   net: use a per ta...
1026
1027
1028
1029
1030
1031
1032
1033
  			copy = min_t(int, copy, pfrag->size - pfrag->offset);
  			if (getfrag(from,
  				    page_address(pfrag->page) + pfrag->offset,
  				    offset, copy, skb->len, skb) < 0)
  				goto error_efault;
  
  			pfrag->offset += copy;
  			skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1034
1035
  			skb->len += copy;
  			skb->data_len += copy;
f945fa7ad   Herbert Xu   [INET]: Fix trues...
1036
1037
  			skb->truesize += copy;
  			atomic_add(copy, &sk->sk_wmem_alloc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1038
1039
1040
1041
1042
1043
  		}
  		offset += copy;
  		length -= copy;
  	}
  
  	return 0;
5640f7685   Eric Dumazet   net: use a per ta...
1044
1045
  error_efault:
  	err = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
  error:
1470ddf7f   Herbert Xu   inet: Remove expl...
1047
  	cork->length -= length;
5e38e2704   Pavel Emelyanov   mib: add net to I...
1048
  	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
1049
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1050
  }
1470ddf7f   Herbert Xu   inet: Remove expl...
1051
1052
1053
  static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
  			 struct ipcm_cookie *ipc, struct rtable **rtp)
  {
f6d8bd051   Eric Dumazet   inet: add RCU pro...
1054
  	struct ip_options_rcu *opt;
1470ddf7f   Herbert Xu   inet: Remove expl...
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
  	struct rtable *rt;
  
  	/*
  	 * setup for corking.
  	 */
  	opt = ipc->opt;
  	if (opt) {
  		if (cork->opt == NULL) {
  			cork->opt = kmalloc(sizeof(struct ip_options) + 40,
  					    sk->sk_allocation);
  			if (unlikely(cork->opt == NULL))
  				return -ENOBUFS;
  		}
f6d8bd051   Eric Dumazet   inet: add RCU pro...
1068
  		memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
1470ddf7f   Herbert Xu   inet: Remove expl...
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
  		cork->flags |= IPCORK_OPT;
  		cork->addr = ipc->addr;
  	}
  	rt = *rtp;
  	if (unlikely(!rt))
  		return -EFAULT;
  	/*
  	 * We steal reference to this route, caller should not release it
  	 */
  	*rtp = NULL;
482fc6094   Hannes Frederic Sowa   ipv4: introduce n...
1079
1080
  	cork->fragsize = ip_sk_use_pmtu(sk) ?
  			 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
1470ddf7f   Herbert Xu   inet: Remove expl...
1081
1082
  	cork->dst = &rt->dst;
  	cork->length = 0;
aa6615814   Francesco Fusco   ipv4: processing ...
1083
1084
1085
  	cork->ttl = ipc->ttl;
  	cork->tos = ipc->tos;
  	cork->priority = ipc->priority;
1470ddf7f   Herbert Xu   inet: Remove expl...
1086
  	cork->tx_flags = ipc->tx_flags;
1470ddf7f   Herbert Xu   inet: Remove expl...
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  
  	return 0;
  }
  
  /*
   *	ip_append_data() and ip_append_page() can make one large IP datagram
   *	from many pieces of data. Each pieces will be holded on the socket
   *	until ip_push_pending_frames() is called. Each piece can be a page
   *	or non-page data.
   *
   *	Not only UDP, other transport protocols - e.g. raw sockets - can use
   *	this interface potentially.
   *
   *	LATER: length must be adjusted by pad at tail, when it is required.
   */
f5fca6086   David S. Miller   ipv4: Pass flow k...
1102
  int ip_append_data(struct sock *sk, struct flowi4 *fl4,
1470ddf7f   Herbert Xu   inet: Remove expl...
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
  		   int getfrag(void *from, char *to, int offset, int len,
  			       int odd, struct sk_buff *skb),
  		   void *from, int length, int transhdrlen,
  		   struct ipcm_cookie *ipc, struct rtable **rtp,
  		   unsigned int flags)
  {
  	struct inet_sock *inet = inet_sk(sk);
  	int err;
  
  	if (flags&MSG_PROBE)
  		return 0;
  
  	if (skb_queue_empty(&sk->sk_write_queue)) {
bdc712b4c   David S. Miller   inet: Decrease ov...
1116
  		err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
1470ddf7f   Herbert Xu   inet: Remove expl...
1117
1118
1119
1120
1121
  		if (err)
  			return err;
  	} else {
  		transhdrlen = 0;
  	}
5640f7685   Eric Dumazet   net: use a per ta...
1122
1123
  	return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
  				sk_page_frag(sk), getfrag,
1470ddf7f   Herbert Xu   inet: Remove expl...
1124
1125
  				from, length, transhdrlen, flags);
  }
f5fca6086   David S. Miller   ipv4: Pass flow k...
1126
  ssize_t	ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1127
1128
1129
1130
1131
1132
  		       int offset, size_t size, int flags)
  {
  	struct inet_sock *inet = inet_sk(sk);
  	struct sk_buff *skb;
  	struct rtable *rt;
  	struct ip_options *opt = NULL;
bdc712b4c   David S. Miller   inet: Decrease ov...
1133
  	struct inet_cork *cork;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
1135
1136
1137
  	int hh_len;
  	int mtu;
  	int len;
  	int err;
daba287b2   Hannes Frederic Sowa   ipv4: fix DO and ...
1138
  	unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1139
1140
1141
1142
1143
1144
1145
1146
1147
  
  	if (inet->hdrincl)
  		return -EPERM;
  
  	if (flags&MSG_PROBE)
  		return 0;
  
  	if (skb_queue_empty(&sk->sk_write_queue))
  		return -EINVAL;
bdc712b4c   David S. Miller   inet: Decrease ov...
1148
1149
1150
1151
  	cork = &inet->cork.base;
  	rt = (struct rtable *)cork->dst;
  	if (cork->flags & IPCORK_OPT)
  		opt = cork->opt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1152

d8d1f30b9   Changli Gao   net-next: remove ...
1153
  	if (!(rt->dst.dev->features&NETIF_F_SG))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1154
  		return -EOPNOTSUPP;
d8d1f30b9   Changli Gao   net-next: remove ...
1155
  	hh_len = LL_RESERVED_SPACE(rt->dst.dev);
bdc712b4c   David S. Miller   inet: Decrease ov...
1156
  	mtu = cork->fragsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
1158
1159
  
  	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
  	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
60ff74673   WANG Cong   net: rename local...
1160
  	maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1161

daba287b2   Hannes Frederic Sowa   ipv4: fix DO and ...
1162
  	if (cork->length + size > maxnonfragsize - fragheaderlen) {
61e7f09d0   Hannes Frederic Sowa   ipv4: consistent ...
1163
1164
  		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
  			       mtu - (opt ? opt->optlen : 0));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1165
1166
1167
1168
1169
  		return -EMSGSIZE;
  	}
  
  	if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  		return -EINVAL;
bdc712b4c   David S. Miller   inet: Decrease ov...
1170
  	cork->length += size;
26cde9f7e   Herbert Xu   udp: Fix bogus UF...
1171
1172
  	if ((size + skb->len > mtu) &&
  	    (sk->sk_protocol == IPPROTO_UDP) &&
d8d1f30b9   Changli Gao   net-next: remove ...
1173
  	    (rt->dst.dev->features & NETIF_F_UFO)) {
7967168ce   Herbert Xu   [NET]: Merge TSO/...
1174
  		skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
f83ef8c0b   Herbert Xu   [IPV6]: Added GSO...
1175
  		skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
7967168ce   Herbert Xu   [NET]: Merge TSO/...
1176
  	}
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
1177

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1178
1179
1180
  
  	while (size > 0) {
  		int i;
89114afd4   Herbert Xu   [NET] gso: Add sk...
1181
  		if (skb_is_gso(skb))
e89e9cf53   Ananda Raju   [IPv4/IPv6]: UFO ...
1182
1183
1184
1185
1186
1187
1188
1189
  			len = size;
  		else {
  
  			/* Check if the remaining data fits into current packet. */
  			len = mtu - skb->len;
  			if (len < size)
  				len = maxfraglen - skb->len;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1190
1191
  		if (len <= 0) {
  			struct sk_buff *skb_prev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1192
1193
1194
  			int alloclen;
  
  			skb_prev = skb;
0d0d2bba9   Jayachandran C   [IPV4]: Remove de...
1195
  			fraggap = skb_prev->len - maxfraglen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
  
  			alloclen = fragheaderlen + hh_len + fraggap + 15;
  			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
  			if (unlikely(!skb)) {
  				err = -ENOBUFS;
  				goto error;
  			}
  
  			/*
  			 *	Fill in the control structures
  			 */
  			skb->ip_summed = CHECKSUM_NONE;
  			skb->csum = 0;
  			skb_reserve(skb, hh_len);
  
  			/*
  			 *	Find where to start putting bytes.
  			 */
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1214
  			skb_put(skb, fragheaderlen + fraggap);
2ca9e6f2c   Arnaldo Carvalho de Melo   [SK_BUFF]: Some m...
1215
  			skb_reset_network_header(skb);
b0e380b1d   Arnaldo Carvalho de Melo   [SK_BUFF]: unions...
1216
1217
  			skb->transport_header = (skb->network_header +
  						 fragheaderlen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1218
  			if (fraggap) {
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1219
1220
  				skb->csum = skb_copy_and_csum_bits(skb_prev,
  								   maxfraglen,
9c70220b7   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1221
  						    skb_transport_header(skb),
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1222
  								   fraggap, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1223
1224
  				skb_prev->csum = csum_sub(skb_prev->csum,
  							  skb->csum);
e9fa4f7bd   Herbert Xu   [INET]: Use pskb_...
1225
  				pskb_trim_unique(skb_prev, maxfraglen);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
  			}
  
  			/*
  			 * Put the packet on the pending queue.
  			 */
  			__skb_queue_tail(&sk->sk_write_queue, skb);
  			continue;
  		}
  
  		i = skb_shinfo(skb)->nr_frags;
  		if (len > size)
  			len = size;
  		if (skb_can_coalesce(skb, i, page, offset)) {
9e903e085   Eric Dumazet   net: add skb frag...
1239
  			skb_frag_size_add(&skb_shinfo(skb)->frags[i-1], len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1240
1241
1242
1243
1244
1245
1246
1247
1248
  		} else if (i < MAX_SKB_FRAGS) {
  			get_page(page);
  			skb_fill_page_desc(skb, i, page, offset, len);
  		} else {
  			err = -EMSGSIZE;
  			goto error;
  		}
  
  		if (skb->ip_summed == CHECKSUM_NONE) {
44bb93633   Al Viro   [NET]: Annotate c...
1249
  			__wsum csum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
1251
1252
1253
1254
1255
  			csum = csum_page(page, offset, len);
  			skb->csum = csum_block_add(skb->csum, csum, skb->len);
  		}
  
  		skb->len += len;
  		skb->data_len += len;
1e34a11d5   David S. Miller   [IPV4]: Add missi...
1256
1257
  		skb->truesize += len;
  		atomic_add(len, &sk->sk_wmem_alloc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1258
1259
1260
1261
1262
1263
  		offset += len;
  		size -= len;
  	}
  	return 0;
  
  error:
bdc712b4c   David S. Miller   inet: Decrease ov...
1264
  	cork->length -= size;
5e38e2704   Pavel Emelyanov   mib: add net to I...
1265
  	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
1267
  	return err;
  }
1470ddf7f   Herbert Xu   inet: Remove expl...
1268
  static void ip_cork_release(struct inet_cork *cork)
429f08e95   Pavel Emelyanov   [IPV4]: Consolida...
1269
  {
1470ddf7f   Herbert Xu   inet: Remove expl...
1270
1271
1272
1273
1274
  	cork->flags &= ~IPCORK_OPT;
  	kfree(cork->opt);
  	cork->opt = NULL;
  	dst_release(cork->dst);
  	cork->dst = NULL;
429f08e95   Pavel Emelyanov   [IPV4]: Consolida...
1275
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
1277
1278
1279
  /*
   *	Combined all pending IP fragments on the socket as one IP datagram
   *	and push them out.
   */
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1280
  struct sk_buff *__ip_make_skb(struct sock *sk,
77968b782   David S. Miller   ipv4: Pass flow k...
1281
  			      struct flowi4 *fl4,
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1282
1283
  			      struct sk_buff_head *queue,
  			      struct inet_cork *cork)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1284
1285
1286
1287
  {
  	struct sk_buff *skb, *tmp_skb;
  	struct sk_buff **tail_skb;
  	struct inet_sock *inet = inet_sk(sk);
0388b0042   Pavel Emelyanov   icmp: add struct ...
1288
  	struct net *net = sock_net(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1289
  	struct ip_options *opt = NULL;
1470ddf7f   Herbert Xu   inet: Remove expl...
1290
  	struct rtable *rt = (struct rtable *)cork->dst;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1291
  	struct iphdr *iph;
76ab608d8   Alexey Dobriyan   [NET]: Endian-ann...
1292
  	__be16 df = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1293
  	__u8 ttl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1294

1470ddf7f   Herbert Xu   inet: Remove expl...
1295
  	if ((skb = __skb_dequeue(queue)) == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1296
1297
1298
1299
  		goto out;
  	tail_skb = &(skb_shinfo(skb)->frag_list);
  
  	/* move skb->data to ip header from ext header */
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1300
  	if (skb->data < skb_network_header(skb))
bbe735e42   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1301
  		__skb_pull(skb, skb_network_offset(skb));
1470ddf7f   Herbert Xu   inet: Remove expl...
1302
  	while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
cfe1fc775   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1303
  		__skb_pull(tmp_skb, skb_network_header_len(skb));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1304
1305
1306
1307
1308
  		*tail_skb = tmp_skb;
  		tail_skb = &(tmp_skb->next);
  		skb->len += tmp_skb->len;
  		skb->data_len += tmp_skb->len;
  		skb->truesize += tmp_skb->truesize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1309
1310
1311
1312
1313
1314
1315
1316
  		tmp_skb->destructor = NULL;
  		tmp_skb->sk = NULL;
  	}
  
  	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
  	 * to fragment the frame generated here. No matter, what transforms
  	 * how transforms change size of the packet, it will come out.
  	 */
60ff74673   WANG Cong   net: rename local...
1317
  	skb->ignore_df = ip_sk_ignore_df(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1318
1319
  
  	/* DF bit is set when we want to see DF on outgoing frames.
60ff74673   WANG Cong   net: rename local...
1320
  	 * If ignore_df is set too, we still allow to fragment this frame
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1321
  	 * locally. */
482fc6094   Hannes Frederic Sowa   ipv4: introduce n...
1322
1323
  	if (inet->pmtudisc == IP_PMTUDISC_DO ||
  	    inet->pmtudisc == IP_PMTUDISC_PROBE ||
d8d1f30b9   Changli Gao   net-next: remove ...
1324
1325
  	    (skb->len <= dst_mtu(&rt->dst) &&
  	     ip_dont_fragment(sk, &rt->dst)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
  		df = htons(IP_DF);
1470ddf7f   Herbert Xu   inet: Remove expl...
1327
1328
  	if (cork->flags & IPCORK_OPT)
  		opt = cork->opt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1329

aa6615814   Francesco Fusco   ipv4: processing ...
1330
1331
1332
  	if (cork->ttl != 0)
  		ttl = cork->ttl;
  	else if (rt->rt_type == RTN_MULTICAST)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1333
1334
  		ttl = inet->mc_ttl;
  	else
d8d1f30b9   Changli Gao   net-next: remove ...
1335
  		ttl = ip_select_ttl(inet, &rt->dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336

749154aa5   Ansis Atteka   ip: use ip_hdr() ...
1337
  	iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1338
1339
  	iph->version = 4;
  	iph->ihl = 5;
aa6615814   Francesco Fusco   ipv4: processing ...
1340
  	iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1341
  	iph->frag_off = df;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1342
1343
  	iph->ttl = ttl;
  	iph->protocol = sk->sk_protocol;
84f9307c5   Eric Dumazet   ipv4: use a 64bit...
1344
  	ip_copy_addrs(iph, fl4);
73f156a6e   Eric Dumazet   inetpeer: get rid...
1345
  	ip_select_ident(skb, sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346

22f728f8f   David S. Miller   ipv4: Always call...
1347
1348
1349
1350
  	if (opt) {
  		iph->ihl += opt->optlen>>2;
  		ip_options_build(skb, opt, cork->addr, rt, 0);
  	}
aa6615814   Francesco Fusco   ipv4: processing ...
1351
  	skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
4a19ec580   Laszlo Attila Toth   [NET]: Introducin...
1352
  	skb->mark = sk->sk_mark;
a21bba945   Eric Dumazet   net: avoid a pair...
1353
1354
1355
1356
  	/*
  	 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
  	 * on dst refcount
  	 */
1470ddf7f   Herbert Xu   inet: Remove expl...
1357
  	cork->dst = NULL;
d8d1f30b9   Changli Gao   net-next: remove ...
1358
  	skb_dst_set(skb, &rt->dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1359

96793b482   David L Stevens   [IPV4]: Add ICMPM...
1360
  	if (iph->protocol == IPPROTO_ICMP)
0388b0042   Pavel Emelyanov   icmp: add struct ...
1361
  		icmp_out_count(net, ((struct icmphdr *)
96793b482   David L Stevens   [IPV4]: Add ICMPM...
1362
  			skb_transport_header(skb))->type);
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1363
1364
1365
1366
  	ip_cork_release(cork);
  out:
  	return skb;
  }
b5ec8eeac   Eric Dumazet   ipv4: fix ip_send...
1367
  int ip_send_skb(struct net *net, struct sk_buff *skb)
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1368
  {
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1369
  	int err;
c439cb2e4   Herbert Xu   [IPV4]: Add ip_lo...
1370
  	err = ip_local_out(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1371
1372
  	if (err) {
  		if (err > 0)
6ce9e7b5f   Eric Dumazet   ip: Report qdisc ...
1373
  			err = net_xmit_errno(err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1374
  		if (err)
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1375
  			IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1376
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1377
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1378
  }
77968b782   David S. Miller   ipv4: Pass flow k...
1379
  int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
1470ddf7f   Herbert Xu   inet: Remove expl...
1380
  {
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1381
  	struct sk_buff *skb;
77968b782   David S. Miller   ipv4: Pass flow k...
1382
  	skb = ip_finish_skb(sk, fl4);
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1383
1384
1385
1386
  	if (!skb)
  		return 0;
  
  	/* Netfilter gets whole the not fragmented skb. */
b5ec8eeac   Eric Dumazet   ipv4: fix ip_send...
1387
  	return ip_send_skb(sock_net(sk), skb);
1470ddf7f   Herbert Xu   inet: Remove expl...
1388
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1389
1390
1391
  /*
   *	Throw away all pending data on the socket.
   */
1470ddf7f   Herbert Xu   inet: Remove expl...
1392
1393
1394
  static void __ip_flush_pending_frames(struct sock *sk,
  				      struct sk_buff_head *queue,
  				      struct inet_cork *cork)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1395
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1396
  	struct sk_buff *skb;
1470ddf7f   Herbert Xu   inet: Remove expl...
1397
  	while ((skb = __skb_dequeue_tail(queue)) != NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1398
  		kfree_skb(skb);
1470ddf7f   Herbert Xu   inet: Remove expl...
1399
1400
1401
1402
1403
  	ip_cork_release(cork);
  }
  
  void ip_flush_pending_frames(struct sock *sk)
  {
bdc712b4c   David S. Miller   inet: Decrease ov...
1404
  	__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1405
  }
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1406
  struct sk_buff *ip_make_skb(struct sock *sk,
77968b782   David S. Miller   ipv4: Pass flow k...
1407
  			    struct flowi4 *fl4,
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1408
1409
1410
1411
1412
1413
  			    int getfrag(void *from, char *to, int offset,
  					int len, int odd, struct sk_buff *skb),
  			    void *from, int length, int transhdrlen,
  			    struct ipcm_cookie *ipc, struct rtable **rtp,
  			    unsigned int flags)
  {
b80d72261   David S. Miller   ipv4: Initialize ...
1414
  	struct inet_cork cork;
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1415
1416
1417
1418
1419
1420
1421
  	struct sk_buff_head queue;
  	int err;
  
  	if (flags & MSG_PROBE)
  		return NULL;
  
  	__skb_queue_head_init(&queue);
b80d72261   David S. Miller   ipv4: Initialize ...
1422
1423
  	cork.flags = 0;
  	cork.addr = 0;
706527280   David S. Miller   ipv4: Initialize ...
1424
  	cork.opt = NULL;
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1425
1426
1427
  	err = ip_setup_cork(sk, &cork, ipc, rtp);
  	if (err)
  		return ERR_PTR(err);
5640f7685   Eric Dumazet   net: use a per ta...
1428
1429
  	err = __ip_append_data(sk, fl4, &queue, &cork,
  			       &current->task_frag, getfrag,
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1430
1431
1432
1433
1434
  			       from, length, transhdrlen, flags);
  	if (err) {
  		__ip_flush_pending_frames(sk, &queue, &cork);
  		return ERR_PTR(err);
  	}
77968b782   David S. Miller   ipv4: Pass flow k...
1435
  	return __ip_make_skb(sk, fl4, &queue, &cork);
1c32c5ad6   Herbert Xu   inet: Add ip_make...
1436
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
1438
1439
1440
  
  /*
   *	Fetch data from kernel space and fill in checksum if needed.
   */
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
1441
  static int ip_reply_glue_bits(void *dptr, char *to, int offset,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1442
1443
  			      int len, int odd, struct sk_buff *skb)
  {
5084205fa   Al Viro   [NET]: Annotate c...
1444
  	__wsum csum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1445
1446
1447
  
  	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
  	skb->csum = csum_block_add(skb->csum, csum, odd);
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
1448
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1449
  }
e905a9eda   YOSHIFUJI Hideaki   [NET] IPV4: Fix w...
1450
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1451
   *	Generic function to send a packet as reply to another packet.
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1452
   *	Used to send some TCP resets/acks so far.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
   *
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1454
   *	Use a fake percpu inet socket to avoid false sharing and contention.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1455
   */
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1456
1457
1458
1459
1460
1461
1462
1463
1464
  static DEFINE_PER_CPU(struct inet_sock, unicast_sock) = {
  	.sk = {
  		.__sk_common = {
  			.skc_refcnt = ATOMIC_INIT(1),
  		},
  		.sk_wmem_alloc	= ATOMIC_INIT(1),
  		.sk_allocation	= GFP_ATOMIC,
  		.sk_flags	= (1UL << SOCK_USE_WRITE_QUEUE),
  	},
0980e56e5   Eric Dumazet   ipv4: tcp: set un...
1465
1466
  	.pmtudisc	= IP_PMTUDISC_WANT,
  	.uc_ttl		= -1,
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1467
  };
24a2d43d8   Eric Dumazet   ipv4: rename ip_o...
1468
1469
1470
1471
  void ip_send_unicast_reply(struct net *net, struct sk_buff *skb,
  			   const struct ip_options *sopt,
  			   __be32 daddr, __be32 saddr,
  			   const struct ip_reply_arg *arg,
70e734167   David S. Miller   ipv4: Show that i...
1472
  			   unsigned int len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1473
  {
f6d8bd051   Eric Dumazet   inet: add RCU pro...
1474
  	struct ip_options_data replyopts;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1475
  	struct ipcm_cookie ipc;
77968b782   David S. Miller   ipv4: Pass flow k...
1476
  	struct flowi4 fl4;
511c3f92a   Eric Dumazet   net: skb->rtable ...
1477
  	struct rtable *rt = skb_rtable(skb);
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1478
1479
1480
  	struct sk_buff *nskb;
  	struct sock *sk;
  	struct inet_sock *inet;
4062090e3   Vasily Averin   ipv4: dst_entry l...
1481
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1482

24a2d43d8   Eric Dumazet   ipv4: rename ip_o...
1483
  	if (__ip_options_echo(&replyopts.opt.opt, skb, sopt))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484
  		return;
0a5ebb800   David S. Miller   ipv4: Pass explic...
1485
  	ipc.addr = daddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1486
  	ipc.opt = NULL;
2244d07bf   Oliver Hartkopp   net: simplify fla...
1487
  	ipc.tx_flags = 0;
aa6615814   Francesco Fusco   ipv4: processing ...
1488
1489
  	ipc.ttl = 0;
  	ipc.tos = -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1490

f6d8bd051   Eric Dumazet   inet: add RCU pro...
1491
  	if (replyopts.opt.opt.optlen) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1492
  		ipc.opt = &replyopts.opt;
f6d8bd051   Eric Dumazet   inet: add RCU pro...
1493
1494
  		if (replyopts.opt.opt.srr)
  			daddr = replyopts.opt.opt.faddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1495
  	}
e110861f8   Lorenzo Colitti   net: add a sysctl...
1496
1497
  	flowi4_init_output(&fl4, arg->bound_dev_if,
  			   IP4_REPLY_MARK(net, skb->mark),
66b13d99d   Eric Dumazet   ipv4: tcp: fix TO...
1498
  			   RT_TOS(arg->tos),
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1499
  			   RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
77968b782   David S. Miller   ipv4: Pass flow k...
1500
  			   ip_reply_arg_flowi_flags(arg),
70e734167   David S. Miller   ipv4: Show that i...
1501
  			   daddr, saddr,
77968b782   David S. Miller   ipv4: Pass flow k...
1502
1503
  			   tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
  	security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1504
  	rt = ip_route_output_key(net, &fl4);
77968b782   David S. Miller   ipv4: Pass flow k...
1505
1506
  	if (IS_ERR(rt))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1507

be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1508
  	inet = &get_cpu_var(unicast_sock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1509

66b13d99d   Eric Dumazet   ipv4: tcp: fix TO...
1510
  	inet->tos = arg->tos;
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1511
  	sk = &inet->sk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1512
  	sk->sk_priority = skb->priority;
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1513
  	sk->sk_protocol = ip_hdr(skb)->protocol;
f0e48dbfc   Patrick McHardy   [TCP]: Honour sk_...
1514
  	sk->sk_bound_dev_if = arg->bound_dev_if;
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1515
1516
1517
  	sock_net_set(sk, net);
  	__skb_queue_head_init(&sk->sk_write_queue);
  	sk->sk_sndbuf = sysctl_wmem_default;
4062090e3   Vasily Averin   ipv4: dst_entry l...
1518
1519
1520
1521
1522
1523
  	err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
  			     len, 0, &ipc, &rt, MSG_DONTWAIT);
  	if (unlikely(err)) {
  		ip_flush_pending_frames(sk);
  		goto out;
  	}
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1524
1525
  	nskb = skb_peek(&sk->sk_write_queue);
  	if (nskb) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1526
  		if (arg->csumoffset >= 0)
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1527
1528
  			*((__sum16 *)skb_transport_header(nskb) +
  			  arg->csumoffset) = csum_fold(csum_add(nskb->csum,
9c70220b7   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1529
  								arg->csum));
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1530
  		nskb->ip_summed = CHECKSUM_NONE;
3a7c384ff   Eric Dumazet   ipv4: tcp: unicas...
1531
  		skb_orphan(nskb);
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1532
  		skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb));
77968b782   David S. Miller   ipv4: Pass flow k...
1533
  		ip_push_pending_frames(sk, &fl4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1534
  	}
4062090e3   Vasily Averin   ipv4: dst_entry l...
1535
  out:
be9f4a44e   Eric Dumazet   ipv4: tcp: remove...
1536
  	put_cpu_var(unicast_sock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1537
1538
1539
  
  	ip_rt_put(rt);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1540
1541
  void __init ip_init(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1542
1543
  	ip_rt_init();
  	inet_initpeers();
72c1d3bdd   WANG Cong   ipv4: register ig...
1544
1545
  #if defined(CONFIG_IP_MULTICAST)
  	igmp_mc_init();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1546
1547
  #endif
  }