sock_map: Introduce BPF_SK_SKB_VERDICT
[linux-block.git] / net / ipv6 / udp.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * UDP over IPv6
1ab1457c 4 * Linux INET6 implementation
1da177e4
LT
5 *
6 * Authors:
1ab1457c 7 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
8 *
9 * Based on linux/ipv4/udp.c
10 *
1da177e4
LT
11 * Fixes:
12 * Hideaki YOSHIFUJI : sin6_scope_id support
13 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
14 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
15 * a single port at the same time.
16 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
17 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
1da177e4
LT
18 */
19
1da177e4
LT
20#include <linux/errno.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/sockios.h>
1da177e4
LT
24#include <linux/net.h>
25#include <linux/in6.h>
26#include <linux/netdevice.h>
27#include <linux/if_arp.h>
28#include <linux/ipv6.h>
29#include <linux/icmpv6.h>
30#include <linux/init.h>
1781f7f5 31#include <linux/module.h>
3305b80c 32#include <linux/skbuff.h>
5a0e3ad6 33#include <linux/slab.h>
7c0f6ba6 34#include <linux/uaccess.h>
0e219ae4 35#include <linux/indirect_call_wrapper.h>
1da177e4 36
496611d7 37#include <net/addrconf.h>
1da177e4
LT
38#include <net/ndisc.h>
39#include <net/protocol.h>
40#include <net/transp_v6.h>
41#include <net/ip6_route.h>
1da177e4 42#include <net/raw.h>
c752f073 43#include <net/tcp_states.h>
1da177e4 44#include <net/ip6_checksum.h>
e7cc0824 45#include <net/ip6_tunnel.h>
1da177e4 46#include <net/xfrm.h>
0bd84065 47#include <net/inet_hashtables.h>
72289b96 48#include <net/inet6_hashtables.h>
076bb0c8 49#include <net/busy_poll.h>
e32ea7e7 50#include <net/sock_reuseport.h>
1da177e4
LT
51
52#include <linux/proc_fs.h>
53#include <linux/seq_file.h>
22911fc5 54#include <trace/events/skb.h>
ba4e58ec 55#include "udp_impl.h"
1da177e4 56
6eada011
ED
57static u32 udp6_ehashfn(const struct net *net,
58 const struct in6_addr *laddr,
59 const u16 lport,
60 const struct in6_addr *faddr,
61 const __be16 fport)
b50026b5 62{
1bbdceef
HFS
63 static u32 udp6_ehash_secret __read_mostly;
64 static u32 udp_ipv6_hash_secret __read_mostly;
65
66 u32 lhash, fhash;
67
68 net_get_random_once(&udp6_ehash_secret,
69 sizeof(udp6_ehash_secret));
70 net_get_random_once(&udp_ipv6_hash_secret,
71 sizeof(udp_ipv6_hash_secret));
72
73 lhash = (__force u32)laddr->s6_addr32[3];
74 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
75
b50026b5 76 return __inet6_ehashfn(lhash, lport, fhash, fport,
1bbdceef 77 udp_ipv6_hash_secret + net_hash_mix(net));
b50026b5
HFS
78}
79
6ba5a3c5 80int udp_v6_get_port(struct sock *sk, unsigned short snum)
1da177e4 81{
30fff923 82 unsigned int hash2_nulladdr =
f0b1e64c 83 ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
9a52e97e 84 unsigned int hash2_partial =
f0b1e64c 85 ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
30fff923 86
d4cada4a 87 /* precompute partial secondary hash */
30fff923 88 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
fe38d2a1 89 return udp_lib_get_port(sk, snum, hash2_nulladdr);
1da177e4
LT
90}
91
f7c46156 92void udp_v6_rehash(struct sock *sk)
719f8358 93{
f0b1e64c 94 u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
efe4208f 95 &sk->sk_v6_rcv_saddr,
719f8358
ED
96 inet_sk(sk)->inet_num);
97
98 udp_lib_rehash(sk, new_hash);
99}
100
d1e37288
SX
101static int compute_score(struct sock *sk, struct net *net,
102 const struct in6_addr *saddr, __be16 sport,
103 const struct in6_addr *daddr, unsigned short hnum,
73545373 104 int dif, int sdif)
645ca708 105{
60c04aec
JP
106 int score;
107 struct inet_sock *inet;
6da5b0f0 108 bool dev_match;
60c04aec
JP
109
110 if (!net_eq(sock_net(sk), net) ||
111 udp_sk(sk)->udp_port_hash != hnum ||
112 sk->sk_family != PF_INET6)
113 return -1;
114
23b0269e
PO
115 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
116 return -1;
117
60c04aec
JP
118 score = 0;
119 inet = inet_sk(sk);
120
121 if (inet->inet_dport) {
122 if (inet->inet_dport != sport)
123 return -1;
124 score++;
125 }
126
60c04aec
JP
127 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
128 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
129 return -1;
130 score++;
131 }
132
6da5b0f0
MM
133 dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
134 if (!dev_match)
135 return -1;
136 score++;
60c04aec 137
7170a977 138 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
70da268b
ED
139 score++;
140
645ca708
ED
141 return score;
142}
143
a57066b1
DM
144static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
145 struct sk_buff *skb,
146 const struct in6_addr *saddr,
147 __be16 sport,
148 const struct in6_addr *daddr,
149 unsigned int hnum)
2a08748c
JS
150{
151 struct sock *reuse_sk = NULL;
152 u32 hash;
153
154 if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
155 hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
156 reuse_sk = reuseport_select_sock(sk, hash, skb,
157 sizeof(struct udphdr));
2a08748c
JS
158 }
159 return reuse_sk;
160}
161
d1e37288 162/* called with rcu_read_lock() */
fddc17de
ED
163static struct sock *udp6_lib_lookup2(struct net *net,
164 const struct in6_addr *saddr, __be16 sport,
1801b570 165 const struct in6_addr *daddr, unsigned int hnum,
73545373
TB
166 int dif, int sdif, struct udp_hslot *hslot2,
167 struct sk_buff *skb)
fddc17de
ED
168{
169 struct sock *sk, *result;
e94a62f5 170 int score, badness;
fddc17de 171
fddc17de
ED
172 result = NULL;
173 badness = -1;
ca065d0c 174 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
d1e37288 175 score = compute_score(sk, net, saddr, sport,
73545373 176 daddr, hnum, dif, sdif);
fddc17de 177 if (score > badness) {
2a08748c
JS
178 result = lookup_reuseport(net, sk, skb,
179 saddr, sport, daddr, hnum);
a57066b1
DM
180 /* Fall back to scoring if group has connections */
181 if (result && !reuseport_has_conns(sk, false))
2a08748c
JS
182 return result;
183
a57066b1 184 result = result ? : sk;
ca065d0c 185 badness = score;
fddc17de
ED
186 }
187 }
fddc17de
ED
188 return result;
189}
190
6d4201b1
JS
191static inline struct sock *udp6_lookup_run_bpf(struct net *net,
192 struct udp_table *udptable,
193 struct sk_buff *skb,
194 const struct in6_addr *saddr,
195 __be16 sport,
196 const struct in6_addr *daddr,
197 u16 hnum)
198{
199 struct sock *sk, *reuse_sk;
200 bool no_reuseport;
201
202 if (udptable != &udp_table)
203 return NULL; /* only UDP is supported */
204
205 no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP,
206 saddr, sport, daddr, hnum, &sk);
207 if (no_reuseport || IS_ERR_OR_NULL(sk))
208 return sk;
209
210 reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
c64c9c28 211 if (reuse_sk)
6d4201b1
JS
212 sk = reuse_sk;
213 return sk;
214}
215
ca065d0c 216/* rcu_read_lock() must be held */
fce82338 217struct sock *__udp6_lib_lookup(struct net *net,
1801b570
DA
218 const struct in6_addr *saddr, __be16 sport,
219 const struct in6_addr *daddr, __be16 dport,
220 int dif, int sdif, struct udp_table *udptable,
221 struct sk_buff *skb)
1da177e4 222{
1da177e4 223 unsigned short hnum = ntohs(dport);
23b0269e
PO
224 unsigned int hash2, slot2;
225 struct udp_hslot *hslot2;
6d4201b1 226 struct sock *result, *sk;
645ca708 227
23b0269e
PO
228 hash2 = ipv6_portaddr_hash(net, daddr, hnum);
229 slot2 = hash2 & udptable->mask;
230 hslot2 = &udptable->hash2[slot2];
231
6d4201b1 232 /* Lookup connected or non-wildcard sockets */
23b0269e 233 result = udp6_lib_lookup2(net, saddr, sport,
73545373 234 daddr, hnum, dif, sdif,
23b0269e 235 hslot2, skb);
6d4201b1
JS
236 if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
237 goto done;
238
239 /* Lookup redirect from BPF */
240 if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
241 sk = udp6_lookup_run_bpf(net, udptable, skb,
242 saddr, sport, daddr, hnum);
243 if (sk) {
244 result = sk;
245 goto done;
246 }
247 }
23b0269e 248
6d4201b1
JS
249 /* Got non-wildcard socket or error on first lookup */
250 if (result)
251 goto done;
fddc17de 252
6d4201b1
JS
253 /* Lookup wildcard sockets */
254 hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
255 slot2 = hash2 & udptable->mask;
256 hslot2 = &udptable->hash2[slot2];
257
258 result = udp6_lib_lookup2(net, saddr, sport,
259 &in6addr_any, hnum, dif, sdif,
260 hslot2, skb);
261done:
26f8113c 262 if (IS_ERR(result))
23b0269e 263 return NULL;
1da177e4
LT
264 return result;
265}
fce82338 266EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
1da177e4 267
607c4aaf
KK
268static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
269 __be16 sport, __be16 dport,
645ca708 270 struct udp_table *udptable)
607c4aaf 271{
b71d1d42 272 const struct ipv6hdr *iph = ipv6_hdr(skb);
607c4aaf 273
ed7cbbce 274 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
adf30907 275 &iph->daddr, dport, inet6_iif(skb),
1801b570 276 inet6_sdif(skb), udptable, skb);
607c4aaf
KK
277}
278
7b58e63e 279struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
63058308
TH
280 __be16 sport, __be16 dport)
281{
282 const struct ipv6hdr *iph = ipv6_hdr(skb);
63058308 283
ed7cbbce 284 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
63058308 285 &iph->daddr, dport, inet6_iif(skb),
257a525f 286 inet6_sdif(skb), &udp_table, NULL);
63058308 287}
63058308 288
ca065d0c
ED
289/* Must be called under rcu_read_lock().
290 * Does increment socket refcount.
291 */
6e86000c 292#if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
aa976fc0
BS
293struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
294 const struct in6_addr *daddr, __be16 dport, int dif)
295{
ca065d0c
ED
296 struct sock *sk;
297
298 sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
1801b570 299 dif, 0, &udp_table, NULL);
41c6d650 300 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
ca065d0c
ED
301 sk = NULL;
302 return sk;
aa976fc0
BS
303}
304EXPORT_SYMBOL_GPL(udp6_lib_lookup);
ca065d0c 305#endif
aa976fc0 306
cb891fa6
PA
307/* do not use the scratch area len for jumbogram: their length execeeds the
308 * scratch area space; note that the IP6CB flags is still in the first
309 * cacheline, so checking for jumbograms is cheap
310 */
311static int udp6_skb_len(struct sk_buff *skb)
312{
313 return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
314}
315
1da177e4 316/*
67ba4152
IM
317 * This should be easy, if there is something there we
318 * return it, otherwise we block.
1da177e4
LT
319 */
320
1b784140 321int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1da177e4
LT
322 int noblock, int flags, int *addr_len)
323{
324 struct ipv6_pinfo *np = inet6_sk(sk);
325 struct inet_sock *inet = inet_sk(sk);
1ab1457c 326 struct sk_buff *skb;
59c2cdae 327 unsigned int ulen, copied;
fd69c399 328 int off, err, peeking = flags & MSG_PEEK;
759e5d00 329 int is_udplite = IS_UDPLITE(sk);
543fc3fb 330 struct udp_mib __percpu *mib;
197c949e 331 bool checksum_valid = false;
f26ba175 332 int is_udp4;
1da177e4 333
1da177e4 334 if (flags & MSG_ERRQUEUE)
85fbaa75 335 return ipv6_recv_error(sk, msg, len, addr_len);
1da177e4 336
4b340ae2 337 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
85fbaa75 338 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4b340ae2 339
1da177e4 340try_again:
a0917e0b 341 off = sk_peek_offset(sk, flags);
fd69c399 342 skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
1da177e4 343 if (!skb)
627d2d6b 344 return err;
1da177e4 345
cb891fa6 346 ulen = udp6_skb_len(skb);
59c2cdae 347 copied = len;
627d2d6b 348 if (copied > ulen - off)
349 copied = ulen - off;
59c2cdae 350 else if (copied < ulen)
1ab1457c 351 msg->msg_flags |= MSG_TRUNC;
1da177e4 352
f26ba175 353 is_udp4 = (skb->protocol == htons(ETH_P_IP));
029a3743 354 mib = __UDPX_MIB(sk, is_udp4);
f26ba175 355
ba4e58ec 356 /*
759e5d00
HX
357 * If checksum is needed at all, try to do it while copying the
358 * data. If the data is truncated, or if we only want a partial
359 * coverage checksum (UDP-Lite), do it before the copy.
ba4e58ec 360 */
ba4e58ec 361
d21dbdfe
ED
362 if (copied < ulen || peeking ||
363 (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
67a51780
PA
364 checksum_valid = udp_skb_csum_unnecessary(skb) ||
365 !__udp_lib_checksum_complete(skb);
197c949e 366 if (!checksum_valid)
1da177e4 367 goto csum_copy_err;
ba4e58ec
GR
368 }
369
67a51780
PA
370 if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
371 if (udp_skb_is_linear(skb))
372 err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
373 else
374 err = skb_copy_datagram_msg(skb, off, msg, copied);
375 } else {
627d2d6b 376 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
1da177e4
LT
377 if (err == -EINVAL)
378 goto csum_copy_err;
379 }
22911fc5 380 if (unlikely(err)) {
fd69c399 381 if (!peeking) {
979402b1 382 atomic_inc(&sk->sk_drops);
029a3743 383 SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
979402b1 384 }
850cbadd 385 kfree_skb(skb);
627d2d6b 386 return err;
22911fc5 387 }
fd69c399 388 if (!peeking)
029a3743 389 SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
cb75994e 390
3b885787 391 sock_recv_ts_and_drops(msg, sk, skb);
1da177e4
LT
392
393 /* Copy the address. */
394 if (msg->msg_name) {
342dfc30 395 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1da177e4 396 sin6->sin6_family = AF_INET6;
4bedb452 397 sin6->sin6_port = udp_hdr(skb)->source;
1da177e4 398 sin6->sin6_flowinfo = 0;
1da177e4 399
842df073 400 if (is_udp4) {
b301e82c
BH
401 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
402 &sin6->sin6_addr);
842df073
HFS
403 sin6->sin6_scope_id = 0;
404 } else {
4e3fd7a0 405 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
842df073
HFS
406 sin6->sin6_scope_id =
407 ipv6_iface_scope_id(&sin6->sin6_addr,
4330487a 408 inet6_iif(skb));
1da177e4 409 }
bceaa902 410 *addr_len = sizeof(*sin6);
983695fa 411
a9ed15da
SF
412 BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
413 (struct sockaddr *)sin6);
1da177e4 414 }
4b261c75 415
bcd1665e
PA
416 if (udp_sk(sk)->gro_enabled)
417 udp_cmsg_recv(msg, sk, skb);
418
4b261c75
HFS
419 if (np->rxopt.all)
420 ip6_datagram_recv_common_ctl(sk, msg, skb);
421
f26ba175 422 if (is_udp4) {
1da177e4 423 if (inet->cmsg_flags)
ad959036 424 ip_cmsg_recv_offset(msg, sk, skb,
10df8e61 425 sizeof(struct udphdr), off);
1da177e4
LT
426 } else {
427 if (np->rxopt.all)
4b261c75 428 ip6_datagram_recv_specific_ctl(sk, msg, skb);
1ab1457c 429 }
1da177e4 430
59c2cdae 431 err = copied;
1da177e4 432 if (flags & MSG_TRUNC)
759e5d00 433 err = ulen;
1da177e4 434
850cbadd 435 skb_consume_udp(sk, skb, peeking ? -err : err);
1da177e4
LT
436 return err;
437
438csum_copy_err:
2276f58a
PA
439 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
440 udp_skb_destructor)) {
029a3743
PA
441 SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
442 SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
0856f939 443 }
850cbadd 444 kfree_skb(skb);
1da177e4 445
beb39db5
ED
446 /* starting over for a new packet, but check if we need to yield */
447 cond_resched();
9cfaa8de 448 msg->msg_flags &= ~MSG_TRUNC;
1da177e4
LT
449 goto try_again;
450}
451
a36e185e
SB
452DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
453void udpv6_encap_enable(void)
454{
9c480601 455 static_branch_inc(&udpv6_encap_needed_key);
a36e185e
SB
456}
457EXPORT_SYMBOL(udpv6_encap_enable);
458
e7cc0824
SB
459/* Handler for tunnels with arbitrary destination ports: no socket lookup, go
460 * through error handlers in encapsulations looking for a match.
461 */
462static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
463 struct inet6_skb_parm *opt,
424a7cd0 464 u8 type, u8 code, int offset, __be32 info)
e7cc0824
SB
465{
466 int i;
467
468 for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
469 int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
424a7cd0
PA
470 u8 type, u8 code, int offset, __be32 info);
471 const struct ip6_tnl_encap_ops *encap;
e7cc0824 472
424a7cd0
PA
473 encap = rcu_dereference(ip6tun_encaps[i]);
474 if (!encap)
e7cc0824 475 continue;
424a7cd0 476 handler = encap->err_handler;
e7cc0824
SB
477 if (handler && !handler(skb, opt, type, code, offset, info))
478 return 0;
479 }
480
481 return -ENOENT;
482}
483
a36e185e
SB
484/* Try to match ICMP errors to UDP tunnels by looking up a socket without
485 * reversing source and destination port: this will match tunnels that force the
486 * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
487 * lwtunnels might actually break this assumption by being configured with
488 * different destination ports on endpoints, in this case we won't be able to
489 * trace ICMP messages back to them.
490 *
e7cc0824
SB
491 * If this doesn't match any socket, probe tunnels with arbitrary destination
492 * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
493 * we've sent packets to won't necessarily match the local destination port.
494 *
a36e185e
SB
495 * Then ask the tunnel implementation to match the error against a valid
496 * association.
497 *
e7cc0824
SB
498 * Return an error if we can't find a match, the socket if we need further
499 * processing, zero otherwise.
a36e185e
SB
500 */
501static struct sock *__udp6_lib_err_encap(struct net *net,
502 const struct ipv6hdr *hdr, int offset,
503 struct udphdr *uh,
504 struct udp_table *udptable,
e7cc0824
SB
505 struct sk_buff *skb,
506 struct inet6_skb_parm *opt,
507 u8 type, u8 code, __be32 info)
a36e185e 508{
a36e185e 509 int network_offset, transport_offset;
a36e185e
SB
510 struct sock *sk;
511
a36e185e
SB
512 network_offset = skb_network_offset(skb);
513 transport_offset = skb_transport_offset(skb);
514
515 /* Network header needs to point to the outer IPv6 header inside ICMP */
516 skb_reset_network_header(skb);
517
518 /* Transport header needs to point to the UDP header */
519 skb_set_transport_header(skb, offset);
520
e7cc0824
SB
521 sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
522 &hdr->saddr, uh->dest,
523 inet6_iif(skb), 0, udptable, skb);
524 if (sk) {
525 int (*lookup)(struct sock *sk, struct sk_buff *skb);
526 struct udp_sock *up = udp_sk(sk);
527
528 lookup = READ_ONCE(up->encap_err_lookup);
529 if (!lookup || lookup(sk, skb))
530 sk = NULL;
531 }
532
533 if (!sk) {
534 sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
535 offset, info));
536 }
a36e185e
SB
537
538 skb_set_transport_header(skb, transport_offset);
539 skb_set_network_header(skb, network_offset);
e7cc0824 540
a36e185e
SB
541 return sk;
542}
543
32bbd879
SB
544int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
545 u8 type, u8 code, int offset, __be32 info,
546 struct udp_table *udptable)
1da177e4
LT
547{
548 struct ipv6_pinfo *np;
b71d1d42
ED
549 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
550 const struct in6_addr *saddr = &hdr->saddr;
551 const struct in6_addr *daddr = &hdr->daddr;
67ba4152 552 struct udphdr *uh = (struct udphdr *)(skb->data+offset);
a36e185e 553 bool tunnel = false;
1da177e4 554 struct sock *sk;
e0d8c1b7 555 int harderr;
1da177e4 556 int err;
7304fe46 557 struct net *net = dev_net(skb->dev);
1da177e4 558
e32ea7e7 559 sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
4ac30c4b 560 inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
d26796ae 561 if (!sk || udp_sk(sk)->encap_type) {
a36e185e 562 /* No socket for error: try tunnels before discarding */
e7cc0824 563 sk = ERR_PTR(-ENOENT);
a36e185e
SB
564 if (static_branch_unlikely(&udpv6_encap_needed_key)) {
565 sk = __udp6_lib_err_encap(net, hdr, offset, uh,
e7cc0824
SB
566 udptable, skb,
567 opt, type, code, info);
568 if (!sk)
569 return 0;
a36e185e
SB
570 }
571
e7cc0824 572 if (IS_ERR(sk)) {
a36e185e
SB
573 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
574 ICMP6_MIB_INERRORS);
e7cc0824 575 return PTR_ERR(sk);
a36e185e 576 }
e7cc0824 577
a36e185e 578 tunnel = true;
7304fe46 579 }
1da177e4 580
e0d8c1b7
WW
581 harderr = icmpv6_err_convert(type, code, &err);
582 np = inet6_sk(sk);
583
93b36cf3
HFS
584 if (type == ICMPV6_PKT_TOOBIG) {
585 if (!ip6_sk_accept_pmtu(sk))
586 goto out;
81aded24 587 ip6_sk_update_pmtu(skb, sk, info);
e0d8c1b7
WW
588 if (np->pmtudisc != IPV6_PMTUDISC_DONT)
589 harderr = 1;
93b36cf3 590 }
1a462d18 591 if (type == NDISC_REDIRECT) {
a36e185e
SB
592 if (tunnel) {
593 ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
594 sk->sk_mark, sk->sk_uid);
595 } else {
596 ip6_sk_redirect(skb, sk);
597 }
1a462d18
DJ
598 goto out;
599 }
81aded24 600
a36e185e
SB
601 /* Tunnels don't have an application socket: don't pass errors back */
602 if (tunnel)
603 goto out;
604
e0d8c1b7
WW
605 if (!np->recverr) {
606 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
607 goto out;
608 } else {
1da177e4 609 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
e0d8c1b7 610 }
b1faf566 611
1da177e4
LT
612 sk->sk_err = err;
613 sk->sk_error_report(sk);
614out:
32bbd879 615 return 0;
1da177e4
LT
616}
617
a3f96c47 618static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
f7ad74fe
BL
619{
620 int rc;
621
efe4208f 622 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
f7ad74fe 623 sock_rps_save_rxhash(sk, skb);
005ec974 624 sk_mark_napi_id(sk, skb);
2c8c56e1 625 sk_incoming_cpu_update(sk);
e68b6e50
ED
626 } else {
627 sk_mark_napi_id_once(sk, skb);
005ec974 628 }
f7ad74fe 629
850cbadd 630 rc = __udp_enqueue_schedule_skb(sk, skb);
f7ad74fe
BL
631 if (rc < 0) {
632 int is_udplite = IS_UDPLITE(sk);
633
634 /* Note that an ENOMEM error is charged twice */
635 if (rc == -ENOMEM)
e61da9e2 636 UDP6_INC_STATS(sock_net(sk),
02c22347 637 UDP_MIB_RCVBUFERRORS, is_udplite);
a3ce2b10
MD
638 else
639 UDP6_INC_STATS(sock_net(sk),
640 UDP_MIB_MEMERRORS, is_udplite);
e61da9e2 641 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
f7ad74fe
BL
642 kfree_skb(skb);
643 return -1;
644 }
850cbadd 645
f7ad74fe
BL
646 return 0;
647}
648
32bbd879
SB
649static __inline__ int udpv6_err(struct sk_buff *skb,
650 struct inet6_skb_parm *opt, u8 type,
651 u8 code, int offset, __be32 info)
ba4e58ec 652{
32bbd879 653 return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
ba4e58ec
GR
654}
655
cf329aa4 656static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
1da177e4 657{
ba4e58ec 658 struct udp_sock *up = udp_sk(sk);
b2bf1e26 659 int is_udplite = IS_UDPLITE(sk);
a18135eb 660
ba4e58ec
GR
661 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
662 goto drop;
1da177e4 663
88ab3108 664 if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
d7f3f621
BL
665 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
666
667 /*
668 * This is an encapsulation socket so pass the skb to
669 * the socket's udp_encap_rcv() hook. Otherwise, just
670 * fall through and pass this up the UDP socket.
671 * up->encap_rcv() returns the following value:
672 * =0 if skb was successfully passed to the encap
673 * handler or was discarded by it.
674 * >0 if skb should be passed on to UDP.
675 * <0 if skb should be resubmitted as proto -N
676 */
677
678 /* if we're overly short, let UDP handle it */
6aa7de05 679 encap_rcv = READ_ONCE(up->encap_rcv);
e5aed006 680 if (encap_rcv) {
d7f3f621
BL
681 int ret;
682
0a80966b
TH
683 /* Verify checksum before giving to encap */
684 if (udp_lib_checksum_complete(skb))
685 goto csum_error;
686
d7f3f621
BL
687 ret = encap_rcv(sk, skb);
688 if (ret <= 0) {
02c22347
ED
689 __UDP_INC_STATS(sock_net(sk),
690 UDP_MIB_INDATAGRAMS,
691 is_udplite);
d7f3f621
BL
692 return -ret;
693 }
694 }
695
696 /* FALLTHROUGH -- it's a UDP Packet */
697 }
698
ba4e58ec
GR
699 /*
700 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
701 */
b0a42277 702 if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
ba4e58ec
GR
703
704 if (up->pcrlen == 0) { /* full coverage was set */
ba7a46f1
JP
705 net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
706 UDP_SKB_CB(skb)->cscov, skb->len);
ba4e58ec
GR
707 goto drop;
708 }
709 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
ba7a46f1
JP
710 net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
711 UDP_SKB_CB(skb)->cscov, up->pcrlen);
ba4e58ec
GR
712 goto drop;
713 }
1da177e4
LT
714 }
715
4b943fae 716 prefetch(&sk->sk_rmem_alloc);
ce25d66a
ED
717 if (rcu_access_pointer(sk->sk_filter) &&
718 udp_lib_checksum_complete(skb))
719 goto csum_error;
720
ba66bbe5 721 if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
a6127697 722 goto drop;
ba4e58ec 723
e6afc8ac 724 udp_csum_pull_header(skb);
cb80ef46 725
d826eb14 726 skb_dst_drop(skb);
cb75994e 727
850cbadd 728 return __udpv6_queue_rcv_skb(sk, skb);
3e215c8d 729
6a5dc9e5 730csum_error:
02c22347 731 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
ba4e58ec 732drop:
02c22347 733 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
cb80ef46 734 atomic_inc(&sk->sk_drops);
ba4e58ec
GR
735 kfree_skb(skb);
736 return -1;
1da177e4
LT
737}
738
cf329aa4
PA
739static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
740{
741 struct sk_buff *next, *segs;
742 int ret;
743
744 if (likely(!udp_unexpected_gso(sk, skb)))
745 return udpv6_queue_rcv_one_skb(sk, skb);
746
747 __skb_push(skb, -skb_mac_offset(skb));
748 segs = udp_rcv_segment(sk, skb, false);
1a186c14 749 skb_list_walk_safe(segs, skb, next) {
cf329aa4
PA
750 __skb_pull(skb, skb_transport_offset(skb));
751
752 ret = udpv6_queue_rcv_one_skb(sk, skb);
753 if (ret > 0)
754 ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
755 true);
756 }
757 return 0;
758}
759
5cf3d461
DH
760static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
761 __be16 loc_port, const struct in6_addr *loc_addr,
762 __be16 rmt_port, const struct in6_addr *rmt_addr,
7bd2db40 763 int dif, int sdif, unsigned short hnum)
1da177e4 764{
5cf3d461 765 struct inet_sock *inet = inet_sk(sk);
1da177e4 766
5cf3d461
DH
767 if (!net_eq(sock_net(sk), net))
768 return false;
769
770 if (udp_sk(sk)->udp_port_hash != hnum ||
771 sk->sk_family != PF_INET6 ||
772 (inet->inet_dport && inet->inet_dport != rmt_port) ||
773 (!ipv6_addr_any(&sk->sk_v6_daddr) &&
774 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
7bd2db40 775 !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
33b4b015
HR
776 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
777 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
5cf3d461
DH
778 return false;
779 if (!inet6_mc_check(sk, loc_addr, rmt_addr))
780 return false;
781 return true;
1da177e4
LT
782}
783
4068579e
TH
784static void udp6_csum_zero_error(struct sk_buff *skb)
785{
786 /* RFC 2460 section 8.1 says that we SHOULD log
787 * this error. Well, it is reasonable.
788 */
ba7a46f1
JP
789 net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
790 &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
791 &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
4068579e
TH
792}
793
1da177e4
LT
794/*
795 * Note: called only from the BH handler context,
796 * so we don't need to lock the hashes.
797 */
e3163493 798static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
b71d1d42 799 const struct in6_addr *saddr, const struct in6_addr *daddr,
36cbb245 800 struct udp_table *udptable, int proto)
1da177e4 801{
ca065d0c 802 struct sock *sk, *first = NULL;
4bedb452 803 const struct udphdr *uh = udp_hdr(skb);
5cf3d461
DH
804 unsigned short hnum = ntohs(uh->dest);
805 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
ca065d0c 806 unsigned int offset = offsetof(typeof(*sk), sk_node);
2dc41cff 807 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
ca065d0c 808 int dif = inet6_iif(skb);
7bd2db40 809 int sdif = inet6_sdif(skb);
ca065d0c
ED
810 struct hlist_node *node;
811 struct sk_buff *nskb;
2dc41cff
DH
812
813 if (use_hash2) {
f0b1e64c 814 hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
73e2d5e3 815 udptable->mask;
f0b1e64c 816 hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
2dc41cff 817start_lookup:
73e2d5e3 818 hslot = &udptable->hash2[hash2];
2dc41cff
DH
819 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
820 }
1da177e4 821
ca065d0c
ED
822 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
823 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
7bd2db40
DM
824 uh->source, saddr, dif, sdif,
825 hnum))
ca065d0c
ED
826 continue;
827 /* If zero checksum and no_check is not on for
828 * the socket then skip it.
829 */
830 if (!uh->check && !udp_sk(sk)->no_check6_rx)
831 continue;
832 if (!first) {
833 first = sk;
834 continue;
835 }
836 nskb = skb_clone(skb, GFP_ATOMIC);
837 if (unlikely(!nskb)) {
838 atomic_inc(&sk->sk_drops);
02c22347
ED
839 __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
840 IS_UDPLITE(sk));
841 __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
842 IS_UDPLITE(sk));
ca065d0c 843 continue;
95766fff 844 }
a1ab77f9 845
ca065d0c
ED
846 if (udpv6_queue_rcv_skb(sk, nskb) > 0)
847 consume_skb(nskb);
848 }
a1ab77f9 849
2dc41cff
DH
850 /* Also lookup *:port if we are using hash2 and haven't done so yet. */
851 if (use_hash2 && hash2 != hash2_any) {
852 hash2 = hash2_any;
853 goto start_lookup;
854 }
855
ca065d0c
ED
856 if (first) {
857 if (udpv6_queue_rcv_skb(first, skb) > 0)
858 consume_skb(skb);
a1ab77f9 859 } else {
ca065d0c 860 kfree_skb(skb);
02c22347
ED
861 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
862 proto == IPPROTO_UDPLITE);
a1ab77f9 863 }
ba4e58ec 864 return 0;
1da177e4
LT
865}
866
64f0f5d1
PA
867static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
868{
869 if (udp_sk_rx_dst_set(sk, dst)) {
870 const struct rt6_info *rt = (const struct rt6_info *)dst;
871
872 inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
873 }
874}
875
eb63f296
PA
876/* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
877 * return code conversion for ip layer consumption
878 */
879static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
880 struct udphdr *uh)
881{
882 int ret;
883
884 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
e4aa33ad 885 skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
eb63f296
PA
886
887 ret = udpv6_queue_rcv_skb(sk, skb);
888
84dad559 889 /* a return value > 0 means to resubmit the input */
eb63f296 890 if (ret > 0)
84dad559 891 return ret;
eb63f296
PA
892 return 0;
893}
894
645ca708 895int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
759e5d00 896 int proto)
1da177e4 897{
ca065d0c 898 const struct in6_addr *saddr, *daddr;
3ffe533c 899 struct net *net = dev_net(skb->dev);
1ab1457c 900 struct udphdr *uh;
ca065d0c 901 struct sock *sk;
71489e21 902 bool refcounted;
1da177e4
LT
903 u32 ulen = 0;
904
905 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
d6bc0149 906 goto discard;
1da177e4 907
0660e03f
ACM
908 saddr = &ipv6_hdr(skb)->saddr;
909 daddr = &ipv6_hdr(skb)->daddr;
4bedb452 910 uh = udp_hdr(skb);
1da177e4
LT
911
912 ulen = ntohs(uh->len);
ba4e58ec
GR
913 if (ulen > skb->len)
914 goto short_packet;
1da177e4 915
759e5d00
HX
916 if (proto == IPPROTO_UDP) {
917 /* UDP validates ulen. */
1da177e4 918
ba4e58ec
GR
919 /* Check for jumbo payload */
920 if (ulen == 0)
921 ulen = skb->len;
1da177e4 922
ba4e58ec
GR
923 if (ulen < sizeof(*uh))
924 goto short_packet;
1da177e4 925
ba4e58ec
GR
926 if (ulen < skb->len) {
927 if (pskb_trim_rcsum(skb, ulen))
928 goto short_packet;
0660e03f
ACM
929 saddr = &ipv6_hdr(skb)->saddr;
930 daddr = &ipv6_hdr(skb)->daddr;
4bedb452 931 uh = udp_hdr(skb);
ba4e58ec 932 }
ba4e58ec 933 }
1da177e4 934
759e5d00 935 if (udp6_csum_init(skb, uh, proto))
6a5dc9e5 936 goto csum_error;
759e5d00 937
c9f2c1ae 938 /* Check if the socket is already available, e.g. due to early demux */
71489e21 939 sk = skb_steal_sock(skb, &refcounted);
c9f2c1ae
PA
940 if (sk) {
941 struct dst_entry *dst = skb_dst(skb);
942 int ret;
943
944 if (unlikely(sk->sk_rx_dst != dst))
64f0f5d1 945 udp6_sk_rx_dst_set(sk, dst);
c9f2c1ae 946
eb63f296 947 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
71489e21
JS
948 if (refcounted)
949 sock_put(sk);
eb63f296
PA
950 goto report_csum_error;
951 }
c9f2c1ae 952
eb63f296 953 ret = udp6_unicast_rcv_skb(sk, skb, uh);
71489e21
JS
954 if (refcounted)
955 sock_put(sk);
eb63f296 956 return ret;
c9f2c1ae
PA
957 }
958
1ab1457c
YH
959 /*
960 * Multicast receive code
1da177e4 961 */
ba4e58ec 962 if (ipv6_addr_is_multicast(daddr))
e3163493 963 return __udp6_lib_mcast_deliver(net, skb,
36cbb245 964 saddr, daddr, udptable, proto);
1da177e4
LT
965
966 /* Unicast */
607c4aaf 967 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
53b24b8f 968 if (sk) {
eb63f296
PA
969 if (!uh->check && !udp_sk(sk)->no_check6_rx)
970 goto report_csum_error;
971 return udp6_unicast_rcv_skb(sk, skb, uh);
1da177e4 972 }
1ab1457c 973
eb63f296
PA
974 if (!uh->check)
975 goto report_csum_error;
4068579e 976
cb80ef46 977 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
c377411f 978 goto discard;
cb80ef46
BL
979
980 if (udp_lib_checksum_complete(skb))
6a5dc9e5 981 goto csum_error;
cb80ef46 982
02c22347 983 __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
cb80ef46
BL
984 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
985
986 kfree_skb(skb);
add459aa 987 return 0;
1da177e4 988
1ab1457c 989short_packet:
ba7a46f1
JP
990 net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
991 proto == IPPROTO_UDPLITE ? "-Lite" : "",
992 saddr, ntohs(uh->source),
993 ulen, skb->len,
994 daddr, ntohs(uh->dest));
6a5dc9e5 995 goto discard;
eb63f296
PA
996
997report_csum_error:
998 udp6_csum_zero_error(skb);
6a5dc9e5 999csum_error:
02c22347 1000 __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1da177e4 1001discard:
02c22347 1002 __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1da177e4 1003 kfree_skb(skb);
add459aa 1004 return 0;
1da177e4 1005}
ba4e58ec 1006
0bd84065 1007
5425077d 1008static struct sock *__udp6_lib_demux_lookup(struct net *net,
1009 __be16 loc_port, const struct in6_addr *loc_addr,
1010 __be16 rmt_port, const struct in6_addr *rmt_addr,
4297a0ef 1011 int dif, int sdif)
5425077d 1012{
0bd84065 1013 unsigned short hnum = ntohs(loc_port);
f0b1e64c 1014 unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
0bd84065 1015 unsigned int slot2 = hash2 & udp_table.mask;
1016 struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
1017 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
5425077d 1018 struct sock *sk;
1019
0bd84065 1020 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
85cb73ff 1021 if (sk->sk_state == TCP_ESTABLISHED &&
4297a0ef 1022 INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
0bd84065 1023 return sk;
1024 /* Only check first socket in chain */
1025 break;
1026 }
1027 return NULL;
5425077d 1028}
1029
97ff7ffb 1030INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb)
5425077d 1031{
1032 struct net *net = dev_net(skb->dev);
1033 const struct udphdr *uh;
1034 struct sock *sk;
1035 struct dst_entry *dst;
1036 int dif = skb->dev->ifindex;
4297a0ef 1037 int sdif = inet6_sdif(skb);
5425077d 1038
1039 if (!pskb_may_pull(skb, skb_transport_offset(skb) +
1040 sizeof(struct udphdr)))
1041 return;
1042
1043 uh = udp_hdr(skb);
1044
1045 if (skb->pkt_type == PACKET_HOST)
1046 sk = __udp6_lib_demux_lookup(net, uh->dest,
1047 &ipv6_hdr(skb)->daddr,
1048 uh->source, &ipv6_hdr(skb)->saddr,
4297a0ef 1049 dif, sdif);
5425077d 1050 else
1051 return;
1052
41c6d650 1053 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
5425077d 1054 return;
1055
1056 skb->sk = sk;
1057 skb->destructor = sock_efree;
1058 dst = READ_ONCE(sk->sk_rx_dst);
1059
1060 if (dst)
1061 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
1062 if (dst) {
d24406c8
WW
1063 /* set noref for now.
1064 * any place which wants to hold dst has to call
1065 * dst_hold_safe()
1066 */
1067 skb_dst_set_noref(skb, dst);
5425077d 1068 }
1069}
1070
0e219ae4 1071INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
ba4e58ec 1072{
645ca708 1073 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
ba4e58ec
GR
1074}
1075
1da177e4
LT
1076/*
1077 * Throw away all pending data and cancel the corking. Socket is locked.
1078 */
1079static void udp_v6_flush_pending_frames(struct sock *sk)
1080{
1081 struct udp_sock *up = udp_sk(sk);
1082
36d926b9
DL
1083 if (up->pending == AF_INET)
1084 udp_flush_pending_frames(sk);
1085 else if (up->pending) {
1da177e4
LT
1086 up->len = 0;
1087 up->pending = 0;
1088 ip6_flush_pending_frames(sk);
1ab1457c 1089 }
1da177e4
LT
1090}
1091
d74bad4e
AI
1092static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1093 int addr_len)
1094{
bddc028a
TH
1095 if (addr_len < offsetofend(struct sockaddr, sa_family))
1096 return -EINVAL;
d74bad4e
AI
1097 /* The following checks are replicated from __ip6_datagram_connect()
1098 * and intended to prevent BPF program called below from accessing
1099 * bytes that are out of the bound specified by user in addr_len.
1100 */
1101 if (uaddr->sa_family == AF_INET) {
1102 if (__ipv6_only_sock(sk))
1103 return -EAFNOSUPPORT;
1104 return udp_pre_connect(sk, uaddr, addr_len);
1105 }
1106
1107 if (addr_len < SIN6_LEN_RFC2133)
1108 return -EINVAL;
1109
1110 return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1111}
1112
493c6be3 1113/**
67ba4152
IM
1114 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
1115 * @sk: socket we are sending on
1116 * @skb: sk_buff containing the filled-in UDP header
1117 * (checksum field must be zeroed out)
b51cd7c8
AL
1118 * @saddr: source address
1119 * @daddr: destination address
1120 * @len: length of packet
493c6be3
SS
1121 */
1122static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1123 const struct in6_addr *saddr,
1124 const struct in6_addr *daddr, int len)
1125{
1126 unsigned int offset;
1127 struct udphdr *uh = udp_hdr(skb);
d39d938c 1128 struct sk_buff *frags = skb_shinfo(skb)->frag_list;
493c6be3
SS
1129 __wsum csum = 0;
1130
d39d938c 1131 if (!frags) {
493c6be3
SS
1132 /* Only one fragment on the socket. */
1133 skb->csum_start = skb_transport_header(skb) - skb->head;
1134 skb->csum_offset = offsetof(struct udphdr, check);
1135 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1136 } else {
1137 /*
1138 * HW-checksum won't work as there are two or more
1139 * fragments on the socket so that all csums of sk_buffs
1140 * should be together
1141 */
1142 offset = skb_transport_offset(skb);
1143 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
63ecc3d9 1144 csum = skb->csum;
493c6be3
SS
1145
1146 skb->ip_summed = CHECKSUM_NONE;
1147
d39d938c
VY
1148 do {
1149 csum = csum_add(csum, frags->csum);
1150 } while ((frags = frags->next));
493c6be3
SS
1151
1152 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1153 csum);
1154 if (uh->check == 0)
1155 uh->check = CSUM_MANGLED_0;
1156 }
1157}
1158
1da177e4
LT
1159/*
1160 * Sending
1161 */
1162
bec1f6f6
WB
1163static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1164 struct inet_cork *cork)
1da177e4 1165{
d39d938c 1166 struct sock *sk = skb->sk;
1da177e4 1167 struct udphdr *uh;
1da177e4 1168 int err = 0;
b2bf1e26 1169 int is_udplite = IS_UDPLITE(sk);
868c86bc 1170 __wsum csum = 0;
d39d938c
VY
1171 int offset = skb_transport_offset(skb);
1172 int len = skb->len - offset;
4094871d 1173 int datalen = len - sizeof(*uh);
1da177e4
LT
1174
1175 /*
1176 * Create a UDP header
1177 */
4bedb452 1178 uh = udp_hdr(skb);
1958b856
DM
1179 uh->source = fl6->fl6_sport;
1180 uh->dest = fl6->fl6_dport;
d39d938c 1181 uh->len = htons(len);
1da177e4
LT
1182 uh->check = 0;
1183
bec1f6f6
WB
1184 if (cork->gso_size) {
1185 const int hlen = skb_network_header_len(skb) +
1186 sizeof(struct udphdr);
1187
0f149c9f
WB
1188 if (hlen + cork->gso_size > cork->fragsize) {
1189 kfree_skb(skb);
bec1f6f6 1190 return -EINVAL;
0f149c9f
WB
1191 }
1192 if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) {
1193 kfree_skb(skb);
bec1f6f6 1194 return -EINVAL;
0f149c9f
WB
1195 }
1196 if (udp_sk(sk)->no_check6_tx) {
1197 kfree_skb(skb);
a8c744a8 1198 return -EINVAL;
0f149c9f 1199 }
ff06342c 1200 if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
0f149c9f
WB
1201 dst_xfrm(skb_dst(skb))) {
1202 kfree_skb(skb);
bec1f6f6 1203 return -EIO;
0f149c9f 1204 }
bec1f6f6 1205
4094871d
JH
1206 if (datalen > cork->gso_size) {
1207 skb_shinfo(skb)->gso_size = cork->gso_size;
1208 skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
1209 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
1210 cork->gso_size);
1211 }
a8c744a8 1212 goto csum_partial;
bec1f6f6
WB
1213 }
1214
b2bf1e26 1215 if (is_udplite)
d39d938c
VY
1216 csum = udplite_csum(skb);
1217 else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */
4068579e
TH
1218 skb->ip_summed = CHECKSUM_NONE;
1219 goto send;
1220 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
a8c744a8 1221csum_partial:
d39d938c 1222 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
493c6be3
SS
1223 goto send;
1224 } else
d39d938c 1225 csum = udp_csum(skb);
1da177e4 1226
ba4e58ec 1227 /* add protocol-dependent pseudo-header */
4c9483b2 1228 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
d39d938c 1229 len, fl6->flowi6_proto, csum);
1da177e4 1230 if (uh->check == 0)
f6ab0288 1231 uh->check = CSUM_MANGLED_0;
1da177e4 1232
493c6be3 1233send:
d39d938c 1234 err = ip6_send_skb(skb);
6ce9e7b5
ED
1235 if (err) {
1236 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
6aef70a8
ED
1237 UDP6_INC_STATS(sock_net(sk),
1238 UDP_MIB_SNDBUFERRORS, is_udplite);
6ce9e7b5
ED
1239 err = 0;
1240 }
6aef70a8
ED
1241 } else {
1242 UDP6_INC_STATS(sock_net(sk),
1243 UDP_MIB_OUTDATAGRAMS, is_udplite);
1244 }
d39d938c
VY
1245 return err;
1246}
1247
1248static int udp_v6_push_pending_frames(struct sock *sk)
1249{
1250 struct sk_buff *skb;
1251 struct udp_sock *up = udp_sk(sk);
1252 struct flowi6 fl6;
1253 int err = 0;
1254
1255 if (up->pending == AF_INET)
1256 return udp_push_pending_frames(sk);
1257
1258 /* ip6_finish_skb will release the cork, so make a copy of
1259 * fl6 here.
1260 */
1261 fl6 = inet_sk(sk)->cork.fl.u.ip6;
1262
1263 skb = ip6_finish_skb(sk);
1264 if (!skb)
1265 goto out;
1266
bec1f6f6 1267 err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
d39d938c 1268
1da177e4
LT
1269out:
1270 up->len = 0;
1271 up->pending = 0;
1272 return err;
1273}
1274
1b784140 1275int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1da177e4
LT
1276{
1277 struct ipv6_txoptions opt_space;
1278 struct udp_sock *up = udp_sk(sk);
1279 struct inet_sock *inet = inet_sk(sk);
1280 struct ipv6_pinfo *np = inet6_sk(sk);
342dfc30 1281 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
20c59de2 1282 struct in6_addr *daddr, *final_p, final;
1da177e4 1283 struct ipv6_txoptions *opt = NULL;
45f6fad8 1284 struct ipv6_txoptions *opt_to_free = NULL;
1da177e4 1285 struct ip6_flowlabel *flowlabel = NULL;
4c9483b2 1286 struct flowi6 fl6;
1da177e4 1287 struct dst_entry *dst;
26879da5 1288 struct ipcm6_cookie ipc6;
1da177e4 1289 int addr_len = msg->msg_namelen;
9f542f61 1290 bool connected = false;
1da177e4 1291 int ulen = len;
1da177e4
LT
1292 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1293 int err;
b2bf1e26 1294 int is_udplite = IS_UDPLITE(sk);
ba4e58ec 1295 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1da177e4 1296
b515430a 1297 ipcm6_init(&ipc6);
bec1f6f6 1298 ipc6.gso_size = up->gso_size;
5fdaa88d 1299 ipc6.sockc.tsflags = sk->sk_tsflags;
c6af0c22 1300 ipc6.sockc.mark = sk->sk_mark;
26879da5 1301
1da177e4
LT
1302 /* destination address check */
1303 if (sin6) {
1304 if (addr_len < offsetof(struct sockaddr, sa_data))
1305 return -EINVAL;
1306
1307 switch (sin6->sin6_family) {
1308 case AF_INET6:
1309 if (addr_len < SIN6_LEN_RFC2133)
1310 return -EINVAL;
1311 daddr = &sin6->sin6_addr;
052d2369
JL
1312 if (ipv6_addr_any(daddr) &&
1313 ipv6_addr_v4mapped(&np->saddr))
1314 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1315 daddr);
1da177e4
LT
1316 break;
1317 case AF_INET:
1318 goto do_udp_sendmsg;
1319 case AF_UNSPEC:
1320 msg->msg_name = sin6 = NULL;
1321 msg->msg_namelen = addr_len = 0;
1322 daddr = NULL;
1323 break;
1324 default:
1325 return -EINVAL;
1326 }
1327 } else if (!up->pending) {
1328 if (sk->sk_state != TCP_ESTABLISHED)
1329 return -EDESTADDRREQ;
efe4208f 1330 daddr = &sk->sk_v6_daddr;
1ab1457c 1331 } else
1da177e4
LT
1332 daddr = NULL;
1333
1334 if (daddr) {
e773e4fa 1335 if (ipv6_addr_v4mapped(daddr)) {
1da177e4
LT
1336 struct sockaddr_in sin;
1337 sin.sin_family = AF_INET;
c720c7e8 1338 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1da177e4
LT
1339 sin.sin_addr.s_addr = daddr->s6_addr32[3];
1340 msg->msg_name = &sin;
1341 msg->msg_namelen = sizeof(sin);
1342do_udp_sendmsg:
1343 if (__ipv6_only_sock(sk))
1344 return -ENETUNREACH;
1b784140 1345 return udp_sendmsg(sk, msg, len);
1da177e4
LT
1346 }
1347 }
1348
1349 if (up->pending == AF_INET)
1b784140 1350 return udp_sendmsg(sk, msg, len);
1da177e4
LT
1351
1352 /* Rough check on arithmetic overflow,
b59e139b 1353 better check is made in ip6_append_data().
1da177e4
LT
1354 */
1355 if (len > INT_MAX - sizeof(struct udphdr))
1356 return -EMSGSIZE;
1ab1457c 1357
03485f2a 1358 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1da177e4
LT
1359 if (up->pending) {
1360 /*
1361 * There are pending frames.
1362 * The socket lock must be held while it's corked.
1363 */
1364 lock_sock(sk);
1365 if (likely(up->pending)) {
1366 if (unlikely(up->pending != AF_INET6)) {
1367 release_sock(sk);
1368 return -EAFNOSUPPORT;
1369 }
1370 dst = NULL;
1371 goto do_append_data;
1372 }
1373 release_sock(sk);
1374 }
1375 ulen += sizeof(struct udphdr);
1376
4c9483b2 1377 memset(&fl6, 0, sizeof(fl6));
1da177e4
LT
1378
1379 if (sin6) {
1380 if (sin6->sin6_port == 0)
1381 return -EINVAL;
1382
1958b856 1383 fl6.fl6_dport = sin6->sin6_port;
1da177e4
LT
1384 daddr = &sin6->sin6_addr;
1385
1386 if (np->sndflow) {
4c9483b2
DM
1387 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1388 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1389 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
59c820b2 1390 if (IS_ERR(flowlabel))
1da177e4 1391 return -EINVAL;
1da177e4
LT
1392 }
1393 }
1394
1395 /*
1396 * Otherwise it will be difficult to maintain
1397 * sk->sk_dst_cache.
1398 */
1399 if (sk->sk_state == TCP_ESTABLISHED &&
efe4208f
ED
1400 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1401 daddr = &sk->sk_v6_daddr;
1da177e4
LT
1402
1403 if (addr_len >= sizeof(struct sockaddr_in6) &&
1404 sin6->sin6_scope_id &&
842df073 1405 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
4c9483b2 1406 fl6.flowi6_oif = sin6->sin6_scope_id;
1da177e4
LT
1407 } else {
1408 if (sk->sk_state != TCP_ESTABLISHED)
1409 return -EDESTADDRREQ;
1410
1958b856 1411 fl6.fl6_dport = inet->inet_dport;
efe4208f 1412 daddr = &sk->sk_v6_daddr;
4c9483b2 1413 fl6.flowlabel = np->flow_label;
9f542f61 1414 connected = true;
1da177e4
LT
1415 }
1416
4c9483b2
DM
1417 if (!fl6.flowi6_oif)
1418 fl6.flowi6_oif = sk->sk_bound_dev_if;
1da177e4 1419
4c9483b2
DM
1420 if (!fl6.flowi6_oif)
1421 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
9f690db7 1422
c6af0c22 1423 fl6.flowi6_mark = ipc6.sockc.mark;
e2d118a1 1424 fl6.flowi6_uid = sk->sk_uid;
51953d5b 1425
1da177e4
LT
1426 if (msg->msg_controllen) {
1427 opt = &opt_space;
1428 memset(opt, 0, sizeof(struct ipv6_txoptions));
1429 opt->tot_len = sizeof(*opt);
26879da5 1430 ipc6.opt = opt;
1da177e4 1431
2e8de857
WB
1432 err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
1433 if (err > 0)
1434 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
5fdaa88d 1435 &ipc6);
1da177e4
LT
1436 if (err < 0) {
1437 fl6_sock_release(flowlabel);
1438 return err;
1439 }
4c9483b2
DM
1440 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1441 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
59c820b2 1442 if (IS_ERR(flowlabel))
1da177e4
LT
1443 return -EINVAL;
1444 }
1445 if (!(opt->opt_nflen|opt->opt_flen))
1446 opt = NULL;
9f542f61 1447 connected = false;
1da177e4 1448 }
45f6fad8
ED
1449 if (!opt) {
1450 opt = txopt_get(np);
1451 opt_to_free = opt;
1452 }
df9890c3
YH
1453 if (flowlabel)
1454 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1455 opt = ipv6_fixup_options(&opt_space, opt);
26879da5 1456 ipc6.opt = opt;
1da177e4 1457
4c9483b2 1458 fl6.flowi6_proto = sk->sk_protocol;
e8e36984 1459 fl6.daddr = *daddr;
4c9483b2 1460 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
4e3fd7a0 1461 fl6.saddr = np->saddr;
1958b856 1462 fl6.fl6_sport = inet->inet_sport;
1ab1457c 1463
a9ed15da 1464 if (cgroup_bpf_enabled(BPF_CGROUP_UDP6_SENDMSG) && !connected) {
1cedee13
AI
1465 err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
1466 (struct sockaddr *)sin6, &fl6.saddr);
1467 if (err)
1468 goto out_no_dst;
1469 if (sin6) {
1470 if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
1471 /* BPF program rewrote IPv6-only by IPv4-mapped
1472 * IPv6. It's currently unsupported.
1473 */
1474 err = -ENOTSUPP;
1475 goto out_no_dst;
1476 }
1477 if (sin6->sin6_port == 0) {
1478 /* BPF program set invalid port. Reject it. */
1479 err = -EINVAL;
1480 goto out_no_dst;
1481 }
1482 fl6.fl6_dport = sin6->sin6_port;
1483 fl6.daddr = sin6->sin6_addr;
1484 }
1485 }
1486
e8e36984
AI
1487 if (ipv6_addr_any(&fl6.daddr))
1488 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1489
4c9483b2 1490 final_p = fl6_update_dst(&fl6, opt, &final);
20c59de2 1491 if (final_p)
9f542f61 1492 connected = false;
1da177e4 1493
4c9483b2
DM
1494 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1495 fl6.flowi6_oif = np->mcast_oif;
9f542f61 1496 connected = false;
c4062dfc
EH
1497 } else if (!fl6.flowi6_oif)
1498 fl6.flowi6_oif = np->ucast_oif;
1da177e4 1499
3df98d79 1500 security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
beb8d13b 1501
38b7097b
HFS
1502 if (ipc6.tclass < 0)
1503 ipc6.tclass = np->tclass;
1504
1505 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1506
4f858c56 1507 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
68d0c6d3
DM
1508 if (IS_ERR(dst)) {
1509 err = PTR_ERR(dst);
1510 dst = NULL;
1da177e4 1511 goto out;
14e50e57 1512 }
1da177e4 1513
26879da5
WW
1514 if (ipc6.hlimit < 0)
1515 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1da177e4
LT
1516
1517 if (msg->msg_flags&MSG_CONFIRM)
1518 goto do_confirm;
1519back_from_confirm:
1520
03485f2a
VY
1521 /* Lockless fast path for the non-corking case */
1522 if (!corkreq) {
1cd7884d 1523 struct inet_cork_full cork;
03485f2a
VY
1524 struct sk_buff *skb;
1525
1526 skb = ip6_make_skb(sk, getfrag, msg, ulen,
26879da5 1527 sizeof(struct udphdr), &ipc6,
03485f2a 1528 &fl6, (struct rt6_info *)dst,
5fdaa88d 1529 msg->msg_flags, &cork);
03485f2a
VY
1530 err = PTR_ERR(skb);
1531 if (!IS_ERR_OR_NULL(skb))
bec1f6f6 1532 err = udp_v6_send_skb(skb, &fl6, &cork.base);
4f858c56 1533 goto out;
03485f2a
VY
1534 }
1535
1da177e4
LT
1536 lock_sock(sk);
1537 if (unlikely(up->pending)) {
1538 /* The socket is already corked while preparing it. */
1539 /* ... which is an evident application bug. --ANK */
1540 release_sock(sk);
1541
ba7a46f1 1542 net_dbg_ratelimited("udp cork app bug 2\n");
1da177e4
LT
1543 err = -EINVAL;
1544 goto out;
1545 }
1546
1547 up->pending = AF_INET6;
1548
1549do_append_data:
26879da5
WW
1550 if (ipc6.dontfrag < 0)
1551 ipc6.dontfrag = np->dontfrag;
1da177e4 1552 up->len += ulen;
26879da5
WW
1553 err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1554 &ipc6, &fl6, (struct rt6_info *)dst,
5fdaa88d 1555 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1da177e4
LT
1556 if (err)
1557 udp_v6_flush_pending_frames(sk);
1558 else if (!corkreq)
4c0a6cb0 1559 err = udp_v6_push_pending_frames(sk);
1e0c14f4
HX
1560 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1561 up->pending = 0;
1da177e4 1562
03485f2a
VY
1563 if (err > 0)
1564 err = np->recverr ? net_xmit_errno(err) : 0;
1565 release_sock(sk);
1566
1da177e4 1567out:
a3c96089 1568 dst_release(dst);
1cedee13 1569out_no_dst:
1da177e4 1570 fl6_sock_release(flowlabel);
45f6fad8 1571 txopt_put(opt_to_free);
cd562c98 1572 if (!err)
1da177e4 1573 return len;
a18135eb
DM
1574 /*
1575 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1576 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1577 * we don't have a good statistic (IpOutDiscards but it can be too many
1578 * things). We could add another new stat but at least for now that
1579 * seems like overkill.
1580 */
1581 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
6aef70a8
ED
1582 UDP6_INC_STATS(sock_net(sk),
1583 UDP_MIB_SNDBUFERRORS, is_udplite);
a18135eb 1584 }
1da177e4
LT
1585 return err;
1586
1587do_confirm:
0dec879f
JA
1588 if (msg->msg_flags & MSG_PROBE)
1589 dst_confirm_neigh(dst, &fl6.daddr);
1da177e4
LT
1590 if (!(msg->msg_flags&MSG_PROBE) || len)
1591 goto back_from_confirm;
1592 err = 0;
1593 goto out;
1594}
1595
7d06b2e0 1596void udpv6_destroy_sock(struct sock *sk)
1da177e4 1597{
44046a59 1598 struct udp_sock *up = udp_sk(sk);
1da177e4
LT
1599 lock_sock(sk);
1600 udp_v6_flush_pending_frames(sk);
1601 release_sock(sk);
1602
60fb9567
PA
1603 if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1604 if (up->encap_type) {
1605 void (*encap_destroy)(struct sock *sk);
1606 encap_destroy = READ_ONCE(up->encap_destroy);
1607 if (encap_destroy)
1608 encap_destroy(sk);
1609 }
a4a600dd 1610 if (up->encap_enabled) {
9c480601 1611 static_branch_dec(&udpv6_encap_needed_key);
a4a600dd
XL
1612 udp_encap_disable();
1613 }
44046a59
TP
1614 }
1615
1da177e4 1616 inet6_destroy_sock(sk);
1da177e4
LT
1617}
1618
1619/*
1620 * Socket option code for UDP
1621 */
a7b75c5a
CH
1622int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1623 unsigned int optlen)
3fdadf7d 1624{
db8dac20 1625 if (level == SOL_UDP || level == SOL_UDPLITE)
91ac1cca 1626 return udp_lib_setsockopt(sk, level, optname,
a7b75c5a 1627 optval, optlen,
4c0a6cb0 1628 udp_v6_push_pending_frames);
ba4e58ec 1629 return ipv6_setsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1630}
1631
ba4e58ec
GR
1632int udpv6_getsockopt(struct sock *sk, int level, int optname,
1633 char __user *optval, int __user *optlen)
3fdadf7d 1634{
db8dac20 1635 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0 1636 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
ba4e58ec 1637 return ipv6_getsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1638}
1639
a8e3bb34
DA
1640/* thinking of making this const? Don't.
1641 * early_demux can change based on sysctl.
1642 */
dddb64bc 1643static struct inet6_protocol udpv6_protocol = {
5425077d 1644 .early_demux = udp_v6_early_demux,
dddb64bc 1645 .early_demux_handler = udp_v6_early_demux,
1da177e4
LT
1646 .handler = udpv6_rcv,
1647 .err_handler = udpv6_err,
1648 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1649};
1650
1651/* ------------------------------------------------------------------------ */
1652#ifdef CONFIG_PROC_FS
ba4e58ec 1653int udp6_seq_show(struct seq_file *seq, void *v)
1da177e4 1654{
17ef66af
LC
1655 if (v == SEQ_START_TOKEN) {
1656 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1657 } else {
1658 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1659 struct inet_sock *inet = inet_sk(v);
1660 __u16 srcp = ntohs(inet->inet_sport);
1661 __u16 destp = ntohs(inet->inet_dport);
6c206b20
PA
1662 __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
1663 udp_rqueue_get(v), bucket);
17ef66af 1664 }
1da177e4
LT
1665 return 0;
1666}
1667
c3506372 1668const struct seq_operations udp6_seq_ops = {
a3d2599b
CH
1669 .start = udp_seq_start,
1670 .next = udp_seq_next,
1671 .stop = udp_seq_stop,
1672 .show = udp6_seq_show,
1673};
c3506372 1674EXPORT_SYMBOL(udp6_seq_ops);
73cb88ec 1675
1da177e4 1676static struct udp_seq_afinfo udp6_seq_afinfo = {
1da177e4 1677 .family = AF_INET6,
645ca708 1678 .udp_table = &udp_table,
1da177e4
LT
1679};
1680
2c8c1e72 1681int __net_init udp6_proc_init(struct net *net)
1da177e4 1682{
c3506372
CH
1683 if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1684 sizeof(struct udp_iter_state), &udp6_seq_afinfo))
a3d2599b
CH
1685 return -ENOMEM;
1686 return 0;
1da177e4
LT
1687}
1688
ec120da6
IM
1689void udp6_proc_exit(struct net *net)
1690{
a3d2599b 1691 remove_proc_entry("udp6", net->proc_net);
1da177e4
LT
1692}
1693#endif /* CONFIG_PROC_FS */
1694
1695/* ------------------------------------------------------------------------ */
1696
1697struct proto udpv6_prot = {
1e802951
TZ
1698 .name = "UDPv6",
1699 .owner = THIS_MODULE,
1700 .close = udp_lib_close,
d74bad4e 1701 .pre_connect = udpv6_pre_connect,
1e802951
TZ
1702 .connect = ip6_datagram_connect,
1703 .disconnect = udp_disconnect,
1704 .ioctl = udp_ioctl,
1705 .init = udp_init_sock,
1706 .destroy = udpv6_destroy_sock,
1707 .setsockopt = udpv6_setsockopt,
1708 .getsockopt = udpv6_getsockopt,
1709 .sendmsg = udpv6_sendmsg,
1710 .recvmsg = udpv6_recvmsg,
1711 .release_cb = ip6_datagram_release_cb,
1712 .hash = udp_lib_hash,
1713 .unhash = udp_lib_unhash,
1714 .rehash = udp_v6_rehash,
1715 .get_port = udp_v6_get_port,
1716 .memory_allocated = &udp_memory_allocated,
1717 .sysctl_mem = sysctl_udp_mem,
1718 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
1719 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1720 .obj_size = sizeof(struct udp6_sock),
1721 .h.udp_table = &udp_table,
1e802951 1722 .diag_destroy = udp_abort,
1da177e4
LT
1723};
1724
1da177e4
LT
1725static struct inet_protosw udpv6_protosw = {
1726 .type = SOCK_DGRAM,
1727 .protocol = IPPROTO_UDP,
1728 .prot = &udpv6_prot,
1729 .ops = &inet6_dgram_ops,
1da177e4
LT
1730 .flags = INET_PROTOSW_PERMANENT,
1731};
1732
7f4e4868 1733int __init udpv6_init(void)
1da177e4 1734{
7f4e4868
DL
1735 int ret;
1736
3336288a
VY
1737 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1738 if (ret)
c6b641a4 1739 goto out;
3336288a 1740
7f4e4868
DL
1741 ret = inet6_register_protosw(&udpv6_protosw);
1742 if (ret)
1743 goto out_udpv6_protocol;
1744out:
1745 return ret;
1746
1747out_udpv6_protocol:
1748 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1749 goto out;
1750}
1751
09f7709f 1752void udpv6_exit(void)
7f4e4868
DL
1753{
1754 inet6_unregister_protosw(&udpv6_protosw);
1755 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1da177e4 1756}