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 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 66 */ 67 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 #include "opt_ipsec.h" 71 72 #include <sys/param.h> 73 #include <sys/errno.h> 74 #include <sys/kernel.h> 75 #include <sys/lock.h> 76 #include <sys/mbuf.h> 77 #include <sys/proc.h> 78 #include <sys/protosw.h> 79 #include <sys/signalvar.h> 80 #include <sys/socket.h> 81 #include <sys/socketvar.h> 82 #include <sys/stat.h> 83 #include <sys/sx.h> 84 #include <sys/sysctl.h> 85 #include <sys/syslog.h> 86 #include <sys/systm.h> 87 88 #include <net/if.h> 89 #include <net/if_types.h> 90 #include <net/route.h> 91 92 #include <netinet/in.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/in_systm.h> 95 #include <netinet/in_var.h> 96 #include <netinet/ip.h> 97 #include <netinet/ip6.h> 98 #include <netinet/icmp6.h> 99 #include <netinet/ip_var.h> 100 #include <netinet/udp.h> 101 #include <netinet/udp_var.h> 102 #include <netinet6/ip6protosw.h> 103 #include <netinet6/ip6_var.h> 104 #include <netinet6/in6_pcb.h> 105 #include <netinet6/udp6_var.h> 106 107 #ifdef IPSEC 108 #include <netinet6/ipsec.h> 109 #include <netinet6/ipsec6.h> 110 #endif /* IPSEC */ 111 112 #ifdef FAST_IPSEC 113 #include <netipsec/ipsec.h> 114 #include <netipsec/ipsec6.h> 115 #endif /* FAST_IPSEC */ 116 117 /* 118 * UDP protocol inplementation. 119 * Per RFC 768, August, 1980. 120 */ 121 122 extern struct protosw inetsw[]; 123 static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *)); 124 static int udp6_detach __P((struct socket *so)); 125 126 static int 127 in6_mcmatch(in6p, ia6, ifp) 128 struct inpcb *in6p; 129 register struct in6_addr *ia6; 130 struct ifnet *ifp; 131 { 132 struct ip6_moptions *im6o = in6p->in6p_moptions; 133 struct in6_multi_mship *imm; 134 135 if (im6o == NULL) 136 return 0; 137 138 for (imm = im6o->im6o_memberships.lh_first; imm != NULL; 139 imm = imm->i6mm_chain.le_next) { 140 if ((ifp == NULL || 141 imm->i6mm_maddr->in6m_ifp == ifp) && 142 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 143 ia6)) 144 return 1; 145 } 146 return 0; 147 } 148 149 int 150 udp6_input(mp, offp, proto) 151 struct mbuf **mp; 152 int *offp, proto; 153 { 154 struct mbuf *m = *mp; 155 register struct ip6_hdr *ip6; 156 register struct udphdr *uh; 157 register struct inpcb *in6p; 158 struct mbuf *opts = NULL; 159 int off = *offp; 160 int plen, ulen; 161 struct sockaddr_in6 udp_in6; 162 163 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 164 165 ip6 = mtod(m, struct ip6_hdr *); 166 167 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 168 /* XXX send icmp6 host/port unreach? */ 169 m_freem(m); 170 return IPPROTO_DONE; 171 } 172 173 udpstat.udps_ipackets++; 174 175 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 176 uh = (struct udphdr *)((caddr_t)ip6 + off); 177 ulen = ntohs((u_short)uh->uh_ulen); 178 179 if (plen != ulen) { 180 udpstat.udps_badlen++; 181 goto bad; 182 } 183 184 /* 185 * Checksum extended UDP header and data. 186 */ 187 if (uh->uh_sum == 0) 188 udpstat.udps_nosum++; 189 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 190 udpstat.udps_badsum++; 191 goto bad; 192 } 193 194 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 195 struct inpcb *last; 196 197 /* 198 * Deliver a multicast datagram to all sockets 199 * for which the local and remote addresses and ports match 200 * those of the incoming datagram. This allows more than 201 * one process to receive multicasts on the same port. 202 * (This really ought to be done for unicast datagrams as 203 * well, but that would cause problems with existing 204 * applications that open both address-specific sockets and 205 * a wildcard socket listening to the same port -- they would 206 * end up receiving duplicates of every unicast datagram. 207 * Those applications open the multiple sockets to overcome an 208 * inadequacy of the UDP socket interface, but for backwards 209 * compatibility we avoid the problem here rather than 210 * fixing the interface. Maybe 4.5BSD will remedy this?) 211 */ 212 213 /* 214 * In a case that laddr should be set to the link-local 215 * address (this happens in RIPng), the multicast address 216 * specified in the received packet does not match with 217 * laddr. To cure this situation, the matching is relaxed 218 * if the receiving interface is the same as one specified 219 * in the socket and if the destination multicast address 220 * matches one of the multicast groups specified in the socket. 221 */ 222 223 /* 224 * Construct sockaddr format source address. 225 */ 226 init_sin6(&udp_in6, m); /* general init */ 227 udp_in6.sin6_port = uh->uh_sport; 228 /* 229 * KAME note: traditionally we dropped udpiphdr from mbuf here. 230 * We need udphdr for IPsec processing so we do that later. 231 */ 232 233 /* 234 * Locate pcb(s) for datagram. 235 * (Algorithm copied from raw_intr().) 236 */ 237 last = NULL; 238 LIST_FOREACH(in6p, &udb, inp_list) { 239 if ((in6p->inp_vflag & INP_IPV6) == 0) 240 continue; 241 if (in6p->in6p_lport != uh->uh_dport) 242 continue; 243 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 244 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 245 &ip6->ip6_dst) && 246 !in6_mcmatch(in6p, &ip6->ip6_dst, 247 m->m_pkthdr.rcvif)) 248 continue; 249 } 250 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 251 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 252 &ip6->ip6_src) || 253 in6p->in6p_fport != uh->uh_sport) 254 continue; 255 } 256 257 if (last != NULL) { 258 struct mbuf *n; 259 260 #ifdef IPSEC 261 /* 262 * Check AH/ESP integrity. 263 */ 264 if (ipsec6_in_reject(m, last)) 265 ipsec6stat.in_polvio++; 266 /* do not inject data into pcb */ 267 else 268 #endif /* IPSEC */ 269 #ifdef FAST_IPSEC 270 /* 271 * Check AH/ESP integrity. 272 */ 273 if (ipsec6_in_reject(m, last)) 274 ; 275 else 276 #endif /* FAST_IPSEC */ 277 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 278 /* 279 * KAME NOTE: do not 280 * m_copy(m, offset, ...) above. 281 * sbappendaddr() expects M_PKTHDR, 282 * and m_copy() will copy M_PKTHDR 283 * only if offset is 0. 284 */ 285 if (last->in6p_flags & IN6P_CONTROLOPTS 286 || last->in6p_socket->so_options & SO_TIMESTAMP) 287 ip6_savecontrol(last, &opts, 288 ip6, n); 289 290 m_adj(n, off + sizeof(struct udphdr)); 291 if (sbappendaddr(&last->in6p_socket->so_rcv, 292 (struct sockaddr *)&udp_in6, 293 n, opts) == 0) { 294 m_freem(n); 295 if (opts) 296 m_freem(opts); 297 udpstat.udps_fullsock++; 298 } else 299 sorwakeup(last->in6p_socket); 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 udpstat.udps_noport++; 324 udpstat.udps_noportmcast++; 325 goto bad; 326 } 327 #ifdef IPSEC 328 /* 329 * Check AH/ESP integrity. 330 */ 331 if (ipsec6_in_reject(m, last)) { 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 if (sbappendaddr(&last->in6p_socket->so_rcv, 350 (struct sockaddr *)&udp_in6, 351 m, opts) == 0) { 352 udpstat.udps_fullsock++; 353 goto bad; 354 } 355 sorwakeup(last->in6p_socket); 356 return IPPROTO_DONE; 357 } 358 /* 359 * Locate pcb for datagram. 360 */ 361 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, 362 &ip6->ip6_dst, uh->uh_dport, 1, 363 m->m_pkthdr.rcvif); 364 if (in6p == 0) { 365 if (log_in_vain) { 366 char buf[INET6_ADDRSTRLEN]; 367 368 strcpy(buf, ip6_sprintf(&ip6->ip6_dst)); 369 log(LOG_INFO, 370 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 371 buf, ntohs(uh->uh_dport), 372 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport)); 373 } 374 udpstat.udps_noport++; 375 if (m->m_flags & M_MCAST) { 376 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 377 udpstat.udps_noportmcast++; 378 goto bad; 379 } 380 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 381 return IPPROTO_DONE; 382 } 383 #ifdef IPSEC 384 /* 385 * Check AH/ESP integrity. 386 */ 387 if (ipsec6_in_reject(m, in6p)) { 388 ipsec6stat.in_polvio++; 389 goto bad; 390 } 391 #endif /* IPSEC */ 392 #ifdef FAST_IPSEC 393 /* 394 * Check AH/ESP integrity. 395 */ 396 if (ipsec6_in_reject(m, in6p)) { 397 goto bad; 398 } 399 #endif /* FAST_IPSEC */ 400 401 /* 402 * Construct sockaddr format source address. 403 * Stuff source address and datagram in user buffer. 404 */ 405 init_sin6(&udp_in6, m); /* general init */ 406 udp_in6.sin6_port = uh->uh_sport; 407 if (in6p->in6p_flags & IN6P_CONTROLOPTS 408 || in6p->in6p_socket->so_options & SO_TIMESTAMP) 409 ip6_savecontrol(in6p, &opts, ip6, m); 410 m_adj(m, off + sizeof(struct udphdr)); 411 if (sbappendaddr(&in6p->in6p_socket->so_rcv, 412 (struct sockaddr *)&udp_in6, 413 m, opts) == 0) { 414 udpstat.udps_fullsock++; 415 goto bad; 416 } 417 sorwakeup(in6p->in6p_socket); 418 return IPPROTO_DONE; 419 bad: 420 if (m) 421 m_freem(m); 422 if (opts) 423 m_freem(opts); 424 return IPPROTO_DONE; 425 } 426 427 void 428 udp6_ctlinput(cmd, sa, d) 429 int cmd; 430 struct sockaddr *sa; 431 void *d; 432 { 433 struct udphdr uh; 434 struct ip6_hdr *ip6; 435 struct mbuf *m; 436 int off = 0; 437 struct ip6ctlparam *ip6cp = NULL; 438 const struct sockaddr_in6 *sa6_src = NULL; 439 struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify; 440 struct udp_portonly { 441 u_int16_t uh_sport; 442 u_int16_t uh_dport; 443 } *uhp; 444 445 if (sa->sa_family != AF_INET6 || 446 sa->sa_len != sizeof(struct sockaddr_in6)) 447 return; 448 449 if ((unsigned)cmd >= PRC_NCMDS) 450 return; 451 if (PRC_IS_REDIRECT(cmd)) 452 notify = in6_rtchange, d = NULL; 453 else if (cmd == PRC_HOSTDEAD) 454 d = NULL; 455 else if (inet6ctlerrmap[cmd] == 0) 456 return; 457 458 /* if the parameter is from icmp6, decode it. */ 459 if (d != NULL) { 460 ip6cp = (struct ip6ctlparam *)d; 461 m = ip6cp->ip6c_m; 462 ip6 = ip6cp->ip6c_ip6; 463 off = ip6cp->ip6c_off; 464 sa6_src = ip6cp->ip6c_src; 465 } else { 466 m = NULL; 467 ip6 = NULL; 468 sa6_src = &sa6_any; 469 } 470 471 if (ip6) { 472 /* 473 * XXX: We assume that when IPV6 is non NULL, 474 * M and OFF are valid. 475 */ 476 477 /* check if we can safely examine src and dst ports */ 478 if (m->m_pkthdr.len < off + sizeof(*uhp)) 479 return; 480 481 bzero(&uh, sizeof(uh)); 482 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 483 484 (void) in6_pcbnotify(&udb, sa, uh.uh_dport, 485 (struct sockaddr *)ip6cp->ip6c_src, 486 uh.uh_sport, cmd, notify); 487 } else 488 (void) in6_pcbnotify(&udb, sa, 0, 489 (const struct sockaddr *)sa6_src, 490 0, cmd, notify); 491 } 492 493 static int 494 udp6_getcred(SYSCTL_HANDLER_ARGS) 495 { 496 struct xucred xuc; 497 struct sockaddr_in6 addrs[2]; 498 struct inpcb *inp; 499 int error, s; 500 501 error = suser(req->td); 502 if (error) 503 return (error); 504 505 if (req->newlen != sizeof(addrs)) 506 return (EINVAL); 507 if (req->oldlen != sizeof(struct xucred)) 508 return (EINVAL); 509 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 510 if (error) 511 return (error); 512 s = splnet(); 513 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, 514 addrs[1].sin6_port, 515 &addrs[0].sin6_addr, addrs[0].sin6_port, 516 1, NULL); 517 if (!inp || !inp->inp_socket) { 518 error = ENOENT; 519 goto out; 520 } 521 cru2x(inp->inp_socket->so_cred, &xuc); 522 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 523 out: 524 splx(s); 525 return (error); 526 } 527 528 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 529 0, 0, 530 udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection"); 531 532 static int 533 udp6_abort(struct socket *so) 534 { 535 struct inpcb *inp; 536 int s; 537 538 inp = sotoinpcb(so); 539 if (inp == 0) 540 return EINVAL; /* ??? possible? panic instead? */ 541 soisdisconnected(so); 542 s = splnet(); 543 in6_pcbdetach(inp); 544 splx(s); 545 return 0; 546 } 547 548 static int 549 udp6_attach(struct socket *so, int proto, struct thread *td) 550 { 551 struct inpcb *inp; 552 int s, error; 553 554 inp = sotoinpcb(so); 555 if (inp != 0) 556 return EINVAL; 557 558 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 559 error = soreserve(so, udp_sendspace, udp_recvspace); 560 if (error) 561 return error; 562 } 563 s = splnet(); 564 error = in_pcballoc(so, &udbinfo, td); 565 splx(s); 566 if (error) 567 return error; 568 inp = (struct inpcb *)so->so_pcb; 569 inp->inp_vflag |= INP_IPV6; 570 if (!ip6_v6only) 571 inp->inp_vflag |= INP_IPV4; 572 inp->in6p_hops = -1; /* use kernel default */ 573 inp->in6p_cksum = -1; /* just to be sure */ 574 /* 575 * XXX: ugly!! 576 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 577 * because the socket may be bound to an IPv6 wildcard address, 578 * which may match an IPv4-mapped IPv6 address. 579 */ 580 inp->inp_ip_ttl = ip_defttl; 581 return 0; 582 } 583 584 static int 585 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 586 { 587 struct inpcb *inp; 588 int s, error; 589 590 inp = sotoinpcb(so); 591 if (inp == 0) 592 return EINVAL; 593 594 inp->inp_vflag &= ~INP_IPV4; 595 inp->inp_vflag |= INP_IPV6; 596 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 597 struct sockaddr_in6 *sin6_p; 598 599 sin6_p = (struct sockaddr_in6 *)nam; 600 601 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 602 inp->inp_vflag |= INP_IPV4; 603 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 604 struct sockaddr_in sin; 605 606 in6_sin6_2_sin(&sin, sin6_p); 607 inp->inp_vflag |= INP_IPV4; 608 inp->inp_vflag &= ~INP_IPV6; 609 s = splnet(); 610 error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 611 splx(s); 612 return error; 613 } 614 } 615 616 s = splnet(); 617 error = in6_pcbbind(inp, nam, td); 618 splx(s); 619 return error; 620 } 621 622 static int 623 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 624 { 625 struct inpcb *inp; 626 int s, error; 627 628 inp = sotoinpcb(so); 629 if (inp == 0) 630 return EINVAL; 631 632 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 633 struct sockaddr_in6 *sin6_p; 634 635 sin6_p = (struct sockaddr_in6 *)nam; 636 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 637 struct sockaddr_in sin; 638 639 if (inp->inp_faddr.s_addr != INADDR_ANY) 640 return EISCONN; 641 in6_sin6_2_sin(&sin, sin6_p); 642 s = splnet(); 643 error = in_pcbconnect(inp, (struct sockaddr *)&sin, td); 644 splx(s); 645 if (error == 0) { 646 inp->inp_vflag |= INP_IPV4; 647 inp->inp_vflag &= ~INP_IPV6; 648 soisconnected(so); 649 } 650 return error; 651 } 652 } 653 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 654 return EISCONN; 655 s = splnet(); 656 error = in6_pcbconnect(inp, nam, td); 657 splx(s); 658 if (error == 0) { 659 if (!ip6_v6only) { /* should be non mapped addr */ 660 inp->inp_vflag &= ~INP_IPV4; 661 inp->inp_vflag |= INP_IPV6; 662 } 663 soisconnected(so); 664 } 665 return error; 666 } 667 668 static int 669 udp6_detach(struct socket *so) 670 { 671 struct inpcb *inp; 672 int s; 673 674 inp = sotoinpcb(so); 675 if (inp == 0) 676 return EINVAL; 677 s = splnet(); 678 in6_pcbdetach(inp); 679 splx(s); 680 return 0; 681 } 682 683 static int 684 udp6_disconnect(struct socket *so) 685 { 686 struct inpcb *inp; 687 int s; 688 689 inp = sotoinpcb(so); 690 if (inp == 0) 691 return EINVAL; 692 693 if (inp->inp_vflag & INP_IPV4) { 694 struct pr_usrreqs *pru; 695 696 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 697 return ((*pru->pru_disconnect)(so)); 698 } 699 700 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 701 return ENOTCONN; 702 703 s = splnet(); 704 in6_pcbdisconnect(inp); 705 inp->in6p_laddr = in6addr_any; 706 splx(s); 707 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 708 return 0; 709 } 710 711 static int 712 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 713 struct mbuf *control, struct thread *td) 714 { 715 struct inpcb *inp; 716 int error = 0; 717 718 inp = sotoinpcb(so); 719 if (inp == 0) { 720 error = EINVAL; 721 goto bad; 722 } 723 724 if (addr) { 725 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 726 error = EINVAL; 727 goto bad; 728 } 729 if (addr->sa_family != AF_INET6) { 730 error = EAFNOSUPPORT; 731 goto bad; 732 } 733 } 734 735 if (!ip6_v6only) { 736 int hasv4addr; 737 struct sockaddr_in6 *sin6 = 0; 738 739 if (addr == 0) 740 hasv4addr = (inp->inp_vflag & INP_IPV4); 741 else { 742 sin6 = (struct sockaddr_in6 *)addr; 743 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 744 ? 1 : 0; 745 } 746 if (hasv4addr) { 747 struct pr_usrreqs *pru; 748 749 if (sin6) 750 in6_sin6_2_sin_in_sock(addr); 751 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 752 error = ((*pru->pru_send)(so, flags, m, addr, control, 753 td)); 754 /* addr will just be freed in sendit(). */ 755 return error; 756 } 757 } 758 759 return udp6_output(inp, m, addr, control, td); 760 761 bad: 762 m_freem(m); 763 return(error); 764 } 765 766 struct pr_usrreqs udp6_usrreqs = { 767 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect, 768 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect, 769 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp, 770 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown, 771 in6_mapped_sockaddr, sosend, soreceive, sopoll 772 }; 773