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