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