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