blob: 139cec29ed06cd092ebdfd2bf0d13aaf67c5359d [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Juhl02c30a82005-05-05 16:16:16 -07009 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 Hideakie905a9e2007-02-09 23:24:47 +090024 * Bradford Johnson: Fix faulty handling of some frames when
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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 Hideakie905a9e2007-02-09 23:24:47 +090035 * Andi Kleen : Split fast and slow ip_build_xmit path
36 * for decreased register pressure on x86
Bhaskar Chowdhurya66e04c2021-03-27 04:42:38 +053037 * and more readability.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 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 Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
51#include <linux/string.h>
52#include <linux/errno.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040053#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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 McHardycfacb052006-01-08 22:36:54 -080070#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/skbuff.h>
72#include <net/sock.h>
73#include <net/arp.h>
74#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <net/checksum.h>
76#include <net/inetpeer.h>
Wei Wangba9e04a2020-09-08 14:09:34 -070077#include <net/inet_ecn.h>
Roopa Prabhu14972cb2016-08-24 20:10:43 -070078#include <net/lwtunnel.h>
Daniel Mack33b48672016-11-23 16:52:29 +010079#include <linux/bpf-cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/igmp.h>
81#include <linux/netfilter_ipv4.h>
82#include <linux/netfilter_bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/netlink.h>
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -070084#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Eric W. Biederman694869b2015-06-12 21:55:31 -050086static int
87ip_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 Zhou49d16b22015-05-15 14:15:37 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Generate a checksum for an outgoing IP datagram. */
Denis Efremov2fbd9672013-05-08 23:19:42 +000092void ip_send_check(struct iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 iph->check = 0;
95 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
96}
Eric Dumazet4bc2f182010-07-09 21:22:10 +000097EXPORT_SYMBOL(ip_send_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Eric W. Biedermancf91a992015-10-07 16:48:45 -050099int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xuc439cb22008-01-11 19:14:00 -0800100{
101 struct iphdr *iph = ip_hdr(skb);
102
103 iph->tot_len = htons(skb->len);
104 ip_send_check(iph);
David Aherna8e3e1a2016-09-10 12:09:53 -0700105
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 Cooperf4180432016-12-01 10:05:10 +0800113 skb->protocol = htons(ETH_P_IP);
114
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500115 return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
116 net, sk, skb, NULL, skb_dst(skb)->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500117 dst_output);
David Miller7026b1d2015-04-05 22:19:04 -0400118}
119
Eric W. Biederman33224b12015-10-07 16:48:46 -0500120int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xuc439cb22008-01-11 19:14:00 -0800121{
122 int err;
123
Eric W. Biedermancf91a992015-10-07 16:48:45 -0500124 err = __ip_local_out(net, sk, skb);
Herbert Xuc439cb22008-01-11 19:14:00 -0800125 if (likely(err == 1))
Eric W. Biederman13206b62015-10-07 16:48:35 -0500126 err = dst_output(net, sk, skb);
Herbert Xuc439cb22008-01-11 19:14:00 -0800127
128 return err;
129}
Eric W. Biedermane2cb77d2015-10-07 16:48:38 -0500130EXPORT_SYMBOL_GPL(ip_local_out);
Herbert Xuc439cb22008-01-11 19:14:00 -0800131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static 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. Miller323e1262010-12-12 21:55:08 -0800137 ttl = ip4_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return ttl;
139}
140
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * Add an ip header to a skbuff and send it out.
143 *
144 */
Eric Dumazetcfe673b2015-09-25 07:39:16 -0700145int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
Wei Wangde033b7d2020-09-09 17:50:47 -0700146 __be32 saddr, __be32 daddr, struct ip_options_rcu *opt,
147 u8 tos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000150 struct rtable *rt = skb_rtable(skb);
Eric W. Biederman77589ce2015-10-07 16:48:42 -0500151 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct iphdr *iph;
153
154 /* Build the IP header. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000155 skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -0300156 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700157 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 iph->version = 4;
159 iph->ihl = 5;
Wei Wangde033b7d2020-09-09 17:50:47 -0700160 iph->tos = tos;
Changli Gaod8d1f302010-06-10 23:31:35 -0700161 iph->ttl = ip_select_ttl(inet, &rt->dst);
David S. Millerdd927a22011-05-04 12:03:30 -0700162 iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
163 iph->saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 iph->protocol = sk->sk_protocol;
Eric Dumazet970a5a32022-01-26 17:10:21 -0800165 /* Do not bother generating IPID for small packets (eg SYNACK) */
166 if (skb->len <= IPV4_MIN_MTU || ip_dont_fragment(sk, &rt->dst)) {
Eric Dumazetcfe673b2015-09-25 07:39:16 -0700167 iph->frag_off = htons(IP_DF);
168 iph->id = 0;
169 } else {
170 iph->frag_off = 0;
Eric Dumazet970a5a32022-01-26 17:10:21 -0800171 /* TCP packets here are SYNACK with fat IPv4/TCP options.
172 * Avoid using the hashed IP ident generator.
173 */
174 if (sk->sk_protocol == IPPROTO_TCP)
175 iph->id = (__force __be16)prandom_u32();
176 else
177 __ip_select_ident(net, iph, 1);
Eric Dumazetcfe673b2015-09-25 07:39:16 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000180 if (opt && opt->opt.optlen) {
181 iph->ihl += opt->opt.optlen>>2;
182 ip_options_build(skb, &opt->opt, daddr, rt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 skb->priority = sk->sk_priority;
Jamal Hadi Salime05a90e2017-07-03 09:51:50 -0400186 if (!skb->mark)
187 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* Send it out. */
Eric W. Biederman33224b12015-10-07 16:48:46 -0500190 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Arnaldo Carvalho de Melod8c97a92005-08-09 20:12:12 -0700192EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
193
Eric W. Biederman694869b2015-06-12 21:55:31 -0500194static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Eric Dumazetadf30902009-06-02 05:19:30 +0000196 struct dst_entry *dst = skb_dst(skb);
Mitsuru Chinen80787eb2007-04-30 00:48:20 -0700197 struct rtable *rt = (struct rtable *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct net_device *dev = dst->dev;
Chuck Leverc2636b42007-10-23 21:07:32 -0700199 unsigned int hh_len = LL_RESERVED_SPACE(dev);
David S. Millerf6b72b622011-07-14 07:53:20 -0700200 struct neighbour *neigh;
David Ahern5c9f7c12019-04-05 16:30:34 -0700201 bool is_v6gw = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Neil Hormanedf391f2009-04-27 02:45:02 -0700203 if (rt->rt_type == RTN_MULTICAST) {
Eric W. Biederman4ba1bf42015-09-15 20:04:01 -0500204 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
Neil Hormanedf391f2009-04-27 02:45:02 -0700205 } else if (rt->rt_type == RTN_BROADCAST)
Eric W. Biederman4ba1bf42015-09-15 20:04:01 -0500206 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
Mitsuru Chinen80787eb2007-04-30 00:48:20 -0700207
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700208 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
Vasily Averin5678a592021-08-02 11:52:35 +0300209 skb = skb_expand_head(skb, hh_len);
210 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700214 if (lwtunnel_xmit_redirect(dst->lwtstate)) {
215 int res = lwtunnel_xmit(skb);
216
217 if (res < 0 || res == LWTUNNEL_XMIT_DONE)
218 return res;
219 }
220
David S. Millera263b302012-07-02 02:02:15 -0700221 rcu_read_lock_bh();
David Ahern5c9f7c12019-04-05 16:30:34 -0700222 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
Vasiliy Kulikov9871f1a2012-08-06 03:55:29 +0000223 if (!IS_ERR(neigh)) {
Julian Anastasov4ff06202017-02-06 23:14:12 +0200224 int res;
225
226 sock_confirm_neigh(skb, neigh);
David Ahern5c9f7c12019-04-05 16:30:34 -0700227 /* if crossing protocols, can not use the cached header */
228 res = neigh_output(neigh, skb, is_v6gw);
David S. Millera263b302012-07-02 02:02:15 -0700229 rcu_read_unlock_bh();
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000230 return res;
231 }
David S. Millera263b302012-07-02 02:02:15 -0700232 rcu_read_unlock_bh();
David S. Miller05e3aa02011-07-16 17:26:00 -0700233
Joe Perchese87cc472012-05-13 21:56:26 +0000234 net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
235 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 kfree_skb(skb);
237 return -EINVAL;
238}
239
Eric W. Biederman694869b2015-06-12 21:55:31 -0500240static int ip_finish_output_gso(struct net *net, struct sock *sk,
241 struct sk_buff *skb, unsigned int mtu)
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200242{
Jason A. Donenfeld88bebdf2020-01-13 18:42:31 -0500243 struct sk_buff *segs, *nskb;
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200244 netdev_features_t features;
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200245 int ret = 0;
246
Lance Richardson9ee6c5d2016-11-02 16:36:17 -0400247 /* common case: seglen is <= mtu
Shmulik Ladkani359ebda2016-07-18 14:49:33 +0300248 */
Daniel Axtens779b7932018-03-01 17:13:37 +1100249 if (skb_gso_validate_network_len(skb, mtu))
Eric W. Biederman694869b2015-06-12 21:55:31 -0500250 return ip_finish_output2(net, sk, skb);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200251
Lance Richardson0ace81e2016-11-09 15:04:39 -0500252 /* Slowpath - GSO segment length exceeds the egress MTU.
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200253 *
Lance Richardson0ace81e2016-11-09 15:04:39 -0500254 * This can happen in several cases:
255 * - Forwarding of a TCP GRO skb, when DF flag is not set.
256 * - Forwarding of an skb that arrived on a virtualization interface
257 * (virtio-net/vhost/tap) with TSO/GSO size set by other network
258 * stack.
259 * - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an
260 * interface with a smaller MTU.
261 * - Arriving GRO skb (or GSO skb in a virtualized environment) that is
262 * bridged to a NETIF_F_TSO tunnel stacked over an interface with an
Bhaskar Chowdhurya66e04c2021-03-27 04:42:38 +0530263 * insufficient MTU.
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200264 */
265 features = netif_skb_features(skb);
Cambda Zhua08e7fd2020-03-26 15:33:14 +0800266 BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200267 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
Florian Westphal330966e2014-10-20 13:49:17 +0200268 if (IS_ERR_OR_NULL(segs)) {
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200269 kfree_skb(skb);
270 return -ENOMEM;
271 }
272
273 consume_skb(skb);
274
Jason A. Donenfeld88bebdf2020-01-13 18:42:31 -0500275 skb_list_walk_safe(segs, segs, nskb) {
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200276 int err;
277
David S. Millera8305bf2018-07-29 20:42:53 -0700278 skb_mark_not_on_list(segs);
Eric W. Biederman694869b2015-06-12 21:55:31 -0500279 err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200280
281 if (err && ret == 0)
282 ret = err;
Jason A. Donenfeld88bebdf2020-01-13 18:42:31 -0500283 }
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200284
285 return ret;
286}
287
brakmo956fe212019-05-28 16:59:38 -0700288static int __ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Florian Westphalc5501eb2015-05-22 16:32:50 +0200290 unsigned int mtu;
291
Patrick McHardy5c901da2006-01-06 23:05:36 -0800292#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
293 /* Policy lookup after SNAT yielded a new policy */
Ian Morris00db4122015-04-03 09:17:27 +0100294 if (skb_dst(skb)->xfrm) {
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800295 IPCB(skb)->flags |= IPSKB_REROUTED;
Eric W. Biederman13206b62015-10-07 16:48:35 -0500296 return dst_output(net, sk, skb);
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800297 }
Patrick McHardy5c901da2006-01-06 23:05:36 -0800298#endif
Shmulik Ladkanifedbb6b42016-06-29 21:47:03 +0300299 mtu = ip_skb_dst_mtu(sk, skb);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200300 if (skb_is_gso(skb))
Eric W. Biederman694869b2015-06-12 21:55:31 -0500301 return ip_finish_output_gso(net, sk, skb, mtu);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200302
Florian Westphalbb4cc1a12021-01-06 00:15:23 +0100303 if (skb->len > mtu || IPCB(skb)->frag_max_size)
Eric W. Biederman694869b2015-06-12 21:55:31 -0500304 return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200305
Eric W. Biederman694869b2015-06-12 21:55:31 -0500306 return ip_finish_output2(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
brakmo956fe212019-05-28 16:59:38 -0700309static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
310{
311 int ret;
312
313 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
314 switch (ret) {
315 case NET_XMIT_SUCCESS:
316 return __ip_finish_output(net, sk, skb);
317 case NET_XMIT_CN:
318 return __ip_finish_output(net, sk, skb) ? : ret;
319 default:
320 kfree_skb(skb);
321 return ret;
322 }
323}
324
Daniel Mack33b48672016-11-23 16:52:29 +0100325static int ip_mc_finish_output(struct net *net, struct sock *sk,
326 struct sk_buff *skb)
327{
Stephen Suryaputra5b18f122019-06-26 02:21:16 -0400328 struct rtable *new_rt;
David S. Millerd96ff262019-06-27 21:06:39 -0700329 bool do_cn = false;
330 int ret, err;
Daniel Mack33b48672016-11-23 16:52:29 +0100331
332 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
brakmo956fe212019-05-28 16:59:38 -0700333 switch (ret) {
brakmo956fe212019-05-28 16:59:38 -0700334 case NET_XMIT_CN:
David S. Millerd96ff262019-06-27 21:06:39 -0700335 do_cn = true;
Joe Perchesa8eceea2020-03-12 15:50:22 -0700336 fallthrough;
David S. Millerd96ff262019-06-27 21:06:39 -0700337 case NET_XMIT_SUCCESS:
338 break;
brakmo956fe212019-05-28 16:59:38 -0700339 default:
Daniel Mack33b48672016-11-23 16:52:29 +0100340 kfree_skb(skb);
341 return ret;
342 }
343
Stephen Suryaputra5b18f122019-06-26 02:21:16 -0400344 /* Reset rt_iif so that inet_iif() will return skb->skb_iif. Setting
345 * this to non-zero causes ipi_ifindex in in_pktinfo to be overwritten,
346 * see ipv4_pktinfo_prepare().
347 */
348 new_rt = rt_dst_clone(net->loopback_dev, skb_rtable(skb));
349 if (new_rt) {
350 new_rt->rt_iif = 0;
351 skb_dst_drop(skb);
352 skb_dst_set(skb, &new_rt->dst);
353 }
354
David S. Millerd96ff262019-06-27 21:06:39 -0700355 err = dev_loopback_xmit(net, sk, skb);
356 return (do_cn && err) ? ret : err;
Daniel Mack33b48672016-11-23 16:52:29 +0100357}
358
Eric W. Biedermanede20592015-10-07 16:48:47 -0500359int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Eric Dumazet511c3f92009-06-02 05:14:27 +0000361 struct rtable *rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700362 struct net_device *dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /*
365 * If the indicated interface is up and running, send the packet.
366 */
Eric W. Biederman88f5cc22015-09-15 20:03:57 -0500367 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 skb->dev = dev;
370 skb->protocol = htons(ETH_P_IP);
371
372 /*
373 * Multicasts are looped back for other local users
374 */
375
376 if (rt->rt_flags&RTCF_MULTICAST) {
Octavian Purdila7ad68482010-01-06 20:37:01 -0800377 if (sk_mc_loop(sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#ifdef CONFIG_IP_MROUTE
379 /* Small optimization: do not loopback not local frames,
380 which returned after forwarding; they will be dropped
381 by ip_mr_input in any case.
382 Note, that local frames are looped back to be delivered
383 to local recipients.
384
385 This check is duplicated in ip_mr_input at the moment.
386 */
Joe Perches9d4fb272009-11-23 10:41:23 -0800387 &&
388 ((rt->rt_flags & RTCF_LOCAL) ||
389 !(IPCB(skb)->flags & IPSKB_FORWARDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif
Joe Perches9d4fb272009-11-23 10:41:23 -0800391 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
393 if (newskb)
Jan Engelhardt9bbc7682010-03-23 04:07:29 +0100394 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500395 net, sk, newskb, NULL, newskb->dev,
Daniel Mack33b48672016-11-23 16:52:29 +0100396 ip_mc_finish_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /* Multicasts with ttl 0 must not go beyond the host */
400
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700401 if (ip_hdr(skb)->ttl == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 kfree_skb(skb);
403 return 0;
404 }
405 }
406
407 if (rt->rt_flags&RTCF_BROADCAST) {
408 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
409 if (newskb)
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500410 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
411 net, sk, newskb, NULL, newskb->dev,
Daniel Mack33b48672016-11-23 16:52:29 +0100412 ip_mc_finish_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500415 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
416 net, sk, skb, NULL, skb->dev,
417 ip_finish_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800418 !(IPCB(skb)->flags & IPSKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Eric W. Biedermanede20592015-10-07 16:48:47 -0500421int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100423 struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev;
Patrick McHardy1bd9bef2006-01-05 12:20:59 -0800424
Eric W. Biederman88f5cc22015-09-15 20:03:57 -0500425 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Patrick McHardy1bd9bef2006-01-05 12:20:59 -0800427 skb->dev = dev;
428 skb->protocol = htons(ETH_P_IP);
429
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500430 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100431 net, sk, skb, indev, dev,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900432 ip_finish_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800433 !(IPCB(skb)->flags & IPSKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
Brian Vazquez6585d7dc2021-02-01 17:41:30 +0000435EXPORT_SYMBOL(ip_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Eric Dumazet84f93072011-11-30 19:00:53 +0000437/*
438 * copy saddr and daddr, possibly using 64bit load/stores
439 * Equivalent to :
440 * iph->saddr = fl4->saddr;
441 * iph->daddr = fl4->daddr;
442 */
443static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
444{
445 BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
446 offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
Gustavo A. R. Silva6321c7a2021-07-26 14:52:51 -0500447
448 iph->saddr = fl4->saddr;
449 iph->daddr = fl4->daddr;
Eric Dumazet84f93072011-11-30 19:00:53 +0000450}
451
Eric Dumazetb0270e92014-04-15 12:58:34 -0400452/* Note: skb->sk can be different from sk, in case of tunnels */
Xin Long69b9e1e2018-07-02 18:21:11 +0800453int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
454 __u8 tos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct inet_sock *inet = inet_sk(sk);
Eric W. Biederman77589ce2015-10-07 16:48:42 -0500457 struct net *net = sock_net(sk);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000458 struct ip_options_rcu *inet_opt;
David S. Millerb57ae012011-05-06 16:24:06 -0700459 struct flowi4 *fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct rtable *rt;
461 struct iphdr *iph;
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000462 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* Skip all of this if the packet is already routed,
465 * f.e. by something like SCTP.
466 */
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000467 rcu_read_lock();
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000468 inet_opt = rcu_dereference(inet->inet_opt);
David S. Millerea4fc0d2011-05-06 22:30:20 -0700469 fl4 = &fl->u.ip4;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000470 rt = skb_rtable(skb);
Ian Morris00db4122015-04-03 09:17:27 +0100471 if (rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto packet_routed;
473
474 /* Make sure we can route this packet. */
475 rt = (struct rtable *)__sk_dst_check(sk, 0);
Ian Morris51456b22015-04-03 09:17:26 +0100476 if (!rt) {
Al Viro3ca3c682006-09-27 18:28:07 -0700477 __be32 daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* Use correct destination address if we have options. */
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000480 daddr = inet->inet_daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000481 if (inet_opt && inet_opt->opt.srr)
482 daddr = inet_opt->opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
David S. Miller78fbfd82011-03-12 00:00:52 -0500484 /* 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. Biederman77589ce2015-10-07 16:48:42 -0500488 rt = ip_route_output_ports(net, fl4, sk,
David S. Miller78fbfd82011-03-12 00:00:52 -0500489 daddr, inet->inet_saddr,
490 inet->inet_dport,
491 inet->inet_sport,
492 sk->sk_protocol,
Xin Long69b9e1e2018-07-02 18:21:11 +0800493 RT_CONN_FLAGS_TOS(sk, tos),
David S. Miller78fbfd82011-03-12 00:00:52 -0500494 sk->sk_bound_dev_if);
495 if (IS_ERR(rt))
496 goto no_route;
Changli Gaod8d1f302010-06-10 23:31:35 -0700497 sk_setup_caps(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700499 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501packet_routed:
David Ahern77d5bc72019-09-17 10:39:49 -0700502 if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto no_route;
504
505 /* OK, we know where to send it, allocate and build IP header. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000506 skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -0300507 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700508 iph = ip_hdr(skb);
Xin Long69b9e1e2018-07-02 18:21:11 +0800509 *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (tos & 0xff));
WANG Cong60ff7462014-05-04 16:39:18 -0700510 if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 iph->frag_off = htons(IP_DF);
512 else
513 iph->frag_off = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -0700514 iph->ttl = ip_select_ttl(inet, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 iph->protocol = sk->sk_protocol;
Eric Dumazet84f93072011-11-30 19:00:53 +0000516 ip_copy_addrs(iph, fl4);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Transport layer set skb->h.foo itself. */
519
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000520 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 Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Eric W. Biederman77589ce2015-10-07 16:48:42 -0500525 ip_select_ident_segs(net, skb, sk,
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100526 skb_shinfo(skb)->gso_segs ?: 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Eric Dumazetb0270e92014-04-15 12:58:34 -0400528 /* TODO : should we use skb->sk here instead of sk ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800530 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Eric W. Biederman33224b12015-10-07 16:48:46 -0500532 res = ip_local_out(net, sk, skb);
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000533 rcu_read_unlock();
534 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536no_route:
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000537 rcu_read_unlock();
Eric W. Biederman77589ce2015-10-07 16:48:42 -0500538 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 kfree_skb(skb);
540 return -EHOSTUNREACH;
541}
Xin Long69b9e1e2018-07-02 18:21:11 +0800542EXPORT_SYMBOL(__ip_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Eric Dumazet05e22e82020-06-19 12:12:34 -0700544int 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}
548EXPORT_SYMBOL(ip_queue_xmit);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static 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 Ladkanid2f0c962019-04-29 16:39:30 +0300555 to->skb_iif = from->skb_iif;
Eric Dumazetadf30902009-06-02 05:19:30 +0000556 skb_dst_drop(to);
Eric Dumazetfe76cda2010-07-01 23:48:22 +0000557 skb_dst_copy(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800559 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Paolo Abeni3dd1c9a2018-07-23 16:50:48 +0200561 skb_copy_hash(to, from);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#ifdef CONFIG_NET_SCHED
564 to->tc_index = from->tc_index;
565#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700566 nf_copy(to, from);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100567 skb_ext_copy(to, from);
Javier Martinez Canillas6ca40d42016-09-09 08:43:16 -0400568#if IS_ENABLED(CONFIG_IP_VS)
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300569 to->ipvs_property = from->ipvs_property;
570#endif
James Morris984bc162006-06-09 00:29:17 -0700571 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Eric W. Biederman694869b2015-06-12 21:55:31 -0500574static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
Florian Westphalc5501eb2015-05-22 16:32:50 +0200575 unsigned int mtu,
Eric W. Biederman694869b2015-06-12 21:55:31 -0500576 int (*output)(struct net *, struct sock *, struct sk_buff *))
Andy Zhou49d16b22015-05-15 14:15:37 -0700577{
578 struct iphdr *iph = ip_hdr(skb);
Andy Zhou49d16b22015-05-15 14:15:37 -0700579
Florian Westphald6b915e2015-05-22 16:32:51 +0200580 if ((iph->frag_off & htons(IP_DF)) == 0)
Eric W. Biederman694869b2015-06-12 21:55:31 -0500581 return ip_do_fragment(net, sk, skb, output);
Florian Westphald6b915e2015-05-22 16:32:51 +0200582
583 if (unlikely(!skb->ignore_df ||
Andy Zhou49d16b22015-05-15 14:15:37 -0700584 (IPCB(skb)->frag_max_size &&
585 IPCB(skb)->frag_max_size > mtu))) {
Eric W. Biederman9479b0a2015-09-15 20:04:00 -0500586 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Andy Zhou49d16b22015-05-15 14:15:37 -0700587 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
588 htonl(mtu));
589 kfree_skb(skb);
590 return -EMSGSIZE;
591 }
592
Eric W. Biederman694869b2015-06-12 21:55:31 -0500593 return ip_do_fragment(net, sk, skb, output);
Andy Zhou49d16b22015-05-15 14:15:37 -0700594}
595
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200596void 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 Dumazetb7034142019-06-02 11:24:18 -0700601 iter->frag = skb_shinfo(skb)->frag_list;
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200602 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}
614EXPORT_SYMBOL(ip_fraglist_init);
615
616void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter)
617{
618 unsigned int hlen = iter->hlen;
619 struct iphdr *iph = iter->iph;
620 struct sk_buff *frag;
621
622 frag = iter->frag;
623 frag->ip_summed = CHECKSUM_NONE;
624 skb_reset_transport_header(frag);
625 __skb_push(frag, hlen);
626 skb_reset_network_header(frag);
627 memcpy(skb_network_header(frag), iph, hlen);
628 iter->iph = ip_hdr(frag);
629 iph = iter->iph;
630 iph->tot_len = htons(frag->len);
631 ip_copy_metadata(frag, skb);
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200632 iter->offset += skb->len - hlen;
633 iph->frag_off = htons(iter->offset >> 3);
634 if (frag->next)
635 iph->frag_off |= htons(IP_MF);
636 /* Ready, complete checksum */
637 ip_send_check(iph);
638}
639EXPORT_SYMBOL(ip_fraglist_prepare);
640
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200641void ip_frag_init(struct sk_buff *skb, unsigned int hlen,
Eric Dumazete7a409c2019-10-19 09:26:37 -0700642 unsigned int ll_rs, unsigned int mtu, bool DF,
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200643 struct ip_frag_state *state)
644{
645 struct iphdr *iph = ip_hdr(skb);
646
Eric Dumazete7a409c2019-10-19 09:26:37 -0700647 state->DF = DF;
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200648 state->hlen = hlen;
649 state->ll_rs = ll_rs;
650 state->mtu = mtu;
651
652 state->left = skb->len - hlen; /* Space per frame */
653 state->ptr = hlen; /* Where to start from */
654
655 state->offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
656 state->not_last_frag = iph->frag_off & htons(IP_MF);
657}
658EXPORT_SYMBOL(ip_frag_init);
659
Pablo Neira Ayuso19c34012019-05-29 13:25:35 +0200660static void ip_frag_ipcb(struct sk_buff *from, struct sk_buff *to,
Yajun Dengfaf482c2021-08-23 11:17:59 +0800661 bool first_frag)
Pablo Neira Ayuso19c34012019-05-29 13:25:35 +0200662{
663 /* Copy the flags to each fragment. */
664 IPCB(to)->flags = IPCB(from)->flags;
665
Pablo Neira Ayuso19c34012019-05-29 13:25:35 +0200666 /* ANK: dirty, but effective trick. Upgrade options only if
667 * the segment to be fragmented was THE FIRST (otherwise,
668 * options are already fixed) and make it ONCE
669 * on the initial skb, so that all the following fragments
670 * will inherit fixed options.
671 */
672 if (first_frag)
673 ip_options_fragment(from);
674}
675
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200676struct sk_buff *ip_frag_next(struct sk_buff *skb, struct ip_frag_state *state)
677{
678 unsigned int len = state->left;
679 struct sk_buff *skb2;
680 struct iphdr *iph;
681
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200682 /* IF: it doesn't fit, use 'mtu' - the data space left */
683 if (len > state->mtu)
684 len = state->mtu;
685 /* IF: we are not sending up to and including the packet end
686 then align the next start on an eight byte boundary */
687 if (len < state->left) {
688 len &= ~7;
689 }
690
691 /* Allocate buffer */
692 skb2 = alloc_skb(len + state->hlen + state->ll_rs, GFP_ATOMIC);
693 if (!skb2)
694 return ERR_PTR(-ENOMEM);
695
696 /*
697 * Set up data on packet
698 */
699
700 ip_copy_metadata(skb2, skb);
701 skb_reserve(skb2, state->ll_rs);
702 skb_put(skb2, len + state->hlen);
703 skb_reset_network_header(skb2);
704 skb2->transport_header = skb2->network_header + state->hlen;
705
706 /*
707 * Charge the memory for the fragment to any owner
708 * it might possess
709 */
710
711 if (skb->sk)
712 skb_set_owner_w(skb2, skb->sk);
713
714 /*
715 * Copy the packet header into the new buffer.
716 */
717
718 skb_copy_from_linear_data(skb, skb_network_header(skb2), state->hlen);
719
720 /*
721 * Copy a block of the IP datagram.
722 */
723 if (skb_copy_bits(skb, state->ptr, skb_transport_header(skb2), len))
724 BUG();
725 state->left -= len;
726
727 /*
728 * Fill in the new header fields.
729 */
730 iph = ip_hdr(skb2);
731 iph->frag_off = htons((state->offset >> 3));
Eric Dumazete7a409c2019-10-19 09:26:37 -0700732 if (state->DF)
733 iph->frag_off |= htons(IP_DF);
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200734
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200735 /*
736 * Added AC : If we are fragmenting a fragment that's not the
737 * last fragment then keep MF on each bit
738 */
739 if (state->left > 0 || state->not_last_frag)
740 iph->frag_off |= htons(IP_MF);
741 state->ptr += len;
742 state->offset += len;
743
744 iph->tot_len = htons(len + state->hlen);
745
746 ip_send_check(iph);
747
748 return skb2;
749}
750EXPORT_SYMBOL(ip_frag_next);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * This IP datagram is too large to be sent in one piece. Break it up into
754 * smaller pieces (each of size equal to IP header plus
755 * a block of the data of the original IP data part) that will yet fit in a
756 * single device frame, and queue such a frame for sending.
757 */
758
Eric W. Biederman694869b2015-06-12 21:55:31 -0500759int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
760 int (*output)(struct net *, struct sock *, struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct sk_buff *skb2;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000764 struct rtable *rt = skb_rtable(skb);
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200765 unsigned int mtu, hlen, ll_rs;
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200766 struct ip_fraglist_iter iter;
Eric Dumazet9669fff2019-10-16 18:00:56 -0700767 ktime_t tstamp = skb->tstamp;
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200768 struct ip_frag_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int err = 0;
770
Hannes Frederic Sowadbd33932015-10-27 22:40:40 +0100771 /* for offloaded checksums cleanup checksum before fragmentation */
772 if (skb->ip_summed == CHECKSUM_PARTIAL &&
773 (err = skb_checksum_help(skb)))
774 goto fail;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * Point into the IP datagram header.
778 */
779
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700780 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Shmulik Ladkanifedbb6b42016-06-29 21:47:03 +0300782 mtu = ip_skb_dst_mtu(sk, skb);
Florian Westphald6b915e2015-05-22 16:32:51 +0200783 if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
784 mtu = IPCB(skb)->frag_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /*
787 * Setup starting values.
788 */
789
790 hlen = iph->ihl * 4;
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100791 mtu = mtu - hlen; /* Size of data space */
Herbert Xu89cee8b2005-12-13 23:14:27 -0800792 IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
Vasily Averin254d9002017-07-14 12:04:16 +0300793 ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* When frag_list is given, use it. First, check its validity:
796 * some transformers could create wrong frag_list or break existing
797 * one, it is not prohibited. In this case fall back to copying.
798 *
799 * LATER: this step can be merged to real generation of fragments,
800 * we can switch to copy when see the first bad fragment.
801 */
David S. Miller21dc3302010-08-23 00:13:46 -0700802 if (skb_has_frag_list(skb)) {
Eric Dumazet3d130082010-09-21 08:47:45 +0000803 struct sk_buff *frag, *frag2;
Alexey Dobriyanc72d8cd2016-11-19 04:08:08 +0300804 unsigned int first_len = skb_pagelen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (first_len - hlen > mtu ||
807 ((first_len - hlen) & 7) ||
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700808 ip_is_fragment(iph) ||
Vasily Averin254d9002017-07-14 12:04:16 +0300809 skb_cloned(skb) ||
810 skb_headroom(skb) < ll_rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto slow_path;
812
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700813 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Correct geometry. */
815 if (frag->len > mtu ||
816 ((frag->len & 7) && frag->next) ||
Vasily Averin254d9002017-07-14 12:04:16 +0300817 skb_headroom(frag) < hlen + ll_rs)
Eric Dumazet3d130082010-09-21 08:47:45 +0000818 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* Partially cloned skb? */
821 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000822 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700823
824 BUG_ON(frag->sk);
825 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700826 frag->sk = skb->sk;
827 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700828 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000829 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 /* Everything is OK. Generate! */
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200833 ip_fraglist_init(skb, iph, hlen, &iter);
Yajun Deng1b9fbe82021-08-30 17:16:40 +0800834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 for (;;) {
836 /* Prepare header of the next frame,
837 * before previous one went down. */
Pablo Neira Ayuso19c34012019-05-29 13:25:35 +0200838 if (iter.frag) {
Jakub Kicinski27a8caa2022-01-21 16:57:31 -0800839 bool first_frag = (iter.offset == 0);
840
Yajun Dengfaf482c2021-08-23 11:17:59 +0800841 IPCB(iter.frag)->flags = IPCB(skb)->flags;
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200842 ip_fraglist_prepare(skb, &iter);
Jakub Kicinski27a8caa2022-01-21 16:57:31 -0800843 if (first_frag && IPCB(skb)->opt.optlen) {
844 /* ipcb->opt is not populated for frags
845 * coming from __ip_make_skb(),
846 * ip_options_fragment() needs optlen
847 */
848 IPCB(iter.frag)->opt.optlen =
849 IPCB(skb)->opt.optlen;
850 ip_options_fragment(iter.frag);
851 ip_send_check(iter.iph);
852 }
Pablo Neira Ayuso19c34012019-05-29 13:25:35 +0200853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Eric Dumazet9669fff2019-10-16 18:00:56 -0700855 skb->tstamp = tstamp;
Eric W. Biederman694869b2015-06-12 21:55:31 -0500856 err = output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Wei Dongdafee492006-08-02 13:41:21 -0700858 if (!err)
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500859 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200860 if (err || !iter.frag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862
Pablo Neira Ayusoc8b17be2019-05-29 13:25:31 +0200863 skb = ip_fraglist_next(&iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 if (err == 0) {
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500867 IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869 }
870
Eric Dumazetb7034142019-06-02 11:24:18 -0700871 kfree_skb_list(iter.frag);
Pablo Neira Ayuso942f1462019-04-04 13:54:20 +0200872
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500873 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000875
876slow_path_clean:
877 skb_walk_frags(skb, frag2) {
878 if (frag2 == frag)
879 break;
880 frag2->sk = NULL;
881 frag2->destructor = NULL;
882 skb->truesize += frag2->truesize;
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
886slow_path:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
888 * Fragment the datagram.
889 */
890
Eric Dumazete7a409c2019-10-19 09:26:37 -0700891 ip_frag_init(skb, hlen, ll_rs, mtu, IPCB(skb)->flags & IPSKB_FRAG_PMTU,
892 &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /*
895 * Keep copying data until we run out.
896 */
897
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200898 while (state.left > 0) {
Pablo Neira Ayuso19c34012019-05-29 13:25:35 +0200899 bool first_frag = (state.offset == 0);
900
Pablo Neira Ayuso065ff792019-05-29 13:25:33 +0200901 skb2 = ip_frag_next(skb, &state);
902 if (IS_ERR(skb2)) {
903 err = PTR_ERR(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto fail;
905 }
Yajun Dengfaf482c2021-08-23 11:17:59 +0800906 ip_frag_ipcb(skb, skb2, first_frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * Put this fragment into the sending queue.
910 */
Eric Dumazet9669fff2019-10-16 18:00:56 -0700911 skb2->tstamp = tstamp;
Eric W. Biederman694869b2015-06-12 21:55:31 -0500912 err = output(net, sk, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (err)
914 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700915
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500916 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000918 consume_skb(skb);
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500919 IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return err;
921
922fail:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900923 kfree_skb(skb);
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500924 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return err;
926}
Andy Zhou49d16b22015-05-15 14:15:37 -0700927EXPORT_SYMBOL(ip_do_fragment);
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929int
930ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
931{
Al Virof69e6d12014-11-24 13:23:40 -0500932 struct msghdr *msg = from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Patrick McHardy84fa7932006-08-29 16:44:56 -0700934 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viro0b62fca2016-11-03 18:17:31 -0400935 if (!copy_from_iter_full(to, len, &msg->msg_iter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -EFAULT;
937 } else {
Al Viro44bb9362006-11-14 21:36:14 -0800938 __wsum csum = 0;
Al Viro0b62fca2016-11-03 18:17:31 -0400939 if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EFAULT;
941 skb->csum = csum_block_add(skb->csum, csum, odd);
942 }
943 return 0;
944}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000945EXPORT_SYMBOL(ip_generic_getfrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Al Viro44bb9362006-11-14 21:36:14 -0800947static inline __wsum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948csum_page(struct page *page, int offset, int copy)
949{
950 char *kaddr;
Al Viro44bb9362006-11-14 21:36:14 -0800951 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 kaddr = kmap(page);
953 csum = csum_partial(kaddr + offset, copy, 0);
954 kunmap(page);
955 return csum;
956}
957
David S. Millerf5fca602011-05-08 17:24:10 -0700958static int __ip_append_data(struct sock *sk,
959 struct flowi4 *fl4,
960 struct sk_buff_head *queue,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000961 struct inet_cork *cork,
Eric Dumazet5640f762012-09-23 23:04:42 +0000962 struct page_frag *pfrag,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000963 int getfrag(void *from, char *to, int offset,
964 int len, int odd, struct sk_buff *skb),
965 void *from, int length, int transhdrlen,
966 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct inet_sock *inet = inet_sk(sk);
Willem de Bruijnb5947e52018-11-30 15:32:39 -0500969 struct ubuf_info *uarg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct sk_buff *skb;
971
Herbert Xu07df5292011-03-01 23:00:58 -0800972 struct ip_options *opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int hh_len;
974 int exthdrlen;
975 int mtu;
976 int copy;
977 int err;
978 int offset = 0;
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +0100979 unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int csummode = CHECKSUM_NONE;
Herbert Xu1470ddf2011-03-01 02:36:47 +0000981 struct rtable *rt = (struct rtable *)cork->dst;
Eric Dumazet694aba62018-03-31 13:16:25 -0700982 unsigned int wmem_alloc_delta = 0;
Willem de Bruijn100f6d82019-05-30 18:01:21 -0400983 bool paged, extra_uref = false;
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400984 u32 tskey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Steffen Klassert96d73032011-06-05 20:48:47 +0000986 skb = skb_peek_tail(queue);
987
988 exthdrlen = !skb ? rt->dst.header_len : 0;
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -0400989 mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
Willem de Bruijn15e36f52018-04-26 13:42:19 -0400990 paged = !!cork->gso_size;
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -0400991
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400992 if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
993 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
994 tskey = sk->sk_tskey++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Changli Gaod8d1f302010-06-10 23:31:35 -0700996 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
999 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
Miaohe Lincbc08a32020-08-29 05:09:18 -04001000 maxnonfragsize = ip_sk_ignore_df(sk) ? IP_MAX_MTU : mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001002 if (cork->length + length > maxnonfragsize - fragheaderlen) {
David S. Millerf5fca602011-05-08 17:24:10 -07001003 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
Hannes Frederic Sowa61e7f092013-12-19 02:13:36 +01001004 mtu - (opt ? opt->optlen : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EMSGSIZE;
1006 }
1007
1008 /*
1009 * transhdrlen > 0 means that this is the first fragment and we wish
1010 * it won't be fragmented in the future.
1011 */
1012 if (transhdrlen &&
1013 length + fragheaderlen <= mtu &&
Tom Herbertc8cd0982015-12-14 11:19:44 -08001014 rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001015 (!(flags & MSG_MORE) || cork->gso_size) &&
Jacek Kalwascd027a52018-04-12 12:03:13 -07001016 (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM)))
Patrick McHardy84fa7932006-08-29 16:44:56 -07001017 csummode = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001019 if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) {
Jonathan Lemon8c793822021-01-06 14:18:37 -08001020 uarg = msg_zerocopy_realloc(sk, length, skb_zcopy(skb));
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001021 if (!uarg)
1022 return -ENOBUFS;
Willem de Bruijn522924b2019-06-07 17:57:48 -04001023 extra_uref = !skb_zcopy(skb); /* only ref on new uarg */
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001024 if (rt->dst.dev->features & NETIF_F_SG &&
1025 csummode == CHECKSUM_PARTIAL) {
1026 paged = true;
1027 } else {
1028 uarg->zerocopy = 0;
Willem de Bruijn52900d22018-11-30 15:32:40 -05001029 skb_zcopy_set(skb, uarg, &extra_uref);
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001030 }
1031 }
1032
Herbert Xu1470ddf2011-03-01 02:36:47 +00001033 cork->length += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* So, what's going on in the loop below?
1036 *
1037 * We use calculated fragment length to generate chained skb,
1038 * each of segments is IP fragment ready for sending to network after
1039 * adding appropriate IP header.
1040 */
1041
Herbert Xu26cde9f2010-06-15 01:52:25 +00001042 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto alloc_new_skb;
1044
1045 while (length > 0) {
1046 /* Check if the remaining data fits into current packet. */
1047 copy = mtu - skb->len;
1048 if (copy < length)
1049 copy = maxfraglen - skb->len;
1050 if (copy <= 0) {
1051 char *data;
1052 unsigned int datalen;
1053 unsigned int fraglen;
1054 unsigned int fraggap;
Jakub Kicinski6d123b82021-06-23 14:44:38 -07001055 unsigned int alloclen, alloc_extra;
Willem de Bruijnaba36932018-11-24 14:21:16 -05001056 unsigned int pagedlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct sk_buff *skb_prev;
1058alloc_new_skb:
1059 skb_prev = skb;
1060 if (skb_prev)
1061 fraggap = skb_prev->len - maxfraglen;
1062 else
1063 fraggap = 0;
1064
1065 /*
1066 * If remaining data exceeds the mtu,
1067 * we know we need more fragment(s).
1068 */
1069 datalen = length + fraggap;
1070 if (datalen > mtu - fragheaderlen)
1071 datalen = maxfraglen - fragheaderlen;
1072 fraglen = datalen + fragheaderlen;
Willem de Bruijnaba36932018-11-24 14:21:16 -05001073 pagedlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Jakub Kicinski6d123b82021-06-23 14:44:38 -07001075 alloc_extra = hh_len + 15;
1076 alloc_extra += exthdrlen;
Steffen Klassert353e5c92011-06-22 01:05:37 +00001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* The last fragment gets additional space at tail.
1079 * Note, with MSG_MORE we overallocate on fragments,
1080 * because we have no idea what fragment will be
1081 * the last.
1082 */
Steffen Klassert33f99dc2011-06-22 01:04:37 +00001083 if (datalen == length + fraggap)
Jakub Kicinski6d123b82021-06-23 14:44:38 -07001084 alloc_extra += rt->dst.trailer_len;
1085
1086 if ((flags & MSG_MORE) &&
1087 !(rt->dst.dev->features&NETIF_F_SG))
1088 alloclen = mtu;
1089 else if (!paged &&
1090 (fraglen + alloc_extra < SKB_MAX_ALLOC ||
1091 !(rt->dst.dev->features & NETIF_F_SG)))
1092 alloclen = fraglen;
1093 else {
1094 alloclen = min_t(int, fraglen, MAX_HEADER);
1095 pagedlen = fraglen - alloclen;
1096 }
1097
1098 alloclen += alloc_extra;
Steffen Klassert33f99dc2011-06-22 01:04:37 +00001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (transhdrlen) {
Jakub Kicinski6d123b82021-06-23 14:44:38 -07001101 skb = sock_alloc_send_skb(sk, alloclen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 (flags & MSG_DONTWAIT), &err);
1103 } else {
1104 skb = NULL;
Eric Dumazet694aba62018-03-31 13:16:25 -07001105 if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 2 * sk->sk_sndbuf)
Jakub Kicinski6d123b82021-06-23 14:44:38 -07001107 skb = alloc_skb(alloclen,
Eric Dumazet694aba62018-03-31 13:16:25 -07001108 sk->sk_allocation);
Ian Morris51456b22015-04-03 09:17:26 +01001109 if (unlikely(!skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 err = -ENOBUFS;
1111 }
Ian Morris51456b22015-04-03 09:17:26 +01001112 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto error;
1114
1115 /*
1116 * Fill in the control structures
1117 */
1118 skb->ip_summed = csummode;
1119 skb->csum = 0;
1120 skb_reserve(skb, hh_len);
Willem de Bruijn11878b42014-07-14 17:55:06 -04001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
1123 * Find where to start putting bytes.
1124 */
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001125 data = skb_put(skb, fraglen + exthdrlen - pagedlen);
Arnaldo Carvalho de Meloc14d2452007-03-11 22:39:41 -03001126 skb_set_network_header(skb, exthdrlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001127 skb->transport_header = (skb->network_header +
1128 fragheaderlen);
Steffen Klassert353e5c92011-06-22 01:05:37 +00001129 data += fragheaderlen + exthdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (fraggap) {
1132 skb->csum = skb_copy_and_csum_bits(
1133 skb_prev, maxfraglen,
Al Viro8d5930d2020-07-10 20:07:10 -04001134 data + transhdrlen, fraggap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 skb_prev->csum = csum_sub(skb_prev->csum,
1136 skb->csum);
1137 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001138 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001141 copy = datalen - transhdrlen - fraggap - pagedlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1143 err = -EFAULT;
1144 kfree_skb(skb);
1145 goto error;
1146 }
1147
1148 offset += copy;
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001149 length -= copy + transhdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 transhdrlen = 0;
1151 exthdrlen = 0;
1152 csummode = CHECKSUM_NONE;
1153
Willem de Bruijn52900d22018-11-30 15:32:40 -05001154 /* only the initial fragment is time stamped */
1155 skb_shinfo(skb)->tx_flags = cork->tx_flags;
1156 cork->tx_flags = 0;
1157 skb_shinfo(skb)->tskey = tskey;
1158 tskey = 0;
1159 skb_zcopy_set(skb, uarg, &extra_uref);
1160
Julian Anastasov0dec8792017-02-06 23:14:16 +02001161 if ((flags & MSG_CONFIRM) && !skb_prev)
1162 skb_set_dst_pending_confirm(skb, 1);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * Put the packet on the pending queue.
1166 */
Eric Dumazet694aba62018-03-31 13:16:25 -07001167 if (!skb->destructor) {
1168 skb->destructor = sock_wfree;
1169 skb->sk = sk;
1170 wmem_alloc_delta += skb->truesize;
1171 }
Herbert Xu1470ddf2011-03-01 02:36:47 +00001172 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 continue;
1174 }
1175
1176 if (copy > length)
1177 copy = length;
1178
Willem de Bruijn113f99c2018-05-17 13:13:29 -04001179 if (!(rt->dst.dev->features&NETIF_F_SG) &&
1180 skb_tailroom(skb) >= copy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned int off;
1182
1183 off = skb->len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001184 if (getfrag(from, skb_put(skb, copy),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 offset, copy, off, skb) < 0) {
1186 __skb_trim(skb, off);
1187 err = -EFAULT;
1188 goto error;
1189 }
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001190 } else if (!uarg || !uarg->zerocopy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int i = skb_shinfo(skb)->nr_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Eric Dumazet5640f762012-09-23 23:04:42 +00001193 err = -ENOMEM;
1194 if (!sk_page_frag_refill(sk, pfrag))
1195 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Eric Dumazet5640f762012-09-23 23:04:42 +00001197 if (!skb_can_coalesce(skb, i, pfrag->page,
1198 pfrag->offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 err = -EMSGSIZE;
Eric Dumazet5640f762012-09-23 23:04:42 +00001200 if (i == MAX_SKB_FRAGS)
1201 goto error;
1202
1203 __skb_fill_page_desc(skb, i, pfrag->page,
1204 pfrag->offset, 0);
1205 skb_shinfo(skb)->nr_frags = ++i;
1206 get_page(pfrag->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001208 copy = min_t(int, copy, pfrag->size - pfrag->offset);
1209 if (getfrag(from,
1210 page_address(pfrag->page) + pfrag->offset,
1211 offset, copy, skb->len, skb) < 0)
1212 goto error_efault;
1213
1214 pfrag->offset += copy;
1215 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 skb->len += copy;
1217 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001218 skb->truesize += copy;
Eric Dumazet694aba62018-03-31 13:16:25 -07001219 wmem_alloc_delta += copy;
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001220 } else {
1221 err = skb_zerocopy_iter_dgram(skb, from, copy);
1222 if (err < 0)
1223 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 offset += copy;
1226 length -= copy;
1227 }
1228
Paolo Abeni9e8445a2018-04-04 14:30:01 +02001229 if (wmem_alloc_delta)
1230 refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return 0;
1232
Eric Dumazet5640f762012-09-23 23:04:42 +00001233error_efault:
1234 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235error:
Jonathan Lemon8e044912021-01-06 14:18:41 -08001236 net_zcopy_put_abort(uarg, extra_uref);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001237 cork->length -= length;
Pavel Emelyanov5e38e272008-07-16 20:19:49 -07001238 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
Eric Dumazet694aba62018-03-31 13:16:25 -07001239 refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Herbert Xu1470ddf2011-03-01 02:36:47 +00001243static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1244 struct ipcm_cookie *ipc, struct rtable **rtp)
1245{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001246 struct ip_options_rcu *opt;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001247 struct rtable *rt;
1248
Gao Feng9783ccd2018-04-16 10:16:45 +08001249 rt = *rtp;
1250 if (unlikely(!rt))
1251 return -EFAULT;
1252
Herbert Xu1470ddf2011-03-01 02:36:47 +00001253 /*
1254 * setup for corking.
1255 */
1256 opt = ipc->opt;
1257 if (opt) {
Ian Morris51456b22015-04-03 09:17:26 +01001258 if (!cork->opt) {
Herbert Xu1470ddf2011-03-01 02:36:47 +00001259 cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1260 sk->sk_allocation);
Ian Morris51456b22015-04-03 09:17:26 +01001261 if (unlikely(!cork->opt))
Herbert Xu1470ddf2011-03-01 02:36:47 +00001262 return -ENOBUFS;
1263 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001264 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001265 cork->flags |= IPCORK_OPT;
1266 cork->addr = ipc->addr;
1267 }
Gao Feng9783ccd2018-04-16 10:16:45 +08001268
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +01001269 cork->fragsize = ip_sk_use_pmtu(sk) ?
Eric Dumazet501a90c2019-12-05 20:43:46 -08001270 dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
1271
1272 if (!inetdev_valid_mtu(cork->fragsize))
1273 return -ENETUNREACH;
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001274
Willem de Bruijnfbf47812018-07-06 10:12:59 -04001275 cork->gso_size = ipc->gso_size;
Eric Dumazet501a90c2019-12-05 20:43:46 -08001276
Herbert Xu1470ddf2011-03-01 02:36:47 +00001277 cork->dst = &rt->dst;
Eric Dumazet501a90c2019-12-05 20:43:46 -08001278 /* We stole this route, caller should not release it. */
1279 *rtp = NULL;
1280
Herbert Xu1470ddf2011-03-01 02:36:47 +00001281 cork->length = 0;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001282 cork->ttl = ipc->ttl;
1283 cork->tos = ipc->tos;
Willem de Bruijnc6af0c22019-09-11 15:50:51 -04001284 cork->mark = ipc->sockc.mark;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001285 cork->priority = ipc->priority;
Jesus Sanchez-Palenciabc969a92018-07-03 15:42:49 -07001286 cork->transmit_time = ipc->sockc.transmit_time;
Willem de Bruijn678ca422018-07-06 10:12:58 -04001287 cork->tx_flags = 0;
1288 sock_tx_timestamp(sk, ipc->sockc.tsflags, &cork->tx_flags);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001289
1290 return 0;
1291}
1292
1293/*
1294 * ip_append_data() and ip_append_page() can make one large IP datagram
1295 * from many pieces of data. Each pieces will be holded on the socket
1296 * until ip_push_pending_frames() is called. Each piece can be a page
1297 * or non-page data.
1298 *
1299 * Not only UDP, other transport protocols - e.g. raw sockets - can use
1300 * this interface potentially.
1301 *
1302 * LATER: length must be adjusted by pad at tail, when it is required.
1303 */
David S. Millerf5fca602011-05-08 17:24:10 -07001304int ip_append_data(struct sock *sk, struct flowi4 *fl4,
Herbert Xu1470ddf2011-03-01 02:36:47 +00001305 int getfrag(void *from, char *to, int offset, int len,
1306 int odd, struct sk_buff *skb),
1307 void *from, int length, int transhdrlen,
1308 struct ipcm_cookie *ipc, struct rtable **rtp,
1309 unsigned int flags)
1310{
1311 struct inet_sock *inet = inet_sk(sk);
1312 int err;
1313
1314 if (flags&MSG_PROBE)
1315 return 0;
1316
1317 if (skb_queue_empty(&sk->sk_write_queue)) {
David S. Millerbdc712b2011-05-06 15:02:07 -07001318 err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001319 if (err)
1320 return err;
1321 } else {
1322 transhdrlen = 0;
1323 }
1324
Eric Dumazet5640f762012-09-23 23:04:42 +00001325 return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1326 sk_page_frag(sk), getfrag,
Herbert Xu1470ddf2011-03-01 02:36:47 +00001327 from, length, transhdrlen, flags);
1328}
1329
David S. Millerf5fca602011-05-08 17:24:10 -07001330ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int offset, size_t size, int flags)
1332{
1333 struct inet_sock *inet = inet_sk(sk);
1334 struct sk_buff *skb;
1335 struct rtable *rt;
1336 struct ip_options *opt = NULL;
David S. Millerbdc712b2011-05-06 15:02:07 -07001337 struct inet_cork *cork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int hh_len;
1339 int mtu;
1340 int len;
1341 int err;
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001342 unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if (inet->hdrincl)
1345 return -EPERM;
1346
1347 if (flags&MSG_PROBE)
1348 return 0;
1349
1350 if (skb_queue_empty(&sk->sk_write_queue))
1351 return -EINVAL;
1352
David S. Millerbdc712b2011-05-06 15:02:07 -07001353 cork = &inet->cork.base;
1354 rt = (struct rtable *)cork->dst;
1355 if (cork->flags & IPCORK_OPT)
1356 opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Miaohe Lin343d8c62020-08-25 08:32:11 -04001358 if (!(rt->dst.dev->features & NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EOPNOTSUPP;
1360
Changli Gaod8d1f302010-06-10 23:31:35 -07001361 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001362 mtu = cork->gso_size ? IP_MAX_MTU : cork->fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1365 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
WANG Cong60ff7462014-05-04 16:39:18 -07001366 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001368 if (cork->length + size > maxnonfragsize - fragheaderlen) {
Hannes Frederic Sowa61e7f092013-12-19 02:13:36 +01001369 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1370 mtu - (opt ? opt->optlen : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EMSGSIZE;
1372 }
1373
Ian Morris51456b22015-04-03 09:17:26 +01001374 skb = skb_peek_tail(&sk->sk_write_queue);
1375 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EINVAL;
1377
Hannes Frederic Sowaa8c4a252016-02-22 18:43:25 +01001378 cork->length += size;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 while (size > 0) {
Willem de Bruijnab2fb7e2017-08-22 11:39:57 -04001381 /* Check if the remaining data fits into current packet. */
1382 len = mtu - skb->len;
1383 if (len < size)
1384 len = maxfraglen - skb->len;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (len <= 0) {
1387 struct sk_buff *skb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int alloclen;
1389
1390 skb_prev = skb;
Jayachandran C0d0d2bb2005-10-13 11:43:02 -07001391 fraggap = skb_prev->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 alloclen = fragheaderlen + hh_len + fraggap + 15;
1394 skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1395 if (unlikely(!skb)) {
1396 err = -ENOBUFS;
1397 goto error;
1398 }
1399
1400 /*
1401 * Fill in the control structures
1402 */
1403 skb->ip_summed = CHECKSUM_NONE;
1404 skb->csum = 0;
1405 skb_reserve(skb, hh_len);
1406
1407 /*
1408 * Find where to start putting bytes.
1409 */
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001410 skb_put(skb, fragheaderlen + fraggap);
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -03001411 skb_reset_network_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001412 skb->transport_header = (skb->network_header +
1413 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (fraggap) {
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001415 skb->csum = skb_copy_and_csum_bits(skb_prev,
1416 maxfraglen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001417 skb_transport_header(skb),
Al Viro8d5930d2020-07-10 20:07:10 -04001418 fraggap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 skb_prev->csum = csum_sub(skb_prev->csum,
1420 skb->csum);
Herbert Xue9fa4f72006-08-13 20:12:58 -07001421 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424 /*
1425 * Put the packet on the pending queue.
1426 */
1427 __skb_queue_tail(&sk->sk_write_queue, skb);
1428 continue;
1429 }
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (len > size)
1432 len = size;
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001433
1434 if (skb_append_pagefrags(skb, page, offset, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 err = -EMSGSIZE;
1436 goto error;
1437 }
1438
1439 if (skb->ip_summed == CHECKSUM_NONE) {
Al Viro44bb9362006-11-14 21:36:14 -08001440 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 csum = csum_page(page, offset, len);
1442 skb->csum = csum_block_add(skb->csum, csum, skb->len);
1443 }
1444
1445 skb->len += len;
1446 skb->data_len += len;
David S. Miller1e34a112008-01-22 23:44:31 -08001447 skb->truesize += len;
Reshetova, Elena14afee42017-06-30 13:08:00 +03001448 refcount_add(len, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 offset += len;
1450 size -= len;
1451 }
1452 return 0;
1453
1454error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001455 cork->length -= size;
Pavel Emelyanov5e38e272008-07-16 20:19:49 -07001456 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return err;
1458}
1459
Herbert Xu1470ddf2011-03-01 02:36:47 +00001460static void ip_cork_release(struct inet_cork *cork)
Pavel Emelyanov429f08e2007-11-05 21:03:24 -08001461{
Herbert Xu1470ddf2011-03-01 02:36:47 +00001462 cork->flags &= ~IPCORK_OPT;
1463 kfree(cork->opt);
1464 cork->opt = NULL;
1465 dst_release(cork->dst);
1466 cork->dst = NULL;
Pavel Emelyanov429f08e2007-11-05 21:03:24 -08001467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/*
1470 * Combined all pending IP fragments on the socket as one IP datagram
1471 * and push them out.
1472 */
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001473struct sk_buff *__ip_make_skb(struct sock *sk,
David S. Miller77968b72011-05-08 17:12:19 -07001474 struct flowi4 *fl4,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001475 struct sk_buff_head *queue,
1476 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct sk_buff *skb, *tmp_skb;
1479 struct sk_buff **tail_skb;
1480 struct inet_sock *inet = inet_sk(sk);
Pavel Emelyanov0388b002008-07-14 23:00:43 -07001481 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct ip_options *opt = NULL;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001483 struct rtable *rt = (struct rtable *)cork->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 struct iphdr *iph;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -08001485 __be16 df = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 __u8 ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Ian Morris51456b22015-04-03 09:17:26 +01001488 skb = __skb_dequeue(queue);
1489 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto out;
1491 tail_skb = &(skb_shinfo(skb)->frag_list);
1492
1493 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001494 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001495 __skb_pull(skb, skb_network_offset(skb));
Herbert Xu1470ddf2011-03-01 02:36:47 +00001496 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001497 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *tail_skb = tmp_skb;
1499 tail_skb = &(tmp_skb->next);
1500 skb->len += tmp_skb->len;
1501 skb->data_len += tmp_skb->len;
1502 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 tmp_skb->destructor = NULL;
1504 tmp_skb->sk = NULL;
1505 }
1506
1507 /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1508 * to fragment the frame generated here. No matter, what transforms
1509 * how transforms change size of the packet, it will come out.
1510 */
WANG Cong60ff7462014-05-04 16:39:18 -07001511 skb->ignore_df = ip_sk_ignore_df(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* DF bit is set when we want to see DF on outgoing frames.
WANG Cong60ff7462014-05-04 16:39:18 -07001514 * If ignore_df is set too, we still allow to fragment this frame
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 * locally. */
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +01001516 if (inet->pmtudisc == IP_PMTUDISC_DO ||
1517 inet->pmtudisc == IP_PMTUDISC_PROBE ||
Changli Gaod8d1f302010-06-10 23:31:35 -07001518 (skb->len <= dst_mtu(&rt->dst) &&
1519 ip_dont_fragment(sk, &rt->dst)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 df = htons(IP_DF);
1521
Herbert Xu1470ddf2011-03-01 02:36:47 +00001522 if (cork->flags & IPCORK_OPT)
1523 opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Francesco Fuscoaa661582013-09-24 15:43:09 +02001525 if (cork->ttl != 0)
1526 ttl = cork->ttl;
1527 else if (rt->rt_type == RTN_MULTICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ttl = inet->mc_ttl;
1529 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001530 ttl = ip_select_ttl(inet, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Ansis Atteka749154a2013-09-18 15:29:52 -07001532 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 iph->version = 4;
1534 iph->ihl = 5;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001535 iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 iph->frag_off = df;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 iph->ttl = ttl;
1538 iph->protocol = sk->sk_protocol;
Eric Dumazet84f93072011-11-30 19:00:53 +00001539 ip_copy_addrs(iph, fl4);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001540 ip_select_ident(net, skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
David S. Miller22f728f2011-05-13 17:21:27 -04001542 if (opt) {
Miaohe Lin5af68892020-08-29 05:21:30 -04001543 iph->ihl += opt->optlen >> 2;
David S. Miller22f728f2011-05-13 17:21:27 -04001544 ip_options_build(skb, opt, cork->addr, rt, 0);
1545 }
1546
Francesco Fuscoaa661582013-09-24 15:43:09 +02001547 skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
Willem de Bruijnc6af0c22019-09-11 15:50:51 -04001548 skb->mark = cork->mark;
Jesus Sanchez-Palenciabc969a92018-07-03 15:42:49 -07001549 skb->tstamp = cork->transmit_time;
Eric Dumazeta21bba92008-11-24 16:07:50 -08001550 /*
1551 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1552 * on dst refcount
1553 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001554 cork->dst = NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001555 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
David L Stevens96793b42007-09-17 09:57:33 -07001557 if (iph->protocol == IPPROTO_ICMP)
Pavel Emelyanov0388b002008-07-14 23:00:43 -07001558 icmp_out_count(net, ((struct icmphdr *)
David L Stevens96793b42007-09-17 09:57:33 -07001559 skb_transport_header(skb))->type);
1560
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001561 ip_cork_release(cork);
1562out:
1563 return skb;
1564}
1565
Eric Dumazetb5ec8ee2012-08-10 02:22:47 +00001566int ip_send_skb(struct net *net, struct sk_buff *skb)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001567{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001568 int err;
1569
Eric W. Biederman33224b12015-10-07 16:48:46 -05001570 err = ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (err) {
1572 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001573 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (err)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001575 IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
David S. Miller77968b72011-05-08 17:12:19 -07001581int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
Herbert Xu1470ddf2011-03-01 02:36:47 +00001582{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001583 struct sk_buff *skb;
1584
David S. Miller77968b72011-05-08 17:12:19 -07001585 skb = ip_finish_skb(sk, fl4);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001586 if (!skb)
1587 return 0;
1588
1589 /* Netfilter gets whole the not fragmented skb. */
Eric Dumazetb5ec8ee2012-08-10 02:22:47 +00001590 return ip_send_skb(sock_net(sk), skb);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001591}
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593/*
1594 * Throw away all pending data on the socket.
1595 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001596static void __ip_flush_pending_frames(struct sock *sk,
1597 struct sk_buff_head *queue,
1598 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 struct sk_buff *skb;
1601
Herbert Xu1470ddf2011-03-01 02:36:47 +00001602 while ((skb = __skb_dequeue_tail(queue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 kfree_skb(skb);
1604
Herbert Xu1470ddf2011-03-01 02:36:47 +00001605 ip_cork_release(cork);
1606}
1607
1608void ip_flush_pending_frames(struct sock *sk)
1609{
David S. Millerbdc712b2011-05-06 15:02:07 -07001610 __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001613struct sk_buff *ip_make_skb(struct sock *sk,
David S. Miller77968b72011-05-08 17:12:19 -07001614 struct flowi4 *fl4,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001615 int getfrag(void *from, char *to, int offset,
1616 int len, int odd, struct sk_buff *skb),
1617 void *from, int length, int transhdrlen,
1618 struct ipcm_cookie *ipc, struct rtable **rtp,
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001619 struct inet_cork *cork, unsigned int flags)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001620{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001621 struct sk_buff_head queue;
1622 int err;
1623
1624 if (flags & MSG_PROBE)
1625 return NULL;
1626
1627 __skb_queue_head_init(&queue);
1628
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001629 cork->flags = 0;
1630 cork->addr = 0;
1631 cork->opt = NULL;
1632 err = ip_setup_cork(sk, cork, ipc, rtp);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001633 if (err)
1634 return ERR_PTR(err);
1635
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001636 err = __ip_append_data(sk, fl4, &queue, cork,
Eric Dumazet5640f762012-09-23 23:04:42 +00001637 &current->task_frag, getfrag,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001638 from, length, transhdrlen, flags);
1639 if (err) {
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001640 __ip_flush_pending_frames(sk, &queue, cork);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001641 return ERR_PTR(err);
1642 }
1643
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001644 return __ip_make_skb(sk, fl4, &queue, cork);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647/*
1648 * Fetch data from kernel space and fill in checksum if needed.
1649 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001650static int ip_reply_glue_bits(void *dptr, char *to, int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int len, int odd, struct sk_buff *skb)
1652{
Al Viro50842052006-11-14 21:36:34 -08001653 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Al Virocc44c172020-07-11 00:12:07 -04001655 csum = csum_partial_copy_nocheck(dptr+offset, to, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 skb->csum = csum_block_add(skb->csum, csum, odd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001660/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * Generic function to send a packet as reply to another packet.
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001662 * Used to send some TCP resets/acks so far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001664void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
Eric Dumazet24a2d432014-09-27 09:50:55 -07001665 const struct ip_options *sopt,
1666 __be32 daddr, __be32 saddr,
1667 const struct ip_reply_arg *arg,
Eric Dumazetd6fb3962019-06-13 21:22:35 -07001668 unsigned int len, u64 transmit_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001670 struct ip_options_data replyopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct ipcm_cookie ipc;
David S. Miller77968b72011-05-08 17:12:19 -07001672 struct flowi4 fl4;
Eric Dumazet511c3f92009-06-02 05:14:27 +00001673 struct rtable *rt = skb_rtable(skb);
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001674 struct net *net = sock_net(sk);
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001675 struct sk_buff *nskb;
Vasily Averin40620902014-10-15 16:24:02 +04001676 int err;
David Ahernf7ba8682015-08-13 14:59:08 -06001677 int oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Paolo Abeni91ed1e62017-08-03 18:07:06 +02001679 if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return;
1681
Willem de Bruijn35178202018-07-06 10:12:54 -04001682 ipcm_init(&ipc);
David S. Miller0a5ebb82011-05-09 13:22:43 -07001683 ipc.addr = daddr;
Eric Dumazetd6fb3962019-06-13 21:22:35 -07001684 ipc.sockc.transmit_time = transmit_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001686 if (replyopts.opt.opt.optlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 ipc.opt = &replyopts.opt;
1688
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001689 if (replyopts.opt.opt.srr)
1690 daddr = replyopts.opt.opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
David Ahernf7ba8682015-08-13 14:59:08 -06001693 oif = arg->bound_dev_if;
David Ahern9b6c14d2016-11-09 09:07:26 -08001694 if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
1695 oif = skb->skb_iif;
David Ahernf7ba8682015-08-13 14:59:08 -06001696
1697 flowi4_init_output(&fl4, oif,
Jon Maxwell00483692018-05-10 16:53:51 +10001698 IP4_REPLY_MARK(net, skb->mark) ?: sk->sk_mark,
Eric Dumazet66b13d92011-10-24 03:06:21 -04001699 RT_TOS(arg->tos),
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001700 RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
David S. Miller77968b72011-05-08 17:12:19 -07001701 ip_reply_arg_flowi_flags(arg),
David S. Miller70e73412012-06-28 03:21:41 -07001702 daddr, saddr,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09001703 tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1704 arg->uid);
Paul Moore3df98d72020-09-27 22:38:26 -04001705 security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001706 rt = ip_route_output_key(net, &fl4);
David S. Miller77968b72011-05-08 17:12:19 -07001707 if (IS_ERR(rt))
1708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Wei Wangba9e04a2020-09-08 14:09:34 -07001710 inet_sk(sk)->tos = arg->tos & ~INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001712 sk->sk_protocol = ip_hdr(skb)->protocol;
Patrick McHardyf0e48db2007-06-04 21:32:46 -07001713 sk->sk_bound_dev_if = arg->bound_dev_if;
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001714 sk->sk_sndbuf = sysctl_wmem_default;
Willem de Bruijn0da75362020-07-01 16:00:06 -04001715 ipc.sockc.mark = fl4.flowi4_mark;
Vasily Averin40620902014-10-15 16:24:02 +04001716 err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
1717 len, 0, &ipc, &rt, MSG_DONTWAIT);
1718 if (unlikely(err)) {
1719 ip_flush_pending_frames(sk);
1720 goto out;
1721 }
1722
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001723 nskb = skb_peek(&sk->sk_write_queue);
1724 if (nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (arg->csumoffset >= 0)
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001726 *((__sum16 *)skb_transport_header(nskb) +
1727 arg->csumoffset) = csum_fold(csum_add(nskb->csum,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001728 arg->csum));
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001729 nskb->ip_summed = CHECKSUM_NONE;
David S. Miller77968b72011-05-08 17:12:19 -07001730 ip_push_pending_frames(sk, &fl4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Vasily Averin40620902014-10-15 16:24:02 +04001732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 ip_rt_put(rt);
1734}
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736void __init ip_init(void)
1737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ip_rt_init();
1739 inet_initpeers();
1740
WANG Cong72c1d3b2014-01-10 16:09:45 -08001741#if defined(CONFIG_IP_MULTICAST)
1742 igmp_mc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743#endif
1744}