blob: 0aba654f5b91c198cce365a5dd619e30b566e798 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UDP over IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/ipv4/udp.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ipv6.h>
33#include <linux/icmpv6.h>
34#include <linux/init.h>
Herbert Xu1781f7f2007-12-11 11:30:32 -080035#include <linux/module.h>
Herbert Xu3305b802005-12-13 23:16:37 -080036#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <net/ndisc.h>
41#include <net/protocol.h>
42#include <net/transp_v6.h>
43#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <net/raw.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070045#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/ip6_checksum.h>
47#include <net/xfrm.h>
Tom Herbert72289b92013-01-22 09:50:44 +000048#include <net/inet6_hashtables.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030049#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <linux/proc_fs.h>
52#include <linux/seq_file.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000053#include <trace/events/skb.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080054#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Eric Dumazet6eada012015-03-18 14:05:33 -070056static u32 udp6_ehashfn(const struct net *net,
57 const struct in6_addr *laddr,
58 const u16 lport,
59 const struct in6_addr *faddr,
60 const __be16 fport)
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +020061{
Hannes Frederic Sowa1bbdcee2013-10-19 21:48:57 +020062 static u32 udp6_ehash_secret __read_mostly;
63 static u32 udp_ipv6_hash_secret __read_mostly;
64
65 u32 lhash, fhash;
66
67 net_get_random_once(&udp6_ehash_secret,
68 sizeof(udp6_ehash_secret));
69 net_get_random_once(&udp_ipv6_hash_secret,
70 sizeof(udp_ipv6_hash_secret));
71
72 lhash = (__force u32)laddr->s6_addr32[3];
73 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
74
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +020075 return __inet6_ehashfn(lhash, lport, fhash, fport,
Hannes Frederic Sowa1bbdcee2013-10-19 21:48:57 +020076 udp_ipv6_hash_secret + net_hash_mix(net));
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +020077}
78
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000079int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
80{
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000081 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000082 int sk2_ipv6only = inet_v6_ipv6only(sk2);
Eric Dumazetefe42082013-10-03 15:42:29 -070083 int addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000084 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
85
86 /* if both are mapped, treat as IPv4 */
87 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
Vlad Yasevich499923c2009-04-09 17:37:33 +000088 return (!sk2_ipv6only &&
Eric Dumazet50805462013-10-02 04:29:50 -070089 (!sk->sk_rcv_saddr || !sk2->sk_rcv_saddr ||
90 sk->sk_rcv_saddr == sk2->sk_rcv_saddr));
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000091
92 if (addr_type2 == IPV6_ADDR_ANY &&
93 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
94 return 1;
95
96 if (addr_type == IPV6_ADDR_ANY &&
Eric Dumazet9fe516b2014-06-27 08:36:16 -070097 !(ipv6_only_sock(sk) && addr_type2 == IPV6_ADDR_MAPPED))
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000098 return 1;
99
100 if (sk2_rcv_saddr6 &&
Eric Dumazetefe42082013-10-03 15:42:29 -0700101 ipv6_addr_equal(&sk->sk_v6_rcv_saddr, sk2_rcv_saddr6))
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +0000102 return 1;
103
104 return 0;
105}
106
Eric Dumazet6eada012015-03-18 14:05:33 -0700107static u32 udp6_portaddr_hash(const struct net *net,
108 const struct in6_addr *addr6,
109 unsigned int port)
Eric Dumazetd4cada42009-11-08 10:17:30 +0000110{
111 unsigned int hash, mix = net_hash_mix(net);
112
113 if (ipv6_addr_any(addr6))
114 hash = jhash_1word(0, mix);
Brian Haley856540e2009-11-09 12:05:53 +0000115 else if (ipv6_addr_v4mapped(addr6))
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700116 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
Eric Dumazetd4cada42009-11-08 10:17:30 +0000117 else
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700118 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
Eric Dumazetd4cada42009-11-08 10:17:30 +0000119
120 return hash ^ port;
121}
122
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700123int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Eric Dumazet30fff922009-11-09 05:26:33 +0000125 unsigned int hash2_nulladdr =
126 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
Jeffrin Jose9a52e972012-05-19 01:45:21 +0000127 unsigned int hash2_partial =
Eric Dumazetefe42082013-10-03 15:42:29 -0700128 udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
Eric Dumazet30fff922009-11-09 05:26:33 +0000129
Eric Dumazetd4cada42009-11-08 10:17:30 +0000130 /* precompute partial secondary hash */
Eric Dumazet30fff922009-11-09 05:26:33 +0000131 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
132 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Eric Dumazet719f8352010-09-08 05:08:44 +0000135static void udp_v6_rehash(struct sock *sk)
136{
137 u16 new_hash = udp6_portaddr_hash(sock_net(sk),
Eric Dumazetefe42082013-10-03 15:42:29 -0700138 &sk->sk_v6_rcv_saddr,
Eric Dumazet719f8352010-09-08 05:08:44 +0000139 inet_sk(sk)->inet_num);
140
141 udp_lib_rehash(sk, new_hash);
142}
143
Eric Dumazet645ca702008-10-29 01:41:45 -0700144static inline int compute_score(struct sock *sk, struct net *net,
145 unsigned short hnum,
Balazs Scheidler88440ae2010-10-21 16:04:33 +0200146 const struct in6_addr *saddr, __be16 sport,
147 const struct in6_addr *daddr, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700148 int dif)
149{
Joe Perches60c04ae2014-12-01 20:29:06 -0800150 int score;
151 struct inet_sock *inet;
Eric Dumazet645ca702008-10-29 01:41:45 -0700152
Joe Perches60c04ae2014-12-01 20:29:06 -0800153 if (!net_eq(sock_net(sk), net) ||
154 udp_sk(sk)->udp_port_hash != hnum ||
155 sk->sk_family != PF_INET6)
156 return -1;
Eric Dumazet645ca702008-10-29 01:41:45 -0700157
Joe Perches60c04ae2014-12-01 20:29:06 -0800158 score = 0;
159 inet = inet_sk(sk);
160
161 if (inet->inet_dport) {
162 if (inet->inet_dport != sport)
163 return -1;
164 score++;
Eric Dumazet645ca702008-10-29 01:41:45 -0700165 }
Joe Perches60c04ae2014-12-01 20:29:06 -0800166
167 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
168 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
169 return -1;
170 score++;
171 }
172
173 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
174 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
175 return -1;
176 score++;
177 }
178
179 if (sk->sk_bound_dev_if) {
180 if (sk->sk_bound_dev_if != dif)
181 return -1;
182 score++;
183 }
184
Eric Dumazet645ca702008-10-29 01:41:45 -0700185 return score;
186}
187
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000188#define SCORE2_MAX (1 + 1 + 1)
189static inline int compute_score2(struct sock *sk, struct net *net,
Joe Perches60c04ae2014-12-01 20:29:06 -0800190 const struct in6_addr *saddr, __be16 sport,
191 const struct in6_addr *daddr,
192 unsigned short hnum, int dif)
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000193{
Joe Perches60c04ae2014-12-01 20:29:06 -0800194 int score;
195 struct inet_sock *inet;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000196
Joe Perches60c04ae2014-12-01 20:29:06 -0800197 if (!net_eq(sock_net(sk), net) ||
198 udp_sk(sk)->udp_port_hash != hnum ||
199 sk->sk_family != PF_INET6)
200 return -1;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000201
Joe Perches60c04ae2014-12-01 20:29:06 -0800202 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
203 return -1;
204
205 score = 0;
206 inet = inet_sk(sk);
207
208 if (inet->inet_dport) {
209 if (inet->inet_dport != sport)
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000210 return -1;
Joe Perches60c04ae2014-12-01 20:29:06 -0800211 score++;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000212 }
Joe Perches60c04ae2014-12-01 20:29:06 -0800213
214 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
215 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
216 return -1;
217 score++;
218 }
219
220 if (sk->sk_bound_dev_if) {
221 if (sk->sk_bound_dev_if != dif)
222 return -1;
223 score++;
224 }
225
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000226 return score;
227}
228
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000229/* called with read_rcu_lock() */
230static struct sock *udp6_lib_lookup2(struct net *net,
231 const struct in6_addr *saddr, __be16 sport,
232 const struct in6_addr *daddr, unsigned int hnum, int dif,
233 struct udp_hslot *hslot2, unsigned int slot2)
234{
235 struct sock *sk, *result;
236 struct hlist_nulls_node *node;
Tom Herbert72289b92013-01-22 09:50:44 +0000237 int score, badness, matches = 0, reuseport = 0;
238 u32 hash = 0;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000239
240begin:
241 result = NULL;
242 badness = -1;
243 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
244 score = compute_score2(sk, net, saddr, sport,
245 daddr, hnum, dif);
246 if (score > badness) {
247 result = sk;
248 badness = score;
Tom Herbert72289b92013-01-22 09:50:44 +0000249 reuseport = sk->sk_reuseport;
250 if (reuseport) {
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +0200251 hash = udp6_ehashfn(net, daddr, hnum,
252 saddr, sport);
Tom Herbert72289b92013-01-22 09:50:44 +0000253 matches = 1;
254 } else if (score == SCORE2_MAX)
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000255 goto exact_match;
Tom Herbert72289b92013-01-22 09:50:44 +0000256 } else if (score == badness && reuseport) {
257 matches++;
Daniel Borkmann8fc54f62014-08-23 20:58:54 +0200258 if (reciprocal_scale(hash, matches) == 0)
Tom Herbert72289b92013-01-22 09:50:44 +0000259 result = sk;
260 hash = next_pseudo_random32(hash);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000261 }
262 }
263 /*
264 * if the nulls value we got at the end of this lookup is
265 * not the expected one, we must restart lookup.
266 * We probably met an item that was moved to another chain.
267 */
268 if (get_nulls_value(node) != slot2)
269 goto begin;
270
271 if (result) {
272exact_match:
Eric Dumazetc31504d2010-11-15 19:58:26 +0000273 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000274 result = NULL;
275 else if (unlikely(compute_score2(result, net, saddr, sport,
276 daddr, hnum, dif) < badness)) {
277 sock_put(result);
278 goto begin;
279 }
280 }
281 return result;
282}
283
Pavel Emelyanovfce82332011-12-09 06:23:34 +0000284struct sock *__udp6_lib_lookup(struct net *net,
Balazs Scheidler88440ae2010-10-21 16:04:33 +0200285 const struct in6_addr *saddr, __be16 sport,
286 const struct in6_addr *daddr, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700287 int dif, struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Eric Dumazet271b72c2008-10-29 02:11:14 -0700289 struct sock *sk, *result;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800290 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned short hnum = ntohs(dport);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000292 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
293 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
Tom Herbert72289b92013-01-22 09:50:44 +0000294 int score, badness, matches = 0, reuseport = 0;
295 u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Eric Dumazet271b72c2008-10-29 02:11:14 -0700297 rcu_read_lock();
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000298 if (hslot->count > 10) {
299 hash2 = udp6_portaddr_hash(net, daddr, hnum);
300 slot2 = hash2 & udptable->mask;
301 hslot2 = &udptable->hash2[slot2];
302 if (hslot->count < hslot2->count)
303 goto begin;
304
305 result = udp6_lib_lookup2(net, saddr, sport,
306 daddr, hnum, dif,
307 hslot2, slot2);
308 if (!result) {
309 hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
310 slot2 = hash2 & udptable->mask;
311 hslot2 = &udptable->hash2[slot2];
312 if (hslot->count < hslot2->count)
313 goto begin;
314
Jorge Boncompte [DTI2]1223c672010-04-08 04:56:48 +0000315 result = udp6_lib_lookup2(net, saddr, sport,
316 &in6addr_any, hnum, dif,
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000317 hslot2, slot2);
318 }
319 rcu_read_unlock();
320 return result;
321 }
Eric Dumazet271b72c2008-10-29 02:11:14 -0700322begin:
323 result = NULL;
324 badness = -1;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800325 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
Eric Dumazet645ca702008-10-29 01:41:45 -0700326 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
327 if (score > badness) {
328 result = sk;
329 badness = score;
Tom Herbert72289b92013-01-22 09:50:44 +0000330 reuseport = sk->sk_reuseport;
331 if (reuseport) {
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +0200332 hash = udp6_ehashfn(net, daddr, hnum,
333 saddr, sport);
Tom Herbert72289b92013-01-22 09:50:44 +0000334 matches = 1;
335 }
336 } else if (score == badness && reuseport) {
337 matches++;
Daniel Borkmann8fc54f62014-08-23 20:58:54 +0200338 if (reciprocal_scale(hash, matches) == 0)
Tom Herbert72289b92013-01-22 09:50:44 +0000339 result = sk;
340 hash = next_pseudo_random32(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
Eric Dumazet88ab1932008-11-16 19:39:21 -0800343 /*
344 * if the nulls value we got at the end of this lookup is
345 * not the expected one, we must restart lookup.
346 * We probably met an item that was moved to another chain.
347 */
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000348 if (get_nulls_value(node) != slot)
Eric Dumazet88ab1932008-11-16 19:39:21 -0800349 goto begin;
350
Eric Dumazet271b72c2008-10-29 02:11:14 -0700351 if (result) {
Eric Dumazetc31504d2010-11-15 19:58:26 +0000352 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
Eric Dumazet271b72c2008-10-29 02:11:14 -0700353 result = NULL;
354 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
355 daddr, dport, dif) < badness)) {
356 sock_put(result);
357 goto begin;
358 }
359 }
360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return result;
362}
Pavel Emelyanovfce82332011-12-09 06:23:34 +0000363EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700365static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
366 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700367 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700368{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700369 struct sock *sk;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000370 const struct ipv6hdr *iph = ipv6_hdr(skb);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700371
Ian Morrise5d08d72014-11-23 21:28:43 +0000372 sk = skb_steal_sock(skb);
373 if (unlikely(sk))
KOVACS Krisztian23542612008-10-07 12:41:01 -0700374 return sk;
Eric Dumazetadf30902009-06-02 05:19:30 +0000375 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
376 &iph->daddr, dport, inet6_iif(skb),
377 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700378}
379
Balazs Scheidleraa976fc2010-10-21 16:05:41 +0200380struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
381 const struct in6_addr *daddr, __be16 dport, int dif)
382{
383 return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
384}
385EXPORT_SYMBOL_GPL(udp6_lib_lookup);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
Ian Morris67ba4152014-08-24 21:53:10 +0100388 * This should be easy, if there is something there we
389 * return it, otherwise we block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391
Ying Xue1b784142015-03-02 15:37:48 +0800392int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int noblock, int flags, int *addr_len)
394{
395 struct ipv6_pinfo *np = inet6_sk(sk);
396 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900397 struct sk_buff *skb;
David S. Miller59c2cda2011-12-01 14:12:55 -0500398 unsigned int ulen, copied;
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000399 int peeked, off = 0;
Herbert Xu759e5d02007-03-25 20:10:56 -0700400 int err;
401 int is_udplite = IS_UDPLITE(sk);
Wei Yongjunf26ba172008-11-02 16:11:01 +0000402 int is_udp4;
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000403 bool slow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (flags & MSG_ERRQUEUE)
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100406 return ipv6_recv_error(sk, msg, len, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Brian Haley4b340ae2010-04-23 11:26:09 +0000408 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100409 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
Brian Haley4b340ae2010-04-23 11:26:09 +0000410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800412 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000413 &peeked, &off, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!skb)
415 goto out;
416
Herbert Xu759e5d02007-03-25 20:10:56 -0700417 ulen = skb->len - sizeof(struct udphdr);
David S. Miller59c2cda2011-12-01 14:12:55 -0500418 copied = len;
419 if (copied > ulen)
420 copied = ulen;
421 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900422 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Wei Yongjunf26ba172008-11-02 16:11:01 +0000424 is_udp4 = (skb->protocol == htons(ETH_P_IP));
425
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800426 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700427 * If checksum is needed at all, try to do it while copying the
428 * data. If the data is truncated, or if we only want a partial
429 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800430 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800431
David S. Miller59c2cda2011-12-01 14:12:55 -0500432 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700433 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800435 }
436
Herbert Xu60476372007-04-09 11:59:39 -0700437 if (skb_csum_unnecessary(skb))
David S. Miller51f3d022014-11-05 16:46:40 -0500438 err = skb_copy_datagram_msg(skb, sizeof(struct udphdr),
439 msg, copied);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800440 else {
Al Viro227158d2014-04-06 18:47:38 -0400441 err = skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr), msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (err == -EINVAL)
443 goto csum_copy_err;
444 }
Eric Dumazet22911fc2012-06-27 00:23:44 +0000445 if (unlikely(err)) {
446 trace_kfree_skb(skb, udpv6_recvmsg);
Eric Dumazet979402b2012-09-05 23:34:44 +0000447 if (!peeked) {
448 atomic_inc(&sk->sk_drops);
449 if (is_udp4)
450 UDP_INC_STATS_USER(sock_net(sk),
451 UDP_MIB_INERRORS,
452 is_udplite);
453 else
454 UDP6_INC_STATS_USER(sock_net(sk),
455 UDP_MIB_INERRORS,
456 is_udplite);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto out_free;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000459 }
Wei Yongjunf26ba172008-11-02 16:11:01 +0000460 if (!peeked) {
461 if (is_udp4)
462 UDP_INC_STATS_USER(sock_net(sk),
463 UDP_MIB_INDATAGRAMS, is_udplite);
464 else
465 UDP6_INC_STATS_USER(sock_net(sk),
466 UDP_MIB_INDATAGRAMS, is_udplite);
467 }
Wang Chencb759942007-12-03 22:33:28 +1100468
Neil Horman3b885782009-10-12 13:26:31 -0700469 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Copy the address. */
472 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100473 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300475 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sin6->sin6_flowinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000478 if (is_udp4) {
Brian Haleyb301e822009-10-07 13:58:25 -0700479 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
480 &sin6->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000481 sin6->sin6_scope_id = 0;
482 } else {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000483 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000484 sin6->sin6_scope_id =
485 ipv6_iface_scope_id(&sin6->sin6_addr,
Duan Jiong43304872014-08-01 09:52:58 +0800486 inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Hannes Frederic Sowabceaa902013-11-18 04:20:45 +0100488 *addr_len = sizeof(*sin6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100490
491 if (np->rxopt.all)
492 ip6_datagram_recv_common_ctl(sk, msg, skb);
493
Wei Yongjunf26ba172008-11-02 16:11:01 +0000494 if (is_udp4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (inet->cmsg_flags)
496 ip_cmsg_recv(msg, skb);
497 } else {
498 if (np->rxopt.all)
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +0100499 ip6_datagram_recv_specific_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David S. Miller59c2cda2011-12-01 14:12:55 -0500502 err = copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700504 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506out_free:
Eric Dumazet9d410c72009-10-30 05:03:53 +0000507 skb_free_datagram_locked(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508out:
509 return err;
510
511csum_copy_err:
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000512 slow = lock_sock_fast(sk);
Wei Yongjun0856f932008-11-02 16:14:27 +0000513 if (!skb_kill_datagram(sk, skb, flags)) {
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000514 if (is_udp4) {
515 UDP_INC_STATS_USER(sock_net(sk),
516 UDP_MIB_CSUMERRORS, is_udplite);
Wei Yongjun0856f932008-11-02 16:14:27 +0000517 UDP_INC_STATS_USER(sock_net(sk),
518 UDP_MIB_INERRORS, is_udplite);
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000519 } else {
520 UDP6_INC_STATS_USER(sock_net(sk),
521 UDP_MIB_CSUMERRORS, is_udplite);
Wei Yongjun0856f932008-11-02 16:14:27 +0000522 UDP6_INC_STATS_USER(sock_net(sk),
523 UDP_MIB_INERRORS, is_udplite);
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000524 }
Wei Yongjun0856f932008-11-02 16:14:27 +0000525 }
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000526 unlock_sock_fast(sk, slow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Eric Dumazetbeb39db2015-05-30 09:16:53 -0700528 /* starting over for a new packet, but check if we need to yield */
529 cond_resched();
Xufeng Zhang9cfaa8d2011-06-21 10:43:40 +0000530 msg->msg_flags &= ~MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto try_again;
532}
533
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800534void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700535 u8 type, u8 code, int offset, __be32 info,
Eric Dumazet645ca702008-10-29 01:41:45 -0700536 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct ipv6_pinfo *np;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000539 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
540 const struct in6_addr *saddr = &hdr->saddr;
541 const struct in6_addr *daddr = &hdr->daddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100542 struct udphdr *uh = (struct udphdr *)(skb->data+offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct sock *sk;
544 int err;
Duan Jiong7304fe42014-07-31 17:54:32 +0800545 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Duan Jiong7304fe42014-07-31 17:54:32 +0800547 sk = __udp6_lib_lookup(net, daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800548 saddr, uh->source, inet6_iif(skb), udptable);
Ian Morris63159f22015-03-29 14:00:04 +0100549 if (!sk) {
Duan Jiong7304fe42014-07-31 17:54:32 +0800550 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
551 ICMP6_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return;
Duan Jiong7304fe42014-07-31 17:54:32 +0800553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100555 if (type == ICMPV6_PKT_TOOBIG) {
556 if (!ip6_sk_accept_pmtu(sk))
557 goto out;
David S. Miller81aded22012-06-15 14:54:11 -0700558 ip6_sk_update_pmtu(skb, sk, info);
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100559 }
Duan Jiong1a462d12013-09-20 18:20:28 +0800560 if (type == NDISC_REDIRECT) {
David S. Millerec18d9a2012-07-12 00:25:15 -0700561 ip6_sk_redirect(skb, sk);
Duan Jiong1a462d12013-09-20 18:20:28 +0800562 goto out;
563 }
David S. Miller81aded22012-06-15 14:54:11 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 np = inet6_sk(sk);
566
567 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
568 goto out;
569
570 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
571 goto out;
572
Eric Dumazetb1faf562010-05-31 23:44:05 -0700573 if (np->recverr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
Eric Dumazetb1faf562010-05-31 23:44:05 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 sk->sk_err = err;
577 sk->sk_error_report(sk);
578out:
579 sock_put(sk);
580}
581
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000582static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
583{
584 int rc;
585
Eric Dumazetefe42082013-10-03 15:42:29 -0700586 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000587 sock_rps_save_rxhash(sk, skb);
Shawn Bohrer005ec972013-10-07 11:01:38 -0500588 sk_mark_napi_id(sk, skb);
Eric Dumazet2c8c56e2014-11-11 05:54:28 -0800589 sk_incoming_cpu_update(sk);
Shawn Bohrer005ec972013-10-07 11:01:38 -0500590 }
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000591
592 rc = sock_queue_rcv_skb(sk, skb);
593 if (rc < 0) {
594 int is_udplite = IS_UDPLITE(sk);
595
596 /* Note that an ENOMEM error is charged twice */
597 if (rc == -ENOMEM)
598 UDP6_INC_STATS_BH(sock_net(sk),
599 UDP_MIB_RCVBUFERRORS, is_udplite);
600 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
601 kfree_skb(skb);
602 return -1;
603 }
604 return 0;
605}
606
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800607static __inline__ void udpv6_err(struct sk_buff *skb,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700608 struct inet6_skb_parm *opt, u8 type,
Ian Morris67ba4152014-08-24 21:53:10 +0100609 u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Eric Dumazet645ca702008-10-29 01:41:45 -0700611 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800612}
613
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000614static struct static_key udpv6_encap_needed __read_mostly;
615void udpv6_encap_enable(void)
616{
617 if (!static_key_enabled(&udpv6_encap_needed))
618 static_key_slow_inc(&udpv6_encap_needed);
619}
620EXPORT_SYMBOL(udpv6_encap_enable);
621
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000622int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800623{
624 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700625 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100626 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700627
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800628 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
629 goto drop;
630
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000631 if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
632 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
633
634 /*
635 * This is an encapsulation socket so pass the skb to
636 * the socket's udp_encap_rcv() hook. Otherwise, just
637 * fall through and pass this up the UDP socket.
638 * up->encap_rcv() returns the following value:
639 * =0 if skb was successfully passed to the encap
640 * handler or was discarded by it.
641 * >0 if skb should be passed on to UDP.
642 * <0 if skb should be resubmitted as proto -N
643 */
644
645 /* if we're overly short, let UDP handle it */
646 encap_rcv = ACCESS_ONCE(up->encap_rcv);
Ian Morris53b24b82015-03-29 14:00:05 +0100647 if (skb->len > sizeof(struct udphdr) && encap_rcv) {
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000648 int ret;
649
Tom Herbert0a809662014-05-07 16:52:39 -0700650 /* Verify checksum before giving to encap */
651 if (udp_lib_checksum_complete(skb))
652 goto csum_error;
653
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000654 ret = encap_rcv(sk, skb);
655 if (ret <= 0) {
656 UDP_INC_STATS_BH(sock_net(sk),
657 UDP_MIB_INDATAGRAMS,
658 is_udplite);
659 return -ret;
660 }
661 }
662
663 /* FALLTHROUGH -- it's a UDP Packet */
664 }
665
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800666 /*
667 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
668 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100669 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800670
671 if (up->pcrlen == 0) { /* full coverage was set */
Joe Perchesba7a46f2014-11-11 10:59:17 -0800672 net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
673 UDP_SKB_CB(skb)->cscov, skb->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800674 goto drop;
675 }
676 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800677 net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
678 UDP_SKB_CB(skb)->cscov, up->pcrlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800679 goto drop;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
Eric Dumazet33d480c2011-08-11 19:30:52 +0000683 if (rcu_access_pointer(sk->sk_filter)) {
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800684 if (udp_lib_checksum_complete(skb))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000685 goto csum_error;
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Sorin Dumitru274f4822014-07-22 21:16:51 +0300688 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
James M Leddy3e215c82014-06-25 17:38:13 -0400689 UDP6_INC_STATS_BH(sock_net(sk),
690 UDP_MIB_RCVBUFERRORS, is_udplite);
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000691 goto drop;
James M Leddy3e215c82014-06-25 17:38:13 -0400692 }
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000693
Eric Dumazetd826eb12011-11-09 07:24:35 +0000694 skb_dst_drop(skb);
Wang Chencb759942007-12-03 22:33:28 +1100695
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000696 bh_lock_sock(sk);
697 rc = 0;
698 if (!sock_owned_by_user(sk))
699 rc = __udpv6_queue_rcv_skb(sk, skb);
700 else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
701 bh_unlock_sock(sk);
702 goto drop;
703 }
704 bh_unlock_sock(sk);
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000705
706 return rc;
James M Leddy3e215c82014-06-25 17:38:13 -0400707
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000708csum_error:
709 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800710drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700711 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000712 atomic_inc(&sk->sk_drops);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800713 kfree_skb(skb);
714 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
David Held5cf3d462014-07-15 23:28:31 -0400717static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
718 __be16 loc_port, const struct in6_addr *loc_addr,
719 __be16 rmt_port, const struct in6_addr *rmt_addr,
720 int dif, unsigned short hnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
David Held5cf3d462014-07-15 23:28:31 -0400722 struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David Held5cf3d462014-07-15 23:28:31 -0400724 if (!net_eq(sock_net(sk), net))
725 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
David Held5cf3d462014-07-15 23:28:31 -0400727 if (udp_sk(sk)->udp_port_hash != hnum ||
728 sk->sk_family != PF_INET6 ||
729 (inet->inet_dport && inet->inet_dport != rmt_port) ||
730 (!ipv6_addr_any(&sk->sk_v6_daddr) &&
731 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
Henning Rogge33b4b012015-05-18 21:08:49 +0200732 (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
733 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
734 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
David Held5cf3d462014-07-15 23:28:31 -0400735 return false;
736 if (!inet6_mc_check(sk, loc_addr, rmt_addr))
737 return false;
738 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000741static void flush_stack(struct sock **stack, unsigned int count,
742 struct sk_buff *skb, unsigned int final)
743{
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000744 struct sk_buff *skb1 = NULL;
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000745 struct sock *sk;
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000746 unsigned int i;
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000747
748 for (i = 0; i < count; i++) {
Eric Dumazetf6b8f322009-11-08 10:20:19 +0000749 sk = stack[i];
Ian Morris63159f22015-03-29 14:00:04 +0100750 if (likely(!skb1))
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000751 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
752 if (!skb1) {
753 atomic_inc(&sk->sk_drops);
754 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
755 IS_UDPLITE(sk));
756 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
757 IS_UDPLITE(sk));
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000758 }
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000759
760 if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
761 skb1 = NULL;
David Held2dc41cf2014-07-15 23:28:32 -0400762 sock_put(sk);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000763 }
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000764 if (unlikely(skb1))
765 kfree_skb(skb1);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000766}
Tom Herbert40685792014-05-02 16:29:58 -0700767
768static void udp6_csum_zero_error(struct sk_buff *skb)
769{
770 /* RFC 2460 section 8.1 says that we SHOULD log
771 * this error. Well, it is reasonable.
772 */
Joe Perchesba7a46f2014-11-11 10:59:17 -0800773 net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
774 &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
775 &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
Tom Herbert40685792014-05-02 16:29:58 -0700776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * Note: called only from the BH handler context,
780 * so we don't need to lock the hashes.
781 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700782static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000783 const struct in6_addr *saddr, const struct in6_addr *daddr,
Rick Jones36cbb242014-11-06 10:37:54 -0800784 struct udp_table *udptable, int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000786 struct sock *sk, *stack[256 / sizeof(struct sock *)];
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300787 const struct udphdr *uh = udp_hdr(skb);
David Held5cf3d462014-07-15 23:28:31 -0400788 struct hlist_nulls_node *node;
789 unsigned short hnum = ntohs(uh->dest);
790 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
791 int dif = inet6_iif(skb);
David Held2dc41cf2014-07-15 23:28:32 -0400792 unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
793 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
Rick Jones36cbb242014-11-06 10:37:54 -0800794 bool inner_flushed = false;
David Held2dc41cf2014-07-15 23:28:32 -0400795
796 if (use_hash2) {
797 hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
798 udp_table.mask;
799 hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
800start_lookup:
801 hslot = &udp_table.hash2[hash2];
802 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Eric Dumazet645ca702008-10-29 01:41:45 -0700805 spin_lock(&hslot->lock);
David Held2dc41cf2014-07-15 23:28:32 -0400806 sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
David Held5cf3d462014-07-15 23:28:31 -0400807 if (__udp_v6_is_mcast_sock(net, sk,
808 uh->dest, daddr,
809 uh->source, saddr,
810 dif, hnum) &&
811 /* If zero checksum and no_check is not on for
812 * the socket then skip it.
813 */
814 (uh->check || udp_sk(sk)->no_check6_rx)) {
815 if (unlikely(count == ARRAY_SIZE(stack))) {
816 flush_stack(stack, count, skb, ~0);
Rick Jones36cbb242014-11-06 10:37:54 -0800817 inner_flushed = true;
David Held5cf3d462014-07-15 23:28:31 -0400818 count = 0;
819 }
Tom Herbert40685792014-05-02 16:29:58 -0700820 stack[count++] = sk;
David Held2dc41cf2014-07-15 23:28:32 -0400821 sock_hold(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000824
Eric Dumazet645ca702008-10-29 01:41:45 -0700825 spin_unlock(&hslot->lock);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000826
David Held2dc41cf2014-07-15 23:28:32 -0400827 /* Also lookup *:port if we are using hash2 and haven't done so yet. */
828 if (use_hash2 && hash2 != hash2_any) {
829 hash2 = hash2_any;
830 goto start_lookup;
831 }
832
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000833 if (count) {
834 flush_stack(stack, count, skb, count - 1);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000835 } else {
Rick Jones36cbb242014-11-06 10:37:54 -0800836 if (!inner_flushed)
837 UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI,
838 proto == IPPROTO_UDPLITE);
839 consume_skb(skb);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000840 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Eric Dumazet645ca702008-10-29 01:41:45 -0700844int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
Herbert Xu759e5d02007-03-25 20:10:56 -0700845 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000847 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900849 struct udphdr *uh;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000850 const struct in6_addr *saddr, *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 u32 ulen = 0;
852
853 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
Bjørn Morkd6bc0142010-05-06 03:44:35 +0000854 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700856 saddr = &ipv6_hdr(skb)->saddr;
857 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300858 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800861 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto short_packet;
863
Herbert Xu759e5d02007-03-25 20:10:56 -0700864 if (proto == IPPROTO_UDP) {
865 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800867 /* Check for jumbo payload */
868 if (ulen == 0)
869 ulen = skb->len;
870
871 if (ulen < sizeof(*uh))
872 goto short_packet;
873
874 if (ulen < skb->len) {
875 if (pskb_trim_rcsum(skb, ulen))
876 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700877 saddr = &ipv6_hdr(skb)->saddr;
878 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300879 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
Herbert Xu759e5d02007-03-25 20:10:56 -0700883 if (udp6_csum_init(skb, uh, proto))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000884 goto csum_error;
Herbert Xu759e5d02007-03-25 20:10:56 -0700885
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900886 /*
887 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800889 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700890 return __udp6_lib_mcast_deliver(net, skb,
Rick Jones36cbb242014-11-06 10:37:54 -0800891 saddr, daddr, udptable, proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* Unicast */
894
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900895 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * check socket cache ... must talk to Alan about his plans
897 * for sock caches... i'll skip this for now.
898 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700899 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Ian Morris53b24b82015-03-29 14:00:05 +0100900 if (sk) {
Eliezer Tamira5b50472013-06-10 11:40:00 +0300901 int ret;
902
Tom Herbert1c194482014-05-23 08:47:32 -0700903 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
Tom Herbert79e0f1c2014-05-05 16:43:58 -0700904 sock_put(sk);
Tom Herbert40685792014-05-02 16:29:58 -0700905 udp6_csum_zero_error(skb);
906 goto csum_error;
907 }
908
Tom Herbert224d0192015-01-05 13:56:14 -0800909 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
Tom Herbert2abb7cd2014-08-31 15:12:43 -0700910 skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
911 ip6_compute_pseudo);
912
Eliezer Tamira5b50472013-06-10 11:40:00 +0300913 ret = udpv6_queue_rcv_skb(sk, skb);
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000914 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000916 /* a return value > 0 means to resubmit the input, but
917 * it wants the return to be -protocol, or 0
918 */
919 if (ret > 0)
920 return -ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900924
Tom Herbert40685792014-05-02 16:29:58 -0700925 if (!uh->check) {
926 udp6_csum_zero_error(skb);
927 goto csum_error;
928 }
929
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000930 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
931 goto discard;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900932
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000933 if (udp_lib_checksum_complete(skb))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000934 goto csum_error;
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000935
936 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
937 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
938
939 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900942short_packet:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800943 net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
944 proto == IPPROTO_UDPLITE ? "-Lite" : "",
945 saddr, ntohs(uh->source),
946 ulen, skb->len,
947 daddr, ntohs(uh->dest));
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000948 goto discard;
949csum_error:
950 UDP6_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700952 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800956
Herbert Xue5bbef22007-10-15 12:50:28 -0700957static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800958{
Eric Dumazet645ca702008-10-29 01:41:45 -0700959 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
963 * Throw away all pending data and cancel the corking. Socket is locked.
964 */
965static void udp_v6_flush_pending_frames(struct sock *sk)
966{
967 struct udp_sock *up = udp_sk(sk);
968
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400969 if (up->pending == AF_INET)
970 udp_flush_pending_frames(sk);
971 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 up->len = 0;
973 up->pending = 0;
974 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000978/**
Ian Morris67ba4152014-08-24 21:53:10 +0100979 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
980 * @sk: socket we are sending on
981 * @skb: sk_buff containing the filled-in UDP header
982 * (checksum field must be zeroed out)
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000983 */
984static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
985 const struct in6_addr *saddr,
986 const struct in6_addr *daddr, int len)
987{
988 unsigned int offset;
989 struct udphdr *uh = udp_hdr(skb);
Vlad Yasevichd39d9382015-01-31 10:40:16 -0500990 struct sk_buff *frags = skb_shinfo(skb)->frag_list;
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000991 __wsum csum = 0;
992
Vlad Yasevichd39d9382015-01-31 10:40:16 -0500993 if (!frags) {
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000994 /* Only one fragment on the socket. */
995 skb->csum_start = skb_transport_header(skb) - skb->head;
996 skb->csum_offset = offsetof(struct udphdr, check);
997 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
998 } else {
999 /*
1000 * HW-checksum won't work as there are two or more
1001 * fragments on the socket so that all csums of sk_buffs
1002 * should be together
1003 */
1004 offset = skb_transport_offset(skb);
1005 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1006
1007 skb->ip_summed = CHECKSUM_NONE;
1008
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001009 do {
1010 csum = csum_add(csum, frags->csum);
1011 } while ((frags = frags->next));
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001012
1013 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1014 csum);
1015 if (uh->check == 0)
1016 uh->check = CSUM_MANGLED_0;
1017 }
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020/*
1021 * Sending
1022 */
1023
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001024static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001026 struct sock *sk = skb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +11001029 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -08001030 __wsum csum = 0;
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001031 int offset = skb_transport_offset(skb);
1032 int len = skb->len - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /*
1035 * Create a UDP header
1036 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001037 uh = udp_hdr(skb);
David S. Miller1958b852011-03-12 16:36:19 -05001038 uh->source = fl6->fl6_sport;
1039 uh->dest = fl6->fl6_dport;
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001040 uh->len = htons(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 uh->check = 0;
1042
Wang Chenb2bf1e22007-12-03 22:34:16 +11001043 if (is_udplite)
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001044 csum = udplite_csum(skb);
1045 else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */
Tom Herbert40685792014-05-02 16:29:58 -07001046 skb->ip_summed = CHECKSUM_NONE;
1047 goto send;
1048 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001049 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001050 goto send;
1051 } else
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001052 csum = udp_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001054 /* add protocol-dependent pseudo-header */
David S. Miller4c9483b2011-03-12 16:22:43 -05001055 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001056 len, fl6->flowi6_proto, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -08001058 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001060send:
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001061 err = ip6_send_skb(skb);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001062 if (err) {
1063 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
1064 UDP6_INC_STATS_USER(sock_net(sk),
1065 UDP_MIB_SNDBUFERRORS, is_udplite);
1066 err = 0;
1067 }
1068 } else
1069 UDP6_INC_STATS_USER(sock_net(sk),
1070 UDP_MIB_OUTDATAGRAMS, is_udplite);
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001071 return err;
1072}
1073
1074static int udp_v6_push_pending_frames(struct sock *sk)
1075{
1076 struct sk_buff *skb;
1077 struct udp_sock *up = udp_sk(sk);
1078 struct flowi6 fl6;
1079 int err = 0;
1080
1081 if (up->pending == AF_INET)
1082 return udp_push_pending_frames(sk);
1083
1084 /* ip6_finish_skb will release the cork, so make a copy of
1085 * fl6 here.
1086 */
1087 fl6 = inet_sk(sk)->cork.fl.u.ip6;
1088
1089 skb = ip6_finish_skb(sk);
1090 if (!skb)
1091 goto out;
1092
1093 err = udp_v6_send_skb(skb, &fl6);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095out:
1096 up->len = 0;
1097 up->pending = 0;
1098 return err;
1099}
1100
Ying Xue1b784142015-03-02 15:37:48 +08001101int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct ipv6_txoptions opt_space;
1104 struct udp_sock *up = udp_sk(sk);
1105 struct inet_sock *inet = inet_sk(sk);
1106 struct ipv6_pinfo *np = inet6_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001107 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001108 struct in6_addr *daddr, *final_p, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct ipv6_txoptions *opt = NULL;
1110 struct ip6_flowlabel *flowlabel = NULL;
David S. Miller4c9483b2011-03-12 16:22:43 -05001111 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct dst_entry *dst;
1113 int addr_len = msg->msg_namelen;
1114 int ulen = len;
1115 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001116 int tclass = -1;
Brian Haley13b52cd2010-04-23 11:26:08 +00001117 int dontfrag = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1119 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -07001120 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +11001121 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001122 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* destination address check */
1125 if (sin6) {
1126 if (addr_len < offsetof(struct sockaddr, sa_data))
1127 return -EINVAL;
1128
1129 switch (sin6->sin6_family) {
1130 case AF_INET6:
1131 if (addr_len < SIN6_LEN_RFC2133)
1132 return -EINVAL;
1133 daddr = &sin6->sin6_addr;
1134 break;
1135 case AF_INET:
1136 goto do_udp_sendmsg;
1137 case AF_UNSPEC:
1138 msg->msg_name = sin6 = NULL;
1139 msg->msg_namelen = addr_len = 0;
1140 daddr = NULL;
1141 break;
1142 default:
1143 return -EINVAL;
1144 }
1145 } else if (!up->pending) {
1146 if (sk->sk_state != TCP_ESTABLISHED)
1147 return -EDESTADDRREQ;
Eric Dumazetefe42082013-10-03 15:42:29 -07001148 daddr = &sk->sk_v6_daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001149 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 daddr = NULL;
1151
1152 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -07001153 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct sockaddr_in sin;
1155 sin.sin_family = AF_INET;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001156 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 sin.sin_addr.s_addr = daddr->s6_addr32[3];
1158 msg->msg_name = &sin;
1159 msg->msg_namelen = sizeof(sin);
1160do_udp_sendmsg:
1161 if (__ipv6_only_sock(sk))
1162 return -ENETUNREACH;
Ying Xue1b784142015-03-02 15:37:48 +08001163 return udp_sendmsg(sk, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 }
1166
1167 if (up->pending == AF_INET)
Ying Xue1b784142015-03-02 15:37:48 +08001168 return udp_sendmsg(sk, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -07001171 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
1173 if (len > INT_MAX - sizeof(struct udphdr))
1174 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001175
Vlad Yasevich03485f22015-01-31 10:40:17 -05001176 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (up->pending) {
1178 /*
1179 * There are pending frames.
1180 * The socket lock must be held while it's corked.
1181 */
1182 lock_sock(sk);
1183 if (likely(up->pending)) {
1184 if (unlikely(up->pending != AF_INET6)) {
1185 release_sock(sk);
1186 return -EAFNOSUPPORT;
1187 }
1188 dst = NULL;
1189 goto do_append_data;
1190 }
1191 release_sock(sk);
1192 }
1193 ulen += sizeof(struct udphdr);
1194
David S. Miller4c9483b2011-03-12 16:22:43 -05001195 memset(&fl6, 0, sizeof(fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (sin6) {
1198 if (sin6->sin6_port == 0)
1199 return -EINVAL;
1200
David S. Miller1958b852011-03-12 16:36:19 -05001201 fl6.fl6_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 daddr = &sin6->sin6_addr;
1203
1204 if (np->sndflow) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001205 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1206 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1207 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Ian Morris63159f22015-03-29 14:00:04 +01001208 if (!flowlabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 }
1212
1213 /*
1214 * Otherwise it will be difficult to maintain
1215 * sk->sk_dst_cache.
1216 */
1217 if (sk->sk_state == TCP_ESTABLISHED &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001218 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1219 daddr = &sk->sk_v6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 if (addr_len >= sizeof(struct sockaddr_in6) &&
1222 sin6->sin6_scope_id &&
Hannes Frederic Sowa842df072013-03-08 02:07:19 +00001223 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
David S. Miller4c9483b2011-03-12 16:22:43 -05001224 fl6.flowi6_oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 } else {
1226 if (sk->sk_state != TCP_ESTABLISHED)
1227 return -EDESTADDRREQ;
1228
David S. Miller1958b852011-03-12 16:36:19 -05001229 fl6.fl6_dport = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -07001230 daddr = &sk->sk_v6_daddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001231 fl6.flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -07001232 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
David S. Miller4c9483b2011-03-12 16:22:43 -05001235 if (!fl6.flowi6_oif)
1236 fl6.flowi6_oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
David S. Miller4c9483b2011-03-12 16:22:43 -05001238 if (!fl6.flowi6_oif)
1239 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
Yang Hongyang9f690db2008-12-16 02:08:29 -08001240
David S. Miller4c9483b2011-03-12 16:22:43 -05001241 fl6.flowi6_mark = sk->sk_mark;
Brian Haley51953d52009-10-05 08:24:16 +00001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (msg->msg_controllen) {
1244 opt = &opt_space;
1245 memset(opt, 0, sizeof(struct ipv6_txoptions));
1246 opt->tot_len = sizeof(*opt);
1247
Tom Parkin73df66f2013-01-31 01:02:24 +00001248 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1249 &hlimit, &tclass, &dontfrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (err < 0) {
1251 fl6_sock_release(flowlabel);
1252 return err;
1253 }
David S. Miller4c9483b2011-03-12 16:22:43 -05001254 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1255 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Ian Morris63159f22015-03-29 14:00:04 +01001256 if (!flowlabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -EINVAL;
1258 }
1259 if (!(opt->opt_nflen|opt->opt_flen))
1260 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -07001261 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
Ian Morris63159f22015-03-29 14:00:04 +01001263 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +09001265 if (flowlabel)
1266 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1267 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
David S. Miller4c9483b2011-03-12 16:22:43 -05001269 fl6.flowi6_proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -04001270 if (!ipv6_addr_any(daddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001271 fl6.daddr = *daddr;
Brian Haley876c7f42008-04-11 00:38:24 -04001272 else
David S. Miller4c9483b2011-03-12 16:22:43 -05001273 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1274 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001275 fl6.saddr = np->saddr;
David S. Miller1958b852011-03-12 16:36:19 -05001276 fl6.fl6_sport = inet->inet_sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001277
David S. Miller4c9483b2011-03-12 16:22:43 -05001278 final_p = fl6_update_dst(&fl6, opt, &final);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001279 if (final_p)
Mitsuru KANDA987905d2005-09-18 00:30:08 -07001280 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
David S. Miller4c9483b2011-03-12 16:22:43 -05001282 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1283 fl6.flowi6_oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -07001284 connected = 0;
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001285 } else if (!fl6.flowi6_oif)
1286 fl6.flowi6_oif = np->ucast_oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
David S. Miller4c9483b2011-03-12 16:22:43 -05001288 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001289
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001290 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001291 if (IS_ERR(dst)) {
1292 err = PTR_ERR(dst);
1293 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Lorenzo Colitti5c986312014-04-29 11:57:34 +09001297 if (hlimit < 0)
1298 hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Gerrit Renkere651f032009-08-09 08:12:48 +00001300 if (tclass < 0)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001301 tclass = np->tclass;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (msg->msg_flags&MSG_CONFIRM)
1304 goto do_confirm;
1305back_from_confirm:
1306
Vlad Yasevich03485f22015-01-31 10:40:17 -05001307 /* Lockless fast path for the non-corking case */
1308 if (!corkreq) {
1309 struct sk_buff *skb;
1310
1311 skb = ip6_make_skb(sk, getfrag, msg, ulen,
1312 sizeof(struct udphdr), hlimit, tclass, opt,
1313 &fl6, (struct rt6_info *)dst,
1314 msg->msg_flags, dontfrag);
1315 err = PTR_ERR(skb);
1316 if (!IS_ERR_OR_NULL(skb))
1317 err = udp_v6_send_skb(skb, &fl6);
1318 goto release_dst;
1319 }
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 lock_sock(sk);
1322 if (unlikely(up->pending)) {
1323 /* The socket is already corked while preparing it. */
1324 /* ... which is an evident application bug. --ANK */
1325 release_sock(sk);
1326
Joe Perchesba7a46f2014-11-11 10:59:17 -08001327 net_dbg_ratelimited("udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 err = -EINVAL;
1329 goto out;
1330 }
1331
1332 up->pending = AF_INET6;
1333
1334do_append_data:
Jiri Pirkoe36d3ff2013-10-19 12:29:15 +02001335 if (dontfrag < 0)
1336 dontfrag = np->dontfrag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 up->len += ulen;
Al Virof69e6d12014-11-24 13:23:40 -05001338 err = ip6_append_data(sk, getfrag, msg, ulen,
David S. Miller4c9483b2011-03-12 16:22:43 -05001339 sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
Ian Morris67ba4152014-08-24 21:53:10 +01001340 (struct rt6_info *)dst,
Brian Haley13b52cd2010-04-23 11:26:08 +00001341 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (err)
1343 udp_v6_flush_pending_frames(sk);
1344 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001345 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -07001346 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1347 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Vlad Yasevich03485f22015-01-31 10:40:17 -05001349 if (err > 0)
1350 err = np->recverr ? net_xmit_errno(err) : 0;
1351 release_sock(sk);
1352
1353release_dst:
David S. Millera5e7c212005-10-03 14:21:58 -07001354 if (dst) {
1355 if (connected) {
1356 ip6_dst_store(sk, dst,
Eric Dumazetefe42082013-10-03 15:42:29 -07001357 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1358 &sk->sk_v6_daddr : NULL,
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001359#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -05001360 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001361 &np->saddr :
1362#endif
1363 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -07001364 } else {
1365 dst_release(dst);
1366 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001367 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -07001368 }
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001371 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -07001373 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return len;
David S. Millera18135e2006-08-15 00:00:09 -07001375 /*
1376 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1377 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1378 * we don't have a good statistic (IpOutDiscards but it can be too many
1379 * things). We could add another new stat but at least for now that
1380 * seems like overkill.
1381 */
1382 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -07001383 UDP6_INC_STATS_USER(sock_net(sk),
1384 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -07001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return err;
1387
1388do_confirm:
1389 dst_confirm(dst);
1390 if (!(msg->msg_flags&MSG_PROBE) || len)
1391 goto back_from_confirm;
1392 err = 0;
1393 goto out;
1394}
1395
Brian Haley7d06b2e2008-06-14 17:04:49 -07001396void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Tom Parkin44046a52013-03-19 06:11:12 +00001398 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 lock_sock(sk);
1400 udp_v6_flush_pending_frames(sk);
1401 release_sock(sk);
1402
Tom Parkin44046a52013-03-19 06:11:12 +00001403 if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
1404 void (*encap_destroy)(struct sock *sk);
1405 encap_destroy = ACCESS_ONCE(up->encap_destroy);
1406 if (encap_destroy)
1407 encap_destroy(sk);
1408 }
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/*
1414 * Socket option code for UDP
1415 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001416int udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001417 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001418{
David S. Millerdb8dac22008-03-06 16:22:02 -08001419 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001420 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1421 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001422 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001423}
1424
1425#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001426int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001427 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001428{
David S. Millerdb8dac22008-03-06 16:22:02 -08001429 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001430 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1431 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001432 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001433}
1434#endif
1435
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001436int udpv6_getsockopt(struct sock *sk, int level, int optname,
1437 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001438{
David S. Millerdb8dac22008-03-06 16:22:02 -08001439 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001440 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001441 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001442}
1443
1444#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001445int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1446 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001447{
David S. Millerdb8dac22008-03-06 16:22:02 -08001448 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001449 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001450 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001451}
1452#endif
1453
Alexey Dobriyan41135cc2009-09-14 12:22:28 +00001454static const struct inet6_protocol udpv6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 .handler = udpv6_rcv,
1456 .err_handler = udpv6_err,
1457 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1458};
1459
1460/* ------------------------------------------------------------------------ */
1461#ifdef CONFIG_PROC_FS
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001462int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001464 if (v == SEQ_START_TOKEN) {
1465 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1466 } else {
1467 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1468 struct inet_sock *inet = inet_sk(v);
1469 __u16 srcp = ntohs(inet->inet_sport);
1470 __u16 destp = ntohs(inet->inet_dport);
1471 ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
1474}
1475
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001476static const struct file_operations udp6_afinfo_seq_fops = {
1477 .owner = THIS_MODULE,
1478 .open = udp_seq_open,
1479 .read = seq_read,
1480 .llseek = seq_lseek,
1481 .release = seq_release_net
1482};
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 .name = "udp6",
1486 .family = AF_INET6,
Eric Dumazet645ca702008-10-29 01:41:45 -07001487 .udp_table = &udp_table,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001488 .seq_fops = &udp6_afinfo_seq_fops,
Denis V. Lunevdda61922008-03-28 18:24:26 -07001489 .seq_ops = {
1490 .show = udp6_seq_show,
1491 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492};
1493
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001494int __net_init udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001496 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Ian Morrisec120da2015-08-14 22:43:38 +01001499void udp6_proc_exit(struct net *net)
1500{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001501 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503#endif /* CONFIG_PROC_FS */
1504
Eric Dumazetf77d6022013-05-09 10:28:16 +00001505void udp_v6_clear_sk(struct sock *sk, int size)
1506{
1507 struct inet_sock *inet = inet_sk(sk);
1508
1509 /* we do not want to clear pinet6 field, because of RCU lookups */
1510 sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1511
1512 size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1513 memset(&inet->pinet6 + 1, 0, size);
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/* ------------------------------------------------------------------------ */
1517
1518struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001519 .name = "UDPv6",
1520 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001521 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001522 .connect = ip6_datagram_connect,
1523 .disconnect = udp_disconnect,
1524 .ioctl = udp_ioctl,
1525 .destroy = udpv6_destroy_sock,
1526 .setsockopt = udpv6_setsockopt,
1527 .getsockopt = udpv6_getsockopt,
1528 .sendmsg = udpv6_sendmsg,
1529 .recvmsg = udpv6_recvmsg,
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +00001530 .backlog_rcv = __udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001531 .hash = udp_lib_hash,
1532 .unhash = udp_lib_unhash,
Eric Dumazet719f8352010-09-08 05:08:44 +00001533 .rehash = udp_v6_rehash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001534 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001535 .memory_allocated = &udp_memory_allocated,
1536 .sysctl_mem = sysctl_udp_mem,
1537 .sysctl_wmem = &sysctl_udp_wmem_min,
1538 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001539 .obj_size = sizeof(struct udp6_sock),
Eric Dumazet271b72c2008-10-29 02:11:14 -07001540 .slab_flags = SLAB_DESTROY_BY_RCU,
Eric Dumazet645ca702008-10-29 01:41:45 -07001541 .h.udp_table = &udp_table,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001542#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001543 .compat_setsockopt = compat_udpv6_setsockopt,
1544 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001545#endif
Eric Dumazetf77d6022013-05-09 10:28:16 +00001546 .clear_sk = udp_v6_clear_sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547};
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549static struct inet_protosw udpv6_protosw = {
1550 .type = SOCK_DGRAM,
1551 .protocol = IPPROTO_UDP,
1552 .prot = &udpv6_prot,
1553 .ops = &inet6_dgram_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .flags = INET_PROTOSW_PERMANENT,
1555};
1556
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001557int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001559 int ret;
1560
Vlad Yasevich33362882012-11-15 08:49:15 +00001561 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1562 if (ret)
Vlad Yasevichc6b641a2012-11-15 08:49:22 +00001563 goto out;
Vlad Yasevich33362882012-11-15 08:49:15 +00001564
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001565 ret = inet6_register_protosw(&udpv6_protosw);
1566 if (ret)
1567 goto out_udpv6_protocol;
1568out:
1569 return ret;
1570
1571out_udpv6_protocol:
1572 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1573 goto out;
1574}
1575
Daniel Lezcano09f77092007-12-13 05:34:58 -08001576void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001577{
1578 inet6_unregister_protosw(&udpv6_protosw);
1579 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}