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