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