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