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