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