xref: /openbmc/linux/net/ipv6/udp.c (revision 8a74ad60)
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>
51db8dac20SDavid S. Miller #include "udp_impl.h"
52db8dac20SDavid S. Miller 
53b2f5e7cdSVlad Yasevich int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
54b2f5e7cdSVlad Yasevich {
55b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
56b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
57c720c7e8SEric Dumazet 	__be32 sk1_rcv_saddr = inet_sk(sk)->inet_rcv_saddr;
58499923c7SVlad Yasevich 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
59b2f5e7cdSVlad Yasevich 	int sk_ipv6only = ipv6_only_sock(sk);
60b2f5e7cdSVlad Yasevich 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
61b2f5e7cdSVlad Yasevich 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
62b2f5e7cdSVlad Yasevich 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
63b2f5e7cdSVlad Yasevich 
64b2f5e7cdSVlad Yasevich 	/* if both are mapped, treat as IPv4 */
65b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
66499923c7SVlad Yasevich 		return (!sk2_ipv6only &&
67c720c7e8SEric Dumazet 			(!sk1_rcv_saddr || !sk2_rcv_saddr ||
68c720c7e8SEric Dumazet 			  sk1_rcv_saddr == sk2_rcv_saddr));
69b2f5e7cdSVlad Yasevich 
70b2f5e7cdSVlad Yasevich 	if (addr_type2 == IPV6_ADDR_ANY &&
71b2f5e7cdSVlad Yasevich 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
72b2f5e7cdSVlad Yasevich 		return 1;
73b2f5e7cdSVlad Yasevich 
74b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_ANY &&
75b2f5e7cdSVlad Yasevich 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
76b2f5e7cdSVlad Yasevich 		return 1;
77b2f5e7cdSVlad Yasevich 
78b2f5e7cdSVlad Yasevich 	if (sk2_rcv_saddr6 &&
79b2f5e7cdSVlad Yasevich 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
80b2f5e7cdSVlad Yasevich 		return 1;
81b2f5e7cdSVlad Yasevich 
82b2f5e7cdSVlad Yasevich 	return 0;
83b2f5e7cdSVlad Yasevich }
84b2f5e7cdSVlad Yasevich 
85d4cada4aSEric Dumazet static unsigned int udp6_portaddr_hash(struct net *net,
86d4cada4aSEric Dumazet 				       const struct in6_addr *addr6,
87d4cada4aSEric Dumazet 				       unsigned int port)
88d4cada4aSEric Dumazet {
89d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
90d4cada4aSEric Dumazet 
91d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
92d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
93856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
940eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
95d4cada4aSEric Dumazet 	else
960eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
97d4cada4aSEric Dumazet 
98d4cada4aSEric Dumazet 	return hash ^ port;
99d4cada4aSEric Dumazet }
100d4cada4aSEric Dumazet 
101d4cada4aSEric Dumazet 
1026ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
103db8dac20SDavid S. Miller {
10430fff923SEric Dumazet 	unsigned int hash2_nulladdr =
10530fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10630fff923SEric Dumazet 	unsigned int hash2_partial =
10730fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0);
10830fff923SEric Dumazet 
109d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
11030fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
11130fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
112db8dac20SDavid S. Miller }
113db8dac20SDavid S. Miller 
114645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
115645ca708SEric Dumazet 				unsigned short hnum,
116db8dac20SDavid S. Miller 				struct in6_addr *saddr, __be16 sport,
117db8dac20SDavid S. Miller 				struct in6_addr *daddr, __be16 dport,
118645ca708SEric Dumazet 				int dif)
119db8dac20SDavid S. Miller {
120645ca708SEric Dumazet 	int score = -1;
121db8dac20SDavid S. Miller 
122d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
123db8dac20SDavid S. Miller 			sk->sk_family == PF_INET6) {
124db8dac20SDavid S. Miller 		struct ipv6_pinfo *np = inet6_sk(sk);
125645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
126645ca708SEric Dumazet 
127645ca708SEric Dumazet 		score = 0;
128c720c7e8SEric Dumazet 		if (inet->inet_dport) {
129c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
130645ca708SEric Dumazet 				return -1;
131db8dac20SDavid S. Miller 			score++;
132db8dac20SDavid S. Miller 		}
133db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->rcv_saddr)) {
134db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
135645ca708SEric Dumazet 				return -1;
136db8dac20SDavid S. Miller 			score++;
137db8dac20SDavid S. Miller 		}
138db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->daddr)) {
139db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->daddr, saddr))
140645ca708SEric Dumazet 				return -1;
141db8dac20SDavid S. Miller 			score++;
142db8dac20SDavid S. Miller 		}
143db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if) {
144db8dac20SDavid S. Miller 			if (sk->sk_bound_dev_if != dif)
145645ca708SEric Dumazet 				return -1;
146db8dac20SDavid S. Miller 			score++;
147db8dac20SDavid S. Miller 		}
148645ca708SEric Dumazet 	}
149645ca708SEric Dumazet 	return score;
150645ca708SEric Dumazet }
151645ca708SEric Dumazet 
152fddc17deSEric Dumazet #define SCORE2_MAX (1 + 1 + 1)
153fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
154fddc17deSEric Dumazet 				const struct in6_addr *saddr, __be16 sport,
155fddc17deSEric Dumazet 				const struct in6_addr *daddr, unsigned short hnum,
156fddc17deSEric Dumazet 				int dif)
157fddc17deSEric Dumazet {
158fddc17deSEric Dumazet 	int score = -1;
159fddc17deSEric Dumazet 
160fddc17deSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
161fddc17deSEric Dumazet 			sk->sk_family == PF_INET6) {
162fddc17deSEric Dumazet 		struct ipv6_pinfo *np = inet6_sk(sk);
163fddc17deSEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
164fddc17deSEric Dumazet 
165fddc17deSEric Dumazet 		if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
166fddc17deSEric Dumazet 			return -1;
167fddc17deSEric Dumazet 		score = 0;
168fddc17deSEric Dumazet 		if (inet->inet_dport) {
169fddc17deSEric Dumazet 			if (inet->inet_dport != sport)
170fddc17deSEric Dumazet 				return -1;
171fddc17deSEric Dumazet 			score++;
172fddc17deSEric Dumazet 		}
173fddc17deSEric Dumazet 		if (!ipv6_addr_any(&np->daddr)) {
174fddc17deSEric Dumazet 			if (!ipv6_addr_equal(&np->daddr, saddr))
175fddc17deSEric Dumazet 				return -1;
176fddc17deSEric Dumazet 			score++;
177fddc17deSEric Dumazet 		}
178fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if) {
179fddc17deSEric Dumazet 			if (sk->sk_bound_dev_if != dif)
180fddc17deSEric Dumazet 				return -1;
181fddc17deSEric Dumazet 			score++;
182fddc17deSEric Dumazet 		}
183fddc17deSEric Dumazet 	}
184fddc17deSEric Dumazet 	return score;
185fddc17deSEric Dumazet }
186fddc17deSEric Dumazet 
187fddc17deSEric Dumazet 
188fddc17deSEric Dumazet /* called with read_rcu_lock() */
189fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
190fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
191fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
192fddc17deSEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
193fddc17deSEric Dumazet {
194fddc17deSEric Dumazet 	struct sock *sk, *result;
195fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
196fddc17deSEric Dumazet 	int score, badness;
197fddc17deSEric Dumazet 
198fddc17deSEric Dumazet begin:
199fddc17deSEric Dumazet 	result = NULL;
200fddc17deSEric Dumazet 	badness = -1;
201fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
202fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
203fddc17deSEric Dumazet 				      daddr, hnum, dif);
204fddc17deSEric Dumazet 		if (score > badness) {
205fddc17deSEric Dumazet 			result = sk;
206fddc17deSEric Dumazet 			badness = score;
207fddc17deSEric Dumazet 			if (score == SCORE2_MAX)
208fddc17deSEric Dumazet 				goto exact_match;
209fddc17deSEric Dumazet 		}
210fddc17deSEric Dumazet 	}
211fddc17deSEric Dumazet 	/*
212fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
213fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
214fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
215fddc17deSEric Dumazet 	 */
216fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
217fddc17deSEric Dumazet 		goto begin;
218fddc17deSEric Dumazet 
219fddc17deSEric Dumazet 	if (result) {
220fddc17deSEric Dumazet exact_match:
221fddc17deSEric Dumazet 		if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
222fddc17deSEric Dumazet 			result = NULL;
223fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
224fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
225fddc17deSEric Dumazet 			sock_put(result);
226fddc17deSEric Dumazet 			goto begin;
227fddc17deSEric Dumazet 		}
228fddc17deSEric Dumazet 	}
229fddc17deSEric Dumazet 	return result;
230fddc17deSEric Dumazet }
231fddc17deSEric Dumazet 
232645ca708SEric Dumazet static struct sock *__udp6_lib_lookup(struct net *net,
233645ca708SEric Dumazet 				      struct in6_addr *saddr, __be16 sport,
234645ca708SEric Dumazet 				      struct in6_addr *daddr, __be16 dport,
235645ca708SEric Dumazet 				      int dif, struct udp_table *udptable)
236645ca708SEric Dumazet {
237271b72c7SEric Dumazet 	struct sock *sk, *result;
23888ab1932SEric Dumazet 	struct hlist_nulls_node *node;
239645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
240fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
241fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
242271b72c7SEric Dumazet 	int score, badness;
243645ca708SEric Dumazet 
244271b72c7SEric Dumazet 	rcu_read_lock();
245fddc17deSEric Dumazet 	if (hslot->count > 10) {
246fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
247fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
248fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
249fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
250fddc17deSEric Dumazet 			goto begin;
251fddc17deSEric Dumazet 
252fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
253fddc17deSEric Dumazet 					  daddr, hnum, dif,
254fddc17deSEric Dumazet 					  hslot2, slot2);
255fddc17deSEric Dumazet 		if (!result) {
256fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, 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 
2621223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
2631223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
264fddc17deSEric Dumazet 						  hslot2, slot2);
265fddc17deSEric Dumazet 		}
266fddc17deSEric Dumazet 		rcu_read_unlock();
267fddc17deSEric Dumazet 		return result;
268fddc17deSEric Dumazet 	}
269271b72c7SEric Dumazet begin:
270271b72c7SEric Dumazet 	result = NULL;
271271b72c7SEric Dumazet 	badness = -1;
27288ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
273645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
274645ca708SEric Dumazet 		if (score > badness) {
275db8dac20SDavid S. Miller 			result = sk;
276db8dac20SDavid S. Miller 			badness = score;
277db8dac20SDavid S. Miller 		}
278db8dac20SDavid S. Miller 	}
27988ab1932SEric Dumazet 	/*
28088ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
28188ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
28288ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
28388ab1932SEric Dumazet 	 */
284fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
28588ab1932SEric Dumazet 		goto begin;
28688ab1932SEric Dumazet 
287271b72c7SEric Dumazet 	if (result) {
288271b72c7SEric Dumazet 		if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
289271b72c7SEric Dumazet 			result = NULL;
290271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
291271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
292271b72c7SEric Dumazet 			sock_put(result);
293271b72c7SEric Dumazet 			goto begin;
294271b72c7SEric Dumazet 		}
295271b72c7SEric Dumazet 	}
296271b72c7SEric Dumazet 	rcu_read_unlock();
297db8dac20SDavid S. Miller 	return result;
298db8dac20SDavid S. Miller }
299db8dac20SDavid S. Miller 
300607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
301607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
302645ca708SEric Dumazet 					  struct udp_table *udptable)
303607c4aafSKOVACS Krisztian {
30423542618SKOVACS Krisztian 	struct sock *sk;
305607c4aafSKOVACS Krisztian 	struct ipv6hdr *iph = ipv6_hdr(skb);
306607c4aafSKOVACS Krisztian 
30723542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
30823542618SKOVACS Krisztian 		return sk;
309adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
310607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
311607c4aafSKOVACS Krisztian 				 udptable);
312607c4aafSKOVACS Krisztian }
313607c4aafSKOVACS Krisztian 
314db8dac20SDavid S. Miller /*
315db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
316db8dac20SDavid S. Miller  * 	return it, otherwise we block.
317db8dac20SDavid S. Miller  */
318db8dac20SDavid S. Miller 
319db8dac20SDavid S. Miller int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
320db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len,
321db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
322db8dac20SDavid S. Miller {
323db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
324db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
325db8dac20SDavid S. Miller 	struct sk_buff *skb;
32681d54ec8SGerrit Renker 	unsigned int ulen;
327db8dac20SDavid S. Miller 	int peeked;
328db8dac20SDavid S. Miller 	int err;
329db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
330f26ba175SWei Yongjun 	int is_udp4;
3318a74ad60SEric Dumazet 	bool slow;
332db8dac20SDavid S. Miller 
333db8dac20SDavid S. Miller 	if (addr_len)
334db8dac20SDavid S. Miller 		*addr_len=sizeof(struct sockaddr_in6);
335db8dac20SDavid S. Miller 
336db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
337db8dac20SDavid S. Miller 		return ipv6_recv_error(sk, msg, len);
338db8dac20SDavid S. Miller 
3394b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
3404b340ae2SBrian Haley 		return ipv6_recv_rxpmtu(sk, msg, len);
3414b340ae2SBrian Haley 
342db8dac20SDavid S. Miller try_again:
343db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
344db8dac20SDavid S. Miller 				  &peeked, &err);
345db8dac20SDavid S. Miller 	if (!skb)
346db8dac20SDavid S. Miller 		goto out;
347db8dac20SDavid S. Miller 
348db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
34981d54ec8SGerrit Renker 	if (len > ulen)
35081d54ec8SGerrit Renker 		len = ulen;
35181d54ec8SGerrit Renker 	else if (len < ulen)
352db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
353db8dac20SDavid S. Miller 
354f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
355f26ba175SWei Yongjun 
356db8dac20SDavid S. Miller 	/*
357db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
358db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
359db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
360db8dac20SDavid S. Miller 	 */
361db8dac20SDavid S. Miller 
36281d54ec8SGerrit Renker 	if (len < ulen || UDP_SKB_CB(skb)->partial_cov) {
363db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
364db8dac20SDavid S. Miller 			goto csum_copy_err;
365db8dac20SDavid S. Miller 	}
366db8dac20SDavid S. Miller 
367db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
368db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
36981d54ec8SGerrit Renker 					      msg->msg_iov,len);
370db8dac20SDavid S. Miller 	else {
371db8dac20SDavid S. Miller 		err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
372db8dac20SDavid S. Miller 		if (err == -EINVAL)
373db8dac20SDavid S. Miller 			goto csum_copy_err;
374db8dac20SDavid S. Miller 	}
375db8dac20SDavid S. Miller 	if (err)
376db8dac20SDavid S. Miller 		goto out_free;
377db8dac20SDavid S. Miller 
378f26ba175SWei Yongjun 	if (!peeked) {
379f26ba175SWei Yongjun 		if (is_udp4)
380f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
381f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
382f26ba175SWei Yongjun 		else
383235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
384235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
385f26ba175SWei Yongjun 	}
386db8dac20SDavid S. Miller 
3873b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
388db8dac20SDavid S. Miller 
389db8dac20SDavid S. Miller 	/* Copy the address. */
390db8dac20SDavid S. Miller 	if (msg->msg_name) {
391db8dac20SDavid S. Miller 		struct sockaddr_in6 *sin6;
392db8dac20SDavid S. Miller 
393db8dac20SDavid S. Miller 		sin6 = (struct sockaddr_in6 *) msg->msg_name;
394db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
395db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
396db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
397db8dac20SDavid S. Miller 		sin6->sin6_scope_id = 0;
398db8dac20SDavid S. Miller 
399f26ba175SWei Yongjun 		if (is_udp4)
400b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
401b301e82cSBrian Haley 					       &sin6->sin6_addr);
402db8dac20SDavid S. Miller 		else {
403db8dac20SDavid S. Miller 			ipv6_addr_copy(&sin6->sin6_addr,
404db8dac20SDavid S. Miller 				       &ipv6_hdr(skb)->saddr);
405db8dac20SDavid S. Miller 			if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
406db8dac20SDavid S. Miller 				sin6->sin6_scope_id = IP6CB(skb)->iif;
407db8dac20SDavid S. Miller 		}
408db8dac20SDavid S. Miller 
409db8dac20SDavid S. Miller 	}
410f26ba175SWei Yongjun 	if (is_udp4) {
411db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
412db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
413db8dac20SDavid S. Miller 	} else {
414db8dac20SDavid S. Miller 		if (np->rxopt.all)
415db8dac20SDavid S. Miller 			datagram_recv_ctl(sk, msg, skb);
416db8dac20SDavid S. Miller 	}
417db8dac20SDavid S. Miller 
41881d54ec8SGerrit Renker 	err = len;
419db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
420db8dac20SDavid S. Miller 		err = ulen;
421db8dac20SDavid S. Miller 
422db8dac20SDavid S. Miller out_free:
4239d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
424db8dac20SDavid S. Miller out:
425db8dac20SDavid S. Miller 	return err;
426db8dac20SDavid S. Miller 
427db8dac20SDavid S. Miller csum_copy_err:
4288a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
4290856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
4300856f939SWei Yongjun 		if (is_udp4)
4310856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
4320856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4330856f939SWei Yongjun 		else
4340856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
4350856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4360856f939SWei Yongjun 	}
4378a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
438db8dac20SDavid S. Miller 
439db8dac20SDavid S. Miller 	if (flags & MSG_DONTWAIT)
440db8dac20SDavid S. Miller 		return -EAGAIN;
441db8dac20SDavid S. Miller 	goto try_again;
442db8dac20SDavid S. Miller }
443db8dac20SDavid S. Miller 
444db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
445d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
446645ca708SEric Dumazet 		    struct udp_table *udptable)
447db8dac20SDavid S. Miller {
448db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
449db8dac20SDavid S. Miller 	struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
450db8dac20SDavid S. Miller 	struct in6_addr *saddr = &hdr->saddr;
451db8dac20SDavid S. Miller 	struct in6_addr *daddr = &hdr->daddr;
452db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr*)(skb->data+offset);
453db8dac20SDavid S. Miller 	struct sock *sk;
454db8dac20SDavid S. Miller 	int err;
455db8dac20SDavid S. Miller 
456c346dca1SYOSHIFUJI Hideaki 	sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
457db8dac20SDavid S. Miller 			       saddr, uh->source, inet6_iif(skb), udptable);
458db8dac20SDavid S. Miller 	if (sk == NULL)
459db8dac20SDavid S. Miller 		return;
460db8dac20SDavid S. Miller 
461db8dac20SDavid S. Miller 	np = inet6_sk(sk);
462db8dac20SDavid S. Miller 
463db8dac20SDavid S. Miller 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
464db8dac20SDavid S. Miller 		goto out;
465db8dac20SDavid S. Miller 
466db8dac20SDavid S. Miller 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
467db8dac20SDavid S. Miller 		goto out;
468db8dac20SDavid S. Miller 
469db8dac20SDavid S. Miller 	if (np->recverr)
470db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
471db8dac20SDavid S. Miller 
472db8dac20SDavid S. Miller 	sk->sk_err = err;
473db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
474db8dac20SDavid S. Miller out:
475db8dac20SDavid S. Miller 	sock_put(sk);
476db8dac20SDavid S. Miller }
477db8dac20SDavid S. Miller 
478db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
479d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
480d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info     )
481db8dac20SDavid S. Miller {
482645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
483db8dac20SDavid S. Miller }
484db8dac20SDavid S. Miller 
485db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
486db8dac20SDavid S. Miller {
487db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
488db8dac20SDavid S. Miller 	int rc;
489db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
490db8dac20SDavid S. Miller 
491db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
492db8dac20SDavid S. Miller 		goto drop;
493db8dac20SDavid S. Miller 
494db8dac20SDavid S. Miller 	/*
495db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
496db8dac20SDavid S. Miller 	 */
497db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
498db8dac20SDavid S. Miller 
499db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
500db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
501db8dac20SDavid S. Miller 				" %d while full coverage %d requested\n",
502db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, skb->len);
503db8dac20SDavid S. Miller 			goto drop;
504db8dac20SDavid S. Miller 		}
505db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
506db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
507db8dac20SDavid S. Miller 						    "too small, need min %d\n",
508db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
509db8dac20SDavid S. Miller 			goto drop;
510db8dac20SDavid S. Miller 		}
511db8dac20SDavid S. Miller 	}
512db8dac20SDavid S. Miller 
513db8dac20SDavid S. Miller 	if (sk->sk_filter) {
514db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
515db8dac20SDavid S. Miller 			goto drop;
516db8dac20SDavid S. Miller 	}
517db8dac20SDavid S. Miller 
518f84af32cSEric Dumazet 	if ((rc = ip_queue_rcv_skb(sk, skb)) < 0) {
519db8dac20SDavid S. Miller 		/* Note that an ENOMEM error is charged twice */
520766e9037SEric Dumazet 		if (rc == -ENOMEM)
521ef28d1a2SPavel Emelyanov 			UDP6_INC_STATS_BH(sock_net(sk),
522ef28d1a2SPavel Emelyanov 					UDP_MIB_RCVBUFERRORS, is_udplite);
5238edf19c2SEric Dumazet 		goto drop_no_sk_drops_inc;
524db8dac20SDavid S. Miller 	}
525db8dac20SDavid S. Miller 
526db8dac20SDavid S. Miller 	return 0;
527db8dac20SDavid S. Miller drop:
5288edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
5298edf19c2SEric Dumazet drop_no_sk_drops_inc:
530ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
531db8dac20SDavid S. Miller 	kfree_skb(skb);
532db8dac20SDavid S. Miller 	return -1;
533db8dac20SDavid S. Miller }
534db8dac20SDavid S. Miller 
535920a4611SEric Dumazet static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
536db8dac20SDavid S. Miller 				      __be16 loc_port, struct in6_addr *loc_addr,
537db8dac20SDavid S. Miller 				      __be16 rmt_port, struct in6_addr *rmt_addr,
538db8dac20SDavid S. Miller 				      int dif)
539db8dac20SDavid S. Miller {
54088ab1932SEric Dumazet 	struct hlist_nulls_node *node;
541db8dac20SDavid S. Miller 	struct sock *s = sk;
542db8dac20SDavid S. Miller 	unsigned short num = ntohs(loc_port);
543db8dac20SDavid S. Miller 
54488ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
545db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
546db8dac20SDavid S. Miller 
547920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net))
548b8ad0cbcSDaniel Lezcano 			continue;
549b8ad0cbcSDaniel Lezcano 
550d4cada4aSEric Dumazet 		if (udp_sk(s)->udp_port_hash == num &&
551d4cada4aSEric Dumazet 		    s->sk_family == PF_INET6) {
552db8dac20SDavid S. Miller 			struct ipv6_pinfo *np = inet6_sk(s);
553c720c7e8SEric Dumazet 			if (inet->inet_dport) {
554c720c7e8SEric Dumazet 				if (inet->inet_dport != rmt_port)
555db8dac20SDavid S. Miller 					continue;
556db8dac20SDavid S. Miller 			}
557db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->daddr) &&
558db8dac20SDavid S. Miller 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
559db8dac20SDavid S. Miller 				continue;
560db8dac20SDavid S. Miller 
561db8dac20SDavid S. Miller 			if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
562db8dac20SDavid S. Miller 				continue;
563db8dac20SDavid S. Miller 
564db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->rcv_saddr)) {
565db8dac20SDavid S. Miller 				if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
566db8dac20SDavid S. Miller 					continue;
567db8dac20SDavid S. Miller 			}
568db8dac20SDavid S. Miller 			if (!inet6_mc_check(s, loc_addr, rmt_addr))
569db8dac20SDavid S. Miller 				continue;
570db8dac20SDavid S. Miller 			return s;
571db8dac20SDavid S. Miller 		}
572db8dac20SDavid S. Miller 	}
573db8dac20SDavid S. Miller 	return NULL;
574db8dac20SDavid S. Miller }
575db8dac20SDavid S. Miller 
576a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
577a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
578a1ab77f9SEric Dumazet {
579a1ab77f9SEric Dumazet 	unsigned int i;
580a1ab77f9SEric Dumazet 	struct sock *sk;
581a1ab77f9SEric Dumazet 	struct sk_buff *skb1;
582a1ab77f9SEric Dumazet 
583a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
584a1ab77f9SEric Dumazet 		skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
585a1ab77f9SEric Dumazet 
586a1ab77f9SEric Dumazet 		sk = stack[i];
587f6b8f32cSEric Dumazet 		if (skb1) {
588c377411fSEric Dumazet 			if (sk_rcvqueues_full(sk, skb)) {
589c377411fSEric Dumazet 				kfree_skb(skb1);
590c377411fSEric Dumazet 				goto drop;
591c377411fSEric Dumazet 			}
592a1ab77f9SEric Dumazet 			bh_lock_sock(sk);
593a1ab77f9SEric Dumazet 			if (!sock_owned_by_user(sk))
594a1ab77f9SEric Dumazet 				udpv6_queue_rcv_skb(sk, skb1);
595a3a858ffSZhu Yi 			else if (sk_add_backlog(sk, skb1)) {
59655349790SZhu Yi 				kfree_skb(skb1);
597a1ab77f9SEric Dumazet 				bh_unlock_sock(sk);
59855349790SZhu Yi 				goto drop;
59955349790SZhu Yi 			}
60055349790SZhu Yi 			bh_unlock_sock(sk);
60155349790SZhu Yi 			continue;
60255349790SZhu Yi 		}
60355349790SZhu Yi drop:
604f6b8f32cSEric Dumazet 		atomic_inc(&sk->sk_drops);
605f6b8f32cSEric Dumazet 		UDP6_INC_STATS_BH(sock_net(sk),
606f6b8f32cSEric Dumazet 				UDP_MIB_RCVBUFERRORS, IS_UDPLITE(sk));
607f6b8f32cSEric Dumazet 		UDP6_INC_STATS_BH(sock_net(sk),
608f6b8f32cSEric Dumazet 				UDP_MIB_INERRORS, IS_UDPLITE(sk));
609a1ab77f9SEric Dumazet 	}
610a1ab77f9SEric Dumazet }
611db8dac20SDavid S. Miller /*
612db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
613db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
614db8dac20SDavid S. Miller  */
615e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
616e3163493SPavel Emelyanov 		struct in6_addr *saddr, struct in6_addr *daddr,
617645ca708SEric Dumazet 		struct udp_table *udptable)
618db8dac20SDavid S. Miller {
619a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
620db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
621f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
622db8dac20SDavid S. Miller 	int dif;
623a1ab77f9SEric Dumazet 	unsigned int i, count = 0;
624db8dac20SDavid S. Miller 
625645ca708SEric Dumazet 	spin_lock(&hslot->lock);
62688ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
627db8dac20SDavid S. Miller 	dif = inet6_iif(skb);
628920a4611SEric Dumazet 	sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
629a1ab77f9SEric Dumazet 	while (sk) {
630a1ab77f9SEric Dumazet 		stack[count++] = sk;
631a1ab77f9SEric Dumazet 		sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
632a1ab77f9SEric Dumazet 				       uh->source, saddr, dif);
633a1ab77f9SEric Dumazet 		if (unlikely(count == ARRAY_SIZE(stack))) {
634a1ab77f9SEric Dumazet 			if (!sk)
635a1ab77f9SEric Dumazet 				break;
636a1ab77f9SEric Dumazet 			flush_stack(stack, count, skb, ~0);
637a1ab77f9SEric Dumazet 			count = 0;
638db8dac20SDavid S. Miller 		}
639a1ab77f9SEric Dumazet 	}
640a1ab77f9SEric Dumazet 	/*
641a1ab77f9SEric Dumazet 	 * before releasing the lock, we must take reference on sockets
642a1ab77f9SEric Dumazet 	 */
643a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++)
644a1ab77f9SEric Dumazet 		sock_hold(stack[i]);
645db8dac20SDavid S. Miller 
646645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
647a1ab77f9SEric Dumazet 
648a1ab77f9SEric Dumazet 	if (count) {
649a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
650a1ab77f9SEric Dumazet 
651a1ab77f9SEric Dumazet 		for (i = 0; i < count; i++)
652a1ab77f9SEric Dumazet 			sock_put(stack[i]);
653a1ab77f9SEric Dumazet 	} else {
654a1ab77f9SEric Dumazet 		kfree_skb(skb);
655a1ab77f9SEric Dumazet 	}
656db8dac20SDavid S. Miller 	return 0;
657db8dac20SDavid S. Miller }
658db8dac20SDavid S. Miller 
659db8dac20SDavid S. Miller static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
660db8dac20SDavid S. Miller 				 int proto)
661db8dac20SDavid S. Miller {
662db8dac20SDavid S. Miller 	int err;
663db8dac20SDavid S. Miller 
664db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
665db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
666db8dac20SDavid S. Miller 
667db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
668db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
669db8dac20SDavid S. Miller 		if (err)
670db8dac20SDavid S. Miller 			return err;
671db8dac20SDavid S. Miller 	}
672db8dac20SDavid S. Miller 
673db8dac20SDavid S. Miller 	if (uh->check == 0) {
674db8dac20SDavid S. Miller 		/* RFC 2460 section 8.1 says that we SHOULD log
675db8dac20SDavid S. Miller 		   this error. Well, it is reasonable.
676db8dac20SDavid S. Miller 		 */
677db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
678db8dac20SDavid S. Miller 		return 1;
679db8dac20SDavid S. Miller 	}
680db8dac20SDavid S. Miller 	if (skb->ip_summed == CHECKSUM_COMPLETE &&
681db8dac20SDavid S. Miller 	    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
682db8dac20SDavid S. Miller 			     skb->len, proto, skb->csum))
683db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_UNNECESSARY;
684db8dac20SDavid S. Miller 
685db8dac20SDavid S. Miller 	if (!skb_csum_unnecessary(skb))
686db8dac20SDavid S. Miller 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
687db8dac20SDavid S. Miller 							 &ipv6_hdr(skb)->daddr,
688db8dac20SDavid S. Miller 							 skb->len, proto, 0));
689db8dac20SDavid S. Miller 
690db8dac20SDavid S. Miller 	return 0;
691db8dac20SDavid S. Miller }
692db8dac20SDavid S. Miller 
693645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
694db8dac20SDavid S. Miller 		   int proto)
695db8dac20SDavid S. Miller {
6963ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
697db8dac20SDavid S. Miller 	struct sock *sk;
698db8dac20SDavid S. Miller 	struct udphdr *uh;
699db8dac20SDavid S. Miller 	struct in6_addr *saddr, *daddr;
700db8dac20SDavid S. Miller 	u32 ulen = 0;
701db8dac20SDavid S. Miller 
702db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
703d6bc0149SBjørn Mork 		goto discard;
704db8dac20SDavid S. Miller 
705db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
706db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
707db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
708db8dac20SDavid S. Miller 
709db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
710db8dac20SDavid S. Miller 	if (ulen > skb->len)
711db8dac20SDavid S. Miller 		goto short_packet;
712db8dac20SDavid S. Miller 
713db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
714db8dac20SDavid S. Miller 		/* UDP validates ulen. */
715db8dac20SDavid S. Miller 
716db8dac20SDavid S. Miller 		/* Check for jumbo payload */
717db8dac20SDavid S. Miller 		if (ulen == 0)
718db8dac20SDavid S. Miller 			ulen = skb->len;
719db8dac20SDavid S. Miller 
720db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
721db8dac20SDavid S. Miller 			goto short_packet;
722db8dac20SDavid S. Miller 
723db8dac20SDavid S. Miller 		if (ulen < skb->len) {
724db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
725db8dac20SDavid S. Miller 				goto short_packet;
726db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
727db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
728db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
729db8dac20SDavid S. Miller 		}
730db8dac20SDavid S. Miller 	}
731db8dac20SDavid S. Miller 
732db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
733db8dac20SDavid S. Miller 		goto discard;
734db8dac20SDavid S. Miller 
735db8dac20SDavid S. Miller 	/*
736db8dac20SDavid S. Miller 	 *	Multicast receive code
737db8dac20SDavid S. Miller 	 */
738db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
739e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
740e3163493SPavel Emelyanov 				saddr, daddr, udptable);
741db8dac20SDavid S. Miller 
742db8dac20SDavid S. Miller 	/* Unicast */
743db8dac20SDavid S. Miller 
744db8dac20SDavid S. Miller 	/*
745db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
746db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
747db8dac20SDavid S. Miller 	 */
748607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
749db8dac20SDavid S. Miller 
750db8dac20SDavid S. Miller 	if (sk == NULL) {
751db8dac20SDavid S. Miller 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
752db8dac20SDavid S. Miller 			goto discard;
753db8dac20SDavid S. Miller 
754db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
755db8dac20SDavid S. Miller 			goto discard;
756ef28d1a2SPavel Emelyanov 		UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
757ef28d1a2SPavel Emelyanov 				proto == IPPROTO_UDPLITE);
758db8dac20SDavid S. Miller 
7593ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
760db8dac20SDavid S. Miller 
761db8dac20SDavid S. Miller 		kfree_skb(skb);
762db8dac20SDavid S. Miller 		return 0;
763db8dac20SDavid S. Miller 	}
764db8dac20SDavid S. Miller 
765db8dac20SDavid S. Miller 	/* deliver */
766db8dac20SDavid S. Miller 
767c377411fSEric Dumazet 	if (sk_rcvqueues_full(sk, skb)) {
768c377411fSEric Dumazet 		sock_put(sk);
769c377411fSEric Dumazet 		goto discard;
770c377411fSEric Dumazet 	}
771d97106eaSHerbert Xu 	bh_lock_sock(sk);
772db8dac20SDavid S. Miller 	if (!sock_owned_by_user(sk))
773db8dac20SDavid S. Miller 		udpv6_queue_rcv_skb(sk, skb);
774a3a858ffSZhu Yi 	else if (sk_add_backlog(sk, skb)) {
77555349790SZhu Yi 		atomic_inc(&sk->sk_drops);
77655349790SZhu Yi 		bh_unlock_sock(sk);
77755349790SZhu Yi 		sock_put(sk);
77855349790SZhu Yi 		goto discard;
77955349790SZhu Yi 	}
780db8dac20SDavid S. Miller 	bh_unlock_sock(sk);
781db8dac20SDavid S. Miller 	sock_put(sk);
782db8dac20SDavid S. Miller 	return 0;
783db8dac20SDavid S. Miller 
784db8dac20SDavid S. Miller short_packet:
785d6bc0149SBjørn Mork 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
786db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
787d6bc0149SBjørn Mork 		       saddr,
788d6bc0149SBjørn Mork 		       ntohs(uh->source),
789d6bc0149SBjørn Mork 		       ulen,
790d6bc0149SBjørn Mork 		       skb->len,
791d6bc0149SBjørn Mork 		       daddr,
792d6bc0149SBjørn Mork 		       ntohs(uh->dest));
793db8dac20SDavid S. Miller 
794db8dac20SDavid S. Miller discard:
795ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
796db8dac20SDavid S. Miller 	kfree_skb(skb);
797db8dac20SDavid S. Miller 	return 0;
798db8dac20SDavid S. Miller }
799db8dac20SDavid S. Miller 
800db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
801db8dac20SDavid S. Miller {
802645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
803db8dac20SDavid S. Miller }
804db8dac20SDavid S. Miller 
805db8dac20SDavid S. Miller /*
806db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
807db8dac20SDavid S. Miller  */
808db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
809db8dac20SDavid S. Miller {
810db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
811db8dac20SDavid S. Miller 
81236d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
81336d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
81436d926b9SDenis V. Lunev 	else if (up->pending) {
815db8dac20SDavid S. Miller 		up->len = 0;
816db8dac20SDavid S. Miller 		up->pending = 0;
817db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
818db8dac20SDavid S. Miller 	}
819db8dac20SDavid S. Miller }
820db8dac20SDavid S. Miller 
821493c6be3SSridhar Samudrala /**
822493c6be3SSridhar Samudrala  * 	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
823493c6be3SSridhar Samudrala  * 	@sk: 	socket we are sending on
824493c6be3SSridhar Samudrala  * 	@skb: 	sk_buff containing the filled-in UDP header
825493c6be3SSridhar Samudrala  * 	        (checksum field must be zeroed out)
826493c6be3SSridhar Samudrala  */
827493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
828493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
829493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
830493c6be3SSridhar Samudrala {
831493c6be3SSridhar Samudrala 	unsigned int offset;
832493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
833493c6be3SSridhar Samudrala 	__wsum csum = 0;
834493c6be3SSridhar Samudrala 
835493c6be3SSridhar Samudrala 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
836493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
837493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
838493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
839493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
840493c6be3SSridhar Samudrala 	} else {
841493c6be3SSridhar Samudrala 		/*
842493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
843493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
844493c6be3SSridhar Samudrala 		 * should be together
845493c6be3SSridhar Samudrala 		 */
846493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
847493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
848493c6be3SSridhar Samudrala 
849493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
850493c6be3SSridhar Samudrala 
851493c6be3SSridhar Samudrala 		skb_queue_walk(&sk->sk_write_queue, skb) {
852493c6be3SSridhar Samudrala 			csum = csum_add(csum, skb->csum);
853493c6be3SSridhar Samudrala 		}
854493c6be3SSridhar Samudrala 
855493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
856493c6be3SSridhar Samudrala 					    csum);
857493c6be3SSridhar Samudrala 		if (uh->check == 0)
858493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
859493c6be3SSridhar Samudrala 	}
860493c6be3SSridhar Samudrala }
861493c6be3SSridhar Samudrala 
862db8dac20SDavid S. Miller /*
863db8dac20SDavid S. Miller  *	Sending
864db8dac20SDavid S. Miller  */
865db8dac20SDavid S. Miller 
866db8dac20SDavid S. Miller static int udp_v6_push_pending_frames(struct sock *sk)
867db8dac20SDavid S. Miller {
868db8dac20SDavid S. Miller 	struct sk_buff *skb;
869db8dac20SDavid S. Miller 	struct udphdr *uh;
870db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
871db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
872db8dac20SDavid S. Miller 	struct flowi *fl = &inet->cork.fl;
873db8dac20SDavid S. Miller 	int err = 0;
874db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
875db8dac20SDavid S. Miller 	__wsum csum = 0;
876db8dac20SDavid S. Miller 
877db8dac20SDavid S. Miller 	/* Grab the skbuff where UDP header space exists. */
878db8dac20SDavid S. Miller 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
879db8dac20SDavid S. Miller 		goto out;
880db8dac20SDavid S. Miller 
881db8dac20SDavid S. Miller 	/*
882db8dac20SDavid S. Miller 	 * Create a UDP header
883db8dac20SDavid S. Miller 	 */
884db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
885db8dac20SDavid S. Miller 	uh->source = fl->fl_ip_sport;
886db8dac20SDavid S. Miller 	uh->dest = fl->fl_ip_dport;
887db8dac20SDavid S. Miller 	uh->len = htons(up->len);
888db8dac20SDavid S. Miller 	uh->check = 0;
889db8dac20SDavid S. Miller 
890db8dac20SDavid S. Miller 	if (is_udplite)
891db8dac20SDavid S. Miller 		csum = udplite_csum_outgoing(sk, skb);
892493c6be3SSridhar Samudrala 	else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
893493c6be3SSridhar Samudrala 		udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst,
894493c6be3SSridhar Samudrala 				     up->len);
895493c6be3SSridhar Samudrala 		goto send;
896493c6be3SSridhar Samudrala 	} else
897db8dac20SDavid S. Miller 		csum = udp_csum_outgoing(sk, skb);
898db8dac20SDavid S. Miller 
899db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
900db8dac20SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
901db8dac20SDavid S. Miller 				    up->len, fl->proto, csum   );
902db8dac20SDavid S. Miller 	if (uh->check == 0)
903db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
904db8dac20SDavid S. Miller 
905493c6be3SSridhar Samudrala send:
906db8dac20SDavid S. Miller 	err = ip6_push_pending_frames(sk);
9076ce9e7b5SEric Dumazet 	if (err) {
9086ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
9096ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
9106ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
9116ce9e7b5SEric Dumazet 			err = 0;
9126ce9e7b5SEric Dumazet 		}
9136ce9e7b5SEric Dumazet 	} else
9146ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
9156ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
916db8dac20SDavid S. Miller out:
917db8dac20SDavid S. Miller 	up->len = 0;
918db8dac20SDavid S. Miller 	up->pending = 0;
919db8dac20SDavid S. Miller 	return err;
920db8dac20SDavid S. Miller }
921db8dac20SDavid S. Miller 
922db8dac20SDavid S. Miller int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
923db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len)
924db8dac20SDavid S. Miller {
925db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
926db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
927db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
928db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
929db8dac20SDavid S. Miller 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
930db8dac20SDavid S. Miller 	struct in6_addr *daddr, *final_p = NULL, final;
931db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
932db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
933db8dac20SDavid S. Miller 	struct flowi fl;
934db8dac20SDavid S. Miller 	struct dst_entry *dst;
935db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
936db8dac20SDavid S. Miller 	int ulen = len;
937db8dac20SDavid S. Miller 	int hlimit = -1;
938db8dac20SDavid S. Miller 	int tclass = -1;
93913b52cd4SBrian Haley 	int dontfrag = -1;
940db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
941db8dac20SDavid S. Miller 	int err;
942db8dac20SDavid S. Miller 	int connected = 0;
943db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
944db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
945db8dac20SDavid S. Miller 
946db8dac20SDavid S. Miller 	/* destination address check */
947db8dac20SDavid S. Miller 	if (sin6) {
948db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
949db8dac20SDavid S. Miller 			return -EINVAL;
950db8dac20SDavid S. Miller 
951db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
952db8dac20SDavid S. Miller 		case AF_INET6:
953db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
954db8dac20SDavid S. Miller 				return -EINVAL;
955db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
956db8dac20SDavid S. Miller 			break;
957db8dac20SDavid S. Miller 		case AF_INET:
958db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
959db8dac20SDavid S. Miller 		case AF_UNSPEC:
960db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
961db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
962db8dac20SDavid S. Miller 			daddr = NULL;
963db8dac20SDavid S. Miller 			break;
964db8dac20SDavid S. Miller 		default:
965db8dac20SDavid S. Miller 			return -EINVAL;
966db8dac20SDavid S. Miller 		}
967db8dac20SDavid S. Miller 	} else if (!up->pending) {
968db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
969db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
970db8dac20SDavid S. Miller 		daddr = &np->daddr;
971db8dac20SDavid S. Miller 	} else
972db8dac20SDavid S. Miller 		daddr = NULL;
973db8dac20SDavid S. Miller 
974db8dac20SDavid S. Miller 	if (daddr) {
975db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
976db8dac20SDavid S. Miller 			struct sockaddr_in sin;
977db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
978c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
979db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
980db8dac20SDavid S. Miller 			msg->msg_name = &sin;
981db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
982db8dac20SDavid S. Miller do_udp_sendmsg:
983db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
984db8dac20SDavid S. Miller 				return -ENETUNREACH;
985db8dac20SDavid S. Miller 			return udp_sendmsg(iocb, sk, msg, len);
986db8dac20SDavid S. Miller 		}
987db8dac20SDavid S. Miller 	}
988db8dac20SDavid S. Miller 
989db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
990db8dac20SDavid S. Miller 		return udp_sendmsg(iocb, sk, msg, len);
991db8dac20SDavid S. Miller 
992db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
993db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
994db8dac20SDavid S. Miller 	   */
995db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
996db8dac20SDavid S. Miller 		return -EMSGSIZE;
997db8dac20SDavid S. Miller 
998db8dac20SDavid S. Miller 	if (up->pending) {
999db8dac20SDavid S. Miller 		/*
1000db8dac20SDavid S. Miller 		 * There are pending frames.
1001db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1002db8dac20SDavid S. Miller 		 */
1003db8dac20SDavid S. Miller 		lock_sock(sk);
1004db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1005db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1006db8dac20SDavid S. Miller 				release_sock(sk);
1007db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1008db8dac20SDavid S. Miller 			}
1009db8dac20SDavid S. Miller 			dst = NULL;
1010db8dac20SDavid S. Miller 			goto do_append_data;
1011db8dac20SDavid S. Miller 		}
1012db8dac20SDavid S. Miller 		release_sock(sk);
1013db8dac20SDavid S. Miller 	}
1014db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1015db8dac20SDavid S. Miller 
1016db8dac20SDavid S. Miller 	memset(&fl, 0, sizeof(fl));
1017db8dac20SDavid S. Miller 
1018db8dac20SDavid S. Miller 	if (sin6) {
1019db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1020db8dac20SDavid S. Miller 			return -EINVAL;
1021db8dac20SDavid S. Miller 
1022db8dac20SDavid S. Miller 		fl.fl_ip_dport = sin6->sin6_port;
1023db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1024db8dac20SDavid S. Miller 
1025db8dac20SDavid S. Miller 		if (np->sndflow) {
1026db8dac20SDavid S. Miller 			fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1027db8dac20SDavid S. Miller 			if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
1028db8dac20SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
1029db8dac20SDavid S. Miller 				if (flowlabel == NULL)
1030db8dac20SDavid S. Miller 					return -EINVAL;
1031db8dac20SDavid S. Miller 				daddr = &flowlabel->dst;
1032db8dac20SDavid S. Miller 			}
1033db8dac20SDavid S. Miller 		}
1034db8dac20SDavid S. Miller 
1035db8dac20SDavid S. Miller 		/*
1036db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1037db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1038db8dac20SDavid S. Miller 		 */
1039db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1040db8dac20SDavid S. Miller 		    ipv6_addr_equal(daddr, &np->daddr))
1041db8dac20SDavid S. Miller 			daddr = &np->daddr;
1042db8dac20SDavid S. Miller 
1043db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1044db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1045db8dac20SDavid S. Miller 		    ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
1046db8dac20SDavid S. Miller 			fl.oif = sin6->sin6_scope_id;
1047db8dac20SDavid S. Miller 	} else {
1048db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1049db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1050db8dac20SDavid S. Miller 
1051c720c7e8SEric Dumazet 		fl.fl_ip_dport = inet->inet_dport;
1052db8dac20SDavid S. Miller 		daddr = &np->daddr;
1053db8dac20SDavid S. Miller 		fl.fl6_flowlabel = np->flow_label;
1054db8dac20SDavid S. Miller 		connected = 1;
1055db8dac20SDavid S. Miller 	}
1056db8dac20SDavid S. Miller 
1057db8dac20SDavid S. Miller 	if (!fl.oif)
1058db8dac20SDavid S. Miller 		fl.oif = sk->sk_bound_dev_if;
1059db8dac20SDavid S. Miller 
10609f690db7SYang Hongyang 	if (!fl.oif)
10619f690db7SYang Hongyang 		fl.oif = np->sticky_pktinfo.ipi6_ifindex;
10629f690db7SYang Hongyang 
106351953d5bSBrian Haley 	fl.mark = sk->sk_mark;
106451953d5bSBrian Haley 
1065db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1066db8dac20SDavid S. Miller 		opt = &opt_space;
1067db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1068db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1069db8dac20SDavid S. Miller 
107013b52cd4SBrian Haley 		err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit,
107113b52cd4SBrian Haley 					&tclass, &dontfrag);
1072db8dac20SDavid S. Miller 		if (err < 0) {
1073db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1074db8dac20SDavid S. Miller 			return err;
1075db8dac20SDavid S. Miller 		}
1076db8dac20SDavid S. Miller 		if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1077db8dac20SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
1078db8dac20SDavid S. Miller 			if (flowlabel == NULL)
1079db8dac20SDavid S. Miller 				return -EINVAL;
1080db8dac20SDavid S. Miller 		}
1081db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1082db8dac20SDavid S. Miller 			opt = NULL;
1083db8dac20SDavid S. Miller 		connected = 0;
1084db8dac20SDavid S. Miller 	}
1085db8dac20SDavid S. Miller 	if (opt == NULL)
1086db8dac20SDavid S. Miller 		opt = np->opt;
1087db8dac20SDavid S. Miller 	if (flowlabel)
1088db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1089db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1090db8dac20SDavid S. Miller 
1091db8dac20SDavid S. Miller 	fl.proto = sk->sk_protocol;
1092876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
1093db8dac20SDavid S. Miller 		ipv6_addr_copy(&fl.fl6_dst, daddr);
1094876c7f41SBrian Haley 	else
1095876c7f41SBrian Haley 		fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1096db8dac20SDavid S. Miller 	if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
1097db8dac20SDavid S. Miller 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
1098c720c7e8SEric Dumazet 	fl.fl_ip_sport = inet->inet_sport;
1099db8dac20SDavid S. Miller 
1100db8dac20SDavid S. Miller 	/* merge ip6_build_xmit from ip6_output */
1101db8dac20SDavid S. Miller 	if (opt && opt->srcrt) {
1102db8dac20SDavid S. Miller 		struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
1103db8dac20SDavid S. Miller 		ipv6_addr_copy(&final, &fl.fl6_dst);
1104db8dac20SDavid S. Miller 		ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1105db8dac20SDavid S. Miller 		final_p = &final;
1106db8dac20SDavid S. Miller 		connected = 0;
1107db8dac20SDavid S. Miller 	}
1108db8dac20SDavid S. Miller 
1109db8dac20SDavid S. Miller 	if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
1110db8dac20SDavid S. Miller 		fl.oif = np->mcast_oif;
1111db8dac20SDavid S. Miller 		connected = 0;
1112db8dac20SDavid S. Miller 	}
1113db8dac20SDavid S. Miller 
1114db8dac20SDavid S. Miller 	security_sk_classify_flow(sk, &fl);
1115db8dac20SDavid S. Miller 
1116db8dac20SDavid S. Miller 	err = ip6_sk_dst_lookup(sk, &dst, &fl);
1117db8dac20SDavid S. Miller 	if (err)
1118db8dac20SDavid S. Miller 		goto out;
1119db8dac20SDavid S. Miller 	if (final_p)
1120db8dac20SDavid S. Miller 		ipv6_addr_copy(&fl.fl6_dst, final_p);
1121db8dac20SDavid S. Miller 
112252479b62SAlexey Dobriyan 	err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
112352479b62SAlexey Dobriyan 	if (err < 0) {
1124db8dac20SDavid S. Miller 		if (err == -EREMOTE)
1125db8dac20SDavid S. Miller 			err = ip6_dst_blackhole(sk, &dst, &fl);
1126db8dac20SDavid S. Miller 		if (err < 0)
1127db8dac20SDavid S. Miller 			goto out;
1128db8dac20SDavid S. Miller 	}
1129db8dac20SDavid S. Miller 
1130db8dac20SDavid S. Miller 	if (hlimit < 0) {
1131db8dac20SDavid S. Miller 		if (ipv6_addr_is_multicast(&fl.fl6_dst))
1132db8dac20SDavid S. Miller 			hlimit = np->mcast_hops;
1133db8dac20SDavid S. Miller 		else
1134db8dac20SDavid S. Miller 			hlimit = np->hop_limit;
1135db8dac20SDavid S. Miller 		if (hlimit < 0)
11366b75d090SYOSHIFUJI Hideaki 			hlimit = ip6_dst_hoplimit(dst);
1137db8dac20SDavid S. Miller 	}
1138db8dac20SDavid S. Miller 
1139db8dac20SDavid S. Miller 	if (tclass < 0)
1140e651f03aSGerrit Renker 		tclass = np->tclass;
1141db8dac20SDavid S. Miller 
114213b52cd4SBrian Haley 	if (dontfrag < 0)
114313b52cd4SBrian Haley 		dontfrag = np->dontfrag;
114413b52cd4SBrian Haley 
1145db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1146db8dac20SDavid S. Miller 		goto do_confirm;
1147db8dac20SDavid S. Miller back_from_confirm:
1148db8dac20SDavid S. Miller 
1149db8dac20SDavid S. Miller 	lock_sock(sk);
1150db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1151db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1152db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1153db8dac20SDavid S. Miller 		release_sock(sk);
1154db8dac20SDavid S. Miller 
1155db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1156db8dac20SDavid S. Miller 		err = -EINVAL;
1157db8dac20SDavid S. Miller 		goto out;
1158db8dac20SDavid S. Miller 	}
1159db8dac20SDavid S. Miller 
1160db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1161db8dac20SDavid S. Miller 
1162db8dac20SDavid S. Miller do_append_data:
1163db8dac20SDavid S. Miller 	up->len += ulen;
1164db8dac20SDavid S. Miller 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1165db8dac20SDavid S. Miller 	err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
1166db8dac20SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl,
1167db8dac20SDavid S. Miller 		(struct rt6_info*)dst,
116813b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1169db8dac20SDavid S. Miller 	if (err)
1170db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1171db8dac20SDavid S. Miller 	else if (!corkreq)
1172db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1173db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1174db8dac20SDavid S. Miller 		up->pending = 0;
1175db8dac20SDavid S. Miller 
1176db8dac20SDavid S. Miller 	if (dst) {
1177db8dac20SDavid S. Miller 		if (connected) {
1178db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
1179db8dac20SDavid S. Miller 				      ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
1180db8dac20SDavid S. Miller 				      &np->daddr : NULL,
1181db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
1182db8dac20SDavid S. Miller 				      ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
1183db8dac20SDavid S. Miller 				      &np->saddr :
1184db8dac20SDavid S. Miller #endif
1185db8dac20SDavid S. Miller 				      NULL);
1186db8dac20SDavid S. Miller 		} else {
1187db8dac20SDavid S. Miller 			dst_release(dst);
1188db8dac20SDavid S. Miller 		}
1189a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1190db8dac20SDavid S. Miller 	}
1191db8dac20SDavid S. Miller 
1192db8dac20SDavid S. Miller 	if (err > 0)
1193db8dac20SDavid S. Miller 		err = np->recverr ? net_xmit_errno(err) : 0;
1194db8dac20SDavid S. Miller 	release_sock(sk);
1195db8dac20SDavid S. Miller out:
1196a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1197db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
1198db8dac20SDavid S. Miller 	if (!err)
1199db8dac20SDavid S. Miller 		return len;
1200db8dac20SDavid S. Miller 	/*
1201db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1202db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1203db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1204db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1205db8dac20SDavid S. Miller 	 * seems like overkill.
1206db8dac20SDavid S. Miller 	 */
1207db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1208235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1209235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1210db8dac20SDavid S. Miller 	}
1211db8dac20SDavid S. Miller 	return err;
1212db8dac20SDavid S. Miller 
1213db8dac20SDavid S. Miller do_confirm:
1214db8dac20SDavid S. Miller 	dst_confirm(dst);
1215db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1216db8dac20SDavid S. Miller 		goto back_from_confirm;
1217db8dac20SDavid S. Miller 	err = 0;
1218db8dac20SDavid S. Miller 	goto out;
1219db8dac20SDavid S. Miller }
1220db8dac20SDavid S. Miller 
12217d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1222db8dac20SDavid S. Miller {
1223db8dac20SDavid S. Miller 	lock_sock(sk);
1224db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1225db8dac20SDavid S. Miller 	release_sock(sk);
1226db8dac20SDavid S. Miller 
1227db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1228db8dac20SDavid S. Miller }
1229db8dac20SDavid S. Miller 
1230db8dac20SDavid S. Miller /*
1231db8dac20SDavid S. Miller  *	Socket option code for UDP
1232db8dac20SDavid S. Miller  */
1233db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1234b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1235db8dac20SDavid S. Miller {
1236db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1237db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1238db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1239db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1240db8dac20SDavid S. Miller }
1241db8dac20SDavid S. Miller 
1242db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1243db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1244b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1245db8dac20SDavid S. Miller {
1246db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1247db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1248db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1249db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1250db8dac20SDavid S. Miller }
1251db8dac20SDavid S. Miller #endif
1252db8dac20SDavid S. Miller 
1253db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1254db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1255db8dac20SDavid S. Miller {
1256db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1257db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1258db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1259db8dac20SDavid S. Miller }
1260db8dac20SDavid S. Miller 
1261db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1262db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1263db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1264db8dac20SDavid S. Miller {
1265db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1266db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1267db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1268db8dac20SDavid S. Miller }
1269db8dac20SDavid S. Miller #endif
1270db8dac20SDavid S. Miller 
1271ba735425SSridhar Samudrala static int udp6_ufo_send_check(struct sk_buff *skb)
1272ba735425SSridhar Samudrala {
1273ba735425SSridhar Samudrala 	struct ipv6hdr *ipv6h;
1274ba735425SSridhar Samudrala 	struct udphdr *uh;
1275ba735425SSridhar Samudrala 
1276ba735425SSridhar Samudrala 	if (!pskb_may_pull(skb, sizeof(*uh)))
1277ba735425SSridhar Samudrala 		return -EINVAL;
1278ba735425SSridhar Samudrala 
1279ba735425SSridhar Samudrala 	ipv6h = ipv6_hdr(skb);
1280ba735425SSridhar Samudrala 	uh = udp_hdr(skb);
1281ba735425SSridhar Samudrala 
1282ba735425SSridhar Samudrala 	uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1283ba735425SSridhar Samudrala 				     IPPROTO_UDP, 0);
1284ba735425SSridhar Samudrala 	skb->csum_start = skb_transport_header(skb) - skb->head;
1285ba735425SSridhar Samudrala 	skb->csum_offset = offsetof(struct udphdr, check);
1286ba735425SSridhar Samudrala 	skb->ip_summed = CHECKSUM_PARTIAL;
1287ba735425SSridhar Samudrala 	return 0;
1288ba735425SSridhar Samudrala }
1289ba735425SSridhar Samudrala 
1290ba735425SSridhar Samudrala static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features)
1291ba735425SSridhar Samudrala {
1292ba735425SSridhar Samudrala 	struct sk_buff *segs = ERR_PTR(-EINVAL);
1293ba735425SSridhar Samudrala 	unsigned int mss;
1294ba735425SSridhar Samudrala 	unsigned int unfrag_ip6hlen, unfrag_len;
1295ba735425SSridhar Samudrala 	struct frag_hdr *fptr;
1296ba735425SSridhar Samudrala 	u8 *mac_start, *prevhdr;
1297ba735425SSridhar Samudrala 	u8 nexthdr;
1298ba735425SSridhar Samudrala 	u8 frag_hdr_sz = sizeof(struct frag_hdr);
1299ba735425SSridhar Samudrala 	int offset;
1300ba735425SSridhar Samudrala 	__wsum csum;
1301ba735425SSridhar Samudrala 
1302ba735425SSridhar Samudrala 	mss = skb_shinfo(skb)->gso_size;
1303ba735425SSridhar Samudrala 	if (unlikely(skb->len <= mss))
1304ba735425SSridhar Samudrala 		goto out;
1305ba735425SSridhar Samudrala 
1306ba735425SSridhar Samudrala 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1307ba735425SSridhar Samudrala 		/* Packet is from an untrusted source, reset gso_segs. */
1308ba735425SSridhar Samudrala 		int type = skb_shinfo(skb)->gso_type;
1309ba735425SSridhar Samudrala 
1310ba735425SSridhar Samudrala 		if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1311ba735425SSridhar Samudrala 			     !(type & (SKB_GSO_UDP))))
1312ba735425SSridhar Samudrala 			goto out;
1313ba735425SSridhar Samudrala 
1314ba735425SSridhar Samudrala 		skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1315ba735425SSridhar Samudrala 
1316ba735425SSridhar Samudrala 		segs = NULL;
1317ba735425SSridhar Samudrala 		goto out;
1318ba735425SSridhar Samudrala 	}
1319ba735425SSridhar Samudrala 
1320ba735425SSridhar Samudrala 	/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1321ba735425SSridhar Samudrala 	 * do checksum of UDP packets sent as multiple IP fragments.
1322ba735425SSridhar Samudrala 	 */
1323ba735425SSridhar Samudrala 	offset = skb->csum_start - skb_headroom(skb);
1324ba735425SSridhar Samudrala 	csum = skb_checksum(skb, offset, skb->len- offset, 0);
1325ba735425SSridhar Samudrala 	offset += skb->csum_offset;
1326ba735425SSridhar Samudrala 	*(__sum16 *)(skb->data + offset) = csum_fold(csum);
1327ba735425SSridhar Samudrala 	skb->ip_summed = CHECKSUM_NONE;
1328ba735425SSridhar Samudrala 
1329ba735425SSridhar Samudrala 	/* Check if there is enough headroom to insert fragment header. */
1330ba735425SSridhar Samudrala 	if ((skb_headroom(skb) < frag_hdr_sz) &&
1331ba735425SSridhar Samudrala 	    pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1332ba735425SSridhar Samudrala 		goto out;
1333ba735425SSridhar Samudrala 
1334ba735425SSridhar Samudrala 	/* Find the unfragmentable header and shift it left by frag_hdr_sz
1335ba735425SSridhar Samudrala 	 * bytes to insert fragment header.
1336ba735425SSridhar Samudrala 	 */
1337ba735425SSridhar Samudrala 	unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1338ba735425SSridhar Samudrala 	nexthdr = *prevhdr;
1339ba735425SSridhar Samudrala 	*prevhdr = NEXTHDR_FRAGMENT;
1340ba735425SSridhar Samudrala 	unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1341ba735425SSridhar Samudrala 		     unfrag_ip6hlen;
1342ba735425SSridhar Samudrala 	mac_start = skb_mac_header(skb);
1343ba735425SSridhar Samudrala 	memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1344ba735425SSridhar Samudrala 
1345ba735425SSridhar Samudrala 	skb->mac_header -= frag_hdr_sz;
1346ba735425SSridhar Samudrala 	skb->network_header -= frag_hdr_sz;
1347ba735425SSridhar Samudrala 
1348ba735425SSridhar Samudrala 	fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1349ba735425SSridhar Samudrala 	fptr->nexthdr = nexthdr;
1350ba735425SSridhar Samudrala 	fptr->reserved = 0;
1351ba735425SSridhar Samudrala 	ipv6_select_ident(fptr);
1352ba735425SSridhar Samudrala 
1353ba735425SSridhar Samudrala 	/* Fragment the skb. ipv6 header and the remaining fields of the
1354ba735425SSridhar Samudrala 	 * fragment header are updated in ipv6_gso_segment()
1355ba735425SSridhar Samudrala 	 */
1356ba735425SSridhar Samudrala 	segs = skb_segment(skb, features);
1357ba735425SSridhar Samudrala 
1358ba735425SSridhar Samudrala out:
1359ba735425SSridhar Samudrala 	return segs;
1360ba735425SSridhar Samudrala }
1361ba735425SSridhar Samudrala 
136241135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1363db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1364db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1365ba735425SSridhar Samudrala 	.gso_send_check =	udp6_ufo_send_check,
1366ba735425SSridhar Samudrala 	.gso_segment	=	udp6_ufo_fragment,
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 
1373db8dac20SDavid S. Miller static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1374db8dac20SDavid S. Miller {
1375db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
1376db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sp);
1377db8dac20SDavid S. Miller 	struct in6_addr *dest, *src;
1378db8dac20SDavid S. Miller 	__u16 destp, srcp;
1379db8dac20SDavid S. Miller 
1380db8dac20SDavid S. Miller 	dest  = &np->daddr;
1381db8dac20SDavid S. Miller 	src   = &np->rcv_saddr;
1382c720c7e8SEric Dumazet 	destp = ntohs(inet->inet_dport);
1383c720c7e8SEric Dumazet 	srcp  = ntohs(inet->inet_sport);
1384db8dac20SDavid S. Miller 	seq_printf(seq,
1385f86dcc5aSEric Dumazet 		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1386cb61cb9bSEric Dumazet 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1387db8dac20SDavid S. Miller 		   bucket,
1388db8dac20SDavid S. Miller 		   src->s6_addr32[0], src->s6_addr32[1],
1389db8dac20SDavid S. Miller 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
1390db8dac20SDavid S. Miller 		   dest->s6_addr32[0], dest->s6_addr32[1],
1391db8dac20SDavid S. Miller 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
1392db8dac20SDavid S. Miller 		   sp->sk_state,
139331e6d363SEric Dumazet 		   sk_wmem_alloc_get(sp),
139431e6d363SEric Dumazet 		   sk_rmem_alloc_get(sp),
1395db8dac20SDavid S. Miller 		   0, 0L, 0,
1396db8dac20SDavid S. Miller 		   sock_i_uid(sp), 0,
1397db8dac20SDavid S. Miller 		   sock_i_ino(sp),
1398cb61cb9bSEric Dumazet 		   atomic_read(&sp->sk_refcnt), sp,
1399cb61cb9bSEric Dumazet 		   atomic_read(&sp->sk_drops));
1400db8dac20SDavid S. Miller }
1401db8dac20SDavid S. Miller 
1402db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1403db8dac20SDavid S. Miller {
1404db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
1405db8dac20SDavid S. Miller 		seq_printf(seq,
1406db8dac20SDavid S. Miller 			   "  sl  "
1407db8dac20SDavid S. Miller 			   "local_address                         "
1408db8dac20SDavid S. Miller 			   "remote_address                        "
1409db8dac20SDavid S. Miller 			   "st tx_queue rx_queue tr tm->when retrnsmt"
1410cb61cb9bSEric Dumazet 			   "   uid  timeout inode ref pointer drops\n");
1411db8dac20SDavid S. Miller 	else
1412db8dac20SDavid S. Miller 		udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1413db8dac20SDavid S. Miller 	return 0;
1414db8dac20SDavid S. Miller }
1415db8dac20SDavid S. Miller 
1416db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1417db8dac20SDavid S. Miller 	.name		= "udp6",
1418db8dac20SDavid S. Miller 	.family		= AF_INET6,
1419645ca708SEric Dumazet 	.udp_table	= &udp_table,
14204ad96d39SDenis V. Lunev 	.seq_fops	= {
14214ad96d39SDenis V. Lunev 		.owner	=	THIS_MODULE,
14224ad96d39SDenis V. Lunev 	},
1423dda61925SDenis V. Lunev 	.seq_ops	= {
1424dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1425dda61925SDenis V. Lunev 	},
1426db8dac20SDavid S. Miller };
1427db8dac20SDavid S. Miller 
14282c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1429db8dac20SDavid S. Miller {
14300c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1431db8dac20SDavid S. Miller }
1432db8dac20SDavid S. Miller 
14330c96d8c5SDaniel Lezcano void udp6_proc_exit(struct net *net) {
14340c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1435db8dac20SDavid S. Miller }
1436db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1437db8dac20SDavid S. Miller 
1438db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1439db8dac20SDavid S. Miller 
1440db8dac20SDavid S. Miller struct proto udpv6_prot = {
1441db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1442db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1443db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1444db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1445db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1446db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1447db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1448db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1449db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1450db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1451db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1452db8dac20SDavid S. Miller 	.backlog_rcv	   = udpv6_queue_rcv_skb,
1453db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1454db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1455db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1456db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1457db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1458db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1459db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1460db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1461271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1462645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1463db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1464db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1465db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1466db8dac20SDavid S. Miller #endif
1467db8dac20SDavid S. Miller };
1468db8dac20SDavid S. Miller 
1469db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1470db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1471db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1472db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1473db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1474db8dac20SDavid S. Miller 	.no_check =  UDP_CSUM_DEFAULT,
1475db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1476db8dac20SDavid S. Miller };
1477db8dac20SDavid S. Miller 
1478db8dac20SDavid S. Miller 
1479db8dac20SDavid S. Miller int __init udpv6_init(void)
1480db8dac20SDavid S. Miller {
1481db8dac20SDavid S. Miller 	int ret;
1482db8dac20SDavid S. Miller 
1483db8dac20SDavid S. Miller 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1484db8dac20SDavid S. Miller 	if (ret)
1485db8dac20SDavid S. Miller 		goto out;
1486db8dac20SDavid S. Miller 
1487db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1488db8dac20SDavid S. Miller 	if (ret)
1489db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1490db8dac20SDavid S. Miller out:
1491db8dac20SDavid S. Miller 	return ret;
1492db8dac20SDavid S. Miller 
1493db8dac20SDavid S. Miller out_udpv6_protocol:
1494db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1495db8dac20SDavid S. Miller 	goto out;
1496db8dac20SDavid S. Miller }
1497db8dac20SDavid S. Miller 
1498db8dac20SDavid S. Miller void udpv6_exit(void)
1499db8dac20SDavid S. Miller {
1500db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1501db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1502db8dac20SDavid S. Miller }
1503