1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * Copyright (c) 2010-2011 Juniper Networks, Inc. 6 * Copyright (c) 2014 Kevin Lo 7 * All rights reserved. 8 * 9 * Portions of this software were developed by Robert N. M. Watson under 10 * contract to Juniper Networks, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the project nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ 37 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ 38 */ 39 40 /*- 41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 42 * The Regents of the University of California. 43 * All rights reserved. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. Neither the name of the University nor the names of its contributors 54 * may be used to endorse or promote products derived from this software 55 * without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * SUCH DAMAGE. 68 */ 69 70 #include <sys/cdefs.h> 71 #include "opt_inet.h" 72 #include "opt_inet6.h" 73 #include "opt_ipsec.h" 74 #include "opt_route.h" 75 #include "opt_rss.h" 76 77 #include <sys/param.h> 78 #include <sys/domain.h> 79 #include <sys/jail.h> 80 #include <sys/kernel.h> 81 #include <sys/lock.h> 82 #include <sys/mbuf.h> 83 #include <sys/priv.h> 84 #include <sys/proc.h> 85 #include <sys/protosw.h> 86 #include <sys/sdt.h> 87 #include <sys/signalvar.h> 88 #include <sys/socket.h> 89 #include <sys/socketvar.h> 90 #include <sys/sx.h> 91 #include <sys/sysctl.h> 92 #include <sys/syslog.h> 93 #include <sys/systm.h> 94 95 #include <net/if.h> 96 #include <net/if_var.h> 97 #include <net/if_types.h> 98 #include <net/route.h> 99 #include <net/rss_config.h> 100 101 #include <netinet/in.h> 102 #include <netinet/in_kdtrace.h> 103 #include <netinet/in_pcb.h> 104 #include <netinet/in_systm.h> 105 #include <netinet/in_var.h> 106 #include <netinet/ip.h> 107 #include <netinet/ip6.h> 108 #include <netinet/icmp6.h> 109 #include <netinet/ip_var.h> 110 #include <netinet/udp.h> 111 #include <netinet/udp_var.h> 112 #include <netinet/udplite.h> 113 114 #include <netinet6/ip6_var.h> 115 #include <netinet6/in6_fib.h> 116 #include <netinet6/in6_pcb.h> 117 #include <netinet6/in6_rss.h> 118 #include <netinet6/udp6_var.h> 119 #include <netinet6/scope6_var.h> 120 121 #include <netipsec/ipsec_support.h> 122 123 #include <security/mac/mac_framework.h> 124 125 VNET_DEFINE(int, zero_checksum_port) = 0; 126 #define V_zero_checksum_port VNET(zero_checksum_port) 127 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW, 128 &VNET_NAME(zero_checksum_port), 0, 129 "Zero UDP checksum allowed for traffic to/from this port."); 130 131 /* 132 * UDP protocol implementation. 133 * Per RFC 768, August, 1980. 134 */ 135 136 static void udp6_detach(struct socket *so); 137 138 static int 139 udp6_append(struct inpcb *inp, struct mbuf *n, int off, 140 struct sockaddr_in6 *fromsa) 141 { 142 struct socket *so; 143 struct mbuf *opts = NULL, *tmp_opts; 144 struct udpcb *up; 145 bool filtered; 146 147 INP_LOCK_ASSERT(inp); 148 149 /* 150 * Engage the tunneling protocol. 151 */ 152 up = intoudpcb(inp); 153 if (up->u_tun_func != NULL) { 154 in_pcbref(inp); 155 INP_RUNLOCK(inp); 156 filtered = (*up->u_tun_func)(n, off, inp, 157 (struct sockaddr *)&fromsa[0], up->u_tun_ctx); 158 INP_RLOCK(inp); 159 if (filtered) 160 return (in_pcbrele_rlocked(inp)); 161 } 162 163 off += sizeof(struct udphdr); 164 165 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 166 /* Check AH/ESP integrity. */ 167 if (IPSEC_ENABLED(ipv6)) { 168 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) { 169 m_freem(n); 170 return (0); 171 } 172 173 /* IPSec UDP encaps. */ 174 if ((up->u_flags & UF_ESPINUDP) != 0 && 175 UDPENCAP_INPUT(ipv6, n, off, AF_INET6) != 0) { 176 return (0); /* Consumed. */ 177 } 178 } 179 #endif /* IPSEC */ 180 #ifdef MAC 181 if (mac_inpcb_check_deliver(inp, n) != 0) { 182 m_freem(n); 183 return (0); 184 } 185 #endif 186 opts = NULL; 187 if (inp->inp_flags & INP_CONTROLOPTS || 188 inp->inp_socket->so_options & SO_TIMESTAMP) 189 ip6_savecontrol(inp, n, &opts); 190 if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) { 191 tmp_opts = sbcreatecontrol(&fromsa[1], 192 sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR, 193 IPPROTO_IPV6, M_NOWAIT); 194 if (tmp_opts) { 195 if (opts) { 196 tmp_opts->m_next = opts; 197 opts = tmp_opts; 198 } else 199 opts = tmp_opts; 200 } 201 } 202 m_adj(n, off); 203 204 so = inp->inp_socket; 205 SOCKBUF_LOCK(&so->so_rcv); 206 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n, 207 opts) == 0) { 208 soroverflow_locked(so); 209 m_freem(n); 210 if (opts) 211 m_freem(opts); 212 UDPSTAT_INC(udps_fullsock); 213 } else 214 sorwakeup_locked(so); 215 return (0); 216 } 217 218 struct udp6_multi_match_ctx { 219 struct ip6_hdr *ip6; 220 struct udphdr *uh; 221 }; 222 223 static bool 224 udp6_multi_match(const struct inpcb *inp, void *v) 225 { 226 struct udp6_multi_match_ctx *ctx = v; 227 228 if ((inp->inp_vflag & INP_IPV6) == 0) 229 return(false); 230 if (inp->inp_lport != ctx->uh->uh_dport) 231 return(false); 232 if (inp->inp_fport != 0 && inp->inp_fport != ctx->uh->uh_sport) 233 return(false); 234 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 235 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ctx->ip6->ip6_dst)) 236 return (false); 237 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 238 (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ctx->ip6->ip6_src) || 239 inp->inp_fport != ctx->uh->uh_sport)) 240 return (false); 241 242 return (true); 243 } 244 245 static int 246 udp6_multi_input(struct mbuf *m, int off, int proto, 247 struct sockaddr_in6 *fromsa) 248 { 249 struct udp6_multi_match_ctx ctx; 250 struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto), 251 INPLOOKUP_RLOCKPCB, udp6_multi_match, &ctx); 252 struct inpcb *inp; 253 struct ip6_moptions *imo; 254 struct mbuf *n; 255 int appends = 0; 256 257 /* 258 * In the event that laddr should be set to the link-local 259 * address (this happens in RIPng), the multicast address 260 * specified in the received packet will not match laddr. To 261 * handle this situation, matching is relaxed if the 262 * receiving interface is the same as one specified in the 263 * socket and if the destination multicast address matches 264 * one of the multicast groups specified in the socket. 265 */ 266 267 /* 268 * KAME note: traditionally we dropped udpiphdr from mbuf 269 * here. We need udphdr for IPsec processing so we do that 270 * later. 271 */ 272 ctx.ip6 = mtod(m, struct ip6_hdr *); 273 ctx.uh = (struct udphdr *)((char *)ctx.ip6 + off); 274 while ((inp = inp_next(&inpi)) != NULL) { 275 INP_RLOCK_ASSERT(inp); 276 /* 277 * XXXRW: Because we weren't holding either the inpcb 278 * or the hash lock when we checked for a match 279 * before, we should probably recheck now that the 280 * inpcb lock is (supposed to be) held. 281 */ 282 /* 283 * Handle socket delivery policy for any-source 284 * and source-specific multicast. [RFC3678] 285 */ 286 if ((imo = inp->in6p_moptions) != NULL) { 287 struct sockaddr_in6 mcaddr; 288 int blocked; 289 290 bzero(&mcaddr, sizeof(struct sockaddr_in6)); 291 mcaddr.sin6_len = sizeof(struct sockaddr_in6); 292 mcaddr.sin6_family = AF_INET6; 293 mcaddr.sin6_addr = ctx.ip6->ip6_dst; 294 295 blocked = im6o_mc_filter(imo, m->m_pkthdr.rcvif, 296 (struct sockaddr *)&mcaddr, 297 (struct sockaddr *)&fromsa[0]); 298 if (blocked != MCAST_PASS) { 299 if (blocked == MCAST_NOTGMEMBER) 300 IP6STAT_INC(ip6s_notmember); 301 if (blocked == MCAST_NOTSMEMBER || 302 blocked == MCAST_MUTED) 303 UDPSTAT_INC(udps_filtermcast); 304 continue; 305 } 306 } 307 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) { 308 if (proto == IPPROTO_UDPLITE) 309 UDPLITE_PROBE(receive, NULL, inp, ctx.ip6, 310 inp, ctx.uh); 311 else 312 UDP_PROBE(receive, NULL, inp, ctx.ip6, inp, 313 ctx.uh); 314 if (udp6_append(inp, n, off, fromsa)) { 315 break; 316 } else 317 appends++; 318 } 319 /* 320 * Don't look for additional matches if this one does 321 * not have either the SO_REUSEPORT or SO_REUSEADDR 322 * socket options set. This heuristic avoids 323 * searching through all pcbs in the common case of a 324 * non-shared port. It assumes that an application 325 * will never clear these options after setting them. 326 */ 327 if ((inp->inp_socket->so_options & 328 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) { 329 INP_RUNLOCK(inp); 330 break; 331 } 332 } 333 m_freem(m); 334 335 if (appends == 0) { 336 /* 337 * No matching pcb found; discard datagram. (No need 338 * to send an ICMP Port Unreachable for a broadcast 339 * or multicast datgram.) 340 */ 341 UDPSTAT_INC(udps_noport); 342 UDPSTAT_INC(udps_noportmcast); 343 } 344 345 return (IPPROTO_DONE); 346 } 347 348 int 349 udp6_input(struct mbuf **mp, int *offp, int proto) 350 { 351 struct mbuf *m = *mp; 352 struct ip6_hdr *ip6; 353 struct udphdr *uh; 354 struct inpcb *inp; 355 struct inpcbinfo *pcbinfo; 356 struct udpcb *up; 357 int off = *offp; 358 int cscov_partial; 359 int plen, ulen; 360 int lookupflags; 361 struct sockaddr_in6 fromsa[2]; 362 struct m_tag *fwd_tag; 363 uint16_t uh_sum; 364 uint8_t nxt; 365 366 NET_EPOCH_ASSERT(); 367 368 if (m->m_len < off + sizeof(struct udphdr)) { 369 m = m_pullup(m, off + sizeof(struct udphdr)); 370 if (m == NULL) { 371 IP6STAT_INC(ip6s_exthdrtoolong); 372 *mp = NULL; 373 return (IPPROTO_DONE); 374 } 375 } 376 ip6 = mtod(m, struct ip6_hdr *); 377 uh = (struct udphdr *)((caddr_t)ip6 + off); 378 379 UDPSTAT_INC(udps_ipackets); 380 381 /* 382 * Destination port of 0 is illegal, based on RFC768. 383 */ 384 if (uh->uh_dport == 0) 385 goto badunlocked; 386 387 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 388 ulen = ntohs((u_short)uh->uh_ulen); 389 390 nxt = proto; 391 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0; 392 if (nxt == IPPROTO_UDPLITE) { 393 /* Zero means checksum over the complete packet. */ 394 if (ulen == 0) 395 ulen = plen; 396 if (ulen == plen) 397 cscov_partial = 0; 398 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) { 399 /* XXX: What is the right UDPLite MIB counter? */ 400 goto badunlocked; 401 } 402 if (uh->uh_sum == 0) { 403 /* XXX: What is the right UDPLite MIB counter? */ 404 goto badunlocked; 405 } 406 } else { 407 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) { 408 UDPSTAT_INC(udps_badlen); 409 goto badunlocked; 410 } 411 if (uh->uh_sum == 0) { 412 UDPSTAT_INC(udps_nosum); 413 /* 414 * dport 0 was rejected earlier so this is OK even if 415 * zero_checksum_port is 0 (which is its default value). 416 */ 417 if (ntohs(uh->uh_dport) == V_zero_checksum_port) 418 goto skip_checksum; 419 else 420 goto badunlocked; 421 } 422 } 423 424 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) && 425 !cscov_partial) { 426 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 427 uh_sum = m->m_pkthdr.csum_data; 428 else 429 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt, 430 m->m_pkthdr.csum_data); 431 uh_sum ^= 0xffff; 432 } else 433 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen); 434 435 if (uh_sum != 0) { 436 UDPSTAT_INC(udps_badsum); 437 goto badunlocked; 438 } 439 440 skip_checksum: 441 /* 442 * Construct sockaddr format source address. 443 */ 444 init_sin6(&fromsa[0], m, 0); 445 fromsa[0].sin6_port = uh->uh_sport; 446 init_sin6(&fromsa[1], m, 1); 447 fromsa[1].sin6_port = uh->uh_dport; 448 449 pcbinfo = udp_get_inpcbinfo(nxt); 450 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 451 *mp = NULL; 452 return (udp6_multi_input(m, off, proto, fromsa)); 453 } 454 455 /* 456 * Locate pcb for datagram. 457 */ 458 lookupflags = INPLOOKUP_RLOCKPCB | 459 (V_udp_bind_all_fibs ? 0 : INPLOOKUP_FIB); 460 461 /* 462 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 463 */ 464 if ((m->m_flags & M_IP6_NEXTHOP) && 465 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 466 struct sockaddr_in6 *next_hop6; 467 468 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 469 470 /* 471 * Transparently forwarded. Pretend to be the destination. 472 * Already got one like this? 473 */ 474 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src, 475 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport, 476 lookupflags, m->m_pkthdr.rcvif, m); 477 if (!inp) { 478 /* 479 * It's new. Try to find the ambushing socket. 480 * Because we've rewritten the destination address, 481 * any hardware-generated hash is ignored. 482 */ 483 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src, 484 uh->uh_sport, &next_hop6->sin6_addr, 485 next_hop6->sin6_port ? htons(next_hop6->sin6_port) : 486 uh->uh_dport, INPLOOKUP_WILDCARD | lookupflags, 487 m->m_pkthdr.rcvif); 488 } 489 /* Remove the tag from the packet. We don't need it anymore. */ 490 m_tag_delete(m, fwd_tag); 491 m->m_flags &= ~M_IP6_NEXTHOP; 492 } else 493 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src, 494 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport, 495 INPLOOKUP_WILDCARD | lookupflags, 496 m->m_pkthdr.rcvif, m); 497 if (inp == NULL) { 498 if (V_udp_log_in_vain) { 499 char ip6bufs[INET6_ADDRSTRLEN]; 500 char ip6bufd[INET6_ADDRSTRLEN]; 501 502 log(LOG_INFO, 503 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 504 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 505 ntohs(uh->uh_dport), 506 ip6_sprintf(ip6bufs, &ip6->ip6_src), 507 ntohs(uh->uh_sport)); 508 } 509 if (nxt == IPPROTO_UDPLITE) 510 UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh); 511 else 512 UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh); 513 UDPSTAT_INC(udps_noport); 514 if (m->m_flags & M_MCAST) { 515 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 516 UDPSTAT_INC(udps_noportmcast); 517 goto badunlocked; 518 } 519 if (V_udp_blackhole && (V_udp_blackhole_local || 520 !in6_localip(&ip6->ip6_src))) 521 goto badunlocked; 522 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 523 *mp = NULL; 524 return (IPPROTO_DONE); 525 } 526 INP_RLOCK_ASSERT(inp); 527 up = intoudpcb(inp); 528 if (cscov_partial) { 529 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) { 530 INP_RUNLOCK(inp); 531 m_freem(m); 532 *mp = NULL; 533 return (IPPROTO_DONE); 534 } 535 } 536 if (nxt == IPPROTO_UDPLITE) 537 UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh); 538 else 539 UDP_PROBE(receive, NULL, inp, ip6, inp, uh); 540 if (udp6_append(inp, m, off, fromsa) == 0) 541 INP_RUNLOCK(inp); 542 *mp = NULL; 543 return (IPPROTO_DONE); 544 545 badunlocked: 546 m_freem(m); 547 *mp = NULL; 548 return (IPPROTO_DONE); 549 } 550 551 static void 552 udp6_common_ctlinput(struct ip6ctlparam *ip6cp, struct inpcbinfo *pcbinfo) 553 { 554 struct udphdr uh; 555 struct ip6_hdr *ip6; 556 struct mbuf *m; 557 struct inpcb *inp; 558 int errno, off = 0; 559 struct udp_portonly { 560 u_int16_t uh_sport; 561 u_int16_t uh_dport; 562 } *uhp; 563 564 if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) == 0) 565 return; 566 567 m = ip6cp->ip6c_m; 568 ip6 = ip6cp->ip6c_ip6; 569 off = ip6cp->ip6c_off; 570 571 /* Check if we can safely examine src and dst ports. */ 572 if (m->m_pkthdr.len < off + sizeof(*uhp)) 573 return; 574 575 bzero(&uh, sizeof(uh)); 576 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 577 578 /* Check to see if its tunneled */ 579 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst, uh.uh_dport, 580 &ip6->ip6_src, uh.uh_sport, INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, 581 m->m_pkthdr.rcvif, m); 582 if (inp != NULL) { 583 struct udpcb *up; 584 udp_tun_icmp_t *func; 585 586 up = intoudpcb(inp); 587 func = up->u_icmp_func; 588 INP_RUNLOCK(inp); 589 if (func != NULL) 590 func(ip6cp); 591 } 592 in6_pcbnotify(pcbinfo, ip6cp->ip6c_finaldst, uh.uh_dport, 593 ip6cp->ip6c_src, uh.uh_sport, errno, ip6cp->ip6c_cmdarg, 594 udp_notify); 595 } 596 597 static void 598 udp6_ctlinput(struct ip6ctlparam *ctl) 599 { 600 601 return (udp6_common_ctlinput(ctl, &V_udbinfo)); 602 } 603 604 static void 605 udplite6_ctlinput(struct ip6ctlparam *ctl) 606 { 607 608 return (udp6_common_ctlinput(ctl, &V_ulitecbinfo)); 609 } 610 611 static int 612 udp6_getcred(SYSCTL_HANDLER_ARGS) 613 { 614 struct xucred xuc; 615 struct sockaddr_in6 addrs[2]; 616 struct epoch_tracker et; 617 struct inpcb *inp; 618 int error; 619 620 if (req->newptr == NULL) 621 return (EINVAL); 622 error = priv_check(req->td, PRIV_NETINET_GETCRED); 623 if (error) 624 return (error); 625 626 if (req->newlen != sizeof(addrs)) 627 return (EINVAL); 628 if (req->oldlen != sizeof(struct xucred)) 629 return (EINVAL); 630 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 631 if (error) 632 return (error); 633 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 || 634 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) { 635 return (error); 636 } 637 NET_EPOCH_ENTER(et); 638 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr, 639 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 640 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 641 NET_EPOCH_EXIT(et); 642 if (inp != NULL) { 643 INP_RLOCK_ASSERT(inp); 644 if (inp->inp_socket == NULL) 645 error = ENOENT; 646 if (error == 0) 647 error = cr_canseesocket(req->td->td_ucred, 648 inp->inp_socket); 649 if (error == 0) 650 cru2x(inp->inp_cred, &xuc); 651 INP_RUNLOCK(inp); 652 } else 653 error = ENOENT; 654 if (error == 0) 655 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 656 return (error); 657 } 658 659 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, 660 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE, 661 0, 0, udp6_getcred, "S,xucred", 662 "Get the xucred of a UDP6 connection"); 663 664 static int 665 udp6_send(struct socket *so, int flags_arg, struct mbuf *m, 666 struct sockaddr *addr6, struct mbuf *control, struct thread *td) 667 { 668 struct inpcb *inp; 669 struct ip6_hdr *ip6; 670 struct udphdr *udp6; 671 struct in6_addr *laddr, *faddr, in6a; 672 struct ip6_pktopts *optp, opt; 673 struct sockaddr_in6 *sin6, tmp; 674 struct epoch_tracker et; 675 int cscov_partial, error, flags, hlen, scope_ambiguous; 676 u_int32_t ulen, plen; 677 uint16_t cscov; 678 u_short fport; 679 uint8_t nxt; 680 681 if (addr6) { 682 error = 0; 683 if (addr6->sa_family != AF_INET6) 684 error = EAFNOSUPPORT; 685 else if (addr6->sa_len != sizeof(struct sockaddr_in6)) 686 error = EINVAL; 687 if (__predict_false(error != 0)) { 688 m_freem(control); 689 m_freem(m); 690 return (error); 691 } 692 } 693 694 sin6 = (struct sockaddr_in6 *)addr6; 695 696 /* 697 * In contrast to IPv4 we do not validate the max. packet length 698 * here due to IPv6 Jumbograms (RFC2675). 699 */ 700 701 scope_ambiguous = 0; 702 if (sin6) { 703 /* Protect *addr6 from overwrites. */ 704 tmp = *sin6; 705 sin6 = &tmp; 706 707 /* 708 * Application should provide a proper zone ID or the use of 709 * default zone IDs should be enabled. Unfortunately, some 710 * applications do not behave as it should, so we need a 711 * workaround. Even if an appropriate ID is not determined, 712 * we'll see if we can determine the outgoing interface. If we 713 * can, determine the zone ID based on the interface below. 714 */ 715 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 716 scope_ambiguous = 1; 717 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) { 718 if (control) 719 m_freem(control); 720 m_freem(m); 721 return (error); 722 } 723 } 724 725 inp = sotoinpcb(so); 726 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 727 /* 728 * In the following cases we want a write lock on the inp for either 729 * local operations or for possible route cache updates in the IPv6 730 * output path: 731 * - on connected sockets (sin6 is NULL) for route cache updates, 732 * - when we are not bound to an address and source port (it is 733 * in6_pcbsetport() which will require the write lock). 734 * 735 * We check the inp fields before actually locking the inp, so 736 * here exists a race, and we may WLOCK the inp and end with already 737 * bound one by other thread. This is fine. 738 */ 739 if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 740 inp->inp_lport == 0)) 741 INP_WLOCK(inp); 742 else 743 INP_RLOCK(inp); 744 745 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ? 746 IPPROTO_UDP : IPPROTO_UDPLITE; 747 748 #ifdef INET 749 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 750 int hasv4addr; 751 752 if (sin6 == NULL) 753 hasv4addr = (inp->inp_vflag & INP_IPV4); 754 else 755 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 756 ? 1 : 0; 757 if (hasv4addr) { 758 /* 759 * XXXRW: We release UDP-layer locks before calling 760 * udp_send() in order to avoid recursion. However, 761 * this does mean there is a short window where inp's 762 * fields are unstable. Could this lead to a 763 * potential race in which the factors causing us to 764 * select the UDPv4 output routine are invalidated? 765 */ 766 INP_UNLOCK(inp); 767 if (sin6) 768 in6_sin6_2_sin_in_sock((struct sockaddr *)sin6); 769 /* addr will just be freed in sendit(). */ 770 return (udp_send(so, flags_arg | PRUS_IPV6, m, 771 (struct sockaddr *)sin6, control, td)); 772 } 773 } else 774 #endif 775 if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 776 /* 777 * Given this is either an IPv6-only socket or no INET is 778 * supported we will fail the send if the given destination 779 * address is a v4mapped address. 780 */ 781 INP_UNLOCK(inp); 782 m_freem(m); 783 m_freem(control); 784 return (EINVAL); 785 } 786 787 NET_EPOCH_ENTER(et); 788 if (control) { 789 if ((error = ip6_setpktopts(control, &opt, 790 inp->in6p_outputopts, td->td_ucred, nxt)) != 0) { 791 goto release; 792 } 793 optp = &opt; 794 } else 795 optp = inp->in6p_outputopts; 796 797 if (sin6) { 798 /* 799 * Since we saw no essential reason for calling in_pcbconnect, 800 * we get rid of such kind of logic, and call in6_selectsrc 801 * and in6_pcbsetport in order to fill in the local address 802 * and the local port. 803 */ 804 if (sin6->sin6_port == 0) { 805 error = EADDRNOTAVAIL; 806 goto release; 807 } 808 809 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 810 /* how about ::ffff:0.0.0.0 case? */ 811 error = EISCONN; 812 goto release; 813 } 814 815 /* 816 * Given we handle the v4mapped case in the INET block above 817 * assert here that it must not happen anymore. 818 */ 819 KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr), 820 ("%s: sin6(%p)->sin6_addr is v4mapped which we " 821 "should have handled.", __func__, sin6)); 822 823 /* This only requires read-locking. */ 824 error = in6_selectsrc_socket(sin6, optp, inp, 825 td->td_ucred, scope_ambiguous, &in6a, NULL); 826 if (error) 827 goto release; 828 laddr = &in6a; 829 830 if (inp->inp_lport == 0) { 831 struct inpcbinfo *pcbinfo; 832 833 INP_WLOCK_ASSERT(inp); 834 835 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 836 INP_HASH_WLOCK(pcbinfo); 837 error = in6_pcbsetport(laddr, inp, td->td_ucred); 838 INP_HASH_WUNLOCK(pcbinfo); 839 if (error != 0) { 840 /* Undo an address bind that may have occurred. */ 841 inp->in6p_laddr = in6addr_any; 842 goto release; 843 } 844 } 845 faddr = &sin6->sin6_addr; 846 fport = sin6->sin6_port; /* allow 0 port */ 847 848 } else { 849 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 850 error = ENOTCONN; 851 goto release; 852 } 853 laddr = &inp->in6p_laddr; 854 faddr = &inp->in6p_faddr; 855 fport = inp->inp_fport; 856 } 857 858 ulen = m->m_pkthdr.len; 859 plen = sizeof(struct udphdr) + ulen; 860 hlen = sizeof(struct ip6_hdr); 861 862 /* 863 * Calculate data length and get a mbuf for UDP, IP6, and possible 864 * link-layer headers. Immediate slide the data pointer back forward 865 * since we won't use that space at this layer. 866 */ 867 M_PREPEND(m, hlen + sizeof(struct udphdr) + max_linkhdr, M_NOWAIT); 868 if (m == NULL) { 869 error = ENOBUFS; 870 goto release; 871 } 872 m->m_data += max_linkhdr; 873 m->m_len -= max_linkhdr; 874 m->m_pkthdr.len -= max_linkhdr; 875 876 /* 877 * Stuff checksum and output datagram. 878 */ 879 cscov = cscov_partial = 0; 880 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 881 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */ 882 udp6->uh_dport = fport; 883 if (nxt == IPPROTO_UDPLITE) { 884 struct udpcb *up; 885 886 up = intoudpcb(inp); 887 cscov = up->u_txcslen; 888 if (cscov >= plen) 889 cscov = 0; 890 udp6->uh_ulen = htons(cscov); 891 /* 892 * For UDP-Lite, checksum coverage length of zero means 893 * the entire UDPLite packet is covered by the checksum. 894 */ 895 cscov_partial = (cscov == 0) ? 0 : 1; 896 } else if (plen <= 0xffff) 897 udp6->uh_ulen = htons((u_short)plen); 898 else 899 udp6->uh_ulen = 0; 900 udp6->uh_sum = 0; 901 902 ip6 = mtod(m, struct ip6_hdr *); 903 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK; 904 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 905 ip6->ip6_vfc |= IPV6_VERSION; 906 ip6->ip6_plen = htons((u_short)plen); 907 ip6->ip6_nxt = nxt; 908 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 909 ip6->ip6_src = *laddr; 910 ip6->ip6_dst = *faddr; 911 912 #ifdef MAC 913 mac_inpcb_create_mbuf(inp, m); 914 #endif 915 916 if (cscov_partial) { 917 if ((udp6->uh_sum = in6_cksum_partial(m, nxt, 918 sizeof(struct ip6_hdr), plen, cscov)) == 0) 919 udp6->uh_sum = 0xffff; 920 } else { 921 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0); 922 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 923 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 924 } 925 926 flags = 0; 927 #if defined(ROUTE_MPATH) || defined(RSS) 928 if (CALC_FLOWID_OUTBOUND_SENDTO) { 929 uint32_t hash_type, hash_val; 930 uint8_t pr; 931 932 pr = inp->inp_socket->so_proto->pr_protocol; 933 934 hash_val = fib6_calc_packet_hash(laddr, faddr, 935 inp->inp_lport, fport, pr, &hash_type); 936 m->m_pkthdr.flowid = hash_val; 937 M_HASHTYPE_SET(m, hash_type); 938 } 939 /* do not use inp flowid */ 940 flags |= IP_NODEFAULTFLOWID; 941 #endif 942 943 UDPSTAT_INC(udps_opackets); 944 if (nxt == IPPROTO_UDPLITE) 945 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6); 946 else 947 UDP_PROBE(send, NULL, inp, ip6, inp, udp6); 948 error = ip6_output(m, optp, 949 INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags, 950 inp->in6p_moptions, NULL, inp); 951 INP_UNLOCK(inp); 952 NET_EPOCH_EXIT(et); 953 954 if (control) { 955 ip6_clearpktopts(&opt, -1); 956 m_freem(control); 957 } 958 return (error); 959 960 release: 961 INP_UNLOCK(inp); 962 NET_EPOCH_EXIT(et); 963 if (control) { 964 ip6_clearpktopts(&opt, -1); 965 m_freem(control); 966 } 967 m_freem(m); 968 969 return (error); 970 } 971 972 static void 973 udp6_abort(struct socket *so) 974 { 975 struct inpcb *inp; 976 struct inpcbinfo *pcbinfo; 977 978 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 979 inp = sotoinpcb(so); 980 KASSERT(inp != NULL, ("udp6_abort: inp == NULL")); 981 982 INP_WLOCK(inp); 983 #ifdef INET 984 if (inp->inp_vflag & INP_IPV4) { 985 INP_WUNLOCK(inp); 986 udp_abort(so); 987 return; 988 } 989 #endif 990 991 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 992 INP_HASH_WLOCK(pcbinfo); 993 in6_pcbdisconnect(inp); 994 INP_HASH_WUNLOCK(pcbinfo); 995 soisdisconnected(so); 996 } 997 INP_WUNLOCK(inp); 998 } 999 1000 static int 1001 udp6_attach(struct socket *so, int proto, struct thread *td) 1002 { 1003 struct inpcbinfo *pcbinfo; 1004 struct inpcb *inp; 1005 struct udpcb *up; 1006 int error; 1007 1008 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1009 inp = sotoinpcb(so); 1010 KASSERT(inp == NULL, ("udp6_attach: inp != NULL")); 1011 1012 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1013 error = soreserve(so, udp_sendspace, udp_recvspace); 1014 if (error) 1015 return (error); 1016 } 1017 error = in_pcballoc(so, pcbinfo); 1018 if (error) 1019 return (error); 1020 inp = (struct inpcb *)so->so_pcb; 1021 inp->in6p_cksum = -1; /* just to be sure */ 1022 /* 1023 * XXX: ugly!! 1024 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 1025 * because the socket may be bound to an IPv6 wildcard address, 1026 * which may match an IPv4-mapped IPv6 address. 1027 */ 1028 inp->inp_ip_ttl = V_ip_defttl; 1029 up = intoudpcb(inp); 1030 bzero(&up->u_start_zero, u_zero_size); 1031 INP_WUNLOCK(inp); 1032 return (0); 1033 } 1034 1035 static int 1036 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1037 { 1038 struct sockaddr_in6 *sin6_p; 1039 struct inpcb *inp; 1040 struct inpcbinfo *pcbinfo; 1041 int error; 1042 u_char vflagsav; 1043 1044 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1045 inp = sotoinpcb(so); 1046 KASSERT(inp != NULL, ("udp6_bind: inp == NULL")); 1047 1048 if (nam->sa_family != AF_INET6) 1049 return (EAFNOSUPPORT); 1050 if (nam->sa_len != sizeof(struct sockaddr_in6)) 1051 return (EINVAL); 1052 1053 sin6_p = (struct sockaddr_in6 *)nam; 1054 1055 INP_WLOCK(inp); 1056 INP_HASH_WLOCK(pcbinfo); 1057 vflagsav = inp->inp_vflag; 1058 inp->inp_vflag &= ~INP_IPV4; 1059 inp->inp_vflag |= INP_IPV6; 1060 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 1061 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 1062 inp->inp_vflag |= INP_IPV4; 1063 #ifdef INET 1064 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 1065 struct sockaddr_in sin; 1066 1067 in6_sin6_2_sin(&sin, sin6_p); 1068 inp->inp_vflag |= INP_IPV4; 1069 inp->inp_vflag &= ~INP_IPV6; 1070 error = in_pcbbind(inp, &sin, 1071 V_udp_bind_all_fibs ? 0 : INPBIND_FIB, 1072 td->td_ucred); 1073 goto out; 1074 } 1075 #endif 1076 } 1077 1078 error = in6_pcbbind(inp, sin6_p, V_udp_bind_all_fibs ? 0 : INPBIND_FIB, 1079 td->td_ucred); 1080 #ifdef INET 1081 out: 1082 #endif 1083 if (error != 0) 1084 inp->inp_vflag = vflagsav; 1085 INP_HASH_WUNLOCK(pcbinfo); 1086 INP_WUNLOCK(inp); 1087 return (error); 1088 } 1089 1090 static void 1091 udp6_close(struct socket *so) 1092 { 1093 struct inpcb *inp; 1094 struct inpcbinfo *pcbinfo; 1095 1096 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1097 inp = sotoinpcb(so); 1098 KASSERT(inp != NULL, ("udp6_close: inp == NULL")); 1099 1100 INP_WLOCK(inp); 1101 #ifdef INET 1102 if (inp->inp_vflag & INP_IPV4) { 1103 INP_WUNLOCK(inp); 1104 (void)udp_disconnect(so); 1105 return; 1106 } 1107 #endif 1108 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1109 INP_HASH_WLOCK(pcbinfo); 1110 in6_pcbdisconnect(inp); 1111 INP_HASH_WUNLOCK(pcbinfo); 1112 soisdisconnected(so); 1113 } 1114 INP_WUNLOCK(inp); 1115 } 1116 1117 static int 1118 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1119 { 1120 struct epoch_tracker et; 1121 struct inpcb *inp; 1122 struct inpcbinfo *pcbinfo; 1123 struct sockaddr_in6 *sin6; 1124 int error; 1125 u_char vflagsav; 1126 1127 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1128 inp = sotoinpcb(so); 1129 KASSERT(inp != NULL, ("udp6_connect: inp == NULL")); 1130 1131 sin6 = (struct sockaddr_in6 *)nam; 1132 if (sin6->sin6_family != AF_INET6) 1133 return (EAFNOSUPPORT); 1134 if (sin6->sin6_len != sizeof(*sin6)) 1135 return (EINVAL); 1136 1137 /* 1138 * XXXRW: Need to clarify locking of v4/v6 flags. 1139 */ 1140 INP_WLOCK(inp); 1141 #ifdef INET 1142 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1143 struct sockaddr_in sin; 1144 1145 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1146 error = EINVAL; 1147 goto out; 1148 } 1149 if ((inp->inp_vflag & INP_IPV4) == 0) { 1150 error = EAFNOSUPPORT; 1151 goto out; 1152 } 1153 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1154 error = EISCONN; 1155 goto out; 1156 } 1157 in6_sin6_2_sin(&sin, sin6); 1158 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr); 1159 if (error != 0) 1160 goto out; 1161 vflagsav = inp->inp_vflag; 1162 inp->inp_vflag |= INP_IPV4; 1163 inp->inp_vflag &= ~INP_IPV6; 1164 NET_EPOCH_ENTER(et); 1165 INP_HASH_WLOCK(pcbinfo); 1166 error = in_pcbconnect(inp, &sin, td->td_ucred); 1167 INP_HASH_WUNLOCK(pcbinfo); 1168 NET_EPOCH_EXIT(et); 1169 /* 1170 * If connect succeeds, mark socket as connected. If 1171 * connect fails and socket is unbound, reset inp_vflag 1172 * field. 1173 */ 1174 if (error == 0) 1175 soisconnected(so); 1176 else if (inp->inp_laddr.s_addr == INADDR_ANY && 1177 inp->inp_lport == 0) 1178 inp->inp_vflag = vflagsav; 1179 goto out; 1180 } else { 1181 if ((inp->inp_vflag & INP_IPV6) == 0) { 1182 error = EAFNOSUPPORT; 1183 goto out; 1184 } 1185 } 1186 #endif 1187 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1188 error = EISCONN; 1189 goto out; 1190 } 1191 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr); 1192 if (error != 0) 1193 goto out; 1194 vflagsav = inp->inp_vflag; 1195 inp->inp_vflag &= ~INP_IPV4; 1196 inp->inp_vflag |= INP_IPV6; 1197 NET_EPOCH_ENTER(et); 1198 INP_HASH_WLOCK(pcbinfo); 1199 error = in6_pcbconnect(inp, sin6, td->td_ucred, true); 1200 INP_HASH_WUNLOCK(pcbinfo); 1201 NET_EPOCH_EXIT(et); 1202 /* 1203 * If connect succeeds, mark socket as connected. If 1204 * connect fails and socket is unbound, reset inp_vflag 1205 * field. 1206 */ 1207 if (error == 0) 1208 soisconnected(so); 1209 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 1210 inp->inp_lport == 0) 1211 inp->inp_vflag = vflagsav; 1212 out: 1213 INP_WUNLOCK(inp); 1214 return (error); 1215 } 1216 1217 static void 1218 udp6_detach(struct socket *so) 1219 { 1220 struct inpcb *inp; 1221 1222 inp = sotoinpcb(so); 1223 KASSERT(inp != NULL, ("udp6_detach: inp == NULL")); 1224 1225 INP_WLOCK(inp); 1226 in_pcbfree(inp); 1227 } 1228 1229 static int 1230 udp6_disconnect(struct socket *so) 1231 { 1232 struct inpcb *inp; 1233 struct inpcbinfo *pcbinfo; 1234 1235 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1236 inp = sotoinpcb(so); 1237 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL")); 1238 1239 INP_WLOCK(inp); 1240 #ifdef INET 1241 if (inp->inp_vflag & INP_IPV4) { 1242 INP_WUNLOCK(inp); 1243 (void)udp_disconnect(so); 1244 return (0); 1245 } 1246 #endif 1247 1248 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1249 INP_WUNLOCK(inp); 1250 return (ENOTCONN); 1251 } 1252 1253 INP_HASH_WLOCK(pcbinfo); 1254 in6_pcbdisconnect(inp); 1255 INP_HASH_WUNLOCK(pcbinfo); 1256 SOCK_LOCK(so); 1257 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1258 SOCK_UNLOCK(so); 1259 INP_WUNLOCK(inp); 1260 return (0); 1261 } 1262 1263 #define UDP6_PROTOSW \ 1264 .pr_type = SOCK_DGRAM, \ 1265 .pr_flags = PR_ATOMIC|PR_ADDR|PR_CAPATTACH, \ 1266 .pr_ctloutput = udp_ctloutput, \ 1267 .pr_abort = udp6_abort, \ 1268 .pr_attach = udp6_attach, \ 1269 .pr_bind = udp6_bind, \ 1270 .pr_connect = udp6_connect, \ 1271 .pr_control = in6_control, \ 1272 .pr_detach = udp6_detach, \ 1273 .pr_disconnect = udp6_disconnect, \ 1274 .pr_peeraddr = in6_mapped_peeraddr, \ 1275 .pr_send = udp6_send, \ 1276 .pr_shutdown = udp_shutdown, \ 1277 .pr_sockaddr = in6_mapped_sockaddr, \ 1278 .pr_soreceive = soreceive_dgram, \ 1279 .pr_sosend = sosend_dgram, \ 1280 .pr_sosetlabel = in_pcbsosetlabel, \ 1281 .pr_close = udp6_close 1282 1283 struct protosw udp6_protosw = { 1284 .pr_protocol = IPPROTO_UDP, 1285 UDP6_PROTOSW 1286 }; 1287 1288 struct protosw udplite6_protosw = { 1289 .pr_protocol = IPPROTO_UDPLITE, 1290 UDP6_PROTOSW 1291 }; 1292 1293 static void 1294 udp6_init(void *arg __unused) 1295 { 1296 1297 IP6PROTO_REGISTER(IPPROTO_UDP, udp6_input, udp6_ctlinput); 1298 IP6PROTO_REGISTER(IPPROTO_UDPLITE, udp6_input, udplite6_ctlinput); 1299 } 1300 SYSINIT(udp6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp6_init, NULL); 1301