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