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