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