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> 38db8dac20SDavid S. Miller #include <asm/uaccess.h> 39db8dac20SDavid S. Miller 40db8dac20SDavid S. Miller #include <net/ndisc.h> 41db8dac20SDavid S. Miller #include <net/protocol.h> 42db8dac20SDavid S. Miller #include <net/transp_v6.h> 43db8dac20SDavid S. Miller #include <net/ip6_route.h> 44db8dac20SDavid S. Miller #include <net/raw.h> 45db8dac20SDavid S. Miller #include <net/tcp_states.h> 46db8dac20SDavid S. Miller #include <net/ip6_checksum.h> 47db8dac20SDavid S. Miller #include <net/xfrm.h> 4872289b96STom Herbert #include <net/inet6_hashtables.h> 49076bb0c8SEliezer Tamir #include <net/busy_poll.h> 50db8dac20SDavid S. Miller 51db8dac20SDavid S. Miller #include <linux/proc_fs.h> 52db8dac20SDavid S. Miller #include <linux/seq_file.h> 5322911fc5SEric Dumazet #include <trace/events/skb.h> 54db8dac20SDavid S. Miller #include "udp_impl.h" 55db8dac20SDavid S. Miller 56b50026b5SHannes Frederic Sowa static unsigned int udp6_ehashfn(struct net *net, 57b50026b5SHannes Frederic Sowa const struct in6_addr *laddr, 58b50026b5SHannes Frederic Sowa const u16 lport, 59b50026b5SHannes Frederic Sowa const struct in6_addr *faddr, 60b50026b5SHannes Frederic Sowa const __be16 fport) 61b50026b5SHannes Frederic Sowa { 621bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly; 631bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly; 641bbdceefSHannes Frederic Sowa 651bbdceefSHannes Frederic Sowa u32 lhash, fhash; 661bbdceefSHannes Frederic Sowa 671bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret, 681bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret)); 691bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret, 701bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret)); 711bbdceefSHannes Frederic Sowa 721bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3]; 731bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret); 741bbdceefSHannes Frederic Sowa 75b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport, 761bbdceefSHannes Frederic Sowa udp_ipv6_hash_secret + net_hash_mix(net)); 77b50026b5SHannes Frederic Sowa } 78b50026b5SHannes Frederic Sowa 79b2f5e7cdSVlad Yasevich int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 80b2f5e7cdSVlad Yasevich { 81b2f5e7cdSVlad Yasevich const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 82b2f5e7cdSVlad Yasevich int sk2_ipv6only = inet_v6_ipv6only(sk2); 83efe4208fSEric Dumazet int addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr); 84b2f5e7cdSVlad Yasevich int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 85b2f5e7cdSVlad Yasevich 86b2f5e7cdSVlad Yasevich /* if both are mapped, treat as IPv4 */ 87b2f5e7cdSVlad Yasevich if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) 88499923c7SVlad Yasevich return (!sk2_ipv6only && 8950805466SEric Dumazet (!sk->sk_rcv_saddr || !sk2->sk_rcv_saddr || 9050805466SEric Dumazet sk->sk_rcv_saddr == sk2->sk_rcv_saddr)); 91b2f5e7cdSVlad Yasevich 92b2f5e7cdSVlad Yasevich if (addr_type2 == IPV6_ADDR_ANY && 93b2f5e7cdSVlad Yasevich !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 94b2f5e7cdSVlad Yasevich return 1; 95b2f5e7cdSVlad Yasevich 96b2f5e7cdSVlad Yasevich if (addr_type == IPV6_ADDR_ANY && 979fe516baSEric Dumazet !(ipv6_only_sock(sk) && addr_type2 == IPV6_ADDR_MAPPED)) 98b2f5e7cdSVlad Yasevich return 1; 99b2f5e7cdSVlad Yasevich 100b2f5e7cdSVlad Yasevich if (sk2_rcv_saddr6 && 101efe4208fSEric Dumazet ipv6_addr_equal(&sk->sk_v6_rcv_saddr, sk2_rcv_saddr6)) 102b2f5e7cdSVlad Yasevich return 1; 103b2f5e7cdSVlad Yasevich 104b2f5e7cdSVlad Yasevich return 0; 105b2f5e7cdSVlad Yasevich } 106b2f5e7cdSVlad Yasevich 107d4cada4aSEric Dumazet static unsigned int udp6_portaddr_hash(struct net *net, 108d4cada4aSEric Dumazet const struct in6_addr *addr6, 109d4cada4aSEric Dumazet unsigned int port) 110d4cada4aSEric Dumazet { 111d4cada4aSEric Dumazet unsigned int hash, mix = net_hash_mix(net); 112d4cada4aSEric Dumazet 113d4cada4aSEric Dumazet if (ipv6_addr_any(addr6)) 114d4cada4aSEric Dumazet hash = jhash_1word(0, mix); 115856540eeSBrian Haley else if (ipv6_addr_v4mapped(addr6)) 1160eae88f3SEric Dumazet hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix); 117d4cada4aSEric Dumazet else 1180eae88f3SEric Dumazet hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix); 119d4cada4aSEric Dumazet 120d4cada4aSEric Dumazet return hash ^ port; 121d4cada4aSEric Dumazet } 122d4cada4aSEric Dumazet 123d4cada4aSEric Dumazet 1246ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum) 125db8dac20SDavid S. Miller { 12630fff923SEric Dumazet unsigned int hash2_nulladdr = 12730fff923SEric Dumazet udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 12830fff923SEric Dumazet unsigned int hash2_partial = 129efe4208fSEric Dumazet udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0); 13030fff923SEric Dumazet 131d4cada4aSEric Dumazet /* precompute partial secondary hash */ 13230fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial; 13330fff923SEric Dumazet return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr); 134db8dac20SDavid S. Miller } 135db8dac20SDavid S. Miller 136719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk) 137719f8358SEric Dumazet { 138719f8358SEric Dumazet u16 new_hash = udp6_portaddr_hash(sock_net(sk), 139efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr, 140719f8358SEric Dumazet inet_sk(sk)->inet_num); 141719f8358SEric Dumazet 142719f8358SEric Dumazet udp_lib_rehash(sk, new_hash); 143719f8358SEric Dumazet } 144719f8358SEric Dumazet 145645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, 146645ca708SEric Dumazet unsigned short hnum, 14788440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 14888440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 149645ca708SEric Dumazet int dif) 150db8dac20SDavid S. Miller { 151645ca708SEric Dumazet int score = -1; 152db8dac20SDavid S. Miller 153d4cada4aSEric Dumazet if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 154db8dac20SDavid S. Miller sk->sk_family == PF_INET6) { 155645ca708SEric Dumazet struct inet_sock *inet = inet_sk(sk); 156645ca708SEric Dumazet 157645ca708SEric Dumazet score = 0; 158c720c7e8SEric Dumazet if (inet->inet_dport) { 159c720c7e8SEric Dumazet if (inet->inet_dport != sport) 160645ca708SEric Dumazet return -1; 161db8dac20SDavid S. Miller score++; 162db8dac20SDavid S. Miller } 163efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) { 164efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 165645ca708SEric Dumazet return -1; 166db8dac20SDavid S. Miller score++; 167db8dac20SDavid S. Miller } 168efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 169efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 170645ca708SEric Dumazet return -1; 171db8dac20SDavid S. Miller score++; 172db8dac20SDavid S. Miller } 173db8dac20SDavid S. Miller if (sk->sk_bound_dev_if) { 174db8dac20SDavid S. Miller if (sk->sk_bound_dev_if != dif) 175645ca708SEric Dumazet return -1; 176db8dac20SDavid S. Miller score++; 177db8dac20SDavid S. Miller } 178645ca708SEric Dumazet } 179645ca708SEric Dumazet return score; 180645ca708SEric Dumazet } 181645ca708SEric Dumazet 182fddc17deSEric Dumazet #define SCORE2_MAX (1 + 1 + 1) 183fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net, 184fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 185fddc17deSEric Dumazet const struct in6_addr *daddr, unsigned short hnum, 186fddc17deSEric Dumazet int dif) 187fddc17deSEric Dumazet { 188fddc17deSEric Dumazet int score = -1; 189fddc17deSEric Dumazet 190fddc17deSEric Dumazet if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 191fddc17deSEric Dumazet sk->sk_family == PF_INET6) { 192fddc17deSEric Dumazet struct inet_sock *inet = inet_sk(sk); 193fddc17deSEric Dumazet 194efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 195fddc17deSEric Dumazet return -1; 196fddc17deSEric Dumazet score = 0; 197fddc17deSEric Dumazet if (inet->inet_dport) { 198fddc17deSEric Dumazet if (inet->inet_dport != sport) 199fddc17deSEric Dumazet return -1; 200fddc17deSEric Dumazet score++; 201fddc17deSEric Dumazet } 202efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 203efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr)) 204fddc17deSEric Dumazet return -1; 205fddc17deSEric Dumazet score++; 206fddc17deSEric Dumazet } 207fddc17deSEric Dumazet if (sk->sk_bound_dev_if) { 208fddc17deSEric Dumazet if (sk->sk_bound_dev_if != dif) 209fddc17deSEric Dumazet return -1; 210fddc17deSEric Dumazet score++; 211fddc17deSEric Dumazet } 212fddc17deSEric Dumazet } 213fddc17deSEric Dumazet return score; 214fddc17deSEric Dumazet } 215fddc17deSEric Dumazet 216fddc17deSEric Dumazet 217fddc17deSEric Dumazet /* called with read_rcu_lock() */ 218fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net, 219fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport, 220fddc17deSEric Dumazet const struct in6_addr *daddr, unsigned int hnum, int dif, 221fddc17deSEric Dumazet struct udp_hslot *hslot2, unsigned int slot2) 222fddc17deSEric Dumazet { 223fddc17deSEric Dumazet struct sock *sk, *result; 224fddc17deSEric Dumazet struct hlist_nulls_node *node; 22572289b96STom Herbert int score, badness, matches = 0, reuseport = 0; 22672289b96STom Herbert u32 hash = 0; 227fddc17deSEric Dumazet 228fddc17deSEric Dumazet begin: 229fddc17deSEric Dumazet result = NULL; 230fddc17deSEric Dumazet badness = -1; 231fddc17deSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) { 232fddc17deSEric Dumazet score = compute_score2(sk, net, saddr, sport, 233fddc17deSEric Dumazet daddr, hnum, dif); 234fddc17deSEric Dumazet if (score > badness) { 235fddc17deSEric Dumazet result = sk; 236fddc17deSEric Dumazet badness = score; 23772289b96STom Herbert reuseport = sk->sk_reuseport; 23872289b96STom Herbert if (reuseport) { 239b50026b5SHannes Frederic Sowa hash = udp6_ehashfn(net, daddr, hnum, 24072289b96STom Herbert saddr, sport); 24172289b96STom Herbert matches = 1; 24272289b96STom Herbert } else if (score == SCORE2_MAX) 243fddc17deSEric Dumazet goto exact_match; 24472289b96STom Herbert } else if (score == badness && reuseport) { 24572289b96STom Herbert matches++; 2468fc54f68SDaniel Borkmann if (reciprocal_scale(hash, matches) == 0) 24772289b96STom Herbert result = sk; 24872289b96STom Herbert hash = next_pseudo_random32(hash); 249fddc17deSEric Dumazet } 250fddc17deSEric Dumazet } 251fddc17deSEric Dumazet /* 252fddc17deSEric Dumazet * if the nulls value we got at the end of this lookup is 253fddc17deSEric Dumazet * not the expected one, we must restart lookup. 254fddc17deSEric Dumazet * We probably met an item that was moved to another chain. 255fddc17deSEric Dumazet */ 256fddc17deSEric Dumazet if (get_nulls_value(node) != slot2) 257fddc17deSEric Dumazet goto begin; 258fddc17deSEric Dumazet 259fddc17deSEric Dumazet if (result) { 260fddc17deSEric Dumazet exact_match: 261c31504dcSEric Dumazet if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2))) 262fddc17deSEric Dumazet result = NULL; 263fddc17deSEric Dumazet else if (unlikely(compute_score2(result, net, saddr, sport, 264fddc17deSEric Dumazet daddr, hnum, dif) < badness)) { 265fddc17deSEric Dumazet sock_put(result); 266fddc17deSEric Dumazet goto begin; 267fddc17deSEric Dumazet } 268fddc17deSEric Dumazet } 269fddc17deSEric Dumazet return result; 270fddc17deSEric Dumazet } 271fddc17deSEric Dumazet 272fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net, 27388440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport, 27488440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, 275645ca708SEric Dumazet int dif, struct udp_table *udptable) 276645ca708SEric Dumazet { 277271b72c7SEric Dumazet struct sock *sk, *result; 27888ab1932SEric Dumazet struct hlist_nulls_node *node; 279645ca708SEric Dumazet unsigned short hnum = ntohs(dport); 280fddc17deSEric Dumazet unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 281fddc17deSEric Dumazet struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 28272289b96STom Herbert int score, badness, matches = 0, reuseport = 0; 28372289b96STom Herbert u32 hash = 0; 284645ca708SEric Dumazet 285271b72c7SEric Dumazet rcu_read_lock(); 286fddc17deSEric Dumazet if (hslot->count > 10) { 287fddc17deSEric Dumazet hash2 = udp6_portaddr_hash(net, daddr, hnum); 288fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 289fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 290fddc17deSEric Dumazet if (hslot->count < hslot2->count) 291fddc17deSEric Dumazet goto begin; 292fddc17deSEric Dumazet 293fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport, 294fddc17deSEric Dumazet daddr, hnum, dif, 295fddc17deSEric Dumazet hslot2, slot2); 296fddc17deSEric Dumazet if (!result) { 297fddc17deSEric Dumazet hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum); 298fddc17deSEric Dumazet slot2 = hash2 & udptable->mask; 299fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2]; 300fddc17deSEric Dumazet if (hslot->count < hslot2->count) 301fddc17deSEric Dumazet goto begin; 302fddc17deSEric Dumazet 3031223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport, 3041223c67cSJorge Boncompte [DTI2] &in6addr_any, hnum, dif, 305fddc17deSEric Dumazet hslot2, slot2); 306fddc17deSEric Dumazet } 307fddc17deSEric Dumazet rcu_read_unlock(); 308fddc17deSEric Dumazet return result; 309fddc17deSEric Dumazet } 310271b72c7SEric Dumazet begin: 311271b72c7SEric Dumazet result = NULL; 312271b72c7SEric Dumazet badness = -1; 31388ab1932SEric Dumazet sk_nulls_for_each_rcu(sk, node, &hslot->head) { 314645ca708SEric Dumazet score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif); 315645ca708SEric Dumazet if (score > badness) { 316db8dac20SDavid S. Miller result = sk; 317db8dac20SDavid S. Miller badness = score; 31872289b96STom Herbert reuseport = sk->sk_reuseport; 31972289b96STom Herbert if (reuseport) { 320b50026b5SHannes Frederic Sowa hash = udp6_ehashfn(net, daddr, hnum, 32172289b96STom Herbert saddr, sport); 32272289b96STom Herbert matches = 1; 32372289b96STom Herbert } 32472289b96STom Herbert } else if (score == badness && reuseport) { 32572289b96STom Herbert matches++; 3268fc54f68SDaniel Borkmann if (reciprocal_scale(hash, matches) == 0) 32772289b96STom Herbert result = sk; 32872289b96STom Herbert hash = next_pseudo_random32(hash); 329db8dac20SDavid S. Miller } 330db8dac20SDavid S. Miller } 33188ab1932SEric Dumazet /* 33288ab1932SEric Dumazet * if the nulls value we got at the end of this lookup is 33388ab1932SEric Dumazet * not the expected one, we must restart lookup. 33488ab1932SEric Dumazet * We probably met an item that was moved to another chain. 33588ab1932SEric Dumazet */ 336fddc17deSEric Dumazet if (get_nulls_value(node) != slot) 33788ab1932SEric Dumazet goto begin; 33888ab1932SEric Dumazet 339271b72c7SEric Dumazet if (result) { 340c31504dcSEric Dumazet if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2))) 341271b72c7SEric Dumazet result = NULL; 342271b72c7SEric Dumazet else if (unlikely(compute_score(result, net, hnum, saddr, sport, 343271b72c7SEric Dumazet daddr, dport, dif) < badness)) { 344271b72c7SEric Dumazet sock_put(result); 345271b72c7SEric Dumazet goto begin; 346271b72c7SEric Dumazet } 347271b72c7SEric Dumazet } 348271b72c7SEric Dumazet rcu_read_unlock(); 349db8dac20SDavid S. Miller return result; 350db8dac20SDavid S. Miller } 351fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 352db8dac20SDavid S. Miller 353607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 354607c4aafSKOVACS Krisztian __be16 sport, __be16 dport, 355645ca708SEric Dumazet struct udp_table *udptable) 356607c4aafSKOVACS Krisztian { 35723542618SKOVACS Krisztian struct sock *sk; 358b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 359607c4aafSKOVACS Krisztian 360e5d08d71SIan Morris sk = skb_steal_sock(skb); 361e5d08d71SIan Morris if (unlikely(sk)) 36223542618SKOVACS Krisztian return sk; 363adf30907SEric Dumazet return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport, 364607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb), 365607c4aafSKOVACS Krisztian udptable); 366607c4aafSKOVACS Krisztian } 367607c4aafSKOVACS Krisztian 368aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 369aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif) 370aa976fc0SBalazs Scheidler { 371aa976fc0SBalazs Scheidler return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table); 372aa976fc0SBalazs Scheidler } 373aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup); 374aa976fc0SBalazs Scheidler 375aa976fc0SBalazs Scheidler 376db8dac20SDavid S. Miller /* 377db8dac20SDavid S. Miller * This should be easy, if there is something there we 378db8dac20SDavid S. Miller * return it, otherwise we block. 379db8dac20SDavid S. Miller */ 380db8dac20SDavid S. Miller 381db8dac20SDavid S. Miller int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 382db8dac20SDavid S. Miller struct msghdr *msg, size_t len, 383db8dac20SDavid S. Miller int noblock, int flags, int *addr_len) 384db8dac20SDavid S. Miller { 385db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 386db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 387db8dac20SDavid S. Miller struct sk_buff *skb; 38859c2cdaeSDavid S. Miller unsigned int ulen, copied; 3893f518bf7SPavel Emelyanov int peeked, off = 0; 390db8dac20SDavid S. Miller int err; 391db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 392f26ba175SWei Yongjun int is_udp4; 3938a74ad60SEric Dumazet bool slow; 394db8dac20SDavid S. Miller 395db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE) 39685fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len); 397db8dac20SDavid S. Miller 3984b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu) 39985fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len); 4004b340ae2SBrian Haley 401db8dac20SDavid S. Miller try_again: 402db8dac20SDavid S. Miller skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 4033f518bf7SPavel Emelyanov &peeked, &off, &err); 404db8dac20SDavid S. Miller if (!skb) 405db8dac20SDavid S. Miller goto out; 406db8dac20SDavid S. Miller 407db8dac20SDavid S. Miller ulen = skb->len - sizeof(struct udphdr); 40859c2cdaeSDavid S. Miller copied = len; 40959c2cdaeSDavid S. Miller if (copied > ulen) 41059c2cdaeSDavid S. Miller copied = ulen; 41159c2cdaeSDavid S. Miller else if (copied < ulen) 412db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC; 413db8dac20SDavid S. Miller 414f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP)); 415f26ba175SWei Yongjun 416db8dac20SDavid S. Miller /* 417db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the 418db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial 419db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy. 420db8dac20SDavid S. Miller */ 421db8dac20SDavid S. Miller 42259c2cdaeSDavid S. Miller if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 423db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 424db8dac20SDavid S. Miller goto csum_copy_err; 425db8dac20SDavid S. Miller } 426db8dac20SDavid S. Miller 427db8dac20SDavid S. Miller if (skb_csum_unnecessary(skb)) 42851f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, sizeof(struct udphdr), 42951f3d02bSDavid S. Miller msg, copied); 430db8dac20SDavid S. Miller else { 431db8dac20SDavid S. Miller err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 432db8dac20SDavid S. Miller if (err == -EINVAL) 433db8dac20SDavid S. Miller goto csum_copy_err; 434db8dac20SDavid S. Miller } 43522911fc5SEric Dumazet if (unlikely(err)) { 43622911fc5SEric Dumazet trace_kfree_skb(skb, udpv6_recvmsg); 437979402b1SEric Dumazet if (!peeked) { 438979402b1SEric Dumazet atomic_inc(&sk->sk_drops); 439979402b1SEric Dumazet if (is_udp4) 440979402b1SEric Dumazet UDP_INC_STATS_USER(sock_net(sk), 441979402b1SEric Dumazet UDP_MIB_INERRORS, 442979402b1SEric Dumazet is_udplite); 443979402b1SEric Dumazet else 444979402b1SEric Dumazet UDP6_INC_STATS_USER(sock_net(sk), 445979402b1SEric Dumazet UDP_MIB_INERRORS, 446979402b1SEric Dumazet is_udplite); 447979402b1SEric Dumazet } 448db8dac20SDavid S. Miller goto out_free; 44922911fc5SEric Dumazet } 450f26ba175SWei Yongjun if (!peeked) { 451f26ba175SWei Yongjun if (is_udp4) 452f26ba175SWei Yongjun UDP_INC_STATS_USER(sock_net(sk), 453f26ba175SWei Yongjun UDP_MIB_INDATAGRAMS, is_udplite); 454f26ba175SWei Yongjun else 455235b9f7aSPavel Emelyanov UDP6_INC_STATS_USER(sock_net(sk), 456235b9f7aSPavel Emelyanov UDP_MIB_INDATAGRAMS, is_udplite); 457f26ba175SWei Yongjun } 458db8dac20SDavid S. Miller 4593b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 460db8dac20SDavid S. Miller 461db8dac20SDavid S. Miller /* Copy the address. */ 462db8dac20SDavid S. Miller if (msg->msg_name) { 463342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 464db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6; 465db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source; 466db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0; 467db8dac20SDavid S. Miller 468842df073SHannes Frederic Sowa if (is_udp4) { 469b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 470b301e82cSBrian Haley &sin6->sin6_addr); 471842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0; 472842df073SHannes Frederic Sowa } else { 4734e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr; 474842df073SHannes Frederic Sowa sin6->sin6_scope_id = 475842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr, 4764330487aSDuan Jiong inet6_iif(skb)); 477db8dac20SDavid S. Miller } 478bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6); 479db8dac20SDavid S. Miller } 4804b261c75SHannes Frederic Sowa 4814b261c75SHannes Frederic Sowa if (np->rxopt.all) 4824b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb); 4834b261c75SHannes Frederic Sowa 484f26ba175SWei Yongjun if (is_udp4) { 485db8dac20SDavid S. Miller if (inet->cmsg_flags) 486db8dac20SDavid S. Miller ip_cmsg_recv(msg, skb); 487db8dac20SDavid S. Miller } else { 488db8dac20SDavid S. Miller if (np->rxopt.all) 4894b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb); 490db8dac20SDavid S. Miller } 491db8dac20SDavid S. Miller 49259c2cdaeSDavid S. Miller err = copied; 493db8dac20SDavid S. Miller if (flags & MSG_TRUNC) 494db8dac20SDavid S. Miller err = ulen; 495db8dac20SDavid S. Miller 496db8dac20SDavid S. Miller out_free: 4979d410c79SEric Dumazet skb_free_datagram_locked(sk, skb); 498db8dac20SDavid S. Miller out: 499db8dac20SDavid S. Miller return err; 500db8dac20SDavid S. Miller 501db8dac20SDavid S. Miller csum_copy_err: 5028a74ad60SEric Dumazet slow = lock_sock_fast(sk); 5030856f939SWei Yongjun if (!skb_kill_datagram(sk, skb, flags)) { 5046a5dc9e5SEric Dumazet if (is_udp4) { 5056a5dc9e5SEric Dumazet UDP_INC_STATS_USER(sock_net(sk), 5066a5dc9e5SEric Dumazet UDP_MIB_CSUMERRORS, is_udplite); 5070856f939SWei Yongjun UDP_INC_STATS_USER(sock_net(sk), 5080856f939SWei Yongjun UDP_MIB_INERRORS, is_udplite); 5096a5dc9e5SEric Dumazet } else { 5106a5dc9e5SEric Dumazet UDP6_INC_STATS_USER(sock_net(sk), 5116a5dc9e5SEric Dumazet UDP_MIB_CSUMERRORS, is_udplite); 5120856f939SWei Yongjun UDP6_INC_STATS_USER(sock_net(sk), 5130856f939SWei Yongjun UDP_MIB_INERRORS, is_udplite); 5140856f939SWei Yongjun } 5156a5dc9e5SEric Dumazet } 5168a74ad60SEric Dumazet unlock_sock_fast(sk, slow); 517db8dac20SDavid S. Miller 51832c90254SXufeng Zhang if (noblock) 519db8dac20SDavid S. Miller return -EAGAIN; 5209cfaa8deSXufeng Zhang 5219cfaa8deSXufeng Zhang /* starting over for a new packet */ 5229cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC; 523db8dac20SDavid S. Miller goto try_again; 524db8dac20SDavid S. Miller } 525db8dac20SDavid S. Miller 526db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 527d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info, 528645ca708SEric Dumazet struct udp_table *udptable) 529db8dac20SDavid S. Miller { 530db8dac20SDavid S. Miller struct ipv6_pinfo *np; 531b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 532b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr; 533b71d1d42SEric Dumazet const struct in6_addr *daddr = &hdr->daddr; 534db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset); 535db8dac20SDavid S. Miller struct sock *sk; 536db8dac20SDavid S. Miller int err; 5377304fe46SDuan Jiong struct net *net = dev_net(skb->dev); 538db8dac20SDavid S. Miller 5397304fe46SDuan Jiong sk = __udp6_lib_lookup(net, daddr, uh->dest, 540db8dac20SDavid S. Miller saddr, uh->source, inet6_iif(skb), udptable); 5417304fe46SDuan Jiong if (sk == NULL) { 5427304fe46SDuan Jiong ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev), 5437304fe46SDuan Jiong ICMP6_MIB_INERRORS); 544db8dac20SDavid S. Miller return; 5457304fe46SDuan Jiong } 546db8dac20SDavid S. Miller 54793b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) { 54893b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk)) 54993b36cf3SHannes Frederic Sowa goto out; 55081aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info); 55193b36cf3SHannes Frederic Sowa } 5521a462d18SDuan Jiong if (type == NDISC_REDIRECT) { 553ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk); 5541a462d18SDuan Jiong goto out; 5551a462d18SDuan Jiong } 55681aded24SDavid S. Miller 557db8dac20SDavid S. Miller np = inet6_sk(sk); 558db8dac20SDavid S. Miller 559db8dac20SDavid S. Miller if (!icmpv6_err_convert(type, code, &err) && !np->recverr) 560db8dac20SDavid S. Miller goto out; 561db8dac20SDavid S. Miller 562db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) 563db8dac20SDavid S. Miller goto out; 564db8dac20SDavid S. Miller 565b1faf566SEric Dumazet if (np->recverr) 566db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 567b1faf566SEric Dumazet 568db8dac20SDavid S. Miller sk->sk_err = err; 569db8dac20SDavid S. Miller sk->sk_error_report(sk); 570db8dac20SDavid S. Miller out: 571db8dac20SDavid S. Miller sock_put(sk); 572db8dac20SDavid S. Miller } 573db8dac20SDavid S. Miller 574f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 575f7ad74feSBenjamin LaHaise { 576f7ad74feSBenjamin LaHaise int rc; 577f7ad74feSBenjamin LaHaise 578efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) { 579f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb); 580005ec974SShawn Bohrer sk_mark_napi_id(sk, skb); 5812c8c56e1SEric Dumazet sk_incoming_cpu_update(sk); 582005ec974SShawn Bohrer } 583f7ad74feSBenjamin LaHaise 584f7ad74feSBenjamin LaHaise rc = sock_queue_rcv_skb(sk, skb); 585f7ad74feSBenjamin LaHaise if (rc < 0) { 586f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk); 587f7ad74feSBenjamin LaHaise 588f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */ 589f7ad74feSBenjamin LaHaise if (rc == -ENOMEM) 590f7ad74feSBenjamin LaHaise UDP6_INC_STATS_BH(sock_net(sk), 591f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite); 592f7ad74feSBenjamin LaHaise UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 593f7ad74feSBenjamin LaHaise kfree_skb(skb); 594f7ad74feSBenjamin LaHaise return -1; 595f7ad74feSBenjamin LaHaise } 596f7ad74feSBenjamin LaHaise return 0; 597f7ad74feSBenjamin LaHaise } 598f7ad74feSBenjamin LaHaise 599db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb, 600d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type, 601d5fdd6baSBrian Haley u8 code, int offset, __be32 info) 602db8dac20SDavid S. Miller { 603645ca708SEric Dumazet __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 604db8dac20SDavid S. Miller } 605db8dac20SDavid S. Miller 606d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly; 607d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void) 608d7f3f621SBenjamin LaHaise { 609d7f3f621SBenjamin LaHaise if (!static_key_enabled(&udpv6_encap_needed)) 610d7f3f621SBenjamin LaHaise static_key_slow_inc(&udpv6_encap_needed); 611d7f3f621SBenjamin LaHaise } 612d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable); 613d7f3f621SBenjamin LaHaise 614db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 615db8dac20SDavid S. Miller { 616db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 617db8dac20SDavid S. Miller int rc; 618db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 619db8dac20SDavid S. Miller 620db8dac20SDavid S. Miller if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 621db8dac20SDavid S. Miller goto drop; 622db8dac20SDavid S. Miller 623d7f3f621SBenjamin LaHaise if (static_key_false(&udpv6_encap_needed) && up->encap_type) { 624d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 625d7f3f621SBenjamin LaHaise 626d7f3f621SBenjamin LaHaise /* 627d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to 628d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just 629d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket. 630d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value: 631d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap 632d7f3f621SBenjamin LaHaise * handler or was discarded by it. 633d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP. 634d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N 635d7f3f621SBenjamin LaHaise */ 636d7f3f621SBenjamin LaHaise 637d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */ 638d7f3f621SBenjamin LaHaise encap_rcv = ACCESS_ONCE(up->encap_rcv); 639d7f3f621SBenjamin LaHaise if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) { 640d7f3f621SBenjamin LaHaise int ret; 641d7f3f621SBenjamin LaHaise 6420a80966bSTom Herbert /* Verify checksum before giving to encap */ 6430a80966bSTom Herbert if (udp_lib_checksum_complete(skb)) 6440a80966bSTom Herbert goto csum_error; 6450a80966bSTom Herbert 646d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb); 647d7f3f621SBenjamin LaHaise if (ret <= 0) { 648d7f3f621SBenjamin LaHaise UDP_INC_STATS_BH(sock_net(sk), 649d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS, 650d7f3f621SBenjamin LaHaise is_udplite); 651d7f3f621SBenjamin LaHaise return -ret; 652d7f3f621SBenjamin LaHaise } 653d7f3f621SBenjamin LaHaise } 654d7f3f621SBenjamin LaHaise 655d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */ 656d7f3f621SBenjamin LaHaise } 657d7f3f621SBenjamin LaHaise 658db8dac20SDavid S. Miller /* 659db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 660db8dac20SDavid S. Miller */ 661db8dac20SDavid S. Miller if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 662db8dac20SDavid S. Miller 663db8dac20SDavid S. Miller if (up->pcrlen == 0) { /* full coverage was set */ 664ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n", 665db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len); 666db8dac20SDavid S. Miller goto drop; 667db8dac20SDavid S. Miller } 668db8dac20SDavid S. Miller if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 669ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n", 670db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, up->pcrlen); 671db8dac20SDavid S. Miller goto drop; 672db8dac20SDavid S. Miller } 673db8dac20SDavid S. Miller } 674db8dac20SDavid S. Miller 67533d480ceSEric Dumazet if (rcu_access_pointer(sk->sk_filter)) { 676db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 6776a5dc9e5SEric Dumazet goto csum_error; 678db8dac20SDavid S. Miller } 679db8dac20SDavid S. Miller 680274f482dSSorin Dumitru if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) { 6813e215c8dSJames M Leddy UDP6_INC_STATS_BH(sock_net(sk), 6823e215c8dSJames M Leddy UDP_MIB_RCVBUFERRORS, is_udplite); 683cb80ef46SBenjamin LaHaise goto drop; 6843e215c8dSJames M Leddy } 685cb80ef46SBenjamin LaHaise 686d826eb14SEric Dumazet skb_dst_drop(skb); 687db8dac20SDavid S. Miller 688cb80ef46SBenjamin LaHaise bh_lock_sock(sk); 689cb80ef46SBenjamin LaHaise rc = 0; 690cb80ef46SBenjamin LaHaise if (!sock_owned_by_user(sk)) 691f7ad74feSBenjamin LaHaise rc = __udpv6_queue_rcv_skb(sk, skb); 692cb80ef46SBenjamin LaHaise else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) { 693cb80ef46SBenjamin LaHaise bh_unlock_sock(sk); 694cb80ef46SBenjamin LaHaise goto drop; 695cb80ef46SBenjamin LaHaise } 696cb80ef46SBenjamin LaHaise bh_unlock_sock(sk); 697f7ad74feSBenjamin LaHaise 698f7ad74feSBenjamin LaHaise return rc; 6993e215c8dSJames M Leddy 7006a5dc9e5SEric Dumazet csum_error: 7016a5dc9e5SEric Dumazet UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite); 702db8dac20SDavid S. Miller drop: 703ef28d1a2SPavel Emelyanov UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 704cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops); 705db8dac20SDavid S. Miller kfree_skb(skb); 706db8dac20SDavid S. Miller return -1; 707db8dac20SDavid S. Miller } 708db8dac20SDavid S. Miller 7095cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk, 710b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr, 711b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr, 7125cf3d461SDavid Held int dif, unsigned short hnum) 713db8dac20SDavid S. Miller { 7149e89fd8bSSven Wegener struct inet_sock *inet = inet_sk(sk); 715db8dac20SDavid S. Miller 7169e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net)) 7175cf3d461SDavid Held return false; 718b8ad0cbcSDaniel Lezcano 7195cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum || 7205cf3d461SDavid Held sk->sk_family != PF_INET6 || 7215cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) || 7225cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) && 7235cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) || 7245cf3d461SDavid Held (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)) 7255cf3d461SDavid Held return false; 7269e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr)) 7275cf3d461SDavid Held return false; 7285cf3d461SDavid Held return true; 729db8dac20SDavid S. Miller } 730db8dac20SDavid S. Miller 731a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count, 732a1ab77f9SEric Dumazet struct sk_buff *skb, unsigned int final) 733a1ab77f9SEric Dumazet { 734cb80ef46SBenjamin LaHaise struct sk_buff *skb1 = NULL; 735a1ab77f9SEric Dumazet struct sock *sk; 736cb80ef46SBenjamin LaHaise unsigned int i; 737a1ab77f9SEric Dumazet 738a1ab77f9SEric Dumazet for (i = 0; i < count; i++) { 739a1ab77f9SEric Dumazet sk = stack[i]; 740cb80ef46SBenjamin LaHaise if (likely(skb1 == NULL)) 741cb80ef46SBenjamin LaHaise skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC); 742cb80ef46SBenjamin LaHaise if (!skb1) { 743f6b8f32cSEric Dumazet atomic_inc(&sk->sk_drops); 744cb80ef46SBenjamin LaHaise UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 745cb80ef46SBenjamin LaHaise IS_UDPLITE(sk)); 746cb80ef46SBenjamin LaHaise UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, 747cb80ef46SBenjamin LaHaise IS_UDPLITE(sk)); 748a1ab77f9SEric Dumazet } 749cb80ef46SBenjamin LaHaise 750cb80ef46SBenjamin LaHaise if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0) 751cb80ef46SBenjamin LaHaise skb1 = NULL; 7522dc41cffSDavid Held sock_put(sk); 753cb80ef46SBenjamin LaHaise } 754cb80ef46SBenjamin LaHaise if (unlikely(skb1)) 755cb80ef46SBenjamin LaHaise kfree_skb(skb1); 756a1ab77f9SEric Dumazet } 7574068579eSTom Herbert 7584068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb) 7594068579eSTom Herbert { 7604068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log 7614068579eSTom Herbert * this error. Well, it is reasonable. 7624068579eSTom Herbert */ 763ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n", 7644068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source), 7654068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest)); 7664068579eSTom Herbert } 7674068579eSTom Herbert 768db8dac20SDavid S. Miller /* 769db8dac20SDavid S. Miller * Note: called only from the BH handler context, 770db8dac20SDavid S. Miller * so we don't need to lock the hashes. 771db8dac20SDavid S. Miller */ 772e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 773b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr, 77436cbb245SRick Jones struct udp_table *udptable, int proto) 775db8dac20SDavid S. Miller { 776a1ab77f9SEric Dumazet struct sock *sk, *stack[256 / sizeof(struct sock *)]; 777db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb); 7785cf3d461SDavid Held struct hlist_nulls_node *node; 7795cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest); 7805cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum); 7815cf3d461SDavid Held int dif = inet6_iif(skb); 7822dc41cffSDavid Held unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node); 7832dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10); 78436cbb245SRick Jones bool inner_flushed = false; 7852dc41cffSDavid Held 7862dc41cffSDavid Held if (use_hash2) { 7872dc41cffSDavid Held hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) & 7882dc41cffSDavid Held udp_table.mask; 7892dc41cffSDavid Held hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask; 7902dc41cffSDavid Held start_lookup: 7912dc41cffSDavid Held hslot = &udp_table.hash2[hash2]; 7922dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); 7932dc41cffSDavid Held } 794db8dac20SDavid S. Miller 795645ca708SEric Dumazet spin_lock(&hslot->lock); 7962dc41cffSDavid Held sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) { 7975cf3d461SDavid Held if (__udp_v6_is_mcast_sock(net, sk, 7985cf3d461SDavid Held uh->dest, daddr, 7995cf3d461SDavid Held uh->source, saddr, 8005cf3d461SDavid Held dif, hnum) && 8011c19448cSTom Herbert /* If zero checksum and no_check is not on for 8024068579eSTom Herbert * the socket then skip it. 8034068579eSTom Herbert */ 8045cf3d461SDavid Held (uh->check || udp_sk(sk)->no_check6_rx)) { 805a1ab77f9SEric Dumazet if (unlikely(count == ARRAY_SIZE(stack))) { 806a1ab77f9SEric Dumazet flush_stack(stack, count, skb, ~0); 80736cbb245SRick Jones inner_flushed = true; 808a1ab77f9SEric Dumazet count = 0; 809db8dac20SDavid S. Miller } 8105cf3d461SDavid Held stack[count++] = sk; 8112dc41cffSDavid Held sock_hold(sk); 8125cf3d461SDavid Held } 813a1ab77f9SEric Dumazet } 814db8dac20SDavid S. Miller 815645ca708SEric Dumazet spin_unlock(&hslot->lock); 816a1ab77f9SEric Dumazet 8172dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */ 8182dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) { 8192dc41cffSDavid Held hash2 = hash2_any; 8202dc41cffSDavid Held goto start_lookup; 8212dc41cffSDavid Held } 8222dc41cffSDavid Held 823a1ab77f9SEric Dumazet if (count) { 824a1ab77f9SEric Dumazet flush_stack(stack, count, skb, count - 1); 825a1ab77f9SEric Dumazet } else { 82636cbb245SRick Jones if (!inner_flushed) 82736cbb245SRick Jones UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI, 82836cbb245SRick Jones proto == IPPROTO_UDPLITE); 82936cbb245SRick Jones consume_skb(skb); 830a1ab77f9SEric Dumazet } 831db8dac20SDavid S. Miller return 0; 832db8dac20SDavid S. Miller } 833db8dac20SDavid S. Miller 834645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 835db8dac20SDavid S. Miller int proto) 836db8dac20SDavid S. Miller { 8373ffe533cSAlexey Dobriyan struct net *net = dev_net(skb->dev); 838db8dac20SDavid S. Miller struct sock *sk; 839db8dac20SDavid S. Miller struct udphdr *uh; 840b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr; 841db8dac20SDavid S. Miller u32 ulen = 0; 842db8dac20SDavid S. Miller 843db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr))) 844d6bc0149SBjørn Mork goto discard; 845db8dac20SDavid S. Miller 846db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 847db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 848db8dac20SDavid S. Miller uh = udp_hdr(skb); 849db8dac20SDavid S. Miller 850db8dac20SDavid S. Miller ulen = ntohs(uh->len); 851db8dac20SDavid S. Miller if (ulen > skb->len) 852db8dac20SDavid S. Miller goto short_packet; 853db8dac20SDavid S. Miller 854db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) { 855db8dac20SDavid S. Miller /* UDP validates ulen. */ 856db8dac20SDavid S. Miller 857db8dac20SDavid S. Miller /* Check for jumbo payload */ 858db8dac20SDavid S. Miller if (ulen == 0) 859db8dac20SDavid S. Miller ulen = skb->len; 860db8dac20SDavid S. Miller 861db8dac20SDavid S. Miller if (ulen < sizeof(*uh)) 862db8dac20SDavid S. Miller goto short_packet; 863db8dac20SDavid S. Miller 864db8dac20SDavid S. Miller if (ulen < skb->len) { 865db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen)) 866db8dac20SDavid S. Miller goto short_packet; 867db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr; 868db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr; 869db8dac20SDavid S. Miller uh = udp_hdr(skb); 870db8dac20SDavid S. Miller } 871db8dac20SDavid S. Miller } 872db8dac20SDavid S. Miller 873db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto)) 8746a5dc9e5SEric Dumazet goto csum_error; 875db8dac20SDavid S. Miller 876db8dac20SDavid S. Miller /* 877db8dac20SDavid S. Miller * Multicast receive code 878db8dac20SDavid S. Miller */ 879db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr)) 880e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb, 88136cbb245SRick Jones saddr, daddr, udptable, proto); 882db8dac20SDavid S. Miller 883db8dac20SDavid S. Miller /* Unicast */ 884db8dac20SDavid S. Miller 885db8dac20SDavid S. Miller /* 886db8dac20SDavid S. Miller * check socket cache ... must talk to Alan about his plans 887db8dac20SDavid S. Miller * for sock caches... i'll skip this for now. 888db8dac20SDavid S. Miller */ 889607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 890cb80ef46SBenjamin LaHaise if (sk != NULL) { 891a5b50476SEliezer Tamir int ret; 892a5b50476SEliezer Tamir 8931c19448cSTom Herbert if (!uh->check && !udp_sk(sk)->no_check6_rx) { 89479e0f1c9STom Herbert sock_put(sk); 8954068579eSTom Herbert udp6_csum_zero_error(skb); 8964068579eSTom Herbert goto csum_error; 8974068579eSTom Herbert } 8984068579eSTom Herbert 8992abb7cdcSTom Herbert if (udp_sk(sk)->convert_csum && uh->check && !IS_UDPLITE(sk)) 9002abb7cdcSTom Herbert skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check, 9012abb7cdcSTom Herbert ip6_compute_pseudo); 9022abb7cdcSTom Herbert 903a5b50476SEliezer Tamir ret = udpv6_queue_rcv_skb(sk, skb); 904cb80ef46SBenjamin LaHaise sock_put(sk); 905db8dac20SDavid S. Miller 906cb80ef46SBenjamin LaHaise /* a return value > 0 means to resubmit the input, but 907cb80ef46SBenjamin LaHaise * it wants the return to be -protocol, or 0 908cb80ef46SBenjamin LaHaise */ 909cb80ef46SBenjamin LaHaise if (ret > 0) 910cb80ef46SBenjamin LaHaise return -ret; 911cb80ef46SBenjamin LaHaise 912cb80ef46SBenjamin LaHaise return 0; 913cb80ef46SBenjamin LaHaise } 914cb80ef46SBenjamin LaHaise 9154068579eSTom Herbert if (!uh->check) { 9164068579eSTom Herbert udp6_csum_zero_error(skb); 9174068579eSTom Herbert goto csum_error; 9184068579eSTom Herbert } 9194068579eSTom Herbert 920db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 921db8dac20SDavid S. Miller goto discard; 922db8dac20SDavid S. Miller 923db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb)) 9246a5dc9e5SEric Dumazet goto csum_error; 925db8dac20SDavid S. Miller 926cb80ef46SBenjamin LaHaise UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 9273ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 928db8dac20SDavid S. Miller 929db8dac20SDavid S. Miller kfree_skb(skb); 930db8dac20SDavid S. Miller return 0; 931db8dac20SDavid S. Miller 932db8dac20SDavid S. Miller short_packet: 933ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 934db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "", 935ba7a46f1SJoe Perches saddr, ntohs(uh->source), 936ba7a46f1SJoe Perches ulen, skb->len, 937ba7a46f1SJoe Perches daddr, ntohs(uh->dest)); 9386a5dc9e5SEric Dumazet goto discard; 9396a5dc9e5SEric Dumazet csum_error: 9406a5dc9e5SEric Dumazet UDP6_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE); 941db8dac20SDavid S. Miller discard: 942ef28d1a2SPavel Emelyanov UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 943db8dac20SDavid S. Miller kfree_skb(skb); 944db8dac20SDavid S. Miller return 0; 945db8dac20SDavid S. Miller } 946db8dac20SDavid S. Miller 947db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb) 948db8dac20SDavid S. Miller { 949645ca708SEric Dumazet return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 950db8dac20SDavid S. Miller } 951db8dac20SDavid S. Miller 952db8dac20SDavid S. Miller /* 953db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked. 954db8dac20SDavid S. Miller */ 955db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk) 956db8dac20SDavid S. Miller { 957db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 958db8dac20SDavid S. Miller 95936d926b9SDenis V. Lunev if (up->pending == AF_INET) 96036d926b9SDenis V. Lunev udp_flush_pending_frames(sk); 96136d926b9SDenis V. Lunev else if (up->pending) { 962db8dac20SDavid S. Miller up->len = 0; 963db8dac20SDavid S. Miller up->pending = 0; 964db8dac20SDavid S. Miller ip6_flush_pending_frames(sk); 965db8dac20SDavid S. Miller } 966db8dac20SDavid S. Miller } 967db8dac20SDavid S. Miller 968493c6be3SSridhar Samudrala /** 969493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming 970493c6be3SSridhar Samudrala * @sk: socket we are sending on 971493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header 972493c6be3SSridhar Samudrala * (checksum field must be zeroed out) 973493c6be3SSridhar Samudrala */ 974493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 975493c6be3SSridhar Samudrala const struct in6_addr *saddr, 976493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len) 977493c6be3SSridhar Samudrala { 978493c6be3SSridhar Samudrala unsigned int offset; 979493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb); 980493c6be3SSridhar Samudrala __wsum csum = 0; 981493c6be3SSridhar Samudrala 982493c6be3SSridhar Samudrala if (skb_queue_len(&sk->sk_write_queue) == 1) { 983493c6be3SSridhar Samudrala /* Only one fragment on the socket. */ 984493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head; 985493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check); 986493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 987493c6be3SSridhar Samudrala } else { 988493c6be3SSridhar Samudrala /* 989493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more 990493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs 991493c6be3SSridhar Samudrala * should be together 992493c6be3SSridhar Samudrala */ 993493c6be3SSridhar Samudrala offset = skb_transport_offset(skb); 994493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 995493c6be3SSridhar Samudrala 996493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE; 997493c6be3SSridhar Samudrala 998493c6be3SSridhar Samudrala skb_queue_walk(&sk->sk_write_queue, skb) { 999493c6be3SSridhar Samudrala csum = csum_add(csum, skb->csum); 1000493c6be3SSridhar Samudrala } 1001493c6be3SSridhar Samudrala 1002493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 1003493c6be3SSridhar Samudrala csum); 1004493c6be3SSridhar Samudrala if (uh->check == 0) 1005493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0; 1006493c6be3SSridhar Samudrala } 1007493c6be3SSridhar Samudrala } 1008493c6be3SSridhar Samudrala 1009db8dac20SDavid S. Miller /* 1010db8dac20SDavid S. Miller * Sending 1011db8dac20SDavid S. Miller */ 1012db8dac20SDavid S. Miller 1013db8dac20SDavid S. Miller static int udp_v6_push_pending_frames(struct sock *sk) 1014db8dac20SDavid S. Miller { 1015db8dac20SDavid S. Miller struct sk_buff *skb; 1016db8dac20SDavid S. Miller struct udphdr *uh; 1017db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1018db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 10198822b64aSHannes Frederic Sowa struct flowi6 *fl6; 1020db8dac20SDavid S. Miller int err = 0; 1021db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1022db8dac20SDavid S. Miller __wsum csum = 0; 1023db8dac20SDavid S. Miller 10248822b64aSHannes Frederic Sowa if (up->pending == AF_INET) 10258822b64aSHannes Frederic Sowa return udp_push_pending_frames(sk); 10268822b64aSHannes Frederic Sowa 10278822b64aSHannes Frederic Sowa fl6 = &inet->cork.fl.u.ip6; 10288822b64aSHannes Frederic Sowa 1029db8dac20SDavid S. Miller /* Grab the skbuff where UDP header space exists. */ 1030e5d08d71SIan Morris skb = skb_peek(&sk->sk_write_queue); 1031e5d08d71SIan Morris if (skb == NULL) 1032db8dac20SDavid S. Miller goto out; 1033db8dac20SDavid S. Miller 1034db8dac20SDavid S. Miller /* 1035db8dac20SDavid S. Miller * Create a UDP header 1036db8dac20SDavid S. Miller */ 1037db8dac20SDavid S. Miller uh = udp_hdr(skb); 10381958b856SDavid S. Miller uh->source = fl6->fl6_sport; 10391958b856SDavid S. Miller uh->dest = fl6->fl6_dport; 1040db8dac20SDavid S. Miller uh->len = htons(up->len); 1041db8dac20SDavid S. Miller uh->check = 0; 1042db8dac20SDavid S. Miller 1043db8dac20SDavid S. Miller if (is_udplite) 1044db8dac20SDavid S. Miller csum = udplite_csum_outgoing(sk, skb); 10451c19448cSTom Herbert else if (up->no_check6_tx) { /* UDP csum disabled */ 10464068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE; 10474068579eSTom Herbert goto send; 10484068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 10494c9483b2SDavid S. Miller udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, 1050493c6be3SSridhar Samudrala up->len); 1051493c6be3SSridhar Samudrala goto send; 1052493c6be3SSridhar Samudrala } else 1053db8dac20SDavid S. Miller csum = udp_csum_outgoing(sk, skb); 1054db8dac20SDavid S. Miller 1055db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */ 10564c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 10574c9483b2SDavid S. Miller up->len, fl6->flowi6_proto, csum); 1058db8dac20SDavid S. Miller if (uh->check == 0) 1059db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0; 1060db8dac20SDavid S. Miller 1061493c6be3SSridhar Samudrala send: 1062db8dac20SDavid S. Miller err = ip6_push_pending_frames(sk); 10636ce9e7b5SEric Dumazet if (err) { 10646ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 10656ce9e7b5SEric Dumazet UDP6_INC_STATS_USER(sock_net(sk), 10666ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite); 10676ce9e7b5SEric Dumazet err = 0; 10686ce9e7b5SEric Dumazet } 10696ce9e7b5SEric Dumazet } else 10706ce9e7b5SEric Dumazet UDP6_INC_STATS_USER(sock_net(sk), 10716ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite); 1072db8dac20SDavid S. Miller out: 1073db8dac20SDavid S. Miller up->len = 0; 1074db8dac20SDavid S. Miller up->pending = 0; 1075db8dac20SDavid S. Miller return err; 1076db8dac20SDavid S. Miller } 1077db8dac20SDavid S. Miller 1078db8dac20SDavid S. Miller int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 1079db8dac20SDavid S. Miller struct msghdr *msg, size_t len) 1080db8dac20SDavid S. Miller { 1081db8dac20SDavid S. Miller struct ipv6_txoptions opt_space; 1082db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk); 1083db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk); 1084db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk); 1085342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 108620c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final; 1087db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL; 1088db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL; 10894c9483b2SDavid S. Miller struct flowi6 fl6; 1090db8dac20SDavid S. Miller struct dst_entry *dst; 1091db8dac20SDavid S. Miller int addr_len = msg->msg_namelen; 1092db8dac20SDavid S. Miller int ulen = len; 1093db8dac20SDavid S. Miller int hlimit = -1; 1094db8dac20SDavid S. Miller int tclass = -1; 109513b52cd4SBrian Haley int dontfrag = -1; 1096db8dac20SDavid S. Miller int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 1097db8dac20SDavid S. Miller int err; 1098db8dac20SDavid S. Miller int connected = 0; 1099db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk); 1100db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1101db8dac20SDavid S. Miller 1102db8dac20SDavid S. Miller /* destination address check */ 1103db8dac20SDavid S. Miller if (sin6) { 1104db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data)) 1105db8dac20SDavid S. Miller return -EINVAL; 1106db8dac20SDavid S. Miller 1107db8dac20SDavid S. Miller switch (sin6->sin6_family) { 1108db8dac20SDavid S. Miller case AF_INET6: 1109db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133) 1110db8dac20SDavid S. Miller return -EINVAL; 1111db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1112db8dac20SDavid S. Miller break; 1113db8dac20SDavid S. Miller case AF_INET: 1114db8dac20SDavid S. Miller goto do_udp_sendmsg; 1115db8dac20SDavid S. Miller case AF_UNSPEC: 1116db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL; 1117db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0; 1118db8dac20SDavid S. Miller daddr = NULL; 1119db8dac20SDavid S. Miller break; 1120db8dac20SDavid S. Miller default: 1121db8dac20SDavid S. Miller return -EINVAL; 1122db8dac20SDavid S. Miller } 1123db8dac20SDavid S. Miller } else if (!up->pending) { 1124db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1125db8dac20SDavid S. Miller return -EDESTADDRREQ; 1126efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1127db8dac20SDavid S. Miller } else 1128db8dac20SDavid S. Miller daddr = NULL; 1129db8dac20SDavid S. Miller 1130db8dac20SDavid S. Miller if (daddr) { 1131db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) { 1132db8dac20SDavid S. Miller struct sockaddr_in sin; 1133db8dac20SDavid S. Miller sin.sin_family = AF_INET; 1134c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1135db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1136db8dac20SDavid S. Miller msg->msg_name = &sin; 1137db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin); 1138db8dac20SDavid S. Miller do_udp_sendmsg: 1139db8dac20SDavid S. Miller if (__ipv6_only_sock(sk)) 1140db8dac20SDavid S. Miller return -ENETUNREACH; 1141db8dac20SDavid S. Miller return udp_sendmsg(iocb, sk, msg, len); 1142db8dac20SDavid S. Miller } 1143db8dac20SDavid S. Miller } 1144db8dac20SDavid S. Miller 1145db8dac20SDavid S. Miller if (up->pending == AF_INET) 1146db8dac20SDavid S. Miller return udp_sendmsg(iocb, sk, msg, len); 1147db8dac20SDavid S. Miller 1148db8dac20SDavid S. Miller /* Rough check on arithmetic overflow, 1149db8dac20SDavid S. Miller better check is made in ip6_append_data(). 1150db8dac20SDavid S. Miller */ 1151db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr)) 1152db8dac20SDavid S. Miller return -EMSGSIZE; 1153db8dac20SDavid S. Miller 1154db8dac20SDavid S. Miller if (up->pending) { 1155db8dac20SDavid S. Miller /* 1156db8dac20SDavid S. Miller * There are pending frames. 1157db8dac20SDavid S. Miller * The socket lock must be held while it's corked. 1158db8dac20SDavid S. Miller */ 1159db8dac20SDavid S. Miller lock_sock(sk); 1160db8dac20SDavid S. Miller if (likely(up->pending)) { 1161db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) { 1162db8dac20SDavid S. Miller release_sock(sk); 1163db8dac20SDavid S. Miller return -EAFNOSUPPORT; 1164db8dac20SDavid S. Miller } 1165db8dac20SDavid S. Miller dst = NULL; 1166db8dac20SDavid S. Miller goto do_append_data; 1167db8dac20SDavid S. Miller } 1168db8dac20SDavid S. Miller release_sock(sk); 1169db8dac20SDavid S. Miller } 1170db8dac20SDavid S. Miller ulen += sizeof(struct udphdr); 1171db8dac20SDavid S. Miller 11724c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1173db8dac20SDavid S. Miller 1174db8dac20SDavid S. Miller if (sin6) { 1175db8dac20SDavid S. Miller if (sin6->sin6_port == 0) 1176db8dac20SDavid S. Miller return -EINVAL; 1177db8dac20SDavid S. Miller 11781958b856SDavid S. Miller fl6.fl6_dport = sin6->sin6_port; 1179db8dac20SDavid S. Miller daddr = &sin6->sin6_addr; 1180db8dac20SDavid S. Miller 1181db8dac20SDavid S. Miller if (np->sndflow) { 11824c9483b2SDavid S. Miller fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 11834c9483b2SDavid S. Miller if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 11844c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1185db8dac20SDavid S. Miller if (flowlabel == NULL) 1186db8dac20SDavid S. Miller return -EINVAL; 1187db8dac20SDavid S. Miller } 1188db8dac20SDavid S. Miller } 1189db8dac20SDavid S. Miller 1190db8dac20SDavid S. Miller /* 1191db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain 1192db8dac20SDavid S. Miller * sk->sk_dst_cache. 1193db8dac20SDavid S. Miller */ 1194db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED && 1195efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr)) 1196efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 1197db8dac20SDavid S. Miller 1198db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) && 1199db8dac20SDavid S. Miller sin6->sin6_scope_id && 1200842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr))) 12014c9483b2SDavid S. Miller fl6.flowi6_oif = sin6->sin6_scope_id; 1202db8dac20SDavid S. Miller } else { 1203db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED) 1204db8dac20SDavid S. Miller return -EDESTADDRREQ; 1205db8dac20SDavid S. Miller 12061958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 1207efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr; 12084c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 1209db8dac20SDavid S. Miller connected = 1; 1210db8dac20SDavid S. Miller } 1211db8dac20SDavid S. Miller 12124c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 12134c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 1214db8dac20SDavid S. Miller 12154c9483b2SDavid S. Miller if (!fl6.flowi6_oif) 12164c9483b2SDavid S. Miller fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 12179f690db7SYang Hongyang 12184c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 121951953d5bSBrian Haley 1220db8dac20SDavid S. Miller if (msg->msg_controllen) { 1221db8dac20SDavid S. Miller opt = &opt_space; 1222db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions)); 1223db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt); 1224db8dac20SDavid S. Miller 122573df66f8STom Parkin err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt, 1226ec0506dbSMaciej Żenczykowski &hlimit, &tclass, &dontfrag); 1227db8dac20SDavid S. Miller if (err < 0) { 1228db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1229db8dac20SDavid S. Miller return err; 1230db8dac20SDavid S. Miller } 12314c9483b2SDavid S. Miller if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 12324c9483b2SDavid S. Miller flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1233db8dac20SDavid S. Miller if (flowlabel == NULL) 1234db8dac20SDavid S. Miller return -EINVAL; 1235db8dac20SDavid S. Miller } 1236db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen)) 1237db8dac20SDavid S. Miller opt = NULL; 1238db8dac20SDavid S. Miller connected = 0; 1239db8dac20SDavid S. Miller } 1240db8dac20SDavid S. Miller if (opt == NULL) 1241db8dac20SDavid S. Miller opt = np->opt; 1242db8dac20SDavid S. Miller if (flowlabel) 1243db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt); 1244db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt); 1245db8dac20SDavid S. Miller 12464c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 1247876c7f41SBrian Haley if (!ipv6_addr_any(daddr)) 12484e3fd7a0SAlexey Dobriyan fl6.daddr = *daddr; 1249876c7f41SBrian Haley else 12504c9483b2SDavid S. Miller fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 12514c9483b2SDavid S. Miller if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 12524e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 12531958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 1254db8dac20SDavid S. Miller 12554c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, opt, &final); 125620c59de2SArnaud Ebalard if (final_p) 1257db8dac20SDavid S. Miller connected = 0; 1258db8dac20SDavid S. Miller 12594c9483b2SDavid S. Miller if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) { 12604c9483b2SDavid S. Miller fl6.flowi6_oif = np->mcast_oif; 1261db8dac20SDavid S. Miller connected = 0; 1262c4062dfcSErich E. Hoover } else if (!fl6.flowi6_oif) 1263c4062dfcSErich E. Hoover fl6.flowi6_oif = np->ucast_oif; 1264db8dac20SDavid S. Miller 12654c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 1266db8dac20SDavid S. Miller 12670e0d44abSSteffen Klassert dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p); 126868d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 126968d0c6d3SDavid S. Miller err = PTR_ERR(dst); 127068d0c6d3SDavid S. Miller dst = NULL; 1271db8dac20SDavid S. Miller goto out; 1272db8dac20SDavid S. Miller } 1273db8dac20SDavid S. Miller 1274db8dac20SDavid S. Miller if (hlimit < 0) 12755c98631cSLorenzo Colitti hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 1276db8dac20SDavid S. Miller 1277db8dac20SDavid S. Miller if (tclass < 0) 1278e651f03aSGerrit Renker tclass = np->tclass; 1279db8dac20SDavid S. Miller 1280db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM) 1281db8dac20SDavid S. Miller goto do_confirm; 1282db8dac20SDavid S. Miller back_from_confirm: 1283db8dac20SDavid S. Miller 1284db8dac20SDavid S. Miller lock_sock(sk); 1285db8dac20SDavid S. Miller if (unlikely(up->pending)) { 1286db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */ 1287db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */ 1288db8dac20SDavid S. Miller release_sock(sk); 1289db8dac20SDavid S. Miller 1290ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n"); 1291db8dac20SDavid S. Miller err = -EINVAL; 1292db8dac20SDavid S. Miller goto out; 1293db8dac20SDavid S. Miller } 1294db8dac20SDavid S. Miller 1295db8dac20SDavid S. Miller up->pending = AF_INET6; 1296db8dac20SDavid S. Miller 1297db8dac20SDavid S. Miller do_append_data: 1298e36d3ff9SJiri Pirko if (dontfrag < 0) 1299e36d3ff9SJiri Pirko dontfrag = np->dontfrag; 1300db8dac20SDavid S. Miller up->len += ulen; 1301db8dac20SDavid S. Miller getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1302db8dac20SDavid S. Miller err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen, 13034c9483b2SDavid S. Miller sizeof(struct udphdr), hlimit, tclass, opt, &fl6, 1304db8dac20SDavid S. Miller (struct rt6_info *)dst, 130513b52cd4SBrian Haley corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag); 1306db8dac20SDavid S. Miller if (err) 1307db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1308db8dac20SDavid S. Miller else if (!corkreq) 1309db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk); 1310db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1311db8dac20SDavid S. Miller up->pending = 0; 1312db8dac20SDavid S. Miller 1313db8dac20SDavid S. Miller if (dst) { 1314db8dac20SDavid S. Miller if (connected) { 1315db8dac20SDavid S. Miller ip6_dst_store(sk, dst, 1316efe4208fSEric Dumazet ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ? 1317efe4208fSEric Dumazet &sk->sk_v6_daddr : NULL, 1318db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 13194c9483b2SDavid S. Miller ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 1320db8dac20SDavid S. Miller &np->saddr : 1321db8dac20SDavid S. Miller #endif 1322db8dac20SDavid S. Miller NULL); 1323db8dac20SDavid S. Miller } else { 1324db8dac20SDavid S. Miller dst_release(dst); 1325db8dac20SDavid S. Miller } 1326a3c96089SYOSHIFUJI Hideaki dst = NULL; 1327db8dac20SDavid S. Miller } 1328db8dac20SDavid S. Miller 1329db8dac20SDavid S. Miller if (err > 0) 1330db8dac20SDavid S. Miller err = np->recverr ? net_xmit_errno(err) : 0; 1331db8dac20SDavid S. Miller release_sock(sk); 1332db8dac20SDavid S. Miller out: 1333a3c96089SYOSHIFUJI Hideaki dst_release(dst); 1334db8dac20SDavid S. Miller fl6_sock_release(flowlabel); 1335db8dac20SDavid S. Miller if (!err) 1336db8dac20SDavid S. Miller return len; 1337db8dac20SDavid S. Miller /* 1338db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1339db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise 1340db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many 1341db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that 1342db8dac20SDavid S. Miller * seems like overkill. 1343db8dac20SDavid S. Miller */ 1344db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 1345235b9f7aSPavel Emelyanov UDP6_INC_STATS_USER(sock_net(sk), 1346235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite); 1347db8dac20SDavid S. Miller } 1348db8dac20SDavid S. Miller return err; 1349db8dac20SDavid S. Miller 1350db8dac20SDavid S. Miller do_confirm: 1351db8dac20SDavid S. Miller dst_confirm(dst); 1352db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len) 1353db8dac20SDavid S. Miller goto back_from_confirm; 1354db8dac20SDavid S. Miller err = 0; 1355db8dac20SDavid S. Miller goto out; 1356db8dac20SDavid S. Miller } 1357db8dac20SDavid S. Miller 13587d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk) 1359db8dac20SDavid S. Miller { 136044046a59STom Parkin struct udp_sock *up = udp_sk(sk); 1361db8dac20SDavid S. Miller lock_sock(sk); 1362db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk); 1363db8dac20SDavid S. Miller release_sock(sk); 1364db8dac20SDavid S. Miller 136544046a59STom Parkin if (static_key_false(&udpv6_encap_needed) && up->encap_type) { 136644046a59STom Parkin void (*encap_destroy)(struct sock *sk); 136744046a59STom Parkin encap_destroy = ACCESS_ONCE(up->encap_destroy); 136844046a59STom Parkin if (encap_destroy) 136944046a59STom Parkin encap_destroy(sk); 137044046a59STom Parkin } 137144046a59STom Parkin 1372db8dac20SDavid S. Miller inet6_destroy_sock(sk); 1373db8dac20SDavid S. Miller } 1374db8dac20SDavid S. Miller 1375db8dac20SDavid S. Miller /* 1376db8dac20SDavid S. Miller * Socket option code for UDP 1377db8dac20SDavid S. Miller */ 1378db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname, 1379b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1380db8dac20SDavid S. Miller { 1381db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1382db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1383db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1384db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen); 1385db8dac20SDavid S. Miller } 1386db8dac20SDavid S. Miller 1387db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1388db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 1389b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 1390db8dac20SDavid S. Miller { 1391db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1392db8dac20SDavid S. Miller return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1393db8dac20SDavid S. Miller udp_v6_push_pending_frames); 1394db8dac20SDavid S. Miller return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); 1395db8dac20SDavid S. Miller } 1396db8dac20SDavid S. Miller #endif 1397db8dac20SDavid S. Miller 1398db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname, 1399db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1400db8dac20SDavid S. Miller { 1401db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1402db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1403db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen); 1404db8dac20SDavid S. Miller } 1405db8dac20SDavid S. Miller 1406db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1407db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 1408db8dac20SDavid S. Miller char __user *optval, int __user *optlen) 1409db8dac20SDavid S. Miller { 1410db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE) 1411db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1412db8dac20SDavid S. Miller return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); 1413db8dac20SDavid S. Miller } 1414db8dac20SDavid S. Miller #endif 1415db8dac20SDavid S. Miller 141641135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = { 1417db8dac20SDavid S. Miller .handler = udpv6_rcv, 1418db8dac20SDavid S. Miller .err_handler = udpv6_err, 1419db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1420db8dac20SDavid S. Miller }; 1421db8dac20SDavid S. Miller 1422db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1423db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS 1424db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v) 1425db8dac20SDavid S. Miller { 142617ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) { 142717ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER); 142817ef66afSLorenzo Colitti } else { 142917ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket; 143017ef66afSLorenzo Colitti struct inet_sock *inet = inet_sk(v); 143117ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport); 143217ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport); 143317ef66afSLorenzo Colitti ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket); 143417ef66afSLorenzo Colitti } 1435db8dac20SDavid S. Miller return 0; 1436db8dac20SDavid S. Miller } 1437db8dac20SDavid S. Miller 143873cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = { 143973cb88ecSArjan van de Ven .owner = THIS_MODULE, 144073cb88ecSArjan van de Ven .open = udp_seq_open, 144173cb88ecSArjan van de Ven .read = seq_read, 144273cb88ecSArjan van de Ven .llseek = seq_lseek, 144373cb88ecSArjan van de Ven .release = seq_release_net 144473cb88ecSArjan van de Ven }; 144573cb88ecSArjan van de Ven 1446db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = { 1447db8dac20SDavid S. Miller .name = "udp6", 1448db8dac20SDavid S. Miller .family = AF_INET6, 1449645ca708SEric Dumazet .udp_table = &udp_table, 145073cb88ecSArjan van de Ven .seq_fops = &udp6_afinfo_seq_fops, 1451dda61925SDenis V. Lunev .seq_ops = { 1452dda61925SDenis V. Lunev .show = udp6_seq_show, 1453dda61925SDenis V. Lunev }, 1454db8dac20SDavid S. Miller }; 1455db8dac20SDavid S. Miller 14562c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net) 1457db8dac20SDavid S. Miller { 14580c96d8c5SDaniel Lezcano return udp_proc_register(net, &udp6_seq_afinfo); 1459db8dac20SDavid S. Miller } 1460db8dac20SDavid S. Miller 14610c96d8c5SDaniel Lezcano void udp6_proc_exit(struct net *net) { 14620c96d8c5SDaniel Lezcano udp_proc_unregister(net, &udp6_seq_afinfo); 1463db8dac20SDavid S. Miller } 1464db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */ 1465db8dac20SDavid S. Miller 1466f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size) 1467f77d6021SEric Dumazet { 1468f77d6021SEric Dumazet struct inet_sock *inet = inet_sk(sk); 1469f77d6021SEric Dumazet 1470f77d6021SEric Dumazet /* we do not want to clear pinet6 field, because of RCU lookups */ 1471f77d6021SEric Dumazet sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6)); 1472f77d6021SEric Dumazet 1473f77d6021SEric Dumazet size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6); 1474f77d6021SEric Dumazet memset(&inet->pinet6 + 1, 0, size); 1475f77d6021SEric Dumazet } 1476f77d6021SEric Dumazet 1477db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */ 1478db8dac20SDavid S. Miller 1479db8dac20SDavid S. Miller struct proto udpv6_prot = { 1480db8dac20SDavid S. Miller .name = "UDPv6", 1481db8dac20SDavid S. Miller .owner = THIS_MODULE, 1482db8dac20SDavid S. Miller .close = udp_lib_close, 1483db8dac20SDavid S. Miller .connect = ip6_datagram_connect, 1484db8dac20SDavid S. Miller .disconnect = udp_disconnect, 1485db8dac20SDavid S. Miller .ioctl = udp_ioctl, 1486db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock, 1487db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt, 1488db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt, 1489db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg, 1490db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg, 1491f7ad74feSBenjamin LaHaise .backlog_rcv = __udpv6_queue_rcv_skb, 1492db8dac20SDavid S. Miller .hash = udp_lib_hash, 1493db8dac20SDavid S. Miller .unhash = udp_lib_unhash, 1494719f8358SEric Dumazet .rehash = udp_v6_rehash, 1495db8dac20SDavid S. Miller .get_port = udp_v6_get_port, 1496db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated, 1497db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem, 1498db8dac20SDavid S. Miller .sysctl_wmem = &sysctl_udp_wmem_min, 1499db8dac20SDavid S. Miller .sysctl_rmem = &sysctl_udp_rmem_min, 1500db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock), 1501271b72c7SEric Dumazet .slab_flags = SLAB_DESTROY_BY_RCU, 1502645ca708SEric Dumazet .h.udp_table = &udp_table, 1503db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT 1504db8dac20SDavid S. Miller .compat_setsockopt = compat_udpv6_setsockopt, 1505db8dac20SDavid S. Miller .compat_getsockopt = compat_udpv6_getsockopt, 1506db8dac20SDavid S. Miller #endif 1507f77d6021SEric Dumazet .clear_sk = udp_v6_clear_sk, 1508db8dac20SDavid S. Miller }; 1509db8dac20SDavid S. Miller 1510db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = { 1511db8dac20SDavid S. Miller .type = SOCK_DGRAM, 1512db8dac20SDavid S. Miller .protocol = IPPROTO_UDP, 1513db8dac20SDavid S. Miller .prot = &udpv6_prot, 1514db8dac20SDavid S. Miller .ops = &inet6_dgram_ops, 1515db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT, 1516db8dac20SDavid S. Miller }; 1517db8dac20SDavid S. Miller 1518db8dac20SDavid S. Miller 1519db8dac20SDavid S. Miller int __init udpv6_init(void) 1520db8dac20SDavid S. Miller { 1521db8dac20SDavid S. Miller int ret; 1522db8dac20SDavid S. Miller 15233336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 15243336288aSVlad Yasevich if (ret) 1525c6b641a4SVlad Yasevich goto out; 15263336288aSVlad Yasevich 1527db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw); 1528db8dac20SDavid S. Miller if (ret) 1529db8dac20SDavid S. Miller goto out_udpv6_protocol; 1530db8dac20SDavid S. Miller out: 1531db8dac20SDavid S. Miller return ret; 1532db8dac20SDavid S. Miller 1533db8dac20SDavid S. Miller out_udpv6_protocol: 1534db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1535db8dac20SDavid S. Miller goto out; 1536db8dac20SDavid S. Miller } 1537db8dac20SDavid S. Miller 1538db8dac20SDavid S. Miller void udpv6_exit(void) 1539db8dac20SDavid S. Miller { 1540db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw); 1541db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1542db8dac20SDavid S. Miller } 1543