1 /* $FreeBSD$ */ 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 * 4. 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/errno.h> 70 #include <sys/kernel.h> 71 #include <sys/lock.h> 72 #include <sys/mbuf.h> 73 #include <sys/priv.h> 74 #include <sys/proc.h> 75 #include <sys/protosw.h> 76 #include <sys/signalvar.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/stat.h> 80 #include <sys/sx.h> 81 #include <sys/sysctl.h> 82 #include <sys/syslog.h> 83 #include <sys/systm.h> 84 85 #include <net/if.h> 86 #include <net/if_types.h> 87 #include <net/route.h> 88 89 #include <netinet/in.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/in_systm.h> 92 #include <netinet/in_var.h> 93 #include <netinet/ip.h> 94 #include <netinet/ip6.h> 95 #include <netinet/icmp6.h> 96 #include <netinet/ip_var.h> 97 #include <netinet/udp.h> 98 #include <netinet/udp_var.h> 99 #include <netinet6/ip6protosw.h> 100 #include <netinet6/ip6_var.h> 101 #include <netinet6/in6_pcb.h> 102 #include <netinet6/udp6_var.h> 103 #include <netinet6/scope6_var.h> 104 105 #ifdef IPSEC 106 #include <netinet6/ipsec.h> 107 #include <netinet6/ipsec6.h> 108 #endif /* IPSEC */ 109 110 #ifdef FAST_IPSEC 111 #include <netipsec/ipsec.h> 112 #include <netipsec/ipsec6.h> 113 #endif /* FAST_IPSEC */ 114 115 /* 116 * UDP protocol inplementation. 117 * Per RFC 768, August, 1980. 118 */ 119 120 extern struct protosw inetsw[]; 121 static void udp6_detach __P((struct socket *so)); 122 123 static void 124 udp6_append(struct inpcb *in6p, struct mbuf *n, int off, 125 struct sockaddr_in6 *fromsa) 126 { 127 struct socket *so; 128 struct mbuf *opts; 129 130 /* XXXRW: Not yet: INP_LOCK_ASSERT(in6p); */ 131 132 #if defined(IPSEC) || defined(FAST_IPSEC) 133 /* 134 * Check AH/ESP integrity. 135 */ 136 if (ipsec6_in_reject(n, in6p)) { 137 m_freem(n); 138 #ifdef IPSEC 139 ipsec6stat.in_polvio++; 140 #endif /* IPSEC */ 141 return; 142 } 143 #endif /*IPSEC || FAST_IPSEC*/ 144 145 opts = NULL; 146 if (in6p->in6p_flags & IN6P_CONTROLOPTS || 147 in6p->inp_socket->so_options & SO_TIMESTAMP) 148 ip6_savecontrol(in6p, n, &opts); 149 m_adj(n, off + sizeof(struct udphdr)); 150 151 so = in6p->inp_socket; 152 SOCKBUF_LOCK(&so->so_rcv); 153 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n, 154 opts) == 0) { 155 SOCKBUF_UNLOCK(&so->so_rcv); 156 m_freem(n); 157 if (opts) 158 m_freem(opts); 159 udpstat.udps_fullsock++; 160 } else 161 sorwakeup_locked(so); 162 } 163 164 int 165 udp6_input(mp, offp, proto) 166 struct mbuf **mp; 167 int *offp, proto; 168 { 169 struct mbuf *m = *mp; 170 register struct ip6_hdr *ip6; 171 register struct udphdr *uh; 172 register struct inpcb *in6p; 173 int off = *offp; 174 int plen, ulen; 175 struct sockaddr_in6 fromsa; 176 177 ip6 = mtod(m, struct ip6_hdr *); 178 179 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 180 /* XXX send icmp6 host/port unreach? */ 181 m_freem(m); 182 return IPPROTO_DONE; 183 } 184 185 #ifndef PULLDOWN_TEST 186 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 187 ip6 = mtod(m, struct ip6_hdr *); 188 uh = (struct udphdr *)((caddr_t)ip6 + off); 189 #else 190 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh)); 191 if (!uh) 192 return IPPROTO_DONE; 193 #endif 194 195 udpstat.udps_ipackets++; 196 197 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 198 ulen = ntohs((u_short)uh->uh_ulen); 199 200 if (plen != ulen) { 201 udpstat.udps_badlen++; 202 goto bad; 203 } 204 205 /* 206 * Checksum extended UDP header and data. 207 */ 208 if (uh->uh_sum == 0) { 209 udpstat.udps_nosum++; 210 goto bad_unlocked; 211 } 212 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 213 udpstat.udps_badsum++; 214 goto bad_unlocked; 215 } 216 217 /* 218 * Construct sockaddr format source address. 219 */ 220 init_sin6(&fromsa, m); 221 fromsa.sin6_port = uh->uh_sport; 222 223 INP_INFO_RLOCK(&udbinfo); 224 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 225 struct inpcb *last; 226 227 /* 228 * Deliver a multicast datagram to all sockets 229 * for which the local and remote addresses and ports match 230 * those of the incoming datagram. This allows more than 231 * one process to receive multicasts on the same port. 232 * (This really ought to be done for unicast datagrams as 233 * well, but that would cause problems with existing 234 * applications that open both address-specific sockets and 235 * a wildcard socket listening to the same port -- they would 236 * end up receiving duplicates of every unicast datagram. 237 * Those applications open the multiple sockets to overcome an 238 * inadequacy of the UDP socket interface, but for backwards 239 * compatibility we avoid the problem here rather than 240 * fixing the interface. Maybe 4.5BSD will remedy this?) 241 */ 242 243 /* 244 * In a case that laddr should be set to the link-local 245 * address (this happens in RIPng), the multicast address 246 * specified in the received packet does not match with 247 * laddr. To cure this situation, the matching is relaxed 248 * if the receiving interface is the same as one specified 249 * in the socket and if the destination multicast address 250 * matches one of the multicast groups specified in the socket. 251 */ 252 253 /* 254 * KAME note: traditionally we dropped udpiphdr from mbuf here. 255 * We need udphdr for IPsec processing so we do that later. 256 */ 257 258 /* 259 * Locate pcb(s) for datagram. 260 * (Algorithm copied from raw_intr().) 261 * 262 * XXXRW: The individual inpcbs need to be locked in the 263 * style of udp_input(). 264 */ 265 last = NULL; 266 LIST_FOREACH(in6p, &udb, inp_list) { 267 if ((in6p->inp_vflag & INP_IPV6) == 0) 268 continue; 269 if (in6p->in6p_lport != uh->uh_dport) 270 continue; 271 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 272 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 273 &ip6->ip6_dst)) 274 continue; 275 } 276 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 277 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 278 &ip6->ip6_src) || 279 in6p->in6p_fport != uh->uh_sport) 280 continue; 281 } 282 283 if (last != NULL) { 284 struct mbuf *n; 285 286 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 287 INP_LOCK(last); 288 udp6_append(last, n, off, &fromsa); 289 INP_UNLOCK(last); 290 } 291 } 292 last = in6p; 293 /* 294 * Don't look for additional matches if this one does 295 * not have either the SO_REUSEPORT or SO_REUSEADDR 296 * socket options set. This heuristic avoids searching 297 * through all pcbs in the common case of a non-shared 298 * port. It assumes that an application will never 299 * clear these options after setting them. 300 */ 301 if ((last->in6p_socket->so_options & 302 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 303 break; 304 } 305 306 if (last == NULL) { 307 /* 308 * No matching pcb found; discard datagram. 309 * (No need to send an ICMP Port Unreachable 310 * for a broadcast or multicast datgram.) 311 */ 312 udpstat.udps_noport++; 313 udpstat.udps_noportmcast++; 314 goto bad; 315 } 316 INP_LOCK(last); 317 udp6_append(last, m, off, &fromsa); 318 INP_UNLOCK(last); 319 INP_INFO_RUNLOCK(&udbinfo); 320 return IPPROTO_DONE; 321 } 322 /* 323 * Locate pcb for datagram. 324 */ 325 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, 326 &ip6->ip6_dst, uh->uh_dport, 1, 327 m->m_pkthdr.rcvif); 328 if (in6p == NULL) { 329 if (udp_log_in_vain) { 330 char ip6bufs[INET6_ADDRSTRLEN]; 331 char ip6bufd[INET6_ADDRSTRLEN]; 332 333 log(LOG_INFO, 334 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 335 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 336 ntohs(uh->uh_dport), 337 ip6_sprintf(ip6bufs, &ip6->ip6_src), 338 ntohs(uh->uh_sport)); 339 } 340 udpstat.udps_noport++; 341 if (m->m_flags & M_MCAST) { 342 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 343 udpstat.udps_noportmcast++; 344 goto bad; 345 } 346 INP_INFO_RUNLOCK(&udbinfo); 347 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 348 return IPPROTO_DONE; 349 } 350 INP_LOCK(in6p); 351 udp6_append(in6p, m, off, &fromsa); 352 INP_UNLOCK(in6p); 353 INP_INFO_RUNLOCK(&udbinfo); 354 return IPPROTO_DONE; 355 bad: 356 INP_INFO_RUNLOCK(&udbinfo); 357 bad_unlocked: 358 if (m) 359 m_freem(m); 360 return IPPROTO_DONE; 361 } 362 363 void 364 udp6_ctlinput(cmd, sa, d) 365 int cmd; 366 struct sockaddr *sa; 367 void *d; 368 { 369 struct udphdr uh; 370 struct ip6_hdr *ip6; 371 struct mbuf *m; 372 int off = 0; 373 struct ip6ctlparam *ip6cp = NULL; 374 const struct sockaddr_in6 *sa6_src = NULL; 375 void *cmdarg; 376 struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify; 377 struct udp_portonly { 378 u_int16_t uh_sport; 379 u_int16_t uh_dport; 380 } *uhp; 381 382 if (sa->sa_family != AF_INET6 || 383 sa->sa_len != sizeof(struct sockaddr_in6)) 384 return; 385 386 if ((unsigned)cmd >= PRC_NCMDS) 387 return; 388 if (PRC_IS_REDIRECT(cmd)) 389 notify = in6_rtchange, d = NULL; 390 else if (cmd == PRC_HOSTDEAD) 391 d = NULL; 392 else if (inet6ctlerrmap[cmd] == 0) 393 return; 394 395 /* if the parameter is from icmp6, decode it. */ 396 if (d != NULL) { 397 ip6cp = (struct ip6ctlparam *)d; 398 m = ip6cp->ip6c_m; 399 ip6 = ip6cp->ip6c_ip6; 400 off = ip6cp->ip6c_off; 401 cmdarg = ip6cp->ip6c_cmdarg; 402 sa6_src = ip6cp->ip6c_src; 403 } else { 404 m = NULL; 405 ip6 = NULL; 406 cmdarg = NULL; 407 sa6_src = &sa6_any; 408 } 409 410 if (ip6) { 411 /* 412 * XXX: We assume that when IPV6 is non NULL, 413 * M and OFF are valid. 414 */ 415 416 /* check if we can safely examine src and dst ports */ 417 if (m->m_pkthdr.len < off + sizeof(*uhp)) 418 return; 419 420 bzero(&uh, sizeof(uh)); 421 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 422 423 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport, 424 (struct sockaddr *)ip6cp->ip6c_src, 425 uh.uh_sport, cmd, cmdarg, notify); 426 } else 427 (void) in6_pcbnotify(&udbinfo, sa, 0, 428 (const struct sockaddr *)sa6_src, 429 0, cmd, cmdarg, notify); 430 } 431 432 static int 433 udp6_getcred(SYSCTL_HANDLER_ARGS) 434 { 435 struct xucred xuc; 436 struct sockaddr_in6 addrs[2]; 437 struct inpcb *inp; 438 int error; 439 440 error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED, 441 SUSER_ALLOWJAIL); 442 if (error) 443 return (error); 444 445 if (req->newlen != sizeof(addrs)) 446 return (EINVAL); 447 if (req->oldlen != sizeof(struct xucred)) 448 return (EINVAL); 449 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 450 if (error) 451 return (error); 452 if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 || 453 (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) { 454 return (error); 455 } 456 INP_INFO_RLOCK(&udbinfo); 457 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, 458 addrs[1].sin6_port, 459 &addrs[0].sin6_addr, addrs[0].sin6_port, 460 1, NULL); 461 if (inp == NULL) { 462 INP_INFO_RUNLOCK(&udbinfo); 463 return (ENOENT); 464 } 465 INP_LOCK(inp); 466 KASSERT(inp->inp_socket != NULL, 467 ("udp6_getcred: inp_socket == NULL")); 468 cru2x(inp->inp_socket->so_cred, &xuc); 469 INP_UNLOCK(inp); 470 INP_INFO_RUNLOCK(&udbinfo); 471 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 472 return (error); 473 } 474 475 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 476 0, 0, 477 udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection"); 478 479 static void 480 udp6_abort(struct socket *so) 481 { 482 struct inpcb *inp; 483 484 inp = sotoinpcb(so); 485 KASSERT(inp != NULL, ("udp6_abort: inp == NULL")); 486 487 #ifdef INET 488 if (inp->inp_vflag & INP_IPV4) { 489 struct pr_usrreqs *pru; 490 491 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 492 (*pru->pru_abort)(so); 493 return; 494 } 495 #endif 496 497 INP_INFO_WLOCK(&udbinfo); 498 INP_LOCK(inp); 499 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 500 in6_pcbdisconnect(inp); 501 inp->in6p_laddr = in6addr_any; 502 soisdisconnected(so); 503 } 504 INP_UNLOCK(inp); 505 INP_INFO_WUNLOCK(&udbinfo); 506 } 507 508 static int 509 udp6_attach(struct socket *so, int proto, struct thread *td) 510 { 511 struct inpcb *inp; 512 int error; 513 514 inp = sotoinpcb(so); 515 KASSERT(inp == NULL, ("udp6_attach: inp != NULL")); 516 517 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 518 error = soreserve(so, udp_sendspace, udp_recvspace); 519 if (error) 520 return error; 521 } 522 INP_INFO_WLOCK(&udbinfo); 523 error = in_pcballoc(so, &udbinfo); 524 if (error) { 525 INP_INFO_WUNLOCK(&udbinfo); 526 return error; 527 } 528 inp = (struct inpcb *)so->so_pcb; 529 INP_INFO_WUNLOCK(&udbinfo); 530 inp->inp_vflag |= INP_IPV6; 531 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 532 inp->inp_vflag |= INP_IPV4; 533 inp->in6p_hops = -1; /* use kernel default */ 534 inp->in6p_cksum = -1; /* just to be sure */ 535 /* 536 * XXX: ugly!! 537 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 538 * because the socket may be bound to an IPv6 wildcard address, 539 * which may match an IPv4-mapped IPv6 address. 540 */ 541 inp->inp_ip_ttl = ip_defttl; 542 INP_UNLOCK(inp); 543 return 0; 544 } 545 546 static int 547 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 548 { 549 struct inpcb *inp; 550 int error; 551 552 inp = sotoinpcb(so); 553 KASSERT(inp != NULL, ("udp6_bind: inp == NULL")); 554 555 INP_INFO_WLOCK(&udbinfo); 556 INP_LOCK(inp); 557 inp->inp_vflag &= ~INP_IPV4; 558 inp->inp_vflag |= INP_IPV6; 559 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 560 struct sockaddr_in6 *sin6_p; 561 562 sin6_p = (struct sockaddr_in6 *)nam; 563 564 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 565 inp->inp_vflag |= INP_IPV4; 566 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 567 struct sockaddr_in sin; 568 569 in6_sin6_2_sin(&sin, sin6_p); 570 inp->inp_vflag |= INP_IPV4; 571 inp->inp_vflag &= ~INP_IPV6; 572 error = in_pcbbind(inp, (struct sockaddr *)&sin, 573 td->td_ucred); 574 goto out; 575 } 576 } 577 578 error = in6_pcbbind(inp, nam, td->td_ucred); 579 out: 580 INP_UNLOCK(inp); 581 INP_INFO_WUNLOCK(&udbinfo); 582 return error; 583 } 584 585 static void 586 udp6_close(struct socket *so) 587 { 588 struct inpcb *inp; 589 590 inp = sotoinpcb(so); 591 KASSERT(inp != NULL, ("udp6_close: inp == NULL")); 592 593 #ifdef INET 594 if (inp->inp_vflag & INP_IPV4) { 595 struct pr_usrreqs *pru; 596 597 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 598 (*pru->pru_disconnect)(so); 599 return; 600 } 601 #endif 602 INP_INFO_WLOCK(&udbinfo); 603 INP_LOCK(inp); 604 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 605 in6_pcbdisconnect(inp); 606 inp->in6p_laddr = in6addr_any; 607 soisdisconnected(so); 608 } 609 INP_UNLOCK(inp); 610 INP_INFO_WUNLOCK(&udbinfo); 611 } 612 613 static int 614 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 615 { 616 struct inpcb *inp; 617 int error; 618 619 inp = sotoinpcb(so); 620 KASSERT(inp != NULL, ("udp6_connect: inp == NULL")); 621 622 INP_INFO_WLOCK(&udbinfo); 623 INP_LOCK(inp); 624 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 625 struct sockaddr_in6 *sin6_p; 626 627 sin6_p = (struct sockaddr_in6 *)nam; 628 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 629 struct sockaddr_in sin; 630 631 if (inp->inp_faddr.s_addr != INADDR_ANY) { 632 error = EISCONN; 633 goto out; 634 } 635 in6_sin6_2_sin(&sin, sin6_p); 636 error = in_pcbconnect(inp, (struct sockaddr *)&sin, 637 td->td_ucred); 638 if (error == 0) { 639 inp->inp_vflag |= INP_IPV4; 640 inp->inp_vflag &= ~INP_IPV6; 641 soisconnected(so); 642 } 643 goto out; 644 } 645 } 646 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 647 error = EISCONN; 648 goto out; 649 } 650 error = in6_pcbconnect(inp, nam, td->td_ucred); 651 if (error == 0) { 652 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 653 /* should be non mapped addr */ 654 inp->inp_vflag &= ~INP_IPV4; 655 inp->inp_vflag |= INP_IPV6; 656 } 657 soisconnected(so); 658 } 659 out: 660 INP_UNLOCK(inp); 661 INP_INFO_WUNLOCK(&udbinfo); 662 return error; 663 } 664 665 static void 666 udp6_detach(struct socket *so) 667 { 668 struct inpcb *inp; 669 670 inp = sotoinpcb(so); 671 KASSERT(inp != NULL, ("udp6_detach: inp == NULL")); 672 673 INP_INFO_WLOCK(&udbinfo); 674 INP_LOCK(inp); 675 in6_pcbdetach(inp); 676 in6_pcbfree(inp); 677 INP_INFO_WUNLOCK(&udbinfo); 678 } 679 680 static int 681 udp6_disconnect(struct socket *so) 682 { 683 struct inpcb *inp; 684 int error; 685 686 inp = sotoinpcb(so); 687 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL")); 688 689 INP_INFO_WLOCK(&udbinfo); 690 INP_LOCK(inp); 691 692 #ifdef INET 693 if (inp->inp_vflag & INP_IPV4) { 694 struct pr_usrreqs *pru; 695 696 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 697 error = (*pru->pru_disconnect)(so); 698 goto out; 699 } 700 #endif 701 702 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 703 error = ENOTCONN; 704 goto out; 705 } 706 707 in6_pcbdisconnect(inp); 708 inp->in6p_laddr = in6addr_any; 709 /* XXXRW: so_state locking? */ 710 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 711 out: 712 INP_UNLOCK(inp); 713 INP_INFO_WUNLOCK(&udbinfo); 714 return 0; 715 } 716 717 static int 718 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 719 struct mbuf *control, struct thread *td) 720 { 721 struct inpcb *inp; 722 int error = 0; 723 724 inp = sotoinpcb(so); 725 KASSERT(inp != NULL, ("udp6_send: inp == NULL")); 726 727 INP_INFO_WLOCK(&udbinfo); 728 INP_LOCK(inp); 729 if (addr) { 730 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 731 error = EINVAL; 732 goto bad; 733 } 734 if (addr->sa_family != AF_INET6) { 735 error = EAFNOSUPPORT; 736 goto bad; 737 } 738 } 739 740 #ifdef INET 741 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 742 int hasv4addr; 743 struct sockaddr_in6 *sin6 = 0; 744 745 if (addr == 0) 746 hasv4addr = (inp->inp_vflag & INP_IPV4); 747 else { 748 sin6 = (struct sockaddr_in6 *)addr; 749 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 750 ? 1 : 0; 751 } 752 if (hasv4addr) { 753 struct pr_usrreqs *pru; 754 755 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 756 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) { 757 /* 758 * when remote addr is IPv4-mapped 759 * address, local addr should not be 760 * an IPv6 address; since you cannot 761 * determine how to map IPv6 source 762 * address to IPv4. 763 */ 764 error = EINVAL; 765 goto out; 766 } 767 if (sin6) 768 in6_sin6_2_sin_in_sock(addr); 769 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 770 error = ((*pru->pru_send)(so, flags, m, addr, control, 771 td)); 772 /* addr will just be freed in sendit(). */ 773 goto out; 774 } 775 } 776 #endif 777 778 error = udp6_output(inp, m, addr, control, td); 779 out: 780 INP_UNLOCK(inp); 781 INP_INFO_WUNLOCK(&udbinfo); 782 return error; 783 784 bad: 785 INP_UNLOCK(inp); 786 INP_INFO_WUNLOCK(&udbinfo); 787 m_freem(m); 788 return (error); 789 } 790 791 struct pr_usrreqs udp6_usrreqs = { 792 .pru_abort = udp6_abort, 793 .pru_attach = udp6_attach, 794 .pru_bind = udp6_bind, 795 .pru_connect = udp6_connect, 796 .pru_control = in6_control, 797 .pru_detach = udp6_detach, 798 .pru_disconnect = udp6_disconnect, 799 .pru_peeraddr = in6_mapped_peeraddr, 800 .pru_send = udp6_send, 801 .pru_shutdown = udp_shutdown, 802 .pru_sockaddr = in6_mapped_sockaddr, 803 .pru_sosetlabel = in_pcbsosetlabel, 804 .pru_close = udp6_close 805 }; 806