xref: /openbmc/linux/net/ipv6/udp.c (revision 59dca1d8)
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>
50e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
51db8dac20SDavid S. Miller 
52db8dac20SDavid S. Miller #include <linux/proc_fs.h>
53db8dac20SDavid S. Miller #include <linux/seq_file.h>
5422911fc5SEric Dumazet #include <trace/events/skb.h>
55db8dac20SDavid S. Miller #include "udp_impl.h"
56db8dac20SDavid S. Miller 
576eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
58b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
59b50026b5SHannes Frederic Sowa 			const u16 lport,
60b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
61b50026b5SHannes Frederic Sowa 			const __be16 fport)
62b50026b5SHannes Frederic Sowa {
631bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
641bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
651bbdceefSHannes Frederic Sowa 
661bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
671bbdceefSHannes Frederic Sowa 
681bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
691bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
701bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
711bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
721bbdceefSHannes Frederic Sowa 
731bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
741bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
751bbdceefSHannes Frederic Sowa 
76b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
771bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
78b50026b5SHannes Frederic Sowa }
79b50026b5SHannes Frederic Sowa 
80e32ea7e7SCraig Gallek /* match_wildcard == true:  IPV6_ADDR_ANY equals to any IPv6 addresses if IPv6
81e32ea7e7SCraig Gallek  *                          only, and any IPv4 addresses if not IPv6 only
82e32ea7e7SCraig Gallek  * match_wildcard == false: addresses must be exactly the same, i.e.
83e32ea7e7SCraig Gallek  *                          IPV6_ADDR_ANY only equals to IPV6_ADDR_ANY,
84e32ea7e7SCraig Gallek  *                          and 0.0.0.0 equals to 0.0.0.0 only
85e32ea7e7SCraig Gallek  */
86e32ea7e7SCraig Gallek int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2,
87e32ea7e7SCraig Gallek 			 bool match_wildcard)
88b2f5e7cdSVlad Yasevich {
89b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
90b2f5e7cdSVlad Yasevich 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
91efe4208fSEric Dumazet 	int addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
92b2f5e7cdSVlad Yasevich 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
93b2f5e7cdSVlad Yasevich 
94b2f5e7cdSVlad Yasevich 	/* if both are mapped, treat as IPv4 */
95e32ea7e7SCraig Gallek 	if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) {
96e32ea7e7SCraig Gallek 		if (!sk2_ipv6only) {
97e32ea7e7SCraig Gallek 			if (sk->sk_rcv_saddr == sk2->sk_rcv_saddr)
98e32ea7e7SCraig Gallek 				return 1;
99e32ea7e7SCraig Gallek 			if (!sk->sk_rcv_saddr || !sk2->sk_rcv_saddr)
100e32ea7e7SCraig Gallek 				return match_wildcard;
101e32ea7e7SCraig Gallek 		}
102e32ea7e7SCraig Gallek 		return 0;
103e32ea7e7SCraig Gallek 	}
104b2f5e7cdSVlad Yasevich 
105e32ea7e7SCraig Gallek 	if (addr_type == IPV6_ADDR_ANY && addr_type2 == IPV6_ADDR_ANY)
106e32ea7e7SCraig Gallek 		return 1;
107e32ea7e7SCraig Gallek 
108e32ea7e7SCraig Gallek 	if (addr_type2 == IPV6_ADDR_ANY && match_wildcard &&
109b2f5e7cdSVlad Yasevich 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
110b2f5e7cdSVlad Yasevich 		return 1;
111b2f5e7cdSVlad Yasevich 
112e32ea7e7SCraig Gallek 	if (addr_type == IPV6_ADDR_ANY && match_wildcard &&
1139fe516baSEric Dumazet 	    !(ipv6_only_sock(sk) && addr_type2 == IPV6_ADDR_MAPPED))
114b2f5e7cdSVlad Yasevich 		return 1;
115b2f5e7cdSVlad Yasevich 
116b2f5e7cdSVlad Yasevich 	if (sk2_rcv_saddr6 &&
117efe4208fSEric Dumazet 	    ipv6_addr_equal(&sk->sk_v6_rcv_saddr, sk2_rcv_saddr6))
118b2f5e7cdSVlad Yasevich 		return 1;
119b2f5e7cdSVlad Yasevich 
120b2f5e7cdSVlad Yasevich 	return 0;
121b2f5e7cdSVlad Yasevich }
122b2f5e7cdSVlad Yasevich 
1236eada011SEric Dumazet static u32 udp6_portaddr_hash(const struct net *net,
124d4cada4aSEric Dumazet 			      const struct in6_addr *addr6,
125d4cada4aSEric Dumazet 			      unsigned int port)
126d4cada4aSEric Dumazet {
127d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
128d4cada4aSEric Dumazet 
129d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
130d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
131856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
1320eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
133d4cada4aSEric Dumazet 	else
1340eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
135d4cada4aSEric Dumazet 
136d4cada4aSEric Dumazet 	return hash ^ port;
137d4cada4aSEric Dumazet }
138d4cada4aSEric Dumazet 
1396ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
140db8dac20SDavid S. Miller {
14130fff923SEric Dumazet 	unsigned int hash2_nulladdr =
14230fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
14330fff923SEric Dumazet 	unsigned int hash2_partial =
144efe4208fSEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
14530fff923SEric Dumazet 
146d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
14730fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
14830fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
149db8dac20SDavid S. Miller }
150db8dac20SDavid S. Miller 
151719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
152719f8358SEric Dumazet {
153719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
154efe4208fSEric Dumazet 					  &sk->sk_v6_rcv_saddr,
155719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
156719f8358SEric Dumazet 
157719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
158719f8358SEric Dumazet }
159719f8358SEric Dumazet 
160645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
161645ca708SEric Dumazet 				unsigned short hnum,
16288440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
16388440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
164645ca708SEric Dumazet 				int dif)
165db8dac20SDavid S. Miller {
16660c04aecSJoe Perches 	int score;
16760c04aecSJoe Perches 	struct inet_sock *inet;
168db8dac20SDavid S. Miller 
16960c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
17060c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
17160c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
17260c04aecSJoe Perches 		return -1;
173645ca708SEric Dumazet 
174645ca708SEric Dumazet 	score = 0;
17560c04aecSJoe Perches 	inet = inet_sk(sk);
17660c04aecSJoe Perches 
177c720c7e8SEric Dumazet 	if (inet->inet_dport) {
178c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
179645ca708SEric Dumazet 			return -1;
180db8dac20SDavid S. Miller 		score++;
181db8dac20SDavid S. Miller 	}
18260c04aecSJoe Perches 
183efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
184efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
185645ca708SEric Dumazet 			return -1;
186db8dac20SDavid S. Miller 		score++;
187db8dac20SDavid S. Miller 	}
18860c04aecSJoe Perches 
189efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
190efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
191645ca708SEric Dumazet 			return -1;
192db8dac20SDavid S. Miller 		score++;
193db8dac20SDavid S. Miller 	}
19460c04aecSJoe Perches 
195db8dac20SDavid S. Miller 	if (sk->sk_bound_dev_if) {
196db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if != dif)
197645ca708SEric Dumazet 			return -1;
198db8dac20SDavid S. Miller 		score++;
199db8dac20SDavid S. Miller 	}
20060c04aecSJoe Perches 
20170da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
20270da268bSEric Dumazet 		score++;
20370da268bSEric Dumazet 
204645ca708SEric Dumazet 	return score;
205645ca708SEric Dumazet }
206645ca708SEric Dumazet 
207fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
208fddc17deSEric Dumazet 				 const struct in6_addr *saddr, __be16 sport,
20960c04aecSJoe Perches 				 const struct in6_addr *daddr,
21060c04aecSJoe Perches 				 unsigned short hnum, int dif)
211fddc17deSEric Dumazet {
21260c04aecSJoe Perches 	int score;
21360c04aecSJoe Perches 	struct inet_sock *inet;
214fddc17deSEric Dumazet 
21560c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
21660c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
21760c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
21860c04aecSJoe Perches 		return -1;
219fddc17deSEric Dumazet 
220efe4208fSEric Dumazet 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
221fddc17deSEric Dumazet 		return -1;
22260c04aecSJoe Perches 
223fddc17deSEric Dumazet 	score = 0;
22460c04aecSJoe Perches 	inet = inet_sk(sk);
22560c04aecSJoe Perches 
226fddc17deSEric Dumazet 	if (inet->inet_dport) {
227fddc17deSEric Dumazet 		if (inet->inet_dport != sport)
228fddc17deSEric Dumazet 			return -1;
229fddc17deSEric Dumazet 		score++;
230fddc17deSEric Dumazet 	}
23160c04aecSJoe Perches 
232efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
233efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
234fddc17deSEric Dumazet 			return -1;
235fddc17deSEric Dumazet 		score++;
236fddc17deSEric Dumazet 	}
23760c04aecSJoe Perches 
238fddc17deSEric Dumazet 	if (sk->sk_bound_dev_if) {
239fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if != dif)
240fddc17deSEric Dumazet 			return -1;
241fddc17deSEric Dumazet 		score++;
242fddc17deSEric Dumazet 	}
24360c04aecSJoe Perches 
24470da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
24570da268bSEric Dumazet 		score++;
24670da268bSEric Dumazet 
247fddc17deSEric Dumazet 	return score;
248fddc17deSEric Dumazet }
249fddc17deSEric Dumazet 
250fddc17deSEric Dumazet /* called with read_rcu_lock() */
251fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
252fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
253fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
2541134158bSCraig Gallek 		struct udp_hslot *hslot2, unsigned int slot2,
2551134158bSCraig Gallek 		struct sk_buff *skb)
256fddc17deSEric Dumazet {
257fddc17deSEric Dumazet 	struct sock *sk, *result;
258fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
25972289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
260ed0dfffdSEric Dumazet 	bool select_ok = true;
26172289b96STom Herbert 	u32 hash = 0;
262fddc17deSEric Dumazet 
263fddc17deSEric Dumazet begin:
264fddc17deSEric Dumazet 	result = NULL;
265fddc17deSEric Dumazet 	badness = -1;
266fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
267fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
268fddc17deSEric Dumazet 				      daddr, hnum, dif);
269fddc17deSEric Dumazet 		if (score > badness) {
270fddc17deSEric Dumazet 			result = sk;
271fddc17deSEric Dumazet 			badness = score;
27272289b96STom Herbert 			reuseport = sk->sk_reuseport;
27372289b96STom Herbert 			if (reuseport) {
274b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
27572289b96STom Herbert 						    saddr, sport);
276ed0dfffdSEric Dumazet 				if (select_ok) {
277ed0dfffdSEric Dumazet 					struct sock *sk2;
278ed0dfffdSEric Dumazet 
2791134158bSCraig Gallek 					sk2 = reuseport_select_sock(sk, hash, skb,
2801134158bSCraig Gallek 							sizeof(struct udphdr));
281e32ea7e7SCraig Gallek 					if (sk2) {
282e32ea7e7SCraig Gallek 						result = sk2;
283ed0dfffdSEric Dumazet 						select_ok = false;
284e32ea7e7SCraig Gallek 						goto found;
285e32ea7e7SCraig Gallek 					}
286ed0dfffdSEric Dumazet 				}
28772289b96STom Herbert 				matches = 1;
28870da268bSEric Dumazet 			}
28972289b96STom Herbert 		} else if (score == badness && reuseport) {
29072289b96STom Herbert 			matches++;
2918fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
29272289b96STom Herbert 				result = sk;
29372289b96STom Herbert 			hash = next_pseudo_random32(hash);
294fddc17deSEric Dumazet 		}
295fddc17deSEric Dumazet 	}
296fddc17deSEric Dumazet 	/*
297fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
298fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
299fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
300fddc17deSEric Dumazet 	 */
301fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
302fddc17deSEric Dumazet 		goto begin;
303fddc17deSEric Dumazet 
304fddc17deSEric Dumazet 	if (result) {
305e32ea7e7SCraig Gallek found:
306c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
307fddc17deSEric Dumazet 			result = NULL;
308fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
309fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
310fddc17deSEric Dumazet 			sock_put(result);
311fddc17deSEric Dumazet 			goto begin;
312fddc17deSEric Dumazet 		}
313fddc17deSEric Dumazet 	}
314fddc17deSEric Dumazet 	return result;
315fddc17deSEric Dumazet }
316fddc17deSEric Dumazet 
317fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
31888440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
31988440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
320538950a1SCraig Gallek 				      int dif, struct udp_table *udptable,
321538950a1SCraig Gallek 				      struct sk_buff *skb)
322645ca708SEric Dumazet {
323271b72c7SEric Dumazet 	struct sock *sk, *result;
32488ab1932SEric Dumazet 	struct hlist_nulls_node *node;
325645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
326fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
327fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
32872289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
329ed0dfffdSEric Dumazet 	bool select_ok = true;
33072289b96STom Herbert 	u32 hash = 0;
331645ca708SEric Dumazet 
332271b72c7SEric Dumazet 	rcu_read_lock();
333fddc17deSEric Dumazet 	if (hslot->count > 10) {
334fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
335fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
336fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
337fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
338fddc17deSEric Dumazet 			goto begin;
339fddc17deSEric Dumazet 
340fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
341fddc17deSEric Dumazet 					  daddr, hnum, dif,
3421134158bSCraig Gallek 					  hslot2, slot2, skb);
343fddc17deSEric Dumazet 		if (!result) {
344fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
345fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
346fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
347fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
348fddc17deSEric Dumazet 				goto begin;
349fddc17deSEric Dumazet 
3501223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
3511223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
3521134158bSCraig Gallek 						  hslot2, slot2, skb);
353fddc17deSEric Dumazet 		}
354fddc17deSEric Dumazet 		rcu_read_unlock();
355fddc17deSEric Dumazet 		return result;
356fddc17deSEric Dumazet 	}
357271b72c7SEric Dumazet begin:
358271b72c7SEric Dumazet 	result = NULL;
359271b72c7SEric Dumazet 	badness = -1;
36088ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
361645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
362645ca708SEric Dumazet 		if (score > badness) {
363db8dac20SDavid S. Miller 			result = sk;
364db8dac20SDavid S. Miller 			badness = score;
36572289b96STom Herbert 			reuseport = sk->sk_reuseport;
36672289b96STom Herbert 			if (reuseport) {
367b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
36872289b96STom Herbert 						    saddr, sport);
369ed0dfffdSEric Dumazet 				if (select_ok) {
370ed0dfffdSEric Dumazet 					struct sock *sk2;
371ed0dfffdSEric Dumazet 
372538950a1SCraig Gallek 					sk2 = reuseport_select_sock(sk, hash, skb,
373538950a1SCraig Gallek 							sizeof(struct udphdr));
374e32ea7e7SCraig Gallek 					if (sk2) {
375e32ea7e7SCraig Gallek 						result = sk2;
376ed0dfffdSEric Dumazet 						select_ok = false;
377e32ea7e7SCraig Gallek 						goto found;
378e32ea7e7SCraig Gallek 					}
379ed0dfffdSEric Dumazet 				}
38072289b96STom Herbert 				matches = 1;
38172289b96STom Herbert 			}
38272289b96STom Herbert 		} else if (score == badness && reuseport) {
38372289b96STom Herbert 			matches++;
3848fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
38572289b96STom Herbert 				result = sk;
38672289b96STom Herbert 			hash = next_pseudo_random32(hash);
387db8dac20SDavid S. Miller 		}
388db8dac20SDavid S. Miller 	}
38988ab1932SEric Dumazet 	/*
39088ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
39188ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
39288ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
39388ab1932SEric Dumazet 	 */
394fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
39588ab1932SEric Dumazet 		goto begin;
39688ab1932SEric Dumazet 
397271b72c7SEric Dumazet 	if (result) {
398e32ea7e7SCraig Gallek found:
399c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
400271b72c7SEric Dumazet 			result = NULL;
401271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
402271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
403271b72c7SEric Dumazet 			sock_put(result);
404271b72c7SEric Dumazet 			goto begin;
405271b72c7SEric Dumazet 		}
406271b72c7SEric Dumazet 	}
407271b72c7SEric Dumazet 	rcu_read_unlock();
408db8dac20SDavid S. Miller 	return result;
409db8dac20SDavid S. Miller }
410fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
411db8dac20SDavid S. Miller 
412607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
413607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
414645ca708SEric Dumazet 					  struct udp_table *udptable)
415607c4aafSKOVACS Krisztian {
41623542618SKOVACS Krisztian 	struct sock *sk;
417b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
418607c4aafSKOVACS Krisztian 
419e5d08d71SIan Morris 	sk = skb_steal_sock(skb);
420e5d08d71SIan Morris 	if (unlikely(sk))
42123542618SKOVACS Krisztian 		return sk;
422adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
423607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
424538950a1SCraig Gallek 				 udptable, skb);
425607c4aafSKOVACS Krisztian }
426607c4aafSKOVACS Krisztian 
427aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
428aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
429aa976fc0SBalazs Scheidler {
430538950a1SCraig Gallek 	return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table, NULL);
431aa976fc0SBalazs Scheidler }
432aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
433aa976fc0SBalazs Scheidler 
434db8dac20SDavid S. Miller /*
435db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
436db8dac20SDavid S. Miller  *	return it, otherwise we block.
437db8dac20SDavid S. Miller  */
438db8dac20SDavid S. Miller 
4391b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
440db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
441db8dac20SDavid S. Miller {
442db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
443db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
444db8dac20SDavid S. Miller 	struct sk_buff *skb;
44559c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
4463f518bf7SPavel Emelyanov 	int peeked, off = 0;
447db8dac20SDavid S. Miller 	int err;
448db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
449197c949eSEric Dumazet 	bool checksum_valid = false;
450f26ba175SWei Yongjun 	int is_udp4;
4518a74ad60SEric Dumazet 	bool slow;
452db8dac20SDavid S. Miller 
453db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
45485fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
455db8dac20SDavid S. Miller 
4564b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
45785fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4584b340ae2SBrian Haley 
459db8dac20SDavid S. Miller try_again:
460db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
4613f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
462db8dac20SDavid S. Miller 	if (!skb)
463db8dac20SDavid S. Miller 		goto out;
464db8dac20SDavid S. Miller 
465db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
46659c2cdaeSDavid S. Miller 	copied = len;
46759c2cdaeSDavid S. Miller 	if (copied > ulen)
46859c2cdaeSDavid S. Miller 		copied = ulen;
46959c2cdaeSDavid S. Miller 	else if (copied < ulen)
470db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
471db8dac20SDavid S. Miller 
472f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
473f26ba175SWei Yongjun 
474db8dac20SDavid S. Miller 	/*
475db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
476db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
477db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
478db8dac20SDavid S. Miller 	 */
479db8dac20SDavid S. Miller 
48059c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
481197c949eSEric Dumazet 		checksum_valid = !udp_lib_checksum_complete(skb);
482197c949eSEric Dumazet 		if (!checksum_valid)
483db8dac20SDavid S. Miller 			goto csum_copy_err;
484db8dac20SDavid S. Miller 	}
485db8dac20SDavid S. Miller 
486197c949eSEric Dumazet 	if (checksum_valid || skb_csum_unnecessary(skb))
48751f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, sizeof(struct udphdr),
48851f3d02bSDavid S. Miller 					    msg, copied);
489db8dac20SDavid S. Miller 	else {
490227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr), msg);
491db8dac20SDavid S. Miller 		if (err == -EINVAL)
492db8dac20SDavid S. Miller 			goto csum_copy_err;
493db8dac20SDavid S. Miller 	}
49422911fc5SEric Dumazet 	if (unlikely(err)) {
49522911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
496979402b1SEric Dumazet 		if (!peeked) {
497979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
498979402b1SEric Dumazet 			if (is_udp4)
499979402b1SEric Dumazet 				UDP_INC_STATS_USER(sock_net(sk),
500979402b1SEric Dumazet 						   UDP_MIB_INERRORS,
501979402b1SEric Dumazet 						   is_udplite);
502979402b1SEric Dumazet 			else
503979402b1SEric Dumazet 				UDP6_INC_STATS_USER(sock_net(sk),
504979402b1SEric Dumazet 						    UDP_MIB_INERRORS,
505979402b1SEric Dumazet 						    is_udplite);
506979402b1SEric Dumazet 		}
507db8dac20SDavid S. Miller 		goto out_free;
50822911fc5SEric Dumazet 	}
509f26ba175SWei Yongjun 	if (!peeked) {
510f26ba175SWei Yongjun 		if (is_udp4)
511f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
512f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
513f26ba175SWei Yongjun 		else
514235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
515235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
516f26ba175SWei Yongjun 	}
517db8dac20SDavid S. Miller 
5183b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
519db8dac20SDavid S. Miller 
520db8dac20SDavid S. Miller 	/* Copy the address. */
521db8dac20SDavid S. Miller 	if (msg->msg_name) {
522342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
523db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
524db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
525db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
526db8dac20SDavid S. Miller 
527842df073SHannes Frederic Sowa 		if (is_udp4) {
528b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
529b301e82cSBrian Haley 					       &sin6->sin6_addr);
530842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
531842df073SHannes Frederic Sowa 		} else {
5324e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
533842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
534842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
5354330487aSDuan Jiong 						    inet6_iif(skb));
536db8dac20SDavid S. Miller 		}
537bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
538db8dac20SDavid S. Miller 	}
5394b261c75SHannes Frederic Sowa 
5404b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
5414b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
5424b261c75SHannes Frederic Sowa 
543f26ba175SWei Yongjun 	if (is_udp4) {
544db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
545db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
546db8dac20SDavid S. Miller 	} else {
547db8dac20SDavid S. Miller 		if (np->rxopt.all)
5484b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
549db8dac20SDavid S. Miller 	}
550db8dac20SDavid S. Miller 
55159c2cdaeSDavid S. Miller 	err = copied;
552db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
553db8dac20SDavid S. Miller 		err = ulen;
554db8dac20SDavid S. Miller 
555db8dac20SDavid S. Miller out_free:
5569d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
557db8dac20SDavid S. Miller out:
558db8dac20SDavid S. Miller 	return err;
559db8dac20SDavid S. Miller 
560db8dac20SDavid S. Miller csum_copy_err:
5618a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
5620856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
5636a5dc9e5SEric Dumazet 		if (is_udp4) {
5646a5dc9e5SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
5656a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
5660856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
5670856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
5686a5dc9e5SEric Dumazet 		} else {
5696a5dc9e5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
5706a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
5710856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
5720856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
5730856f939SWei Yongjun 		}
5746a5dc9e5SEric Dumazet 	}
5758a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
576db8dac20SDavid S. Miller 
577beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
578beb39db5SEric Dumazet 	cond_resched();
5799cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
580db8dac20SDavid S. Miller 	goto try_again;
581db8dac20SDavid S. Miller }
582db8dac20SDavid S. Miller 
583db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
584d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
585645ca708SEric Dumazet 		    struct udp_table *udptable)
586db8dac20SDavid S. Miller {
587db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
588b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
589b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
590b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
591db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
592db8dac20SDavid S. Miller 	struct sock *sk;
593db8dac20SDavid S. Miller 	int err;
5947304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
595db8dac20SDavid S. Miller 
596e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
597538950a1SCraig Gallek 			       inet6_iif(skb), udptable, skb);
59863159f29SIan Morris 	if (!sk) {
5997304fe46SDuan Jiong 		ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
6007304fe46SDuan Jiong 				   ICMP6_MIB_INERRORS);
601db8dac20SDavid S. Miller 		return;
6027304fe46SDuan Jiong 	}
603db8dac20SDavid S. Miller 
60493b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
60593b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
60693b36cf3SHannes Frederic Sowa 			goto out;
60781aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
60893b36cf3SHannes Frederic Sowa 	}
6091a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
610ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
6111a462d18SDuan Jiong 		goto out;
6121a462d18SDuan Jiong 	}
61381aded24SDavid S. Miller 
614db8dac20SDavid S. Miller 	np = inet6_sk(sk);
615db8dac20SDavid S. Miller 
616db8dac20SDavid S. Miller 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
617db8dac20SDavid S. Miller 		goto out;
618db8dac20SDavid S. Miller 
619db8dac20SDavid S. Miller 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
620db8dac20SDavid S. Miller 		goto out;
621db8dac20SDavid S. Miller 
622b1faf566SEric Dumazet 	if (np->recverr)
623db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
624b1faf566SEric Dumazet 
625db8dac20SDavid S. Miller 	sk->sk_err = err;
626db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
627db8dac20SDavid S. Miller out:
628db8dac20SDavid S. Miller 	sock_put(sk);
629db8dac20SDavid S. Miller }
630db8dac20SDavid S. Miller 
631f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
632f7ad74feSBenjamin LaHaise {
633f7ad74feSBenjamin LaHaise 	int rc;
634f7ad74feSBenjamin LaHaise 
635efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
636f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
637005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
6382c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
639005ec974SShawn Bohrer 	}
640f7ad74feSBenjamin LaHaise 
641f7ad74feSBenjamin LaHaise 	rc = sock_queue_rcv_skb(sk, skb);
642f7ad74feSBenjamin LaHaise 	if (rc < 0) {
643f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
644f7ad74feSBenjamin LaHaise 
645f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
646f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
647f7ad74feSBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk),
648f7ad74feSBenjamin LaHaise 					UDP_MIB_RCVBUFERRORS, is_udplite);
649f7ad74feSBenjamin LaHaise 		UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
650f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
651f7ad74feSBenjamin LaHaise 		return -1;
652f7ad74feSBenjamin LaHaise 	}
653f7ad74feSBenjamin LaHaise 	return 0;
654f7ad74feSBenjamin LaHaise }
655f7ad74feSBenjamin LaHaise 
656db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
657d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
658d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info)
659db8dac20SDavid S. Miller {
660645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
661db8dac20SDavid S. Miller }
662db8dac20SDavid S. Miller 
663d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
664d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
665d7f3f621SBenjamin LaHaise {
666d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
667d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
668d7f3f621SBenjamin LaHaise }
669d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
670d7f3f621SBenjamin LaHaise 
671db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
672db8dac20SDavid S. Miller {
673db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
674db8dac20SDavid S. Miller 	int rc;
675db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
676db8dac20SDavid S. Miller 
677db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
678db8dac20SDavid S. Miller 		goto drop;
679db8dac20SDavid S. Miller 
680d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
681d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
682d7f3f621SBenjamin LaHaise 
683d7f3f621SBenjamin LaHaise 		/*
684d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
685d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
686d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
687d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
688d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
689d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
690d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
691d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
692d7f3f621SBenjamin LaHaise 		 */
693d7f3f621SBenjamin LaHaise 
694d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
695d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
69653b24b8fSIan Morris 		if (skb->len > sizeof(struct udphdr) && encap_rcv) {
697d7f3f621SBenjamin LaHaise 			int ret;
698d7f3f621SBenjamin LaHaise 
6990a80966bSTom Herbert 			/* Verify checksum before giving to encap */
7000a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
7010a80966bSTom Herbert 				goto csum_error;
7020a80966bSTom Herbert 
703d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
704d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
705d7f3f621SBenjamin LaHaise 				UDP_INC_STATS_BH(sock_net(sk),
706d7f3f621SBenjamin LaHaise 						 UDP_MIB_INDATAGRAMS,
707d7f3f621SBenjamin LaHaise 						 is_udplite);
708d7f3f621SBenjamin LaHaise 				return -ret;
709d7f3f621SBenjamin LaHaise 			}
710d7f3f621SBenjamin LaHaise 		}
711d7f3f621SBenjamin LaHaise 
712d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
713d7f3f621SBenjamin LaHaise 	}
714d7f3f621SBenjamin LaHaise 
715db8dac20SDavid S. Miller 	/*
716db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
717db8dac20SDavid S. Miller 	 */
718db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
719db8dac20SDavid S. Miller 
720db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
721ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
722db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
723db8dac20SDavid S. Miller 			goto drop;
724db8dac20SDavid S. Miller 		}
725db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
726ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
727db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
728db8dac20SDavid S. Miller 			goto drop;
729db8dac20SDavid S. Miller 		}
730db8dac20SDavid S. Miller 	}
731db8dac20SDavid S. Miller 
73233d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
733db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
7346a5dc9e5SEric Dumazet 			goto csum_error;
735db8dac20SDavid S. Miller 	}
736db8dac20SDavid S. Miller 
737274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
7383e215c8dSJames M Leddy 		UDP6_INC_STATS_BH(sock_net(sk),
7393e215c8dSJames M Leddy 				  UDP_MIB_RCVBUFERRORS, is_udplite);
740cb80ef46SBenjamin LaHaise 		goto drop;
7413e215c8dSJames M Leddy 	}
742cb80ef46SBenjamin LaHaise 
743d826eb14SEric Dumazet 	skb_dst_drop(skb);
744db8dac20SDavid S. Miller 
745cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
746cb80ef46SBenjamin LaHaise 	rc = 0;
747cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
748f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
749cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
750cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
751cb80ef46SBenjamin LaHaise 		goto drop;
752cb80ef46SBenjamin LaHaise 	}
753cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
754f7ad74feSBenjamin LaHaise 
755f7ad74feSBenjamin LaHaise 	return rc;
7563e215c8dSJames M Leddy 
7576a5dc9e5SEric Dumazet csum_error:
7586a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
759db8dac20SDavid S. Miller drop:
760ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
761cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
762db8dac20SDavid S. Miller 	kfree_skb(skb);
763db8dac20SDavid S. Miller 	return -1;
764db8dac20SDavid S. Miller }
765db8dac20SDavid S. Miller 
7665cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
767b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
768b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
7695cf3d461SDavid Held 				   int dif, unsigned short hnum)
770db8dac20SDavid S. Miller {
7719e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
772db8dac20SDavid S. Miller 
7739e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
7745cf3d461SDavid Held 		return false;
775b8ad0cbcSDaniel Lezcano 
7765cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
7775cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
7785cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
7795cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
7805cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
78133b4b015SHenning Rogge 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
78233b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
78333b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
7845cf3d461SDavid Held 		return false;
7859e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
7865cf3d461SDavid Held 		return false;
7875cf3d461SDavid Held 	return true;
788db8dac20SDavid S. Miller }
789db8dac20SDavid S. Miller 
790a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
791a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
792a1ab77f9SEric Dumazet {
793cb80ef46SBenjamin LaHaise 	struct sk_buff *skb1 = NULL;
794a1ab77f9SEric Dumazet 	struct sock *sk;
795cb80ef46SBenjamin LaHaise 	unsigned int i;
796a1ab77f9SEric Dumazet 
797a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
798a1ab77f9SEric Dumazet 		sk = stack[i];
79963159f29SIan Morris 		if (likely(!skb1))
800cb80ef46SBenjamin LaHaise 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
801cb80ef46SBenjamin LaHaise 		if (!skb1) {
802f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
803cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
804cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
805cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
806cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
807a1ab77f9SEric Dumazet 		}
808cb80ef46SBenjamin LaHaise 
809cb80ef46SBenjamin LaHaise 		if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
810cb80ef46SBenjamin LaHaise 			skb1 = NULL;
8112dc41cffSDavid Held 		sock_put(sk);
812cb80ef46SBenjamin LaHaise 	}
813cb80ef46SBenjamin LaHaise 	if (unlikely(skb1))
814cb80ef46SBenjamin LaHaise 		kfree_skb(skb1);
815a1ab77f9SEric Dumazet }
8164068579eSTom Herbert 
8174068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
8184068579eSTom Herbert {
8194068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
8204068579eSTom Herbert 	 * this error. Well, it is reasonable.
8214068579eSTom Herbert 	 */
822ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
8234068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
8244068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
8254068579eSTom Herbert }
8264068579eSTom Herbert 
827db8dac20SDavid S. Miller /*
828db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
829db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
830db8dac20SDavid S. Miller  */
831e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
832b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
83336cbb245SRick Jones 		struct udp_table *udptable, int proto)
834db8dac20SDavid S. Miller {
835a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
836db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
8375cf3d461SDavid Held 	struct hlist_nulls_node *node;
8385cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
8395cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
8405cf3d461SDavid Held 	int dif = inet6_iif(skb);
8412dc41cffSDavid Held 	unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
8422dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
84336cbb245SRick Jones 	bool inner_flushed = false;
8442dc41cffSDavid Held 
8452dc41cffSDavid Held 	if (use_hash2) {
8462dc41cffSDavid Held 		hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
8472dc41cffSDavid Held 			    udp_table.mask;
8482dc41cffSDavid Held 		hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
8492dc41cffSDavid Held start_lookup:
8502dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
8512dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
8522dc41cffSDavid Held 	}
853db8dac20SDavid S. Miller 
854645ca708SEric Dumazet 	spin_lock(&hslot->lock);
8552dc41cffSDavid Held 	sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
8565cf3d461SDavid Held 		if (__udp_v6_is_mcast_sock(net, sk,
8575cf3d461SDavid Held 					   uh->dest, daddr,
8585cf3d461SDavid Held 					   uh->source, saddr,
8595cf3d461SDavid Held 					   dif, hnum) &&
8601c19448cSTom Herbert 		    /* If zero checksum and no_check is not on for
8614068579eSTom Herbert 		     * the socket then skip it.
8624068579eSTom Herbert 		     */
8635cf3d461SDavid Held 		    (uh->check || udp_sk(sk)->no_check6_rx)) {
864a1ab77f9SEric Dumazet 			if (unlikely(count == ARRAY_SIZE(stack))) {
865a1ab77f9SEric Dumazet 				flush_stack(stack, count, skb, ~0);
86636cbb245SRick Jones 				inner_flushed = true;
867a1ab77f9SEric Dumazet 				count = 0;
868db8dac20SDavid S. Miller 			}
8695cf3d461SDavid Held 			stack[count++] = sk;
8702dc41cffSDavid Held 			sock_hold(sk);
8715cf3d461SDavid Held 		}
872a1ab77f9SEric Dumazet 	}
873db8dac20SDavid S. Miller 
874645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
875a1ab77f9SEric Dumazet 
8762dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
8772dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
8782dc41cffSDavid Held 		hash2 = hash2_any;
8792dc41cffSDavid Held 		goto start_lookup;
8802dc41cffSDavid Held 	}
8812dc41cffSDavid Held 
882a1ab77f9SEric Dumazet 	if (count) {
883a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
884a1ab77f9SEric Dumazet 	} else {
88536cbb245SRick Jones 		if (!inner_flushed)
88636cbb245SRick Jones 			UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI,
88736cbb245SRick Jones 					 proto == IPPROTO_UDPLITE);
88836cbb245SRick Jones 		consume_skb(skb);
889a1ab77f9SEric Dumazet 	}
890db8dac20SDavid S. Miller 	return 0;
891db8dac20SDavid S. Miller }
892db8dac20SDavid S. Miller 
893645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
894db8dac20SDavid S. Miller 		   int proto)
895db8dac20SDavid S. Miller {
8963ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
897db8dac20SDavid S. Miller 	struct sock *sk;
898db8dac20SDavid S. Miller 	struct udphdr *uh;
899b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
900db8dac20SDavid S. Miller 	u32 ulen = 0;
901db8dac20SDavid S. Miller 
902db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
903d6bc0149SBjørn Mork 		goto discard;
904db8dac20SDavid S. Miller 
905db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
906db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
907db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
908db8dac20SDavid S. Miller 
909db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
910db8dac20SDavid S. Miller 	if (ulen > skb->len)
911db8dac20SDavid S. Miller 		goto short_packet;
912db8dac20SDavid S. Miller 
913db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
914db8dac20SDavid S. Miller 		/* UDP validates ulen. */
915db8dac20SDavid S. Miller 
916db8dac20SDavid S. Miller 		/* Check for jumbo payload */
917db8dac20SDavid S. Miller 		if (ulen == 0)
918db8dac20SDavid S. Miller 			ulen = skb->len;
919db8dac20SDavid S. Miller 
920db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
921db8dac20SDavid S. Miller 			goto short_packet;
922db8dac20SDavid S. Miller 
923db8dac20SDavid S. Miller 		if (ulen < skb->len) {
924db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
925db8dac20SDavid S. Miller 				goto short_packet;
926db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
927db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
928db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
929db8dac20SDavid S. Miller 		}
930db8dac20SDavid S. Miller 	}
931db8dac20SDavid S. Miller 
932db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
9336a5dc9e5SEric Dumazet 		goto csum_error;
934db8dac20SDavid S. Miller 
935db8dac20SDavid S. Miller 	/*
936db8dac20SDavid S. Miller 	 *	Multicast receive code
937db8dac20SDavid S. Miller 	 */
938db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
939e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
94036cbb245SRick Jones 				saddr, daddr, udptable, proto);
941db8dac20SDavid S. Miller 
942db8dac20SDavid S. Miller 	/* Unicast */
943db8dac20SDavid S. Miller 
944db8dac20SDavid S. Miller 	/*
945db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
946db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
947db8dac20SDavid S. Miller 	 */
948607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
94953b24b8fSIan Morris 	if (sk) {
950a5b50476SEliezer Tamir 		int ret;
951a5b50476SEliezer Tamir 
9521c19448cSTom Herbert 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
95379e0f1c9STom Herbert 			sock_put(sk);
9544068579eSTom Herbert 			udp6_csum_zero_error(skb);
9554068579eSTom Herbert 			goto csum_error;
9564068579eSTom Herbert 		}
9574068579eSTom Herbert 
958224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
9592abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
9602abb7cdcSTom Herbert 						 ip6_compute_pseudo);
9612abb7cdcSTom Herbert 
962a5b50476SEliezer Tamir 		ret = udpv6_queue_rcv_skb(sk, skb);
963cb80ef46SBenjamin LaHaise 		sock_put(sk);
964db8dac20SDavid S. Miller 
96559dca1d8SBill Sommerfeld 		/* a return value > 0 means to resubmit the input */
966cb80ef46SBenjamin LaHaise 		if (ret > 0)
96759dca1d8SBill Sommerfeld 			return ret;
968cb80ef46SBenjamin LaHaise 
969cb80ef46SBenjamin LaHaise 		return 0;
970cb80ef46SBenjamin LaHaise 	}
971cb80ef46SBenjamin LaHaise 
9724068579eSTom Herbert 	if (!uh->check) {
9734068579eSTom Herbert 		udp6_csum_zero_error(skb);
9744068579eSTom Herbert 		goto csum_error;
9754068579eSTom Herbert 	}
9764068579eSTom Herbert 
977db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
978db8dac20SDavid S. Miller 		goto discard;
979db8dac20SDavid S. Miller 
980db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
9816a5dc9e5SEric Dumazet 		goto csum_error;
982db8dac20SDavid S. Miller 
983cb80ef46SBenjamin LaHaise 	UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
9843ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
985db8dac20SDavid S. Miller 
986db8dac20SDavid S. Miller 	kfree_skb(skb);
987db8dac20SDavid S. Miller 	return 0;
988db8dac20SDavid S. Miller 
989db8dac20SDavid S. Miller short_packet:
990ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
991db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
992ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
993ba7a46f1SJoe Perches 			    ulen, skb->len,
994ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
9956a5dc9e5SEric Dumazet 	goto discard;
9966a5dc9e5SEric Dumazet csum_error:
9976a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
998db8dac20SDavid S. Miller discard:
999ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1000db8dac20SDavid S. Miller 	kfree_skb(skb);
1001db8dac20SDavid S. Miller 	return 0;
1002db8dac20SDavid S. Miller }
1003db8dac20SDavid S. Miller 
1004db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
1005db8dac20SDavid S. Miller {
1006645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1007db8dac20SDavid S. Miller }
1008db8dac20SDavid S. Miller 
1009db8dac20SDavid S. Miller /*
1010db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
1011db8dac20SDavid S. Miller  */
1012db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
1013db8dac20SDavid S. Miller {
1014db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1015db8dac20SDavid S. Miller 
101636d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
101736d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
101836d926b9SDenis V. Lunev 	else if (up->pending) {
1019db8dac20SDavid S. Miller 		up->len = 0;
1020db8dac20SDavid S. Miller 		up->pending = 0;
1021db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
1022db8dac20SDavid S. Miller 	}
1023db8dac20SDavid S. Miller }
1024db8dac20SDavid S. Miller 
1025493c6be3SSridhar Samudrala /**
1026493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1027493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
1028493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
1029493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
1030493c6be3SSridhar Samudrala  */
1031493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1032493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
1033493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
1034493c6be3SSridhar Samudrala {
1035493c6be3SSridhar Samudrala 	unsigned int offset;
1036493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
1037d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1038493c6be3SSridhar Samudrala 	__wsum csum = 0;
1039493c6be3SSridhar Samudrala 
1040d39d938cSVlad Yasevich 	if (!frags) {
1041493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
1042493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
1043493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
1044493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1045493c6be3SSridhar Samudrala 	} else {
1046493c6be3SSridhar Samudrala 		/*
1047493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
1048493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
1049493c6be3SSridhar Samudrala 		 * should be together
1050493c6be3SSridhar Samudrala 		 */
1051493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
1052493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1053493c6be3SSridhar Samudrala 
1054493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
1055493c6be3SSridhar Samudrala 
1056d39d938cSVlad Yasevich 		do {
1057d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
1058d39d938cSVlad Yasevich 		} while ((frags = frags->next));
1059493c6be3SSridhar Samudrala 
1060493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1061493c6be3SSridhar Samudrala 					    csum);
1062493c6be3SSridhar Samudrala 		if (uh->check == 0)
1063493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
1064493c6be3SSridhar Samudrala 	}
1065493c6be3SSridhar Samudrala }
1066493c6be3SSridhar Samudrala 
1067db8dac20SDavid S. Miller /*
1068db8dac20SDavid S. Miller  *	Sending
1069db8dac20SDavid S. Miller  */
1070db8dac20SDavid S. Miller 
1071d39d938cSVlad Yasevich static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
1072db8dac20SDavid S. Miller {
1073d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
1074db8dac20SDavid S. Miller 	struct udphdr *uh;
1075db8dac20SDavid S. Miller 	int err = 0;
1076db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1077db8dac20SDavid S. Miller 	__wsum csum = 0;
1078d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
1079d39d938cSVlad Yasevich 	int len = skb->len - offset;
1080db8dac20SDavid S. Miller 
1081db8dac20SDavid S. Miller 	/*
1082db8dac20SDavid S. Miller 	 * Create a UDP header
1083db8dac20SDavid S. Miller 	 */
1084db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
10851958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
10861958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
1087d39d938cSVlad Yasevich 	uh->len = htons(len);
1088db8dac20SDavid S. Miller 	uh->check = 0;
1089db8dac20SDavid S. Miller 
1090db8dac20SDavid S. Miller 	if (is_udplite)
1091d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
1092d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
10934068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
10944068579eSTom Herbert 		goto send;
10954068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1096d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1097493c6be3SSridhar Samudrala 		goto send;
1098493c6be3SSridhar Samudrala 	} else
1099d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1100db8dac20SDavid S. Miller 
1101db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
11024c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1103d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1104db8dac20SDavid S. Miller 	if (uh->check == 0)
1105db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1106db8dac20SDavid S. Miller 
1107493c6be3SSridhar Samudrala send:
1108d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
11096ce9e7b5SEric Dumazet 	if (err) {
11106ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
11116ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
11126ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
11136ce9e7b5SEric Dumazet 			err = 0;
11146ce9e7b5SEric Dumazet 		}
11156ce9e7b5SEric Dumazet 	} else
11166ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
11176ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
1118d39d938cSVlad Yasevich 	return err;
1119d39d938cSVlad Yasevich }
1120d39d938cSVlad Yasevich 
1121d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1122d39d938cSVlad Yasevich {
1123d39d938cSVlad Yasevich 	struct sk_buff *skb;
1124d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1125d39d938cSVlad Yasevich 	struct flowi6 fl6;
1126d39d938cSVlad Yasevich 	int err = 0;
1127d39d938cSVlad Yasevich 
1128d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1129d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1130d39d938cSVlad Yasevich 
1131d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
1132d39d938cSVlad Yasevich 	 * fl6 here.
1133d39d938cSVlad Yasevich 	 */
1134d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1135d39d938cSVlad Yasevich 
1136d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1137d39d938cSVlad Yasevich 	if (!skb)
1138d39d938cSVlad Yasevich 		goto out;
1139d39d938cSVlad Yasevich 
1140d39d938cSVlad Yasevich 	err = udp_v6_send_skb(skb, &fl6);
1141d39d938cSVlad Yasevich 
1142db8dac20SDavid S. Miller out:
1143db8dac20SDavid S. Miller 	up->len = 0;
1144db8dac20SDavid S. Miller 	up->pending = 0;
1145db8dac20SDavid S. Miller 	return err;
1146db8dac20SDavid S. Miller }
1147db8dac20SDavid S. Miller 
11481b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1149db8dac20SDavid S. Miller {
1150db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1151db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1152db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1153db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1154342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
115520c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1156db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
115745f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1158db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
11594c9483b2SDavid S. Miller 	struct flowi6 fl6;
1160db8dac20SDavid S. Miller 	struct dst_entry *dst;
1161db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1162db8dac20SDavid S. Miller 	int ulen = len;
1163db8dac20SDavid S. Miller 	int hlimit = -1;
1164db8dac20SDavid S. Miller 	int tclass = -1;
116513b52cd4SBrian Haley 	int dontfrag = -1;
1166db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1167db8dac20SDavid S. Miller 	int err;
1168db8dac20SDavid S. Miller 	int connected = 0;
1169db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1170db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1171db8dac20SDavid S. Miller 
1172db8dac20SDavid S. Miller 	/* destination address check */
1173db8dac20SDavid S. Miller 	if (sin6) {
1174db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1175db8dac20SDavid S. Miller 			return -EINVAL;
1176db8dac20SDavid S. Miller 
1177db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1178db8dac20SDavid S. Miller 		case AF_INET6:
1179db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1180db8dac20SDavid S. Miller 				return -EINVAL;
1181db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1182db8dac20SDavid S. Miller 			break;
1183db8dac20SDavid S. Miller 		case AF_INET:
1184db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1185db8dac20SDavid S. Miller 		case AF_UNSPEC:
1186db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1187db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1188db8dac20SDavid S. Miller 			daddr = NULL;
1189db8dac20SDavid S. Miller 			break;
1190db8dac20SDavid S. Miller 		default:
1191db8dac20SDavid S. Miller 			return -EINVAL;
1192db8dac20SDavid S. Miller 		}
1193db8dac20SDavid S. Miller 	} else if (!up->pending) {
1194db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1195db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1196efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1197db8dac20SDavid S. Miller 	} else
1198db8dac20SDavid S. Miller 		daddr = NULL;
1199db8dac20SDavid S. Miller 
1200db8dac20SDavid S. Miller 	if (daddr) {
1201db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1202db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1203db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1204c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1205db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1206db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1207db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1208db8dac20SDavid S. Miller do_udp_sendmsg:
1209db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1210db8dac20SDavid S. Miller 				return -ENETUNREACH;
12111b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1212db8dac20SDavid S. Miller 		}
1213db8dac20SDavid S. Miller 	}
1214db8dac20SDavid S. Miller 
1215db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
12161b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1217db8dac20SDavid S. Miller 
1218db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1219db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1220db8dac20SDavid S. Miller 	   */
1221db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1222db8dac20SDavid S. Miller 		return -EMSGSIZE;
1223db8dac20SDavid S. Miller 
122403485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1225db8dac20SDavid S. Miller 	if (up->pending) {
1226db8dac20SDavid S. Miller 		/*
1227db8dac20SDavid S. Miller 		 * There are pending frames.
1228db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1229db8dac20SDavid S. Miller 		 */
1230db8dac20SDavid S. Miller 		lock_sock(sk);
1231db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1232db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1233db8dac20SDavid S. Miller 				release_sock(sk);
1234db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1235db8dac20SDavid S. Miller 			}
1236db8dac20SDavid S. Miller 			dst = NULL;
1237db8dac20SDavid S. Miller 			goto do_append_data;
1238db8dac20SDavid S. Miller 		}
1239db8dac20SDavid S. Miller 		release_sock(sk);
1240db8dac20SDavid S. Miller 	}
1241db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1242db8dac20SDavid S. Miller 
12434c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1244db8dac20SDavid S. Miller 
1245db8dac20SDavid S. Miller 	if (sin6) {
1246db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1247db8dac20SDavid S. Miller 			return -EINVAL;
1248db8dac20SDavid S. Miller 
12491958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1250db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1251db8dac20SDavid S. Miller 
1252db8dac20SDavid S. Miller 		if (np->sndflow) {
12534c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
12544c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
12554c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
125663159f29SIan Morris 				if (!flowlabel)
1257db8dac20SDavid S. Miller 					return -EINVAL;
1258db8dac20SDavid S. Miller 			}
1259db8dac20SDavid S. Miller 		}
1260db8dac20SDavid S. Miller 
1261db8dac20SDavid S. Miller 		/*
1262db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1263db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1264db8dac20SDavid S. Miller 		 */
1265db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1266efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1267efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1268db8dac20SDavid S. Miller 
1269db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1270db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1271842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
12724c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1273db8dac20SDavid S. Miller 	} else {
1274db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1275db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1276db8dac20SDavid S. Miller 
12771958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1278efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
12794c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1280db8dac20SDavid S. Miller 		connected = 1;
1281db8dac20SDavid S. Miller 	}
1282db8dac20SDavid S. Miller 
12834c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
12844c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1285db8dac20SDavid S. Miller 
12864c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
12874c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
12889f690db7SYang Hongyang 
12894c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
129051953d5bSBrian Haley 
1291db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1292db8dac20SDavid S. Miller 		opt = &opt_space;
1293db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1294db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1295db8dac20SDavid S. Miller 
129673df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1297ec0506dbSMaciej Żenczykowski 					    &hlimit, &tclass, &dontfrag);
1298db8dac20SDavid S. Miller 		if (err < 0) {
1299db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1300db8dac20SDavid S. Miller 			return err;
1301db8dac20SDavid S. Miller 		}
13024c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
13034c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
130463159f29SIan Morris 			if (!flowlabel)
1305db8dac20SDavid S. Miller 				return -EINVAL;
1306db8dac20SDavid S. Miller 		}
1307db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1308db8dac20SDavid S. Miller 			opt = NULL;
1309db8dac20SDavid S. Miller 		connected = 0;
1310db8dac20SDavid S. Miller 	}
131145f6fad8SEric Dumazet 	if (!opt) {
131245f6fad8SEric Dumazet 		opt = txopt_get(np);
131345f6fad8SEric Dumazet 		opt_to_free = opt;
131445f6fad8SEric Dumazet 	}
1315db8dac20SDavid S. Miller 	if (flowlabel)
1316db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1317db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1318db8dac20SDavid S. Miller 
13194c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1320876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
13214e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1322876c7f41SBrian Haley 	else
13234c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
13244c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
13254e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
13261958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1327db8dac20SDavid S. Miller 
13284c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
132920c59de2SArnaud Ebalard 	if (final_p)
1330db8dac20SDavid S. Miller 		connected = 0;
1331db8dac20SDavid S. Miller 
13324c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
13334c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1334db8dac20SDavid S. Miller 		connected = 0;
1335c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1336c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1337db8dac20SDavid S. Miller 
13384c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1339db8dac20SDavid S. Miller 
13400e0d44abSSteffen Klassert 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
134168d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
134268d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
134368d0c6d3SDavid S. Miller 		dst = NULL;
1344db8dac20SDavid S. Miller 		goto out;
1345db8dac20SDavid S. Miller 	}
1346db8dac20SDavid S. Miller 
1347db8dac20SDavid S. Miller 	if (hlimit < 0)
13485c98631cSLorenzo Colitti 		hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1349db8dac20SDavid S. Miller 
1350db8dac20SDavid S. Miller 	if (tclass < 0)
1351e651f03aSGerrit Renker 		tclass = np->tclass;
1352db8dac20SDavid S. Miller 
1353db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1354db8dac20SDavid S. Miller 		goto do_confirm;
1355db8dac20SDavid S. Miller back_from_confirm:
1356db8dac20SDavid S. Miller 
135703485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
135803485f2aSVlad Yasevich 	if (!corkreq) {
135903485f2aSVlad Yasevich 		struct sk_buff *skb;
136003485f2aSVlad Yasevich 
136103485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
136203485f2aSVlad Yasevich 				   sizeof(struct udphdr), hlimit, tclass, opt,
136303485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
136403485f2aSVlad Yasevich 				   msg->msg_flags, dontfrag);
136503485f2aSVlad Yasevich 		err = PTR_ERR(skb);
136603485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
136703485f2aSVlad Yasevich 			err = udp_v6_send_skb(skb, &fl6);
136803485f2aSVlad Yasevich 		goto release_dst;
136903485f2aSVlad Yasevich 	}
137003485f2aSVlad Yasevich 
1371db8dac20SDavid S. Miller 	lock_sock(sk);
1372db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1373db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1374db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1375db8dac20SDavid S. Miller 		release_sock(sk);
1376db8dac20SDavid S. Miller 
1377ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1378db8dac20SDavid S. Miller 		err = -EINVAL;
1379db8dac20SDavid S. Miller 		goto out;
1380db8dac20SDavid S. Miller 	}
1381db8dac20SDavid S. Miller 
1382db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1383db8dac20SDavid S. Miller 
1384db8dac20SDavid S. Miller do_append_data:
1385e36d3ff9SJiri Pirko 	if (dontfrag < 0)
1386e36d3ff9SJiri Pirko 		dontfrag = np->dontfrag;
1387db8dac20SDavid S. Miller 	up->len += ulen;
1388f69e6d13SAl Viro 	err = ip6_append_data(sk, getfrag, msg, ulen,
13894c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1390db8dac20SDavid S. Miller 		(struct rt6_info *)dst,
139113b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1392db8dac20SDavid S. Miller 	if (err)
1393db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1394db8dac20SDavid S. Miller 	else if (!corkreq)
1395db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1396db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1397db8dac20SDavid S. Miller 		up->pending = 0;
1398db8dac20SDavid S. Miller 
139903485f2aSVlad Yasevich 	if (err > 0)
140003485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
140103485f2aSVlad Yasevich 	release_sock(sk);
140203485f2aSVlad Yasevich 
140303485f2aSVlad Yasevich release_dst:
1404db8dac20SDavid S. Miller 	if (dst) {
1405db8dac20SDavid S. Miller 		if (connected) {
1406db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
1407efe4208fSEric Dumazet 				      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1408efe4208fSEric Dumazet 				      &sk->sk_v6_daddr : NULL,
1409db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
14104c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1411db8dac20SDavid S. Miller 				      &np->saddr :
1412db8dac20SDavid S. Miller #endif
1413db8dac20SDavid S. Miller 				      NULL);
1414db8dac20SDavid S. Miller 		} else {
1415db8dac20SDavid S. Miller 			dst_release(dst);
1416db8dac20SDavid S. Miller 		}
1417a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1418db8dac20SDavid S. Miller 	}
1419db8dac20SDavid S. Miller 
1420db8dac20SDavid S. Miller out:
1421a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1422db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
142345f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1424db8dac20SDavid S. Miller 	if (!err)
1425db8dac20SDavid S. Miller 		return len;
1426db8dac20SDavid S. Miller 	/*
1427db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1428db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1429db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1430db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1431db8dac20SDavid S. Miller 	 * seems like overkill.
1432db8dac20SDavid S. Miller 	 */
1433db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1434235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1435235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1436db8dac20SDavid S. Miller 	}
1437db8dac20SDavid S. Miller 	return err;
1438db8dac20SDavid S. Miller 
1439db8dac20SDavid S. Miller do_confirm:
1440db8dac20SDavid S. Miller 	dst_confirm(dst);
1441db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1442db8dac20SDavid S. Miller 		goto back_from_confirm;
1443db8dac20SDavid S. Miller 	err = 0;
1444db8dac20SDavid S. Miller 	goto out;
1445db8dac20SDavid S. Miller }
1446db8dac20SDavid S. Miller 
14477d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1448db8dac20SDavid S. Miller {
144944046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1450db8dac20SDavid S. Miller 	lock_sock(sk);
1451db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1452db8dac20SDavid S. Miller 	release_sock(sk);
1453db8dac20SDavid S. Miller 
145444046a59STom Parkin 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
145544046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
145644046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
145744046a59STom Parkin 		if (encap_destroy)
145844046a59STom Parkin 			encap_destroy(sk);
145944046a59STom Parkin 	}
146044046a59STom Parkin 
1461db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1462db8dac20SDavid S. Miller }
1463db8dac20SDavid S. Miller 
1464db8dac20SDavid S. Miller /*
1465db8dac20SDavid S. Miller  *	Socket option code for UDP
1466db8dac20SDavid S. Miller  */
1467db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1468b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1469db8dac20SDavid S. Miller {
1470db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1471db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1472db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1473db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1474db8dac20SDavid S. Miller }
1475db8dac20SDavid S. Miller 
1476db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1477db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1478b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1479db8dac20SDavid S. Miller {
1480db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1481db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1482db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1483db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1484db8dac20SDavid S. Miller }
1485db8dac20SDavid S. Miller #endif
1486db8dac20SDavid S. Miller 
1487db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1488db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1489db8dac20SDavid S. Miller {
1490db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1491db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1492db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1493db8dac20SDavid S. Miller }
1494db8dac20SDavid S. Miller 
1495db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1496db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1497db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1498db8dac20SDavid S. Miller {
1499db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1500db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1501db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1502db8dac20SDavid S. Miller }
1503db8dac20SDavid S. Miller #endif
1504db8dac20SDavid S. Miller 
150541135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1506db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1507db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1508db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1509db8dac20SDavid S. Miller };
1510db8dac20SDavid S. Miller 
1511db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1512db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1513db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1514db8dac20SDavid S. Miller {
151517ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
151617ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
151717ef66afSLorenzo Colitti 	} else {
151817ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
151917ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
152017ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
152117ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
152217ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
152317ef66afSLorenzo Colitti 	}
1524db8dac20SDavid S. Miller 	return 0;
1525db8dac20SDavid S. Miller }
1526db8dac20SDavid S. Miller 
152773cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
152873cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
152973cb88ecSArjan van de Ven 	.open     = udp_seq_open,
153073cb88ecSArjan van de Ven 	.read     = seq_read,
153173cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
153273cb88ecSArjan van de Ven 	.release  = seq_release_net
153373cb88ecSArjan van de Ven };
153473cb88ecSArjan van de Ven 
1535db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1536db8dac20SDavid S. Miller 	.name		= "udp6",
1537db8dac20SDavid S. Miller 	.family		= AF_INET6,
1538645ca708SEric Dumazet 	.udp_table	= &udp_table,
153973cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1540dda61925SDenis V. Lunev 	.seq_ops	= {
1541dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1542dda61925SDenis V. Lunev 	},
1543db8dac20SDavid S. Miller };
1544db8dac20SDavid S. Miller 
15452c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1546db8dac20SDavid S. Miller {
15470c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1548db8dac20SDavid S. Miller }
1549db8dac20SDavid S. Miller 
1550ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1551ec120da6SIan Morris {
15520c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1553db8dac20SDavid S. Miller }
1554db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1555db8dac20SDavid S. Miller 
1556f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size)
1557f77d6021SEric Dumazet {
1558f77d6021SEric Dumazet 	struct inet_sock *inet = inet_sk(sk);
1559f77d6021SEric Dumazet 
1560f77d6021SEric Dumazet 	/* we do not want to clear pinet6 field, because of RCU lookups */
1561f77d6021SEric Dumazet 	sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1562f77d6021SEric Dumazet 
1563f77d6021SEric Dumazet 	size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1564f77d6021SEric Dumazet 	memset(&inet->pinet6 + 1, 0, size);
1565f77d6021SEric Dumazet }
1566f77d6021SEric Dumazet 
1567db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1568db8dac20SDavid S. Miller 
1569db8dac20SDavid S. Miller struct proto udpv6_prot = {
1570db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1571db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1572db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1573db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1574db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1575db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1576db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1577db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1578db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1579db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1580db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1581f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1582db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1583db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1584719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1585db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1586db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1587db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1588db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1589db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1590db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1591271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1592645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1593db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1594db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1595db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1596db8dac20SDavid S. Miller #endif
1597f77d6021SEric Dumazet 	.clear_sk	   = udp_v6_clear_sk,
1598db8dac20SDavid S. Miller };
1599db8dac20SDavid S. Miller 
1600db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1601db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1602db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1603db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1604db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1605db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1606db8dac20SDavid S. Miller };
1607db8dac20SDavid S. Miller 
1608db8dac20SDavid S. Miller int __init udpv6_init(void)
1609db8dac20SDavid S. Miller {
1610db8dac20SDavid S. Miller 	int ret;
1611db8dac20SDavid S. Miller 
16123336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
16133336288aSVlad Yasevich 	if (ret)
1614c6b641a4SVlad Yasevich 		goto out;
16153336288aSVlad Yasevich 
1616db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1617db8dac20SDavid S. Miller 	if (ret)
1618db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1619db8dac20SDavid S. Miller out:
1620db8dac20SDavid S. Miller 	return ret;
1621db8dac20SDavid S. Miller 
1622db8dac20SDavid S. Miller out_udpv6_protocol:
1623db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1624db8dac20SDavid S. Miller 	goto out;
1625db8dac20SDavid S. Miller }
1626db8dac20SDavid S. Miller 
1627db8dac20SDavid S. Miller void udpv6_exit(void)
1628db8dac20SDavid S. Miller {
1629db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1630db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1631db8dac20SDavid S. Miller }
1632