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