xref: /openbmc/linux/net/ipv6/udp.c (revision e36d3ff9)
1db8dac20SDavid S. Miller /*
2db8dac20SDavid S. Miller  *	UDP over IPv6
3db8dac20SDavid S. Miller  *	Linux INET6 implementation
4db8dac20SDavid S. Miller  *
5db8dac20SDavid S. Miller  *	Authors:
6db8dac20SDavid S. Miller  *	Pedro Roque		<roque@di.fc.ul.pt>
7db8dac20SDavid S. Miller  *
8db8dac20SDavid S. Miller  *	Based on linux/ipv4/udp.c
9db8dac20SDavid S. Miller  *
10db8dac20SDavid S. Miller  *	Fixes:
11db8dac20SDavid S. Miller  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
12db8dac20SDavid S. Miller  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
13db8dac20SDavid S. Miller  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
14db8dac20SDavid S. Miller  *					a single port at the same time.
15db8dac20SDavid S. Miller  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
16db8dac20SDavid S. Miller  *      YOSHIFUJI Hideaki @USAGI:	convert /proc/net/udp6 to seq_file.
17db8dac20SDavid S. Miller  *
18db8dac20SDavid S. Miller  *	This program is free software; you can redistribute it and/or
19db8dac20SDavid S. Miller  *      modify it under the terms of the GNU General Public License
20db8dac20SDavid S. Miller  *      as published by the Free Software Foundation; either version
21db8dac20SDavid S. Miller  *      2 of the License, or (at your option) any later version.
22db8dac20SDavid S. Miller  */
23db8dac20SDavid S. Miller 
24db8dac20SDavid S. Miller #include <linux/errno.h>
25db8dac20SDavid S. Miller #include <linux/types.h>
26db8dac20SDavid S. Miller #include <linux/socket.h>
27db8dac20SDavid S. Miller #include <linux/sockios.h>
28db8dac20SDavid S. Miller #include <linux/net.h>
29db8dac20SDavid S. Miller #include <linux/in6.h>
30db8dac20SDavid S. Miller #include <linux/netdevice.h>
31db8dac20SDavid S. Miller #include <linux/if_arp.h>
32db8dac20SDavid S. Miller #include <linux/ipv6.h>
33db8dac20SDavid S. Miller #include <linux/icmpv6.h>
34db8dac20SDavid S. Miller #include <linux/init.h>
35db8dac20SDavid S. Miller #include <linux/module.h>
36db8dac20SDavid S. Miller #include <linux/skbuff.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
38db8dac20SDavid S. Miller #include <asm/uaccess.h>
39db8dac20SDavid S. Miller 
40db8dac20SDavid S. Miller #include <net/ndisc.h>
41db8dac20SDavid S. Miller #include <net/protocol.h>
42db8dac20SDavid S. Miller #include <net/transp_v6.h>
43db8dac20SDavid S. Miller #include <net/ip6_route.h>
44db8dac20SDavid S. Miller #include <net/raw.h>
45db8dac20SDavid S. Miller #include <net/tcp_states.h>
46db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
47db8dac20SDavid S. Miller #include <net/xfrm.h>
4872289b96STom Herbert #include <net/inet6_hashtables.h>
49076bb0c8SEliezer Tamir #include <net/busy_poll.h>
50db8dac20SDavid S. Miller 
51db8dac20SDavid S. Miller #include <linux/proc_fs.h>
52db8dac20SDavid S. Miller #include <linux/seq_file.h>
5322911fc5SEric Dumazet #include <trace/events/skb.h>
54db8dac20SDavid S. Miller #include "udp_impl.h"
55db8dac20SDavid S. Miller 
56b2f5e7cdSVlad Yasevich int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
57b2f5e7cdSVlad Yasevich {
58b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
59b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
6068835abaSEric Dumazet 	__be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
6168835abaSEric Dumazet 	__be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
62b2f5e7cdSVlad Yasevich 	int sk_ipv6only = ipv6_only_sock(sk);
63b2f5e7cdSVlad Yasevich 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
64b2f5e7cdSVlad Yasevich 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
65b2f5e7cdSVlad Yasevich 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
66b2f5e7cdSVlad Yasevich 
67b2f5e7cdSVlad Yasevich 	/* if both are mapped, treat as IPv4 */
68b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
69499923c7SVlad Yasevich 		return (!sk2_ipv6only &&
70c720c7e8SEric Dumazet 			(!sk1_rcv_saddr || !sk2_rcv_saddr ||
71c720c7e8SEric Dumazet 			  sk1_rcv_saddr == sk2_rcv_saddr));
72b2f5e7cdSVlad Yasevich 
73b2f5e7cdSVlad Yasevich 	if (addr_type2 == IPV6_ADDR_ANY &&
74b2f5e7cdSVlad Yasevich 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
75b2f5e7cdSVlad Yasevich 		return 1;
76b2f5e7cdSVlad Yasevich 
77b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_ANY &&
78b2f5e7cdSVlad Yasevich 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
79b2f5e7cdSVlad Yasevich 		return 1;
80b2f5e7cdSVlad Yasevich 
81b2f5e7cdSVlad Yasevich 	if (sk2_rcv_saddr6 &&
82b2f5e7cdSVlad Yasevich 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
83b2f5e7cdSVlad Yasevich 		return 1;
84b2f5e7cdSVlad Yasevich 
85b2f5e7cdSVlad Yasevich 	return 0;
86b2f5e7cdSVlad Yasevich }
87b2f5e7cdSVlad Yasevich 
88d4cada4aSEric Dumazet static unsigned int udp6_portaddr_hash(struct net *net,
89d4cada4aSEric Dumazet 				       const struct in6_addr *addr6,
90d4cada4aSEric Dumazet 				       unsigned int port)
91d4cada4aSEric Dumazet {
92d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
93d4cada4aSEric Dumazet 
94d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
95d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
96856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
970eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
98d4cada4aSEric Dumazet 	else
990eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
100d4cada4aSEric Dumazet 
101d4cada4aSEric Dumazet 	return hash ^ port;
102d4cada4aSEric Dumazet }
103d4cada4aSEric Dumazet 
104d4cada4aSEric Dumazet 
1056ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
106db8dac20SDavid S. Miller {
10730fff923SEric Dumazet 	unsigned int hash2_nulladdr =
10830fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10930fff923SEric Dumazet 	unsigned int hash2_partial =
11030fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0);
11130fff923SEric Dumazet 
112d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
11330fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
11430fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
115db8dac20SDavid S. Miller }
116db8dac20SDavid S. Miller 
117719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
118719f8358SEric Dumazet {
119719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
120719f8358SEric Dumazet 					  &inet6_sk(sk)->rcv_saddr,
121719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
122719f8358SEric Dumazet 
123719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
124719f8358SEric Dumazet }
125719f8358SEric Dumazet 
126645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
127645ca708SEric Dumazet 				unsigned short hnum,
12888440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
12988440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
130645ca708SEric Dumazet 				int dif)
131db8dac20SDavid S. Miller {
132645ca708SEric Dumazet 	int score = -1;
133db8dac20SDavid S. Miller 
134d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
135db8dac20SDavid S. Miller 			sk->sk_family == PF_INET6) {
136db8dac20SDavid S. Miller 		struct ipv6_pinfo *np = inet6_sk(sk);
137645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
138645ca708SEric Dumazet 
139645ca708SEric Dumazet 		score = 0;
140c720c7e8SEric Dumazet 		if (inet->inet_dport) {
141c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
142645ca708SEric Dumazet 				return -1;
143db8dac20SDavid S. Miller 			score++;
144db8dac20SDavid S. Miller 		}
145db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->rcv_saddr)) {
146db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
147645ca708SEric Dumazet 				return -1;
148db8dac20SDavid S. Miller 			score++;
149db8dac20SDavid S. Miller 		}
150db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->daddr)) {
151db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->daddr, saddr))
152645ca708SEric Dumazet 				return -1;
153db8dac20SDavid S. Miller 			score++;
154db8dac20SDavid S. Miller 		}
155db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if) {
156db8dac20SDavid S. Miller 			if (sk->sk_bound_dev_if != dif)
157645ca708SEric Dumazet 				return -1;
158db8dac20SDavid S. Miller 			score++;
159db8dac20SDavid S. Miller 		}
160645ca708SEric Dumazet 	}
161645ca708SEric Dumazet 	return score;
162645ca708SEric Dumazet }
163645ca708SEric Dumazet 
164fddc17deSEric Dumazet #define SCORE2_MAX (1 + 1 + 1)
165fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
166fddc17deSEric Dumazet 				const struct in6_addr *saddr, __be16 sport,
167fddc17deSEric Dumazet 				const struct in6_addr *daddr, unsigned short hnum,
168fddc17deSEric Dumazet 				int dif)
169fddc17deSEric Dumazet {
170fddc17deSEric Dumazet 	int score = -1;
171fddc17deSEric Dumazet 
172fddc17deSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
173fddc17deSEric Dumazet 			sk->sk_family == PF_INET6) {
174fddc17deSEric Dumazet 		struct ipv6_pinfo *np = inet6_sk(sk);
175fddc17deSEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
176fddc17deSEric Dumazet 
177fddc17deSEric Dumazet 		if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
178fddc17deSEric Dumazet 			return -1;
179fddc17deSEric Dumazet 		score = 0;
180fddc17deSEric Dumazet 		if (inet->inet_dport) {
181fddc17deSEric Dumazet 			if (inet->inet_dport != sport)
182fddc17deSEric Dumazet 				return -1;
183fddc17deSEric Dumazet 			score++;
184fddc17deSEric Dumazet 		}
185fddc17deSEric Dumazet 		if (!ipv6_addr_any(&np->daddr)) {
186fddc17deSEric Dumazet 			if (!ipv6_addr_equal(&np->daddr, saddr))
187fddc17deSEric Dumazet 				return -1;
188fddc17deSEric Dumazet 			score++;
189fddc17deSEric Dumazet 		}
190fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if) {
191fddc17deSEric Dumazet 			if (sk->sk_bound_dev_if != dif)
192fddc17deSEric Dumazet 				return -1;
193fddc17deSEric Dumazet 			score++;
194fddc17deSEric Dumazet 		}
195fddc17deSEric Dumazet 	}
196fddc17deSEric Dumazet 	return score;
197fddc17deSEric Dumazet }
198fddc17deSEric Dumazet 
199fddc17deSEric Dumazet 
200fddc17deSEric Dumazet /* called with read_rcu_lock() */
201fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
202fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
203fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
204fddc17deSEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
205fddc17deSEric Dumazet {
206fddc17deSEric Dumazet 	struct sock *sk, *result;
207fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
20872289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
20972289b96STom Herbert 	u32 hash = 0;
210fddc17deSEric Dumazet 
211fddc17deSEric Dumazet begin:
212fddc17deSEric Dumazet 	result = NULL;
213fddc17deSEric Dumazet 	badness = -1;
214fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
215fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
216fddc17deSEric Dumazet 				      daddr, hnum, dif);
217fddc17deSEric Dumazet 		if (score > badness) {
218fddc17deSEric Dumazet 			result = sk;
219fddc17deSEric Dumazet 			badness = score;
22072289b96STom Herbert 			reuseport = sk->sk_reuseport;
22172289b96STom Herbert 			if (reuseport) {
22272289b96STom Herbert 				hash = inet6_ehashfn(net, daddr, hnum,
22372289b96STom Herbert 						     saddr, sport);
22472289b96STom Herbert 				matches = 1;
22572289b96STom Herbert 			} else if (score == SCORE2_MAX)
226fddc17deSEric Dumazet 				goto exact_match;
22772289b96STom Herbert 		} else if (score == badness && reuseport) {
22872289b96STom Herbert 			matches++;
22972289b96STom Herbert 			if (((u64)hash * matches) >> 32 == 0)
23072289b96STom Herbert 				result = sk;
23172289b96STom Herbert 			hash = next_pseudo_random32(hash);
232fddc17deSEric Dumazet 		}
233fddc17deSEric Dumazet 	}
234fddc17deSEric Dumazet 	/*
235fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
236fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
237fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
238fddc17deSEric Dumazet 	 */
239fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
240fddc17deSEric Dumazet 		goto begin;
241fddc17deSEric Dumazet 
242fddc17deSEric Dumazet 	if (result) {
243fddc17deSEric Dumazet exact_match:
244c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
245fddc17deSEric Dumazet 			result = NULL;
246fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
247fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
248fddc17deSEric Dumazet 			sock_put(result);
249fddc17deSEric Dumazet 			goto begin;
250fddc17deSEric Dumazet 		}
251fddc17deSEric Dumazet 	}
252fddc17deSEric Dumazet 	return result;
253fddc17deSEric Dumazet }
254fddc17deSEric Dumazet 
255fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
25688440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
25788440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
258645ca708SEric Dumazet 				      int dif, struct udp_table *udptable)
259645ca708SEric Dumazet {
260271b72c7SEric Dumazet 	struct sock *sk, *result;
26188ab1932SEric Dumazet 	struct hlist_nulls_node *node;
262645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
263fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
264fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
26572289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
26672289b96STom Herbert 	u32 hash = 0;
267645ca708SEric Dumazet 
268271b72c7SEric Dumazet 	rcu_read_lock();
269fddc17deSEric Dumazet 	if (hslot->count > 10) {
270fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
271fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
272fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
273fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
274fddc17deSEric Dumazet 			goto begin;
275fddc17deSEric Dumazet 
276fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
277fddc17deSEric Dumazet 					  daddr, hnum, dif,
278fddc17deSEric Dumazet 					  hslot2, slot2);
279fddc17deSEric Dumazet 		if (!result) {
280fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
281fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
282fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
283fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
284fddc17deSEric Dumazet 				goto begin;
285fddc17deSEric Dumazet 
2861223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
2871223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
288fddc17deSEric Dumazet 						  hslot2, slot2);
289fddc17deSEric Dumazet 		}
290fddc17deSEric Dumazet 		rcu_read_unlock();
291fddc17deSEric Dumazet 		return result;
292fddc17deSEric Dumazet 	}
293271b72c7SEric Dumazet begin:
294271b72c7SEric Dumazet 	result = NULL;
295271b72c7SEric Dumazet 	badness = -1;
29688ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
297645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
298645ca708SEric Dumazet 		if (score > badness) {
299db8dac20SDavid S. Miller 			result = sk;
300db8dac20SDavid S. Miller 			badness = score;
30172289b96STom Herbert 			reuseport = sk->sk_reuseport;
30272289b96STom Herbert 			if (reuseport) {
30372289b96STom Herbert 				hash = inet6_ehashfn(net, daddr, hnum,
30472289b96STom Herbert 						     saddr, sport);
30572289b96STom Herbert 				matches = 1;
30672289b96STom Herbert 			}
30772289b96STom Herbert 		} else if (score == badness && reuseport) {
30872289b96STom Herbert 			matches++;
30972289b96STom Herbert 			if (((u64)hash * matches) >> 32 == 0)
31072289b96STom Herbert 				result = sk;
31172289b96STom Herbert 			hash = next_pseudo_random32(hash);
312db8dac20SDavid S. Miller 		}
313db8dac20SDavid S. Miller 	}
31488ab1932SEric Dumazet 	/*
31588ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
31688ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
31788ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
31888ab1932SEric Dumazet 	 */
319fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
32088ab1932SEric Dumazet 		goto begin;
32188ab1932SEric Dumazet 
322271b72c7SEric Dumazet 	if (result) {
323c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
324271b72c7SEric Dumazet 			result = NULL;
325271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
326271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
327271b72c7SEric Dumazet 			sock_put(result);
328271b72c7SEric Dumazet 			goto begin;
329271b72c7SEric Dumazet 		}
330271b72c7SEric Dumazet 	}
331271b72c7SEric Dumazet 	rcu_read_unlock();
332db8dac20SDavid S. Miller 	return result;
333db8dac20SDavid S. Miller }
334fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
335db8dac20SDavid S. Miller 
336607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
337607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
338645ca708SEric Dumazet 					  struct udp_table *udptable)
339607c4aafSKOVACS Krisztian {
34023542618SKOVACS Krisztian 	struct sock *sk;
341b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
342607c4aafSKOVACS Krisztian 
34323542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
34423542618SKOVACS Krisztian 		return sk;
345adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
346607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
347607c4aafSKOVACS Krisztian 				 udptable);
348607c4aafSKOVACS Krisztian }
349607c4aafSKOVACS Krisztian 
350aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
351aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
352aa976fc0SBalazs Scheidler {
353aa976fc0SBalazs Scheidler 	return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
354aa976fc0SBalazs Scheidler }
355aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
356aa976fc0SBalazs Scheidler 
357aa976fc0SBalazs Scheidler 
358db8dac20SDavid S. Miller /*
359db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
360db8dac20SDavid S. Miller  * 	return it, otherwise we block.
361db8dac20SDavid S. Miller  */
362db8dac20SDavid S. Miller 
363db8dac20SDavid S. Miller int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
364db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len,
365db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
366db8dac20SDavid S. Miller {
367db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
368db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
369db8dac20SDavid S. Miller 	struct sk_buff *skb;
37059c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
3713f518bf7SPavel Emelyanov 	int peeked, off = 0;
372db8dac20SDavid S. Miller 	int err;
373db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
374f26ba175SWei Yongjun 	int is_udp4;
3758a74ad60SEric Dumazet 	bool slow;
376db8dac20SDavid S. Miller 
377db8dac20SDavid S. Miller 	if (addr_len)
378db8dac20SDavid S. Miller 		*addr_len = sizeof(struct sockaddr_in6);
379db8dac20SDavid S. Miller 
380db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
381db8dac20SDavid S. Miller 		return ipv6_recv_error(sk, msg, len);
382db8dac20SDavid S. Miller 
3834b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
3844b340ae2SBrian Haley 		return ipv6_recv_rxpmtu(sk, msg, len);
3854b340ae2SBrian Haley 
386db8dac20SDavid S. Miller try_again:
387db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
3883f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
389db8dac20SDavid S. Miller 	if (!skb)
390db8dac20SDavid S. Miller 		goto out;
391db8dac20SDavid S. Miller 
392db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
39359c2cdaeSDavid S. Miller 	copied = len;
39459c2cdaeSDavid S. Miller 	if (copied > ulen)
39559c2cdaeSDavid S. Miller 		copied = ulen;
39659c2cdaeSDavid S. Miller 	else if (copied < ulen)
397db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
398db8dac20SDavid S. Miller 
399f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
400f26ba175SWei Yongjun 
401db8dac20SDavid S. Miller 	/*
402db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
403db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
404db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
405db8dac20SDavid S. Miller 	 */
406db8dac20SDavid S. Miller 
40759c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
408db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
409db8dac20SDavid S. Miller 			goto csum_copy_err;
410db8dac20SDavid S. Miller 	}
411db8dac20SDavid S. Miller 
412db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
413db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
41459c2cdaeSDavid S. Miller 					      msg->msg_iov, copied);
415db8dac20SDavid S. Miller 	else {
416db8dac20SDavid S. Miller 		err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
417db8dac20SDavid S. Miller 		if (err == -EINVAL)
418db8dac20SDavid S. Miller 			goto csum_copy_err;
419db8dac20SDavid S. Miller 	}
42022911fc5SEric Dumazet 	if (unlikely(err)) {
42122911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
422979402b1SEric Dumazet 		if (!peeked) {
423979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
424979402b1SEric Dumazet 			if (is_udp4)
425979402b1SEric Dumazet 				UDP_INC_STATS_USER(sock_net(sk),
426979402b1SEric Dumazet 						   UDP_MIB_INERRORS,
427979402b1SEric Dumazet 						   is_udplite);
428979402b1SEric Dumazet 			else
429979402b1SEric Dumazet 				UDP6_INC_STATS_USER(sock_net(sk),
430979402b1SEric Dumazet 						    UDP_MIB_INERRORS,
431979402b1SEric Dumazet 						    is_udplite);
432979402b1SEric Dumazet 		}
433db8dac20SDavid S. Miller 		goto out_free;
43422911fc5SEric Dumazet 	}
435f26ba175SWei Yongjun 	if (!peeked) {
436f26ba175SWei Yongjun 		if (is_udp4)
437f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
438f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
439f26ba175SWei Yongjun 		else
440235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
441235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
442f26ba175SWei Yongjun 	}
443db8dac20SDavid S. Miller 
4443b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
445db8dac20SDavid S. Miller 
446db8dac20SDavid S. Miller 	/* Copy the address. */
447db8dac20SDavid S. Miller 	if (msg->msg_name) {
448db8dac20SDavid S. Miller 		struct sockaddr_in6 *sin6;
449db8dac20SDavid S. Miller 
450db8dac20SDavid S. Miller 		sin6 = (struct sockaddr_in6 *) msg->msg_name;
451db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
452db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
453db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
454db8dac20SDavid S. Miller 
455842df073SHannes Frederic Sowa 		if (is_udp4) {
456b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
457b301e82cSBrian Haley 					       &sin6->sin6_addr);
458842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
459842df073SHannes Frederic Sowa 		} else {
4604e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
461842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
462842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
463842df073SHannes Frederic Sowa 						    IP6CB(skb)->iif);
464db8dac20SDavid S. Miller 		}
465db8dac20SDavid S. Miller 
466db8dac20SDavid S. Miller 	}
467f26ba175SWei Yongjun 	if (is_udp4) {
468db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
469db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
470db8dac20SDavid S. Miller 	} else {
471db8dac20SDavid S. Miller 		if (np->rxopt.all)
47273df66f8STom Parkin 			ip6_datagram_recv_ctl(sk, msg, skb);
473db8dac20SDavid S. Miller 	}
474db8dac20SDavid S. Miller 
47559c2cdaeSDavid S. Miller 	err = copied;
476db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
477db8dac20SDavid S. Miller 		err = ulen;
478db8dac20SDavid S. Miller 
479db8dac20SDavid S. Miller out_free:
4809d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
481db8dac20SDavid S. Miller out:
482db8dac20SDavid S. Miller 	return err;
483db8dac20SDavid S. Miller 
484db8dac20SDavid S. Miller csum_copy_err:
4858a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
4860856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
4876a5dc9e5SEric Dumazet 		if (is_udp4) {
4886a5dc9e5SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
4896a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
4900856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
4910856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4926a5dc9e5SEric Dumazet 		} else {
4936a5dc9e5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
4946a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
4950856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
4960856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4970856f939SWei Yongjun 		}
4986a5dc9e5SEric Dumazet 	}
4998a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
500db8dac20SDavid S. Miller 
50132c90254SXufeng Zhang 	if (noblock)
502db8dac20SDavid S. Miller 		return -EAGAIN;
5039cfaa8deSXufeng Zhang 
5049cfaa8deSXufeng Zhang 	/* starting over for a new packet */
5059cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
506db8dac20SDavid S. Miller 	goto try_again;
507db8dac20SDavid S. Miller }
508db8dac20SDavid S. Miller 
509db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
510d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
511645ca708SEric Dumazet 		    struct udp_table *udptable)
512db8dac20SDavid S. Miller {
513db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
514b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
515b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
516b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
517db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr*)(skb->data+offset);
518db8dac20SDavid S. Miller 	struct sock *sk;
519db8dac20SDavid S. Miller 	int err;
520db8dac20SDavid S. Miller 
521c346dca1SYOSHIFUJI Hideaki 	sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
522db8dac20SDavid S. Miller 			       saddr, uh->source, inet6_iif(skb), udptable);
523db8dac20SDavid S. Miller 	if (sk == NULL)
524db8dac20SDavid S. Miller 		return;
525db8dac20SDavid S. Miller 
52681aded24SDavid S. Miller 	if (type == ICMPV6_PKT_TOOBIG)
52781aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
5281a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
529ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
5301a462d18SDuan Jiong 		goto out;
5311a462d18SDuan Jiong 	}
53281aded24SDavid S. Miller 
533db8dac20SDavid S. Miller 	np = inet6_sk(sk);
534db8dac20SDavid S. Miller 
535db8dac20SDavid S. Miller 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
536db8dac20SDavid S. Miller 		goto out;
537db8dac20SDavid S. Miller 
538db8dac20SDavid S. Miller 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
539db8dac20SDavid S. Miller 		goto out;
540db8dac20SDavid S. Miller 
541b1faf566SEric Dumazet 	if (np->recverr)
542db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
543b1faf566SEric Dumazet 
544db8dac20SDavid S. Miller 	sk->sk_err = err;
545db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
546db8dac20SDavid S. Miller out:
547db8dac20SDavid S. Miller 	sock_put(sk);
548db8dac20SDavid S. Miller }
549db8dac20SDavid S. Miller 
550f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
551f7ad74feSBenjamin LaHaise {
552f7ad74feSBenjamin LaHaise 	int rc;
553f7ad74feSBenjamin LaHaise 
554f7ad74feSBenjamin LaHaise 	if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
555f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
556f7ad74feSBenjamin LaHaise 
557f7ad74feSBenjamin LaHaise 	rc = sock_queue_rcv_skb(sk, skb);
558f7ad74feSBenjamin LaHaise 	if (rc < 0) {
559f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
560f7ad74feSBenjamin LaHaise 
561f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
562f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
563f7ad74feSBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk),
564f7ad74feSBenjamin LaHaise 					UDP_MIB_RCVBUFERRORS, is_udplite);
565f7ad74feSBenjamin LaHaise 		UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
566f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
567f7ad74feSBenjamin LaHaise 		return -1;
568f7ad74feSBenjamin LaHaise 	}
569f7ad74feSBenjamin LaHaise 	return 0;
570f7ad74feSBenjamin LaHaise }
571f7ad74feSBenjamin LaHaise 
572db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
573d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
574d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info     )
575db8dac20SDavid S. Miller {
576645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
577db8dac20SDavid S. Miller }
578db8dac20SDavid S. Miller 
579d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
580d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
581d7f3f621SBenjamin LaHaise {
582d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
583d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
584d7f3f621SBenjamin LaHaise }
585d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
586d7f3f621SBenjamin LaHaise 
587db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
588db8dac20SDavid S. Miller {
589db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
590db8dac20SDavid S. Miller 	int rc;
591db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
592db8dac20SDavid S. Miller 
593db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
594db8dac20SDavid S. Miller 		goto drop;
595db8dac20SDavid S. Miller 
596d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
597d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
598d7f3f621SBenjamin LaHaise 
599d7f3f621SBenjamin LaHaise 		/*
600d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
601d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
602d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
603d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
604d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
605d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
606d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
607d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
608d7f3f621SBenjamin LaHaise 		 */
609d7f3f621SBenjamin LaHaise 
610d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
611d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
612d7f3f621SBenjamin LaHaise 		if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
613d7f3f621SBenjamin LaHaise 			int ret;
614d7f3f621SBenjamin LaHaise 
615d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
616d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
617d7f3f621SBenjamin LaHaise 				UDP_INC_STATS_BH(sock_net(sk),
618d7f3f621SBenjamin LaHaise 						 UDP_MIB_INDATAGRAMS,
619d7f3f621SBenjamin LaHaise 						 is_udplite);
620d7f3f621SBenjamin LaHaise 				return -ret;
621d7f3f621SBenjamin LaHaise 			}
622d7f3f621SBenjamin LaHaise 		}
623d7f3f621SBenjamin LaHaise 
624d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
625d7f3f621SBenjamin LaHaise 	}
626d7f3f621SBenjamin LaHaise 
627db8dac20SDavid S. Miller 	/*
628db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
629db8dac20SDavid S. Miller 	 */
630db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
631db8dac20SDavid S. Miller 
632db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
633db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
634db8dac20SDavid S. Miller 				" %d while full coverage %d requested\n",
635db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, skb->len);
636db8dac20SDavid S. Miller 			goto drop;
637db8dac20SDavid S. Miller 		}
638db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
639db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
640db8dac20SDavid S. Miller 						    "too small, need min %d\n",
641db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
642db8dac20SDavid S. Miller 			goto drop;
643db8dac20SDavid S. Miller 		}
644db8dac20SDavid S. Miller 	}
645db8dac20SDavid S. Miller 
64633d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
647db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
6486a5dc9e5SEric Dumazet 			goto csum_error;
649db8dac20SDavid S. Miller 	}
650db8dac20SDavid S. Miller 
651cb80ef46SBenjamin LaHaise 	if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf))
652cb80ef46SBenjamin LaHaise 		goto drop;
653cb80ef46SBenjamin LaHaise 
654d826eb14SEric Dumazet 	skb_dst_drop(skb);
655db8dac20SDavid S. Miller 
656cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
657cb80ef46SBenjamin LaHaise 	rc = 0;
658cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
659f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
660cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
661cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
662cb80ef46SBenjamin LaHaise 		goto drop;
663cb80ef46SBenjamin LaHaise 	}
664cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
665f7ad74feSBenjamin LaHaise 
666f7ad74feSBenjamin LaHaise 	return rc;
6676a5dc9e5SEric Dumazet csum_error:
6686a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
669db8dac20SDavid S. Miller drop:
670ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
671cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
672db8dac20SDavid S. Miller 	kfree_skb(skb);
673db8dac20SDavid S. Miller 	return -1;
674db8dac20SDavid S. Miller }
675db8dac20SDavid S. Miller 
676920a4611SEric Dumazet static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
677b71d1d42SEric Dumazet 				      __be16 loc_port, const struct in6_addr *loc_addr,
678b71d1d42SEric Dumazet 				      __be16 rmt_port, const struct in6_addr *rmt_addr,
679db8dac20SDavid S. Miller 				      int dif)
680db8dac20SDavid S. Miller {
68188ab1932SEric Dumazet 	struct hlist_nulls_node *node;
682db8dac20SDavid S. Miller 	struct sock *s = sk;
683db8dac20SDavid S. Miller 	unsigned short num = ntohs(loc_port);
684db8dac20SDavid S. Miller 
68588ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
686db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
687db8dac20SDavid S. Miller 
688920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net))
689b8ad0cbcSDaniel Lezcano 			continue;
690b8ad0cbcSDaniel Lezcano 
691d4cada4aSEric Dumazet 		if (udp_sk(s)->udp_port_hash == num &&
692d4cada4aSEric Dumazet 		    s->sk_family == PF_INET6) {
693db8dac20SDavid S. Miller 			struct ipv6_pinfo *np = inet6_sk(s);
694c720c7e8SEric Dumazet 			if (inet->inet_dport) {
695c720c7e8SEric Dumazet 				if (inet->inet_dport != rmt_port)
696db8dac20SDavid S. Miller 					continue;
697db8dac20SDavid S. Miller 			}
698db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->daddr) &&
699db8dac20SDavid S. Miller 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
700db8dac20SDavid S. Miller 				continue;
701db8dac20SDavid S. Miller 
702db8dac20SDavid S. Miller 			if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
703db8dac20SDavid S. Miller 				continue;
704db8dac20SDavid S. Miller 
705db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->rcv_saddr)) {
706db8dac20SDavid S. Miller 				if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
707db8dac20SDavid S. Miller 					continue;
708db8dac20SDavid S. Miller 			}
709db8dac20SDavid S. Miller 			if (!inet6_mc_check(s, loc_addr, rmt_addr))
710db8dac20SDavid S. Miller 				continue;
711db8dac20SDavid S. Miller 			return s;
712db8dac20SDavid S. Miller 		}
713db8dac20SDavid S. Miller 	}
714db8dac20SDavid S. Miller 	return NULL;
715db8dac20SDavid S. Miller }
716db8dac20SDavid S. Miller 
717a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
718a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
719a1ab77f9SEric Dumazet {
720cb80ef46SBenjamin LaHaise 	struct sk_buff *skb1 = NULL;
721a1ab77f9SEric Dumazet 	struct sock *sk;
722cb80ef46SBenjamin LaHaise 	unsigned int i;
723a1ab77f9SEric Dumazet 
724a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
725a1ab77f9SEric Dumazet 		sk = stack[i];
726cb80ef46SBenjamin LaHaise 		if (likely(skb1 == NULL))
727cb80ef46SBenjamin LaHaise 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
728cb80ef46SBenjamin LaHaise 		if (!skb1) {
729f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
730cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
731cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
732cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
733cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
734a1ab77f9SEric Dumazet 		}
735cb80ef46SBenjamin LaHaise 
736cb80ef46SBenjamin LaHaise 		if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
737cb80ef46SBenjamin LaHaise 			skb1 = NULL;
738cb80ef46SBenjamin LaHaise 	}
739cb80ef46SBenjamin LaHaise 	if (unlikely(skb1))
740cb80ef46SBenjamin LaHaise 		kfree_skb(skb1);
741a1ab77f9SEric Dumazet }
742db8dac20SDavid S. Miller /*
743db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
744db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
745db8dac20SDavid S. Miller  */
746e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
747b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
748645ca708SEric Dumazet 		struct udp_table *udptable)
749db8dac20SDavid S. Miller {
750a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
751db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
752f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
753db8dac20SDavid S. Miller 	int dif;
754a1ab77f9SEric Dumazet 	unsigned int i, count = 0;
755db8dac20SDavid S. Miller 
756645ca708SEric Dumazet 	spin_lock(&hslot->lock);
75788ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
758db8dac20SDavid S. Miller 	dif = inet6_iif(skb);
759920a4611SEric Dumazet 	sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
760a1ab77f9SEric Dumazet 	while (sk) {
761a1ab77f9SEric Dumazet 		stack[count++] = sk;
762a1ab77f9SEric Dumazet 		sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
763a1ab77f9SEric Dumazet 				       uh->source, saddr, dif);
764a1ab77f9SEric Dumazet 		if (unlikely(count == ARRAY_SIZE(stack))) {
765a1ab77f9SEric Dumazet 			if (!sk)
766a1ab77f9SEric Dumazet 				break;
767a1ab77f9SEric Dumazet 			flush_stack(stack, count, skb, ~0);
768a1ab77f9SEric Dumazet 			count = 0;
769db8dac20SDavid S. Miller 		}
770a1ab77f9SEric Dumazet 	}
771a1ab77f9SEric Dumazet 	/*
772a1ab77f9SEric Dumazet 	 * before releasing the lock, we must take reference on sockets
773a1ab77f9SEric Dumazet 	 */
774a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++)
775a1ab77f9SEric Dumazet 		sock_hold(stack[i]);
776db8dac20SDavid S. Miller 
777645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
778a1ab77f9SEric Dumazet 
779a1ab77f9SEric Dumazet 	if (count) {
780a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
781a1ab77f9SEric Dumazet 
782a1ab77f9SEric Dumazet 		for (i = 0; i < count; i++)
783a1ab77f9SEric Dumazet 			sock_put(stack[i]);
784a1ab77f9SEric Dumazet 	} else {
785a1ab77f9SEric Dumazet 		kfree_skb(skb);
786a1ab77f9SEric Dumazet 	}
787db8dac20SDavid S. Miller 	return 0;
788db8dac20SDavid S. Miller }
789db8dac20SDavid S. Miller 
790645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
791db8dac20SDavid S. Miller 		   int proto)
792db8dac20SDavid S. Miller {
7933ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
794db8dac20SDavid S. Miller 	struct sock *sk;
795db8dac20SDavid S. Miller 	struct udphdr *uh;
796b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
797db8dac20SDavid S. Miller 	u32 ulen = 0;
798db8dac20SDavid S. Miller 
799db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
800d6bc0149SBjørn Mork 		goto discard;
801db8dac20SDavid S. Miller 
802db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
803db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
804db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
805db8dac20SDavid S. Miller 
806db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
807db8dac20SDavid S. Miller 	if (ulen > skb->len)
808db8dac20SDavid S. Miller 		goto short_packet;
809db8dac20SDavid S. Miller 
810db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
811db8dac20SDavid S. Miller 		/* UDP validates ulen. */
812db8dac20SDavid S. Miller 
813db8dac20SDavid S. Miller 		/* Check for jumbo payload */
814db8dac20SDavid S. Miller 		if (ulen == 0)
815db8dac20SDavid S. Miller 			ulen = skb->len;
816db8dac20SDavid S. Miller 
817db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
818db8dac20SDavid S. Miller 			goto short_packet;
819db8dac20SDavid S. Miller 
820db8dac20SDavid S. Miller 		if (ulen < skb->len) {
821db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
822db8dac20SDavid S. Miller 				goto short_packet;
823db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
824db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
825db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
826db8dac20SDavid S. Miller 		}
827db8dac20SDavid S. Miller 	}
828db8dac20SDavid S. Miller 
829db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
8306a5dc9e5SEric Dumazet 		goto csum_error;
831db8dac20SDavid S. Miller 
832db8dac20SDavid S. Miller 	/*
833db8dac20SDavid S. Miller 	 *	Multicast receive code
834db8dac20SDavid S. Miller 	 */
835db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
836e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
837e3163493SPavel Emelyanov 				saddr, daddr, udptable);
838db8dac20SDavid S. Miller 
839db8dac20SDavid S. Miller 	/* Unicast */
840db8dac20SDavid S. Miller 
841db8dac20SDavid S. Miller 	/*
842db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
843db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
844db8dac20SDavid S. Miller 	 */
845607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
846cb80ef46SBenjamin LaHaise 	if (sk != NULL) {
847a5b50476SEliezer Tamir 		int ret;
848a5b50476SEliezer Tamir 
8498b80cda5SEliezer Tamir 		sk_mark_napi_id(sk, skb);
850a5b50476SEliezer Tamir 		ret = udpv6_queue_rcv_skb(sk, skb);
851cb80ef46SBenjamin LaHaise 		sock_put(sk);
852db8dac20SDavid S. Miller 
853cb80ef46SBenjamin LaHaise 		/* a return value > 0 means to resubmit the input, but
854cb80ef46SBenjamin LaHaise 		 * it wants the return to be -protocol, or 0
855cb80ef46SBenjamin LaHaise 		 */
856cb80ef46SBenjamin LaHaise 		if (ret > 0)
857cb80ef46SBenjamin LaHaise 			return -ret;
858cb80ef46SBenjamin LaHaise 
859cb80ef46SBenjamin LaHaise 		return 0;
860cb80ef46SBenjamin LaHaise 	}
861cb80ef46SBenjamin LaHaise 
862db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
863db8dac20SDavid S. Miller 		goto discard;
864db8dac20SDavid S. Miller 
865db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
8666a5dc9e5SEric Dumazet 		goto csum_error;
867db8dac20SDavid S. Miller 
868cb80ef46SBenjamin LaHaise 	UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
8693ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
870db8dac20SDavid S. Miller 
871db8dac20SDavid S. Miller 	kfree_skb(skb);
872db8dac20SDavid S. Miller 	return 0;
873db8dac20SDavid S. Miller 
874db8dac20SDavid S. Miller short_packet:
875d6bc0149SBjørn Mork 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
876db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
877d6bc0149SBjørn Mork 		       saddr,
878d6bc0149SBjørn Mork 		       ntohs(uh->source),
879d6bc0149SBjørn Mork 		       ulen,
880d6bc0149SBjørn Mork 		       skb->len,
881d6bc0149SBjørn Mork 		       daddr,
882d6bc0149SBjørn Mork 		       ntohs(uh->dest));
8836a5dc9e5SEric Dumazet 	goto discard;
8846a5dc9e5SEric Dumazet csum_error:
8856a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
886db8dac20SDavid S. Miller discard:
887ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
888db8dac20SDavid S. Miller 	kfree_skb(skb);
889db8dac20SDavid S. Miller 	return 0;
890db8dac20SDavid S. Miller }
891db8dac20SDavid S. Miller 
892db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
893db8dac20SDavid S. Miller {
894645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
895db8dac20SDavid S. Miller }
896db8dac20SDavid S. Miller 
897db8dac20SDavid S. Miller /*
898db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
899db8dac20SDavid S. Miller  */
900db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
901db8dac20SDavid S. Miller {
902db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
903db8dac20SDavid S. Miller 
90436d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
90536d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
90636d926b9SDenis V. Lunev 	else if (up->pending) {
907db8dac20SDavid S. Miller 		up->len = 0;
908db8dac20SDavid S. Miller 		up->pending = 0;
909db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
910db8dac20SDavid S. Miller 	}
911db8dac20SDavid S. Miller }
912db8dac20SDavid S. Miller 
913493c6be3SSridhar Samudrala /**
914493c6be3SSridhar Samudrala  * 	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
915493c6be3SSridhar Samudrala  * 	@sk: 	socket we are sending on
916493c6be3SSridhar Samudrala  * 	@skb: 	sk_buff containing the filled-in UDP header
917493c6be3SSridhar Samudrala  * 	        (checksum field must be zeroed out)
918493c6be3SSridhar Samudrala  */
919493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
920493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
921493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
922493c6be3SSridhar Samudrala {
923493c6be3SSridhar Samudrala 	unsigned int offset;
924493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
925493c6be3SSridhar Samudrala 	__wsum csum = 0;
926493c6be3SSridhar Samudrala 
927493c6be3SSridhar Samudrala 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
928493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
929493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
930493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
931493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
932493c6be3SSridhar Samudrala 	} else {
933493c6be3SSridhar Samudrala 		/*
934493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
935493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
936493c6be3SSridhar Samudrala 		 * should be together
937493c6be3SSridhar Samudrala 		 */
938493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
939493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
940493c6be3SSridhar Samudrala 
941493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
942493c6be3SSridhar Samudrala 
943493c6be3SSridhar Samudrala 		skb_queue_walk(&sk->sk_write_queue, skb) {
944493c6be3SSridhar Samudrala 			csum = csum_add(csum, skb->csum);
945493c6be3SSridhar Samudrala 		}
946493c6be3SSridhar Samudrala 
947493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
948493c6be3SSridhar Samudrala 					    csum);
949493c6be3SSridhar Samudrala 		if (uh->check == 0)
950493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
951493c6be3SSridhar Samudrala 	}
952493c6be3SSridhar Samudrala }
953493c6be3SSridhar Samudrala 
954db8dac20SDavid S. Miller /*
955db8dac20SDavid S. Miller  *	Sending
956db8dac20SDavid S. Miller  */
957db8dac20SDavid S. Miller 
958db8dac20SDavid S. Miller static int udp_v6_push_pending_frames(struct sock *sk)
959db8dac20SDavid S. Miller {
960db8dac20SDavid S. Miller 	struct sk_buff *skb;
961db8dac20SDavid S. Miller 	struct udphdr *uh;
962db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
963db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
9648822b64aSHannes Frederic Sowa 	struct flowi6 *fl6;
965db8dac20SDavid S. Miller 	int err = 0;
966db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
967db8dac20SDavid S. Miller 	__wsum csum = 0;
968db8dac20SDavid S. Miller 
9698822b64aSHannes Frederic Sowa 	if (up->pending == AF_INET)
9708822b64aSHannes Frederic Sowa 		return udp_push_pending_frames(sk);
9718822b64aSHannes Frederic Sowa 
9728822b64aSHannes Frederic Sowa 	fl6 = &inet->cork.fl.u.ip6;
9738822b64aSHannes Frederic Sowa 
974db8dac20SDavid S. Miller 	/* Grab the skbuff where UDP header space exists. */
975db8dac20SDavid S. Miller 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
976db8dac20SDavid S. Miller 		goto out;
977db8dac20SDavid S. Miller 
978db8dac20SDavid S. Miller 	/*
979db8dac20SDavid S. Miller 	 * Create a UDP header
980db8dac20SDavid S. Miller 	 */
981db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
9821958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
9831958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
984db8dac20SDavid S. Miller 	uh->len = htons(up->len);
985db8dac20SDavid S. Miller 	uh->check = 0;
986db8dac20SDavid S. Miller 
987db8dac20SDavid S. Miller 	if (is_udplite)
988db8dac20SDavid S. Miller 		csum = udplite_csum_outgoing(sk, skb);
989493c6be3SSridhar Samudrala 	else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
9904c9483b2SDavid S. Miller 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
991493c6be3SSridhar Samudrala 				     up->len);
992493c6be3SSridhar Samudrala 		goto send;
993493c6be3SSridhar Samudrala 	} else
994db8dac20SDavid S. Miller 		csum = udp_csum_outgoing(sk, skb);
995db8dac20SDavid S. Miller 
996db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
9974c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
9984c9483b2SDavid S. Miller 				    up->len, fl6->flowi6_proto, csum);
999db8dac20SDavid S. Miller 	if (uh->check == 0)
1000db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1001db8dac20SDavid S. Miller 
1002493c6be3SSridhar Samudrala send:
1003db8dac20SDavid S. Miller 	err = ip6_push_pending_frames(sk);
10046ce9e7b5SEric Dumazet 	if (err) {
10056ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
10066ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
10076ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
10086ce9e7b5SEric Dumazet 			err = 0;
10096ce9e7b5SEric Dumazet 		}
10106ce9e7b5SEric Dumazet 	} else
10116ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
10126ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
1013db8dac20SDavid S. Miller out:
1014db8dac20SDavid S. Miller 	up->len = 0;
1015db8dac20SDavid S. Miller 	up->pending = 0;
1016db8dac20SDavid S. Miller 	return err;
1017db8dac20SDavid S. Miller }
1018db8dac20SDavid S. Miller 
1019db8dac20SDavid S. Miller int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
1020db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len)
1021db8dac20SDavid S. Miller {
1022db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1023db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1024db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1025db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1026db8dac20SDavid S. Miller 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
102720c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1028db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
1029db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
10304c9483b2SDavid S. Miller 	struct flowi6 fl6;
1031db8dac20SDavid S. Miller 	struct dst_entry *dst;
1032db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1033db8dac20SDavid S. Miller 	int ulen = len;
1034db8dac20SDavid S. Miller 	int hlimit = -1;
1035db8dac20SDavid S. Miller 	int tclass = -1;
103613b52cd4SBrian Haley 	int dontfrag = -1;
1037db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1038db8dac20SDavid S. Miller 	int err;
1039db8dac20SDavid S. Miller 	int connected = 0;
1040db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1041db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1042db8dac20SDavid S. Miller 
1043db8dac20SDavid S. Miller 	/* destination address check */
1044db8dac20SDavid S. Miller 	if (sin6) {
1045db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1046db8dac20SDavid S. Miller 			return -EINVAL;
1047db8dac20SDavid S. Miller 
1048db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1049db8dac20SDavid S. Miller 		case AF_INET6:
1050db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1051db8dac20SDavid S. Miller 				return -EINVAL;
1052db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1053db8dac20SDavid S. Miller 			break;
1054db8dac20SDavid S. Miller 		case AF_INET:
1055db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1056db8dac20SDavid S. Miller 		case AF_UNSPEC:
1057db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1058db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1059db8dac20SDavid S. Miller 			daddr = NULL;
1060db8dac20SDavid S. Miller 			break;
1061db8dac20SDavid S. Miller 		default:
1062db8dac20SDavid S. Miller 			return -EINVAL;
1063db8dac20SDavid S. Miller 		}
1064db8dac20SDavid S. Miller 	} else if (!up->pending) {
1065db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1066db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1067db8dac20SDavid S. Miller 		daddr = &np->daddr;
1068db8dac20SDavid S. Miller 	} else
1069db8dac20SDavid S. Miller 		daddr = NULL;
1070db8dac20SDavid S. Miller 
1071db8dac20SDavid S. Miller 	if (daddr) {
1072db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1073db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1074db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1075c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1076db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1077db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1078db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1079db8dac20SDavid S. Miller do_udp_sendmsg:
1080db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1081db8dac20SDavid S. Miller 				return -ENETUNREACH;
1082db8dac20SDavid S. Miller 			return udp_sendmsg(iocb, sk, msg, len);
1083db8dac20SDavid S. Miller 		}
1084db8dac20SDavid S. Miller 	}
1085db8dac20SDavid S. Miller 
1086db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
1087db8dac20SDavid S. Miller 		return udp_sendmsg(iocb, sk, msg, len);
1088db8dac20SDavid S. Miller 
1089db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1090db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1091db8dac20SDavid S. Miller 	   */
1092db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1093db8dac20SDavid S. Miller 		return -EMSGSIZE;
1094db8dac20SDavid S. Miller 
1095db8dac20SDavid S. Miller 	if (up->pending) {
1096db8dac20SDavid S. Miller 		/*
1097db8dac20SDavid S. Miller 		 * There are pending frames.
1098db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1099db8dac20SDavid S. Miller 		 */
1100db8dac20SDavid S. Miller 		lock_sock(sk);
1101db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1102db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1103db8dac20SDavid S. Miller 				release_sock(sk);
1104db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1105db8dac20SDavid S. Miller 			}
1106db8dac20SDavid S. Miller 			dst = NULL;
1107db8dac20SDavid S. Miller 			goto do_append_data;
1108db8dac20SDavid S. Miller 		}
1109db8dac20SDavid S. Miller 		release_sock(sk);
1110db8dac20SDavid S. Miller 	}
1111db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1112db8dac20SDavid S. Miller 
11134c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1114db8dac20SDavid S. Miller 
1115db8dac20SDavid S. Miller 	if (sin6) {
1116db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1117db8dac20SDavid S. Miller 			return -EINVAL;
1118db8dac20SDavid S. Miller 
11191958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1120db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1121db8dac20SDavid S. Miller 
1122db8dac20SDavid S. Miller 		if (np->sndflow) {
11234c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
11244c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
11254c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1126db8dac20SDavid S. Miller 				if (flowlabel == NULL)
1127db8dac20SDavid S. Miller 					return -EINVAL;
1128db8dac20SDavid S. Miller 				daddr = &flowlabel->dst;
1129db8dac20SDavid S. Miller 			}
1130db8dac20SDavid S. Miller 		}
1131db8dac20SDavid S. Miller 
1132db8dac20SDavid S. Miller 		/*
1133db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1134db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1135db8dac20SDavid S. Miller 		 */
1136db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1137db8dac20SDavid S. Miller 		    ipv6_addr_equal(daddr, &np->daddr))
1138db8dac20SDavid S. Miller 			daddr = &np->daddr;
1139db8dac20SDavid S. Miller 
1140db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1141db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1142842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
11434c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1144db8dac20SDavid S. Miller 	} else {
1145db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1146db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1147db8dac20SDavid S. Miller 
11481958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1149db8dac20SDavid S. Miller 		daddr = &np->daddr;
11504c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1151db8dac20SDavid S. Miller 		connected = 1;
1152db8dac20SDavid S. Miller 	}
1153db8dac20SDavid S. Miller 
11544c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11554c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1156db8dac20SDavid S. Miller 
11574c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11584c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
11599f690db7SYang Hongyang 
11604c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
116151953d5bSBrian Haley 
1162db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1163db8dac20SDavid S. Miller 		opt = &opt_space;
1164db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1165db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1166db8dac20SDavid S. Miller 
116773df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1168ec0506dbSMaciej Żenczykowski 					    &hlimit, &tclass, &dontfrag);
1169db8dac20SDavid S. Miller 		if (err < 0) {
1170db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1171db8dac20SDavid S. Miller 			return err;
1172db8dac20SDavid S. Miller 		}
11734c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
11744c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1175db8dac20SDavid S. Miller 			if (flowlabel == NULL)
1176db8dac20SDavid S. Miller 				return -EINVAL;
1177db8dac20SDavid S. Miller 		}
1178db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1179db8dac20SDavid S. Miller 			opt = NULL;
1180db8dac20SDavid S. Miller 		connected = 0;
1181db8dac20SDavid S. Miller 	}
1182db8dac20SDavid S. Miller 	if (opt == NULL)
1183db8dac20SDavid S. Miller 		opt = np->opt;
1184db8dac20SDavid S. Miller 	if (flowlabel)
1185db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1186db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1187db8dac20SDavid S. Miller 
11884c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1189876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
11904e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1191876c7f41SBrian Haley 	else
11924c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
11934c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
11944e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
11951958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1196db8dac20SDavid S. Miller 
11974c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
119820c59de2SArnaud Ebalard 	if (final_p)
1199db8dac20SDavid S. Miller 		connected = 0;
1200db8dac20SDavid S. Miller 
12014c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
12024c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1203db8dac20SDavid S. Miller 		connected = 0;
1204c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1205c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1206db8dac20SDavid S. Miller 
12074c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1208db8dac20SDavid S. Miller 
12094c9483b2SDavid S. Miller 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
121068d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
121168d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
121268d0c6d3SDavid S. Miller 		dst = NULL;
1213db8dac20SDavid S. Miller 		goto out;
1214db8dac20SDavid S. Miller 	}
1215db8dac20SDavid S. Miller 
1216db8dac20SDavid S. Miller 	if (hlimit < 0) {
12174c9483b2SDavid S. Miller 		if (ipv6_addr_is_multicast(&fl6.daddr))
1218db8dac20SDavid S. Miller 			hlimit = np->mcast_hops;
1219db8dac20SDavid S. Miller 		else
1220db8dac20SDavid S. Miller 			hlimit = np->hop_limit;
1221db8dac20SDavid S. Miller 		if (hlimit < 0)
12226b75d090SYOSHIFUJI Hideaki 			hlimit = ip6_dst_hoplimit(dst);
1223db8dac20SDavid S. Miller 	}
1224db8dac20SDavid S. Miller 
1225db8dac20SDavid S. Miller 	if (tclass < 0)
1226e651f03aSGerrit Renker 		tclass = np->tclass;
1227db8dac20SDavid S. Miller 
1228db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1229db8dac20SDavid S. Miller 		goto do_confirm;
1230db8dac20SDavid S. Miller back_from_confirm:
1231db8dac20SDavid S. Miller 
1232db8dac20SDavid S. Miller 	lock_sock(sk);
1233db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1234db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1235db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1236db8dac20SDavid S. Miller 		release_sock(sk);
1237db8dac20SDavid S. Miller 
1238db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1239db8dac20SDavid S. Miller 		err = -EINVAL;
1240db8dac20SDavid S. Miller 		goto out;
1241db8dac20SDavid S. Miller 	}
1242db8dac20SDavid S. Miller 
1243db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1244db8dac20SDavid S. Miller 
1245db8dac20SDavid S. Miller do_append_data:
1246e36d3ff9SJiri Pirko 	if (dontfrag < 0)
1247e36d3ff9SJiri Pirko 		dontfrag = np->dontfrag;
1248db8dac20SDavid S. Miller 	up->len += ulen;
1249db8dac20SDavid S. Miller 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1250db8dac20SDavid S. Miller 	err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
12514c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1252db8dac20SDavid S. Miller 		(struct rt6_info*)dst,
125313b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1254db8dac20SDavid S. Miller 	if (err)
1255db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1256db8dac20SDavid S. Miller 	else if (!corkreq)
1257db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1258db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1259db8dac20SDavid S. Miller 		up->pending = 0;
1260db8dac20SDavid S. Miller 
1261db8dac20SDavid S. Miller 	if (dst) {
1262db8dac20SDavid S. Miller 		if (connected) {
1263db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
12644c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
1265db8dac20SDavid S. Miller 				      &np->daddr : NULL,
1266db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
12674c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1268db8dac20SDavid S. Miller 				      &np->saddr :
1269db8dac20SDavid S. Miller #endif
1270db8dac20SDavid S. Miller 				      NULL);
1271db8dac20SDavid S. Miller 		} else {
1272db8dac20SDavid S. Miller 			dst_release(dst);
1273db8dac20SDavid S. Miller 		}
1274a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1275db8dac20SDavid S. Miller 	}
1276db8dac20SDavid S. Miller 
1277db8dac20SDavid S. Miller 	if (err > 0)
1278db8dac20SDavid S. Miller 		err = np->recverr ? net_xmit_errno(err) : 0;
1279db8dac20SDavid S. Miller 	release_sock(sk);
1280db8dac20SDavid S. Miller out:
1281a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1282db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
1283db8dac20SDavid S. Miller 	if (!err)
1284db8dac20SDavid S. Miller 		return len;
1285db8dac20SDavid S. Miller 	/*
1286db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1287db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1288db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1289db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1290db8dac20SDavid S. Miller 	 * seems like overkill.
1291db8dac20SDavid S. Miller 	 */
1292db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1293235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1294235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1295db8dac20SDavid S. Miller 	}
1296db8dac20SDavid S. Miller 	return err;
1297db8dac20SDavid S. Miller 
1298db8dac20SDavid S. Miller do_confirm:
1299db8dac20SDavid S. Miller 	dst_confirm(dst);
1300db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1301db8dac20SDavid S. Miller 		goto back_from_confirm;
1302db8dac20SDavid S. Miller 	err = 0;
1303db8dac20SDavid S. Miller 	goto out;
1304db8dac20SDavid S. Miller }
1305db8dac20SDavid S. Miller 
13067d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1307db8dac20SDavid S. Miller {
130844046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1309db8dac20SDavid S. Miller 	lock_sock(sk);
1310db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1311db8dac20SDavid S. Miller 	release_sock(sk);
1312db8dac20SDavid S. Miller 
131344046a59STom Parkin 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
131444046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
131544046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
131644046a59STom Parkin 		if (encap_destroy)
131744046a59STom Parkin 			encap_destroy(sk);
131844046a59STom Parkin 	}
131944046a59STom Parkin 
1320db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1321db8dac20SDavid S. Miller }
1322db8dac20SDavid S. Miller 
1323db8dac20SDavid S. Miller /*
1324db8dac20SDavid S. Miller  *	Socket option code for UDP
1325db8dac20SDavid S. Miller  */
1326db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1327b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1328db8dac20SDavid S. Miller {
1329db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1330db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1331db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1332db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1333db8dac20SDavid S. Miller }
1334db8dac20SDavid S. Miller 
1335db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1336db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1337b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1338db8dac20SDavid S. Miller {
1339db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1340db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1341db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1342db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1343db8dac20SDavid S. Miller }
1344db8dac20SDavid S. Miller #endif
1345db8dac20SDavid S. Miller 
1346db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1347db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1348db8dac20SDavid S. Miller {
1349db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1350db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1351db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1352db8dac20SDavid S. Miller }
1353db8dac20SDavid S. Miller 
1354db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1355db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1356db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1357db8dac20SDavid S. Miller {
1358db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1359db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1360db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1361db8dac20SDavid S. Miller }
1362db8dac20SDavid S. Miller #endif
1363db8dac20SDavid S. Miller 
136441135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1365db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1366db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1367db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1368db8dac20SDavid S. Miller };
1369db8dac20SDavid S. Miller 
1370db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1371db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1372db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1373db8dac20SDavid S. Miller {
137417ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
137517ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
137617ef66afSLorenzo Colitti 	} else {
137717ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
137817ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
137917ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
138017ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
138117ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
138217ef66afSLorenzo Colitti 	}
1383db8dac20SDavid S. Miller 	return 0;
1384db8dac20SDavid S. Miller }
1385db8dac20SDavid S. Miller 
138673cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
138773cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
138873cb88ecSArjan van de Ven 	.open     = udp_seq_open,
138973cb88ecSArjan van de Ven 	.read     = seq_read,
139073cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
139173cb88ecSArjan van de Ven 	.release  = seq_release_net
139273cb88ecSArjan van de Ven };
139373cb88ecSArjan van de Ven 
1394db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1395db8dac20SDavid S. Miller 	.name		= "udp6",
1396db8dac20SDavid S. Miller 	.family		= AF_INET6,
1397645ca708SEric Dumazet 	.udp_table	= &udp_table,
139873cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1399dda61925SDenis V. Lunev 	.seq_ops	= {
1400dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1401dda61925SDenis V. Lunev 	},
1402db8dac20SDavid S. Miller };
1403db8dac20SDavid S. Miller 
14042c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1405db8dac20SDavid S. Miller {
14060c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1407db8dac20SDavid S. Miller }
1408db8dac20SDavid S. Miller 
14090c96d8c5SDaniel Lezcano void udp6_proc_exit(struct net *net) {
14100c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1411db8dac20SDavid S. Miller }
1412db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1413db8dac20SDavid S. Miller 
1414f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size)
1415f77d6021SEric Dumazet {
1416f77d6021SEric Dumazet 	struct inet_sock *inet = inet_sk(sk);
1417f77d6021SEric Dumazet 
1418f77d6021SEric Dumazet 	/* we do not want to clear pinet6 field, because of RCU lookups */
1419f77d6021SEric Dumazet 	sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1420f77d6021SEric Dumazet 
1421f77d6021SEric Dumazet 	size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1422f77d6021SEric Dumazet 	memset(&inet->pinet6 + 1, 0, size);
1423f77d6021SEric Dumazet }
1424f77d6021SEric Dumazet 
1425db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1426db8dac20SDavid S. Miller 
1427db8dac20SDavid S. Miller struct proto udpv6_prot = {
1428db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1429db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1430db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1431db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1432db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1433db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1434db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1435db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1436db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1437db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1438db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1439f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1440db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1441db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1442719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1443db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1444db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1445db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1446db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1447db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1448db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1449271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1450645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1451db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1452db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1453db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1454db8dac20SDavid S. Miller #endif
1455f77d6021SEric Dumazet 	.clear_sk	   = udp_v6_clear_sk,
1456db8dac20SDavid S. Miller };
1457db8dac20SDavid S. Miller 
1458db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1459db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1460db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1461db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1462db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1463db8dac20SDavid S. Miller 	.no_check =  UDP_CSUM_DEFAULT,
1464db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1465db8dac20SDavid S. Miller };
1466db8dac20SDavid S. Miller 
1467db8dac20SDavid S. Miller 
1468db8dac20SDavid S. Miller int __init udpv6_init(void)
1469db8dac20SDavid S. Miller {
1470db8dac20SDavid S. Miller 	int ret;
1471db8dac20SDavid S. Miller 
14723336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
14733336288aSVlad Yasevich 	if (ret)
1474c6b641a4SVlad Yasevich 		goto out;
14753336288aSVlad Yasevich 
1476db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1477db8dac20SDavid S. Miller 	if (ret)
1478db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1479db8dac20SDavid S. Miller out:
1480db8dac20SDavid S. Miller 	return ret;
1481db8dac20SDavid S. Miller 
1482db8dac20SDavid S. Miller out_udpv6_protocol:
1483db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1484db8dac20SDavid S. Miller 	goto out;
1485db8dac20SDavid S. Miller }
1486db8dac20SDavid S. Miller 
1487db8dac20SDavid S. Miller void udpv6_exit(void)
1488db8dac20SDavid S. Miller {
1489db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1490db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1491db8dac20SDavid S. Miller }
1492