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> 48db8dac20SDavid S. Miller #include <net/xfrm.h> 490bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h> 5072289b96STom Herbert #include <net/inet6_hashtables.h> 51076bb0c8SEliezer Tamir #include <net/busy_poll.h> 52e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 53db8dac20SDavid S. Miller 54db8dac20SDavid S. Miller #include <linux/proc_fs.h> 55db8dac20SDavid S. Miller #include <linux/seq_file.h> 5622911fc5SEric Dumazet #include <trace/events/skb.h> 57db8dac20SDavid S. Miller #include "udp_impl.h" 58db8dac20SDavid S. Miller 59d38afeecSKuniyuki Iwashima static void udpv6_destruct_sock(struct sock *sk) 60d38afeecSKuniyuki Iwashima { 61d38afeecSKuniyuki Iwashima udp_destruct_common(sk); 62d38afeecSKuniyuki Iwashima inet6_sock_destruct(sk); 63d38afeecSKuniyuki Iwashima } 64d38afeecSKuniyuki Iwashima 65d38afeecSKuniyuki Iwashima int udpv6_init_sock(struct sock *sk) 66d38afeecSKuniyuki Iwashima { 678a3854c7SPaolo Abeni udp_lib_init_sock(sk); 68d38afeecSKuniyuki Iwashima sk->sk_destruct = udpv6_destruct_sock; 698f279fb0SPavel Begunkov set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); 70d38afeecSKuniyuki Iwashima return 0; 71d38afeecSKuniyuki Iwashima } 72d38afeecSKuniyuki Iwashima 736eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net, 74b50026b5SHannes Frederic Sowa const struct in6_addr *laddr, 75b50026b5SHannes Frederic Sowa const u16 lport, 76b50026b5SHannes Frederic Sowa const struct in6_addr *faddr, 77b50026b5SHannes Frederic Sowa const __be16 fport) 78b50026b5SHannes Frederic Sowa { 791bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly; 801bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly; 811bbdceefSHannes Frederic Sowa 821bbdceefSHannes Frederic Sowa u32 lhash, fhash; 831bbdceefSHannes Frederic Sowa 841bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret, 851bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret)); 861bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret, 871bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret)); 881bbdceefSHannes Frederic Sowa 891bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3]; 901bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 911bbdceefSHannes Frederic Sowa 92b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport, 931bbdceefSHannes Frederic Sowa udp_ipv6_hash_secret + net_hash_mix(net)); 94b50026b5SHannes Frederic Sowa } 95b50026b5SHannes Frederic Sowa 966ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 97db8dac20SDavid S. Miller { 9830fff923SEric Dumazet unsigned int hash2_nulladdr = 99f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 10030fff923SEric Dumazet unsigned int hash2_partial = 101f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 10230fff923SEric Dumazet 103d4cada4aSEric Dumazet /* precompute partial secondary hash */ 10430fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 105fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 106db8dac20SDavid S. Miller } 107db8dac20SDavid S. Miller 108f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk) 109719f8358SEric Dumazet { 110f0b1e64cSMartin KaFai Lau u16 new_hash = ipv6_portaddr_hash(sock_net(sk), 111efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr, 112719f8358SEric Dumazet inet_sk(sk)->inet_num); 113719f8358SEric Dumazet 114719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 115719f8358SEric Dumazet } 116719f8358SEric Dumazet 117d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 11888440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 119d1e37288SSu, Xuemin const struct in6_addr *daddr, unsigned short hnum, 12073545373STim Beale int dif, int sdif) 121db8dac20SDavid S. Miller { 1224c971d2fSEric Dumazet int bound_dev_if, score; 12360c04aecSJoe Perches struct inet_sock *inet; 1246da5b0f0SMike Manning bool dev_match; 125db8dac20SDavid S. Miller 12660c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 12760c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 12860c04aecSJoe Perches sk->sk_family != PF_INET6) 12960c04aecSJoe Perches return -1; 130645ca708SEric Dumazet 13123b0269eSPeter Oskolkov if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 13223b0269eSPeter Oskolkov return -1; 13323b0269eSPeter Oskolkov 134645ca708SEric Dumazet score = 0; 13560c04aecSJoe Perches inet = inet_sk(sk); 13660c04aecSJoe Perches 137c720c7e8SEric Dumazet if (inet->inet_dport) { 138c720c7e8SEric Dumazet if (inet->inet_dport != sport) 139645ca708SEric Dumazet return -1; 140db8dac20SDavid S. Miller score++; 141db8dac20SDavid S. Miller } 14260c04aecSJoe Perches 143efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 144efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 145645ca708SEric Dumazet return -1; 146db8dac20SDavid S. Miller score++; 147db8dac20SDavid S. Miller } 14860c04aecSJoe Perches 1494c971d2fSEric Dumazet bound_dev_if = READ_ONCE(sk->sk_bound_dev_if); 1504c971d2fSEric Dumazet dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif); 15169678bcdSPaolo Abeni if (!dev_match) 152645ca708SEric Dumazet return -1; 1534c971d2fSEric Dumazet if (bound_dev_if) 154db8dac20SDavid S. Miller score++; 15560c04aecSJoe Perches 1567170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 15770da268bSEric Dumazet score++; 15870da268bSEric Dumazet 159645ca708SEric Dumazet return score; 160645ca708SEric Dumazet } 161645ca708SEric Dumazet 162a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk, 1632a08748cSJakub Sitnicki struct sk_buff *skb, 1642a08748cSJakub Sitnicki const struct in6_addr *saddr, 1652a08748cSJakub Sitnicki __be16 sport, 1662a08748cSJakub Sitnicki const struct in6_addr *daddr, 1672a08748cSJakub Sitnicki unsigned int hnum) 1682a08748cSJakub Sitnicki { 1692a08748cSJakub Sitnicki struct sock *reuse_sk = NULL; 1702a08748cSJakub Sitnicki u32 hash; 1712a08748cSJakub Sitnicki 1722a08748cSJakub Sitnicki if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 1732a08748cSJakub Sitnicki hash = udp6_ehashfn(net, daddr, hnum, saddr, sport); 1742a08748cSJakub Sitnicki reuse_sk = reuseport_select_sock(sk, hash, skb, 1752a08748cSJakub Sitnicki sizeof(struct udphdr)); 1762a08748cSJakub Sitnicki } 1772a08748cSJakub Sitnicki return reuse_sk; 1782a08748cSJakub Sitnicki } 1792a08748cSJakub Sitnicki 180d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 181fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net, 182fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 1831801b570SDavid Ahern const struct in6_addr *daddr, unsigned int hnum, 18473545373STim Beale int dif, int sdif, struct udp_hslot *hslot2, 18573545373STim Beale struct sk_buff *skb) 186fddc17deSEric Dumazet { 187fddc17deSEric Dumazet struct sock *sk, *result; 188e94a62f5SPaolo Abeni int score, badness; 189fddc17deSEric Dumazet 190fddc17deSEric Dumazet result = NULL; 191fddc17deSEric Dumazet badness = -1; 192ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 193d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 19473545373STim Beale daddr, hnum, dif, sdif); 195fddc17deSEric Dumazet if (score > badness) { 1962a08748cSJakub Sitnicki result = lookup_reuseport(net, sk, skb, 1972a08748cSJakub Sitnicki saddr, sport, daddr, hnum); 198a57066b1SDavid S. Miller /* Fall back to scoring if group has connections */ 19969421bf9SKuniyuki Iwashima if (result && !reuseport_has_conns(sk)) 200ca065d0cSEric Dumazet return result; 2012a08748cSJakub Sitnicki 202a57066b1SDavid S. Miller result = result ? : sk; 203ca065d0cSEric Dumazet badness = score; 204fddc17deSEric Dumazet } 205fddc17deSEric Dumazet } 206fddc17deSEric Dumazet return result; 207fddc17deSEric Dumazet } 208fddc17deSEric Dumazet 2096d4201b1SJakub Sitnicki static inline struct sock *udp6_lookup_run_bpf(struct net *net, 2106d4201b1SJakub Sitnicki struct udp_table *udptable, 2116d4201b1SJakub Sitnicki struct sk_buff *skb, 2126d4201b1SJakub Sitnicki const struct in6_addr *saddr, 2136d4201b1SJakub Sitnicki __be16 sport, 2146d4201b1SJakub Sitnicki const struct in6_addr *daddr, 215f8931565SMark Pashmfouroush u16 hnum, const int dif) 2166d4201b1SJakub Sitnicki { 2176d4201b1SJakub Sitnicki struct sock *sk, *reuse_sk; 2186d4201b1SJakub Sitnicki bool no_reuseport; 2196d4201b1SJakub Sitnicki 2206d4201b1SJakub Sitnicki if (udptable != &udp_table) 2216d4201b1SJakub Sitnicki return NULL; /* only UDP is supported */ 2226d4201b1SJakub Sitnicki 223f8931565SMark Pashmfouroush no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP, saddr, sport, 224f8931565SMark Pashmfouroush daddr, hnum, dif, &sk); 2256d4201b1SJakub Sitnicki if (no_reuseport || IS_ERR_OR_NULL(sk)) 2266d4201b1SJakub Sitnicki return sk; 2276d4201b1SJakub Sitnicki 2286d4201b1SJakub Sitnicki reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); 229c64c9c28SJakub Sitnicki if (reuse_sk) 2306d4201b1SJakub Sitnicki sk = reuse_sk; 2316d4201b1SJakub Sitnicki return sk; 2326d4201b1SJakub Sitnicki } 2336d4201b1SJakub Sitnicki 234ca065d0cSEric Dumazet /* rcu_read_lock() must be held */ 235fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net, 23688440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 23788440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 2381801b570SDavid Ahern int dif, int sdif, struct udp_table *udptable, 239538950a1SCraig Gallek struct sk_buff *skb) 240645ca708SEric Dumazet { 241645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 24223b0269eSPeter Oskolkov unsigned int hash2, slot2; 24323b0269eSPeter Oskolkov struct udp_hslot *hslot2; 2446d4201b1SJakub Sitnicki struct sock *result, *sk; 245645ca708SEric Dumazet 246f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum); 247fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 248fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 249fddc17deSEric Dumazet 2506d4201b1SJakub Sitnicki /* Lookup connected or non-wildcard sockets */ 251fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport, 25273545373STim Beale daddr, hnum, dif, sdif, 253d1e37288SSu, Xuemin hslot2, skb); 2546d4201b1SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 2556d4201b1SJakub Sitnicki goto done; 2566d4201b1SJakub Sitnicki 2576d4201b1SJakub Sitnicki /* Lookup redirect from BPF */ 2586d4201b1SJakub Sitnicki if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 2596d4201b1SJakub Sitnicki sk = udp6_lookup_run_bpf(net, udptable, skb, 260f8931565SMark Pashmfouroush saddr, sport, daddr, hnum, dif); 2616d4201b1SJakub Sitnicki if (sk) { 2626d4201b1SJakub Sitnicki result = sk; 2636d4201b1SJakub Sitnicki goto done; 2646d4201b1SJakub Sitnicki } 2656d4201b1SJakub Sitnicki } 2666d4201b1SJakub Sitnicki 2676d4201b1SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 2686d4201b1SJakub Sitnicki if (result) 2696d4201b1SJakub Sitnicki goto done; 2706d4201b1SJakub Sitnicki 2716d4201b1SJakub Sitnicki /* Lookup wildcard sockets */ 272f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum); 273fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 274fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 275fddc17deSEric Dumazet 2761223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport, 27723b0269eSPeter Oskolkov &in6addr_any, hnum, dif, sdif, 27873545373STim Beale hslot2, skb); 2796d4201b1SJakub Sitnicki done: 28026f8113cSEnrico Weigelt if (IS_ERR(result)) 2818217ca65SMartin KaFai Lau return NULL; 282fddc17deSEric Dumazet return result; 283fddc17deSEric Dumazet } 284fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 285db8dac20SDavid S. Miller 286607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 287607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 288645ca708SEric Dumazet struct udp_table *udptable) 289607c4aafSKOVACS Krisztian { 290b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 291607c4aafSKOVACS Krisztian 292ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 293607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 2941801b570SDavid Ahern inet6_sdif(skb), udptable, skb); 295607c4aafSKOVACS Krisztian } 296607c4aafSKOVACS Krisztian 2977b58e63eSEric Dumazet struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb, 29863058308STom Herbert __be16 sport, __be16 dport) 29963058308STom Herbert { 30063058308STom Herbert const struct ipv6hdr *iph = ipv6_hdr(skb); 30163058308STom Herbert 302ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 30363058308STom Herbert &iph->daddr, dport, inet6_iif(skb), 304257a525fSMartin KaFai Lau inet6_sdif(skb), &udp_table, NULL); 30563058308STom Herbert } 30663058308STom Herbert 307ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 308ca065d0cSEric Dumazet * Does increment socket refcount. 309ca065d0cSEric Dumazet */ 3106e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6) 311aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 312aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif) 313aa976fc0SBalazs Scheidler { 314ca065d0cSEric Dumazet struct sock *sk; 315ca065d0cSEric Dumazet 316ca065d0cSEric Dumazet sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, 3171801b570SDavid Ahern dif, 0, &udp_table, NULL); 31841c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 319ca065d0cSEric Dumazet sk = NULL; 320ca065d0cSEric Dumazet return sk; 321aa976fc0SBalazs Scheidler } 322aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup); 323ca065d0cSEric Dumazet #endif 324aa976fc0SBalazs Scheidler 325cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the 326cb891fa6SPaolo Abeni * scratch area space; note that the IP6CB flags is still in the first 327cb891fa6SPaolo Abeni * cacheline, so checking for jumbograms is cheap 328cb891fa6SPaolo Abeni */ 329cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb) 330cb891fa6SPaolo Abeni { 331cb891fa6SPaolo Abeni return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb); 332cb891fa6SPaolo Abeni } 333cb891fa6SPaolo Abeni 334db8dac20SDavid S. Miller /* 335db8dac20SDavid S. Miller * This should be easy, if there is something there we 336db8dac20SDavid S. Miller * return it, otherwise we block. 337db8dac20SDavid S. Miller */ 338db8dac20SDavid S. Miller 3391b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 340ec095263SOliver Hartkopp int flags, int *addr_len) 341db8dac20SDavid S. Miller { 342db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 343db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 344db8dac20SDavid S. Miller struct sk_buff *skb; 34559c2cdaeSDavid S. Miller unsigned int ulen, copied; 346fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 347db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 348543fc3fbSPaolo Abeni struct udp_mib __percpu *mib; 349197c949eSEric Dumazet bool checksum_valid = false; 350f26ba175SWei Yongjun int is_udp4; 351db8dac20SDavid S. Miller 352db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 35385fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 354db8dac20SDavid S. Miller 3554b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 35685fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 3574b340ae2SBrian Haley 358db8dac20SDavid S. Miller try_again: 359a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 360ec095263SOliver Hartkopp skb = __skb_recv_udp(sk, flags, &off, &err); 361db8dac20SDavid S. Miller if (!skb) 362627d2d6bSsamanthakumar return err; 363db8dac20SDavid S. Miller 364cb891fa6SPaolo Abeni ulen = udp6_skb_len(skb); 36559c2cdaeSDavid S. Miller copied = len; 366627d2d6bSsamanthakumar if (copied > ulen - off) 367627d2d6bSsamanthakumar copied = ulen - off; 36859c2cdaeSDavid S. Miller else if (copied < ulen) 369db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 370db8dac20SDavid S. Miller 371f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 372029a3743SPaolo Abeni mib = __UDPX_MIB(sk, is_udp4); 373f26ba175SWei Yongjun 374db8dac20SDavid S. Miller /* 375db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 376db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 377db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 378db8dac20SDavid S. Miller */ 379db8dac20SDavid S. Miller 380d21dbdfeSEric Dumazet if (copied < ulen || peeking || 381d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 38267a51780SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 38367a51780SPaolo Abeni !__udp_lib_checksum_complete(skb); 384197c949eSEric Dumazet if (!checksum_valid) 385db8dac20SDavid S. Miller goto csum_copy_err; 386db8dac20SDavid S. Miller } 387db8dac20SDavid S. Miller 38867a51780SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 38967a51780SPaolo Abeni if (udp_skb_is_linear(skb)) 39067a51780SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 39167a51780SPaolo Abeni else 392627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 39367a51780SPaolo Abeni } else { 394627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 395db8dac20SDavid S. Miller if (err == -EINVAL) 396db8dac20SDavid S. Miller goto csum_copy_err; 397db8dac20SDavid S. Miller } 39822911fc5SEric Dumazet if (unlikely(err)) { 399fd69c399SPaolo Abeni if (!peeking) { 400979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 401029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 402979402b1SEric Dumazet } 403850cbaddSPaolo Abeni kfree_skb(skb); 404627d2d6bSsamanthakumar return err; 40522911fc5SEric Dumazet } 406fd69c399SPaolo Abeni if (!peeking) 407029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS); 408db8dac20SDavid S. Miller 4096fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb); 410db8dac20SDavid S. Miller 411db8dac20SDavid S. Miller /* Copy the address. */ 412db8dac20SDavid S. Miller if (msg->msg_name) { 413342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 414db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 415db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 416db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 417db8dac20SDavid S. Miller 418842df073SHannes Frederic Sowa if (is_udp4) { 419b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 420b301e82cSBrian Haley &sin6->sin6_addr); 421842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0; 422842df073SHannes Frederic Sowa } else { 4234e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 424842df073SHannes Frederic Sowa sin6->sin6_scope_id = 425842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr, 4264330487aSDuan Jiong inet6_iif(skb)); 427db8dac20SDavid S. Miller } 428bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 429983695faSDaniel Borkmann 430983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, 431983695faSDaniel Borkmann (struct sockaddr *)sin6); 432db8dac20SDavid S. Miller } 4334b261c75SHannes Frederic Sowa 434bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 435bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 436bcd1665eSPaolo Abeni 4374b261c75SHannes Frederic Sowa if (np->rxopt.all) 4384b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb); 4394b261c75SHannes Frederic Sowa 440f26ba175SWei Yongjun if (is_udp4) { 441db8dac20SDavid S. Miller if (inet->cmsg_flags) 442ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, 44310df8e61SEric Dumazet sizeof(struct udphdr), off); 444db8dac20SDavid S. Miller } else { 445db8dac20SDavid S. Miller if (np->rxopt.all) 4464b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb); 447db8dac20SDavid S. Miller } 448db8dac20SDavid S. Miller 44959c2cdaeSDavid S. Miller err = copied; 450db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 451db8dac20SDavid S. Miller err = ulen; 452db8dac20SDavid S. Miller 453850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 454db8dac20SDavid S. Miller return err; 455db8dac20SDavid S. Miller 456db8dac20SDavid S. Miller csum_copy_err: 4572276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 4582276f58aSPaolo Abeni udp_skb_destructor)) { 459029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS); 460029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 4616a5dc9e5SEric Dumazet } 462850cbaddSPaolo Abeni kfree_skb(skb); 463db8dac20SDavid S. Miller 464beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 465beb39db5SEric Dumazet cond_resched(); 4669cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 467db8dac20SDavid S. Miller goto try_again; 468db8dac20SDavid S. Miller } 469db8dac20SDavid S. Miller 470a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 471a36e185eSStefano Brivio void udpv6_encap_enable(void) 472a36e185eSStefano Brivio { 4739c480601SPaolo Abeni static_branch_inc(&udpv6_encap_needed_key); 474a36e185eSStefano Brivio } 475a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable); 476a36e185eSStefano Brivio 477e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 478e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 479e7cc0824SStefano Brivio */ 480e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb, 481e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 482424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info) 483e7cc0824SStefano Brivio { 484e7cc0824SStefano Brivio int i; 485e7cc0824SStefano Brivio 486e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 487e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 488424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info); 489424a7cd0SPaolo Abeni const struct ip6_tnl_encap_ops *encap; 490e7cc0824SStefano Brivio 491424a7cd0SPaolo Abeni encap = rcu_dereference(ip6tun_encaps[i]); 492424a7cd0SPaolo Abeni if (!encap) 493e7cc0824SStefano Brivio continue; 494424a7cd0SPaolo Abeni handler = encap->err_handler; 495e7cc0824SStefano Brivio if (handler && !handler(skb, opt, type, code, offset, info)) 496e7cc0824SStefano Brivio return 0; 497e7cc0824SStefano Brivio } 498e7cc0824SStefano Brivio 499e7cc0824SStefano Brivio return -ENOENT; 500e7cc0824SStefano Brivio } 501e7cc0824SStefano Brivio 502a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 503a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 504a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 505a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 506a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 507a36e185eSStefano Brivio * trace ICMP messages back to them. 508a36e185eSStefano Brivio * 509e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 510e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 511e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 512e7cc0824SStefano Brivio * 513a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 514a36e185eSStefano Brivio * association. 515a36e185eSStefano Brivio * 516e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 517e7cc0824SStefano Brivio * processing, zero otherwise. 518a36e185eSStefano Brivio */ 519a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net, 520a36e185eSStefano Brivio const struct ipv6hdr *hdr, int offset, 521a36e185eSStefano Brivio struct udphdr *uh, 522a36e185eSStefano Brivio struct udp_table *udptable, 5239bfce73cSVadim Fedorenko struct sock *sk, 524e7cc0824SStefano Brivio struct sk_buff *skb, 525e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 526e7cc0824SStefano Brivio u8 type, u8 code, __be32 info) 527a36e185eSStefano Brivio { 5289bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb); 529a36e185eSStefano Brivio int network_offset, transport_offset; 5309bfce73cSVadim Fedorenko struct udp_sock *up; 531a36e185eSStefano Brivio 532a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 533a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 534a36e185eSStefano Brivio 535a36e185eSStefano Brivio /* Network header needs to point to the outer IPv6 header inside ICMP */ 536a36e185eSStefano Brivio skb_reset_network_header(skb); 537a36e185eSStefano Brivio 538a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 539a36e185eSStefano Brivio skb_set_transport_header(skb, offset); 540a36e185eSStefano Brivio 5419bfce73cSVadim Fedorenko if (sk) { 5429bfce73cSVadim Fedorenko up = udp_sk(sk); 5439bfce73cSVadim Fedorenko 5449bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup); 5459bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb)) 5469bfce73cSVadim Fedorenko sk = NULL; 5479bfce73cSVadim Fedorenko 5489bfce73cSVadim Fedorenko goto out; 5499bfce73cSVadim Fedorenko } 5509bfce73cSVadim Fedorenko 551e7cc0824SStefano Brivio sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source, 552e7cc0824SStefano Brivio &hdr->saddr, uh->dest, 553e7cc0824SStefano Brivio inet6_iif(skb), 0, udptable, skb); 554e7cc0824SStefano Brivio if (sk) { 5559bfce73cSVadim Fedorenko up = udp_sk(sk); 556e7cc0824SStefano Brivio 557a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 558a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 559a36e185eSStefano Brivio sk = NULL; 560e7cc0824SStefano Brivio } 561e7cc0824SStefano Brivio 5629bfce73cSVadim Fedorenko out: 563e7cc0824SStefano Brivio if (!sk) { 564e7cc0824SStefano Brivio sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code, 565e7cc0824SStefano Brivio offset, info)); 566e7cc0824SStefano Brivio } 567a36e185eSStefano Brivio 568a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 569a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 570e7cc0824SStefano Brivio 571a36e185eSStefano Brivio return sk; 572a36e185eSStefano Brivio } 573a36e185eSStefano Brivio 57432bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 575d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 576645ca708SEric Dumazet struct udp_table *udptable) 577db8dac20SDavid S. Miller { 578db8dac20SDavid S. Miller struct ipv6_pinfo *np; 579b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 580b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr; 581222a011eSAndrew Lunn const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr; 582db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset); 583a36e185eSStefano Brivio bool tunnel = false; 584db8dac20SDavid S. Miller struct sock *sk; 585e0d8c1b7SWei Wang int harderr; 586db8dac20SDavid S. Miller int err; 5877304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 588db8dac20SDavid S. Miller 589e32ea7e7SCraig Gallek sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source, 5904ac30c4bSMartin KaFai Lau inet6_iif(skb), inet6_sdif(skb), udptable, NULL); 5919bfce73cSVadim Fedorenko 592d26796aeSXin Long if (!sk || udp_sk(sk)->encap_type) { 593a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 594a36e185eSStefano Brivio if (static_branch_unlikely(&udpv6_encap_needed_key)) { 595a36e185eSStefano Brivio sk = __udp6_lib_err_encap(net, hdr, offset, uh, 5969bfce73cSVadim Fedorenko udptable, sk, skb, 597e7cc0824SStefano Brivio opt, type, code, info); 598e7cc0824SStefano Brivio if (!sk) 599e7cc0824SStefano Brivio return 0; 6009bfce73cSVadim Fedorenko } else 6019bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT); 602a36e185eSStefano Brivio 603e7cc0824SStefano Brivio if (IS_ERR(sk)) { 604a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), 6057304fe46SDuan Jiong ICMP6_MIB_INERRORS); 606e7cc0824SStefano Brivio return PTR_ERR(sk); 6077304fe46SDuan Jiong } 608e7cc0824SStefano Brivio 609a36e185eSStefano Brivio tunnel = true; 610a36e185eSStefano Brivio } 611db8dac20SDavid S. Miller 612e0d8c1b7SWei Wang harderr = icmpv6_err_convert(type, code, &err); 613e0d8c1b7SWei Wang np = inet6_sk(sk); 614e0d8c1b7SWei Wang 61593b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) { 61693b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk)) 61793b36cf3SHannes Frederic Sowa goto out; 61881aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 619e0d8c1b7SWei Wang if (np->pmtudisc != IPV6_PMTUDISC_DONT) 620e0d8c1b7SWei Wang harderr = 1; 62193b36cf3SHannes Frederic Sowa } 6221a462d18SDuan Jiong if (type == NDISC_REDIRECT) { 623a36e185eSStefano Brivio if (tunnel) { 624a36e185eSStefano Brivio ip6_redirect(skb, sock_net(sk), inet6_iif(skb), 625a36e185eSStefano Brivio sk->sk_mark, sk->sk_uid); 626a36e185eSStefano Brivio } else { 627ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 628a36e185eSStefano Brivio } 6291a462d18SDuan Jiong goto out; 6301a462d18SDuan Jiong } 63181aded24SDavid S. Miller 632a36e185eSStefano Brivio /* Tunnels don't have an application socket: don't pass errors back */ 633ac56a0b4SDavid Howells if (tunnel) { 634ac56a0b4SDavid Howells if (udp_sk(sk)->encap_err_rcv) 63542fb06b3SDavid Howells udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest, 63642fb06b3SDavid Howells ntohl(info), (u8 *)(uh+1)); 637a36e185eSStefano Brivio goto out; 638ac56a0b4SDavid Howells } 639a36e185eSStefano Brivio 640e0d8c1b7SWei Wang if (!np->recverr) { 641e0d8c1b7SWei Wang if (!harderr || sk->sk_state != TCP_ESTABLISHED) 642db8dac20SDavid S. Miller goto out; 643e0d8c1b7SWei Wang } else { 644db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 645e0d8c1b7SWei Wang } 646b1faf566SEric Dumazet 647db8dac20SDavid S. Miller sk->sk_err = err; 648e3ae2365SAlexander Aring sk_error_report(sk); 649db8dac20SDavid S. Miller out: 65032bbd879SStefano Brivio return 0; 651db8dac20SDavid S. Miller } 652db8dac20SDavid S. Miller 653a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 654f7ad74feSBenjamin LaHaise { 655f7ad74feSBenjamin LaHaise int rc; 656f7ad74feSBenjamin LaHaise 657efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 658f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb); 659005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 6602c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 661e68b6e50SEric Dumazet } else { 662e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 663005ec974SShawn Bohrer } 664f7ad74feSBenjamin LaHaise 665850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 666f7ad74feSBenjamin LaHaise if (rc < 0) { 667f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk); 6680d92efdeSDonald Hunter enum skb_drop_reason drop_reason; 669f7ad74feSBenjamin LaHaise 670f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */ 6710d92efdeSDonald Hunter if (rc == -ENOMEM) { 672e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), 673f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite); 6740d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF; 6750d92efdeSDonald Hunter } else { 676a3ce2b10SMenglong Dong UDP6_INC_STATS(sock_net(sk), 677a3ce2b10SMenglong Dong UDP_MIB_MEMERRORS, is_udplite); 6780d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_PROTO_MEM; 6790d92efdeSDonald Hunter } 680e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 6810d92efdeSDonald Hunter kfree_skb_reason(skb, drop_reason); 682f7ad74feSBenjamin LaHaise return -1; 683f7ad74feSBenjamin LaHaise } 684850cbaddSPaolo Abeni 685f7ad74feSBenjamin LaHaise return 0; 686f7ad74feSBenjamin LaHaise } 687f7ad74feSBenjamin LaHaise 68832bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb, 689d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 690d5fdd6baSBrian Haley u8 code, int offset, __be32 info) 691db8dac20SDavid S. Miller { 69232bbd879SStefano Brivio return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 693db8dac20SDavid S. Miller } 694db8dac20SDavid S. Miller 695cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 696db8dac20SDavid S. Miller { 6970d92efdeSDonald Hunter enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; 698db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 699db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 700db8dac20SDavid S. Miller 7010d92efdeSDonald Hunter if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) { 7020d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_XFRM_POLICY; 703db8dac20SDavid S. Miller goto drop; 7040d92efdeSDonald Hunter } 705db8dac20SDavid S. Miller 70688ab3108SDavidlohr Bueso if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) { 707d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 708d7f3f621SBenjamin LaHaise 709d7f3f621SBenjamin LaHaise /* 710d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to 711d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just 712d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket. 713d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value: 714d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap 715d7f3f621SBenjamin LaHaise * handler or was discarded by it. 716d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP. 717d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N 718d7f3f621SBenjamin LaHaise */ 719d7f3f621SBenjamin LaHaise 720d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */ 7216aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 722e5aed006SHannes Frederic Sowa if (encap_rcv) { 723d7f3f621SBenjamin LaHaise int ret; 724d7f3f621SBenjamin LaHaise 7250a80966bSTom Herbert /* Verify checksum before giving to encap */ 7260a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 7270a80966bSTom Herbert goto csum_error; 7280a80966bSTom Herbert 729d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb); 730d7f3f621SBenjamin LaHaise if (ret <= 0) { 731250962e4SMenglong Dong __UDP6_INC_STATS(sock_net(sk), 732d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS, 733d7f3f621SBenjamin LaHaise is_udplite); 734d7f3f621SBenjamin LaHaise return -ret; 735d7f3f621SBenjamin LaHaise } 736d7f3f621SBenjamin LaHaise } 737d7f3f621SBenjamin LaHaise 738d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */ 739d7f3f621SBenjamin LaHaise } 740d7f3f621SBenjamin LaHaise 741db8dac20SDavid S. Miller /* 742db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 743db8dac20SDavid S. Miller */ 744b0a42277SMiaohe Lin if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 745db8dac20SDavid S. Miller 746db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 747ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 748db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 749db8dac20SDavid S. Miller goto drop; 750db8dac20SDavid S. Miller } 751db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 752ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 753db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 754db8dac20SDavid S. Miller goto drop; 755db8dac20SDavid S. Miller } 756db8dac20SDavid S. Miller } 757db8dac20SDavid S. Miller 7584b943faeSPaolo Abeni prefetch(&sk->sk_rmem_alloc); 759ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 760ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 7616a5dc9e5SEric Dumazet goto csum_error; 762ce25d66aSEric Dumazet 7630d92efdeSDonald Hunter if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) { 7640d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_SOCKET_FILTER; 765a6127697SMichal Kubeček goto drop; 7660d92efdeSDonald Hunter } 767db8dac20SDavid S. Miller 768e6afc8acSsamanthakumar udp_csum_pull_header(skb); 769cb80ef46SBenjamin LaHaise 770d826eb14SEric Dumazet skb_dst_drop(skb); 771db8dac20SDavid S. Miller 772850cbaddSPaolo Abeni return __udpv6_queue_rcv_skb(sk, skb); 7733e215c8dSJames M Leddy 7746a5dc9e5SEric Dumazet csum_error: 7750d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_UDP_CSUM; 77602c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 777db8dac20SDavid S. Miller drop: 77802c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 779cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops); 7800d92efdeSDonald Hunter kfree_skb_reason(skb, drop_reason); 781db8dac20SDavid S. Miller return -1; 782db8dac20SDavid S. Miller } 783db8dac20SDavid S. Miller 784cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 785cf329aa4SPaolo Abeni { 786cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 787cf329aa4SPaolo Abeni int ret; 788cf329aa4SPaolo Abeni 789cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 790cf329aa4SPaolo Abeni return udpv6_queue_rcv_one_skb(sk, skb); 791cf329aa4SPaolo Abeni 792cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 793cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, false); 7941a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 795cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 796cf329aa4SPaolo Abeni 797000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb); 798cf329aa4SPaolo Abeni ret = udpv6_queue_rcv_one_skb(sk, skb); 799cf329aa4SPaolo Abeni if (ret > 0) 800cf329aa4SPaolo Abeni ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret, 801cf329aa4SPaolo Abeni true); 802cf329aa4SPaolo Abeni } 803cf329aa4SPaolo Abeni return 0; 804cf329aa4SPaolo Abeni } 805cf329aa4SPaolo Abeni 8065cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, 807b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr, 808b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr, 8097bd2db40SDewi Morgan int dif, int sdif, unsigned short hnum) 810db8dac20SDavid S. Miller { 8119e89fd8bSSven Wegener struct inet_sock *inet = inet_sk(sk); 812db8dac20SDavid S. Miller 8139e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net)) 8145cf3d461SDavid Held return false; 815b8ad0cbcSDaniel Lezcano 8165cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum || 8175cf3d461SDavid Held sk->sk_family != PF_INET6 || 8185cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) || 8195cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) && 8205cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 8214c971d2fSEric Dumazet !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) || 82233b4b015SHenning Rogge (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && 82333b4b015SHenning Rogge !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) 8245cf3d461SDavid Held return false; 8259e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 8265cf3d461SDavid Held return false; 8275cf3d461SDavid Held return true; 828db8dac20SDavid S. Miller } 829db8dac20SDavid S. Miller 8304068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb) 8314068579eSTom Herbert { 8324068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log 8334068579eSTom Herbert * this error. Well, it is reasonable. 8344068579eSTom Herbert */ 835ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 8364068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 8374068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 8384068579eSTom Herbert } 8394068579eSTom Herbert 840db8dac20SDavid S. Miller /* 841db8dac20SDavid S. Miller * Note: called only from the BH handler context, 842db8dac20SDavid S. Miller * so we don't need to lock the hashes. 843db8dac20SDavid S. Miller */ 844e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 845b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr, 84636cbb245SRick Jones struct udp_table *udptable, int proto) 847db8dac20SDavid S. Miller { 848ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 849db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 8505cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 8515cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 852ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 8532dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 854ca065d0cSEric Dumazet int dif = inet6_iif(skb); 8557bd2db40SDewi Morgan int sdif = inet6_sdif(skb); 856ca065d0cSEric Dumazet struct hlist_node *node; 857ca065d0cSEric Dumazet struct sk_buff *nskb; 8582dc41cffSDavid Held 8592dc41cffSDavid Held if (use_hash2) { 860f0b1e64cSMartin KaFai Lau hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) & 86173e2d5e3SPablo Neira udptable->mask; 862f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask; 8632dc41cffSDavid Held start_lookup: 86473e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 8652dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 8662dc41cffSDavid Held } 867db8dac20SDavid S. Miller 868ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 869ca065d0cSEric Dumazet if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr, 8707bd2db40SDewi Morgan uh->source, saddr, dif, sdif, 8717bd2db40SDewi Morgan hnum)) 872ca065d0cSEric Dumazet continue; 8731c19448cSTom Herbert /* If zero checksum and no_check is not on for 8744068579eSTom Herbert * the socket then skip it. 8754068579eSTom Herbert */ 876ca065d0cSEric Dumazet if (!uh->check && !udp_sk(sk)->no_check6_rx) 877ca065d0cSEric Dumazet continue; 878ca065d0cSEric Dumazet if (!first) { 879ca065d0cSEric Dumazet first = sk; 880ca065d0cSEric Dumazet continue; 881db8dac20SDavid S. Miller } 882ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 883ca065d0cSEric Dumazet if (unlikely(!nskb)) { 884ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 88502c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 886ca065d0cSEric Dumazet IS_UDPLITE(sk)); 88702c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, 888ca065d0cSEric Dumazet IS_UDPLITE(sk)); 889ca065d0cSEric Dumazet continue; 890a1ab77f9SEric Dumazet } 891db8dac20SDavid S. Miller 892ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(sk, nskb) > 0) 893ca065d0cSEric Dumazet consume_skb(nskb); 894ca065d0cSEric Dumazet } 895a1ab77f9SEric Dumazet 8962dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 8972dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 8982dc41cffSDavid Held hash2 = hash2_any; 8992dc41cffSDavid Held goto start_lookup; 9002dc41cffSDavid Held } 9012dc41cffSDavid Held 902ca065d0cSEric Dumazet if (first) { 903ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(first, skb) > 0) 904ca065d0cSEric Dumazet consume_skb(skb); 905a1ab77f9SEric Dumazet } else { 906ca065d0cSEric Dumazet kfree_skb(skb); 90702c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 90836cbb245SRick Jones proto == IPPROTO_UDPLITE); 909a1ab77f9SEric Dumazet } 910db8dac20SDavid S. Miller return 0; 911db8dac20SDavid S. Miller } 912db8dac20SDavid S. Miller 91364f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 91464f0f5d1SPaolo Abeni { 91564f0f5d1SPaolo Abeni if (udp_sk_rx_dst_set(sk, dst)) { 91664f0f5d1SPaolo Abeni const struct rt6_info *rt = (const struct rt6_info *)dst; 91764f0f5d1SPaolo Abeni 918ef57c161SEric Dumazet sk->sk_rx_dst_cookie = rt6_get_cookie(rt); 91964f0f5d1SPaolo Abeni } 92064f0f5d1SPaolo Abeni } 92164f0f5d1SPaolo Abeni 922eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 923eb63f296SPaolo Abeni * return code conversion for ip layer consumption 924eb63f296SPaolo Abeni */ 925eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 926eb63f296SPaolo Abeni struct udphdr *uh) 927eb63f296SPaolo Abeni { 928eb63f296SPaolo Abeni int ret; 929eb63f296SPaolo Abeni 930eb63f296SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 931e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo); 932eb63f296SPaolo Abeni 933eb63f296SPaolo Abeni ret = udpv6_queue_rcv_skb(sk, skb); 934eb63f296SPaolo Abeni 93584dad559SPaolo Abeni /* a return value > 0 means to resubmit the input */ 936eb63f296SPaolo Abeni if (ret > 0) 93784dad559SPaolo Abeni return ret; 938eb63f296SPaolo Abeni return 0; 939eb63f296SPaolo Abeni } 940eb63f296SPaolo Abeni 941645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 942db8dac20SDavid S. Miller int proto) 943db8dac20SDavid S. Miller { 9444cf91f82SDavid Ahern enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; 945b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 946ca065d0cSEric Dumazet struct net *net = dev_net(skb->dev); 947ca065d0cSEric Dumazet struct udphdr *uh; 948ca065d0cSEric Dumazet struct sock *sk; 94971489e21SJoe Stringer bool refcounted; 950db8dac20SDavid S. Miller u32 ulen = 0; 951db8dac20SDavid S. Miller 952db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 953d6bc0149SBjørn Mork goto discard; 954db8dac20SDavid S. Miller 955db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 956db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 957db8dac20SDavid S. Miller uh = udp_hdr(skb); 958db8dac20SDavid S. Miller 959db8dac20SDavid S. Miller ulen = ntohs(uh->len); 960db8dac20SDavid S. Miller if (ulen > skb->len) 961db8dac20SDavid S. Miller goto short_packet; 962db8dac20SDavid S. Miller 963db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 964db8dac20SDavid S. Miller /* UDP validates ulen. */ 965db8dac20SDavid S. Miller 966db8dac20SDavid S. Miller /* Check for jumbo payload */ 967db8dac20SDavid S. Miller if (ulen == 0) 968db8dac20SDavid S. Miller ulen = skb->len; 969db8dac20SDavid S. Miller 970db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 971db8dac20SDavid S. Miller goto short_packet; 972db8dac20SDavid S. Miller 973db8dac20SDavid S. Miller if (ulen < skb->len) { 974db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 975db8dac20SDavid S. Miller goto short_packet; 976db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 977db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 978db8dac20SDavid S. Miller uh = udp_hdr(skb); 979db8dac20SDavid S. Miller } 980db8dac20SDavid S. Miller } 981db8dac20SDavid S. Miller 982db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 9836a5dc9e5SEric Dumazet goto csum_error; 984db8dac20SDavid S. Miller 985c9f2c1aeSPaolo Abeni /* Check if the socket is already available, e.g. due to early demux */ 98671489e21SJoe Stringer sk = skb_steal_sock(skb, &refcounted); 987c9f2c1aeSPaolo Abeni if (sk) { 988c9f2c1aeSPaolo Abeni struct dst_entry *dst = skb_dst(skb); 989c9f2c1aeSPaolo Abeni int ret; 990c9f2c1aeSPaolo Abeni 9918f905c0eSEric Dumazet if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst)) 99264f0f5d1SPaolo Abeni udp6_sk_rx_dst_set(sk, dst); 993c9f2c1aeSPaolo Abeni 994eb63f296SPaolo Abeni if (!uh->check && !udp_sk(sk)->no_check6_rx) { 99571489e21SJoe Stringer if (refcounted) 996c9f2c1aeSPaolo Abeni sock_put(sk); 997eb63f296SPaolo Abeni goto report_csum_error; 998eb63f296SPaolo Abeni } 999c9f2c1aeSPaolo Abeni 1000eb63f296SPaolo Abeni ret = udp6_unicast_rcv_skb(sk, skb, uh); 100171489e21SJoe Stringer if (refcounted) 1002eb63f296SPaolo Abeni sock_put(sk); 1003c9f2c1aeSPaolo Abeni return ret; 1004c9f2c1aeSPaolo Abeni } 1005c9f2c1aeSPaolo Abeni 1006db8dac20SDavid S. Miller /* 1007db8dac20SDavid S. Miller * Multicast receive code 1008db8dac20SDavid S. Miller */ 1009db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 1010e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 101136cbb245SRick Jones saddr, daddr, udptable, proto); 1012db8dac20SDavid S. Miller 1013db8dac20SDavid S. Miller /* Unicast */ 1014607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 101553b24b8fSIan Morris if (sk) { 1016eb63f296SPaolo Abeni if (!uh->check && !udp_sk(sk)->no_check6_rx) 1017eb63f296SPaolo Abeni goto report_csum_error; 1018eb63f296SPaolo Abeni return udp6_unicast_rcv_skb(sk, skb, uh); 10194068579eSTom Herbert } 10204068579eSTom Herbert 10214cf91f82SDavid Ahern reason = SKB_DROP_REASON_NO_SOCKET; 10224cf91f82SDavid Ahern 1023eb63f296SPaolo Abeni if (!uh->check) 1024eb63f296SPaolo Abeni goto report_csum_error; 10254068579eSTom Herbert 1026db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 1027db8dac20SDavid S. Miller goto discard; 1028db8dac20SDavid S. Miller 1029db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 10306a5dc9e5SEric Dumazet goto csum_error; 1031db8dac20SDavid S. Miller 103202c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 10333ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 1034db8dac20SDavid S. Miller 10354cf91f82SDavid Ahern kfree_skb_reason(skb, reason); 1036db8dac20SDavid S. Miller return 0; 1037db8dac20SDavid S. Miller 1038db8dac20SDavid S. Miller short_packet: 10394cf91f82SDavid Ahern if (reason == SKB_DROP_REASON_NOT_SPECIFIED) 10404cf91f82SDavid Ahern reason = SKB_DROP_REASON_PKT_TOO_SMALL; 1041ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 1042db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1043ba7a46f1SJoe Perches saddr, ntohs(uh->source), 1044ba7a46f1SJoe Perches ulen, skb->len, 1045ba7a46f1SJoe Perches daddr, ntohs(uh->dest)); 10466a5dc9e5SEric Dumazet goto discard; 1047eb63f296SPaolo Abeni 1048eb63f296SPaolo Abeni report_csum_error: 1049eb63f296SPaolo Abeni udp6_csum_zero_error(skb); 10506a5dc9e5SEric Dumazet csum_error: 10514cf91f82SDavid Ahern if (reason == SKB_DROP_REASON_NOT_SPECIFIED) 10524cf91f82SDavid Ahern reason = SKB_DROP_REASON_UDP_CSUM; 105302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 1054db8dac20SDavid S. Miller discard: 105502c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 10564cf91f82SDavid Ahern kfree_skb_reason(skb, reason); 1057db8dac20SDavid S. Miller return 0; 1058db8dac20SDavid S. Miller } 1059db8dac20SDavid S. Miller 10600bd84065Ssubashab@codeaurora.org 10615425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net, 10625425077dSsubashab@codeaurora.org __be16 loc_port, const struct in6_addr *loc_addr, 10635425077dSsubashab@codeaurora.org __be16 rmt_port, const struct in6_addr *rmt_addr, 10644297a0efSDavid Ahern int dif, int sdif) 10655425077dSsubashab@codeaurora.org { 10660bd84065Ssubashab@codeaurora.org unsigned short hnum = ntohs(loc_port); 1067919dfa0bSKuniyuki Iwashima unsigned int hash2, slot2; 1068919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot2; 1069919dfa0bSKuniyuki Iwashima __portpair ports; 10705425077dSsubashab@codeaurora.org struct sock *sk; 10715425077dSsubashab@codeaurora.org 1072919dfa0bSKuniyuki Iwashima hash2 = ipv6_portaddr_hash(net, loc_addr, hnum); 1073919dfa0bSKuniyuki Iwashima slot2 = hash2 & udp_table.mask; 1074919dfa0bSKuniyuki Iwashima hslot2 = &udp_table.hash2[slot2]; 1075919dfa0bSKuniyuki Iwashima ports = INET_COMBINED_PORTS(rmt_port, hnum); 1076919dfa0bSKuniyuki Iwashima 10770bd84065Ssubashab@codeaurora.org udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 107885cb73ffSWei Wang if (sk->sk_state == TCP_ESTABLISHED && 10795d368f03SEric Dumazet inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif)) 10805425077dSsubashab@codeaurora.org return sk; 10810bd84065Ssubashab@codeaurora.org /* Only check first socket in chain */ 10820bd84065Ssubashab@codeaurora.org break; 10830bd84065Ssubashab@codeaurora.org } 10840bd84065Ssubashab@codeaurora.org return NULL; 10855425077dSsubashab@codeaurora.org } 10865425077dSsubashab@codeaurora.org 108711052589SKuniyuki Iwashima void udp_v6_early_demux(struct sk_buff *skb) 10885425077dSsubashab@codeaurora.org { 10895425077dSsubashab@codeaurora.org struct net *net = dev_net(skb->dev); 10905425077dSsubashab@codeaurora.org const struct udphdr *uh; 10915425077dSsubashab@codeaurora.org struct sock *sk; 10925425077dSsubashab@codeaurora.org struct dst_entry *dst; 10935425077dSsubashab@codeaurora.org int dif = skb->dev->ifindex; 10944297a0efSDavid Ahern int sdif = inet6_sdif(skb); 10955425077dSsubashab@codeaurora.org 10965425077dSsubashab@codeaurora.org if (!pskb_may_pull(skb, skb_transport_offset(skb) + 10975425077dSsubashab@codeaurora.org sizeof(struct udphdr))) 10985425077dSsubashab@codeaurora.org return; 10995425077dSsubashab@codeaurora.org 11005425077dSsubashab@codeaurora.org uh = udp_hdr(skb); 11015425077dSsubashab@codeaurora.org 11025425077dSsubashab@codeaurora.org if (skb->pkt_type == PACKET_HOST) 11035425077dSsubashab@codeaurora.org sk = __udp6_lib_demux_lookup(net, uh->dest, 11045425077dSsubashab@codeaurora.org &ipv6_hdr(skb)->daddr, 11055425077dSsubashab@codeaurora.org uh->source, &ipv6_hdr(skb)->saddr, 11064297a0efSDavid Ahern dif, sdif); 11075425077dSsubashab@codeaurora.org else 11085425077dSsubashab@codeaurora.org return; 11095425077dSsubashab@codeaurora.org 111041c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 11115425077dSsubashab@codeaurora.org return; 11125425077dSsubashab@codeaurora.org 11135425077dSsubashab@codeaurora.org skb->sk = sk; 11145425077dSsubashab@codeaurora.org skb->destructor = sock_efree; 11158f905c0eSEric Dumazet dst = rcu_dereference(sk->sk_rx_dst); 11165425077dSsubashab@codeaurora.org 11175425077dSsubashab@codeaurora.org if (dst) 1118ef57c161SEric Dumazet dst = dst_check(dst, sk->sk_rx_dst_cookie); 11195425077dSsubashab@codeaurora.org if (dst) { 1120d24406c8SWei Wang /* set noref for now. 1121d24406c8SWei Wang * any place which wants to hold dst has to call 1122d24406c8SWei Wang * dst_hold_safe() 1123d24406c8SWei Wang */ 11245425077dSsubashab@codeaurora.org skb_dst_set_noref(skb, dst); 11255425077dSsubashab@codeaurora.org } 11265425077dSsubashab@codeaurora.org } 11275425077dSsubashab@codeaurora.org 11280e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb) 1129db8dac20SDavid S. Miller { 1130645ca708SEric Dumazet return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1131db8dac20SDavid S. Miller } 1132db8dac20SDavid S. Miller 1133db8dac20SDavid S. Miller /* 1134db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 1135db8dac20SDavid S. Miller */ 1136db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 1137db8dac20SDavid S. Miller { 1138db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1139db8dac20SDavid S. Miller 114036d926b9SDenis V. Lunev if (up->pending == AF_INET) 114136d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 114236d926b9SDenis V. Lunev else if (up->pending) { 1143db8dac20SDavid S. Miller up->len = 0; 1144db8dac20SDavid S. Miller up->pending = 0; 1145db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 1146db8dac20SDavid S. Miller } 1147db8dac20SDavid S. Miller } 1148db8dac20SDavid S. Miller 1149d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr, 1150d74bad4eSAndrey Ignatov int addr_len) 1151d74bad4eSAndrey Ignatov { 1152bddc028aSTetsuo Handa if (addr_len < offsetofend(struct sockaddr, sa_family)) 1153bddc028aSTetsuo Handa return -EINVAL; 1154d74bad4eSAndrey Ignatov /* The following checks are replicated from __ip6_datagram_connect() 1155d74bad4eSAndrey Ignatov * and intended to prevent BPF program called below from accessing 1156d74bad4eSAndrey Ignatov * bytes that are out of the bound specified by user in addr_len. 1157d74bad4eSAndrey Ignatov */ 1158d74bad4eSAndrey Ignatov if (uaddr->sa_family == AF_INET) { 115989e9c728SKuniyuki Iwashima if (ipv6_only_sock(sk)) 1160d74bad4eSAndrey Ignatov return -EAFNOSUPPORT; 1161d74bad4eSAndrey Ignatov return udp_pre_connect(sk, uaddr, addr_len); 1162d74bad4eSAndrey Ignatov } 1163d74bad4eSAndrey Ignatov 1164d74bad4eSAndrey Ignatov if (addr_len < SIN6_LEN_RFC2133) 1165d74bad4eSAndrey Ignatov return -EINVAL; 1166d74bad4eSAndrey Ignatov 1167d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr); 1168d74bad4eSAndrey Ignatov } 1169d74bad4eSAndrey Ignatov 1170493c6be3SSridhar Samudrala /** 1171493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 1172493c6be3SSridhar Samudrala * @sk: socket we are sending on 1173493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 1174493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 1175b51cd7c8SAndrew Lunn * @saddr: source address 1176b51cd7c8SAndrew Lunn * @daddr: destination address 1177b51cd7c8SAndrew Lunn * @len: length of packet 1178493c6be3SSridhar Samudrala */ 1179493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 1180493c6be3SSridhar Samudrala const struct in6_addr *saddr, 1181493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 1182493c6be3SSridhar Samudrala { 1183493c6be3SSridhar Samudrala unsigned int offset; 1184493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 1185d39d938cSVlad Yasevich struct sk_buff *frags = skb_shinfo(skb)->frag_list; 1186493c6be3SSridhar Samudrala __wsum csum = 0; 1187493c6be3SSridhar Samudrala 1188d39d938cSVlad Yasevich if (!frags) { 1189493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 1190493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 1191493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 1192493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 1193493c6be3SSridhar Samudrala } else { 1194493c6be3SSridhar Samudrala /* 1195493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 1196493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 1197493c6be3SSridhar Samudrala * should be together 1198493c6be3SSridhar Samudrala */ 1199493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 1200493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 120163ecc3d9SSubash Abhinov Kasiviswanathan csum = skb->csum; 1202493c6be3SSridhar Samudrala 1203493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1204493c6be3SSridhar Samudrala 1205d39d938cSVlad Yasevich do { 1206d39d938cSVlad Yasevich csum = csum_add(csum, frags->csum); 1207d39d938cSVlad Yasevich } while ((frags = frags->next)); 1208493c6be3SSridhar Samudrala 1209493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1210493c6be3SSridhar Samudrala csum); 1211493c6be3SSridhar Samudrala if (uh->check == 0) 1212493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 1213493c6be3SSridhar Samudrala } 1214493c6be3SSridhar Samudrala } 1215493c6be3SSridhar Samudrala 1216db8dac20SDavid S. Miller /* 1217db8dac20SDavid S. Miller * Sending 1218db8dac20SDavid S. Miller */ 1219db8dac20SDavid S. Miller 1220bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6, 1221bec1f6f6SWillem de Bruijn struct inet_cork *cork) 1222db8dac20SDavid S. Miller { 1223d39d938cSVlad Yasevich struct sock *sk = skb->sk; 1224db8dac20SDavid S. Miller struct udphdr *uh; 1225db8dac20SDavid S. Miller int err = 0; 1226db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1227db8dac20SDavid S. Miller __wsum csum = 0; 1228d39d938cSVlad Yasevich int offset = skb_transport_offset(skb); 1229d39d938cSVlad Yasevich int len = skb->len - offset; 12304094871dSJosh Hunt int datalen = len - sizeof(*uh); 1231db8dac20SDavid S. Miller 1232db8dac20SDavid S. Miller /* 1233db8dac20SDavid S. Miller * Create a UDP header 1234db8dac20SDavid S. Miller */ 1235db8dac20SDavid S. Miller uh = udp_hdr(skb); 12361958b856SDavid S. Miller uh->source = fl6->fl6_sport; 12371958b856SDavid S. Miller uh->dest = fl6->fl6_dport; 1238d39d938cSVlad Yasevich uh->len = htons(len); 1239db8dac20SDavid S. Miller uh->check = 0; 1240db8dac20SDavid S. Miller 1241bec1f6f6SWillem de Bruijn if (cork->gso_size) { 1242bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 1243bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 1244bec1f6f6SWillem de Bruijn 12450f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 12460f149c9fSWillem de Bruijn kfree_skb(skb); 1247bec1f6f6SWillem de Bruijn return -EINVAL; 12480f149c9fSWillem de Bruijn } 1249736ef37fSCoco Li if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) { 12500f149c9fSWillem de Bruijn kfree_skb(skb); 1251bec1f6f6SWillem de Bruijn return -EINVAL; 12520f149c9fSWillem de Bruijn } 12530f149c9fSWillem de Bruijn if (udp_sk(sk)->no_check6_tx) { 12540f149c9fSWillem de Bruijn kfree_skb(skb); 1255a8c744a8SWillem de Bruijn return -EINVAL; 12560f149c9fSWillem de Bruijn } 1257ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 12580f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 12590f149c9fSWillem de Bruijn kfree_skb(skb); 1260bec1f6f6SWillem de Bruijn return -EIO; 12610f149c9fSWillem de Bruijn } 1262bec1f6f6SWillem de Bruijn 12634094871dSJosh Hunt if (datalen > cork->gso_size) { 1264bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 1265bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 12664094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 126744b321e5SJosh Hunt cork->gso_size); 12684094871dSJosh Hunt } 1269a8c744a8SWillem de Bruijn goto csum_partial; 1270bec1f6f6SWillem de Bruijn } 1271bec1f6f6SWillem de Bruijn 1272db8dac20SDavid S. Miller if (is_udplite) 1273d39d938cSVlad Yasevich csum = udplite_csum(skb); 1274d39d938cSVlad Yasevich else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */ 12754068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE; 12764068579eSTom Herbert goto send; 12774068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1278a8c744a8SWillem de Bruijn csum_partial: 1279d39d938cSVlad Yasevich udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len); 1280493c6be3SSridhar Samudrala goto send; 1281493c6be3SSridhar Samudrala } else 1282d39d938cSVlad Yasevich csum = udp_csum(skb); 1283db8dac20SDavid S. Miller 1284db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 12854c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 1286d39d938cSVlad Yasevich len, fl6->flowi6_proto, csum); 1287db8dac20SDavid S. Miller if (uh->check == 0) 1288db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 1289db8dac20SDavid S. Miller 1290493c6be3SSridhar Samudrala send: 1291d39d938cSVlad Yasevich err = ip6_send_skb(skb); 12926ce9e7b5SEric Dumazet if (err) { 12936ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 12946aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12956ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 12966ce9e7b5SEric Dumazet err = 0; 12976ce9e7b5SEric Dumazet } 12986aef70a8SEric Dumazet } else { 12996aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 13006ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 13016aef70a8SEric Dumazet } 1302d39d938cSVlad Yasevich return err; 1303d39d938cSVlad Yasevich } 1304d39d938cSVlad Yasevich 1305d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk) 1306d39d938cSVlad Yasevich { 1307d39d938cSVlad Yasevich struct sk_buff *skb; 1308d39d938cSVlad Yasevich struct udp_sock *up = udp_sk(sk); 1309d39d938cSVlad Yasevich int err = 0; 1310d39d938cSVlad Yasevich 1311d39d938cSVlad Yasevich if (up->pending == AF_INET) 1312d39d938cSVlad Yasevich return udp_push_pending_frames(sk); 1313d39d938cSVlad Yasevich 1314d39d938cSVlad Yasevich skb = ip6_finish_skb(sk); 1315d39d938cSVlad Yasevich if (!skb) 1316d39d938cSVlad Yasevich goto out; 1317d39d938cSVlad Yasevich 1318940ea00bSPavel Begunkov err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6, 1319940ea00bSPavel Begunkov &inet_sk(sk)->cork.base); 1320db8dac20SDavid S. Miller out: 1321db8dac20SDavid S. Miller up->len = 0; 1322db8dac20SDavid S. Miller up->pending = 0; 1323db8dac20SDavid S. Miller return err; 1324db8dac20SDavid S. Miller } 1325db8dac20SDavid S. Miller 13261b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1327db8dac20SDavid S. Miller { 1328db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 1329db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1330db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1331db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 1332342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 133320c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 1334db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 133545f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 1336db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 13375298953eSPavel Begunkov struct inet_cork_full cork; 13385298953eSPavel Begunkov struct flowi6 *fl6 = &cork.fl.u.ip6; 1339db8dac20SDavid S. Miller struct dst_entry *dst; 134026879da5SWei Wang struct ipcm6_cookie ipc6; 1341db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 13429f542f61SAlexey Kodanev bool connected = false; 1343db8dac20SDavid S. Miller int ulen = len; 1344a9f59707SEric Dumazet int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE; 1345db8dac20SDavid S. Miller int err; 1346db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1347db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1348db8dac20SDavid S. Miller 1349b515430aSWillem de Bruijn ipcm6_init(&ipc6); 135018a419baSEric Dumazet ipc6.gso_size = READ_ONCE(up->gso_size); 13515fdaa88dSWillem de Bruijn ipc6.sockc.tsflags = sk->sk_tsflags; 1352c6af0c22SWillem de Bruijn ipc6.sockc.mark = sk->sk_mark; 135326879da5SWei Wang 1354db8dac20SDavid S. Miller /* destination address check */ 1355db8dac20SDavid S. Miller if (sin6) { 1356db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 1357db8dac20SDavid S. Miller return -EINVAL; 1358db8dac20SDavid S. Miller 1359db8dac20SDavid S. Miller switch (sin6->sin6_family) { 1360db8dac20SDavid S. Miller case AF_INET6: 1361db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 1362db8dac20SDavid S. Miller return -EINVAL; 1363db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1364052d2369SJonathan T. Leighton if (ipv6_addr_any(daddr) && 1365052d2369SJonathan T. Leighton ipv6_addr_v4mapped(&np->saddr)) 1366052d2369SJonathan T. Leighton ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 1367052d2369SJonathan T. Leighton daddr); 1368db8dac20SDavid S. Miller break; 1369db8dac20SDavid S. Miller case AF_INET: 1370db8dac20SDavid S. Miller goto do_udp_sendmsg; 1371db8dac20SDavid S. Miller case AF_UNSPEC: 1372db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 1373db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 1374db8dac20SDavid S. Miller daddr = NULL; 1375db8dac20SDavid S. Miller break; 1376db8dac20SDavid S. Miller default: 1377db8dac20SDavid S. Miller return -EINVAL; 1378db8dac20SDavid S. Miller } 1379db8dac20SDavid S. Miller } else if (!up->pending) { 1380db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1381db8dac20SDavid S. Miller return -EDESTADDRREQ; 1382efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1383db8dac20SDavid S. Miller } else 1384db8dac20SDavid S. Miller daddr = NULL; 1385db8dac20SDavid S. Miller 1386db8dac20SDavid S. Miller if (daddr) { 1387db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 1388db8dac20SDavid S. Miller struct sockaddr_in sin; 1389db8dac20SDavid S. Miller sin.sin_family = AF_INET; 1390c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1391db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1392db8dac20SDavid S. Miller msg->msg_name = &sin; 1393db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 1394db8dac20SDavid S. Miller do_udp_sendmsg: 139589e9c728SKuniyuki Iwashima if (ipv6_only_sock(sk)) 1396db8dac20SDavid S. Miller return -ENETUNREACH; 13971b784140SYing Xue return udp_sendmsg(sk, msg, len); 1398db8dac20SDavid S. Miller } 1399db8dac20SDavid S. Miller } 1400db8dac20SDavid S. Miller 1401db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 1402db8dac20SDavid S. Miller better check is made in ip6_append_data(). 1403db8dac20SDavid S. Miller */ 1404db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 1405db8dac20SDavid S. Miller return -EMSGSIZE; 1406db8dac20SDavid S. Miller 140703485f2aSVlad Yasevich getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1408db8dac20SDavid S. Miller if (up->pending) { 1409406c4a0aSPavel Begunkov if (up->pending == AF_INET) 1410406c4a0aSPavel Begunkov return udp_sendmsg(sk, msg, len); 1411db8dac20SDavid S. Miller /* 1412db8dac20SDavid S. Miller * There are pending frames. 1413db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1414db8dac20SDavid S. Miller */ 1415db8dac20SDavid S. Miller lock_sock(sk); 1416db8dac20SDavid S. Miller if (likely(up->pending)) { 1417db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 1418db8dac20SDavid S. Miller release_sock(sk); 1419db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1420db8dac20SDavid S. Miller } 1421db8dac20SDavid S. Miller dst = NULL; 1422db8dac20SDavid S. Miller goto do_append_data; 1423db8dac20SDavid S. Miller } 1424db8dac20SDavid S. Miller release_sock(sk); 1425db8dac20SDavid S. Miller } 1426db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1427db8dac20SDavid S. Miller 14285298953eSPavel Begunkov memset(fl6, 0, sizeof(*fl6)); 1429db8dac20SDavid S. Miller 1430db8dac20SDavid S. Miller if (sin6) { 1431db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 1432db8dac20SDavid S. Miller return -EINVAL; 1433db8dac20SDavid S. Miller 14345298953eSPavel Begunkov fl6->fl6_dport = sin6->sin6_port; 1435db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1436db8dac20SDavid S. Miller 1437db8dac20SDavid S. Miller if (np->sndflow) { 14385298953eSPavel Begunkov fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 14395298953eSPavel Begunkov if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) { 14405298953eSPavel Begunkov flowlabel = fl6_sock_lookup(sk, fl6->flowlabel); 144159c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 1442db8dac20SDavid S. Miller return -EINVAL; 1443db8dac20SDavid S. Miller } 1444db8dac20SDavid S. Miller } 1445db8dac20SDavid S. Miller 1446db8dac20SDavid S. Miller /* 1447db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 1448db8dac20SDavid S. Miller * sk->sk_dst_cache. 1449db8dac20SDavid S. Miller */ 1450db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 1451efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1452efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1453db8dac20SDavid S. Miller 1454db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 1455db8dac20SDavid S. Miller sin6->sin6_scope_id && 1456842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 14575298953eSPavel Begunkov fl6->flowi6_oif = sin6->sin6_scope_id; 1458db8dac20SDavid S. Miller } else { 1459db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1460db8dac20SDavid S. Miller return -EDESTADDRREQ; 1461db8dac20SDavid S. Miller 14625298953eSPavel Begunkov fl6->fl6_dport = inet->inet_dport; 1463efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 14645298953eSPavel Begunkov fl6->flowlabel = np->flow_label; 14659f542f61SAlexey Kodanev connected = true; 1466db8dac20SDavid S. Miller } 1467db8dac20SDavid S. Miller 14685298953eSPavel Begunkov if (!fl6->flowi6_oif) 14694c971d2fSEric Dumazet fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if); 1470db8dac20SDavid S. Miller 14715298953eSPavel Begunkov if (!fl6->flowi6_oif) 14725298953eSPavel Begunkov fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 14739f690db7SYang Hongyang 14745298953eSPavel Begunkov fl6->flowi6_uid = sk->sk_uid; 147551953d5bSBrian Haley 1476db8dac20SDavid S. Miller if (msg->msg_controllen) { 1477db8dac20SDavid S. Miller opt = &opt_space; 1478db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 1479db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 148026879da5SWei Wang ipc6.opt = opt; 1481db8dac20SDavid S. Miller 14822e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc6.gso_size); 14832e8de857SWillem de Bruijn if (err > 0) 14845298953eSPavel Begunkov err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6, 14855fdaa88dSWillem de Bruijn &ipc6); 1486db8dac20SDavid S. Miller if (err < 0) { 1487db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1488db8dac20SDavid S. Miller return err; 1489db8dac20SDavid S. Miller } 14905298953eSPavel Begunkov if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 14915298953eSPavel Begunkov flowlabel = fl6_sock_lookup(sk, fl6->flowlabel); 149259c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 1493db8dac20SDavid S. Miller return -EINVAL; 1494db8dac20SDavid S. Miller } 1495db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 1496db8dac20SDavid S. Miller opt = NULL; 14979f542f61SAlexey Kodanev connected = false; 1498db8dac20SDavid S. Miller } 149945f6fad8SEric Dumazet if (!opt) { 150045f6fad8SEric Dumazet opt = txopt_get(np); 150145f6fad8SEric Dumazet opt_to_free = opt; 150245f6fad8SEric Dumazet } 1503db8dac20SDavid S. Miller if (flowlabel) 1504db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 1505db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 150626879da5SWei Wang ipc6.opt = opt; 1507db8dac20SDavid S. Miller 15085298953eSPavel Begunkov fl6->flowi6_proto = sk->sk_protocol; 15095298953eSPavel Begunkov fl6->flowi6_mark = ipc6.sockc.mark; 15105298953eSPavel Begunkov fl6->daddr = *daddr; 15115298953eSPavel Begunkov if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr)) 15125298953eSPavel Begunkov fl6->saddr = np->saddr; 15135298953eSPavel Begunkov fl6->fl6_sport = inet->inet_sport; 1514db8dac20SDavid S. Miller 15156fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) { 15161cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, 15175298953eSPavel Begunkov (struct sockaddr *)sin6, 15185298953eSPavel Begunkov &fl6->saddr); 15191cedee13SAndrey Ignatov if (err) 15201cedee13SAndrey Ignatov goto out_no_dst; 15211cedee13SAndrey Ignatov if (sin6) { 15221cedee13SAndrey Ignatov if (ipv6_addr_v4mapped(&sin6->sin6_addr)) { 15231cedee13SAndrey Ignatov /* BPF program rewrote IPv6-only by IPv4-mapped 15241cedee13SAndrey Ignatov * IPv6. It's currently unsupported. 15251cedee13SAndrey Ignatov */ 15261cedee13SAndrey Ignatov err = -ENOTSUPP; 15271cedee13SAndrey Ignatov goto out_no_dst; 15281cedee13SAndrey Ignatov } 15291cedee13SAndrey Ignatov if (sin6->sin6_port == 0) { 15301cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 15311cedee13SAndrey Ignatov err = -EINVAL; 15321cedee13SAndrey Ignatov goto out_no_dst; 15331cedee13SAndrey Ignatov } 15345298953eSPavel Begunkov fl6->fl6_dport = sin6->sin6_port; 15355298953eSPavel Begunkov fl6->daddr = sin6->sin6_addr; 15361cedee13SAndrey Ignatov } 15371cedee13SAndrey Ignatov } 15381cedee13SAndrey Ignatov 15395298953eSPavel Begunkov if (ipv6_addr_any(&fl6->daddr)) 15405298953eSPavel Begunkov fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 1541e8e36984SAndrey Ignatov 15425298953eSPavel Begunkov final_p = fl6_update_dst(fl6, opt, &final); 154320c59de2SArnaud Ebalard if (final_p) 15449f542f61SAlexey Kodanev connected = false; 1545db8dac20SDavid S. Miller 15465298953eSPavel Begunkov if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) { 15475298953eSPavel Begunkov fl6->flowi6_oif = np->mcast_oif; 15489f542f61SAlexey Kodanev connected = false; 15495298953eSPavel Begunkov } else if (!fl6->flowi6_oif) 15505298953eSPavel Begunkov fl6->flowi6_oif = np->ucast_oif; 1551db8dac20SDavid S. Miller 15525298953eSPavel Begunkov security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6)); 1553db8dac20SDavid S. Miller 155438b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 155538b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 155638b7097bSHannes Frederic Sowa 15575298953eSPavel Begunkov fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel); 155838b7097bSHannes Frederic Sowa 15595298953eSPavel Begunkov dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected); 156068d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 156168d0c6d3SDavid S. Miller err = PTR_ERR(dst); 156268d0c6d3SDavid S. Miller dst = NULL; 1563db8dac20SDavid S. Miller goto out; 1564db8dac20SDavid S. Miller } 1565db8dac20SDavid S. Miller 156626879da5SWei Wang if (ipc6.hlimit < 0) 15675298953eSPavel Begunkov ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst); 1568db8dac20SDavid S. Miller 1569db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1570db8dac20SDavid S. Miller goto do_confirm; 1571db8dac20SDavid S. Miller back_from_confirm: 1572db8dac20SDavid S. Miller 157303485f2aSVlad Yasevich /* Lockless fast path for the non-corking case */ 157403485f2aSVlad Yasevich if (!corkreq) { 157503485f2aSVlad Yasevich struct sk_buff *skb; 157603485f2aSVlad Yasevich 157703485f2aSVlad Yasevich skb = ip6_make_skb(sk, getfrag, msg, ulen, 157826879da5SWei Wang sizeof(struct udphdr), &ipc6, 1579f37a4cc6SPavel Begunkov (struct rt6_info *)dst, 15805fdaa88dSWillem de Bruijn msg->msg_flags, &cork); 158103485f2aSVlad Yasevich err = PTR_ERR(skb); 158203485f2aSVlad Yasevich if (!IS_ERR_OR_NULL(skb)) 15835298953eSPavel Begunkov err = udp_v6_send_skb(skb, fl6, &cork.base); 158440ac240cSPavel Begunkov /* ip6_make_skb steals dst reference */ 158540ac240cSPavel Begunkov goto out_no_dst; 158603485f2aSVlad Yasevich } 158703485f2aSVlad Yasevich 1588db8dac20SDavid S. Miller lock_sock(sk); 1589db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1590db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1591db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1592db8dac20SDavid S. Miller release_sock(sk); 1593db8dac20SDavid S. Miller 1594ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n"); 1595db8dac20SDavid S. Miller err = -EINVAL; 1596db8dac20SDavid S. Miller goto out; 1597db8dac20SDavid S. Miller } 1598db8dac20SDavid S. Miller 1599db8dac20SDavid S. Miller up->pending = AF_INET6; 1600db8dac20SDavid S. Miller 1601db8dac20SDavid S. Miller do_append_data: 160226879da5SWei Wang if (ipc6.dontfrag < 0) 160326879da5SWei Wang ipc6.dontfrag = np->dontfrag; 1604db8dac20SDavid S. Miller up->len += ulen; 160526879da5SWei Wang err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 16065298953eSPavel Begunkov &ipc6, fl6, (struct rt6_info *)dst, 16075fdaa88dSWillem de Bruijn corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1608db8dac20SDavid S. Miller if (err) 1609db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1610db8dac20SDavid S. Miller else if (!corkreq) 1611db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 1612db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1613db8dac20SDavid S. Miller up->pending = 0; 1614db8dac20SDavid S. Miller 161503485f2aSVlad Yasevich if (err > 0) 161603485f2aSVlad Yasevich err = np->recverr ? net_xmit_errno(err) : 0; 161703485f2aSVlad Yasevich release_sock(sk); 161803485f2aSVlad Yasevich 1619db8dac20SDavid S. Miller out: 1620a3c96089SYOSHIFUJI Hideaki dst_release(dst); 16211cedee13SAndrey Ignatov out_no_dst: 1622db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 162345f6fad8SEric Dumazet txopt_put(opt_to_free); 1624db8dac20SDavid S. Miller if (!err) 1625db8dac20SDavid S. Miller return len; 1626db8dac20SDavid S. Miller /* 1627db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1628db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1629db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1630db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1631db8dac20SDavid S. Miller * seems like overkill. 1632db8dac20SDavid S. Miller */ 1633db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 16346aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 1635235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1636db8dac20SDavid S. Miller } 1637db8dac20SDavid S. Miller return err; 1638db8dac20SDavid S. Miller 1639db8dac20SDavid S. Miller do_confirm: 16400dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 16415298953eSPavel Begunkov dst_confirm_neigh(dst, &fl6->daddr); 1642db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1643db8dac20SDavid S. Miller goto back_from_confirm; 1644db8dac20SDavid S. Miller err = 0; 1645db8dac20SDavid S. Miller goto out; 1646db8dac20SDavid S. Miller } 1647ed472b0cSDavid Howells EXPORT_SYMBOL(udpv6_sendmsg); 1648db8dac20SDavid S. Miller 16497d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1650db8dac20SDavid S. Miller { 165144046a59STom Parkin struct udp_sock *up = udp_sk(sk); 1652db8dac20SDavid S. Miller lock_sock(sk); 1653a8b897c7SPaolo Abeni 1654a8b897c7SPaolo Abeni /* protects from races with udp_abort() */ 1655a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 1656db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1657db8dac20SDavid S. Miller release_sock(sk); 1658db8dac20SDavid S. Miller 165960fb9567SPaolo Abeni if (static_branch_unlikely(&udpv6_encap_needed_key)) { 166060fb9567SPaolo Abeni if (up->encap_type) { 166144046a59STom Parkin void (*encap_destroy)(struct sock *sk); 16626aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 166344046a59STom Parkin if (encap_destroy) 166444046a59STom Parkin encap_destroy(sk); 166544046a59STom Parkin } 1666a4a600ddSXin Long if (up->encap_enabled) { 16679c480601SPaolo Abeni static_branch_dec(&udpv6_encap_needed_key); 1668a4a600ddSXin Long udp_encap_disable(); 1669a4a600ddSXin Long } 167060fb9567SPaolo Abeni } 1671db8dac20SDavid S. Miller } 1672db8dac20SDavid S. Miller 1673db8dac20SDavid S. Miller /* 1674db8dac20SDavid S. Miller * Socket option code for UDP 1675db8dac20SDavid S. Miller */ 1676a7b75c5aSChristoph Hellwig int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1677a7b75c5aSChristoph Hellwig unsigned int optlen) 1678db8dac20SDavid S. Miller { 16798a3854c7SPaolo Abeni if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET) 168091ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 1681a7b75c5aSChristoph Hellwig optval, optlen, 1682db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1683db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1684db8dac20SDavid S. Miller } 1685db8dac20SDavid S. Miller 1686db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1687db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1688db8dac20SDavid S. Miller { 1689db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1690db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1691db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1692db8dac20SDavid S. Miller } 1693db8dac20SDavid S. Miller 169411052589SKuniyuki Iwashima static const struct inet6_protocol udpv6_protocol = { 1695db8dac20SDavid S. Miller .handler = udpv6_rcv, 1696db8dac20SDavid S. Miller .err_handler = udpv6_err, 1697db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1698db8dac20SDavid S. Miller }; 1699db8dac20SDavid S. Miller 1700db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1701db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1702db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1703db8dac20SDavid S. Miller { 170417ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 170517ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 170617ef66afSLorenzo Colitti } else { 170717ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket; 170817ef66afSLorenzo Colitti struct inet_sock *inet = inet_sk(v); 170917ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport); 171017ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport); 17116c206b20SPaolo Abeni __ip6_dgram_sock_seq_show(seq, v, srcp, destp, 17126c206b20SPaolo Abeni udp_rqueue_get(v), bucket); 171317ef66afSLorenzo Colitti } 1714db8dac20SDavid S. Miller return 0; 1715db8dac20SDavid S. Miller } 1716db8dac20SDavid S. Miller 1717c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = { 1718a3d2599bSChristoph Hellwig .start = udp_seq_start, 1719a3d2599bSChristoph Hellwig .next = udp_seq_next, 1720a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 1721a3d2599bSChristoph Hellwig .show = udp6_seq_show, 1722a3d2599bSChristoph Hellwig }; 1723c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops); 172473cb88ecSArjan van de Ven 1725db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1726db8dac20SDavid S. Miller .family = AF_INET6, 1727645ca708SEric Dumazet .udp_table = &udp_table, 1728db8dac20SDavid S. Miller }; 1729db8dac20SDavid S. Miller 17302c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net) 1731db8dac20SDavid S. Miller { 1732c3506372SChristoph Hellwig if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops, 1733c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp6_seq_afinfo)) 1734a3d2599bSChristoph Hellwig return -ENOMEM; 1735a3d2599bSChristoph Hellwig return 0; 1736db8dac20SDavid S. Miller } 1737db8dac20SDavid S. Miller 1738ec120da6SIan Morris void udp6_proc_exit(struct net *net) 1739ec120da6SIan Morris { 1740a3d2599bSChristoph Hellwig remove_proc_entry("udp6", net->proc_net); 1741db8dac20SDavid S. Miller } 1742db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1743db8dac20SDavid S. Miller 1744db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1745db8dac20SDavid S. Miller 1746db8dac20SDavid S. Miller struct proto udpv6_prot = { 1747db8dac20SDavid S. Miller .name = "UDPv6", 1748db8dac20SDavid S. Miller .owner = THIS_MODULE, 1749db8dac20SDavid S. Miller .close = udp_lib_close, 1750d74bad4eSAndrey Ignatov .pre_connect = udpv6_pre_connect, 1751db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1752db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1753db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1754d38afeecSKuniyuki Iwashima .init = udpv6_init_sock, 1755db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1756db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1757db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1758db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1759db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 1760e646b657SMartin KaFai Lau .release_cb = ip6_datagram_release_cb, 1761db8dac20SDavid S. Miller .hash = udp_lib_hash, 1762db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1763719f8358SEric Dumazet .rehash = udp_v6_rehash, 1764db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 176591a760b2SMenglong Dong .put_port = udp_lib_unhash, 17668a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL 17678a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto, 17688a59f9d1SCong Wang #endif 17690defbb0aSEric Dumazet 1770db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 17710defbb0aSEric Dumazet .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc, 17720defbb0aSEric Dumazet 1773db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 17741e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 17751e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 1776db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1777*67fb4330SKuniyuki Iwashima .h.udp_table = NULL, 17785d77dca8SDavid Ahern .diag_destroy = udp_abort, 1779db8dac20SDavid S. Miller }; 1780db8dac20SDavid S. Miller 1781db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1782db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1783db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1784db8dac20SDavid S. Miller .prot = &udpv6_prot, 1785db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1786db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1787db8dac20SDavid S. Miller }; 1788db8dac20SDavid S. Miller 1789db8dac20SDavid S. Miller int __init udpv6_init(void) 1790db8dac20SDavid S. Miller { 1791db8dac20SDavid S. Miller int ret; 1792db8dac20SDavid S. Miller 17933336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 17943336288aSVlad Yasevich if (ret) 1795c6b641a4SVlad Yasevich goto out; 17963336288aSVlad Yasevich 1797db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1798db8dac20SDavid S. Miller if (ret) 1799db8dac20SDavid S. Miller goto out_udpv6_protocol; 1800db8dac20SDavid S. Miller out: 1801db8dac20SDavid S. Miller return ret; 1802db8dac20SDavid S. Miller 1803db8dac20SDavid S. Miller out_udpv6_protocol: 1804db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1805db8dac20SDavid S. Miller goto out; 1806db8dac20SDavid S. Miller } 1807db8dac20SDavid S. Miller 1808db8dac20SDavid S. Miller void udpv6_exit(void) 1809db8dac20SDavid S. Miller { 1810db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1811db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1812db8dac20SDavid S. Miller } 1813