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