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