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