udp6_usrreq.c revision 125776
1356843Sdim/* $FreeBSD: head/sys/netinet6/udp6_usrreq.c 125776 2004-02-13 14:50:01Z ume $ */ 2356843Sdim/* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */ 3356843Sdim 4356843Sdim/* 5356843Sdim * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6356843Sdim * All rights reserved. 7356843Sdim * 8356843Sdim * Redistribution and use in source and binary forms, with or without 9356843Sdim * modification, are permitted provided that the following conditions 10356843Sdim * are met: 11356843Sdim * 1. Redistributions of source code must retain the above copyright 12356843Sdim * notice, this list of conditions and the following disclaimer. 13356843Sdim * 2. Redistributions in binary form must reproduce the above copyright 14356843Sdim * notice, this list of conditions and the following disclaimer in the 15356843Sdim * documentation and/or other materials provided with the distribution. 16356843Sdim * 3. Neither the name of the project nor the names of its contributors 17356843Sdim * may be used to endorse or promote products derived from this software 18356843Sdim * without specific prior written permission. 19356843Sdim * 20356843Sdim * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21356843Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22356843Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23356843Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24356843Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25356843Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26356843Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27356843Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28356843Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29356843Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30356843Sdim * SUCH DAMAGE. 31356843Sdim */ 32356843Sdim 33356843Sdim/* 34356843Sdim * Copyright (c) 1982, 1986, 1989, 1993 35356843Sdim * The Regents of the University of California. All rights reserved. 36356843Sdim * 37356843Sdim * Redistribution and use in source and binary forms, with or without 38356843Sdim * modification, are permitted provided that the following conditions 39356843Sdim * are met: 40356843Sdim * 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 122extern struct protosw inetsw[]; 123static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *)); 124static int udp6_detach __P((struct socket *so)); 125 126static int 127in6_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 149int 150udp6_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 fromsa; 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(&fromsa, m); /* general init */ 227 fromsa.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, n, &opts); 288 289 m_adj(n, off + sizeof(struct udphdr)); 290 if (sbappendaddr(&last->in6p_socket->so_rcv, 291 (struct sockaddr *)&fromsa, 292 n, opts) == 0) { 293 m_freem(n); 294 if (opts) 295 m_freem(opts); 296 udpstat.udps_fullsock++; 297 } else 298 sorwakeup(last->in6p_socket); 299 opts = NULL; 300 } 301 } 302 last = in6p; 303 /* 304 * Don't look for additional matches if this one does 305 * not have either the SO_REUSEPORT or SO_REUSEADDR 306 * socket options set. This heuristic avoids searching 307 * through all pcbs in the common case of a non-shared 308 * port. It assumes that an application will never 309 * clear these options after setting them. 310 */ 311 if ((last->in6p_socket->so_options & 312 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 313 break; 314 } 315 316 if (last == NULL) { 317 /* 318 * No matching pcb found; discard datagram. 319 * (No need to send an ICMP Port Unreachable 320 * for a broadcast or multicast datgram.) 321 */ 322 udpstat.udps_noport++; 323 udpstat.udps_noportmcast++; 324 goto bad; 325 } 326#ifdef IPSEC 327 /* 328 * Check AH/ESP integrity. 329 */ 330 if (ipsec6_in_reject(m, last)) { 331 ipsec6stat.in_polvio++; 332 goto bad; 333 } 334#endif /* IPSEC */ 335#ifdef FAST_IPSEC 336 /* 337 * Check AH/ESP integrity. 338 */ 339 if (ipsec6_in_reject(m, last)) { 340 goto bad; 341 } 342#endif /* FAST_IPSEC */ 343 if (last->in6p_flags & IN6P_CONTROLOPTS 344 || last->in6p_socket->so_options & SO_TIMESTAMP) 345 ip6_savecontrol(last, m, &opts); 346 347 m_adj(m, off + sizeof(struct udphdr)); 348 if (sbappendaddr(&last->in6p_socket->so_rcv, 349 (struct sockaddr *)&fromsa, 350 m, opts) == 0) { 351 udpstat.udps_fullsock++; 352 goto bad; 353 } 354 sorwakeup(last->in6p_socket); 355 return IPPROTO_DONE; 356 } 357 /* 358 * Locate pcb for datagram. 359 */ 360 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, 361 &ip6->ip6_dst, uh->uh_dport, 1, 362 m->m_pkthdr.rcvif); 363 if (in6p == 0) { 364 if (log_in_vain) { 365 char buf[INET6_ADDRSTRLEN]; 366 367 strcpy(buf, ip6_sprintf(&ip6->ip6_dst)); 368 log(LOG_INFO, 369 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 370 buf, ntohs(uh->uh_dport), 371 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport)); 372 } 373 udpstat.udps_noport++; 374 if (m->m_flags & M_MCAST) { 375 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 376 udpstat.udps_noportmcast++; 377 goto bad; 378 } 379 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 380 return IPPROTO_DONE; 381 } 382#ifdef IPSEC 383 /* 384 * Check AH/ESP integrity. 385 */ 386 if (ipsec6_in_reject(m, in6p)) { 387 ipsec6stat.in_polvio++; 388 goto bad; 389 } 390#endif /* IPSEC */ 391#ifdef FAST_IPSEC 392 /* 393 * Check AH/ESP integrity. 394 */ 395 if (ipsec6_in_reject(m, in6p)) { 396 goto bad; 397 } 398#endif /* FAST_IPSEC */ 399 400 /* 401 * Construct sockaddr format source address. 402 * Stuff source address and datagram in user buffer. 403 */ 404 init_sin6(&fromsa, m); /* general init */ 405 fromsa.sin6_port = uh->uh_sport; 406 if (in6p->in6p_flags & IN6P_CONTROLOPTS 407 || in6p->in6p_socket->so_options & SO_TIMESTAMP) 408 ip6_savecontrol(in6p, m, &opts); 409 m_adj(m, off + sizeof(struct udphdr)); 410 if (sbappendaddr(&in6p->in6p_socket->so_rcv, 411 (struct sockaddr *)&fromsa, 412 m, opts) == 0) { 413 udpstat.udps_fullsock++; 414 goto bad; 415 } 416 sorwakeup(in6p->in6p_socket); 417 return IPPROTO_DONE; 418bad: 419 if (m) 420 m_freem(m); 421 if (opts) 422 m_freem(opts); 423 return IPPROTO_DONE; 424} 425 426void 427udp6_ctlinput(cmd, sa, d) 428 int cmd; 429 struct sockaddr *sa; 430 void *d; 431{ 432 struct udphdr uh; 433 struct ip6_hdr *ip6; 434 struct mbuf *m; 435 int off = 0; 436 struct ip6ctlparam *ip6cp = NULL; 437 const struct sockaddr_in6 *sa6_src = NULL; 438 void *cmdarg; 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 cmdarg = ip6cp->ip6c_cmdarg; 465 sa6_src = ip6cp->ip6c_src; 466 } else { 467 m = NULL; 468 ip6 = NULL; 469 cmdarg = NULL; 470 sa6_src = &sa6_any; 471 } 472 473 if (ip6) { 474 /* 475 * XXX: We assume that when IPV6 is non NULL, 476 * M and OFF are valid. 477 */ 478 479 /* check if we can safely examine src and dst ports */ 480 if (m->m_pkthdr.len < off + sizeof(*uhp)) 481 return; 482 483 bzero(&uh, sizeof(uh)); 484 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 485 486 (void) in6_pcbnotify(&udb, sa, uh.uh_dport, 487 (struct sockaddr *)ip6cp->ip6c_src, 488 uh.uh_sport, cmd, cmdarg, notify); 489 } else 490 (void) in6_pcbnotify(&udb, sa, 0, 491 (const struct sockaddr *)sa6_src, 492 0, cmd, cmdarg, notify); 493} 494 495static int 496udp6_getcred(SYSCTL_HANDLER_ARGS) 497{ 498 struct xucred xuc; 499 struct sockaddr_in6 addrs[2]; 500 struct inpcb *inp; 501 int error, s; 502 503 error = suser(req->td); 504 if (error) 505 return (error); 506 507 if (req->newlen != sizeof(addrs)) 508 return (EINVAL); 509 if (req->oldlen != sizeof(struct xucred)) 510 return (EINVAL); 511 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 512 if (error) 513 return (error); 514 s = splnet(); 515 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, 516 addrs[1].sin6_port, 517 &addrs[0].sin6_addr, addrs[0].sin6_port, 518 1, NULL); 519 if (!inp || !inp->inp_socket) { 520 error = ENOENT; 521 goto out; 522 } 523 cru2x(inp->inp_socket->so_cred, &xuc); 524 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 525out: 526 splx(s); 527 return (error); 528} 529 530SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 531 0, 0, 532 udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection"); 533 534static int 535udp6_abort(struct socket *so) 536{ 537 struct inpcb *inp; 538 int s; 539 540 inp = sotoinpcb(so); 541 if (inp == 0) 542 return EINVAL; /* ??? possible? panic instead? */ 543 soisdisconnected(so); 544 s = splnet(); 545 in6_pcbdetach(inp); 546 splx(s); 547 return 0; 548} 549 550static int 551udp6_attach(struct socket *so, int proto, struct thread *td) 552{ 553 struct inpcb *inp; 554 int s, error; 555 556 inp = sotoinpcb(so); 557 if (inp != 0) 558 return EINVAL; 559 560 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 561 error = soreserve(so, udp_sendspace, udp_recvspace); 562 if (error) 563 return error; 564 } 565 s = splnet(); 566 error = in_pcballoc(so, &udbinfo, td, "udp6inp"); 567 splx(s); 568 if (error) 569 return error; 570 inp = (struct inpcb *)so->so_pcb; 571 inp->inp_vflag |= INP_IPV6; 572 if (!ip6_v6only) 573 inp->inp_vflag |= INP_IPV4; 574 inp->in6p_hops = -1; /* use kernel default */ 575 inp->in6p_cksum = -1; /* just to be sure */ 576 /* 577 * XXX: ugly!! 578 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 579 * because the socket may be bound to an IPv6 wildcard address, 580 * which may match an IPv4-mapped IPv6 address. 581 */ 582 inp->inp_ip_ttl = ip_defttl; 583 return 0; 584} 585 586static int 587udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 588{ 589 struct inpcb *inp; 590 int s, error; 591 592 inp = sotoinpcb(so); 593 if (inp == 0) 594 return EINVAL; 595 596 inp->inp_vflag &= ~INP_IPV4; 597 inp->inp_vflag |= INP_IPV6; 598 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 599 struct sockaddr_in6 *sin6_p; 600 601 sin6_p = (struct sockaddr_in6 *)nam; 602 603 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 604 inp->inp_vflag |= INP_IPV4; 605 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 606 struct sockaddr_in sin; 607 608 in6_sin6_2_sin(&sin, sin6_p); 609 inp->inp_vflag |= INP_IPV4; 610 inp->inp_vflag &= ~INP_IPV6; 611 s = splnet(); 612 error = in_pcbbind(inp, (struct sockaddr *)&sin, td); 613 splx(s); 614 return error; 615 } 616 } 617 618 s = splnet(); 619 error = in6_pcbbind(inp, nam, td); 620 splx(s); 621 return error; 622} 623 624static int 625udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 626{ 627 struct inpcb *inp; 628 int s, error; 629 630 inp = sotoinpcb(so); 631 if (inp == 0) 632 return EINVAL; 633 634 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 635 struct sockaddr_in6 *sin6_p; 636 637 sin6_p = (struct sockaddr_in6 *)nam; 638 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 639 struct sockaddr_in sin; 640 641 if (inp->inp_faddr.s_addr != INADDR_ANY) 642 return EISCONN; 643 in6_sin6_2_sin(&sin, sin6_p); 644 s = splnet(); 645 error = in_pcbconnect(inp, (struct sockaddr *)&sin, td); 646 splx(s); 647 if (error == 0) { 648 inp->inp_vflag |= INP_IPV4; 649 inp->inp_vflag &= ~INP_IPV6; 650 soisconnected(so); 651 } 652 return error; 653 } 654 } 655 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 656 return EISCONN; 657 s = splnet(); 658 error = in6_pcbconnect(inp, nam, td); 659 splx(s); 660 if (error == 0) { 661 if (!ip6_v6only) { /* should be non mapped addr */ 662 inp->inp_vflag &= ~INP_IPV4; 663 inp->inp_vflag |= INP_IPV6; 664 } 665 soisconnected(so); 666 } 667 return error; 668} 669 670static int 671udp6_detach(struct socket *so) 672{ 673 struct inpcb *inp; 674 int s; 675 676 inp = sotoinpcb(so); 677 if (inp == 0) 678 return EINVAL; 679 s = splnet(); 680 in6_pcbdetach(inp); 681 splx(s); 682 return 0; 683} 684 685static int 686udp6_disconnect(struct socket *so) 687{ 688 struct inpcb *inp; 689 int s; 690 691 inp = sotoinpcb(so); 692 if (inp == 0) 693 return EINVAL; 694 695 if (inp->inp_vflag & INP_IPV4) { 696 struct pr_usrreqs *pru; 697 698 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 699 return ((*pru->pru_disconnect)(so)); 700 } 701 702 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 703 return ENOTCONN; 704 705 s = splnet(); 706 in6_pcbdisconnect(inp); 707 inp->in6p_laddr = in6addr_any; 708 splx(s); 709 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 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 if (inp == 0) { 722 error = EINVAL; 723 goto bad; 724 } 725 726 if (addr) { 727 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 728 error = EINVAL; 729 goto bad; 730 } 731 if (addr->sa_family != AF_INET6) { 732 error = EAFNOSUPPORT; 733 goto bad; 734 } 735 } 736 737 if (!ip6_v6only) { 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 (sin6) 752 in6_sin6_2_sin_in_sock(addr); 753 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 754 error = ((*pru->pru_send)(so, flags, m, addr, control, 755 td)); 756 /* addr will just be freed in sendit(). */ 757 return error; 758 } 759 } 760 761 return udp6_output(inp, m, addr, control, td); 762 763 bad: 764 m_freem(m); 765 return (error); 766} 767 768struct pr_usrreqs udp6_usrreqs = { 769 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect, 770 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect, 771 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp, 772 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown, 773 in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 774}; 775