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 20*aef2fedaSJakub 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> 44db8dac20SDavid S. Miller #include <net/tcp_states.h> 45db8dac20SDavid S. Miller #include <net/ip6_checksum.h> 46e7cc0824SStefano Brivio #include <net/ip6_tunnel.h> 47db8dac20SDavid S. Miller #include <net/xfrm.h> 480bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h> 4972289b96STom Herbert #include <net/inet6_hashtables.h> 50076bb0c8SEliezer Tamir #include <net/busy_poll.h> 51e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 52db8dac20SDavid S. Miller 53db8dac20SDavid S. Miller #include <linux/proc_fs.h> 54db8dac20SDavid S. Miller #include <linux/seq_file.h> 5522911fc5SEric Dumazet #include <trace/events/skb.h> 56db8dac20SDavid S. Miller #include "udp_impl.h" 57db8dac20SDavid S. Miller 586eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net, 59b50026b5SHannes Frederic Sowa const struct in6_addr *laddr, 60b50026b5SHannes Frederic Sowa const u16 lport, 61b50026b5SHannes Frederic Sowa const struct in6_addr *faddr, 62b50026b5SHannes Frederic Sowa const __be16 fport) 63b50026b5SHannes Frederic Sowa { 641bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly; 651bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly; 661bbdceefSHannes Frederic Sowa 671bbdceefSHannes Frederic Sowa u32 lhash, fhash; 681bbdceefSHannes Frederic Sowa 691bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret, 701bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret)); 711bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret, 721bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret)); 731bbdceefSHannes Frederic Sowa 741bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3]; 751bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 761bbdceefSHannes Frederic Sowa 77b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport, 781bbdceefSHannes Frederic Sowa udp_ipv6_hash_secret + net_hash_mix(net)); 79b50026b5SHannes Frederic Sowa } 80b50026b5SHannes Frederic Sowa 816ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 82db8dac20SDavid S. Miller { 8330fff923SEric Dumazet unsigned int hash2_nulladdr = 84f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 8530fff923SEric Dumazet unsigned int hash2_partial = 86f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 8730fff923SEric Dumazet 88d4cada4aSEric Dumazet /* precompute partial secondary hash */ 8930fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 90fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 91db8dac20SDavid S. Miller } 92db8dac20SDavid S. Miller 93f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk) 94719f8358SEric Dumazet { 95f0b1e64cSMartin KaFai Lau u16 new_hash = ipv6_portaddr_hash(sock_net(sk), 96efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr, 97719f8358SEric Dumazet inet_sk(sk)->inet_num); 98719f8358SEric Dumazet 99719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 100719f8358SEric Dumazet } 101719f8358SEric Dumazet 102d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 10388440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 104d1e37288SSu, Xuemin const struct in6_addr *daddr, unsigned short hnum, 10573545373STim Beale int dif, int sdif) 106db8dac20SDavid S. Miller { 10760c04aecSJoe Perches int score; 10860c04aecSJoe Perches struct inet_sock *inet; 1096da5b0f0SMike Manning bool dev_match; 110db8dac20SDavid S. Miller 11160c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 11260c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 11360c04aecSJoe Perches sk->sk_family != PF_INET6) 11460c04aecSJoe Perches return -1; 115645ca708SEric Dumazet 11623b0269eSPeter Oskolkov if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 11723b0269eSPeter Oskolkov return -1; 11823b0269eSPeter Oskolkov 119645ca708SEric Dumazet score = 0; 12060c04aecSJoe Perches inet = inet_sk(sk); 12160c04aecSJoe Perches 122c720c7e8SEric Dumazet if (inet->inet_dport) { 123c720c7e8SEric Dumazet if (inet->inet_dport != sport) 124645ca708SEric Dumazet return -1; 125db8dac20SDavid S. Miller score++; 126db8dac20SDavid S. Miller } 12760c04aecSJoe Perches 128efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 129efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 130645ca708SEric Dumazet return -1; 131db8dac20SDavid S. Miller score++; 132db8dac20SDavid S. Miller } 13360c04aecSJoe Perches 1346da5b0f0SMike Manning dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif); 13569678bcdSPaolo Abeni if (!dev_match) 136645ca708SEric Dumazet return -1; 1378d6c414cSMike Manning if (sk->sk_bound_dev_if) 138db8dac20SDavid S. Miller score++; 13960c04aecSJoe Perches 1407170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 14170da268bSEric Dumazet score++; 14270da268bSEric Dumazet 143645ca708SEric Dumazet return score; 144645ca708SEric Dumazet } 145645ca708SEric Dumazet 146a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk, 1472a08748cSJakub Sitnicki struct sk_buff *skb, 1482a08748cSJakub Sitnicki const struct in6_addr *saddr, 1492a08748cSJakub Sitnicki __be16 sport, 1502a08748cSJakub Sitnicki const struct in6_addr *daddr, 1512a08748cSJakub Sitnicki unsigned int hnum) 1522a08748cSJakub Sitnicki { 1532a08748cSJakub Sitnicki struct sock *reuse_sk = NULL; 1542a08748cSJakub Sitnicki u32 hash; 1552a08748cSJakub Sitnicki 1562a08748cSJakub Sitnicki if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) { 1572a08748cSJakub Sitnicki hash = udp6_ehashfn(net, daddr, hnum, saddr, sport); 1582a08748cSJakub Sitnicki reuse_sk = reuseport_select_sock(sk, hash, skb, 1592a08748cSJakub Sitnicki sizeof(struct udphdr)); 1602a08748cSJakub Sitnicki } 1612a08748cSJakub Sitnicki return reuse_sk; 1622a08748cSJakub Sitnicki } 1632a08748cSJakub Sitnicki 164d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 165fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net, 166fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 1671801b570SDavid Ahern const struct in6_addr *daddr, unsigned int hnum, 16873545373STim Beale int dif, int sdif, struct udp_hslot *hslot2, 16973545373STim Beale struct sk_buff *skb) 170fddc17deSEric Dumazet { 171fddc17deSEric Dumazet struct sock *sk, *result; 172e94a62f5SPaolo Abeni int score, badness; 173fddc17deSEric Dumazet 174fddc17deSEric Dumazet result = NULL; 175fddc17deSEric Dumazet badness = -1; 176ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 177d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 17873545373STim Beale daddr, hnum, dif, sdif); 179fddc17deSEric Dumazet if (score > badness) { 1802a08748cSJakub Sitnicki result = lookup_reuseport(net, sk, skb, 1812a08748cSJakub Sitnicki saddr, sport, daddr, hnum); 182a57066b1SDavid S. Miller /* Fall back to scoring if group has connections */ 183a57066b1SDavid S. Miller if (result && !reuseport_has_conns(sk, false)) 184ca065d0cSEric Dumazet return result; 1852a08748cSJakub Sitnicki 186a57066b1SDavid S. Miller result = result ? : sk; 187ca065d0cSEric Dumazet badness = score; 188fddc17deSEric Dumazet } 189fddc17deSEric Dumazet } 190fddc17deSEric Dumazet return result; 191fddc17deSEric Dumazet } 192fddc17deSEric Dumazet 1936d4201b1SJakub Sitnicki static inline struct sock *udp6_lookup_run_bpf(struct net *net, 1946d4201b1SJakub Sitnicki struct udp_table *udptable, 1956d4201b1SJakub Sitnicki struct sk_buff *skb, 1966d4201b1SJakub Sitnicki const struct in6_addr *saddr, 1976d4201b1SJakub Sitnicki __be16 sport, 1986d4201b1SJakub Sitnicki const struct in6_addr *daddr, 199f8931565SMark Pashmfouroush u16 hnum, const int dif) 2006d4201b1SJakub Sitnicki { 2016d4201b1SJakub Sitnicki struct sock *sk, *reuse_sk; 2026d4201b1SJakub Sitnicki bool no_reuseport; 2036d4201b1SJakub Sitnicki 2046d4201b1SJakub Sitnicki if (udptable != &udp_table) 2056d4201b1SJakub Sitnicki return NULL; /* only UDP is supported */ 2066d4201b1SJakub Sitnicki 207f8931565SMark Pashmfouroush no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP, saddr, sport, 208f8931565SMark Pashmfouroush daddr, hnum, dif, &sk); 2096d4201b1SJakub Sitnicki if (no_reuseport || IS_ERR_OR_NULL(sk)) 2106d4201b1SJakub Sitnicki return sk; 2116d4201b1SJakub Sitnicki 2126d4201b1SJakub Sitnicki reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum); 213c64c9c28SJakub Sitnicki if (reuse_sk) 2146d4201b1SJakub Sitnicki sk = reuse_sk; 2156d4201b1SJakub Sitnicki return sk; 2166d4201b1SJakub Sitnicki } 2176d4201b1SJakub Sitnicki 218ca065d0cSEric Dumazet /* rcu_read_lock() must be held */ 219fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net, 22088440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 22188440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 2221801b570SDavid Ahern int dif, int sdif, struct udp_table *udptable, 223538950a1SCraig Gallek struct sk_buff *skb) 224645ca708SEric Dumazet { 225645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 22623b0269eSPeter Oskolkov unsigned int hash2, slot2; 22723b0269eSPeter Oskolkov struct udp_hslot *hslot2; 2286d4201b1SJakub Sitnicki struct sock *result, *sk; 229645ca708SEric Dumazet 230f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum); 231fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 232fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 233fddc17deSEric Dumazet 2346d4201b1SJakub Sitnicki /* Lookup connected or non-wildcard sockets */ 235fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport, 23673545373STim Beale daddr, hnum, dif, sdif, 237d1e37288SSu, Xuemin hslot2, skb); 2386d4201b1SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED) 2396d4201b1SJakub Sitnicki goto done; 2406d4201b1SJakub Sitnicki 2416d4201b1SJakub Sitnicki /* Lookup redirect from BPF */ 2426d4201b1SJakub Sitnicki if (static_branch_unlikely(&bpf_sk_lookup_enabled)) { 2436d4201b1SJakub Sitnicki sk = udp6_lookup_run_bpf(net, udptable, skb, 244f8931565SMark Pashmfouroush saddr, sport, daddr, hnum, dif); 2456d4201b1SJakub Sitnicki if (sk) { 2466d4201b1SJakub Sitnicki result = sk; 2476d4201b1SJakub Sitnicki goto done; 2486d4201b1SJakub Sitnicki } 2496d4201b1SJakub Sitnicki } 2506d4201b1SJakub Sitnicki 2516d4201b1SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */ 2526d4201b1SJakub Sitnicki if (result) 2536d4201b1SJakub Sitnicki goto done; 2546d4201b1SJakub Sitnicki 2556d4201b1SJakub Sitnicki /* Lookup wildcard sockets */ 256f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum); 257fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 258fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 259fddc17deSEric Dumazet 2601223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport, 26123b0269eSPeter Oskolkov &in6addr_any, hnum, dif, sdif, 26273545373STim Beale hslot2, skb); 2636d4201b1SJakub Sitnicki done: 26426f8113cSEnrico Weigelt if (IS_ERR(result)) 2658217ca65SMartin KaFai Lau return NULL; 266fddc17deSEric Dumazet return result; 267fddc17deSEric Dumazet } 268fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 269db8dac20SDavid S. Miller 270607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 271607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 272645ca708SEric Dumazet struct udp_table *udptable) 273607c4aafSKOVACS Krisztian { 274b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 275607c4aafSKOVACS Krisztian 276ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 277607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 2781801b570SDavid Ahern inet6_sdif(skb), udptable, skb); 279607c4aafSKOVACS Krisztian } 280607c4aafSKOVACS Krisztian 2817b58e63eSEric Dumazet struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb, 28263058308STom Herbert __be16 sport, __be16 dport) 28363058308STom Herbert { 28463058308STom Herbert const struct ipv6hdr *iph = ipv6_hdr(skb); 28563058308STom Herbert 286ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 28763058308STom Herbert &iph->daddr, dport, inet6_iif(skb), 288257a525fSMartin KaFai Lau inet6_sdif(skb), &udp_table, NULL); 28963058308STom Herbert } 29063058308STom Herbert 291ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 292ca065d0cSEric Dumazet * Does increment socket refcount. 293ca065d0cSEric Dumazet */ 2946e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6) 295aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 296aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif) 297aa976fc0SBalazs Scheidler { 298ca065d0cSEric Dumazet struct sock *sk; 299ca065d0cSEric Dumazet 300ca065d0cSEric Dumazet sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, 3011801b570SDavid Ahern dif, 0, &udp_table, NULL); 30241c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 303ca065d0cSEric Dumazet sk = NULL; 304ca065d0cSEric Dumazet return sk; 305aa976fc0SBalazs Scheidler } 306aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup); 307ca065d0cSEric Dumazet #endif 308aa976fc0SBalazs Scheidler 309cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the 310cb891fa6SPaolo Abeni * scratch area space; note that the IP6CB flags is still in the first 311cb891fa6SPaolo Abeni * cacheline, so checking for jumbograms is cheap 312cb891fa6SPaolo Abeni */ 313cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb) 314cb891fa6SPaolo Abeni { 315cb891fa6SPaolo Abeni return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb); 316cb891fa6SPaolo Abeni } 317cb891fa6SPaolo Abeni 318db8dac20SDavid S. Miller /* 319db8dac20SDavid S. Miller * This should be easy, if there is something there we 320db8dac20SDavid S. Miller * return it, otherwise we block. 321db8dac20SDavid S. Miller */ 322db8dac20SDavid S. Miller 3231b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 324db8dac20SDavid S. Miller int noblock, int flags, int *addr_len) 325db8dac20SDavid S. Miller { 326db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 327db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 328db8dac20SDavid S. Miller struct sk_buff *skb; 32959c2cdaeSDavid S. Miller unsigned int ulen, copied; 330fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK; 331db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 332543fc3fbSPaolo Abeni struct udp_mib __percpu *mib; 333197c949eSEric Dumazet bool checksum_valid = false; 334f26ba175SWei Yongjun int is_udp4; 335db8dac20SDavid S. Miller 336db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 33785fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 338db8dac20SDavid S. Miller 3394b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 34085fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 3414b340ae2SBrian Haley 342db8dac20SDavid S. Miller try_again: 343a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 344fd69c399SPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &off, &err); 345db8dac20SDavid S. Miller if (!skb) 346627d2d6bSsamanthakumar return err; 347db8dac20SDavid S. Miller 348cb891fa6SPaolo Abeni ulen = udp6_skb_len(skb); 34959c2cdaeSDavid S. Miller copied = len; 350627d2d6bSsamanthakumar if (copied > ulen - off) 351627d2d6bSsamanthakumar copied = ulen - off; 35259c2cdaeSDavid S. Miller else if (copied < ulen) 353db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 354db8dac20SDavid S. Miller 355f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 356029a3743SPaolo Abeni mib = __UDPX_MIB(sk, is_udp4); 357f26ba175SWei Yongjun 358db8dac20SDavid S. Miller /* 359db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 360db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 361db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 362db8dac20SDavid S. Miller */ 363db8dac20SDavid S. Miller 364d21dbdfeSEric Dumazet if (copied < ulen || peeking || 365d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 36667a51780SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 36767a51780SPaolo Abeni !__udp_lib_checksum_complete(skb); 368197c949eSEric Dumazet if (!checksum_valid) 369db8dac20SDavid S. Miller goto csum_copy_err; 370db8dac20SDavid S. Miller } 371db8dac20SDavid S. Miller 37267a51780SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 37367a51780SPaolo Abeni if (udp_skb_is_linear(skb)) 37467a51780SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 37567a51780SPaolo Abeni else 376627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 37767a51780SPaolo Abeni } else { 378627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 379db8dac20SDavid S. Miller if (err == -EINVAL) 380db8dac20SDavid S. Miller goto csum_copy_err; 381db8dac20SDavid S. Miller } 38222911fc5SEric Dumazet if (unlikely(err)) { 383fd69c399SPaolo Abeni if (!peeking) { 384979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 385029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 386979402b1SEric Dumazet } 387850cbaddSPaolo Abeni kfree_skb(skb); 388627d2d6bSsamanthakumar return err; 38922911fc5SEric Dumazet } 390fd69c399SPaolo Abeni if (!peeking) 391029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS); 392db8dac20SDavid S. Miller 3933b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 394db8dac20SDavid S. Miller 395db8dac20SDavid S. Miller /* Copy the address. */ 396db8dac20SDavid S. Miller if (msg->msg_name) { 397342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 398db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 399db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 400db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 401db8dac20SDavid S. Miller 402842df073SHannes Frederic Sowa if (is_udp4) { 403b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 404b301e82cSBrian Haley &sin6->sin6_addr); 405842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0; 406842df073SHannes Frederic Sowa } else { 4074e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 408842df073SHannes Frederic Sowa sin6->sin6_scope_id = 409842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr, 4104330487aSDuan Jiong inet6_iif(skb)); 411db8dac20SDavid S. Miller } 412bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 413983695faSDaniel Borkmann 414983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk, 415983695faSDaniel Borkmann (struct sockaddr *)sin6); 416db8dac20SDavid S. Miller } 4174b261c75SHannes Frederic Sowa 418bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 419bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 420bcd1665eSPaolo Abeni 4214b261c75SHannes Frederic Sowa if (np->rxopt.all) 4224b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb); 4234b261c75SHannes Frederic Sowa 424f26ba175SWei Yongjun if (is_udp4) { 425db8dac20SDavid S. Miller if (inet->cmsg_flags) 426ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, 42710df8e61SEric Dumazet sizeof(struct udphdr), off); 428db8dac20SDavid S. Miller } else { 429db8dac20SDavid S. Miller if (np->rxopt.all) 4304b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb); 431db8dac20SDavid S. Miller } 432db8dac20SDavid S. Miller 43359c2cdaeSDavid S. Miller err = copied; 434db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 435db8dac20SDavid S. Miller err = ulen; 436db8dac20SDavid S. Miller 437850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 438db8dac20SDavid S. Miller return err; 439db8dac20SDavid S. Miller 440db8dac20SDavid S. Miller csum_copy_err: 4412276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 4422276f58aSPaolo Abeni udp_skb_destructor)) { 443029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS); 444029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 4456a5dc9e5SEric Dumazet } 446850cbaddSPaolo Abeni kfree_skb(skb); 447db8dac20SDavid S. Miller 448beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 449beb39db5SEric Dumazet cond_resched(); 4509cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 451db8dac20SDavid S. Miller goto try_again; 452db8dac20SDavid S. Miller } 453db8dac20SDavid S. Miller 454a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 455a36e185eSStefano Brivio void udpv6_encap_enable(void) 456a36e185eSStefano Brivio { 4579c480601SPaolo Abeni static_branch_inc(&udpv6_encap_needed_key); 458a36e185eSStefano Brivio } 459a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable); 460a36e185eSStefano Brivio 461e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 462e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 463e7cc0824SStefano Brivio */ 464e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb, 465e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 466424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info) 467e7cc0824SStefano Brivio { 468e7cc0824SStefano Brivio int i; 469e7cc0824SStefano Brivio 470e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 471e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 472424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info); 473424a7cd0SPaolo Abeni const struct ip6_tnl_encap_ops *encap; 474e7cc0824SStefano Brivio 475424a7cd0SPaolo Abeni encap = rcu_dereference(ip6tun_encaps[i]); 476424a7cd0SPaolo Abeni if (!encap) 477e7cc0824SStefano Brivio continue; 478424a7cd0SPaolo Abeni handler = encap->err_handler; 479e7cc0824SStefano Brivio if (handler && !handler(skb, opt, type, code, offset, info)) 480e7cc0824SStefano Brivio return 0; 481e7cc0824SStefano Brivio } 482e7cc0824SStefano Brivio 483e7cc0824SStefano Brivio return -ENOENT; 484e7cc0824SStefano Brivio } 485e7cc0824SStefano Brivio 486a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 487a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 488a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 489a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 490a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 491a36e185eSStefano Brivio * trace ICMP messages back to them. 492a36e185eSStefano Brivio * 493e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 494e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 495e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 496e7cc0824SStefano Brivio * 497a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 498a36e185eSStefano Brivio * association. 499a36e185eSStefano Brivio * 500e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 501e7cc0824SStefano Brivio * processing, zero otherwise. 502a36e185eSStefano Brivio */ 503a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net, 504a36e185eSStefano Brivio const struct ipv6hdr *hdr, int offset, 505a36e185eSStefano Brivio struct udphdr *uh, 506a36e185eSStefano Brivio struct udp_table *udptable, 5079bfce73cSVadim Fedorenko struct sock *sk, 508e7cc0824SStefano Brivio struct sk_buff *skb, 509e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 510e7cc0824SStefano Brivio u8 type, u8 code, __be32 info) 511a36e185eSStefano Brivio { 5129bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb); 513a36e185eSStefano Brivio int network_offset, transport_offset; 5149bfce73cSVadim Fedorenko struct udp_sock *up; 515a36e185eSStefano Brivio 516a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 517a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 518a36e185eSStefano Brivio 519a36e185eSStefano Brivio /* Network header needs to point to the outer IPv6 header inside ICMP */ 520a36e185eSStefano Brivio skb_reset_network_header(skb); 521a36e185eSStefano Brivio 522a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 523a36e185eSStefano Brivio skb_set_transport_header(skb, offset); 524a36e185eSStefano Brivio 5259bfce73cSVadim Fedorenko if (sk) { 5269bfce73cSVadim Fedorenko up = udp_sk(sk); 5279bfce73cSVadim Fedorenko 5289bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup); 5299bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb)) 5309bfce73cSVadim Fedorenko sk = NULL; 5319bfce73cSVadim Fedorenko 5329bfce73cSVadim Fedorenko goto out; 5339bfce73cSVadim Fedorenko } 5349bfce73cSVadim Fedorenko 535e7cc0824SStefano Brivio sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source, 536e7cc0824SStefano Brivio &hdr->saddr, uh->dest, 537e7cc0824SStefano Brivio inet6_iif(skb), 0, udptable, skb); 538e7cc0824SStefano Brivio if (sk) { 5399bfce73cSVadim Fedorenko up = udp_sk(sk); 540e7cc0824SStefano Brivio 541a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 542a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 543a36e185eSStefano Brivio sk = NULL; 544e7cc0824SStefano Brivio } 545e7cc0824SStefano Brivio 5469bfce73cSVadim Fedorenko out: 547e7cc0824SStefano Brivio if (!sk) { 548e7cc0824SStefano Brivio sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code, 549e7cc0824SStefano Brivio offset, info)); 550e7cc0824SStefano Brivio } 551a36e185eSStefano Brivio 552a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 553a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 554e7cc0824SStefano Brivio 555a36e185eSStefano Brivio return sk; 556a36e185eSStefano Brivio } 557a36e185eSStefano Brivio 55832bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 559d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 560645ca708SEric Dumazet struct udp_table *udptable) 561db8dac20SDavid S. Miller { 562db8dac20SDavid S. Miller struct ipv6_pinfo *np; 563b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 564b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr; 565b71d1d42SEric Dumazet const struct in6_addr *daddr = &hdr->daddr; 566db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset); 567a36e185eSStefano Brivio bool tunnel = false; 568db8dac20SDavid S. Miller struct sock *sk; 569e0d8c1b7SWei Wang int harderr; 570db8dac20SDavid S. Miller int err; 5717304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 572db8dac20SDavid S. Miller 573e32ea7e7SCraig Gallek sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source, 5744ac30c4bSMartin KaFai Lau inet6_iif(skb), inet6_sdif(skb), udptable, NULL); 5759bfce73cSVadim Fedorenko 576d26796aeSXin Long if (!sk || udp_sk(sk)->encap_type) { 577a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 578a36e185eSStefano Brivio if (static_branch_unlikely(&udpv6_encap_needed_key)) { 579a36e185eSStefano Brivio sk = __udp6_lib_err_encap(net, hdr, offset, uh, 5809bfce73cSVadim Fedorenko udptable, sk, skb, 581e7cc0824SStefano Brivio opt, type, code, info); 582e7cc0824SStefano Brivio if (!sk) 583e7cc0824SStefano Brivio return 0; 5849bfce73cSVadim Fedorenko } else 5859bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT); 586a36e185eSStefano Brivio 587e7cc0824SStefano Brivio if (IS_ERR(sk)) { 588a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), 5897304fe46SDuan Jiong ICMP6_MIB_INERRORS); 590e7cc0824SStefano Brivio return PTR_ERR(sk); 5917304fe46SDuan Jiong } 592e7cc0824SStefano Brivio 593a36e185eSStefano Brivio tunnel = true; 594a36e185eSStefano Brivio } 595db8dac20SDavid S. Miller 596e0d8c1b7SWei Wang harderr = icmpv6_err_convert(type, code, &err); 597e0d8c1b7SWei Wang np = inet6_sk(sk); 598e0d8c1b7SWei Wang 59993b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) { 60093b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk)) 60193b36cf3SHannes Frederic Sowa goto out; 60281aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 603e0d8c1b7SWei Wang if (np->pmtudisc != IPV6_PMTUDISC_DONT) 604e0d8c1b7SWei Wang harderr = 1; 60593b36cf3SHannes Frederic Sowa } 6061a462d18SDuan Jiong if (type == NDISC_REDIRECT) { 607a36e185eSStefano Brivio if (tunnel) { 608a36e185eSStefano Brivio ip6_redirect(skb, sock_net(sk), inet6_iif(skb), 609a36e185eSStefano Brivio sk->sk_mark, sk->sk_uid); 610a36e185eSStefano Brivio } else { 611ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 612a36e185eSStefano Brivio } 6131a462d18SDuan Jiong goto out; 6141a462d18SDuan Jiong } 61581aded24SDavid S. Miller 616a36e185eSStefano Brivio /* Tunnels don't have an application socket: don't pass errors back */ 617a36e185eSStefano Brivio if (tunnel) 618a36e185eSStefano Brivio goto out; 619a36e185eSStefano Brivio 620e0d8c1b7SWei Wang if (!np->recverr) { 621e0d8c1b7SWei Wang if (!harderr || sk->sk_state != TCP_ESTABLISHED) 622db8dac20SDavid S. Miller goto out; 623e0d8c1b7SWei Wang } else { 624db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 625e0d8c1b7SWei Wang } 626b1faf566SEric Dumazet 627db8dac20SDavid S. Miller sk->sk_err = err; 628e3ae2365SAlexander Aring sk_error_report(sk); 629db8dac20SDavid S. Miller out: 63032bbd879SStefano Brivio return 0; 631db8dac20SDavid S. Miller } 632db8dac20SDavid S. Miller 633a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 634f7ad74feSBenjamin LaHaise { 635f7ad74feSBenjamin LaHaise int rc; 636f7ad74feSBenjamin LaHaise 637efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 638f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb); 639005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 6402c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 641e68b6e50SEric Dumazet } else { 642e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 643005ec974SShawn Bohrer } 644f7ad74feSBenjamin LaHaise 645850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 646f7ad74feSBenjamin LaHaise if (rc < 0) { 647f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk); 648f7ad74feSBenjamin LaHaise 649f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */ 650f7ad74feSBenjamin LaHaise if (rc == -ENOMEM) 651e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), 652f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite); 653a3ce2b10SMenglong Dong else 654a3ce2b10SMenglong Dong UDP6_INC_STATS(sock_net(sk), 655a3ce2b10SMenglong Dong UDP_MIB_MEMERRORS, is_udplite); 656e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 657f7ad74feSBenjamin LaHaise kfree_skb(skb); 658f7ad74feSBenjamin LaHaise return -1; 659f7ad74feSBenjamin LaHaise } 660850cbaddSPaolo Abeni 661f7ad74feSBenjamin LaHaise return 0; 662f7ad74feSBenjamin LaHaise } 663f7ad74feSBenjamin LaHaise 66432bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb, 665d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 666d5fdd6baSBrian Haley u8 code, int offset, __be32 info) 667db8dac20SDavid S. Miller { 66832bbd879SStefano Brivio return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 669db8dac20SDavid S. Miller } 670db8dac20SDavid S. Miller 671cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 672db8dac20SDavid S. Miller { 673db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 674db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 675db8dac20SDavid S. Miller 676db8dac20SDavid S. Miller if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 677db8dac20SDavid S. Miller goto drop; 678db8dac20SDavid S. Miller 67988ab3108SDavidlohr Bueso if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) { 680d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 681d7f3f621SBenjamin LaHaise 682d7f3f621SBenjamin LaHaise /* 683d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to 684d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just 685d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket. 686d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value: 687d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap 688d7f3f621SBenjamin LaHaise * handler or was discarded by it. 689d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP. 690d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N 691d7f3f621SBenjamin LaHaise */ 692d7f3f621SBenjamin LaHaise 693d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */ 6946aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 695e5aed006SHannes Frederic Sowa if (encap_rcv) { 696d7f3f621SBenjamin LaHaise int ret; 697d7f3f621SBenjamin LaHaise 6980a80966bSTom Herbert /* Verify checksum before giving to encap */ 6990a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 7000a80966bSTom Herbert goto csum_error; 7010a80966bSTom Herbert 702d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb); 703d7f3f621SBenjamin LaHaise if (ret <= 0) { 704250962e4SMenglong Dong __UDP6_INC_STATS(sock_net(sk), 705d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS, 706d7f3f621SBenjamin LaHaise is_udplite); 707d7f3f621SBenjamin LaHaise return -ret; 708d7f3f621SBenjamin LaHaise } 709d7f3f621SBenjamin LaHaise } 710d7f3f621SBenjamin LaHaise 711d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */ 712d7f3f621SBenjamin LaHaise } 713d7f3f621SBenjamin LaHaise 714db8dac20SDavid S. Miller /* 715db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 716db8dac20SDavid S. Miller */ 717b0a42277SMiaohe Lin if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 718db8dac20SDavid S. Miller 719db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 720ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 721db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 722db8dac20SDavid S. Miller goto drop; 723db8dac20SDavid S. Miller } 724db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 725ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 726db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 727db8dac20SDavid S. Miller goto drop; 728db8dac20SDavid S. Miller } 729db8dac20SDavid S. Miller } 730db8dac20SDavid S. Miller 7314b943faeSPaolo Abeni prefetch(&sk->sk_rmem_alloc); 732ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 733ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 7346a5dc9e5SEric Dumazet goto csum_error; 735ce25d66aSEric Dumazet 736ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 737a6127697SMichal Kubeček goto drop; 738db8dac20SDavid S. Miller 739e6afc8acSsamanthakumar udp_csum_pull_header(skb); 740cb80ef46SBenjamin LaHaise 741d826eb14SEric Dumazet skb_dst_drop(skb); 742db8dac20SDavid S. Miller 743850cbaddSPaolo Abeni return __udpv6_queue_rcv_skb(sk, skb); 7443e215c8dSJames M Leddy 7456a5dc9e5SEric Dumazet csum_error: 74602c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 747db8dac20SDavid S. Miller drop: 74802c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 749cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops); 750db8dac20SDavid S. Miller kfree_skb(skb); 751db8dac20SDavid S. Miller return -1; 752db8dac20SDavid S. Miller } 753db8dac20SDavid S. Miller 754cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 755cf329aa4SPaolo Abeni { 756cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 757cf329aa4SPaolo Abeni int ret; 758cf329aa4SPaolo Abeni 759cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 760cf329aa4SPaolo Abeni return udpv6_queue_rcv_one_skb(sk, skb); 761cf329aa4SPaolo Abeni 762cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 763cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, false); 7641a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) { 765cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 766cf329aa4SPaolo Abeni 767000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb); 768cf329aa4SPaolo Abeni ret = udpv6_queue_rcv_one_skb(sk, skb); 769cf329aa4SPaolo Abeni if (ret > 0) 770cf329aa4SPaolo Abeni ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret, 771cf329aa4SPaolo Abeni true); 772cf329aa4SPaolo Abeni } 773cf329aa4SPaolo Abeni return 0; 774cf329aa4SPaolo Abeni } 775cf329aa4SPaolo Abeni 7765cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, 777b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr, 778b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr, 7797bd2db40SDewi Morgan int dif, int sdif, unsigned short hnum) 780db8dac20SDavid S. Miller { 7819e89fd8bSSven Wegener struct inet_sock *inet = inet_sk(sk); 782db8dac20SDavid S. Miller 7839e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net)) 7845cf3d461SDavid Held return false; 785b8ad0cbcSDaniel Lezcano 7865cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum || 7875cf3d461SDavid Held sk->sk_family != PF_INET6 || 7885cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) || 7895cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) && 7905cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 7917bd2db40SDewi Morgan !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) || 79233b4b015SHenning Rogge (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && 79333b4b015SHenning Rogge !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) 7945cf3d461SDavid Held return false; 7959e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 7965cf3d461SDavid Held return false; 7975cf3d461SDavid Held return true; 798db8dac20SDavid S. Miller } 799db8dac20SDavid S. Miller 8004068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb) 8014068579eSTom Herbert { 8024068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log 8034068579eSTom Herbert * this error. Well, it is reasonable. 8044068579eSTom Herbert */ 805ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 8064068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 8074068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 8084068579eSTom Herbert } 8094068579eSTom Herbert 810db8dac20SDavid S. Miller /* 811db8dac20SDavid S. Miller * Note: called only from the BH handler context, 812db8dac20SDavid S. Miller * so we don't need to lock the hashes. 813db8dac20SDavid S. Miller */ 814e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 815b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr, 81636cbb245SRick Jones struct udp_table *udptable, int proto) 817db8dac20SDavid S. Miller { 818ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 819db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 8205cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 8215cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 822ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 8232dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 824ca065d0cSEric Dumazet int dif = inet6_iif(skb); 8257bd2db40SDewi Morgan int sdif = inet6_sdif(skb); 826ca065d0cSEric Dumazet struct hlist_node *node; 827ca065d0cSEric Dumazet struct sk_buff *nskb; 8282dc41cffSDavid Held 8292dc41cffSDavid Held if (use_hash2) { 830f0b1e64cSMartin KaFai Lau hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) & 83173e2d5e3SPablo Neira udptable->mask; 832f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask; 8332dc41cffSDavid Held start_lookup: 83473e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 8352dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 8362dc41cffSDavid Held } 837db8dac20SDavid S. Miller 838ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 839ca065d0cSEric Dumazet if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr, 8407bd2db40SDewi Morgan uh->source, saddr, dif, sdif, 8417bd2db40SDewi Morgan hnum)) 842ca065d0cSEric Dumazet continue; 8431c19448cSTom Herbert /* If zero checksum and no_check is not on for 8444068579eSTom Herbert * the socket then skip it. 8454068579eSTom Herbert */ 846ca065d0cSEric Dumazet if (!uh->check && !udp_sk(sk)->no_check6_rx) 847ca065d0cSEric Dumazet continue; 848ca065d0cSEric Dumazet if (!first) { 849ca065d0cSEric Dumazet first = sk; 850ca065d0cSEric Dumazet continue; 851db8dac20SDavid S. Miller } 852ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 853ca065d0cSEric Dumazet if (unlikely(!nskb)) { 854ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 85502c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 856ca065d0cSEric Dumazet IS_UDPLITE(sk)); 85702c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, 858ca065d0cSEric Dumazet IS_UDPLITE(sk)); 859ca065d0cSEric Dumazet continue; 860a1ab77f9SEric Dumazet } 861db8dac20SDavid S. Miller 862ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(sk, nskb) > 0) 863ca065d0cSEric Dumazet consume_skb(nskb); 864ca065d0cSEric Dumazet } 865a1ab77f9SEric Dumazet 8662dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 8672dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 8682dc41cffSDavid Held hash2 = hash2_any; 8692dc41cffSDavid Held goto start_lookup; 8702dc41cffSDavid Held } 8712dc41cffSDavid Held 872ca065d0cSEric Dumazet if (first) { 873ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(first, skb) > 0) 874ca065d0cSEric Dumazet consume_skb(skb); 875a1ab77f9SEric Dumazet } else { 876ca065d0cSEric Dumazet kfree_skb(skb); 87702c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 87836cbb245SRick Jones proto == IPPROTO_UDPLITE); 879a1ab77f9SEric Dumazet } 880db8dac20SDavid S. Miller return 0; 881db8dac20SDavid S. Miller } 882db8dac20SDavid S. Miller 88364f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 88464f0f5d1SPaolo Abeni { 88564f0f5d1SPaolo Abeni if (udp_sk_rx_dst_set(sk, dst)) { 88664f0f5d1SPaolo Abeni const struct rt6_info *rt = (const struct rt6_info *)dst; 88764f0f5d1SPaolo Abeni 888ef57c161SEric Dumazet sk->sk_rx_dst_cookie = rt6_get_cookie(rt); 88964f0f5d1SPaolo Abeni } 89064f0f5d1SPaolo Abeni } 89164f0f5d1SPaolo Abeni 892eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 893eb63f296SPaolo Abeni * return code conversion for ip layer consumption 894eb63f296SPaolo Abeni */ 895eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 896eb63f296SPaolo Abeni struct udphdr *uh) 897eb63f296SPaolo Abeni { 898eb63f296SPaolo Abeni int ret; 899eb63f296SPaolo Abeni 900eb63f296SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 901e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo); 902eb63f296SPaolo Abeni 903eb63f296SPaolo Abeni ret = udpv6_queue_rcv_skb(sk, skb); 904eb63f296SPaolo Abeni 90584dad559SPaolo Abeni /* a return value > 0 means to resubmit the input */ 906eb63f296SPaolo Abeni if (ret > 0) 90784dad559SPaolo Abeni return ret; 908eb63f296SPaolo Abeni return 0; 909eb63f296SPaolo Abeni } 910eb63f296SPaolo Abeni 911645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 912db8dac20SDavid S. Miller int proto) 913db8dac20SDavid S. Miller { 914b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 915ca065d0cSEric Dumazet struct net *net = dev_net(skb->dev); 916ca065d0cSEric Dumazet struct udphdr *uh; 917ca065d0cSEric Dumazet struct sock *sk; 91871489e21SJoe Stringer bool refcounted; 919db8dac20SDavid S. Miller u32 ulen = 0; 920db8dac20SDavid S. Miller 921db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 922d6bc0149SBjørn Mork goto discard; 923db8dac20SDavid S. Miller 924db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 925db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 926db8dac20SDavid S. Miller uh = udp_hdr(skb); 927db8dac20SDavid S. Miller 928db8dac20SDavid S. Miller ulen = ntohs(uh->len); 929db8dac20SDavid S. Miller if (ulen > skb->len) 930db8dac20SDavid S. Miller goto short_packet; 931db8dac20SDavid S. Miller 932db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 933db8dac20SDavid S. Miller /* UDP validates ulen. */ 934db8dac20SDavid S. Miller 935db8dac20SDavid S. Miller /* Check for jumbo payload */ 936db8dac20SDavid S. Miller if (ulen == 0) 937db8dac20SDavid S. Miller ulen = skb->len; 938db8dac20SDavid S. Miller 939db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 940db8dac20SDavid S. Miller goto short_packet; 941db8dac20SDavid S. Miller 942db8dac20SDavid S. Miller if (ulen < skb->len) { 943db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 944db8dac20SDavid S. Miller goto short_packet; 945db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 946db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 947db8dac20SDavid S. Miller uh = udp_hdr(skb); 948db8dac20SDavid S. Miller } 949db8dac20SDavid S. Miller } 950db8dac20SDavid S. Miller 951db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 9526a5dc9e5SEric Dumazet goto csum_error; 953db8dac20SDavid S. Miller 954c9f2c1aeSPaolo Abeni /* Check if the socket is already available, e.g. due to early demux */ 95571489e21SJoe Stringer sk = skb_steal_sock(skb, &refcounted); 956c9f2c1aeSPaolo Abeni if (sk) { 957c9f2c1aeSPaolo Abeni struct dst_entry *dst = skb_dst(skb); 958c9f2c1aeSPaolo Abeni int ret; 959c9f2c1aeSPaolo Abeni 960c9f2c1aeSPaolo Abeni if (unlikely(sk->sk_rx_dst != dst)) 96164f0f5d1SPaolo Abeni udp6_sk_rx_dst_set(sk, dst); 962c9f2c1aeSPaolo Abeni 963eb63f296SPaolo Abeni if (!uh->check && !udp_sk(sk)->no_check6_rx) { 96471489e21SJoe Stringer if (refcounted) 965c9f2c1aeSPaolo Abeni sock_put(sk); 966eb63f296SPaolo Abeni goto report_csum_error; 967eb63f296SPaolo Abeni } 968c9f2c1aeSPaolo Abeni 969eb63f296SPaolo Abeni ret = udp6_unicast_rcv_skb(sk, skb, uh); 97071489e21SJoe Stringer if (refcounted) 971eb63f296SPaolo Abeni sock_put(sk); 972c9f2c1aeSPaolo Abeni return ret; 973c9f2c1aeSPaolo Abeni } 974c9f2c1aeSPaolo Abeni 975db8dac20SDavid S. Miller /* 976db8dac20SDavid S. Miller * Multicast receive code 977db8dac20SDavid S. Miller */ 978db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 979e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 98036cbb245SRick Jones saddr, daddr, udptable, proto); 981db8dac20SDavid S. Miller 982db8dac20SDavid S. Miller /* Unicast */ 983607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 98453b24b8fSIan Morris if (sk) { 985eb63f296SPaolo Abeni if (!uh->check && !udp_sk(sk)->no_check6_rx) 986eb63f296SPaolo Abeni goto report_csum_error; 987eb63f296SPaolo Abeni return udp6_unicast_rcv_skb(sk, skb, uh); 9884068579eSTom Herbert } 9894068579eSTom Herbert 990eb63f296SPaolo Abeni if (!uh->check) 991eb63f296SPaolo Abeni goto report_csum_error; 9924068579eSTom Herbert 993db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 994db8dac20SDavid S. Miller goto discard; 995db8dac20SDavid S. Miller 996db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 9976a5dc9e5SEric Dumazet goto csum_error; 998db8dac20SDavid S. Miller 99902c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 10003ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 1001db8dac20SDavid S. Miller 1002db8dac20SDavid S. Miller kfree_skb(skb); 1003db8dac20SDavid S. Miller return 0; 1004db8dac20SDavid S. Miller 1005db8dac20SDavid S. Miller short_packet: 1006ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 1007db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 1008ba7a46f1SJoe Perches saddr, ntohs(uh->source), 1009ba7a46f1SJoe Perches ulen, skb->len, 1010ba7a46f1SJoe Perches daddr, ntohs(uh->dest)); 10116a5dc9e5SEric Dumazet goto discard; 1012eb63f296SPaolo Abeni 1013eb63f296SPaolo Abeni report_csum_error: 1014eb63f296SPaolo Abeni udp6_csum_zero_error(skb); 10156a5dc9e5SEric Dumazet csum_error: 101602c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 1017db8dac20SDavid S. Miller discard: 101802c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1019db8dac20SDavid S. Miller kfree_skb(skb); 1020db8dac20SDavid S. Miller return 0; 1021db8dac20SDavid S. Miller } 1022db8dac20SDavid S. Miller 10230bd84065Ssubashab@codeaurora.org 10245425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net, 10255425077dSsubashab@codeaurora.org __be16 loc_port, const struct in6_addr *loc_addr, 10265425077dSsubashab@codeaurora.org __be16 rmt_port, const struct in6_addr *rmt_addr, 10274297a0efSDavid Ahern int dif, int sdif) 10285425077dSsubashab@codeaurora.org { 10290bd84065Ssubashab@codeaurora.org unsigned short hnum = ntohs(loc_port); 1030f0b1e64cSMartin KaFai Lau unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum); 10310bd84065Ssubashab@codeaurora.org unsigned int slot2 = hash2 & udp_table.mask; 10320bd84065Ssubashab@codeaurora.org struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 10330bd84065Ssubashab@codeaurora.org const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 10345425077dSsubashab@codeaurora.org struct sock *sk; 10355425077dSsubashab@codeaurora.org 10360bd84065Ssubashab@codeaurora.org udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 103785cb73ffSWei Wang if (sk->sk_state == TCP_ESTABLISHED && 10384297a0efSDavid Ahern INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif)) 10395425077dSsubashab@codeaurora.org return sk; 10400bd84065Ssubashab@codeaurora.org /* Only check first socket in chain */ 10410bd84065Ssubashab@codeaurora.org break; 10420bd84065Ssubashab@codeaurora.org } 10430bd84065Ssubashab@codeaurora.org return NULL; 10445425077dSsubashab@codeaurora.org } 10455425077dSsubashab@codeaurora.org 104697ff7ffbSPaolo Abeni INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb) 10475425077dSsubashab@codeaurora.org { 10485425077dSsubashab@codeaurora.org struct net *net = dev_net(skb->dev); 10495425077dSsubashab@codeaurora.org const struct udphdr *uh; 10505425077dSsubashab@codeaurora.org struct sock *sk; 10515425077dSsubashab@codeaurora.org struct dst_entry *dst; 10525425077dSsubashab@codeaurora.org int dif = skb->dev->ifindex; 10534297a0efSDavid Ahern int sdif = inet6_sdif(skb); 10545425077dSsubashab@codeaurora.org 10555425077dSsubashab@codeaurora.org if (!pskb_may_pull(skb, skb_transport_offset(skb) + 10565425077dSsubashab@codeaurora.org sizeof(struct udphdr))) 10575425077dSsubashab@codeaurora.org return; 10585425077dSsubashab@codeaurora.org 10595425077dSsubashab@codeaurora.org uh = udp_hdr(skb); 10605425077dSsubashab@codeaurora.org 10615425077dSsubashab@codeaurora.org if (skb->pkt_type == PACKET_HOST) 10625425077dSsubashab@codeaurora.org sk = __udp6_lib_demux_lookup(net, uh->dest, 10635425077dSsubashab@codeaurora.org &ipv6_hdr(skb)->daddr, 10645425077dSsubashab@codeaurora.org uh->source, &ipv6_hdr(skb)->saddr, 10654297a0efSDavid Ahern dif, sdif); 10665425077dSsubashab@codeaurora.org else 10675425077dSsubashab@codeaurora.org return; 10685425077dSsubashab@codeaurora.org 106941c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 10705425077dSsubashab@codeaurora.org return; 10715425077dSsubashab@codeaurora.org 10725425077dSsubashab@codeaurora.org skb->sk = sk; 10735425077dSsubashab@codeaurora.org skb->destructor = sock_efree; 10745425077dSsubashab@codeaurora.org dst = READ_ONCE(sk->sk_rx_dst); 10755425077dSsubashab@codeaurora.org 10765425077dSsubashab@codeaurora.org if (dst) 1077ef57c161SEric Dumazet dst = dst_check(dst, sk->sk_rx_dst_cookie); 10785425077dSsubashab@codeaurora.org if (dst) { 1079d24406c8SWei Wang /* set noref for now. 1080d24406c8SWei Wang * any place which wants to hold dst has to call 1081d24406c8SWei Wang * dst_hold_safe() 1082d24406c8SWei Wang */ 10835425077dSsubashab@codeaurora.org skb_dst_set_noref(skb, dst); 10845425077dSsubashab@codeaurora.org } 10855425077dSsubashab@codeaurora.org } 10865425077dSsubashab@codeaurora.org 10870e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb) 1088db8dac20SDavid S. Miller { 1089645ca708SEric Dumazet return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1090db8dac20SDavid S. Miller } 1091db8dac20SDavid S. Miller 1092db8dac20SDavid S. Miller /* 1093db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 1094db8dac20SDavid S. Miller */ 1095db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 1096db8dac20SDavid S. Miller { 1097db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1098db8dac20SDavid S. Miller 109936d926b9SDenis V. Lunev if (up->pending == AF_INET) 110036d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 110136d926b9SDenis V. Lunev else if (up->pending) { 1102db8dac20SDavid S. Miller up->len = 0; 1103db8dac20SDavid S. Miller up->pending = 0; 1104db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 1105db8dac20SDavid S. Miller } 1106db8dac20SDavid S. Miller } 1107db8dac20SDavid S. Miller 1108d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr, 1109d74bad4eSAndrey Ignatov int addr_len) 1110d74bad4eSAndrey Ignatov { 1111bddc028aSTetsuo Handa if (addr_len < offsetofend(struct sockaddr, sa_family)) 1112bddc028aSTetsuo Handa return -EINVAL; 1113d74bad4eSAndrey Ignatov /* The following checks are replicated from __ip6_datagram_connect() 1114d74bad4eSAndrey Ignatov * and intended to prevent BPF program called below from accessing 1115d74bad4eSAndrey Ignatov * bytes that are out of the bound specified by user in addr_len. 1116d74bad4eSAndrey Ignatov */ 1117d74bad4eSAndrey Ignatov if (uaddr->sa_family == AF_INET) { 1118d74bad4eSAndrey Ignatov if (__ipv6_only_sock(sk)) 1119d74bad4eSAndrey Ignatov return -EAFNOSUPPORT; 1120d74bad4eSAndrey Ignatov return udp_pre_connect(sk, uaddr, addr_len); 1121d74bad4eSAndrey Ignatov } 1122d74bad4eSAndrey Ignatov 1123d74bad4eSAndrey Ignatov if (addr_len < SIN6_LEN_RFC2133) 1124d74bad4eSAndrey Ignatov return -EINVAL; 1125d74bad4eSAndrey Ignatov 1126d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr); 1127d74bad4eSAndrey Ignatov } 1128d74bad4eSAndrey Ignatov 1129493c6be3SSridhar Samudrala /** 1130493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 1131493c6be3SSridhar Samudrala * @sk: socket we are sending on 1132493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 1133493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 1134b51cd7c8SAndrew Lunn * @saddr: source address 1135b51cd7c8SAndrew Lunn * @daddr: destination address 1136b51cd7c8SAndrew Lunn * @len: length of packet 1137493c6be3SSridhar Samudrala */ 1138493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 1139493c6be3SSridhar Samudrala const struct in6_addr *saddr, 1140493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 1141493c6be3SSridhar Samudrala { 1142493c6be3SSridhar Samudrala unsigned int offset; 1143493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 1144d39d938cSVlad Yasevich struct sk_buff *frags = skb_shinfo(skb)->frag_list; 1145493c6be3SSridhar Samudrala __wsum csum = 0; 1146493c6be3SSridhar Samudrala 1147d39d938cSVlad Yasevich if (!frags) { 1148493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 1149493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 1150493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 1151493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 1152493c6be3SSridhar Samudrala } else { 1153493c6be3SSridhar Samudrala /* 1154493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 1155493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 1156493c6be3SSridhar Samudrala * should be together 1157493c6be3SSridhar Samudrala */ 1158493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 1159493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 116063ecc3d9SSubash Abhinov Kasiviswanathan csum = skb->csum; 1161493c6be3SSridhar Samudrala 1162493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1163493c6be3SSridhar Samudrala 1164d39d938cSVlad Yasevich do { 1165d39d938cSVlad Yasevich csum = csum_add(csum, frags->csum); 1166d39d938cSVlad Yasevich } while ((frags = frags->next)); 1167493c6be3SSridhar Samudrala 1168493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1169493c6be3SSridhar Samudrala csum); 1170493c6be3SSridhar Samudrala if (uh->check == 0) 1171493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 1172493c6be3SSridhar Samudrala } 1173493c6be3SSridhar Samudrala } 1174493c6be3SSridhar Samudrala 1175db8dac20SDavid S. Miller /* 1176db8dac20SDavid S. Miller * Sending 1177db8dac20SDavid S. Miller */ 1178db8dac20SDavid S. Miller 1179bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6, 1180bec1f6f6SWillem de Bruijn struct inet_cork *cork) 1181db8dac20SDavid S. Miller { 1182d39d938cSVlad Yasevich struct sock *sk = skb->sk; 1183db8dac20SDavid S. Miller struct udphdr *uh; 1184db8dac20SDavid S. Miller int err = 0; 1185db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1186db8dac20SDavid S. Miller __wsum csum = 0; 1187d39d938cSVlad Yasevich int offset = skb_transport_offset(skb); 1188d39d938cSVlad Yasevich int len = skb->len - offset; 11894094871dSJosh Hunt int datalen = len - sizeof(*uh); 1190db8dac20SDavid S. Miller 1191db8dac20SDavid S. Miller /* 1192db8dac20SDavid S. Miller * Create a UDP header 1193db8dac20SDavid S. Miller */ 1194db8dac20SDavid S. Miller uh = udp_hdr(skb); 11951958b856SDavid S. Miller uh->source = fl6->fl6_sport; 11961958b856SDavid S. Miller uh->dest = fl6->fl6_dport; 1197d39d938cSVlad Yasevich uh->len = htons(len); 1198db8dac20SDavid S. Miller uh->check = 0; 1199db8dac20SDavid S. Miller 1200bec1f6f6SWillem de Bruijn if (cork->gso_size) { 1201bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 1202bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 1203bec1f6f6SWillem de Bruijn 12040f149c9fSWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) { 12050f149c9fSWillem de Bruijn kfree_skb(skb); 1206bec1f6f6SWillem de Bruijn return -EINVAL; 12070f149c9fSWillem de Bruijn } 12080f149c9fSWillem de Bruijn if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) { 12090f149c9fSWillem de Bruijn kfree_skb(skb); 1210bec1f6f6SWillem de Bruijn return -EINVAL; 12110f149c9fSWillem de Bruijn } 12120f149c9fSWillem de Bruijn if (udp_sk(sk)->no_check6_tx) { 12130f149c9fSWillem de Bruijn kfree_skb(skb); 1214a8c744a8SWillem de Bruijn return -EINVAL; 12150f149c9fSWillem de Bruijn } 1216ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 12170f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) { 12180f149c9fSWillem de Bruijn kfree_skb(skb); 1219bec1f6f6SWillem de Bruijn return -EIO; 12200f149c9fSWillem de Bruijn } 1221bec1f6f6SWillem de Bruijn 12224094871dSJosh Hunt if (datalen > cork->gso_size) { 1223bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 1224bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 12254094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen, 122644b321e5SJosh Hunt cork->gso_size); 12274094871dSJosh Hunt } 1228a8c744a8SWillem de Bruijn goto csum_partial; 1229bec1f6f6SWillem de Bruijn } 1230bec1f6f6SWillem de Bruijn 1231db8dac20SDavid S. Miller if (is_udplite) 1232d39d938cSVlad Yasevich csum = udplite_csum(skb); 1233d39d938cSVlad Yasevich else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */ 12344068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE; 12354068579eSTom Herbert goto send; 12364068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1237a8c744a8SWillem de Bruijn csum_partial: 1238d39d938cSVlad Yasevich udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len); 1239493c6be3SSridhar Samudrala goto send; 1240493c6be3SSridhar Samudrala } else 1241d39d938cSVlad Yasevich csum = udp_csum(skb); 1242db8dac20SDavid S. Miller 1243db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 12444c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 1245d39d938cSVlad Yasevich len, fl6->flowi6_proto, csum); 1246db8dac20SDavid S. Miller if (uh->check == 0) 1247db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 1248db8dac20SDavid S. Miller 1249493c6be3SSridhar Samudrala send: 1250d39d938cSVlad Yasevich err = ip6_send_skb(skb); 12516ce9e7b5SEric Dumazet if (err) { 12526ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 12536aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12546ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 12556ce9e7b5SEric Dumazet err = 0; 12566ce9e7b5SEric Dumazet } 12576aef70a8SEric Dumazet } else { 12586aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12596ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 12606aef70a8SEric Dumazet } 1261d39d938cSVlad Yasevich return err; 1262d39d938cSVlad Yasevich } 1263d39d938cSVlad Yasevich 1264d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk) 1265d39d938cSVlad Yasevich { 1266d39d938cSVlad Yasevich struct sk_buff *skb; 1267d39d938cSVlad Yasevich struct udp_sock *up = udp_sk(sk); 1268d39d938cSVlad Yasevich struct flowi6 fl6; 1269d39d938cSVlad Yasevich int err = 0; 1270d39d938cSVlad Yasevich 1271d39d938cSVlad Yasevich if (up->pending == AF_INET) 1272d39d938cSVlad Yasevich return udp_push_pending_frames(sk); 1273d39d938cSVlad Yasevich 1274d39d938cSVlad Yasevich /* ip6_finish_skb will release the cork, so make a copy of 1275d39d938cSVlad Yasevich * fl6 here. 1276d39d938cSVlad Yasevich */ 1277d39d938cSVlad Yasevich fl6 = inet_sk(sk)->cork.fl.u.ip6; 1278d39d938cSVlad Yasevich 1279d39d938cSVlad Yasevich skb = ip6_finish_skb(sk); 1280d39d938cSVlad Yasevich if (!skb) 1281d39d938cSVlad Yasevich goto out; 1282d39d938cSVlad Yasevich 1283bec1f6f6SWillem de Bruijn err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base); 1284d39d938cSVlad Yasevich 1285db8dac20SDavid S. Miller out: 1286db8dac20SDavid S. Miller up->len = 0; 1287db8dac20SDavid S. Miller up->pending = 0; 1288db8dac20SDavid S. Miller return err; 1289db8dac20SDavid S. Miller } 1290db8dac20SDavid S. Miller 12911b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1292db8dac20SDavid S. Miller { 1293db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 1294db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1295db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1296db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 1297342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 129820c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 1299db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 130045f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 1301db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 13024c9483b2SDavid S. Miller struct flowi6 fl6; 1303db8dac20SDavid S. Miller struct dst_entry *dst; 130426879da5SWei Wang struct ipcm6_cookie ipc6; 1305db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 13069f542f61SAlexey Kodanev bool connected = false; 1307db8dac20SDavid S. Miller int ulen = len; 1308a9f59707SEric Dumazet int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE; 1309db8dac20SDavid S. Miller int err; 1310db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1311db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1312db8dac20SDavid S. Miller 1313b515430aSWillem de Bruijn ipcm6_init(&ipc6); 131418a419baSEric Dumazet ipc6.gso_size = READ_ONCE(up->gso_size); 13155fdaa88dSWillem de Bruijn ipc6.sockc.tsflags = sk->sk_tsflags; 1316c6af0c22SWillem de Bruijn ipc6.sockc.mark = sk->sk_mark; 131726879da5SWei Wang 1318db8dac20SDavid S. Miller /* destination address check */ 1319db8dac20SDavid S. Miller if (sin6) { 1320db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 1321db8dac20SDavid S. Miller return -EINVAL; 1322db8dac20SDavid S. Miller 1323db8dac20SDavid S. Miller switch (sin6->sin6_family) { 1324db8dac20SDavid S. Miller case AF_INET6: 1325db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 1326db8dac20SDavid S. Miller return -EINVAL; 1327db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1328052d2369SJonathan T. Leighton if (ipv6_addr_any(daddr) && 1329052d2369SJonathan T. Leighton ipv6_addr_v4mapped(&np->saddr)) 1330052d2369SJonathan T. Leighton ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 1331052d2369SJonathan T. Leighton daddr); 1332db8dac20SDavid S. Miller break; 1333db8dac20SDavid S. Miller case AF_INET: 1334db8dac20SDavid S. Miller goto do_udp_sendmsg; 1335db8dac20SDavid S. Miller case AF_UNSPEC: 1336db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 1337db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 1338db8dac20SDavid S. Miller daddr = NULL; 1339db8dac20SDavid S. Miller break; 1340db8dac20SDavid S. Miller default: 1341db8dac20SDavid S. Miller return -EINVAL; 1342db8dac20SDavid S. Miller } 1343db8dac20SDavid S. Miller } else if (!up->pending) { 1344db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1345db8dac20SDavid S. Miller return -EDESTADDRREQ; 1346efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1347db8dac20SDavid S. Miller } else 1348db8dac20SDavid S. Miller daddr = NULL; 1349db8dac20SDavid S. Miller 1350db8dac20SDavid S. Miller if (daddr) { 1351db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 1352db8dac20SDavid S. Miller struct sockaddr_in sin; 1353db8dac20SDavid S. Miller sin.sin_family = AF_INET; 1354c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1355db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1356db8dac20SDavid S. Miller msg->msg_name = &sin; 1357db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 1358db8dac20SDavid S. Miller do_udp_sendmsg: 1359db8dac20SDavid S. Miller if (__ipv6_only_sock(sk)) 1360db8dac20SDavid S. Miller return -ENETUNREACH; 13611b784140SYing Xue return udp_sendmsg(sk, msg, len); 1362db8dac20SDavid S. Miller } 1363db8dac20SDavid S. Miller } 1364db8dac20SDavid S. Miller 1365db8dac20SDavid S. Miller if (up->pending == AF_INET) 13661b784140SYing Xue return udp_sendmsg(sk, msg, len); 1367db8dac20SDavid S. Miller 1368db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 1369db8dac20SDavid S. Miller better check is made in ip6_append_data(). 1370db8dac20SDavid S. Miller */ 1371db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 1372db8dac20SDavid S. Miller return -EMSGSIZE; 1373db8dac20SDavid S. Miller 137403485f2aSVlad Yasevich getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1375db8dac20SDavid S. Miller if (up->pending) { 1376db8dac20SDavid S. Miller /* 1377db8dac20SDavid S. Miller * There are pending frames. 1378db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1379db8dac20SDavid S. Miller */ 1380db8dac20SDavid S. Miller lock_sock(sk); 1381db8dac20SDavid S. Miller if (likely(up->pending)) { 1382db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 1383db8dac20SDavid S. Miller release_sock(sk); 1384db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1385db8dac20SDavid S. Miller } 1386db8dac20SDavid S. Miller dst = NULL; 1387db8dac20SDavid S. Miller goto do_append_data; 1388db8dac20SDavid S. Miller } 1389db8dac20SDavid S. Miller release_sock(sk); 1390db8dac20SDavid S. Miller } 1391db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1392db8dac20SDavid S. Miller 13934c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1394db8dac20SDavid S. Miller 1395db8dac20SDavid S. Miller if (sin6) { 1396db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 1397db8dac20SDavid S. Miller return -EINVAL; 1398db8dac20SDavid S. Miller 13991958b856SDavid S. Miller fl6.fl6_dport = sin6->sin6_port; 1400db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1401db8dac20SDavid S. Miller 1402db8dac20SDavid S. Miller if (np->sndflow) { 14034c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 14044c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 14054c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 140659c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 1407db8dac20SDavid S. Miller return -EINVAL; 1408db8dac20SDavid S. Miller } 1409db8dac20SDavid S. Miller } 1410db8dac20SDavid S. Miller 1411db8dac20SDavid S. Miller /* 1412db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 1413db8dac20SDavid S. Miller * sk->sk_dst_cache. 1414db8dac20SDavid S. Miller */ 1415db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 1416efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1417efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1418db8dac20SDavid S. Miller 1419db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 1420db8dac20SDavid S. Miller sin6->sin6_scope_id && 1421842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 14224c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 1423db8dac20SDavid S. Miller } else { 1424db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1425db8dac20SDavid S. Miller return -EDESTADDRREQ; 1426db8dac20SDavid S. Miller 14271958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 1428efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 14294c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 14309f542f61SAlexey Kodanev connected = true; 1431db8dac20SDavid S. Miller } 1432db8dac20SDavid S. Miller 14334c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 14344c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 1435db8dac20SDavid S. Miller 14364c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 14374c9483b2SDavid S. Miller fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 14389f690db7SYang Hongyang 1439e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 144051953d5bSBrian Haley 1441db8dac20SDavid S. Miller if (msg->msg_controllen) { 1442db8dac20SDavid S. Miller opt = &opt_space; 1443db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 1444db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 144526879da5SWei Wang ipc6.opt = opt; 1446db8dac20SDavid S. Miller 14472e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc6.gso_size); 14482e8de857SWillem de Bruijn if (err > 0) 14492e8de857SWillem de Bruijn err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, 14505fdaa88dSWillem de Bruijn &ipc6); 1451db8dac20SDavid S. Miller if (err < 0) { 1452db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1453db8dac20SDavid S. Miller return err; 1454db8dac20SDavid S. Miller } 14554c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 14564c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 145759c820b2SWillem de Bruijn if (IS_ERR(flowlabel)) 1458db8dac20SDavid S. Miller return -EINVAL; 1459db8dac20SDavid S. Miller } 1460db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 1461db8dac20SDavid S. Miller opt = NULL; 14629f542f61SAlexey Kodanev connected = false; 1463db8dac20SDavid S. Miller } 146445f6fad8SEric Dumazet if (!opt) { 146545f6fad8SEric Dumazet opt = txopt_get(np); 146645f6fad8SEric Dumazet opt_to_free = opt; 146745f6fad8SEric Dumazet } 1468db8dac20SDavid S. Miller if (flowlabel) 1469db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 1470db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 147126879da5SWei Wang ipc6.opt = opt; 1472db8dac20SDavid S. Miller 14734c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 147442dcfd85SJakub Kicinski fl6.flowi6_mark = ipc6.sockc.mark; 14754e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 14764c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 14774e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 14781958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 1479db8dac20SDavid S. Miller 14806fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) { 14811cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, 14821cedee13SAndrey Ignatov (struct sockaddr *)sin6, &fl6.saddr); 14831cedee13SAndrey Ignatov if (err) 14841cedee13SAndrey Ignatov goto out_no_dst; 14851cedee13SAndrey Ignatov if (sin6) { 14861cedee13SAndrey Ignatov if (ipv6_addr_v4mapped(&sin6->sin6_addr)) { 14871cedee13SAndrey Ignatov /* BPF program rewrote IPv6-only by IPv4-mapped 14881cedee13SAndrey Ignatov * IPv6. It's currently unsupported. 14891cedee13SAndrey Ignatov */ 14901cedee13SAndrey Ignatov err = -ENOTSUPP; 14911cedee13SAndrey Ignatov goto out_no_dst; 14921cedee13SAndrey Ignatov } 14931cedee13SAndrey Ignatov if (sin6->sin6_port == 0) { 14941cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 14951cedee13SAndrey Ignatov err = -EINVAL; 14961cedee13SAndrey Ignatov goto out_no_dst; 14971cedee13SAndrey Ignatov } 14981cedee13SAndrey Ignatov fl6.fl6_dport = sin6->sin6_port; 14991cedee13SAndrey Ignatov fl6.daddr = sin6->sin6_addr; 15001cedee13SAndrey Ignatov } 15011cedee13SAndrey Ignatov } 15021cedee13SAndrey Ignatov 1503e8e36984SAndrey Ignatov if (ipv6_addr_any(&fl6.daddr)) 1504e8e36984SAndrey Ignatov fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 1505e8e36984SAndrey Ignatov 15064c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 150720c59de2SArnaud Ebalard if (final_p) 15089f542f61SAlexey Kodanev connected = false; 1509db8dac20SDavid S. Miller 15104c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) { 15114c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 15129f542f61SAlexey Kodanev connected = false; 1513c4062dfcSErich E. Hoover } else if (!fl6.flowi6_oif) 1514c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 1515db8dac20SDavid S. Miller 15163df98d79SPaul Moore security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 1517db8dac20SDavid S. Miller 151838b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 151938b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 152038b7097bSHannes Frederic Sowa 152138b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 152238b7097bSHannes Frederic Sowa 15234f858c56SAlexey Kodanev dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected); 152468d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 152568d0c6d3SDavid S. Miller err = PTR_ERR(dst); 152668d0c6d3SDavid S. Miller dst = NULL; 1527db8dac20SDavid S. Miller goto out; 1528db8dac20SDavid S. Miller } 1529db8dac20SDavid S. Miller 153026879da5SWei Wang if (ipc6.hlimit < 0) 153126879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 1532db8dac20SDavid S. Miller 1533db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1534db8dac20SDavid S. Miller goto do_confirm; 1535db8dac20SDavid S. Miller back_from_confirm: 1536db8dac20SDavid S. Miller 153703485f2aSVlad Yasevich /* Lockless fast path for the non-corking case */ 153803485f2aSVlad Yasevich if (!corkreq) { 15391cd7884dSWillem de Bruijn struct inet_cork_full cork; 154003485f2aSVlad Yasevich struct sk_buff *skb; 154103485f2aSVlad Yasevich 154203485f2aSVlad Yasevich skb = ip6_make_skb(sk, getfrag, msg, ulen, 154326879da5SWei Wang sizeof(struct udphdr), &ipc6, 154403485f2aSVlad Yasevich &fl6, (struct rt6_info *)dst, 15455fdaa88dSWillem de Bruijn msg->msg_flags, &cork); 154603485f2aSVlad Yasevich err = PTR_ERR(skb); 154703485f2aSVlad Yasevich if (!IS_ERR_OR_NULL(skb)) 1548bec1f6f6SWillem de Bruijn err = udp_v6_send_skb(skb, &fl6, &cork.base); 15494f858c56SAlexey Kodanev goto out; 155003485f2aSVlad Yasevich } 155103485f2aSVlad Yasevich 1552db8dac20SDavid S. Miller lock_sock(sk); 1553db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1554db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1555db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1556db8dac20SDavid S. Miller release_sock(sk); 1557db8dac20SDavid S. Miller 1558ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n"); 1559db8dac20SDavid S. Miller err = -EINVAL; 1560db8dac20SDavid S. Miller goto out; 1561db8dac20SDavid S. Miller } 1562db8dac20SDavid S. Miller 1563db8dac20SDavid S. Miller up->pending = AF_INET6; 1564db8dac20SDavid S. Miller 1565db8dac20SDavid S. Miller do_append_data: 156626879da5SWei Wang if (ipc6.dontfrag < 0) 156726879da5SWei Wang ipc6.dontfrag = np->dontfrag; 1568db8dac20SDavid S. Miller up->len += ulen; 156926879da5SWei Wang err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 157026879da5SWei Wang &ipc6, &fl6, (struct rt6_info *)dst, 15715fdaa88dSWillem de Bruijn corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1572db8dac20SDavid S. Miller if (err) 1573db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1574db8dac20SDavid S. Miller else if (!corkreq) 1575db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 1576db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1577db8dac20SDavid S. Miller up->pending = 0; 1578db8dac20SDavid S. Miller 157903485f2aSVlad Yasevich if (err > 0) 158003485f2aSVlad Yasevich err = np->recverr ? net_xmit_errno(err) : 0; 158103485f2aSVlad Yasevich release_sock(sk); 158203485f2aSVlad Yasevich 1583db8dac20SDavid S. Miller out: 1584a3c96089SYOSHIFUJI Hideaki dst_release(dst); 15851cedee13SAndrey Ignatov out_no_dst: 1586db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 158745f6fad8SEric Dumazet txopt_put(opt_to_free); 1588db8dac20SDavid S. Miller if (!err) 1589db8dac20SDavid S. Miller return len; 1590db8dac20SDavid S. Miller /* 1591db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1592db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1593db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1594db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1595db8dac20SDavid S. Miller * seems like overkill. 1596db8dac20SDavid S. Miller */ 1597db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 15986aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 1599235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1600db8dac20SDavid S. Miller } 1601db8dac20SDavid S. Miller return err; 1602db8dac20SDavid S. Miller 1603db8dac20SDavid S. Miller do_confirm: 16040dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 16050dec879fSJulian Anastasov dst_confirm_neigh(dst, &fl6.daddr); 1606db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1607db8dac20SDavid S. Miller goto back_from_confirm; 1608db8dac20SDavid S. Miller err = 0; 1609db8dac20SDavid S. Miller goto out; 1610db8dac20SDavid S. Miller } 1611db8dac20SDavid S. Miller 16127d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1613db8dac20SDavid S. Miller { 161444046a59STom Parkin struct udp_sock *up = udp_sk(sk); 1615db8dac20SDavid S. Miller lock_sock(sk); 1616a8b897c7SPaolo Abeni 1617a8b897c7SPaolo Abeni /* protects from races with udp_abort() */ 1618a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD); 1619db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1620db8dac20SDavid S. Miller release_sock(sk); 1621db8dac20SDavid S. Miller 162260fb9567SPaolo Abeni if (static_branch_unlikely(&udpv6_encap_needed_key)) { 162360fb9567SPaolo Abeni if (up->encap_type) { 162444046a59STom Parkin void (*encap_destroy)(struct sock *sk); 16256aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 162644046a59STom Parkin if (encap_destroy) 162744046a59STom Parkin encap_destroy(sk); 162844046a59STom Parkin } 1629a4a600ddSXin Long if (up->encap_enabled) { 16309c480601SPaolo Abeni static_branch_dec(&udpv6_encap_needed_key); 1631a4a600ddSXin Long udp_encap_disable(); 1632a4a600ddSXin Long } 163360fb9567SPaolo Abeni } 163444046a59STom Parkin 1635db8dac20SDavid S. Miller inet6_destroy_sock(sk); 1636db8dac20SDavid S. Miller } 1637db8dac20SDavid S. Miller 1638db8dac20SDavid S. Miller /* 1639db8dac20SDavid S. Miller * Socket option code for UDP 1640db8dac20SDavid S. Miller */ 1641a7b75c5aSChristoph Hellwig int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1642a7b75c5aSChristoph Hellwig unsigned int optlen) 1643db8dac20SDavid S. Miller { 1644db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 164591ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname, 1646a7b75c5aSChristoph Hellwig optval, optlen, 1647db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1648db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1649db8dac20SDavid S. Miller } 1650db8dac20SDavid S. Miller 1651db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1652db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1653db8dac20SDavid S. Miller { 1654db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1655db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1656db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1657db8dac20SDavid S. Miller } 1658db8dac20SDavid S. Miller 1659a8e3bb34SDavid Ahern /* thinking of making this const? Don't. 1660a8e3bb34SDavid Ahern * early_demux can change based on sysctl. 1661a8e3bb34SDavid Ahern */ 1662dddb64bcSsubashab@codeaurora.org static struct inet6_protocol udpv6_protocol = { 16635425077dSsubashab@codeaurora.org .early_demux = udp_v6_early_demux, 1664dddb64bcSsubashab@codeaurora.org .early_demux_handler = udp_v6_early_demux, 1665db8dac20SDavid S. Miller .handler = udpv6_rcv, 1666db8dac20SDavid S. Miller .err_handler = udpv6_err, 1667db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1668db8dac20SDavid S. Miller }; 1669db8dac20SDavid S. Miller 1670db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1671db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1672db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1673db8dac20SDavid S. Miller { 167417ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 167517ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 167617ef66afSLorenzo Colitti } else { 167717ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket; 167817ef66afSLorenzo Colitti struct inet_sock *inet = inet_sk(v); 167917ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport); 168017ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport); 16816c206b20SPaolo Abeni __ip6_dgram_sock_seq_show(seq, v, srcp, destp, 16826c206b20SPaolo Abeni udp_rqueue_get(v), bucket); 168317ef66afSLorenzo Colitti } 1684db8dac20SDavid S. Miller return 0; 1685db8dac20SDavid S. Miller } 1686db8dac20SDavid S. Miller 1687c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = { 1688a3d2599bSChristoph Hellwig .start = udp_seq_start, 1689a3d2599bSChristoph Hellwig .next = udp_seq_next, 1690a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 1691a3d2599bSChristoph Hellwig .show = udp6_seq_show, 1692a3d2599bSChristoph Hellwig }; 1693c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops); 169473cb88ecSArjan van de Ven 1695db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1696db8dac20SDavid S. Miller .family = AF_INET6, 1697645ca708SEric Dumazet .udp_table = &udp_table, 1698db8dac20SDavid S. Miller }; 1699db8dac20SDavid S. Miller 17002c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net) 1701db8dac20SDavid S. Miller { 1702c3506372SChristoph Hellwig if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops, 1703c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp6_seq_afinfo)) 1704a3d2599bSChristoph Hellwig return -ENOMEM; 1705a3d2599bSChristoph Hellwig return 0; 1706db8dac20SDavid S. Miller } 1707db8dac20SDavid S. Miller 1708ec120da6SIan Morris void udp6_proc_exit(struct net *net) 1709ec120da6SIan Morris { 1710a3d2599bSChristoph Hellwig remove_proc_entry("udp6", net->proc_net); 1711db8dac20SDavid S. Miller } 1712db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1713db8dac20SDavid S. Miller 1714db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1715db8dac20SDavid S. Miller 1716db8dac20SDavid S. Miller struct proto udpv6_prot = { 1717db8dac20SDavid S. Miller .name = "UDPv6", 1718db8dac20SDavid S. Miller .owner = THIS_MODULE, 1719db8dac20SDavid S. Miller .close = udp_lib_close, 1720d74bad4eSAndrey Ignatov .pre_connect = udpv6_pre_connect, 1721db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1722db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1723db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1724850cbaddSPaolo Abeni .init = udp_init_sock, 1725db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1726db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1727db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1728db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1729db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 1730e646b657SMartin KaFai Lau .release_cb = ip6_datagram_release_cb, 1731db8dac20SDavid S. Miller .hash = udp_lib_hash, 1732db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1733719f8358SEric Dumazet .rehash = udp_v6_rehash, 1734db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 17358a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL 17368a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto, 17378a59f9d1SCong Wang #endif 1738db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1739db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 17401e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 17411e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 1742db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1743645ca708SEric Dumazet .h.udp_table = &udp_table, 17445d77dca8SDavid Ahern .diag_destroy = udp_abort, 1745db8dac20SDavid S. Miller }; 1746db8dac20SDavid S. Miller 1747db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1748db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1749db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1750db8dac20SDavid S. Miller .prot = &udpv6_prot, 1751db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1752db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1753db8dac20SDavid S. Miller }; 1754db8dac20SDavid S. Miller 1755db8dac20SDavid S. Miller int __init udpv6_init(void) 1756db8dac20SDavid S. Miller { 1757db8dac20SDavid S. Miller int ret; 1758db8dac20SDavid S. Miller 17593336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 17603336288aSVlad Yasevich if (ret) 1761c6b641a4SVlad Yasevich goto out; 17623336288aSVlad Yasevich 1763db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1764db8dac20SDavid S. Miller if (ret) 1765db8dac20SDavid S. Miller goto out_udpv6_protocol; 1766db8dac20SDavid S. Miller out: 1767db8dac20SDavid S. Miller return ret; 1768db8dac20SDavid S. Miller 1769db8dac20SDavid S. Miller out_udpv6_protocol: 1770db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1771db8dac20SDavid S. Miller goto out; 1772db8dac20SDavid S. Miller } 1773db8dac20SDavid S. Miller 1774db8dac20SDavid S. Miller void udpv6_exit(void) 1775db8dac20SDavid S. Miller { 1776db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1777db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1778db8dac20SDavid S. Miller } 1779