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