xref: /openbmc/linux/net/ipv6/udp.c (revision 73df66f8)
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>
48db8dac20SDavid S. Miller 
49db8dac20SDavid S. Miller #include <linux/proc_fs.h>
50db8dac20SDavid S. Miller #include <linux/seq_file.h>
5122911fc5SEric Dumazet #include <trace/events/skb.h>
52db8dac20SDavid S. Miller #include "udp_impl.h"
53db8dac20SDavid S. Miller 
54b2f5e7cdSVlad Yasevich int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
55b2f5e7cdSVlad Yasevich {
56b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
57b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
5868835abaSEric Dumazet 	__be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
5968835abaSEric Dumazet 	__be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
60b2f5e7cdSVlad Yasevich 	int sk_ipv6only = ipv6_only_sock(sk);
61b2f5e7cdSVlad Yasevich 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
62b2f5e7cdSVlad Yasevich 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
63b2f5e7cdSVlad Yasevich 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
64b2f5e7cdSVlad Yasevich 
65b2f5e7cdSVlad Yasevich 	/* if both are mapped, treat as IPv4 */
66b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
67499923c7SVlad Yasevich 		return (!sk2_ipv6only &&
68c720c7e8SEric Dumazet 			(!sk1_rcv_saddr || !sk2_rcv_saddr ||
69c720c7e8SEric Dumazet 			  sk1_rcv_saddr == sk2_rcv_saddr));
70b2f5e7cdSVlad Yasevich 
71b2f5e7cdSVlad Yasevich 	if (addr_type2 == IPV6_ADDR_ANY &&
72b2f5e7cdSVlad Yasevich 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
73b2f5e7cdSVlad Yasevich 		return 1;
74b2f5e7cdSVlad Yasevich 
75b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_ANY &&
76b2f5e7cdSVlad Yasevich 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
77b2f5e7cdSVlad Yasevich 		return 1;
78b2f5e7cdSVlad Yasevich 
79b2f5e7cdSVlad Yasevich 	if (sk2_rcv_saddr6 &&
80b2f5e7cdSVlad Yasevich 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
81b2f5e7cdSVlad Yasevich 		return 1;
82b2f5e7cdSVlad Yasevich 
83b2f5e7cdSVlad Yasevich 	return 0;
84b2f5e7cdSVlad Yasevich }
85b2f5e7cdSVlad Yasevich 
86d4cada4aSEric Dumazet static unsigned int udp6_portaddr_hash(struct net *net,
87d4cada4aSEric Dumazet 				       const struct in6_addr *addr6,
88d4cada4aSEric Dumazet 				       unsigned int port)
89d4cada4aSEric Dumazet {
90d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
91d4cada4aSEric Dumazet 
92d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
93d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
94856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
950eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
96d4cada4aSEric Dumazet 	else
970eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
98d4cada4aSEric Dumazet 
99d4cada4aSEric Dumazet 	return hash ^ port;
100d4cada4aSEric Dumazet }
101d4cada4aSEric Dumazet 
102d4cada4aSEric Dumazet 
1036ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
104db8dac20SDavid S. Miller {
10530fff923SEric Dumazet 	unsigned int hash2_nulladdr =
10630fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10730fff923SEric Dumazet 	unsigned int hash2_partial =
10830fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0);
10930fff923SEric Dumazet 
110d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
11130fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
11230fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
113db8dac20SDavid S. Miller }
114db8dac20SDavid S. Miller 
115719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
116719f8358SEric Dumazet {
117719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
118719f8358SEric Dumazet 					  &inet6_sk(sk)->rcv_saddr,
119719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
120719f8358SEric Dumazet 
121719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
122719f8358SEric Dumazet }
123719f8358SEric Dumazet 
124645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
125645ca708SEric Dumazet 				unsigned short hnum,
12688440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
12788440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
128645ca708SEric Dumazet 				int dif)
129db8dac20SDavid S. Miller {
130645ca708SEric Dumazet 	int score = -1;
131db8dac20SDavid S. Miller 
132d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
133db8dac20SDavid S. Miller 			sk->sk_family == PF_INET6) {
134db8dac20SDavid S. Miller 		struct ipv6_pinfo *np = inet6_sk(sk);
135645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
136645ca708SEric Dumazet 
137645ca708SEric Dumazet 		score = 0;
138c720c7e8SEric Dumazet 		if (inet->inet_dport) {
139c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
140645ca708SEric Dumazet 				return -1;
141db8dac20SDavid S. Miller 			score++;
142db8dac20SDavid S. Miller 		}
143db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->rcv_saddr)) {
144db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
145645ca708SEric Dumazet 				return -1;
146db8dac20SDavid S. Miller 			score++;
147db8dac20SDavid S. Miller 		}
148db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->daddr)) {
149db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->daddr, saddr))
150645ca708SEric Dumazet 				return -1;
151db8dac20SDavid S. Miller 			score++;
152db8dac20SDavid S. Miller 		}
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 		}
158645ca708SEric Dumazet 	}
159645ca708SEric Dumazet 	return score;
160645ca708SEric Dumazet }
161645ca708SEric Dumazet 
162fddc17deSEric Dumazet #define SCORE2_MAX (1 + 1 + 1)
163fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
164fddc17deSEric Dumazet 				const struct in6_addr *saddr, __be16 sport,
165fddc17deSEric Dumazet 				const struct in6_addr *daddr, unsigned short hnum,
166fddc17deSEric Dumazet 				int dif)
167fddc17deSEric Dumazet {
168fddc17deSEric Dumazet 	int score = -1;
169fddc17deSEric Dumazet 
170fddc17deSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
171fddc17deSEric Dumazet 			sk->sk_family == PF_INET6) {
172fddc17deSEric Dumazet 		struct ipv6_pinfo *np = inet6_sk(sk);
173fddc17deSEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
174fddc17deSEric Dumazet 
175fddc17deSEric Dumazet 		if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
176fddc17deSEric Dumazet 			return -1;
177fddc17deSEric Dumazet 		score = 0;
178fddc17deSEric Dumazet 		if (inet->inet_dport) {
179fddc17deSEric Dumazet 			if (inet->inet_dport != sport)
180fddc17deSEric Dumazet 				return -1;
181fddc17deSEric Dumazet 			score++;
182fddc17deSEric Dumazet 		}
183fddc17deSEric Dumazet 		if (!ipv6_addr_any(&np->daddr)) {
184fddc17deSEric Dumazet 			if (!ipv6_addr_equal(&np->daddr, saddr))
185fddc17deSEric Dumazet 				return -1;
186fddc17deSEric Dumazet 			score++;
187fddc17deSEric Dumazet 		}
188fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if) {
189fddc17deSEric Dumazet 			if (sk->sk_bound_dev_if != dif)
190fddc17deSEric Dumazet 				return -1;
191fddc17deSEric Dumazet 			score++;
192fddc17deSEric Dumazet 		}
193fddc17deSEric Dumazet 	}
194fddc17deSEric Dumazet 	return score;
195fddc17deSEric Dumazet }
196fddc17deSEric Dumazet 
197fddc17deSEric Dumazet 
198fddc17deSEric Dumazet /* called with read_rcu_lock() */
199fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
200fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
201fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
202fddc17deSEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
203fddc17deSEric Dumazet {
204fddc17deSEric Dumazet 	struct sock *sk, *result;
205fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
206fddc17deSEric Dumazet 	int score, badness;
207fddc17deSEric Dumazet 
208fddc17deSEric Dumazet begin:
209fddc17deSEric Dumazet 	result = NULL;
210fddc17deSEric Dumazet 	badness = -1;
211fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
212fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
213fddc17deSEric Dumazet 				      daddr, hnum, dif);
214fddc17deSEric Dumazet 		if (score > badness) {
215fddc17deSEric Dumazet 			result = sk;
216fddc17deSEric Dumazet 			badness = score;
217fddc17deSEric Dumazet 			if (score == SCORE2_MAX)
218fddc17deSEric Dumazet 				goto exact_match;
219fddc17deSEric Dumazet 		}
220fddc17deSEric Dumazet 	}
221fddc17deSEric Dumazet 	/*
222fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
223fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
224fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
225fddc17deSEric Dumazet 	 */
226fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
227fddc17deSEric Dumazet 		goto begin;
228fddc17deSEric Dumazet 
229fddc17deSEric Dumazet 	if (result) {
230fddc17deSEric Dumazet exact_match:
231c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
232fddc17deSEric Dumazet 			result = NULL;
233fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
234fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
235fddc17deSEric Dumazet 			sock_put(result);
236fddc17deSEric Dumazet 			goto begin;
237fddc17deSEric Dumazet 		}
238fddc17deSEric Dumazet 	}
239fddc17deSEric Dumazet 	return result;
240fddc17deSEric Dumazet }
241fddc17deSEric Dumazet 
242fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
24388440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
24488440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
245645ca708SEric Dumazet 				      int dif, struct udp_table *udptable)
246645ca708SEric Dumazet {
247271b72c7SEric Dumazet 	struct sock *sk, *result;
24888ab1932SEric Dumazet 	struct hlist_nulls_node *node;
249645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
250fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
251fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
252271b72c7SEric Dumazet 	int score, badness;
253645ca708SEric Dumazet 
254271b72c7SEric Dumazet 	rcu_read_lock();
255fddc17deSEric Dumazet 	if (hslot->count > 10) {
256fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
257fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
258fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
259fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
260fddc17deSEric Dumazet 			goto begin;
261fddc17deSEric Dumazet 
262fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
263fddc17deSEric Dumazet 					  daddr, hnum, dif,
264fddc17deSEric Dumazet 					  hslot2, slot2);
265fddc17deSEric Dumazet 		if (!result) {
266fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
267fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
268fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
269fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
270fddc17deSEric Dumazet 				goto begin;
271fddc17deSEric Dumazet 
2721223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
2731223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
274fddc17deSEric Dumazet 						  hslot2, slot2);
275fddc17deSEric Dumazet 		}
276fddc17deSEric Dumazet 		rcu_read_unlock();
277fddc17deSEric Dumazet 		return result;
278fddc17deSEric Dumazet 	}
279271b72c7SEric Dumazet begin:
280271b72c7SEric Dumazet 	result = NULL;
281271b72c7SEric Dumazet 	badness = -1;
28288ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
283645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
284645ca708SEric Dumazet 		if (score > badness) {
285db8dac20SDavid S. Miller 			result = sk;
286db8dac20SDavid S. Miller 			badness = score;
287db8dac20SDavid S. Miller 		}
288db8dac20SDavid S. Miller 	}
28988ab1932SEric Dumazet 	/*
29088ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
29188ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
29288ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
29388ab1932SEric Dumazet 	 */
294fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
29588ab1932SEric Dumazet 		goto begin;
29688ab1932SEric Dumazet 
297271b72c7SEric Dumazet 	if (result) {
298c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
299271b72c7SEric Dumazet 			result = NULL;
300271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
301271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
302271b72c7SEric Dumazet 			sock_put(result);
303271b72c7SEric Dumazet 			goto begin;
304271b72c7SEric Dumazet 		}
305271b72c7SEric Dumazet 	}
306271b72c7SEric Dumazet 	rcu_read_unlock();
307db8dac20SDavid S. Miller 	return result;
308db8dac20SDavid S. Miller }
309fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
310db8dac20SDavid S. Miller 
311607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
312607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
313645ca708SEric Dumazet 					  struct udp_table *udptable)
314607c4aafSKOVACS Krisztian {
31523542618SKOVACS Krisztian 	struct sock *sk;
316b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
317607c4aafSKOVACS Krisztian 
31823542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
31923542618SKOVACS Krisztian 		return sk;
320adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
321607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
322607c4aafSKOVACS Krisztian 				 udptable);
323607c4aafSKOVACS Krisztian }
324607c4aafSKOVACS Krisztian 
325aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
326aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
327aa976fc0SBalazs Scheidler {
328aa976fc0SBalazs Scheidler 	return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
329aa976fc0SBalazs Scheidler }
330aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
331aa976fc0SBalazs Scheidler 
332aa976fc0SBalazs Scheidler 
333db8dac20SDavid S. Miller /*
334db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
335db8dac20SDavid S. Miller  * 	return it, otherwise we block.
336db8dac20SDavid S. Miller  */
337db8dac20SDavid S. Miller 
338db8dac20SDavid S. Miller int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
339db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len,
340db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
341db8dac20SDavid S. Miller {
342db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
343db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
344db8dac20SDavid S. Miller 	struct sk_buff *skb;
34559c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
3463f518bf7SPavel Emelyanov 	int peeked, off = 0;
347db8dac20SDavid S. Miller 	int err;
348db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
349f26ba175SWei Yongjun 	int is_udp4;
3508a74ad60SEric Dumazet 	bool slow;
351db8dac20SDavid S. Miller 
352db8dac20SDavid S. Miller 	if (addr_len)
353db8dac20SDavid S. Miller 		*addr_len = sizeof(struct sockaddr_in6);
354db8dac20SDavid S. Miller 
355db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
356db8dac20SDavid S. Miller 		return ipv6_recv_error(sk, msg, len);
357db8dac20SDavid S. Miller 
3584b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
3594b340ae2SBrian Haley 		return ipv6_recv_rxpmtu(sk, msg, len);
3604b340ae2SBrian Haley 
361db8dac20SDavid S. Miller try_again:
362db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
3633f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
364db8dac20SDavid S. Miller 	if (!skb)
365db8dac20SDavid S. Miller 		goto out;
366db8dac20SDavid S. Miller 
367db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
36859c2cdaeSDavid S. Miller 	copied = len;
36959c2cdaeSDavid S. Miller 	if (copied > ulen)
37059c2cdaeSDavid S. Miller 		copied = ulen;
37159c2cdaeSDavid S. Miller 	else if (copied < ulen)
372db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
373db8dac20SDavid S. Miller 
374f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
375f26ba175SWei Yongjun 
376db8dac20SDavid S. Miller 	/*
377db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
378db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
379db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
380db8dac20SDavid S. Miller 	 */
381db8dac20SDavid S. Miller 
38259c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
383db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
384db8dac20SDavid S. Miller 			goto csum_copy_err;
385db8dac20SDavid S. Miller 	}
386db8dac20SDavid S. Miller 
387db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
388db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
38959c2cdaeSDavid S. Miller 					      msg->msg_iov, copied);
390db8dac20SDavid S. Miller 	else {
391db8dac20SDavid S. Miller 		err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
392db8dac20SDavid S. Miller 		if (err == -EINVAL)
393db8dac20SDavid S. Miller 			goto csum_copy_err;
394db8dac20SDavid S. Miller 	}
39522911fc5SEric Dumazet 	if (unlikely(err)) {
39622911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
397979402b1SEric Dumazet 		if (!peeked) {
398979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
399979402b1SEric Dumazet 			if (is_udp4)
400979402b1SEric Dumazet 				UDP_INC_STATS_USER(sock_net(sk),
401979402b1SEric Dumazet 						   UDP_MIB_INERRORS,
402979402b1SEric Dumazet 						   is_udplite);
403979402b1SEric Dumazet 			else
404979402b1SEric Dumazet 				UDP6_INC_STATS_USER(sock_net(sk),
405979402b1SEric Dumazet 						    UDP_MIB_INERRORS,
406979402b1SEric Dumazet 						    is_udplite);
407979402b1SEric Dumazet 		}
408db8dac20SDavid S. Miller 		goto out_free;
40922911fc5SEric Dumazet 	}
410f26ba175SWei Yongjun 	if (!peeked) {
411f26ba175SWei Yongjun 		if (is_udp4)
412f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
413f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
414f26ba175SWei Yongjun 		else
415235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
416235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
417f26ba175SWei Yongjun 	}
418db8dac20SDavid S. Miller 
4193b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
420db8dac20SDavid S. Miller 
421db8dac20SDavid S. Miller 	/* Copy the address. */
422db8dac20SDavid S. Miller 	if (msg->msg_name) {
423db8dac20SDavid S. Miller 		struct sockaddr_in6 *sin6;
424db8dac20SDavid S. Miller 
425db8dac20SDavid S. Miller 		sin6 = (struct sockaddr_in6 *) msg->msg_name;
426db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
427db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
428db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
429db8dac20SDavid S. Miller 		sin6->sin6_scope_id = 0;
430db8dac20SDavid S. Miller 
431f26ba175SWei Yongjun 		if (is_udp4)
432b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
433b301e82cSBrian Haley 					       &sin6->sin6_addr);
434db8dac20SDavid S. Miller 		else {
4354e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
436db8dac20SDavid S. Miller 			if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
437db8dac20SDavid S. Miller 				sin6->sin6_scope_id = IP6CB(skb)->iif;
438db8dac20SDavid S. Miller 		}
439db8dac20SDavid S. Miller 
440db8dac20SDavid S. Miller 	}
441f26ba175SWei Yongjun 	if (is_udp4) {
442db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
443db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
444db8dac20SDavid S. Miller 	} else {
445db8dac20SDavid S. Miller 		if (np->rxopt.all)
44673df66f8STom Parkin 			ip6_datagram_recv_ctl(sk, msg, skb);
447db8dac20SDavid S. Miller 	}
448db8dac20SDavid S. Miller 
44959c2cdaeSDavid S. Miller 	err = copied;
450db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
451db8dac20SDavid S. Miller 		err = ulen;
452db8dac20SDavid S. Miller 
453db8dac20SDavid S. Miller out_free:
4549d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
455db8dac20SDavid S. Miller out:
456db8dac20SDavid S. Miller 	return err;
457db8dac20SDavid S. Miller 
458db8dac20SDavid S. Miller csum_copy_err:
4598a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
4600856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
4610856f939SWei Yongjun 		if (is_udp4)
4620856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
4630856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4640856f939SWei Yongjun 		else
4650856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
4660856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4670856f939SWei Yongjun 	}
4688a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
469db8dac20SDavid S. Miller 
47032c90254SXufeng Zhang 	if (noblock)
471db8dac20SDavid S. Miller 		return -EAGAIN;
4729cfaa8deSXufeng Zhang 
4739cfaa8deSXufeng Zhang 	/* starting over for a new packet */
4749cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
475db8dac20SDavid S. Miller 	goto try_again;
476db8dac20SDavid S. Miller }
477db8dac20SDavid S. Miller 
478db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
479d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
480645ca708SEric Dumazet 		    struct udp_table *udptable)
481db8dac20SDavid S. Miller {
482db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
483b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
484b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
485b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
486db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr*)(skb->data+offset);
487db8dac20SDavid S. Miller 	struct sock *sk;
488db8dac20SDavid S. Miller 	int err;
489db8dac20SDavid S. Miller 
490c346dca1SYOSHIFUJI Hideaki 	sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
491db8dac20SDavid S. Miller 			       saddr, uh->source, inet6_iif(skb), udptable);
492db8dac20SDavid S. Miller 	if (sk == NULL)
493db8dac20SDavid S. Miller 		return;
494db8dac20SDavid S. Miller 
49581aded24SDavid S. Miller 	if (type == ICMPV6_PKT_TOOBIG)
49681aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
497ec18d9a2SDavid S. Miller 	if (type == NDISC_REDIRECT)
498ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
49981aded24SDavid S. Miller 
500db8dac20SDavid S. Miller 	np = inet6_sk(sk);
501db8dac20SDavid S. Miller 
502db8dac20SDavid S. Miller 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
503db8dac20SDavid S. Miller 		goto out;
504db8dac20SDavid S. Miller 
505db8dac20SDavid S. Miller 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
506db8dac20SDavid S. Miller 		goto out;
507db8dac20SDavid S. Miller 
508b1faf566SEric Dumazet 	if (np->recverr)
509db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
510b1faf566SEric Dumazet 
511db8dac20SDavid S. Miller 	sk->sk_err = err;
512db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
513db8dac20SDavid S. Miller out:
514db8dac20SDavid S. Miller 	sock_put(sk);
515db8dac20SDavid S. Miller }
516db8dac20SDavid S. Miller 
517f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
518f7ad74feSBenjamin LaHaise {
519f7ad74feSBenjamin LaHaise 	int rc;
520f7ad74feSBenjamin LaHaise 
521f7ad74feSBenjamin LaHaise 	if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
522f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
523f7ad74feSBenjamin LaHaise 
524f7ad74feSBenjamin LaHaise 	rc = sock_queue_rcv_skb(sk, skb);
525f7ad74feSBenjamin LaHaise 	if (rc < 0) {
526f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
527f7ad74feSBenjamin LaHaise 
528f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
529f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
530f7ad74feSBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk),
531f7ad74feSBenjamin LaHaise 					UDP_MIB_RCVBUFERRORS, is_udplite);
532f7ad74feSBenjamin LaHaise 		UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
533f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
534f7ad74feSBenjamin LaHaise 		return -1;
535f7ad74feSBenjamin LaHaise 	}
536f7ad74feSBenjamin LaHaise 	return 0;
537f7ad74feSBenjamin LaHaise }
538f7ad74feSBenjamin LaHaise 
539db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
540d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
541d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info     )
542db8dac20SDavid S. Miller {
543645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
544db8dac20SDavid S. Miller }
545db8dac20SDavid S. Miller 
546d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
547d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
548d7f3f621SBenjamin LaHaise {
549d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
550d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
551d7f3f621SBenjamin LaHaise }
552d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
553d7f3f621SBenjamin LaHaise 
554db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
555db8dac20SDavid S. Miller {
556db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
557db8dac20SDavid S. Miller 	int rc;
558db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
559db8dac20SDavid S. Miller 
560db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
561db8dac20SDavid S. Miller 		goto drop;
562db8dac20SDavid S. Miller 
563d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
564d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
565d7f3f621SBenjamin LaHaise 
566d7f3f621SBenjamin LaHaise 		/*
567d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
568d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
569d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
570d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
571d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
572d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
573d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
574d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
575d7f3f621SBenjamin LaHaise 		 */
576d7f3f621SBenjamin LaHaise 
577d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
578d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
579d7f3f621SBenjamin LaHaise 		if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
580d7f3f621SBenjamin LaHaise 			int ret;
581d7f3f621SBenjamin LaHaise 
582d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
583d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
584d7f3f621SBenjamin LaHaise 				UDP_INC_STATS_BH(sock_net(sk),
585d7f3f621SBenjamin LaHaise 						 UDP_MIB_INDATAGRAMS,
586d7f3f621SBenjamin LaHaise 						 is_udplite);
587d7f3f621SBenjamin LaHaise 				return -ret;
588d7f3f621SBenjamin LaHaise 			}
589d7f3f621SBenjamin LaHaise 		}
590d7f3f621SBenjamin LaHaise 
591d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
592d7f3f621SBenjamin LaHaise 	}
593d7f3f621SBenjamin LaHaise 
594db8dac20SDavid S. Miller 	/*
595db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
596db8dac20SDavid S. Miller 	 */
597db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
598db8dac20SDavid S. Miller 
599db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
600db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
601db8dac20SDavid S. Miller 				" %d while full coverage %d requested\n",
602db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, skb->len);
603db8dac20SDavid S. Miller 			goto drop;
604db8dac20SDavid S. Miller 		}
605db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
606db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
607db8dac20SDavid S. Miller 						    "too small, need min %d\n",
608db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
609db8dac20SDavid S. Miller 			goto drop;
610db8dac20SDavid S. Miller 		}
611db8dac20SDavid S. Miller 	}
612db8dac20SDavid S. Miller 
61333d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
614db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
615db8dac20SDavid S. Miller 			goto drop;
616db8dac20SDavid S. Miller 	}
617db8dac20SDavid S. Miller 
618cb80ef46SBenjamin LaHaise 	if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf))
619cb80ef46SBenjamin LaHaise 		goto drop;
620cb80ef46SBenjamin LaHaise 
621d826eb14SEric Dumazet 	skb_dst_drop(skb);
622db8dac20SDavid S. Miller 
623cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
624cb80ef46SBenjamin LaHaise 	rc = 0;
625cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
626f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
627cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
628cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
629cb80ef46SBenjamin LaHaise 		goto drop;
630cb80ef46SBenjamin LaHaise 	}
631cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
632f7ad74feSBenjamin LaHaise 
633f7ad74feSBenjamin LaHaise 	return rc;
634db8dac20SDavid S. Miller drop:
635ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
636cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
637db8dac20SDavid S. Miller 	kfree_skb(skb);
638db8dac20SDavid S. Miller 	return -1;
639db8dac20SDavid S. Miller }
640db8dac20SDavid S. Miller 
641920a4611SEric Dumazet static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
642b71d1d42SEric Dumazet 				      __be16 loc_port, const struct in6_addr *loc_addr,
643b71d1d42SEric Dumazet 				      __be16 rmt_port, const struct in6_addr *rmt_addr,
644db8dac20SDavid S. Miller 				      int dif)
645db8dac20SDavid S. Miller {
64688ab1932SEric Dumazet 	struct hlist_nulls_node *node;
647db8dac20SDavid S. Miller 	struct sock *s = sk;
648db8dac20SDavid S. Miller 	unsigned short num = ntohs(loc_port);
649db8dac20SDavid S. Miller 
65088ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
651db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
652db8dac20SDavid S. Miller 
653920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net))
654b8ad0cbcSDaniel Lezcano 			continue;
655b8ad0cbcSDaniel Lezcano 
656d4cada4aSEric Dumazet 		if (udp_sk(s)->udp_port_hash == num &&
657d4cada4aSEric Dumazet 		    s->sk_family == PF_INET6) {
658db8dac20SDavid S. Miller 			struct ipv6_pinfo *np = inet6_sk(s);
659c720c7e8SEric Dumazet 			if (inet->inet_dport) {
660c720c7e8SEric Dumazet 				if (inet->inet_dport != rmt_port)
661db8dac20SDavid S. Miller 					continue;
662db8dac20SDavid S. Miller 			}
663db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->daddr) &&
664db8dac20SDavid S. Miller 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
665db8dac20SDavid S. Miller 				continue;
666db8dac20SDavid S. Miller 
667db8dac20SDavid S. Miller 			if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
668db8dac20SDavid S. Miller 				continue;
669db8dac20SDavid S. Miller 
670db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->rcv_saddr)) {
671db8dac20SDavid S. Miller 				if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
672db8dac20SDavid S. Miller 					continue;
673db8dac20SDavid S. Miller 			}
674db8dac20SDavid S. Miller 			if (!inet6_mc_check(s, loc_addr, rmt_addr))
675db8dac20SDavid S. Miller 				continue;
676db8dac20SDavid S. Miller 			return s;
677db8dac20SDavid S. Miller 		}
678db8dac20SDavid S. Miller 	}
679db8dac20SDavid S. Miller 	return NULL;
680db8dac20SDavid S. Miller }
681db8dac20SDavid S. Miller 
682a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
683a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
684a1ab77f9SEric Dumazet {
685cb80ef46SBenjamin LaHaise 	struct sk_buff *skb1 = NULL;
686a1ab77f9SEric Dumazet 	struct sock *sk;
687cb80ef46SBenjamin LaHaise 	unsigned int i;
688a1ab77f9SEric Dumazet 
689a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
690a1ab77f9SEric Dumazet 		sk = stack[i];
691cb80ef46SBenjamin LaHaise 		if (likely(skb1 == NULL))
692cb80ef46SBenjamin LaHaise 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
693cb80ef46SBenjamin LaHaise 		if (!skb1) {
694f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
695cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
696cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
697cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
698cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
699a1ab77f9SEric Dumazet 		}
700cb80ef46SBenjamin LaHaise 
701cb80ef46SBenjamin LaHaise 		if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
702cb80ef46SBenjamin LaHaise 			skb1 = NULL;
703cb80ef46SBenjamin LaHaise 	}
704cb80ef46SBenjamin LaHaise 	if (unlikely(skb1))
705cb80ef46SBenjamin LaHaise 		kfree_skb(skb1);
706a1ab77f9SEric Dumazet }
707db8dac20SDavid S. Miller /*
708db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
709db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
710db8dac20SDavid S. Miller  */
711e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
712b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
713645ca708SEric Dumazet 		struct udp_table *udptable)
714db8dac20SDavid S. Miller {
715a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
716db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
717f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
718db8dac20SDavid S. Miller 	int dif;
719a1ab77f9SEric Dumazet 	unsigned int i, count = 0;
720db8dac20SDavid S. Miller 
721645ca708SEric Dumazet 	spin_lock(&hslot->lock);
72288ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
723db8dac20SDavid S. Miller 	dif = inet6_iif(skb);
724920a4611SEric Dumazet 	sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
725a1ab77f9SEric Dumazet 	while (sk) {
726a1ab77f9SEric Dumazet 		stack[count++] = sk;
727a1ab77f9SEric Dumazet 		sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
728a1ab77f9SEric Dumazet 				       uh->source, saddr, dif);
729a1ab77f9SEric Dumazet 		if (unlikely(count == ARRAY_SIZE(stack))) {
730a1ab77f9SEric Dumazet 			if (!sk)
731a1ab77f9SEric Dumazet 				break;
732a1ab77f9SEric Dumazet 			flush_stack(stack, count, skb, ~0);
733a1ab77f9SEric Dumazet 			count = 0;
734db8dac20SDavid S. Miller 		}
735a1ab77f9SEric Dumazet 	}
736a1ab77f9SEric Dumazet 	/*
737a1ab77f9SEric Dumazet 	 * before releasing the lock, we must take reference on sockets
738a1ab77f9SEric Dumazet 	 */
739a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++)
740a1ab77f9SEric Dumazet 		sock_hold(stack[i]);
741db8dac20SDavid S. Miller 
742645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
743a1ab77f9SEric Dumazet 
744a1ab77f9SEric Dumazet 	if (count) {
745a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
746a1ab77f9SEric Dumazet 
747a1ab77f9SEric Dumazet 		for (i = 0; i < count; i++)
748a1ab77f9SEric Dumazet 			sock_put(stack[i]);
749a1ab77f9SEric Dumazet 	} else {
750a1ab77f9SEric Dumazet 		kfree_skb(skb);
751a1ab77f9SEric Dumazet 	}
752db8dac20SDavid S. Miller 	return 0;
753db8dac20SDavid S. Miller }
754db8dac20SDavid S. Miller 
755db8dac20SDavid S. Miller static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
756db8dac20SDavid S. Miller 				 int proto)
757db8dac20SDavid S. Miller {
758db8dac20SDavid S. Miller 	int err;
759db8dac20SDavid S. Miller 
760db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
761db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
762db8dac20SDavid S. Miller 
763db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
764db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
765db8dac20SDavid S. Miller 		if (err)
766db8dac20SDavid S. Miller 			return err;
767db8dac20SDavid S. Miller 	}
768db8dac20SDavid S. Miller 
769db8dac20SDavid S. Miller 	if (uh->check == 0) {
770db8dac20SDavid S. Miller 		/* RFC 2460 section 8.1 says that we SHOULD log
771db8dac20SDavid S. Miller 		   this error. Well, it is reasonable.
772db8dac20SDavid S. Miller 		 */
773db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
774db8dac20SDavid S. Miller 		return 1;
775db8dac20SDavid S. Miller 	}
776db8dac20SDavid S. Miller 	if (skb->ip_summed == CHECKSUM_COMPLETE &&
777db8dac20SDavid S. Miller 	    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
778db8dac20SDavid S. Miller 			     skb->len, proto, skb->csum))
779db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_UNNECESSARY;
780db8dac20SDavid S. Miller 
781db8dac20SDavid S. Miller 	if (!skb_csum_unnecessary(skb))
782db8dac20SDavid S. Miller 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
783db8dac20SDavid S. Miller 							 &ipv6_hdr(skb)->daddr,
784db8dac20SDavid S. Miller 							 skb->len, proto, 0));
785db8dac20SDavid S. Miller 
786db8dac20SDavid S. Miller 	return 0;
787db8dac20SDavid S. Miller }
788db8dac20SDavid S. Miller 
789645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
790db8dac20SDavid S. Miller 		   int proto)
791db8dac20SDavid S. Miller {
7923ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
793db8dac20SDavid S. Miller 	struct sock *sk;
794db8dac20SDavid S. Miller 	struct udphdr *uh;
795b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
796db8dac20SDavid S. Miller 	u32 ulen = 0;
797db8dac20SDavid S. Miller 
798db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
799d6bc0149SBjørn Mork 		goto discard;
800db8dac20SDavid S. Miller 
801db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
802db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
803db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
804db8dac20SDavid S. Miller 
805db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
806db8dac20SDavid S. Miller 	if (ulen > skb->len)
807db8dac20SDavid S. Miller 		goto short_packet;
808db8dac20SDavid S. Miller 
809db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
810db8dac20SDavid S. Miller 		/* UDP validates ulen. */
811db8dac20SDavid S. Miller 
812db8dac20SDavid S. Miller 		/* Check for jumbo payload */
813db8dac20SDavid S. Miller 		if (ulen == 0)
814db8dac20SDavid S. Miller 			ulen = skb->len;
815db8dac20SDavid S. Miller 
816db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
817db8dac20SDavid S. Miller 			goto short_packet;
818db8dac20SDavid S. Miller 
819db8dac20SDavid S. Miller 		if (ulen < skb->len) {
820db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
821db8dac20SDavid S. Miller 				goto short_packet;
822db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
823db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
824db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
825db8dac20SDavid S. Miller 		}
826db8dac20SDavid S. Miller 	}
827db8dac20SDavid S. Miller 
828db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
829db8dac20SDavid S. Miller 		goto discard;
830db8dac20SDavid S. Miller 
831db8dac20SDavid S. Miller 	/*
832db8dac20SDavid S. Miller 	 *	Multicast receive code
833db8dac20SDavid S. Miller 	 */
834db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
835e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
836e3163493SPavel Emelyanov 				saddr, daddr, udptable);
837db8dac20SDavid S. Miller 
838db8dac20SDavid S. Miller 	/* Unicast */
839db8dac20SDavid S. Miller 
840db8dac20SDavid S. Miller 	/*
841db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
842db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
843db8dac20SDavid S. Miller 	 */
844607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
845cb80ef46SBenjamin LaHaise 	if (sk != NULL) {
846cb80ef46SBenjamin LaHaise 		int ret = udpv6_queue_rcv_skb(sk, skb);
847cb80ef46SBenjamin LaHaise 		sock_put(sk);
848db8dac20SDavid S. Miller 
849cb80ef46SBenjamin LaHaise 		/* a return value > 0 means to resubmit the input, but
850cb80ef46SBenjamin LaHaise 		 * it wants the return to be -protocol, or 0
851cb80ef46SBenjamin LaHaise 		 */
852cb80ef46SBenjamin LaHaise 		if (ret > 0)
853cb80ef46SBenjamin LaHaise 			return -ret;
854cb80ef46SBenjamin LaHaise 
855cb80ef46SBenjamin LaHaise 		return 0;
856cb80ef46SBenjamin LaHaise 	}
857cb80ef46SBenjamin LaHaise 
858db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
859db8dac20SDavid S. Miller 		goto discard;
860db8dac20SDavid S. Miller 
861db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
862db8dac20SDavid S. Miller 		goto discard;
863db8dac20SDavid S. Miller 
864cb80ef46SBenjamin LaHaise 	UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
8653ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
866db8dac20SDavid S. Miller 
867db8dac20SDavid S. Miller 	kfree_skb(skb);
868db8dac20SDavid S. Miller 	return 0;
869db8dac20SDavid S. Miller 
870db8dac20SDavid S. Miller short_packet:
871d6bc0149SBjørn Mork 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
872db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
873d6bc0149SBjørn Mork 		       saddr,
874d6bc0149SBjørn Mork 		       ntohs(uh->source),
875d6bc0149SBjørn Mork 		       ulen,
876d6bc0149SBjørn Mork 		       skb->len,
877d6bc0149SBjørn Mork 		       daddr,
878d6bc0149SBjørn Mork 		       ntohs(uh->dest));
879db8dac20SDavid S. Miller 
880db8dac20SDavid S. Miller discard:
881ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
882db8dac20SDavid S. Miller 	kfree_skb(skb);
883db8dac20SDavid S. Miller 	return 0;
884db8dac20SDavid S. Miller }
885db8dac20SDavid S. Miller 
886db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
887db8dac20SDavid S. Miller {
888645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
889db8dac20SDavid S. Miller }
890db8dac20SDavid S. Miller 
891db8dac20SDavid S. Miller /*
892db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
893db8dac20SDavid S. Miller  */
894db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
895db8dac20SDavid S. Miller {
896db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
897db8dac20SDavid S. Miller 
89836d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
89936d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
90036d926b9SDenis V. Lunev 	else if (up->pending) {
901db8dac20SDavid S. Miller 		up->len = 0;
902db8dac20SDavid S. Miller 		up->pending = 0;
903db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
904db8dac20SDavid S. Miller 	}
905db8dac20SDavid S. Miller }
906db8dac20SDavid S. Miller 
907493c6be3SSridhar Samudrala /**
908493c6be3SSridhar Samudrala  * 	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
909493c6be3SSridhar Samudrala  * 	@sk: 	socket we are sending on
910493c6be3SSridhar Samudrala  * 	@skb: 	sk_buff containing the filled-in UDP header
911493c6be3SSridhar Samudrala  * 	        (checksum field must be zeroed out)
912493c6be3SSridhar Samudrala  */
913493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
914493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
915493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
916493c6be3SSridhar Samudrala {
917493c6be3SSridhar Samudrala 	unsigned int offset;
918493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
919493c6be3SSridhar Samudrala 	__wsum csum = 0;
920493c6be3SSridhar Samudrala 
921493c6be3SSridhar Samudrala 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
922493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
923493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
924493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
925493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
926493c6be3SSridhar Samudrala 	} else {
927493c6be3SSridhar Samudrala 		/*
928493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
929493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
930493c6be3SSridhar Samudrala 		 * should be together
931493c6be3SSridhar Samudrala 		 */
932493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
933493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
934493c6be3SSridhar Samudrala 
935493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
936493c6be3SSridhar Samudrala 
937493c6be3SSridhar Samudrala 		skb_queue_walk(&sk->sk_write_queue, skb) {
938493c6be3SSridhar Samudrala 			csum = csum_add(csum, skb->csum);
939493c6be3SSridhar Samudrala 		}
940493c6be3SSridhar Samudrala 
941493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
942493c6be3SSridhar Samudrala 					    csum);
943493c6be3SSridhar Samudrala 		if (uh->check == 0)
944493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
945493c6be3SSridhar Samudrala 	}
946493c6be3SSridhar Samudrala }
947493c6be3SSridhar Samudrala 
948db8dac20SDavid S. Miller /*
949db8dac20SDavid S. Miller  *	Sending
950db8dac20SDavid S. Miller  */
951db8dac20SDavid S. Miller 
952db8dac20SDavid S. Miller static int udp_v6_push_pending_frames(struct sock *sk)
953db8dac20SDavid S. Miller {
954db8dac20SDavid S. Miller 	struct sk_buff *skb;
955db8dac20SDavid S. Miller 	struct udphdr *uh;
956db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
957db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
9584c9483b2SDavid S. Miller 	struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
959db8dac20SDavid S. Miller 	int err = 0;
960db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
961db8dac20SDavid S. Miller 	__wsum csum = 0;
962db8dac20SDavid S. Miller 
963db8dac20SDavid S. Miller 	/* Grab the skbuff where UDP header space exists. */
964db8dac20SDavid S. Miller 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
965db8dac20SDavid S. Miller 		goto out;
966db8dac20SDavid S. Miller 
967db8dac20SDavid S. Miller 	/*
968db8dac20SDavid S. Miller 	 * Create a UDP header
969db8dac20SDavid S. Miller 	 */
970db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
9711958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
9721958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
973db8dac20SDavid S. Miller 	uh->len = htons(up->len);
974db8dac20SDavid S. Miller 	uh->check = 0;
975db8dac20SDavid S. Miller 
976db8dac20SDavid S. Miller 	if (is_udplite)
977db8dac20SDavid S. Miller 		csum = udplite_csum_outgoing(sk, skb);
978493c6be3SSridhar Samudrala 	else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
9794c9483b2SDavid S. Miller 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
980493c6be3SSridhar Samudrala 				     up->len);
981493c6be3SSridhar Samudrala 		goto send;
982493c6be3SSridhar Samudrala 	} else
983db8dac20SDavid S. Miller 		csum = udp_csum_outgoing(sk, skb);
984db8dac20SDavid S. Miller 
985db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
9864c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
9874c9483b2SDavid S. Miller 				    up->len, fl6->flowi6_proto, csum);
988db8dac20SDavid S. Miller 	if (uh->check == 0)
989db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
990db8dac20SDavid S. Miller 
991493c6be3SSridhar Samudrala send:
992db8dac20SDavid S. Miller 	err = ip6_push_pending_frames(sk);
9936ce9e7b5SEric Dumazet 	if (err) {
9946ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
9956ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
9966ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
9976ce9e7b5SEric Dumazet 			err = 0;
9986ce9e7b5SEric Dumazet 		}
9996ce9e7b5SEric Dumazet 	} else
10006ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
10016ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
1002db8dac20SDavid S. Miller out:
1003db8dac20SDavid S. Miller 	up->len = 0;
1004db8dac20SDavid S. Miller 	up->pending = 0;
1005db8dac20SDavid S. Miller 	return err;
1006db8dac20SDavid S. Miller }
1007db8dac20SDavid S. Miller 
1008db8dac20SDavid S. Miller int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
1009db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len)
1010db8dac20SDavid S. Miller {
1011db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1012db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1013db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1014db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1015db8dac20SDavid S. Miller 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
101620c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1017db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
1018db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
10194c9483b2SDavid S. Miller 	struct flowi6 fl6;
1020db8dac20SDavid S. Miller 	struct dst_entry *dst;
1021db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1022db8dac20SDavid S. Miller 	int ulen = len;
1023db8dac20SDavid S. Miller 	int hlimit = -1;
1024db8dac20SDavid S. Miller 	int tclass = -1;
102513b52cd4SBrian Haley 	int dontfrag = -1;
1026db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1027db8dac20SDavid S. Miller 	int err;
1028db8dac20SDavid S. Miller 	int connected = 0;
1029db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1030db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1031db8dac20SDavid S. Miller 
1032db8dac20SDavid S. Miller 	/* destination address check */
1033db8dac20SDavid S. Miller 	if (sin6) {
1034db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1035db8dac20SDavid S. Miller 			return -EINVAL;
1036db8dac20SDavid S. Miller 
1037db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1038db8dac20SDavid S. Miller 		case AF_INET6:
1039db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1040db8dac20SDavid S. Miller 				return -EINVAL;
1041db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1042db8dac20SDavid S. Miller 			break;
1043db8dac20SDavid S. Miller 		case AF_INET:
1044db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1045db8dac20SDavid S. Miller 		case AF_UNSPEC:
1046db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1047db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1048db8dac20SDavid S. Miller 			daddr = NULL;
1049db8dac20SDavid S. Miller 			break;
1050db8dac20SDavid S. Miller 		default:
1051db8dac20SDavid S. Miller 			return -EINVAL;
1052db8dac20SDavid S. Miller 		}
1053db8dac20SDavid S. Miller 	} else if (!up->pending) {
1054db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1055db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1056db8dac20SDavid S. Miller 		daddr = &np->daddr;
1057db8dac20SDavid S. Miller 	} else
1058db8dac20SDavid S. Miller 		daddr = NULL;
1059db8dac20SDavid S. Miller 
1060db8dac20SDavid S. Miller 	if (daddr) {
1061db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1062db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1063db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1064c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1065db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1066db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1067db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1068db8dac20SDavid S. Miller do_udp_sendmsg:
1069db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1070db8dac20SDavid S. Miller 				return -ENETUNREACH;
1071db8dac20SDavid S. Miller 			return udp_sendmsg(iocb, sk, msg, len);
1072db8dac20SDavid S. Miller 		}
1073db8dac20SDavid S. Miller 	}
1074db8dac20SDavid S. Miller 
1075db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
1076db8dac20SDavid S. Miller 		return udp_sendmsg(iocb, sk, msg, len);
1077db8dac20SDavid S. Miller 
1078db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1079db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1080db8dac20SDavid S. Miller 	   */
1081db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1082db8dac20SDavid S. Miller 		return -EMSGSIZE;
1083db8dac20SDavid S. Miller 
1084db8dac20SDavid S. Miller 	if (up->pending) {
1085db8dac20SDavid S. Miller 		/*
1086db8dac20SDavid S. Miller 		 * There are pending frames.
1087db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1088db8dac20SDavid S. Miller 		 */
1089db8dac20SDavid S. Miller 		lock_sock(sk);
1090db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1091db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1092db8dac20SDavid S. Miller 				release_sock(sk);
1093db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1094db8dac20SDavid S. Miller 			}
1095db8dac20SDavid S. Miller 			dst = NULL;
1096db8dac20SDavid S. Miller 			goto do_append_data;
1097db8dac20SDavid S. Miller 		}
1098db8dac20SDavid S. Miller 		release_sock(sk);
1099db8dac20SDavid S. Miller 	}
1100db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1101db8dac20SDavid S. Miller 
11024c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1103db8dac20SDavid S. Miller 
1104db8dac20SDavid S. Miller 	if (sin6) {
1105db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1106db8dac20SDavid S. Miller 			return -EINVAL;
1107db8dac20SDavid S. Miller 
11081958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1109db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1110db8dac20SDavid S. Miller 
1111db8dac20SDavid S. Miller 		if (np->sndflow) {
11124c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
11134c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
11144c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1115db8dac20SDavid S. Miller 				if (flowlabel == NULL)
1116db8dac20SDavid S. Miller 					return -EINVAL;
1117db8dac20SDavid S. Miller 				daddr = &flowlabel->dst;
1118db8dac20SDavid S. Miller 			}
1119db8dac20SDavid S. Miller 		}
1120db8dac20SDavid S. Miller 
1121db8dac20SDavid S. Miller 		/*
1122db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1123db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1124db8dac20SDavid S. Miller 		 */
1125db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1126db8dac20SDavid S. Miller 		    ipv6_addr_equal(daddr, &np->daddr))
1127db8dac20SDavid S. Miller 			daddr = &np->daddr;
1128db8dac20SDavid S. Miller 
1129db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1130db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1131db8dac20SDavid S. Miller 		    ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
11324c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1133db8dac20SDavid S. Miller 	} else {
1134db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1135db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1136db8dac20SDavid S. Miller 
11371958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1138db8dac20SDavid S. Miller 		daddr = &np->daddr;
11394c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1140db8dac20SDavid S. Miller 		connected = 1;
1141db8dac20SDavid S. Miller 	}
1142db8dac20SDavid S. Miller 
11434c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11444c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1145db8dac20SDavid S. Miller 
11464c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11474c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
11489f690db7SYang Hongyang 
11494c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
115051953d5bSBrian Haley 
1151db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1152db8dac20SDavid S. Miller 		opt = &opt_space;
1153db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1154db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1155db8dac20SDavid S. Miller 
115673df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1157ec0506dbSMaciej Żenczykowski 					    &hlimit, &tclass, &dontfrag);
1158db8dac20SDavid S. Miller 		if (err < 0) {
1159db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1160db8dac20SDavid S. Miller 			return err;
1161db8dac20SDavid S. Miller 		}
11624c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
11634c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1164db8dac20SDavid S. Miller 			if (flowlabel == NULL)
1165db8dac20SDavid S. Miller 				return -EINVAL;
1166db8dac20SDavid S. Miller 		}
1167db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1168db8dac20SDavid S. Miller 			opt = NULL;
1169db8dac20SDavid S. Miller 		connected = 0;
1170db8dac20SDavid S. Miller 	}
1171db8dac20SDavid S. Miller 	if (opt == NULL)
1172db8dac20SDavid S. Miller 		opt = np->opt;
1173db8dac20SDavid S. Miller 	if (flowlabel)
1174db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1175db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1176db8dac20SDavid S. Miller 
11774c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1178876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
11794e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1180876c7f41SBrian Haley 	else
11814c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
11824c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
11834e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
11841958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1185db8dac20SDavid S. Miller 
11864c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
118720c59de2SArnaud Ebalard 	if (final_p)
1188db8dac20SDavid S. Miller 		connected = 0;
1189db8dac20SDavid S. Miller 
11904c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
11914c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1192db8dac20SDavid S. Miller 		connected = 0;
1193c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1194c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1195db8dac20SDavid S. Miller 
11964c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1197db8dac20SDavid S. Miller 
11984c9483b2SDavid S. Miller 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
119968d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
120068d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
120168d0c6d3SDavid S. Miller 		dst = NULL;
1202db8dac20SDavid S. Miller 		goto out;
1203db8dac20SDavid S. Miller 	}
1204db8dac20SDavid S. Miller 
1205db8dac20SDavid S. Miller 	if (hlimit < 0) {
12064c9483b2SDavid S. Miller 		if (ipv6_addr_is_multicast(&fl6.daddr))
1207db8dac20SDavid S. Miller 			hlimit = np->mcast_hops;
1208db8dac20SDavid S. Miller 		else
1209db8dac20SDavid S. Miller 			hlimit = np->hop_limit;
1210db8dac20SDavid S. Miller 		if (hlimit < 0)
12116b75d090SYOSHIFUJI Hideaki 			hlimit = ip6_dst_hoplimit(dst);
1212db8dac20SDavid S. Miller 	}
1213db8dac20SDavid S. Miller 
1214db8dac20SDavid S. Miller 	if (tclass < 0)
1215e651f03aSGerrit Renker 		tclass = np->tclass;
1216db8dac20SDavid S. Miller 
121713b52cd4SBrian Haley 	if (dontfrag < 0)
121813b52cd4SBrian Haley 		dontfrag = np->dontfrag;
121913b52cd4SBrian Haley 
1220db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1221db8dac20SDavid S. Miller 		goto do_confirm;
1222db8dac20SDavid S. Miller back_from_confirm:
1223db8dac20SDavid S. Miller 
1224db8dac20SDavid S. Miller 	lock_sock(sk);
1225db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1226db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1227db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1228db8dac20SDavid S. Miller 		release_sock(sk);
1229db8dac20SDavid S. Miller 
1230db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1231db8dac20SDavid S. Miller 		err = -EINVAL;
1232db8dac20SDavid S. Miller 		goto out;
1233db8dac20SDavid S. Miller 	}
1234db8dac20SDavid S. Miller 
1235db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1236db8dac20SDavid S. Miller 
1237db8dac20SDavid S. Miller do_append_data:
1238db8dac20SDavid S. Miller 	up->len += ulen;
1239db8dac20SDavid S. Miller 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1240db8dac20SDavid S. Miller 	err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
12414c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1242db8dac20SDavid S. Miller 		(struct rt6_info*)dst,
124313b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1244db8dac20SDavid S. Miller 	if (err)
1245db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1246db8dac20SDavid S. Miller 	else if (!corkreq)
1247db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1248db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1249db8dac20SDavid S. Miller 		up->pending = 0;
1250db8dac20SDavid S. Miller 
1251db8dac20SDavid S. Miller 	if (dst) {
1252db8dac20SDavid S. Miller 		if (connected) {
1253db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
12544c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
1255db8dac20SDavid S. Miller 				      &np->daddr : NULL,
1256db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
12574c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1258db8dac20SDavid S. Miller 				      &np->saddr :
1259db8dac20SDavid S. Miller #endif
1260db8dac20SDavid S. Miller 				      NULL);
1261db8dac20SDavid S. Miller 		} else {
1262db8dac20SDavid S. Miller 			dst_release(dst);
1263db8dac20SDavid S. Miller 		}
1264a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1265db8dac20SDavid S. Miller 	}
1266db8dac20SDavid S. Miller 
1267db8dac20SDavid S. Miller 	if (err > 0)
1268db8dac20SDavid S. Miller 		err = np->recverr ? net_xmit_errno(err) : 0;
1269db8dac20SDavid S. Miller 	release_sock(sk);
1270db8dac20SDavid S. Miller out:
1271a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1272db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
1273db8dac20SDavid S. Miller 	if (!err)
1274db8dac20SDavid S. Miller 		return len;
1275db8dac20SDavid S. Miller 	/*
1276db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1277db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1278db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1279db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1280db8dac20SDavid S. Miller 	 * seems like overkill.
1281db8dac20SDavid S. Miller 	 */
1282db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1283235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1284235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1285db8dac20SDavid S. Miller 	}
1286db8dac20SDavid S. Miller 	return err;
1287db8dac20SDavid S. Miller 
1288db8dac20SDavid S. Miller do_confirm:
1289db8dac20SDavid S. Miller 	dst_confirm(dst);
1290db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1291db8dac20SDavid S. Miller 		goto back_from_confirm;
1292db8dac20SDavid S. Miller 	err = 0;
1293db8dac20SDavid S. Miller 	goto out;
1294db8dac20SDavid S. Miller }
1295db8dac20SDavid S. Miller 
12967d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1297db8dac20SDavid S. Miller {
1298db8dac20SDavid S. Miller 	lock_sock(sk);
1299db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1300db8dac20SDavid S. Miller 	release_sock(sk);
1301db8dac20SDavid S. Miller 
1302db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1303db8dac20SDavid S. Miller }
1304db8dac20SDavid S. Miller 
1305db8dac20SDavid S. Miller /*
1306db8dac20SDavid S. Miller  *	Socket option code for UDP
1307db8dac20SDavid S. Miller  */
1308db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1309b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1310db8dac20SDavid S. Miller {
1311db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1312db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1313db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1314db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1315db8dac20SDavid S. Miller }
1316db8dac20SDavid S. Miller 
1317db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1318db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1319b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1320db8dac20SDavid S. Miller {
1321db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1322db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1323db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1324db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1325db8dac20SDavid S. Miller }
1326db8dac20SDavid S. Miller #endif
1327db8dac20SDavid S. Miller 
1328db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1329db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1330db8dac20SDavid S. Miller {
1331db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1332db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1333db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1334db8dac20SDavid S. Miller }
1335db8dac20SDavid S. Miller 
1336db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1337db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1338db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1339db8dac20SDavid S. Miller {
1340db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1341db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1342db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1343db8dac20SDavid S. Miller }
1344db8dac20SDavid S. Miller #endif
1345db8dac20SDavid S. Miller 
134641135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1347db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1348db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1349db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1350db8dac20SDavid S. Miller };
1351db8dac20SDavid S. Miller 
1352db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1353db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1354db8dac20SDavid S. Miller 
1355db8dac20SDavid S. Miller static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1356db8dac20SDavid S. Miller {
1357db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
1358db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sp);
1359b71d1d42SEric Dumazet 	const struct in6_addr *dest, *src;
1360db8dac20SDavid S. Miller 	__u16 destp, srcp;
1361db8dac20SDavid S. Miller 
1362db8dac20SDavid S. Miller 	dest  = &np->daddr;
1363db8dac20SDavid S. Miller 	src   = &np->rcv_saddr;
1364c720c7e8SEric Dumazet 	destp = ntohs(inet->inet_dport);
1365c720c7e8SEric Dumazet 	srcp  = ntohs(inet->inet_sport);
1366db8dac20SDavid S. Miller 	seq_printf(seq,
1367f86dcc5aSEric Dumazet 		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
136871338aa7SDan Rosenberg 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
1369db8dac20SDavid S. Miller 		   bucket,
1370db8dac20SDavid S. Miller 		   src->s6_addr32[0], src->s6_addr32[1],
1371db8dac20SDavid S. Miller 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
1372db8dac20SDavid S. Miller 		   dest->s6_addr32[0], dest->s6_addr32[1],
1373db8dac20SDavid S. Miller 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
1374db8dac20SDavid S. Miller 		   sp->sk_state,
137531e6d363SEric Dumazet 		   sk_wmem_alloc_get(sp),
137631e6d363SEric Dumazet 		   sk_rmem_alloc_get(sp),
1377db8dac20SDavid S. Miller 		   0, 0L, 0,
1378a7cb5a49SEric W. Biederman 		   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1379a7cb5a49SEric W. Biederman 		   0,
1380db8dac20SDavid S. Miller 		   sock_i_ino(sp),
1381cb61cb9bSEric Dumazet 		   atomic_read(&sp->sk_refcnt), sp,
1382cb61cb9bSEric Dumazet 		   atomic_read(&sp->sk_drops));
1383db8dac20SDavid S. Miller }
1384db8dac20SDavid S. Miller 
1385db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1386db8dac20SDavid S. Miller {
1387db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
1388db8dac20SDavid S. Miller 		seq_printf(seq,
1389db8dac20SDavid S. Miller 			   "  sl  "
1390db8dac20SDavid S. Miller 			   "local_address                         "
1391db8dac20SDavid S. Miller 			   "remote_address                        "
1392db8dac20SDavid S. Miller 			   "st tx_queue rx_queue tr tm->when retrnsmt"
1393cb61cb9bSEric Dumazet 			   "   uid  timeout inode ref pointer drops\n");
1394db8dac20SDavid S. Miller 	else
1395db8dac20SDavid S. Miller 		udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1396db8dac20SDavid S. Miller 	return 0;
1397db8dac20SDavid S. Miller }
1398db8dac20SDavid S. Miller 
139973cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
140073cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
140173cb88ecSArjan van de Ven 	.open     = udp_seq_open,
140273cb88ecSArjan van de Ven 	.read     = seq_read,
140373cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
140473cb88ecSArjan van de Ven 	.release  = seq_release_net
140573cb88ecSArjan van de Ven };
140673cb88ecSArjan van de Ven 
1407db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1408db8dac20SDavid S. Miller 	.name		= "udp6",
1409db8dac20SDavid S. Miller 	.family		= AF_INET6,
1410645ca708SEric Dumazet 	.udp_table	= &udp_table,
141173cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1412dda61925SDenis V. Lunev 	.seq_ops	= {
1413dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1414dda61925SDenis V. Lunev 	},
1415db8dac20SDavid S. Miller };
1416db8dac20SDavid S. Miller 
14172c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1418db8dac20SDavid S. Miller {
14190c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1420db8dac20SDavid S. Miller }
1421db8dac20SDavid S. Miller 
14220c96d8c5SDaniel Lezcano void udp6_proc_exit(struct net *net) {
14230c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1424db8dac20SDavid S. Miller }
1425db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1426db8dac20SDavid S. Miller 
1427db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1428db8dac20SDavid S. Miller 
1429db8dac20SDavid S. Miller struct proto udpv6_prot = {
1430db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1431db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1432db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1433db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1434db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1435db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1436db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1437db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1438db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1439db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1440db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1441f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1442db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1443db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1444719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1445db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1446db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1447db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1448db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1449db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1450db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1451271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1452645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1453db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1454db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1455db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1456db8dac20SDavid S. Miller #endif
1457fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
1458db8dac20SDavid S. Miller };
1459db8dac20SDavid S. Miller 
1460db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1461db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1462db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1463db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1464db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1465db8dac20SDavid S. Miller 	.no_check =  UDP_CSUM_DEFAULT,
1466db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1467db8dac20SDavid S. Miller };
1468db8dac20SDavid S. Miller 
1469db8dac20SDavid S. Miller 
1470db8dac20SDavid S. Miller int __init udpv6_init(void)
1471db8dac20SDavid S. Miller {
1472db8dac20SDavid S. Miller 	int ret;
1473db8dac20SDavid S. Miller 
14743336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
14753336288aSVlad Yasevich 	if (ret)
1476c6b641a4SVlad Yasevich 		goto out;
14773336288aSVlad Yasevich 
1478db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1479db8dac20SDavid S. Miller 	if (ret)
1480db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1481db8dac20SDavid S. Miller out:
1482db8dac20SDavid S. Miller 	return ret;
1483db8dac20SDavid S. Miller 
1484db8dac20SDavid S. Miller out_udpv6_protocol:
1485db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1486db8dac20SDavid S. Miller 	goto out;
1487db8dac20SDavid S. Miller }
1488db8dac20SDavid S. Miller 
1489db8dac20SDavid S. Miller void udpv6_exit(void)
1490db8dac20SDavid S. Miller {
1491db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1492db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1493db8dac20SDavid S. Miller }
1494