12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2db8dac20SDavid S. Miller /* 3db8dac20SDavid S. Miller * UDP over IPv6 4db8dac20SDavid S. Miller * Linux INET6 implementation 5db8dac20SDavid S. Miller * 6db8dac20SDavid S. Miller * Authors: 7db8dac20SDavid S. Miller * Pedro Roque <roque@di.fc.ul.pt> 8db8dac20SDavid S. Miller * 9db8dac20SDavid S. Miller * Based on linux/ipv4/udp.c 10db8dac20SDavid S. Miller * 11db8dac20SDavid S. Miller * Fixes: 12db8dac20SDavid S. Miller * Hideaki YOSHIFUJI : sin6_scope_id support 13db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 14db8dac20SDavid S. Miller * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 15db8dac20SDavid S. Miller * a single port at the same time. 16db8dac20SDavid S. Miller * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 17db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. 18db8dac20SDavid S. Miller */ 19db8dac20SDavid S. Miller 20aef2fedaSJakub Kicinski #include <linux/bpf-cgroup.h> 21db8dac20SDavid S. Miller #include <linux/errno.h> 22db8dac20SDavid S. Miller #include <linux/types.h> 23db8dac20SDavid S. Miller #include <linux/socket.h> 24db8dac20SDavid S. Miller #include <linux/sockios.h> 25db8dac20SDavid S. Miller #include <linux/net.h> 26db8dac20SDavid S. Miller #include <linux/in6.h> 27db8dac20SDavid S. Miller #include <linux/netdevice.h> 28db8dac20SDavid S. Miller #include <linux/if_arp.h> 29db8dac20SDavid S. Miller #include <linux/ipv6.h> 30db8dac20SDavid S. Miller #include <linux/icmpv6.h> 31db8dac20SDavid S. Miller #include <linux/init.h> 32db8dac20SDavid S. Miller #include <linux/module.h> 33db8dac20SDavid S. Miller #include <linux/skbuff.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 357c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 360e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h> 37db8dac20SDavid S. Miller 38496611d7SCraig Gallek #include <net/addrconf.h> 39db8dac20SDavid S. Miller #include <net/ndisc.h> 40db8dac20SDavid S. Miller #include <net/protocol.h> 41db8dac20SDavid S. Miller #include <net/transp_v6.h> 42db8dac20SDavid S. Miller #include <net/ip6_route.h> 43db8dac20SDavid S. Miller #include <net/raw.h> 44222a011eSAndrew Lunn #include <net/seg6.h> 45db8dac20SDavid S. Miller #include <net/tcp_states.h> 46db8dac20SDavid S. Miller #include <net/ip6_checksum.h> 47e7cc0824SStefano Brivio #include <net/ip6_tunnel.h> 488139dccdSIvan Babrou #include <trace/events/udp.h> 49db8dac20SDavid S. Miller #include <net/xfrm.h> 500bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h> 5172289b96STom Herbert #include <net/inet6_hashtables.h> 52076bb0c8SEliezer Tamir #include <net/busy_poll.h> 53e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 547938cd15SRichard Gobert #include <net/gro.h> 55db8dac20SDavid S. Miller 56db8dac20SDavid S. Miller #include <linux/proc_fs.h> 57db8dac20SDavid S. Miller #include <linux/seq_file.h> 5822911fc5SEric Dumazet #include <trace/events/skb.h> 59db8dac20SDavid S. Miller #include "udp_impl.h" 60db8dac20SDavid S. Miller 61d38afeecSKuniyuki Iwashima static void udpv6_destruct_sock(struct sock *sk) 62d38afeecSKuniyuki Iwashima { 63d38afeecSKuniyuki Iwashima udp_destruct_common(sk); 64d38afeecSKuniyuki Iwashima inet6_sock_destruct(sk); 65d38afeecSKuniyuki Iwashima } 66d38afeecSKuniyuki Iwashima 67d38afeecSKuniyuki Iwashima int udpv6_init_sock(struct sock *sk) 68d38afeecSKuniyuki Iwashima { 698a3854c7SPaolo Abeni udp_lib_init_sock(sk); 70d38afeecSKuniyuki Iwashima sk->sk_destruct = udpv6_destruct_sock; 718f279fb0SPavel Begunkov set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 72d38afeecSKuniyuki Iwashima return 0; 73d38afeecSKuniyuki Iwashima } 74d38afeecSKuniyuki Iwashima 750f495f76SLorenz Bauer INDIRECT_CALLABLE_SCOPE 760f495f76SLorenz Bauer u32 udp6_ehashfn(const struct net *net, 77b50026b5SHannes Frederic Sowa const struct in6_addr *laddr, 78b50026b5SHannes Frederic Sowa const u16 lport, 79b50026b5SHannes Frederic Sowa const struct in6_addr *faddr, 80b50026b5SHannes Frederic Sowa const __be16 fport) 81b50026b5SHannes Frederic Sowa { 821bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly; 831bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly; 841bbdceefSHannes Frederic Sowa 851bbdceefSHannes Frederic Sowa u32 lhash, fhash; 861bbdceefSHannes Frederic Sowa 871bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret, 881bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret)); 891bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret, 901bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret)); 911bbdceefSHannes Frederic Sowa 921bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3]; 931bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 941bbdceefSHannes Frederic Sowa 95b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport, 9651d03e2fSEric Dumazet udp6_ehash_secret + net_hash_mix(net)); 97b50026b5SHannes Frederic Sowa } 98b50026b5SHannes Frederic Sowa 996ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 100db8dac20SDavid S. Miller { 10130fff923SEric Dumazet unsigned int hash2_nulladdr = 102f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 10330fff923SEric Dumazet unsigned int hash2_partial = 104f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 10530fff923SEric Dumazet 106d4cada4aSEric Dumazet /* precompute partial secondary hash */ 10730fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 108fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 109db8dac20SDavid S. Miller } 110db8dac20SDavid S. Miller 111f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk) 112719f8358SEric Dumazet { 113f0b1e64cSMartin KaFai Lau u16 new_hash = ipv6_portaddr_hash(sock_net(sk), 114efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr, 115719f8358SEric Dumazet inet_sk(sk)->inet_num); 116719f8358SEric Dumazet 117719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 118719f8358SEric Dumazet } 119719f8358SEric Dumazet 120d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 12188440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 122d1e37288SSu, Xuemin const struct in6_addr *daddr, unsigned short hnum, 12373545373STim Beale int dif, int sdif) 124db8dac20SDavid S. Miller { 1254c971d2fSEric Dumazet int bound_dev_if, score; 12660c04aecSJoe Perches struct inet_sock *inet; 1276da5b0f0SMike Manning bool dev_match; 128db8dac20SDavid S. Miller 12960c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 13060c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 13160c04aecSJoe Perches sk->sk_family != PF_INET6) 13260c04aecSJoe Perches return -1; 133645ca708SEric Dumazet 13423b0269eSPeter Oskolkov if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 13523b0269eSPeter Oskolkov return -1; 13623b0269eSPeter Oskolkov 137645ca708SEric Dumazet score = 0; 13860c04aecSJoe Perches inet = inet_sk(sk); 13960c04aecSJoe Perches 140c720c7e8SEric Dumazet if (inet->inet_dport) { 141c720c7e8SEric Dumazet if (inet->inet_dport != sport) 142645ca708SEric Dumazet return -1; 143db8dac20SDavid S. Miller score++; 144db8dac20SDavid S. Miller } 14560c04aecSJoe Perches 146efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 147efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 148645ca708SEric Dumazet return -1; 149db8dac20SDavid S. Miller score++; 150db8dac20SDavid S. Miller } 15160c04aecSJoe Perches 1524c971d2fSEric Dumazet bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); 1534c971d2fSEric Dumazet dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif); 15469678bcdSPaolo Abeni if (!dev_match) 155645ca708SEric Dumazet return -1; 1564c971d2fSEric Dumazet if (bound_dev_if) 157db8dac20SDavid S. Miller score++; 15860c04aecSJoe Perches 1597170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 16070da268bSEric Dumazet score++; 16170da268bSEric Dumazet 162645ca708SEric Dumazet return score; 163645ca708SEric Dumazet } 164645ca708SEric Dumazet 165d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 166fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net, 167fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 1681801b570SDavid Ahern const struct in6_addr *daddr, unsigned int hnum, 16973545373STim Beale int dif, int sdif, struct udp_hslot *hslot2, 17073545373STim Beale struct sk_buff *skb) 171fddc17deSEric Dumazet { 172fddc17deSEric Dumazet struct sock *sk, *result; 173e94a62f5SPaolo Abeni int score, badness; 174fddc17deSEric Dumazet 175fddc17deSEric Dumazet result = NULL; 176fddc17deSEric Dumazet badness = -1; 177ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 178d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 17973545373STim Beale daddr, hnum, dif, sdif); 180fddc17deSEric Dumazet if (score > badness) { 181ca065d0cSEric Dumazet badness = score; 1820f495f76SLorenz Bauer 1830f495f76SLorenz Bauer if (sk->sk_state == TCP_ESTABLISHED) { 1840f495f76SLorenz Bauer result = sk; 1850f495f76SLorenz Bauer continue; 1860f495f76SLorenz Bauer } 1870f495f76SLorenz Bauer 1880f495f76SLorenz Bauer result = inet6_lookup_reuseport(net, sk, skb, sizeof(struct udphdr), 1890f495f76SLorenz Bauer saddr, sport, daddr, hnum, udp6_ehashfn); 190f0ea27e7SLorenz Bauer if (!result) { 191f0ea27e7SLorenz Bauer result = sk; 192f0ea27e7SLorenz Bauer continue; 193f0ea27e7SLorenz Bauer } 194f0ea27e7SLorenz Bauer 195e32ea7e7SCraig Gallek /* Fall back to scoring if group has connections */ 196f0ea27e7SLorenz Bauer if (!reuseport_has_conns(sk)) 197e32ea7e7SCraig Gallek return result; 198ed0dfffdSEric Dumazet 199f0ea27e7SLorenz Bauer /* Reuseport logic returned an error, keep original score. */ 200f0ea27e7SLorenz Bauer if (IS_ERR(result)) 201f0ea27e7SLorenz Bauer continue; 202f0ea27e7SLorenz Bauer 203f0ea27e7SLorenz Bauer badness = compute_score(sk, net, saddr, sport, 204f0ea27e7SLorenz Bauer daddr, hnum, dif, sdif); 205fddc17deSEric Dumazet } 206fddc17deSEric Dumazet } 207fddc17deSEric Dumazet return result; 208fddc17deSEric Dumazet } 209fddc17deSEric Dumazet 210ca065d0cSEric Dumazet /* rcu_read_lock() must be held */ 211fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net, 21288440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 21388440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 2141801b570SDavid Ahern int dif, int sdif, struct udp_table *udptable, 215538950a1SCraig Gallek struct sk_buff *skb) 216645ca708SEric Dumazet { 217645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 21823b0269eSPeter Oskolkov unsigned int hash2, slot2; 21923b0269eSPeter Oskolkov struct udp_hslot *hslot2; 2206d4201b1SJakub Sitnicki struct sock *result, *sk; 221645ca708SEric Dumazet 222f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum); 223fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 224fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 225fddc17deSEric Dumazet 2266d4201b1SJakub Sitnicki /* Lookup connected or non-wildcard sockets */ 227fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport, 22873545373STim Beale daddr, hnum, dif, sdif, 229d1e37288SSu, Xuemin hslot2, skb); 2306d4201b1SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 2316d4201b1SJakub Sitnicki goto done; 2326d4201b1SJakub Sitnicki 2336d4201b1SJakub Sitnicki /* Lookup redirect from BPF */ 2346c886db2SLorenz Bauer if (static_branch_unlikely(&bpf_sk_lookup_enabled) && 2356c886db2SLorenz Bauer udptable == net->ipv4.udp_table) { 2366c886db2SLorenz Bauer sk = inet6_lookup_run_sk_lookup(net, IPPROTO_UDP, skb, sizeof(struct udphdr), 2376c886db2SLorenz Bauer saddr, sport, daddr, hnum, dif, 2386c886db2SLorenz Bauer udp6_ehashfn); 2396d4201b1SJakub Sitnicki if (sk) { 2406d4201b1SJakub Sitnicki result = sk; 2416d4201b1SJakub Sitnicki goto done; 2426d4201b1SJakub Sitnicki } 2436d4201b1SJakub Sitnicki } 2446d4201b1SJakub Sitnicki 2456d4201b1SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 2466d4201b1SJakub Sitnicki if (result) 2476d4201b1SJakub Sitnicki goto done; 2486d4201b1SJakub Sitnicki 2496d4201b1SJakub Sitnicki /* Lookup wildcard sockets */ 250f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum); 251fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 252fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 253fddc17deSEric Dumazet 2541223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport, 25523b0269eSPeter Oskolkov &in6addr_any, hnum, dif, sdif, 25673545373STim Beale hslot2, skb); 2576d4201b1SJakub Sitnicki done: 25826f8113cSEnrico Weigelt if (IS_ERR(result)) 2598217ca65SMartin KaFai Lau return NULL; 260fddc17deSEric Dumazet return result; 261fddc17deSEric Dumazet } 262fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 263db8dac20SDavid S. Miller 264607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 265607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 266645ca708SEric Dumazet struct udp_table *udptable) 267607c4aafSKOVACS Krisztian { 268b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 269607c4aafSKOVACS Krisztian 270ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 271607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 2721801b570SDavid Ahern inet6_sdif(skb), udptable, skb); 273607c4aafSKOVACS Krisztian } 274607c4aafSKOVACS Krisztian 2757b58e63eSEric Dumazet struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb, 27663058308STom Herbert __be16 sport, __be16 dport) 27763058308STom Herbert { 27863058308STom Herbert const struct ipv6hdr *iph = ipv6_hdr(skb); 279ba6aac15SKuniyuki Iwashima struct net *net = dev_net(skb->dev); 2807938cd15SRichard Gobert int iif, sdif; 2817938cd15SRichard Gobert 2827938cd15SRichard Gobert inet6_get_iif_sdif(skb, &iif, &sdif); 28363058308STom Herbert 284ba6aac15SKuniyuki Iwashima return __udp6_lib_lookup(net, &iph->saddr, sport, 2857938cd15SRichard Gobert &iph->daddr, dport, iif, 2867938cd15SRichard Gobert sdif, net->ipv4.udp_table, NULL); 28763058308STom Herbert } 28863058308STom Herbert 289ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 290ca065d0cSEric Dumazet * Does increment socket refcount. 291ca065d0cSEric Dumazet */ 2926e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6) 293aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 294aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif) 295aa976fc0SBalazs Scheidler { 296ca065d0cSEric Dumazet struct sock *sk; 297ca065d0cSEric Dumazet 298ca065d0cSEric Dumazet sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, 299ba6aac15SKuniyuki Iwashima dif, 0, net->ipv4.udp_table, NULL); 30041c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 301ca065d0cSEric Dumazet sk = NULL; 302ca065d0cSEric Dumazet return sk; 303aa976fc0SBalazs Scheidler } 304aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup); 305ca065d0cSEric Dumazet #endif 306aa976fc0SBalazs Scheidler 307cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the 308cb891fa6SPaolo Abeni * scratch area space; note that the IP6CB flags is still in the first 309cb891fa6SPaolo Abeni * cacheline, so checking for jumbograms is cheap 310cb891fa6SPaolo Abeni */ 311cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb) 312cb891fa6SPaolo Abeni { 313cb891fa6SPaolo Abeni return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb); 314cb891fa6SPaolo Abeni } 315cb891fa6SPaolo Abeni 316db8dac20SDavid S. Miller /* 317db8dac20SDavid S. Miller * This should be easy, if there is something there we 318db8dac20SDavid S. Miller * return it, otherwise we block. 319db8dac20SDavid S. Miller */ 320db8dac20SDavid S. Miller 3211b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 322ec095263SOliver Hartkopp int flags, int *addr_len) 323db8dac20SDavid S. Miller { 324db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 325db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 326db8dac20SDavid S. Miller struct sk_buff *skb; 32759c2cdaeSDavid S. Miller unsigned int ulen, copied; 328fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 329db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 330543fc3fbSPaolo Abeni struct udp_mib __percpu *mib; 331197c949eSEric Dumazet bool checksum_valid = false; 332f26ba175SWei Yongjun int is_udp4; 333db8dac20SDavid S. Miller 334db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 33585fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 336db8dac20SDavid S. Miller 3374b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 33885fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 3394b340ae2SBrian Haley 340db8dac20SDavid S. Miller try_again: 341a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 342ec095263SOliver Hartkopp skb = __skb_recv_udp(sk, flags, &off, &err); 343db8dac20SDavid S. Miller if (!skb) 344627d2d6bSsamanthakumar return err; 345db8dac20SDavid S. Miller 346cb891fa6SPaolo Abeni ulen = udp6_skb_len(skb); 34759c2cdaeSDavid S. Miller copied = len; 348627d2d6bSsamanthakumar if (copied > ulen - off) 349627d2d6bSsamanthakumar copied = ulen - off; 35059c2cdaeSDavid S. Miller else if (copied < ulen) 351db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 352db8dac20SDavid S. Miller 353f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 354029a3743SPaolo Abeni mib = __UDPX_MIB(sk, is_udp4); 355f26ba175SWei Yongjun 356db8dac20SDavid S. Miller /* 357db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 358db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 359db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 360db8dac20SDavid S. Miller */ 361db8dac20SDavid S. Miller 362d21dbdfeSEric Dumazet if (copied < ulen || peeking || 363d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 36467a51780SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 36567a51780SPaolo Abeni !__udp_lib_checksum_complete(skb); 366197c949eSEric Dumazet if (!checksum_valid) 367db8dac20SDavid S. Miller goto csum_copy_err; 368db8dac20SDavid S. Miller } 369db8dac20SDavid S. Miller 37067a51780SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 37167a51780SPaolo Abeni if (udp_skb_is_linear(skb)) 37267a51780SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 37367a51780SPaolo Abeni else 374627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 37567a51780SPaolo Abeni } else { 376627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 377db8dac20SDavid S. Miller if (err == -EINVAL) 378db8dac20SDavid S. Miller goto csum_copy_err; 379db8dac20SDavid S. Miller } 38022911fc5SEric Dumazet if (unlikely(err)) { 381fd69c399SPaolo Abeni if (!peeking) { 382979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 383029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 384979402b1SEric Dumazet } 385850cbaddSPaolo Abeni kfree_skb(skb); 386627d2d6bSsamanthakumar return err; 38722911fc5SEric Dumazet } 388fd69c399SPaolo Abeni if (!peeking) 389029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS); 390db8dac20SDavid S. Miller 3916fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 392db8dac20SDavid S. Miller 393db8dac20SDavid S. Miller /* Copy the address. */ 394db8dac20SDavid S. Miller if (msg->msg_name) { 395342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 396db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 397db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 398db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 399db8dac20SDavid S. Miller 400842df073SHannes Frederic Sowa if (is_udp4) { 401b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 402b301e82cSBrian Haley &sin6->sin6_addr); 403842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0; 404842df073SHannes Frederic Sowa } else { 4054e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 406842df073SHannes Frederic Sowa sin6->sin6_scope_id = 407842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr, 4084330487aSDuan Jiong inet6_iif(skb)); 409db8dac20SDavid S. Miller } 410bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 411983695faSDaniel Borkmann 412983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, 413983695faSDaniel Borkmann (struct sockaddr *)sin6); 414db8dac20SDavid S. Miller } 4154b261c75SHannes Frederic Sowa 41667188b2eSEric Dumazet if (udp_test_bit(GRO_ENABLED, sk)) 417bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 418bcd1665eSPaolo Abeni 4194b261c75SHannes Frederic Sowa if (np->rxopt.all) 4204b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb); 4214b261c75SHannes Frederic Sowa 422f26ba175SWei Yongjun if (is_udp4) { 423c274af22SEric Dumazet if (inet_cmsg_flags(inet)) 424ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, 42510df8e61SEric Dumazet sizeof(struct udphdr), off); 426db8dac20SDavid S. Miller } else { 427db8dac20SDavid S. Miller if (np->rxopt.all) 4284b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb); 429db8dac20SDavid S. Miller } 430db8dac20SDavid S. Miller 43159c2cdaeSDavid S. Miller err = copied; 432db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 433db8dac20SDavid S. Miller err = ulen; 434db8dac20SDavid S. Miller 435850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 436db8dac20SDavid S. Miller return err; 437db8dac20SDavid S. Miller 438db8dac20SDavid S. Miller csum_copy_err: 4392276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 4402276f58aSPaolo Abeni udp_skb_destructor)) { 441029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS); 442029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 4436a5dc9e5SEric Dumazet } 444850cbaddSPaolo Abeni kfree_skb(skb); 445db8dac20SDavid S. Miller 446beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 447beb39db5SEric Dumazet cond_resched(); 4489cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 449db8dac20SDavid S. Miller goto try_again; 450db8dac20SDavid S. Miller } 451db8dac20SDavid S. Miller 452a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 453a36e185eSStefano Brivio void udpv6_encap_enable(void) 454a36e185eSStefano Brivio { 4559c480601SPaolo Abeni static_branch_inc(&udpv6_encap_needed_key); 456a36e185eSStefano Brivio } 457a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable); 458a36e185eSStefano Brivio 459e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 460e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 461e7cc0824SStefano Brivio */ 462e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb, 463e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 464424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info) 465e7cc0824SStefano Brivio { 466e7cc0824SStefano Brivio int i; 467e7cc0824SStefano Brivio 468e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 469e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 470424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info); 471424a7cd0SPaolo Abeni const struct ip6_tnl_encap_ops *encap; 472e7cc0824SStefano Brivio 473424a7cd0SPaolo Abeni encap = rcu_dereference(ip6tun_encaps[i]); 474424a7cd0SPaolo Abeni if (!encap) 475e7cc0824SStefano Brivio continue; 476424a7cd0SPaolo Abeni handler = encap->err_handler; 477e7cc0824SStefano Brivio if (handler && !handler(skb, opt, type, code, offset, info)) 478e7cc0824SStefano Brivio return 0; 479e7cc0824SStefano Brivio } 480e7cc0824SStefano Brivio 481e7cc0824SStefano Brivio return -ENOENT; 482e7cc0824SStefano Brivio } 483e7cc0824SStefano Brivio 484a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 485a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 486a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 487a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 488a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 489a36e185eSStefano Brivio * trace ICMP messages back to them. 490a36e185eSStefano Brivio * 491e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 492e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 493e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 494e7cc0824SStefano Brivio * 495a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 496a36e185eSStefano Brivio * association. 497a36e185eSStefano Brivio * 498e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 499e7cc0824SStefano Brivio * processing, zero otherwise. 500a36e185eSStefano Brivio */ 501a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net, 502a36e185eSStefano Brivio const struct ipv6hdr *hdr, int offset, 503a36e185eSStefano Brivio struct udphdr *uh, 504a36e185eSStefano Brivio struct udp_table *udptable, 5059bfce73cSVadim Fedorenko struct sock *sk, 506e7cc0824SStefano Brivio struct sk_buff *skb, 507e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 508e7cc0824SStefano Brivio u8 type, u8 code, __be32 info) 509a36e185eSStefano Brivio { 5109bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb); 511a36e185eSStefano Brivio int network_offset, transport_offset; 5129bfce73cSVadim Fedorenko struct udp_sock *up; 513a36e185eSStefano Brivio 514a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 515a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 516a36e185eSStefano Brivio 517a36e185eSStefano Brivio /* Network header needs to point to the outer IPv6 header inside ICMP */ 518a36e185eSStefano Brivio skb_reset_network_header(skb); 519a36e185eSStefano Brivio 520a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 521a36e185eSStefano Brivio skb_set_transport_header(skb, offset); 522a36e185eSStefano Brivio 5239bfce73cSVadim Fedorenko if (sk) { 5249bfce73cSVadim Fedorenko up = udp_sk(sk); 5259bfce73cSVadim Fedorenko 5269bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup); 5279bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb)) 5289bfce73cSVadim Fedorenko sk = NULL; 5299bfce73cSVadim Fedorenko 5309bfce73cSVadim Fedorenko goto out; 5319bfce73cSVadim Fedorenko } 5329bfce73cSVadim Fedorenko 533e7cc0824SStefano Brivio sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source, 534e7cc0824SStefano Brivio &hdr->saddr, uh->dest, 535e7cc0824SStefano Brivio inet6_iif(skb), 0, udptable, skb); 536e7cc0824SStefano Brivio if (sk) { 5379bfce73cSVadim Fedorenko up = udp_sk(sk); 538e7cc0824SStefano Brivio 539a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 540a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 541a36e185eSStefano Brivio sk = NULL; 542e7cc0824SStefano Brivio } 543e7cc0824SStefano Brivio 5449bfce73cSVadim Fedorenko out: 545e7cc0824SStefano Brivio if (!sk) { 546e7cc0824SStefano Brivio sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code, 547e7cc0824SStefano Brivio offset, info)); 548e7cc0824SStefano Brivio } 549a36e185eSStefano Brivio 550a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 551a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 552e7cc0824SStefano Brivio 553a36e185eSStefano Brivio return sk; 554a36e185eSStefano Brivio } 555a36e185eSStefano Brivio 55632bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 557d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 558645ca708SEric Dumazet struct udp_table *udptable) 559db8dac20SDavid S. Miller { 560db8dac20SDavid S. Miller struct ipv6_pinfo *np; 561b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 562b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr; 563222a011eSAndrew Lunn const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr; 564db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset); 565a36e185eSStefano Brivio bool tunnel = false; 566db8dac20SDavid S. Miller struct sock *sk; 567e0d8c1b7SWei Wang int harderr; 568db8dac20SDavid S. Miller int err; 5697304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 570db8dac20SDavid S. Miller 571e32ea7e7SCraig Gallek sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source, 5724ac30c4bSMartin KaFai Lau inet6_iif(skb), inet6_sdif(skb), udptable, NULL); 5739bfce73cSVadim Fedorenko 574*4781a75dSEric Dumazet if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) { 575a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 576a36e185eSStefano Brivio if (static_branch_unlikely(&udpv6_encap_needed_key)) { 577a36e185eSStefano Brivio sk = __udp6_lib_err_encap(net, hdr, offset, uh, 5789bfce73cSVadim Fedorenko udptable, sk, skb, 579e7cc0824SStefano Brivio opt, type, code, info); 580e7cc0824SStefano Brivio if (!sk) 581e7cc0824SStefano Brivio return 0; 5829bfce73cSVadim Fedorenko } else 5839bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT); 584a36e185eSStefano Brivio 585e7cc0824SStefano Brivio if (IS_ERR(sk)) { 586a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), 5877304fe46SDuan Jiong ICMP6_MIB_INERRORS); 588e7cc0824SStefano Brivio return PTR_ERR(sk); 5897304fe46SDuan Jiong } 590e7cc0824SStefano Brivio 591a36e185eSStefano Brivio tunnel = true; 592a36e185eSStefano Brivio } 593db8dac20SDavid S. Miller 594e0d8c1b7SWei Wang harderr = icmpv6_err_convert(type, code, &err); 595e0d8c1b7SWei Wang np = inet6_sk(sk); 596e0d8c1b7SWei Wang 59793b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) { 59893b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk)) 59993b36cf3SHannes Frederic Sowa goto out; 60081aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 601e0d8c1b7SWei Wang if (np->pmtudisc != IPV6_PMTUDISC_DONT) 602e0d8c1b7SWei Wang harderr = 1; 60393b36cf3SHannes Frederic Sowa } 6041a462d18SDuan Jiong if (type == NDISC_REDIRECT) { 605a36e185eSStefano Brivio if (tunnel) { 606a36e185eSStefano Brivio ip6_redirect(skb, sock_net(sk), inet6_iif(skb), 6073c5b4d69SEric Dumazet READ_ONCE(sk->sk_mark), sk->sk_uid); 608a36e185eSStefano Brivio } else { 609ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 610a36e185eSStefano Brivio } 6111a462d18SDuan Jiong goto out; 6121a462d18SDuan Jiong } 61381aded24SDavid S. Miller 614a36e185eSStefano Brivio /* Tunnels don't have an application socket: don't pass errors back */ 615ac56a0b4SDavid Howells if (tunnel) { 616ac56a0b4SDavid Howells if (udp_sk(sk)->encap_err_rcv) 61742fb06b3SDavid Howells udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest, 61842fb06b3SDavid Howells ntohl(info), (u8 *)(uh+1)); 619a36e185eSStefano Brivio goto out; 620ac56a0b4SDavid Howells } 621a36e185eSStefano Brivio 622e0d8c1b7SWei Wang if (!np->recverr) { 623e0d8c1b7SWei Wang if (!harderr || sk->sk_state != TCP_ESTABLISHED) 624db8dac20SDavid S. Miller goto out; 625e0d8c1b7SWei Wang } else { 626db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 627e0d8c1b7SWei Wang } 628b1faf566SEric Dumazet 629db8dac20SDavid S. Miller sk->sk_err = err; 630e3ae2365SAlexander Aring sk_error_report(sk); 631db8dac20SDavid S. Miller out: 63232bbd879SStefano Brivio return 0; 633db8dac20SDavid S. Miller } 634db8dac20SDavid S. Miller 635a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 636f7ad74feSBenjamin LaHaise { 637f7ad74feSBenjamin LaHaise int rc; 638f7ad74feSBenjamin LaHaise 639efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 640f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb); 641005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 6422c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 643e68b6e50SEric Dumazet } else { 644e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 645005ec974SShawn Bohrer } 646f7ad74feSBenjamin LaHaise 647850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 648f7ad74feSBenjamin LaHaise if (rc < 0) { 649f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk); 6500d92efdeSDonald Hunter enum skb_drop_reason drop_reason; 651f7ad74feSBenjamin LaHaise 652f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */ 6530d92efdeSDonald Hunter if (rc == -ENOMEM) { 654e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), 655f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite); 6560d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF; 6570d92efdeSDonald Hunter } else { 658a3ce2b10SMenglong Dong UDP6_INC_STATS(sock_net(sk), 659a3ce2b10SMenglong Dong UDP_MIB_MEMERRORS, is_udplite); 6600d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_PROTO_MEM; 6610d92efdeSDonald Hunter } 662e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 6630d92efdeSDonald Hunter kfree_skb_reason(skb, drop_reason); 6648139dccdSIvan Babrou trace_udp_fail_queue_rcv_skb(rc, sk); 665f7ad74feSBenjamin LaHaise return -1; 666f7ad74feSBenjamin LaHaise } 667850cbaddSPaolo Abeni 668f7ad74feSBenjamin LaHaise return 0; 669f7ad74feSBenjamin LaHaise } 670f7ad74feSBenjamin LaHaise 67132bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb, 672d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 673d5fdd6baSBrian Haley u8 code, int offset, __be32 info) 674db8dac20SDavid S. Miller { 675ba6aac15SKuniyuki Iwashima return __udp6_lib_err(skb, opt, type, code, offset, info, 676ba6aac15SKuniyuki Iwashima dev_net(skb->dev)->ipv4.udp_table); 677db8dac20SDavid S. Miller } 678db8dac20SDavid S. Miller 679cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 680db8dac20SDavid S. Miller { 6810d92efdeSDonald Hunter enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 682db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 683db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 684db8dac20SDavid S. Miller 6850d92efdeSDonald Hunter if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { 6860d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_XFRM_POLICY; 687db8dac20SDavid S. Miller goto drop; 6880d92efdeSDonald Hunter } 689b0e214d2SMadhu Koriginja nf_reset_ct(skb); 690db8dac20SDavid S. Miller 691*4781a75dSEric Dumazet if (static_branch_unlikely(&udpv6_encap_needed_key) && 692*4781a75dSEric Dumazet READ_ONCE(up->encap_type)) { 693d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 694d7f3f621SBenjamin LaHaise 695d7f3f621SBenjamin LaHaise /* 696d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to 697d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just 698d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket. 699d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value: 700d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap 701d7f3f621SBenjamin LaHaise * handler or was discarded by it. 702d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP. 703d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N 704d7f3f621SBenjamin LaHaise */ 705d7f3f621SBenjamin LaHaise 706d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */ 7076aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 708e5aed006SHannes Frederic Sowa if (encap_rcv) { 709d7f3f621SBenjamin LaHaise int ret; 710d7f3f621SBenjamin LaHaise 7110a80966bSTom Herbert /* Verify checksum before giving to encap */ 7120a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 7130a80966bSTom Herbert goto csum_error; 7140a80966bSTom Herbert 715d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb); 716d7f3f621SBenjamin LaHaise if (ret <= 0) { 717250962e4SMenglong Dong __UDP6_INC_STATS(sock_net(sk), 718d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS, 719d7f3f621SBenjamin LaHaise is_udplite); 720d7f3f621SBenjamin LaHaise return -ret; 721d7f3f621SBenjamin LaHaise } 722d7f3f621SBenjamin LaHaise } 723d7f3f621SBenjamin LaHaise 724d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */ 725d7f3f621SBenjamin LaHaise } 726d7f3f621SBenjamin LaHaise 727db8dac20SDavid S. Miller /* 728db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 729db8dac20SDavid S. Miller */ 730b0a42277SMiaohe Lin if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 731db8dac20SDavid S. Miller 732db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 733ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 734db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 735db8dac20SDavid S. Miller goto drop; 736db8dac20SDavid S. Miller } 737db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 738ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 739db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 740db8dac20SDavid S. Miller goto drop; 741db8dac20SDavid S. Miller } 742db8dac20SDavid S. Miller } 743db8dac20SDavid S. Miller 7444b943faeSPaolo Abeni prefetch(&sk->sk_rmem_alloc); 745ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 746ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 7476a5dc9e5SEric Dumazet goto csum_error; 748ce25d66aSEric Dumazet 7490d92efdeSDonald Hunter if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) { 7500d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 751a6127697SMichal Kubeček goto drop; 7520d92efdeSDonald Hunter } 753db8dac20SDavid S. Miller 754e6afc8acSsamanthakumar udp_csum_pull_header(skb); 755cb80ef46SBenjamin LaHaise 756d826eb14SEric Dumazet skb_dst_drop(skb); 757db8dac20SDavid S. Miller 758850cbaddSPaolo Abeni return __udpv6_queue_rcv_skb(sk, skb); 7593e215c8dSJames M Leddy 7606a5dc9e5SEric Dumazet csum_error: 7610d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_UDP_CSUM; 76202c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 763db8dac20SDavid S. Miller drop: 76402c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 765cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops); 7660d92efdeSDonald Hunter kfree_skb_reason(skb, drop_reason); 767db8dac20SDavid S. Miller return -1; 768db8dac20SDavid S. Miller } 769db8dac20SDavid S. Miller 770cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 771cf329aa4SPaolo Abeni { 772cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 773cf329aa4SPaolo Abeni int ret; 774cf329aa4SPaolo Abeni 775cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 776cf329aa4SPaolo Abeni return udpv6_queue_rcv_one_skb(sk, skb); 777cf329aa4SPaolo Abeni 778cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 779cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, false); 7801a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 781cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 782cf329aa4SPaolo Abeni 783000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb); 784cf329aa4SPaolo Abeni ret = udpv6_queue_rcv_one_skb(sk, skb); 785cf329aa4SPaolo Abeni if (ret > 0) 786cf329aa4SPaolo Abeni ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret, 787cf329aa4SPaolo Abeni true); 788cf329aa4SPaolo Abeni } 789cf329aa4SPaolo Abeni return 0; 790cf329aa4SPaolo Abeni } 791cf329aa4SPaolo Abeni 792dc3731baSEric Dumazet static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk, 793b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr, 794b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr, 7957bd2db40SDewi Morgan int dif, int sdif, unsigned short hnum) 796db8dac20SDavid S. Miller { 797dc3731baSEric Dumazet const struct inet_sock *inet = inet_sk(sk); 798db8dac20SDavid S. Miller 7999e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net)) 8005cf3d461SDavid Held return false; 801b8ad0cbcSDaniel Lezcano 8025cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum || 8035cf3d461SDavid Held sk->sk_family != PF_INET6 || 8045cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) || 8055cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) && 8065cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 8074c971d2fSEric Dumazet !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) || 80833b4b015SHenning Rogge (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && 80933b4b015SHenning Rogge !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) 8105cf3d461SDavid Held return false; 8119e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 8125cf3d461SDavid Held return false; 8135cf3d461SDavid Held return true; 814db8dac20SDavid S. Miller } 815db8dac20SDavid S. Miller 8164068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb) 8174068579eSTom Herbert { 8184068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log 8194068579eSTom Herbert * this error. Well, it is reasonable. 8204068579eSTom Herbert */ 821ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 8224068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 8234068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 8244068579eSTom Herbert } 8254068579eSTom Herbert 826db8dac20SDavid S. Miller /* 827db8dac20SDavid S. Miller * Note: called only from the BH handler context, 828db8dac20SDavid S. Miller * so we don't need to lock the hashes. 829db8dac20SDavid S. Miller */ 830e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 831b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr, 83236cbb245SRick Jones struct udp_table *udptable, int proto) 833db8dac20SDavid S. Miller { 834ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 835db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 8365cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 8375cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 838ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 8392dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 840ca065d0cSEric Dumazet int dif = inet6_iif(skb); 8417bd2db40SDewi Morgan int sdif = inet6_sdif(skb); 842ca065d0cSEric Dumazet struct hlist_node *node; 843ca065d0cSEric Dumazet struct sk_buff *nskb; 8442dc41cffSDavid Held 8452dc41cffSDavid Held if (use_hash2) { 846f0b1e64cSMartin KaFai Lau hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) & 84773e2d5e3SPablo Neira udptable->mask; 848f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask; 8492dc41cffSDavid Held start_lookup: 85073e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 8512dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 8522dc41cffSDavid Held } 853db8dac20SDavid S. Miller 854ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 855ca065d0cSEric Dumazet if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr, 8567bd2db40SDewi Morgan uh->source, saddr, dif, sdif, 8577bd2db40SDewi Morgan hnum)) 858ca065d0cSEric Dumazet continue; 8591c19448cSTom Herbert /* If zero checksum and no_check is not on for 8604068579eSTom Herbert * the socket then skip it. 8614068579eSTom Herbert */ 86206de37fcSEric Dumazet if (!uh->check && !udp_get_no_check6_rx(sk)) 863ca065d0cSEric Dumazet continue; 864ca065d0cSEric Dumazet if (!first) { 865ca065d0cSEric Dumazet first = sk; 866ca065d0cSEric Dumazet continue; 867db8dac20SDavid S. Miller } 868ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 869ca065d0cSEric Dumazet if (unlikely(!nskb)) { 870ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 87102c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 872ca065d0cSEric Dumazet IS_UDPLITE(sk)); 87302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, 874ca065d0cSEric Dumazet IS_UDPLITE(sk)); 875ca065d0cSEric Dumazet continue; 876a1ab77f9SEric Dumazet } 877db8dac20SDavid S. Miller 878ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(sk, nskb) > 0) 879ca065d0cSEric Dumazet consume_skb(nskb); 880ca065d0cSEric Dumazet } 881a1ab77f9SEric Dumazet 8822dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 8832dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 8842dc41cffSDavid Held hash2 = hash2_any; 8852dc41cffSDavid Held goto start_lookup; 8862dc41cffSDavid Held } 8872dc41cffSDavid Held 888ca065d0cSEric Dumazet if (first) { 889ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(first, skb) > 0) 890ca065d0cSEric Dumazet consume_skb(skb); 891a1ab77f9SEric Dumazet } else { 892ca065d0cSEric Dumazet kfree_skb(skb); 89302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 89436cbb245SRick Jones proto == IPPROTO_UDPLITE); 895a1ab77f9SEric Dumazet } 896db8dac20SDavid S. Miller return 0; 897db8dac20SDavid S. Miller } 898db8dac20SDavid S. Miller 89964f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 90064f0f5d1SPaolo Abeni { 90164f0f5d1SPaolo Abeni if (udp_sk_rx_dst_set(sk, dst)) { 90264f0f5d1SPaolo Abeni const struct rt6_info *rt = (const struct rt6_info *)dst; 90364f0f5d1SPaolo Abeni 904ef57c161SEric Dumazet sk->sk_rx_dst_cookie = rt6_get_cookie(rt); 90564f0f5d1SPaolo Abeni } 90664f0f5d1SPaolo Abeni } 90764f0f5d1SPaolo Abeni 908eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 909eb63f296SPaolo Abeni * return code conversion for ip layer consumption 910eb63f296SPaolo Abeni */ 911eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 912eb63f296SPaolo Abeni struct udphdr *uh) 913eb63f296SPaolo Abeni { 914eb63f296SPaolo Abeni int ret; 915eb63f296SPaolo Abeni 916eb63f296SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 917e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo); 918eb63f296SPaolo Abeni 919eb63f296SPaolo Abeni ret = udpv6_queue_rcv_skb(sk, skb); 920eb63f296SPaolo Abeni 92184dad559SPaolo Abeni /* a return value > 0 means to resubmit the input */ 922eb63f296SPaolo Abeni if (ret > 0) 92384dad559SPaolo Abeni return ret; 924eb63f296SPaolo Abeni return 0; 925eb63f296SPaolo Abeni } 926eb63f296SPaolo Abeni 927645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 928db8dac20SDavid S. Miller int proto) 929db8dac20SDavid S. Miller { 9304cf91f82SDavid Ahern enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 931b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 932ca065d0cSEric Dumazet struct net *net = dev_net(skb->dev); 933ca065d0cSEric Dumazet struct udphdr *uh; 934ca065d0cSEric Dumazet struct sock *sk; 93571489e21SJoe Stringer bool refcounted; 936db8dac20SDavid S. Miller u32 ulen = 0; 937db8dac20SDavid S. Miller 938db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 939d6bc0149SBjørn Mork goto discard; 940db8dac20SDavid S. Miller 941db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 942db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 943db8dac20SDavid S. Miller uh = udp_hdr(skb); 944db8dac20SDavid S. Miller 945db8dac20SDavid S. Miller ulen = ntohs(uh->len); 946db8dac20SDavid S. Miller if (ulen > skb->len) 947db8dac20SDavid S. Miller goto short_packet; 948db8dac20SDavid S. Miller 949db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 950db8dac20SDavid S. Miller /* UDP validates ulen. */ 951db8dac20SDavid S. Miller 952db8dac20SDavid S. Miller /* Check for jumbo payload */ 953db8dac20SDavid S. Miller if (ulen == 0) 954db8dac20SDavid S. Miller ulen = skb->len; 955db8dac20SDavid S. Miller 956db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 957db8dac20SDavid S. Miller goto short_packet; 958db8dac20SDavid S. Miller 959db8dac20SDavid S. Miller if (ulen < skb->len) { 960db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 961db8dac20SDavid S. Miller goto short_packet; 962db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 963db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 964db8dac20SDavid S. Miller uh = udp_hdr(skb); 965db8dac20SDavid S. Miller } 966db8dac20SDavid S. Miller } 967db8dac20SDavid S. Miller 968db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 9696a5dc9e5SEric Dumazet goto csum_error; 970db8dac20SDavid S. Miller 971c9f2c1aeSPaolo Abeni /* Check if the socket is already available, e.g. due to early demux */ 9729c02bec9SLorenz Bauer sk = inet6_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest, 9739c02bec9SLorenz Bauer &refcounted, udp6_ehashfn); 9749c02bec9SLorenz Bauer if (IS_ERR(sk)) 9759c02bec9SLorenz Bauer goto no_sk; 9769c02bec9SLorenz Bauer 977c9f2c1aeSPaolo Abeni if (sk) { 978c9f2c1aeSPaolo Abeni struct dst_entry *dst = skb_dst(skb); 979c9f2c1aeSPaolo Abeni int ret; 980c9f2c1aeSPaolo Abeni 9818f905c0eSEric Dumazet if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 98264f0f5d1SPaolo Abeni udp6_sk_rx_dst_set(sk, dst); 983c9f2c1aeSPaolo Abeni 98406de37fcSEric Dumazet if (!uh->check && !udp_get_no_check6_rx(sk)) { 98571489e21SJoe Stringer if (refcounted) 986c9f2c1aeSPaolo Abeni sock_put(sk); 987eb63f296SPaolo Abeni goto report_csum_error; 988eb63f296SPaolo Abeni } 989c9f2c1aeSPaolo Abeni 990eb63f296SPaolo Abeni ret = udp6_unicast_rcv_skb(sk, skb, uh); 99171489e21SJoe Stringer if (refcounted) 992eb63f296SPaolo Abeni sock_put(sk); 993c9f2c1aeSPaolo Abeni return ret; 994c9f2c1aeSPaolo Abeni } 995c9f2c1aeSPaolo Abeni 996db8dac20SDavid S. Miller /* 997db8dac20SDavid S. Miller * Multicast receive code 998db8dac20SDavid S. Miller */ 999db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 1000e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 100136cbb245SRick Jones saddr, daddr, udptable, proto); 1002db8dac20SDavid S. Miller 1003db8dac20SDavid S. Miller /* Unicast */ 1004607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 100553b24b8fSIan Morris if (sk) { 100606de37fcSEric Dumazet if (!uh->check && !udp_get_no_check6_rx(sk)) 1007eb63f296SPaolo Abeni goto report_csum_error; 1008eb63f296SPaolo Abeni return udp6_unicast_rcv_skb(sk, skb, uh); 10094068579eSTom Herbert } 10109c02bec9SLorenz Bauer no_sk: 10114cf91f82SDavid Ahern reason = SKB_DROP_REASON_NO_SOCKET; 10124cf91f82SDavid Ahern 1013eb63f296SPaolo Abeni if (!uh->check) 1014eb63f296SPaolo Abeni goto report_csum_error; 10154068579eSTom Herbert 1016db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 1017db8dac20SDavid S. Miller goto discard; 1018b0e214d2SMadhu Koriginja nf_reset_ct(skb); 1019db8dac20SDavid S. Miller 1020db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 10216a5dc9e5SEric Dumazet goto csum_error; 1022db8dac20SDavid S. Miller 102302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 10243ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 1025db8dac20SDavid S. Miller 10264cf91f82SDavid Ahern kfree_skb_reason(skb, reason); 1027db8dac20SDavid S. Miller return 0; 1028db8dac20SDavid S. Miller 1029db8dac20SDavid S. Miller short_packet: 10304cf91f82SDavid Ahern if (reason == SKB_DROP_REASON_NOT_SPECIFIED) 10314cf91f82SDavid Ahern reason = SKB_DROP_REASON_PKT_TOO_SMALL; 1032ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 1033db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1034ba7a46f1SJoe Perches saddr, ntohs(uh->source), 1035ba7a46f1SJoe Perches ulen, skb->len, 1036ba7a46f1SJoe Perches daddr, ntohs(uh->dest)); 10376a5dc9e5SEric Dumazet goto discard; 1038eb63f296SPaolo Abeni 1039eb63f296SPaolo Abeni report_csum_error: 1040eb63f296SPaolo Abeni udp6_csum_zero_error(skb); 10416a5dc9e5SEric Dumazet csum_error: 10424cf91f82SDavid Ahern if (reason == SKB_DROP_REASON_NOT_SPECIFIED) 10434cf91f82SDavid Ahern reason = SKB_DROP_REASON_UDP_CSUM; 104402c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 1045db8dac20SDavid S. Miller discard: 104602c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 10474cf91f82SDavid Ahern kfree_skb_reason(skb, reason); 1048db8dac20SDavid S. Miller return 0; 1049db8dac20SDavid S. Miller } 1050db8dac20SDavid S. Miller 10510bd84065Ssubashab@codeaurora.org 10525425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net, 10535425077dSsubashab@codeaurora.org __be16 loc_port, const struct in6_addr *loc_addr, 10545425077dSsubashab@codeaurora.org __be16 rmt_port, const struct in6_addr *rmt_addr, 10554297a0efSDavid Ahern int dif, int sdif) 10565425077dSsubashab@codeaurora.org { 1057ba6aac15SKuniyuki Iwashima struct udp_table *udptable = net->ipv4.udp_table; 10580bd84065Ssubashab@codeaurora.org unsigned short hnum = ntohs(loc_port); 1059919dfa0bSKuniyuki Iwashima unsigned int hash2, slot2; 1060919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot2; 1061919dfa0bSKuniyuki Iwashima __portpair ports; 10625425077dSsubashab@codeaurora.org struct sock *sk; 10635425077dSsubashab@codeaurora.org 1064919dfa0bSKuniyuki Iwashima hash2 = ipv6_portaddr_hash(net, loc_addr, hnum); 1065ba6aac15SKuniyuki Iwashima slot2 = hash2 & udptable->mask; 1066ba6aac15SKuniyuki Iwashima hslot2 = &udptable->hash2[slot2]; 1067919dfa0bSKuniyuki Iwashima ports = INET_COMBINED_PORTS(rmt_port, hnum); 1068919dfa0bSKuniyuki Iwashima 10690bd84065Ssubashab@codeaurora.org udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 107085cb73ffSWei Wang if (sk->sk_state == TCP_ESTABLISHED && 10715d368f03SEric Dumazet inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif)) 10725425077dSsubashab@codeaurora.org return sk; 10730bd84065Ssubashab@codeaurora.org /* Only check first socket in chain */ 10740bd84065Ssubashab@codeaurora.org break; 10750bd84065Ssubashab@codeaurora.org } 10760bd84065Ssubashab@codeaurora.org return NULL; 10775425077dSsubashab@codeaurora.org } 10785425077dSsubashab@codeaurora.org 107911052589SKuniyuki Iwashima void udp_v6_early_demux(struct sk_buff *skb) 10805425077dSsubashab@codeaurora.org { 10815425077dSsubashab@codeaurora.org struct net *net = dev_net(skb->dev); 10825425077dSsubashab@codeaurora.org const struct udphdr *uh; 10835425077dSsubashab@codeaurora.org struct sock *sk; 10845425077dSsubashab@codeaurora.org struct dst_entry *dst; 10855425077dSsubashab@codeaurora.org int dif = skb->dev->ifindex; 10864297a0efSDavid Ahern int sdif = inet6_sdif(skb); 10875425077dSsubashab@codeaurora.org 10885425077dSsubashab@codeaurora.org if (!pskb_may_pull(skb, skb_transport_offset(skb) + 10895425077dSsubashab@codeaurora.org sizeof(struct udphdr))) 10905425077dSsubashab@codeaurora.org return; 10915425077dSsubashab@codeaurora.org 10925425077dSsubashab@codeaurora.org uh = udp_hdr(skb); 10935425077dSsubashab@codeaurora.org 10945425077dSsubashab@codeaurora.org if (skb->pkt_type == PACKET_HOST) 10955425077dSsubashab@codeaurora.org sk = __udp6_lib_demux_lookup(net, uh->dest, 10965425077dSsubashab@codeaurora.org &ipv6_hdr(skb)->daddr, 10975425077dSsubashab@codeaurora.org uh->source, &ipv6_hdr(skb)->saddr, 10984297a0efSDavid Ahern dif, sdif); 10995425077dSsubashab@codeaurora.org else 11005425077dSsubashab@codeaurora.org return; 11015425077dSsubashab@codeaurora.org 110241c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 11035425077dSsubashab@codeaurora.org return; 11045425077dSsubashab@codeaurora.org 11055425077dSsubashab@codeaurora.org skb->sk = sk; 11065425077dSsubashab@codeaurora.org skb->destructor = sock_efree; 11078f905c0eSEric Dumazet dst = rcu_dereference(sk->sk_rx_dst); 11085425077dSsubashab@codeaurora.org 11095425077dSsubashab@codeaurora.org if (dst) 1110ef57c161SEric Dumazet dst = dst_check(dst, sk->sk_rx_dst_cookie); 11115425077dSsubashab@codeaurora.org if (dst) { 1112d24406c8SWei Wang /* set noref for now. 1113d24406c8SWei Wang * any place which wants to hold dst has to call 1114d24406c8SWei Wang * dst_hold_safe() 1115d24406c8SWei Wang */ 11165425077dSsubashab@codeaurora.org skb_dst_set_noref(skb, dst); 11175425077dSsubashab@codeaurora.org } 11185425077dSsubashab@codeaurora.org } 11195425077dSsubashab@codeaurora.org 11200e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb) 1121db8dac20SDavid S. Miller { 1122ba6aac15SKuniyuki Iwashima return __udp6_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP); 1123db8dac20SDavid S. Miller } 1124db8dac20SDavid S. Miller 1125db8dac20SDavid S. Miller /* 1126db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 1127db8dac20SDavid S. Miller */ 1128db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 1129db8dac20SDavid S. Miller { 1130db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1131db8dac20SDavid S. Miller 113236d926b9SDenis V. Lunev if (up->pending == AF_INET) 113336d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 113436d926b9SDenis V. Lunev else if (up->pending) { 1135db8dac20SDavid S. Miller up->len = 0; 1136db8dac20SDavid S. Miller up->pending = 0; 1137db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 1138db8dac20SDavid S. Miller } 1139db8dac20SDavid S. Miller } 1140db8dac20SDavid S. Miller 1141d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr, 1142d74bad4eSAndrey Ignatov int addr_len) 1143d74bad4eSAndrey Ignatov { 1144bddc028aSTetsuo Handa if (addr_len < offsetofend(struct sockaddr, sa_family)) 1145bddc028aSTetsuo Handa return -EINVAL; 1146d74bad4eSAndrey Ignatov /* The following checks are replicated from __ip6_datagram_connect() 1147d74bad4eSAndrey Ignatov * and intended to prevent BPF program called below from accessing 1148d74bad4eSAndrey Ignatov * bytes that are out of the bound specified by user in addr_len. 1149d74bad4eSAndrey Ignatov */ 1150d74bad4eSAndrey Ignatov if (uaddr->sa_family == AF_INET) { 115189e9c728SKuniyuki Iwashima if (ipv6_only_sock(sk)) 1152d74bad4eSAndrey Ignatov return -EAFNOSUPPORT; 1153d74bad4eSAndrey Ignatov return udp_pre_connect(sk, uaddr, addr_len); 1154d74bad4eSAndrey Ignatov } 1155d74bad4eSAndrey Ignatov 1156d74bad4eSAndrey Ignatov if (addr_len < SIN6_LEN_RFC2133) 1157d74bad4eSAndrey Ignatov return -EINVAL; 1158d74bad4eSAndrey Ignatov 1159d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr); 1160d74bad4eSAndrey Ignatov } 1161d74bad4eSAndrey Ignatov 1162493c6be3SSridhar Samudrala /** 1163493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 1164493c6be3SSridhar Samudrala * @sk: socket we are sending on 1165493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 1166493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 1167b51cd7c8SAndrew Lunn * @saddr: source address 1168b51cd7c8SAndrew Lunn * @daddr: destination address 1169b51cd7c8SAndrew Lunn * @len: length of packet 1170493c6be3SSridhar Samudrala */ 1171493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 1172493c6be3SSridhar Samudrala const struct in6_addr *saddr, 1173493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 1174493c6be3SSridhar Samudrala { 1175493c6be3SSridhar Samudrala unsigned int offset; 1176493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 1177d39d938cSVlad Yasevich struct sk_buff *frags = skb_shinfo(skb)->frag_list; 1178493c6be3SSridhar Samudrala __wsum csum = 0; 1179493c6be3SSridhar Samudrala 1180d39d938cSVlad Yasevich if (!frags) { 1181493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 1182493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 1183493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 1184493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 1185493c6be3SSridhar Samudrala } else { 1186493c6be3SSridhar Samudrala /* 1187493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 1188493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 1189493c6be3SSridhar Samudrala * should be together 1190493c6be3SSridhar Samudrala */ 1191493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 1192493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 119363ecc3d9SSubash Abhinov Kasiviswanathan csum = skb->csum; 1194493c6be3SSridhar Samudrala 1195493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1196493c6be3SSridhar Samudrala 1197d39d938cSVlad Yasevich do { 1198d39d938cSVlad Yasevich csum = csum_add(csum, frags->csum); 1199d39d938cSVlad Yasevich } while ((frags = frags->next)); 1200493c6be3SSridhar Samudrala 1201493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1202493c6be3SSridhar Samudrala csum); 1203493c6be3SSridhar Samudrala if (uh->check == 0) 1204493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 1205493c6be3SSridhar Samudrala } 1206493c6be3SSridhar Samudrala } 1207493c6be3SSridhar Samudrala 1208db8dac20SDavid S. Miller /* 1209db8dac20SDavid S. Miller * Sending 1210db8dac20SDavid S. Miller */ 1211db8dac20SDavid S. Miller 1212bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6, 1213bec1f6f6SWillem de Bruijn struct inet_cork *cork) 1214db8dac20SDavid S. Miller { 1215d39d938cSVlad Yasevich struct sock *sk = skb->sk; 1216db8dac20SDavid S. Miller struct udphdr *uh; 1217db8dac20SDavid S. Miller int err = 0; 1218db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1219db8dac20SDavid S. Miller __wsum csum = 0; 1220d39d938cSVlad Yasevich int offset = skb_transport_offset(skb); 1221d39d938cSVlad Yasevich int len = skb->len - offset; 12224094871dSJosh Hunt int datalen = len - sizeof(*uh); 1223db8dac20SDavid S. Miller 1224db8dac20SDavid S. Miller /* 1225db8dac20SDavid S. Miller * Create a UDP header 1226db8dac20SDavid S. Miller */ 1227db8dac20SDavid S. Miller uh = udp_hdr(skb); 12281958b856SDavid S. Miller uh->source = fl6->fl6_sport; 12291958b856SDavid S. Miller uh->dest = fl6->fl6_dport; 1230d39d938cSVlad Yasevich uh->len = htons(len); 1231db8dac20SDavid S. Miller uh->check = 0; 1232db8dac20SDavid S. Miller 1233bec1f6f6SWillem de Bruijn if (cork->gso_size) { 1234bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 1235bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 1236bec1f6f6SWillem de Bruijn 12370f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 12380f149c9fSWillem de Bruijn kfree_skb(skb); 1239bec1f6f6SWillem de Bruijn return -EINVAL; 12400f149c9fSWillem de Bruijn } 1241736ef37fSCoco Li if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 12420f149c9fSWillem de Bruijn kfree_skb(skb); 1243bec1f6f6SWillem de Bruijn return -EINVAL; 12440f149c9fSWillem de Bruijn } 124539fc39a0SEric Dumazet if (udp_get_no_check6_tx(sk)) { 12460f149c9fSWillem de Bruijn kfree_skb(skb); 1247a8c744a8SWillem de Bruijn return -EINVAL; 12480f149c9fSWillem de Bruijn } 1249ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 12500f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 12510f149c9fSWillem de Bruijn kfree_skb(skb); 1252bec1f6f6SWillem de Bruijn return -EIO; 12530f149c9fSWillem de Bruijn } 1254bec1f6f6SWillem de Bruijn 12554094871dSJosh Hunt if (datalen > cork->gso_size) { 1256bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 1257bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 12584094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 125944b321e5SJosh Hunt cork->gso_size); 12604094871dSJosh Hunt } 1261a8c744a8SWillem de Bruijn goto csum_partial; 1262bec1f6f6SWillem de Bruijn } 1263bec1f6f6SWillem de Bruijn 1264db8dac20SDavid S. Miller if (is_udplite) 1265d39d938cSVlad Yasevich csum = udplite_csum(skb); 126639fc39a0SEric Dumazet else if (udp_get_no_check6_tx(sk)) { /* UDP csum disabled */ 12674068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE; 12684068579eSTom Herbert goto send; 12694068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1270a8c744a8SWillem de Bruijn csum_partial: 1271d39d938cSVlad Yasevich udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len); 1272493c6be3SSridhar Samudrala goto send; 1273493c6be3SSridhar Samudrala } else 1274d39d938cSVlad Yasevich csum = udp_csum(skb); 1275db8dac20SDavid S. Miller 1276db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 12774c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 1278d39d938cSVlad Yasevich len, fl6->flowi6_proto, csum); 1279db8dac20SDavid S. Miller if (uh->check == 0) 1280db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 1281db8dac20SDavid S. Miller 1282493c6be3SSridhar Samudrala send: 1283d39d938cSVlad Yasevich err = ip6_send_skb(skb); 12846ce9e7b5SEric Dumazet if (err) { 12856ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 12866aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12876ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 12886ce9e7b5SEric Dumazet err = 0; 12896ce9e7b5SEric Dumazet } 12906aef70a8SEric Dumazet } else { 12916aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12926ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 12936aef70a8SEric Dumazet } 1294d39d938cSVlad Yasevich return err; 1295d39d938cSVlad Yasevich } 1296d39d938cSVlad Yasevich 1297d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk) 1298d39d938cSVlad Yasevich { 1299d39d938cSVlad Yasevich struct sk_buff *skb; 1300d39d938cSVlad Yasevich struct udp_sock *up = udp_sk(sk); 1301d39d938cSVlad Yasevich int err = 0; 1302d39d938cSVlad Yasevich 1303d39d938cSVlad Yasevich if (up->pending == AF_INET) 1304d39d938cSVlad Yasevich return udp_push_pending_frames(sk); 1305d39d938cSVlad Yasevich 1306d39d938cSVlad Yasevich skb = ip6_finish_skb(sk); 1307d39d938cSVlad Yasevich if (!skb) 1308d39d938cSVlad Yasevich goto out; 1309d39d938cSVlad Yasevich 1310940ea00bSPavel Begunkov err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6, 1311940ea00bSPavel Begunkov &inet_sk(sk)->cork.base); 1312db8dac20SDavid S. Miller out: 1313db8dac20SDavid S. Miller up->len = 0; 1314db8dac20SDavid S. Miller up->pending = 0; 1315db8dac20SDavid S. Miller return err; 1316db8dac20SDavid S. Miller } 1317db8dac20SDavid S. Miller 13181b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1319db8dac20SDavid S. Miller { 1320db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 1321db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1322db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1323db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 1324342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 132520c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 1326db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 132745f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 1328db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 13295298953eSPavel Begunkov struct inet_cork_full cork; 13305298953eSPavel Begunkov struct flowi6 *fl6 = &cork.fl.u.ip6; 1331db8dac20SDavid S. Miller struct dst_entry *dst; 133226879da5SWei Wang struct ipcm6_cookie ipc6; 1333db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 13349f542f61SAlexey Kodanev bool connected = false; 1335db8dac20SDavid S. Miller int ulen = len; 13366965d1d8SEric Dumazet int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE; 1337db8dac20SDavid S. Miller int err; 1338db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1339db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1340db8dac20SDavid S. Miller 1341b515430aSWillem de Bruijn ipcm6_init(&ipc6); 134218a419baSEric Dumazet ipc6.gso_size = READ_ONCE(up->gso_size); 1343e3390b30SEric Dumazet ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); 13443c5b4d69SEric Dumazet ipc6.sockc.mark = READ_ONCE(sk->sk_mark); 134526879da5SWei Wang 1346db8dac20SDavid S. Miller /* destination address check */ 1347db8dac20SDavid S. Miller if (sin6) { 1348db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 1349db8dac20SDavid S. Miller return -EINVAL; 1350db8dac20SDavid S. Miller 1351db8dac20SDavid S. Miller switch (sin6->sin6_family) { 1352db8dac20SDavid S. Miller case AF_INET6: 1353db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 1354db8dac20SDavid S. Miller return -EINVAL; 1355db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1356052d2369SJonathan T. Leighton if (ipv6_addr_any(daddr) && 1357052d2369SJonathan T. Leighton ipv6_addr_v4mapped(&np->saddr)) 1358052d2369SJonathan T. Leighton ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 1359052d2369SJonathan T. Leighton daddr); 1360db8dac20SDavid S. Miller break; 1361db8dac20SDavid S. Miller case AF_INET: 1362db8dac20SDavid S. Miller goto do_udp_sendmsg; 1363db8dac20SDavid S. Miller case AF_UNSPEC: 1364db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 1365db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 1366db8dac20SDavid S. Miller daddr = NULL; 1367db8dac20SDavid S. Miller break; 1368db8dac20SDavid S. Miller default: 1369db8dac20SDavid S. Miller return -EINVAL; 1370db8dac20SDavid S. Miller } 1371db8dac20SDavid S. Miller } else if (!up->pending) { 1372db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1373db8dac20SDavid S. Miller return -EDESTADDRREQ; 1374efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1375db8dac20SDavid S. Miller } else 1376db8dac20SDavid S. Miller daddr = NULL; 1377db8dac20SDavid S. Miller 1378db8dac20SDavid S. Miller if (daddr) { 1379db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 1380db8dac20SDavid S. Miller struct sockaddr_in sin; 1381db8dac20SDavid S. Miller sin.sin_family = AF_INET; 1382c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1383db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1384db8dac20SDavid S. Miller msg->msg_name = &sin; 1385db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 1386db8dac20SDavid S. Miller do_udp_sendmsg: 13871c5950fcSEric Dumazet err = ipv6_only_sock(sk) ? 13881c5950fcSEric Dumazet -ENETUNREACH : udp_sendmsg(sk, msg, len); 13891c5950fcSEric Dumazet msg->msg_name = sin6; 13901c5950fcSEric Dumazet msg->msg_namelen = addr_len; 13911c5950fcSEric Dumazet return err; 1392db8dac20SDavid S. Miller } 1393db8dac20SDavid S. Miller } 1394db8dac20SDavid S. Miller 1395db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 1396db8dac20SDavid S. Miller better check is made in ip6_append_data(). 1397db8dac20SDavid S. Miller */ 1398db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 1399db8dac20SDavid S. Miller return -EMSGSIZE; 1400db8dac20SDavid S. Miller 140103485f2aSVlad Yasevich getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1402db8dac20SDavid S. Miller if (up->pending) { 1403406c4a0aSPavel Begunkov if (up->pending == AF_INET) 1404406c4a0aSPavel Begunkov return udp_sendmsg(sk, msg, len); 1405db8dac20SDavid S. Miller /* 1406db8dac20SDavid S. Miller * There are pending frames. 1407db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1408db8dac20SDavid S. Miller */ 1409db8dac20SDavid S. Miller lock_sock(sk); 1410db8dac20SDavid S. Miller if (likely(up->pending)) { 1411db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 1412db8dac20SDavid S. Miller release_sock(sk); 1413db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1414db8dac20SDavid S. Miller } 1415db8dac20SDavid S. Miller dst = NULL; 1416db8dac20SDavid S. Miller goto do_append_data; 1417db8dac20SDavid S. Miller } 1418db8dac20SDavid S. Miller release_sock(sk); 1419db8dac20SDavid S. Miller } 1420db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1421db8dac20SDavid S. Miller 14225298953eSPavel Begunkov memset(fl6, 0, sizeof(*fl6)); 1423db8dac20SDavid S. Miller 1424db8dac20SDavid S. Miller if (sin6) { 1425db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 1426db8dac20SDavid S. Miller return -EINVAL; 1427db8dac20SDavid S. Miller 14285298953eSPavel Begunkov fl6->fl6_dport = sin6->sin6_port; 1429db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1430db8dac20SDavid S. Miller 1431db8dac20SDavid S. Miller if (np->sndflow) { 14325298953eSPavel Begunkov fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 14335298953eSPavel Begunkov if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) { 14345298953eSPavel Begunkov flowlabel = fl6_sock_lookup(sk, fl6->flowlabel); 143559c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 1436db8dac20SDavid S. Miller return -EINVAL; 1437db8dac20SDavid S. Miller } 1438db8dac20SDavid S. Miller } 1439db8dac20SDavid S. Miller 1440db8dac20SDavid S. Miller /* 1441db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 1442db8dac20SDavid S. Miller * sk->sk_dst_cache. 1443db8dac20SDavid S. Miller */ 1444db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 1445efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1446efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1447db8dac20SDavid S. Miller 1448db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 1449db8dac20SDavid S. Miller sin6->sin6_scope_id && 1450842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 14515298953eSPavel Begunkov fl6->flowi6_oif = sin6->sin6_scope_id; 1452db8dac20SDavid S. Miller } else { 1453db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1454db8dac20SDavid S. Miller return -EDESTADDRREQ; 1455db8dac20SDavid S. Miller 14565298953eSPavel Begunkov fl6->fl6_dport = inet->inet_dport; 1457efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 14585298953eSPavel Begunkov fl6->flowlabel = np->flow_label; 14599f542f61SAlexey Kodanev connected = true; 1460db8dac20SDavid S. Miller } 1461db8dac20SDavid S. Miller 14625298953eSPavel Begunkov if (!fl6->flowi6_oif) 14634c971d2fSEric Dumazet fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if); 1464db8dac20SDavid S. Miller 14655298953eSPavel Begunkov if (!fl6->flowi6_oif) 14665298953eSPavel Begunkov fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 14679f690db7SYang Hongyang 14685298953eSPavel Begunkov fl6->flowi6_uid = sk->sk_uid; 146951953d5bSBrian Haley 1470db8dac20SDavid S. Miller if (msg->msg_controllen) { 1471db8dac20SDavid S. Miller opt = &opt_space; 1472db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 1473db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 147426879da5SWei Wang ipc6.opt = opt; 1475db8dac20SDavid S. Miller 14762e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc6.gso_size); 14772e8de857SWillem de Bruijn if (err > 0) 14785298953eSPavel Begunkov err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6, 14795fdaa88dSWillem de Bruijn &ipc6); 1480db8dac20SDavid S. Miller if (err < 0) { 1481db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1482db8dac20SDavid S. Miller return err; 1483db8dac20SDavid S. Miller } 14845298953eSPavel Begunkov if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 14855298953eSPavel Begunkov flowlabel = fl6_sock_lookup(sk, fl6->flowlabel); 148659c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 1487db8dac20SDavid S. Miller return -EINVAL; 1488db8dac20SDavid S. Miller } 1489db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 1490db8dac20SDavid S. Miller opt = NULL; 14919f542f61SAlexey Kodanev connected = false; 1492db8dac20SDavid S. Miller } 149345f6fad8SEric Dumazet if (!opt) { 149445f6fad8SEric Dumazet opt = txopt_get(np); 149545f6fad8SEric Dumazet opt_to_free = opt; 149645f6fad8SEric Dumazet } 1497db8dac20SDavid S. Miller if (flowlabel) 1498db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 1499db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 150026879da5SWei Wang ipc6.opt = opt; 1501db8dac20SDavid S. Miller 15025298953eSPavel Begunkov fl6->flowi6_proto = sk->sk_protocol; 15035298953eSPavel Begunkov fl6->flowi6_mark = ipc6.sockc.mark; 15045298953eSPavel Begunkov fl6->daddr = *daddr; 15055298953eSPavel Begunkov if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr)) 15065298953eSPavel Begunkov fl6->saddr = np->saddr; 15075298953eSPavel Begunkov fl6->fl6_sport = inet->inet_sport; 1508db8dac20SDavid S. Miller 15096fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) { 15101cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, 15115298953eSPavel Begunkov (struct sockaddr *)sin6, 15125298953eSPavel Begunkov &fl6->saddr); 15131cedee13SAndrey Ignatov if (err) 15141cedee13SAndrey Ignatov goto out_no_dst; 15151cedee13SAndrey Ignatov if (sin6) { 15161cedee13SAndrey Ignatov if (ipv6_addr_v4mapped(&sin6->sin6_addr)) { 15171cedee13SAndrey Ignatov /* BPF program rewrote IPv6-only by IPv4-mapped 15181cedee13SAndrey Ignatov * IPv6. It's currently unsupported. 15191cedee13SAndrey Ignatov */ 15201cedee13SAndrey Ignatov err = -ENOTSUPP; 15211cedee13SAndrey Ignatov goto out_no_dst; 15221cedee13SAndrey Ignatov } 15231cedee13SAndrey Ignatov if (sin6->sin6_port == 0) { 15241cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 15251cedee13SAndrey Ignatov err = -EINVAL; 15261cedee13SAndrey Ignatov goto out_no_dst; 15271cedee13SAndrey Ignatov } 15285298953eSPavel Begunkov fl6->fl6_dport = sin6->sin6_port; 15295298953eSPavel Begunkov fl6->daddr = sin6->sin6_addr; 15301cedee13SAndrey Ignatov } 15311cedee13SAndrey Ignatov } 15321cedee13SAndrey Ignatov 15335298953eSPavel Begunkov if (ipv6_addr_any(&fl6->daddr)) 15345298953eSPavel Begunkov fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 1535e8e36984SAndrey Ignatov 15365298953eSPavel Begunkov final_p = fl6_update_dst(fl6, opt, &final); 153720c59de2SArnaud Ebalard if (final_p) 15389f542f61SAlexey Kodanev connected = false; 1539db8dac20SDavid S. Miller 15405298953eSPavel Begunkov if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) { 15415298953eSPavel Begunkov fl6->flowi6_oif = np->mcast_oif; 15429f542f61SAlexey Kodanev connected = false; 15435298953eSPavel Begunkov } else if (!fl6->flowi6_oif) 15445298953eSPavel Begunkov fl6->flowi6_oif = np->ucast_oif; 1545db8dac20SDavid S. Miller 15465298953eSPavel Begunkov security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6)); 1547db8dac20SDavid S. Miller 154838b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 154938b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 155038b7097bSHannes Frederic Sowa 15515298953eSPavel Begunkov fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel); 155238b7097bSHannes Frederic Sowa 15535298953eSPavel Begunkov dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected); 155468d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 155568d0c6d3SDavid S. Miller err = PTR_ERR(dst); 155668d0c6d3SDavid S. Miller dst = NULL; 1557db8dac20SDavid S. Miller goto out; 1558db8dac20SDavid S. Miller } 1559db8dac20SDavid S. Miller 156026879da5SWei Wang if (ipc6.hlimit < 0) 15615298953eSPavel Begunkov ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst); 1562db8dac20SDavid S. Miller 1563db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1564db8dac20SDavid S. Miller goto do_confirm; 1565db8dac20SDavid S. Miller back_from_confirm: 1566db8dac20SDavid S. Miller 156703485f2aSVlad Yasevich /* Lockless fast path for the non-corking case */ 156803485f2aSVlad Yasevich if (!corkreq) { 156903485f2aSVlad Yasevich struct sk_buff *skb; 157003485f2aSVlad Yasevich 157103485f2aSVlad Yasevich skb = ip6_make_skb(sk, getfrag, msg, ulen, 157226879da5SWei Wang sizeof(struct udphdr), &ipc6, 1573f37a4cc6SPavel Begunkov (struct rt6_info *)dst, 15745fdaa88dSWillem de Bruijn msg->msg_flags, &cork); 157503485f2aSVlad Yasevich err = PTR_ERR(skb); 157603485f2aSVlad Yasevich if (!IS_ERR_OR_NULL(skb)) 15775298953eSPavel Begunkov err = udp_v6_send_skb(skb, fl6, &cork.base); 157840ac240cSPavel Begunkov /* ip6_make_skb steals dst reference */ 157940ac240cSPavel Begunkov goto out_no_dst; 158003485f2aSVlad Yasevich } 158103485f2aSVlad Yasevich 1582db8dac20SDavid S. Miller lock_sock(sk); 1583db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1584db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1585db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1586db8dac20SDavid S. Miller release_sock(sk); 1587db8dac20SDavid S. Miller 1588ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n"); 1589db8dac20SDavid S. Miller err = -EINVAL; 1590db8dac20SDavid S. Miller goto out; 1591db8dac20SDavid S. Miller } 1592db8dac20SDavid S. Miller 1593db8dac20SDavid S. Miller up->pending = AF_INET6; 1594db8dac20SDavid S. Miller 1595db8dac20SDavid S. Miller do_append_data: 159626879da5SWei Wang if (ipc6.dontfrag < 0) 159726879da5SWei Wang ipc6.dontfrag = np->dontfrag; 1598db8dac20SDavid S. Miller up->len += ulen; 159926879da5SWei Wang err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 16005298953eSPavel Begunkov &ipc6, fl6, (struct rt6_info *)dst, 16015fdaa88dSWillem de Bruijn corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1602db8dac20SDavid S. Miller if (err) 1603db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1604db8dac20SDavid S. Miller else if (!corkreq) 1605db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 1606db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1607db8dac20SDavid S. Miller up->pending = 0; 1608db8dac20SDavid S. Miller 160903485f2aSVlad Yasevich if (err > 0) 161003485f2aSVlad Yasevich err = np->recverr ? net_xmit_errno(err) : 0; 161103485f2aSVlad Yasevich release_sock(sk); 161203485f2aSVlad Yasevich 1613db8dac20SDavid S. Miller out: 1614a3c96089SYOSHIFUJI Hideaki dst_release(dst); 16151cedee13SAndrey Ignatov out_no_dst: 1616db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 161745f6fad8SEric Dumazet txopt_put(opt_to_free); 1618db8dac20SDavid S. Miller if (!err) 1619db8dac20SDavid S. Miller return len; 1620db8dac20SDavid S. Miller /* 1621db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1622db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1623db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1624db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1625db8dac20SDavid S. Miller * seems like overkill. 1626db8dac20SDavid S. Miller */ 1627db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 16286aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 1629235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1630db8dac20SDavid S. Miller } 1631db8dac20SDavid S. Miller return err; 1632db8dac20SDavid S. Miller 1633db8dac20SDavid S. Miller do_confirm: 16340dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 16355298953eSPavel Begunkov dst_confirm_neigh(dst, &fl6->daddr); 1636db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1637db8dac20SDavid S. Miller goto back_from_confirm; 1638db8dac20SDavid S. Miller err = 0; 1639db8dac20SDavid S. Miller goto out; 1640db8dac20SDavid S. Miller } 1641ed472b0cSDavid Howells EXPORT_SYMBOL(udpv6_sendmsg); 1642db8dac20SDavid S. Miller 16431d7e4538SDavid Howells static void udpv6_splice_eof(struct socket *sock) 16441d7e4538SDavid Howells { 16451d7e4538SDavid Howells struct sock *sk = sock->sk; 16461d7e4538SDavid Howells struct udp_sock *up = udp_sk(sk); 16471d7e4538SDavid Howells 16486965d1d8SEric Dumazet if (!up->pending || udp_test_bit(CORK, sk)) 16491d7e4538SDavid Howells return; 16501d7e4538SDavid Howells 16511d7e4538SDavid Howells lock_sock(sk); 16526965d1d8SEric Dumazet if (up->pending && !udp_test_bit(CORK, sk)) 16531d7e4538SDavid Howells udp_v6_push_pending_frames(sk); 16541d7e4538SDavid Howells release_sock(sk); 16551d7e4538SDavid Howells } 16561d7e4538SDavid Howells 16577d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1658db8dac20SDavid S. Miller { 165944046a59STom Parkin struct udp_sock *up = udp_sk(sk); 1660db8dac20SDavid S. Miller lock_sock(sk); 1661a8b897c7SPaolo Abeni 1662a8b897c7SPaolo Abeni /* protects from races with udp_abort() */ 1663a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 1664db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1665db8dac20SDavid S. Miller release_sock(sk); 1666db8dac20SDavid S. Miller 166760fb9567SPaolo Abeni if (static_branch_unlikely(&udpv6_encap_needed_key)) { 166860fb9567SPaolo Abeni if (up->encap_type) { 166944046a59STom Parkin void (*encap_destroy)(struct sock *sk); 16706aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 167144046a59STom Parkin if (encap_destroy) 167244046a59STom Parkin encap_destroy(sk); 167344046a59STom Parkin } 1674f1f1f5ccSEric Dumazet if (udp_test_bit(ENCAP_ENABLED, sk)) { 16759c480601SPaolo Abeni static_branch_dec(&udpv6_encap_needed_key); 1676a4a600ddSXin Long udp_encap_disable(); 1677a4a600ddSXin Long } 167860fb9567SPaolo Abeni } 1679db8dac20SDavid S. Miller } 1680db8dac20SDavid S. Miller 1681db8dac20SDavid S. Miller /* 1682db8dac20SDavid S. Miller * Socket option code for UDP 1683db8dac20SDavid S. Miller */ 1684a7b75c5aSChristoph Hellwig int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1685a7b75c5aSChristoph Hellwig unsigned int optlen) 1686db8dac20SDavid S. Miller { 16878a3854c7SPaolo Abeni if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET) 168891ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 1689a7b75c5aSChristoph Hellwig optval, optlen, 1690db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1691db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1692db8dac20SDavid S. Miller } 1693db8dac20SDavid S. Miller 1694db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1695db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1696db8dac20SDavid S. Miller { 1697db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1698db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1699db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1700db8dac20SDavid S. Miller } 1701db8dac20SDavid S. Miller 170211052589SKuniyuki Iwashima static const struct inet6_protocol udpv6_protocol = { 1703db8dac20SDavid S. Miller .handler = udpv6_rcv, 1704db8dac20SDavid S. Miller .err_handler = udpv6_err, 1705db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1706db8dac20SDavid S. Miller }; 1707db8dac20SDavid S. Miller 1708db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1709db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1710db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1711db8dac20SDavid S. Miller { 171217ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 171317ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 171417ef66afSLorenzo Colitti } else { 171517ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket; 1716abc17a11SEric Dumazet const struct inet_sock *inet = inet_sk((const struct sock *)v); 171717ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport); 171817ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport); 17196c206b20SPaolo Abeni __ip6_dgram_sock_seq_show(seq, v, srcp, destp, 17206c206b20SPaolo Abeni udp_rqueue_get(v), bucket); 172117ef66afSLorenzo Colitti } 1722db8dac20SDavid S. Miller return 0; 1723db8dac20SDavid S. Miller } 1724db8dac20SDavid S. Miller 1725c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = { 1726a3d2599bSChristoph Hellwig .start = udp_seq_start, 1727a3d2599bSChristoph Hellwig .next = udp_seq_next, 1728a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 1729a3d2599bSChristoph Hellwig .show = udp6_seq_show, 1730a3d2599bSChristoph Hellwig }; 1731c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops); 173273cb88ecSArjan van de Ven 1733db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1734db8dac20SDavid S. Miller .family = AF_INET6, 1735478aee5dSKuniyuki Iwashima .udp_table = NULL, 1736db8dac20SDavid S. Miller }; 1737db8dac20SDavid S. Miller 17382c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net) 1739db8dac20SDavid S. Miller { 1740c3506372SChristoph Hellwig if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops, 1741c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp6_seq_afinfo)) 1742a3d2599bSChristoph Hellwig return -ENOMEM; 1743a3d2599bSChristoph Hellwig return 0; 1744db8dac20SDavid S. Miller } 1745db8dac20SDavid S. Miller 1746ec120da6SIan Morris void udp6_proc_exit(struct net *net) 1747ec120da6SIan Morris { 1748a3d2599bSChristoph Hellwig remove_proc_entry("udp6", net->proc_net); 1749db8dac20SDavid S. Miller } 1750db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1751db8dac20SDavid S. Miller 1752db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1753db8dac20SDavid S. Miller 1754db8dac20SDavid S. Miller struct proto udpv6_prot = { 1755db8dac20SDavid S. Miller .name = "UDPv6", 1756db8dac20SDavid S. Miller .owner = THIS_MODULE, 1757db8dac20SDavid S. Miller .close = udp_lib_close, 1758d74bad4eSAndrey Ignatov .pre_connect = udpv6_pre_connect, 1759db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1760db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1761db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1762d38afeecSKuniyuki Iwashima .init = udpv6_init_sock, 1763db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1764db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1765db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1766db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1767db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 17681d7e4538SDavid Howells .splice_eof = udpv6_splice_eof, 1769e646b657SMartin KaFai Lau .release_cb = ip6_datagram_release_cb, 1770db8dac20SDavid S. Miller .hash = udp_lib_hash, 1771db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1772719f8358SEric Dumazet .rehash = udp_v6_rehash, 1773db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 177491a760b2SMenglong Dong .put_port = udp_lib_unhash, 17758a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL 17768a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto, 17778a59f9d1SCong Wang #endif 17780defbb0aSEric Dumazet 1779db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 17800defbb0aSEric Dumazet .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc, 17810defbb0aSEric Dumazet 1782db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 17831e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 17841e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 1785db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1786f5f80e32SEric Dumazet .ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6), 178767fb4330SKuniyuki Iwashima .h.udp_table = NULL, 17885d77dca8SDavid Ahern .diag_destroy = udp_abort, 1789db8dac20SDavid S. Miller }; 1790db8dac20SDavid S. Miller 1791db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1792db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1793db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1794db8dac20SDavid S. Miller .prot = &udpv6_prot, 1795db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1796db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1797db8dac20SDavid S. Miller }; 1798db8dac20SDavid S. Miller 1799db8dac20SDavid S. Miller int __init udpv6_init(void) 1800db8dac20SDavid S. Miller { 1801db8dac20SDavid S. Miller int ret; 1802db8dac20SDavid S. Miller 18033336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 18043336288aSVlad Yasevich if (ret) 1805c6b641a4SVlad Yasevich goto out; 18063336288aSVlad Yasevich 1807db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1808db8dac20SDavid S. Miller if (ret) 1809db8dac20SDavid S. Miller goto out_udpv6_protocol; 1810db8dac20SDavid S. Miller out: 1811db8dac20SDavid S. Miller return ret; 1812db8dac20SDavid S. Miller 1813db8dac20SDavid S. Miller out_udpv6_protocol: 1814db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1815db8dac20SDavid S. Miller goto out; 1816db8dac20SDavid S. Miller } 1817db8dac20SDavid S. Miller 1818db8dac20SDavid S. Miller void udpv6_exit(void) 1819db8dac20SDavid S. Miller { 1820db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1821db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1822db8dac20SDavid S. Miller } 1823