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> 48db8dac20SDavid S. Miller #include <net/xfrm.h> 490bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h> 5072289b96STom Herbert #include <net/inet6_hashtables.h> 51076bb0c8SEliezer Tamir #include <net/busy_poll.h> 52e32ea7e7SCraig Gallek #include <net/sock_reuseport.h> 53db8dac20SDavid S. Miller 54db8dac20SDavid S. Miller #include <linux/proc_fs.h> 55db8dac20SDavid S. Miller #include <linux/seq_file.h> 5622911fc5SEric Dumazet #include <trace/events/skb.h> 57db8dac20SDavid S. Miller #include "udp_impl.h" 58db8dac20SDavid S. Miller 5963a6fff3SRobert Shearman static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb) 6063a6fff3SRobert Shearman { 6163a6fff3SRobert Shearman #if defined(CONFIG_NET_L3_MASTER_DEV) 6263a6fff3SRobert Shearman if (!net->ipv4.sysctl_udp_l3mdev_accept && 6363a6fff3SRobert Shearman skb && ipv6_l3mdev_skb(IP6CB(skb)->flags)) 6463a6fff3SRobert Shearman return true; 6563a6fff3SRobert Shearman #endif 6663a6fff3SRobert Shearman return false; 6763a6fff3SRobert Shearman } 6863a6fff3SRobert Shearman 696eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net, 70b50026b5SHannes Frederic Sowa const struct in6_addr *laddr, 71b50026b5SHannes Frederic Sowa const u16 lport, 72b50026b5SHannes Frederic Sowa const struct in6_addr *faddr, 73b50026b5SHannes Frederic Sowa const __be16 fport) 74b50026b5SHannes Frederic Sowa { 751bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly; 761bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly; 771bbdceefSHannes Frederic Sowa 781bbdceefSHannes Frederic Sowa u32 lhash, fhash; 791bbdceefSHannes Frederic Sowa 801bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret, 811bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret)); 821bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret, 831bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret)); 841bbdceefSHannes Frederic Sowa 851bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3]; 861bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 871bbdceefSHannes Frederic Sowa 88b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport, 891bbdceefSHannes Frederic Sowa udp_ipv6_hash_secret + net_hash_mix(net)); 90b50026b5SHannes Frederic Sowa } 91b50026b5SHannes Frederic Sowa 926eada011SEric Dumazet static u32 udp6_portaddr_hash(const struct net *net, 93d4cada4aSEric Dumazet const struct in6_addr *addr6, 94d4cada4aSEric Dumazet unsigned int port) 95d4cada4aSEric Dumazet { 96d4cada4aSEric Dumazet unsigned int hash, mix = net_hash_mix(net); 97d4cada4aSEric Dumazet 98d4cada4aSEric Dumazet if (ipv6_addr_any(addr6)) 99d4cada4aSEric Dumazet hash = jhash_1word(0, mix); 100856540eeSBrian Haley else if (ipv6_addr_v4mapped(addr6)) 1010eae88f3SEric Dumazet hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix); 102d4cada4aSEric Dumazet else 1030eae88f3SEric Dumazet hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix); 104d4cada4aSEric Dumazet 105d4cada4aSEric Dumazet return hash ^ port; 106d4cada4aSEric Dumazet } 107d4cada4aSEric Dumazet 1086ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 109db8dac20SDavid S. Miller { 11030fff923SEric Dumazet unsigned int hash2_nulladdr = 11130fff923SEric Dumazet udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 11230fff923SEric Dumazet unsigned int hash2_partial = 113efe4208fSEric Dumazet udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 11430fff923SEric Dumazet 115d4cada4aSEric Dumazet /* precompute partial secondary hash */ 11630fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 117fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr); 118db8dac20SDavid S. Miller } 119db8dac20SDavid S. Miller 120719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk) 121719f8358SEric Dumazet { 122719f8358SEric Dumazet u16 new_hash = udp6_portaddr_hash(sock_net(sk), 123efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr, 124719f8358SEric Dumazet inet_sk(sk)->inet_num); 125719f8358SEric Dumazet 126719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 127719f8358SEric Dumazet } 128719f8358SEric Dumazet 129d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net, 13088440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 131d1e37288SSu, Xuemin const struct in6_addr *daddr, unsigned short hnum, 13263a6fff3SRobert Shearman int dif, bool exact_dif) 133db8dac20SDavid S. Miller { 13460c04aecSJoe Perches int score; 13560c04aecSJoe Perches struct inet_sock *inet; 136db8dac20SDavid S. Miller 13760c04aecSJoe Perches if (!net_eq(sock_net(sk), net) || 13860c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum || 13960c04aecSJoe Perches sk->sk_family != PF_INET6) 14060c04aecSJoe Perches return -1; 141645ca708SEric Dumazet 142645ca708SEric Dumazet score = 0; 14360c04aecSJoe Perches inet = inet_sk(sk); 14460c04aecSJoe Perches 145c720c7e8SEric Dumazet if (inet->inet_dport) { 146c720c7e8SEric Dumazet if (inet->inet_dport != sport) 147645ca708SEric Dumazet return -1; 148db8dac20SDavid S. Miller score++; 149db8dac20SDavid S. Miller } 15060c04aecSJoe Perches 151efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { 152efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 153645ca708SEric Dumazet return -1; 154db8dac20SDavid S. Miller score++; 155db8dac20SDavid S. Miller } 15660c04aecSJoe Perches 157efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 158efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 159645ca708SEric Dumazet return -1; 160db8dac20SDavid S. Miller score++; 161db8dac20SDavid S. Miller } 16260c04aecSJoe Perches 16363a6fff3SRobert Shearman if (sk->sk_bound_dev_if || exact_dif) { 164db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 165645ca708SEric Dumazet return -1; 166db8dac20SDavid S. Miller score++; 167db8dac20SDavid S. Miller } 16860c04aecSJoe Perches 16970da268bSEric Dumazet if (sk->sk_incoming_cpu == raw_smp_processor_id()) 17070da268bSEric Dumazet score++; 17170da268bSEric Dumazet 172645ca708SEric Dumazet return score; 173645ca708SEric Dumazet } 174645ca708SEric Dumazet 175d1e37288SSu, Xuemin /* called with rcu_read_lock() */ 176fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net, 177fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 178fddc17deSEric Dumazet const struct in6_addr *daddr, unsigned int hnum, int dif, 17963a6fff3SRobert Shearman bool exact_dif, struct udp_hslot *hslot2, 1801134158bSCraig Gallek struct sk_buff *skb) 181fddc17deSEric Dumazet { 182fddc17deSEric Dumazet struct sock *sk, *result; 18372289b96STom Herbert int score, badness, matches = 0, reuseport = 0; 18472289b96STom Herbert u32 hash = 0; 185fddc17deSEric Dumazet 186fddc17deSEric Dumazet result = NULL; 187fddc17deSEric Dumazet badness = -1; 188ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 189d1e37288SSu, Xuemin score = compute_score(sk, net, saddr, sport, 19063a6fff3SRobert Shearman daddr, hnum, dif, exact_dif); 191fddc17deSEric Dumazet if (score > badness) { 19272289b96STom Herbert reuseport = sk->sk_reuseport; 19372289b96STom Herbert if (reuseport) { 194b50026b5SHannes Frederic Sowa hash = udp6_ehashfn(net, daddr, hnum, 19572289b96STom Herbert saddr, sport); 196ed0dfffdSEric Dumazet 197ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 1981134158bSCraig Gallek sizeof(struct udphdr)); 199ca065d0cSEric Dumazet if (result) 200ca065d0cSEric Dumazet return result; 20172289b96STom Herbert matches = 1; 20270da268bSEric Dumazet } 203ca065d0cSEric Dumazet result = sk; 204ca065d0cSEric Dumazet badness = score; 20572289b96STom Herbert } else if (score == badness && reuseport) { 20672289b96STom Herbert matches++; 2078fc54f68SDaniel Borkmann if (reciprocal_scale(hash, matches) == 0) 20872289b96STom Herbert result = sk; 20972289b96STom Herbert hash = next_pseudo_random32(hash); 210fddc17deSEric Dumazet } 211fddc17deSEric Dumazet } 212fddc17deSEric Dumazet return result; 213fddc17deSEric Dumazet } 214fddc17deSEric Dumazet 215ca065d0cSEric Dumazet /* rcu_read_lock() must be held */ 216fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net, 21788440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 21888440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 219538950a1SCraig Gallek int dif, struct udp_table *udptable, 220538950a1SCraig Gallek struct sk_buff *skb) 221645ca708SEric Dumazet { 222271b72c7SEric Dumazet struct sock *sk, *result; 223645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 224fddc17deSEric Dumazet unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 225fddc17deSEric Dumazet struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 22663a6fff3SRobert Shearman bool exact_dif = udp6_lib_exact_dif_match(net, skb); 22772289b96STom Herbert int score, badness, matches = 0, reuseport = 0; 22872289b96STom Herbert u32 hash = 0; 229645ca708SEric Dumazet 230fddc17deSEric Dumazet if (hslot->count > 10) { 231fddc17deSEric Dumazet hash2 = udp6_portaddr_hash(net, daddr, hnum); 232fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 233fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 234fddc17deSEric Dumazet if (hslot->count < hslot2->count) 235fddc17deSEric Dumazet goto begin; 236fddc17deSEric Dumazet 237fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport, 23863a6fff3SRobert Shearman daddr, hnum, dif, exact_dif, 239d1e37288SSu, Xuemin hslot2, skb); 240fddc17deSEric Dumazet if (!result) { 241d1e37288SSu, Xuemin unsigned int old_slot2 = slot2; 242fddc17deSEric Dumazet hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum); 243fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 244d1e37288SSu, Xuemin /* avoid searching the same slot again. */ 245d1e37288SSu, Xuemin if (unlikely(slot2 == old_slot2)) 246d1e37288SSu, Xuemin return result; 247d1e37288SSu, Xuemin 248fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 249fddc17deSEric Dumazet if (hslot->count < hslot2->count) 250fddc17deSEric Dumazet goto begin; 251fddc17deSEric Dumazet 2521223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport, 253d1e37288SSu, Xuemin daddr, hnum, dif, 25463a6fff3SRobert Shearman exact_dif, hslot2, 25563a6fff3SRobert Shearman skb); 256fddc17deSEric Dumazet } 257fddc17deSEric Dumazet return result; 258fddc17deSEric Dumazet } 259271b72c7SEric Dumazet begin: 260271b72c7SEric Dumazet result = NULL; 261271b72c7SEric Dumazet badness = -1; 262ca065d0cSEric Dumazet sk_for_each_rcu(sk, &hslot->head) { 26363a6fff3SRobert Shearman score = compute_score(sk, net, saddr, sport, daddr, hnum, dif, 26463a6fff3SRobert Shearman exact_dif); 265645ca708SEric Dumazet if (score > badness) { 26672289b96STom Herbert reuseport = sk->sk_reuseport; 26772289b96STom Herbert if (reuseport) { 268b50026b5SHannes Frederic Sowa hash = udp6_ehashfn(net, daddr, hnum, 26972289b96STom Herbert saddr, sport); 270ca065d0cSEric Dumazet result = reuseport_select_sock(sk, hash, skb, 271538950a1SCraig Gallek sizeof(struct udphdr)); 272ca065d0cSEric Dumazet if (result) 273ca065d0cSEric Dumazet return result; 27472289b96STom Herbert matches = 1; 27572289b96STom Herbert } 276ca065d0cSEric Dumazet result = sk; 277ca065d0cSEric Dumazet badness = score; 27872289b96STom Herbert } else if (score == badness && reuseport) { 27972289b96STom Herbert matches++; 2808fc54f68SDaniel Borkmann if (reciprocal_scale(hash, matches) == 0) 28172289b96STom Herbert result = sk; 28272289b96STom Herbert hash = next_pseudo_random32(hash); 283db8dac20SDavid S. Miller } 284db8dac20SDavid S. Miller } 285db8dac20SDavid S. Miller return result; 286db8dac20SDavid S. Miller } 287fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 288db8dac20SDavid S. Miller 289607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 290607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 291645ca708SEric Dumazet struct udp_table *udptable) 292607c4aafSKOVACS Krisztian { 293b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 294607c4aafSKOVACS Krisztian 295ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 296607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 297538950a1SCraig Gallek udptable, skb); 298607c4aafSKOVACS Krisztian } 299607c4aafSKOVACS Krisztian 30063058308STom Herbert struct sock *udp6_lib_lookup_skb(struct sk_buff *skb, 30163058308STom Herbert __be16 sport, __be16 dport) 30263058308STom Herbert { 30363058308STom Herbert const struct ipv6hdr *iph = ipv6_hdr(skb); 30463058308STom Herbert 305ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport, 30663058308STom Herbert &iph->daddr, dport, inet6_iif(skb), 30763058308STom Herbert &udp_table, skb); 30863058308STom Herbert } 30963058308STom Herbert EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb); 31063058308STom Herbert 311ca065d0cSEric Dumazet /* Must be called under rcu_read_lock(). 312ca065d0cSEric Dumazet * Does increment socket refcount. 313ca065d0cSEric Dumazet */ 314ca065d0cSEric Dumazet #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \ 31530f58158SArnd Bergmann IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY) || \ 31630f58158SArnd Bergmann IS_ENABLED(CONFIG_NF_SOCKET_IPV6) 317aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 318aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif) 319aa976fc0SBalazs Scheidler { 320ca065d0cSEric Dumazet struct sock *sk; 321ca065d0cSEric Dumazet 322ca065d0cSEric Dumazet sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport, 323ca065d0cSEric Dumazet dif, &udp_table, NULL); 32441c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) 325ca065d0cSEric Dumazet sk = NULL; 326ca065d0cSEric Dumazet return sk; 327aa976fc0SBalazs Scheidler } 328aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup); 329ca065d0cSEric Dumazet #endif 330aa976fc0SBalazs Scheidler 331cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the 332cb891fa6SPaolo Abeni * scratch area space; note that the IP6CB flags is still in the first 333cb891fa6SPaolo Abeni * cacheline, so checking for jumbograms is cheap 334cb891fa6SPaolo Abeni */ 335cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb) 336cb891fa6SPaolo Abeni { 337cb891fa6SPaolo Abeni return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb); 338cb891fa6SPaolo Abeni } 339cb891fa6SPaolo Abeni 340db8dac20SDavid S. Miller /* 341db8dac20SDavid S. Miller * This should be easy, if there is something there we 342db8dac20SDavid S. Miller * return it, otherwise we block. 343db8dac20SDavid S. Miller */ 344db8dac20SDavid S. Miller 3451b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 346db8dac20SDavid S. Miller int noblock, int flags, int *addr_len) 347db8dac20SDavid S. Miller { 348db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 349db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 350db8dac20SDavid S. Miller struct sk_buff *skb; 35159c2cdaeSDavid S. Miller unsigned int ulen, copied; 352627d2d6bSsamanthakumar int peeked, peeking, off; 353db8dac20SDavid S. Miller int err; 354db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 355197c949eSEric Dumazet bool checksum_valid = false; 356f26ba175SWei Yongjun int is_udp4; 357db8dac20SDavid S. Miller 358db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 35985fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 360db8dac20SDavid S. Miller 3614b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 36285fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 3634b340ae2SBrian Haley 364db8dac20SDavid S. Miller try_again: 365a0917e0bSMatthew Dawson peeking = flags & MSG_PEEK; 366a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags); 3677c13f97fSPaolo Abeni skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err); 368db8dac20SDavid S. Miller if (!skb) 369627d2d6bSsamanthakumar return err; 370db8dac20SDavid S. Miller 371cb891fa6SPaolo Abeni ulen = udp6_skb_len(skb); 37259c2cdaeSDavid S. Miller copied = len; 373627d2d6bSsamanthakumar if (copied > ulen - off) 374627d2d6bSsamanthakumar copied = ulen - off; 37559c2cdaeSDavid S. Miller else if (copied < ulen) 376db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 377db8dac20SDavid S. Miller 378f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 379f26ba175SWei Yongjun 380db8dac20SDavid S. Miller /* 381db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 382db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 383db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 384db8dac20SDavid S. Miller */ 385db8dac20SDavid S. Miller 386d21dbdfeSEric Dumazet if (copied < ulen || peeking || 387d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) { 38867a51780SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) || 38967a51780SPaolo Abeni !__udp_lib_checksum_complete(skb); 390197c949eSEric Dumazet if (!checksum_valid) 391db8dac20SDavid S. Miller goto csum_copy_err; 392db8dac20SDavid S. Miller } 393db8dac20SDavid S. Miller 39467a51780SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) { 39567a51780SPaolo Abeni if (udp_skb_is_linear(skb)) 39667a51780SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter); 39767a51780SPaolo Abeni else 398627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied); 39967a51780SPaolo Abeni } else { 400627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg); 401db8dac20SDavid S. Miller if (err == -EINVAL) 402db8dac20SDavid S. Miller goto csum_copy_err; 403db8dac20SDavid S. Miller } 40422911fc5SEric Dumazet if (unlikely(err)) { 405979402b1SEric Dumazet if (!peeked) { 406979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 407979402b1SEric Dumazet if (is_udp4) 4086aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 409979402b1SEric Dumazet is_udplite); 410979402b1SEric Dumazet else 4116aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, 412979402b1SEric Dumazet is_udplite); 413979402b1SEric Dumazet } 414850cbaddSPaolo Abeni kfree_skb(skb); 415627d2d6bSsamanthakumar return err; 41622911fc5SEric Dumazet } 417f26ba175SWei Yongjun if (!peeked) { 418f26ba175SWei Yongjun if (is_udp4) 4196aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS, 4206aef70a8SEric Dumazet is_udplite); 421f26ba175SWei Yongjun else 4226aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS, 4236aef70a8SEric Dumazet is_udplite); 424f26ba175SWei Yongjun } 425db8dac20SDavid S. Miller 4263b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 427db8dac20SDavid S. Miller 428db8dac20SDavid S. Miller /* Copy the address. */ 429db8dac20SDavid S. Miller if (msg->msg_name) { 430342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 431db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 432db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 433db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 434db8dac20SDavid S. Miller 435842df073SHannes Frederic Sowa if (is_udp4) { 436b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 437b301e82cSBrian Haley &sin6->sin6_addr); 438842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0; 439842df073SHannes Frederic Sowa } else { 4404e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 441842df073SHannes Frederic Sowa sin6->sin6_scope_id = 442842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr, 4434330487aSDuan Jiong inet6_iif(skb)); 444db8dac20SDavid S. Miller } 445bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 446db8dac20SDavid S. Miller } 4474b261c75SHannes Frederic Sowa 4484b261c75SHannes Frederic Sowa if (np->rxopt.all) 4494b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb); 4504b261c75SHannes Frederic Sowa 451f26ba175SWei Yongjun if (is_udp4) { 452db8dac20SDavid S. Miller if (inet->cmsg_flags) 453ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb, 45410df8e61SEric Dumazet sizeof(struct udphdr), off); 455db8dac20SDavid S. Miller } else { 456db8dac20SDavid S. Miller if (np->rxopt.all) 4574b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb); 458db8dac20SDavid S. Miller } 459db8dac20SDavid S. Miller 46059c2cdaeSDavid S. Miller err = copied; 461db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 462db8dac20SDavid S. Miller err = ulen; 463db8dac20SDavid S. Miller 464850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err); 465db8dac20SDavid S. Miller return err; 466db8dac20SDavid S. Miller 467db8dac20SDavid S. Miller csum_copy_err: 4682276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags, 4692276f58aSPaolo Abeni udp_skb_destructor)) { 4706a5dc9e5SEric Dumazet if (is_udp4) { 4716aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 4726a5dc9e5SEric Dumazet UDP_MIB_CSUMERRORS, is_udplite); 4736aef70a8SEric Dumazet UDP_INC_STATS(sock_net(sk), 4740856f939SWei Yongjun UDP_MIB_INERRORS, is_udplite); 4756a5dc9e5SEric Dumazet } else { 4766aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 4776a5dc9e5SEric Dumazet UDP_MIB_CSUMERRORS, is_udplite); 4786aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 4790856f939SWei Yongjun UDP_MIB_INERRORS, is_udplite); 4800856f939SWei Yongjun } 4816a5dc9e5SEric Dumazet } 482850cbaddSPaolo Abeni kfree_skb(skb); 483db8dac20SDavid S. Miller 484beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */ 485beb39db5SEric Dumazet cond_resched(); 4869cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 487db8dac20SDavid S. Miller goto try_again; 488db8dac20SDavid S. Miller } 489db8dac20SDavid S. Miller 490db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 491d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 492645ca708SEric Dumazet struct udp_table *udptable) 493db8dac20SDavid S. Miller { 494db8dac20SDavid S. Miller struct ipv6_pinfo *np; 495b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 496b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr; 497b71d1d42SEric Dumazet const struct in6_addr *daddr = &hdr->daddr; 498db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset); 499db8dac20SDavid S. Miller struct sock *sk; 500e0d8c1b7SWei Wang int harderr; 501db8dac20SDavid S. Miller int err; 5027304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 503db8dac20SDavid S. Miller 504e32ea7e7SCraig Gallek sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source, 505538950a1SCraig Gallek inet6_iif(skb), udptable, skb); 50663159f29SIan Morris if (!sk) { 507a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), 5087304fe46SDuan Jiong ICMP6_MIB_INERRORS); 509db8dac20SDavid S. Miller return; 5107304fe46SDuan Jiong } 511db8dac20SDavid S. Miller 512e0d8c1b7SWei Wang harderr = icmpv6_err_convert(type, code, &err); 513e0d8c1b7SWei Wang np = inet6_sk(sk); 514e0d8c1b7SWei Wang 51593b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) { 51693b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk)) 51793b36cf3SHannes Frederic Sowa goto out; 51881aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 519e0d8c1b7SWei Wang if (np->pmtudisc != IPV6_PMTUDISC_DONT) 520e0d8c1b7SWei Wang harderr = 1; 52193b36cf3SHannes Frederic Sowa } 5221a462d18SDuan Jiong if (type == NDISC_REDIRECT) { 523ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 5241a462d18SDuan Jiong goto out; 5251a462d18SDuan Jiong } 52681aded24SDavid S. Miller 527e0d8c1b7SWei Wang if (!np->recverr) { 528e0d8c1b7SWei Wang if (!harderr || sk->sk_state != TCP_ESTABLISHED) 529db8dac20SDavid S. Miller goto out; 530e0d8c1b7SWei Wang } else { 531db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 532e0d8c1b7SWei Wang } 533b1faf566SEric Dumazet 534db8dac20SDavid S. Miller sk->sk_err = err; 535db8dac20SDavid S. Miller sk->sk_error_report(sk); 536db8dac20SDavid S. Miller out: 537ca065d0cSEric Dumazet return; 538db8dac20SDavid S. Miller } 539db8dac20SDavid S. Miller 540a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 541f7ad74feSBenjamin LaHaise { 542f7ad74feSBenjamin LaHaise int rc; 543f7ad74feSBenjamin LaHaise 544efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 545f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb); 546005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 5472c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 548e68b6e50SEric Dumazet } else { 549e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb); 550005ec974SShawn Bohrer } 551f7ad74feSBenjamin LaHaise 552850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb); 553f7ad74feSBenjamin LaHaise if (rc < 0) { 554f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk); 555f7ad74feSBenjamin LaHaise 556f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */ 557f7ad74feSBenjamin LaHaise if (rc == -ENOMEM) 558e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), 559f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite); 560e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 561f7ad74feSBenjamin LaHaise kfree_skb(skb); 562f7ad74feSBenjamin LaHaise return -1; 563f7ad74feSBenjamin LaHaise } 564850cbaddSPaolo Abeni 565f7ad74feSBenjamin LaHaise return 0; 566f7ad74feSBenjamin LaHaise } 567f7ad74feSBenjamin LaHaise 568db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb, 569d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 570d5fdd6baSBrian Haley u8 code, int offset, __be32 info) 571db8dac20SDavid S. Miller { 572645ca708SEric Dumazet __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 573db8dac20SDavid S. Miller } 574db8dac20SDavid S. Miller 575d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly; 576d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void) 577d7f3f621SBenjamin LaHaise { 578d7f3f621SBenjamin LaHaise if (!static_key_enabled(&udpv6_encap_needed)) 579d7f3f621SBenjamin LaHaise static_key_slow_inc(&udpv6_encap_needed); 580d7f3f621SBenjamin LaHaise } 581d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable); 582d7f3f621SBenjamin LaHaise 583a3f96c47SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 584db8dac20SDavid S. Miller { 585db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 586db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 587db8dac20SDavid S. Miller 588db8dac20SDavid S. Miller if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 589db8dac20SDavid S. Miller goto drop; 590db8dac20SDavid S. Miller 591d7f3f621SBenjamin LaHaise if (static_key_false(&udpv6_encap_needed) && up->encap_type) { 592d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 593d7f3f621SBenjamin LaHaise 594d7f3f621SBenjamin LaHaise /* 595d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to 596d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just 597d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket. 598d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value: 599d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap 600d7f3f621SBenjamin LaHaise * handler or was discarded by it. 601d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP. 602d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N 603d7f3f621SBenjamin LaHaise */ 604d7f3f621SBenjamin LaHaise 605d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */ 606d7f3f621SBenjamin LaHaise encap_rcv = ACCESS_ONCE(up->encap_rcv); 607e5aed006SHannes Frederic Sowa if (encap_rcv) { 608d7f3f621SBenjamin LaHaise int ret; 609d7f3f621SBenjamin LaHaise 6100a80966bSTom Herbert /* Verify checksum before giving to encap */ 6110a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 6120a80966bSTom Herbert goto csum_error; 6130a80966bSTom Herbert 614d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb); 615d7f3f621SBenjamin LaHaise if (ret <= 0) { 61602c22347SEric Dumazet __UDP_INC_STATS(sock_net(sk), 617d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS, 618d7f3f621SBenjamin LaHaise is_udplite); 619d7f3f621SBenjamin LaHaise return -ret; 620d7f3f621SBenjamin LaHaise } 621d7f3f621SBenjamin LaHaise } 622d7f3f621SBenjamin LaHaise 623d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */ 624d7f3f621SBenjamin LaHaise } 625d7f3f621SBenjamin LaHaise 626db8dac20SDavid S. Miller /* 627db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 628db8dac20SDavid S. Miller */ 629db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 630db8dac20SDavid S. Miller 631db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 632ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 633db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 634db8dac20SDavid S. Miller goto drop; 635db8dac20SDavid S. Miller } 636db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 637ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 638db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 639db8dac20SDavid S. Miller goto drop; 640db8dac20SDavid S. Miller } 641db8dac20SDavid S. Miller } 642db8dac20SDavid S. Miller 6434b943faeSPaolo Abeni prefetch(&sk->sk_rmem_alloc); 644ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) && 645ce25d66aSEric Dumazet udp_lib_checksum_complete(skb)) 6466a5dc9e5SEric Dumazet goto csum_error; 647ce25d66aSEric Dumazet 648ba66bbe5SDaniel Borkmann if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) 649a6127697SMichal Kubeček goto drop; 650db8dac20SDavid S. Miller 651e6afc8acSsamanthakumar udp_csum_pull_header(skb); 652cb80ef46SBenjamin LaHaise 653d826eb14SEric Dumazet skb_dst_drop(skb); 654db8dac20SDavid S. Miller 655850cbaddSPaolo Abeni return __udpv6_queue_rcv_skb(sk, skb); 6563e215c8dSJames M Leddy 6576a5dc9e5SEric Dumazet csum_error: 65802c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 659db8dac20SDavid S. Miller drop: 66002c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 661cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops); 662db8dac20SDavid S. Miller kfree_skb(skb); 663db8dac20SDavid S. Miller return -1; 664db8dac20SDavid S. Miller } 665db8dac20SDavid S. Miller 6665cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, 667b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr, 668b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr, 6695cf3d461SDavid Held int dif, unsigned short hnum) 670db8dac20SDavid S. Miller { 6719e89fd8bSSven Wegener struct inet_sock *inet = inet_sk(sk); 672db8dac20SDavid S. Miller 6739e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net)) 6745cf3d461SDavid Held return false; 675b8ad0cbcSDaniel Lezcano 6765cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum || 6775cf3d461SDavid Held sk->sk_family != PF_INET6 || 6785cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) || 6795cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) && 6805cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 68133b4b015SHenning Rogge (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) || 68233b4b015SHenning Rogge (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && 68333b4b015SHenning Rogge !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))) 6845cf3d461SDavid Held return false; 6859e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 6865cf3d461SDavid Held return false; 6875cf3d461SDavid Held return true; 688db8dac20SDavid S. Miller } 689db8dac20SDavid S. Miller 6904068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb) 6914068579eSTom Herbert { 6924068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log 6934068579eSTom Herbert * this error. Well, it is reasonable. 6944068579eSTom Herbert */ 695ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 6964068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 6974068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 6984068579eSTom Herbert } 6994068579eSTom Herbert 700db8dac20SDavid S. Miller /* 701db8dac20SDavid S. Miller * Note: called only from the BH handler context, 702db8dac20SDavid S. Miller * so we don't need to lock the hashes. 703db8dac20SDavid S. Miller */ 704e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 705b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr, 70636cbb245SRick Jones struct udp_table *udptable, int proto) 707db8dac20SDavid S. Miller { 708ca065d0cSEric Dumazet struct sock *sk, *first = NULL; 709db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 7105cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 7115cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 712ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node); 7132dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 714ca065d0cSEric Dumazet int dif = inet6_iif(skb); 715ca065d0cSEric Dumazet struct hlist_node *node; 716ca065d0cSEric Dumazet struct sk_buff *nskb; 7172dc41cffSDavid Held 7182dc41cffSDavid Held if (use_hash2) { 7192dc41cffSDavid Held hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) & 72073e2d5e3SPablo Neira udptable->mask; 72173e2d5e3SPablo Neira hash2 = udp6_portaddr_hash(net, daddr, hnum) & udptable->mask; 7222dc41cffSDavid Held start_lookup: 72373e2d5e3SPablo Neira hslot = &udptable->hash2[hash2]; 7242dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 7252dc41cffSDavid Held } 726db8dac20SDavid S. Miller 727ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) { 728ca065d0cSEric Dumazet if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr, 729ca065d0cSEric Dumazet uh->source, saddr, dif, hnum)) 730ca065d0cSEric Dumazet continue; 7311c19448cSTom Herbert /* If zero checksum and no_check is not on for 7324068579eSTom Herbert * the socket then skip it. 7334068579eSTom Herbert */ 734ca065d0cSEric Dumazet if (!uh->check && !udp_sk(sk)->no_check6_rx) 735ca065d0cSEric Dumazet continue; 736ca065d0cSEric Dumazet if (!first) { 737ca065d0cSEric Dumazet first = sk; 738ca065d0cSEric Dumazet continue; 739db8dac20SDavid S. Miller } 740ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC); 741ca065d0cSEric Dumazet if (unlikely(!nskb)) { 742ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops); 74302c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS, 744ca065d0cSEric Dumazet IS_UDPLITE(sk)); 74502c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, 746ca065d0cSEric Dumazet IS_UDPLITE(sk)); 747ca065d0cSEric Dumazet continue; 748a1ab77f9SEric Dumazet } 749db8dac20SDavid S. Miller 750ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(sk, nskb) > 0) 751ca065d0cSEric Dumazet consume_skb(nskb); 752ca065d0cSEric Dumazet } 753a1ab77f9SEric Dumazet 7542dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 7552dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 7562dc41cffSDavid Held hash2 = hash2_any; 7572dc41cffSDavid Held goto start_lookup; 7582dc41cffSDavid Held } 7592dc41cffSDavid Held 760ca065d0cSEric Dumazet if (first) { 761ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(first, skb) > 0) 762ca065d0cSEric Dumazet consume_skb(skb); 763a1ab77f9SEric Dumazet } else { 764ca065d0cSEric Dumazet kfree_skb(skb); 76502c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI, 76636cbb245SRick Jones proto == IPPROTO_UDPLITE); 767a1ab77f9SEric Dumazet } 768db8dac20SDavid S. Miller return 0; 769db8dac20SDavid S. Miller } 770db8dac20SDavid S. Miller 771645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 772db8dac20SDavid S. Miller int proto) 773db8dac20SDavid S. Miller { 774b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 775ca065d0cSEric Dumazet struct net *net = dev_net(skb->dev); 776ca065d0cSEric Dumazet struct udphdr *uh; 777ca065d0cSEric Dumazet struct sock *sk; 778db8dac20SDavid S. Miller u32 ulen = 0; 779db8dac20SDavid S. Miller 780db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 781d6bc0149SBjørn Mork goto discard; 782db8dac20SDavid S. Miller 783db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 784db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 785db8dac20SDavid S. Miller uh = udp_hdr(skb); 786db8dac20SDavid S. Miller 787db8dac20SDavid S. Miller ulen = ntohs(uh->len); 788db8dac20SDavid S. Miller if (ulen > skb->len) 789db8dac20SDavid S. Miller goto short_packet; 790db8dac20SDavid S. Miller 791db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 792db8dac20SDavid S. Miller /* UDP validates ulen. */ 793db8dac20SDavid S. Miller 794db8dac20SDavid S. Miller /* Check for jumbo payload */ 795db8dac20SDavid S. Miller if (ulen == 0) 796db8dac20SDavid S. Miller ulen = skb->len; 797db8dac20SDavid S. Miller 798db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 799db8dac20SDavid S. Miller goto short_packet; 800db8dac20SDavid S. Miller 801db8dac20SDavid S. Miller if (ulen < skb->len) { 802db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 803db8dac20SDavid S. Miller goto short_packet; 804db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 805db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 806db8dac20SDavid S. Miller uh = udp_hdr(skb); 807db8dac20SDavid S. Miller } 808db8dac20SDavid S. Miller } 809db8dac20SDavid S. Miller 810db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 8116a5dc9e5SEric Dumazet goto csum_error; 812db8dac20SDavid S. Miller 813c9f2c1aeSPaolo Abeni /* Check if the socket is already available, e.g. due to early demux */ 814c9f2c1aeSPaolo Abeni sk = skb_steal_sock(skb); 815c9f2c1aeSPaolo Abeni if (sk) { 816c9f2c1aeSPaolo Abeni struct dst_entry *dst = skb_dst(skb); 817c9f2c1aeSPaolo Abeni int ret; 818c9f2c1aeSPaolo Abeni 819c9f2c1aeSPaolo Abeni if (unlikely(sk->sk_rx_dst != dst)) 820c9f2c1aeSPaolo Abeni udp_sk_rx_dst_set(sk, dst); 821c9f2c1aeSPaolo Abeni 822c9f2c1aeSPaolo Abeni ret = udpv6_queue_rcv_skb(sk, skb); 823c9f2c1aeSPaolo Abeni sock_put(sk); 824c9f2c1aeSPaolo Abeni 825c9f2c1aeSPaolo Abeni /* a return value > 0 means to resubmit the input */ 826c9f2c1aeSPaolo Abeni if (ret > 0) 827c9f2c1aeSPaolo Abeni return ret; 828c9f2c1aeSPaolo Abeni return 0; 829c9f2c1aeSPaolo Abeni } 830c9f2c1aeSPaolo Abeni 831db8dac20SDavid S. Miller /* 832db8dac20SDavid S. Miller * Multicast receive code 833db8dac20SDavid S. Miller */ 834db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 835e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 83636cbb245SRick Jones saddr, daddr, udptable, proto); 837db8dac20SDavid S. Miller 838db8dac20SDavid S. Miller /* Unicast */ 839607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 84053b24b8fSIan Morris if (sk) { 841a5b50476SEliezer Tamir int ret; 842a5b50476SEliezer Tamir 8431c19448cSTom Herbert if (!uh->check && !udp_sk(sk)->no_check6_rx) { 8444068579eSTom Herbert udp6_csum_zero_error(skb); 8454068579eSTom Herbert goto csum_error; 8464068579eSTom Herbert } 8474068579eSTom Herbert 848224d019cSTom Herbert if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 8492abb7cdcSTom Herbert skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, 8502abb7cdcSTom Herbert ip6_compute_pseudo); 8512abb7cdcSTom Herbert 852a5b50476SEliezer Tamir ret = udpv6_queue_rcv_skb(sk, skb); 853db8dac20SDavid S. Miller 85459dca1d8SBill Sommerfeld /* a return value > 0 means to resubmit the input */ 855cb80ef46SBenjamin LaHaise if (ret > 0) 85659dca1d8SBill Sommerfeld return ret; 857cb80ef46SBenjamin LaHaise 858cb80ef46SBenjamin LaHaise return 0; 859cb80ef46SBenjamin LaHaise } 860cb80ef46SBenjamin LaHaise 8614068579eSTom Herbert if (!uh->check) { 8624068579eSTom Herbert udp6_csum_zero_error(skb); 8634068579eSTom Herbert goto csum_error; 8644068579eSTom Herbert } 8654068579eSTom Herbert 866db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 867db8dac20SDavid S. Miller goto discard; 868db8dac20SDavid S. Miller 869db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 8706a5dc9e5SEric Dumazet goto csum_error; 871db8dac20SDavid S. Miller 87202c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 8733ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 874db8dac20SDavid S. Miller 875db8dac20SDavid S. Miller kfree_skb(skb); 876db8dac20SDavid S. Miller return 0; 877db8dac20SDavid S. Miller 878db8dac20SDavid S. Miller short_packet: 879ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 880db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 881ba7a46f1SJoe Perches saddr, ntohs(uh->source), 882ba7a46f1SJoe Perches ulen, skb->len, 883ba7a46f1SJoe Perches daddr, ntohs(uh->dest)); 8846a5dc9e5SEric Dumazet goto discard; 8856a5dc9e5SEric Dumazet csum_error: 88602c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 887db8dac20SDavid S. Miller discard: 88802c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 889db8dac20SDavid S. Miller kfree_skb(skb); 890db8dac20SDavid S. Miller return 0; 891db8dac20SDavid S. Miller } 892db8dac20SDavid S. Miller 8930bd84065Ssubashab@codeaurora.org 8945425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net, 8955425077dSsubashab@codeaurora.org __be16 loc_port, const struct in6_addr *loc_addr, 8965425077dSsubashab@codeaurora.org __be16 rmt_port, const struct in6_addr *rmt_addr, 8975425077dSsubashab@codeaurora.org int dif) 8985425077dSsubashab@codeaurora.org { 8990bd84065Ssubashab@codeaurora.org unsigned short hnum = ntohs(loc_port); 9000bd84065Ssubashab@codeaurora.org unsigned int hash2 = udp6_portaddr_hash(net, loc_addr, hnum); 9010bd84065Ssubashab@codeaurora.org unsigned int slot2 = hash2 & udp_table.mask; 9020bd84065Ssubashab@codeaurora.org struct udp_hslot *hslot2 = &udp_table.hash2[slot2]; 9030bd84065Ssubashab@codeaurora.org const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum); 9045425077dSsubashab@codeaurora.org struct sock *sk; 9055425077dSsubashab@codeaurora.org 9060bd84065Ssubashab@codeaurora.org udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) { 90785cb73ffSWei Wang if (sk->sk_state == TCP_ESTABLISHED && 90885cb73ffSWei Wang INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif)) 9095425077dSsubashab@codeaurora.org return sk; 9100bd84065Ssubashab@codeaurora.org /* Only check first socket in chain */ 9110bd84065Ssubashab@codeaurora.org break; 9120bd84065Ssubashab@codeaurora.org } 9130bd84065Ssubashab@codeaurora.org return NULL; 9145425077dSsubashab@codeaurora.org } 9155425077dSsubashab@codeaurora.org 9165425077dSsubashab@codeaurora.org static void udp_v6_early_demux(struct sk_buff *skb) 9175425077dSsubashab@codeaurora.org { 9185425077dSsubashab@codeaurora.org struct net *net = dev_net(skb->dev); 9195425077dSsubashab@codeaurora.org const struct udphdr *uh; 9205425077dSsubashab@codeaurora.org struct sock *sk; 9215425077dSsubashab@codeaurora.org struct dst_entry *dst; 9225425077dSsubashab@codeaurora.org int dif = skb->dev->ifindex; 9235425077dSsubashab@codeaurora.org 9245425077dSsubashab@codeaurora.org if (!pskb_may_pull(skb, skb_transport_offset(skb) + 9255425077dSsubashab@codeaurora.org sizeof(struct udphdr))) 9265425077dSsubashab@codeaurora.org return; 9275425077dSsubashab@codeaurora.org 9285425077dSsubashab@codeaurora.org uh = udp_hdr(skb); 9295425077dSsubashab@codeaurora.org 9305425077dSsubashab@codeaurora.org if (skb->pkt_type == PACKET_HOST) 9315425077dSsubashab@codeaurora.org sk = __udp6_lib_demux_lookup(net, uh->dest, 9325425077dSsubashab@codeaurora.org &ipv6_hdr(skb)->daddr, 9335425077dSsubashab@codeaurora.org uh->source, &ipv6_hdr(skb)->saddr, 9345425077dSsubashab@codeaurora.org dif); 9355425077dSsubashab@codeaurora.org else 9365425077dSsubashab@codeaurora.org return; 9375425077dSsubashab@codeaurora.org 93841c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 9395425077dSsubashab@codeaurora.org return; 9405425077dSsubashab@codeaurora.org 9415425077dSsubashab@codeaurora.org skb->sk = sk; 9425425077dSsubashab@codeaurora.org skb->destructor = sock_efree; 9435425077dSsubashab@codeaurora.org dst = READ_ONCE(sk->sk_rx_dst); 9445425077dSsubashab@codeaurora.org 9455425077dSsubashab@codeaurora.org if (dst) 9465425077dSsubashab@codeaurora.org dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie); 9475425077dSsubashab@codeaurora.org if (dst) { 948d24406c8SWei Wang /* set noref for now. 949d24406c8SWei Wang * any place which wants to hold dst has to call 950d24406c8SWei Wang * dst_hold_safe() 951d24406c8SWei Wang */ 9525425077dSsubashab@codeaurora.org skb_dst_set_noref(skb, dst); 9535425077dSsubashab@codeaurora.org } 9545425077dSsubashab@codeaurora.org } 9555425077dSsubashab@codeaurora.org 956db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb) 957db8dac20SDavid S. Miller { 958645ca708SEric Dumazet return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 959db8dac20SDavid S. Miller } 960db8dac20SDavid S. Miller 961db8dac20SDavid S. Miller /* 962db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 963db8dac20SDavid S. Miller */ 964db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 965db8dac20SDavid S. Miller { 966db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 967db8dac20SDavid S. Miller 96836d926b9SDenis V. Lunev if (up->pending == AF_INET) 96936d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 97036d926b9SDenis V. Lunev else if (up->pending) { 971db8dac20SDavid S. Miller up->len = 0; 972db8dac20SDavid S. Miller up->pending = 0; 973db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 974db8dac20SDavid S. Miller } 975db8dac20SDavid S. Miller } 976db8dac20SDavid S. Miller 977493c6be3SSridhar Samudrala /** 978493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 979493c6be3SSridhar Samudrala * @sk: socket we are sending on 980493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 981493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 982493c6be3SSridhar Samudrala */ 983493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 984493c6be3SSridhar Samudrala const struct in6_addr *saddr, 985493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 986493c6be3SSridhar Samudrala { 987493c6be3SSridhar Samudrala unsigned int offset; 988493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 989d39d938cSVlad Yasevich struct sk_buff *frags = skb_shinfo(skb)->frag_list; 990493c6be3SSridhar Samudrala __wsum csum = 0; 991493c6be3SSridhar Samudrala 992d39d938cSVlad Yasevich if (!frags) { 993493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 994493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 995493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 996493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 997493c6be3SSridhar Samudrala } else { 998493c6be3SSridhar Samudrala /* 999493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 1000493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 1001493c6be3SSridhar Samudrala * should be together 1002493c6be3SSridhar Samudrala */ 1003493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 1004493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 1005493c6be3SSridhar Samudrala 1006493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 1007493c6be3SSridhar Samudrala 1008d39d938cSVlad Yasevich do { 1009d39d938cSVlad Yasevich csum = csum_add(csum, frags->csum); 1010d39d938cSVlad Yasevich } while ((frags = frags->next)); 1011493c6be3SSridhar Samudrala 1012493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1013493c6be3SSridhar Samudrala csum); 1014493c6be3SSridhar Samudrala if (uh->check == 0) 1015493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 1016493c6be3SSridhar Samudrala } 1017493c6be3SSridhar Samudrala } 1018493c6be3SSridhar Samudrala 1019db8dac20SDavid S. Miller /* 1020db8dac20SDavid S. Miller * Sending 1021db8dac20SDavid S. Miller */ 1022db8dac20SDavid S. Miller 1023d39d938cSVlad Yasevich static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6) 1024db8dac20SDavid S. Miller { 1025d39d938cSVlad Yasevich struct sock *sk = skb->sk; 1026db8dac20SDavid S. Miller struct udphdr *uh; 1027db8dac20SDavid S. Miller int err = 0; 1028db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1029db8dac20SDavid S. Miller __wsum csum = 0; 1030d39d938cSVlad Yasevich int offset = skb_transport_offset(skb); 1031d39d938cSVlad Yasevich int len = skb->len - offset; 1032db8dac20SDavid S. Miller 1033db8dac20SDavid S. Miller /* 1034db8dac20SDavid S. Miller * Create a UDP header 1035db8dac20SDavid S. Miller */ 1036db8dac20SDavid S. Miller uh = udp_hdr(skb); 10371958b856SDavid S. Miller uh->source = fl6->fl6_sport; 10381958b856SDavid S. Miller uh->dest = fl6->fl6_dport; 1039d39d938cSVlad Yasevich uh->len = htons(len); 1040db8dac20SDavid S. Miller uh->check = 0; 1041db8dac20SDavid S. Miller 1042db8dac20SDavid S. Miller if (is_udplite) 1043d39d938cSVlad Yasevich csum = udplite_csum(skb); 1044d39d938cSVlad Yasevich else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */ 10454068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE; 10464068579eSTom Herbert goto send; 10474068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 1048d39d938cSVlad Yasevich udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len); 1049493c6be3SSridhar Samudrala goto send; 1050493c6be3SSridhar Samudrala } else 1051d39d938cSVlad Yasevich csum = udp_csum(skb); 1052db8dac20SDavid S. Miller 1053db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 10544c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 1055d39d938cSVlad Yasevich len, fl6->flowi6_proto, csum); 1056db8dac20SDavid S. Miller if (uh->check == 0) 1057db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 1058db8dac20SDavid S. Miller 1059493c6be3SSridhar Samudrala send: 1060d39d938cSVlad Yasevich err = ip6_send_skb(skb); 10616ce9e7b5SEric Dumazet if (err) { 10626ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 10636aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 10646ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 10656ce9e7b5SEric Dumazet err = 0; 10666ce9e7b5SEric Dumazet } 10676aef70a8SEric Dumazet } else { 10686aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 10696ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 10706aef70a8SEric Dumazet } 1071d39d938cSVlad Yasevich return err; 1072d39d938cSVlad Yasevich } 1073d39d938cSVlad Yasevich 1074d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk) 1075d39d938cSVlad Yasevich { 1076d39d938cSVlad Yasevich struct sk_buff *skb; 1077d39d938cSVlad Yasevich struct udp_sock *up = udp_sk(sk); 1078d39d938cSVlad Yasevich struct flowi6 fl6; 1079d39d938cSVlad Yasevich int err = 0; 1080d39d938cSVlad Yasevich 1081d39d938cSVlad Yasevich if (up->pending == AF_INET) 1082d39d938cSVlad Yasevich return udp_push_pending_frames(sk); 1083d39d938cSVlad Yasevich 1084d39d938cSVlad Yasevich /* ip6_finish_skb will release the cork, so make a copy of 1085d39d938cSVlad Yasevich * fl6 here. 1086d39d938cSVlad Yasevich */ 1087d39d938cSVlad Yasevich fl6 = inet_sk(sk)->cork.fl.u.ip6; 1088d39d938cSVlad Yasevich 1089d39d938cSVlad Yasevich skb = ip6_finish_skb(sk); 1090d39d938cSVlad Yasevich if (!skb) 1091d39d938cSVlad Yasevich goto out; 1092d39d938cSVlad Yasevich 1093d39d938cSVlad Yasevich err = udp_v6_send_skb(skb, &fl6); 1094d39d938cSVlad Yasevich 1095db8dac20SDavid S. Miller out: 1096db8dac20SDavid S. Miller up->len = 0; 1097db8dac20SDavid S. Miller up->pending = 0; 1098db8dac20SDavid S. Miller return err; 1099db8dac20SDavid S. Miller } 1100db8dac20SDavid S. Miller 11011b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) 1102db8dac20SDavid S. Miller { 1103db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 1104db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1105db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1106db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 1107342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 110820c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 1109db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 111045f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL; 1111db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 11124c9483b2SDavid S. Miller struct flowi6 fl6; 1113db8dac20SDavid S. Miller struct dst_entry *dst; 111426879da5SWei Wang struct ipcm6_cookie ipc6; 1115db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 1116db8dac20SDavid S. Miller int ulen = len; 1117db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 1118db8dac20SDavid S. Miller int err; 1119db8dac20SDavid S. Miller int connected = 0; 1120db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1121db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1122ad1e46a8SSoheil Hassas Yeganeh struct sockcm_cookie sockc; 1123db8dac20SDavid S. Miller 112426879da5SWei Wang ipc6.hlimit = -1; 112526879da5SWei Wang ipc6.tclass = -1; 112626879da5SWei Wang ipc6.dontfrag = -1; 1127d515684dSAlexander Potapenko sockc.tsflags = sk->sk_tsflags; 112826879da5SWei Wang 1129db8dac20SDavid S. Miller /* destination address check */ 1130db8dac20SDavid S. Miller if (sin6) { 1131db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 1132db8dac20SDavid S. Miller return -EINVAL; 1133db8dac20SDavid S. Miller 1134db8dac20SDavid S. Miller switch (sin6->sin6_family) { 1135db8dac20SDavid S. Miller case AF_INET6: 1136db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 1137db8dac20SDavid S. Miller return -EINVAL; 1138db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1139052d2369SJonathan T. Leighton if (ipv6_addr_any(daddr) && 1140052d2369SJonathan T. Leighton ipv6_addr_v4mapped(&np->saddr)) 1141052d2369SJonathan T. Leighton ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK), 1142052d2369SJonathan T. Leighton daddr); 1143db8dac20SDavid S. Miller break; 1144db8dac20SDavid S. Miller case AF_INET: 1145db8dac20SDavid S. Miller goto do_udp_sendmsg; 1146db8dac20SDavid S. Miller case AF_UNSPEC: 1147db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 1148db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 1149db8dac20SDavid S. Miller daddr = NULL; 1150db8dac20SDavid S. Miller break; 1151db8dac20SDavid S. Miller default: 1152db8dac20SDavid S. Miller return -EINVAL; 1153db8dac20SDavid S. Miller } 1154db8dac20SDavid S. Miller } else if (!up->pending) { 1155db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1156db8dac20SDavid S. Miller return -EDESTADDRREQ; 1157efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1158db8dac20SDavid S. Miller } else 1159db8dac20SDavid S. Miller daddr = NULL; 1160db8dac20SDavid S. Miller 1161db8dac20SDavid S. Miller if (daddr) { 1162db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 1163db8dac20SDavid S. Miller struct sockaddr_in sin; 1164db8dac20SDavid S. Miller sin.sin_family = AF_INET; 1165c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1166db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1167db8dac20SDavid S. Miller msg->msg_name = &sin; 1168db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 1169db8dac20SDavid S. Miller do_udp_sendmsg: 1170db8dac20SDavid S. Miller if (__ipv6_only_sock(sk)) 1171db8dac20SDavid S. Miller return -ENETUNREACH; 11721b784140SYing Xue return udp_sendmsg(sk, msg, len); 1173db8dac20SDavid S. Miller } 1174db8dac20SDavid S. Miller } 1175db8dac20SDavid S. Miller 1176db8dac20SDavid S. Miller if (up->pending == AF_INET) 11771b784140SYing Xue return udp_sendmsg(sk, msg, len); 1178db8dac20SDavid S. Miller 1179db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 1180db8dac20SDavid S. Miller better check is made in ip6_append_data(). 1181db8dac20SDavid S. Miller */ 1182db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 1183db8dac20SDavid S. Miller return -EMSGSIZE; 1184db8dac20SDavid S. Miller 118503485f2aSVlad Yasevich getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1186db8dac20SDavid S. Miller if (up->pending) { 1187db8dac20SDavid S. Miller /* 1188db8dac20SDavid S. Miller * There are pending frames. 1189db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1190db8dac20SDavid S. Miller */ 1191db8dac20SDavid S. Miller lock_sock(sk); 1192db8dac20SDavid S. Miller if (likely(up->pending)) { 1193db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 1194db8dac20SDavid S. Miller release_sock(sk); 1195db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1196db8dac20SDavid S. Miller } 1197db8dac20SDavid S. Miller dst = NULL; 1198db8dac20SDavid S. Miller goto do_append_data; 1199db8dac20SDavid S. Miller } 1200db8dac20SDavid S. Miller release_sock(sk); 1201db8dac20SDavid S. Miller } 1202db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1203db8dac20SDavid S. Miller 12044c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1205db8dac20SDavid S. Miller 1206db8dac20SDavid S. Miller if (sin6) { 1207db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 1208db8dac20SDavid S. Miller return -EINVAL; 1209db8dac20SDavid S. Miller 12101958b856SDavid S. Miller fl6.fl6_dport = sin6->sin6_port; 1211db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1212db8dac20SDavid S. Miller 1213db8dac20SDavid S. Miller if (np->sndflow) { 12144c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 12154c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 12164c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 121763159f29SIan Morris if (!flowlabel) 1218db8dac20SDavid S. Miller return -EINVAL; 1219db8dac20SDavid S. Miller } 1220db8dac20SDavid S. Miller } 1221db8dac20SDavid S. Miller 1222db8dac20SDavid S. Miller /* 1223db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 1224db8dac20SDavid S. Miller * sk->sk_dst_cache. 1225db8dac20SDavid S. Miller */ 1226db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 1227efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1228efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1229db8dac20SDavid S. Miller 1230db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 1231db8dac20SDavid S. Miller sin6->sin6_scope_id && 1232842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 12334c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 1234db8dac20SDavid S. Miller } else { 1235db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1236db8dac20SDavid S. Miller return -EDESTADDRREQ; 1237db8dac20SDavid S. Miller 12381958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 1239efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 12404c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 1241db8dac20SDavid S. Miller connected = 1; 1242db8dac20SDavid S. Miller } 1243db8dac20SDavid S. Miller 12444c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 12454c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 1246db8dac20SDavid S. Miller 12474c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 12484c9483b2SDavid S. Miller fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 12499f690db7SYang Hongyang 12504c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 1251e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 125251953d5bSBrian Haley 1253db8dac20SDavid S. Miller if (msg->msg_controllen) { 1254db8dac20SDavid S. Miller opt = &opt_space; 1255db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 1256db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 125726879da5SWei Wang ipc6.opt = opt; 1258db8dac20SDavid S. Miller 125926879da5SWei Wang err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc); 1260db8dac20SDavid S. Miller if (err < 0) { 1261db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1262db8dac20SDavid S. Miller return err; 1263db8dac20SDavid S. Miller } 12644c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 12654c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 126663159f29SIan Morris if (!flowlabel) 1267db8dac20SDavid S. Miller return -EINVAL; 1268db8dac20SDavid S. Miller } 1269db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 1270db8dac20SDavid S. Miller opt = NULL; 1271db8dac20SDavid S. Miller connected = 0; 1272db8dac20SDavid S. Miller } 127345f6fad8SEric Dumazet if (!opt) { 127445f6fad8SEric Dumazet opt = txopt_get(np); 127545f6fad8SEric Dumazet opt_to_free = opt; 127645f6fad8SEric Dumazet } 1277db8dac20SDavid S. Miller if (flowlabel) 1278db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 1279db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 128026879da5SWei Wang ipc6.opt = opt; 1281db8dac20SDavid S. Miller 12824c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 1283876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 12844e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 1285876c7f41SBrian Haley else 12864c9483b2SDavid S. Miller fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 12874c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 12884e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 12891958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 1290db8dac20SDavid S. Miller 12914c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 129220c59de2SArnaud Ebalard if (final_p) 1293db8dac20SDavid S. Miller connected = 0; 1294db8dac20SDavid S. Miller 12954c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) { 12964c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 1297db8dac20SDavid S. Miller connected = 0; 1298c4062dfcSErich E. Hoover } else if (!fl6.flowi6_oif) 1299c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 1300db8dac20SDavid S. Miller 13014c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 1302db8dac20SDavid S. Miller 130338b7097bSHannes Frederic Sowa if (ipc6.tclass < 0) 130438b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass; 130538b7097bSHannes Frederic Sowa 130638b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 130738b7097bSHannes Frederic Sowa 13080e0d44abSSteffen Klassert dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p); 130968d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 131068d0c6d3SDavid S. Miller err = PTR_ERR(dst); 131168d0c6d3SDavid S. Miller dst = NULL; 1312db8dac20SDavid S. Miller goto out; 1313db8dac20SDavid S. Miller } 1314db8dac20SDavid S. Miller 131526879da5SWei Wang if (ipc6.hlimit < 0) 131626879da5SWei Wang ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 1317db8dac20SDavid S. Miller 1318db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1319db8dac20SDavid S. Miller goto do_confirm; 1320db8dac20SDavid S. Miller back_from_confirm: 1321db8dac20SDavid S. Miller 132203485f2aSVlad Yasevich /* Lockless fast path for the non-corking case */ 132303485f2aSVlad Yasevich if (!corkreq) { 132403485f2aSVlad Yasevich struct sk_buff *skb; 132503485f2aSVlad Yasevich 132603485f2aSVlad Yasevich skb = ip6_make_skb(sk, getfrag, msg, ulen, 132726879da5SWei Wang sizeof(struct udphdr), &ipc6, 132803485f2aSVlad Yasevich &fl6, (struct rt6_info *)dst, 132926879da5SWei Wang msg->msg_flags, &sockc); 133003485f2aSVlad Yasevich err = PTR_ERR(skb); 133103485f2aSVlad Yasevich if (!IS_ERR_OR_NULL(skb)) 133203485f2aSVlad Yasevich err = udp_v6_send_skb(skb, &fl6); 133303485f2aSVlad Yasevich goto release_dst; 133403485f2aSVlad Yasevich } 133503485f2aSVlad Yasevich 1336db8dac20SDavid S. Miller lock_sock(sk); 1337db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1338db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1339db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1340db8dac20SDavid S. Miller release_sock(sk); 1341db8dac20SDavid S. Miller 1342ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n"); 1343db8dac20SDavid S. Miller err = -EINVAL; 1344db8dac20SDavid S. Miller goto out; 1345db8dac20SDavid S. Miller } 1346db8dac20SDavid S. Miller 1347db8dac20SDavid S. Miller up->pending = AF_INET6; 1348db8dac20SDavid S. Miller 1349db8dac20SDavid S. Miller do_append_data: 135026879da5SWei Wang if (ipc6.dontfrag < 0) 135126879da5SWei Wang ipc6.dontfrag = np->dontfrag; 1352db8dac20SDavid S. Miller up->len += ulen; 135326879da5SWei Wang err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 135426879da5SWei Wang &ipc6, &fl6, (struct rt6_info *)dst, 135526879da5SWei Wang corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, &sockc); 1356db8dac20SDavid S. Miller if (err) 1357db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1358db8dac20SDavid S. Miller else if (!corkreq) 1359db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 1360db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1361db8dac20SDavid S. Miller up->pending = 0; 1362db8dac20SDavid S. Miller 136303485f2aSVlad Yasevich if (err > 0) 136403485f2aSVlad Yasevich err = np->recverr ? net_xmit_errno(err) : 0; 136503485f2aSVlad Yasevich release_sock(sk); 136603485f2aSVlad Yasevich 136703485f2aSVlad Yasevich release_dst: 1368db8dac20SDavid S. Miller if (dst) { 1369db8dac20SDavid S. Miller if (connected) { 1370db8dac20SDavid S. Miller ip6_dst_store(sk, dst, 1371efe4208fSEric Dumazet ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ? 1372efe4208fSEric Dumazet &sk->sk_v6_daddr : NULL, 1373db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 13744c9483b2SDavid S. Miller ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 1375db8dac20SDavid S. Miller &np->saddr : 1376db8dac20SDavid S. Miller #endif 1377db8dac20SDavid S. Miller NULL); 1378db8dac20SDavid S. Miller } else { 1379db8dac20SDavid S. Miller dst_release(dst); 1380db8dac20SDavid S. Miller } 1381a3c96089SYOSHIFUJI Hideaki dst = NULL; 1382db8dac20SDavid S. Miller } 1383db8dac20SDavid S. Miller 1384db8dac20SDavid S. Miller out: 1385a3c96089SYOSHIFUJI Hideaki dst_release(dst); 1386db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 138745f6fad8SEric Dumazet txopt_put(opt_to_free); 1388db8dac20SDavid S. Miller if (!err) 1389db8dac20SDavid S. Miller return len; 1390db8dac20SDavid S. Miller /* 1391db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1392db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1393db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1394db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1395db8dac20SDavid S. Miller * seems like overkill. 1396db8dac20SDavid S. Miller */ 1397db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 13986aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk), 1399235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1400db8dac20SDavid S. Miller } 1401db8dac20SDavid S. Miller return err; 1402db8dac20SDavid S. Miller 1403db8dac20SDavid S. Miller do_confirm: 14040dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE) 14050dec879fSJulian Anastasov dst_confirm_neigh(dst, &fl6.daddr); 1406db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1407db8dac20SDavid S. Miller goto back_from_confirm; 1408db8dac20SDavid S. Miller err = 0; 1409db8dac20SDavid S. Miller goto out; 1410db8dac20SDavid S. Miller } 1411db8dac20SDavid S. Miller 14127d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1413db8dac20SDavid S. Miller { 141444046a59STom Parkin struct udp_sock *up = udp_sk(sk); 1415db8dac20SDavid S. Miller lock_sock(sk); 1416db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1417db8dac20SDavid S. Miller release_sock(sk); 1418db8dac20SDavid S. Miller 141944046a59STom Parkin if (static_key_false(&udpv6_encap_needed) && up->encap_type) { 142044046a59STom Parkin void (*encap_destroy)(struct sock *sk); 142144046a59STom Parkin encap_destroy = ACCESS_ONCE(up->encap_destroy); 142244046a59STom Parkin if (encap_destroy) 142344046a59STom Parkin encap_destroy(sk); 142444046a59STom Parkin } 142544046a59STom Parkin 1426db8dac20SDavid S. Miller inet6_destroy_sock(sk); 1427db8dac20SDavid S. Miller } 1428db8dac20SDavid S. Miller 1429db8dac20SDavid S. Miller /* 1430db8dac20SDavid S. Miller * Socket option code for UDP 1431db8dac20SDavid S. Miller */ 1432db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname, 1433b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1434db8dac20SDavid S. Miller { 1435db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1436db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1437db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1438db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1439db8dac20SDavid S. Miller } 1440db8dac20SDavid S. Miller 1441db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1442db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 1443b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1444db8dac20SDavid S. Miller { 1445db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1446db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1447db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1448db8dac20SDavid S. Miller return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); 1449db8dac20SDavid S. Miller } 1450db8dac20SDavid S. Miller #endif 1451db8dac20SDavid S. Miller 1452db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1453db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1454db8dac20SDavid S. Miller { 1455db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1456db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1457db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1458db8dac20SDavid S. Miller } 1459db8dac20SDavid S. Miller 1460db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1461db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 1462db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1463db8dac20SDavid S. Miller { 1464db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1465db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1466db8dac20SDavid S. Miller return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); 1467db8dac20SDavid S. Miller } 1468db8dac20SDavid S. Miller #endif 1469db8dac20SDavid S. Miller 1470dddb64bcSsubashab@codeaurora.org static struct inet6_protocol udpv6_protocol = { 14715425077dSsubashab@codeaurora.org .early_demux = udp_v6_early_demux, 1472dddb64bcSsubashab@codeaurora.org .early_demux_handler = udp_v6_early_demux, 1473db8dac20SDavid S. Miller .handler = udpv6_rcv, 1474db8dac20SDavid S. Miller .err_handler = udpv6_err, 1475db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1476db8dac20SDavid S. Miller }; 1477db8dac20SDavid S. Miller 1478db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1479db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1480db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1481db8dac20SDavid S. Miller { 148217ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 148317ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 148417ef66afSLorenzo Colitti } else { 148517ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket; 148617ef66afSLorenzo Colitti struct inet_sock *inet = inet_sk(v); 148717ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport); 148817ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport); 148917ef66afSLorenzo Colitti ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket); 149017ef66afSLorenzo Colitti } 1491db8dac20SDavid S. Miller return 0; 1492db8dac20SDavid S. Miller } 1493db8dac20SDavid S. Miller 149473cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = { 149573cb88ecSArjan van de Ven .owner = THIS_MODULE, 149673cb88ecSArjan van de Ven .open = udp_seq_open, 149773cb88ecSArjan van de Ven .read = seq_read, 149873cb88ecSArjan van de Ven .llseek = seq_lseek, 149973cb88ecSArjan van de Ven .release = seq_release_net 150073cb88ecSArjan van de Ven }; 150173cb88ecSArjan van de Ven 1502db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1503db8dac20SDavid S. Miller .name = "udp6", 1504db8dac20SDavid S. Miller .family = AF_INET6, 1505645ca708SEric Dumazet .udp_table = &udp_table, 150673cb88ecSArjan van de Ven .seq_fops = &udp6_afinfo_seq_fops, 1507dda61925SDenis V. Lunev .seq_ops = { 1508dda61925SDenis V. Lunev .show = udp6_seq_show, 1509dda61925SDenis V. Lunev }, 1510db8dac20SDavid S. Miller }; 1511db8dac20SDavid S. Miller 15122c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net) 1513db8dac20SDavid S. Miller { 15140c96d8c5SDaniel Lezcano return udp_proc_register(net, &udp6_seq_afinfo); 1515db8dac20SDavid S. Miller } 1516db8dac20SDavid S. Miller 1517ec120da6SIan Morris void udp6_proc_exit(struct net *net) 1518ec120da6SIan Morris { 15190c96d8c5SDaniel Lezcano udp_proc_unregister(net, &udp6_seq_afinfo); 1520db8dac20SDavid S. Miller } 1521db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1522db8dac20SDavid S. Miller 1523db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1524db8dac20SDavid S. Miller 1525db8dac20SDavid S. Miller struct proto udpv6_prot = { 1526db8dac20SDavid S. Miller .name = "UDPv6", 1527db8dac20SDavid S. Miller .owner = THIS_MODULE, 1528db8dac20SDavid S. Miller .close = udp_lib_close, 1529db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1530db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1531db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1532850cbaddSPaolo Abeni .init = udp_init_sock, 1533db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1534db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1535db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1536db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1537db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 1538e646b657SMartin KaFai Lau .release_cb = ip6_datagram_release_cb, 1539db8dac20SDavid S. Miller .hash = udp_lib_hash, 1540db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1541719f8358SEric Dumazet .rehash = udp_v6_rehash, 1542db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 1543db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1544db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1545db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1546db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1547db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1548645ca708SEric Dumazet .h.udp_table = &udp_table, 1549db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1550db8dac20SDavid S. Miller .compat_setsockopt = compat_udpv6_setsockopt, 1551db8dac20SDavid S. Miller .compat_getsockopt = compat_udpv6_getsockopt, 1552db8dac20SDavid S. Miller #endif 15535d77dca8SDavid Ahern .diag_destroy = udp_abort, 1554db8dac20SDavid S. Miller }; 1555db8dac20SDavid S. Miller 1556db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1557db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1558db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1559db8dac20SDavid S. Miller .prot = &udpv6_prot, 1560db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1561db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1562db8dac20SDavid S. Miller }; 1563db8dac20SDavid S. Miller 1564db8dac20SDavid S. Miller int __init udpv6_init(void) 1565db8dac20SDavid S. Miller { 1566db8dac20SDavid S. Miller int ret; 1567db8dac20SDavid S. Miller 15683336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 15693336288aSVlad Yasevich if (ret) 1570c6b641a4SVlad Yasevich goto out; 15713336288aSVlad Yasevich 1572db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1573db8dac20SDavid S. Miller if (ret) 1574db8dac20SDavid S. Miller goto out_udpv6_protocol; 1575db8dac20SDavid S. Miller out: 1576db8dac20SDavid S. Miller return ret; 1577db8dac20SDavid S. Miller 1578db8dac20SDavid S. Miller out_udpv6_protocol: 1579db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1580db8dac20SDavid S. Miller goto out; 1581db8dac20SDavid S. Miller } 1582db8dac20SDavid S. Miller 1583db8dac20SDavid S. Miller void udpv6_exit(void) 1584db8dac20SDavid S. Miller { 1585db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1586db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1587db8dac20SDavid S. Miller } 1588