1db8dac20SDavid S. Miller /* 2db8dac20SDavid S. Miller * UDP over IPv6 3db8dac20SDavid S. Miller * Linux INET6 implementation 4db8dac20SDavid S. Miller * 5db8dac20SDavid S. Miller * Authors: 6db8dac20SDavid S. Miller * Pedro Roque <roque@di.fc.ul.pt> 7db8dac20SDavid S. Miller * 8db8dac20SDavid S. Miller * Based on linux/ipv4/udp.c 9db8dac20SDavid S. Miller * 10db8dac20SDavid S. Miller * Fixes: 11db8dac20SDavid S. Miller * Hideaki YOSHIFUJI : sin6_scope_id support 12db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 13db8dac20SDavid S. Miller * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 14db8dac20SDavid S. Miller * a single port at the same time. 15db8dac20SDavid S. Miller * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 16db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. 17db8dac20SDavid S. Miller * 18db8dac20SDavid S. Miller * This program is free software; you can redistribute it and/or 19db8dac20SDavid S. Miller * modify it under the terms of the GNU General Public License 20db8dac20SDavid S. Miller * as published by the Free Software Foundation; either version 21db8dac20SDavid S. Miller * 2 of the License, or (at your option) any later version. 22db8dac20SDavid S. Miller */ 23db8dac20SDavid S. Miller 24db8dac20SDavid S. Miller #include <linux/errno.h> 25db8dac20SDavid S. Miller #include <linux/types.h> 26db8dac20SDavid S. Miller #include <linux/socket.h> 27db8dac20SDavid S. Miller #include <linux/sockios.h> 28db8dac20SDavid S. Miller #include <linux/net.h> 29db8dac20SDavid S. Miller #include <linux/in6.h> 30db8dac20SDavid S. Miller #include <linux/netdevice.h> 31db8dac20SDavid S. Miller #include <linux/if_arp.h> 32db8dac20SDavid S. Miller #include <linux/ipv6.h> 33db8dac20SDavid S. Miller #include <linux/icmpv6.h> 34db8dac20SDavid S. Miller #include <linux/init.h> 35db8dac20SDavid S. Miller #include <linux/module.h> 36db8dac20SDavid S. Miller #include <linux/skbuff.h> 375a0e3ad6STejun Heo #include <linux/slab.h> 387c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 39db8dac20SDavid S. Miller 40496611d7SCraig Gallek #include <net/addrconf.h> 41db8dac20SDavid S. Miller #include <net/ndisc.h> 42db8dac20SDavid S. Miller #include <net/protocol.h> 43db8dac20SDavid S. Miller #include <net/transp_v6.h> 44db8dac20SDavid S. Miller #include <net/ip6_route.h> 45db8dac20SDavid S. Miller #include <net/raw.h> 46db8dac20SDavid S. Miller #include <net/tcp_states.h> 47db8dac20SDavid S. Miller #include <net/ip6_checksum.h> 48e7cc0824SStefano Brivio #include <net/ip6_tunnel.h> 49db8dac20SDavid S. Miller #include <net/xfrm.h> 500bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h> 5172289b96STom Herbert #include <net/inet6_hashtables.h> 52076bb0c8SEliezer Tamir #include <net/busy_poll.h> 53e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 54db8dac20SDavid S. Miller 55db8dac20SDavid S. Miller #include <linux/proc_fs.h> 56db8dac20SDavid S. Miller #include <linux/seq_file.h> 5722911fc5SEric Dumazet #include <trace/events/skb.h> 58db8dac20SDavid S. Miller #include "udp_impl.h" 59db8dac20SDavid S. Miller 6063a6fff3SRobert Shearman static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb) 6163a6fff3SRobert Shearman { 6263a6fff3SRobert Shearman #if defined(CONFIG_NET_L3_MASTER_DEV) 6363a6fff3SRobert Shearman if (!net->ipv4.sysctl_udp_l3mdev_accept && 6463a6fff3SRobert Shearman skb && ipv6_l3mdev_skb(IP6CB(skb)->flags)) 6563a6fff3SRobert Shearman return true; 6663a6fff3SRobert Shearman #endif 6763a6fff3SRobert Shearman return false; 6863a6fff3SRobert Shearman } 6963a6fff3SRobert Shearman 706eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net, 71b50026b5SHannes Frederic Sowa const struct in6_addr *laddr, 72b50026b5SHannes Frederic Sowa const u16 lport, 73b50026b5SHannes Frederic Sowa const struct in6_addr *faddr, 74b50026b5SHannes Frederic Sowa const __be16 fport) 75b50026b5SHannes Frederic Sowa { 761bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly; 771bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly; 781bbdceefSHannes Frederic Sowa 791bbdceefSHannes Frederic Sowa u32 lhash, fhash; 801bbdceefSHannes Frederic Sowa 811bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret, 821bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret)); 831bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret, 841bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret)); 851bbdceefSHannes Frederic Sowa 861bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3]; 871bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 881bbdceefSHannes Frederic Sowa 89b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport, 901bbdceefSHannes Frederic Sowa udp_ipv6_hash_secret + net_hash_mix(net)); 91b50026b5SHannes Frederic Sowa } 92b50026b5SHannes Frederic Sowa 936ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 94db8dac20SDavid S. Miller { 9530fff923SEric Dumazet unsigned int hash2_nulladdr = 96f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 9730fff923SEric Dumazet unsigned int hash2_partial = 98f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 9930fff923SEric Dumazet 100d4cada4aSEric Dumazet /* precompute partial secondary hash */ 10130fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 102fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 103db8dac20SDavid S. Miller } 104db8dac20SDavid S. Miller 105719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk) 106719f8358SEric Dumazet { 107f0b1e64cSMartin KaFai Lau u16 new_hash = ipv6_portaddr_hash(sock_net(sk), 108efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr, 109719f8358SEric Dumazet inet_sk(sk)->inet_num); 110719f8358SEric Dumazet 111719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 112719f8358SEric Dumazet } 113719f8358SEric Dumazet 114d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 11588440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 116d1e37288SSu, Xuemin const struct in6_addr *daddr, unsigned short hnum, 1171801b570SDavid Ahern int dif, int sdif, bool exact_dif) 118db8dac20SDavid S. Miller { 11960c04aecSJoe Perches int score; 12060c04aecSJoe Perches struct inet_sock *inet; 1216da5b0f0SMike Manning bool dev_match; 122db8dac20SDavid S. Miller 12360c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 12460c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 12560c04aecSJoe Perches sk->sk_family != PF_INET6) 12660c04aecSJoe Perches return -1; 127645ca708SEric Dumazet 128645ca708SEric Dumazet score = 0; 12960c04aecSJoe Perches inet = inet_sk(sk); 13060c04aecSJoe Perches 131c720c7e8SEric Dumazet if (inet->inet_dport) { 132c720c7e8SEric Dumazet if (inet->inet_dport != sport) 133645ca708SEric Dumazet return -1; 134db8dac20SDavid S. Miller score++; 135db8dac20SDavid S. Miller } 13660c04aecSJoe Perches 137efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { 138efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 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 1496da5b0f0SMike Manning dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif); 15069678bcdSPaolo Abeni if (!dev_match) 151645ca708SEric Dumazet return -1; 152db8dac20SDavid S. Miller score++; 15360c04aecSJoe Perches 15470da268bSEric Dumazet if (sk->sk_incoming_cpu == raw_smp_processor_id()) 15570da268bSEric Dumazet score++; 15670da268bSEric Dumazet 157645ca708SEric Dumazet return score; 158645ca708SEric Dumazet } 159645ca708SEric Dumazet 160d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 161fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net, 162fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 1631801b570SDavid Ahern const struct in6_addr *daddr, unsigned int hnum, 1641801b570SDavid Ahern int dif, int sdif, bool exact_dif, 1651801b570SDavid Ahern struct udp_hslot *hslot2, struct sk_buff *skb) 166fddc17deSEric Dumazet { 167fddc17deSEric Dumazet struct sock *sk, *result; 168e94a62f5SPaolo Abeni int score, badness; 16972289b96STom Herbert u32 hash = 0; 170fddc17deSEric Dumazet 171fddc17deSEric Dumazet result = NULL; 172fddc17deSEric Dumazet badness = -1; 173ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 174d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 1751801b570SDavid Ahern daddr, hnum, dif, sdif, exact_dif); 176fddc17deSEric Dumazet if (score > badness) { 177e94a62f5SPaolo Abeni if (sk->sk_reuseport) { 178b50026b5SHannes Frederic Sowa hash = udp6_ehashfn(net, daddr, hnum, 17972289b96STom Herbert saddr, sport); 180ed0dfffdSEric Dumazet 181ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 1821134158bSCraig Gallek sizeof(struct udphdr)); 183ca065d0cSEric Dumazet if (result) 184ca065d0cSEric Dumazet return result; 18570da268bSEric Dumazet } 186ca065d0cSEric Dumazet result = sk; 187ca065d0cSEric Dumazet badness = score; 188fddc17deSEric Dumazet } 189fddc17deSEric Dumazet } 190fddc17deSEric Dumazet return result; 191fddc17deSEric Dumazet } 192fddc17deSEric Dumazet 193ca065d0cSEric Dumazet /* rcu_read_lock() must be held */ 194fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net, 19588440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 19688440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 1971801b570SDavid Ahern int dif, int sdif, struct udp_table *udptable, 198538950a1SCraig Gallek struct sk_buff *skb) 199645ca708SEric Dumazet { 200271b72c7SEric Dumazet struct sock *sk, *result; 201645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 202fddc17deSEric Dumazet unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 203fddc17deSEric Dumazet struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 20463a6fff3SRobert Shearman bool exact_dif = udp6_lib_exact_dif_match(net, skb); 205e94a62f5SPaolo Abeni int score, badness; 20672289b96STom Herbert u32 hash = 0; 207645ca708SEric Dumazet 208fddc17deSEric Dumazet if (hslot->count > 10) { 209f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum); 210fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 211fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 212fddc17deSEric Dumazet if (hslot->count < hslot2->count) 213fddc17deSEric Dumazet goto begin; 214fddc17deSEric Dumazet 215fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport, 2161801b570SDavid Ahern daddr, hnum, dif, sdif, exact_dif, 217d1e37288SSu, Xuemin hslot2, skb); 218fddc17deSEric Dumazet if (!result) { 219d1e37288SSu, Xuemin unsigned int old_slot2 = slot2; 220f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum); 221fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 222d1e37288SSu, Xuemin /* avoid searching the same slot again. */ 223d1e37288SSu, Xuemin if (unlikely(slot2 == old_slot2)) 224d1e37288SSu, Xuemin return result; 225d1e37288SSu, Xuemin 226fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 227fddc17deSEric Dumazet if (hslot->count < hslot2->count) 228fddc17deSEric Dumazet goto begin; 229fddc17deSEric Dumazet 2301223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport, 2311801b570SDavid Ahern daddr, hnum, dif, sdif, 23263a6fff3SRobert Shearman exact_dif, hslot2, 23363a6fff3SRobert Shearman skb); 234fddc17deSEric Dumazet } 2358217ca65SMartin KaFai Lau if (unlikely(IS_ERR(result))) 2368217ca65SMartin KaFai Lau return NULL; 237fddc17deSEric Dumazet return result; 238fddc17deSEric Dumazet } 239271b72c7SEric Dumazet begin: 240271b72c7SEric Dumazet result = NULL; 241271b72c7SEric Dumazet badness = -1; 242ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 24363a6fff3SRobert Shearman score = compute_score(sk, net, saddr, sport, daddr, hnum, dif, 2441801b570SDavid Ahern sdif, exact_dif); 245645ca708SEric Dumazet if (score > badness) { 246e94a62f5SPaolo Abeni if (sk->sk_reuseport) { 247b50026b5SHannes Frederic Sowa hash = udp6_ehashfn(net, daddr, hnum, 24872289b96STom Herbert saddr, sport); 249ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 250538950a1SCraig Gallek sizeof(struct udphdr)); 2518217ca65SMartin KaFai Lau if (unlikely(IS_ERR(result))) 2528217ca65SMartin KaFai Lau return NULL; 253ca065d0cSEric Dumazet if (result) 254ca065d0cSEric Dumazet return result; 25572289b96STom Herbert } 256ca065d0cSEric Dumazet result = sk; 257ca065d0cSEric Dumazet badness = score; 258db8dac20SDavid S. Miller } 259db8dac20SDavid S. Miller } 260db8dac20SDavid S. Miller return result; 261db8dac20SDavid S. Miller } 262fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 263db8dac20SDavid S. Miller 264607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 265607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 266645ca708SEric Dumazet struct udp_table *udptable) 267607c4aafSKOVACS Krisztian { 268b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 269607c4aafSKOVACS Krisztian 270ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 271607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 2721801b570SDavid Ahern inet6_sdif(skb), udptable, skb); 273607c4aafSKOVACS Krisztian } 274607c4aafSKOVACS Krisztian 27563058308STom Herbert struct sock *udp6_lib_lookup_skb(struct sk_buff *skb, 27663058308STom Herbert __be16 sport, __be16 dport) 27763058308STom Herbert { 27863058308STom Herbert const struct ipv6hdr *iph = ipv6_hdr(skb); 27963058308STom Herbert 280ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 28163058308STom Herbert &iph->daddr, dport, inet6_iif(skb), 2821801b570SDavid Ahern inet6_sdif(skb), &udp_table, skb); 28363058308STom Herbert } 28463058308STom Herbert EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb); 28563058308STom Herbert 286ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 287ca065d0cSEric Dumazet * Does increment socket refcount. 288ca065d0cSEric Dumazet */ 2896e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6) 290aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 291aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif) 292aa976fc0SBalazs Scheidler { 293ca065d0cSEric Dumazet struct sock *sk; 294ca065d0cSEric Dumazet 295ca065d0cSEric Dumazet sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, 2961801b570SDavid Ahern dif, 0, &udp_table, NULL); 29741c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 298ca065d0cSEric Dumazet sk = NULL; 299ca065d0cSEric Dumazet return sk; 300aa976fc0SBalazs Scheidler } 301aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup); 302ca065d0cSEric Dumazet #endif 303aa976fc0SBalazs Scheidler 304cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the 305cb891fa6SPaolo Abeni * scratch area space; note that the IP6CB flags is still in the first 306cb891fa6SPaolo Abeni * cacheline, so checking for jumbograms is cheap 307cb891fa6SPaolo Abeni */ 308cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb) 309cb891fa6SPaolo Abeni { 310cb891fa6SPaolo Abeni return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb); 311cb891fa6SPaolo Abeni } 312cb891fa6SPaolo Abeni 313db8dac20SDavid S. Miller /* 314db8dac20SDavid S. Miller * This should be easy, if there is something there we 315db8dac20SDavid S. Miller * return it, otherwise we block. 316db8dac20SDavid S. Miller */ 317db8dac20SDavid S. Miller 3181b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 319db8dac20SDavid S. Miller int noblock, int flags, int *addr_len) 320db8dac20SDavid S. Miller { 321db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 322db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 323db8dac20SDavid S. Miller struct sk_buff *skb; 32459c2cdaeSDavid S. Miller unsigned int ulen, copied; 325627d2d6bSsamanthakumar int peeked, peeking, off; 326db8dac20SDavid S. Miller int err; 327db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 328197c949eSEric Dumazet bool checksum_valid = false; 329029a3743SPaolo Abeni struct udp_mib *mib; 330f26ba175SWei Yongjun int is_udp4; 331db8dac20SDavid S. Miller 332db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 33385fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 334db8dac20SDavid S. Miller 3354b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 33685fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 3374b340ae2SBrian Haley 338db8dac20SDavid S. Miller try_again: 339a0917e0bSMatthew Dawson peeking = flags & MSG_PEEK; 340a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 3417c13f97fSPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err); 342db8dac20SDavid S. Miller if (!skb) 343627d2d6bSsamanthakumar return err; 344db8dac20SDavid S. Miller 345cb891fa6SPaolo Abeni ulen = udp6_skb_len(skb); 34659c2cdaeSDavid S. Miller copied = len; 347627d2d6bSsamanthakumar if (copied > ulen - off) 348627d2d6bSsamanthakumar copied = ulen - off; 34959c2cdaeSDavid S. Miller else if (copied < ulen) 350db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 351db8dac20SDavid S. Miller 352f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 353029a3743SPaolo Abeni mib = __UDPX_MIB(sk, is_udp4); 354f26ba175SWei Yongjun 355db8dac20SDavid S. Miller /* 356db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 357db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 358db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 359db8dac20SDavid S. Miller */ 360db8dac20SDavid S. Miller 361d21dbdfeSEric Dumazet if (copied < ulen || peeking || 362d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 36367a51780SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 36467a51780SPaolo Abeni !__udp_lib_checksum_complete(skb); 365197c949eSEric Dumazet if (!checksum_valid) 366db8dac20SDavid S. Miller goto csum_copy_err; 367db8dac20SDavid S. Miller } 368db8dac20SDavid S. Miller 36967a51780SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 37067a51780SPaolo Abeni if (udp_skb_is_linear(skb)) 37167a51780SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 37267a51780SPaolo Abeni else 373627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 37467a51780SPaolo Abeni } else { 375627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 376db8dac20SDavid S. Miller if (err == -EINVAL) 377db8dac20SDavid S. Miller goto csum_copy_err; 378db8dac20SDavid S. Miller } 37922911fc5SEric Dumazet if (unlikely(err)) { 380979402b1SEric Dumazet if (!peeked) { 381979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 382029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 383979402b1SEric Dumazet } 384850cbaddSPaolo Abeni kfree_skb(skb); 385627d2d6bSsamanthakumar return err; 38622911fc5SEric Dumazet } 387029a3743SPaolo Abeni if (!peeked) 388029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS); 389db8dac20SDavid S. Miller 3903b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 391db8dac20SDavid S. Miller 392db8dac20SDavid S. Miller /* Copy the address. */ 393db8dac20SDavid S. Miller if (msg->msg_name) { 394342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 395db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 396db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 397db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 398db8dac20SDavid S. Miller 399842df073SHannes Frederic Sowa if (is_udp4) { 400b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 401b301e82cSBrian Haley &sin6->sin6_addr); 402842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0; 403842df073SHannes Frederic Sowa } else { 4044e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 405842df073SHannes Frederic Sowa sin6->sin6_scope_id = 406842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr, 4074330487aSDuan Jiong inet6_iif(skb)); 408db8dac20SDavid S. Miller } 409bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 410db8dac20SDavid S. Miller } 4114b261c75SHannes Frederic Sowa 412bcd1665eSPaolo Abeni if (udp_sk(sk)->gro_enabled) 413bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb); 414bcd1665eSPaolo Abeni 4154b261c75SHannes Frederic Sowa if (np->rxopt.all) 4164b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb); 4174b261c75SHannes Frederic Sowa 418f26ba175SWei Yongjun if (is_udp4) { 419db8dac20SDavid S. Miller if (inet->cmsg_flags) 420ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, 42110df8e61SEric Dumazet sizeof(struct udphdr), off); 422db8dac20SDavid S. Miller } else { 423db8dac20SDavid S. Miller if (np->rxopt.all) 4244b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb); 425db8dac20SDavid S. Miller } 426db8dac20SDavid S. Miller 42759c2cdaeSDavid S. Miller err = copied; 428db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 429db8dac20SDavid S. Miller err = ulen; 430db8dac20SDavid S. Miller 431850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 432db8dac20SDavid S. Miller return err; 433db8dac20SDavid S. Miller 434db8dac20SDavid S. Miller csum_copy_err: 4352276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 4362276f58aSPaolo Abeni udp_skb_destructor)) { 437029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS); 438029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS); 4396a5dc9e5SEric Dumazet } 440850cbaddSPaolo Abeni kfree_skb(skb); 441db8dac20SDavid S. Miller 442beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 443beb39db5SEric Dumazet cond_resched(); 4449cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 445db8dac20SDavid S. Miller goto try_again; 446db8dac20SDavid S. Miller } 447db8dac20SDavid S. Miller 448a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key); 449a36e185eSStefano Brivio void udpv6_encap_enable(void) 450a36e185eSStefano Brivio { 451a36e185eSStefano Brivio static_branch_enable(&udpv6_encap_needed_key); 452a36e185eSStefano Brivio } 453a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable); 454a36e185eSStefano Brivio 455e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go 456e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match. 457e7cc0824SStefano Brivio */ 458e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb, 459e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 460e7cc0824SStefano Brivio u8 type, u8 code, int offset, u32 info) 461e7cc0824SStefano Brivio { 462e7cc0824SStefano Brivio int i; 463e7cc0824SStefano Brivio 464e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) { 465e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt, 466e7cc0824SStefano Brivio u8 type, u8 code, int offset, u32 info); 467e7cc0824SStefano Brivio 468e7cc0824SStefano Brivio if (!ip6tun_encaps[i]) 469e7cc0824SStefano Brivio continue; 470e7cc0824SStefano Brivio handler = rcu_dereference(ip6tun_encaps[i]->err_handler); 471e7cc0824SStefano Brivio if (handler && !handler(skb, opt, type, code, offset, info)) 472e7cc0824SStefano Brivio return 0; 473e7cc0824SStefano Brivio } 474e7cc0824SStefano Brivio 475e7cc0824SStefano Brivio return -ENOENT; 476e7cc0824SStefano Brivio } 477e7cc0824SStefano Brivio 478a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without 479a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the 480a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that 481a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with 482a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to 483a36e185eSStefano Brivio * trace ICMP messages back to them. 484a36e185eSStefano Brivio * 485e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination 486e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port 487e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port. 488e7cc0824SStefano Brivio * 489a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid 490a36e185eSStefano Brivio * association. 491a36e185eSStefano Brivio * 492e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further 493e7cc0824SStefano Brivio * processing, zero otherwise. 494a36e185eSStefano Brivio */ 495a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net, 496a36e185eSStefano Brivio const struct ipv6hdr *hdr, int offset, 497a36e185eSStefano Brivio struct udphdr *uh, 498a36e185eSStefano Brivio struct udp_table *udptable, 499e7cc0824SStefano Brivio struct sk_buff *skb, 500e7cc0824SStefano Brivio struct inet6_skb_parm *opt, 501e7cc0824SStefano Brivio u8 type, u8 code, __be32 info) 502a36e185eSStefano Brivio { 503a36e185eSStefano Brivio int network_offset, transport_offset; 504a36e185eSStefano Brivio struct sock *sk; 505a36e185eSStefano Brivio 506a36e185eSStefano Brivio network_offset = skb_network_offset(skb); 507a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb); 508a36e185eSStefano Brivio 509a36e185eSStefano Brivio /* Network header needs to point to the outer IPv6 header inside ICMP */ 510a36e185eSStefano Brivio skb_reset_network_header(skb); 511a36e185eSStefano Brivio 512a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */ 513a36e185eSStefano Brivio skb_set_transport_header(skb, offset); 514a36e185eSStefano Brivio 515e7cc0824SStefano Brivio sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source, 516e7cc0824SStefano Brivio &hdr->saddr, uh->dest, 517e7cc0824SStefano Brivio inet6_iif(skb), 0, udptable, skb); 518e7cc0824SStefano Brivio if (sk) { 519e7cc0824SStefano Brivio int (*lookup)(struct sock *sk, struct sk_buff *skb); 520e7cc0824SStefano Brivio struct udp_sock *up = udp_sk(sk); 521e7cc0824SStefano Brivio 522a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup); 523a36e185eSStefano Brivio if (!lookup || lookup(sk, skb)) 524a36e185eSStefano Brivio sk = NULL; 525e7cc0824SStefano Brivio } 526e7cc0824SStefano Brivio 527e7cc0824SStefano Brivio if (!sk) { 528e7cc0824SStefano Brivio sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code, 529e7cc0824SStefano Brivio offset, info)); 530e7cc0824SStefano Brivio } 531a36e185eSStefano Brivio 532a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset); 533a36e185eSStefano Brivio skb_set_network_header(skb, network_offset); 534e7cc0824SStefano Brivio 535a36e185eSStefano Brivio return sk; 536a36e185eSStefano Brivio } 537a36e185eSStefano Brivio 53832bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 539d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 540645ca708SEric Dumazet struct udp_table *udptable) 541db8dac20SDavid S. Miller { 542db8dac20SDavid S. Miller struct ipv6_pinfo *np; 543b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 544b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr; 545b71d1d42SEric Dumazet const struct in6_addr *daddr = &hdr->daddr; 546db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset); 547a36e185eSStefano Brivio bool tunnel = false; 548db8dac20SDavid S. Miller struct sock *sk; 549e0d8c1b7SWei Wang int harderr; 550db8dac20SDavid S. Miller int err; 5517304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 552db8dac20SDavid S. Miller 553e32ea7e7SCraig Gallek sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source, 554f64bf6b8SMike Manning inet6_iif(skb), inet6_sdif(skb), udptable, skb); 55563159f29SIan Morris if (!sk) { 556a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */ 557e7cc0824SStefano Brivio sk = ERR_PTR(-ENOENT); 558a36e185eSStefano Brivio if (static_branch_unlikely(&udpv6_encap_needed_key)) { 559a36e185eSStefano Brivio sk = __udp6_lib_err_encap(net, hdr, offset, uh, 560e7cc0824SStefano Brivio udptable, skb, 561e7cc0824SStefano Brivio opt, type, code, info); 562e7cc0824SStefano Brivio if (!sk) 563e7cc0824SStefano Brivio return 0; 564a36e185eSStefano Brivio } 565a36e185eSStefano Brivio 566e7cc0824SStefano Brivio if (IS_ERR(sk)) { 567a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), 5687304fe46SDuan Jiong ICMP6_MIB_INERRORS); 569e7cc0824SStefano Brivio return PTR_ERR(sk); 5707304fe46SDuan Jiong } 571e7cc0824SStefano Brivio 572a36e185eSStefano Brivio tunnel = true; 573a36e185eSStefano Brivio } 574db8dac20SDavid S. Miller 575e0d8c1b7SWei Wang harderr = icmpv6_err_convert(type, code, &err); 576e0d8c1b7SWei Wang np = inet6_sk(sk); 577e0d8c1b7SWei Wang 57893b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) { 57993b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk)) 58093b36cf3SHannes Frederic Sowa goto out; 58181aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 582e0d8c1b7SWei Wang if (np->pmtudisc != IPV6_PMTUDISC_DONT) 583e0d8c1b7SWei Wang harderr = 1; 58493b36cf3SHannes Frederic Sowa } 5851a462d18SDuan Jiong if (type == NDISC_REDIRECT) { 586a36e185eSStefano Brivio if (tunnel) { 587a36e185eSStefano Brivio ip6_redirect(skb, sock_net(sk), inet6_iif(skb), 588a36e185eSStefano Brivio sk->sk_mark, sk->sk_uid); 589a36e185eSStefano Brivio } else { 590ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 591a36e185eSStefano Brivio } 5921a462d18SDuan Jiong goto out; 5931a462d18SDuan Jiong } 59481aded24SDavid S. Miller 595a36e185eSStefano Brivio /* Tunnels don't have an application socket: don't pass errors back */ 596a36e185eSStefano Brivio if (tunnel) 597a36e185eSStefano Brivio goto out; 598a36e185eSStefano Brivio 599e0d8c1b7SWei Wang if (!np->recverr) { 600e0d8c1b7SWei Wang if (!harderr || sk->sk_state != TCP_ESTABLISHED) 601db8dac20SDavid S. Miller goto out; 602e0d8c1b7SWei Wang } else { 603db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 604e0d8c1b7SWei Wang } 605b1faf566SEric Dumazet 606db8dac20SDavid S. Miller sk->sk_err = err; 607db8dac20SDavid S. Miller sk->sk_error_report(sk); 608db8dac20SDavid S. Miller out: 60932bbd879SStefano Brivio return 0; 610db8dac20SDavid S. Miller } 611db8dac20SDavid S. Miller 612a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 613f7ad74feSBenjamin LaHaise { 614f7ad74feSBenjamin LaHaise int rc; 615f7ad74feSBenjamin LaHaise 616efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 617f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb); 618005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 6192c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 620e68b6e50SEric Dumazet } else { 621e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 622005ec974SShawn Bohrer } 623f7ad74feSBenjamin LaHaise 624850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 625f7ad74feSBenjamin LaHaise if (rc < 0) { 626f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk); 627f7ad74feSBenjamin LaHaise 628f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */ 629f7ad74feSBenjamin LaHaise if (rc == -ENOMEM) 630e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), 631f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite); 632e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 633f7ad74feSBenjamin LaHaise kfree_skb(skb); 634f7ad74feSBenjamin LaHaise return -1; 635f7ad74feSBenjamin LaHaise } 636850cbaddSPaolo Abeni 637f7ad74feSBenjamin LaHaise return 0; 638f7ad74feSBenjamin LaHaise } 639f7ad74feSBenjamin LaHaise 64032bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb, 641d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 642d5fdd6baSBrian Haley u8 code, int offset, __be32 info) 643db8dac20SDavid S. Miller { 64432bbd879SStefano Brivio return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 645db8dac20SDavid S. Miller } 646db8dac20SDavid S. Miller 647cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb) 648db8dac20SDavid S. Miller { 649db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 650db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 651db8dac20SDavid S. Miller 652db8dac20SDavid S. Miller if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 653db8dac20SDavid S. Miller goto drop; 654db8dac20SDavid S. Miller 65588ab3108SDavidlohr Bueso if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) { 656d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 657d7f3f621SBenjamin LaHaise 658d7f3f621SBenjamin LaHaise /* 659d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to 660d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just 661d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket. 662d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value: 663d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap 664d7f3f621SBenjamin LaHaise * handler or was discarded by it. 665d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP. 666d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N 667d7f3f621SBenjamin LaHaise */ 668d7f3f621SBenjamin LaHaise 669d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */ 6706aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv); 671e5aed006SHannes Frederic Sowa if (encap_rcv) { 672d7f3f621SBenjamin LaHaise int ret; 673d7f3f621SBenjamin LaHaise 6740a80966bSTom Herbert /* Verify checksum before giving to encap */ 6750a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 6760a80966bSTom Herbert goto csum_error; 6770a80966bSTom Herbert 678d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb); 679d7f3f621SBenjamin LaHaise if (ret <= 0) { 68002c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 681d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS, 682d7f3f621SBenjamin LaHaise is_udplite); 683d7f3f621SBenjamin LaHaise return -ret; 684d7f3f621SBenjamin LaHaise } 685d7f3f621SBenjamin LaHaise } 686d7f3f621SBenjamin LaHaise 687d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */ 688d7f3f621SBenjamin LaHaise } 689d7f3f621SBenjamin LaHaise 690db8dac20SDavid S. Miller /* 691db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 692db8dac20SDavid S. Miller */ 693db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 694db8dac20SDavid S. Miller 695db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 696ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 697db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 698db8dac20SDavid S. Miller goto drop; 699db8dac20SDavid S. Miller } 700db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 701ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 702db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 703db8dac20SDavid S. Miller goto drop; 704db8dac20SDavid S. Miller } 705db8dac20SDavid S. Miller } 706db8dac20SDavid S. Miller 7074b943faeSPaolo Abeni prefetch(&sk->sk_rmem_alloc); 708ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 709ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 7106a5dc9e5SEric Dumazet goto csum_error; 711ce25d66aSEric Dumazet 712ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 713a6127697SMichal Kubeček goto drop; 714db8dac20SDavid S. Miller 715e6afc8acSsamanthakumar udp_csum_pull_header(skb); 716cb80ef46SBenjamin LaHaise 717d826eb14SEric Dumazet skb_dst_drop(skb); 718db8dac20SDavid S. Miller 719850cbaddSPaolo Abeni return __udpv6_queue_rcv_skb(sk, skb); 7203e215c8dSJames M Leddy 7216a5dc9e5SEric Dumazet csum_error: 72202c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 723db8dac20SDavid S. Miller drop: 72402c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 725cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops); 726db8dac20SDavid S. Miller kfree_skb(skb); 727db8dac20SDavid S. Miller return -1; 728db8dac20SDavid S. Miller } 729db8dac20SDavid S. Miller 730cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 731cf329aa4SPaolo Abeni { 732cf329aa4SPaolo Abeni struct sk_buff *next, *segs; 733cf329aa4SPaolo Abeni int ret; 734cf329aa4SPaolo Abeni 735cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb))) 736cf329aa4SPaolo Abeni return udpv6_queue_rcv_one_skb(sk, skb); 737cf329aa4SPaolo Abeni 738cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb)); 739cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, false); 740cf329aa4SPaolo Abeni for (skb = segs; skb; skb = next) { 741cf329aa4SPaolo Abeni next = skb->next; 742cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb)); 743cf329aa4SPaolo Abeni 744cf329aa4SPaolo Abeni ret = udpv6_queue_rcv_one_skb(sk, skb); 745cf329aa4SPaolo Abeni if (ret > 0) 746cf329aa4SPaolo Abeni ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret, 747cf329aa4SPaolo Abeni true); 748cf329aa4SPaolo Abeni } 749cf329aa4SPaolo Abeni return 0; 750cf329aa4SPaolo Abeni } 751cf329aa4SPaolo Abeni 7525cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, 753b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr, 754b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr, 7557bd2db40SDewi Morgan int dif, int sdif, unsigned short hnum) 756db8dac20SDavid S. Miller { 7579e89fd8bSSven Wegener struct inet_sock *inet = inet_sk(sk); 758db8dac20SDavid S. Miller 7599e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net)) 7605cf3d461SDavid Held return false; 761b8ad0cbcSDaniel Lezcano 7625cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum || 7635cf3d461SDavid Held sk->sk_family != PF_INET6 || 7645cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) || 7655cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) && 7665cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 7677bd2db40SDewi Morgan !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) || 76833b4b015SHenning Rogge (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && 76933b4b015SHenning Rogge !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) 7705cf3d461SDavid Held return false; 7719e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 7725cf3d461SDavid Held return false; 7735cf3d461SDavid Held return true; 774db8dac20SDavid S. Miller } 775db8dac20SDavid S. Miller 7764068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb) 7774068579eSTom Herbert { 7784068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log 7794068579eSTom Herbert * this error. Well, it is reasonable. 7804068579eSTom Herbert */ 781ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 7824068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 7834068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 7844068579eSTom Herbert } 7854068579eSTom Herbert 786db8dac20SDavid S. Miller /* 787db8dac20SDavid S. Miller * Note: called only from the BH handler context, 788db8dac20SDavid S. Miller * so we don't need to lock the hashes. 789db8dac20SDavid S. Miller */ 790e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 791b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr, 79236cbb245SRick Jones struct udp_table *udptable, int proto) 793db8dac20SDavid S. Miller { 794ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 795db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 7965cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 7975cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 798ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 7992dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 800ca065d0cSEric Dumazet int dif = inet6_iif(skb); 8017bd2db40SDewi Morgan int sdif = inet6_sdif(skb); 802ca065d0cSEric Dumazet struct hlist_node *node; 803ca065d0cSEric Dumazet struct sk_buff *nskb; 8042dc41cffSDavid Held 8052dc41cffSDavid Held if (use_hash2) { 806f0b1e64cSMartin KaFai Lau hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) & 80773e2d5e3SPablo Neira udptable->mask; 808f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask; 8092dc41cffSDavid Held start_lookup: 81073e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 8112dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 8122dc41cffSDavid Held } 813db8dac20SDavid S. Miller 814ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 815ca065d0cSEric Dumazet if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr, 8167bd2db40SDewi Morgan uh->source, saddr, dif, sdif, 8177bd2db40SDewi Morgan hnum)) 818ca065d0cSEric Dumazet continue; 8191c19448cSTom Herbert /* If zero checksum and no_check is not on for 8204068579eSTom Herbert * the socket then skip it. 8214068579eSTom Herbert */ 822ca065d0cSEric Dumazet if (!uh->check && !udp_sk(sk)->no_check6_rx) 823ca065d0cSEric Dumazet continue; 824ca065d0cSEric Dumazet if (!first) { 825ca065d0cSEric Dumazet first = sk; 826ca065d0cSEric Dumazet continue; 827db8dac20SDavid S. Miller } 828ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 829ca065d0cSEric Dumazet if (unlikely(!nskb)) { 830ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 83102c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 832ca065d0cSEric Dumazet IS_UDPLITE(sk)); 83302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, 834ca065d0cSEric Dumazet IS_UDPLITE(sk)); 835ca065d0cSEric Dumazet continue; 836a1ab77f9SEric Dumazet } 837db8dac20SDavid S. Miller 838ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(sk, nskb) > 0) 839ca065d0cSEric Dumazet consume_skb(nskb); 840ca065d0cSEric Dumazet } 841a1ab77f9SEric Dumazet 8422dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 8432dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 8442dc41cffSDavid Held hash2 = hash2_any; 8452dc41cffSDavid Held goto start_lookup; 8462dc41cffSDavid Held } 8472dc41cffSDavid Held 848ca065d0cSEric Dumazet if (first) { 849ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(first, skb) > 0) 850ca065d0cSEric Dumazet consume_skb(skb); 851a1ab77f9SEric Dumazet } else { 852ca065d0cSEric Dumazet kfree_skb(skb); 85302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 85436cbb245SRick Jones proto == IPPROTO_UDPLITE); 855a1ab77f9SEric Dumazet } 856db8dac20SDavid S. Miller return 0; 857db8dac20SDavid S. Miller } 858db8dac20SDavid S. Miller 85964f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst) 86064f0f5d1SPaolo Abeni { 86164f0f5d1SPaolo Abeni if (udp_sk_rx_dst_set(sk, dst)) { 86264f0f5d1SPaolo Abeni const struct rt6_info *rt = (const struct rt6_info *)dst; 86364f0f5d1SPaolo Abeni 86464f0f5d1SPaolo Abeni inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt); 86564f0f5d1SPaolo Abeni } 86664f0f5d1SPaolo Abeni } 86764f0f5d1SPaolo Abeni 868eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and 869eb63f296SPaolo Abeni * return code conversion for ip layer consumption 870eb63f296SPaolo Abeni */ 871eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb, 872eb63f296SPaolo Abeni struct udphdr *uh) 873eb63f296SPaolo Abeni { 874eb63f296SPaolo Abeni int ret; 875eb63f296SPaolo Abeni 876eb63f296SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 877eb63f296SPaolo Abeni skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, 878eb63f296SPaolo Abeni ip6_compute_pseudo); 879eb63f296SPaolo Abeni 880eb63f296SPaolo Abeni ret = udpv6_queue_rcv_skb(sk, skb); 881eb63f296SPaolo Abeni 88284dad559SPaolo Abeni /* a return value > 0 means to resubmit the input */ 883eb63f296SPaolo Abeni if (ret > 0) 88484dad559SPaolo Abeni return ret; 885eb63f296SPaolo Abeni return 0; 886eb63f296SPaolo Abeni } 887eb63f296SPaolo Abeni 888645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 889db8dac20SDavid S. Miller int proto) 890db8dac20SDavid S. Miller { 891b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 892ca065d0cSEric Dumazet struct net *net = dev_net(skb->dev); 893ca065d0cSEric Dumazet struct udphdr *uh; 894ca065d0cSEric Dumazet struct sock *sk; 895db8dac20SDavid S. Miller u32 ulen = 0; 896db8dac20SDavid S. Miller 897db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 898d6bc0149SBjørn Mork goto discard; 899db8dac20SDavid S. Miller 900db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 901db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 902db8dac20SDavid S. Miller uh = udp_hdr(skb); 903db8dac20SDavid S. Miller 904db8dac20SDavid S. Miller ulen = ntohs(uh->len); 905db8dac20SDavid S. Miller if (ulen > skb->len) 906db8dac20SDavid S. Miller goto short_packet; 907db8dac20SDavid S. Miller 908db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 909db8dac20SDavid S. Miller /* UDP validates ulen. */ 910db8dac20SDavid S. Miller 911db8dac20SDavid S. Miller /* Check for jumbo payload */ 912db8dac20SDavid S. Miller if (ulen == 0) 913db8dac20SDavid S. Miller ulen = skb->len; 914db8dac20SDavid S. Miller 915db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 916db8dac20SDavid S. Miller goto short_packet; 917db8dac20SDavid S. Miller 918db8dac20SDavid S. Miller if (ulen < skb->len) { 919db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 920db8dac20SDavid S. Miller goto short_packet; 921db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 922db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 923db8dac20SDavid S. Miller uh = udp_hdr(skb); 924db8dac20SDavid S. Miller } 925db8dac20SDavid S. Miller } 926db8dac20SDavid S. Miller 927db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 9286a5dc9e5SEric Dumazet goto csum_error; 929db8dac20SDavid S. Miller 930c9f2c1aeSPaolo Abeni /* Check if the socket is already available, e.g. due to early demux */ 931c9f2c1aeSPaolo Abeni sk = skb_steal_sock(skb); 932c9f2c1aeSPaolo Abeni if (sk) { 933c9f2c1aeSPaolo Abeni struct dst_entry *dst = skb_dst(skb); 934c9f2c1aeSPaolo Abeni int ret; 935c9f2c1aeSPaolo Abeni 936c9f2c1aeSPaolo Abeni if (unlikely(sk->sk_rx_dst != dst)) 93764f0f5d1SPaolo Abeni udp6_sk_rx_dst_set(sk, dst); 938c9f2c1aeSPaolo Abeni 939eb63f296SPaolo Abeni if (!uh->check && !udp_sk(sk)->no_check6_rx) { 940c9f2c1aeSPaolo Abeni sock_put(sk); 941eb63f296SPaolo Abeni goto report_csum_error; 942eb63f296SPaolo Abeni } 943c9f2c1aeSPaolo Abeni 944eb63f296SPaolo Abeni ret = udp6_unicast_rcv_skb(sk, skb, uh); 945eb63f296SPaolo Abeni sock_put(sk); 946c9f2c1aeSPaolo Abeni return ret; 947c9f2c1aeSPaolo Abeni } 948c9f2c1aeSPaolo Abeni 949db8dac20SDavid S. Miller /* 950db8dac20SDavid S. Miller * Multicast receive code 951db8dac20SDavid S. Miller */ 952db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 953e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 95436cbb245SRick Jones saddr, daddr, udptable, proto); 955db8dac20SDavid S. Miller 956db8dac20SDavid S. Miller /* Unicast */ 957607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 95853b24b8fSIan Morris if (sk) { 959eb63f296SPaolo Abeni if (!uh->check && !udp_sk(sk)->no_check6_rx) 960eb63f296SPaolo Abeni goto report_csum_error; 961eb63f296SPaolo Abeni return udp6_unicast_rcv_skb(sk, skb, uh); 9624068579eSTom Herbert } 9634068579eSTom Herbert 964eb63f296SPaolo Abeni if (!uh->check) 965eb63f296SPaolo Abeni goto report_csum_error; 9664068579eSTom Herbert 967db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 968db8dac20SDavid S. Miller goto discard; 969db8dac20SDavid S. Miller 970db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 9716a5dc9e5SEric Dumazet goto csum_error; 972db8dac20SDavid S. Miller 97302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 9743ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 975db8dac20SDavid S. Miller 976db8dac20SDavid S. Miller kfree_skb(skb); 977db8dac20SDavid S. Miller return 0; 978db8dac20SDavid S. Miller 979db8dac20SDavid S. Miller short_packet: 980ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 981db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 982ba7a46f1SJoe Perches saddr, ntohs(uh->source), 983ba7a46f1SJoe Perches ulen, skb->len, 984ba7a46f1SJoe Perches daddr, ntohs(uh->dest)); 9856a5dc9e5SEric Dumazet goto discard; 986eb63f296SPaolo Abeni 987eb63f296SPaolo Abeni report_csum_error: 988eb63f296SPaolo Abeni udp6_csum_zero_error(skb); 9896a5dc9e5SEric Dumazet csum_error: 99002c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 991db8dac20SDavid S. Miller discard: 99202c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 993db8dac20SDavid S. Miller kfree_skb(skb); 994db8dac20SDavid S. Miller return 0; 995db8dac20SDavid S. Miller } 996db8dac20SDavid S. Miller 9970bd84065Ssubashab@codeaurora.org 9985425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net, 9995425077dSsubashab@codeaurora.org __be16 loc_port, const struct in6_addr *loc_addr, 10005425077dSsubashab@codeaurora.org __be16 rmt_port, const struct in6_addr *rmt_addr, 10014297a0efSDavid Ahern int dif, int sdif) 10025425077dSsubashab@codeaurora.org { 10030bd84065Ssubashab@codeaurora.org unsigned short hnum = ntohs(loc_port); 1004f0b1e64cSMartin KaFai Lau unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum); 10050bd84065Ssubashab@codeaurora.org unsigned int slot2 = hash2 & udp_table.mask; 10060bd84065Ssubashab@codeaurora.org struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 10070bd84065Ssubashab@codeaurora.org const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 10085425077dSsubashab@codeaurora.org struct sock *sk; 10095425077dSsubashab@codeaurora.org 10100bd84065Ssubashab@codeaurora.org udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 101185cb73ffSWei Wang if (sk->sk_state == TCP_ESTABLISHED && 10124297a0efSDavid Ahern INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif)) 10135425077dSsubashab@codeaurora.org return sk; 10140bd84065Ssubashab@codeaurora.org /* Only check first socket in chain */ 10150bd84065Ssubashab@codeaurora.org break; 10160bd84065Ssubashab@codeaurora.org } 10170bd84065Ssubashab@codeaurora.org return NULL; 10185425077dSsubashab@codeaurora.org } 10195425077dSsubashab@codeaurora.org 10205425077dSsubashab@codeaurora.org static void udp_v6_early_demux(struct sk_buff *skb) 10215425077dSsubashab@codeaurora.org { 10225425077dSsubashab@codeaurora.org struct net *net = dev_net(skb->dev); 10235425077dSsubashab@codeaurora.org const struct udphdr *uh; 10245425077dSsubashab@codeaurora.org struct sock *sk; 10255425077dSsubashab@codeaurora.org struct dst_entry *dst; 10265425077dSsubashab@codeaurora.org int dif = skb->dev->ifindex; 10274297a0efSDavid Ahern int sdif = inet6_sdif(skb); 10285425077dSsubashab@codeaurora.org 10295425077dSsubashab@codeaurora.org if (!pskb_may_pull(skb, skb_transport_offset(skb) + 10305425077dSsubashab@codeaurora.org sizeof(struct udphdr))) 10315425077dSsubashab@codeaurora.org return; 10325425077dSsubashab@codeaurora.org 10335425077dSsubashab@codeaurora.org uh = udp_hdr(skb); 10345425077dSsubashab@codeaurora.org 10355425077dSsubashab@codeaurora.org if (skb->pkt_type == PACKET_HOST) 10365425077dSsubashab@codeaurora.org sk = __udp6_lib_demux_lookup(net, uh->dest, 10375425077dSsubashab@codeaurora.org &ipv6_hdr(skb)->daddr, 10385425077dSsubashab@codeaurora.org uh->source, &ipv6_hdr(skb)->saddr, 10394297a0efSDavid Ahern dif, sdif); 10405425077dSsubashab@codeaurora.org else 10415425077dSsubashab@codeaurora.org return; 10425425077dSsubashab@codeaurora.org 104341c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 10445425077dSsubashab@codeaurora.org return; 10455425077dSsubashab@codeaurora.org 10465425077dSsubashab@codeaurora.org skb->sk = sk; 10475425077dSsubashab@codeaurora.org skb->destructor = sock_efree; 10485425077dSsubashab@codeaurora.org dst = READ_ONCE(sk->sk_rx_dst); 10495425077dSsubashab@codeaurora.org 10505425077dSsubashab@codeaurora.org if (dst) 10515425077dSsubashab@codeaurora.org dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie); 10525425077dSsubashab@codeaurora.org if (dst) { 1053d24406c8SWei Wang /* set noref for now. 1054d24406c8SWei Wang * any place which wants to hold dst has to call 1055d24406c8SWei Wang * dst_hold_safe() 1056d24406c8SWei Wang */ 10575425077dSsubashab@codeaurora.org skb_dst_set_noref(skb, dst); 10585425077dSsubashab@codeaurora.org } 10595425077dSsubashab@codeaurora.org } 10605425077dSsubashab@codeaurora.org 1061db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb) 1062db8dac20SDavid S. Miller { 1063645ca708SEric Dumazet return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1064db8dac20SDavid S. Miller } 1065db8dac20SDavid S. Miller 1066db8dac20SDavid S. Miller /* 1067db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 1068db8dac20SDavid S. Miller */ 1069db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 1070db8dac20SDavid S. Miller { 1071db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1072db8dac20SDavid S. Miller 107336d926b9SDenis V. Lunev if (up->pending == AF_INET) 107436d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 107536d926b9SDenis V. Lunev else if (up->pending) { 1076db8dac20SDavid S. Miller up->len = 0; 1077db8dac20SDavid S. Miller up->pending = 0; 1078db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 1079db8dac20SDavid S. Miller } 1080db8dac20SDavid S. Miller } 1081db8dac20SDavid S. Miller 1082d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr, 1083d74bad4eSAndrey Ignatov int addr_len) 1084d74bad4eSAndrey Ignatov { 1085d74bad4eSAndrey Ignatov /* The following checks are replicated from __ip6_datagram_connect() 1086d74bad4eSAndrey Ignatov * and intended to prevent BPF program called below from accessing 1087d74bad4eSAndrey Ignatov * bytes that are out of the bound specified by user in addr_len. 1088d74bad4eSAndrey Ignatov */ 1089d74bad4eSAndrey Ignatov if (uaddr->sa_family == AF_INET) { 1090d74bad4eSAndrey Ignatov if (__ipv6_only_sock(sk)) 1091d74bad4eSAndrey Ignatov return -EAFNOSUPPORT; 1092d74bad4eSAndrey Ignatov return udp_pre_connect(sk, uaddr, addr_len); 1093d74bad4eSAndrey Ignatov } 1094d74bad4eSAndrey Ignatov 1095d74bad4eSAndrey Ignatov if (addr_len < SIN6_LEN_RFC2133) 1096d74bad4eSAndrey Ignatov return -EINVAL; 1097d74bad4eSAndrey Ignatov 1098d74bad4eSAndrey Ignatov return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr); 1099d74bad4eSAndrey Ignatov } 1100d74bad4eSAndrey Ignatov 1101493c6be3SSridhar Samudrala /** 1102493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 1103493c6be3SSridhar Samudrala * @sk: socket we are sending on 1104493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 1105493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 1106493c6be3SSridhar Samudrala */ 1107493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 1108493c6be3SSridhar Samudrala const struct in6_addr *saddr, 1109493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 1110493c6be3SSridhar Samudrala { 1111493c6be3SSridhar Samudrala unsigned int offset; 1112493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 1113d39d938cSVlad Yasevich struct sk_buff *frags = skb_shinfo(skb)->frag_list; 1114493c6be3SSridhar Samudrala __wsum csum = 0; 1115493c6be3SSridhar Samudrala 1116d39d938cSVlad Yasevich if (!frags) { 1117493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 1118493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 1119493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 1120493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 1121493c6be3SSridhar Samudrala } else { 1122493c6be3SSridhar Samudrala /* 1123493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 1124493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 1125493c6be3SSridhar Samudrala * should be together 1126493c6be3SSridhar Samudrala */ 1127493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 1128493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 112963ecc3d9SSubash Abhinov Kasiviswanathan csum = skb->csum; 1130493c6be3SSridhar Samudrala 1131493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1132493c6be3SSridhar Samudrala 1133d39d938cSVlad Yasevich do { 1134d39d938cSVlad Yasevich csum = csum_add(csum, frags->csum); 1135d39d938cSVlad Yasevich } while ((frags = frags->next)); 1136493c6be3SSridhar Samudrala 1137493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1138493c6be3SSridhar Samudrala csum); 1139493c6be3SSridhar Samudrala if (uh->check == 0) 1140493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 1141493c6be3SSridhar Samudrala } 1142493c6be3SSridhar Samudrala } 1143493c6be3SSridhar Samudrala 1144db8dac20SDavid S. Miller /* 1145db8dac20SDavid S. Miller * Sending 1146db8dac20SDavid S. Miller */ 1147db8dac20SDavid S. Miller 1148bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6, 1149bec1f6f6SWillem de Bruijn struct inet_cork *cork) 1150db8dac20SDavid S. Miller { 1151d39d938cSVlad Yasevich struct sock *sk = skb->sk; 1152db8dac20SDavid S. Miller struct udphdr *uh; 1153db8dac20SDavid S. Miller int err = 0; 1154db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1155db8dac20SDavid S. Miller __wsum csum = 0; 1156d39d938cSVlad Yasevich int offset = skb_transport_offset(skb); 1157d39d938cSVlad Yasevich int len = skb->len - offset; 1158db8dac20SDavid S. Miller 1159db8dac20SDavid S. Miller /* 1160db8dac20SDavid S. Miller * Create a UDP header 1161db8dac20SDavid S. Miller */ 1162db8dac20SDavid S. Miller uh = udp_hdr(skb); 11631958b856SDavid S. Miller uh->source = fl6->fl6_sport; 11641958b856SDavid S. Miller uh->dest = fl6->fl6_dport; 1165d39d938cSVlad Yasevich uh->len = htons(len); 1166db8dac20SDavid S. Miller uh->check = 0; 1167db8dac20SDavid S. Miller 1168bec1f6f6SWillem de Bruijn if (cork->gso_size) { 1169bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) + 1170bec1f6f6SWillem de Bruijn sizeof(struct udphdr); 1171bec1f6f6SWillem de Bruijn 1172bec1f6f6SWillem de Bruijn if (hlen + cork->gso_size > cork->fragsize) 1173bec1f6f6SWillem de Bruijn return -EINVAL; 1174bec1f6f6SWillem de Bruijn if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) 1175bec1f6f6SWillem de Bruijn return -EINVAL; 1176a8c744a8SWillem de Bruijn if (udp_sk(sk)->no_check6_tx) 1177a8c744a8SWillem de Bruijn return -EINVAL; 1178ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite || 1179ff06342cSWillem de Bruijn dst_xfrm(skb_dst(skb))) 1180bec1f6f6SWillem de Bruijn return -EIO; 1181bec1f6f6SWillem de Bruijn 1182bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size; 1183bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4; 1184a8c744a8SWillem de Bruijn goto csum_partial; 1185bec1f6f6SWillem de Bruijn } 1186bec1f6f6SWillem de Bruijn 1187db8dac20SDavid S. Miller if (is_udplite) 1188d39d938cSVlad Yasevich csum = udplite_csum(skb); 1189d39d938cSVlad Yasevich else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */ 11904068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE; 11914068579eSTom Herbert goto send; 11924068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1193a8c744a8SWillem de Bruijn csum_partial: 1194d39d938cSVlad Yasevich udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len); 1195493c6be3SSridhar Samudrala goto send; 1196493c6be3SSridhar Samudrala } else 1197d39d938cSVlad Yasevich csum = udp_csum(skb); 1198db8dac20SDavid S. Miller 1199db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 12004c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 1201d39d938cSVlad Yasevich len, fl6->flowi6_proto, csum); 1202db8dac20SDavid S. Miller if (uh->check == 0) 1203db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 1204db8dac20SDavid S. Miller 1205493c6be3SSridhar Samudrala send: 1206d39d938cSVlad Yasevich err = ip6_send_skb(skb); 12076ce9e7b5SEric Dumazet if (err) { 12086ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 12096aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12106ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 12116ce9e7b5SEric Dumazet err = 0; 12126ce9e7b5SEric Dumazet } 12136aef70a8SEric Dumazet } else { 12146aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 12156ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 12166aef70a8SEric Dumazet } 1217d39d938cSVlad Yasevich return err; 1218d39d938cSVlad Yasevich } 1219d39d938cSVlad Yasevich 1220d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk) 1221d39d938cSVlad Yasevich { 1222d39d938cSVlad Yasevich struct sk_buff *skb; 1223d39d938cSVlad Yasevich struct udp_sock *up = udp_sk(sk); 1224d39d938cSVlad Yasevich struct flowi6 fl6; 1225d39d938cSVlad Yasevich int err = 0; 1226d39d938cSVlad Yasevich 1227d39d938cSVlad Yasevich if (up->pending == AF_INET) 1228d39d938cSVlad Yasevich return udp_push_pending_frames(sk); 1229d39d938cSVlad Yasevich 1230d39d938cSVlad Yasevich /* ip6_finish_skb will release the cork, so make a copy of 1231d39d938cSVlad Yasevich * fl6 here. 1232d39d938cSVlad Yasevich */ 1233d39d938cSVlad Yasevich fl6 = inet_sk(sk)->cork.fl.u.ip6; 1234d39d938cSVlad Yasevich 1235d39d938cSVlad Yasevich skb = ip6_finish_skb(sk); 1236d39d938cSVlad Yasevich if (!skb) 1237d39d938cSVlad Yasevich goto out; 1238d39d938cSVlad Yasevich 1239bec1f6f6SWillem de Bruijn err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base); 1240d39d938cSVlad Yasevich 1241db8dac20SDavid S. Miller out: 1242db8dac20SDavid S. Miller up->len = 0; 1243db8dac20SDavid S. Miller up->pending = 0; 1244db8dac20SDavid S. Miller return err; 1245db8dac20SDavid S. Miller } 1246db8dac20SDavid S. Miller 12471b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1248db8dac20SDavid S. Miller { 1249db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 1250db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1251db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1252db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 1253342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 125420c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 1255db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 125645f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 1257db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 12584c9483b2SDavid S. Miller struct flowi6 fl6; 1259db8dac20SDavid S. Miller struct dst_entry *dst; 126026879da5SWei Wang struct ipcm6_cookie ipc6; 1261db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 12629f542f61SAlexey Kodanev bool connected = false; 1263db8dac20SDavid S. Miller int ulen = len; 1264db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 1265db8dac20SDavid S. Miller int err; 1266db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1267db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1268db8dac20SDavid S. Miller 1269b515430aSWillem de Bruijn ipcm6_init(&ipc6); 1270bec1f6f6SWillem de Bruijn ipc6.gso_size = up->gso_size; 12715fdaa88dSWillem de Bruijn ipc6.sockc.tsflags = sk->sk_tsflags; 127226879da5SWei Wang 1273db8dac20SDavid S. Miller /* destination address check */ 1274db8dac20SDavid S. Miller if (sin6) { 1275db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 1276db8dac20SDavid S. Miller return -EINVAL; 1277db8dac20SDavid S. Miller 1278db8dac20SDavid S. Miller switch (sin6->sin6_family) { 1279db8dac20SDavid S. Miller case AF_INET6: 1280db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 1281db8dac20SDavid S. Miller return -EINVAL; 1282db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1283052d2369SJonathan T. Leighton if (ipv6_addr_any(daddr) && 1284052d2369SJonathan T. Leighton ipv6_addr_v4mapped(&np->saddr)) 1285052d2369SJonathan T. Leighton ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 1286052d2369SJonathan T. Leighton daddr); 1287db8dac20SDavid S. Miller break; 1288db8dac20SDavid S. Miller case AF_INET: 1289db8dac20SDavid S. Miller goto do_udp_sendmsg; 1290db8dac20SDavid S. Miller case AF_UNSPEC: 1291db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 1292db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 1293db8dac20SDavid S. Miller daddr = NULL; 1294db8dac20SDavid S. Miller break; 1295db8dac20SDavid S. Miller default: 1296db8dac20SDavid S. Miller return -EINVAL; 1297db8dac20SDavid S. Miller } 1298db8dac20SDavid S. Miller } else if (!up->pending) { 1299db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1300db8dac20SDavid S. Miller return -EDESTADDRREQ; 1301efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1302db8dac20SDavid S. Miller } else 1303db8dac20SDavid S. Miller daddr = NULL; 1304db8dac20SDavid S. Miller 1305db8dac20SDavid S. Miller if (daddr) { 1306db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 1307db8dac20SDavid S. Miller struct sockaddr_in sin; 1308db8dac20SDavid S. Miller sin.sin_family = AF_INET; 1309c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1310db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1311db8dac20SDavid S. Miller msg->msg_name = &sin; 1312db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 1313db8dac20SDavid S. Miller do_udp_sendmsg: 1314db8dac20SDavid S. Miller if (__ipv6_only_sock(sk)) 1315db8dac20SDavid S. Miller return -ENETUNREACH; 13161b784140SYing Xue return udp_sendmsg(sk, msg, len); 1317db8dac20SDavid S. Miller } 1318db8dac20SDavid S. Miller } 1319db8dac20SDavid S. Miller 1320db8dac20SDavid S. Miller if (up->pending == AF_INET) 13211b784140SYing Xue return udp_sendmsg(sk, msg, len); 1322db8dac20SDavid S. Miller 1323db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 1324db8dac20SDavid S. Miller better check is made in ip6_append_data(). 1325db8dac20SDavid S. Miller */ 1326db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 1327db8dac20SDavid S. Miller return -EMSGSIZE; 1328db8dac20SDavid S. Miller 132903485f2aSVlad Yasevich getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1330db8dac20SDavid S. Miller if (up->pending) { 1331db8dac20SDavid S. Miller /* 1332db8dac20SDavid S. Miller * There are pending frames. 1333db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1334db8dac20SDavid S. Miller */ 1335db8dac20SDavid S. Miller lock_sock(sk); 1336db8dac20SDavid S. Miller if (likely(up->pending)) { 1337db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 1338db8dac20SDavid S. Miller release_sock(sk); 1339db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1340db8dac20SDavid S. Miller } 1341db8dac20SDavid S. Miller dst = NULL; 1342db8dac20SDavid S. Miller goto do_append_data; 1343db8dac20SDavid S. Miller } 1344db8dac20SDavid S. Miller release_sock(sk); 1345db8dac20SDavid S. Miller } 1346db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1347db8dac20SDavid S. Miller 13484c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1349db8dac20SDavid S. Miller 1350db8dac20SDavid S. Miller if (sin6) { 1351db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 1352db8dac20SDavid S. Miller return -EINVAL; 1353db8dac20SDavid S. Miller 13541958b856SDavid S. Miller fl6.fl6_dport = sin6->sin6_port; 1355db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1356db8dac20SDavid S. Miller 1357db8dac20SDavid S. Miller if (np->sndflow) { 13584c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 13594c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 13604c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 136163159f29SIan Morris if (!flowlabel) 1362db8dac20SDavid S. Miller return -EINVAL; 1363db8dac20SDavid S. Miller } 1364db8dac20SDavid S. Miller } 1365db8dac20SDavid S. Miller 1366db8dac20SDavid S. Miller /* 1367db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 1368db8dac20SDavid S. Miller * sk->sk_dst_cache. 1369db8dac20SDavid S. Miller */ 1370db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 1371efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1372efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1373db8dac20SDavid S. Miller 1374db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 1375db8dac20SDavid S. Miller sin6->sin6_scope_id && 1376842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 13774c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 1378db8dac20SDavid S. Miller } else { 1379db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1380db8dac20SDavid S. Miller return -EDESTADDRREQ; 1381db8dac20SDavid S. Miller 13821958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 1383efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 13844c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 13859f542f61SAlexey Kodanev connected = true; 1386db8dac20SDavid S. Miller } 1387db8dac20SDavid S. Miller 13884c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 13894c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 1390db8dac20SDavid S. Miller 13914c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 13924c9483b2SDavid S. Miller fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 13939f690db7SYang Hongyang 13944c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 1395e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 139651953d5bSBrian Haley 1397db8dac20SDavid S. Miller if (msg->msg_controllen) { 1398db8dac20SDavid S. Miller opt = &opt_space; 1399db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 1400db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 140126879da5SWei Wang ipc6.opt = opt; 1402db8dac20SDavid S. Miller 14032e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc6.gso_size); 14042e8de857SWillem de Bruijn if (err > 0) 14052e8de857SWillem de Bruijn err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, 14065fdaa88dSWillem de Bruijn &ipc6); 1407db8dac20SDavid S. Miller if (err < 0) { 1408db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1409db8dac20SDavid S. Miller return err; 1410db8dac20SDavid S. Miller } 14114c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 14124c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 141363159f29SIan Morris if (!flowlabel) 1414db8dac20SDavid S. Miller return -EINVAL; 1415db8dac20SDavid S. Miller } 1416db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 1417db8dac20SDavid S. Miller opt = NULL; 14189f542f61SAlexey Kodanev connected = false; 1419db8dac20SDavid S. Miller } 142045f6fad8SEric Dumazet if (!opt) { 142145f6fad8SEric Dumazet opt = txopt_get(np); 142245f6fad8SEric Dumazet opt_to_free = opt; 142345f6fad8SEric Dumazet } 1424db8dac20SDavid S. Miller if (flowlabel) 1425db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 1426db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 142726879da5SWei Wang ipc6.opt = opt; 1428db8dac20SDavid S. Miller 14294c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 1430876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 14314e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 1432876c7f41SBrian Haley else 14334c9483b2SDavid S. Miller fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 14344c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 14354e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 14361958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 1437db8dac20SDavid S. Miller 14381cedee13SAndrey Ignatov if (cgroup_bpf_enabled && !connected) { 14391cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, 14401cedee13SAndrey Ignatov (struct sockaddr *)sin6, &fl6.saddr); 14411cedee13SAndrey Ignatov if (err) 14421cedee13SAndrey Ignatov goto out_no_dst; 14431cedee13SAndrey Ignatov if (sin6) { 14441cedee13SAndrey Ignatov if (ipv6_addr_v4mapped(&sin6->sin6_addr)) { 14451cedee13SAndrey Ignatov /* BPF program rewrote IPv6-only by IPv4-mapped 14461cedee13SAndrey Ignatov * IPv6. It's currently unsupported. 14471cedee13SAndrey Ignatov */ 14481cedee13SAndrey Ignatov err = -ENOTSUPP; 14491cedee13SAndrey Ignatov goto out_no_dst; 14501cedee13SAndrey Ignatov } 14511cedee13SAndrey Ignatov if (sin6->sin6_port == 0) { 14521cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */ 14531cedee13SAndrey Ignatov err = -EINVAL; 14541cedee13SAndrey Ignatov goto out_no_dst; 14551cedee13SAndrey Ignatov } 14561cedee13SAndrey Ignatov fl6.fl6_dport = sin6->sin6_port; 14571cedee13SAndrey Ignatov fl6.daddr = sin6->sin6_addr; 14581cedee13SAndrey Ignatov } 14591cedee13SAndrey Ignatov } 14601cedee13SAndrey Ignatov 14614c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 146220c59de2SArnaud Ebalard if (final_p) 14639f542f61SAlexey Kodanev connected = false; 1464db8dac20SDavid S. Miller 14654c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) { 14664c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 14679f542f61SAlexey Kodanev connected = false; 1468c4062dfcSErich E. Hoover } else if (!fl6.flowi6_oif) 1469c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 1470db8dac20SDavid S. Miller 14714c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 1472db8dac20SDavid S. Miller 147338b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 147438b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 147538b7097bSHannes Frederic Sowa 147638b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 147738b7097bSHannes Frederic Sowa 14784f858c56SAlexey Kodanev dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected); 147968d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 148068d0c6d3SDavid S. Miller err = PTR_ERR(dst); 148168d0c6d3SDavid S. Miller dst = NULL; 1482db8dac20SDavid S. Miller goto out; 1483db8dac20SDavid S. Miller } 1484db8dac20SDavid S. Miller 148526879da5SWei Wang if (ipc6.hlimit < 0) 148626879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 1487db8dac20SDavid S. Miller 1488db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1489db8dac20SDavid S. Miller goto do_confirm; 1490db8dac20SDavid S. Miller back_from_confirm: 1491db8dac20SDavid S. Miller 149203485f2aSVlad Yasevich /* Lockless fast path for the non-corking case */ 149303485f2aSVlad Yasevich if (!corkreq) { 14941cd7884dSWillem de Bruijn struct inet_cork_full cork; 149503485f2aSVlad Yasevich struct sk_buff *skb; 149603485f2aSVlad Yasevich 149703485f2aSVlad Yasevich skb = ip6_make_skb(sk, getfrag, msg, ulen, 149826879da5SWei Wang sizeof(struct udphdr), &ipc6, 149903485f2aSVlad Yasevich &fl6, (struct rt6_info *)dst, 15005fdaa88dSWillem de Bruijn msg->msg_flags, &cork); 150103485f2aSVlad Yasevich err = PTR_ERR(skb); 150203485f2aSVlad Yasevich if (!IS_ERR_OR_NULL(skb)) 1503bec1f6f6SWillem de Bruijn err = udp_v6_send_skb(skb, &fl6, &cork.base); 15044f858c56SAlexey Kodanev goto out; 150503485f2aSVlad Yasevich } 150603485f2aSVlad Yasevich 1507db8dac20SDavid S. Miller lock_sock(sk); 1508db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1509db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1510db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1511db8dac20SDavid S. Miller release_sock(sk); 1512db8dac20SDavid S. Miller 1513ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n"); 1514db8dac20SDavid S. Miller err = -EINVAL; 1515db8dac20SDavid S. Miller goto out; 1516db8dac20SDavid S. Miller } 1517db8dac20SDavid S. Miller 1518db8dac20SDavid S. Miller up->pending = AF_INET6; 1519db8dac20SDavid S. Miller 1520db8dac20SDavid S. Miller do_append_data: 152126879da5SWei Wang if (ipc6.dontfrag < 0) 152226879da5SWei Wang ipc6.dontfrag = np->dontfrag; 1523db8dac20SDavid S. Miller up->len += ulen; 152426879da5SWei Wang err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 152526879da5SWei Wang &ipc6, &fl6, (struct rt6_info *)dst, 15265fdaa88dSWillem de Bruijn corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1527db8dac20SDavid S. Miller if (err) 1528db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1529db8dac20SDavid S. Miller else if (!corkreq) 1530db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 1531db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1532db8dac20SDavid S. Miller up->pending = 0; 1533db8dac20SDavid S. Miller 153403485f2aSVlad Yasevich if (err > 0) 153503485f2aSVlad Yasevich err = np->recverr ? net_xmit_errno(err) : 0; 153603485f2aSVlad Yasevich release_sock(sk); 153703485f2aSVlad Yasevich 1538db8dac20SDavid S. Miller out: 1539a3c96089SYOSHIFUJI Hideaki dst_release(dst); 15401cedee13SAndrey Ignatov out_no_dst: 1541db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 154245f6fad8SEric Dumazet txopt_put(opt_to_free); 1543db8dac20SDavid S. Miller if (!err) 1544db8dac20SDavid S. Miller return len; 1545db8dac20SDavid S. Miller /* 1546db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1547db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1548db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1549db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1550db8dac20SDavid S. Miller * seems like overkill. 1551db8dac20SDavid S. Miller */ 1552db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 15536aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 1554235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1555db8dac20SDavid S. Miller } 1556db8dac20SDavid S. Miller return err; 1557db8dac20SDavid S. Miller 1558db8dac20SDavid S. Miller do_confirm: 15590dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 15600dec879fSJulian Anastasov dst_confirm_neigh(dst, &fl6.daddr); 1561db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1562db8dac20SDavid S. Miller goto back_from_confirm; 1563db8dac20SDavid S. Miller err = 0; 1564db8dac20SDavid S. Miller goto out; 1565db8dac20SDavid S. Miller } 1566db8dac20SDavid S. Miller 15677d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1568db8dac20SDavid S. Miller { 156944046a59STom Parkin struct udp_sock *up = udp_sk(sk); 1570db8dac20SDavid S. Miller lock_sock(sk); 1571db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1572db8dac20SDavid S. Miller release_sock(sk); 1573db8dac20SDavid S. Miller 157460fb9567SPaolo Abeni if (static_branch_unlikely(&udpv6_encap_needed_key)) { 157560fb9567SPaolo Abeni if (up->encap_type) { 157644046a59STom Parkin void (*encap_destroy)(struct sock *sk); 15776aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy); 157844046a59STom Parkin if (encap_destroy) 157944046a59STom Parkin encap_destroy(sk); 158044046a59STom Parkin } 158160fb9567SPaolo Abeni if (up->encap_enabled) 158260fb9567SPaolo Abeni static_branch_disable(&udpv6_encap_needed_key); 158360fb9567SPaolo Abeni } 158444046a59STom Parkin 1585db8dac20SDavid S. Miller inet6_destroy_sock(sk); 1586db8dac20SDavid S. Miller } 1587db8dac20SDavid S. Miller 1588db8dac20SDavid S. Miller /* 1589db8dac20SDavid S. Miller * Socket option code for UDP 1590db8dac20SDavid S. Miller */ 1591db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname, 1592b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1593db8dac20SDavid S. Miller { 1594db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1595db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1596db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1597db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1598db8dac20SDavid S. Miller } 1599db8dac20SDavid S. Miller 1600db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1601db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 1602b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1603db8dac20SDavid S. Miller { 1604db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1605db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1606db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1607db8dac20SDavid S. Miller return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); 1608db8dac20SDavid S. Miller } 1609db8dac20SDavid S. Miller #endif 1610db8dac20SDavid S. Miller 1611db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1612db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1613db8dac20SDavid S. Miller { 1614db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1615db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1616db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1617db8dac20SDavid S. Miller } 1618db8dac20SDavid S. Miller 1619db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1620db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 1621db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1622db8dac20SDavid S. Miller { 1623db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1624db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1625db8dac20SDavid S. Miller return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); 1626db8dac20SDavid S. Miller } 1627db8dac20SDavid S. Miller #endif 1628db8dac20SDavid S. Miller 1629a8e3bb34SDavid Ahern /* thinking of making this const? Don't. 1630a8e3bb34SDavid Ahern * early_demux can change based on sysctl. 1631a8e3bb34SDavid Ahern */ 1632dddb64bcSsubashab@codeaurora.org static struct inet6_protocol udpv6_protocol = { 16335425077dSsubashab@codeaurora.org .early_demux = udp_v6_early_demux, 1634dddb64bcSsubashab@codeaurora.org .early_demux_handler = udp_v6_early_demux, 1635db8dac20SDavid S. Miller .handler = udpv6_rcv, 1636db8dac20SDavid S. Miller .err_handler = udpv6_err, 1637db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1638db8dac20SDavid S. Miller }; 1639db8dac20SDavid S. Miller 1640db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1641db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1642db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1643db8dac20SDavid S. Miller { 164417ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 164517ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 164617ef66afSLorenzo Colitti } else { 164717ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket; 164817ef66afSLorenzo Colitti struct inet_sock *inet = inet_sk(v); 164917ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport); 165017ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport); 16516c206b20SPaolo Abeni __ip6_dgram_sock_seq_show(seq, v, srcp, destp, 16526c206b20SPaolo Abeni udp_rqueue_get(v), bucket); 165317ef66afSLorenzo Colitti } 1654db8dac20SDavid S. Miller return 0; 1655db8dac20SDavid S. Miller } 1656db8dac20SDavid S. Miller 1657c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = { 1658a3d2599bSChristoph Hellwig .start = udp_seq_start, 1659a3d2599bSChristoph Hellwig .next = udp_seq_next, 1660a3d2599bSChristoph Hellwig .stop = udp_seq_stop, 1661a3d2599bSChristoph Hellwig .show = udp6_seq_show, 1662a3d2599bSChristoph Hellwig }; 1663c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops); 166473cb88ecSArjan van de Ven 1665db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1666db8dac20SDavid S. Miller .family = AF_INET6, 1667645ca708SEric Dumazet .udp_table = &udp_table, 1668db8dac20SDavid S. Miller }; 1669db8dac20SDavid S. Miller 16702c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net) 1671db8dac20SDavid S. Miller { 1672c3506372SChristoph Hellwig if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops, 1673c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp6_seq_afinfo)) 1674a3d2599bSChristoph Hellwig return -ENOMEM; 1675a3d2599bSChristoph Hellwig return 0; 1676db8dac20SDavid S. Miller } 1677db8dac20SDavid S. Miller 1678ec120da6SIan Morris void udp6_proc_exit(struct net *net) 1679ec120da6SIan Morris { 1680a3d2599bSChristoph Hellwig remove_proc_entry("udp6", net->proc_net); 1681db8dac20SDavid S. Miller } 1682db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1683db8dac20SDavid S. Miller 1684db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1685db8dac20SDavid S. Miller 1686db8dac20SDavid S. Miller struct proto udpv6_prot = { 1687db8dac20SDavid S. Miller .name = "UDPv6", 1688db8dac20SDavid S. Miller .owner = THIS_MODULE, 1689db8dac20SDavid S. Miller .close = udp_lib_close, 1690d74bad4eSAndrey Ignatov .pre_connect = udpv6_pre_connect, 1691db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1692db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1693db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1694850cbaddSPaolo Abeni .init = udp_init_sock, 1695db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1696db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1697db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1698db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1699db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 1700e646b657SMartin KaFai Lau .release_cb = ip6_datagram_release_cb, 1701db8dac20SDavid S. Miller .hash = udp_lib_hash, 1702db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1703719f8358SEric Dumazet .rehash = udp_v6_rehash, 1704db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 1705db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1706db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 17071e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), 17081e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), 1709db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1710645ca708SEric Dumazet .h.udp_table = &udp_table, 1711db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1712db8dac20SDavid S. Miller .compat_setsockopt = compat_udpv6_setsockopt, 1713db8dac20SDavid S. Miller .compat_getsockopt = compat_udpv6_getsockopt, 1714db8dac20SDavid S. Miller #endif 17155d77dca8SDavid Ahern .diag_destroy = udp_abort, 1716db8dac20SDavid S. Miller }; 1717db8dac20SDavid S. Miller 1718db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1719db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1720db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1721db8dac20SDavid S. Miller .prot = &udpv6_prot, 1722db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1723db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1724db8dac20SDavid S. Miller }; 1725db8dac20SDavid S. Miller 1726db8dac20SDavid S. Miller int __init udpv6_init(void) 1727db8dac20SDavid S. Miller { 1728db8dac20SDavid S. Miller int ret; 1729db8dac20SDavid S. Miller 17303336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 17313336288aSVlad Yasevich if (ret) 1732c6b641a4SVlad Yasevich goto out; 17333336288aSVlad Yasevich 1734db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1735db8dac20SDavid S. Miller if (ret) 1736db8dac20SDavid S. Miller goto out_udpv6_protocol; 1737db8dac20SDavid S. Miller out: 1738db8dac20SDavid S. Miller return ret; 1739db8dac20SDavid S. Miller 1740db8dac20SDavid S. Miller out_udpv6_protocol: 1741db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1742db8dac20SDavid S. Miller goto out; 1743db8dac20SDavid S. Miller } 1744db8dac20SDavid S. Miller 1745db8dac20SDavid S. Miller void udpv6_exit(void) 1746db8dac20SDavid S. Miller { 1747db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1748db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1749db8dac20SDavid S. Miller } 1750