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