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.
6 * The Internet Protocol (IP) output module.
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Donald Becker, <becker@super.org>
11 * Alan Cox, <Alan.Cox@linux.org>
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>
18 * See ip_input.c for original log
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
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
42 * Hirokazu Takahashi: sendfile() on UDP works now.
45 #include <linux/uaccess.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/kernel.h>
50 #include <linux/string.h>
51 #include <linux/errno.h>
52 #include <linux/highmem.h>
53 #include <linux/slab.h>
55 #include <linux/socket.h>
56 #include <linux/sockios.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>
67 #include <net/protocol.h>
68 #include <net/route.h>
70 #include <linux/skbuff.h>
74 #include <net/checksum.h>
75 #include <net/inetpeer.h>
76 #include <net/inet_ecn.h>
77 #include <net/lwtunnel.h>
78 #include <linux/bpf-cgroup.h>
79 #include <linux/igmp.h>
80 #include <linux/netfilter_ipv4.h>
81 #include <linux/netfilter_bridge.h>
82 #include <linux/netlink.h>
83 #include <linux/tcp.h>
86 ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
88 int (*output)(struct net *, struct sock *, struct sk_buff *));
90 /* Generate a checksum for an outgoing IP datagram. */
91 void ip_send_check(struct iphdr *iph)
94 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
96 EXPORT_SYMBOL(ip_send_check);
98 int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
100 struct iphdr *iph = ip_hdr(skb);
102 iph->tot_len = htons(skb->len);
105 /* if egress device is enslaved to an L3 master device pass the
106 * skb to its handler for processing
108 skb = l3mdev_ip_out(sk, skb);
112 skb->protocol = htons(ETH_P_IP);
114 return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
115 net, sk, skb, NULL, skb_dst(skb)->dev,
119 int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
123 err = __ip_local_out(net, sk, skb);
124 if (likely(err == 1))
125 err = dst_output(net, sk, skb);
129 EXPORT_SYMBOL_GPL(ip_local_out);
131 static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
133 int ttl = inet->uc_ttl;
136 ttl = ip4_dst_hoplimit(dst);
141 * Add an ip header to a skbuff and send it out.
144 int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
145 __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
147 struct inet_sock *inet = inet_sk(sk);
148 struct rtable *rt = skb_rtable(skb);
149 struct net *net = sock_net(sk);
152 /* Build the IP header. */
153 skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
154 skb_reset_network_header(skb);
158 iph->tos = inet->tos;
159 iph->ttl = ip_select_ttl(inet, &rt->dst);
160 iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
162 iph->protocol = sk->sk_protocol;
163 /* Do not bother generating IPID for small packets (eg SYNACK) */
164 if (skb->len <= IPV4_MIN_MTU || ip_dont_fragment(sk, &rt->dst)) {
165 iph->frag_off = htons(IP_DF);
169 /* TCP packets here are SYNACK with fat IPv4/TCP options.
170 * Avoid using the hashed IP ident generator.
172 if (sk->sk_protocol == IPPROTO_TCP)
173 iph->id = (__force __be16)prandom_u32();
175 __ip_select_ident(net, iph, 1);
178 if (opt && opt->opt.optlen) {
179 iph->ihl += opt->opt.optlen>>2;
180 ip_options_build(skb, &opt->opt, daddr, rt, 0);
183 skb->priority = sk->sk_priority;
185 skb->mark = sk->sk_mark;
188 return ip_local_out(net, skb->sk, skb);
190 EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
192 static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
194 struct dst_entry *dst = skb_dst(skb);
195 struct rtable *rt = (struct rtable *)dst;
196 struct net_device *dev = dst->dev;
197 unsigned int hh_len = LL_RESERVED_SPACE(dev);
198 struct neighbour *neigh;
201 if (rt->rt_type == RTN_MULTICAST) {
202 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
203 } else if (rt->rt_type == RTN_BROADCAST)
204 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
206 /* Be paranoid, rather than too clever. */
207 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
208 struct sk_buff *skb2;
210 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
216 skb_set_owner_w(skb2, skb->sk);
221 if (lwtunnel_xmit_redirect(dst->lwtstate)) {
222 int res = lwtunnel_xmit(skb);
224 if (res < 0 || res == LWTUNNEL_XMIT_DONE)
229 nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
230 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
231 if (unlikely(!neigh))
232 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
233 if (!IS_ERR(neigh)) {
236 sock_confirm_neigh(skb, neigh);
237 res = neigh_output(neigh, skb);
239 rcu_read_unlock_bh();
242 rcu_read_unlock_bh();
244 net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
250 static int ip_finish_output_gso(struct net *net, struct sock *sk,
251 struct sk_buff *skb, unsigned int mtu)
253 netdev_features_t features;
254 struct sk_buff *segs;
257 /* common case: seglen is <= mtu
259 if (skb_gso_validate_mtu(skb, mtu))
260 return ip_finish_output2(net, sk, skb);
262 /* Slowpath - GSO segment length exceeds the egress MTU.
264 * This can happen in several cases:
265 * - Forwarding of a TCP GRO skb, when DF flag is not set.
266 * - Forwarding of an skb that arrived on a virtualization interface
267 * (virtio-net/vhost/tap) with TSO/GSO size set by other network
269 * - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an
270 * interface with a smaller MTU.
271 * - Arriving GRO skb (or GSO skb in a virtualized environment) that is
272 * bridged to a NETIF_F_TSO tunnel stacked over an interface with an
275 features = netif_skb_features(skb);
276 BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
277 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
278 if (IS_ERR_OR_NULL(segs)) {
286 struct sk_buff *nskb = segs->next;
290 err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
300 static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
305 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
311 #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
312 /* Policy lookup after SNAT yielded a new policy */
313 if (skb_dst(skb)->xfrm) {
314 IPCB(skb)->flags |= IPSKB_REROUTED;
315 return dst_output(net, sk, skb);
318 mtu = ip_skb_dst_mtu(sk, skb);
320 return ip_finish_output_gso(net, sk, skb, mtu);
322 if (skb->len > mtu || IPCB(skb)->frag_max_size)
323 return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
325 return ip_finish_output2(net, sk, skb);
328 static int ip_mc_finish_output(struct net *net, struct sock *sk,
333 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
339 return dev_loopback_xmit(net, sk, skb);
342 int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
344 struct rtable *rt = skb_rtable(skb);
345 struct net_device *dev = rt->dst.dev;
348 * If the indicated interface is up and running, send the packet.
350 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
353 skb->protocol = htons(ETH_P_IP);
356 * Multicasts are looped back for other local users
359 if (rt->rt_flags&RTCF_MULTICAST) {
361 #ifdef CONFIG_IP_MROUTE
362 /* Small optimization: do not loopback not local frames,
363 which returned after forwarding; they will be dropped
364 by ip_mr_input in any case.
365 Note, that local frames are looped back to be delivered
368 This check is duplicated in ip_mr_input at the moment.
371 ((rt->rt_flags & RTCF_LOCAL) ||
372 !(IPCB(skb)->flags & IPSKB_FORWARDED))
375 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
377 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
378 net, sk, newskb, NULL, newskb->dev,
379 ip_mc_finish_output);
382 /* Multicasts with ttl 0 must not go beyond the host */
384 if (ip_hdr(skb)->ttl == 0) {
390 if (rt->rt_flags&RTCF_BROADCAST) {
391 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
393 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
394 net, sk, newskb, NULL, newskb->dev,
395 ip_mc_finish_output);
398 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
399 net, sk, skb, NULL, skb->dev,
401 !(IPCB(skb)->flags & IPSKB_REROUTED));
404 int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
406 struct net_device *dev = skb_dst(skb)->dev;
408 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
411 skb->protocol = htons(ETH_P_IP);
413 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
414 net, sk, skb, NULL, dev,
416 !(IPCB(skb)->flags & IPSKB_REROUTED));
420 * copy saddr and daddr, possibly using 64bit load/stores
422 * iph->saddr = fl4->saddr;
423 * iph->daddr = fl4->daddr;
425 static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
427 BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
428 offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
430 iph->saddr = fl4->saddr;
431 iph->daddr = fl4->daddr;
434 /* Note: skb->sk can be different from sk, in case of tunnels */
435 int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
437 struct inet_sock *inet = inet_sk(sk);
438 struct net *net = sock_net(sk);
439 struct ip_options_rcu *inet_opt;
445 /* Skip all of this if the packet is already routed,
446 * f.e. by something like SCTP.
449 inet_opt = rcu_dereference(inet->inet_opt);
451 rt = skb_rtable(skb);
455 /* Make sure we can route this packet. */
456 rt = (struct rtable *)__sk_dst_check(sk, 0);
460 /* Use correct destination address if we have options. */
461 daddr = inet->inet_daddr;
462 if (inet_opt && inet_opt->opt.srr)
463 daddr = inet_opt->opt.faddr;
465 /* If this fails, retransmit mechanism of transport layer will
466 * keep trying until route appears or the connection times
469 rt = ip_route_output_ports(net, fl4, sk,
470 daddr, inet->inet_saddr,
475 sk->sk_bound_dev_if);
478 sk_setup_caps(sk, &rt->dst);
480 skb_dst_set_noref(skb, &rt->dst);
483 if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
486 /* OK, we know where to send it, allocate and build IP header. */
487 skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
488 skb_reset_network_header(skb);
490 *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
491 if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
492 iph->frag_off = htons(IP_DF);
495 iph->ttl = ip_select_ttl(inet, &rt->dst);
496 iph->protocol = sk->sk_protocol;
497 ip_copy_addrs(iph, fl4);
499 /* Transport layer set skb->h.foo itself. */
501 if (inet_opt && inet_opt->opt.optlen) {
502 iph->ihl += inet_opt->opt.optlen >> 2;
503 ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
506 ip_select_ident_segs(net, skb, sk,
507 skb_shinfo(skb)->gso_segs ?: 1);
509 /* TODO : should we use skb->sk here instead of sk ? */
510 skb->priority = sk->sk_priority;
511 skb->mark = sk->sk_mark;
513 res = ip_local_out(net, sk, skb);
519 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
521 return -EHOSTUNREACH;
523 EXPORT_SYMBOL(ip_queue_xmit);
525 static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
527 to->pkt_type = from->pkt_type;
528 to->priority = from->priority;
529 to->protocol = from->protocol;
530 to->skb_iif = from->skb_iif;
532 skb_dst_copy(to, from);
534 to->mark = from->mark;
536 skb_copy_hash(to, from);
538 /* Copy the flags to each fragment. */
539 IPCB(to)->flags = IPCB(from)->flags;
541 #ifdef CONFIG_NET_SCHED
542 to->tc_index = from->tc_index;
545 #if IS_ENABLED(CONFIG_IP_VS)
546 to->ipvs_property = from->ipvs_property;
548 skb_copy_secmark(to, from);
551 static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
553 int (*output)(struct net *, struct sock *, struct sk_buff *))
555 struct iphdr *iph = ip_hdr(skb);
557 if ((iph->frag_off & htons(IP_DF)) == 0)
558 return ip_do_fragment(net, sk, skb, output);
560 if (unlikely(!skb->ignore_df ||
561 (IPCB(skb)->frag_max_size &&
562 IPCB(skb)->frag_max_size > mtu))) {
563 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
564 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
570 return ip_do_fragment(net, sk, skb, output);
574 * This IP datagram is too large to be sent in one piece. Break it up into
575 * smaller pieces (each of size equal to IP header plus
576 * a block of the data of the original IP data part) that will yet fit in a
577 * single device frame, and queue such a frame for sending.
580 int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
581 int (*output)(struct net *, struct sock *, struct sk_buff *))
585 struct sk_buff *skb2;
586 unsigned int mtu, hlen, left, len, ll_rs;
588 __be16 not_last_frag;
589 struct rtable *rt = skb_rtable(skb);
592 /* for offloaded checksums cleanup checksum before fragmentation */
593 if (skb->ip_summed == CHECKSUM_PARTIAL &&
594 (err = skb_checksum_help(skb)))
598 * Point into the IP datagram header.
603 mtu = ip_skb_dst_mtu(sk, skb);
604 if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
605 mtu = IPCB(skb)->frag_max_size;
608 * Setup starting values.
612 mtu = mtu - hlen; /* Size of data space */
613 IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
614 ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
616 /* When frag_list is given, use it. First, check its validity:
617 * some transformers could create wrong frag_list or break existing
618 * one, it is not prohibited. In this case fall back to copying.
620 * LATER: this step can be merged to real generation of fragments,
621 * we can switch to copy when see the first bad fragment.
623 if (skb_has_frag_list(skb)) {
624 struct sk_buff *frag, *frag2;
625 unsigned int first_len = skb_pagelen(skb);
627 if (first_len - hlen > mtu ||
628 ((first_len - hlen) & 7) ||
629 ip_is_fragment(iph) ||
631 skb_headroom(skb) < ll_rs)
634 skb_walk_frags(skb, frag) {
635 /* Correct geometry. */
636 if (frag->len > mtu ||
637 ((frag->len & 7) && frag->next) ||
638 skb_headroom(frag) < hlen + ll_rs)
639 goto slow_path_clean;
641 /* Partially cloned skb? */
642 if (skb_shared(frag))
643 goto slow_path_clean;
648 frag->destructor = sock_wfree;
650 skb->truesize -= frag->truesize;
653 /* Everything is OK. Generate! */
657 frag = skb_shinfo(skb)->frag_list;
658 skb_frag_list_init(skb);
659 skb->data_len = first_len - skb_headlen(skb);
660 skb->len = first_len;
661 iph->tot_len = htons(first_len);
662 iph->frag_off = htons(IP_MF);
666 /* Prepare header of the next frame,
667 * before previous one went down. */
669 frag->ip_summed = CHECKSUM_NONE;
670 skb_reset_transport_header(frag);
671 __skb_push(frag, hlen);
672 skb_reset_network_header(frag);
673 memcpy(skb_network_header(frag), iph, hlen);
675 iph->tot_len = htons(frag->len);
676 ip_copy_metadata(frag, skb);
678 ip_options_fragment(frag);
679 offset += skb->len - hlen;
680 iph->frag_off = htons(offset>>3);
682 iph->frag_off |= htons(IP_MF);
683 /* Ready, complete checksum */
687 err = output(net, sk, skb);
690 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
700 IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
709 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
713 skb_walk_frags(skb, frag2) {
717 frag2->destructor = NULL;
718 skb->truesize += frag2->truesize;
725 left = skb->len - hlen; /* Space per frame */
726 ptr = hlen; /* Where to start from */
729 * Fragment the datagram.
732 offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
733 not_last_frag = iph->frag_off & htons(IP_MF);
736 * Keep copying data until we run out.
741 /* IF: it doesn't fit, use 'mtu' - the data space left */
744 /* IF: we are not sending up to and including the packet end
745 then align the next start on an eight byte boundary */
750 /* Allocate buffer */
751 skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
758 * Set up data on packet
761 ip_copy_metadata(skb2, skb);
762 skb_reserve(skb2, ll_rs);
763 skb_put(skb2, len + hlen);
764 skb_reset_network_header(skb2);
765 skb2->transport_header = skb2->network_header + hlen;
768 * Charge the memory for the fragment to any owner
773 skb_set_owner_w(skb2, skb->sk);
776 * Copy the packet header into the new buffer.
779 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
782 * Copy a block of the IP datagram.
784 if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
789 * Fill in the new header fields.
792 iph->frag_off = htons((offset >> 3));
794 if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
795 iph->frag_off |= htons(IP_DF);
797 /* ANK: dirty, but effective trick. Upgrade options only if
798 * the segment to be fragmented was THE FIRST (otherwise,
799 * options are already fixed) and make it ONCE
800 * on the initial skb, so that all the following fragments
801 * will inherit fixed options.
804 ip_options_fragment(skb);
807 * Added AC : If we are fragmenting a fragment that's not the
808 * last fragment then keep MF on each bit
810 if (left > 0 || not_last_frag)
811 iph->frag_off |= htons(IP_MF);
816 * Put this fragment into the sending queue.
818 iph->tot_len = htons(len + hlen);
822 err = output(net, sk, skb2);
826 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
829 IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
834 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
837 EXPORT_SYMBOL(ip_do_fragment);
840 ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
842 struct msghdr *msg = from;
844 if (skb->ip_summed == CHECKSUM_PARTIAL) {
845 if (!copy_from_iter_full(to, len, &msg->msg_iter))
849 if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
851 skb->csum = csum_block_add(skb->csum, csum, odd);
855 EXPORT_SYMBOL(ip_generic_getfrag);
858 csum_page(struct page *page, int offset, int copy)
863 csum = csum_partial(kaddr + offset, copy, 0);
868 static int __ip_append_data(struct sock *sk,
870 struct sk_buff_head *queue,
871 struct inet_cork *cork,
872 struct page_frag *pfrag,
873 int getfrag(void *from, char *to, int offset,
874 int len, int odd, struct sk_buff *skb),
875 void *from, int length, int transhdrlen,
878 struct inet_sock *inet = inet_sk(sk);
881 struct ip_options *opt = cork->opt;
888 unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
889 int csummode = CHECKSUM_NONE;
890 struct rtable *rt = (struct rtable *)cork->dst;
893 skb = skb_peek_tail(queue);
895 exthdrlen = !skb ? rt->dst.header_len : 0;
896 mtu = cork->fragsize;
897 if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
898 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
899 tskey = sk->sk_tskey++;
901 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
903 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
904 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
905 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
907 if (cork->length + length > maxnonfragsize - fragheaderlen) {
908 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
909 mtu - (opt ? opt->optlen : 0));
914 * transhdrlen > 0 means that this is the first fragment and we wish
915 * it won't be fragmented in the future.
918 length + fragheaderlen <= mtu &&
919 rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
920 !(flags & MSG_MORE) &&
922 csummode = CHECKSUM_PARTIAL;
924 cork->length += length;
926 /* So, what's going on in the loop below?
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.
937 /* Check if the remaining data fits into current packet. */
938 copy = mtu - skb->len;
940 copy = maxfraglen - skb->len;
943 unsigned int datalen;
944 unsigned int fraglen;
945 unsigned int fraggap;
946 unsigned int alloclen;
947 struct sk_buff *skb_prev;
951 fraggap = skb_prev->len - maxfraglen;
956 * If remaining data exceeds the mtu,
957 * we know we need more fragment(s).
959 datalen = length + fraggap;
960 if (datalen > mtu - fragheaderlen)
961 datalen = maxfraglen - fragheaderlen;
962 fraglen = datalen + fragheaderlen;
964 if ((flags & MSG_MORE) &&
965 !(rt->dst.dev->features&NETIF_F_SG))
970 alloclen += exthdrlen;
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
977 if (datalen == length + fraggap)
978 alloclen += rt->dst.trailer_len;
981 skb = sock_alloc_send_skb(sk,
982 alloclen + hh_len + 15,
983 (flags & MSG_DONTWAIT), &err);
986 if (refcount_read(&sk->sk_wmem_alloc) <=
988 skb = sock_wmalloc(sk,
989 alloclen + hh_len + 15, 1,
998 * Fill in the control structures
1000 skb->ip_summed = csummode;
1002 skb_reserve(skb, hh_len);
1004 /* only the initial fragment is time stamped */
1005 skb_shinfo(skb)->tx_flags = cork->tx_flags;
1007 skb_shinfo(skb)->tskey = tskey;
1011 * Find where to start putting bytes.
1013 data = skb_put(skb, fraglen + exthdrlen);
1014 skb_set_network_header(skb, exthdrlen);
1015 skb->transport_header = (skb->network_header +
1017 data += fragheaderlen + exthdrlen;
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,
1026 pskb_trim_unique(skb_prev, maxfraglen);
1029 copy = datalen - transhdrlen - fraggap;
1030 if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1037 length -= datalen - fraggap;
1040 csummode = CHECKSUM_NONE;
1042 if ((flags & MSG_CONFIRM) && !skb_prev)
1043 skb_set_dst_pending_confirm(skb, 1);
1046 * Put the packet on the pending queue.
1048 __skb_queue_tail(queue, skb);
1055 if (!(rt->dst.dev->features&NETIF_F_SG) &&
1056 skb_tailroom(skb) >= copy) {
1060 if (getfrag(from, skb_put(skb, copy),
1061 offset, copy, off, skb) < 0) {
1062 __skb_trim(skb, off);
1067 int i = skb_shinfo(skb)->nr_frags;
1070 if (!sk_page_frag_refill(sk, pfrag))
1073 if (!skb_can_coalesce(skb, i, pfrag->page,
1076 if (i == MAX_SKB_FRAGS)
1079 __skb_fill_page_desc(skb, i, pfrag->page,
1081 skb_shinfo(skb)->nr_frags = ++i;
1082 get_page(pfrag->page);
1084 copy = min_t(int, copy, pfrag->size - pfrag->offset);
1086 page_address(pfrag->page) + pfrag->offset,
1087 offset, copy, skb->len, skb) < 0)
1090 pfrag->offset += copy;
1091 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1093 skb->data_len += copy;
1094 skb->truesize += copy;
1095 refcount_add(copy, &sk->sk_wmem_alloc);
1106 cork->length -= length;
1107 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1111 static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1112 struct ipcm_cookie *ipc, struct rtable **rtp)
1114 struct ip_options_rcu *opt;
1118 * setup for corking.
1123 cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1125 if (unlikely(!cork->opt))
1128 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
1129 cork->flags |= IPCORK_OPT;
1130 cork->addr = ipc->addr;
1136 cork->fragsize = ip_sk_use_pmtu(sk) ?
1137 dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
1139 if (!inetdev_valid_mtu(cork->fragsize))
1140 return -ENETUNREACH;
1142 cork->dst = &rt->dst;
1143 /* We stole this route, caller should not release it. */
1147 cork->ttl = ipc->ttl;
1148 cork->tos = ipc->tos;
1149 cork->priority = ipc->priority;
1150 cork->tx_flags = ipc->tx_flags;
1156 * ip_append_data() and ip_append_page() can make one large IP datagram
1157 * from many pieces of data. Each pieces will be holded on the socket
1158 * until ip_push_pending_frames() is called. Each piece can be a page
1161 * Not only UDP, other transport protocols - e.g. raw sockets - can use
1162 * this interface potentially.
1164 * LATER: length must be adjusted by pad at tail, when it is required.
1166 int ip_append_data(struct sock *sk, struct flowi4 *fl4,
1167 int getfrag(void *from, char *to, int offset, int len,
1168 int odd, struct sk_buff *skb),
1169 void *from, int length, int transhdrlen,
1170 struct ipcm_cookie *ipc, struct rtable **rtp,
1173 struct inet_sock *inet = inet_sk(sk);
1176 if (flags&MSG_PROBE)
1179 if (skb_queue_empty(&sk->sk_write_queue)) {
1180 err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
1187 return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1188 sk_page_frag(sk), getfrag,
1189 from, length, transhdrlen, flags);
1192 ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1193 int offset, size_t size, int flags)
1195 struct inet_sock *inet = inet_sk(sk);
1196 struct sk_buff *skb;
1198 struct ip_options *opt = NULL;
1199 struct inet_cork *cork;
1204 unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
1209 if (flags&MSG_PROBE)
1212 if (skb_queue_empty(&sk->sk_write_queue))
1215 cork = &inet->cork.base;
1216 rt = (struct rtable *)cork->dst;
1217 if (cork->flags & IPCORK_OPT)
1220 if (!(rt->dst.dev->features&NETIF_F_SG))
1223 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1224 mtu = cork->fragsize;
1226 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1227 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
1228 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
1230 if (cork->length + size > maxnonfragsize - fragheaderlen) {
1231 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1232 mtu - (opt ? opt->optlen : 0));
1236 skb = skb_peek_tail(&sk->sk_write_queue);
1240 cork->length += size;
1243 /* Check if the remaining data fits into current packet. */
1244 len = mtu - skb->len;
1246 len = maxfraglen - skb->len;
1249 struct sk_buff *skb_prev;
1253 fraggap = skb_prev->len - maxfraglen;
1255 alloclen = fragheaderlen + hh_len + fraggap + 15;
1256 skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1257 if (unlikely(!skb)) {
1263 * Fill in the control structures
1265 skb->ip_summed = CHECKSUM_NONE;
1267 skb_reserve(skb, hh_len);
1270 * Find where to start putting bytes.
1272 skb_put(skb, fragheaderlen + fraggap);
1273 skb_reset_network_header(skb);
1274 skb->transport_header = (skb->network_header +
1277 skb->csum = skb_copy_and_csum_bits(skb_prev,
1279 skb_transport_header(skb),
1281 skb_prev->csum = csum_sub(skb_prev->csum,
1283 pskb_trim_unique(skb_prev, maxfraglen);
1287 * Put the packet on the pending queue.
1289 __skb_queue_tail(&sk->sk_write_queue, skb);
1296 if (skb_append_pagefrags(skb, page, offset, len)) {
1301 if (skb->ip_summed == CHECKSUM_NONE) {
1303 csum = csum_page(page, offset, len);
1304 skb->csum = csum_block_add(skb->csum, csum, skb->len);
1308 skb->data_len += len;
1309 skb->truesize += len;
1310 refcount_add(len, &sk->sk_wmem_alloc);
1317 cork->length -= size;
1318 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1322 static void ip_cork_release(struct inet_cork *cork)
1324 cork->flags &= ~IPCORK_OPT;
1327 dst_release(cork->dst);
1332 * Combined all pending IP fragments on the socket as one IP datagram
1333 * and push them out.
1335 struct sk_buff *__ip_make_skb(struct sock *sk,
1337 struct sk_buff_head *queue,
1338 struct inet_cork *cork)
1340 struct sk_buff *skb, *tmp_skb;
1341 struct sk_buff **tail_skb;
1342 struct inet_sock *inet = inet_sk(sk);
1343 struct net *net = sock_net(sk);
1344 struct ip_options *opt = NULL;
1345 struct rtable *rt = (struct rtable *)cork->dst;
1350 skb = __skb_dequeue(queue);
1353 tail_skb = &(skb_shinfo(skb)->frag_list);
1355 /* move skb->data to ip header from ext header */
1356 if (skb->data < skb_network_header(skb))
1357 __skb_pull(skb, skb_network_offset(skb));
1358 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1359 __skb_pull(tmp_skb, skb_network_header_len(skb));
1360 *tail_skb = tmp_skb;
1361 tail_skb = &(tmp_skb->next);
1362 skb->len += tmp_skb->len;
1363 skb->data_len += tmp_skb->len;
1364 skb->truesize += tmp_skb->truesize;
1365 tmp_skb->destructor = NULL;
1369 /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1370 * to fragment the frame generated here. No matter, what transforms
1371 * how transforms change size of the packet, it will come out.
1373 skb->ignore_df = ip_sk_ignore_df(sk);
1375 /* DF bit is set when we want to see DF on outgoing frames.
1376 * If ignore_df is set too, we still allow to fragment this frame
1378 if (inet->pmtudisc == IP_PMTUDISC_DO ||
1379 inet->pmtudisc == IP_PMTUDISC_PROBE ||
1380 (skb->len <= dst_mtu(&rt->dst) &&
1381 ip_dont_fragment(sk, &rt->dst)))
1384 if (cork->flags & IPCORK_OPT)
1389 else if (rt->rt_type == RTN_MULTICAST)
1392 ttl = ip_select_ttl(inet, &rt->dst);
1397 iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1400 iph->protocol = sk->sk_protocol;
1401 ip_copy_addrs(iph, fl4);
1402 ip_select_ident(net, skb, sk);
1405 iph->ihl += opt->optlen>>2;
1406 ip_options_build(skb, opt, cork->addr, rt, 0);
1409 skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1410 skb->mark = sk->sk_mark;
1412 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1416 skb_dst_set(skb, &rt->dst);
1418 if (iph->protocol == IPPROTO_ICMP)
1419 icmp_out_count(net, ((struct icmphdr *)
1420 skb_transport_header(skb))->type);
1422 ip_cork_release(cork);
1427 int ip_send_skb(struct net *net, struct sk_buff *skb)
1431 err = ip_local_out(net, skb->sk, skb);
1434 err = net_xmit_errno(err);
1436 IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
1442 int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
1444 struct sk_buff *skb;
1446 skb = ip_finish_skb(sk, fl4);
1450 /* Netfilter gets whole the not fragmented skb. */
1451 return ip_send_skb(sock_net(sk), skb);
1455 * Throw away all pending data on the socket.
1457 static void __ip_flush_pending_frames(struct sock *sk,
1458 struct sk_buff_head *queue,
1459 struct inet_cork *cork)
1461 struct sk_buff *skb;
1463 while ((skb = __skb_dequeue_tail(queue)) != NULL)
1466 ip_cork_release(cork);
1469 void ip_flush_pending_frames(struct sock *sk)
1471 __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
1474 struct sk_buff *ip_make_skb(struct sock *sk,
1476 int getfrag(void *from, char *to, int offset,
1477 int len, int odd, struct sk_buff *skb),
1478 void *from, int length, int transhdrlen,
1479 struct ipcm_cookie *ipc, struct rtable **rtp,
1482 struct inet_cork cork;
1483 struct sk_buff_head queue;
1486 if (flags & MSG_PROBE)
1489 __skb_queue_head_init(&queue);
1494 err = ip_setup_cork(sk, &cork, ipc, rtp);
1496 return ERR_PTR(err);
1498 err = __ip_append_data(sk, fl4, &queue, &cork,
1499 ¤t->task_frag, getfrag,
1500 from, length, transhdrlen, flags);
1502 __ip_flush_pending_frames(sk, &queue, &cork);
1503 return ERR_PTR(err);
1506 return __ip_make_skb(sk, fl4, &queue, &cork);
1510 * Fetch data from kernel space and fill in checksum if needed.
1512 static int ip_reply_glue_bits(void *dptr, char *to, int offset,
1513 int len, int odd, struct sk_buff *skb)
1517 csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
1518 skb->csum = csum_block_add(skb->csum, csum, odd);
1523 * Generic function to send a packet as reply to another packet.
1524 * Used to send some TCP resets/acks so far.
1526 void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
1527 const struct ip_options *sopt,
1528 __be32 daddr, __be32 saddr,
1529 const struct ip_reply_arg *arg,
1532 struct ip_options_data replyopts;
1533 struct ipcm_cookie ipc;
1535 struct rtable *rt = skb_rtable(skb);
1536 struct net *net = sock_net(sk);
1537 struct sk_buff *nskb;
1541 if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt))
1550 if (replyopts.opt.opt.optlen) {
1551 ipc.opt = &replyopts.opt;
1553 if (replyopts.opt.opt.srr)
1554 daddr = replyopts.opt.opt.faddr;
1557 oif = arg->bound_dev_if;
1558 if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
1561 flowi4_init_output(&fl4, oif,
1562 IP4_REPLY_MARK(net, skb->mark),
1564 RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1565 ip_reply_arg_flowi_flags(arg),
1567 tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1569 security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1570 rt = ip_route_output_key(net, &fl4);
1574 inet_sk(sk)->tos = arg->tos & ~INET_ECN_MASK;
1576 sk->sk_priority = skb->priority;
1577 sk->sk_protocol = ip_hdr(skb)->protocol;
1578 sk->sk_bound_dev_if = arg->bound_dev_if;
1579 sk->sk_sndbuf = sysctl_wmem_default;
1580 sk->sk_mark = fl4.flowi4_mark;
1581 err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
1582 len, 0, &ipc, &rt, MSG_DONTWAIT);
1583 if (unlikely(err)) {
1584 ip_flush_pending_frames(sk);
1588 nskb = skb_peek(&sk->sk_write_queue);
1590 if (arg->csumoffset >= 0)
1591 *((__sum16 *)skb_transport_header(nskb) +
1592 arg->csumoffset) = csum_fold(csum_add(nskb->csum,
1594 nskb->ip_summed = CHECKSUM_NONE;
1595 ip_push_pending_frames(sk, &fl4);
1601 void __init ip_init(void)
1606 #if defined(CONFIG_IP_MULTICAST)