xref: /openbmc/linux/net/ipv6/udp.c (revision e0d8c1b7)
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 
40496611d7SCraig Gallek #include <net/addrconf.h>
41db8dac20SDavid S. Miller #include <net/ndisc.h>
42db8dac20SDavid S. Miller #include <net/protocol.h>
43db8dac20SDavid S. Miller #include <net/transp_v6.h>
44db8dac20SDavid S. Miller #include <net/ip6_route.h>
45db8dac20SDavid S. Miller #include <net/raw.h>
46db8dac20SDavid S. Miller #include <net/tcp_states.h>
47db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
48db8dac20SDavid S. Miller #include <net/xfrm.h>
4972289b96STom Herbert #include <net/inet6_hashtables.h>
50076bb0c8SEliezer Tamir #include <net/busy_poll.h>
51e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
52db8dac20SDavid S. Miller 
53db8dac20SDavid S. Miller #include <linux/proc_fs.h>
54db8dac20SDavid S. Miller #include <linux/seq_file.h>
5522911fc5SEric Dumazet #include <trace/events/skb.h>
56db8dac20SDavid S. Miller #include "udp_impl.h"
57db8dac20SDavid S. Miller 
586eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
59b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
60b50026b5SHannes Frederic Sowa 			const u16 lport,
61b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
62b50026b5SHannes Frederic Sowa 			const __be16 fport)
63b50026b5SHannes Frederic Sowa {
641bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
651bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
661bbdceefSHannes Frederic Sowa 
671bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
681bbdceefSHannes Frederic Sowa 
691bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
701bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
711bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
721bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
731bbdceefSHannes Frederic Sowa 
741bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
751bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
761bbdceefSHannes Frederic Sowa 
77b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
781bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
79b50026b5SHannes Frederic Sowa }
80b50026b5SHannes Frederic Sowa 
816eada011SEric Dumazet static u32 udp6_portaddr_hash(const struct net *net,
82d4cada4aSEric Dumazet 			      const struct in6_addr *addr6,
83d4cada4aSEric Dumazet 			      unsigned int port)
84d4cada4aSEric Dumazet {
85d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
86d4cada4aSEric Dumazet 
87d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
88d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
89856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
900eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
91d4cada4aSEric Dumazet 	else
920eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
93d4cada4aSEric Dumazet 
94d4cada4aSEric Dumazet 	return hash ^ port;
95d4cada4aSEric Dumazet }
96d4cada4aSEric Dumazet 
976ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
98db8dac20SDavid S. Miller {
9930fff923SEric Dumazet 	unsigned int hash2_nulladdr =
10030fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10130fff923SEric Dumazet 	unsigned int hash2_partial =
102efe4208fSEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
10330fff923SEric Dumazet 
104d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
10530fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
10630fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
107db8dac20SDavid S. Miller }
108db8dac20SDavid S. Miller 
109719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
110719f8358SEric Dumazet {
111719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
112efe4208fSEric Dumazet 					  &sk->sk_v6_rcv_saddr,
113719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
114719f8358SEric Dumazet 
115719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
116719f8358SEric Dumazet }
117719f8358SEric Dumazet 
118645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
119645ca708SEric Dumazet 				unsigned short hnum,
12088440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
12188440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
122645ca708SEric Dumazet 				int dif)
123db8dac20SDavid S. Miller {
12460c04aecSJoe Perches 	int score;
12560c04aecSJoe Perches 	struct inet_sock *inet;
126db8dac20SDavid S. Miller 
12760c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
12860c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
12960c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
13060c04aecSJoe Perches 		return -1;
131645ca708SEric Dumazet 
132645ca708SEric Dumazet 	score = 0;
13360c04aecSJoe Perches 	inet = inet_sk(sk);
13460c04aecSJoe Perches 
135c720c7e8SEric Dumazet 	if (inet->inet_dport) {
136c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
137645ca708SEric Dumazet 			return -1;
138db8dac20SDavid S. Miller 		score++;
139db8dac20SDavid S. Miller 	}
14060c04aecSJoe Perches 
141efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
142efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
143645ca708SEric Dumazet 			return -1;
144db8dac20SDavid S. Miller 		score++;
145db8dac20SDavid S. Miller 	}
14660c04aecSJoe Perches 
147efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
148efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
149645ca708SEric Dumazet 			return -1;
150db8dac20SDavid S. Miller 		score++;
151db8dac20SDavid S. Miller 	}
15260c04aecSJoe Perches 
153db8dac20SDavid S. Miller 	if (sk->sk_bound_dev_if) {
154db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if != dif)
155645ca708SEric Dumazet 			return -1;
156db8dac20SDavid S. Miller 		score++;
157db8dac20SDavid S. Miller 	}
15860c04aecSJoe Perches 
15970da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
16070da268bSEric Dumazet 		score++;
16170da268bSEric Dumazet 
162645ca708SEric Dumazet 	return score;
163645ca708SEric Dumazet }
164645ca708SEric Dumazet 
165fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
166fddc17deSEric Dumazet 				 const struct in6_addr *saddr, __be16 sport,
16760c04aecSJoe Perches 				 const struct in6_addr *daddr,
16860c04aecSJoe Perches 				 unsigned short hnum, int dif)
169fddc17deSEric Dumazet {
17060c04aecSJoe Perches 	int score;
17160c04aecSJoe Perches 	struct inet_sock *inet;
172fddc17deSEric Dumazet 
17360c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
17460c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
17560c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
17660c04aecSJoe Perches 		return -1;
177fddc17deSEric Dumazet 
178efe4208fSEric Dumazet 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
179fddc17deSEric Dumazet 		return -1;
18060c04aecSJoe Perches 
181fddc17deSEric Dumazet 	score = 0;
18260c04aecSJoe Perches 	inet = inet_sk(sk);
18360c04aecSJoe Perches 
184fddc17deSEric Dumazet 	if (inet->inet_dport) {
185fddc17deSEric Dumazet 		if (inet->inet_dport != sport)
186fddc17deSEric Dumazet 			return -1;
187fddc17deSEric Dumazet 		score++;
188fddc17deSEric Dumazet 	}
18960c04aecSJoe Perches 
190efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
191efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
192fddc17deSEric Dumazet 			return -1;
193fddc17deSEric Dumazet 		score++;
194fddc17deSEric Dumazet 	}
19560c04aecSJoe Perches 
196fddc17deSEric Dumazet 	if (sk->sk_bound_dev_if) {
197fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if != dif)
198fddc17deSEric Dumazet 			return -1;
199fddc17deSEric Dumazet 		score++;
200fddc17deSEric Dumazet 	}
20160c04aecSJoe Perches 
20270da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
20370da268bSEric Dumazet 		score++;
20470da268bSEric Dumazet 
205fddc17deSEric Dumazet 	return score;
206fddc17deSEric Dumazet }
207fddc17deSEric Dumazet 
208fddc17deSEric Dumazet /* called with read_rcu_lock() */
209fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
210fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
211fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
2121134158bSCraig Gallek 		struct udp_hslot *hslot2, unsigned int slot2,
2131134158bSCraig Gallek 		struct sk_buff *skb)
214fddc17deSEric Dumazet {
215fddc17deSEric Dumazet 	struct sock *sk, *result;
216fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
21772289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
218ed0dfffdSEric Dumazet 	bool select_ok = true;
21972289b96STom Herbert 	u32 hash = 0;
220fddc17deSEric Dumazet 
221fddc17deSEric Dumazet begin:
222fddc17deSEric Dumazet 	result = NULL;
223fddc17deSEric Dumazet 	badness = -1;
224fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
225fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
226fddc17deSEric Dumazet 				      daddr, hnum, dif);
227fddc17deSEric Dumazet 		if (score > badness) {
228fddc17deSEric Dumazet 			result = sk;
229fddc17deSEric Dumazet 			badness = score;
23072289b96STom Herbert 			reuseport = sk->sk_reuseport;
23172289b96STom Herbert 			if (reuseport) {
232b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
23372289b96STom Herbert 						    saddr, sport);
234ed0dfffdSEric Dumazet 				if (select_ok) {
235ed0dfffdSEric Dumazet 					struct sock *sk2;
236ed0dfffdSEric Dumazet 
2371134158bSCraig Gallek 					sk2 = reuseport_select_sock(sk, hash, skb,
2381134158bSCraig Gallek 							sizeof(struct udphdr));
239e32ea7e7SCraig Gallek 					if (sk2) {
240e32ea7e7SCraig Gallek 						result = sk2;
241ed0dfffdSEric Dumazet 						select_ok = false;
242e32ea7e7SCraig Gallek 						goto found;
243e32ea7e7SCraig Gallek 					}
244ed0dfffdSEric Dumazet 				}
24572289b96STom Herbert 				matches = 1;
24670da268bSEric Dumazet 			}
24772289b96STom Herbert 		} else if (score == badness && reuseport) {
24872289b96STom Herbert 			matches++;
2498fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
25072289b96STom Herbert 				result = sk;
25172289b96STom Herbert 			hash = next_pseudo_random32(hash);
252fddc17deSEric Dumazet 		}
253fddc17deSEric Dumazet 	}
254fddc17deSEric Dumazet 	/*
255fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
256fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
257fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
258fddc17deSEric Dumazet 	 */
259fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
260fddc17deSEric Dumazet 		goto begin;
261fddc17deSEric Dumazet 
262fddc17deSEric Dumazet 	if (result) {
263e32ea7e7SCraig Gallek found:
264c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
265fddc17deSEric Dumazet 			result = NULL;
266fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
267fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
268fddc17deSEric Dumazet 			sock_put(result);
269fddc17deSEric Dumazet 			goto begin;
270fddc17deSEric Dumazet 		}
271fddc17deSEric Dumazet 	}
272fddc17deSEric Dumazet 	return result;
273fddc17deSEric Dumazet }
274fddc17deSEric Dumazet 
275fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
27688440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
27788440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
278538950a1SCraig Gallek 				      int dif, struct udp_table *udptable,
279538950a1SCraig Gallek 				      struct sk_buff *skb)
280645ca708SEric Dumazet {
281271b72c7SEric Dumazet 	struct sock *sk, *result;
28288ab1932SEric Dumazet 	struct hlist_nulls_node *node;
283645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
284fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
285fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
28672289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
287ed0dfffdSEric Dumazet 	bool select_ok = true;
28872289b96STom Herbert 	u32 hash = 0;
289645ca708SEric Dumazet 
290271b72c7SEric Dumazet 	rcu_read_lock();
291fddc17deSEric Dumazet 	if (hslot->count > 10) {
292fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
293fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
294fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
295fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
296fddc17deSEric Dumazet 			goto begin;
297fddc17deSEric Dumazet 
298fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
299fddc17deSEric Dumazet 					  daddr, hnum, dif,
3001134158bSCraig Gallek 					  hslot2, slot2, skb);
301fddc17deSEric Dumazet 		if (!result) {
302fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
303fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
304fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
305fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
306fddc17deSEric Dumazet 				goto begin;
307fddc17deSEric Dumazet 
3081223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
3091223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
3101134158bSCraig Gallek 						  hslot2, slot2, skb);
311fddc17deSEric Dumazet 		}
312fddc17deSEric Dumazet 		rcu_read_unlock();
313fddc17deSEric Dumazet 		return result;
314fddc17deSEric Dumazet 	}
315271b72c7SEric Dumazet begin:
316271b72c7SEric Dumazet 	result = NULL;
317271b72c7SEric Dumazet 	badness = -1;
31888ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
319645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
320645ca708SEric Dumazet 		if (score > badness) {
321db8dac20SDavid S. Miller 			result = sk;
322db8dac20SDavid S. Miller 			badness = score;
32372289b96STom Herbert 			reuseport = sk->sk_reuseport;
32472289b96STom Herbert 			if (reuseport) {
325b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
32672289b96STom Herbert 						    saddr, sport);
327ed0dfffdSEric Dumazet 				if (select_ok) {
328ed0dfffdSEric Dumazet 					struct sock *sk2;
329ed0dfffdSEric Dumazet 
330538950a1SCraig Gallek 					sk2 = reuseport_select_sock(sk, hash, skb,
331538950a1SCraig Gallek 							sizeof(struct udphdr));
332e32ea7e7SCraig Gallek 					if (sk2) {
333e32ea7e7SCraig Gallek 						result = sk2;
334ed0dfffdSEric Dumazet 						select_ok = false;
335e32ea7e7SCraig Gallek 						goto found;
336e32ea7e7SCraig Gallek 					}
337ed0dfffdSEric Dumazet 				}
33872289b96STom Herbert 				matches = 1;
33972289b96STom Herbert 			}
34072289b96STom Herbert 		} else if (score == badness && reuseport) {
34172289b96STom Herbert 			matches++;
3428fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
34372289b96STom Herbert 				result = sk;
34472289b96STom Herbert 			hash = next_pseudo_random32(hash);
345db8dac20SDavid S. Miller 		}
346db8dac20SDavid S. Miller 	}
34788ab1932SEric Dumazet 	/*
34888ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
34988ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
35088ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
35188ab1932SEric Dumazet 	 */
352fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
35388ab1932SEric Dumazet 		goto begin;
35488ab1932SEric Dumazet 
355271b72c7SEric Dumazet 	if (result) {
356e32ea7e7SCraig Gallek found:
357c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
358271b72c7SEric Dumazet 			result = NULL;
359271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
360271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
361271b72c7SEric Dumazet 			sock_put(result);
362271b72c7SEric Dumazet 			goto begin;
363271b72c7SEric Dumazet 		}
364271b72c7SEric Dumazet 	}
365271b72c7SEric Dumazet 	rcu_read_unlock();
366db8dac20SDavid S. Miller 	return result;
367db8dac20SDavid S. Miller }
368fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
369db8dac20SDavid S. Miller 
370607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
371607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
372645ca708SEric Dumazet 					  struct udp_table *udptable)
373607c4aafSKOVACS Krisztian {
37423542618SKOVACS Krisztian 	struct sock *sk;
375b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
376607c4aafSKOVACS Krisztian 
377e5d08d71SIan Morris 	sk = skb_steal_sock(skb);
378e5d08d71SIan Morris 	if (unlikely(sk))
37923542618SKOVACS Krisztian 		return sk;
380adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
381607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
382538950a1SCraig Gallek 				 udptable, skb);
383607c4aafSKOVACS Krisztian }
384607c4aafSKOVACS Krisztian 
385aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
386aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
387aa976fc0SBalazs Scheidler {
388538950a1SCraig Gallek 	return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table, NULL);
389aa976fc0SBalazs Scheidler }
390aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
391aa976fc0SBalazs Scheidler 
392db8dac20SDavid S. Miller /*
393db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
394db8dac20SDavid S. Miller  *	return it, otherwise we block.
395db8dac20SDavid S. Miller  */
396db8dac20SDavid S. Miller 
3971b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
398db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
399db8dac20SDavid S. Miller {
400db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
401db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
402db8dac20SDavid S. Miller 	struct sk_buff *skb;
40359c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
4043f518bf7SPavel Emelyanov 	int peeked, off = 0;
405db8dac20SDavid S. Miller 	int err;
406db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
407197c949eSEric Dumazet 	bool checksum_valid = false;
408f26ba175SWei Yongjun 	int is_udp4;
4098a74ad60SEric Dumazet 	bool slow;
410db8dac20SDavid S. Miller 
411db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
41285fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
413db8dac20SDavid S. Miller 
4144b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
41585fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4164b340ae2SBrian Haley 
417db8dac20SDavid S. Miller try_again:
418db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
4193f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
420db8dac20SDavid S. Miller 	if (!skb)
421db8dac20SDavid S. Miller 		goto out;
422db8dac20SDavid S. Miller 
423db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
42459c2cdaeSDavid S. Miller 	copied = len;
42559c2cdaeSDavid S. Miller 	if (copied > ulen)
42659c2cdaeSDavid S. Miller 		copied = ulen;
42759c2cdaeSDavid S. Miller 	else if (copied < ulen)
428db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
429db8dac20SDavid S. Miller 
430f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
431f26ba175SWei Yongjun 
432db8dac20SDavid S. Miller 	/*
433db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
434db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
435db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
436db8dac20SDavid S. Miller 	 */
437db8dac20SDavid S. Miller 
43859c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
439197c949eSEric Dumazet 		checksum_valid = !udp_lib_checksum_complete(skb);
440197c949eSEric Dumazet 		if (!checksum_valid)
441db8dac20SDavid S. Miller 			goto csum_copy_err;
442db8dac20SDavid S. Miller 	}
443db8dac20SDavid S. Miller 
444197c949eSEric Dumazet 	if (checksum_valid || skb_csum_unnecessary(skb))
44551f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, sizeof(struct udphdr),
44651f3d02bSDavid S. Miller 					    msg, copied);
447db8dac20SDavid S. Miller 	else {
448227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr), msg);
449db8dac20SDavid S. Miller 		if (err == -EINVAL)
450db8dac20SDavid S. Miller 			goto csum_copy_err;
451db8dac20SDavid S. Miller 	}
45222911fc5SEric Dumazet 	if (unlikely(err)) {
45322911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
454979402b1SEric Dumazet 		if (!peeked) {
455979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
456979402b1SEric Dumazet 			if (is_udp4)
457979402b1SEric Dumazet 				UDP_INC_STATS_USER(sock_net(sk),
458979402b1SEric Dumazet 						   UDP_MIB_INERRORS,
459979402b1SEric Dumazet 						   is_udplite);
460979402b1SEric Dumazet 			else
461979402b1SEric Dumazet 				UDP6_INC_STATS_USER(sock_net(sk),
462979402b1SEric Dumazet 						    UDP_MIB_INERRORS,
463979402b1SEric Dumazet 						    is_udplite);
464979402b1SEric Dumazet 		}
465db8dac20SDavid S. Miller 		goto out_free;
46622911fc5SEric Dumazet 	}
467f26ba175SWei Yongjun 	if (!peeked) {
468f26ba175SWei Yongjun 		if (is_udp4)
469f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
470f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
471f26ba175SWei Yongjun 		else
472235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
473235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
474f26ba175SWei Yongjun 	}
475db8dac20SDavid S. Miller 
4763b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
477db8dac20SDavid S. Miller 
478db8dac20SDavid S. Miller 	/* Copy the address. */
479db8dac20SDavid S. Miller 	if (msg->msg_name) {
480342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
481db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
482db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
483db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
484db8dac20SDavid S. Miller 
485842df073SHannes Frederic Sowa 		if (is_udp4) {
486b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
487b301e82cSBrian Haley 					       &sin6->sin6_addr);
488842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
489842df073SHannes Frederic Sowa 		} else {
4904e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
491842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
492842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
4934330487aSDuan Jiong 						    inet6_iif(skb));
494db8dac20SDavid S. Miller 		}
495bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
496db8dac20SDavid S. Miller 	}
4974b261c75SHannes Frederic Sowa 
4984b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
4994b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
5004b261c75SHannes Frederic Sowa 
501f26ba175SWei Yongjun 	if (is_udp4) {
502db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
503db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
504db8dac20SDavid S. Miller 	} else {
505db8dac20SDavid S. Miller 		if (np->rxopt.all)
5064b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
507db8dac20SDavid S. Miller 	}
508db8dac20SDavid S. Miller 
50959c2cdaeSDavid S. Miller 	err = copied;
510db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
511db8dac20SDavid S. Miller 		err = ulen;
512db8dac20SDavid S. Miller 
513db8dac20SDavid S. Miller out_free:
5149d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
515db8dac20SDavid S. Miller out:
516db8dac20SDavid S. Miller 	return err;
517db8dac20SDavid S. Miller 
518db8dac20SDavid S. Miller csum_copy_err:
5198a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
5200856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
5216a5dc9e5SEric Dumazet 		if (is_udp4) {
5226a5dc9e5SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
5236a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
5240856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
5250856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
5266a5dc9e5SEric Dumazet 		} else {
5276a5dc9e5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
5286a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
5290856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
5300856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
5310856f939SWei Yongjun 		}
5326a5dc9e5SEric Dumazet 	}
5338a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
534db8dac20SDavid S. Miller 
535beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
536beb39db5SEric Dumazet 	cond_resched();
5379cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
538db8dac20SDavid S. Miller 	goto try_again;
539db8dac20SDavid S. Miller }
540db8dac20SDavid S. Miller 
541db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
542d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
543645ca708SEric Dumazet 		    struct udp_table *udptable)
544db8dac20SDavid S. Miller {
545db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
546b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
547b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
548b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
549db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
550db8dac20SDavid S. Miller 	struct sock *sk;
551e0d8c1b7SWei Wang 	int harderr;
552db8dac20SDavid S. Miller 	int err;
5537304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
554db8dac20SDavid S. Miller 
555e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
556538950a1SCraig Gallek 			       inet6_iif(skb), udptable, skb);
55763159f29SIan Morris 	if (!sk) {
5587304fe46SDuan Jiong 		ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
5597304fe46SDuan Jiong 				   ICMP6_MIB_INERRORS);
560db8dac20SDavid S. Miller 		return;
5617304fe46SDuan Jiong 	}
562db8dac20SDavid S. Miller 
563e0d8c1b7SWei Wang 	harderr = icmpv6_err_convert(type, code, &err);
564e0d8c1b7SWei Wang 	np = inet6_sk(sk);
565e0d8c1b7SWei Wang 
56693b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
56793b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
56893b36cf3SHannes Frederic Sowa 			goto out;
56981aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
570e0d8c1b7SWei Wang 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
571e0d8c1b7SWei Wang 			harderr = 1;
57293b36cf3SHannes Frederic Sowa 	}
5731a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
574ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
5751a462d18SDuan Jiong 		goto out;
5761a462d18SDuan Jiong 	}
57781aded24SDavid S. Miller 
578e0d8c1b7SWei Wang 	if (!np->recverr) {
579e0d8c1b7SWei Wang 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
580db8dac20SDavid S. Miller 			goto out;
581e0d8c1b7SWei Wang 	} else {
582db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
583e0d8c1b7SWei Wang 	}
584b1faf566SEric Dumazet 
585db8dac20SDavid S. Miller 	sk->sk_err = err;
586db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
587db8dac20SDavid S. Miller out:
588db8dac20SDavid S. Miller 	sock_put(sk);
589db8dac20SDavid S. Miller }
590db8dac20SDavid S. Miller 
591f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
592f7ad74feSBenjamin LaHaise {
593f7ad74feSBenjamin LaHaise 	int rc;
594f7ad74feSBenjamin LaHaise 
595efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
596f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
597005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
5982c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
599005ec974SShawn Bohrer 	}
600f7ad74feSBenjamin LaHaise 
601f7ad74feSBenjamin LaHaise 	rc = sock_queue_rcv_skb(sk, skb);
602f7ad74feSBenjamin LaHaise 	if (rc < 0) {
603f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
604f7ad74feSBenjamin LaHaise 
605f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
606f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
607f7ad74feSBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk),
608f7ad74feSBenjamin LaHaise 					UDP_MIB_RCVBUFERRORS, is_udplite);
609f7ad74feSBenjamin LaHaise 		UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
610f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
611f7ad74feSBenjamin LaHaise 		return -1;
612f7ad74feSBenjamin LaHaise 	}
613f7ad74feSBenjamin LaHaise 	return 0;
614f7ad74feSBenjamin LaHaise }
615f7ad74feSBenjamin LaHaise 
616db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
617d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
618d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info)
619db8dac20SDavid S. Miller {
620645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
621db8dac20SDavid S. Miller }
622db8dac20SDavid S. Miller 
623d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
624d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
625d7f3f621SBenjamin LaHaise {
626d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
627d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
628d7f3f621SBenjamin LaHaise }
629d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
630d7f3f621SBenjamin LaHaise 
631db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
632db8dac20SDavid S. Miller {
633db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
634db8dac20SDavid S. Miller 	int rc;
635db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
636db8dac20SDavid S. Miller 
637db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
638db8dac20SDavid S. Miller 		goto drop;
639db8dac20SDavid S. Miller 
640d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
641d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
642d7f3f621SBenjamin LaHaise 
643d7f3f621SBenjamin LaHaise 		/*
644d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
645d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
646d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
647d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
648d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
649d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
650d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
651d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
652d7f3f621SBenjamin LaHaise 		 */
653d7f3f621SBenjamin LaHaise 
654d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
655d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
65653b24b8fSIan Morris 		if (skb->len > sizeof(struct udphdr) && encap_rcv) {
657d7f3f621SBenjamin LaHaise 			int ret;
658d7f3f621SBenjamin LaHaise 
6590a80966bSTom Herbert 			/* Verify checksum before giving to encap */
6600a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
6610a80966bSTom Herbert 				goto csum_error;
6620a80966bSTom Herbert 
663d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
664d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
665d7f3f621SBenjamin LaHaise 				UDP_INC_STATS_BH(sock_net(sk),
666d7f3f621SBenjamin LaHaise 						 UDP_MIB_INDATAGRAMS,
667d7f3f621SBenjamin LaHaise 						 is_udplite);
668d7f3f621SBenjamin LaHaise 				return -ret;
669d7f3f621SBenjamin LaHaise 			}
670d7f3f621SBenjamin LaHaise 		}
671d7f3f621SBenjamin LaHaise 
672d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
673d7f3f621SBenjamin LaHaise 	}
674d7f3f621SBenjamin LaHaise 
675db8dac20SDavid S. Miller 	/*
676db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
677db8dac20SDavid S. Miller 	 */
678db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
679db8dac20SDavid S. Miller 
680db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
681ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
682db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
683db8dac20SDavid S. Miller 			goto drop;
684db8dac20SDavid S. Miller 		}
685db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
686ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
687db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
688db8dac20SDavid S. Miller 			goto drop;
689db8dac20SDavid S. Miller 		}
690db8dac20SDavid S. Miller 	}
691db8dac20SDavid S. Miller 
69233d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
693db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
6946a5dc9e5SEric Dumazet 			goto csum_error;
695db8dac20SDavid S. Miller 	}
696db8dac20SDavid S. Miller 
697274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
6983e215c8dSJames M Leddy 		UDP6_INC_STATS_BH(sock_net(sk),
6993e215c8dSJames M Leddy 				  UDP_MIB_RCVBUFERRORS, is_udplite);
700cb80ef46SBenjamin LaHaise 		goto drop;
7013e215c8dSJames M Leddy 	}
702cb80ef46SBenjamin LaHaise 
703d826eb14SEric Dumazet 	skb_dst_drop(skb);
704db8dac20SDavid S. Miller 
705cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
706cb80ef46SBenjamin LaHaise 	rc = 0;
707cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
708f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
709cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
710cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
711cb80ef46SBenjamin LaHaise 		goto drop;
712cb80ef46SBenjamin LaHaise 	}
713cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
714f7ad74feSBenjamin LaHaise 
715f7ad74feSBenjamin LaHaise 	return rc;
7163e215c8dSJames M Leddy 
7176a5dc9e5SEric Dumazet csum_error:
7186a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
719db8dac20SDavid S. Miller drop:
720ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
721cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
722db8dac20SDavid S. Miller 	kfree_skb(skb);
723db8dac20SDavid S. Miller 	return -1;
724db8dac20SDavid S. Miller }
725db8dac20SDavid S. Miller 
7265cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
727b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
728b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
7295cf3d461SDavid Held 				   int dif, unsigned short hnum)
730db8dac20SDavid S. Miller {
7319e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
732db8dac20SDavid S. Miller 
7339e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
7345cf3d461SDavid Held 		return false;
735b8ad0cbcSDaniel Lezcano 
7365cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
7375cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
7385cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
7395cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
7405cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
74133b4b015SHenning Rogge 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
74233b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
74333b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
7445cf3d461SDavid Held 		return false;
7459e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
7465cf3d461SDavid Held 		return false;
7475cf3d461SDavid Held 	return true;
748db8dac20SDavid S. Miller }
749db8dac20SDavid S. Miller 
750a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
751a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
752a1ab77f9SEric Dumazet {
753cb80ef46SBenjamin LaHaise 	struct sk_buff *skb1 = NULL;
754a1ab77f9SEric Dumazet 	struct sock *sk;
755cb80ef46SBenjamin LaHaise 	unsigned int i;
756a1ab77f9SEric Dumazet 
757a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
758a1ab77f9SEric Dumazet 		sk = stack[i];
75963159f29SIan Morris 		if (likely(!skb1))
760cb80ef46SBenjamin LaHaise 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
761cb80ef46SBenjamin LaHaise 		if (!skb1) {
762f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
763cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
764cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
765cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
766cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
767a1ab77f9SEric Dumazet 		}
768cb80ef46SBenjamin LaHaise 
769cb80ef46SBenjamin LaHaise 		if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
770cb80ef46SBenjamin LaHaise 			skb1 = NULL;
7712dc41cffSDavid Held 		sock_put(sk);
772cb80ef46SBenjamin LaHaise 	}
773cb80ef46SBenjamin LaHaise 	if (unlikely(skb1))
774cb80ef46SBenjamin LaHaise 		kfree_skb(skb1);
775a1ab77f9SEric Dumazet }
7764068579eSTom Herbert 
7774068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
7784068579eSTom Herbert {
7794068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
7804068579eSTom Herbert 	 * this error. Well, it is reasonable.
7814068579eSTom Herbert 	 */
782ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
7834068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
7844068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
7854068579eSTom Herbert }
7864068579eSTom Herbert 
787db8dac20SDavid S. Miller /*
788db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
789db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
790db8dac20SDavid S. Miller  */
791e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
792b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
79336cbb245SRick Jones 		struct udp_table *udptable, int proto)
794db8dac20SDavid S. Miller {
795a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
796db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
7975cf3d461SDavid Held 	struct hlist_nulls_node *node;
7985cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
7995cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
8005cf3d461SDavid Held 	int dif = inet6_iif(skb);
8012dc41cffSDavid Held 	unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
8022dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
80336cbb245SRick Jones 	bool inner_flushed = false;
8042dc41cffSDavid Held 
8052dc41cffSDavid Held 	if (use_hash2) {
8062dc41cffSDavid Held 		hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
8072dc41cffSDavid Held 			    udp_table.mask;
8082dc41cffSDavid Held 		hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
8092dc41cffSDavid Held start_lookup:
8102dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
8112dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
8122dc41cffSDavid Held 	}
813db8dac20SDavid S. Miller 
814645ca708SEric Dumazet 	spin_lock(&hslot->lock);
8152dc41cffSDavid Held 	sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
8165cf3d461SDavid Held 		if (__udp_v6_is_mcast_sock(net, sk,
8175cf3d461SDavid Held 					   uh->dest, daddr,
8185cf3d461SDavid Held 					   uh->source, saddr,
8195cf3d461SDavid Held 					   dif, hnum) &&
8201c19448cSTom Herbert 		    /* If zero checksum and no_check is not on for
8214068579eSTom Herbert 		     * the socket then skip it.
8224068579eSTom Herbert 		     */
8235cf3d461SDavid Held 		    (uh->check || udp_sk(sk)->no_check6_rx)) {
824a1ab77f9SEric Dumazet 			if (unlikely(count == ARRAY_SIZE(stack))) {
825a1ab77f9SEric Dumazet 				flush_stack(stack, count, skb, ~0);
82636cbb245SRick Jones 				inner_flushed = true;
827a1ab77f9SEric Dumazet 				count = 0;
828db8dac20SDavid S. Miller 			}
8295cf3d461SDavid Held 			stack[count++] = sk;
8302dc41cffSDavid Held 			sock_hold(sk);
8315cf3d461SDavid Held 		}
832a1ab77f9SEric Dumazet 	}
833db8dac20SDavid S. Miller 
834645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
835a1ab77f9SEric Dumazet 
8362dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
8372dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
8382dc41cffSDavid Held 		hash2 = hash2_any;
8392dc41cffSDavid Held 		goto start_lookup;
8402dc41cffSDavid Held 	}
8412dc41cffSDavid Held 
842a1ab77f9SEric Dumazet 	if (count) {
843a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
844a1ab77f9SEric Dumazet 	} else {
84536cbb245SRick Jones 		if (!inner_flushed)
84636cbb245SRick Jones 			UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI,
84736cbb245SRick Jones 					 proto == IPPROTO_UDPLITE);
84836cbb245SRick Jones 		consume_skb(skb);
849a1ab77f9SEric Dumazet 	}
850db8dac20SDavid S. Miller 	return 0;
851db8dac20SDavid S. Miller }
852db8dac20SDavid S. Miller 
853645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
854db8dac20SDavid S. Miller 		   int proto)
855db8dac20SDavid S. Miller {
8563ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
857db8dac20SDavid S. Miller 	struct sock *sk;
858db8dac20SDavid S. Miller 	struct udphdr *uh;
859b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
860db8dac20SDavid S. Miller 	u32 ulen = 0;
861db8dac20SDavid S. Miller 
862db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
863d6bc0149SBjørn Mork 		goto discard;
864db8dac20SDavid S. Miller 
865db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
866db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
867db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
868db8dac20SDavid S. Miller 
869db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
870db8dac20SDavid S. Miller 	if (ulen > skb->len)
871db8dac20SDavid S. Miller 		goto short_packet;
872db8dac20SDavid S. Miller 
873db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
874db8dac20SDavid S. Miller 		/* UDP validates ulen. */
875db8dac20SDavid S. Miller 
876db8dac20SDavid S. Miller 		/* Check for jumbo payload */
877db8dac20SDavid S. Miller 		if (ulen == 0)
878db8dac20SDavid S. Miller 			ulen = skb->len;
879db8dac20SDavid S. Miller 
880db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
881db8dac20SDavid S. Miller 			goto short_packet;
882db8dac20SDavid S. Miller 
883db8dac20SDavid S. Miller 		if (ulen < skb->len) {
884db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
885db8dac20SDavid S. Miller 				goto short_packet;
886db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
887db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
888db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
889db8dac20SDavid S. Miller 		}
890db8dac20SDavid S. Miller 	}
891db8dac20SDavid S. Miller 
892db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
8936a5dc9e5SEric Dumazet 		goto csum_error;
894db8dac20SDavid S. Miller 
895db8dac20SDavid S. Miller 	/*
896db8dac20SDavid S. Miller 	 *	Multicast receive code
897db8dac20SDavid S. Miller 	 */
898db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
899e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
90036cbb245SRick Jones 				saddr, daddr, udptable, proto);
901db8dac20SDavid S. Miller 
902db8dac20SDavid S. Miller 	/* Unicast */
903db8dac20SDavid S. Miller 
904db8dac20SDavid S. Miller 	/*
905db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
906db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
907db8dac20SDavid S. Miller 	 */
908607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
90953b24b8fSIan Morris 	if (sk) {
910a5b50476SEliezer Tamir 		int ret;
911a5b50476SEliezer Tamir 
9121c19448cSTom Herbert 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
91379e0f1c9STom Herbert 			sock_put(sk);
9144068579eSTom Herbert 			udp6_csum_zero_error(skb);
9154068579eSTom Herbert 			goto csum_error;
9164068579eSTom Herbert 		}
9174068579eSTom Herbert 
918224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
9192abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
9202abb7cdcSTom Herbert 						 ip6_compute_pseudo);
9212abb7cdcSTom Herbert 
922a5b50476SEliezer Tamir 		ret = udpv6_queue_rcv_skb(sk, skb);
923cb80ef46SBenjamin LaHaise 		sock_put(sk);
924db8dac20SDavid S. Miller 
925cb80ef46SBenjamin LaHaise 		/* a return value > 0 means to resubmit the input, but
926cb80ef46SBenjamin LaHaise 		 * it wants the return to be -protocol, or 0
927cb80ef46SBenjamin LaHaise 		 */
928cb80ef46SBenjamin LaHaise 		if (ret > 0)
929cb80ef46SBenjamin LaHaise 			return -ret;
930cb80ef46SBenjamin LaHaise 
931cb80ef46SBenjamin LaHaise 		return 0;
932cb80ef46SBenjamin LaHaise 	}
933cb80ef46SBenjamin LaHaise 
9344068579eSTom Herbert 	if (!uh->check) {
9354068579eSTom Herbert 		udp6_csum_zero_error(skb);
9364068579eSTom Herbert 		goto csum_error;
9374068579eSTom Herbert 	}
9384068579eSTom Herbert 
939db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
940db8dac20SDavid S. Miller 		goto discard;
941db8dac20SDavid S. Miller 
942db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
9436a5dc9e5SEric Dumazet 		goto csum_error;
944db8dac20SDavid S. Miller 
945cb80ef46SBenjamin LaHaise 	UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
9463ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
947db8dac20SDavid S. Miller 
948db8dac20SDavid S. Miller 	kfree_skb(skb);
949db8dac20SDavid S. Miller 	return 0;
950db8dac20SDavid S. Miller 
951db8dac20SDavid S. Miller short_packet:
952ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
953db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
954ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
955ba7a46f1SJoe Perches 			    ulen, skb->len,
956ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
9576a5dc9e5SEric Dumazet 	goto discard;
9586a5dc9e5SEric Dumazet csum_error:
9596a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
960db8dac20SDavid S. Miller discard:
961ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
962db8dac20SDavid S. Miller 	kfree_skb(skb);
963db8dac20SDavid S. Miller 	return 0;
964db8dac20SDavid S. Miller }
965db8dac20SDavid S. Miller 
966db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
967db8dac20SDavid S. Miller {
968645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
969db8dac20SDavid S. Miller }
970db8dac20SDavid S. Miller 
971db8dac20SDavid S. Miller /*
972db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
973db8dac20SDavid S. Miller  */
974db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
975db8dac20SDavid S. Miller {
976db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
977db8dac20SDavid S. Miller 
97836d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
97936d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
98036d926b9SDenis V. Lunev 	else if (up->pending) {
981db8dac20SDavid S. Miller 		up->len = 0;
982db8dac20SDavid S. Miller 		up->pending = 0;
983db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
984db8dac20SDavid S. Miller 	}
985db8dac20SDavid S. Miller }
986db8dac20SDavid S. Miller 
987493c6be3SSridhar Samudrala /**
988493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
989493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
990493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
991493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
992493c6be3SSridhar Samudrala  */
993493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
994493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
995493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
996493c6be3SSridhar Samudrala {
997493c6be3SSridhar Samudrala 	unsigned int offset;
998493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
999d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1000493c6be3SSridhar Samudrala 	__wsum csum = 0;
1001493c6be3SSridhar Samudrala 
1002d39d938cSVlad Yasevich 	if (!frags) {
1003493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
1004493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
1005493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
1006493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1007493c6be3SSridhar Samudrala 	} else {
1008493c6be3SSridhar Samudrala 		/*
1009493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
1010493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
1011493c6be3SSridhar Samudrala 		 * should be together
1012493c6be3SSridhar Samudrala 		 */
1013493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
1014493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1015493c6be3SSridhar Samudrala 
1016493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
1017493c6be3SSridhar Samudrala 
1018d39d938cSVlad Yasevich 		do {
1019d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
1020d39d938cSVlad Yasevich 		} while ((frags = frags->next));
1021493c6be3SSridhar Samudrala 
1022493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1023493c6be3SSridhar Samudrala 					    csum);
1024493c6be3SSridhar Samudrala 		if (uh->check == 0)
1025493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
1026493c6be3SSridhar Samudrala 	}
1027493c6be3SSridhar Samudrala }
1028493c6be3SSridhar Samudrala 
1029db8dac20SDavid S. Miller /*
1030db8dac20SDavid S. Miller  *	Sending
1031db8dac20SDavid S. Miller  */
1032db8dac20SDavid S. Miller 
1033d39d938cSVlad Yasevich static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
1034db8dac20SDavid S. Miller {
1035d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
1036db8dac20SDavid S. Miller 	struct udphdr *uh;
1037db8dac20SDavid S. Miller 	int err = 0;
1038db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1039db8dac20SDavid S. Miller 	__wsum csum = 0;
1040d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
1041d39d938cSVlad Yasevich 	int len = skb->len - offset;
1042db8dac20SDavid S. Miller 
1043db8dac20SDavid S. Miller 	/*
1044db8dac20SDavid S. Miller 	 * Create a UDP header
1045db8dac20SDavid S. Miller 	 */
1046db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
10471958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
10481958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
1049d39d938cSVlad Yasevich 	uh->len = htons(len);
1050db8dac20SDavid S. Miller 	uh->check = 0;
1051db8dac20SDavid S. Miller 
1052db8dac20SDavid S. Miller 	if (is_udplite)
1053d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
1054d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
10554068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
10564068579eSTom Herbert 		goto send;
10574068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1058d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1059493c6be3SSridhar Samudrala 		goto send;
1060493c6be3SSridhar Samudrala 	} else
1061d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1062db8dac20SDavid S. Miller 
1063db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
10644c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1065d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1066db8dac20SDavid S. Miller 	if (uh->check == 0)
1067db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1068db8dac20SDavid S. Miller 
1069493c6be3SSridhar Samudrala send:
1070d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
10716ce9e7b5SEric Dumazet 	if (err) {
10726ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
10736ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
10746ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
10756ce9e7b5SEric Dumazet 			err = 0;
10766ce9e7b5SEric Dumazet 		}
10776ce9e7b5SEric Dumazet 	} else
10786ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
10796ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
1080d39d938cSVlad Yasevich 	return err;
1081d39d938cSVlad Yasevich }
1082d39d938cSVlad Yasevich 
1083d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1084d39d938cSVlad Yasevich {
1085d39d938cSVlad Yasevich 	struct sk_buff *skb;
1086d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1087d39d938cSVlad Yasevich 	struct flowi6 fl6;
1088d39d938cSVlad Yasevich 	int err = 0;
1089d39d938cSVlad Yasevich 
1090d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1091d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1092d39d938cSVlad Yasevich 
1093d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
1094d39d938cSVlad Yasevich 	 * fl6 here.
1095d39d938cSVlad Yasevich 	 */
1096d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1097d39d938cSVlad Yasevich 
1098d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1099d39d938cSVlad Yasevich 	if (!skb)
1100d39d938cSVlad Yasevich 		goto out;
1101d39d938cSVlad Yasevich 
1102d39d938cSVlad Yasevich 	err = udp_v6_send_skb(skb, &fl6);
1103d39d938cSVlad Yasevich 
1104db8dac20SDavid S. Miller out:
1105db8dac20SDavid S. Miller 	up->len = 0;
1106db8dac20SDavid S. Miller 	up->pending = 0;
1107db8dac20SDavid S. Miller 	return err;
1108db8dac20SDavid S. Miller }
1109db8dac20SDavid S. Miller 
11101b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1111db8dac20SDavid S. Miller {
1112db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1113db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1114db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1115db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1116342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
111720c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1118db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
111945f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1120db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
11214c9483b2SDavid S. Miller 	struct flowi6 fl6;
1122db8dac20SDavid S. Miller 	struct dst_entry *dst;
1123db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1124db8dac20SDavid S. Miller 	int ulen = len;
1125db8dac20SDavid S. Miller 	int hlimit = -1;
1126db8dac20SDavid S. Miller 	int tclass = -1;
112713b52cd4SBrian Haley 	int dontfrag = -1;
1128db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1129db8dac20SDavid S. Miller 	int err;
1130db8dac20SDavid S. Miller 	int connected = 0;
1131db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1132db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1133db8dac20SDavid S. Miller 
1134db8dac20SDavid S. Miller 	/* destination address check */
1135db8dac20SDavid S. Miller 	if (sin6) {
1136db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1137db8dac20SDavid S. Miller 			return -EINVAL;
1138db8dac20SDavid S. Miller 
1139db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1140db8dac20SDavid S. Miller 		case AF_INET6:
1141db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1142db8dac20SDavid S. Miller 				return -EINVAL;
1143db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1144db8dac20SDavid S. Miller 			break;
1145db8dac20SDavid S. Miller 		case AF_INET:
1146db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1147db8dac20SDavid S. Miller 		case AF_UNSPEC:
1148db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1149db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1150db8dac20SDavid S. Miller 			daddr = NULL;
1151db8dac20SDavid S. Miller 			break;
1152db8dac20SDavid S. Miller 		default:
1153db8dac20SDavid S. Miller 			return -EINVAL;
1154db8dac20SDavid S. Miller 		}
1155db8dac20SDavid S. Miller 	} else if (!up->pending) {
1156db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1157db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1158efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1159db8dac20SDavid S. Miller 	} else
1160db8dac20SDavid S. Miller 		daddr = NULL;
1161db8dac20SDavid S. Miller 
1162db8dac20SDavid S. Miller 	if (daddr) {
1163db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1164db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1165db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1166c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1167db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1168db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1169db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1170db8dac20SDavid S. Miller do_udp_sendmsg:
1171db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1172db8dac20SDavid S. Miller 				return -ENETUNREACH;
11731b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1174db8dac20SDavid S. Miller 		}
1175db8dac20SDavid S. Miller 	}
1176db8dac20SDavid S. Miller 
1177db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
11781b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1179db8dac20SDavid S. Miller 
1180db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1181db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1182db8dac20SDavid S. Miller 	   */
1183db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1184db8dac20SDavid S. Miller 		return -EMSGSIZE;
1185db8dac20SDavid S. Miller 
118603485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1187db8dac20SDavid S. Miller 	if (up->pending) {
1188db8dac20SDavid S. Miller 		/*
1189db8dac20SDavid S. Miller 		 * There are pending frames.
1190db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1191db8dac20SDavid S. Miller 		 */
1192db8dac20SDavid S. Miller 		lock_sock(sk);
1193db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1194db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1195db8dac20SDavid S. Miller 				release_sock(sk);
1196db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1197db8dac20SDavid S. Miller 			}
1198db8dac20SDavid S. Miller 			dst = NULL;
1199db8dac20SDavid S. Miller 			goto do_append_data;
1200db8dac20SDavid S. Miller 		}
1201db8dac20SDavid S. Miller 		release_sock(sk);
1202db8dac20SDavid S. Miller 	}
1203db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1204db8dac20SDavid S. Miller 
12054c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1206db8dac20SDavid S. Miller 
1207db8dac20SDavid S. Miller 	if (sin6) {
1208db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1209db8dac20SDavid S. Miller 			return -EINVAL;
1210db8dac20SDavid S. Miller 
12111958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1212db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1213db8dac20SDavid S. Miller 
1214db8dac20SDavid S. Miller 		if (np->sndflow) {
12154c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
12164c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
12174c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
121863159f29SIan Morris 				if (!flowlabel)
1219db8dac20SDavid S. Miller 					return -EINVAL;
1220db8dac20SDavid S. Miller 			}
1221db8dac20SDavid S. Miller 		}
1222db8dac20SDavid S. Miller 
1223db8dac20SDavid S. Miller 		/*
1224db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1225db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1226db8dac20SDavid S. Miller 		 */
1227db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1228efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1229efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1230db8dac20SDavid S. Miller 
1231db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1232db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1233842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
12344c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1235db8dac20SDavid S. Miller 	} else {
1236db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1237db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1238db8dac20SDavid S. Miller 
12391958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1240efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
12414c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1242db8dac20SDavid S. Miller 		connected = 1;
1243db8dac20SDavid S. Miller 	}
1244db8dac20SDavid S. Miller 
12454c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
12464c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1247db8dac20SDavid S. Miller 
12484c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
12494c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
12509f690db7SYang Hongyang 
12514c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
125251953d5bSBrian Haley 
1253db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1254db8dac20SDavid S. Miller 		opt = &opt_space;
1255db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1256db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1257db8dac20SDavid S. Miller 
125873df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1259ec0506dbSMaciej Żenczykowski 					    &hlimit, &tclass, &dontfrag);
1260db8dac20SDavid S. Miller 		if (err < 0) {
1261db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1262db8dac20SDavid S. Miller 			return err;
1263db8dac20SDavid S. Miller 		}
12644c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
12654c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
126663159f29SIan Morris 			if (!flowlabel)
1267db8dac20SDavid S. Miller 				return -EINVAL;
1268db8dac20SDavid S. Miller 		}
1269db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1270db8dac20SDavid S. Miller 			opt = NULL;
1271db8dac20SDavid S. Miller 		connected = 0;
1272db8dac20SDavid S. Miller 	}
127345f6fad8SEric Dumazet 	if (!opt) {
127445f6fad8SEric Dumazet 		opt = txopt_get(np);
127545f6fad8SEric Dumazet 		opt_to_free = opt;
127645f6fad8SEric Dumazet 	}
1277db8dac20SDavid S. Miller 	if (flowlabel)
1278db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1279db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1280db8dac20SDavid S. Miller 
12814c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1282876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
12834e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1284876c7f41SBrian Haley 	else
12854c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
12864c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
12874e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
12881958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1289db8dac20SDavid S. Miller 
12904c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
129120c59de2SArnaud Ebalard 	if (final_p)
1292db8dac20SDavid S. Miller 		connected = 0;
1293db8dac20SDavid S. Miller 
12944c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
12954c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1296db8dac20SDavid S. Miller 		connected = 0;
1297c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1298c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1299db8dac20SDavid S. Miller 
13004c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1301db8dac20SDavid S. Miller 
13020e0d44abSSteffen Klassert 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
130368d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
130468d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
130568d0c6d3SDavid S. Miller 		dst = NULL;
1306db8dac20SDavid S. Miller 		goto out;
1307db8dac20SDavid S. Miller 	}
1308db8dac20SDavid S. Miller 
1309db8dac20SDavid S. Miller 	if (hlimit < 0)
13105c98631cSLorenzo Colitti 		hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1311db8dac20SDavid S. Miller 
1312db8dac20SDavid S. Miller 	if (tclass < 0)
1313e651f03aSGerrit Renker 		tclass = np->tclass;
1314db8dac20SDavid S. Miller 
1315db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1316db8dac20SDavid S. Miller 		goto do_confirm;
1317db8dac20SDavid S. Miller back_from_confirm:
1318db8dac20SDavid S. Miller 
131903485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
132003485f2aSVlad Yasevich 	if (!corkreq) {
132103485f2aSVlad Yasevich 		struct sk_buff *skb;
132203485f2aSVlad Yasevich 
132303485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
132403485f2aSVlad Yasevich 				   sizeof(struct udphdr), hlimit, tclass, opt,
132503485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
132603485f2aSVlad Yasevich 				   msg->msg_flags, dontfrag);
132703485f2aSVlad Yasevich 		err = PTR_ERR(skb);
132803485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
132903485f2aSVlad Yasevich 			err = udp_v6_send_skb(skb, &fl6);
133003485f2aSVlad Yasevich 		goto release_dst;
133103485f2aSVlad Yasevich 	}
133203485f2aSVlad Yasevich 
1333db8dac20SDavid S. Miller 	lock_sock(sk);
1334db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1335db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1336db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1337db8dac20SDavid S. Miller 		release_sock(sk);
1338db8dac20SDavid S. Miller 
1339ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1340db8dac20SDavid S. Miller 		err = -EINVAL;
1341db8dac20SDavid S. Miller 		goto out;
1342db8dac20SDavid S. Miller 	}
1343db8dac20SDavid S. Miller 
1344db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1345db8dac20SDavid S. Miller 
1346db8dac20SDavid S. Miller do_append_data:
1347e36d3ff9SJiri Pirko 	if (dontfrag < 0)
1348e36d3ff9SJiri Pirko 		dontfrag = np->dontfrag;
1349db8dac20SDavid S. Miller 	up->len += ulen;
1350f69e6d13SAl Viro 	err = ip6_append_data(sk, getfrag, msg, ulen,
13514c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1352db8dac20SDavid S. Miller 		(struct rt6_info *)dst,
135313b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1354db8dac20SDavid S. Miller 	if (err)
1355db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1356db8dac20SDavid S. Miller 	else if (!corkreq)
1357db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1358db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1359db8dac20SDavid S. Miller 		up->pending = 0;
1360db8dac20SDavid S. Miller 
136103485f2aSVlad Yasevich 	if (err > 0)
136203485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
136303485f2aSVlad Yasevich 	release_sock(sk);
136403485f2aSVlad Yasevich 
136503485f2aSVlad Yasevich release_dst:
1366db8dac20SDavid S. Miller 	if (dst) {
1367db8dac20SDavid S. Miller 		if (connected) {
1368db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
1369efe4208fSEric Dumazet 				      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1370efe4208fSEric Dumazet 				      &sk->sk_v6_daddr : NULL,
1371db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
13724c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1373db8dac20SDavid S. Miller 				      &np->saddr :
1374db8dac20SDavid S. Miller #endif
1375db8dac20SDavid S. Miller 				      NULL);
1376db8dac20SDavid S. Miller 		} else {
1377db8dac20SDavid S. Miller 			dst_release(dst);
1378db8dac20SDavid S. Miller 		}
1379a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1380db8dac20SDavid S. Miller 	}
1381db8dac20SDavid S. Miller 
1382db8dac20SDavid S. Miller out:
1383a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1384db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
138545f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1386db8dac20SDavid S. Miller 	if (!err)
1387db8dac20SDavid S. Miller 		return len;
1388db8dac20SDavid S. Miller 	/*
1389db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1390db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1391db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1392db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1393db8dac20SDavid S. Miller 	 * seems like overkill.
1394db8dac20SDavid S. Miller 	 */
1395db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1396235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1397235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1398db8dac20SDavid S. Miller 	}
1399db8dac20SDavid S. Miller 	return err;
1400db8dac20SDavid S. Miller 
1401db8dac20SDavid S. Miller do_confirm:
1402db8dac20SDavid S. Miller 	dst_confirm(dst);
1403db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1404db8dac20SDavid S. Miller 		goto back_from_confirm;
1405db8dac20SDavid S. Miller 	err = 0;
1406db8dac20SDavid S. Miller 	goto out;
1407db8dac20SDavid S. Miller }
1408db8dac20SDavid S. Miller 
14097d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1410db8dac20SDavid S. Miller {
141144046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1412db8dac20SDavid S. Miller 	lock_sock(sk);
1413db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1414db8dac20SDavid S. Miller 	release_sock(sk);
1415db8dac20SDavid S. Miller 
141644046a59STom Parkin 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
141744046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
141844046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
141944046a59STom Parkin 		if (encap_destroy)
142044046a59STom Parkin 			encap_destroy(sk);
142144046a59STom Parkin 	}
142244046a59STom Parkin 
1423db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1424db8dac20SDavid S. Miller }
1425db8dac20SDavid S. Miller 
1426db8dac20SDavid S. Miller /*
1427db8dac20SDavid S. Miller  *	Socket option code for UDP
1428db8dac20SDavid S. Miller  */
1429db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1430b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1431db8dac20SDavid S. Miller {
1432db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1433db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1434db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1435db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1436db8dac20SDavid S. Miller }
1437db8dac20SDavid S. Miller 
1438db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1439db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1440b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1441db8dac20SDavid S. Miller {
1442db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1443db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1444db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1445db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1446db8dac20SDavid S. Miller }
1447db8dac20SDavid S. Miller #endif
1448db8dac20SDavid S. Miller 
1449db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1450db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1451db8dac20SDavid S. Miller {
1452db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1453db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1454db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1455db8dac20SDavid S. Miller }
1456db8dac20SDavid S. Miller 
1457db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1458db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1459db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1460db8dac20SDavid S. Miller {
1461db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1462db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1463db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1464db8dac20SDavid S. Miller }
1465db8dac20SDavid S. Miller #endif
1466db8dac20SDavid S. Miller 
146741135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1468db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1469db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1470db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1471db8dac20SDavid S. Miller };
1472db8dac20SDavid S. Miller 
1473db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1474db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1475db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1476db8dac20SDavid S. Miller {
147717ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
147817ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
147917ef66afSLorenzo Colitti 	} else {
148017ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
148117ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
148217ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
148317ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
148417ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
148517ef66afSLorenzo Colitti 	}
1486db8dac20SDavid S. Miller 	return 0;
1487db8dac20SDavid S. Miller }
1488db8dac20SDavid S. Miller 
148973cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
149073cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
149173cb88ecSArjan van de Ven 	.open     = udp_seq_open,
149273cb88ecSArjan van de Ven 	.read     = seq_read,
149373cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
149473cb88ecSArjan van de Ven 	.release  = seq_release_net
149573cb88ecSArjan van de Ven };
149673cb88ecSArjan van de Ven 
1497db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1498db8dac20SDavid S. Miller 	.name		= "udp6",
1499db8dac20SDavid S. Miller 	.family		= AF_INET6,
1500645ca708SEric Dumazet 	.udp_table	= &udp_table,
150173cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1502dda61925SDenis V. Lunev 	.seq_ops	= {
1503dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1504dda61925SDenis V. Lunev 	},
1505db8dac20SDavid S. Miller };
1506db8dac20SDavid S. Miller 
15072c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1508db8dac20SDavid S. Miller {
15090c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1510db8dac20SDavid S. Miller }
1511db8dac20SDavid S. Miller 
1512ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1513ec120da6SIan Morris {
15140c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1515db8dac20SDavid S. Miller }
1516db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1517db8dac20SDavid S. Miller 
1518f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size)
1519f77d6021SEric Dumazet {
1520f77d6021SEric Dumazet 	struct inet_sock *inet = inet_sk(sk);
1521f77d6021SEric Dumazet 
1522f77d6021SEric Dumazet 	/* we do not want to clear pinet6 field, because of RCU lookups */
1523f77d6021SEric Dumazet 	sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1524f77d6021SEric Dumazet 
1525f77d6021SEric Dumazet 	size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1526f77d6021SEric Dumazet 	memset(&inet->pinet6 + 1, 0, size);
1527f77d6021SEric Dumazet }
1528f77d6021SEric Dumazet 
1529db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1530db8dac20SDavid S. Miller 
1531db8dac20SDavid S. Miller struct proto udpv6_prot = {
1532db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1533db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1534db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1535db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1536db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1537db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1538db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1539db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1540db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1541db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1542db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1543f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1544db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1545db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1546719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1547db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1548db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1549db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1550db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1551db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1552db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1553271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1554645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1555db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1556db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1557db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1558db8dac20SDavid S. Miller #endif
1559f77d6021SEric Dumazet 	.clear_sk	   = udp_v6_clear_sk,
1560db8dac20SDavid S. Miller };
1561db8dac20SDavid S. Miller 
1562db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1563db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1564db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1565db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1566db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1567db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1568db8dac20SDavid S. Miller };
1569db8dac20SDavid S. Miller 
1570db8dac20SDavid S. Miller int __init udpv6_init(void)
1571db8dac20SDavid S. Miller {
1572db8dac20SDavid S. Miller 	int ret;
1573db8dac20SDavid S. Miller 
15743336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
15753336288aSVlad Yasevich 	if (ret)
1576c6b641a4SVlad Yasevich 		goto out;
15773336288aSVlad Yasevich 
1578db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1579db8dac20SDavid S. Miller 	if (ret)
1580db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1581db8dac20SDavid S. Miller out:
1582db8dac20SDavid S. Miller 	return ret;
1583db8dac20SDavid S. Miller 
1584db8dac20SDavid S. Miller out_udpv6_protocol:
1585db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1586db8dac20SDavid S. Miller 	goto out;
1587db8dac20SDavid S. Miller }
1588db8dac20SDavid S. Miller 
1589db8dac20SDavid S. Miller void udpv6_exit(void)
1590db8dac20SDavid S. Miller {
1591db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1592db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1593db8dac20SDavid S. Miller }
1594