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