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