Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * The Internet Protocol (IP) output module. |
| 8 | * |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 9 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 11 | * Donald Becker, <becker@super.org> |
| 12 | * Alan Cox, <Alan.Cox@linux.org> |
| 13 | * Richard Underwood |
| 14 | * Stefan Becker, <stefanb@yello.ping.de> |
| 15 | * Jorge Cwik, <jorge@laser.satlink.net> |
| 16 | * Arnt Gulbrandsen, <agulbra@nvg.unit.no> |
| 17 | * Hirokazu Takahashi, <taka@valinux.co.jp> |
| 18 | * |
| 19 | * See ip_input.c for original log |
| 20 | * |
| 21 | * Fixes: |
| 22 | * Alan Cox : Missing nonblock feature in ip_build_xmit. |
| 23 | * Mike Kilburn : htons() missing in ip_build_xmit. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 24 | * Bradford Johnson: Fix faulty handling of some frames when |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * no route is found. |
| 26 | * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit |
| 27 | * (in case if packet not accepted by |
| 28 | * output firewall rules) |
| 29 | * Mike McLagan : Routing by source |
| 30 | * Alexey Kuznetsov: use new route cache |
| 31 | * Andi Kleen: Fix broken PMTU recovery and remove |
| 32 | * some redundant tests. |
| 33 | * Vitaly E. Lavrov : Transparent proxy revived after year coma. |
| 34 | * Andi Kleen : Replace ip_reply with ip_send_reply. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 35 | * Andi Kleen : Split fast and slow ip_build_xmit path |
| 36 | * for decreased register pressure on x86 |
| 37 | * and more readibility. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * Marc Boucher : When call_out_firewall returns FW_QUEUE, |
| 39 | * silently drop skb instead of failing with -EPERM. |
| 40 | * Detlev Wengorz : Copy protocol for fragments. |
| 41 | * Hirokazu Takahashi: HW checksumming for outgoing UDP |
| 42 | * datagrams. |
| 43 | * Hirokazu Takahashi: sendfile() on UDP works now. |
| 44 | */ |
| 45 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 46 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/module.h> |
| 48 | #include <linux/types.h> |
| 49 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/mm.h> |
| 51 | #include <linux/string.h> |
| 52 | #include <linux/errno.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 53 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #include <linux/socket.h> |
| 57 | #include <linux/sockios.h> |
| 58 | #include <linux/in.h> |
| 59 | #include <linux/inet.h> |
| 60 | #include <linux/netdevice.h> |
| 61 | #include <linux/etherdevice.h> |
| 62 | #include <linux/proc_fs.h> |
| 63 | #include <linux/stat.h> |
| 64 | #include <linux/init.h> |
| 65 | |
| 66 | #include <net/snmp.h> |
| 67 | #include <net/ip.h> |
| 68 | #include <net/protocol.h> |
| 69 | #include <net/route.h> |
Patrick McHardy | cfacb05 | 2006-01-08 22:36:54 -0800 | [diff] [blame] | 70 | #include <net/xfrm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #include <linux/skbuff.h> |
| 72 | #include <net/sock.h> |
| 73 | #include <net/arp.h> |
| 74 | #include <net/icmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #include <net/checksum.h> |
| 76 | #include <net/inetpeer.h> |
Wei Wang | ba9e04a | 2020-09-08 14:09:34 -0700 | [diff] [blame] | 77 | #include <net/inet_ecn.h> |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 78 | #include <net/lwtunnel.h> |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 79 | #include <linux/bpf-cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <linux/igmp.h> |
| 81 | #include <linux/netfilter_ipv4.h> |
| 82 | #include <linux/netfilter_bridge.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <linux/netlink.h> |
Arnaldo Carvalho de Melo | 6cbb0df | 2005-08-09 19:49:02 -0700 | [diff] [blame] | 84 | #include <linux/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 86 | static int |
| 87 | ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, |
| 88 | unsigned int mtu, |
| 89 | int (*output)(struct net *, struct sock *, struct sk_buff *)); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* Generate a checksum for an outgoing IP datagram. */ |
Denis Efremov | 2fbd967 | 2013-05-08 23:19:42 +0000 | [diff] [blame] | 92 | void ip_send_check(struct iphdr *iph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | iph->check = 0; |
| 95 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 96 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 97 | EXPORT_SYMBOL(ip_send_check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Eric W. Biederman | cf91a99 | 2015-10-07 16:48:45 -0500 | [diff] [blame] | 99 | int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 100 | { |
| 101 | struct iphdr *iph = ip_hdr(skb); |
| 102 | |
| 103 | iph->tot_len = htons(skb->len); |
| 104 | ip_send_check(iph); |
David Ahern | a8e3e1a | 2016-09-10 12:09:53 -0700 | [diff] [blame] | 105 | |
| 106 | /* if egress device is enslaved to an L3 master device pass the |
| 107 | * skb to its handler for processing |
| 108 | */ |
| 109 | skb = l3mdev_ip_out(sk, skb); |
| 110 | if (unlikely(!skb)) |
| 111 | return 0; |
| 112 | |
Eli Cooper | f418043 | 2016-12-01 10:05:10 +0800 | [diff] [blame] | 113 | skb->protocol = htons(ETH_P_IP); |
| 114 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 115 | return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, |
| 116 | net, sk, skb, NULL, skb_dst(skb)->dev, |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 117 | dst_output); |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Eric W. Biederman | 33224b1 | 2015-10-07 16:48:46 -0500 | [diff] [blame] | 120 | int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb) |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 121 | { |
| 122 | int err; |
| 123 | |
Eric W. Biederman | cf91a99 | 2015-10-07 16:48:45 -0500 | [diff] [blame] | 124 | err = __ip_local_out(net, sk, skb); |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 125 | if (likely(err == 1)) |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 126 | err = dst_output(net, sk, skb); |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 127 | |
| 128 | return err; |
| 129 | } |
Eric W. Biederman | e2cb77d | 2015-10-07 16:48:38 -0500 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(ip_local_out); |
Herbert Xu | c439cb2 | 2008-01-11 19:14:00 -0800 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst) |
| 133 | { |
| 134 | int ttl = inet->uc_ttl; |
| 135 | |
| 136 | if (ttl < 0) |
David S. Miller | 323e126 | 2010-12-12 21:55:08 -0800 | [diff] [blame] | 137 | ttl = ip4_dst_hoplimit(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return ttl; |
| 139 | } |
| 140 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 141 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * Add an ip header to a skbuff and send it out. |
| 143 | * |
| 144 | */ |
Eric Dumazet | cfe673b | 2015-09-25 07:39:16 -0700 | [diff] [blame] | 145 | int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk, |
Wei Wang | de033b7d | 2020-09-09 17:50:47 -0700 | [diff] [blame] | 146 | __be32 saddr, __be32 daddr, struct ip_options_rcu *opt, |
| 147 | u8 tos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
| 149 | struct inet_sock *inet = inet_sk(sk); |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 150 | struct rtable *rt = skb_rtable(skb); |
Eric W. Biederman | 77589ce | 2015-10-07 16:48:42 -0500 | [diff] [blame] | 151 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | struct iphdr *iph; |
| 153 | |
| 154 | /* Build the IP header. */ |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 155 | skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0)); |
Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 156 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 157 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | iph->version = 4; |
| 159 | iph->ihl = 5; |
Wei Wang | de033b7d | 2020-09-09 17:50:47 -0700 | [diff] [blame] | 160 | iph->tos = tos; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 161 | iph->ttl = ip_select_ttl(inet, &rt->dst); |
David S. Miller | dd927a2 | 2011-05-04 12:03:30 -0700 | [diff] [blame] | 162 | iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); |
| 163 | iph->saddr = saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | iph->protocol = sk->sk_protocol; |
Eric Dumazet | cfe673b | 2015-09-25 07:39:16 -0700 | [diff] [blame] | 165 | if (ip_dont_fragment(sk, &rt->dst)) { |
| 166 | iph->frag_off = htons(IP_DF); |
| 167 | iph->id = 0; |
| 168 | } else { |
| 169 | iph->frag_off = 0; |
Eric W. Biederman | 77589ce | 2015-10-07 16:48:42 -0500 | [diff] [blame] | 170 | __ip_select_ident(net, iph, 1); |
Eric Dumazet | cfe673b | 2015-09-25 07:39:16 -0700 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 173 | if (opt && opt->opt.optlen) { |
| 174 | iph->ihl += opt->opt.optlen>>2; |
| 175 | ip_options_build(skb, &opt->opt, daddr, rt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | skb->priority = sk->sk_priority; |
Jamal Hadi Salim | e05a90e | 2017-07-03 09:51:50 -0400 | [diff] [blame] | 179 | if (!skb->mark) |
| 180 | skb->mark = sk->sk_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | /* Send it out. */ |
Eric W. Biederman | 33224b1 | 2015-10-07 16:48:46 -0500 | [diff] [blame] | 183 | return ip_local_out(net, skb->sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
Arnaldo Carvalho de Melo | d8c97a9 | 2005-08-09 20:12:12 -0700 | [diff] [blame] | 185 | EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); |
| 186 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 187 | static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 189 | struct dst_entry *dst = skb_dst(skb); |
Mitsuru Chinen | 80787eb | 2007-04-30 00:48:20 -0700 | [diff] [blame] | 190 | struct rtable *rt = (struct rtable *)dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | struct net_device *dev = dst->dev; |
Chuck Lever | c2636b4 | 2007-10-23 21:07:32 -0700 | [diff] [blame] | 192 | unsigned int hh_len = LL_RESERVED_SPACE(dev); |
David S. Miller | f6b72b62 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 193 | struct neighbour *neigh; |
David Ahern | 5c9f7c1 | 2019-04-05 16:30:34 -0700 | [diff] [blame] | 194 | bool is_v6gw = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 196 | if (rt->rt_type == RTN_MULTICAST) { |
Eric W. Biederman | 4ba1bf4 | 2015-09-15 20:04:01 -0500 | [diff] [blame] | 197 | IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len); |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 198 | } else if (rt->rt_type == RTN_BROADCAST) |
Eric W. Biederman | 4ba1bf4 | 2015-09-15 20:04:01 -0500 | [diff] [blame] | 199 | IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len); |
Mitsuru Chinen | 80787eb | 2007-04-30 00:48:20 -0700 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* Be paranoid, rather than too clever. */ |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 202 | if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct sk_buff *skb2; |
| 204 | |
| 205 | skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 206 | if (!skb2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | kfree_skb(skb); |
| 208 | return -ENOMEM; |
| 209 | } |
| 210 | if (skb->sk) |
| 211 | skb_set_owner_w(skb2, skb->sk); |
Eric Dumazet | 5d0ba55 | 2012-06-04 01:17:19 +0000 | [diff] [blame] | 212 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | skb = skb2; |
| 214 | } |
| 215 | |
Roopa Prabhu | 14972cb | 2016-08-24 20:10:43 -0700 | [diff] [blame] | 216 | if (lwtunnel_xmit_redirect(dst->lwtstate)) { |
| 217 | int res = lwtunnel_xmit(skb); |
| 218 | |
| 219 | if (res < 0 || res == LWTUNNEL_XMIT_DONE) |
| 220 | return res; |
| 221 | } |
| 222 | |
David S. Miller | a263b30 | 2012-07-02 02:02:15 -0700 | [diff] [blame] | 223 | rcu_read_lock_bh(); |
David Ahern | 5c9f7c1 | 2019-04-05 16:30:34 -0700 | [diff] [blame] | 224 | neigh = ip_neigh_for_gw(rt, skb, &is_v6gw); |
Vasiliy Kulikov | 9871f1a | 2012-08-06 03:55:29 +0000 | [diff] [blame] | 225 | if (!IS_ERR(neigh)) { |
Julian Anastasov | 4ff0620 | 2017-02-06 23:14:12 +0200 | [diff] [blame] | 226 | int res; |
| 227 | |
| 228 | sock_confirm_neigh(skb, neigh); |
David Ahern | 5c9f7c1 | 2019-04-05 16:30:34 -0700 | [diff] [blame] | 229 | /* if crossing protocols, can not use the cached header */ |
| 230 | res = neigh_output(neigh, skb, is_v6gw); |
David S. Miller | a263b30 | 2012-07-02 02:02:15 -0700 | [diff] [blame] | 231 | rcu_read_unlock_bh(); |
Eric Dumazet | f2c31e3 | 2011-07-29 19:00:53 +0000 | [diff] [blame] | 232 | return res; |
| 233 | } |
David S. Miller | a263b30 | 2012-07-02 02:02:15 -0700 | [diff] [blame] | 234 | rcu_read_unlock_bh(); |
David S. Miller | 05e3aa0 | 2011-07-16 17:26:00 -0700 | [diff] [blame] | 235 | |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 236 | net_dbg_ratelimited("%s: No header cache and no neighbour!\n", |
| 237 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | kfree_skb(skb); |
| 239 | return -EINVAL; |
| 240 | } |
| 241 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 242 | static int ip_finish_output_gso(struct net *net, struct sock *sk, |
| 243 | struct sk_buff *skb, unsigned int mtu) |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 244 | { |
Jason A. Donenfeld | 88bebdf | 2020-01-13 18:42:31 -0500 | [diff] [blame] | 245 | struct sk_buff *segs, *nskb; |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 246 | netdev_features_t features; |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 247 | int ret = 0; |
| 248 | |
Lance Richardson | 9ee6c5d | 2016-11-02 16:36:17 -0400 | [diff] [blame] | 249 | /* common case: seglen is <= mtu |
Shmulik Ladkani | 359ebda | 2016-07-18 14:49:33 +0300 | [diff] [blame] | 250 | */ |
Daniel Axtens | 779b793 | 2018-03-01 17:13:37 +1100 | [diff] [blame] | 251 | if (skb_gso_validate_network_len(skb, mtu)) |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 252 | return ip_finish_output2(net, sk, skb); |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 253 | |
Lance Richardson | 0ace81e | 2016-11-09 15:04:39 -0500 | [diff] [blame] | 254 | /* Slowpath - GSO segment length exceeds the egress MTU. |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 255 | * |
Lance Richardson | 0ace81e | 2016-11-09 15:04:39 -0500 | [diff] [blame] | 256 | * This can happen in several cases: |
| 257 | * - Forwarding of a TCP GRO skb, when DF flag is not set. |
| 258 | * - Forwarding of an skb that arrived on a virtualization interface |
| 259 | * (virtio-net/vhost/tap) with TSO/GSO size set by other network |
| 260 | * stack. |
| 261 | * - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an |
| 262 | * interface with a smaller MTU. |
| 263 | * - Arriving GRO skb (or GSO skb in a virtualized environment) that is |
| 264 | * bridged to a NETIF_F_TSO tunnel stacked over an interface with an |
| 265 | * insufficent MTU. |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 266 | */ |
| 267 | features = netif_skb_features(skb); |
Cambda Zhu | a08e7fd | 2020-03-26 15:33:14 +0800 | [diff] [blame] | 268 | BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET); |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 269 | segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); |
Florian Westphal | 330966e | 2014-10-20 13:49:17 +0200 | [diff] [blame] | 270 | if (IS_ERR_OR_NULL(segs)) { |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 271 | kfree_skb(skb); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | |
| 275 | consume_skb(skb); |
| 276 | |
Jason A. Donenfeld | 88bebdf | 2020-01-13 18:42:31 -0500 | [diff] [blame] | 277 | skb_list_walk_safe(segs, segs, nskb) { |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 278 | int err; |
| 279 | |
David S. Miller | a8305bf | 2018-07-29 20:42:53 -0700 | [diff] [blame] | 280 | skb_mark_not_on_list(segs); |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 281 | err = ip_fragment(net, sk, segs, mtu, ip_finish_output2); |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 282 | |
| 283 | if (err && ret == 0) |
| 284 | ret = err; |
Jason A. Donenfeld | 88bebdf | 2020-01-13 18:42:31 -0500 | [diff] [blame] | 285 | } |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
brakmo | 956fe21 | 2019-05-28 16:59:38 -0700 | [diff] [blame] | 290 | static int __ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Florian Westphal | c5501eb | 2015-05-22 16:32:50 +0200 | [diff] [blame] | 292 | unsigned int mtu; |
| 293 | |
Patrick McHardy | 5c901da | 2006-01-06 23:05:36 -0800 | [diff] [blame] | 294 | #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) |
| 295 | /* Policy lookup after SNAT yielded a new policy */ |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 296 | if (skb_dst(skb)->xfrm) { |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 297 | IPCB(skb)->flags |= IPSKB_REROUTED; |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 298 | return dst_output(net, sk, skb); |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 299 | } |
Patrick McHardy | 5c901da | 2006-01-06 23:05:36 -0800 | [diff] [blame] | 300 | #endif |
Shmulik Ladkani | fedbb6b4 | 2016-06-29 21:47:03 +0300 | [diff] [blame] | 301 | mtu = ip_skb_dst_mtu(sk, skb); |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 302 | if (skb_is_gso(skb)) |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 303 | return ip_finish_output_gso(net, sk, skb, mtu); |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 304 | |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 305 | if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU)) |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 306 | return ip_fragment(net, sk, skb, mtu, ip_finish_output2); |
Florian Westphal | c7ba65d | 2014-05-05 15:00:43 +0200 | [diff] [blame] | 307 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 308 | return ip_finish_output2(net, sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
brakmo | 956fe21 | 2019-05-28 16:59:38 -0700 | [diff] [blame] | 311 | static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 312 | { |
| 313 | int ret; |
| 314 | |
| 315 | ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); |
| 316 | switch (ret) { |
| 317 | case NET_XMIT_SUCCESS: |
| 318 | return __ip_finish_output(net, sk, skb); |
| 319 | case NET_XMIT_CN: |
| 320 | return __ip_finish_output(net, sk, skb) ? : ret; |
| 321 | default: |
| 322 | kfree_skb(skb); |
| 323 | return ret; |
| 324 | } |
| 325 | } |
| 326 | |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 327 | static int ip_mc_finish_output(struct net *net, struct sock *sk, |
| 328 | struct sk_buff *skb) |
| 329 | { |
Stephen Suryaputra | 5b18f12 | 2019-06-26 02:21:16 -0400 | [diff] [blame] | 330 | struct rtable *new_rt; |
David S. Miller | d96ff26 | 2019-06-27 21:06:39 -0700 | [diff] [blame] | 331 | bool do_cn = false; |
| 332 | int ret, err; |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 333 | |
| 334 | ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb); |
brakmo | 956fe21 | 2019-05-28 16:59:38 -0700 | [diff] [blame] | 335 | switch (ret) { |
brakmo | 956fe21 | 2019-05-28 16:59:38 -0700 | [diff] [blame] | 336 | case NET_XMIT_CN: |
David S. Miller | d96ff26 | 2019-06-27 21:06:39 -0700 | [diff] [blame] | 337 | do_cn = true; |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 338 | fallthrough; |
David S. Miller | d96ff26 | 2019-06-27 21:06:39 -0700 | [diff] [blame] | 339 | case NET_XMIT_SUCCESS: |
| 340 | break; |
brakmo | 956fe21 | 2019-05-28 16:59:38 -0700 | [diff] [blame] | 341 | default: |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 342 | kfree_skb(skb); |
| 343 | return ret; |
| 344 | } |
| 345 | |
Stephen Suryaputra | 5b18f12 | 2019-06-26 02:21:16 -0400 | [diff] [blame] | 346 | /* Reset rt_iif so that inet_iif() will return skb->skb_iif. Setting |
| 347 | * this to non-zero causes ipi_ifindex in in_pktinfo to be overwritten, |
| 348 | * see ipv4_pktinfo_prepare(). |
| 349 | */ |
| 350 | new_rt = rt_dst_clone(net->loopback_dev, skb_rtable(skb)); |
| 351 | if (new_rt) { |
| 352 | new_rt->rt_iif = 0; |
| 353 | skb_dst_drop(skb); |
| 354 | skb_dst_set(skb, &new_rt->dst); |
| 355 | } |
| 356 | |
David S. Miller | d96ff26 | 2019-06-27 21:06:39 -0700 | [diff] [blame] | 357 | err = dev_loopback_xmit(net, sk, skb); |
| 358 | return (do_cn && err) ? ret : err; |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 361 | int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 363 | struct rtable *rt = skb_rtable(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 364 | struct net_device *dev = rt->dst.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * If the indicated interface is up and running, send the packet. |
| 368 | */ |
Eric W. Biederman | 88f5cc2 | 2015-09-15 20:03:57 -0500 | [diff] [blame] | 369 | IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | skb->dev = dev; |
| 372 | skb->protocol = htons(ETH_P_IP); |
| 373 | |
| 374 | /* |
| 375 | * Multicasts are looped back for other local users |
| 376 | */ |
| 377 | |
| 378 | if (rt->rt_flags&RTCF_MULTICAST) { |
Octavian Purdila | 7ad6848 | 2010-01-06 20:37:01 -0800 | [diff] [blame] | 379 | if (sk_mc_loop(sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | #ifdef CONFIG_IP_MROUTE |
| 381 | /* Small optimization: do not loopback not local frames, |
| 382 | which returned after forwarding; they will be dropped |
| 383 | by ip_mr_input in any case. |
| 384 | Note, that local frames are looped back to be delivered |
| 385 | to local recipients. |
| 386 | |
| 387 | This check is duplicated in ip_mr_input at the moment. |
| 388 | */ |
Joe Perches | 9d4fb27 | 2009-11-23 10:41:23 -0800 | [diff] [blame] | 389 | && |
| 390 | ((rt->rt_flags & RTCF_LOCAL) || |
| 391 | !(IPCB(skb)->flags & IPSKB_FORWARDED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | #endif |
Joe Perches | 9d4fb27 | 2009-11-23 10:41:23 -0800 | [diff] [blame] | 393 | ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); |
| 395 | if (newskb) |
Jan Engelhardt | 9bbc768 | 2010-03-23 04:07:29 +0100 | [diff] [blame] | 396 | NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 397 | net, sk, newskb, NULL, newskb->dev, |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 398 | ip_mc_finish_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* Multicasts with ttl 0 must not go beyond the host */ |
| 402 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 403 | if (ip_hdr(skb)->ttl == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | kfree_skb(skb); |
| 405 | return 0; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (rt->rt_flags&RTCF_BROADCAST) { |
| 410 | struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); |
| 411 | if (newskb) |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 412 | NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
| 413 | net, sk, newskb, NULL, newskb->dev, |
Daniel Mack | 33b4867 | 2016-11-23 16:52:29 +0100 | [diff] [blame] | 414 | ip_mc_finish_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 417 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
| 418 | net, sk, skb, NULL, skb->dev, |
| 419 | ip_finish_output, |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 420 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 423 | int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Phil Sutter | 28f8bfd | 2019-11-12 17:14:37 +0100 | [diff] [blame] | 425 | struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev; |
Patrick McHardy | 1bd9bef | 2006-01-05 12:20:59 -0800 | [diff] [blame] | 426 | |
Eric W. Biederman | 88f5cc2 | 2015-09-15 20:03:57 -0500 | [diff] [blame] | 427 | IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Patrick McHardy | 1bd9bef | 2006-01-05 12:20:59 -0800 | [diff] [blame] | 429 | skb->dev = dev; |
| 430 | skb->protocol = htons(ETH_P_IP); |
| 431 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 432 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
Phil Sutter | 28f8bfd | 2019-11-12 17:14:37 +0100 | [diff] [blame] | 433 | net, sk, skb, indev, dev, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 434 | ip_finish_output, |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 435 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Eric Dumazet | 84f9307 | 2011-11-30 19:00:53 +0000 | [diff] [blame] | 438 | /* |
| 439 | * copy saddr and daddr, possibly using 64bit load/stores |
| 440 | * Equivalent to : |
| 441 | * iph->saddr = fl4->saddr; |
| 442 | * iph->daddr = fl4->daddr; |
| 443 | */ |
| 444 | static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4) |
| 445 | { |
| 446 | BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) != |
| 447 | offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr)); |
| 448 | memcpy(&iph->saddr, &fl4->saddr, |
| 449 | sizeof(fl4->saddr) + sizeof(fl4->daddr)); |
| 450 | } |
| 451 | |
Eric Dumazet | b0270e9 | 2014-04-15 12:58:34 -0400 | [diff] [blame] | 452 | /* Note: skb->sk can be different from sk, in case of tunnels */ |
Xin Long | 69b9e1e | 2018-07-02 18:21:11 +0800 | [diff] [blame] | 453 | int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl, |
| 454 | __u8 tos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | struct inet_sock *inet = inet_sk(sk); |
Eric W. Biederman | 77589ce | 2015-10-07 16:48:42 -0500 | [diff] [blame] | 457 | struct net *net = sock_net(sk); |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 458 | struct ip_options_rcu *inet_opt; |
David S. Miller | b57ae01 | 2011-05-06 16:24:06 -0700 | [diff] [blame] | 459 | struct flowi4 *fl4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | struct rtable *rt; |
| 461 | struct iphdr *iph; |
Eric Dumazet | ab6e3fe | 2010-05-10 11:31:49 +0000 | [diff] [blame] | 462 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | /* Skip all of this if the packet is already routed, |
| 465 | * f.e. by something like SCTP. |
| 466 | */ |
Eric Dumazet | ab6e3fe | 2010-05-10 11:31:49 +0000 | [diff] [blame] | 467 | rcu_read_lock(); |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 468 | inet_opt = rcu_dereference(inet->inet_opt); |
David S. Miller | ea4fc0d | 2011-05-06 22:30:20 -0700 | [diff] [blame] | 469 | fl4 = &fl->u.ip4; |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 470 | rt = skb_rtable(skb); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 471 | if (rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | goto packet_routed; |
| 473 | |
| 474 | /* Make sure we can route this packet. */ |
| 475 | rt = (struct rtable *)__sk_dst_check(sk, 0); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 476 | if (!rt) { |
Al Viro | 3ca3c68 | 2006-09-27 18:28:07 -0700 | [diff] [blame] | 477 | __be32 daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | /* Use correct destination address if we have options. */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 480 | daddr = inet->inet_daddr; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 481 | if (inet_opt && inet_opt->opt.srr) |
| 482 | daddr = inet_opt->opt.faddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 484 | /* If this fails, retransmit mechanism of transport layer will |
| 485 | * keep trying until route appears or the connection times |
| 486 | * itself out. |
| 487 | */ |
Eric W. Biederman | 77589ce | 2015-10-07 16:48:42 -0500 | [diff] [blame] | 488 | rt = ip_route_output_ports(net, fl4, sk, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 489 | daddr, inet->inet_saddr, |
| 490 | inet->inet_dport, |
| 491 | inet->inet_sport, |
| 492 | sk->sk_protocol, |
Xin Long | 69b9e1e | 2018-07-02 18:21:11 +0800 | [diff] [blame] | 493 | RT_CONN_FLAGS_TOS(sk, tos), |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 494 | sk->sk_bound_dev_if); |
| 495 | if (IS_ERR(rt)) |
| 496 | goto no_route; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 497 | sk_setup_caps(sk, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 499 | skb_dst_set_noref(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | packet_routed: |
David Ahern | 77d5bc7 | 2019-09-17 10:39:49 -0700 | [diff] [blame] | 502 | if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | goto no_route; |
| 504 | |
| 505 | /* OK, we know where to send it, allocate and build IP header. */ |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 506 | skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0)); |
Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 507 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 508 | iph = ip_hdr(skb); |
Xin Long | 69b9e1e | 2018-07-02 18:21:11 +0800 | [diff] [blame] | 509 | *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (tos & 0xff)); |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 510 | if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | iph->frag_off = htons(IP_DF); |
| 512 | else |
| 513 | iph->frag_off = 0; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 514 | iph->ttl = ip_select_ttl(inet, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | iph->protocol = sk->sk_protocol; |
Eric Dumazet | 84f9307 | 2011-11-30 19:00:53 +0000 | [diff] [blame] | 516 | ip_copy_addrs(iph, fl4); |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* Transport layer set skb->h.foo itself. */ |
| 519 | |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 520 | if (inet_opt && inet_opt->opt.optlen) { |
| 521 | iph->ihl += inet_opt->opt.optlen >> 2; |
| 522 | ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Eric W. Biederman | 77589ce | 2015-10-07 16:48:42 -0500 | [diff] [blame] | 525 | ip_select_ident_segs(net, skb, sk, |
Hannes Frederic Sowa | b6a7719 | 2015-03-25 17:07:44 +0100 | [diff] [blame] | 526 | skb_shinfo(skb)->gso_segs ?: 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Eric Dumazet | b0270e9 | 2014-04-15 12:58:34 -0400 | [diff] [blame] | 528 | /* TODO : should we use skb->sk here instead of sk ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | skb->priority = sk->sk_priority; |
Laszlo Attila Toth | 4a19ec5 | 2008-01-30 19:08:16 -0800 | [diff] [blame] | 530 | skb->mark = sk->sk_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Eric W. Biederman | 33224b1 | 2015-10-07 16:48:46 -0500 | [diff] [blame] | 532 | res = ip_local_out(net, sk, skb); |
Eric Dumazet | ab6e3fe | 2010-05-10 11:31:49 +0000 | [diff] [blame] | 533 | rcu_read_unlock(); |
| 534 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | no_route: |
Eric Dumazet | ab6e3fe | 2010-05-10 11:31:49 +0000 | [diff] [blame] | 537 | rcu_read_unlock(); |
Eric W. Biederman | 77589ce | 2015-10-07 16:48:42 -0500 | [diff] [blame] | 538 | IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | kfree_skb(skb); |
| 540 | return -EHOSTUNREACH; |
| 541 | } |
Xin Long | 69b9e1e | 2018-07-02 18:21:11 +0800 | [diff] [blame] | 542 | EXPORT_SYMBOL(__ip_queue_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Eric Dumazet | 05e22e8 | 2020-06-19 12:12:34 -0700 | [diff] [blame] | 544 | int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl) |
| 545 | { |
| 546 | return __ip_queue_xmit(sk, skb, fl, inet_sk(sk)->tos); |
| 547 | } |
| 548 | EXPORT_SYMBOL(ip_queue_xmit); |
| 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) |
| 551 | { |
| 552 | to->pkt_type = from->pkt_type; |
| 553 | to->priority = from->priority; |
| 554 | to->protocol = from->protocol; |
Shmulik Ladkani | d2f0c96 | 2019-04-29 16:39:30 +0300 | [diff] [blame] | 555 | to->skb_iif = from->skb_iif; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 556 | skb_dst_drop(to); |
Eric Dumazet | fe76cda | 2010-07-01 23:48:22 +0000 | [diff] [blame] | 557 | skb_dst_copy(to, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | to->dev = from->dev; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 559 | to->mark = from->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Paolo Abeni | 3dd1c9a | 2018-07-23 16:50:48 +0200 | [diff] [blame] | 561 | skb_copy_hash(to, from); |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | #ifdef CONFIG_NET_SCHED |
| 564 | to->tc_index = from->tc_index; |
| 565 | #endif |
Yasuyuki Kozakai | e7ac05f | 2007-03-14 16:44:01 -0700 | [diff] [blame] | 566 | nf_copy(to, from); |
Florian Westphal | df5042f | 2018-12-18 17:15:16 +0100 | [diff] [blame] | 567 | skb_ext_copy(to, from); |
Javier Martinez Canillas | 6ca40d4 | 2016-09-09 08:43:16 -0400 | [diff] [blame] | 568 | #if IS_ENABLED(CONFIG_IP_VS) |
Julian Anastasov | c98d80e | 2005-10-22 13:39:21 +0300 | [diff] [blame] | 569 | to->ipvs_property = from->ipvs_property; |
| 570 | #endif |
James Morris | 984bc16 | 2006-06-09 00:29:17 -0700 | [diff] [blame] | 571 | skb_copy_secmark(to, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 574 | static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, |
Florian Westphal | c5501eb | 2015-05-22 16:32:50 +0200 | [diff] [blame] | 575 | unsigned int mtu, |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 576 | int (*output)(struct net *, struct sock *, struct sk_buff *)) |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 577 | { |
| 578 | struct iphdr *iph = ip_hdr(skb); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 579 | |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 580 | if ((iph->frag_off & htons(IP_DF)) == 0) |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 581 | return ip_do_fragment(net, sk, skb, output); |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 582 | |
| 583 | if (unlikely(!skb->ignore_df || |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 584 | (IPCB(skb)->frag_max_size && |
| 585 | IPCB(skb)->frag_max_size > mtu))) { |
Eric W. Biederman | 9479b0a | 2015-09-15 20:04:00 -0500 | [diff] [blame] | 586 | IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 587 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 588 | htonl(mtu)); |
| 589 | kfree_skb(skb); |
| 590 | return -EMSGSIZE; |
| 591 | } |
| 592 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 593 | return ip_do_fragment(net, sk, skb, output); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 596 | void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph, |
| 597 | unsigned int hlen, struct ip_fraglist_iter *iter) |
| 598 | { |
| 599 | unsigned int first_len = skb_pagelen(skb); |
| 600 | |
Eric Dumazet | b703414 | 2019-06-02 11:24:18 -0700 | [diff] [blame] | 601 | iter->frag = skb_shinfo(skb)->frag_list; |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 602 | skb_frag_list_init(skb); |
| 603 | |
| 604 | iter->offset = 0; |
| 605 | iter->iph = iph; |
| 606 | iter->hlen = hlen; |
| 607 | |
| 608 | skb->data_len = first_len - skb_headlen(skb); |
| 609 | skb->len = first_len; |
| 610 | iph->tot_len = htons(first_len); |
| 611 | iph->frag_off = htons(IP_MF); |
| 612 | ip_send_check(iph); |
| 613 | } |
| 614 | EXPORT_SYMBOL(ip_fraglist_init); |
| 615 | |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 616 | static void ip_fraglist_ipcb_prepare(struct sk_buff *skb, |
| 617 | struct ip_fraglist_iter *iter) |
| 618 | { |
| 619 | struct sk_buff *to = iter->frag; |
| 620 | |
| 621 | /* Copy the flags to each fragment. */ |
| 622 | IPCB(to)->flags = IPCB(skb)->flags; |
| 623 | |
| 624 | if (iter->offset == 0) |
| 625 | ip_options_fragment(to); |
| 626 | } |
| 627 | |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 628 | void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter) |
| 629 | { |
| 630 | unsigned int hlen = iter->hlen; |
| 631 | struct iphdr *iph = iter->iph; |
| 632 | struct sk_buff *frag; |
| 633 | |
| 634 | frag = iter->frag; |
| 635 | frag->ip_summed = CHECKSUM_NONE; |
| 636 | skb_reset_transport_header(frag); |
| 637 | __skb_push(frag, hlen); |
| 638 | skb_reset_network_header(frag); |
| 639 | memcpy(skb_network_header(frag), iph, hlen); |
| 640 | iter->iph = ip_hdr(frag); |
| 641 | iph = iter->iph; |
| 642 | iph->tot_len = htons(frag->len); |
| 643 | ip_copy_metadata(frag, skb); |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 644 | iter->offset += skb->len - hlen; |
| 645 | iph->frag_off = htons(iter->offset >> 3); |
| 646 | if (frag->next) |
| 647 | iph->frag_off |= htons(IP_MF); |
| 648 | /* Ready, complete checksum */ |
| 649 | ip_send_check(iph); |
| 650 | } |
| 651 | EXPORT_SYMBOL(ip_fraglist_prepare); |
| 652 | |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 653 | void ip_frag_init(struct sk_buff *skb, unsigned int hlen, |
Eric Dumazet | e7a409c | 2019-10-19 09:26:37 -0700 | [diff] [blame] | 654 | unsigned int ll_rs, unsigned int mtu, bool DF, |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 655 | struct ip_frag_state *state) |
| 656 | { |
| 657 | struct iphdr *iph = ip_hdr(skb); |
| 658 | |
Eric Dumazet | e7a409c | 2019-10-19 09:26:37 -0700 | [diff] [blame] | 659 | state->DF = DF; |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 660 | state->hlen = hlen; |
| 661 | state->ll_rs = ll_rs; |
| 662 | state->mtu = mtu; |
| 663 | |
| 664 | state->left = skb->len - hlen; /* Space per frame */ |
| 665 | state->ptr = hlen; /* Where to start from */ |
| 666 | |
| 667 | state->offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3; |
| 668 | state->not_last_frag = iph->frag_off & htons(IP_MF); |
| 669 | } |
| 670 | EXPORT_SYMBOL(ip_frag_init); |
| 671 | |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 672 | static void ip_frag_ipcb(struct sk_buff *from, struct sk_buff *to, |
| 673 | bool first_frag, struct ip_frag_state *state) |
| 674 | { |
| 675 | /* Copy the flags to each fragment. */ |
| 676 | IPCB(to)->flags = IPCB(from)->flags; |
| 677 | |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 678 | /* ANK: dirty, but effective trick. Upgrade options only if |
| 679 | * the segment to be fragmented was THE FIRST (otherwise, |
| 680 | * options are already fixed) and make it ONCE |
| 681 | * on the initial skb, so that all the following fragments |
| 682 | * will inherit fixed options. |
| 683 | */ |
| 684 | if (first_frag) |
| 685 | ip_options_fragment(from); |
| 686 | } |
| 687 | |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 688 | struct sk_buff *ip_frag_next(struct sk_buff *skb, struct ip_frag_state *state) |
| 689 | { |
| 690 | unsigned int len = state->left; |
| 691 | struct sk_buff *skb2; |
| 692 | struct iphdr *iph; |
| 693 | |
| 694 | len = state->left; |
| 695 | /* IF: it doesn't fit, use 'mtu' - the data space left */ |
| 696 | if (len > state->mtu) |
| 697 | len = state->mtu; |
| 698 | /* IF: we are not sending up to and including the packet end |
| 699 | then align the next start on an eight byte boundary */ |
| 700 | if (len < state->left) { |
| 701 | len &= ~7; |
| 702 | } |
| 703 | |
| 704 | /* Allocate buffer */ |
| 705 | skb2 = alloc_skb(len + state->hlen + state->ll_rs, GFP_ATOMIC); |
| 706 | if (!skb2) |
| 707 | return ERR_PTR(-ENOMEM); |
| 708 | |
| 709 | /* |
| 710 | * Set up data on packet |
| 711 | */ |
| 712 | |
| 713 | ip_copy_metadata(skb2, skb); |
| 714 | skb_reserve(skb2, state->ll_rs); |
| 715 | skb_put(skb2, len + state->hlen); |
| 716 | skb_reset_network_header(skb2); |
| 717 | skb2->transport_header = skb2->network_header + state->hlen; |
| 718 | |
| 719 | /* |
| 720 | * Charge the memory for the fragment to any owner |
| 721 | * it might possess |
| 722 | */ |
| 723 | |
| 724 | if (skb->sk) |
| 725 | skb_set_owner_w(skb2, skb->sk); |
| 726 | |
| 727 | /* |
| 728 | * Copy the packet header into the new buffer. |
| 729 | */ |
| 730 | |
| 731 | skb_copy_from_linear_data(skb, skb_network_header(skb2), state->hlen); |
| 732 | |
| 733 | /* |
| 734 | * Copy a block of the IP datagram. |
| 735 | */ |
| 736 | if (skb_copy_bits(skb, state->ptr, skb_transport_header(skb2), len)) |
| 737 | BUG(); |
| 738 | state->left -= len; |
| 739 | |
| 740 | /* |
| 741 | * Fill in the new header fields. |
| 742 | */ |
| 743 | iph = ip_hdr(skb2); |
| 744 | iph->frag_off = htons((state->offset >> 3)); |
Eric Dumazet | e7a409c | 2019-10-19 09:26:37 -0700 | [diff] [blame] | 745 | if (state->DF) |
| 746 | iph->frag_off |= htons(IP_DF); |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 747 | |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 748 | /* |
| 749 | * Added AC : If we are fragmenting a fragment that's not the |
| 750 | * last fragment then keep MF on each bit |
| 751 | */ |
| 752 | if (state->left > 0 || state->not_last_frag) |
| 753 | iph->frag_off |= htons(IP_MF); |
| 754 | state->ptr += len; |
| 755 | state->offset += len; |
| 756 | |
| 757 | iph->tot_len = htons(len + state->hlen); |
| 758 | |
| 759 | ip_send_check(iph); |
| 760 | |
| 761 | return skb2; |
| 762 | } |
| 763 | EXPORT_SYMBOL(ip_frag_next); |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | /* |
| 766 | * This IP datagram is too large to be sent in one piece. Break it up into |
| 767 | * smaller pieces (each of size equal to IP header plus |
| 768 | * a block of the data of the original IP data part) that will yet fit in a |
| 769 | * single device frame, and queue such a frame for sending. |
| 770 | */ |
| 771 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 772 | int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, |
| 773 | int (*output)(struct net *, struct sock *, struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
| 775 | struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | struct sk_buff *skb2; |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 777 | struct rtable *rt = skb_rtable(skb); |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 778 | unsigned int mtu, hlen, ll_rs; |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 779 | struct ip_fraglist_iter iter; |
Eric Dumazet | 9669fff | 2019-10-16 18:00:56 -0700 | [diff] [blame] | 780 | ktime_t tstamp = skb->tstamp; |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 781 | struct ip_frag_state state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | int err = 0; |
| 783 | |
Hannes Frederic Sowa | dbd3393 | 2015-10-27 22:40:40 +0100 | [diff] [blame] | 784 | /* for offloaded checksums cleanup checksum before fragmentation */ |
| 785 | if (skb->ip_summed == CHECKSUM_PARTIAL && |
| 786 | (err = skb_checksum_help(skb))) |
| 787 | goto fail; |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | /* |
| 790 | * Point into the IP datagram header. |
| 791 | */ |
| 792 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 793 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
Shmulik Ladkani | fedbb6b4 | 2016-06-29 21:47:03 +0300 | [diff] [blame] | 795 | mtu = ip_skb_dst_mtu(sk, skb); |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 796 | if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu) |
| 797 | mtu = IPCB(skb)->frag_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
| 799 | /* |
| 800 | * Setup starting values. |
| 801 | */ |
| 802 | |
| 803 | hlen = iph->ihl * 4; |
Hannes Frederic Sowa | f87c10a | 2014-01-09 10:01:15 +0100 | [diff] [blame] | 804 | mtu = mtu - hlen; /* Size of data space */ |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 805 | IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE; |
Vasily Averin | 254d900 | 2017-07-14 12:04:16 +0300 | [diff] [blame] | 806 | ll_rs = LL_RESERVED_SPACE(rt->dst.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | /* When frag_list is given, use it. First, check its validity: |
| 809 | * some transformers could create wrong frag_list or break existing |
| 810 | * one, it is not prohibited. In this case fall back to copying. |
| 811 | * |
| 812 | * LATER: this step can be merged to real generation of fragments, |
| 813 | * we can switch to copy when see the first bad fragment. |
| 814 | */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 815 | if (skb_has_frag_list(skb)) { |
Eric Dumazet | 3d13008 | 2010-09-21 08:47:45 +0000 | [diff] [blame] | 816 | struct sk_buff *frag, *frag2; |
Alexey Dobriyan | c72d8cd | 2016-11-19 04:08:08 +0300 | [diff] [blame] | 817 | unsigned int first_len = skb_pagelen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
| 819 | if (first_len - hlen > mtu || |
| 820 | ((first_len - hlen) & 7) || |
Paul Gortmaker | 56f8a75 | 2011-06-21 20:33:34 -0700 | [diff] [blame] | 821 | ip_is_fragment(iph) || |
Vasily Averin | 254d900 | 2017-07-14 12:04:16 +0300 | [diff] [blame] | 822 | skb_cloned(skb) || |
| 823 | skb_headroom(skb) < ll_rs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | goto slow_path; |
| 825 | |
David S. Miller | d7fcf1a | 2009-06-09 00:19:37 -0700 | [diff] [blame] | 826 | skb_walk_frags(skb, frag) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | /* Correct geometry. */ |
| 828 | if (frag->len > mtu || |
| 829 | ((frag->len & 7) && frag->next) || |
Vasily Averin | 254d900 | 2017-07-14 12:04:16 +0300 | [diff] [blame] | 830 | skb_headroom(frag) < hlen + ll_rs) |
Eric Dumazet | 3d13008 | 2010-09-21 08:47:45 +0000 | [diff] [blame] | 831 | goto slow_path_clean; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | /* Partially cloned skb? */ |
| 834 | if (skb_shared(frag)) |
Eric Dumazet | 3d13008 | 2010-09-21 08:47:45 +0000 | [diff] [blame] | 835 | goto slow_path_clean; |
Herbert Xu | 2fdba6b | 2005-05-18 22:52:33 -0700 | [diff] [blame] | 836 | |
| 837 | BUG_ON(frag->sk); |
| 838 | if (skb->sk) { |
Herbert Xu | 2fdba6b | 2005-05-18 22:52:33 -0700 | [diff] [blame] | 839 | frag->sk = skb->sk; |
| 840 | frag->destructor = sock_wfree; |
Herbert Xu | 2fdba6b | 2005-05-18 22:52:33 -0700 | [diff] [blame] | 841 | } |
Eric Dumazet | 3d13008 | 2010-09-21 08:47:45 +0000 | [diff] [blame] | 842 | skb->truesize -= frag->truesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* Everything is OK. Generate! */ |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 846 | ip_fraglist_init(skb, iph, hlen, &iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
| 848 | for (;;) { |
| 849 | /* Prepare header of the next frame, |
| 850 | * before previous one went down. */ |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 851 | if (iter.frag) { |
| 852 | ip_fraglist_ipcb_prepare(skb, &iter); |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 853 | ip_fraglist_prepare(skb, &iter); |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
Eric Dumazet | 9669fff | 2019-10-16 18:00:56 -0700 | [diff] [blame] | 856 | skb->tstamp = tstamp; |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 857 | err = output(net, sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Wei Dong | dafee49 | 2006-08-02 13:41:21 -0700 | [diff] [blame] | 859 | if (!err) |
Eric W. Biederman | 26a949d | 2015-09-15 20:03:59 -0500 | [diff] [blame] | 860 | IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 861 | if (err || !iter.frag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | break; |
| 863 | |
Pablo Neira Ayuso | c8b17be | 2019-05-29 13:25:31 +0200 | [diff] [blame] | 864 | skb = ip_fraglist_next(&iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | if (err == 0) { |
Eric W. Biederman | 26a949d | 2015-09-15 20:03:59 -0500 | [diff] [blame] | 868 | IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
Eric Dumazet | b703414 | 2019-06-02 11:24:18 -0700 | [diff] [blame] | 872 | kfree_skb_list(iter.frag); |
Pablo Neira Ayuso | 942f146 | 2019-04-04 13:54:20 +0200 | [diff] [blame] | 873 | |
Eric W. Biederman | 26a949d | 2015-09-15 20:03:59 -0500 | [diff] [blame] | 874 | IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | return err; |
Eric Dumazet | 3d13008 | 2010-09-21 08:47:45 +0000 | [diff] [blame] | 876 | |
| 877 | slow_path_clean: |
| 878 | skb_walk_frags(skb, frag2) { |
| 879 | if (frag2 == frag) |
| 880 | break; |
| 881 | frag2->sk = NULL; |
| 882 | frag2->destructor = NULL; |
| 883 | skb->truesize += frag2->truesize; |
| 884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | slow_path: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | /* |
| 889 | * Fragment the datagram. |
| 890 | */ |
| 891 | |
Eric Dumazet | e7a409c | 2019-10-19 09:26:37 -0700 | [diff] [blame] | 892 | ip_frag_init(skb, hlen, ll_rs, mtu, IPCB(skb)->flags & IPSKB_FRAG_PMTU, |
| 893 | &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | /* |
| 896 | * Keep copying data until we run out. |
| 897 | */ |
| 898 | |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 899 | while (state.left > 0) { |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 900 | bool first_frag = (state.offset == 0); |
| 901 | |
Pablo Neira Ayuso | 065ff79 | 2019-05-29 13:25:33 +0200 | [diff] [blame] | 902 | skb2 = ip_frag_next(skb, &state); |
| 903 | if (IS_ERR(skb2)) { |
| 904 | err = PTR_ERR(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | goto fail; |
| 906 | } |
Pablo Neira Ayuso | 19c3401 | 2019-05-29 13:25:35 +0200 | [diff] [blame] | 907 | ip_frag_ipcb(skb, skb2, first_frag, &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
| 909 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | * Put this fragment into the sending queue. |
| 911 | */ |
Eric Dumazet | 9669fff | 2019-10-16 18:00:56 -0700 | [diff] [blame] | 912 | skb2->tstamp = tstamp; |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 913 | err = output(net, sk, skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if (err) |
| 915 | goto fail; |
Wei Dong | dafee49 | 2006-08-02 13:41:21 -0700 | [diff] [blame] | 916 | |
Eric W. Biederman | 26a949d | 2015-09-15 20:03:59 -0500 | [diff] [blame] | 917 | IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
Eric Dumazet | 5d0ba55 | 2012-06-04 01:17:19 +0000 | [diff] [blame] | 919 | consume_skb(skb); |
Eric W. Biederman | 26a949d | 2015-09-15 20:03:59 -0500 | [diff] [blame] | 920 | IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return err; |
| 922 | |
| 923 | fail: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 924 | kfree_skb(skb); |
Eric W. Biederman | 26a949d | 2015-09-15 20:03:59 -0500 | [diff] [blame] | 925 | IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return err; |
| 927 | } |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 928 | EXPORT_SYMBOL(ip_do_fragment); |
Patrick McHardy | 2e2f7ae | 2006-04-04 13:42:35 -0700 | [diff] [blame] | 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | int |
| 931 | ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb) |
| 932 | { |
Al Viro | f69e6d1 | 2014-11-24 13:23:40 -0500 | [diff] [blame] | 933 | struct msghdr *msg = from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 935 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | 0b62fca | 2016-11-03 18:17:31 -0400 | [diff] [blame] | 936 | if (!copy_from_iter_full(to, len, &msg->msg_iter)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | return -EFAULT; |
| 938 | } else { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 939 | __wsum csum = 0; |
Al Viro | 0b62fca | 2016-11-03 18:17:31 -0400 | [diff] [blame] | 940 | if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return -EFAULT; |
| 942 | skb->csum = csum_block_add(skb->csum, csum, odd); |
| 943 | } |
| 944 | return 0; |
| 945 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 946 | EXPORT_SYMBOL(ip_generic_getfrag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 948 | static inline __wsum |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | csum_page(struct page *page, int offset, int copy) |
| 950 | { |
| 951 | char *kaddr; |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 952 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | kaddr = kmap(page); |
| 954 | csum = csum_partial(kaddr + offset, copy, 0); |
| 955 | kunmap(page); |
| 956 | return csum; |
| 957 | } |
| 958 | |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 959 | static int __ip_append_data(struct sock *sk, |
| 960 | struct flowi4 *fl4, |
| 961 | struct sk_buff_head *queue, |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 962 | struct inet_cork *cork, |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 963 | struct page_frag *pfrag, |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 964 | int getfrag(void *from, char *to, int offset, |
| 965 | int len, int odd, struct sk_buff *skb), |
| 966 | void *from, int length, int transhdrlen, |
| 967 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
| 969 | struct inet_sock *inet = inet_sk(sk); |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 970 | struct ubuf_info *uarg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | struct sk_buff *skb; |
| 972 | |
Herbert Xu | 07df529 | 2011-03-01 23:00:58 -0800 | [diff] [blame] | 973 | struct ip_options *opt = cork->opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | int hh_len; |
| 975 | int exthdrlen; |
| 976 | int mtu; |
| 977 | int copy; |
| 978 | int err; |
| 979 | int offset = 0; |
Hannes Frederic Sowa | daba287 | 2013-10-27 17:29:11 +0100 | [diff] [blame] | 980 | unsigned int maxfraglen, fragheaderlen, maxnonfragsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | int csummode = CHECKSUM_NONE; |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 982 | struct rtable *rt = (struct rtable *)cork->dst; |
Eric Dumazet | 694aba6 | 2018-03-31 13:16:25 -0700 | [diff] [blame] | 983 | unsigned int wmem_alloc_delta = 0; |
Willem de Bruijn | 100f6d8 | 2019-05-30 18:01:21 -0400 | [diff] [blame] | 984 | bool paged, extra_uref = false; |
Willem de Bruijn | 09c2d25 | 2014-08-04 22:11:47 -0400 | [diff] [blame] | 985 | u32 tskey = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Steffen Klassert | 96d7303 | 2011-06-05 20:48:47 +0000 | [diff] [blame] | 987 | skb = skb_peek_tail(queue); |
| 988 | |
| 989 | exthdrlen = !skb ? rt->dst.header_len : 0; |
Willem de Bruijn | bec1f6f | 2018-04-26 13:42:17 -0400 | [diff] [blame] | 990 | mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize; |
Willem de Bruijn | 15e36f5 | 2018-04-26 13:42:19 -0400 | [diff] [blame] | 991 | paged = !!cork->gso_size; |
Willem de Bruijn | bec1f6f | 2018-04-26 13:42:17 -0400 | [diff] [blame] | 992 | |
Willem de Bruijn | 09c2d25 | 2014-08-04 22:11:47 -0400 | [diff] [blame] | 993 | if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP && |
| 994 | sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) |
| 995 | tskey = sk->sk_tskey++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 997 | hh_len = LL_RESERVED_SPACE(rt->dst.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); |
| 1000 | maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; |
Miaohe Lin | cbc08a3 | 2020-08-29 05:09:18 -0400 | [diff] [blame] | 1001 | maxnonfragsize = ip_sk_ignore_df(sk) ? IP_MAX_MTU : mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Hannes Frederic Sowa | daba287 | 2013-10-27 17:29:11 +0100 | [diff] [blame] | 1003 | if (cork->length + length > maxnonfragsize - fragheaderlen) { |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 1004 | ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, |
Hannes Frederic Sowa | 61e7f09 | 2013-12-19 02:13:36 +0100 | [diff] [blame] | 1005 | mtu - (opt ? opt->optlen : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | return -EMSGSIZE; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * transhdrlen > 0 means that this is the first fragment and we wish |
| 1011 | * it won't be fragmented in the future. |
| 1012 | */ |
| 1013 | if (transhdrlen && |
| 1014 | length + fragheaderlen <= mtu && |
Tom Herbert | c8cd098 | 2015-12-14 11:19:44 -0800 | [diff] [blame] | 1015 | rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) && |
Willem de Bruijn | bec1f6f | 2018-04-26 13:42:17 -0400 | [diff] [blame] | 1016 | (!(flags & MSG_MORE) || cork->gso_size) && |
Jacek Kalwas | cd027a5 | 2018-04-12 12:03:13 -0700 | [diff] [blame] | 1017 | (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM))) |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1018 | csummode = CHECKSUM_PARTIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1020 | if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) { |
| 1021 | uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb)); |
| 1022 | if (!uarg) |
| 1023 | return -ENOBUFS; |
Willem de Bruijn | 522924b | 2019-06-07 17:57:48 -0400 | [diff] [blame] | 1024 | extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1025 | if (rt->dst.dev->features & NETIF_F_SG && |
| 1026 | csummode == CHECKSUM_PARTIAL) { |
| 1027 | paged = true; |
| 1028 | } else { |
| 1029 | uarg->zerocopy = 0; |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1030 | skb_zcopy_set(skb, uarg, &extra_uref); |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1034 | cork->length += length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* So, what's going on in the loop below? |
| 1037 | * |
| 1038 | * We use calculated fragment length to generate chained skb, |
| 1039 | * each of segments is IP fragment ready for sending to network after |
| 1040 | * adding appropriate IP header. |
| 1041 | */ |
| 1042 | |
Herbert Xu | 26cde9f | 2010-06-15 01:52:25 +0000 | [diff] [blame] | 1043 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | goto alloc_new_skb; |
| 1045 | |
| 1046 | while (length > 0) { |
| 1047 | /* Check if the remaining data fits into current packet. */ |
| 1048 | copy = mtu - skb->len; |
| 1049 | if (copy < length) |
| 1050 | copy = maxfraglen - skb->len; |
| 1051 | if (copy <= 0) { |
| 1052 | char *data; |
| 1053 | unsigned int datalen; |
| 1054 | unsigned int fraglen; |
| 1055 | unsigned int fraggap; |
| 1056 | unsigned int alloclen; |
Willem de Bruijn | aba3693 | 2018-11-24 14:21:16 -0500 | [diff] [blame] | 1057 | unsigned int pagedlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | struct sk_buff *skb_prev; |
| 1059 | alloc_new_skb: |
| 1060 | skb_prev = skb; |
| 1061 | if (skb_prev) |
| 1062 | fraggap = skb_prev->len - maxfraglen; |
| 1063 | else |
| 1064 | fraggap = 0; |
| 1065 | |
| 1066 | /* |
| 1067 | * If remaining data exceeds the mtu, |
| 1068 | * we know we need more fragment(s). |
| 1069 | */ |
| 1070 | datalen = length + fraggap; |
| 1071 | if (datalen > mtu - fragheaderlen) |
| 1072 | datalen = maxfraglen - fragheaderlen; |
| 1073 | fraglen = datalen + fragheaderlen; |
Willem de Bruijn | aba3693 | 2018-11-24 14:21:16 -0500 | [diff] [blame] | 1074 | pagedlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1076 | if ((flags & MSG_MORE) && |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1077 | !(rt->dst.dev->features&NETIF_F_SG)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | alloclen = mtu; |
Willem de Bruijn | 15e36f5 | 2018-04-26 13:42:19 -0400 | [diff] [blame] | 1079 | else if (!paged) |
Eric Dumazet | 59104f0 | 2010-09-20 20:16:27 +0000 | [diff] [blame] | 1080 | alloclen = fraglen; |
Willem de Bruijn | 15e36f5 | 2018-04-26 13:42:19 -0400 | [diff] [blame] | 1081 | else { |
| 1082 | alloclen = min_t(int, fraglen, MAX_HEADER); |
| 1083 | pagedlen = fraglen - alloclen; |
| 1084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Steffen Klassert | 353e5c9 | 2011-06-22 01:05:37 +0000 | [diff] [blame] | 1086 | alloclen += exthdrlen; |
| 1087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | /* The last fragment gets additional space at tail. |
| 1089 | * Note, with MSG_MORE we overallocate on fragments, |
| 1090 | * because we have no idea what fragment will be |
| 1091 | * the last. |
| 1092 | */ |
Steffen Klassert | 33f99dc | 2011-06-22 01:04:37 +0000 | [diff] [blame] | 1093 | if (datalen == length + fraggap) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1094 | alloclen += rt->dst.trailer_len; |
Steffen Klassert | 33f99dc | 2011-06-22 01:04:37 +0000 | [diff] [blame] | 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (transhdrlen) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1097 | skb = sock_alloc_send_skb(sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | alloclen + hh_len + 15, |
| 1099 | (flags & MSG_DONTWAIT), &err); |
| 1100 | } else { |
| 1101 | skb = NULL; |
Eric Dumazet | 694aba6 | 2018-03-31 13:16:25 -0700 | [diff] [blame] | 1102 | if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | 2 * sk->sk_sndbuf) |
Eric Dumazet | 694aba6 | 2018-03-31 13:16:25 -0700 | [diff] [blame] | 1104 | skb = alloc_skb(alloclen + hh_len + 15, |
| 1105 | sk->sk_allocation); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1106 | if (unlikely(!skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | err = -ENOBUFS; |
| 1108 | } |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1109 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | goto error; |
| 1111 | |
| 1112 | /* |
| 1113 | * Fill in the control structures |
| 1114 | */ |
| 1115 | skb->ip_summed = csummode; |
| 1116 | skb->csum = 0; |
| 1117 | skb_reserve(skb, hh_len); |
Willem de Bruijn | 11878b4 | 2014-07-14 17:55:06 -0400 | [diff] [blame] | 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* |
| 1120 | * Find where to start putting bytes. |
| 1121 | */ |
Willem de Bruijn | 15e36f5 | 2018-04-26 13:42:19 -0400 | [diff] [blame] | 1122 | data = skb_put(skb, fraglen + exthdrlen - pagedlen); |
Arnaldo Carvalho de Melo | c14d245 | 2007-03-11 22:39:41 -0300 | [diff] [blame] | 1123 | skb_set_network_header(skb, exthdrlen); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1124 | skb->transport_header = (skb->network_header + |
| 1125 | fragheaderlen); |
Steffen Klassert | 353e5c9 | 2011-06-22 01:05:37 +0000 | [diff] [blame] | 1126 | data += fragheaderlen + exthdrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (fraggap) { |
| 1129 | skb->csum = skb_copy_and_csum_bits( |
| 1130 | skb_prev, maxfraglen, |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 1131 | data + transhdrlen, fraggap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | skb_prev->csum = csum_sub(skb_prev->csum, |
| 1133 | skb->csum); |
| 1134 | data += fraggap; |
Herbert Xu | e9fa4f7 | 2006-08-13 20:12:58 -0700 | [diff] [blame] | 1135 | pskb_trim_unique(skb_prev, maxfraglen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Willem de Bruijn | 15e36f5 | 2018-04-26 13:42:19 -0400 | [diff] [blame] | 1138 | copy = datalen - transhdrlen - fraggap - pagedlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { |
| 1140 | err = -EFAULT; |
| 1141 | kfree_skb(skb); |
| 1142 | goto error; |
| 1143 | } |
| 1144 | |
| 1145 | offset += copy; |
Willem de Bruijn | 15e36f5 | 2018-04-26 13:42:19 -0400 | [diff] [blame] | 1146 | length -= copy + transhdrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | transhdrlen = 0; |
| 1148 | exthdrlen = 0; |
| 1149 | csummode = CHECKSUM_NONE; |
| 1150 | |
Willem de Bruijn | 52900d2 | 2018-11-30 15:32:40 -0500 | [diff] [blame] | 1151 | /* only the initial fragment is time stamped */ |
| 1152 | skb_shinfo(skb)->tx_flags = cork->tx_flags; |
| 1153 | cork->tx_flags = 0; |
| 1154 | skb_shinfo(skb)->tskey = tskey; |
| 1155 | tskey = 0; |
| 1156 | skb_zcopy_set(skb, uarg, &extra_uref); |
| 1157 | |
Julian Anastasov | 0dec879 | 2017-02-06 23:14:16 +0200 | [diff] [blame] | 1158 | if ((flags & MSG_CONFIRM) && !skb_prev) |
| 1159 | skb_set_dst_pending_confirm(skb, 1); |
| 1160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | /* |
| 1162 | * Put the packet on the pending queue. |
| 1163 | */ |
Eric Dumazet | 694aba6 | 2018-03-31 13:16:25 -0700 | [diff] [blame] | 1164 | if (!skb->destructor) { |
| 1165 | skb->destructor = sock_wfree; |
| 1166 | skb->sk = sk; |
| 1167 | wmem_alloc_delta += skb->truesize; |
| 1168 | } |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1169 | __skb_queue_tail(queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | continue; |
| 1171 | } |
| 1172 | |
| 1173 | if (copy > length) |
| 1174 | copy = length; |
| 1175 | |
Willem de Bruijn | 113f99c | 2018-05-17 13:13:29 -0400 | [diff] [blame] | 1176 | if (!(rt->dst.dev->features&NETIF_F_SG) && |
| 1177 | skb_tailroom(skb) >= copy) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | unsigned int off; |
| 1179 | |
| 1180 | off = skb->len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1181 | if (getfrag(from, skb_put(skb, copy), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | offset, copy, off, skb) < 0) { |
| 1183 | __skb_trim(skb, off); |
| 1184 | err = -EFAULT; |
| 1185 | goto error; |
| 1186 | } |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1187 | } else if (!uarg || !uarg->zerocopy) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | int i = skb_shinfo(skb)->nr_frags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1190 | err = -ENOMEM; |
| 1191 | if (!sk_page_frag_refill(sk, pfrag)) |
| 1192 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1194 | if (!skb_can_coalesce(skb, i, pfrag->page, |
| 1195 | pfrag->offset)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | err = -EMSGSIZE; |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1197 | if (i == MAX_SKB_FRAGS) |
| 1198 | goto error; |
| 1199 | |
| 1200 | __skb_fill_page_desc(skb, i, pfrag->page, |
| 1201 | pfrag->offset, 0); |
| 1202 | skb_shinfo(skb)->nr_frags = ++i; |
| 1203 | get_page(pfrag->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1205 | copy = min_t(int, copy, pfrag->size - pfrag->offset); |
| 1206 | if (getfrag(from, |
| 1207 | page_address(pfrag->page) + pfrag->offset, |
| 1208 | offset, copy, skb->len, skb) < 0) |
| 1209 | goto error_efault; |
| 1210 | |
| 1211 | pfrag->offset += copy; |
| 1212 | skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | skb->len += copy; |
| 1214 | skb->data_len += copy; |
Herbert Xu | f945fa7 | 2008-01-22 22:39:26 -0800 | [diff] [blame] | 1215 | skb->truesize += copy; |
Eric Dumazet | 694aba6 | 2018-03-31 13:16:25 -0700 | [diff] [blame] | 1216 | wmem_alloc_delta += copy; |
Willem de Bruijn | b5947e5 | 2018-11-30 15:32:39 -0500 | [diff] [blame] | 1217 | } else { |
| 1218 | err = skb_zerocopy_iter_dgram(skb, from, copy); |
| 1219 | if (err < 0) |
| 1220 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | offset += copy; |
| 1223 | length -= copy; |
| 1224 | } |
| 1225 | |
Paolo Abeni | 9e8445a | 2018-04-04 14:30:01 +0200 | [diff] [blame] | 1226 | if (wmem_alloc_delta) |
| 1227 | refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | return 0; |
| 1229 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1230 | error_efault: |
| 1231 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | error: |
Willem de Bruijn | 97ef7b4 | 2018-12-08 06:22:46 -0500 | [diff] [blame] | 1233 | if (uarg) |
| 1234 | sock_zerocopy_put_abort(uarg, extra_uref); |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1235 | cork->length -= length; |
Pavel Emelyanov | 5e38e27 | 2008-07-16 20:19:49 -0700 | [diff] [blame] | 1236 | IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); |
Eric Dumazet | 694aba6 | 2018-03-31 13:16:25 -0700 | [diff] [blame] | 1237 | refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1238 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1241 | static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, |
| 1242 | struct ipcm_cookie *ipc, struct rtable **rtp) |
| 1243 | { |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1244 | struct ip_options_rcu *opt; |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1245 | struct rtable *rt; |
| 1246 | |
Gao Feng | 9783ccd | 2018-04-16 10:16:45 +0800 | [diff] [blame] | 1247 | rt = *rtp; |
| 1248 | if (unlikely(!rt)) |
| 1249 | return -EFAULT; |
| 1250 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1251 | /* |
| 1252 | * setup for corking. |
| 1253 | */ |
| 1254 | opt = ipc->opt; |
| 1255 | if (opt) { |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1256 | if (!cork->opt) { |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1257 | cork->opt = kmalloc(sizeof(struct ip_options) + 40, |
| 1258 | sk->sk_allocation); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1259 | if (unlikely(!cork->opt)) |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1260 | return -ENOBUFS; |
| 1261 | } |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1262 | memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1263 | cork->flags |= IPCORK_OPT; |
| 1264 | cork->addr = ipc->addr; |
| 1265 | } |
Gao Feng | 9783ccd | 2018-04-16 10:16:45 +0800 | [diff] [blame] | 1266 | |
Hannes Frederic Sowa | 482fc60 | 2013-11-05 02:24:17 +0100 | [diff] [blame] | 1267 | cork->fragsize = ip_sk_use_pmtu(sk) ? |
Eric Dumazet | 501a90c | 2019-12-05 20:43:46 -0800 | [diff] [blame] | 1268 | dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu); |
| 1269 | |
| 1270 | if (!inetdev_valid_mtu(cork->fragsize)) |
| 1271 | return -ENETUNREACH; |
Willem de Bruijn | bec1f6f | 2018-04-26 13:42:17 -0400 | [diff] [blame] | 1272 | |
Willem de Bruijn | fbf4781 | 2018-07-06 10:12:59 -0400 | [diff] [blame] | 1273 | cork->gso_size = ipc->gso_size; |
Eric Dumazet | 501a90c | 2019-12-05 20:43:46 -0800 | [diff] [blame] | 1274 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1275 | cork->dst = &rt->dst; |
Eric Dumazet | 501a90c | 2019-12-05 20:43:46 -0800 | [diff] [blame] | 1276 | /* We stole this route, caller should not release it. */ |
| 1277 | *rtp = NULL; |
| 1278 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1279 | cork->length = 0; |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 1280 | cork->ttl = ipc->ttl; |
| 1281 | cork->tos = ipc->tos; |
Willem de Bruijn | c6af0c2 | 2019-09-11 15:50:51 -0400 | [diff] [blame] | 1282 | cork->mark = ipc->sockc.mark; |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 1283 | cork->priority = ipc->priority; |
Jesus Sanchez-Palencia | bc969a9 | 2018-07-03 15:42:49 -0700 | [diff] [blame] | 1284 | cork->transmit_time = ipc->sockc.transmit_time; |
Willem de Bruijn | 678ca42 | 2018-07-06 10:12:58 -0400 | [diff] [blame] | 1285 | cork->tx_flags = 0; |
| 1286 | sock_tx_timestamp(sk, ipc->sockc.tsflags, &cork->tx_flags); |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * ip_append_data() and ip_append_page() can make one large IP datagram |
| 1293 | * from many pieces of data. Each pieces will be holded on the socket |
| 1294 | * until ip_push_pending_frames() is called. Each piece can be a page |
| 1295 | * or non-page data. |
| 1296 | * |
| 1297 | * Not only UDP, other transport protocols - e.g. raw sockets - can use |
| 1298 | * this interface potentially. |
| 1299 | * |
| 1300 | * LATER: length must be adjusted by pad at tail, when it is required. |
| 1301 | */ |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 1302 | int ip_append_data(struct sock *sk, struct flowi4 *fl4, |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1303 | int getfrag(void *from, char *to, int offset, int len, |
| 1304 | int odd, struct sk_buff *skb), |
| 1305 | void *from, int length, int transhdrlen, |
| 1306 | struct ipcm_cookie *ipc, struct rtable **rtp, |
| 1307 | unsigned int flags) |
| 1308 | { |
| 1309 | struct inet_sock *inet = inet_sk(sk); |
| 1310 | int err; |
| 1311 | |
| 1312 | if (flags&MSG_PROBE) |
| 1313 | return 0; |
| 1314 | |
| 1315 | if (skb_queue_empty(&sk->sk_write_queue)) { |
David S. Miller | bdc712b | 2011-05-06 15:02:07 -0700 | [diff] [blame] | 1316 | err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp); |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1317 | if (err) |
| 1318 | return err; |
| 1319 | } else { |
| 1320 | transhdrlen = 0; |
| 1321 | } |
| 1322 | |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1323 | return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base, |
| 1324 | sk_page_frag(sk), getfrag, |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1325 | from, length, transhdrlen, flags); |
| 1326 | } |
| 1327 | |
David S. Miller | f5fca60 | 2011-05-08 17:24:10 -0700 | [diff] [blame] | 1328 | ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | int offset, size_t size, int flags) |
| 1330 | { |
| 1331 | struct inet_sock *inet = inet_sk(sk); |
| 1332 | struct sk_buff *skb; |
| 1333 | struct rtable *rt; |
| 1334 | struct ip_options *opt = NULL; |
David S. Miller | bdc712b | 2011-05-06 15:02:07 -0700 | [diff] [blame] | 1335 | struct inet_cork *cork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | int hh_len; |
| 1337 | int mtu; |
| 1338 | int len; |
| 1339 | int err; |
Hannes Frederic Sowa | daba287 | 2013-10-27 17:29:11 +0100 | [diff] [blame] | 1340 | unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | if (inet->hdrincl) |
| 1343 | return -EPERM; |
| 1344 | |
| 1345 | if (flags&MSG_PROBE) |
| 1346 | return 0; |
| 1347 | |
| 1348 | if (skb_queue_empty(&sk->sk_write_queue)) |
| 1349 | return -EINVAL; |
| 1350 | |
David S. Miller | bdc712b | 2011-05-06 15:02:07 -0700 | [diff] [blame] | 1351 | cork = &inet->cork.base; |
| 1352 | rt = (struct rtable *)cork->dst; |
| 1353 | if (cork->flags & IPCORK_OPT) |
| 1354 | opt = cork->opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Miaohe Lin | 343d8c6 | 2020-08-25 08:32:11 -0400 | [diff] [blame] | 1356 | if (!(rt->dst.dev->features & NETIF_F_SG)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | return -EOPNOTSUPP; |
| 1358 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1359 | hh_len = LL_RESERVED_SPACE(rt->dst.dev); |
Willem de Bruijn | bec1f6f | 2018-04-26 13:42:17 -0400 | [diff] [blame] | 1360 | mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); |
| 1363 | maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 1364 | maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Hannes Frederic Sowa | daba287 | 2013-10-27 17:29:11 +0100 | [diff] [blame] | 1366 | if (cork->length + size > maxnonfragsize - fragheaderlen) { |
Hannes Frederic Sowa | 61e7f09 | 2013-12-19 02:13:36 +0100 | [diff] [blame] | 1367 | ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, |
| 1368 | mtu - (opt ? opt->optlen : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | return -EMSGSIZE; |
| 1370 | } |
| 1371 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1372 | skb = skb_peek_tail(&sk->sk_write_queue); |
| 1373 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | return -EINVAL; |
| 1375 | |
Hannes Frederic Sowa | a8c4a25 | 2016-02-22 18:43:25 +0100 | [diff] [blame] | 1376 | cork->length += size; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | while (size > 0) { |
Willem de Bruijn | ab2fb7e | 2017-08-22 11:39:57 -0400 | [diff] [blame] | 1379 | /* Check if the remaining data fits into current packet. */ |
| 1380 | len = mtu - skb->len; |
| 1381 | if (len < size) |
| 1382 | len = maxfraglen - skb->len; |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | if (len <= 0) { |
| 1385 | struct sk_buff *skb_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | int alloclen; |
| 1387 | |
| 1388 | skb_prev = skb; |
Jayachandran C | 0d0d2bb | 2005-10-13 11:43:02 -0700 | [diff] [blame] | 1389 | fraggap = skb_prev->len - maxfraglen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
| 1391 | alloclen = fragheaderlen + hh_len + fraggap + 15; |
| 1392 | skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); |
| 1393 | if (unlikely(!skb)) { |
| 1394 | err = -ENOBUFS; |
| 1395 | goto error; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * Fill in the control structures |
| 1400 | */ |
| 1401 | skb->ip_summed = CHECKSUM_NONE; |
| 1402 | skb->csum = 0; |
| 1403 | skb_reserve(skb, hh_len); |
| 1404 | |
| 1405 | /* |
| 1406 | * Find where to start putting bytes. |
| 1407 | */ |
Arnaldo Carvalho de Melo | 967b05f | 2007-03-13 13:51:52 -0300 | [diff] [blame] | 1408 | skb_put(skb, fragheaderlen + fraggap); |
Arnaldo Carvalho de Melo | 2ca9e6f | 2007-03-10 19:15:25 -0300 | [diff] [blame] | 1409 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1410 | skb->transport_header = (skb->network_header + |
| 1411 | fragheaderlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | if (fraggap) { |
Arnaldo Carvalho de Melo | 967b05f | 2007-03-13 13:51:52 -0300 | [diff] [blame] | 1413 | skb->csum = skb_copy_and_csum_bits(skb_prev, |
| 1414 | maxfraglen, |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1415 | skb_transport_header(skb), |
Al Viro | 8d5930d | 2020-07-10 20:07:10 -0400 | [diff] [blame] | 1416 | fraggap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | skb_prev->csum = csum_sub(skb_prev->csum, |
| 1418 | skb->csum); |
Herbert Xu | e9fa4f7 | 2006-08-13 20:12:58 -0700 | [diff] [blame] | 1419 | pskb_trim_unique(skb_prev, maxfraglen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Put the packet on the pending queue. |
| 1424 | */ |
| 1425 | __skb_queue_tail(&sk->sk_write_queue, skb); |
| 1426 | continue; |
| 1427 | } |
| 1428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | if (len > size) |
| 1430 | len = size; |
Hannes Frederic Sowa | be12a1f | 2015-05-21 16:59:58 +0200 | [diff] [blame] | 1431 | |
| 1432 | if (skb_append_pagefrags(skb, page, offset, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | err = -EMSGSIZE; |
| 1434 | goto error; |
| 1435 | } |
| 1436 | |
| 1437 | if (skb->ip_summed == CHECKSUM_NONE) { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 1438 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | csum = csum_page(page, offset, len); |
| 1440 | skb->csum = csum_block_add(skb->csum, csum, skb->len); |
| 1441 | } |
| 1442 | |
| 1443 | skb->len += len; |
| 1444 | skb->data_len += len; |
David S. Miller | 1e34a11 | 2008-01-22 23:44:31 -0800 | [diff] [blame] | 1445 | skb->truesize += len; |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 1446 | refcount_add(len, &sk->sk_wmem_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | offset += len; |
| 1448 | size -= len; |
| 1449 | } |
| 1450 | return 0; |
| 1451 | |
| 1452 | error: |
David S. Miller | bdc712b | 2011-05-06 15:02:07 -0700 | [diff] [blame] | 1453 | cork->length -= size; |
Pavel Emelyanov | 5e38e27 | 2008-07-16 20:19:49 -0700 | [diff] [blame] | 1454 | IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | return err; |
| 1456 | } |
| 1457 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1458 | static void ip_cork_release(struct inet_cork *cork) |
Pavel Emelyanov | 429f08e | 2007-11-05 21:03:24 -0800 | [diff] [blame] | 1459 | { |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1460 | cork->flags &= ~IPCORK_OPT; |
| 1461 | kfree(cork->opt); |
| 1462 | cork->opt = NULL; |
| 1463 | dst_release(cork->dst); |
| 1464 | cork->dst = NULL; |
Pavel Emelyanov | 429f08e | 2007-11-05 21:03:24 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * Combined all pending IP fragments on the socket as one IP datagram |
| 1469 | * and push them out. |
| 1470 | */ |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1471 | struct sk_buff *__ip_make_skb(struct sock *sk, |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1472 | struct flowi4 *fl4, |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1473 | struct sk_buff_head *queue, |
| 1474 | struct inet_cork *cork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
| 1476 | struct sk_buff *skb, *tmp_skb; |
| 1477 | struct sk_buff **tail_skb; |
| 1478 | struct inet_sock *inet = inet_sk(sk); |
Pavel Emelyanov | 0388b00 | 2008-07-14 23:00:43 -0700 | [diff] [blame] | 1479 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | struct ip_options *opt = NULL; |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1481 | struct rtable *rt = (struct rtable *)cork->dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | struct iphdr *iph; |
Alexey Dobriyan | 76ab608 | 2006-01-06 13:24:29 -0800 | [diff] [blame] | 1483 | __be16 df = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | __u8 ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1486 | skb = __skb_dequeue(queue); |
| 1487 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | goto out; |
| 1489 | tail_skb = &(skb_shinfo(skb)->frag_list); |
| 1490 | |
| 1491 | /* move skb->data to ip header from ext header */ |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1492 | if (skb->data < skb_network_header(skb)) |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 1493 | __skb_pull(skb, skb_network_offset(skb)); |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1494 | while ((tmp_skb = __skb_dequeue(queue)) != NULL) { |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 1495 | __skb_pull(tmp_skb, skb_network_header_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | *tail_skb = tmp_skb; |
| 1497 | tail_skb = &(tmp_skb->next); |
| 1498 | skb->len += tmp_skb->len; |
| 1499 | skb->data_len += tmp_skb->len; |
| 1500 | skb->truesize += tmp_skb->truesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | tmp_skb->destructor = NULL; |
| 1502 | tmp_skb->sk = NULL; |
| 1503 | } |
| 1504 | |
| 1505 | /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow |
| 1506 | * to fragment the frame generated here. No matter, what transforms |
| 1507 | * how transforms change size of the packet, it will come out. |
| 1508 | */ |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 1509 | skb->ignore_df = ip_sk_ignore_df(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
| 1511 | /* DF bit is set when we want to see DF on outgoing frames. |
WANG Cong | 60ff746 | 2014-05-04 16:39:18 -0700 | [diff] [blame] | 1512 | * If ignore_df is set too, we still allow to fragment this frame |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | * locally. */ |
Hannes Frederic Sowa | 482fc60 | 2013-11-05 02:24:17 +0100 | [diff] [blame] | 1514 | if (inet->pmtudisc == IP_PMTUDISC_DO || |
| 1515 | inet->pmtudisc == IP_PMTUDISC_PROBE || |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1516 | (skb->len <= dst_mtu(&rt->dst) && |
| 1517 | ip_dont_fragment(sk, &rt->dst))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | df = htons(IP_DF); |
| 1519 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1520 | if (cork->flags & IPCORK_OPT) |
| 1521 | opt = cork->opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 1523 | if (cork->ttl != 0) |
| 1524 | ttl = cork->ttl; |
| 1525 | else if (rt->rt_type == RTN_MULTICAST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | ttl = inet->mc_ttl; |
| 1527 | else |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1528 | ttl = ip_select_ttl(inet, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
Ansis Atteka | 749154a | 2013-09-18 15:29:52 -0700 | [diff] [blame] | 1530 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | iph->version = 4; |
| 1532 | iph->ihl = 5; |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 1533 | iph->tos = (cork->tos != -1) ? cork->tos : inet->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | iph->frag_off = df; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | iph->ttl = ttl; |
| 1536 | iph->protocol = sk->sk_protocol; |
Eric Dumazet | 84f9307 | 2011-11-30 19:00:53 +0000 | [diff] [blame] | 1537 | ip_copy_addrs(iph, fl4); |
Hannes Frederic Sowa | b6a7719 | 2015-03-25 17:07:44 +0100 | [diff] [blame] | 1538 | ip_select_ident(net, skb, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
David S. Miller | 22f728f | 2011-05-13 17:21:27 -0400 | [diff] [blame] | 1540 | if (opt) { |
Miaohe Lin | 5af6889 | 2020-08-29 05:21:30 -0400 | [diff] [blame] | 1541 | iph->ihl += opt->optlen >> 2; |
David S. Miller | 22f728f | 2011-05-13 17:21:27 -0400 | [diff] [blame] | 1542 | ip_options_build(skb, opt, cork->addr, rt, 0); |
| 1543 | } |
| 1544 | |
Francesco Fusco | aa66158 | 2013-09-24 15:43:09 +0200 | [diff] [blame] | 1545 | skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority; |
Willem de Bruijn | c6af0c2 | 2019-09-11 15:50:51 -0400 | [diff] [blame] | 1546 | skb->mark = cork->mark; |
Jesus Sanchez-Palencia | bc969a9 | 2018-07-03 15:42:49 -0700 | [diff] [blame] | 1547 | skb->tstamp = cork->transmit_time; |
Eric Dumazet | a21bba9 | 2008-11-24 16:07:50 -0800 | [diff] [blame] | 1548 | /* |
| 1549 | * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec |
| 1550 | * on dst refcount |
| 1551 | */ |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1552 | cork->dst = NULL; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1553 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
David L Stevens | 96793b4 | 2007-09-17 09:57:33 -0700 | [diff] [blame] | 1555 | if (iph->protocol == IPPROTO_ICMP) |
Pavel Emelyanov | 0388b00 | 2008-07-14 23:00:43 -0700 | [diff] [blame] | 1556 | icmp_out_count(net, ((struct icmphdr *) |
David L Stevens | 96793b4 | 2007-09-17 09:57:33 -0700 | [diff] [blame] | 1557 | skb_transport_header(skb))->type); |
| 1558 | |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1559 | ip_cork_release(cork); |
| 1560 | out: |
| 1561 | return skb; |
| 1562 | } |
| 1563 | |
Eric Dumazet | b5ec8ee | 2012-08-10 02:22:47 +0000 | [diff] [blame] | 1564 | int ip_send_skb(struct net *net, struct sk_buff *skb) |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1565 | { |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1566 | int err; |
| 1567 | |
Eric W. Biederman | 33224b1 | 2015-10-07 16:48:46 -0500 | [diff] [blame] | 1568 | err = ip_local_out(net, skb->sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | if (err) { |
| 1570 | if (err > 0) |
Eric Dumazet | 6ce9e7b | 2009-09-02 18:05:33 -0700 | [diff] [blame] | 1571 | err = net_xmit_errno(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | if (err) |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1573 | IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1579 | int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4) |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1580 | { |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1581 | struct sk_buff *skb; |
| 1582 | |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1583 | skb = ip_finish_skb(sk, fl4); |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1584 | if (!skb) |
| 1585 | return 0; |
| 1586 | |
| 1587 | /* Netfilter gets whole the not fragmented skb. */ |
Eric Dumazet | b5ec8ee | 2012-08-10 02:22:47 +0000 | [diff] [blame] | 1588 | return ip_send_skb(sock_net(sk), skb); |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | * Throw away all pending data on the socket. |
| 1593 | */ |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1594 | static void __ip_flush_pending_frames(struct sock *sk, |
| 1595 | struct sk_buff_head *queue, |
| 1596 | struct inet_cork *cork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | struct sk_buff *skb; |
| 1599 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1600 | while ((skb = __skb_dequeue_tail(queue)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | kfree_skb(skb); |
| 1602 | |
Herbert Xu | 1470ddf | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1603 | ip_cork_release(cork); |
| 1604 | } |
| 1605 | |
| 1606 | void ip_flush_pending_frames(struct sock *sk) |
| 1607 | { |
David S. Miller | bdc712b | 2011-05-06 15:02:07 -0700 | [diff] [blame] | 1608 | __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1611 | struct sk_buff *ip_make_skb(struct sock *sk, |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1612 | struct flowi4 *fl4, |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1613 | int getfrag(void *from, char *to, int offset, |
| 1614 | int len, int odd, struct sk_buff *skb), |
| 1615 | void *from, int length, int transhdrlen, |
| 1616 | struct ipcm_cookie *ipc, struct rtable **rtp, |
Willem de Bruijn | 1cd7884 | 2018-04-26 13:42:15 -0400 | [diff] [blame] | 1617 | struct inet_cork *cork, unsigned int flags) |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1618 | { |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1619 | struct sk_buff_head queue; |
| 1620 | int err; |
| 1621 | |
| 1622 | if (flags & MSG_PROBE) |
| 1623 | return NULL; |
| 1624 | |
| 1625 | __skb_queue_head_init(&queue); |
| 1626 | |
Willem de Bruijn | 1cd7884 | 2018-04-26 13:42:15 -0400 | [diff] [blame] | 1627 | cork->flags = 0; |
| 1628 | cork->addr = 0; |
| 1629 | cork->opt = NULL; |
| 1630 | err = ip_setup_cork(sk, cork, ipc, rtp); |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1631 | if (err) |
| 1632 | return ERR_PTR(err); |
| 1633 | |
Willem de Bruijn | 1cd7884 | 2018-04-26 13:42:15 -0400 | [diff] [blame] | 1634 | err = __ip_append_data(sk, fl4, &queue, cork, |
Eric Dumazet | 5640f76 | 2012-09-23 23:04:42 +0000 | [diff] [blame] | 1635 | ¤t->task_frag, getfrag, |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1636 | from, length, transhdrlen, flags); |
| 1637 | if (err) { |
Willem de Bruijn | 1cd7884 | 2018-04-26 13:42:15 -0400 | [diff] [blame] | 1638 | __ip_flush_pending_frames(sk, &queue, cork); |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1639 | return ERR_PTR(err); |
| 1640 | } |
| 1641 | |
Willem de Bruijn | 1cd7884 | 2018-04-26 13:42:15 -0400 | [diff] [blame] | 1642 | return __ip_make_skb(sk, fl4, &queue, cork); |
Herbert Xu | 1c32c5a | 2011-03-01 02:36:47 +0000 | [diff] [blame] | 1643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | |
| 1645 | /* |
| 1646 | * Fetch data from kernel space and fill in checksum if needed. |
| 1647 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1648 | static int ip_reply_glue_bits(void *dptr, char *to, int offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | int len, int odd, struct sk_buff *skb) |
| 1650 | { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 1651 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
Al Viro | cc44c17 | 2020-07-11 00:12:07 -0400 | [diff] [blame] | 1653 | csum = csum_partial_copy_nocheck(dptr+offset, to, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | skb->csum = csum_block_add(skb->csum, csum, odd); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1655 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1658 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | * Generic function to send a packet as reply to another packet. |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1660 | * Used to send some TCP resets/acks so far. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | */ |
Eric Dumazet | bdbbb85 | 2015-01-29 21:35:05 -0800 | [diff] [blame] | 1662 | void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, |
Eric Dumazet | 24a2d43 | 2014-09-27 09:50:55 -0700 | [diff] [blame] | 1663 | const struct ip_options *sopt, |
| 1664 | __be32 daddr, __be32 saddr, |
| 1665 | const struct ip_reply_arg *arg, |
Eric Dumazet | d6fb396 | 2019-06-13 21:22:35 -0700 | [diff] [blame] | 1666 | unsigned int len, u64 transmit_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | { |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1668 | struct ip_options_data replyopts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | struct ipcm_cookie ipc; |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1670 | struct flowi4 fl4; |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 1671 | struct rtable *rt = skb_rtable(skb); |
Eric Dumazet | bdbbb85 | 2015-01-29 21:35:05 -0800 | [diff] [blame] | 1672 | struct net *net = sock_net(sk); |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1673 | struct sk_buff *nskb; |
Vasily Averin | 4062090 | 2014-10-15 16:24:02 +0400 | [diff] [blame] | 1674 | int err; |
David Ahern | f7ba868 | 2015-08-13 14:59:08 -0600 | [diff] [blame] | 1675 | int oif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 1677 | if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | return; |
| 1679 | |
Willem de Bruijn | 3517820 | 2018-07-06 10:12:54 -0400 | [diff] [blame] | 1680 | ipcm_init(&ipc); |
David S. Miller | 0a5ebb8 | 2011-05-09 13:22:43 -0700 | [diff] [blame] | 1681 | ipc.addr = daddr; |
Eric Dumazet | d6fb396 | 2019-06-13 21:22:35 -0700 | [diff] [blame] | 1682 | ipc.sockc.transmit_time = transmit_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1684 | if (replyopts.opt.opt.optlen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | ipc.opt = &replyopts.opt; |
| 1686 | |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1687 | if (replyopts.opt.opt.srr) |
| 1688 | daddr = replyopts.opt.opt.faddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
David Ahern | f7ba868 | 2015-08-13 14:59:08 -0600 | [diff] [blame] | 1691 | oif = arg->bound_dev_if; |
David Ahern | 9b6c14d | 2016-11-09 09:07:26 -0800 | [diff] [blame] | 1692 | if (!oif && netif_index_is_l3_master(net, skb->skb_iif)) |
| 1693 | oif = skb->skb_iif; |
David Ahern | f7ba868 | 2015-08-13 14:59:08 -0600 | [diff] [blame] | 1694 | |
| 1695 | flowi4_init_output(&fl4, oif, |
Jon Maxwell | 0048369 | 2018-05-10 16:53:51 +1000 | [diff] [blame] | 1696 | IP4_REPLY_MARK(net, skb->mark) ?: sk->sk_mark, |
Eric Dumazet | 66b13d9 | 2011-10-24 03:06:21 -0400 | [diff] [blame] | 1697 | RT_TOS(arg->tos), |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1698 | RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol, |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1699 | ip_reply_arg_flowi_flags(arg), |
David S. Miller | 70e7341 | 2012-06-28 03:21:41 -0700 | [diff] [blame] | 1700 | daddr, saddr, |
Lorenzo Colitti | e2d118a | 2016-11-04 02:23:43 +0900 | [diff] [blame] | 1701 | tcp_hdr(skb)->source, tcp_hdr(skb)->dest, |
| 1702 | arg->uid); |
Paul Moore | 3df98d7 | 2020-09-27 22:38:26 -0400 | [diff] [blame] | 1703 | security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4)); |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1704 | rt = ip_route_output_key(net, &fl4); |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1705 | if (IS_ERR(rt)) |
| 1706 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
Wei Wang | ba9e04a | 2020-09-08 14:09:34 -0700 | [diff] [blame] | 1708 | inet_sk(sk)->tos = arg->tos & ~INET_ECN_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1710 | sk->sk_protocol = ip_hdr(skb)->protocol; |
Patrick McHardy | f0e48db | 2007-06-04 21:32:46 -0700 | [diff] [blame] | 1711 | sk->sk_bound_dev_if = arg->bound_dev_if; |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1712 | sk->sk_sndbuf = sysctl_wmem_default; |
Willem de Bruijn | 0da7536 | 2020-07-01 16:00:06 -0400 | [diff] [blame] | 1713 | ipc.sockc.mark = fl4.flowi4_mark; |
Vasily Averin | 4062090 | 2014-10-15 16:24:02 +0400 | [diff] [blame] | 1714 | err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, |
| 1715 | len, 0, &ipc, &rt, MSG_DONTWAIT); |
| 1716 | if (unlikely(err)) { |
| 1717 | ip_flush_pending_frames(sk); |
| 1718 | goto out; |
| 1719 | } |
| 1720 | |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1721 | nskb = skb_peek(&sk->sk_write_queue); |
| 1722 | if (nskb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | if (arg->csumoffset >= 0) |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1724 | *((__sum16 *)skb_transport_header(nskb) + |
| 1725 | arg->csumoffset) = csum_fold(csum_add(nskb->csum, |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1726 | arg->csum)); |
Eric Dumazet | be9f4a4 | 2012-07-19 07:34:03 +0000 | [diff] [blame] | 1727 | nskb->ip_summed = CHECKSUM_NONE; |
David S. Miller | 77968b7 | 2011-05-08 17:12:19 -0700 | [diff] [blame] | 1728 | ip_push_pending_frames(sk, &fl4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | } |
Vasily Averin | 4062090 | 2014-10-15 16:24:02 +0400 | [diff] [blame] | 1730 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | ip_rt_put(rt); |
| 1732 | } |
| 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | void __init ip_init(void) |
| 1735 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | ip_rt_init(); |
| 1737 | inet_initpeers(); |
| 1738 | |
WANG Cong | 72c1d3b | 2014-01-10 16:09:45 -0800 | [diff] [blame] | 1739 | #if defined(CONFIG_IP_MULTICAST) |
| 1740 | igmp_mc_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | #endif |
| 1742 | } |