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