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