xref: /openbmc/linux/net/ipv6/udp.c (revision c8f44aff)
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);
5768835abaSEric Dumazet 	__be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
5868835abaSEric Dumazet 	__be32 sk2_rcv_saddr = sk_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 
114719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
115719f8358SEric Dumazet {
116719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
117719f8358SEric Dumazet 					  &inet6_sk(sk)->rcv_saddr,
118719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
119719f8358SEric Dumazet 
120719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
121719f8358SEric Dumazet }
122719f8358SEric Dumazet 
123645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
124645ca708SEric Dumazet 				unsigned short hnum,
12588440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
12688440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
127645ca708SEric Dumazet 				int dif)
128db8dac20SDavid S. Miller {
129645ca708SEric Dumazet 	int score = -1;
130db8dac20SDavid S. Miller 
131d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
132db8dac20SDavid S. Miller 			sk->sk_family == PF_INET6) {
133db8dac20SDavid S. Miller 		struct ipv6_pinfo *np = inet6_sk(sk);
134645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
135645ca708SEric Dumazet 
136645ca708SEric Dumazet 		score = 0;
137c720c7e8SEric Dumazet 		if (inet->inet_dport) {
138c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
139645ca708SEric Dumazet 				return -1;
140db8dac20SDavid S. Miller 			score++;
141db8dac20SDavid S. Miller 		}
142db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->rcv_saddr)) {
143db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
144645ca708SEric Dumazet 				return -1;
145db8dac20SDavid S. Miller 			score++;
146db8dac20SDavid S. Miller 		}
147db8dac20SDavid S. Miller 		if (!ipv6_addr_any(&np->daddr)) {
148db8dac20SDavid S. Miller 			if (!ipv6_addr_equal(&np->daddr, saddr))
149645ca708SEric Dumazet 				return -1;
150db8dac20SDavid S. Miller 			score++;
151db8dac20SDavid S. Miller 		}
152db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if) {
153db8dac20SDavid S. Miller 			if (sk->sk_bound_dev_if != dif)
154645ca708SEric Dumazet 				return -1;
155db8dac20SDavid S. Miller 			score++;
156db8dac20SDavid S. Miller 		}
157645ca708SEric Dumazet 	}
158645ca708SEric Dumazet 	return score;
159645ca708SEric Dumazet }
160645ca708SEric Dumazet 
161fddc17deSEric Dumazet #define SCORE2_MAX (1 + 1 + 1)
162fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
163fddc17deSEric Dumazet 				const struct in6_addr *saddr, __be16 sport,
164fddc17deSEric Dumazet 				const struct in6_addr *daddr, unsigned short hnum,
165fddc17deSEric Dumazet 				int dif)
166fddc17deSEric Dumazet {
167fddc17deSEric Dumazet 	int score = -1;
168fddc17deSEric Dumazet 
169fddc17deSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
170fddc17deSEric Dumazet 			sk->sk_family == PF_INET6) {
171fddc17deSEric Dumazet 		struct ipv6_pinfo *np = inet6_sk(sk);
172fddc17deSEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
173fddc17deSEric Dumazet 
174fddc17deSEric Dumazet 		if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
175fddc17deSEric Dumazet 			return -1;
176fddc17deSEric Dumazet 		score = 0;
177fddc17deSEric Dumazet 		if (inet->inet_dport) {
178fddc17deSEric Dumazet 			if (inet->inet_dport != sport)
179fddc17deSEric Dumazet 				return -1;
180fddc17deSEric Dumazet 			score++;
181fddc17deSEric Dumazet 		}
182fddc17deSEric Dumazet 		if (!ipv6_addr_any(&np->daddr)) {
183fddc17deSEric Dumazet 			if (!ipv6_addr_equal(&np->daddr, saddr))
184fddc17deSEric Dumazet 				return -1;
185fddc17deSEric Dumazet 			score++;
186fddc17deSEric Dumazet 		}
187fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if) {
188fddc17deSEric Dumazet 			if (sk->sk_bound_dev_if != dif)
189fddc17deSEric Dumazet 				return -1;
190fddc17deSEric Dumazet 			score++;
191fddc17deSEric Dumazet 		}
192fddc17deSEric Dumazet 	}
193fddc17deSEric Dumazet 	return score;
194fddc17deSEric Dumazet }
195fddc17deSEric Dumazet 
196fddc17deSEric Dumazet 
197fddc17deSEric Dumazet /* called with read_rcu_lock() */
198fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
199fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
200fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
201fddc17deSEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
202fddc17deSEric Dumazet {
203fddc17deSEric Dumazet 	struct sock *sk, *result;
204fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
205fddc17deSEric Dumazet 	int score, badness;
206fddc17deSEric Dumazet 
207fddc17deSEric Dumazet begin:
208fddc17deSEric Dumazet 	result = NULL;
209fddc17deSEric Dumazet 	badness = -1;
210fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
211fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
212fddc17deSEric Dumazet 				      daddr, hnum, dif);
213fddc17deSEric Dumazet 		if (score > badness) {
214fddc17deSEric Dumazet 			result = sk;
215fddc17deSEric Dumazet 			badness = score;
216fddc17deSEric Dumazet 			if (score == SCORE2_MAX)
217fddc17deSEric Dumazet 				goto exact_match;
218fddc17deSEric Dumazet 		}
219fddc17deSEric Dumazet 	}
220fddc17deSEric Dumazet 	/*
221fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
222fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
223fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
224fddc17deSEric Dumazet 	 */
225fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
226fddc17deSEric Dumazet 		goto begin;
227fddc17deSEric Dumazet 
228fddc17deSEric Dumazet 	if (result) {
229fddc17deSEric Dumazet exact_match:
230c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
231fddc17deSEric Dumazet 			result = NULL;
232fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
233fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
234fddc17deSEric Dumazet 			sock_put(result);
235fddc17deSEric Dumazet 			goto begin;
236fddc17deSEric Dumazet 		}
237fddc17deSEric Dumazet 	}
238fddc17deSEric Dumazet 	return result;
239fddc17deSEric Dumazet }
240fddc17deSEric Dumazet 
241645ca708SEric Dumazet static struct sock *__udp6_lib_lookup(struct net *net,
24288440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
24388440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
244645ca708SEric Dumazet 				      int dif, struct udp_table *udptable)
245645ca708SEric Dumazet {
246271b72c7SEric Dumazet 	struct sock *sk, *result;
24788ab1932SEric Dumazet 	struct hlist_nulls_node *node;
248645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
249fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
250fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
251271b72c7SEric Dumazet 	int score, badness;
252645ca708SEric Dumazet 
253271b72c7SEric Dumazet 	rcu_read_lock();
254fddc17deSEric Dumazet 	if (hslot->count > 10) {
255fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
256fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
257fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
258fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
259fddc17deSEric Dumazet 			goto begin;
260fddc17deSEric Dumazet 
261fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
262fddc17deSEric Dumazet 					  daddr, hnum, dif,
263fddc17deSEric Dumazet 					  hslot2, slot2);
264fddc17deSEric Dumazet 		if (!result) {
265fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
266fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
267fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
268fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
269fddc17deSEric Dumazet 				goto begin;
270fddc17deSEric Dumazet 
2711223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
2721223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
273fddc17deSEric Dumazet 						  hslot2, slot2);
274fddc17deSEric Dumazet 		}
275fddc17deSEric Dumazet 		rcu_read_unlock();
276fddc17deSEric Dumazet 		return result;
277fddc17deSEric Dumazet 	}
278271b72c7SEric Dumazet begin:
279271b72c7SEric Dumazet 	result = NULL;
280271b72c7SEric Dumazet 	badness = -1;
28188ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
282645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
283645ca708SEric Dumazet 		if (score > badness) {
284db8dac20SDavid S. Miller 			result = sk;
285db8dac20SDavid S. Miller 			badness = score;
286db8dac20SDavid S. Miller 		}
287db8dac20SDavid S. Miller 	}
28888ab1932SEric Dumazet 	/*
28988ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
29088ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
29188ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
29288ab1932SEric Dumazet 	 */
293fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
29488ab1932SEric Dumazet 		goto begin;
29588ab1932SEric Dumazet 
296271b72c7SEric Dumazet 	if (result) {
297c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
298271b72c7SEric Dumazet 			result = NULL;
299271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
300271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
301271b72c7SEric Dumazet 			sock_put(result);
302271b72c7SEric Dumazet 			goto begin;
303271b72c7SEric Dumazet 		}
304271b72c7SEric Dumazet 	}
305271b72c7SEric Dumazet 	rcu_read_unlock();
306db8dac20SDavid S. Miller 	return result;
307db8dac20SDavid S. Miller }
308db8dac20SDavid S. Miller 
309607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
310607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
311645ca708SEric Dumazet 					  struct udp_table *udptable)
312607c4aafSKOVACS Krisztian {
31323542618SKOVACS Krisztian 	struct sock *sk;
314b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
315607c4aafSKOVACS Krisztian 
31623542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
31723542618SKOVACS Krisztian 		return sk;
318adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
319607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
320607c4aafSKOVACS Krisztian 				 udptable);
321607c4aafSKOVACS Krisztian }
322607c4aafSKOVACS Krisztian 
323aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
324aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
325aa976fc0SBalazs Scheidler {
326aa976fc0SBalazs Scheidler 	return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
327aa976fc0SBalazs Scheidler }
328aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
329aa976fc0SBalazs Scheidler 
330aa976fc0SBalazs Scheidler 
331db8dac20SDavid S. Miller /*
332db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
333db8dac20SDavid S. Miller  * 	return it, otherwise we block.
334db8dac20SDavid S. Miller  */
335db8dac20SDavid S. Miller 
336db8dac20SDavid S. Miller int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
337db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len,
338db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
339db8dac20SDavid S. Miller {
340db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
341db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
342db8dac20SDavid S. Miller 	struct sk_buff *skb;
34381d54ec8SGerrit Renker 	unsigned int ulen;
344db8dac20SDavid S. Miller 	int peeked;
345db8dac20SDavid S. Miller 	int err;
346db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
347f26ba175SWei Yongjun 	int is_udp4;
3488a74ad60SEric Dumazet 	bool slow;
349db8dac20SDavid S. Miller 
350db8dac20SDavid S. Miller 	if (addr_len)
351db8dac20SDavid S. Miller 		*addr_len=sizeof(struct sockaddr_in6);
352db8dac20SDavid S. Miller 
353db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
354db8dac20SDavid S. Miller 		return ipv6_recv_error(sk, msg, len);
355db8dac20SDavid S. Miller 
3564b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
3574b340ae2SBrian Haley 		return ipv6_recv_rxpmtu(sk, msg, len);
3584b340ae2SBrian Haley 
359db8dac20SDavid S. Miller try_again:
360db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
361db8dac20SDavid S. Miller 				  &peeked, &err);
362db8dac20SDavid S. Miller 	if (!skb)
363db8dac20SDavid S. Miller 		goto out;
364db8dac20SDavid S. Miller 
365db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
36681d54ec8SGerrit Renker 	if (len > ulen)
36781d54ec8SGerrit Renker 		len = ulen;
36881d54ec8SGerrit Renker 	else if (len < ulen)
369db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
370db8dac20SDavid S. Miller 
371f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
372f26ba175SWei Yongjun 
373db8dac20SDavid S. Miller 	/*
374db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
375db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
376db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
377db8dac20SDavid S. Miller 	 */
378db8dac20SDavid S. Miller 
37981d54ec8SGerrit Renker 	if (len < ulen || UDP_SKB_CB(skb)->partial_cov) {
380db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
381db8dac20SDavid S. Miller 			goto csum_copy_err;
382db8dac20SDavid S. Miller 	}
383db8dac20SDavid S. Miller 
384db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
385db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
38681d54ec8SGerrit Renker 					      msg->msg_iov,len);
387db8dac20SDavid S. Miller 	else {
388db8dac20SDavid S. Miller 		err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
389db8dac20SDavid S. Miller 		if (err == -EINVAL)
390db8dac20SDavid S. Miller 			goto csum_copy_err;
391db8dac20SDavid S. Miller 	}
392db8dac20SDavid S. Miller 	if (err)
393db8dac20SDavid S. Miller 		goto out_free;
394db8dac20SDavid S. Miller 
395f26ba175SWei Yongjun 	if (!peeked) {
396f26ba175SWei Yongjun 		if (is_udp4)
397f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
398f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
399f26ba175SWei Yongjun 		else
400235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
401235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
402f26ba175SWei Yongjun 	}
403db8dac20SDavid S. Miller 
4043b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
405db8dac20SDavid S. Miller 
406db8dac20SDavid S. Miller 	/* Copy the address. */
407db8dac20SDavid S. Miller 	if (msg->msg_name) {
408db8dac20SDavid S. Miller 		struct sockaddr_in6 *sin6;
409db8dac20SDavid S. Miller 
410db8dac20SDavid S. Miller 		sin6 = (struct sockaddr_in6 *) msg->msg_name;
411db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
412db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
413db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
414db8dac20SDavid S. Miller 		sin6->sin6_scope_id = 0;
415db8dac20SDavid S. Miller 
416f26ba175SWei Yongjun 		if (is_udp4)
417b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
418b301e82cSBrian Haley 					       &sin6->sin6_addr);
419db8dac20SDavid S. Miller 		else {
420db8dac20SDavid S. Miller 			ipv6_addr_copy(&sin6->sin6_addr,
421db8dac20SDavid S. Miller 				       &ipv6_hdr(skb)->saddr);
422db8dac20SDavid S. Miller 			if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
423db8dac20SDavid S. Miller 				sin6->sin6_scope_id = IP6CB(skb)->iif;
424db8dac20SDavid S. Miller 		}
425db8dac20SDavid S. Miller 
426db8dac20SDavid S. Miller 	}
427f26ba175SWei Yongjun 	if (is_udp4) {
428db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
429db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
430db8dac20SDavid S. Miller 	} else {
431db8dac20SDavid S. Miller 		if (np->rxopt.all)
432db8dac20SDavid S. Miller 			datagram_recv_ctl(sk, msg, skb);
433db8dac20SDavid S. Miller 	}
434db8dac20SDavid S. Miller 
43581d54ec8SGerrit Renker 	err = len;
436db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
437db8dac20SDavid S. Miller 		err = ulen;
438db8dac20SDavid S. Miller 
439db8dac20SDavid S. Miller out_free:
4409d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
441db8dac20SDavid S. Miller out:
442db8dac20SDavid S. Miller 	return err;
443db8dac20SDavid S. Miller 
444db8dac20SDavid S. Miller csum_copy_err:
4458a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
4460856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
4470856f939SWei Yongjun 		if (is_udp4)
4480856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
4490856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4500856f939SWei Yongjun 		else
4510856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
4520856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
4530856f939SWei Yongjun 	}
4548a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
455db8dac20SDavid S. Miller 
45632c90254SXufeng Zhang 	if (noblock)
457db8dac20SDavid S. Miller 		return -EAGAIN;
4589cfaa8deSXufeng Zhang 
4599cfaa8deSXufeng Zhang 	/* starting over for a new packet */
4609cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
461db8dac20SDavid S. Miller 	goto try_again;
462db8dac20SDavid S. Miller }
463db8dac20SDavid S. Miller 
464db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
465d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
466645ca708SEric Dumazet 		    struct udp_table *udptable)
467db8dac20SDavid S. Miller {
468db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
469b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
470b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
471b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
472db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr*)(skb->data+offset);
473db8dac20SDavid S. Miller 	struct sock *sk;
474db8dac20SDavid S. Miller 	int err;
475db8dac20SDavid S. Miller 
476c346dca1SYOSHIFUJI Hideaki 	sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
477db8dac20SDavid S. Miller 			       saddr, uh->source, inet6_iif(skb), udptable);
478db8dac20SDavid S. Miller 	if (sk == NULL)
479db8dac20SDavid S. Miller 		return;
480db8dac20SDavid S. Miller 
481db8dac20SDavid S. Miller 	np = inet6_sk(sk);
482db8dac20SDavid S. Miller 
483db8dac20SDavid S. Miller 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
484db8dac20SDavid S. Miller 		goto out;
485db8dac20SDavid S. Miller 
486db8dac20SDavid S. Miller 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
487db8dac20SDavid S. Miller 		goto out;
488db8dac20SDavid S. Miller 
489b1faf566SEric Dumazet 	if (np->recverr)
490db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
491b1faf566SEric Dumazet 
492db8dac20SDavid S. Miller 	sk->sk_err = err;
493db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
494db8dac20SDavid S. Miller out:
495db8dac20SDavid S. Miller 	sock_put(sk);
496db8dac20SDavid S. Miller }
497db8dac20SDavid S. Miller 
498db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
499d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
500d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info     )
501db8dac20SDavid S. Miller {
502645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
503db8dac20SDavid S. Miller }
504db8dac20SDavid S. Miller 
505db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
506db8dac20SDavid S. Miller {
507db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
508db8dac20SDavid S. Miller 	int rc;
509db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
510db8dac20SDavid S. Miller 
51147482f13SNeil Horman 	if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
512bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
51347482f13SNeil Horman 
514db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
515db8dac20SDavid S. Miller 		goto drop;
516db8dac20SDavid S. Miller 
517db8dac20SDavid S. Miller 	/*
518db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
519db8dac20SDavid S. Miller 	 */
520db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
521db8dac20SDavid S. Miller 
522db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
523db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
524db8dac20SDavid S. Miller 				" %d while full coverage %d requested\n",
525db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, skb->len);
526db8dac20SDavid S. Miller 			goto drop;
527db8dac20SDavid S. Miller 		}
528db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
529db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
530db8dac20SDavid S. Miller 						    "too small, need min %d\n",
531db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
532db8dac20SDavid S. Miller 			goto drop;
533db8dac20SDavid S. Miller 		}
534db8dac20SDavid S. Miller 	}
535db8dac20SDavid S. Miller 
53633d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
537db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
538db8dac20SDavid S. Miller 			goto drop;
539db8dac20SDavid S. Miller 	}
540db8dac20SDavid S. Miller 
541d826eb14SEric Dumazet 	skb_dst_drop(skb);
542d826eb14SEric Dumazet 	rc = sock_queue_rcv_skb(sk, skb);
543d826eb14SEric Dumazet 	if (rc < 0) {
544db8dac20SDavid S. Miller 		/* Note that an ENOMEM error is charged twice */
545766e9037SEric Dumazet 		if (rc == -ENOMEM)
546ef28d1a2SPavel Emelyanov 			UDP6_INC_STATS_BH(sock_net(sk),
547ef28d1a2SPavel Emelyanov 					UDP_MIB_RCVBUFERRORS, is_udplite);
5488edf19c2SEric Dumazet 		goto drop_no_sk_drops_inc;
549db8dac20SDavid S. Miller 	}
550db8dac20SDavid S. Miller 
551db8dac20SDavid S. Miller 	return 0;
552db8dac20SDavid S. Miller drop:
5538edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
5548edf19c2SEric Dumazet drop_no_sk_drops_inc:
555ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
556db8dac20SDavid S. Miller 	kfree_skb(skb);
557db8dac20SDavid S. Miller 	return -1;
558db8dac20SDavid S. Miller }
559db8dac20SDavid S. Miller 
560920a4611SEric Dumazet static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
561b71d1d42SEric Dumazet 				      __be16 loc_port, const struct in6_addr *loc_addr,
562b71d1d42SEric Dumazet 				      __be16 rmt_port, const struct in6_addr *rmt_addr,
563db8dac20SDavid S. Miller 				      int dif)
564db8dac20SDavid S. Miller {
56588ab1932SEric Dumazet 	struct hlist_nulls_node *node;
566db8dac20SDavid S. Miller 	struct sock *s = sk;
567db8dac20SDavid S. Miller 	unsigned short num = ntohs(loc_port);
568db8dac20SDavid S. Miller 
56988ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
570db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
571db8dac20SDavid S. Miller 
572920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net))
573b8ad0cbcSDaniel Lezcano 			continue;
574b8ad0cbcSDaniel Lezcano 
575d4cada4aSEric Dumazet 		if (udp_sk(s)->udp_port_hash == num &&
576d4cada4aSEric Dumazet 		    s->sk_family == PF_INET6) {
577db8dac20SDavid S. Miller 			struct ipv6_pinfo *np = inet6_sk(s);
578c720c7e8SEric Dumazet 			if (inet->inet_dport) {
579c720c7e8SEric Dumazet 				if (inet->inet_dport != rmt_port)
580db8dac20SDavid S. Miller 					continue;
581db8dac20SDavid S. Miller 			}
582db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->daddr) &&
583db8dac20SDavid S. Miller 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
584db8dac20SDavid S. Miller 				continue;
585db8dac20SDavid S. Miller 
586db8dac20SDavid S. Miller 			if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
587db8dac20SDavid S. Miller 				continue;
588db8dac20SDavid S. Miller 
589db8dac20SDavid S. Miller 			if (!ipv6_addr_any(&np->rcv_saddr)) {
590db8dac20SDavid S. Miller 				if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
591db8dac20SDavid S. Miller 					continue;
592db8dac20SDavid S. Miller 			}
593db8dac20SDavid S. Miller 			if (!inet6_mc_check(s, loc_addr, rmt_addr))
594db8dac20SDavid S. Miller 				continue;
595db8dac20SDavid S. Miller 			return s;
596db8dac20SDavid S. Miller 		}
597db8dac20SDavid S. Miller 	}
598db8dac20SDavid S. Miller 	return NULL;
599db8dac20SDavid S. Miller }
600db8dac20SDavid S. Miller 
601a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
602a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
603a1ab77f9SEric Dumazet {
604a1ab77f9SEric Dumazet 	unsigned int i;
605a1ab77f9SEric Dumazet 	struct sock *sk;
606a1ab77f9SEric Dumazet 	struct sk_buff *skb1;
607a1ab77f9SEric Dumazet 
608a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
609a1ab77f9SEric Dumazet 		skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
610a1ab77f9SEric Dumazet 
611a1ab77f9SEric Dumazet 		sk = stack[i];
612f6b8f32cSEric Dumazet 		if (skb1) {
613c0722400SJiri Pirko 			if (sk_rcvqueues_full(sk, skb1)) {
614c377411fSEric Dumazet 				kfree_skb(skb1);
615c377411fSEric Dumazet 				goto drop;
616c377411fSEric Dumazet 			}
617a1ab77f9SEric Dumazet 			bh_lock_sock(sk);
618a1ab77f9SEric Dumazet 			if (!sock_owned_by_user(sk))
619a1ab77f9SEric Dumazet 				udpv6_queue_rcv_skb(sk, skb1);
620a3a858ffSZhu Yi 			else if (sk_add_backlog(sk, skb1)) {
62155349790SZhu Yi 				kfree_skb(skb1);
622a1ab77f9SEric Dumazet 				bh_unlock_sock(sk);
62355349790SZhu Yi 				goto drop;
62455349790SZhu Yi 			}
62555349790SZhu Yi 			bh_unlock_sock(sk);
62655349790SZhu Yi 			continue;
62755349790SZhu Yi 		}
62855349790SZhu Yi drop:
629f6b8f32cSEric Dumazet 		atomic_inc(&sk->sk_drops);
630f6b8f32cSEric Dumazet 		UDP6_INC_STATS_BH(sock_net(sk),
631f6b8f32cSEric Dumazet 				UDP_MIB_RCVBUFERRORS, IS_UDPLITE(sk));
632f6b8f32cSEric Dumazet 		UDP6_INC_STATS_BH(sock_net(sk),
633f6b8f32cSEric Dumazet 				UDP_MIB_INERRORS, IS_UDPLITE(sk));
634a1ab77f9SEric Dumazet 	}
635a1ab77f9SEric Dumazet }
636db8dac20SDavid S. Miller /*
637db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
638db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
639db8dac20SDavid S. Miller  */
640e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
641b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
642645ca708SEric Dumazet 		struct udp_table *udptable)
643db8dac20SDavid S. Miller {
644a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
645db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
646f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
647db8dac20SDavid S. Miller 	int dif;
648a1ab77f9SEric Dumazet 	unsigned int i, count = 0;
649db8dac20SDavid S. Miller 
650645ca708SEric Dumazet 	spin_lock(&hslot->lock);
65188ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
652db8dac20SDavid S. Miller 	dif = inet6_iif(skb);
653920a4611SEric Dumazet 	sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
654a1ab77f9SEric Dumazet 	while (sk) {
655a1ab77f9SEric Dumazet 		stack[count++] = sk;
656a1ab77f9SEric Dumazet 		sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
657a1ab77f9SEric Dumazet 				       uh->source, saddr, dif);
658a1ab77f9SEric Dumazet 		if (unlikely(count == ARRAY_SIZE(stack))) {
659a1ab77f9SEric Dumazet 			if (!sk)
660a1ab77f9SEric Dumazet 				break;
661a1ab77f9SEric Dumazet 			flush_stack(stack, count, skb, ~0);
662a1ab77f9SEric Dumazet 			count = 0;
663db8dac20SDavid S. Miller 		}
664a1ab77f9SEric Dumazet 	}
665a1ab77f9SEric Dumazet 	/*
666a1ab77f9SEric Dumazet 	 * before releasing the lock, we must take reference on sockets
667a1ab77f9SEric Dumazet 	 */
668a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++)
669a1ab77f9SEric Dumazet 		sock_hold(stack[i]);
670db8dac20SDavid S. Miller 
671645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
672a1ab77f9SEric Dumazet 
673a1ab77f9SEric Dumazet 	if (count) {
674a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
675a1ab77f9SEric Dumazet 
676a1ab77f9SEric Dumazet 		for (i = 0; i < count; i++)
677a1ab77f9SEric Dumazet 			sock_put(stack[i]);
678a1ab77f9SEric Dumazet 	} else {
679a1ab77f9SEric Dumazet 		kfree_skb(skb);
680a1ab77f9SEric Dumazet 	}
681db8dac20SDavid S. Miller 	return 0;
682db8dac20SDavid S. Miller }
683db8dac20SDavid S. Miller 
684db8dac20SDavid S. Miller static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
685db8dac20SDavid S. Miller 				 int proto)
686db8dac20SDavid S. Miller {
687db8dac20SDavid S. Miller 	int err;
688db8dac20SDavid S. Miller 
689db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
690db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
691db8dac20SDavid S. Miller 
692db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
693db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
694db8dac20SDavid S. Miller 		if (err)
695db8dac20SDavid S. Miller 			return err;
696db8dac20SDavid S. Miller 	}
697db8dac20SDavid S. Miller 
698db8dac20SDavid S. Miller 	if (uh->check == 0) {
699db8dac20SDavid S. Miller 		/* RFC 2460 section 8.1 says that we SHOULD log
700db8dac20SDavid S. Miller 		   this error. Well, it is reasonable.
701db8dac20SDavid S. Miller 		 */
702db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
703db8dac20SDavid S. Miller 		return 1;
704db8dac20SDavid S. Miller 	}
705db8dac20SDavid S. Miller 	if (skb->ip_summed == CHECKSUM_COMPLETE &&
706db8dac20SDavid S. Miller 	    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
707db8dac20SDavid S. Miller 			     skb->len, proto, skb->csum))
708db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_UNNECESSARY;
709db8dac20SDavid S. Miller 
710db8dac20SDavid S. Miller 	if (!skb_csum_unnecessary(skb))
711db8dac20SDavid S. Miller 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
712db8dac20SDavid S. Miller 							 &ipv6_hdr(skb)->daddr,
713db8dac20SDavid S. Miller 							 skb->len, proto, 0));
714db8dac20SDavid S. Miller 
715db8dac20SDavid S. Miller 	return 0;
716db8dac20SDavid S. Miller }
717db8dac20SDavid S. Miller 
718645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
719db8dac20SDavid S. Miller 		   int proto)
720db8dac20SDavid S. Miller {
7213ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
722db8dac20SDavid S. Miller 	struct sock *sk;
723db8dac20SDavid S. Miller 	struct udphdr *uh;
724b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
725db8dac20SDavid S. Miller 	u32 ulen = 0;
726db8dac20SDavid S. Miller 
727db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
728d6bc0149SBjørn Mork 		goto discard;
729db8dac20SDavid S. Miller 
730db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
731db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
732db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
733db8dac20SDavid S. Miller 
734db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
735db8dac20SDavid S. Miller 	if (ulen > skb->len)
736db8dac20SDavid S. Miller 		goto short_packet;
737db8dac20SDavid S. Miller 
738db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
739db8dac20SDavid S. Miller 		/* UDP validates ulen. */
740db8dac20SDavid S. Miller 
741db8dac20SDavid S. Miller 		/* Check for jumbo payload */
742db8dac20SDavid S. Miller 		if (ulen == 0)
743db8dac20SDavid S. Miller 			ulen = skb->len;
744db8dac20SDavid S. Miller 
745db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
746db8dac20SDavid S. Miller 			goto short_packet;
747db8dac20SDavid S. Miller 
748db8dac20SDavid S. Miller 		if (ulen < skb->len) {
749db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
750db8dac20SDavid S. Miller 				goto short_packet;
751db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
752db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
753db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
754db8dac20SDavid S. Miller 		}
755db8dac20SDavid S. Miller 	}
756db8dac20SDavid S. Miller 
757db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
758db8dac20SDavid S. Miller 		goto discard;
759db8dac20SDavid S. Miller 
760db8dac20SDavid S. Miller 	/*
761db8dac20SDavid S. Miller 	 *	Multicast receive code
762db8dac20SDavid S. Miller 	 */
763db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
764e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
765e3163493SPavel Emelyanov 				saddr, daddr, udptable);
766db8dac20SDavid S. Miller 
767db8dac20SDavid S. Miller 	/* Unicast */
768db8dac20SDavid S. Miller 
769db8dac20SDavid S. Miller 	/*
770db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
771db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
772db8dac20SDavid S. Miller 	 */
773607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
774db8dac20SDavid S. Miller 
775db8dac20SDavid S. Miller 	if (sk == NULL) {
776db8dac20SDavid S. Miller 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
777db8dac20SDavid S. Miller 			goto discard;
778db8dac20SDavid S. Miller 
779db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
780db8dac20SDavid S. Miller 			goto discard;
781ef28d1a2SPavel Emelyanov 		UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
782ef28d1a2SPavel Emelyanov 				proto == IPPROTO_UDPLITE);
783db8dac20SDavid S. Miller 
7843ffe533cSAlexey Dobriyan 		icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
785db8dac20SDavid S. Miller 
786db8dac20SDavid S. Miller 		kfree_skb(skb);
787db8dac20SDavid S. Miller 		return 0;
788db8dac20SDavid S. Miller 	}
789db8dac20SDavid S. Miller 
790db8dac20SDavid S. Miller 	/* deliver */
791db8dac20SDavid S. Miller 
792c377411fSEric Dumazet 	if (sk_rcvqueues_full(sk, skb)) {
793c377411fSEric Dumazet 		sock_put(sk);
794c377411fSEric Dumazet 		goto discard;
795c377411fSEric Dumazet 	}
796d97106eaSHerbert Xu 	bh_lock_sock(sk);
797db8dac20SDavid S. Miller 	if (!sock_owned_by_user(sk))
798db8dac20SDavid S. Miller 		udpv6_queue_rcv_skb(sk, skb);
799a3a858ffSZhu Yi 	else if (sk_add_backlog(sk, skb)) {
80055349790SZhu Yi 		atomic_inc(&sk->sk_drops);
80155349790SZhu Yi 		bh_unlock_sock(sk);
80255349790SZhu Yi 		sock_put(sk);
80355349790SZhu Yi 		goto discard;
80455349790SZhu Yi 	}
805db8dac20SDavid S. Miller 	bh_unlock_sock(sk);
806db8dac20SDavid S. Miller 	sock_put(sk);
807db8dac20SDavid S. Miller 	return 0;
808db8dac20SDavid S. Miller 
809db8dac20SDavid S. Miller short_packet:
810d6bc0149SBjørn Mork 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
811db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
812d6bc0149SBjørn Mork 		       saddr,
813d6bc0149SBjørn Mork 		       ntohs(uh->source),
814d6bc0149SBjørn Mork 		       ulen,
815d6bc0149SBjørn Mork 		       skb->len,
816d6bc0149SBjørn Mork 		       daddr,
817d6bc0149SBjørn Mork 		       ntohs(uh->dest));
818db8dac20SDavid S. Miller 
819db8dac20SDavid S. Miller discard:
820ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
821db8dac20SDavid S. Miller 	kfree_skb(skb);
822db8dac20SDavid S. Miller 	return 0;
823db8dac20SDavid S. Miller }
824db8dac20SDavid S. Miller 
825db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
826db8dac20SDavid S. Miller {
827645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
828db8dac20SDavid S. Miller }
829db8dac20SDavid S. Miller 
830db8dac20SDavid S. Miller /*
831db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
832db8dac20SDavid S. Miller  */
833db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
834db8dac20SDavid S. Miller {
835db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
836db8dac20SDavid S. Miller 
83736d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
83836d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
83936d926b9SDenis V. Lunev 	else if (up->pending) {
840db8dac20SDavid S. Miller 		up->len = 0;
841db8dac20SDavid S. Miller 		up->pending = 0;
842db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
843db8dac20SDavid S. Miller 	}
844db8dac20SDavid S. Miller }
845db8dac20SDavid S. Miller 
846493c6be3SSridhar Samudrala /**
847493c6be3SSridhar Samudrala  * 	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
848493c6be3SSridhar Samudrala  * 	@sk: 	socket we are sending on
849493c6be3SSridhar Samudrala  * 	@skb: 	sk_buff containing the filled-in UDP header
850493c6be3SSridhar Samudrala  * 	        (checksum field must be zeroed out)
851493c6be3SSridhar Samudrala  */
852493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
853493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
854493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
855493c6be3SSridhar Samudrala {
856493c6be3SSridhar Samudrala 	unsigned int offset;
857493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
858493c6be3SSridhar Samudrala 	__wsum csum = 0;
859493c6be3SSridhar Samudrala 
860493c6be3SSridhar Samudrala 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
861493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
862493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
863493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
864493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
865493c6be3SSridhar Samudrala 	} else {
866493c6be3SSridhar Samudrala 		/*
867493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
868493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
869493c6be3SSridhar Samudrala 		 * should be together
870493c6be3SSridhar Samudrala 		 */
871493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
872493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
873493c6be3SSridhar Samudrala 
874493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
875493c6be3SSridhar Samudrala 
876493c6be3SSridhar Samudrala 		skb_queue_walk(&sk->sk_write_queue, skb) {
877493c6be3SSridhar Samudrala 			csum = csum_add(csum, skb->csum);
878493c6be3SSridhar Samudrala 		}
879493c6be3SSridhar Samudrala 
880493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
881493c6be3SSridhar Samudrala 					    csum);
882493c6be3SSridhar Samudrala 		if (uh->check == 0)
883493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
884493c6be3SSridhar Samudrala 	}
885493c6be3SSridhar Samudrala }
886493c6be3SSridhar Samudrala 
887db8dac20SDavid S. Miller /*
888db8dac20SDavid S. Miller  *	Sending
889db8dac20SDavid S. Miller  */
890db8dac20SDavid S. Miller 
891db8dac20SDavid S. Miller static int udp_v6_push_pending_frames(struct sock *sk)
892db8dac20SDavid S. Miller {
893db8dac20SDavid S. Miller 	struct sk_buff *skb;
894db8dac20SDavid S. Miller 	struct udphdr *uh;
895db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
896db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
8974c9483b2SDavid S. Miller 	struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
898db8dac20SDavid S. Miller 	int err = 0;
899db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
900db8dac20SDavid S. Miller 	__wsum csum = 0;
901db8dac20SDavid S. Miller 
902db8dac20SDavid S. Miller 	/* Grab the skbuff where UDP header space exists. */
903db8dac20SDavid S. Miller 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
904db8dac20SDavid S. Miller 		goto out;
905db8dac20SDavid S. Miller 
906db8dac20SDavid S. Miller 	/*
907db8dac20SDavid S. Miller 	 * Create a UDP header
908db8dac20SDavid S. Miller 	 */
909db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
9101958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
9111958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
912db8dac20SDavid S. Miller 	uh->len = htons(up->len);
913db8dac20SDavid S. Miller 	uh->check = 0;
914db8dac20SDavid S. Miller 
915db8dac20SDavid S. Miller 	if (is_udplite)
916db8dac20SDavid S. Miller 		csum = udplite_csum_outgoing(sk, skb);
917493c6be3SSridhar Samudrala 	else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
9184c9483b2SDavid S. Miller 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
919493c6be3SSridhar Samudrala 				     up->len);
920493c6be3SSridhar Samudrala 		goto send;
921493c6be3SSridhar Samudrala 	} else
922db8dac20SDavid S. Miller 		csum = udp_csum_outgoing(sk, skb);
923db8dac20SDavid S. Miller 
924db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
9254c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
9264c9483b2SDavid S. Miller 				    up->len, fl6->flowi6_proto, csum);
927db8dac20SDavid S. Miller 	if (uh->check == 0)
928db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
929db8dac20SDavid S. Miller 
930493c6be3SSridhar Samudrala send:
931db8dac20SDavid S. Miller 	err = ip6_push_pending_frames(sk);
9326ce9e7b5SEric Dumazet 	if (err) {
9336ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
9346ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
9356ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
9366ce9e7b5SEric Dumazet 			err = 0;
9376ce9e7b5SEric Dumazet 		}
9386ce9e7b5SEric Dumazet 	} else
9396ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
9406ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
941db8dac20SDavid S. Miller out:
942db8dac20SDavid S. Miller 	up->len = 0;
943db8dac20SDavid S. Miller 	up->pending = 0;
944db8dac20SDavid S. Miller 	return err;
945db8dac20SDavid S. Miller }
946db8dac20SDavid S. Miller 
947db8dac20SDavid S. Miller int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
948db8dac20SDavid S. Miller 		  struct msghdr *msg, size_t len)
949db8dac20SDavid S. Miller {
950db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
951db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
952db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
953db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
954db8dac20SDavid S. Miller 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
95520c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
956db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
957db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
9584c9483b2SDavid S. Miller 	struct flowi6 fl6;
959db8dac20SDavid S. Miller 	struct dst_entry *dst;
960db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
961db8dac20SDavid S. Miller 	int ulen = len;
962db8dac20SDavid S. Miller 	int hlimit = -1;
963db8dac20SDavid S. Miller 	int tclass = -1;
96413b52cd4SBrian Haley 	int dontfrag = -1;
965db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
966db8dac20SDavid S. Miller 	int err;
967db8dac20SDavid S. Miller 	int connected = 0;
968db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
969db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
970db8dac20SDavid S. Miller 
971db8dac20SDavid S. Miller 	/* destination address check */
972db8dac20SDavid S. Miller 	if (sin6) {
973db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
974db8dac20SDavid S. Miller 			return -EINVAL;
975db8dac20SDavid S. Miller 
976db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
977db8dac20SDavid S. Miller 		case AF_INET6:
978db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
979db8dac20SDavid S. Miller 				return -EINVAL;
980db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
981db8dac20SDavid S. Miller 			break;
982db8dac20SDavid S. Miller 		case AF_INET:
983db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
984db8dac20SDavid S. Miller 		case AF_UNSPEC:
985db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
986db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
987db8dac20SDavid S. Miller 			daddr = NULL;
988db8dac20SDavid S. Miller 			break;
989db8dac20SDavid S. Miller 		default:
990db8dac20SDavid S. Miller 			return -EINVAL;
991db8dac20SDavid S. Miller 		}
992db8dac20SDavid S. Miller 	} else if (!up->pending) {
993db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
994db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
995db8dac20SDavid S. Miller 		daddr = &np->daddr;
996db8dac20SDavid S. Miller 	} else
997db8dac20SDavid S. Miller 		daddr = NULL;
998db8dac20SDavid S. Miller 
999db8dac20SDavid S. Miller 	if (daddr) {
1000db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1001db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1002db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1003c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1004db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1005db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1006db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1007db8dac20SDavid S. Miller do_udp_sendmsg:
1008db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1009db8dac20SDavid S. Miller 				return -ENETUNREACH;
1010db8dac20SDavid S. Miller 			return udp_sendmsg(iocb, sk, msg, len);
1011db8dac20SDavid S. Miller 		}
1012db8dac20SDavid S. Miller 	}
1013db8dac20SDavid S. Miller 
1014db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
1015db8dac20SDavid S. Miller 		return udp_sendmsg(iocb, sk, msg, len);
1016db8dac20SDavid S. Miller 
1017db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1018db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1019db8dac20SDavid S. Miller 	   */
1020db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1021db8dac20SDavid S. Miller 		return -EMSGSIZE;
1022db8dac20SDavid S. Miller 
1023db8dac20SDavid S. Miller 	if (up->pending) {
1024db8dac20SDavid S. Miller 		/*
1025db8dac20SDavid S. Miller 		 * There are pending frames.
1026db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1027db8dac20SDavid S. Miller 		 */
1028db8dac20SDavid S. Miller 		lock_sock(sk);
1029db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1030db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1031db8dac20SDavid S. Miller 				release_sock(sk);
1032db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1033db8dac20SDavid S. Miller 			}
1034db8dac20SDavid S. Miller 			dst = NULL;
1035db8dac20SDavid S. Miller 			goto do_append_data;
1036db8dac20SDavid S. Miller 		}
1037db8dac20SDavid S. Miller 		release_sock(sk);
1038db8dac20SDavid S. Miller 	}
1039db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1040db8dac20SDavid S. Miller 
10414c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1042db8dac20SDavid S. Miller 
1043db8dac20SDavid S. Miller 	if (sin6) {
1044db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1045db8dac20SDavid S. Miller 			return -EINVAL;
1046db8dac20SDavid S. Miller 
10471958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1048db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1049db8dac20SDavid S. Miller 
1050db8dac20SDavid S. Miller 		if (np->sndflow) {
10514c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
10524c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
10534c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1054db8dac20SDavid S. Miller 				if (flowlabel == NULL)
1055db8dac20SDavid S. Miller 					return -EINVAL;
1056db8dac20SDavid S. Miller 				daddr = &flowlabel->dst;
1057db8dac20SDavid S. Miller 			}
1058db8dac20SDavid S. Miller 		}
1059db8dac20SDavid S. Miller 
1060db8dac20SDavid S. Miller 		/*
1061db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1062db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1063db8dac20SDavid S. Miller 		 */
1064db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1065db8dac20SDavid S. Miller 		    ipv6_addr_equal(daddr, &np->daddr))
1066db8dac20SDavid S. Miller 			daddr = &np->daddr;
1067db8dac20SDavid S. Miller 
1068db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1069db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1070db8dac20SDavid S. Miller 		    ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
10714c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1072db8dac20SDavid S. Miller 	} else {
1073db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1074db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1075db8dac20SDavid S. Miller 
10761958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1077db8dac20SDavid S. Miller 		daddr = &np->daddr;
10784c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1079db8dac20SDavid S. Miller 		connected = 1;
1080db8dac20SDavid S. Miller 	}
1081db8dac20SDavid S. Miller 
10824c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
10834c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1084db8dac20SDavid S. Miller 
10854c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
10864c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
10879f690db7SYang Hongyang 
10884c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
108951953d5bSBrian Haley 
1090db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1091db8dac20SDavid S. Miller 		opt = &opt_space;
1092db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1093db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1094db8dac20SDavid S. Miller 
1095ec0506dbSMaciej Żenczykowski 		err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1096ec0506dbSMaciej Żenczykowski 					&hlimit, &tclass, &dontfrag);
1097db8dac20SDavid S. Miller 		if (err < 0) {
1098db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1099db8dac20SDavid S. Miller 			return err;
1100db8dac20SDavid S. Miller 		}
11014c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
11024c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1103db8dac20SDavid S. Miller 			if (flowlabel == NULL)
1104db8dac20SDavid S. Miller 				return -EINVAL;
1105db8dac20SDavid S. Miller 		}
1106db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1107db8dac20SDavid S. Miller 			opt = NULL;
1108db8dac20SDavid S. Miller 		connected = 0;
1109db8dac20SDavid S. Miller 	}
1110db8dac20SDavid S. Miller 	if (opt == NULL)
1111db8dac20SDavid S. Miller 		opt = np->opt;
1112db8dac20SDavid S. Miller 	if (flowlabel)
1113db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1114db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1115db8dac20SDavid S. Miller 
11164c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1117876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
11184c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.daddr, daddr);
1119876c7f41SBrian Haley 	else
11204c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
11214c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
11224c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.saddr, &np->saddr);
11231958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1124db8dac20SDavid S. Miller 
11254c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
112620c59de2SArnaud Ebalard 	if (final_p)
1127db8dac20SDavid S. Miller 		connected = 0;
1128db8dac20SDavid S. Miller 
11294c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
11304c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1131db8dac20SDavid S. Miller 		connected = 0;
1132db8dac20SDavid S. Miller 	}
1133db8dac20SDavid S. Miller 
11344c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1135db8dac20SDavid S. Miller 
11364c9483b2SDavid S. Miller 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
113768d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
113868d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
113968d0c6d3SDavid S. Miller 		dst = NULL;
1140db8dac20SDavid S. Miller 		goto out;
1141db8dac20SDavid S. Miller 	}
1142db8dac20SDavid S. Miller 
1143db8dac20SDavid S. Miller 	if (hlimit < 0) {
11444c9483b2SDavid S. Miller 		if (ipv6_addr_is_multicast(&fl6.daddr))
1145db8dac20SDavid S. Miller 			hlimit = np->mcast_hops;
1146db8dac20SDavid S. Miller 		else
1147db8dac20SDavid S. Miller 			hlimit = np->hop_limit;
1148db8dac20SDavid S. Miller 		if (hlimit < 0)
11496b75d090SYOSHIFUJI Hideaki 			hlimit = ip6_dst_hoplimit(dst);
1150db8dac20SDavid S. Miller 	}
1151db8dac20SDavid S. Miller 
1152db8dac20SDavid S. Miller 	if (tclass < 0)
1153e651f03aSGerrit Renker 		tclass = np->tclass;
1154db8dac20SDavid S. Miller 
115513b52cd4SBrian Haley 	if (dontfrag < 0)
115613b52cd4SBrian Haley 		dontfrag = np->dontfrag;
115713b52cd4SBrian Haley 
1158db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1159db8dac20SDavid S. Miller 		goto do_confirm;
1160db8dac20SDavid S. Miller back_from_confirm:
1161db8dac20SDavid S. Miller 
1162db8dac20SDavid S. Miller 	lock_sock(sk);
1163db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1164db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1165db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1166db8dac20SDavid S. Miller 		release_sock(sk);
1167db8dac20SDavid S. Miller 
1168db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1169db8dac20SDavid S. Miller 		err = -EINVAL;
1170db8dac20SDavid S. Miller 		goto out;
1171db8dac20SDavid S. Miller 	}
1172db8dac20SDavid S. Miller 
1173db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1174db8dac20SDavid S. Miller 
1175db8dac20SDavid S. Miller do_append_data:
1176db8dac20SDavid S. Miller 	up->len += ulen;
1177db8dac20SDavid S. Miller 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1178db8dac20SDavid S. Miller 	err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
11794c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1180db8dac20SDavid S. Miller 		(struct rt6_info*)dst,
118113b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1182db8dac20SDavid S. Miller 	if (err)
1183db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1184db8dac20SDavid S. Miller 	else if (!corkreq)
1185db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1186db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1187db8dac20SDavid S. Miller 		up->pending = 0;
1188db8dac20SDavid S. Miller 
1189db8dac20SDavid S. Miller 	if (dst) {
1190db8dac20SDavid S. Miller 		if (connected) {
1191db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
11924c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
1193db8dac20SDavid S. Miller 				      &np->daddr : NULL,
1194db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
11954c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1196db8dac20SDavid S. Miller 				      &np->saddr :
1197db8dac20SDavid S. Miller #endif
1198db8dac20SDavid S. Miller 				      NULL);
1199db8dac20SDavid S. Miller 		} else {
1200db8dac20SDavid S. Miller 			dst_release(dst);
1201db8dac20SDavid S. Miller 		}
1202a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1203db8dac20SDavid S. Miller 	}
1204db8dac20SDavid S. Miller 
1205db8dac20SDavid S. Miller 	if (err > 0)
1206db8dac20SDavid S. Miller 		err = np->recverr ? net_xmit_errno(err) : 0;
1207db8dac20SDavid S. Miller 	release_sock(sk);
1208db8dac20SDavid S. Miller out:
1209a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1210db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
1211db8dac20SDavid S. Miller 	if (!err)
1212db8dac20SDavid S. Miller 		return len;
1213db8dac20SDavid S. Miller 	/*
1214db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1215db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1216db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1217db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1218db8dac20SDavid S. Miller 	 * seems like overkill.
1219db8dac20SDavid S. Miller 	 */
1220db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1221235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1222235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1223db8dac20SDavid S. Miller 	}
1224db8dac20SDavid S. Miller 	return err;
1225db8dac20SDavid S. Miller 
1226db8dac20SDavid S. Miller do_confirm:
1227db8dac20SDavid S. Miller 	dst_confirm(dst);
1228db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1229db8dac20SDavid S. Miller 		goto back_from_confirm;
1230db8dac20SDavid S. Miller 	err = 0;
1231db8dac20SDavid S. Miller 	goto out;
1232db8dac20SDavid S. Miller }
1233db8dac20SDavid S. Miller 
12347d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1235db8dac20SDavid S. Miller {
1236db8dac20SDavid S. Miller 	lock_sock(sk);
1237db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1238db8dac20SDavid S. Miller 	release_sock(sk);
1239db8dac20SDavid S. Miller 
1240db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1241db8dac20SDavid S. Miller }
1242db8dac20SDavid S. Miller 
1243db8dac20SDavid S. Miller /*
1244db8dac20SDavid S. Miller  *	Socket option code for UDP
1245db8dac20SDavid S. Miller  */
1246db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1247b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1248db8dac20SDavid S. Miller {
1249db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1250db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1251db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1252db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1253db8dac20SDavid S. Miller }
1254db8dac20SDavid S. Miller 
1255db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1256db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1257b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1258db8dac20SDavid S. Miller {
1259db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1260db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1261db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1262db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1263db8dac20SDavid S. Miller }
1264db8dac20SDavid S. Miller #endif
1265db8dac20SDavid S. Miller 
1266db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1267db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1268db8dac20SDavid S. Miller {
1269db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1270db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1271db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1272db8dac20SDavid S. Miller }
1273db8dac20SDavid S. Miller 
1274db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1275db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1276db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1277db8dac20SDavid S. Miller {
1278db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1279db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1280db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1281db8dac20SDavid S. Miller }
1282db8dac20SDavid S. Miller #endif
1283db8dac20SDavid S. Miller 
1284ba735425SSridhar Samudrala static int udp6_ufo_send_check(struct sk_buff *skb)
1285ba735425SSridhar Samudrala {
1286b71d1d42SEric Dumazet 	const struct ipv6hdr *ipv6h;
1287ba735425SSridhar Samudrala 	struct udphdr *uh;
1288ba735425SSridhar Samudrala 
1289ba735425SSridhar Samudrala 	if (!pskb_may_pull(skb, sizeof(*uh)))
1290ba735425SSridhar Samudrala 		return -EINVAL;
1291ba735425SSridhar Samudrala 
1292ba735425SSridhar Samudrala 	ipv6h = ipv6_hdr(skb);
1293ba735425SSridhar Samudrala 	uh = udp_hdr(skb);
1294ba735425SSridhar Samudrala 
1295ba735425SSridhar Samudrala 	uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1296ba735425SSridhar Samudrala 				     IPPROTO_UDP, 0);
1297ba735425SSridhar Samudrala 	skb->csum_start = skb_transport_header(skb) - skb->head;
1298ba735425SSridhar Samudrala 	skb->csum_offset = offsetof(struct udphdr, check);
1299ba735425SSridhar Samudrala 	skb->ip_summed = CHECKSUM_PARTIAL;
1300ba735425SSridhar Samudrala 	return 0;
1301ba735425SSridhar Samudrala }
1302ba735425SSridhar Samudrala 
1303c8f44affSMichał Mirosław static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
1304c8f44affSMichał Mirosław 	netdev_features_t features)
1305ba735425SSridhar Samudrala {
1306ba735425SSridhar Samudrala 	struct sk_buff *segs = ERR_PTR(-EINVAL);
1307ba735425SSridhar Samudrala 	unsigned int mss;
1308ba735425SSridhar Samudrala 	unsigned int unfrag_ip6hlen, unfrag_len;
1309ba735425SSridhar Samudrala 	struct frag_hdr *fptr;
1310ba735425SSridhar Samudrala 	u8 *mac_start, *prevhdr;
1311ba735425SSridhar Samudrala 	u8 nexthdr;
1312ba735425SSridhar Samudrala 	u8 frag_hdr_sz = sizeof(struct frag_hdr);
1313ba735425SSridhar Samudrala 	int offset;
1314ba735425SSridhar Samudrala 	__wsum csum;
1315ba735425SSridhar Samudrala 
1316ba735425SSridhar Samudrala 	mss = skb_shinfo(skb)->gso_size;
1317ba735425SSridhar Samudrala 	if (unlikely(skb->len <= mss))
1318ba735425SSridhar Samudrala 		goto out;
1319ba735425SSridhar Samudrala 
1320ba735425SSridhar Samudrala 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1321ba735425SSridhar Samudrala 		/* Packet is from an untrusted source, reset gso_segs. */
1322ba735425SSridhar Samudrala 		int type = skb_shinfo(skb)->gso_type;
1323ba735425SSridhar Samudrala 
1324ba735425SSridhar Samudrala 		if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1325ba735425SSridhar Samudrala 			     !(type & (SKB_GSO_UDP))))
1326ba735425SSridhar Samudrala 			goto out;
1327ba735425SSridhar Samudrala 
1328ba735425SSridhar Samudrala 		skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1329ba735425SSridhar Samudrala 
1330ba735425SSridhar Samudrala 		segs = NULL;
1331ba735425SSridhar Samudrala 		goto out;
1332ba735425SSridhar Samudrala 	}
1333ba735425SSridhar Samudrala 
1334ba735425SSridhar Samudrala 	/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1335ba735425SSridhar Samudrala 	 * do checksum of UDP packets sent as multiple IP fragments.
1336ba735425SSridhar Samudrala 	 */
133796339d6cSShan Wei 	offset = skb_checksum_start_offset(skb);
1338ba735425SSridhar Samudrala 	csum = skb_checksum(skb, offset, skb->len- offset, 0);
1339ba735425SSridhar Samudrala 	offset += skb->csum_offset;
1340ba735425SSridhar Samudrala 	*(__sum16 *)(skb->data + offset) = csum_fold(csum);
1341ba735425SSridhar Samudrala 	skb->ip_summed = CHECKSUM_NONE;
1342ba735425SSridhar Samudrala 
1343ba735425SSridhar Samudrala 	/* Check if there is enough headroom to insert fragment header. */
1344a9cf73eaSShan Wei 	if ((skb_mac_header(skb) < skb->head + frag_hdr_sz) &&
1345ba735425SSridhar Samudrala 	    pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1346ba735425SSridhar Samudrala 		goto out;
1347ba735425SSridhar Samudrala 
1348ba735425SSridhar Samudrala 	/* Find the unfragmentable header and shift it left by frag_hdr_sz
1349ba735425SSridhar Samudrala 	 * bytes to insert fragment header.
1350ba735425SSridhar Samudrala 	 */
1351ba735425SSridhar Samudrala 	unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1352ba735425SSridhar Samudrala 	nexthdr = *prevhdr;
1353ba735425SSridhar Samudrala 	*prevhdr = NEXTHDR_FRAGMENT;
1354ba735425SSridhar Samudrala 	unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1355ba735425SSridhar Samudrala 		     unfrag_ip6hlen;
1356ba735425SSridhar Samudrala 	mac_start = skb_mac_header(skb);
1357ba735425SSridhar Samudrala 	memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1358ba735425SSridhar Samudrala 
1359ba735425SSridhar Samudrala 	skb->mac_header -= frag_hdr_sz;
1360ba735425SSridhar Samudrala 	skb->network_header -= frag_hdr_sz;
1361ba735425SSridhar Samudrala 
1362ba735425SSridhar Samudrala 	fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1363ba735425SSridhar Samudrala 	fptr->nexthdr = nexthdr;
1364ba735425SSridhar Samudrala 	fptr->reserved = 0;
136587c48fa3SEric Dumazet 	ipv6_select_ident(fptr, (struct rt6_info *)skb_dst(skb));
1366ba735425SSridhar Samudrala 
1367ba735425SSridhar Samudrala 	/* Fragment the skb. ipv6 header and the remaining fields of the
1368ba735425SSridhar Samudrala 	 * fragment header are updated in ipv6_gso_segment()
1369ba735425SSridhar Samudrala 	 */
1370ba735425SSridhar Samudrala 	segs = skb_segment(skb, features);
1371ba735425SSridhar Samudrala 
1372ba735425SSridhar Samudrala out:
1373ba735425SSridhar Samudrala 	return segs;
1374ba735425SSridhar Samudrala }
1375ba735425SSridhar Samudrala 
137641135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1377db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1378db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1379ba735425SSridhar Samudrala 	.gso_send_check =	udp6_ufo_send_check,
1380ba735425SSridhar Samudrala 	.gso_segment	=	udp6_ufo_fragment,
1381db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1382db8dac20SDavid S. Miller };
1383db8dac20SDavid S. Miller 
1384db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1385db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1386db8dac20SDavid S. Miller 
1387db8dac20SDavid S. Miller static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1388db8dac20SDavid S. Miller {
1389db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
1390db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sp);
1391b71d1d42SEric Dumazet 	const struct in6_addr *dest, *src;
1392db8dac20SDavid S. Miller 	__u16 destp, srcp;
1393db8dac20SDavid S. Miller 
1394db8dac20SDavid S. Miller 	dest  = &np->daddr;
1395db8dac20SDavid S. Miller 	src   = &np->rcv_saddr;
1396c720c7e8SEric Dumazet 	destp = ntohs(inet->inet_dport);
1397c720c7e8SEric Dumazet 	srcp  = ntohs(inet->inet_sport);
1398db8dac20SDavid S. Miller 	seq_printf(seq,
1399f86dcc5aSEric Dumazet 		   "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
140071338aa7SDan Rosenberg 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
1401db8dac20SDavid S. Miller 		   bucket,
1402db8dac20SDavid S. Miller 		   src->s6_addr32[0], src->s6_addr32[1],
1403db8dac20SDavid S. Miller 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
1404db8dac20SDavid S. Miller 		   dest->s6_addr32[0], dest->s6_addr32[1],
1405db8dac20SDavid S. Miller 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
1406db8dac20SDavid S. Miller 		   sp->sk_state,
140731e6d363SEric Dumazet 		   sk_wmem_alloc_get(sp),
140831e6d363SEric Dumazet 		   sk_rmem_alloc_get(sp),
1409db8dac20SDavid S. Miller 		   0, 0L, 0,
1410db8dac20SDavid S. Miller 		   sock_i_uid(sp), 0,
1411db8dac20SDavid S. Miller 		   sock_i_ino(sp),
1412cb61cb9bSEric Dumazet 		   atomic_read(&sp->sk_refcnt), sp,
1413cb61cb9bSEric Dumazet 		   atomic_read(&sp->sk_drops));
1414db8dac20SDavid S. Miller }
1415db8dac20SDavid S. Miller 
1416db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1417db8dac20SDavid S. Miller {
1418db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
1419db8dac20SDavid S. Miller 		seq_printf(seq,
1420db8dac20SDavid S. Miller 			   "  sl  "
1421db8dac20SDavid S. Miller 			   "local_address                         "
1422db8dac20SDavid S. Miller 			   "remote_address                        "
1423db8dac20SDavid S. Miller 			   "st tx_queue rx_queue tr tm->when retrnsmt"
1424cb61cb9bSEric Dumazet 			   "   uid  timeout inode ref pointer drops\n");
1425db8dac20SDavid S. Miller 	else
1426db8dac20SDavid S. Miller 		udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1427db8dac20SDavid S. Miller 	return 0;
1428db8dac20SDavid S. Miller }
1429db8dac20SDavid S. Miller 
143073cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
143173cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
143273cb88ecSArjan van de Ven 	.open     = udp_seq_open,
143373cb88ecSArjan van de Ven 	.read     = seq_read,
143473cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
143573cb88ecSArjan van de Ven 	.release  = seq_release_net
143673cb88ecSArjan van de Ven };
143773cb88ecSArjan van de Ven 
1438db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1439db8dac20SDavid S. Miller 	.name		= "udp6",
1440db8dac20SDavid S. Miller 	.family		= AF_INET6,
1441645ca708SEric Dumazet 	.udp_table	= &udp_table,
144273cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1443dda61925SDenis V. Lunev 	.seq_ops	= {
1444dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1445dda61925SDenis V. Lunev 	},
1446db8dac20SDavid S. Miller };
1447db8dac20SDavid S. Miller 
14482c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1449db8dac20SDavid S. Miller {
14500c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1451db8dac20SDavid S. Miller }
1452db8dac20SDavid S. Miller 
14530c96d8c5SDaniel Lezcano void udp6_proc_exit(struct net *net) {
14540c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1455db8dac20SDavid S. Miller }
1456db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1457db8dac20SDavid S. Miller 
1458db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1459db8dac20SDavid S. Miller 
1460db8dac20SDavid S. Miller struct proto udpv6_prot = {
1461db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1462db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1463db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1464db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1465db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1466db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1467db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1468db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1469db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1470db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1471db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1472db8dac20SDavid S. Miller 	.backlog_rcv	   = udpv6_queue_rcv_skb,
1473db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1474db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1475719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1476db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1477db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1478db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1479db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1480db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1481db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1482271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1483645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1484db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1485db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1486db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1487db8dac20SDavid S. Miller #endif
1488fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
1489db8dac20SDavid S. Miller };
1490db8dac20SDavid S. Miller 
1491db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1492db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1493db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1494db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1495db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1496db8dac20SDavid S. Miller 	.no_check =  UDP_CSUM_DEFAULT,
1497db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1498db8dac20SDavid S. Miller };
1499db8dac20SDavid S. Miller 
1500db8dac20SDavid S. Miller 
1501db8dac20SDavid S. Miller int __init udpv6_init(void)
1502db8dac20SDavid S. Miller {
1503db8dac20SDavid S. Miller 	int ret;
1504db8dac20SDavid S. Miller 
1505db8dac20SDavid S. Miller 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1506db8dac20SDavid S. Miller 	if (ret)
1507db8dac20SDavid S. Miller 		goto out;
1508db8dac20SDavid S. Miller 
1509db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1510db8dac20SDavid S. Miller 	if (ret)
1511db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1512db8dac20SDavid S. Miller out:
1513db8dac20SDavid S. Miller 	return ret;
1514db8dac20SDavid S. Miller 
1515db8dac20SDavid S. Miller out_udpv6_protocol:
1516db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1517db8dac20SDavid S. Miller 	goto out;
1518db8dac20SDavid S. Miller }
1519db8dac20SDavid S. Miller 
1520db8dac20SDavid S. Miller void udpv6_exit(void)
1521db8dac20SDavid S. Miller {
1522db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1523db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1524db8dac20SDavid S. Miller }
1525