sctp6_usrreq.c revision 170091
1/*- 2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * a) Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * b) Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the distribution. 13 * 14 * c) Neither the name of Cisco Systems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30/* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */ 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 170091 2007-05-29 09:29:03Z rrs $"); 33 34 35#include <netinet/sctp_os.h> 36#include <sys/proc.h> 37#include <netinet/sctp_pcb.h> 38#include <netinet/sctp_header.h> 39#include <netinet/sctp_var.h> 40#if defined(INET6) 41#include <netinet6/sctp6_var.h> 42#endif 43#include <netinet/sctp_sysctl.h> 44#include <netinet/sctp_output.h> 45#include <netinet/sctp_uio.h> 46#include <netinet/sctp_asconf.h> 47#include <netinet/sctputil.h> 48#include <netinet/sctp_indata.h> 49#include <netinet/sctp_timer.h> 50#include <netinet/sctp_auth.h> 51#include <netinet/sctp_input.h> 52#include <netinet/sctp_output.h> 53#include <netinet/sctp_bsd_addr.h> 54 55 56extern struct protosw inetsw[]; 57 58 59 60 61int 62sctp6_input(i_pak, offp, proto) 63 struct mbuf **i_pak; 64 int *offp; 65 int proto; 66{ 67 struct mbuf *m; 68 struct ip6_hdr *ip6; 69 struct sctphdr *sh; 70 struct sctp_inpcb *in6p = NULL; 71 struct sctp_nets *net; 72 int refcount_up = 0; 73 uint32_t check, calc_check; 74 uint32_t vrf_id = 0, table_id = 0; 75 struct inpcb *in6p_ip; 76 struct sctp_chunkhdr *ch; 77 int length, mlen, offset, iphlen; 78 uint8_t ecn_bits; 79 struct sctp_tcb *stcb = NULL; 80 int pkt_len = 0; 81 int off = *offp; 82 83 /* get the VRF and table id's */ 84 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { 85 SCTP_RELEASE_PKT(*i_pak); 86 return (-1); 87 } 88 if (SCTP_GET_PKT_TABLEID(*i_pak, table_id)) { 89 SCTP_RELEASE_PKT(*i_pak); 90 return (-1); 91 } 92 m = SCTP_HEADER_TO_CHAIN(*i_pak); 93 pkt_len = SCTP_HEADER_LEN((*i_pak)); 94 95#ifdef SCTP_PACKET_LOGGING 96 sctp_packet_log(m, pkt_len); 97#endif 98 ip6 = mtod(m, struct ip6_hdr *); 99 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */ 100 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, 101 (int)(sizeof(*sh) + sizeof(*ch))); 102 if (sh == NULL) { 103 SCTP_STAT_INCR(sctps_hdrops); 104 return IPPROTO_DONE; 105 } 106 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 107 iphlen = off; 108 offset = iphlen + sizeof(*sh) + sizeof(*ch); 109 110#if defined(NFAITH) && NFAITH > 0 111 112 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) { 113 /* XXX send icmp6 host/port unreach? */ 114 goto bad; 115 } 116#endif /* NFAITH defined and > 0 */ 117 SCTP_STAT_INCR(sctps_recvpackets); 118 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 119 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n", 120 iphlen, pkt_len); 121 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 122 /* No multi-cast support in SCTP */ 123 goto bad; 124 } 125 /* destination port of 0 is illegal, based on RFC2960. */ 126 if (sh->dest_port == 0) 127 goto bad; 128 if ((sctp_no_csum_on_loopback == 0) || 129 (!SCTP_IS_IT_LOOPBACK(m))) { 130 /* 131 * we do NOT validate things from the loopback if the sysctl 132 * is set to 1. 133 */ 134 check = sh->checksum; /* save incoming checksum */ 135 if ((check == 0) && (sctp_no_csum_on_loopback)) { 136 /* 137 * special hook for where we got a local address 138 * somehow routed across a non IFT_LOOP type 139 * interface 140 */ 141 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst)) 142 goto sctp_skip_csum; 143 } 144 sh->checksum = 0; /* prepare for calc */ 145 calc_check = sctp_calculate_sum(m, &mlen, iphlen); 146 if (calc_check != check) { 147 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 148 calc_check, check, m, mlen, iphlen); 149 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 150 sh, ch, &in6p, &net, vrf_id); 151 /* in6p's ref-count increased && stcb locked */ 152 if ((in6p) && (stcb)) { 153 sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 154 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2); 155 } else if ((in6p != NULL) && (stcb == NULL)) { 156 refcount_up = 1; 157 } 158 SCTP_STAT_INCR(sctps_badsum); 159 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 160 goto bad; 161 } 162 sh->checksum = calc_check; 163 } 164sctp_skip_csum: 165 net = NULL; 166 /* 167 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 168 * IP/SCTP/first chunk header... 169 */ 170 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 171 sh, ch, &in6p, &net, vrf_id); 172 /* in6p's ref-count increased */ 173 if (in6p == NULL) { 174 struct sctp_init_chunk *init_chk, chunk_buf; 175 176 SCTP_STAT_INCR(sctps_noport); 177 if (ch->chunk_type == SCTP_INITIATION) { 178 /* 179 * we do a trick here to get the INIT tag, dig in 180 * and get the tag from the INIT and put it in the 181 * common header. 182 */ 183 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 184 iphlen + sizeof(*sh), sizeof(*init_chk), 185 (uint8_t *) & chunk_buf); 186 if (init_chk) 187 sh->v_tag = init_chk->init.initiate_tag; 188 else 189 sh->v_tag = 0; 190 } 191 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 192 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, 193 table_id); 194 goto bad; 195 } 196 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 197 goto bad; 198 } 199 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 200 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, 201 table_id); 202 goto bad; 203 } else if (stcb == NULL) { 204 refcount_up = 1; 205 } 206 in6p_ip = (struct inpcb *)in6p; 207#ifdef IPSEC 208 /* 209 * Check AH/ESP integrity. 210 */ 211 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) { 212/* XXX */ 213 ipsec6stat.in_polvio++; 214 goto bad; 215 } 216#endif /* IPSEC */ 217 218 /* 219 * CONTROL chunk processing 220 */ 221 offset -= sizeof(*ch); 222 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); 223 224 /* Length now holds the total packet length payload + iphlen */ 225 length = ntohs(ip6->ip6_plen) + iphlen; 226 227 /* sa_ignore NO_NULL_CHK */ 228 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 229 in6p, stcb, net, ecn_bits, vrf_id, table_id); 230 /* inp's ref-count reduced && stcb unlocked */ 231 /* XXX this stuff below gets moved to appropriate parts later... */ 232 if (m) 233 sctp_m_freem(m); 234 if ((in6p) && refcount_up) { 235 /* reduce ref-count */ 236 SCTP_INP_WLOCK(in6p); 237 SCTP_INP_DECR_REF(in6p); 238 SCTP_INP_WUNLOCK(in6p); 239 } 240 return IPPROTO_DONE; 241 242bad: 243 if (stcb) { 244 SCTP_TCB_UNLOCK(stcb); 245 } 246 if ((in6p) && refcount_up) { 247 /* reduce ref-count */ 248 SCTP_INP_WLOCK(in6p); 249 SCTP_INP_DECR_REF(in6p); 250 SCTP_INP_WUNLOCK(in6p); 251 } 252 if (m) 253 sctp_m_freem(m); 254 return IPPROTO_DONE; 255} 256 257 258static void 259sctp6_notify_mbuf(struct sctp_inpcb *inp, 260 struct icmp6_hdr *icmp6, 261 struct sctphdr *sh, 262 struct sctp_tcb *stcb, 263 struct sctp_nets *net) 264{ 265 uint32_t nxtsz; 266 267 if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 268 (icmp6 == NULL) || (sh == NULL)) { 269 goto out; 270 } 271 /* First do we even look at it? */ 272 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) 273 goto out; 274 275 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) { 276 /* not PACKET TO BIG */ 277 goto out; 278 } 279 /* 280 * ok we need to look closely. We could even get smarter and look at 281 * anyone that we sent to in case we get a different ICMP that tells 282 * us there is no way to reach a host, but for this impl, all we 283 * care about is MTU discovery. 284 */ 285 nxtsz = ntohl(icmp6->icmp6_mtu); 286 /* Stop any PMTU timer */ 287 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1); 288 289 /* Adjust destination size limit */ 290 if (net->mtu > nxtsz) { 291 net->mtu = nxtsz; 292 } 293 /* now what about the ep? */ 294 if (stcb->asoc.smallest_mtu > nxtsz) { 295 struct sctp_tmit_chunk *chk; 296 297 /* Adjust that too */ 298 stcb->asoc.smallest_mtu = nxtsz; 299 /* now off to subtract IP_DF flag if needed */ 300 301 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 302 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 303 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 304 } 305 } 306 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 307 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 308 /* 309 * For this guy we also mark for immediate 310 * resend since we sent to big of chunk 311 */ 312 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 313 if (chk->sent != SCTP_DATAGRAM_RESEND) 314 stcb->asoc.sent_queue_retran_cnt++; 315 chk->sent = SCTP_DATAGRAM_RESEND; 316 chk->rec.data.doing_fast_retransmit = 0; 317 318 chk->sent = SCTP_DATAGRAM_RESEND; 319 /* Clear any time so NO RTT is being done */ 320 chk->sent_rcv_time.tv_sec = 0; 321 chk->sent_rcv_time.tv_usec = 0; 322 stcb->asoc.total_flight -= chk->send_size; 323 net->flight_size -= chk->send_size; 324 } 325 } 326 } 327 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 328out: 329 if (stcb) { 330 SCTP_TCB_UNLOCK(stcb); 331 } 332} 333 334 335void 336sctp6_ctlinput(cmd, pktdst, d) 337 int cmd; 338 struct sockaddr *pktdst; 339 void *d; 340{ 341 struct sctphdr sh; 342 struct ip6ctlparam *ip6cp = NULL; 343 uint32_t vrf_id; 344 int cm; 345 346 vrf_id = SCTP_DEFAULT_VRFID; 347 348 if (pktdst->sa_family != AF_INET6 || 349 pktdst->sa_len != sizeof(struct sockaddr_in6)) 350 return; 351 352 if ((unsigned)cmd >= PRC_NCMDS) 353 return; 354 if (PRC_IS_REDIRECT(cmd)) { 355 d = NULL; 356 } else if (inet6ctlerrmap[cmd] == 0) { 357 return; 358 } 359 /* if the parameter is from icmp6, decode it. */ 360 if (d != NULL) { 361 ip6cp = (struct ip6ctlparam *)d; 362 } else { 363 ip6cp = (struct ip6ctlparam *)NULL; 364 } 365 366 if (ip6cp) { 367 /* 368 * XXX: We assume that when IPV6 is non NULL, M and OFF are 369 * valid. 370 */ 371 /* check if we can safely examine src and dst ports */ 372 struct sctp_inpcb *inp = NULL; 373 struct sctp_tcb *stcb = NULL; 374 struct sctp_nets *net = NULL; 375 struct sockaddr_in6 final; 376 377 if (ip6cp->ip6c_m == NULL) 378 return; 379 380 bzero(&sh, sizeof(sh)); 381 bzero(&final, sizeof(final)); 382 inp = NULL; 383 net = NULL; 384 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), 385 (caddr_t)&sh); 386 ip6cp->ip6c_src->sin6_port = sh.src_port; 387 final.sin6_len = sizeof(final); 388 final.sin6_family = AF_INET6; 389 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; 390 final.sin6_port = sh.dest_port; 391 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src, 392 (struct sockaddr *)&final, 393 &inp, &net, 1, vrf_id); 394 /* inp's ref-count increased && stcb locked */ 395 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 396 if (cmd == PRC_MSGSIZE) { 397 sctp6_notify_mbuf(inp, 398 ip6cp->ip6c_icmp6, 399 &sh, 400 stcb, 401 net); 402 /* inp's ref-count reduced && stcb unlocked */ 403 } else { 404 if (cmd == PRC_HOSTDEAD) { 405 cm = EHOSTUNREACH; 406 } else { 407 cm = inet6ctlerrmap[cmd]; 408 } 409 sctp_notify(inp, cm, &sh, 410 (struct sockaddr *)&final, 411 stcb, net); 412 /* inp's ref-count reduced && stcb unlocked */ 413 } 414 } else { 415 if (PRC_IS_REDIRECT(cmd) && inp) { 416 in6_rtchange((struct in6pcb *)inp, 417 inet6ctlerrmap[cmd]); 418 } 419 if (inp) { 420 /* reduce inp's ref-count */ 421 SCTP_INP_WLOCK(inp); 422 SCTP_INP_DECR_REF(inp); 423 SCTP_INP_WUNLOCK(inp); 424 } 425 if (stcb) 426 SCTP_TCB_UNLOCK(stcb); 427 } 428 } 429} 430 431/* 432 * this routine can probably be collasped into the one in sctp_userreq.c 433 * since they do the same thing and now we lookup with a sockaddr 434 */ 435static int 436sctp6_getcred(SYSCTL_HANDLER_ARGS) 437{ 438 struct xucred xuc; 439 struct sockaddr_in6 addrs[2]; 440 struct sctp_inpcb *inp; 441 struct sctp_nets *net; 442 struct sctp_tcb *stcb; 443 int error; 444 uint32_t vrf_id; 445 446 vrf_id = SCTP_DEFAULT_VRFID; 447 448 /* 449 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket 450 * visibility is scoped using cr_canseesocket(), which it is not 451 * here. 452 */ 453 error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_RESERVEDPORT, 454 0); 455 if (error) 456 return (error); 457 458 if (req->newlen != sizeof(addrs)) 459 return (EINVAL); 460 if (req->oldlen != sizeof(struct ucred)) 461 return (EINVAL); 462 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 463 if (error) 464 return (error); 465 466 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]), 467 sin6tosa(&addrs[1]), 468 &inp, &net, 1, vrf_id); 469 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 470 if ((inp != NULL) && (stcb == NULL)) { 471 /* reduce ref-count */ 472 SCTP_INP_WLOCK(inp); 473 SCTP_INP_DECR_REF(inp); 474 goto cred_can_cont; 475 } 476 error = ENOENT; 477 goto out; 478 } 479 SCTP_TCB_UNLOCK(stcb); 480 /* 481 * We use the write lock here, only since in the error leg we need 482 * it. If we used RLOCK, then we would have to 483 * wlock/decr/unlock/rlock. Which in theory could create a hole. 484 * Better to use higher wlock. 485 */ 486 SCTP_INP_WLOCK(inp); 487cred_can_cont: 488 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 489 if (error) { 490 SCTP_INP_WUNLOCK(inp); 491 goto out; 492 } 493 cru2x(inp->sctp_socket->so_cred, &xuc); 494 SCTP_INP_WUNLOCK(inp); 495 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 496out: 497 return (error); 498} 499 500SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 501 0, 0, 502 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 503 504 505/* This is the same as the sctp_abort() could be made common */ 506static void 507sctp6_abort(struct socket *so) 508{ 509 struct sctp_inpcb *inp; 510 uint32_t flags; 511 512 inp = (struct sctp_inpcb *)so->so_pcb; 513 if (inp == 0) 514 return; 515sctp_must_try_again: 516 flags = inp->sctp_flags; 517#ifdef SCTP_LOG_CLOSING 518 sctp_log_closing(inp, NULL, 17); 519#endif 520 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 521 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 522#ifdef SCTP_LOG_CLOSING 523 sctp_log_closing(inp, NULL, 16); 524#endif 525 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 526 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 527 SOCK_LOCK(so); 528 SCTP_SB_CLEAR(so->so_snd); 529 /* 530 * same for the rcv ones, they are only here for the 531 * accounting/select. 532 */ 533 SCTP_SB_CLEAR(so->so_rcv); 534 /* Now null out the reference, we are completely detached. */ 535 so->so_pcb = NULL; 536 SOCK_UNLOCK(so); 537 } else { 538 flags = inp->sctp_flags; 539 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 540 goto sctp_must_try_again; 541 } 542 } 543 return; 544} 545 546static int 547sctp6_attach(struct socket *so, int proto, struct thread *p) 548{ 549 struct in6pcb *inp6; 550 int error; 551 struct sctp_inpcb *inp; 552 553 inp = (struct sctp_inpcb *)so->so_pcb; 554 if (inp != NULL) 555 return EINVAL; 556 557 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 558 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace); 559 if (error) 560 return error; 561 } 562 error = sctp_inpcb_alloc(so); 563 if (error) 564 return error; 565 inp = (struct sctp_inpcb *)so->so_pcb; 566 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 567 inp6 = (struct in6pcb *)inp; 568 569 inp6->inp_vflag |= INP_IPV6; 570 inp6->in6p_hops = -1; /* use kernel default */ 571 inp6->in6p_cksum = -1; /* just to be sure */ 572#ifdef INET 573 /* 574 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 575 * socket as well, because the socket may be bound to an IPv6 576 * wildcard address, which may match an IPv4-mapped IPv6 address. 577 */ 578 inp6->inp_ip_ttl = ip_defttl; 579#endif 580 /* 581 * Hmm what about the IPSEC stuff that is missing here but in 582 * sctp_attach()? 583 */ 584 return 0; 585} 586 587static int 588sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 589{ 590 struct sctp_inpcb *inp; 591 struct in6pcb *inp6; 592 int error; 593 594 inp = (struct sctp_inpcb *)so->so_pcb; 595 if (inp == 0) 596 return EINVAL; 597 598 if (addr) { 599 if ((addr->sa_family == AF_INET6) && 600 (addr->sa_len != sizeof(struct sockaddr_in6))) { 601 return EINVAL; 602 } 603 if ((addr->sa_family == AF_INET) && 604 (addr->sa_len != sizeof(struct sockaddr_in))) { 605 return EINVAL; 606 } 607 } 608 inp6 = (struct in6pcb *)inp; 609 inp6->inp_vflag &= ~INP_IPV4; 610 inp6->inp_vflag |= INP_IPV6; 611 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { 612 if (addr->sa_family == AF_INET) { 613 /* binding v4 addr to v6 socket, so reset flags */ 614 inp6->inp_vflag |= INP_IPV4; 615 inp6->inp_vflag &= ~INP_IPV6; 616 } else { 617 struct sockaddr_in6 *sin6_p; 618 619 sin6_p = (struct sockaddr_in6 *)addr; 620 621 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 622 inp6->inp_vflag |= INP_IPV4; 623 } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 624 struct sockaddr_in sin; 625 626 in6_sin6_2_sin(&sin, sin6_p); 627 inp6->inp_vflag |= INP_IPV4; 628 inp6->inp_vflag &= ~INP_IPV6; 629 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p); 630 return error; 631 } 632 } 633 } else if (addr != NULL) { 634 /* IPV6_V6ONLY socket */ 635 if (addr->sa_family == AF_INET) { 636 /* can't bind v4 addr to v6 only socket! */ 637 return EINVAL; 638 } else { 639 struct sockaddr_in6 *sin6_p; 640 641 sin6_p = (struct sockaddr_in6 *)addr; 642 643 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) 644 /* can't bind v4-mapped addrs either! */ 645 /* NOTE: we don't support SIIT */ 646 return EINVAL; 647 } 648 } 649 error = sctp_inpcb_bind(so, addr, p); 650 return error; 651} 652 653 654static void 655sctp6_close(struct socket *so) 656{ 657 struct sctp_inpcb *inp; 658 uint32_t flags; 659 660 inp = (struct sctp_inpcb *)so->so_pcb; 661 if (inp == 0) 662 return; 663 664 /* 665 * Inform all the lower layer assoc that we are done. 666 */ 667sctp_must_try_again: 668 flags = inp->sctp_flags; 669#ifdef SCTP_LOG_CLOSING 670 sctp_log_closing(inp, NULL, 17); 671#endif 672 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 673 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 674 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) || 675 (so->so_rcv.sb_cc > 0)) { 676#ifdef SCTP_LOG_CLOSING 677 sctp_log_closing(inp, NULL, 13); 678#endif 679 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT 680 ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 681 } else { 682#ifdef SCTP_LOG_CLOSING 683 sctp_log_closing(inp, NULL, 14); 684#endif 685 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, 686 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 687 } 688 /* 689 * The socket is now detached, no matter what the state of 690 * the SCTP association. 691 */ 692 SOCK_LOCK(so); 693 SCTP_SB_CLEAR(so->so_snd); 694 /* 695 * same for the rcv ones, they are only here for the 696 * accounting/select. 697 */ 698 SCTP_SB_CLEAR(so->so_rcv); 699 /* Now null out the reference, we are completely detached. */ 700 so->so_pcb = NULL; 701 SOCK_UNLOCK(so); 702 } else { 703 flags = inp->sctp_flags; 704 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 705 goto sctp_must_try_again; 706 } 707 } 708 return; 709 710} 711 712/* This could be made common with sctp_detach() since they are identical */ 713 714static 715int 716sctp6_disconnect(struct socket *so) 717{ 718 struct sctp_inpcb *inp; 719 720 inp = (struct sctp_inpcb *)so->so_pcb; 721 if (inp == NULL) { 722 return (ENOTCONN); 723 } 724 SCTP_INP_RLOCK(inp); 725 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 726 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) { 727 /* No connection */ 728 SCTP_INP_RUNLOCK(inp); 729 return (ENOTCONN); 730 } else { 731 int some_on_streamwheel = 0; 732 struct sctp_association *asoc; 733 struct sctp_tcb *stcb; 734 735 stcb = LIST_FIRST(&inp->sctp_asoc_list); 736 if (stcb == NULL) { 737 SCTP_INP_RUNLOCK(inp); 738 return (EINVAL); 739 } 740 SCTP_TCB_LOCK(stcb); 741 asoc = &stcb->asoc; 742 if (((so->so_options & SO_LINGER) && 743 (so->so_linger == 0)) || 744 (so->so_rcv.sb_cc > 0)) { 745 if (SCTP_GET_STATE(asoc) != 746 SCTP_STATE_COOKIE_WAIT) { 747 /* Left with Data unread */ 748 struct mbuf *op_err; 749 750 op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT); 751 sctp_send_abort_tcb(stcb, op_err); 752 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 753 } 754 SCTP_INP_RUNLOCK(inp); 755 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 756 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 757 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 758 } 759 sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE, 760 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2); 761 /* No unlock tcb assoc is gone */ 762 return (0); 763 } 764 if (!TAILQ_EMPTY(&asoc->out_wheel)) { 765 /* Check to see if some data queued */ 766 struct sctp_stream_out *outs; 767 768 TAILQ_FOREACH(outs, &asoc->out_wheel, 769 next_spoke) { 770 if (!TAILQ_EMPTY(&outs->outqueue)) { 771 some_on_streamwheel = 1; 772 break; 773 } 774 } 775 } 776 if (TAILQ_EMPTY(&asoc->send_queue) && 777 TAILQ_EMPTY(&asoc->sent_queue) && 778 (some_on_streamwheel == 0)) { 779 /* nothing queued to send, so I'm done... */ 780 if ((SCTP_GET_STATE(asoc) != 781 SCTP_STATE_SHUTDOWN_SENT) && 782 (SCTP_GET_STATE(asoc) != 783 SCTP_STATE_SHUTDOWN_ACK_SENT)) { 784 /* only send SHUTDOWN the first time */ 785 sctp_send_shutdown(stcb, stcb->asoc.primary_destination); 786 sctp_chunk_output(stcb->sctp_ep, stcb, 1); 787 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 788 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 789 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 790 } 791 asoc->state = SCTP_STATE_SHUTDOWN_SENT; 792 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 793 stcb->sctp_ep, stcb, 794 asoc->primary_destination); 795 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 796 stcb->sctp_ep, stcb, 797 asoc->primary_destination); 798 } 799 } else { 800 /* 801 * we still got (or just got) data to send, 802 * so set SHUTDOWN_PENDING 803 */ 804 /* 805 * XXX sockets draft says that MSG_EOF 806 * should be sent with no data. currently, 807 * we will allow user data to be sent first 808 * and move to SHUTDOWN-PENDING 809 */ 810 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 811 } 812 SCTP_TCB_UNLOCK(stcb); 813 SCTP_INP_RUNLOCK(inp); 814 return (0); 815 } 816 } else { 817 /* UDP model does not support this */ 818 SCTP_INP_RUNLOCK(inp); 819 return EOPNOTSUPP; 820 } 821} 822 823 824int 825sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 826 struct mbuf *control, struct thread *p); 827 828 829static int 830sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 831 struct mbuf *control, struct thread *p) 832{ 833 struct sctp_inpcb *inp; 834 struct inpcb *in_inp; 835 struct in6pcb *inp6; 836 837#ifdef INET 838 struct sockaddr_in6 *sin6; 839 840#endif /* INET */ 841 /* No SPL needed since sctp_output does this */ 842 843 inp = (struct sctp_inpcb *)so->so_pcb; 844 if (inp == NULL) { 845 if (control) { 846 SCTP_RELEASE_PKT(control); 847 control = NULL; 848 } 849 SCTP_RELEASE_PKT(m); 850 return EINVAL; 851 } 852 in_inp = (struct inpcb *)inp; 853 inp6 = (struct in6pcb *)inp; 854 /* 855 * For the TCP model we may get a NULL addr, if we are a connected 856 * socket thats ok. 857 */ 858 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 859 (addr == NULL)) { 860 goto connected_type; 861 } 862 if (addr == NULL) { 863 SCTP_RELEASE_PKT(m); 864 if (control) { 865 SCTP_RELEASE_PKT(control); 866 control = NULL; 867 } 868 return (EDESTADDRREQ); 869 } 870#ifdef INET 871 sin6 = (struct sockaddr_in6 *)addr; 872 if (SCTP_IPV6_V6ONLY(inp6)) { 873 /* 874 * if IPV6_V6ONLY flag, we discard datagrams destined to a 875 * v4 addr or v4-mapped addr 876 */ 877 if (addr->sa_family == AF_INET) { 878 return EINVAL; 879 } 880 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 881 return EINVAL; 882 } 883 } 884 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 885 if (!ip6_v6only) { 886 struct sockaddr_in sin; 887 888 /* convert v4-mapped into v4 addr and send */ 889 in6_sin6_2_sin(&sin, sin6); 890 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin, 891 control, p); 892 } else { 893 /* mapped addresses aren't enabled */ 894 return EINVAL; 895 } 896 } 897#endif /* INET */ 898connected_type: 899 /* now what about control */ 900 if (control) { 901 if (inp->control) { 902 SCTP_PRINTF("huh? control set?\n"); 903 SCTP_RELEASE_PKT(inp->control); 904 inp->control = NULL; 905 } 906 inp->control = control; 907 } 908 /* Place the data */ 909 if (inp->pkt) { 910 SCTP_BUF_NEXT(inp->pkt_last) = m; 911 inp->pkt_last = m; 912 } else { 913 inp->pkt_last = inp->pkt = m; 914 } 915 if ( 916 /* FreeBSD and MacOSX uses a flag passed */ 917 ((flags & PRUS_MORETOCOME) == 0) 918 ) { 919 /* 920 * note with the current version this code will only be used 921 * by OpenBSD, NetBSD and FreeBSD have methods for 922 * re-defining sosend() to use sctp_sosend(). One can 923 * optionaly switch back to this code (by changing back the 924 * defininitions but this is not advisable. 925 */ 926 int ret; 927 928 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 929 inp->pkt = NULL; 930 inp->control = NULL; 931 return (ret); 932 } else { 933 return (0); 934 } 935} 936 937static int 938sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 939{ 940 uint32_t vrf_id; 941 int error = 0; 942 struct sctp_inpcb *inp; 943 struct in6pcb *inp6; 944 struct sctp_tcb *stcb; 945 946#ifdef INET 947 struct sockaddr_in6 *sin6; 948 struct sockaddr_storage ss; 949 950#endif /* INET */ 951 952 inp6 = (struct in6pcb *)so->so_pcb; 953 inp = (struct sctp_inpcb *)so->so_pcb; 954 if (inp == 0) { 955 return (ECONNRESET); /* I made the same as TCP since we are 956 * not setup? */ 957 } 958 if (addr == NULL) { 959 return (EINVAL); 960 } 961 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) { 962 return (EINVAL); 963 } 964 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) { 965 return (EINVAL); 966 } 967 vrf_id = inp->def_vrf_id; 968 SCTP_ASOC_CREATE_LOCK(inp); 969 SCTP_INP_RLOCK(inp); 970 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 971 SCTP_PCB_FLAGS_UNBOUND) { 972 /* Bind a ephemeral port */ 973 SCTP_INP_RUNLOCK(inp); 974 error = sctp6_bind(so, NULL, p); 975 if (error) { 976 SCTP_ASOC_CREATE_UNLOCK(inp); 977 978 return (error); 979 } 980 SCTP_INP_RLOCK(inp); 981 } 982 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 983 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 984 /* We are already connected AND the TCP model */ 985 SCTP_INP_RUNLOCK(inp); 986 SCTP_ASOC_CREATE_UNLOCK(inp); 987 return (EADDRINUSE); 988 } 989#ifdef INET 990 sin6 = (struct sockaddr_in6 *)addr; 991 if (SCTP_IPV6_V6ONLY(inp6)) { 992 /* 993 * if IPV6_V6ONLY flag, ignore connections destined to a v4 994 * addr or v4-mapped addr 995 */ 996 if (addr->sa_family == AF_INET) { 997 SCTP_INP_RUNLOCK(inp); 998 SCTP_ASOC_CREATE_UNLOCK(inp); 999 return EINVAL; 1000 } 1001 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1002 SCTP_INP_RUNLOCK(inp); 1003 SCTP_ASOC_CREATE_UNLOCK(inp); 1004 return EINVAL; 1005 } 1006 } 1007 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1008 if (!ip6_v6only) { 1009 /* convert v4-mapped into v4 addr */ 1010 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); 1011 addr = (struct sockaddr *)&ss; 1012 } else { 1013 /* mapped addresses aren't enabled */ 1014 SCTP_INP_RUNLOCK(inp); 1015 SCTP_ASOC_CREATE_UNLOCK(inp); 1016 return EINVAL; 1017 } 1018 } else 1019#endif /* INET */ 1020 addr = addr; /* for true v6 address case */ 1021 1022 /* Now do we connect? */ 1023 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1024 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1025 if (stcb) { 1026 SCTP_TCB_UNLOCK(stcb); 1027 } 1028 SCTP_INP_RUNLOCK(inp); 1029 } else { 1030 SCTP_INP_RUNLOCK(inp); 1031 SCTP_INP_WLOCK(inp); 1032 SCTP_INP_INCR_REF(inp); 1033 SCTP_INP_WUNLOCK(inp); 1034 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 1035 if (stcb == NULL) { 1036 SCTP_INP_WLOCK(inp); 1037 SCTP_INP_DECR_REF(inp); 1038 SCTP_INP_WUNLOCK(inp); 1039 } 1040 } 1041 1042 if (stcb != NULL) { 1043 /* Already have or am bring up an association */ 1044 SCTP_ASOC_CREATE_UNLOCK(inp); 1045 SCTP_TCB_UNLOCK(stcb); 1046 return (EALREADY); 1047 } 1048 /* We are GOOD to go */ 1049 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id); 1050 SCTP_ASOC_CREATE_UNLOCK(inp); 1051 if (stcb == NULL) { 1052 /* Gak! no memory */ 1053 return (error); 1054 } 1055 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1056 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1057 /* Set the connected flag so we can queue data */ 1058 soisconnecting(so); 1059 } 1060 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1061 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1062 1063 /* initialize authentication parameters for the assoc */ 1064 sctp_initialize_auth_params(inp, stcb); 1065 1066 sctp_send_initiate(inp, stcb); 1067 SCTP_TCB_UNLOCK(stcb); 1068 return error; 1069} 1070 1071static int 1072sctp6_getaddr(struct socket *so, struct sockaddr **addr) 1073{ 1074 struct sockaddr_in6 *sin6; 1075 struct sctp_inpcb *inp; 1076 uint32_t vrf_id; 1077 struct sctp_ifa *sctp_ifa; 1078 1079 int error; 1080 1081 /* 1082 * Do the malloc first in case it blocks. 1083 */ 1084 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1085 sin6->sin6_family = AF_INET6; 1086 sin6->sin6_len = sizeof(*sin6); 1087 1088 inp = (struct sctp_inpcb *)so->so_pcb; 1089 if (inp == NULL) { 1090 SCTP_FREE_SONAME(sin6); 1091 return ECONNRESET; 1092 } 1093 SCTP_INP_RLOCK(inp); 1094 sin6->sin6_port = inp->sctp_lport; 1095 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1096 /* For the bound all case you get back 0 */ 1097 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1098 struct sctp_tcb *stcb; 1099 struct sockaddr_in6 *sin_a6; 1100 struct sctp_nets *net; 1101 int fnd; 1102 1103 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1104 if (stcb == NULL) { 1105 goto notConn6; 1106 } 1107 fnd = 0; 1108 sin_a6 = NULL; 1109 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1110 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1111 if (sin_a6 == NULL) 1112 /* this will make coverity happy */ 1113 continue; 1114 1115 if (sin_a6->sin6_family == AF_INET6) { 1116 fnd = 1; 1117 break; 1118 } 1119 } 1120 if ((!fnd) || (sin_a6 == NULL)) { 1121 /* punt */ 1122 goto notConn6; 1123 } 1124 vrf_id = inp->def_vrf_id; 1125 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id); 1126 if (sctp_ifa) { 1127 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 1128 } 1129 } else { 1130 /* For the bound all case you get back 0 */ 1131 notConn6: 1132 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1133 } 1134 } else { 1135 /* Take the first IPv6 address in the list */ 1136 struct sctp_laddr *laddr; 1137 int fnd = 0; 1138 1139 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1140 if (laddr->ifa->address.sa.sa_family == AF_INET6) { 1141 struct sockaddr_in6 *sin_a; 1142 1143 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; 1144 sin6->sin6_addr = sin_a->sin6_addr; 1145 fnd = 1; 1146 break; 1147 } 1148 } 1149 if (!fnd) { 1150 SCTP_FREE_SONAME(sin6); 1151 SCTP_INP_RUNLOCK(inp); 1152 return ENOENT; 1153 } 1154 } 1155 SCTP_INP_RUNLOCK(inp); 1156 /* Scoping things for v6 */ 1157 if ((error = sa6_recoverscope(sin6)) != 0) { 1158 SCTP_FREE_SONAME(sin6); 1159 return (error); 1160 } 1161 (*addr) = (struct sockaddr *)sin6; 1162 return (0); 1163} 1164 1165static int 1166sctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1167{ 1168 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr; 1169 int fnd; 1170 struct sockaddr_in6 *sin_a6; 1171 struct sctp_inpcb *inp; 1172 struct sctp_tcb *stcb; 1173 struct sctp_nets *net; 1174 1175 int error; 1176 1177 /* 1178 * Do the malloc first in case it blocks. 1179 */ 1180 inp = (struct sctp_inpcb *)so->so_pcb; 1181 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 1182 /* UDP type and listeners will drop out here */ 1183 return (ENOTCONN); 1184 } 1185 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1186 sin6->sin6_family = AF_INET6; 1187 sin6->sin6_len = sizeof(*sin6); 1188 1189 /* We must recapture incase we blocked */ 1190 inp = (struct sctp_inpcb *)so->so_pcb; 1191 if (inp == NULL) { 1192 SCTP_FREE_SONAME(sin6); 1193 return ECONNRESET; 1194 } 1195 SCTP_INP_RLOCK(inp); 1196 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1197 if (stcb) { 1198 SCTP_TCB_LOCK(stcb); 1199 } 1200 SCTP_INP_RUNLOCK(inp); 1201 if (stcb == NULL) { 1202 SCTP_FREE_SONAME(sin6); 1203 return ECONNRESET; 1204 } 1205 fnd = 0; 1206 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1207 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1208 if (sin_a6->sin6_family == AF_INET6) { 1209 fnd = 1; 1210 sin6->sin6_port = stcb->rport; 1211 sin6->sin6_addr = sin_a6->sin6_addr; 1212 break; 1213 } 1214 } 1215 SCTP_TCB_UNLOCK(stcb); 1216 if (!fnd) { 1217 /* No IPv4 address */ 1218 SCTP_FREE_SONAME(sin6); 1219 return ENOENT; 1220 } 1221 if ((error = sa6_recoverscope(sin6)) != 0) 1222 return (error); 1223 *addr = (struct sockaddr *)sin6; 1224 return (0); 1225} 1226 1227static int 1228sctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1229{ 1230 struct sockaddr *addr; 1231 struct in6pcb *inp6 = sotoin6pcb(so); 1232 int error; 1233 1234 if (inp6 == NULL) 1235 return EINVAL; 1236 1237 /* allow v6 addresses precedence */ 1238 error = sctp6_getaddr(so, nam); 1239 if (error) { 1240 /* try v4 next if v6 failed */ 1241 error = sctp_ingetaddr(so, nam); 1242 if (error) { 1243 return (error); 1244 } 1245 addr = *nam; 1246 /* if I'm V6ONLY, convert it to v4-mapped */ 1247 if (SCTP_IPV6_V6ONLY(inp6)) { 1248 struct sockaddr_in6 sin6; 1249 1250 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1251 memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1252 1253 } 1254 } 1255 return (error); 1256} 1257 1258 1259static int 1260sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1261{ 1262 struct sockaddr *addr = *nam; 1263 struct in6pcb *inp6 = sotoin6pcb(so); 1264 int error; 1265 1266 if (inp6 == NULL) 1267 return EINVAL; 1268 1269 /* allow v6 addresses precedence */ 1270 error = sctp6_peeraddr(so, nam); 1271 if (error) { 1272 /* try v4 next if v6 failed */ 1273 error = sctp_peeraddr(so, nam); 1274 if (error) { 1275 return (error); 1276 } 1277 /* if I'm V6ONLY, convert it to v4-mapped */ 1278 if (SCTP_IPV6_V6ONLY(inp6)) { 1279 struct sockaddr_in6 sin6; 1280 1281 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1282 memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1283 } 1284 } 1285 return error; 1286} 1287 1288struct pr_usrreqs sctp6_usrreqs = { 1289 .pru_abort = sctp6_abort, 1290 .pru_accept = sctp_accept, 1291 .pru_attach = sctp6_attach, 1292 .pru_bind = sctp6_bind, 1293 .pru_connect = sctp6_connect, 1294 .pru_control = in6_control, 1295 .pru_close = sctp6_close, 1296 .pru_detach = sctp6_close, 1297 .pru_sopoll = sopoll_generic, 1298 .pru_disconnect = sctp6_disconnect, 1299 .pru_listen = sctp_listen, 1300 .pru_peeraddr = sctp6_getpeeraddr, 1301 .pru_send = sctp6_send, 1302 .pru_shutdown = sctp_shutdown, 1303 .pru_sockaddr = sctp6_in6getaddr, 1304 .pru_sosend = sctp_sosend, 1305 .pru_soreceive = sctp_soreceive 1306}; 1307