1 /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.13 2003/01/24 05:11:35 sam Exp $ */ 2 /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 62 */ 63 64 #include "opt_inet.h" 65 #include "opt_inet6.h" 66 #include "opt_ipsec.h" 67 68 #include <sys/param.h> 69 #include <sys/kernel.h> 70 #include <sys/mbuf.h> 71 #include <sys/protosw.h> 72 #include <sys/socket.h> 73 #include <sys/socketvar.h> 74 #include <sys/sysctl.h> 75 #include <sys/errno.h> 76 #include <sys/stat.h> 77 #include <sys/systm.h> 78 #include <sys/syslog.h> 79 #include <sys/proc.h> 80 #include <sys/priv.h> 81 82 #include <sys/thread2.h> 83 #include <sys/socketvar2.h> 84 #include <sys/msgport2.h> 85 86 #include <net/if.h> 87 #include <net/route.h> 88 #include <net/if_types.h> 89 #include <net/netisr2.h> 90 91 #include <netinet/in.h> 92 #include <netinet/in_systm.h> 93 #include <netinet/ip.h> 94 #include <netinet/in_pcb.h> 95 #include <netinet/in_var.h> 96 #include <netinet/ip_var.h> 97 #include <netinet/udp.h> 98 #include <netinet/udp_var.h> 99 #include <netinet/ip6.h> 100 #include <netinet6/ip6_var.h> 101 #include <netinet6/in6_pcb.h> 102 #include <netinet/icmp6.h> 103 #include <netinet6/udp6_var.h> 104 #include <netinet6/ip6protosw.h> 105 106 #ifdef IPSEC 107 #include <netinet6/ipsec.h> 108 #include <netinet6/ipsec6.h> 109 #endif /* IPSEC */ 110 111 #ifdef FAST_IPSEC 112 #include <netproto/ipsec/ipsec.h> 113 #include <netproto/ipsec/ipsec6.h> 114 #endif /* FAST_IPSEC */ 115 116 /* 117 * UDP protocol inplementation. 118 * Per RFC 768, August, 1980. 119 */ 120 121 extern struct protosw inetsw[]; 122 static int in6_mcmatch (struct inpcb *, struct in6_addr *, struct ifnet *); 123 124 static int 125 in6_mcmatch(struct inpcb *in6p, struct in6_addr *ia6, struct ifnet *ifp) 126 { 127 struct ip6_moptions *im6o = in6p->in6p_moptions; 128 struct in6_multi_mship *imm; 129 130 if (im6o == NULL) 131 return 0; 132 133 for (imm = im6o->im6o_memberships.lh_first; imm != NULL; 134 imm = imm->i6mm_chain.le_next) { 135 if ((ifp == NULL || 136 imm->i6mm_maddr->in6m_ifp == ifp) && 137 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 138 ia6)) 139 return 1; 140 } 141 return 0; 142 } 143 144 int 145 udp6_input(struct mbuf **mp, int *offp, int proto) 146 { 147 struct mbuf *m = *mp; 148 struct ip6_hdr *ip6; 149 struct udphdr *uh; 150 struct inpcb *in6p; 151 struct mbuf *opts = NULL; 152 int off = *offp; 153 int plen, ulen; 154 struct sockaddr_in6 udp_in6; 155 struct socket *so; 156 157 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 158 159 ip6 = mtod(m, struct ip6_hdr *); 160 161 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 162 /* XXX send icmp6 host/port unreach? */ 163 m_freem(m); 164 return IPPROTO_DONE; 165 } 166 167 udp_stat.udps_ipackets++; 168 169 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 170 uh = (struct udphdr *)((caddr_t)ip6 + off); 171 ulen = ntohs((u_short)uh->uh_ulen); 172 173 if (plen != ulen) { 174 udp_stat.udps_badlen++; 175 goto bad; 176 } 177 178 /* 179 * Checksum extended UDP header and data. 180 */ 181 if (uh->uh_sum == 0) 182 udp_stat.udps_nosum++; 183 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 184 udp_stat.udps_badsum++; 185 goto bad; 186 } 187 188 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 189 struct inpcb *last; 190 191 /* 192 * Deliver a multicast datagram to all sockets 193 * for which the local and remote addresses and ports match 194 * those of the incoming datagram. This allows more than 195 * one process to receive multicasts on the same port. 196 * (This really ought to be done for unicast datagrams as 197 * well, but that would cause problems with existing 198 * applications that open both address-specific sockets and 199 * a wildcard socket listening to the same port -- they would 200 * end up receiving duplicates of every unicast datagram. 201 * Those applications open the multiple sockets to overcome an 202 * inadequacy of the UDP socket interface, but for backwards 203 * compatibility we avoid the problem here rather than 204 * fixing the interface. Maybe 4.5BSD will remedy this?) 205 */ 206 207 /* 208 * In a case that laddr should be set to the link-local 209 * address (this happens in RIPng), the multicast address 210 * specified in the received packet does not match with 211 * laddr. To cure this situation, the matching is relaxed 212 * if the receiving interface is the same as one specified 213 * in the socket and if the destination multicast address 214 * matches one of the multicast groups specified in the socket. 215 */ 216 217 /* 218 * Construct sockaddr format source address. 219 */ 220 init_sin6(&udp_in6, m); /* general init */ 221 udp_in6.sin6_port = uh->uh_sport; 222 /* 223 * KAME note: traditionally we dropped udpiphdr from mbuf here. 224 * We need udphdr for IPsec processing so we do that later. 225 */ 226 227 /* 228 * Locate pcb(s) for datagram. 229 * (Algorithm copied from raw_intr().) 230 */ 231 last = NULL; 232 LIST_FOREACH(in6p, &udbinfo.pcblisthead, inp_list) { 233 KKASSERT((in6p->inp_flags & INP_PLACEMARKER) == 0); 234 235 if (!(in6p->inp_vflag & INP_IPV6)) 236 continue; 237 if (in6p->in6p_lport != uh->uh_dport) 238 continue; 239 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 240 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 241 &ip6->ip6_dst) && 242 !in6_mcmatch(in6p, &ip6->ip6_dst, 243 m->m_pkthdr.rcvif)) 244 continue; 245 } 246 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 247 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 248 &ip6->ip6_src) || 249 in6p->in6p_fport != uh->uh_sport) 250 continue; 251 } 252 253 if (last != NULL) { 254 struct mbuf *n; 255 256 #ifdef IPSEC 257 /* 258 * Check AH/ESP integrity. 259 */ 260 if (ipsec6_in_reject_so(m, last->inp_socket)) 261 ipsec6stat.in_polvio++; 262 /* do not inject data into pcb */ 263 else 264 #endif /* IPSEC */ 265 #ifdef FAST_IPSEC 266 /* 267 * Check AH/ESP integrity. 268 */ 269 if (ipsec6_in_reject(m, last)) 270 ; 271 else 272 #endif /* FAST_IPSEC */ 273 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 274 /* 275 * KAME NOTE: do not 276 * m_copy(m, offset, ...) above. 277 * ssb_appendaddr() expects M_PKTHDR, 278 * and m_copy() will copy M_PKTHDR 279 * only if offset is 0. 280 */ 281 so = last->in6p_socket; 282 if ((last->in6p_flags & IN6P_CONTROLOPTS) || 283 (so->so_options & SO_TIMESTAMP)) { 284 ip6_savecontrol(last, &opts, 285 ip6, n); 286 } 287 m_adj(n, off + sizeof(struct udphdr)); 288 lwkt_gettoken(&so->so_rcv.ssb_token); 289 if (ssb_appendaddr(&so->so_rcv, 290 (struct sockaddr *)&udp_in6, 291 n, opts) == 0) { 292 m_freem(n); 293 if (opts) 294 m_freem(opts); 295 udp_stat.udps_fullsock++; 296 } else { 297 sorwakeup(so); 298 } 299 lwkt_reltoken(&so->so_rcv.ssb_token); 300 opts = NULL; 301 } 302 } 303 last = in6p; 304 /* 305 * Don't look for additional matches if this one does 306 * not have either the SO_REUSEPORT or SO_REUSEADDR 307 * socket options set. This heuristic avoids searching 308 * through all pcbs in the common case of a non-shared 309 * port. It assumes that an application will never 310 * clear these options after setting them. 311 */ 312 if ((last->in6p_socket->so_options & 313 (SO_REUSEPORT | SO_REUSEADDR)) == 0) 314 break; 315 } 316 317 if (last == NULL) { 318 /* 319 * No matching pcb found; discard datagram. 320 * (No need to send an ICMP Port Unreachable 321 * for a broadcast or multicast datgram.) 322 */ 323 udp_stat.udps_noport++; 324 udp_stat.udps_noportmcast++; 325 goto bad; 326 } 327 #ifdef IPSEC 328 /* 329 * Check AH/ESP integrity. 330 */ 331 if (ipsec6_in_reject_so(m, last->inp_socket)) { 332 ipsec6stat.in_polvio++; 333 goto bad; 334 } 335 #endif /* IPSEC */ 336 #ifdef FAST_IPSEC 337 /* 338 * Check AH/ESP integrity. 339 */ 340 if (ipsec6_in_reject(m, last)) { 341 goto bad; 342 } 343 #endif /* FAST_IPSEC */ 344 if (last->in6p_flags & IN6P_CONTROLOPTS 345 || last->in6p_socket->so_options & SO_TIMESTAMP) 346 ip6_savecontrol(last, &opts, ip6, m); 347 348 m_adj(m, off + sizeof(struct udphdr)); 349 so = last->in6p_socket; 350 lwkt_gettoken(&so->so_rcv.ssb_token); 351 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&udp_in6, 352 m, opts) == 0) { 353 udp_stat.udps_fullsock++; 354 lwkt_reltoken(&so->so_rcv.ssb_token); 355 goto bad; 356 } 357 sorwakeup(so); 358 lwkt_reltoken(&so->so_rcv.ssb_token); 359 return IPPROTO_DONE; 360 } 361 /* 362 * Locate pcb for datagram. 363 */ 364 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, 365 &ip6->ip6_dst, uh->uh_dport, 1, 366 m->m_pkthdr.rcvif); 367 if (in6p == NULL) { 368 if (log_in_vain) { 369 char buf[INET6_ADDRSTRLEN]; 370 371 strcpy(buf, ip6_sprintf(&ip6->ip6_dst)); 372 log(LOG_INFO, 373 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 374 buf, ntohs(uh->uh_dport), 375 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport)); 376 } 377 udp_stat.udps_noport++; 378 if (m->m_flags & M_MCAST) { 379 kprintf("UDP6: M_MCAST is set in a unicast packet.\n"); 380 udp_stat.udps_noportmcast++; 381 goto bad; 382 } 383 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 384 return IPPROTO_DONE; 385 } 386 #ifdef IPSEC 387 /* 388 * Check AH/ESP integrity. 389 */ 390 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) { 391 ipsec6stat.in_polvio++; 392 goto bad; 393 } 394 #endif /* IPSEC */ 395 #ifdef FAST_IPSEC 396 /* 397 * Check AH/ESP integrity. 398 */ 399 if (ipsec6_in_reject(m, in6p)) { 400 goto bad; 401 } 402 #endif /* FAST_IPSEC */ 403 404 /* 405 * Construct sockaddr format source address. 406 * Stuff source address and datagram in user buffer. 407 */ 408 init_sin6(&udp_in6, m); /* general init */ 409 udp_in6.sin6_port = uh->uh_sport; 410 if (in6p->in6p_flags & IN6P_CONTROLOPTS 411 || in6p->in6p_socket->so_options & SO_TIMESTAMP) 412 ip6_savecontrol(in6p, &opts, ip6, m); 413 m_adj(m, off + sizeof(struct udphdr)); 414 so = in6p->in6p_socket; 415 lwkt_gettoken(&so->so_rcv.ssb_token); 416 if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&udp_in6, 417 m, opts) == 0) { 418 udp_stat.udps_fullsock++; 419 lwkt_reltoken(&so->so_rcv.ssb_token); 420 goto bad; 421 } 422 sorwakeup(so); 423 lwkt_reltoken(&so->so_rcv.ssb_token); 424 return IPPROTO_DONE; 425 bad: 426 if (m) 427 m_freem(m); 428 if (opts) 429 m_freem(opts); 430 return IPPROTO_DONE; 431 } 432 433 void 434 udp6_ctlinput(netmsg_t msg) 435 { 436 int cmd = msg->ctlinput.nm_cmd; 437 struct sockaddr *sa = msg->ctlinput.nm_arg; 438 void *d = msg->ctlinput.nm_extra; 439 struct udphdr uh; 440 struct ip6_hdr *ip6; 441 struct mbuf *m; 442 int off = 0; 443 struct ip6ctlparam *ip6cp = NULL; 444 const struct sockaddr_in6 *sa6_src = NULL; 445 void (*notify) (struct inpcb *, int) = udp_notify; 446 struct udp_portonly { 447 u_int16_t uh_sport; 448 u_int16_t uh_dport; 449 } *uhp; 450 451 if (sa->sa_family != AF_INET6 || 452 sa->sa_len != sizeof(struct sockaddr_in6)) 453 goto out; 454 455 if ((unsigned)cmd >= PRC_NCMDS) 456 goto out; 457 if (PRC_IS_REDIRECT(cmd)) 458 notify = in6_rtchange, d = NULL; 459 else if (cmd == PRC_HOSTDEAD) 460 d = NULL; 461 else if (inet6ctlerrmap[cmd] == 0) 462 goto out; 463 464 /* if the parameter is from icmp6, decode it. */ 465 if (d != NULL) { 466 ip6cp = (struct ip6ctlparam *)d; 467 m = ip6cp->ip6c_m; 468 ip6 = ip6cp->ip6c_ip6; 469 off = ip6cp->ip6c_off; 470 sa6_src = ip6cp->ip6c_src; 471 } else { 472 m = NULL; 473 ip6 = NULL; 474 sa6_src = &sa6_any; 475 } 476 477 if (ip6) { 478 /* 479 * XXX: We assume that when IPV6 is non NULL, 480 * M and OFF are valid. 481 */ 482 483 /* check if we can safely examine src and dst ports */ 484 if (m->m_pkthdr.len < off + sizeof(*uhp)) 485 return; 486 487 bzero(&uh, sizeof(uh)); 488 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 489 490 in6_pcbnotify(&udbinfo.pcblisthead, sa, uh.uh_dport, 491 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, 492 cmd, 0, notify); 493 } else { 494 in6_pcbnotify(&udbinfo.pcblisthead, sa, 0, 495 (const struct sockaddr *)sa6_src, 0, 496 cmd, 0, notify); 497 } 498 out: 499 lwkt_replymsg(&msg->ctlinput.base.lmsg, 0); 500 } 501 502 static int 503 udp6_getcred(SYSCTL_HANDLER_ARGS) 504 { 505 struct sockaddr_in6 addrs[2]; 506 struct inpcb *inp; 507 int error; 508 509 error = priv_check(req->td, PRIV_ROOT); 510 if (error) 511 return (error); 512 513 if (req->newlen != sizeof(addrs)) 514 return (EINVAL); 515 if (req->oldlen != sizeof(struct ucred)) 516 return (EINVAL); 517 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 518 if (error) 519 return (error); 520 crit_enter(); 521 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, 522 addrs[1].sin6_port, 523 &addrs[0].sin6_addr, addrs[0].sin6_port, 524 1, NULL); 525 if (!inp || !inp->inp_socket) { 526 error = ENOENT; 527 goto out; 528 } 529 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, 530 sizeof(struct ucred)); 531 532 out: 533 crit_exit(); 534 return (error); 535 } 536 537 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 538 0, 0, 539 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection"); 540 541 /* 542 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort() 543 * will sofree() it when we return. 544 */ 545 static void 546 udp6_abort(netmsg_t msg) 547 { 548 struct socket *so = msg->abort.base.nm_so; 549 struct inpcb *inp; 550 int error; 551 552 inp = so->so_pcb; 553 if (inp) { 554 soisdisconnected(so); 555 556 udbinfo_barrier_set(); 557 in6_pcbdetach(inp); 558 udbinfo_barrier_rem(); 559 error = 0; 560 } else { 561 error = EINVAL; 562 } 563 lwkt_replymsg(&msg->abort.base.lmsg, error); 564 } 565 566 static void 567 udp6_attach(netmsg_t msg) 568 { 569 struct socket *so = msg->attach.base.nm_so; 570 struct pru_attach_info *ai = msg->attach.nm_ai; 571 struct inpcb *inp; 572 int error; 573 574 inp = so->so_pcb; 575 if (inp != NULL) { 576 error = EINVAL; 577 goto out; 578 } 579 580 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) { 581 error = soreserve(so, udp_sendspace, udp_recvspace, 582 ai->sb_rlimit); 583 if (error) 584 goto out; 585 } 586 587 udbinfo_barrier_set(); 588 error = in_pcballoc(so, &udbinfo); 589 udbinfo_barrier_rem(); 590 591 if (error) 592 goto out; 593 594 sosetport(so, netisr_cpuport(0)); 595 inp = (struct inpcb *)so->so_pcb; 596 inp->inp_vflag |= INP_IPV6; 597 if (!ip6_v6only) 598 inp->inp_vflag |= INP_IPV4; 599 inp->in6p_hops = -1; /* use kernel default */ 600 inp->in6p_cksum = -1; /* just to be sure */ 601 /* 602 * XXX: ugly!! 603 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 604 * because the socket may be bound to an IPv6 wildcard address, 605 * which may match an IPv4-mapped IPv6 address. 606 */ 607 inp->inp_ip_ttl = ip_defttl; 608 error = 0; 609 out: 610 lwkt_replymsg(&msg->attach.base.lmsg, error); 611 } 612 613 static void 614 udp6_bind(netmsg_t msg) 615 { 616 struct socket *so =msg->bind.base.nm_so; 617 struct sockaddr *nam = msg->bind.nm_nam; 618 struct thread *td = msg->bind.nm_td; 619 struct sockaddr_in6 *sin6_p = (struct sockaddr_in6 *)nam; 620 struct inpcb *inp; 621 int error; 622 623 inp = so->so_pcb; 624 if (inp == NULL) { 625 error = EINVAL; 626 goto out; 627 } 628 629 inp->inp_vflag &= ~INP_IPV4; 630 inp->inp_vflag |= INP_IPV6; 631 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) { 632 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 633 inp->inp_vflag |= INP_IPV4; 634 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 635 struct sockaddr_in sin; 636 637 in6_sin6_2_sin(&sin, sin6_p); 638 inp->inp_vflag |= INP_IPV4; 639 inp->inp_vflag &= ~INP_IPV6; 640 crit_enter(); 641 error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 642 crit_exit(); 643 goto out; 644 } 645 } 646 647 crit_enter(); 648 error = in6_pcbbind(inp, nam, td); 649 crit_exit(); 650 if (error == 0) { 651 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 652 inp->inp_flags |= INP_WASBOUND_NOTANY; 653 654 udbinfo_barrier_set(); 655 in_pcbinswildcardhash(inp); 656 udbinfo_barrier_rem(); 657 } 658 out: 659 lwkt_replymsg(&msg->bind.base.lmsg, error); 660 } 661 662 static void 663 udp6_connect(netmsg_t msg) 664 { 665 struct socket *so = msg->connect.base.nm_so; 666 struct sockaddr *nam = msg->connect.nm_nam; 667 struct thread *td = msg->connect.nm_td; 668 struct inpcb *inp; 669 int error; 670 671 udbinfo_barrier_set(); 672 673 inp = so->so_pcb; 674 if (inp == NULL) { 675 error = EINVAL; 676 goto out; 677 } 678 679 if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) { 680 struct sockaddr_in6 *sin6_p; 681 682 sin6_p = (struct sockaddr_in6 *)nam; 683 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 684 struct sockaddr_in sin; 685 686 if (inp->inp_faddr.s_addr != INADDR_ANY) { 687 error = EISCONN; 688 goto out; 689 } 690 in6_sin6_2_sin(&sin, sin6_p); 691 crit_enter(); 692 if (inp->inp_flags & INP_WILDCARD) 693 in_pcbremwildcardhash(inp); 694 error = in_pcbconnect(inp, (struct sockaddr *)&sin, td); 695 crit_exit(); 696 if (error == 0) { 697 inp->inp_vflag |= INP_IPV4; 698 inp->inp_vflag &= ~INP_IPV6; 699 soisconnected(so); 700 } 701 goto out; 702 } 703 } 704 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 705 error = EISCONN; 706 goto out; 707 } 708 if (inp->inp_flags & INP_WILDCARD) 709 in_pcbremwildcardhash(inp); 710 if (!prison_remote_ip(td, nam)) { 711 error = EAFNOSUPPORT; /* IPv4 only jail */ 712 goto out; 713 } 714 crit_enter(); 715 error = in6_pcbconnect(inp, nam, td); 716 crit_exit(); 717 if (error == 0) { 718 if (!ip6_v6only) { /* should be non mapped addr */ 719 inp->inp_vflag &= ~INP_IPV4; 720 inp->inp_vflag |= INP_IPV6; 721 } 722 soisconnected(so); 723 } else if (error == EAFNOSUPPORT) { /* connection dissolved */ 724 /* 725 * Follow traditional BSD behavior and retain 726 * the local port binding. But, fix the old misbehavior 727 * of overwriting any previously bound local address. 728 */ 729 if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) 730 inp->in6p_laddr = kin6addr_any; 731 in_pcbinswildcardhash(inp); 732 } 733 out: 734 udbinfo_barrier_rem(); 735 lwkt_replymsg(&msg->connect.base.lmsg, error); 736 } 737 738 static void 739 udp6_detach(netmsg_t msg) 740 { 741 struct socket *so = msg->detach.base.nm_so; 742 struct inpcb *inp; 743 int error; 744 745 inp = so->so_pcb; 746 if (inp) { 747 udbinfo_barrier_set(); 748 in6_pcbdetach(inp); 749 udbinfo_barrier_rem(); 750 error = 0; 751 } else { 752 error = EINVAL; 753 } 754 lwkt_replymsg(&msg->detach.base.lmsg, error); 755 } 756 757 static void 758 udp6_disconnect(netmsg_t msg) 759 { 760 struct socket *so = msg->disconnect.base.nm_so; 761 struct inpcb *inp; 762 int error; 763 764 inp = so->so_pcb; 765 if (inp == NULL) { 766 error = EINVAL; 767 goto out; 768 } 769 770 if (inp->inp_vflag & INP_IPV4) { 771 const struct pr_usrreqs *pru; 772 773 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 774 pru->pru_disconnect(msg); /* XXX on right port? */ 775 return; 776 } 777 778 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 779 error = ENOTCONN; 780 } else { 781 udbinfo_barrier_set(); 782 in6_pcbdisconnect(inp); 783 udbinfo_barrier_rem(); 784 soclrstate(so, SS_ISCONNECTED); /* XXX */ 785 error = 0; 786 } 787 out: 788 lwkt_replymsg(&msg->disconnect.base.lmsg, error); 789 } 790 791 static void 792 udp6_send(netmsg_t msg) 793 { 794 struct socket *so = msg->send.base.nm_so; 795 struct mbuf *m = msg->send.nm_m; 796 struct sockaddr *addr = msg->send.nm_addr; 797 struct mbuf *control = msg->send.nm_control; 798 struct thread *td = msg->send.nm_td; 799 struct inpcb *inp; 800 int error = 0; 801 802 inp = so->so_pcb; 803 if (inp == NULL) { 804 error = EINVAL; 805 goto bad; 806 } 807 808 if (addr) { 809 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 810 error = EINVAL; 811 goto bad; 812 } 813 if (addr->sa_family != AF_INET6) { 814 error = EAFNOSUPPORT; 815 goto bad; 816 } 817 } 818 819 if (!ip6_v6only) { 820 int hasv4addr; 821 struct sockaddr_in6 *sin6 = NULL; 822 823 if (addr == NULL) 824 hasv4addr = (inp->inp_vflag & INP_IPV4); 825 else { 826 sin6 = (struct sockaddr_in6 *)addr; 827 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 828 ? 1 : 0; 829 } 830 if (hasv4addr) { 831 const struct pr_usrreqs *pru; 832 833 if (sin6) 834 in6_sin6_2_sin_in_sock(addr); 835 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 836 pru->pru_send(msg); 837 /* msg invalid now */ 838 return; 839 } 840 } 841 842 error = udp6_output(inp, m, addr, control, td); 843 lwkt_replymsg(&msg->send.base.lmsg, error); 844 return; 845 bad: 846 m_freem(m); 847 lwkt_replymsg(&msg->send.base.lmsg, error); 848 } 849 850 struct pr_usrreqs udp6_usrreqs = { 851 .pru_abort = udp6_abort, 852 .pru_accept = pr_generic_notsupp, 853 .pru_attach = udp6_attach, 854 .pru_bind = udp6_bind, 855 .pru_connect = udp6_connect, 856 .pru_connect2 = pr_generic_notsupp, 857 .pru_control = in6_control_dispatch, 858 .pru_detach = udp6_detach, 859 .pru_disconnect = udp6_disconnect, 860 .pru_listen = pr_generic_notsupp, 861 .pru_peeraddr = in6_mapped_peeraddr_dispatch, 862 .pru_rcvd = pr_generic_notsupp, 863 .pru_rcvoob = pr_generic_notsupp, 864 .pru_send = udp6_send, 865 .pru_sense = pru_sense_null, 866 .pru_shutdown = udp_shutdown, 867 .pru_sockaddr = in6_mapped_sockaddr_dispatch, 868 .pru_sosend = sosend, 869 .pru_soreceive = soreceive 870 }; 871 872