1 /*- 2 * Copyright (c) 2001-2008, 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$"); 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 #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_bsd_addr.h> 52 #include <netinet/udp.h> 53 54 55 56 extern struct sctp_cc_functions sctp_cc_functions[]; 57 extern struct sctp_ss_functions sctp_ss_functions[]; 58 59 void 60 sctp_init(void) 61 { 62 u_long sb_max_adj; 63 64 /* Initialize and modify the sysctled variables */ 65 sctp_init_sysctls(); 66 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE) 67 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8); 68 /* 69 * Allow a user to take no more than 1/2 the number of clusters or 70 * the SB_MAX whichever is smaller for the send window. 71 */ 72 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES)); 73 SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj, 74 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT)); 75 /* 76 * Now for the recv window, should we take the same amount? or 77 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For 78 * now I will just copy. 79 */ 80 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace); 81 SCTP_BASE_VAR(first_time) = 0; 82 SCTP_BASE_VAR(sctp_pcb_initialized) = 0; 83 sctp_pcb_init(); 84 #if defined(SCTP_PACKET_LOGGING) 85 SCTP_BASE_VAR(packet_log_writers) = 0; 86 SCTP_BASE_VAR(packet_log_end) = 0; 87 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE); 88 #endif 89 } 90 91 void 92 sctp_finish(void) 93 { 94 sctp_pcb_finish(); 95 } 96 97 98 99 void 100 sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz) 101 { 102 struct sctp_tmit_chunk *chk; 103 uint16_t overhead; 104 105 /* Adjust that too */ 106 stcb->asoc.smallest_mtu = nxtsz; 107 /* now off to subtract IP_DF flag if needed */ 108 overhead = IP_HDR_SIZE; 109 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) { 110 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id); 111 } 112 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 113 if ((chk->send_size + overhead) > nxtsz) { 114 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 115 } 116 } 117 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 118 if ((chk->send_size + overhead) > nxtsz) { 119 /* 120 * For this guy we also mark for immediate resend 121 * since we sent to big of chunk 122 */ 123 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 124 if (chk->sent < SCTP_DATAGRAM_RESEND) { 125 sctp_flight_size_decrease(chk); 126 sctp_total_flight_decrease(stcb, chk); 127 } 128 if (chk->sent != SCTP_DATAGRAM_RESEND) { 129 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 130 } 131 chk->sent = SCTP_DATAGRAM_RESEND; 132 chk->rec.data.doing_fast_retransmit = 0; 133 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 134 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU, 135 chk->whoTo->flight_size, 136 chk->book_size, 137 (uintptr_t) chk->whoTo, 138 chk->rec.data.TSN_seq); 139 } 140 /* Clear any time so NO RTT is being done */ 141 chk->do_rtt = 0; 142 } 143 } 144 } 145 146 #ifdef INET 147 static void 148 sctp_notify_mbuf(struct sctp_inpcb *inp, 149 struct sctp_tcb *stcb, 150 struct sctp_nets *net, 151 struct ip *ip, 152 struct sctphdr *sh) 153 { 154 struct icmp *icmph; 155 int totsz, tmr_stopped = 0; 156 uint16_t nxtsz; 157 158 /* protection */ 159 if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 160 (ip == NULL) || (sh == NULL)) { 161 if (stcb != NULL) { 162 SCTP_TCB_UNLOCK(stcb); 163 } 164 return; 165 } 166 /* First job is to verify the vtag matches what I would send */ 167 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 168 SCTP_TCB_UNLOCK(stcb); 169 return; 170 } 171 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) - 172 sizeof(struct ip))); 173 if (icmph->icmp_type != ICMP_UNREACH) { 174 /* We only care about unreachable */ 175 SCTP_TCB_UNLOCK(stcb); 176 return; 177 } 178 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) { 179 /* not a unreachable message due to frag. */ 180 SCTP_TCB_UNLOCK(stcb); 181 return; 182 } 183 totsz = ntohs(ip->ip_len); 184 185 nxtsz = ntohs(icmph->icmp_nextmtu); 186 if (nxtsz == 0) { 187 /* 188 * old type router that does not tell us what the next size 189 * mtu is. Rats we will have to guess (in a educated fashion 190 * of course) 191 */ 192 nxtsz = sctp_get_prev_mtu(totsz); 193 } 194 /* Stop any PMTU timer */ 195 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 196 tmr_stopped = 1; 197 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 198 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1); 199 } 200 /* Adjust destination size limit */ 201 if (net->mtu > nxtsz) { 202 net->mtu = nxtsz; 203 if (net->port) { 204 net->mtu -= sizeof(struct udphdr); 205 } 206 } 207 /* now what about the ep? */ 208 if (stcb->asoc.smallest_mtu > nxtsz) { 209 sctp_pathmtu_adjustment(stcb, nxtsz); 210 } 211 if (tmr_stopped) 212 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 213 214 SCTP_TCB_UNLOCK(stcb); 215 } 216 217 #endif 218 219 void 220 sctp_notify(struct sctp_inpcb *inp, 221 struct ip *ip, 222 struct sctphdr *sh, 223 struct sockaddr *to, 224 struct sctp_tcb *stcb, 225 struct sctp_nets *net) 226 { 227 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 228 struct socket *so; 229 230 #endif 231 struct icmp *icmph; 232 233 /* protection */ 234 if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 235 (sh == NULL) || (to == NULL)) { 236 if (stcb) 237 SCTP_TCB_UNLOCK(stcb); 238 return; 239 } 240 /* First job is to verify the vtag matches what I would send */ 241 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 242 SCTP_TCB_UNLOCK(stcb); 243 return; 244 } 245 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) - 246 sizeof(struct ip))); 247 if (icmph->icmp_type != ICMP_UNREACH) { 248 /* We only care about unreachable */ 249 SCTP_TCB_UNLOCK(stcb); 250 return; 251 } 252 if ((icmph->icmp_code == ICMP_UNREACH_NET) || 253 (icmph->icmp_code == ICMP_UNREACH_HOST) || 254 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) || 255 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) || 256 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) || 257 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) || 258 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) || 259 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) { 260 261 /* 262 * Hmm reachablity problems we must examine closely. If its 263 * not reachable, we may have lost a network. Or if there is 264 * NO protocol at the other end named SCTP. well we consider 265 * it a OOTB abort. 266 */ 267 if (net->dest_state & SCTP_ADDR_REACHABLE) { 268 /* Ok that destination is NOT reachable */ 269 net->dest_state &= ~SCTP_ADDR_REACHABLE; 270 net->dest_state &= ~SCTP_ADDR_PF; 271 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 272 stcb, 0, 273 (void *)net, SCTP_SO_NOT_LOCKED); 274 } 275 SCTP_TCB_UNLOCK(stcb); 276 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) || 277 (icmph->icmp_code == ICMP_UNREACH_PORT)) { 278 /* 279 * Here the peer is either playing tricks on us, including 280 * an address that belongs to someone who does not support 281 * SCTP OR was a userland implementation that shutdown and 282 * now is dead. In either case treat it like a OOTB abort 283 * with no TCB 284 */ 285 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED); 286 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 287 so = SCTP_INP_SO(inp); 288 atomic_add_int(&stcb->asoc.refcnt, 1); 289 SCTP_TCB_UNLOCK(stcb); 290 SCTP_SOCKET_LOCK(so, 1); 291 SCTP_TCB_LOCK(stcb); 292 atomic_subtract_int(&stcb->asoc.refcnt, 1); 293 #endif 294 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 295 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 296 SCTP_SOCKET_UNLOCK(so, 1); 297 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */ 298 #endif 299 /* no need to unlock here, since the TCB is gone */ 300 } else { 301 SCTP_TCB_UNLOCK(stcb); 302 } 303 } 304 305 #ifdef INET 306 void 307 sctp_ctlinput(cmd, sa, vip) 308 int cmd; 309 struct sockaddr *sa; 310 void *vip; 311 { 312 struct ip *ip = vip; 313 struct sctphdr *sh; 314 uint32_t vrf_id; 315 316 /* FIX, for non-bsd is this right? */ 317 vrf_id = SCTP_DEFAULT_VRFID; 318 if (sa->sa_family != AF_INET || 319 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) { 320 return; 321 } 322 if (PRC_IS_REDIRECT(cmd)) { 323 ip = 0; 324 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) { 325 return; 326 } 327 if (ip) { 328 struct sctp_inpcb *inp = NULL; 329 struct sctp_tcb *stcb = NULL; 330 struct sctp_nets *net = NULL; 331 struct sockaddr_in to, from; 332 333 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 334 bzero(&to, sizeof(to)); 335 bzero(&from, sizeof(from)); 336 from.sin_family = to.sin_family = AF_INET; 337 from.sin_len = to.sin_len = sizeof(to); 338 from.sin_port = sh->src_port; 339 from.sin_addr = ip->ip_src; 340 to.sin_port = sh->dest_port; 341 to.sin_addr = ip->ip_dst; 342 343 /* 344 * 'to' holds the dest of the packet that failed to be sent. 345 * 'from' holds our local endpoint address. Thus we reverse 346 * the to and the from in the lookup. 347 */ 348 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&to, 349 (struct sockaddr *)&from, 350 &inp, &net, 1, vrf_id); 351 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 352 if (cmd != PRC_MSGSIZE) { 353 sctp_notify(inp, ip, sh, 354 (struct sockaddr *)&to, stcb, 355 net); 356 } else { 357 /* handle possible ICMP size messages */ 358 sctp_notify_mbuf(inp, stcb, net, ip, sh); 359 } 360 } else { 361 if ((stcb == NULL) && (inp != NULL)) { 362 /* reduce ref-count */ 363 SCTP_INP_WLOCK(inp); 364 SCTP_INP_DECR_REF(inp); 365 SCTP_INP_WUNLOCK(inp); 366 } 367 if (stcb) { 368 SCTP_TCB_UNLOCK(stcb); 369 } 370 } 371 } 372 return; 373 } 374 375 #endif 376 377 static int 378 sctp_getcred(SYSCTL_HANDLER_ARGS) 379 { 380 struct xucred xuc; 381 struct sockaddr_in addrs[2]; 382 struct sctp_inpcb *inp; 383 struct sctp_nets *net; 384 struct sctp_tcb *stcb; 385 int error; 386 uint32_t vrf_id; 387 388 /* FIX, for non-bsd is this right? */ 389 vrf_id = SCTP_DEFAULT_VRFID; 390 391 error = priv_check(req->td, PRIV_NETINET_GETCRED); 392 393 if (error) 394 return (error); 395 396 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 397 if (error) 398 return (error); 399 400 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[1]), 401 sintosa(&addrs[0]), 402 &inp, &net, 1, vrf_id); 403 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 404 if ((inp != NULL) && (stcb == NULL)) { 405 /* reduce ref-count */ 406 SCTP_INP_WLOCK(inp); 407 SCTP_INP_DECR_REF(inp); 408 goto cred_can_cont; 409 } 410 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 411 error = ENOENT; 412 goto out; 413 } 414 SCTP_TCB_UNLOCK(stcb); 415 /* 416 * We use the write lock here, only since in the error leg we need 417 * it. If we used RLOCK, then we would have to 418 * wlock/decr/unlock/rlock. Which in theory could create a hole. 419 * Better to use higher wlock. 420 */ 421 SCTP_INP_WLOCK(inp); 422 cred_can_cont: 423 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 424 if (error) { 425 SCTP_INP_WUNLOCK(inp); 426 goto out; 427 } 428 cru2x(inp->sctp_socket->so_cred, &xuc); 429 SCTP_INP_WUNLOCK(inp); 430 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 431 out: 432 return (error); 433 } 434 435 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 436 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection"); 437 438 439 #ifdef INET 440 static void 441 sctp_abort(struct socket *so) 442 { 443 struct sctp_inpcb *inp; 444 uint32_t flags; 445 446 inp = (struct sctp_inpcb *)so->so_pcb; 447 if (inp == NULL) { 448 return; 449 } 450 sctp_must_try_again: 451 flags = inp->sctp_flags; 452 #ifdef SCTP_LOG_CLOSING 453 sctp_log_closing(inp, NULL, 17); 454 #endif 455 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 456 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 457 #ifdef SCTP_LOG_CLOSING 458 sctp_log_closing(inp, NULL, 16); 459 #endif 460 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 461 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 462 SOCK_LOCK(so); 463 SCTP_SB_CLEAR(so->so_snd); 464 /* 465 * same for the rcv ones, they are only here for the 466 * accounting/select. 467 */ 468 SCTP_SB_CLEAR(so->so_rcv); 469 470 /* Now null out the reference, we are completely detached. */ 471 so->so_pcb = NULL; 472 SOCK_UNLOCK(so); 473 } else { 474 flags = inp->sctp_flags; 475 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 476 goto sctp_must_try_again; 477 } 478 } 479 return; 480 } 481 482 static int 483 sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED) 484 { 485 struct sctp_inpcb *inp; 486 struct inpcb *ip_inp; 487 int error; 488 uint32_t vrf_id = SCTP_DEFAULT_VRFID; 489 490 #ifdef IPSEC 491 uint32_t flags; 492 493 #endif 494 495 inp = (struct sctp_inpcb *)so->so_pcb; 496 if (inp != 0) { 497 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 498 return (EINVAL); 499 } 500 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 501 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 502 if (error) { 503 return (error); 504 } 505 } 506 error = sctp_inpcb_alloc(so, vrf_id); 507 if (error) { 508 return (error); 509 } 510 inp = (struct sctp_inpcb *)so->so_pcb; 511 SCTP_INP_WLOCK(inp); 512 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */ 513 ip_inp = &inp->ip_inp.inp; 514 ip_inp->inp_vflag |= INP_IPV4; 515 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 516 #ifdef IPSEC 517 error = ipsec_init_policy(so, &ip_inp->inp_sp); 518 #ifdef SCTP_LOG_CLOSING 519 sctp_log_closing(inp, NULL, 17); 520 #endif 521 if (error != 0) { 522 try_again: 523 flags = inp->sctp_flags; 524 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 525 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 526 #ifdef SCTP_LOG_CLOSING 527 sctp_log_closing(inp, NULL, 15); 528 #endif 529 SCTP_INP_WUNLOCK(inp); 530 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 531 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 532 } else { 533 flags = inp->sctp_flags; 534 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 535 goto try_again; 536 } else { 537 SCTP_INP_WUNLOCK(inp); 538 } 539 } 540 return (error); 541 } 542 #endif /* IPSEC */ 543 SCTP_INP_WUNLOCK(inp); 544 return (0); 545 } 546 547 static int 548 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 549 { 550 struct sctp_inpcb *inp; 551 552 inp = (struct sctp_inpcb *)so->so_pcb; 553 if (inp == NULL) { 554 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 555 return (EINVAL); 556 } 557 if (addr != NULL) { 558 if ((addr->sa_family != AF_INET) || 559 (addr->sa_len != sizeof(struct sockaddr_in))) { 560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 561 return (EINVAL); 562 } 563 } 564 return (sctp_inpcb_bind(so, addr, NULL, p)); 565 } 566 567 #endif 568 void 569 sctp_close(struct socket *so) 570 { 571 struct sctp_inpcb *inp; 572 uint32_t flags; 573 574 inp = (struct sctp_inpcb *)so->so_pcb; 575 if (inp == NULL) 576 return; 577 578 /* 579 * Inform all the lower layer assoc that we are done. 580 */ 581 sctp_must_try_again: 582 flags = inp->sctp_flags; 583 #ifdef SCTP_LOG_CLOSING 584 sctp_log_closing(inp, NULL, 17); 585 #endif 586 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 587 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 588 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) || 589 (so->so_rcv.sb_cc > 0)) { 590 #ifdef SCTP_LOG_CLOSING 591 sctp_log_closing(inp, NULL, 13); 592 #endif 593 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 594 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 595 } else { 596 #ifdef SCTP_LOG_CLOSING 597 sctp_log_closing(inp, NULL, 14); 598 #endif 599 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, 600 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 601 } 602 /* 603 * The socket is now detached, no matter what the state of 604 * the SCTP association. 605 */ 606 SOCK_LOCK(so); 607 SCTP_SB_CLEAR(so->so_snd); 608 /* 609 * same for the rcv ones, they are only here for the 610 * accounting/select. 611 */ 612 SCTP_SB_CLEAR(so->so_rcv); 613 614 /* Now null out the reference, we are completely detached. */ 615 so->so_pcb = NULL; 616 SOCK_UNLOCK(so); 617 } else { 618 flags = inp->sctp_flags; 619 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 620 goto sctp_must_try_again; 621 } 622 } 623 return; 624 } 625 626 627 int 628 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 629 struct mbuf *control, struct thread *p); 630 631 632 int 633 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 634 struct mbuf *control, struct thread *p) 635 { 636 struct sctp_inpcb *inp; 637 int error; 638 639 inp = (struct sctp_inpcb *)so->so_pcb; 640 if (inp == NULL) { 641 if (control) { 642 sctp_m_freem(control); 643 control = NULL; 644 } 645 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 646 sctp_m_freem(m); 647 return (EINVAL); 648 } 649 /* Got to have an to address if we are NOT a connected socket */ 650 if ((addr == NULL) && 651 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || 652 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))) { 653 goto connected_type; 654 } else if (addr == NULL) { 655 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ); 656 error = EDESTADDRREQ; 657 sctp_m_freem(m); 658 if (control) { 659 sctp_m_freem(control); 660 control = NULL; 661 } 662 return (error); 663 } 664 #ifdef INET6 665 if (addr->sa_family != AF_INET) { 666 /* must be a v4 address! */ 667 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ); 668 sctp_m_freem(m); 669 if (control) { 670 sctp_m_freem(control); 671 control = NULL; 672 } 673 error = EDESTADDRREQ; 674 return (error); 675 } 676 #endif /* INET6 */ 677 connected_type: 678 /* now what about control */ 679 if (control) { 680 if (inp->control) { 681 SCTP_PRINTF("huh? control set?\n"); 682 sctp_m_freem(inp->control); 683 inp->control = NULL; 684 } 685 inp->control = control; 686 } 687 /* Place the data */ 688 if (inp->pkt) { 689 SCTP_BUF_NEXT(inp->pkt_last) = m; 690 inp->pkt_last = m; 691 } else { 692 inp->pkt_last = inp->pkt = m; 693 } 694 if ( 695 /* FreeBSD uses a flag passed */ 696 ((flags & PRUS_MORETOCOME) == 0) 697 ) { 698 /* 699 * note with the current version this code will only be used 700 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for 701 * re-defining sosend to use the sctp_sosend. One can 702 * optionally switch back to this code (by changing back the 703 * definitions) but this is not advisable. This code is used 704 * by FreeBSD when sending a file with sendfile() though. 705 */ 706 int ret; 707 708 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 709 inp->pkt = NULL; 710 inp->control = NULL; 711 return (ret); 712 } else { 713 return (0); 714 } 715 } 716 717 int 718 sctp_disconnect(struct socket *so) 719 { 720 struct sctp_inpcb *inp; 721 722 inp = (struct sctp_inpcb *)so->so_pcb; 723 if (inp == NULL) { 724 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN); 725 return (ENOTCONN); 726 } 727 SCTP_INP_RLOCK(inp); 728 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 729 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 730 if (LIST_EMPTY(&inp->sctp_asoc_list)) { 731 /* No connection */ 732 SCTP_INP_RUNLOCK(inp); 733 return (0); 734 } else { 735 struct sctp_association *asoc; 736 struct sctp_tcb *stcb; 737 738 stcb = LIST_FIRST(&inp->sctp_asoc_list); 739 if (stcb == NULL) { 740 SCTP_INP_RUNLOCK(inp); 741 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 742 return (EINVAL); 743 } 744 SCTP_TCB_LOCK(stcb); 745 asoc = &stcb->asoc; 746 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 747 /* We are about to be freed, out of here */ 748 SCTP_TCB_UNLOCK(stcb); 749 SCTP_INP_RUNLOCK(inp); 750 return (0); 751 } 752 if (((so->so_options & SO_LINGER) && 753 (so->so_linger == 0)) || 754 (so->so_rcv.sb_cc > 0)) { 755 if (SCTP_GET_STATE(asoc) != 756 SCTP_STATE_COOKIE_WAIT) { 757 /* Left with Data unread */ 758 struct mbuf *err; 759 760 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA); 761 if (err) { 762 /* 763 * Fill in the user 764 * initiated abort 765 */ 766 struct sctp_paramhdr *ph; 767 768 ph = mtod(err, struct sctp_paramhdr *); 769 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr); 770 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); 771 ph->param_length = htons(SCTP_BUF_LEN(err)); 772 } 773 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED); 774 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 775 } 776 SCTP_INP_RUNLOCK(inp); 777 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 778 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 779 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 780 } 781 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3); 782 /* No unlock tcb assoc is gone */ 783 return (0); 784 } 785 if (TAILQ_EMPTY(&asoc->send_queue) && 786 TAILQ_EMPTY(&asoc->sent_queue) && 787 (asoc->stream_queue_cnt == 0)) { 788 /* there is nothing queued to send, so done */ 789 if (asoc->locked_on_sending) { 790 goto abort_anyway; 791 } 792 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 793 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 794 /* only send SHUTDOWN 1st time thru */ 795 struct sctp_nets *netp; 796 797 if (stcb->asoc.alternate) { 798 netp = stcb->asoc.alternate; 799 } else { 800 netp = stcb->asoc.primary_destination; 801 } 802 sctp_stop_timers_for_shutdown(stcb); 803 sctp_send_shutdown(stcb, netp); 804 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED); 805 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 806 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 807 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 808 } 809 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); 810 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 811 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 812 stcb->sctp_ep, stcb, netp); 813 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 814 stcb->sctp_ep, stcb, netp); 815 816 } 817 } else { 818 /* 819 * we still got (or just got) data to send, 820 * so set SHUTDOWN_PENDING 821 */ 822 /* 823 * XXX sockets draft says that SCTP_EOF 824 * should be sent with no data. currently, 825 * we will allow user data to be sent first 826 * and move to SHUTDOWN-PENDING 827 */ 828 struct sctp_nets *netp; 829 830 if (stcb->asoc.alternate) { 831 netp = stcb->asoc.alternate; 832 } else { 833 netp = stcb->asoc.primary_destination; 834 } 835 836 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 837 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 838 netp); 839 if (asoc->locked_on_sending) { 840 /* Locked to send out the data */ 841 struct sctp_stream_queue_pending *sp; 842 843 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); 844 if (sp == NULL) { 845 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n", 846 asoc->locked_on_sending->stream_no); 847 } else { 848 if ((sp->length == 0) && (sp->msg_is_complete == 0)) 849 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 850 } 851 } 852 if (TAILQ_EMPTY(&asoc->send_queue) && 853 TAILQ_EMPTY(&asoc->sent_queue) && 854 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 855 struct mbuf *op_err; 856 857 abort_anyway: 858 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 859 0, M_NOWAIT, 1, MT_DATA); 860 if (op_err) { 861 /* 862 * Fill in the user 863 * initiated abort 864 */ 865 struct sctp_paramhdr *ph; 866 uint32_t *ippp; 867 868 SCTP_BUF_LEN(op_err) = 869 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)); 870 ph = mtod(op_err, 871 struct sctp_paramhdr *); 872 ph->param_type = htons( 873 SCTP_CAUSE_USER_INITIATED_ABT); 874 ph->param_length = htons(SCTP_BUF_LEN(op_err)); 875 ippp = (uint32_t *) (ph + 1); 876 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4); 877 } 878 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4; 879 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); 880 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 881 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 882 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 883 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 884 } 885 SCTP_INP_RUNLOCK(inp); 886 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5); 887 return (0); 888 } else { 889 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); 890 } 891 } 892 soisdisconnecting(so); 893 SCTP_TCB_UNLOCK(stcb); 894 SCTP_INP_RUNLOCK(inp); 895 return (0); 896 } 897 /* not reached */ 898 } else { 899 /* UDP model does not support this */ 900 SCTP_INP_RUNLOCK(inp); 901 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 902 return (EOPNOTSUPP); 903 } 904 } 905 906 int 907 sctp_flush(struct socket *so, int how) 908 { 909 /* 910 * We will just clear out the values and let subsequent close clear 911 * out the data, if any. Note if the user did a shutdown(SHUT_RD) 912 * they will not be able to read the data, the socket will block 913 * that from happening. 914 */ 915 struct sctp_inpcb *inp; 916 917 inp = (struct sctp_inpcb *)so->so_pcb; 918 if (inp == NULL) { 919 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 920 return (EINVAL); 921 } 922 SCTP_INP_RLOCK(inp); 923 /* For the 1 to many model this does nothing */ 924 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 925 SCTP_INP_RUNLOCK(inp); 926 return (0); 927 } 928 SCTP_INP_RUNLOCK(inp); 929 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) { 930 /* 931 * First make sure the sb will be happy, we don't use these 932 * except maybe the count 933 */ 934 SCTP_INP_WLOCK(inp); 935 SCTP_INP_READ_LOCK(inp); 936 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ; 937 SCTP_INP_READ_UNLOCK(inp); 938 SCTP_INP_WUNLOCK(inp); 939 so->so_rcv.sb_cc = 0; 940 so->so_rcv.sb_mbcnt = 0; 941 so->so_rcv.sb_mb = NULL; 942 } 943 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) { 944 /* 945 * First make sure the sb will be happy, we don't use these 946 * except maybe the count 947 */ 948 so->so_snd.sb_cc = 0; 949 so->so_snd.sb_mbcnt = 0; 950 so->so_snd.sb_mb = NULL; 951 952 } 953 return (0); 954 } 955 956 int 957 sctp_shutdown(struct socket *so) 958 { 959 struct sctp_inpcb *inp; 960 961 inp = (struct sctp_inpcb *)so->so_pcb; 962 if (inp == NULL) { 963 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 964 return (EINVAL); 965 } 966 SCTP_INP_RLOCK(inp); 967 /* For UDP model this is a invalid call */ 968 if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 969 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { 970 /* Restore the flags that the soshutdown took away. */ 971 SOCKBUF_LOCK(&so->so_rcv); 972 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE; 973 SOCKBUF_UNLOCK(&so->so_rcv); 974 /* This proc will wakeup for read and do nothing (I hope) */ 975 SCTP_INP_RUNLOCK(inp); 976 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 977 return (EOPNOTSUPP); 978 } 979 /* 980 * Ok if we reach here its the TCP model and it is either a SHUT_WR 981 * or SHUT_RDWR. This means we put the shutdown flag against it. 982 */ 983 { 984 struct sctp_tcb *stcb; 985 struct sctp_association *asoc; 986 987 if ((so->so_state & 988 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) { 989 SCTP_INP_RUNLOCK(inp); 990 return (ENOTCONN); 991 } 992 socantsendmore(so); 993 994 stcb = LIST_FIRST(&inp->sctp_asoc_list); 995 if (stcb == NULL) { 996 /* 997 * Ok we hit the case that the shutdown call was 998 * made after an abort or something. Nothing to do 999 * now. 1000 */ 1001 SCTP_INP_RUNLOCK(inp); 1002 return (0); 1003 } 1004 SCTP_TCB_LOCK(stcb); 1005 asoc = &stcb->asoc; 1006 if (TAILQ_EMPTY(&asoc->send_queue) && 1007 TAILQ_EMPTY(&asoc->sent_queue) && 1008 (asoc->stream_queue_cnt == 0)) { 1009 if (asoc->locked_on_sending) { 1010 goto abort_anyway; 1011 } 1012 /* there is nothing queued to send, so I'm done... */ 1013 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 1014 /* only send SHUTDOWN the first time through */ 1015 struct sctp_nets *netp; 1016 1017 if (stcb->asoc.alternate) { 1018 netp = stcb->asoc.alternate; 1019 } else { 1020 netp = stcb->asoc.primary_destination; 1021 } 1022 sctp_stop_timers_for_shutdown(stcb); 1023 sctp_send_shutdown(stcb, netp); 1024 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED); 1025 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 1026 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 1027 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 1028 } 1029 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); 1030 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 1031 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 1032 stcb->sctp_ep, stcb, netp); 1033 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 1034 stcb->sctp_ep, stcb, netp); 1035 } 1036 } else { 1037 /* 1038 * we still got (or just got) data to send, so set 1039 * SHUTDOWN_PENDING 1040 */ 1041 struct sctp_nets *netp; 1042 1043 if (stcb->asoc.alternate) { 1044 netp = stcb->asoc.alternate; 1045 } else { 1046 netp = stcb->asoc.primary_destination; 1047 } 1048 1049 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 1050 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 1051 netp); 1052 1053 if (asoc->locked_on_sending) { 1054 /* Locked to send out the data */ 1055 struct sctp_stream_queue_pending *sp; 1056 1057 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); 1058 if (sp == NULL) { 1059 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n", 1060 asoc->locked_on_sending->stream_no); 1061 } else { 1062 if ((sp->length == 0) && (sp->msg_is_complete == 0)) { 1063 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 1064 } 1065 } 1066 } 1067 if (TAILQ_EMPTY(&asoc->send_queue) && 1068 TAILQ_EMPTY(&asoc->sent_queue) && 1069 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 1070 struct mbuf *op_err; 1071 1072 abort_anyway: 1073 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 1074 0, M_NOWAIT, 1, MT_DATA); 1075 if (op_err) { 1076 /* Fill in the user initiated abort */ 1077 struct sctp_paramhdr *ph; 1078 uint32_t *ippp; 1079 1080 SCTP_BUF_LEN(op_err) = 1081 sizeof(struct sctp_paramhdr) + sizeof(uint32_t); 1082 ph = mtod(op_err, 1083 struct sctp_paramhdr *); 1084 ph->param_type = htons( 1085 SCTP_CAUSE_USER_INITIATED_ABT); 1086 ph->param_length = htons(SCTP_BUF_LEN(op_err)); 1087 ippp = (uint32_t *) (ph + 1); 1088 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6); 1089 } 1090 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6; 1091 sctp_abort_an_association(stcb->sctp_ep, stcb, 1092 op_err, SCTP_SO_LOCKED); 1093 goto skip_unlock; 1094 } else { 1095 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); 1096 } 1097 } 1098 SCTP_TCB_UNLOCK(stcb); 1099 } 1100 skip_unlock: 1101 SCTP_INP_RUNLOCK(inp); 1102 return (0); 1103 } 1104 1105 /* 1106 * copies a "user" presentable address and removes embedded scope, etc. 1107 * returns 0 on success, 1 on error 1108 */ 1109 static uint32_t 1110 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa) 1111 { 1112 #ifdef INET6 1113 struct sockaddr_in6 lsa6; 1114 1115 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa, 1116 &lsa6); 1117 #endif 1118 memcpy(ss, sa, sa->sa_len); 1119 return (0); 1120 } 1121 1122 1123 1124 /* 1125 * NOTE: assumes addr lock is held 1126 */ 1127 static size_t 1128 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp, 1129 struct sctp_tcb *stcb, 1130 size_t limit, 1131 struct sockaddr_storage *sas, 1132 uint32_t vrf_id) 1133 { 1134 struct sctp_ifn *sctp_ifn; 1135 struct sctp_ifa *sctp_ifa; 1136 int loopback_scope, ipv4_local_scope, local_scope, site_scope; 1137 size_t actual; 1138 int ipv4_addr_legal, ipv6_addr_legal; 1139 struct sctp_vrf *vrf; 1140 1141 actual = 0; 1142 if (limit <= 0) 1143 return (actual); 1144 1145 if (stcb) { 1146 /* Turn on all the appropriate scope */ 1147 loopback_scope = stcb->asoc.loopback_scope; 1148 ipv4_local_scope = stcb->asoc.ipv4_local_scope; 1149 local_scope = stcb->asoc.local_scope; 1150 site_scope = stcb->asoc.site_scope; 1151 } else { 1152 /* Turn on ALL scope, since we look at the EP */ 1153 loopback_scope = ipv4_local_scope = local_scope = 1154 site_scope = 1; 1155 } 1156 ipv4_addr_legal = ipv6_addr_legal = 0; 1157 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1158 ipv6_addr_legal = 1; 1159 if (SCTP_IPV6_V6ONLY(inp) == 0) { 1160 ipv4_addr_legal = 1; 1161 } 1162 } else { 1163 ipv4_addr_legal = 1; 1164 } 1165 vrf = sctp_find_vrf(vrf_id); 1166 if (vrf == NULL) { 1167 return (0); 1168 } 1169 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1170 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 1171 if ((loopback_scope == 0) && 1172 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 1173 /* Skip loopback if loopback_scope not set */ 1174 continue; 1175 } 1176 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 1177 if (stcb) { 1178 /* 1179 * For the BOUND-ALL case, the list 1180 * associated with a TCB is Always 1181 * considered a reverse list.. i.e. 1182 * it lists addresses that are NOT 1183 * part of the association. If this 1184 * is one of those we must skip it. 1185 */ 1186 if (sctp_is_addr_restricted(stcb, 1187 sctp_ifa)) { 1188 continue; 1189 } 1190 } 1191 switch (sctp_ifa->address.sa.sa_family) { 1192 #ifdef INET 1193 case AF_INET: 1194 if (ipv4_addr_legal) { 1195 struct sockaddr_in *sin; 1196 1197 sin = (struct sockaddr_in *)&sctp_ifa->address.sa; 1198 if (sin->sin_addr.s_addr == 0) { 1199 /* 1200 * we skip 1201 * unspecifed 1202 * addresses 1203 */ 1204 continue; 1205 } 1206 if ((ipv4_local_scope == 0) && 1207 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 1208 continue; 1209 } 1210 #ifdef INET6 1211 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) { 1212 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas); 1213 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1214 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6)); 1215 actual += sizeof(struct sockaddr_in6); 1216 } else { 1217 #endif 1218 memcpy(sas, sin, sizeof(*sin)); 1219 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport; 1220 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin)); 1221 actual += sizeof(*sin); 1222 #ifdef INET6 1223 } 1224 #endif 1225 if (actual >= limit) { 1226 return (actual); 1227 } 1228 } else { 1229 continue; 1230 } 1231 break; 1232 #endif 1233 #ifdef INET6 1234 case AF_INET6: 1235 if (ipv6_addr_legal) { 1236 struct sockaddr_in6 *sin6; 1237 1238 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa; 1239 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1240 /* 1241 * we skip 1242 * unspecifed 1243 * addresses 1244 */ 1245 continue; 1246 } 1247 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 1248 if (local_scope == 0) 1249 continue; 1250 if (sin6->sin6_scope_id == 0) { 1251 if (sa6_recoverscope(sin6) != 0) 1252 /* 1253 * 1254 * bad 1255 * 1256 * li 1257 * nk 1258 * 1259 * loc 1260 * al 1261 * 1262 * add 1263 * re 1264 * ss 1265 * */ 1266 continue; 1267 } 1268 } 1269 if ((site_scope == 0) && 1270 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { 1271 continue; 1272 } 1273 memcpy(sas, sin6, sizeof(*sin6)); 1274 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1275 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6)); 1276 actual += sizeof(*sin6); 1277 if (actual >= limit) { 1278 return (actual); 1279 } 1280 } else { 1281 continue; 1282 } 1283 break; 1284 #endif 1285 default: 1286 /* TSNH */ 1287 break; 1288 } 1289 } 1290 } 1291 } else { 1292 struct sctp_laddr *laddr; 1293 1294 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1295 if (stcb) { 1296 if (sctp_is_addr_restricted(stcb, laddr->ifa)) { 1297 continue; 1298 } 1299 } 1300 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa)) 1301 continue; 1302 1303 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1304 sas = (struct sockaddr_storage *)((caddr_t)sas + 1305 laddr->ifa->address.sa.sa_len); 1306 actual += laddr->ifa->address.sa.sa_len; 1307 if (actual >= limit) { 1308 return (actual); 1309 } 1310 } 1311 } 1312 return (actual); 1313 } 1314 1315 static size_t 1316 sctp_fill_up_addresses(struct sctp_inpcb *inp, 1317 struct sctp_tcb *stcb, 1318 size_t limit, 1319 struct sockaddr_storage *sas) 1320 { 1321 size_t size = 0; 1322 1323 SCTP_IPI_ADDR_RLOCK(); 1324 /* fill up addresses for the endpoint's default vrf */ 1325 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas, 1326 inp->def_vrf_id); 1327 SCTP_IPI_ADDR_RUNLOCK(); 1328 return (size); 1329 } 1330 1331 /* 1332 * NOTE: assumes addr lock is held 1333 */ 1334 static int 1335 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id) 1336 { 1337 int cnt = 0; 1338 struct sctp_vrf *vrf = NULL; 1339 1340 /* 1341 * In both sub-set bound an bound_all cases we return the MAXIMUM 1342 * number of addresses that you COULD get. In reality the sub-set 1343 * bound may have an exclusion list for a given TCB OR in the 1344 * bound-all case a TCB may NOT include the loopback or other 1345 * addresses as well. 1346 */ 1347 vrf = sctp_find_vrf(vrf_id); 1348 if (vrf == NULL) { 1349 return (0); 1350 } 1351 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1352 struct sctp_ifn *sctp_ifn; 1353 struct sctp_ifa *sctp_ifa; 1354 1355 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 1356 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 1357 /* Count them if they are the right type */ 1358 switch (sctp_ifa->address.sa.sa_family) { 1359 #ifdef INET 1360 case AF_INET: 1361 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) 1362 cnt += sizeof(struct sockaddr_in6); 1363 else 1364 cnt += sizeof(struct sockaddr_in); 1365 break; 1366 #endif 1367 #ifdef INET6 1368 case AF_INET6: 1369 cnt += sizeof(struct sockaddr_in6); 1370 break; 1371 #endif 1372 default: 1373 break; 1374 } 1375 } 1376 } 1377 } else { 1378 struct sctp_laddr *laddr; 1379 1380 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1381 switch (laddr->ifa->address.sa.sa_family) { 1382 #ifdef INET 1383 case AF_INET: 1384 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) 1385 cnt += sizeof(struct sockaddr_in6); 1386 else 1387 cnt += sizeof(struct sockaddr_in); 1388 break; 1389 #endif 1390 #ifdef INET6 1391 case AF_INET6: 1392 cnt += sizeof(struct sockaddr_in6); 1393 break; 1394 #endif 1395 default: 1396 break; 1397 } 1398 } 1399 } 1400 return (cnt); 1401 } 1402 1403 static int 1404 sctp_count_max_addresses(struct sctp_inpcb *inp) 1405 { 1406 int cnt = 0; 1407 1408 SCTP_IPI_ADDR_RLOCK(); 1409 /* count addresses for the endpoint's default VRF */ 1410 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id); 1411 SCTP_IPI_ADDR_RUNLOCK(); 1412 return (cnt); 1413 } 1414 1415 static int 1416 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval, 1417 size_t optsize, void *p, int delay) 1418 { 1419 int error = 0; 1420 int creat_lock_on = 0; 1421 struct sctp_tcb *stcb = NULL; 1422 struct sockaddr *sa; 1423 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr; 1424 uint32_t vrf_id; 1425 int bad_addresses = 0; 1426 sctp_assoc_t *a_id; 1427 1428 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n"); 1429 1430 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1431 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 1432 /* We are already connected AND the TCP model */ 1433 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE); 1434 return (EADDRINUSE); 1435 } 1436 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && 1437 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) { 1438 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1439 return (EINVAL); 1440 } 1441 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1442 SCTP_INP_RLOCK(inp); 1443 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1444 SCTP_INP_RUNLOCK(inp); 1445 } 1446 if (stcb) { 1447 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 1448 return (EALREADY); 1449 } 1450 SCTP_INP_INCR_REF(inp); 1451 SCTP_ASOC_CREATE_LOCK(inp); 1452 creat_lock_on = 1; 1453 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1454 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 1455 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT); 1456 error = EFAULT; 1457 goto out_now; 1458 } 1459 totaddrp = (int *)optval; 1460 totaddr = *totaddrp; 1461 sa = (struct sockaddr *)(totaddrp + 1); 1462 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses); 1463 if ((stcb != NULL) || bad_addresses) { 1464 /* Already have or am bring up an association */ 1465 SCTP_ASOC_CREATE_UNLOCK(inp); 1466 creat_lock_on = 0; 1467 if (stcb) 1468 SCTP_TCB_UNLOCK(stcb); 1469 if (bad_addresses == 0) { 1470 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 1471 error = EALREADY; 1472 } 1473 goto out_now; 1474 } 1475 #ifdef INET6 1476 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 1477 (num_v6 > 0)) { 1478 error = EINVAL; 1479 goto out_now; 1480 } 1481 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 1482 (num_v4 > 0)) { 1483 struct in6pcb *inp6; 1484 1485 inp6 = (struct in6pcb *)inp; 1486 if (SCTP_IPV6_V6ONLY(inp6)) { 1487 /* 1488 * if IPV6_V6ONLY flag, ignore connections destined 1489 * to a v4 addr or v4-mapped addr 1490 */ 1491 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1492 error = EINVAL; 1493 goto out_now; 1494 } 1495 } 1496 #endif /* INET6 */ 1497 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 1498 SCTP_PCB_FLAGS_UNBOUND) { 1499 /* Bind a ephemeral port */ 1500 error = sctp_inpcb_bind(so, NULL, NULL, p); 1501 if (error) { 1502 goto out_now; 1503 } 1504 } 1505 /* FIX ME: do we want to pass in a vrf on the connect call? */ 1506 vrf_id = inp->def_vrf_id; 1507 1508 1509 /* We are GOOD to go */ 1510 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id, 1511 (struct thread *)p 1512 ); 1513 if (stcb == NULL) { 1514 /* Gak! no memory */ 1515 goto out_now; 1516 } 1517 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1518 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1519 /* Set the connected flag so we can queue data */ 1520 soisconnecting(so); 1521 } 1522 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT); 1523 /* move to second address */ 1524 switch (sa->sa_family) { 1525 #ifdef INET 1526 case AF_INET: 1527 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); 1528 break; 1529 #endif 1530 #ifdef INET6 1531 case AF_INET6: 1532 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); 1533 break; 1534 #endif 1535 default: 1536 break; 1537 } 1538 1539 error = 0; 1540 sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error); 1541 /* Fill in the return id */ 1542 if (error) { 1543 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6); 1544 goto out_now; 1545 } 1546 a_id = (sctp_assoc_t *) optval; 1547 *a_id = sctp_get_associd(stcb); 1548 1549 /* initialize authentication parameters for the assoc */ 1550 sctp_initialize_auth_params(inp, stcb); 1551 1552 if (delay) { 1553 /* doing delayed connection */ 1554 stcb->asoc.delayed_connection = 1; 1555 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination); 1556 } else { 1557 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1558 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 1559 } 1560 SCTP_TCB_UNLOCK(stcb); 1561 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1562 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1563 /* Set the connected flag so we can queue data */ 1564 soisconnecting(so); 1565 } 1566 out_now: 1567 if (creat_lock_on) { 1568 SCTP_ASOC_CREATE_UNLOCK(inp); 1569 } 1570 SCTP_INP_DECR_REF(inp); 1571 return (error); 1572 } 1573 1574 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \ 1575 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\ 1576 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \ 1577 SCTP_INP_RLOCK(inp); \ 1578 stcb = LIST_FIRST(&inp->sctp_asoc_list); \ 1579 if (stcb) { \ 1580 SCTP_TCB_LOCK(stcb); \ 1581 } \ 1582 SCTP_INP_RUNLOCK(inp); \ 1583 } else if (assoc_id > SCTP_ALL_ASSOC) { \ 1584 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \ 1585 if (stcb == NULL) { \ 1586 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \ 1587 error = ENOENT; \ 1588 break; \ 1589 } \ 1590 } else { \ 1591 stcb = NULL; \ 1592 } \ 1593 } 1594 1595 1596 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\ 1597 if (size < sizeof(type)) { \ 1598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \ 1599 error = EINVAL; \ 1600 break; \ 1601 } else { \ 1602 destp = (type *)srcp; \ 1603 } \ 1604 } 1605 1606 static int 1607 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize, 1608 void *p) 1609 { 1610 struct sctp_inpcb *inp = NULL; 1611 int error, val = 0; 1612 struct sctp_tcb *stcb = NULL; 1613 1614 if (optval == NULL) { 1615 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1616 return (EINVAL); 1617 } 1618 inp = (struct sctp_inpcb *)so->so_pcb; 1619 if (inp == NULL) { 1620 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1621 return EINVAL; 1622 } 1623 error = 0; 1624 1625 switch (optname) { 1626 case SCTP_NODELAY: 1627 case SCTP_AUTOCLOSE: 1628 case SCTP_EXPLICIT_EOR: 1629 case SCTP_AUTO_ASCONF: 1630 case SCTP_DISABLE_FRAGMENTS: 1631 case SCTP_I_WANT_MAPPED_V4_ADDR: 1632 case SCTP_USE_EXT_RCVINFO: 1633 SCTP_INP_RLOCK(inp); 1634 switch (optname) { 1635 case SCTP_DISABLE_FRAGMENTS: 1636 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT); 1637 break; 1638 case SCTP_I_WANT_MAPPED_V4_ADDR: 1639 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4); 1640 break; 1641 case SCTP_AUTO_ASCONF: 1642 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1643 /* only valid for bound all sockets */ 1644 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 1645 } else { 1646 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1647 error = EINVAL; 1648 goto flags_out; 1649 } 1650 break; 1651 case SCTP_EXPLICIT_EOR: 1652 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); 1653 break; 1654 case SCTP_NODELAY: 1655 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY); 1656 break; 1657 case SCTP_USE_EXT_RCVINFO: 1658 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO); 1659 break; 1660 case SCTP_AUTOCLOSE: 1661 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) 1662 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time); 1663 else 1664 val = 0; 1665 break; 1666 1667 default: 1668 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT); 1669 error = ENOPROTOOPT; 1670 } /* end switch (sopt->sopt_name) */ 1671 if (*optsize < sizeof(val)) { 1672 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1673 error = EINVAL; 1674 } 1675 flags_out: 1676 SCTP_INP_RUNLOCK(inp); 1677 if (error == 0) { 1678 /* return the option value */ 1679 *(int *)optval = val; 1680 *optsize = sizeof(val); 1681 } 1682 break; 1683 case SCTP_GET_PACKET_LOG: 1684 { 1685 #ifdef SCTP_PACKET_LOGGING 1686 uint8_t *target; 1687 int ret; 1688 1689 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize); 1690 ret = sctp_copy_out_packet_log(target, (int)*optsize); 1691 *optsize = ret; 1692 #else 1693 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 1694 error = EOPNOTSUPP; 1695 #endif 1696 break; 1697 } 1698 case SCTP_REUSE_PORT: 1699 { 1700 uint32_t *value; 1701 1702 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 1703 /* Can't do this for a 1-m socket */ 1704 error = EINVAL; 1705 break; 1706 } 1707 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1708 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE); 1709 *optsize = sizeof(uint32_t); 1710 break; 1711 } 1712 case SCTP_PARTIAL_DELIVERY_POINT: 1713 { 1714 uint32_t *value; 1715 1716 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1717 *value = inp->partial_delivery_point; 1718 *optsize = sizeof(uint32_t); 1719 break; 1720 } 1721 case SCTP_FRAGMENT_INTERLEAVE: 1722 { 1723 uint32_t *value; 1724 1725 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1726 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) { 1727 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) { 1728 *value = SCTP_FRAG_LEVEL_2; 1729 } else { 1730 *value = SCTP_FRAG_LEVEL_1; 1731 } 1732 } else { 1733 *value = SCTP_FRAG_LEVEL_0; 1734 } 1735 *optsize = sizeof(uint32_t); 1736 break; 1737 } 1738 case SCTP_CMT_ON_OFF: 1739 { 1740 struct sctp_assoc_value *av; 1741 1742 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1743 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1744 if (stcb) { 1745 av->assoc_value = stcb->asoc.sctp_cmt_on_off; 1746 SCTP_TCB_UNLOCK(stcb); 1747 } else { 1748 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1749 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 1750 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 1751 SCTP_INP_RLOCK(inp); 1752 av->assoc_value = inp->sctp_cmt_on_off; 1753 SCTP_INP_RUNLOCK(inp); 1754 } else { 1755 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1756 error = EINVAL; 1757 } 1758 } 1759 if (error == 0) { 1760 *optsize = sizeof(struct sctp_assoc_value); 1761 } 1762 break; 1763 } 1764 case SCTP_PLUGGABLE_CC: 1765 { 1766 struct sctp_assoc_value *av; 1767 1768 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1769 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1770 if (stcb) { 1771 av->assoc_value = stcb->asoc.congestion_control_module; 1772 SCTP_TCB_UNLOCK(stcb); 1773 } else { 1774 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1775 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 1776 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 1777 SCTP_INP_RLOCK(inp); 1778 av->assoc_value = inp->sctp_ep.sctp_default_cc_module; 1779 SCTP_INP_RUNLOCK(inp); 1780 } else { 1781 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1782 error = EINVAL; 1783 } 1784 } 1785 if (error == 0) { 1786 *optsize = sizeof(struct sctp_assoc_value); 1787 } 1788 break; 1789 } 1790 case SCTP_CC_OPTION: 1791 { 1792 struct sctp_cc_option *cc_opt; 1793 1794 SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize); 1795 SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id); 1796 if (stcb == NULL) { 1797 error = EINVAL; 1798 } else { 1799 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) { 1800 error = ENOTSUP; 1801 } else { 1802 error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt); 1803 *optsize = sizeof(struct sctp_cc_option); 1804 } 1805 SCTP_TCB_UNLOCK(stcb); 1806 } 1807 break; 1808 } 1809 case SCTP_PLUGGABLE_SS: 1810 { 1811 struct sctp_assoc_value *av; 1812 1813 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1814 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1815 if (stcb) { 1816 av->assoc_value = stcb->asoc.stream_scheduling_module; 1817 SCTP_TCB_UNLOCK(stcb); 1818 } else { 1819 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1820 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 1821 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 1822 SCTP_INP_RLOCK(inp); 1823 av->assoc_value = inp->sctp_ep.sctp_default_ss_module; 1824 SCTP_INP_RUNLOCK(inp); 1825 } else { 1826 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1827 error = EINVAL; 1828 } 1829 } 1830 if (error == 0) { 1831 *optsize = sizeof(struct sctp_assoc_value); 1832 } 1833 break; 1834 } 1835 case SCTP_SS_VALUE: 1836 { 1837 struct sctp_stream_value *av; 1838 1839 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize); 1840 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1841 if (stcb) { 1842 if (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id], 1843 &av->stream_value) < 0) { 1844 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1845 error = EINVAL; 1846 } else { 1847 *optsize = sizeof(struct sctp_stream_value); 1848 } 1849 SCTP_TCB_UNLOCK(stcb); 1850 } else { 1851 /* 1852 * Can't get stream value without 1853 * association 1854 */ 1855 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1856 error = EINVAL; 1857 } 1858 break; 1859 } 1860 case SCTP_GET_ADDR_LEN: 1861 { 1862 struct sctp_assoc_value *av; 1863 1864 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1865 error = EINVAL; 1866 #ifdef INET 1867 if (av->assoc_value == AF_INET) { 1868 av->assoc_value = sizeof(struct sockaddr_in); 1869 error = 0; 1870 } 1871 #endif 1872 #ifdef INET6 1873 if (av->assoc_value == AF_INET6) { 1874 av->assoc_value = sizeof(struct sockaddr_in6); 1875 error = 0; 1876 } 1877 #endif 1878 if (error) { 1879 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 1880 } else { 1881 *optsize = sizeof(struct sctp_assoc_value); 1882 } 1883 break; 1884 } 1885 case SCTP_GET_ASSOC_NUMBER: 1886 { 1887 uint32_t *value, cnt; 1888 1889 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1890 cnt = 0; 1891 SCTP_INP_RLOCK(inp); 1892 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1893 cnt++; 1894 } 1895 SCTP_INP_RUNLOCK(inp); 1896 *value = cnt; 1897 *optsize = sizeof(uint32_t); 1898 break; 1899 } 1900 case SCTP_GET_ASSOC_ID_LIST: 1901 { 1902 struct sctp_assoc_ids *ids; 1903 unsigned int at, limit; 1904 1905 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize); 1906 at = 0; 1907 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t); 1908 SCTP_INP_RLOCK(inp); 1909 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1910 if (at < limit) { 1911 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb); 1912 } else { 1913 error = EINVAL; 1914 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 1915 break; 1916 } 1917 } 1918 SCTP_INP_RUNLOCK(inp); 1919 if (error == 0) { 1920 ids->gaids_number_of_ids = at; 1921 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t)); 1922 } 1923 break; 1924 } 1925 case SCTP_CONTEXT: 1926 { 1927 struct sctp_assoc_value *av; 1928 1929 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1930 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1931 1932 if (stcb) { 1933 av->assoc_value = stcb->asoc.context; 1934 SCTP_TCB_UNLOCK(stcb); 1935 } else { 1936 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 1937 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 1938 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 1939 SCTP_INP_RLOCK(inp); 1940 av->assoc_value = inp->sctp_context; 1941 SCTP_INP_RUNLOCK(inp); 1942 } else { 1943 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 1944 error = EINVAL; 1945 } 1946 } 1947 if (error == 0) { 1948 *optsize = sizeof(struct sctp_assoc_value); 1949 } 1950 break; 1951 } 1952 case SCTP_VRF_ID: 1953 { 1954 uint32_t *default_vrfid; 1955 1956 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize); 1957 *default_vrfid = inp->def_vrf_id; 1958 *optsize = sizeof(uint32_t); 1959 break; 1960 } 1961 case SCTP_GET_ASOC_VRF: 1962 { 1963 struct sctp_assoc_value *id; 1964 1965 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize); 1966 SCTP_FIND_STCB(inp, stcb, id->assoc_id); 1967 if (stcb == NULL) { 1968 error = EINVAL; 1969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 1970 } else { 1971 id->assoc_value = stcb->asoc.vrf_id; 1972 *optsize = sizeof(struct sctp_assoc_value); 1973 } 1974 break; 1975 } 1976 case SCTP_GET_VRF_IDS: 1977 { 1978 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 1979 error = EOPNOTSUPP; 1980 break; 1981 } 1982 case SCTP_GET_NONCE_VALUES: 1983 { 1984 struct sctp_get_nonce_values *gnv; 1985 1986 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize); 1987 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id); 1988 1989 if (stcb) { 1990 gnv->gn_peers_tag = stcb->asoc.peer_vtag; 1991 gnv->gn_local_tag = stcb->asoc.my_vtag; 1992 SCTP_TCB_UNLOCK(stcb); 1993 *optsize = sizeof(struct sctp_get_nonce_values); 1994 } else { 1995 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN); 1996 error = ENOTCONN; 1997 } 1998 break; 1999 } 2000 case SCTP_DELAYED_SACK: 2001 { 2002 struct sctp_sack_info *sack; 2003 2004 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize); 2005 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id); 2006 if (stcb) { 2007 sack->sack_delay = stcb->asoc.delayed_ack; 2008 sack->sack_freq = stcb->asoc.sack_freq; 2009 SCTP_TCB_UNLOCK(stcb); 2010 } else { 2011 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2012 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2013 (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) { 2014 SCTP_INP_RLOCK(inp); 2015 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]); 2016 sack->sack_freq = inp->sctp_ep.sctp_sack_freq; 2017 SCTP_INP_RUNLOCK(inp); 2018 } else { 2019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2020 error = EINVAL; 2021 } 2022 } 2023 if (error == 0) { 2024 *optsize = sizeof(struct sctp_sack_info); 2025 } 2026 break; 2027 } 2028 case SCTP_GET_SNDBUF_USE: 2029 { 2030 struct sctp_sockstat *ss; 2031 2032 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize); 2033 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id); 2034 2035 if (stcb) { 2036 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size; 2037 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue + 2038 stcb->asoc.size_on_all_streams); 2039 SCTP_TCB_UNLOCK(stcb); 2040 *optsize = sizeof(struct sctp_sockstat); 2041 } else { 2042 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN); 2043 error = ENOTCONN; 2044 } 2045 break; 2046 } 2047 case SCTP_MAX_BURST: 2048 { 2049 struct sctp_assoc_value *av; 2050 2051 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 2052 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 2053 2054 if (stcb) { 2055 av->assoc_value = stcb->asoc.max_burst; 2056 SCTP_TCB_UNLOCK(stcb); 2057 } else { 2058 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2059 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2060 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 2061 SCTP_INP_RLOCK(inp); 2062 av->assoc_value = inp->sctp_ep.max_burst; 2063 SCTP_INP_RUNLOCK(inp); 2064 } else { 2065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2066 error = EINVAL; 2067 } 2068 } 2069 if (error == 0) { 2070 *optsize = sizeof(struct sctp_assoc_value); 2071 } 2072 break; 2073 } 2074 case SCTP_MAXSEG: 2075 { 2076 struct sctp_assoc_value *av; 2077 int ovh; 2078 2079 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 2080 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 2081 2082 if (stcb) { 2083 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc); 2084 SCTP_TCB_UNLOCK(stcb); 2085 } else { 2086 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2087 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2088 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 2089 SCTP_INP_RLOCK(inp); 2090 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2091 ovh = SCTP_MED_OVERHEAD; 2092 } else { 2093 ovh = SCTP_MED_V4_OVERHEAD; 2094 } 2095 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT) 2096 av->assoc_value = 0; 2097 else 2098 av->assoc_value = inp->sctp_frag_point - ovh; 2099 SCTP_INP_RUNLOCK(inp); 2100 } else { 2101 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2102 error = EINVAL; 2103 } 2104 } 2105 if (error == 0) { 2106 *optsize = sizeof(struct sctp_assoc_value); 2107 } 2108 break; 2109 } 2110 case SCTP_GET_STAT_LOG: 2111 error = sctp_fill_stat_log(optval, optsize); 2112 break; 2113 case SCTP_EVENTS: 2114 { 2115 struct sctp_event_subscribe *events; 2116 2117 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize); 2118 memset(events, 0, sizeof(struct sctp_event_subscribe)); 2119 SCTP_INP_RLOCK(inp); 2120 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) 2121 events->sctp_data_io_event = 1; 2122 2123 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT)) 2124 events->sctp_association_event = 1; 2125 2126 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT)) 2127 events->sctp_address_event = 1; 2128 2129 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) 2130 events->sctp_send_failure_event = 1; 2131 2132 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR)) 2133 events->sctp_peer_error_event = 1; 2134 2135 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) 2136 events->sctp_shutdown_event = 1; 2137 2138 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) 2139 events->sctp_partial_delivery_event = 1; 2140 2141 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) 2142 events->sctp_adaptation_layer_event = 1; 2143 2144 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT)) 2145 events->sctp_authentication_event = 1; 2146 2147 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT)) 2148 events->sctp_sender_dry_event = 1; 2149 2150 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) 2151 events->sctp_stream_reset_event = 1; 2152 SCTP_INP_RUNLOCK(inp); 2153 *optsize = sizeof(struct sctp_event_subscribe); 2154 break; 2155 } 2156 case SCTP_ADAPTATION_LAYER: 2157 { 2158 uint32_t *value; 2159 2160 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 2161 2162 SCTP_INP_RLOCK(inp); 2163 *value = inp->sctp_ep.adaptation_layer_indicator; 2164 SCTP_INP_RUNLOCK(inp); 2165 *optsize = sizeof(uint32_t); 2166 break; 2167 } 2168 case SCTP_SET_INITIAL_DBG_SEQ: 2169 { 2170 uint32_t *value; 2171 2172 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 2173 SCTP_INP_RLOCK(inp); 2174 *value = inp->sctp_ep.initial_sequence_debug; 2175 SCTP_INP_RUNLOCK(inp); 2176 *optsize = sizeof(uint32_t); 2177 break; 2178 } 2179 case SCTP_GET_LOCAL_ADDR_SIZE: 2180 { 2181 uint32_t *value; 2182 2183 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 2184 SCTP_INP_RLOCK(inp); 2185 *value = sctp_count_max_addresses(inp); 2186 SCTP_INP_RUNLOCK(inp); 2187 *optsize = sizeof(uint32_t); 2188 break; 2189 } 2190 case SCTP_GET_REMOTE_ADDR_SIZE: 2191 { 2192 uint32_t *value; 2193 size_t size; 2194 struct sctp_nets *net; 2195 2196 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 2197 /* FIXME MT: change to sctp_assoc_value? */ 2198 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value); 2199 2200 if (stcb) { 2201 size = 0; 2202 /* Count the sizes */ 2203 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2204 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) { 2205 size += sizeof(struct sockaddr_in6); 2206 } else { 2207 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { 2208 #ifdef INET 2209 case AF_INET: 2210 size += sizeof(struct sockaddr_in); 2211 break; 2212 #endif 2213 #ifdef INET6 2214 case AF_INET6: 2215 size += sizeof(struct sockaddr_in6); 2216 break; 2217 #endif 2218 default: 2219 break; 2220 } 2221 } 2222 } 2223 SCTP_TCB_UNLOCK(stcb); 2224 *value = (uint32_t) size; 2225 *optsize = sizeof(uint32_t); 2226 } else { 2227 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN); 2228 error = ENOTCONN; 2229 } 2230 break; 2231 } 2232 case SCTP_GET_PEER_ADDRESSES: 2233 /* 2234 * Get the address information, an array is passed in to 2235 * fill up we pack it. 2236 */ 2237 { 2238 size_t cpsz, left; 2239 struct sockaddr_storage *sas; 2240 struct sctp_nets *net; 2241 struct sctp_getaddresses *saddr; 2242 2243 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize); 2244 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id); 2245 2246 if (stcb) { 2247 left = (*optsize) - sizeof(struct sctp_getaddresses); 2248 *optsize = sizeof(struct sctp_getaddresses); 2249 sas = (struct sockaddr_storage *)&saddr->addr[0]; 2250 2251 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2252 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) { 2253 cpsz = sizeof(struct sockaddr_in6); 2254 } else { 2255 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) { 2256 #ifdef INET 2257 case AF_INET: 2258 cpsz = sizeof(struct sockaddr_in); 2259 break; 2260 #endif 2261 #ifdef INET6 2262 case AF_INET6: 2263 cpsz = sizeof(struct sockaddr_in6); 2264 break; 2265 #endif 2266 default: 2267 cpsz = 0; 2268 break; 2269 } 2270 } 2271 if (cpsz == 0) { 2272 break; 2273 } 2274 if (left < cpsz) { 2275 /* not enough room. */ 2276 break; 2277 } 2278 #if defined(INET) && defined(INET6) 2279 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) && 2280 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) { 2281 /* Must map the address */ 2282 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr, 2283 (struct sockaddr_in6 *)sas); 2284 } else { 2285 #endif 2286 memcpy(sas, &net->ro._l_addr, cpsz); 2287 #if defined(INET) && defined(INET6) 2288 } 2289 #endif 2290 ((struct sockaddr_in *)sas)->sin_port = stcb->rport; 2291 2292 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz); 2293 left -= cpsz; 2294 *optsize += cpsz; 2295 } 2296 SCTP_TCB_UNLOCK(stcb); 2297 } else { 2298 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 2299 error = ENOENT; 2300 } 2301 break; 2302 } 2303 case SCTP_GET_LOCAL_ADDRESSES: 2304 { 2305 size_t limit, actual; 2306 struct sockaddr_storage *sas; 2307 struct sctp_getaddresses *saddr; 2308 2309 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize); 2310 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id); 2311 2312 sas = (struct sockaddr_storage *)&saddr->addr[0]; 2313 limit = *optsize - sizeof(sctp_assoc_t); 2314 actual = sctp_fill_up_addresses(inp, stcb, limit, sas); 2315 if (stcb) { 2316 SCTP_TCB_UNLOCK(stcb); 2317 } 2318 *optsize = sizeof(struct sockaddr_storage) + actual; 2319 break; 2320 } 2321 case SCTP_PEER_ADDR_PARAMS: 2322 { 2323 struct sctp_paddrparams *paddrp; 2324 struct sctp_nets *net; 2325 2326 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize); 2327 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id); 2328 2329 net = NULL; 2330 if (stcb) { 2331 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 2332 } else { 2333 /* 2334 * We increment here since 2335 * sctp_findassociation_ep_addr() wil do a 2336 * decrement if it finds the stcb as long as 2337 * the locked tcb (last argument) is NOT a 2338 * TCB.. aka NULL. 2339 */ 2340 SCTP_INP_INCR_REF(inp); 2341 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL); 2342 if (stcb == NULL) { 2343 SCTP_INP_DECR_REF(inp); 2344 } 2345 } 2346 if (stcb && (net == NULL)) { 2347 struct sockaddr *sa; 2348 2349 sa = (struct sockaddr *)&paddrp->spp_address; 2350 #ifdef INET 2351 if (sa->sa_family == AF_INET) { 2352 struct sockaddr_in *sin; 2353 2354 sin = (struct sockaddr_in *)sa; 2355 if (sin->sin_addr.s_addr) { 2356 error = EINVAL; 2357 SCTP_TCB_UNLOCK(stcb); 2358 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 2359 break; 2360 } 2361 } else 2362 #endif 2363 #ifdef INET6 2364 if (sa->sa_family == AF_INET6) { 2365 struct sockaddr_in6 *sin6; 2366 2367 sin6 = (struct sockaddr_in6 *)sa; 2368 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 2369 error = EINVAL; 2370 SCTP_TCB_UNLOCK(stcb); 2371 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 2372 break; 2373 } 2374 } else 2375 #endif 2376 { 2377 error = EAFNOSUPPORT; 2378 SCTP_TCB_UNLOCK(stcb); 2379 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 2380 break; 2381 } 2382 } 2383 if (stcb) { 2384 /* Applies to the specific association */ 2385 paddrp->spp_flags = 0; 2386 if (net) { 2387 int ovh; 2388 2389 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2390 ovh = SCTP_MED_OVERHEAD; 2391 } else { 2392 ovh = SCTP_MED_V4_OVERHEAD; 2393 } 2394 2395 paddrp->spp_hbinterval = net->heart_beat_delay; 2396 paddrp->spp_pathmaxrxt = net->failure_threshold; 2397 paddrp->spp_pathmtu = net->mtu - ovh; 2398 /* get flags for HB */ 2399 if (net->dest_state & SCTP_ADDR_NOHB) { 2400 paddrp->spp_flags |= SPP_HB_DISABLE; 2401 } else { 2402 paddrp->spp_flags |= SPP_HB_ENABLE; 2403 } 2404 /* get flags for PMTU */ 2405 if (net->dest_state & SCTP_ADDR_NO_PMTUD) { 2406 paddrp->spp_flags |= SPP_PMTUD_ENABLE; 2407 } else { 2408 paddrp->spp_flags |= SPP_PMTUD_DISABLE; 2409 } 2410 if (net->dscp & 0x01) { 2411 paddrp->spp_dscp = net->dscp & 0xfc; 2412 paddrp->spp_flags |= SPP_DSCP; 2413 } 2414 #ifdef INET6 2415 if ((net->ro._l_addr.sa.sa_family == AF_INET6) && 2416 (net->flowlabel & 0x80000000)) { 2417 paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff; 2418 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL; 2419 } 2420 #endif 2421 } else { 2422 /* 2423 * No destination so return default 2424 * value 2425 */ 2426 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure; 2427 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc); 2428 if (stcb->asoc.default_dscp & 0x01) { 2429 paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc; 2430 paddrp->spp_flags |= SPP_DSCP; 2431 } 2432 #ifdef INET6 2433 if (stcb->asoc.default_flowlabel & 0x80000000) { 2434 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff; 2435 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL; 2436 } 2437 #endif 2438 /* default settings should be these */ 2439 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { 2440 paddrp->spp_flags |= SPP_HB_DISABLE; 2441 } else { 2442 paddrp->spp_flags |= SPP_HB_ENABLE; 2443 } 2444 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { 2445 paddrp->spp_flags |= SPP_PMTUD_DISABLE; 2446 } else { 2447 paddrp->spp_flags |= SPP_PMTUD_ENABLE; 2448 } 2449 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay; 2450 } 2451 paddrp->spp_assoc_id = sctp_get_associd(stcb); 2452 SCTP_TCB_UNLOCK(stcb); 2453 } else { 2454 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2455 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2456 (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) { 2457 /* Use endpoint defaults */ 2458 SCTP_INP_RLOCK(inp); 2459 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure; 2460 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]); 2461 paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC; 2462 /* get inp's default */ 2463 if (inp->sctp_ep.default_dscp & 0x01) { 2464 paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc; 2465 paddrp->spp_flags |= SPP_DSCP; 2466 } 2467 #ifdef INET6 2468 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 2469 (inp->sctp_ep.default_flowlabel & 0x80000000)) { 2470 paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff; 2471 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL; 2472 } 2473 #endif 2474 /* can't return this */ 2475 paddrp->spp_pathmtu = 0; 2476 2477 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { 2478 paddrp->spp_flags |= SPP_HB_ENABLE; 2479 } else { 2480 paddrp->spp_flags |= SPP_HB_DISABLE; 2481 } 2482 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { 2483 paddrp->spp_flags |= SPP_PMTUD_ENABLE; 2484 } else { 2485 paddrp->spp_flags |= SPP_PMTUD_DISABLE; 2486 } 2487 SCTP_INP_RUNLOCK(inp); 2488 } else { 2489 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2490 error = EINVAL; 2491 } 2492 } 2493 if (error == 0) { 2494 *optsize = sizeof(struct sctp_paddrparams); 2495 } 2496 break; 2497 } 2498 case SCTP_GET_PEER_ADDR_INFO: 2499 { 2500 struct sctp_paddrinfo *paddri; 2501 struct sctp_nets *net; 2502 2503 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize); 2504 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id); 2505 2506 net = NULL; 2507 if (stcb) { 2508 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address); 2509 } else { 2510 /* 2511 * We increment here since 2512 * sctp_findassociation_ep_addr() wil do a 2513 * decrement if it finds the stcb as long as 2514 * the locked tcb (last argument) is NOT a 2515 * TCB.. aka NULL. 2516 */ 2517 SCTP_INP_INCR_REF(inp); 2518 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL); 2519 if (stcb == NULL) { 2520 SCTP_INP_DECR_REF(inp); 2521 } 2522 } 2523 2524 if ((stcb) && (net)) { 2525 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 2526 /* It's unconfirmed */ 2527 paddri->spinfo_state = SCTP_UNCONFIRMED; 2528 } else if (net->dest_state & SCTP_ADDR_REACHABLE) { 2529 /* It's active */ 2530 paddri->spinfo_state = SCTP_ACTIVE; 2531 } else { 2532 /* It's inactive */ 2533 paddri->spinfo_state = SCTP_INACTIVE; 2534 } 2535 paddri->spinfo_cwnd = net->cwnd; 2536 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT; 2537 paddri->spinfo_rto = net->RTO; 2538 paddri->spinfo_assoc_id = sctp_get_associd(stcb); 2539 paddri->spinfo_mtu = net->mtu; 2540 SCTP_TCB_UNLOCK(stcb); 2541 *optsize = sizeof(struct sctp_paddrinfo); 2542 } else { 2543 if (stcb) { 2544 SCTP_TCB_UNLOCK(stcb); 2545 } 2546 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 2547 error = ENOENT; 2548 } 2549 break; 2550 } 2551 case SCTP_PCB_STATUS: 2552 { 2553 struct sctp_pcbinfo *spcb; 2554 2555 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize); 2556 sctp_fill_pcbinfo(spcb); 2557 *optsize = sizeof(struct sctp_pcbinfo); 2558 break; 2559 } 2560 case SCTP_STATUS: 2561 { 2562 struct sctp_nets *net; 2563 struct sctp_status *sstat; 2564 2565 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize); 2566 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id); 2567 2568 if (stcb == NULL) { 2569 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2570 error = EINVAL; 2571 break; 2572 } 2573 /* 2574 * I think passing the state is fine since 2575 * sctp_constants.h will be available to the user 2576 * land. 2577 */ 2578 sstat->sstat_state = stcb->asoc.state; 2579 sstat->sstat_assoc_id = sctp_get_associd(stcb); 2580 sstat->sstat_rwnd = stcb->asoc.peers_rwnd; 2581 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt; 2582 /* 2583 * We can't include chunks that have been passed to 2584 * the socket layer. Only things in queue. 2585 */ 2586 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue + 2587 stcb->asoc.cnt_on_all_streams); 2588 2589 2590 sstat->sstat_instrms = stcb->asoc.streamincnt; 2591 sstat->sstat_outstrms = stcb->asoc.streamoutcnt; 2592 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc); 2593 memcpy(&sstat->sstat_primary.spinfo_address, 2594 &stcb->asoc.primary_destination->ro._l_addr, 2595 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len); 2596 net = stcb->asoc.primary_destination; 2597 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport; 2598 /* 2599 * Again the user can get info from sctp_constants.h 2600 * for what the state of the network is. 2601 */ 2602 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { 2603 /* It's unconfirmed */ 2604 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED; 2605 } else if (net->dest_state & SCTP_ADDR_REACHABLE) { 2606 /* It's active */ 2607 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE; 2608 } else { 2609 /* It's inactive */ 2610 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE; 2611 } 2612 sstat->sstat_primary.spinfo_cwnd = net->cwnd; 2613 sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT; 2614 sstat->sstat_primary.spinfo_rto = net->RTO; 2615 sstat->sstat_primary.spinfo_mtu = net->mtu; 2616 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb); 2617 SCTP_TCB_UNLOCK(stcb); 2618 *optsize = sizeof(struct sctp_status); 2619 break; 2620 } 2621 case SCTP_RTOINFO: 2622 { 2623 struct sctp_rtoinfo *srto; 2624 2625 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize); 2626 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id); 2627 2628 if (stcb) { 2629 srto->srto_initial = stcb->asoc.initial_rto; 2630 srto->srto_max = stcb->asoc.maxrto; 2631 srto->srto_min = stcb->asoc.minrto; 2632 SCTP_TCB_UNLOCK(stcb); 2633 } else { 2634 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2635 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2636 (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) { 2637 SCTP_INP_RLOCK(inp); 2638 srto->srto_initial = inp->sctp_ep.initial_rto; 2639 srto->srto_max = inp->sctp_ep.sctp_maxrto; 2640 srto->srto_min = inp->sctp_ep.sctp_minrto; 2641 SCTP_INP_RUNLOCK(inp); 2642 } else { 2643 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2644 error = EINVAL; 2645 } 2646 } 2647 if (error == 0) { 2648 *optsize = sizeof(struct sctp_rtoinfo); 2649 } 2650 break; 2651 } 2652 case SCTP_TIMEOUTS: 2653 { 2654 struct sctp_timeouts *stimo; 2655 2656 SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize); 2657 SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id); 2658 2659 if (stcb) { 2660 stimo->stimo_init = stcb->asoc.timoinit; 2661 stimo->stimo_data = stcb->asoc.timodata; 2662 stimo->stimo_sack = stcb->asoc.timosack; 2663 stimo->stimo_shutdown = stcb->asoc.timoshutdown; 2664 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat; 2665 stimo->stimo_cookie = stcb->asoc.timocookie; 2666 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack; 2667 SCTP_TCB_UNLOCK(stcb); 2668 *optsize = sizeof(struct sctp_timeouts); 2669 } else { 2670 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2671 error = EINVAL; 2672 } 2673 break; 2674 } 2675 case SCTP_ASSOCINFO: 2676 { 2677 struct sctp_assocparams *sasoc; 2678 2679 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize); 2680 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id); 2681 2682 if (stcb) { 2683 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life); 2684 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times; 2685 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets; 2686 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd; 2687 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd; 2688 SCTP_TCB_UNLOCK(stcb); 2689 } else { 2690 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2691 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2692 (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) { 2693 SCTP_INP_RLOCK(inp); 2694 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life); 2695 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times; 2696 sasoc->sasoc_number_peer_destinations = 0; 2697 sasoc->sasoc_peer_rwnd = 0; 2698 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv); 2699 SCTP_INP_RUNLOCK(inp); 2700 } else { 2701 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2702 error = EINVAL; 2703 } 2704 } 2705 if (error == 0) { 2706 *optsize = sizeof(struct sctp_assocparams); 2707 } 2708 break; 2709 } 2710 case SCTP_DEFAULT_SEND_PARAM: 2711 { 2712 struct sctp_sndrcvinfo *s_info; 2713 2714 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize); 2715 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id); 2716 2717 if (stcb) { 2718 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send)); 2719 SCTP_TCB_UNLOCK(stcb); 2720 } else { 2721 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2722 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2723 (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) { 2724 SCTP_INP_RLOCK(inp); 2725 memcpy(s_info, &inp->def_send, sizeof(inp->def_send)); 2726 SCTP_INP_RUNLOCK(inp); 2727 } else { 2728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2729 error = EINVAL; 2730 } 2731 } 2732 if (error == 0) { 2733 *optsize = sizeof(struct sctp_sndrcvinfo); 2734 } 2735 break; 2736 } 2737 case SCTP_INITMSG: 2738 { 2739 struct sctp_initmsg *sinit; 2740 2741 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize); 2742 SCTP_INP_RLOCK(inp); 2743 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count; 2744 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome; 2745 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times; 2746 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max; 2747 SCTP_INP_RUNLOCK(inp); 2748 *optsize = sizeof(struct sctp_initmsg); 2749 break; 2750 } 2751 case SCTP_PRIMARY_ADDR: 2752 /* we allow a "get" operation on this */ 2753 { 2754 struct sctp_setprim *ssp; 2755 2756 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize); 2757 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id); 2758 2759 if (stcb) { 2760 /* simply copy out the sockaddr_storage... */ 2761 int len; 2762 2763 len = *optsize; 2764 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len) 2765 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len; 2766 2767 memcpy(&ssp->ssp_addr, 2768 &stcb->asoc.primary_destination->ro._l_addr, 2769 len); 2770 SCTP_TCB_UNLOCK(stcb); 2771 *optsize = sizeof(struct sctp_setprim); 2772 } else { 2773 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2774 error = EINVAL; 2775 } 2776 break; 2777 } 2778 case SCTP_HMAC_IDENT: 2779 { 2780 struct sctp_hmacalgo *shmac; 2781 sctp_hmaclist_t *hmaclist; 2782 uint32_t size; 2783 int i; 2784 2785 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize); 2786 2787 SCTP_INP_RLOCK(inp); 2788 hmaclist = inp->sctp_ep.local_hmacs; 2789 if (hmaclist == NULL) { 2790 /* no HMACs to return */ 2791 *optsize = sizeof(*shmac); 2792 SCTP_INP_RUNLOCK(inp); 2793 break; 2794 } 2795 /* is there room for all of the hmac ids? */ 2796 size = sizeof(*shmac) + (hmaclist->num_algo * 2797 sizeof(shmac->shmac_idents[0])); 2798 if ((size_t)(*optsize) < size) { 2799 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2800 error = EINVAL; 2801 SCTP_INP_RUNLOCK(inp); 2802 break; 2803 } 2804 /* copy in the list */ 2805 shmac->shmac_number_of_idents = hmaclist->num_algo; 2806 for (i = 0; i < hmaclist->num_algo; i++) { 2807 shmac->shmac_idents[i] = hmaclist->hmac[i]; 2808 } 2809 SCTP_INP_RUNLOCK(inp); 2810 *optsize = size; 2811 break; 2812 } 2813 case SCTP_AUTH_ACTIVE_KEY: 2814 { 2815 struct sctp_authkeyid *scact; 2816 2817 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize); 2818 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id); 2819 2820 if (stcb) { 2821 /* get the active key on the assoc */ 2822 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid; 2823 SCTP_TCB_UNLOCK(stcb); 2824 } else { 2825 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2826 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2827 (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) { 2828 /* get the endpoint active key */ 2829 SCTP_INP_RLOCK(inp); 2830 scact->scact_keynumber = inp->sctp_ep.default_keyid; 2831 SCTP_INP_RUNLOCK(inp); 2832 } else { 2833 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2834 error = EINVAL; 2835 } 2836 } 2837 if (error == 0) { 2838 *optsize = sizeof(struct sctp_authkeyid); 2839 } 2840 break; 2841 } 2842 case SCTP_LOCAL_AUTH_CHUNKS: 2843 { 2844 struct sctp_authchunks *sac; 2845 sctp_auth_chklist_t *chklist = NULL; 2846 size_t size = 0; 2847 2848 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize); 2849 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id); 2850 2851 if (stcb) { 2852 /* get off the assoc */ 2853 chklist = stcb->asoc.local_auth_chunks; 2854 /* is there enough space? */ 2855 size = sctp_auth_get_chklist_size(chklist); 2856 if (*optsize < (sizeof(struct sctp_authchunks) + size)) { 2857 error = EINVAL; 2858 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 2859 } else { 2860 /* copy in the chunks */ 2861 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks); 2862 sac->gauth_number_of_chunks = (uint32_t) size; 2863 *optsize = sizeof(struct sctp_authchunks) + size; 2864 } 2865 SCTP_TCB_UNLOCK(stcb); 2866 } else { 2867 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2868 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2869 (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) { 2870 /* get off the endpoint */ 2871 SCTP_INP_RLOCK(inp); 2872 chklist = inp->sctp_ep.local_auth_chunks; 2873 /* is there enough space? */ 2874 size = sctp_auth_get_chklist_size(chklist); 2875 if (*optsize < (sizeof(struct sctp_authchunks) + size)) { 2876 error = EINVAL; 2877 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 2878 } else { 2879 /* copy in the chunks */ 2880 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks); 2881 sac->gauth_number_of_chunks = (uint32_t) size; 2882 *optsize = sizeof(struct sctp_authchunks) + size; 2883 } 2884 SCTP_INP_RUNLOCK(inp); 2885 } else { 2886 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2887 error = EINVAL; 2888 } 2889 } 2890 break; 2891 } 2892 case SCTP_PEER_AUTH_CHUNKS: 2893 { 2894 struct sctp_authchunks *sac; 2895 sctp_auth_chklist_t *chklist = NULL; 2896 size_t size = 0; 2897 2898 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize); 2899 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id); 2900 2901 if (stcb) { 2902 /* get off the assoc */ 2903 chklist = stcb->asoc.peer_auth_chunks; 2904 /* is there enough space? */ 2905 size = sctp_auth_get_chklist_size(chklist); 2906 if (*optsize < (sizeof(struct sctp_authchunks) + size)) { 2907 error = EINVAL; 2908 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 2909 } else { 2910 /* copy in the chunks */ 2911 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks); 2912 sac->gauth_number_of_chunks = (uint32_t) size; 2913 *optsize = sizeof(struct sctp_authchunks) + size; 2914 } 2915 SCTP_TCB_UNLOCK(stcb); 2916 } else { 2917 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 2918 error = ENOENT; 2919 } 2920 break; 2921 } 2922 case SCTP_EVENT: 2923 { 2924 struct sctp_event *event; 2925 uint32_t event_type; 2926 2927 SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize); 2928 SCTP_FIND_STCB(inp, stcb, event->se_assoc_id); 2929 2930 switch (event->se_type) { 2931 case SCTP_ASSOC_CHANGE: 2932 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT; 2933 break; 2934 case SCTP_PEER_ADDR_CHANGE: 2935 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT; 2936 break; 2937 case SCTP_REMOTE_ERROR: 2938 event_type = SCTP_PCB_FLAGS_RECVPEERERR; 2939 break; 2940 case SCTP_SEND_FAILED: 2941 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT; 2942 break; 2943 case SCTP_SHUTDOWN_EVENT: 2944 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT; 2945 break; 2946 case SCTP_ADAPTATION_INDICATION: 2947 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT; 2948 break; 2949 case SCTP_PARTIAL_DELIVERY_EVENT: 2950 event_type = SCTP_PCB_FLAGS_PDAPIEVNT; 2951 break; 2952 case SCTP_AUTHENTICATION_EVENT: 2953 event_type = SCTP_PCB_FLAGS_AUTHEVNT; 2954 break; 2955 case SCTP_STREAM_RESET_EVENT: 2956 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT; 2957 break; 2958 case SCTP_SENDER_DRY_EVENT: 2959 event_type = SCTP_PCB_FLAGS_DRYEVNT; 2960 break; 2961 case SCTP_NOTIFICATIONS_STOPPED_EVENT: 2962 event_type = 0; 2963 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP); 2964 error = ENOTSUP; 2965 break; 2966 case SCTP_ASSOC_RESET_EVENT: 2967 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT; 2968 break; 2969 case SCTP_STREAM_CHANGE_EVENT: 2970 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT; 2971 break; 2972 case SCTP_SEND_FAILED_EVENT: 2973 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT; 2974 break; 2975 default: 2976 event_type = 0; 2977 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2978 error = EINVAL; 2979 break; 2980 } 2981 if (event_type > 0) { 2982 if (stcb) { 2983 event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type); 2984 SCTP_TCB_UNLOCK(stcb); 2985 } else { 2986 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2987 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 2988 (event->se_assoc_id == SCTP_FUTURE_ASSOC)) { 2989 SCTP_INP_RLOCK(inp); 2990 event->se_on = sctp_is_feature_on(inp, event_type); 2991 SCTP_INP_RUNLOCK(inp); 2992 } else { 2993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 2994 error = EINVAL; 2995 } 2996 } 2997 } 2998 if (error == 0) { 2999 *optsize = sizeof(struct sctp_event); 3000 } 3001 break; 3002 } 3003 case SCTP_RECVRCVINFO: 3004 { 3005 int onoff; 3006 3007 if (*optsize < sizeof(int)) { 3008 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3009 error = EINVAL; 3010 } else { 3011 SCTP_INP_RLOCK(inp); 3012 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO); 3013 SCTP_INP_RUNLOCK(inp); 3014 } 3015 if (error == 0) { 3016 /* return the option value */ 3017 *(int *)optval = onoff; 3018 *optsize = sizeof(int); 3019 } 3020 break; 3021 } 3022 case SCTP_RECVNXTINFO: 3023 { 3024 int onoff; 3025 3026 if (*optsize < sizeof(int)) { 3027 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3028 error = EINVAL; 3029 } else { 3030 SCTP_INP_RLOCK(inp); 3031 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO); 3032 SCTP_INP_RUNLOCK(inp); 3033 } 3034 if (error == 0) { 3035 /* return the option value */ 3036 *(int *)optval = onoff; 3037 *optsize = sizeof(int); 3038 } 3039 break; 3040 } 3041 case SCTP_DEFAULT_SNDINFO: 3042 { 3043 struct sctp_sndinfo *info; 3044 3045 SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize); 3046 SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id); 3047 3048 if (stcb) { 3049 info->snd_sid = stcb->asoc.def_send.sinfo_stream; 3050 info->snd_flags = stcb->asoc.def_send.sinfo_flags; 3051 info->snd_flags &= 0xfff0; 3052 info->snd_ppid = stcb->asoc.def_send.sinfo_ppid; 3053 info->snd_context = stcb->asoc.def_send.sinfo_context; 3054 SCTP_TCB_UNLOCK(stcb); 3055 } else { 3056 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3057 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3058 (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) { 3059 SCTP_INP_RLOCK(inp); 3060 info->snd_sid = inp->def_send.sinfo_stream; 3061 info->snd_flags = inp->def_send.sinfo_flags; 3062 info->snd_flags &= 0xfff0; 3063 info->snd_ppid = inp->def_send.sinfo_ppid; 3064 info->snd_context = inp->def_send.sinfo_context; 3065 SCTP_INP_RUNLOCK(inp); 3066 } else { 3067 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3068 error = EINVAL; 3069 } 3070 } 3071 if (error == 0) { 3072 *optsize = sizeof(struct sctp_sndinfo); 3073 } 3074 break; 3075 } 3076 case SCTP_DEFAULT_PRINFO: 3077 { 3078 struct sctp_default_prinfo *info; 3079 3080 SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize); 3081 SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id); 3082 3083 if (stcb) { 3084 info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags); 3085 info->pr_value = stcb->asoc.def_send.sinfo_timetolive; 3086 SCTP_TCB_UNLOCK(stcb); 3087 } else { 3088 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3089 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3090 (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) { 3091 SCTP_INP_RLOCK(inp); 3092 info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags); 3093 info->pr_value = inp->def_send.sinfo_timetolive; 3094 SCTP_INP_RUNLOCK(inp); 3095 } else { 3096 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3097 error = EINVAL; 3098 } 3099 } 3100 if (error == 0) { 3101 *optsize = sizeof(struct sctp_default_prinfo); 3102 } 3103 break; 3104 } 3105 case SCTP_PEER_ADDR_THLDS: 3106 { 3107 struct sctp_paddrthlds *thlds; 3108 struct sctp_nets *net; 3109 3110 SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize); 3111 SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id); 3112 3113 net = NULL; 3114 if (stcb) { 3115 net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_address); 3116 } else { 3117 /* 3118 * We increment here since 3119 * sctp_findassociation_ep_addr() wil do a 3120 * decrement if it finds the stcb as long as 3121 * the locked tcb (last argument) is NOT a 3122 * TCB.. aka NULL. 3123 */ 3124 SCTP_INP_INCR_REF(inp); 3125 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&thlds->spt_address, &net, NULL, NULL); 3126 if (stcb == NULL) { 3127 SCTP_INP_DECR_REF(inp); 3128 } 3129 } 3130 if (stcb && (net == NULL)) { 3131 struct sockaddr *sa; 3132 3133 sa = (struct sockaddr *)&thlds->spt_address; 3134 #ifdef INET 3135 if (sa->sa_family == AF_INET) { 3136 struct sockaddr_in *sin; 3137 3138 sin = (struct sockaddr_in *)sa; 3139 if (sin->sin_addr.s_addr) { 3140 error = EINVAL; 3141 SCTP_TCB_UNLOCK(stcb); 3142 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 3143 break; 3144 } 3145 } else 3146 #endif 3147 #ifdef INET6 3148 if (sa->sa_family == AF_INET6) { 3149 struct sockaddr_in6 *sin6; 3150 3151 sin6 = (struct sockaddr_in6 *)sa; 3152 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 3153 error = EINVAL; 3154 SCTP_TCB_UNLOCK(stcb); 3155 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 3156 break; 3157 } 3158 } else 3159 #endif 3160 { 3161 error = EAFNOSUPPORT; 3162 SCTP_TCB_UNLOCK(stcb); 3163 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 3164 break; 3165 } 3166 } 3167 if (stcb) { 3168 if (net) { 3169 thlds->spt_pathmaxrxt = net->failure_threshold; 3170 thlds->spt_pathpfthld = net->pf_threshold; 3171 } else { 3172 thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure; 3173 thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold; 3174 } 3175 thlds->spt_assoc_id = sctp_get_associd(stcb); 3176 SCTP_TCB_UNLOCK(stcb); 3177 } else { 3178 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3179 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3180 (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) { 3181 /* Use endpoint defaults */ 3182 SCTP_INP_RLOCK(inp); 3183 thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure; 3184 thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold; 3185 SCTP_INP_RUNLOCK(inp); 3186 } else { 3187 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3188 error = EINVAL; 3189 } 3190 } 3191 if (error == 0) { 3192 *optsize = sizeof(struct sctp_paddrthlds); 3193 } 3194 break; 3195 } 3196 case SCTP_REMOTE_UDP_ENCAPS_PORT: 3197 { 3198 struct sctp_udpencaps *encaps; 3199 struct sctp_nets *net; 3200 3201 SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize); 3202 SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id); 3203 3204 if (stcb) { 3205 net = sctp_findnet(stcb, (struct sockaddr *)&encaps->sue_address); 3206 } else { 3207 /* 3208 * We increment here since 3209 * sctp_findassociation_ep_addr() wil do a 3210 * decrement if it finds the stcb as long as 3211 * the locked tcb (last argument) is NOT a 3212 * TCB.. aka NULL. 3213 */ 3214 net = NULL; 3215 SCTP_INP_INCR_REF(inp); 3216 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&encaps->sue_address, &net, NULL, NULL); 3217 if (stcb == NULL) { 3218 SCTP_INP_DECR_REF(inp); 3219 } 3220 } 3221 if (stcb && (net == NULL)) { 3222 struct sockaddr *sa; 3223 3224 sa = (struct sockaddr *)&encaps->sue_address; 3225 #ifdef INET 3226 if (sa->sa_family == AF_INET) { 3227 struct sockaddr_in *sin; 3228 3229 sin = (struct sockaddr_in *)sa; 3230 if (sin->sin_addr.s_addr) { 3231 error = EINVAL; 3232 SCTP_TCB_UNLOCK(stcb); 3233 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 3234 break; 3235 } 3236 } else 3237 #endif 3238 #ifdef INET6 3239 if (sa->sa_family == AF_INET6) { 3240 struct sockaddr_in6 *sin6; 3241 3242 sin6 = (struct sockaddr_in6 *)sa; 3243 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 3244 error = EINVAL; 3245 SCTP_TCB_UNLOCK(stcb); 3246 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 3247 break; 3248 } 3249 } else 3250 #endif 3251 { 3252 error = EAFNOSUPPORT; 3253 SCTP_TCB_UNLOCK(stcb); 3254 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 3255 break; 3256 } 3257 } 3258 if (stcb) { 3259 if (net) { 3260 encaps->sue_port = net->port; 3261 } else { 3262 encaps->sue_port = stcb->asoc.port; 3263 } 3264 SCTP_TCB_UNLOCK(stcb); 3265 } else { 3266 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3267 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3268 (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) { 3269 SCTP_INP_RLOCK(inp); 3270 encaps->sue_port = inp->sctp_ep.port; 3271 SCTP_INP_RUNLOCK(inp); 3272 } else { 3273 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3274 error = EINVAL; 3275 } 3276 } 3277 if (error == 0) { 3278 *optsize = sizeof(struct sctp_paddrparams); 3279 } 3280 break; 3281 } 3282 case SCTP_ENABLE_STREAM_RESET: 3283 { 3284 struct sctp_assoc_value *av; 3285 3286 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 3287 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3288 3289 if (stcb) { 3290 av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support; 3291 SCTP_TCB_UNLOCK(stcb); 3292 } else { 3293 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3294 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3295 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 3296 SCTP_INP_RLOCK(inp); 3297 av->assoc_value = (uint32_t) inp->local_strreset_support; 3298 SCTP_INP_RUNLOCK(inp); 3299 } else { 3300 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3301 error = EINVAL; 3302 } 3303 } 3304 if (error == 0) { 3305 *optsize = sizeof(struct sctp_assoc_value); 3306 } 3307 break; 3308 } 3309 default: 3310 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT); 3311 error = ENOPROTOOPT; 3312 break; 3313 } /* end switch (sopt->sopt_name) */ 3314 if (error) { 3315 *optsize = 0; 3316 } 3317 return (error); 3318 } 3319 3320 static int 3321 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize, 3322 void *p) 3323 { 3324 int error, set_opt; 3325 uint32_t *mopt; 3326 struct sctp_tcb *stcb = NULL; 3327 struct sctp_inpcb *inp = NULL; 3328 uint32_t vrf_id; 3329 3330 if (optval == NULL) { 3331 SCTP_PRINTF("optval is NULL\n"); 3332 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3333 return (EINVAL); 3334 } 3335 inp = (struct sctp_inpcb *)so->so_pcb; 3336 if (inp == NULL) { 3337 SCTP_PRINTF("inp is NULL?\n"); 3338 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3339 return (EINVAL); 3340 } 3341 vrf_id = inp->def_vrf_id; 3342 3343 error = 0; 3344 switch (optname) { 3345 case SCTP_NODELAY: 3346 case SCTP_AUTOCLOSE: 3347 case SCTP_AUTO_ASCONF: 3348 case SCTP_EXPLICIT_EOR: 3349 case SCTP_DISABLE_FRAGMENTS: 3350 case SCTP_USE_EXT_RCVINFO: 3351 case SCTP_I_WANT_MAPPED_V4_ADDR: 3352 /* copy in the option value */ 3353 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize); 3354 set_opt = 0; 3355 if (error) 3356 break; 3357 switch (optname) { 3358 case SCTP_DISABLE_FRAGMENTS: 3359 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT; 3360 break; 3361 case SCTP_AUTO_ASCONF: 3362 /* 3363 * NOTE: we don't really support this flag 3364 */ 3365 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 3366 /* only valid for bound all sockets */ 3367 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) && 3368 (*mopt != 0)) { 3369 /* forbidden by admin */ 3370 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM); 3371 return (EPERM); 3372 } 3373 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF; 3374 } else { 3375 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3376 return (EINVAL); 3377 } 3378 break; 3379 case SCTP_EXPLICIT_EOR: 3380 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR; 3381 break; 3382 case SCTP_USE_EXT_RCVINFO: 3383 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO; 3384 break; 3385 case SCTP_I_WANT_MAPPED_V4_ADDR: 3386 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 3387 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4; 3388 } else { 3389 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3390 return (EINVAL); 3391 } 3392 break; 3393 case SCTP_NODELAY: 3394 set_opt = SCTP_PCB_FLAGS_NODELAY; 3395 break; 3396 case SCTP_AUTOCLOSE: 3397 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3398 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 3399 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3400 return (EINVAL); 3401 } 3402 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE; 3403 /* 3404 * The value is in ticks. Note this does not effect 3405 * old associations, only new ones. 3406 */ 3407 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt); 3408 break; 3409 } 3410 SCTP_INP_WLOCK(inp); 3411 if (*mopt != 0) { 3412 sctp_feature_on(inp, set_opt); 3413 } else { 3414 sctp_feature_off(inp, set_opt); 3415 } 3416 SCTP_INP_WUNLOCK(inp); 3417 break; 3418 case SCTP_REUSE_PORT: 3419 { 3420 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize); 3421 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { 3422 /* Can't set it after we are bound */ 3423 error = EINVAL; 3424 break; 3425 } 3426 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) { 3427 /* Can't do this for a 1-m socket */ 3428 error = EINVAL; 3429 break; 3430 } 3431 if (optval) 3432 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE); 3433 else 3434 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE); 3435 break; 3436 } 3437 case SCTP_PARTIAL_DELIVERY_POINT: 3438 { 3439 uint32_t *value; 3440 3441 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize); 3442 if (*value > SCTP_SB_LIMIT_RCV(so)) { 3443 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3444 error = EINVAL; 3445 break; 3446 } 3447 inp->partial_delivery_point = *value; 3448 break; 3449 } 3450 case SCTP_FRAGMENT_INTERLEAVE: 3451 /* not yet until we re-write sctp_recvmsg() */ 3452 { 3453 uint32_t *level; 3454 3455 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize); 3456 if (*level == SCTP_FRAG_LEVEL_2) { 3457 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 3458 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 3459 } else if (*level == SCTP_FRAG_LEVEL_1) { 3460 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 3461 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 3462 } else if (*level == SCTP_FRAG_LEVEL_0) { 3463 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 3464 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 3465 3466 } else { 3467 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3468 error = EINVAL; 3469 } 3470 break; 3471 } 3472 case SCTP_CMT_ON_OFF: 3473 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) { 3474 struct sctp_assoc_value *av; 3475 3476 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 3477 if (av->assoc_value > SCTP_CMT_MAX) { 3478 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3479 error = EINVAL; 3480 break; 3481 } 3482 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3483 if (stcb) { 3484 stcb->asoc.sctp_cmt_on_off = av->assoc_value; 3485 SCTP_TCB_UNLOCK(stcb); 3486 } else { 3487 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3488 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3489 (av->assoc_id == SCTP_FUTURE_ASSOC) || 3490 (av->assoc_id == SCTP_ALL_ASSOC)) { 3491 SCTP_INP_WLOCK(inp); 3492 inp->sctp_cmt_on_off = av->assoc_value; 3493 SCTP_INP_WUNLOCK(inp); 3494 } 3495 if ((av->assoc_id == SCTP_CURRENT_ASSOC) || 3496 (av->assoc_id == SCTP_ALL_ASSOC)) { 3497 SCTP_INP_RLOCK(inp); 3498 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3499 SCTP_TCB_LOCK(stcb); 3500 stcb->asoc.sctp_cmt_on_off = av->assoc_value; 3501 SCTP_TCB_UNLOCK(stcb); 3502 } 3503 SCTP_INP_RUNLOCK(inp); 3504 } 3505 } 3506 } else { 3507 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT); 3508 error = ENOPROTOOPT; 3509 } 3510 break; 3511 case SCTP_PLUGGABLE_CC: 3512 { 3513 struct sctp_assoc_value *av; 3514 struct sctp_nets *net; 3515 3516 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 3517 if ((av->assoc_value != SCTP_CC_RFC2581) && 3518 (av->assoc_value != SCTP_CC_HSTCP) && 3519 (av->assoc_value != SCTP_CC_HTCP) && 3520 (av->assoc_value != SCTP_CC_RTCC)) { 3521 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3522 error = EINVAL; 3523 break; 3524 } 3525 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3526 if (stcb) { 3527 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value]; 3528 stcb->asoc.congestion_control_module = av->assoc_value; 3529 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) { 3530 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3531 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net); 3532 } 3533 } 3534 SCTP_TCB_UNLOCK(stcb); 3535 } else { 3536 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3537 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3538 (av->assoc_id == SCTP_FUTURE_ASSOC) || 3539 (av->assoc_id == SCTP_ALL_ASSOC)) { 3540 SCTP_INP_WLOCK(inp); 3541 inp->sctp_ep.sctp_default_cc_module = av->assoc_value; 3542 SCTP_INP_WUNLOCK(inp); 3543 } 3544 if ((av->assoc_id == SCTP_CURRENT_ASSOC) || 3545 (av->assoc_id == SCTP_ALL_ASSOC)) { 3546 SCTP_INP_RLOCK(inp); 3547 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3548 SCTP_TCB_LOCK(stcb); 3549 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value]; 3550 stcb->asoc.congestion_control_module = av->assoc_value; 3551 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) { 3552 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3553 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net); 3554 } 3555 } 3556 SCTP_TCB_UNLOCK(stcb); 3557 } 3558 SCTP_INP_RUNLOCK(inp); 3559 } 3560 } 3561 break; 3562 } 3563 case SCTP_CC_OPTION: 3564 { 3565 struct sctp_cc_option *cc_opt; 3566 3567 SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize); 3568 SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id); 3569 if (stcb == NULL) { 3570 if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) { 3571 SCTP_INP_RLOCK(inp); 3572 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3573 SCTP_TCB_LOCK(stcb); 3574 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) { 3575 (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt); 3576 } 3577 SCTP_TCB_UNLOCK(stcb); 3578 } 3579 SCTP_INP_RUNLOCK(inp); 3580 } else { 3581 error = EINVAL; 3582 } 3583 } else { 3584 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) { 3585 error = ENOTSUP; 3586 } else { 3587 error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, 3588 cc_opt); 3589 } 3590 SCTP_TCB_UNLOCK(stcb); 3591 } 3592 break; 3593 } 3594 case SCTP_PLUGGABLE_SS: 3595 { 3596 struct sctp_assoc_value *av; 3597 3598 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 3599 if ((av->assoc_value != SCTP_SS_DEFAULT) && 3600 (av->assoc_value != SCTP_SS_ROUND_ROBIN) && 3601 (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) && 3602 (av->assoc_value != SCTP_SS_PRIORITY) && 3603 (av->assoc_value != SCTP_SS_FAIR_BANDWITH) && 3604 (av->assoc_value != SCTP_SS_FIRST_COME)) { 3605 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3606 error = EINVAL; 3607 break; 3608 } 3609 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3610 if (stcb) { 3611 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1); 3612 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value]; 3613 stcb->asoc.stream_scheduling_module = av->assoc_value; 3614 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1); 3615 SCTP_TCB_UNLOCK(stcb); 3616 } else { 3617 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3618 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3619 (av->assoc_id == SCTP_FUTURE_ASSOC) || 3620 (av->assoc_id == SCTP_ALL_ASSOC)) { 3621 SCTP_INP_WLOCK(inp); 3622 inp->sctp_ep.sctp_default_ss_module = av->assoc_value; 3623 SCTP_INP_WUNLOCK(inp); 3624 } 3625 if ((av->assoc_id == SCTP_CURRENT_ASSOC) || 3626 (av->assoc_id == SCTP_ALL_ASSOC)) { 3627 SCTP_INP_RLOCK(inp); 3628 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3629 SCTP_TCB_LOCK(stcb); 3630 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1); 3631 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value]; 3632 stcb->asoc.stream_scheduling_module = av->assoc_value; 3633 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1); 3634 SCTP_TCB_UNLOCK(stcb); 3635 } 3636 SCTP_INP_RUNLOCK(inp); 3637 } 3638 } 3639 break; 3640 } 3641 case SCTP_SS_VALUE: 3642 { 3643 struct sctp_stream_value *av; 3644 3645 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize); 3646 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3647 if (stcb) { 3648 if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id], 3649 av->stream_value) < 0) { 3650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3651 error = EINVAL; 3652 } 3653 SCTP_TCB_UNLOCK(stcb); 3654 } else { 3655 if (av->assoc_id == SCTP_CURRENT_ASSOC) { 3656 SCTP_INP_RLOCK(inp); 3657 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3658 SCTP_TCB_LOCK(stcb); 3659 stcb->asoc.ss_functions.sctp_ss_set_value(stcb, 3660 &stcb->asoc, 3661 &stcb->asoc.strmout[av->stream_id], 3662 av->stream_value); 3663 SCTP_TCB_UNLOCK(stcb); 3664 } 3665 SCTP_INP_RUNLOCK(inp); 3666 3667 } else { 3668 /* 3669 * Can't set stream value without 3670 * association 3671 */ 3672 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3673 error = EINVAL; 3674 } 3675 } 3676 break; 3677 } 3678 case SCTP_CLR_STAT_LOG: 3679 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 3680 error = EOPNOTSUPP; 3681 break; 3682 case SCTP_CONTEXT: 3683 { 3684 struct sctp_assoc_value *av; 3685 3686 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 3687 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3688 3689 if (stcb) { 3690 stcb->asoc.context = av->assoc_value; 3691 SCTP_TCB_UNLOCK(stcb); 3692 } else { 3693 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3694 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3695 (av->assoc_id == SCTP_FUTURE_ASSOC) || 3696 (av->assoc_id == SCTP_ALL_ASSOC)) { 3697 SCTP_INP_WLOCK(inp); 3698 inp->sctp_context = av->assoc_value; 3699 SCTP_INP_WUNLOCK(inp); 3700 } 3701 if ((av->assoc_id == SCTP_CURRENT_ASSOC) || 3702 (av->assoc_id == SCTP_ALL_ASSOC)) { 3703 SCTP_INP_RLOCK(inp); 3704 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3705 SCTP_TCB_LOCK(stcb); 3706 stcb->asoc.context = av->assoc_value; 3707 SCTP_TCB_UNLOCK(stcb); 3708 } 3709 SCTP_INP_RUNLOCK(inp); 3710 } 3711 } 3712 break; 3713 } 3714 case SCTP_VRF_ID: 3715 { 3716 uint32_t *default_vrfid; 3717 3718 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize); 3719 if (*default_vrfid > SCTP_MAX_VRF_ID) { 3720 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3721 error = EINVAL; 3722 break; 3723 } 3724 inp->def_vrf_id = *default_vrfid; 3725 break; 3726 } 3727 case SCTP_DEL_VRF_ID: 3728 { 3729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 3730 error = EOPNOTSUPP; 3731 break; 3732 } 3733 case SCTP_ADD_VRF_ID: 3734 { 3735 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 3736 error = EOPNOTSUPP; 3737 break; 3738 } 3739 case SCTP_DELAYED_SACK: 3740 { 3741 struct sctp_sack_info *sack; 3742 3743 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize); 3744 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id); 3745 if (sack->sack_delay) { 3746 if (sack->sack_delay > SCTP_MAX_SACK_DELAY) 3747 sack->sack_delay = SCTP_MAX_SACK_DELAY; 3748 if (MSEC_TO_TICKS(sack->sack_delay) < 1) { 3749 sack->sack_delay = TICKS_TO_MSEC(1); 3750 } 3751 } 3752 if (stcb) { 3753 if (sack->sack_delay) { 3754 stcb->asoc.delayed_ack = sack->sack_delay; 3755 } 3756 if (sack->sack_freq) { 3757 stcb->asoc.sack_freq = sack->sack_freq; 3758 } 3759 SCTP_TCB_UNLOCK(stcb); 3760 } else { 3761 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3762 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3763 (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) || 3764 (sack->sack_assoc_id == SCTP_ALL_ASSOC)) { 3765 SCTP_INP_WLOCK(inp); 3766 if (sack->sack_delay) { 3767 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay); 3768 } 3769 if (sack->sack_freq) { 3770 inp->sctp_ep.sctp_sack_freq = sack->sack_freq; 3771 } 3772 SCTP_INP_WUNLOCK(inp); 3773 } 3774 if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) || 3775 (sack->sack_assoc_id == SCTP_ALL_ASSOC)) { 3776 SCTP_INP_RLOCK(inp); 3777 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3778 SCTP_TCB_LOCK(stcb); 3779 if (sack->sack_delay) { 3780 stcb->asoc.delayed_ack = sack->sack_delay; 3781 } 3782 if (sack->sack_freq) { 3783 stcb->asoc.sack_freq = sack->sack_freq; 3784 } 3785 SCTP_TCB_UNLOCK(stcb); 3786 } 3787 SCTP_INP_RUNLOCK(inp); 3788 } 3789 } 3790 break; 3791 } 3792 case SCTP_AUTH_CHUNK: 3793 { 3794 struct sctp_authchunk *sauth; 3795 3796 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize); 3797 3798 SCTP_INP_WLOCK(inp); 3799 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) { 3800 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3801 error = EINVAL; 3802 } 3803 SCTP_INP_WUNLOCK(inp); 3804 break; 3805 } 3806 case SCTP_AUTH_KEY: 3807 { 3808 struct sctp_authkey *sca; 3809 struct sctp_keyhead *shared_keys; 3810 sctp_sharedkey_t *shared_key; 3811 sctp_key_t *key = NULL; 3812 size_t size; 3813 3814 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize); 3815 if (sca->sca_keylength == 0) { 3816 size = optsize - sizeof(struct sctp_authkey); 3817 } else { 3818 if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) { 3819 size = sca->sca_keylength; 3820 } else { 3821 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3822 error = EINVAL; 3823 break; 3824 } 3825 } 3826 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id); 3827 3828 if (stcb) { 3829 shared_keys = &stcb->asoc.shared_keys; 3830 /* clear the cached keys for this key id */ 3831 sctp_clear_cachedkeys(stcb, sca->sca_keynumber); 3832 /* 3833 * create the new shared key and 3834 * insert/replace it 3835 */ 3836 if (size > 0) { 3837 key = sctp_set_key(sca->sca_key, (uint32_t) size); 3838 if (key == NULL) { 3839 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM); 3840 error = ENOMEM; 3841 SCTP_TCB_UNLOCK(stcb); 3842 break; 3843 } 3844 } 3845 shared_key = sctp_alloc_sharedkey(); 3846 if (shared_key == NULL) { 3847 sctp_free_key(key); 3848 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM); 3849 error = ENOMEM; 3850 SCTP_TCB_UNLOCK(stcb); 3851 break; 3852 } 3853 shared_key->key = key; 3854 shared_key->keyid = sca->sca_keynumber; 3855 error = sctp_insert_sharedkey(shared_keys, shared_key); 3856 SCTP_TCB_UNLOCK(stcb); 3857 } else { 3858 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 3859 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 3860 (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) || 3861 (sca->sca_assoc_id == SCTP_ALL_ASSOC)) { 3862 SCTP_INP_WLOCK(inp); 3863 shared_keys = &inp->sctp_ep.shared_keys; 3864 /* 3865 * clear the cached keys on all 3866 * assocs for this key id 3867 */ 3868 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber); 3869 /* 3870 * create the new shared key and 3871 * insert/replace it 3872 */ 3873 if (size > 0) { 3874 key = sctp_set_key(sca->sca_key, (uint32_t) size); 3875 if (key == NULL) { 3876 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM); 3877 error = ENOMEM; 3878 SCTP_INP_WUNLOCK(inp); 3879 break; 3880 } 3881 } 3882 shared_key = sctp_alloc_sharedkey(); 3883 if (shared_key == NULL) { 3884 sctp_free_key(key); 3885 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM); 3886 error = ENOMEM; 3887 SCTP_INP_WUNLOCK(inp); 3888 break; 3889 } 3890 shared_key->key = key; 3891 shared_key->keyid = sca->sca_keynumber; 3892 error = sctp_insert_sharedkey(shared_keys, shared_key); 3893 SCTP_INP_WUNLOCK(inp); 3894 } 3895 if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) || 3896 (sca->sca_assoc_id == SCTP_ALL_ASSOC)) { 3897 SCTP_INP_RLOCK(inp); 3898 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 3899 SCTP_TCB_LOCK(stcb); 3900 shared_keys = &stcb->asoc.shared_keys; 3901 /* 3902 * clear the cached keys for 3903 * this key id 3904 */ 3905 sctp_clear_cachedkeys(stcb, sca->sca_keynumber); 3906 /* 3907 * create the new shared key 3908 * and insert/replace it 3909 */ 3910 if (size > 0) { 3911 key = sctp_set_key(sca->sca_key, (uint32_t) size); 3912 if (key == NULL) { 3913 SCTP_TCB_UNLOCK(stcb); 3914 continue; 3915 } 3916 } 3917 shared_key = sctp_alloc_sharedkey(); 3918 if (shared_key == NULL) { 3919 sctp_free_key(key); 3920 SCTP_TCB_UNLOCK(stcb); 3921 continue; 3922 } 3923 shared_key->key = key; 3924 shared_key->keyid = sca->sca_keynumber; 3925 error = sctp_insert_sharedkey(shared_keys, shared_key); 3926 SCTP_TCB_UNLOCK(stcb); 3927 } 3928 SCTP_INP_RUNLOCK(inp); 3929 } 3930 } 3931 break; 3932 } 3933 case SCTP_HMAC_IDENT: 3934 { 3935 struct sctp_hmacalgo *shmac; 3936 sctp_hmaclist_t *hmaclist; 3937 uint16_t hmacid; 3938 uint32_t i; 3939 size_t found; 3940 3941 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize); 3942 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) { 3943 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3944 error = EINVAL; 3945 break; 3946 } 3947 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents); 3948 if (hmaclist == NULL) { 3949 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM); 3950 error = ENOMEM; 3951 break; 3952 } 3953 for (i = 0; i < shmac->shmac_number_of_idents; i++) { 3954 hmacid = shmac->shmac_idents[i]; 3955 if (sctp_auth_add_hmacid(hmaclist, hmacid)) { 3956 /* invalid HMACs were found */ ; 3957 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3958 error = EINVAL; 3959 sctp_free_hmaclist(hmaclist); 3960 goto sctp_set_hmac_done; 3961 } 3962 } 3963 found = 0; 3964 for (i = 0; i < hmaclist->num_algo; i++) { 3965 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) { 3966 /* already in list */ 3967 found = 1; 3968 } 3969 } 3970 if (!found) { 3971 sctp_free_hmaclist(hmaclist); 3972 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 3973 error = EINVAL; 3974 break; 3975 } 3976 /* set it on the endpoint */ 3977 SCTP_INP_WLOCK(inp); 3978 if (inp->sctp_ep.local_hmacs) 3979 sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 3980 inp->sctp_ep.local_hmacs = hmaclist; 3981 SCTP_INP_WUNLOCK(inp); 3982 sctp_set_hmac_done: 3983 break; 3984 } 3985 case SCTP_AUTH_ACTIVE_KEY: 3986 { 3987 struct sctp_authkeyid *scact; 3988 3989 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize); 3990 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id); 3991 3992 /* set the active key on the right place */ 3993 if (stcb) { 3994 /* set the active key on the assoc */ 3995 if (sctp_auth_setactivekey(stcb, 3996 scact->scact_keynumber)) { 3997 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, 3998 SCTP_FROM_SCTP_USRREQ, 3999 EINVAL); 4000 error = EINVAL; 4001 } 4002 SCTP_TCB_UNLOCK(stcb); 4003 } else { 4004 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4005 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4006 (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) || 4007 (scact->scact_assoc_id == SCTP_ALL_ASSOC)) { 4008 SCTP_INP_WLOCK(inp); 4009 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) { 4010 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4011 error = EINVAL; 4012 } 4013 SCTP_INP_WUNLOCK(inp); 4014 } 4015 if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) || 4016 (scact->scact_assoc_id == SCTP_ALL_ASSOC)) { 4017 SCTP_INP_RLOCK(inp); 4018 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4019 SCTP_TCB_LOCK(stcb); 4020 sctp_auth_setactivekey(stcb, scact->scact_keynumber); 4021 SCTP_TCB_UNLOCK(stcb); 4022 } 4023 SCTP_INP_RUNLOCK(inp); 4024 } 4025 } 4026 break; 4027 } 4028 case SCTP_AUTH_DELETE_KEY: 4029 { 4030 struct sctp_authkeyid *scdel; 4031 4032 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize); 4033 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id); 4034 4035 /* delete the key from the right place */ 4036 if (stcb) { 4037 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) { 4038 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4039 error = EINVAL; 4040 } 4041 SCTP_TCB_UNLOCK(stcb); 4042 } else { 4043 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4044 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4045 (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) || 4046 (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) { 4047 SCTP_INP_WLOCK(inp); 4048 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) { 4049 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4050 error = EINVAL; 4051 } 4052 SCTP_INP_WUNLOCK(inp); 4053 } 4054 if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) || 4055 (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) { 4056 SCTP_INP_RLOCK(inp); 4057 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4058 SCTP_TCB_LOCK(stcb); 4059 sctp_delete_sharedkey(stcb, scdel->scact_keynumber); 4060 SCTP_TCB_UNLOCK(stcb); 4061 } 4062 SCTP_INP_RUNLOCK(inp); 4063 } 4064 } 4065 break; 4066 } 4067 case SCTP_AUTH_DEACTIVATE_KEY: 4068 { 4069 struct sctp_authkeyid *keyid; 4070 4071 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize); 4072 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id); 4073 4074 /* deactivate the key from the right place */ 4075 if (stcb) { 4076 if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) { 4077 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4078 error = EINVAL; 4079 } 4080 SCTP_TCB_UNLOCK(stcb); 4081 } else { 4082 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4083 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4084 (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) || 4085 (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) { 4086 SCTP_INP_WLOCK(inp); 4087 if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) { 4088 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4089 error = EINVAL; 4090 } 4091 SCTP_INP_WUNLOCK(inp); 4092 } 4093 if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) || 4094 (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) { 4095 SCTP_INP_RLOCK(inp); 4096 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4097 SCTP_TCB_LOCK(stcb); 4098 sctp_deact_sharedkey(stcb, keyid->scact_keynumber); 4099 SCTP_TCB_UNLOCK(stcb); 4100 } 4101 SCTP_INP_RUNLOCK(inp); 4102 } 4103 } 4104 break; 4105 } 4106 case SCTP_ENABLE_STREAM_RESET: 4107 { 4108 struct sctp_assoc_value *av; 4109 4110 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 4111 if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) { 4112 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4113 error = EINVAL; 4114 break; 4115 } 4116 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 4117 if (stcb) { 4118 stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value; 4119 SCTP_TCB_UNLOCK(stcb); 4120 } else { 4121 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4122 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4123 (av->assoc_id == SCTP_FUTURE_ASSOC) || 4124 (av->assoc_id == SCTP_ALL_ASSOC)) { 4125 SCTP_INP_WLOCK(inp); 4126 inp->local_strreset_support = (uint8_t) av->assoc_value; 4127 SCTP_INP_WUNLOCK(inp); 4128 } 4129 if ((av->assoc_id == SCTP_CURRENT_ASSOC) || 4130 (av->assoc_id == SCTP_ALL_ASSOC)) { 4131 SCTP_INP_RLOCK(inp); 4132 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4133 SCTP_TCB_LOCK(stcb); 4134 stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value; 4135 SCTP_TCB_UNLOCK(stcb); 4136 } 4137 SCTP_INP_RUNLOCK(inp); 4138 } 4139 } 4140 break; 4141 } 4142 case SCTP_RESET_STREAMS: 4143 { 4144 struct sctp_reset_streams *strrst; 4145 int i, send_out = 0; 4146 int send_in = 0; 4147 4148 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize); 4149 SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id); 4150 if (stcb == NULL) { 4151 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 4152 error = ENOENT; 4153 break; 4154 } 4155 if (stcb->asoc.peer_supports_strreset == 0) { 4156 /* 4157 * Peer does not support the chunk type. 4158 */ 4159 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 4160 error = EOPNOTSUPP; 4161 SCTP_TCB_UNLOCK(stcb); 4162 break; 4163 } 4164 if (stcb->asoc.stream_reset_outstanding) { 4165 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 4166 error = EALREADY; 4167 SCTP_TCB_UNLOCK(stcb); 4168 break; 4169 } 4170 if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) { 4171 send_in = 1; 4172 } 4173 if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) { 4174 send_out = 1; 4175 } 4176 if ((send_in == 0) && (send_out == 0)) { 4177 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4178 error = EINVAL; 4179 SCTP_TCB_UNLOCK(stcb); 4180 break; 4181 } 4182 for (i = 0; i < strrst->srs_number_streams; i++) { 4183 if ((send_in) && 4184 (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) { 4185 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4186 error = EINVAL; 4187 break; 4188 } 4189 if ((send_out) && 4190 (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) { 4191 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4192 error = EINVAL; 4193 break; 4194 } 4195 } 4196 if (error) { 4197 SCTP_TCB_UNLOCK(stcb); 4198 break; 4199 } 4200 error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams, 4201 strrst->srs_stream_list, 4202 send_out, send_in, 0, 0, 0, 0, 0); 4203 4204 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED); 4205 SCTP_TCB_UNLOCK(stcb); 4206 break; 4207 } 4208 case SCTP_ADD_STREAMS: 4209 { 4210 struct sctp_add_streams *stradd; 4211 uint8_t addstream = 0; 4212 uint16_t add_o_strmcnt = 0; 4213 uint16_t add_i_strmcnt = 0; 4214 4215 SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize); 4216 SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id); 4217 if (stcb == NULL) { 4218 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 4219 error = ENOENT; 4220 break; 4221 } 4222 if (stcb->asoc.peer_supports_strreset == 0) { 4223 /* 4224 * Peer does not support the chunk type. 4225 */ 4226 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 4227 error = EOPNOTSUPP; 4228 SCTP_TCB_UNLOCK(stcb); 4229 break; 4230 } 4231 if (stcb->asoc.stream_reset_outstanding) { 4232 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 4233 error = EALREADY; 4234 SCTP_TCB_UNLOCK(stcb); 4235 break; 4236 } 4237 if ((stradd->sas_outstrms == 0) && 4238 (stradd->sas_instrms == 0)) { 4239 error = EINVAL; 4240 goto skip_stuff; 4241 } 4242 if (stradd->sas_outstrms) { 4243 addstream = 1; 4244 /* We allocate here */ 4245 add_o_strmcnt = stradd->sas_outstrms; 4246 if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) { 4247 /* You can't have more than 64k */ 4248 error = EINVAL; 4249 goto skip_stuff; 4250 } 4251 } 4252 if (stradd->sas_instrms) { 4253 int cnt; 4254 4255 addstream |= 2; 4256 /* 4257 * We allocate inside 4258 * sctp_send_str_reset_req() 4259 */ 4260 add_i_strmcnt = stradd->sas_instrms; 4261 cnt = add_i_strmcnt; 4262 cnt += stcb->asoc.streamincnt; 4263 if (cnt > 0x0000ffff) { 4264 /* You can't have more than 64k */ 4265 error = EINVAL; 4266 goto skip_stuff; 4267 } 4268 if (cnt > (int)stcb->asoc.max_inbound_streams) { 4269 /* More than you are allowed */ 4270 error = EINVAL; 4271 goto skip_stuff; 4272 } 4273 } 4274 error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0); 4275 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED); 4276 skip_stuff: 4277 SCTP_TCB_UNLOCK(stcb); 4278 break; 4279 } 4280 case SCTP_RESET_ASSOC: 4281 { 4282 uint32_t *value; 4283 4284 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize); 4285 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value); 4286 if (stcb == NULL) { 4287 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 4288 error = ENOENT; 4289 break; 4290 } 4291 if (stcb->asoc.peer_supports_strreset == 0) { 4292 /* 4293 * Peer does not support the chunk type. 4294 */ 4295 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 4296 error = EOPNOTSUPP; 4297 SCTP_TCB_UNLOCK(stcb); 4298 break; 4299 } 4300 if (stcb->asoc.stream_reset_outstanding) { 4301 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 4302 error = EALREADY; 4303 SCTP_TCB_UNLOCK(stcb); 4304 break; 4305 } 4306 error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, 0, 0, 0, 0); 4307 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED); 4308 SCTP_TCB_UNLOCK(stcb); 4309 break; 4310 } 4311 case SCTP_CONNECT_X: 4312 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) { 4313 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4314 error = EINVAL; 4315 break; 4316 } 4317 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0); 4318 break; 4319 case SCTP_CONNECT_X_DELAYED: 4320 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) { 4321 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4322 error = EINVAL; 4323 break; 4324 } 4325 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1); 4326 break; 4327 case SCTP_CONNECT_X_COMPLETE: 4328 { 4329 struct sockaddr *sa; 4330 struct sctp_nets *net; 4331 4332 /* FIXME MT: check correct? */ 4333 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize); 4334 4335 /* find tcb */ 4336 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 4337 SCTP_INP_RLOCK(inp); 4338 stcb = LIST_FIRST(&inp->sctp_asoc_list); 4339 if (stcb) { 4340 SCTP_TCB_LOCK(stcb); 4341 net = sctp_findnet(stcb, sa); 4342 } 4343 SCTP_INP_RUNLOCK(inp); 4344 } else { 4345 /* 4346 * We increment here since 4347 * sctp_findassociation_ep_addr() wil do a 4348 * decrement if it finds the stcb as long as 4349 * the locked tcb (last argument) is NOT a 4350 * TCB.. aka NULL. 4351 */ 4352 SCTP_INP_INCR_REF(inp); 4353 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL); 4354 if (stcb == NULL) { 4355 SCTP_INP_DECR_REF(inp); 4356 } 4357 } 4358 4359 if (stcb == NULL) { 4360 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 4361 error = ENOENT; 4362 break; 4363 } 4364 if (stcb->asoc.delayed_connection == 1) { 4365 stcb->asoc.delayed_connection = 0; 4366 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 4367 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, 4368 stcb->asoc.primary_destination, 4369 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9); 4370 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 4371 } else { 4372 /* 4373 * already expired or did not use delayed 4374 * connectx 4375 */ 4376 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 4377 error = EALREADY; 4378 } 4379 SCTP_TCB_UNLOCK(stcb); 4380 break; 4381 } 4382 case SCTP_MAX_BURST: 4383 { 4384 struct sctp_assoc_value *av; 4385 4386 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 4387 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 4388 4389 if (stcb) { 4390 stcb->asoc.max_burst = av->assoc_value; 4391 SCTP_TCB_UNLOCK(stcb); 4392 } else { 4393 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4394 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4395 (av->assoc_id == SCTP_FUTURE_ASSOC) || 4396 (av->assoc_id == SCTP_ALL_ASSOC)) { 4397 SCTP_INP_WLOCK(inp); 4398 inp->sctp_ep.max_burst = av->assoc_value; 4399 SCTP_INP_WUNLOCK(inp); 4400 } 4401 if ((av->assoc_id == SCTP_CURRENT_ASSOC) || 4402 (av->assoc_id == SCTP_ALL_ASSOC)) { 4403 SCTP_INP_RLOCK(inp); 4404 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4405 SCTP_TCB_LOCK(stcb); 4406 stcb->asoc.max_burst = av->assoc_value; 4407 SCTP_TCB_UNLOCK(stcb); 4408 } 4409 SCTP_INP_RUNLOCK(inp); 4410 } 4411 } 4412 break; 4413 } 4414 case SCTP_MAXSEG: 4415 { 4416 struct sctp_assoc_value *av; 4417 int ovh; 4418 4419 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 4420 SCTP_FIND_STCB(inp, stcb, av->assoc_id); 4421 4422 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 4423 ovh = SCTP_MED_OVERHEAD; 4424 } else { 4425 ovh = SCTP_MED_V4_OVERHEAD; 4426 } 4427 if (stcb) { 4428 if (av->assoc_value) { 4429 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh); 4430 } else { 4431 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; 4432 } 4433 SCTP_TCB_UNLOCK(stcb); 4434 } else { 4435 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4436 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4437 (av->assoc_id == SCTP_FUTURE_ASSOC)) { 4438 SCTP_INP_WLOCK(inp); 4439 /* 4440 * FIXME MT: I think this is not in 4441 * tune with the API ID 4442 */ 4443 if (av->assoc_value) { 4444 inp->sctp_frag_point = (av->assoc_value + ovh); 4445 } else { 4446 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; 4447 } 4448 SCTP_INP_WUNLOCK(inp); 4449 } else { 4450 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4451 error = EINVAL; 4452 } 4453 } 4454 break; 4455 } 4456 case SCTP_EVENTS: 4457 { 4458 struct sctp_event_subscribe *events; 4459 4460 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize); 4461 4462 SCTP_INP_WLOCK(inp); 4463 if (events->sctp_data_io_event) { 4464 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT); 4465 } else { 4466 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT); 4467 } 4468 4469 if (events->sctp_association_event) { 4470 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT); 4471 } else { 4472 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT); 4473 } 4474 4475 if (events->sctp_address_event) { 4476 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT); 4477 } else { 4478 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT); 4479 } 4480 4481 if (events->sctp_send_failure_event) { 4482 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT); 4483 } else { 4484 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT); 4485 } 4486 4487 if (events->sctp_peer_error_event) { 4488 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR); 4489 } else { 4490 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR); 4491 } 4492 4493 if (events->sctp_shutdown_event) { 4494 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT); 4495 } else { 4496 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT); 4497 } 4498 4499 if (events->sctp_partial_delivery_event) { 4500 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT); 4501 } else { 4502 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT); 4503 } 4504 4505 if (events->sctp_adaptation_layer_event) { 4506 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT); 4507 } else { 4508 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT); 4509 } 4510 4511 if (events->sctp_authentication_event) { 4512 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT); 4513 } else { 4514 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT); 4515 } 4516 4517 if (events->sctp_sender_dry_event) { 4518 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT); 4519 } else { 4520 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT); 4521 } 4522 4523 if (events->sctp_stream_reset_event) { 4524 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT); 4525 } else { 4526 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT); 4527 } 4528 SCTP_INP_WUNLOCK(inp); 4529 4530 SCTP_INP_RLOCK(inp); 4531 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4532 SCTP_TCB_LOCK(stcb); 4533 if (events->sctp_association_event) { 4534 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT); 4535 } else { 4536 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT); 4537 } 4538 if (events->sctp_address_event) { 4539 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT); 4540 } else { 4541 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT); 4542 } 4543 if (events->sctp_send_failure_event) { 4544 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT); 4545 } else { 4546 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT); 4547 } 4548 if (events->sctp_peer_error_event) { 4549 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR); 4550 } else { 4551 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR); 4552 } 4553 if (events->sctp_shutdown_event) { 4554 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT); 4555 } else { 4556 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT); 4557 } 4558 if (events->sctp_partial_delivery_event) { 4559 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT); 4560 } else { 4561 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT); 4562 } 4563 if (events->sctp_adaptation_layer_event) { 4564 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT); 4565 } else { 4566 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT); 4567 } 4568 if (events->sctp_authentication_event) { 4569 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT); 4570 } else { 4571 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT); 4572 } 4573 if (events->sctp_sender_dry_event) { 4574 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT); 4575 } else { 4576 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT); 4577 } 4578 if (events->sctp_stream_reset_event) { 4579 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT); 4580 } else { 4581 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT); 4582 } 4583 SCTP_TCB_UNLOCK(stcb); 4584 } 4585 /* 4586 * Send up the sender dry event only for 1-to-1 4587 * style sockets. 4588 */ 4589 if (events->sctp_sender_dry_event) { 4590 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4591 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 4592 stcb = LIST_FIRST(&inp->sctp_asoc_list); 4593 if (stcb) { 4594 SCTP_TCB_LOCK(stcb); 4595 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 4596 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 4597 (stcb->asoc.stream_queue_cnt == 0)) { 4598 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED); 4599 } 4600 SCTP_TCB_UNLOCK(stcb); 4601 } 4602 } 4603 } 4604 SCTP_INP_RUNLOCK(inp); 4605 break; 4606 } 4607 case SCTP_ADAPTATION_LAYER: 4608 { 4609 struct sctp_setadaptation *adap_bits; 4610 4611 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize); 4612 SCTP_INP_WLOCK(inp); 4613 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind; 4614 SCTP_INP_WUNLOCK(inp); 4615 break; 4616 } 4617 #ifdef SCTP_DEBUG 4618 case SCTP_SET_INITIAL_DBG_SEQ: 4619 { 4620 uint32_t *vvv; 4621 4622 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize); 4623 SCTP_INP_WLOCK(inp); 4624 inp->sctp_ep.initial_sequence_debug = *vvv; 4625 SCTP_INP_WUNLOCK(inp); 4626 break; 4627 } 4628 #endif 4629 case SCTP_DEFAULT_SEND_PARAM: 4630 { 4631 struct sctp_sndrcvinfo *s_info; 4632 4633 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize); 4634 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id); 4635 4636 if (stcb) { 4637 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) { 4638 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send))); 4639 } else { 4640 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4641 error = EINVAL; 4642 } 4643 SCTP_TCB_UNLOCK(stcb); 4644 } else { 4645 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4646 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4647 (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) || 4648 (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) { 4649 SCTP_INP_WLOCK(inp); 4650 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send))); 4651 SCTP_INP_WUNLOCK(inp); 4652 } 4653 if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) || 4654 (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) { 4655 SCTP_INP_RLOCK(inp); 4656 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 4657 SCTP_TCB_LOCK(stcb); 4658 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) { 4659 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send))); 4660 } 4661 SCTP_TCB_UNLOCK(stcb); 4662 } 4663 SCTP_INP_RUNLOCK(inp); 4664 } 4665 } 4666 break; 4667 } 4668 case SCTP_PEER_ADDR_PARAMS: 4669 { 4670 struct sctp_paddrparams *paddrp; 4671 struct sctp_nets *net; 4672 4673 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize); 4674 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id); 4675 net = NULL; 4676 if (stcb) { 4677 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 4678 } else { 4679 /* 4680 * We increment here since 4681 * sctp_findassociation_ep_addr() wil do a 4682 * decrement if it finds the stcb as long as 4683 * the locked tcb (last argument) is NOT a 4684 * TCB.. aka NULL. 4685 */ 4686 SCTP_INP_INCR_REF(inp); 4687 stcb = sctp_findassociation_ep_addr(&inp, 4688 (struct sockaddr *)&paddrp->spp_address, 4689 &net, NULL, NULL); 4690 if (stcb == NULL) { 4691 SCTP_INP_DECR_REF(inp); 4692 } 4693 } 4694 if (stcb && (net == NULL)) { 4695 struct sockaddr *sa; 4696 4697 sa = (struct sockaddr *)&paddrp->spp_address; 4698 #ifdef INET 4699 if (sa->sa_family == AF_INET) { 4700 4701 struct sockaddr_in *sin; 4702 4703 sin = (struct sockaddr_in *)sa; 4704 if (sin->sin_addr.s_addr) { 4705 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4706 SCTP_TCB_UNLOCK(stcb); 4707 error = EINVAL; 4708 break; 4709 } 4710 } else 4711 #endif 4712 #ifdef INET6 4713 if (sa->sa_family == AF_INET6) { 4714 struct sockaddr_in6 *sin6; 4715 4716 sin6 = (struct sockaddr_in6 *)sa; 4717 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 4718 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4719 SCTP_TCB_UNLOCK(stcb); 4720 error = EINVAL; 4721 break; 4722 } 4723 } else 4724 #endif 4725 { 4726 error = EAFNOSUPPORT; 4727 SCTP_TCB_UNLOCK(stcb); 4728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 4729 break; 4730 } 4731 } 4732 /* sanity checks */ 4733 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) { 4734 if (stcb) 4735 SCTP_TCB_UNLOCK(stcb); 4736 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4737 return (EINVAL); 4738 } 4739 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) { 4740 if (stcb) 4741 SCTP_TCB_UNLOCK(stcb); 4742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 4743 return (EINVAL); 4744 } 4745 if (stcb) { 4746 /************************TCB SPECIFIC SET ******************/ 4747 /* 4748 * do we change the timer for HB, we run 4749 * only one? 4750 */ 4751 int ovh = 0; 4752 4753 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 4754 ovh = SCTP_MED_OVERHEAD; 4755 } else { 4756 ovh = SCTP_MED_V4_OVERHEAD; 4757 } 4758 4759 /* network sets ? */ 4760 if (net) { 4761 /************************NET SPECIFIC SET ******************/ 4762 if (paddrp->spp_flags & SPP_HB_DISABLE) { 4763 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) && 4764 !(net->dest_state & SCTP_ADDR_NOHB)) { 4765 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 4766 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 4767 } 4768 net->dest_state |= SCTP_ADDR_NOHB; 4769 } 4770 if (paddrp->spp_flags & SPP_HB_ENABLE) { 4771 if (paddrp->spp_hbinterval) { 4772 net->heart_beat_delay = paddrp->spp_hbinterval; 4773 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) { 4774 net->heart_beat_delay = 0; 4775 } 4776 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 4777 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 4778 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 4779 net->dest_state &= ~SCTP_ADDR_NOHB; 4780 } 4781 if (paddrp->spp_flags & SPP_HB_DEMAND) { 4782 /* on demand HB */ 4783 sctp_send_hb(stcb, net, SCTP_SO_LOCKED); 4784 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED); 4785 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 4786 } 4787 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) { 4788 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 4789 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 4790 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 4791 } 4792 net->dest_state |= SCTP_ADDR_NO_PMTUD; 4793 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) { 4794 net->mtu = paddrp->spp_pathmtu + ovh; 4795 if (net->mtu < stcb->asoc.smallest_mtu) { 4796 sctp_pathmtu_adjustment(stcb, net->mtu); 4797 } 4798 } 4799 } 4800 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) { 4801 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 4802 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 4803 } 4804 net->dest_state &= ~SCTP_ADDR_NO_PMTUD; 4805 } 4806 if (paddrp->spp_pathmaxrxt) { 4807 if (net->dest_state & SCTP_ADDR_PF) { 4808 if (net->error_count > paddrp->spp_pathmaxrxt) { 4809 net->dest_state &= ~SCTP_ADDR_PF; 4810 } 4811 } else { 4812 if ((net->error_count <= paddrp->spp_pathmaxrxt) && 4813 (net->error_count > net->pf_threshold)) { 4814 net->dest_state |= SCTP_ADDR_PF; 4815 sctp_send_hb(stcb, net, SCTP_SO_LOCKED); 4816 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3); 4817 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 4818 } 4819 } 4820 if (net->dest_state & SCTP_ADDR_REACHABLE) { 4821 if (net->error_count > paddrp->spp_pathmaxrxt) { 4822 net->dest_state &= ~SCTP_ADDR_REACHABLE; 4823 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED); 4824 } 4825 } else { 4826 if (net->error_count <= paddrp->spp_pathmaxrxt) { 4827 net->dest_state |= SCTP_ADDR_REACHABLE; 4828 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED); 4829 } 4830 } 4831 net->failure_threshold = paddrp->spp_pathmaxrxt; 4832 } 4833 if (paddrp->spp_flags & SPP_DSCP) { 4834 net->dscp = paddrp->spp_dscp & 0xfc; 4835 net->dscp |= 0x01; 4836 } 4837 #ifdef INET6 4838 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) { 4839 if (net->ro._l_addr.sa.sa_family == AF_INET6) { 4840 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff; 4841 net->flowlabel |= 0x80000000; 4842 } 4843 } 4844 #endif 4845 } else { 4846 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/ 4847 if (paddrp->spp_pathmaxrxt) { 4848 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt; 4849 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4850 if (net->dest_state & SCTP_ADDR_PF) { 4851 if (net->error_count > paddrp->spp_pathmaxrxt) { 4852 net->dest_state &= ~SCTP_ADDR_PF; 4853 } 4854 } else { 4855 if ((net->error_count <= paddrp->spp_pathmaxrxt) && 4856 (net->error_count > net->pf_threshold)) { 4857 net->dest_state |= SCTP_ADDR_PF; 4858 sctp_send_hb(stcb, net, SCTP_SO_LOCKED); 4859 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3); 4860 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 4861 } 4862 } 4863 if (net->dest_state & SCTP_ADDR_REACHABLE) { 4864 if (net->error_count > paddrp->spp_pathmaxrxt) { 4865 net->dest_state &= ~SCTP_ADDR_REACHABLE; 4866 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED); 4867 } 4868 } else { 4869 if (net->error_count <= paddrp->spp_pathmaxrxt) { 4870 net->dest_state |= SCTP_ADDR_REACHABLE; 4871 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED); 4872 } 4873 } 4874 net->failure_threshold = paddrp->spp_pathmaxrxt; 4875 } 4876 } 4877 if (paddrp->spp_flags & SPP_HB_ENABLE) { 4878 if (paddrp->spp_hbinterval) { 4879 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval; 4880 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) { 4881 stcb->asoc.heart_beat_delay = 0; 4882 } 4883 /* Turn back on the timer */ 4884 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4885 if (paddrp->spp_hbinterval) { 4886 net->heart_beat_delay = paddrp->spp_hbinterval; 4887 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) { 4888 net->heart_beat_delay = 0; 4889 } 4890 if (net->dest_state & SCTP_ADDR_NOHB) { 4891 net->dest_state &= ~SCTP_ADDR_NOHB; 4892 } 4893 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 4894 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 4895 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 4896 } 4897 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT); 4898 } 4899 if (paddrp->spp_flags & SPP_HB_DISABLE) { 4900 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4901 if (!(net->dest_state & SCTP_ADDR_NOHB)) { 4902 net->dest_state |= SCTP_ADDR_NOHB; 4903 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) { 4904 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 4905 } 4906 } 4907 } 4908 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT); 4909 } 4910 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) { 4911 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4912 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 4913 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 4914 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 4915 } 4916 net->dest_state |= SCTP_ADDR_NO_PMTUD; 4917 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) { 4918 net->mtu = paddrp->spp_pathmtu + ovh; 4919 if (net->mtu < stcb->asoc.smallest_mtu) { 4920 sctp_pathmtu_adjustment(stcb, net->mtu); 4921 } 4922 } 4923 } 4924 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD); 4925 } 4926 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) { 4927 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4928 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 4929 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 4930 } 4931 net->dest_state &= ~SCTP_ADDR_NO_PMTUD; 4932 } 4933 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD); 4934 } 4935 if (paddrp->spp_flags & SPP_DSCP) { 4936 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4937 net->dscp = paddrp->spp_dscp & 0xfc; 4938 net->dscp |= 0x01; 4939 } 4940 stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc; 4941 stcb->asoc.default_dscp |= 0x01; 4942 } 4943 #ifdef INET6 4944 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) { 4945 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4946 if (net->ro._l_addr.sa.sa_family == AF_INET6) { 4947 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff; 4948 net->flowlabel |= 0x80000000; 4949 } 4950 } 4951 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff; 4952 stcb->asoc.default_flowlabel |= 0x80000000; 4953 } 4954 #endif 4955 } 4956 SCTP_TCB_UNLOCK(stcb); 4957 } else { 4958 /************************NO TCB, SET TO default stuff ******************/ 4959 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 4960 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 4961 (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) { 4962 SCTP_INP_WLOCK(inp); 4963 /* 4964 * For the TOS/FLOWLABEL stuff you 4965 * set it with the options on the 4966 * socket 4967 */ 4968 if (paddrp->spp_pathmaxrxt) { 4969 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt; 4970 } 4971 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) 4972 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0; 4973 else if (paddrp->spp_hbinterval) { 4974 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL) 4975 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL; 4976 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval); 4977 } 4978 if (paddrp->spp_flags & SPP_HB_ENABLE) { 4979 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) { 4980 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0; 4981 } else if (paddrp->spp_hbinterval) { 4982 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval); 4983 } 4984 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT); 4985 } else if (paddrp->spp_flags & SPP_HB_DISABLE) { 4986 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT); 4987 } 4988 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) { 4989 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD); 4990 } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) { 4991 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD); 4992 } 4993 if (paddrp->spp_flags & SPP_DSCP) { 4994 inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc; 4995 inp->sctp_ep.default_dscp |= 0x01; 4996 } 4997 #ifdef INET6 4998 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) { 4999 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 5000 inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff; 5001 inp->sctp_ep.default_flowlabel |= 0x80000000; 5002 } 5003 } 5004 #endif 5005 SCTP_INP_WUNLOCK(inp); 5006 } else { 5007 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5008 error = EINVAL; 5009 } 5010 } 5011 break; 5012 } 5013 case SCTP_RTOINFO: 5014 { 5015 struct sctp_rtoinfo *srto; 5016 uint32_t new_init, new_min, new_max; 5017 5018 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize); 5019 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id); 5020 5021 if (stcb) { 5022 if (srto->srto_initial) 5023 new_init = srto->srto_initial; 5024 else 5025 new_init = stcb->asoc.initial_rto; 5026 if (srto->srto_max) 5027 new_max = srto->srto_max; 5028 else 5029 new_max = stcb->asoc.maxrto; 5030 if (srto->srto_min) 5031 new_min = srto->srto_min; 5032 else 5033 new_min = stcb->asoc.minrto; 5034 if ((new_min <= new_init) && (new_init <= new_max)) { 5035 stcb->asoc.initial_rto = new_init; 5036 stcb->asoc.maxrto = new_max; 5037 stcb->asoc.minrto = new_min; 5038 } else { 5039 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5040 error = EINVAL; 5041 } 5042 SCTP_TCB_UNLOCK(stcb); 5043 } else { 5044 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5045 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5046 (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) { 5047 SCTP_INP_WLOCK(inp); 5048 if (srto->srto_initial) 5049 new_init = srto->srto_initial; 5050 else 5051 new_init = inp->sctp_ep.initial_rto; 5052 if (srto->srto_max) 5053 new_max = srto->srto_max; 5054 else 5055 new_max = inp->sctp_ep.sctp_maxrto; 5056 if (srto->srto_min) 5057 new_min = srto->srto_min; 5058 else 5059 new_min = inp->sctp_ep.sctp_minrto; 5060 if ((new_min <= new_init) && (new_init <= new_max)) { 5061 inp->sctp_ep.initial_rto = new_init; 5062 inp->sctp_ep.sctp_maxrto = new_max; 5063 inp->sctp_ep.sctp_minrto = new_min; 5064 } else { 5065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5066 error = EINVAL; 5067 } 5068 SCTP_INP_WUNLOCK(inp); 5069 } else { 5070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5071 error = EINVAL; 5072 } 5073 } 5074 break; 5075 } 5076 case SCTP_ASSOCINFO: 5077 { 5078 struct sctp_assocparams *sasoc; 5079 5080 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize); 5081 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id); 5082 if (sasoc->sasoc_cookie_life) { 5083 /* boundary check the cookie life */ 5084 if (sasoc->sasoc_cookie_life < 1000) 5085 sasoc->sasoc_cookie_life = 1000; 5086 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) { 5087 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE; 5088 } 5089 } 5090 if (stcb) { 5091 if (sasoc->sasoc_asocmaxrxt) 5092 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt; 5093 if (sasoc->sasoc_cookie_life) { 5094 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life); 5095 } 5096 SCTP_TCB_UNLOCK(stcb); 5097 } else { 5098 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5099 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5100 (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) { 5101 SCTP_INP_WLOCK(inp); 5102 if (sasoc->sasoc_asocmaxrxt) 5103 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt; 5104 if (sasoc->sasoc_cookie_life) { 5105 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life); 5106 } 5107 SCTP_INP_WUNLOCK(inp); 5108 } else { 5109 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5110 error = EINVAL; 5111 } 5112 } 5113 break; 5114 } 5115 case SCTP_INITMSG: 5116 { 5117 struct sctp_initmsg *sinit; 5118 5119 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize); 5120 SCTP_INP_WLOCK(inp); 5121 if (sinit->sinit_num_ostreams) 5122 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams; 5123 5124 if (sinit->sinit_max_instreams) 5125 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams; 5126 5127 if (sinit->sinit_max_attempts) 5128 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts; 5129 5130 if (sinit->sinit_max_init_timeo) 5131 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo; 5132 SCTP_INP_WUNLOCK(inp); 5133 break; 5134 } 5135 case SCTP_PRIMARY_ADDR: 5136 { 5137 struct sctp_setprim *spa; 5138 struct sctp_nets *net; 5139 5140 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize); 5141 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id); 5142 5143 net = NULL; 5144 if (stcb) { 5145 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr); 5146 } else { 5147 /* 5148 * We increment here since 5149 * sctp_findassociation_ep_addr() wil do a 5150 * decrement if it finds the stcb as long as 5151 * the locked tcb (last argument) is NOT a 5152 * TCB.. aka NULL. 5153 */ 5154 SCTP_INP_INCR_REF(inp); 5155 stcb = sctp_findassociation_ep_addr(&inp, 5156 (struct sockaddr *)&spa->ssp_addr, 5157 &net, NULL, NULL); 5158 if (stcb == NULL) { 5159 SCTP_INP_DECR_REF(inp); 5160 } 5161 } 5162 5163 if ((stcb) && (net)) { 5164 if ((net != stcb->asoc.primary_destination) && 5165 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) { 5166 /* Ok we need to set it */ 5167 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) { 5168 if ((stcb->asoc.alternate) && 5169 (!(net->dest_state & SCTP_ADDR_PF)) && 5170 (net->dest_state & SCTP_ADDR_REACHABLE)) { 5171 sctp_free_remote_addr(stcb->asoc.alternate); 5172 stcb->asoc.alternate = NULL; 5173 } 5174 } 5175 } 5176 } else { 5177 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5178 error = EINVAL; 5179 } 5180 if (stcb) { 5181 SCTP_TCB_UNLOCK(stcb); 5182 } 5183 break; 5184 } 5185 case SCTP_SET_DYNAMIC_PRIMARY: 5186 { 5187 union sctp_sockstore *ss; 5188 5189 error = priv_check(curthread, 5190 PRIV_NETINET_RESERVEDPORT); 5191 if (error) 5192 break; 5193 5194 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize); 5195 /* SUPER USER CHECK? */ 5196 error = sctp_dynamic_set_primary(&ss->sa, vrf_id); 5197 break; 5198 } 5199 case SCTP_SET_PEER_PRIMARY_ADDR: 5200 { 5201 struct sctp_setpeerprim *sspp; 5202 5203 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize); 5204 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id); 5205 if (stcb != NULL) { 5206 struct sctp_ifa *ifa; 5207 5208 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr, 5209 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED); 5210 if (ifa == NULL) { 5211 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5212 error = EINVAL; 5213 goto out_of_it; 5214 } 5215 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 5216 /* 5217 * Must validate the ifa found is in 5218 * our ep 5219 */ 5220 struct sctp_laddr *laddr; 5221 int found = 0; 5222 5223 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 5224 if (laddr->ifa == NULL) { 5225 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", 5226 __FUNCTION__); 5227 continue; 5228 } 5229 if (laddr->ifa == ifa) { 5230 found = 1; 5231 break; 5232 } 5233 } 5234 if (!found) { 5235 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5236 error = EINVAL; 5237 goto out_of_it; 5238 } 5239 } 5240 if (sctp_set_primary_ip_address_sa(stcb, 5241 (struct sockaddr *)&sspp->sspp_addr) != 0) { 5242 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5243 error = EINVAL; 5244 } 5245 out_of_it: 5246 SCTP_TCB_UNLOCK(stcb); 5247 } else { 5248 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5249 error = EINVAL; 5250 } 5251 break; 5252 } 5253 case SCTP_BINDX_ADD_ADDR: 5254 { 5255 struct sctp_getaddresses *addrs; 5256 struct thread *td; 5257 5258 td = (struct thread *)p; 5259 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, 5260 optsize); 5261 #ifdef INET 5262 if (addrs->addr->sa_family == AF_INET) { 5263 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) { 5264 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5265 error = EINVAL; 5266 break; 5267 } 5268 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) { 5269 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error); 5270 break; 5271 } 5272 } else 5273 #endif 5274 #ifdef INET6 5275 if (addrs->addr->sa_family == AF_INET6) { 5276 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) { 5277 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5278 error = EINVAL; 5279 break; 5280 } 5281 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr), 5282 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { 5283 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error); 5284 break; 5285 } 5286 } else 5287 #endif 5288 { 5289 error = EAFNOSUPPORT; 5290 break; 5291 } 5292 sctp_bindx_add_address(so, inp, addrs->addr, 5293 addrs->sget_assoc_id, vrf_id, 5294 &error, p); 5295 break; 5296 } 5297 case SCTP_BINDX_REM_ADDR: 5298 { 5299 struct sctp_getaddresses *addrs; 5300 struct thread *td; 5301 5302 td = (struct thread *)p; 5303 5304 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize); 5305 #ifdef INET 5306 if (addrs->addr->sa_family == AF_INET) { 5307 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) { 5308 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5309 error = EINVAL; 5310 break; 5311 } 5312 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) { 5313 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error); 5314 break; 5315 } 5316 } else 5317 #endif 5318 #ifdef INET6 5319 if (addrs->addr->sa_family == AF_INET6) { 5320 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) { 5321 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5322 error = EINVAL; 5323 break; 5324 } 5325 if (td != NULL && 5326 (error = prison_local_ip6(td->td_ucred, 5327 &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr), 5328 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { 5329 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error); 5330 break; 5331 } 5332 } else 5333 #endif 5334 { 5335 error = EAFNOSUPPORT; 5336 break; 5337 } 5338 sctp_bindx_delete_address(inp, addrs->addr, 5339 addrs->sget_assoc_id, vrf_id, 5340 &error); 5341 break; 5342 } 5343 case SCTP_EVENT: 5344 { 5345 struct sctp_event *event; 5346 uint32_t event_type; 5347 5348 SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize); 5349 SCTP_FIND_STCB(inp, stcb, event->se_assoc_id); 5350 switch (event->se_type) { 5351 case SCTP_ASSOC_CHANGE: 5352 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT; 5353 break; 5354 case SCTP_PEER_ADDR_CHANGE: 5355 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT; 5356 break; 5357 case SCTP_REMOTE_ERROR: 5358 event_type = SCTP_PCB_FLAGS_RECVPEERERR; 5359 break; 5360 case SCTP_SEND_FAILED: 5361 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT; 5362 break; 5363 case SCTP_SHUTDOWN_EVENT: 5364 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT; 5365 break; 5366 case SCTP_ADAPTATION_INDICATION: 5367 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT; 5368 break; 5369 case SCTP_PARTIAL_DELIVERY_EVENT: 5370 event_type = SCTP_PCB_FLAGS_PDAPIEVNT; 5371 break; 5372 case SCTP_AUTHENTICATION_EVENT: 5373 event_type = SCTP_PCB_FLAGS_AUTHEVNT; 5374 break; 5375 case SCTP_STREAM_RESET_EVENT: 5376 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT; 5377 break; 5378 case SCTP_SENDER_DRY_EVENT: 5379 event_type = SCTP_PCB_FLAGS_DRYEVNT; 5380 break; 5381 case SCTP_NOTIFICATIONS_STOPPED_EVENT: 5382 event_type = 0; 5383 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP); 5384 error = ENOTSUP; 5385 break; 5386 case SCTP_ASSOC_RESET_EVENT: 5387 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT; 5388 break; 5389 case SCTP_STREAM_CHANGE_EVENT: 5390 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT; 5391 break; 5392 case SCTP_SEND_FAILED_EVENT: 5393 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT; 5394 break; 5395 default: 5396 event_type = 0; 5397 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5398 error = EINVAL; 5399 break; 5400 } 5401 if (event_type > 0) { 5402 if (stcb) { 5403 if (event->se_on) { 5404 sctp_stcb_feature_on(inp, stcb, event_type); 5405 if (event_type == SCTP_PCB_FLAGS_DRYEVNT) { 5406 if (TAILQ_EMPTY(&stcb->asoc.send_queue) && 5407 TAILQ_EMPTY(&stcb->asoc.sent_queue) && 5408 (stcb->asoc.stream_queue_cnt == 0)) { 5409 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED); 5410 } 5411 } 5412 } else { 5413 sctp_stcb_feature_off(inp, stcb, event_type); 5414 } 5415 SCTP_TCB_UNLOCK(stcb); 5416 } else { 5417 /* 5418 * We don't want to send up a storm 5419 * of events, so return an error for 5420 * sender dry events 5421 */ 5422 if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) && 5423 ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) && 5424 ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) && 5425 ((event->se_assoc_id == SCTP_ALL_ASSOC) || 5426 (event->se_assoc_id == SCTP_CURRENT_ASSOC))) { 5427 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP); 5428 error = ENOTSUP; 5429 break; 5430 } 5431 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5432 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5433 (event->se_assoc_id == SCTP_FUTURE_ASSOC) || 5434 (event->se_assoc_id == SCTP_ALL_ASSOC)) { 5435 SCTP_INP_WLOCK(inp); 5436 if (event->se_on) { 5437 sctp_feature_on(inp, event_type); 5438 } else { 5439 sctp_feature_off(inp, event_type); 5440 } 5441 SCTP_INP_WUNLOCK(inp); 5442 } 5443 if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) || 5444 (event->se_assoc_id == SCTP_ALL_ASSOC)) { 5445 SCTP_INP_RLOCK(inp); 5446 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5447 SCTP_TCB_LOCK(stcb); 5448 if (event->se_on) { 5449 sctp_stcb_feature_on(inp, stcb, event_type); 5450 } else { 5451 sctp_stcb_feature_off(inp, stcb, event_type); 5452 } 5453 SCTP_TCB_UNLOCK(stcb); 5454 } 5455 SCTP_INP_RUNLOCK(inp); 5456 } 5457 } 5458 } 5459 break; 5460 } 5461 case SCTP_RECVRCVINFO: 5462 { 5463 int *onoff; 5464 5465 SCTP_CHECK_AND_CAST(onoff, optval, int, optsize); 5466 SCTP_INP_WLOCK(inp); 5467 if (*onoff != 0) { 5468 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO); 5469 } else { 5470 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO); 5471 } 5472 SCTP_INP_WUNLOCK(inp); 5473 break; 5474 } 5475 case SCTP_RECVNXTINFO: 5476 { 5477 int *onoff; 5478 5479 SCTP_CHECK_AND_CAST(onoff, optval, int, optsize); 5480 SCTP_INP_WLOCK(inp); 5481 if (*onoff != 0) { 5482 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO); 5483 } else { 5484 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO); 5485 } 5486 SCTP_INP_WUNLOCK(inp); 5487 break; 5488 } 5489 case SCTP_DEFAULT_SNDINFO: 5490 { 5491 struct sctp_sndinfo *info; 5492 uint16_t policy; 5493 5494 SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize); 5495 SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id); 5496 5497 if (stcb) { 5498 if (info->snd_sid < stcb->asoc.streamoutcnt) { 5499 stcb->asoc.def_send.sinfo_stream = info->snd_sid; 5500 policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags); 5501 stcb->asoc.def_send.sinfo_flags = info->snd_flags; 5502 stcb->asoc.def_send.sinfo_flags |= policy; 5503 stcb->asoc.def_send.sinfo_ppid = info->snd_ppid; 5504 stcb->asoc.def_send.sinfo_context = info->snd_context; 5505 } else { 5506 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5507 error = EINVAL; 5508 } 5509 SCTP_TCB_UNLOCK(stcb); 5510 } else { 5511 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5512 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5513 (info->snd_assoc_id == SCTP_FUTURE_ASSOC) || 5514 (info->snd_assoc_id == SCTP_ALL_ASSOC)) { 5515 SCTP_INP_WLOCK(inp); 5516 inp->def_send.sinfo_stream = info->snd_sid; 5517 policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags); 5518 inp->def_send.sinfo_flags = info->snd_flags; 5519 inp->def_send.sinfo_flags |= policy; 5520 inp->def_send.sinfo_ppid = info->snd_ppid; 5521 inp->def_send.sinfo_context = info->snd_context; 5522 SCTP_INP_WUNLOCK(inp); 5523 } 5524 if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) || 5525 (info->snd_assoc_id == SCTP_ALL_ASSOC)) { 5526 SCTP_INP_RLOCK(inp); 5527 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5528 SCTP_TCB_LOCK(stcb); 5529 if (info->snd_sid < stcb->asoc.streamoutcnt) { 5530 stcb->asoc.def_send.sinfo_stream = info->snd_sid; 5531 policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags); 5532 stcb->asoc.def_send.sinfo_flags = info->snd_flags; 5533 stcb->asoc.def_send.sinfo_flags |= policy; 5534 stcb->asoc.def_send.sinfo_ppid = info->snd_ppid; 5535 stcb->asoc.def_send.sinfo_context = info->snd_context; 5536 } 5537 SCTP_TCB_UNLOCK(stcb); 5538 } 5539 SCTP_INP_RUNLOCK(inp); 5540 } 5541 } 5542 break; 5543 } 5544 case SCTP_DEFAULT_PRINFO: 5545 { 5546 struct sctp_default_prinfo *info; 5547 5548 SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize); 5549 SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id); 5550 5551 if (PR_SCTP_INVALID_POLICY(info->pr_policy)) { 5552 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5553 error = EINVAL; 5554 break; 5555 } 5556 if (stcb) { 5557 stcb->asoc.def_send.sinfo_flags &= 0xfff0; 5558 stcb->asoc.def_send.sinfo_flags |= info->pr_policy; 5559 stcb->asoc.def_send.sinfo_timetolive = info->pr_value; 5560 SCTP_TCB_UNLOCK(stcb); 5561 } else { 5562 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5563 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5564 (info->pr_assoc_id == SCTP_FUTURE_ASSOC) || 5565 (info->pr_assoc_id == SCTP_ALL_ASSOC)) { 5566 SCTP_INP_WLOCK(inp); 5567 inp->def_send.sinfo_flags &= 0xfff0; 5568 inp->def_send.sinfo_flags |= info->pr_policy; 5569 inp->def_send.sinfo_timetolive = info->pr_value; 5570 SCTP_INP_WUNLOCK(inp); 5571 } 5572 if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) || 5573 (info->pr_assoc_id == SCTP_ALL_ASSOC)) { 5574 SCTP_INP_RLOCK(inp); 5575 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 5576 SCTP_TCB_LOCK(stcb); 5577 stcb->asoc.def_send.sinfo_flags &= 0xfff0; 5578 stcb->asoc.def_send.sinfo_flags |= info->pr_policy; 5579 stcb->asoc.def_send.sinfo_timetolive = info->pr_value; 5580 SCTP_TCB_UNLOCK(stcb); 5581 } 5582 SCTP_INP_RUNLOCK(inp); 5583 } 5584 } 5585 break; 5586 } 5587 case SCTP_PEER_ADDR_THLDS: 5588 /* Applies to the specific association */ 5589 { 5590 struct sctp_paddrthlds *thlds; 5591 struct sctp_nets *net; 5592 5593 SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize); 5594 SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id); 5595 net = NULL; 5596 if (stcb) { 5597 net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_assoc_id); 5598 } else { 5599 /* 5600 * We increment here since 5601 * sctp_findassociation_ep_addr() wil do a 5602 * decrement if it finds the stcb as long as 5603 * the locked tcb (last argument) is NOT a 5604 * TCB.. aka NULL. 5605 */ 5606 SCTP_INP_INCR_REF(inp); 5607 stcb = sctp_findassociation_ep_addr(&inp, 5608 (struct sockaddr *)&thlds->spt_assoc_id, 5609 &net, NULL, NULL); 5610 if (stcb == NULL) { 5611 SCTP_INP_DECR_REF(inp); 5612 } 5613 } 5614 if (stcb && (net == NULL)) { 5615 struct sockaddr *sa; 5616 5617 sa = (struct sockaddr *)&thlds->spt_assoc_id; 5618 #ifdef INET 5619 if (sa->sa_family == AF_INET) { 5620 5621 struct sockaddr_in *sin; 5622 5623 sin = (struct sockaddr_in *)sa; 5624 if (sin->sin_addr.s_addr) { 5625 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5626 SCTP_TCB_UNLOCK(stcb); 5627 error = EINVAL; 5628 break; 5629 } 5630 } else 5631 #endif 5632 #ifdef INET6 5633 if (sa->sa_family == AF_INET6) { 5634 struct sockaddr_in6 *sin6; 5635 5636 sin6 = (struct sockaddr_in6 *)sa; 5637 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 5638 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5639 SCTP_TCB_UNLOCK(stcb); 5640 error = EINVAL; 5641 break; 5642 } 5643 } else 5644 #endif 5645 { 5646 error = EAFNOSUPPORT; 5647 SCTP_TCB_UNLOCK(stcb); 5648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 5649 break; 5650 } 5651 } 5652 if (stcb) { 5653 if (net) { 5654 if (net->dest_state & SCTP_ADDR_PF) { 5655 if ((net->failure_threshold > thlds->spt_pathmaxrxt) || 5656 (net->failure_threshold <= thlds->spt_pathpfthld)) { 5657 net->dest_state &= ~SCTP_ADDR_PF; 5658 } 5659 } else { 5660 if ((net->failure_threshold > thlds->spt_pathpfthld) && 5661 (net->failure_threshold <= thlds->spt_pathmaxrxt)) { 5662 net->dest_state |= SCTP_ADDR_PF; 5663 sctp_send_hb(stcb, net, SCTP_SO_LOCKED); 5664 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3); 5665 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 5666 } 5667 } 5668 if (net->dest_state & SCTP_ADDR_REACHABLE) { 5669 if (net->failure_threshold > thlds->spt_pathmaxrxt) { 5670 net->dest_state &= ~SCTP_ADDR_REACHABLE; 5671 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED); 5672 } 5673 } else { 5674 if (net->failure_threshold <= thlds->spt_pathmaxrxt) { 5675 net->dest_state |= SCTP_ADDR_REACHABLE; 5676 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED); 5677 } 5678 } 5679 net->failure_threshold = thlds->spt_pathmaxrxt; 5680 net->pf_threshold = thlds->spt_pathpfthld; 5681 } else { 5682 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 5683 if (net->dest_state & SCTP_ADDR_PF) { 5684 if ((net->failure_threshold > thlds->spt_pathmaxrxt) || 5685 (net->failure_threshold <= thlds->spt_pathpfthld)) { 5686 net->dest_state &= ~SCTP_ADDR_PF; 5687 } 5688 } else { 5689 if ((net->failure_threshold > thlds->spt_pathpfthld) && 5690 (net->failure_threshold <= thlds->spt_pathmaxrxt)) { 5691 net->dest_state |= SCTP_ADDR_PF; 5692 sctp_send_hb(stcb, net, SCTP_SO_LOCKED); 5693 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3); 5694 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 5695 } 5696 } 5697 if (net->dest_state & SCTP_ADDR_REACHABLE) { 5698 if (net->failure_threshold > thlds->spt_pathmaxrxt) { 5699 net->dest_state &= ~SCTP_ADDR_REACHABLE; 5700 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED); 5701 } 5702 } else { 5703 if (net->failure_threshold <= thlds->spt_pathmaxrxt) { 5704 net->dest_state |= SCTP_ADDR_REACHABLE; 5705 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED); 5706 } 5707 } 5708 net->failure_threshold = thlds->spt_pathmaxrxt; 5709 net->pf_threshold = thlds->spt_pathpfthld; 5710 } 5711 stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt; 5712 stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld; 5713 } 5714 } else { 5715 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5716 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5717 (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) { 5718 SCTP_INP_WLOCK(inp); 5719 inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt; 5720 inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld; 5721 SCTP_INP_WUNLOCK(inp); 5722 } else { 5723 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5724 error = EINVAL; 5725 } 5726 } 5727 break; 5728 } 5729 case SCTP_REMOTE_UDP_ENCAPS_PORT: 5730 { 5731 struct sctp_udpencaps *encaps; 5732 struct sctp_nets *net; 5733 5734 SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize); 5735 SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id); 5736 if (stcb) { 5737 net = sctp_findnet(stcb, (struct sockaddr *)&encaps->sue_address); 5738 } else { 5739 /* 5740 * We increment here since 5741 * sctp_findassociation_ep_addr() wil do a 5742 * decrement if it finds the stcb as long as 5743 * the locked tcb (last argument) is NOT a 5744 * TCB.. aka NULL. 5745 */ 5746 net = NULL; 5747 SCTP_INP_INCR_REF(inp); 5748 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&encaps->sue_address, &net, NULL, NULL); 5749 if (stcb == NULL) { 5750 SCTP_INP_DECR_REF(inp); 5751 } 5752 } 5753 if (stcb && (net == NULL)) { 5754 struct sockaddr *sa; 5755 5756 sa = (struct sockaddr *)&encaps->sue_address; 5757 #ifdef INET 5758 if (sa->sa_family == AF_INET) { 5759 5760 struct sockaddr_in *sin; 5761 5762 sin = (struct sockaddr_in *)sa; 5763 if (sin->sin_addr.s_addr) { 5764 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5765 SCTP_TCB_UNLOCK(stcb); 5766 error = EINVAL; 5767 break; 5768 } 5769 } else 5770 #endif 5771 #ifdef INET6 5772 if (sa->sa_family == AF_INET6) { 5773 struct sockaddr_in6 *sin6; 5774 5775 sin6 = (struct sockaddr_in6 *)sa; 5776 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 5777 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5778 SCTP_TCB_UNLOCK(stcb); 5779 error = EINVAL; 5780 break; 5781 } 5782 } else 5783 #endif 5784 { 5785 error = EAFNOSUPPORT; 5786 SCTP_TCB_UNLOCK(stcb); 5787 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 5788 break; 5789 } 5790 } 5791 if (stcb) { 5792 if (net) { 5793 net->port = encaps->sue_port; 5794 } else { 5795 stcb->asoc.port = encaps->sue_port; 5796 } 5797 SCTP_TCB_UNLOCK(stcb); 5798 } else { 5799 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 5800 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || 5801 (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) { 5802 SCTP_INP_WLOCK(inp); 5803 inp->sctp_ep.port = encaps->sue_port; 5804 SCTP_INP_WUNLOCK(inp); 5805 } else { 5806 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5807 error = EINVAL; 5808 } 5809 } 5810 break; 5811 } 5812 default: 5813 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT); 5814 error = ENOPROTOOPT; 5815 break; 5816 } /* end switch (opt) */ 5817 return (error); 5818 } 5819 5820 int 5821 sctp_ctloutput(struct socket *so, struct sockopt *sopt) 5822 { 5823 void *optval = NULL; 5824 size_t optsize = 0; 5825 void *p; 5826 int error = 0; 5827 5828 if (sopt->sopt_level != IPPROTO_SCTP) { 5829 /* wrong proto level... send back up to IP */ 5830 #ifdef INET6 5831 if (INP_CHECK_SOCKAF(so, AF_INET6)) 5832 error = ip6_ctloutput(so, sopt); 5833 #endif /* INET6 */ 5834 #if defined(INET) && defined(INET6) 5835 else 5836 #endif 5837 #ifdef INET 5838 error = ip_ctloutput(so, sopt); 5839 #endif 5840 return (error); 5841 } 5842 optsize = sopt->sopt_valsize; 5843 if (optsize) { 5844 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT); 5845 if (optval == NULL) { 5846 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS); 5847 return (ENOBUFS); 5848 } 5849 error = sooptcopyin(sopt, optval, optsize, optsize); 5850 if (error) { 5851 SCTP_FREE(optval, SCTP_M_SOCKOPT); 5852 goto out; 5853 } 5854 } 5855 p = (void *)sopt->sopt_td; 5856 if (sopt->sopt_dir == SOPT_SET) { 5857 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p); 5858 } else if (sopt->sopt_dir == SOPT_GET) { 5859 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p); 5860 } else { 5861 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5862 error = EINVAL; 5863 } 5864 if ((error == 0) && (optval != NULL)) { 5865 error = sooptcopyout(sopt, optval, optsize); 5866 SCTP_FREE(optval, SCTP_M_SOCKOPT); 5867 } else if (optval != NULL) { 5868 SCTP_FREE(optval, SCTP_M_SOCKOPT); 5869 } 5870 out: 5871 return (error); 5872 } 5873 5874 #ifdef INET 5875 static int 5876 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 5877 { 5878 int error = 0; 5879 int create_lock_on = 0; 5880 uint32_t vrf_id; 5881 struct sctp_inpcb *inp; 5882 struct sctp_tcb *stcb = NULL; 5883 5884 inp = (struct sctp_inpcb *)so->so_pcb; 5885 if (inp == NULL) { 5886 /* I made the same as TCP since we are not setup? */ 5887 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5888 return (ECONNRESET); 5889 } 5890 if (addr == NULL) { 5891 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5892 return EINVAL; 5893 } 5894 switch (addr->sa_family) { 5895 #ifdef INET6 5896 case AF_INET6: 5897 { 5898 struct sockaddr_in6 *sin6p; 5899 5900 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 5901 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5902 return (EINVAL); 5903 } 5904 sin6p = (struct sockaddr_in6 *)addr; 5905 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) { 5906 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 5907 return (error); 5908 } 5909 break; 5910 } 5911 #endif 5912 #ifdef INET 5913 case AF_INET: 5914 { 5915 struct sockaddr_in *sinp; 5916 5917 if (addr->sa_len != sizeof(struct sockaddr_in)) { 5918 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5919 return (EINVAL); 5920 } 5921 sinp = (struct sockaddr_in *)addr; 5922 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) { 5923 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error); 5924 return (error); 5925 } 5926 break; 5927 } 5928 #endif 5929 default: 5930 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT); 5931 return (EAFNOSUPPORT); 5932 } 5933 SCTP_INP_INCR_REF(inp); 5934 SCTP_ASOC_CREATE_LOCK(inp); 5935 create_lock_on = 1; 5936 5937 5938 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 5939 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 5940 /* Should I really unlock ? */ 5941 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT); 5942 error = EFAULT; 5943 goto out_now; 5944 } 5945 #ifdef INET6 5946 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 5947 (addr->sa_family == AF_INET6)) { 5948 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5949 error = EINVAL; 5950 goto out_now; 5951 } 5952 #endif /* INET6 */ 5953 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 5954 SCTP_PCB_FLAGS_UNBOUND) { 5955 /* Bind a ephemeral port */ 5956 error = sctp_inpcb_bind(so, NULL, NULL, p); 5957 if (error) { 5958 goto out_now; 5959 } 5960 } 5961 /* Now do we connect? */ 5962 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && 5963 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) { 5964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 5965 error = EINVAL; 5966 goto out_now; 5967 } 5968 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 5969 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 5970 /* We are already connected AND the TCP model */ 5971 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE); 5972 error = EADDRINUSE; 5973 goto out_now; 5974 } 5975 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 5976 SCTP_INP_RLOCK(inp); 5977 stcb = LIST_FIRST(&inp->sctp_asoc_list); 5978 SCTP_INP_RUNLOCK(inp); 5979 } else { 5980 /* 5981 * We increment here since sctp_findassociation_ep_addr() 5982 * will do a decrement if it finds the stcb as long as the 5983 * locked tcb (last argument) is NOT a TCB.. aka NULL. 5984 */ 5985 SCTP_INP_INCR_REF(inp); 5986 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 5987 if (stcb == NULL) { 5988 SCTP_INP_DECR_REF(inp); 5989 } else { 5990 SCTP_TCB_UNLOCK(stcb); 5991 } 5992 } 5993 if (stcb != NULL) { 5994 /* Already have or am bring up an association */ 5995 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY); 5996 error = EALREADY; 5997 goto out_now; 5998 } 5999 vrf_id = inp->def_vrf_id; 6000 /* We are GOOD to go */ 6001 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p); 6002 if (stcb == NULL) { 6003 /* Gak! no memory */ 6004 goto out_now; 6005 } 6006 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 6007 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 6008 /* Set the connected flag so we can queue data */ 6009 soisconnecting(so); 6010 } 6011 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT); 6012 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 6013 6014 /* initialize authentication parameters for the assoc */ 6015 sctp_initialize_auth_params(inp, stcb); 6016 6017 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 6018 SCTP_TCB_UNLOCK(stcb); 6019 out_now: 6020 if (create_lock_on) { 6021 SCTP_ASOC_CREATE_UNLOCK(inp); 6022 } 6023 SCTP_INP_DECR_REF(inp); 6024 return (error); 6025 } 6026 6027 #endif 6028 6029 int 6030 sctp_listen(struct socket *so, int backlog, struct thread *p) 6031 { 6032 /* 6033 * Note this module depends on the protocol processing being called 6034 * AFTER any socket level flags and backlog are applied to the 6035 * socket. The traditional way that the socket flags are applied is 6036 * AFTER protocol processing. We have made a change to the 6037 * sys/kern/uipc_socket.c module to reverse this but this MUST be in 6038 * place if the socket API for SCTP is to work properly. 6039 */ 6040 6041 int error = 0; 6042 struct sctp_inpcb *inp; 6043 6044 inp = (struct sctp_inpcb *)so->so_pcb; 6045 if (inp == NULL) { 6046 /* I made the same as TCP since we are not setup? */ 6047 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 6048 return (ECONNRESET); 6049 } 6050 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) { 6051 /* See if we have a listener */ 6052 struct sctp_inpcb *tinp; 6053 union sctp_sockstore store, *sp; 6054 6055 sp = &store; 6056 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 6057 /* not bound all */ 6058 struct sctp_laddr *laddr; 6059 6060 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 6061 memcpy(&store, &laddr->ifa->address, sizeof(store)); 6062 switch (sp->sa.sa_family) { 6063 #ifdef INET 6064 case AF_INET: 6065 sp->sin.sin_port = inp->sctp_lport; 6066 break; 6067 #endif 6068 #ifdef INET6 6069 case AF_INET6: 6070 sp->sin6.sin6_port = inp->sctp_lport; 6071 break; 6072 #endif 6073 default: 6074 break; 6075 } 6076 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id); 6077 if (tinp && (tinp != inp) && 6078 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) && 6079 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 6080 (tinp->sctp_socket->so_qlimit)) { 6081 /* 6082 * we have a listener already and 6083 * its not this inp. 6084 */ 6085 SCTP_INP_DECR_REF(tinp); 6086 return (EADDRINUSE); 6087 } else if (tinp) { 6088 SCTP_INP_DECR_REF(tinp); 6089 } 6090 } 6091 } else { 6092 /* Setup a local addr bound all */ 6093 memset(&store, 0, sizeof(store)); 6094 switch (sp->sa.sa_family) { 6095 #ifdef INET 6096 case AF_INET: 6097 store.sin.sin_port = inp->sctp_lport; 6098 break; 6099 #endif 6100 #ifdef INET6 6101 case AF_INET6: 6102 sp->sin6.sin6_port = inp->sctp_lport; 6103 break; 6104 #endif 6105 default: 6106 break; 6107 } 6108 #ifdef INET6 6109 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 6110 store.sa.sa_family = AF_INET6; 6111 store.sa.sa_len = sizeof(struct sockaddr_in6); 6112 } 6113 #endif 6114 #ifdef INET 6115 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { 6116 store.sa.sa_family = AF_INET; 6117 store.sa.sa_len = sizeof(struct sockaddr_in); 6118 } 6119 #endif 6120 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id); 6121 if (tinp && (tinp != inp) && 6122 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) && 6123 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 6124 (tinp->sctp_socket->so_qlimit)) { 6125 /* 6126 * we have a listener already and its not 6127 * this inp. 6128 */ 6129 SCTP_INP_DECR_REF(tinp); 6130 return (EADDRINUSE); 6131 } else if (tinp) { 6132 SCTP_INP_DECR_REF(inp); 6133 } 6134 } 6135 } 6136 SCTP_INP_RLOCK(inp); 6137 #ifdef SCTP_LOCK_LOGGING 6138 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) { 6139 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK); 6140 } 6141 #endif 6142 SOCK_LOCK(so); 6143 error = solisten_proto_check(so); 6144 if (error) { 6145 SOCK_UNLOCK(so); 6146 SCTP_INP_RUNLOCK(inp); 6147 return (error); 6148 } 6149 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && 6150 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 6151 /* 6152 * The unlucky case - We are in the tcp pool with this guy. 6153 * - Someone else is in the main inp slot. - We must move 6154 * this guy (the listener) to the main slot - We must then 6155 * move the guy that was listener to the TCP Pool. 6156 */ 6157 if (sctp_swap_inpcb_for_listen(inp)) { 6158 goto in_use; 6159 } 6160 } 6161 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 6162 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 6163 /* We are already connected AND the TCP model */ 6164 in_use: 6165 SCTP_INP_RUNLOCK(inp); 6166 SOCK_UNLOCK(so); 6167 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE); 6168 return (EADDRINUSE); 6169 } 6170 SCTP_INP_RUNLOCK(inp); 6171 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 6172 /* We must do a bind. */ 6173 SOCK_UNLOCK(so); 6174 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) { 6175 /* bind error, probably perm */ 6176 return (error); 6177 } 6178 SOCK_LOCK(so); 6179 } 6180 /* It appears for 7.0 and on, we must always call this. */ 6181 solisten_proto(so, backlog); 6182 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 6183 /* remove the ACCEPTCONN flag for one-to-many sockets */ 6184 so->so_options &= ~SO_ACCEPTCONN; 6185 } 6186 if (backlog == 0) { 6187 /* turning off listen */ 6188 so->so_options &= ~SO_ACCEPTCONN; 6189 } 6190 SOCK_UNLOCK(so); 6191 return (error); 6192 } 6193 6194 static int sctp_defered_wakeup_cnt = 0; 6195 6196 int 6197 sctp_accept(struct socket *so, struct sockaddr **addr) 6198 { 6199 struct sctp_tcb *stcb; 6200 struct sctp_inpcb *inp; 6201 union sctp_sockstore store; 6202 6203 #ifdef INET6 6204 int error; 6205 6206 #endif 6207 inp = (struct sctp_inpcb *)so->so_pcb; 6208 6209 if (inp == NULL) { 6210 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 6211 return (ECONNRESET); 6212 } 6213 SCTP_INP_RLOCK(inp); 6214 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 6215 SCTP_INP_RUNLOCK(inp); 6216 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP); 6217 return (EOPNOTSUPP); 6218 } 6219 if (so->so_state & SS_ISDISCONNECTED) { 6220 SCTP_INP_RUNLOCK(inp); 6221 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED); 6222 return (ECONNABORTED); 6223 } 6224 stcb = LIST_FIRST(&inp->sctp_asoc_list); 6225 if (stcb == NULL) { 6226 SCTP_INP_RUNLOCK(inp); 6227 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 6228 return (ECONNRESET); 6229 } 6230 SCTP_TCB_LOCK(stcb); 6231 SCTP_INP_RUNLOCK(inp); 6232 store = stcb->asoc.primary_destination->ro._l_addr; 6233 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; 6234 SCTP_TCB_UNLOCK(stcb); 6235 switch (store.sa.sa_family) { 6236 #ifdef INET 6237 case AF_INET: 6238 { 6239 struct sockaddr_in *sin; 6240 6241 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin); 6242 if (sin == NULL) 6243 return (ENOMEM); 6244 sin->sin_family = AF_INET; 6245 sin->sin_len = sizeof(*sin); 6246 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port; 6247 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr; 6248 *addr = (struct sockaddr *)sin; 6249 break; 6250 } 6251 #endif 6252 #ifdef INET6 6253 case AF_INET6: 6254 { 6255 struct sockaddr_in6 *sin6; 6256 6257 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 6258 if (sin6 == NULL) 6259 return (ENOMEM); 6260 sin6->sin6_family = AF_INET6; 6261 sin6->sin6_len = sizeof(*sin6); 6262 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port; 6263 6264 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr; 6265 if ((error = sa6_recoverscope(sin6)) != 0) { 6266 SCTP_FREE_SONAME(sin6); 6267 return (error); 6268 } 6269 *addr = (struct sockaddr *)sin6; 6270 break; 6271 } 6272 #endif 6273 default: 6274 /* TSNH */ 6275 break; 6276 } 6277 /* Wake any delayed sleep action */ 6278 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) { 6279 SCTP_INP_WLOCK(inp); 6280 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE; 6281 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) { 6282 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; 6283 SCTP_INP_WUNLOCK(inp); 6284 SOCKBUF_LOCK(&inp->sctp_socket->so_snd); 6285 if (sowriteable(inp->sctp_socket)) { 6286 sowwakeup_locked(inp->sctp_socket); 6287 } else { 6288 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd); 6289 } 6290 SCTP_INP_WLOCK(inp); 6291 } 6292 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) { 6293 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; 6294 SCTP_INP_WUNLOCK(inp); 6295 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv); 6296 if (soreadable(inp->sctp_socket)) { 6297 sctp_defered_wakeup_cnt++; 6298 sorwakeup_locked(inp->sctp_socket); 6299 } else { 6300 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv); 6301 } 6302 SCTP_INP_WLOCK(inp); 6303 } 6304 SCTP_INP_WUNLOCK(inp); 6305 } 6306 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 6307 SCTP_TCB_LOCK(stcb); 6308 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7); 6309 } 6310 return (0); 6311 } 6312 6313 #ifdef INET 6314 int 6315 sctp_ingetaddr(struct socket *so, struct sockaddr **addr) 6316 { 6317 struct sockaddr_in *sin; 6318 uint32_t vrf_id; 6319 struct sctp_inpcb *inp; 6320 struct sctp_ifa *sctp_ifa; 6321 6322 /* 6323 * Do the malloc first in case it blocks. 6324 */ 6325 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin); 6326 if (sin == NULL) 6327 return (ENOMEM); 6328 sin->sin_family = AF_INET; 6329 sin->sin_len = sizeof(*sin); 6330 inp = (struct sctp_inpcb *)so->so_pcb; 6331 if (!inp) { 6332 SCTP_FREE_SONAME(sin); 6333 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 6334 return (ECONNRESET); 6335 } 6336 SCTP_INP_RLOCK(inp); 6337 sin->sin_port = inp->sctp_lport; 6338 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 6339 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 6340 struct sctp_tcb *stcb; 6341 struct sockaddr_in *sin_a; 6342 struct sctp_nets *net; 6343 int fnd; 6344 6345 stcb = LIST_FIRST(&inp->sctp_asoc_list); 6346 if (stcb == NULL) { 6347 goto notConn; 6348 } 6349 fnd = 0; 6350 sin_a = NULL; 6351 SCTP_TCB_LOCK(stcb); 6352 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 6353 sin_a = (struct sockaddr_in *)&net->ro._l_addr; 6354 if (sin_a == NULL) 6355 /* this will make coverity happy */ 6356 continue; 6357 6358 if (sin_a->sin_family == AF_INET) { 6359 fnd = 1; 6360 break; 6361 } 6362 } 6363 if ((!fnd) || (sin_a == NULL)) { 6364 /* punt */ 6365 SCTP_TCB_UNLOCK(stcb); 6366 goto notConn; 6367 } 6368 vrf_id = inp->def_vrf_id; 6369 sctp_ifa = sctp_source_address_selection(inp, 6370 stcb, 6371 (sctp_route_t *) & net->ro, 6372 net, 0, vrf_id); 6373 if (sctp_ifa) { 6374 sin->sin_addr = sctp_ifa->address.sin.sin_addr; 6375 sctp_free_ifa(sctp_ifa); 6376 } 6377 SCTP_TCB_UNLOCK(stcb); 6378 } else { 6379 /* For the bound all case you get back 0 */ 6380 notConn: 6381 sin->sin_addr.s_addr = 0; 6382 } 6383 6384 } else { 6385 /* Take the first IPv4 address in the list */ 6386 struct sctp_laddr *laddr; 6387 int fnd = 0; 6388 6389 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 6390 if (laddr->ifa->address.sa.sa_family == AF_INET) { 6391 struct sockaddr_in *sin_a; 6392 6393 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa; 6394 sin->sin_addr = sin_a->sin_addr; 6395 fnd = 1; 6396 break; 6397 } 6398 } 6399 if (!fnd) { 6400 SCTP_FREE_SONAME(sin); 6401 SCTP_INP_RUNLOCK(inp); 6402 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 6403 return (ENOENT); 6404 } 6405 } 6406 SCTP_INP_RUNLOCK(inp); 6407 (*addr) = (struct sockaddr *)sin; 6408 return (0); 6409 } 6410 6411 int 6412 sctp_peeraddr(struct socket *so, struct sockaddr **addr) 6413 { 6414 struct sockaddr_in *sin; 6415 int fnd; 6416 struct sockaddr_in *sin_a; 6417 struct sctp_inpcb *inp; 6418 struct sctp_tcb *stcb; 6419 struct sctp_nets *net; 6420 6421 /* Do the malloc first in case it blocks. */ 6422 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin); 6423 if (sin == NULL) 6424 return (ENOMEM); 6425 sin->sin_family = AF_INET; 6426 sin->sin_len = sizeof(*sin); 6427 6428 inp = (struct sctp_inpcb *)so->so_pcb; 6429 if ((inp == NULL) || 6430 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 6431 /* UDP type and listeners will drop out here */ 6432 SCTP_FREE_SONAME(sin); 6433 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN); 6434 return (ENOTCONN); 6435 } 6436 SCTP_INP_RLOCK(inp); 6437 stcb = LIST_FIRST(&inp->sctp_asoc_list); 6438 if (stcb) { 6439 SCTP_TCB_LOCK(stcb); 6440 } 6441 SCTP_INP_RUNLOCK(inp); 6442 if (stcb == NULL) { 6443 SCTP_FREE_SONAME(sin); 6444 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); 6445 return (ECONNRESET); 6446 } 6447 fnd = 0; 6448 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 6449 sin_a = (struct sockaddr_in *)&net->ro._l_addr; 6450 if (sin_a->sin_family == AF_INET) { 6451 fnd = 1; 6452 sin->sin_port = stcb->rport; 6453 sin->sin_addr = sin_a->sin_addr; 6454 break; 6455 } 6456 } 6457 SCTP_TCB_UNLOCK(stcb); 6458 if (!fnd) { 6459 /* No IPv4 address */ 6460 SCTP_FREE_SONAME(sin); 6461 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); 6462 return (ENOENT); 6463 } 6464 (*addr) = (struct sockaddr *)sin; 6465 return (0); 6466 } 6467 6468 struct pr_usrreqs sctp_usrreqs = { 6469 .pru_abort = sctp_abort, 6470 .pru_accept = sctp_accept, 6471 .pru_attach = sctp_attach, 6472 .pru_bind = sctp_bind, 6473 .pru_connect = sctp_connect, 6474 .pru_control = in_control, 6475 .pru_close = sctp_close, 6476 .pru_detach = sctp_close, 6477 .pru_sopoll = sopoll_generic, 6478 .pru_flush = sctp_flush, 6479 .pru_disconnect = sctp_disconnect, 6480 .pru_listen = sctp_listen, 6481 .pru_peeraddr = sctp_peeraddr, 6482 .pru_send = sctp_sendm, 6483 .pru_shutdown = sctp_shutdown, 6484 .pru_sockaddr = sctp_ingetaddr, 6485 .pru_sosend = sctp_sosend, 6486 .pru_soreceive = sctp_soreceive 6487 }; 6488 6489 #endif 6490