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