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