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