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