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