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