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