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