1 /* $KAME: sctp_usrreq.c,v 1.50 2005/06/16 20:45:29 jinmei Exp $ */ 2 /* $NetBSD: sctp_usrreq.c,v 1.13 2018/09/03 16:29:36 riastradh Exp $ */ 3 4 /* 5 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Cisco Systems, Inc. 19 * 4. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: sctp_usrreq.c,v 1.13 2018/09/03 16:29:36 riastradh Exp $"); 37 38 #ifdef _KERNEL_OPT 39 #include "opt_inet.h" 40 #include "opt_sctp.h" 41 #endif /* _KERNEL_OPT */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/domain.h> 49 #include <sys/proc.h> 50 #include <sys/protosw.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/syslog.h> 55 #include <net/if.h> 56 #include <net/if_types.h> 57 #include <net/route.h> 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/ip.h> 61 #include <netinet/ip6.h> 62 #include <netinet/in_pcb.h> 63 #include <netinet/in_var.h> 64 #include <netinet/ip_var.h> 65 #include <netinet6/ip6_var.h> 66 #include <netinet6/in6_var.h> 67 #include <netinet6/scope6_var.h> 68 69 #include <netinet/ip_icmp.h> 70 #include <netinet/icmp_var.h> 71 #include <netinet/sctp_pcb.h> 72 #include <netinet/sctp_header.h> 73 #include <netinet/sctp_var.h> 74 #include <netinet/sctp_output.h> 75 #include <netinet/sctp_uio.h> 76 #include <netinet/sctp_asconf.h> 77 #include <netinet/sctputil.h> 78 #include <netinet/sctp_indata.h> 79 #include <netinet/sctp_asconf.h> 80 #ifdef IPSEC 81 #include <netipsec/ipsec.h> 82 #include <netipsec/key.h> 83 #endif /* IPSEC */ 84 85 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) 86 #ifndef in6pcb 87 #define in6pcb inpcb 88 #endif 89 #ifndef sotoin6pcb 90 #define sotoin6pcb sotoinpcb 91 #endif 92 #endif 93 94 #ifdef SCTP_DEBUG 95 extern u_int32_t sctp_debug_on; 96 #endif /* SCTP_DEBUG */ 97 98 /* 99 * sysctl tunable variables 100 */ 101 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF; 102 int sctp_max_burst_default = SCTP_DEF_MAX_BURST; 103 int sctp_peer_chunk_oh = sizeof(struct mbuf); 104 int sctp_strict_init = 1; 105 int sctp_no_csum_on_loopback = 1; 106 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE; 107 int sctp_sendspace = (128 * 1024); 108 int sctp_recvspace = 128 * (1024 + 109 #ifdef INET6 110 sizeof(struct sockaddr_in6) 111 #else 112 sizeof(struct sockaddr_in) 113 #endif 114 ); 115 int sctp_strict_sacks = 0; 116 int sctp_ecn = 1; 117 int sctp_ecn_nonce = 0; 118 119 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC; 120 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC; 121 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC; 122 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC; 123 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC; 124 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND; 125 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND; 126 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL; 127 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND; 128 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE; 129 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT; 130 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND; 131 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2; 132 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL; 133 134 static void sysctl_net_inet_sctp_setup(struct sysctllog **); 135 136 void 137 sctp_init(void) 138 { 139 /* Init the SCTP pcb in sctp_pcb.c */ 140 u_long sb_max_adj; 141 142 sysctl_net_inet_sctp_setup(NULL); 143 144 sctp_pcb_init(); 145 146 if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE) 147 sctp_max_chunks_on_queue = nmbclusters; 148 /* 149 * Allow a user to take no more than 1/2 the number of clusters 150 * or the SB_MAX whichever is smaller for the send window. 151 */ 152 sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES)); 153 sctp_sendspace = uimin((uimin(SB_MAX, sb_max_adj)), 154 ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT)); 155 /* 156 * Now for the recv window, should we take the same amount? 157 * or should I do 1/2 the SB_MAX instead in the SB_MAX min above. 158 * For now I will just copy. 159 */ 160 sctp_recvspace = sctp_sendspace; 161 } 162 163 #ifdef INET6 164 void 165 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip) 166 { 167 memset(ip6, 0, sizeof(*ip6)); 168 169 ip6->ip6_vfc = IPV6_VERSION; 170 ip6->ip6_plen = ip->ip_len; 171 ip6->ip6_nxt = ip->ip_p; 172 ip6->ip6_hlim = ip->ip_ttl; 173 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 174 IPV6_ADDR_INT32_SMP; 175 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 176 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 177 } 178 #endif /* INET6 */ 179 180 static void 181 sctp_split_chunks(struct sctp_association *asoc, 182 struct sctp_stream_out *strm, 183 struct sctp_tmit_chunk *chk) 184 { 185 struct sctp_tmit_chunk *new_chk; 186 187 /* First we need a chunk */ 188 new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk); 189 if (new_chk == NULL) { 190 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 191 return; 192 } 193 sctppcbinfo.ipi_count_chunk++; 194 sctppcbinfo.ipi_gencnt_chunk++; 195 /* Copy it all */ 196 *new_chk = *chk; 197 /* split the data */ 198 new_chk->data = m_split(chk->data, (chk->send_size>>1), M_DONTWAIT); 199 if (new_chk->data == NULL) { 200 /* Can't split */ 201 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 202 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk); 203 sctppcbinfo.ipi_count_chunk--; 204 if ((int)sctppcbinfo.ipi_count_chunk < 0) { 205 panic("Chunk count is negative"); 206 } 207 sctppcbinfo.ipi_gencnt_chunk++; 208 return; 209 210 } 211 /* Data is now split adjust sizes */ 212 chk->send_size >>= 1; 213 new_chk->send_size >>= 1; 214 215 chk->book_size >>= 1; 216 new_chk->book_size >>= 1; 217 218 /* now adjust the marks */ 219 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG; 220 chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG; 221 222 new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG; 223 new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG; 224 225 /* Increase ref count if dest is set */ 226 if (chk->whoTo) { 227 new_chk->whoTo->ref_count++; 228 } 229 /* now drop it on the end of the list*/ 230 asoc->stream_queue_cnt++; 231 TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next); 232 } 233 234 static void 235 sctp_notify_mbuf(struct sctp_inpcb *inp, 236 struct sctp_tcb *stcb, 237 struct sctp_nets *net, 238 struct ip *ip, 239 struct sctphdr *sh) 240 241 { 242 struct icmp *icmph; 243 int totsz; 244 uint16_t nxtsz; 245 246 /* protection */ 247 if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 248 (ip == NULL) || (sh == NULL)) { 249 if (stcb != NULL) { 250 SCTP_TCB_UNLOCK(stcb); 251 } 252 return; 253 } 254 /* First job is to verify the vtag matches what I would send */ 255 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 256 SCTP_TCB_UNLOCK(stcb); 257 return; 258 } 259 icmph = (struct icmp *)((vaddr_t)ip - (sizeof(struct icmp) - 260 sizeof(struct ip))); 261 if (icmph->icmp_type != ICMP_UNREACH) { 262 /* We only care about unreachable */ 263 SCTP_TCB_UNLOCK(stcb); 264 return; 265 } 266 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) { 267 /* not a unreachable message due to frag. */ 268 SCTP_TCB_UNLOCK(stcb); 269 return; 270 } 271 totsz = ip->ip_len; 272 nxtsz = ntohs(icmph->icmp_seq); 273 if (nxtsz == 0) { 274 /* 275 * old type router that does not tell us what the next size 276 * mtu is. Rats we will have to guess (in a educated fashion 277 * of course) 278 */ 279 nxtsz = find_next_best_mtu(totsz); 280 } 281 282 /* Stop any PMTU timer */ 283 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 284 285 /* Adjust destination size limit */ 286 if (net->mtu > nxtsz) { 287 net->mtu = nxtsz; 288 } 289 /* now what about the ep? */ 290 if (stcb->asoc.smallest_mtu > nxtsz) { 291 struct sctp_tmit_chunk *chk, *nchk; 292 struct sctp_stream_out *strm; 293 /* Adjust that too */ 294 stcb->asoc.smallest_mtu = nxtsz; 295 /* now off to subtract IP_DF flag if needed */ 296 297 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 298 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) { 299 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 300 } 301 } 302 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 303 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) { 304 /* 305 * For this guy we also mark for immediate 306 * resend since we sent to big of chunk 307 */ 308 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 309 if (chk->sent != SCTP_DATAGRAM_RESEND) { 310 stcb->asoc.sent_queue_retran_cnt++; 311 } 312 chk->sent = SCTP_DATAGRAM_RESEND; 313 chk->rec.data.doing_fast_retransmit = 0; 314 315 /* Clear any time so NO RTT is being done */ 316 chk->do_rtt = 0; 317 sctp_total_flight_decrease(stcb, chk); 318 if (net->flight_size >= chk->book_size) { 319 net->flight_size -= chk->book_size; 320 } else { 321 net->flight_size = 0; 322 } 323 } 324 } 325 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) { 326 chk = TAILQ_FIRST(&strm->outqueue); 327 while (chk) { 328 nchk = TAILQ_NEXT(chk, sctp_next); 329 if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) { 330 sctp_split_chunks(&stcb->asoc, strm, chk); 331 } 332 chk = nchk; 333 } 334 } 335 } 336 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 337 SCTP_TCB_UNLOCK(stcb); 338 } 339 340 341 void 342 sctp_notify(struct sctp_inpcb *inp, 343 int errno, 344 struct sctphdr *sh, 345 struct sockaddr *to, 346 struct sctp_tcb *stcb, 347 struct sctp_nets *net) 348 { 349 /* protection */ 350 if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 351 (sh == NULL) || (to == NULL)) { 352 #ifdef SCTP_DEBUG 353 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 354 printf("sctp-notify, bad call\n"); 355 } 356 #endif /* SCTP_DEBUG */ 357 return; 358 } 359 /* First job is to verify the vtag matches what I would send */ 360 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 361 return; 362 } 363 364 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */ 365 366 if ((errno == EHOSTUNREACH) || /* Host is not reachable */ 367 (errno == EHOSTDOWN) || /* Host is down */ 368 (errno == ECONNREFUSED) || /* Host refused the connection, (not an abort?) */ 369 (errno == ENOPROTOOPT) /* SCTP is not present on host */ 370 ) { 371 /* 372 * Hmm reachablity problems we must examine closely. 373 * If its not reachable, we may have lost a network. 374 * Or if there is NO protocol at the other end named SCTP. 375 * well we consider it a OOTB abort. 376 */ 377 if ((errno == EHOSTUNREACH) || (errno == EHOSTDOWN)) { 378 if (net->dest_state & SCTP_ADDR_REACHABLE) { 379 /* Ok that destination is NOT reachable */ 380 net->dest_state &= ~SCTP_ADDR_REACHABLE; 381 net->dest_state |= SCTP_ADDR_NOT_REACHABLE; 382 net->error_count = net->failure_threshold + 1; 383 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 384 stcb, SCTP_FAILED_THRESHOLD, 385 (void *)net); 386 } 387 if (stcb) { 388 SCTP_TCB_UNLOCK(stcb); 389 } 390 } else { 391 /* 392 * Here the peer is either playing tricks on us, 393 * including an address that belongs to someone who 394 * does not support SCTP OR was a userland 395 * implementation that shutdown and now is dead. In 396 * either case treat it like a OOTB abort with no TCB 397 */ 398 sctp_abort_notification(stcb, SCTP_PEER_FAULTY); 399 sctp_free_assoc(inp, stcb); 400 /* no need to unlock here, since the TCB is gone */ 401 } 402 } else { 403 /* Send all others to the app */ 404 if (inp->sctp_socket) { 405 inp->sctp_socket->so_error = errno; 406 sctp_sowwakeup(inp, inp->sctp_socket); 407 } 408 if (stcb) { 409 SCTP_TCB_UNLOCK(stcb); 410 } 411 } 412 } 413 414 void * 415 sctp_ctlinput(int cmd, const struct sockaddr *sa, void *vip) 416 { 417 struct ip *ip = vip; 418 struct sctphdr *sh; 419 int s; 420 421 if (sa->sa_family != AF_INET || 422 ((const struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) { 423 return (NULL); 424 } 425 426 if (PRC_IS_REDIRECT(cmd)) { 427 ip = 0; 428 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) { 429 return (NULL); 430 } 431 if (ip) { 432 struct sctp_inpcb *inp; 433 struct sctp_tcb *stcb; 434 struct sctp_nets *net; 435 struct sockaddr_in to, from; 436 437 sh = (struct sctphdr *)((vaddr_t)ip + (ip->ip_hl << 2)); 438 memset(&to, 0, sizeof(to)); 439 memset(&from, 0, sizeof(from)); 440 from.sin_family = to.sin_family = AF_INET; 441 from.sin_len = to.sin_len = sizeof(to); 442 from.sin_port = sh->src_port; 443 from.sin_addr = ip->ip_src; 444 to.sin_port = sh->dest_port; 445 to.sin_addr = ip->ip_dst; 446 447 /* 448 * 'to' holds the dest of the packet that failed to be sent. 449 * 'from' holds our local endpoint address. 450 * Thus we reverse the to and the from in the lookup. 451 */ 452 s = splsoftnet(); 453 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from, 454 (struct sockaddr *)&to, 455 &inp, &net, 1); 456 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 457 if (cmd != PRC_MSGSIZE) { 458 int cm; 459 if (cmd == PRC_HOSTDEAD) { 460 cm = EHOSTUNREACH; 461 } else { 462 cm = inetctlerrmap[cmd]; 463 } 464 sctp_notify(inp, cm, sh, 465 (struct sockaddr *)&to, stcb, 466 net); 467 } else { 468 /* handle possible ICMP size messages */ 469 sctp_notify_mbuf(inp, stcb, net, ip, sh); 470 } 471 } else { 472 #if defined(__FreeBSD__) && __FreeBSD_version < 500000 473 /* XXX must be fixed for 5.x and higher, leave for 4.x */ 474 if (PRC_IS_REDIRECT(cmd) && inp) { 475 in_rtchange((struct inpcb *)inp, 476 inetctlerrmap[cmd]); 477 } 478 #endif 479 if ((stcb == NULL) && (inp != NULL)) { 480 /* reduce ref-count */ 481 SCTP_INP_WLOCK(inp); 482 SCTP_INP_DECR_REF(inp); 483 SCTP_INP_WUNLOCK(inp); 484 } 485 486 } 487 splx(s); 488 } 489 return (NULL); 490 } 491 492 static int 493 sctp_abort(struct socket *so) 494 { 495 struct sctp_inpcb *inp; 496 497 inp = (struct sctp_inpcb *)so->so_pcb; 498 if (inp == 0) 499 return EINVAL; /* ??? possible? panic instead? */ 500 501 sctp_inpcb_free(inp, 1); 502 return 0; 503 } 504 505 static int 506 sctp_attach(struct socket *so, int proto) 507 { 508 struct sctp_inpcb *inp; 509 #ifdef IPSEC 510 struct inpcb *ip_inp; 511 #endif 512 int error; 513 514 sosetlock(so); 515 inp = (struct sctp_inpcb *)so->so_pcb; 516 if (inp != 0) { 517 return EINVAL; 518 } 519 error = soreserve(so, sctp_sendspace, sctp_recvspace); 520 if (error) { 521 return error; 522 } 523 error = sctp_inpcb_alloc(so); 524 if (error) { 525 return error; 526 } 527 inp = (struct sctp_inpcb *)so->so_pcb; 528 SCTP_INP_WLOCK(inp); 529 530 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */ 531 #ifdef IPSEC 532 ip_inp = &inp->ip_inp.inp; 533 ip_inp->inp_af = proto; 534 #endif 535 inp->inp_vflag |= INP_IPV4; 536 inp->inp_ip_ttl = ip_defttl; 537 538 #ifdef IPSEC 539 error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp); 540 if (error != 0) { 541 sctp_inpcb_free(inp, 1); 542 return error; 543 } 544 #endif /*IPSEC*/ 545 SCTP_INP_WUNLOCK(inp); 546 so->so_send = sctp_sosend; 547 return 0; 548 } 549 550 static int 551 sctp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 552 { 553 struct sctp_inpcb *inp; 554 int error; 555 556 KASSERT(solocked(so)); 557 558 #ifdef INET6 559 if (nam && nam->sa_family != AF_INET) 560 /* must be a v4 address! */ 561 return EINVAL; 562 #endif /* INET6 */ 563 564 inp = (struct sctp_inpcb *)so->so_pcb; 565 if (inp == 0) 566 return EINVAL; 567 568 error = sctp_inpcb_bind(so, nam, l); 569 return error; 570 } 571 572 573 static int 574 sctp_detach(struct socket *so) 575 { 576 struct sctp_inpcb *inp; 577 578 inp = (struct sctp_inpcb *)so->so_pcb; 579 if (inp == 0) 580 return EINVAL; 581 582 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) || 583 (so->so_rcv.sb_cc > 0)) { 584 sctp_inpcb_free(inp, 1); 585 } else { 586 sctp_inpcb_free(inp, 0); 587 } 588 return 0; 589 } 590 591 static int 592 sctp_recvoob(struct socket *so, struct mbuf *m, int flags) 593 { 594 KASSERT(solocked(so)); 595 596 return EOPNOTSUPP; 597 } 598 599 int 600 sctp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr, 601 struct mbuf *control, struct lwp *l) 602 { 603 struct sctp_inpcb *inp; 604 int error; 605 inp = (struct sctp_inpcb *)so->so_pcb; 606 if (inp == 0) { 607 if (control) { 608 sctp_m_freem(control); 609 control = NULL; 610 } 611 sctp_m_freem(m); 612 return EINVAL; 613 } 614 /* Got to have an to address if we are NOT a connected socket */ 615 if ((addr == NULL) && 616 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || 617 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) 618 ) { 619 goto connected_type; 620 } else if (addr == NULL) { 621 error = EDESTADDRREQ; 622 sctp_m_freem(m); 623 if (control) { 624 sctp_m_freem(control); 625 control = NULL; 626 } 627 return (error); 628 } 629 #ifdef INET6 630 if (addr->sa_family != AF_INET) { 631 /* must be a v4 address! */ 632 sctp_m_freem(m); 633 if (control) { 634 sctp_m_freem(control); 635 control = NULL; 636 } 637 error = EDESTADDRREQ; 638 return EINVAL; 639 } 640 #endif /* INET6 */ 641 connected_type: 642 /* now what about control */ 643 if (control) { 644 if (inp->control) { 645 printf("huh? control set?\n"); 646 sctp_m_freem(inp->control); 647 inp->control = NULL; 648 } 649 inp->control = control; 650 } 651 /* add it in possibly */ 652 if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) { 653 struct mbuf *x; 654 int c_len; 655 656 c_len = 0; 657 /* How big is it */ 658 for (x=m;x;x = x->m_next) { 659 c_len += x->m_len; 660 } 661 inp->pkt->m_pkthdr.len += c_len; 662 } 663 /* Place the data */ 664 if (inp->pkt) { 665 inp->pkt_last->m_next = m; 666 inp->pkt_last = m; 667 } else { 668 inp->pkt_last = inp->pkt = m; 669 } 670 if ((so->so_state & SS_MORETOCOME) == 0) { 671 /* 672 * note with the current version this code will only be used 673 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for 674 * re-defining sosend to use the sctp_sosend. One can 675 * optionally switch back to this code (by changing back the 676 * definitions) but this is not advisable. 677 */ 678 int ret; 679 ret = sctp_output(inp, inp->pkt, addr, inp->control, l, 0); 680 inp->pkt = NULL; 681 inp->control = NULL; 682 return (ret); 683 } else { 684 return (0); 685 } 686 } 687 688 static int 689 sctp_disconnect(struct socket *so) 690 { 691 struct sctp_inpcb *inp; 692 int s; 693 694 inp = (struct sctp_inpcb *)so->so_pcb; 695 if (inp == NULL) { 696 return (ENOTCONN); 697 } 698 s = splsoftnet(); 699 SCTP_INP_RLOCK(inp); 700 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 701 if (LIST_EMPTY(&inp->sctp_asoc_list)) { 702 /* No connection */ 703 SCTP_INP_RUNLOCK(inp); 704 splx(s); 705 return (0); 706 } else { 707 int some_on_streamwheel = 0; 708 struct sctp_association *asoc; 709 struct sctp_tcb *stcb; 710 711 stcb = LIST_FIRST(&inp->sctp_asoc_list); 712 if (stcb == NULL) { 713 SCTP_INP_RUNLOCK(inp); 714 splx(s); 715 return (EINVAL); 716 } 717 asoc = &stcb->asoc; 718 SCTP_TCB_LOCK(stcb); 719 if (((so->so_options & SO_LINGER) && 720 (so->so_linger == 0)) || 721 (so->so_rcv.sb_cc > 0)) { 722 if (SCTP_GET_STATE(asoc) != 723 SCTP_STATE_COOKIE_WAIT) { 724 /* Left with Data unread */ 725 struct mbuf *err; 726 err = NULL; 727 MGET(err, M_DONTWAIT, MT_DATA); 728 if (err) { 729 /* Fill in the user initiated abort */ 730 struct sctp_paramhdr *ph; 731 ph = mtod(err, struct sctp_paramhdr *); 732 err->m_len = sizeof(struct sctp_paramhdr); 733 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); 734 ph->param_length = htons(err->m_len); 735 } 736 sctp_send_abort_tcb(stcb, err); 737 } 738 SCTP_INP_RUNLOCK(inp); 739 sctp_free_assoc(inp, stcb); 740 /* No unlock tcb assoc is gone */ 741 splx(s); 742 return (0); 743 } 744 if (!TAILQ_EMPTY(&asoc->out_wheel)) { 745 /* Check to see if some data queued */ 746 struct sctp_stream_out *outs; 747 TAILQ_FOREACH(outs, &asoc->out_wheel, 748 next_spoke) { 749 if (!TAILQ_EMPTY(&outs->outqueue)) { 750 some_on_streamwheel = 1; 751 break; 752 } 753 } 754 } 755 756 if (TAILQ_EMPTY(&asoc->send_queue) && 757 TAILQ_EMPTY(&asoc->sent_queue) && 758 (some_on_streamwheel == 0)) { 759 /* there is nothing queued to send, so done */ 760 if ((SCTP_GET_STATE(asoc) != 761 SCTP_STATE_SHUTDOWN_SENT) && 762 (SCTP_GET_STATE(asoc) != 763 SCTP_STATE_SHUTDOWN_ACK_SENT)) { 764 /* only send SHUTDOWN 1st time thru */ 765 #ifdef SCTP_DEBUG 766 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) { 767 printf("%s:%d sends a shutdown\n", 768 __FILE__, 769 __LINE__ 770 ); 771 } 772 #endif 773 sctp_send_shutdown(stcb, 774 stcb->asoc.primary_destination); 775 sctp_chunk_output(stcb->sctp_ep, stcb, 1); 776 asoc->state = SCTP_STATE_SHUTDOWN_SENT; 777 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 778 stcb->sctp_ep, stcb, 779 asoc->primary_destination); 780 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 781 stcb->sctp_ep, stcb, 782 asoc->primary_destination); 783 } 784 } else { 785 /* 786 * we still got (or just got) data to send, 787 * so set SHUTDOWN_PENDING 788 */ 789 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 790 } 791 SCTP_TCB_UNLOCK(stcb); 792 SCTP_INP_RUNLOCK(inp); 793 splx(s); 794 return (0); 795 } 796 /* not reached */ 797 } else { 798 /* UDP model does not support this */ 799 SCTP_INP_RUNLOCK(inp); 800 splx(s); 801 return EOPNOTSUPP; 802 } 803 } 804 805 int 806 sctp_shutdown(struct socket *so) 807 { 808 struct sctp_inpcb *inp; 809 810 inp = (struct sctp_inpcb *)so->so_pcb; 811 if (inp == 0) { 812 return EINVAL; 813 } 814 SCTP_INP_RLOCK(inp); 815 /* For UDP model this is a invalid call */ 816 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 817 /* Restore the flags that the soshutdown took away. */ 818 so->so_state &= ~SS_CANTRCVMORE; 819 /* This proc will wakeup for read and do nothing (I hope) */ 820 SCTP_INP_RUNLOCK(inp); 821 return (EOPNOTSUPP); 822 } 823 /* 824 * Ok if we reach here its the TCP model and it is either a SHUT_WR 825 * or SHUT_RDWR. This means we put the shutdown flag against it. 826 */ 827 { 828 int some_on_streamwheel = 0; 829 struct sctp_tcb *stcb; 830 struct sctp_association *asoc; 831 socantsendmore(so); 832 833 stcb = LIST_FIRST(&inp->sctp_asoc_list); 834 if (stcb == NULL) { 835 /* 836 * Ok we hit the case that the shutdown call was made 837 * after an abort or something. Nothing to do now. 838 */ 839 return (0); 840 } 841 SCTP_TCB_LOCK(stcb); 842 asoc = &stcb->asoc; 843 844 if (!TAILQ_EMPTY(&asoc->out_wheel)) { 845 /* Check to see if some data queued */ 846 struct sctp_stream_out *outs; 847 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) { 848 if (!TAILQ_EMPTY(&outs->outqueue)) { 849 some_on_streamwheel = 1; 850 break; 851 } 852 } 853 } 854 if (TAILQ_EMPTY(&asoc->send_queue) && 855 TAILQ_EMPTY(&asoc->sent_queue) && 856 (some_on_streamwheel == 0)) { 857 /* there is nothing queued to send, so I'm done... */ 858 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 859 /* only send SHUTDOWN the first time through */ 860 #ifdef SCTP_DEBUG 861 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) { 862 printf("%s:%d sends a shutdown\n", 863 __FILE__, 864 __LINE__ 865 ); 866 } 867 #endif 868 sctp_send_shutdown(stcb, 869 stcb->asoc.primary_destination); 870 sctp_chunk_output(stcb->sctp_ep, stcb, 1); 871 asoc->state = SCTP_STATE_SHUTDOWN_SENT; 872 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 873 stcb->sctp_ep, stcb, 874 asoc->primary_destination); 875 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 876 stcb->sctp_ep, stcb, 877 asoc->primary_destination); 878 } 879 } else { 880 /* 881 * we still got (or just got) data to send, so 882 * set SHUTDOWN_PENDING 883 */ 884 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 885 } 886 SCTP_TCB_UNLOCK(stcb); 887 } 888 SCTP_INP_RUNLOCK(inp); 889 return 0; 890 } 891 892 /* 893 * copies a "user" presentable address and removes embedded scope, etc. 894 * returns 0 on success, 1 on error 895 */ 896 static uint32_t 897 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa) 898 { 899 struct sockaddr_in6 lsa6; 900 901 sctp_recover_scope((struct sockaddr_in6 *)sa, &lsa6); 902 memcpy(ss, sa, sa->sa_len); 903 return (0); 904 } 905 906 907 static int 908 sctp_fill_up_addresses(struct sctp_inpcb *inp, 909 struct sctp_tcb *stcb, 910 int limit, 911 struct sockaddr_storage *sas) 912 { 913 struct ifnet *ifn; 914 struct ifaddr *ifa; 915 int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual; 916 int ipv4_addr_legal, ipv6_addr_legal; 917 actual = 0; 918 if (limit <= 0) 919 return (actual); 920 921 if (stcb) { 922 /* Turn on all the appropriate scope */ 923 loopback_scope = stcb->asoc.loopback_scope; 924 ipv4_local_scope = stcb->asoc.ipv4_local_scope; 925 local_scope = stcb->asoc.local_scope; 926 site_scope = stcb->asoc.site_scope; 927 } else { 928 /* Turn on ALL scope, since we look at the EP */ 929 loopback_scope = ipv4_local_scope = local_scope = 930 site_scope = 1; 931 } 932 ipv4_addr_legal = ipv6_addr_legal = 0; 933 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 934 ipv6_addr_legal = 1; 935 if ( 936 #if defined(__OpenBSD__) 937 (0) /* we always do dual bind */ 938 #elif defined (__NetBSD__) 939 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY) 940 #else 941 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY) 942 #endif 943 == 0) { 944 ipv4_addr_legal = 1; 945 } 946 } else { 947 ipv4_addr_legal = 1; 948 } 949 950 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 951 int s = pserialize_read_enter(); 952 IFNET_READER_FOREACH(ifn) { 953 if ((loopback_scope == 0) && 954 (ifn->if_type == IFT_LOOP)) { 955 /* Skip loopback if loopback_scope not set */ 956 continue; 957 } 958 IFADDR_READER_FOREACH(ifa, ifn) { 959 if (stcb) { 960 /* 961 * For the BOUND-ALL case, the list 962 * associated with a TCB is Always 963 * considered a reverse list.. i.e. 964 * it lists addresses that are NOT 965 * part of the association. If this 966 * is one of those we must skip it. 967 */ 968 if (sctp_is_addr_restricted(stcb, 969 ifa->ifa_addr)) { 970 continue; 971 } 972 } 973 if ((ifa->ifa_addr->sa_family == AF_INET) && 974 (ipv4_addr_legal)) { 975 struct sockaddr_in *sin; 976 sin = (struct sockaddr_in *)ifa->ifa_addr; 977 if (sin->sin_addr.s_addr == 0) { 978 /* we skip unspecifed addresses */ 979 continue; 980 } 981 if ((ipv4_local_scope == 0) && 982 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 983 continue; 984 } 985 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) { 986 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas); 987 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 988 sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(struct sockaddr_in6)); 989 actual += sizeof(struct sockaddr_in6); 990 } else { 991 memcpy(sas, sin, sizeof(*sin)); 992 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport; 993 sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin)); 994 actual += sizeof(*sin); 995 } 996 if (actual >= limit) { 997 pserialize_read_exit(s); 998 return (actual); 999 } 1000 } else if ((ifa->ifa_addr->sa_family == AF_INET6) && 1001 (ipv6_addr_legal)) { 1002 struct sockaddr_in6 *sin6; 1003 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1004 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1005 /* 1006 * we skip unspecified 1007 * addresses 1008 */ 1009 continue; 1010 } 1011 if ((site_scope == 0) && 1012 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { 1013 continue; 1014 } 1015 memcpy(sas, sin6, sizeof(*sin6)); 1016 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1017 sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin6)); 1018 actual += sizeof(*sin6); 1019 if (actual >= limit) { 1020 pserialize_read_exit(s); 1021 return (actual); 1022 } 1023 } 1024 } 1025 } 1026 pserialize_read_exit(s); 1027 } else { 1028 struct sctp_laddr *laddr; 1029 /* 1030 * If we have a TCB and we do NOT support ASCONF (it's 1031 * turned off or otherwise) then the list is always the 1032 * true list of addresses (the else case below). Otherwise 1033 * the list on the association is a list of addresses that 1034 * are NOT part of the association. 1035 */ 1036 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) { 1037 /* The list is a NEGATIVE list */ 1038 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1039 if (stcb) { 1040 if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) { 1041 continue; 1042 } 1043 } 1044 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr)) 1045 continue; 1046 1047 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1048 sas = (struct sockaddr_storage *)((vaddr_t)sas + 1049 laddr->ifa->ifa_addr->sa_len); 1050 actual += laddr->ifa->ifa_addr->sa_len; 1051 if (actual >= limit) { 1052 return (actual); 1053 } 1054 } 1055 } else { 1056 /* The list is a positive list if present */ 1057 if (stcb) { 1058 /* Must use the specific association list */ 1059 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, 1060 sctp_nxt_addr) { 1061 if (sctp_fill_user_address(sas, 1062 laddr->ifa->ifa_addr)) 1063 continue; 1064 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1065 sas = (struct sockaddr_storage *)((vaddr_t)sas + 1066 laddr->ifa->ifa_addr->sa_len); 1067 actual += laddr->ifa->ifa_addr->sa_len; 1068 if (actual >= limit) { 1069 return (actual); 1070 } 1071 } 1072 } else { 1073 /* No endpoint so use the endpoints individual list */ 1074 LIST_FOREACH(laddr, &inp->sctp_addr_list, 1075 sctp_nxt_addr) { 1076 if (sctp_fill_user_address(sas, 1077 laddr->ifa->ifa_addr)) 1078 continue; 1079 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1080 sas = (struct sockaddr_storage *)((vaddr_t)sas + 1081 laddr->ifa->ifa_addr->sa_len); 1082 actual += laddr->ifa->ifa_addr->sa_len; 1083 if (actual >= limit) { 1084 return (actual); 1085 } 1086 } 1087 } 1088 } 1089 } 1090 return (actual); 1091 } 1092 1093 static int 1094 sctp_count_max_addresses(struct sctp_inpcb *inp) 1095 { 1096 int cnt = 0; 1097 /* 1098 * In both sub-set bound an bound_all cases we return the MAXIMUM 1099 * number of addresses that you COULD get. In reality the sub-set 1100 * bound may have an exclusion list for a given TCB OR in the 1101 * bound-all case a TCB may NOT include the loopback or other 1102 * addresses as well. 1103 */ 1104 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1105 struct ifnet *ifn; 1106 struct ifaddr *ifa; 1107 int s; 1108 1109 s = pserialize_read_enter(); 1110 IFNET_READER_FOREACH(ifn) { 1111 IFADDR_READER_FOREACH(ifa, ifn) { 1112 /* Count them if they are the right type */ 1113 if (ifa->ifa_addr->sa_family == AF_INET) { 1114 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) 1115 cnt += sizeof(struct sockaddr_in6); 1116 else 1117 cnt += sizeof(struct sockaddr_in); 1118 1119 } else if (ifa->ifa_addr->sa_family == AF_INET6) 1120 cnt += sizeof(struct sockaddr_in6); 1121 } 1122 } 1123 pserialize_read_exit(s); 1124 } else { 1125 struct sctp_laddr *laddr; 1126 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1127 if (laddr->ifa->ifa_addr->sa_family == AF_INET) { 1128 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) 1129 cnt += sizeof(struct sockaddr_in6); 1130 else 1131 cnt += sizeof(struct sockaddr_in); 1132 1133 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6) 1134 cnt += sizeof(struct sockaddr_in6); 1135 } 1136 } 1137 return (cnt); 1138 } 1139 1140 static int 1141 sctp_do_connect_x(struct socket *so, struct sctp_connectx_addrs *sca, 1142 struct lwp *l, int delay) 1143 { 1144 int error = 0; 1145 struct sctp_inpcb *inp; 1146 struct sctp_tcb *stcb = NULL; 1147 struct sockaddr *sa; 1148 int num_v6=0, num_v4=0, totaddr, i, incr, at; 1149 char buf[2048]; 1150 size_t len; 1151 sctp_assoc_t id; 1152 #ifdef SCTP_DEBUG 1153 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 1154 printf("Connectx called\n"); 1155 } 1156 #endif /* SCTP_DEBUG */ 1157 1158 inp = (struct sctp_inpcb *)so->so_pcb; 1159 if (inp == 0) 1160 return EINVAL; 1161 1162 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1163 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 1164 /* We are already connected AND the TCP model */ 1165 return (EADDRINUSE); 1166 } 1167 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1168 SCTP_INP_RLOCK(inp); 1169 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1170 SCTP_INP_RUNLOCK(inp); 1171 } 1172 if (stcb) { 1173 return (EALREADY); 1174 1175 } 1176 SCTP_ASOC_CREATE_LOCK(inp); 1177 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 1178 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 1179 SCTP_ASOC_CREATE_UNLOCK(inp); 1180 return (EFAULT); 1181 } 1182 1183 len = sca->cx_len; 1184 totaddr = sca->cx_num; 1185 if (len > sizeof(buf)) { 1186 return E2BIG; 1187 } 1188 error = copyin(sca->cx_addrs, buf, len); 1189 if (error) { 1190 return error; 1191 } 1192 sa = (struct sockaddr *)buf; 1193 at = incr = 0; 1194 /* account and validate addresses */ 1195 SCTP_INP_WLOCK(inp); 1196 SCTP_INP_INCR_REF(inp); 1197 SCTP_INP_WUNLOCK(inp); 1198 for (i = 0; i < totaddr; i++) { 1199 if (sa->sa_family == AF_INET) { 1200 num_v4++; 1201 incr = sizeof(struct sockaddr_in); 1202 } else if (sa->sa_family == AF_INET6) { 1203 struct sockaddr_in6 *sin6; 1204 sin6 = (struct sockaddr_in6 *)sa; 1205 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1206 /* Must be non-mapped for connectx */ 1207 SCTP_ASOC_CREATE_UNLOCK(inp); 1208 return EINVAL; 1209 } 1210 num_v6++; 1211 incr = sizeof(struct sockaddr_in6); 1212 } else { 1213 totaddr = i; 1214 break; 1215 } 1216 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL); 1217 if (stcb != NULL) { 1218 /* Already have or am bring up an association */ 1219 SCTP_ASOC_CREATE_UNLOCK(inp); 1220 SCTP_TCB_UNLOCK(stcb); 1221 return (EALREADY); 1222 } 1223 if ((at + incr) > len) { 1224 totaddr = i; 1225 break; 1226 } 1227 sa = (struct sockaddr *)((vaddr_t)sa + incr); 1228 } 1229 sa = (struct sockaddr *)buf; 1230 SCTP_INP_WLOCK(inp); 1231 SCTP_INP_DECR_REF(inp); 1232 SCTP_INP_WUNLOCK(inp); 1233 #ifdef INET6 1234 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 1235 (num_v6 > 0)) { 1236 SCTP_INP_WUNLOCK(inp); 1237 SCTP_ASOC_CREATE_UNLOCK(inp); 1238 return (EINVAL); 1239 } 1240 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 1241 (num_v4 > 0)) { 1242 struct in6pcb *inp6; 1243 inp6 = (struct in6pcb *)inp; 1244 if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) { 1245 /* 1246 * if IPV6_V6ONLY flag, ignore connections 1247 * destined to a v4 addr or v4-mapped addr 1248 */ 1249 SCTP_INP_WUNLOCK(inp); 1250 SCTP_ASOC_CREATE_UNLOCK(inp); 1251 return EINVAL; 1252 } 1253 } 1254 #endif /* INET6 */ 1255 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 1256 SCTP_PCB_FLAGS_UNBOUND) { 1257 /* Bind a ephemeral port */ 1258 SCTP_INP_WUNLOCK(inp); 1259 error = sctp_inpcb_bind(so, NULL, l); 1260 if (error) { 1261 SCTP_ASOC_CREATE_UNLOCK(inp); 1262 return (error); 1263 } 1264 } else { 1265 SCTP_INP_WUNLOCK(inp); 1266 } 1267 /* We are GOOD to go */ 1268 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0); 1269 if (stcb == NULL) { 1270 /* Gak! no memory */ 1271 SCTP_ASOC_CREATE_UNLOCK(inp); 1272 return (error); 1273 } 1274 1275 /* move to second address */ 1276 if (sa->sa_family == AF_INET) 1277 sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in)); 1278 else 1279 sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in6)); 1280 1281 for (i = 1; i < totaddr; i++) { 1282 if (sa->sa_family == AF_INET) { 1283 incr = sizeof(struct sockaddr_in); 1284 if (sctp_add_remote_addr(stcb, sa, 0, 8)) { 1285 /* assoc gone no un-lock */ 1286 sctp_free_assoc(inp, stcb); 1287 SCTP_ASOC_CREATE_UNLOCK(inp); 1288 return (ENOBUFS); 1289 } 1290 1291 } else if (sa->sa_family == AF_INET6) { 1292 incr = sizeof(struct sockaddr_in6); 1293 if (sctp_add_remote_addr(stcb, sa, 0, 8)) { 1294 /* assoc gone no un-lock */ 1295 sctp_free_assoc(inp, stcb); 1296 SCTP_ASOC_CREATE_UNLOCK(inp); 1297 return (ENOBUFS); 1298 } 1299 } 1300 sa = (struct sockaddr *)((vaddr_t)sa + incr); 1301 } 1302 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1303 1304 id = sctp_get_associd(stcb); 1305 memcpy(&sca->cx_num, &id, sizeof(sctp_assoc_t)); 1306 1307 if (delay) { 1308 /* doing delayed connection */ 1309 stcb->asoc.delayed_connection = 1; 1310 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination); 1311 } else { 1312 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1313 sctp_send_initiate(inp, stcb); 1314 } 1315 SCTP_TCB_UNLOCK(stcb); 1316 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1317 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1318 /* Set the connected flag so we can queue data */ 1319 soisconnecting(so); 1320 } 1321 SCTP_ASOC_CREATE_UNLOCK(inp); 1322 return error; 1323 } 1324 1325 1326 static int 1327 sctp_optsget(struct socket *so, struct sockopt *sopt) 1328 { 1329 struct sctp_inpcb *inp; 1330 int error, optval=0; 1331 int *ovp; 1332 struct sctp_tcb *stcb = NULL; 1333 1334 inp = (struct sctp_inpcb *)so->so_pcb; 1335 if (inp == 0) 1336 return EINVAL; 1337 error = 0; 1338 1339 #ifdef SCTP_DEBUG 1340 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) { 1341 printf("optsget opt:%x sz:%zu\n", sopt->sopt_name, 1342 sopt->sopt_size); 1343 } 1344 #endif /* SCTP_DEBUG */ 1345 1346 switch (sopt->sopt_name) { 1347 case SCTP_NODELAY: 1348 case SCTP_AUTOCLOSE: 1349 case SCTP_AUTO_ASCONF: 1350 case SCTP_DISABLE_FRAGMENTS: 1351 case SCTP_I_WANT_MAPPED_V4_ADDR: 1352 #ifdef SCTP_DEBUG 1353 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) { 1354 printf("other stuff\n"); 1355 } 1356 #endif /* SCTP_DEBUG */ 1357 SCTP_INP_RLOCK(inp); 1358 switch (sopt->sopt_name) { 1359 case SCTP_DISABLE_FRAGMENTS: 1360 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT; 1361 break; 1362 case SCTP_I_WANT_MAPPED_V4_ADDR: 1363 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4; 1364 break; 1365 case SCTP_AUTO_ASCONF: 1366 optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF; 1367 break; 1368 case SCTP_NODELAY: 1369 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY; 1370 break; 1371 case SCTP_AUTOCLOSE: 1372 if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) == 1373 SCTP_PCB_FLAGS_AUTOCLOSE) 1374 optval = inp->sctp_ep.auto_close_time; 1375 else 1376 optval = 0; 1377 break; 1378 1379 default: 1380 error = ENOPROTOOPT; 1381 } /* end switch (sopt->sopt_name) */ 1382 if (sopt->sopt_name != SCTP_AUTOCLOSE) { 1383 /* make it an "on/off" value */ 1384 optval = (optval != 0); 1385 } 1386 if (sopt->sopt_size < sizeof(int)) { 1387 error = EINVAL; 1388 } 1389 SCTP_INP_RUNLOCK(inp); 1390 if (error == 0) { 1391 /* return the option value */ 1392 ovp = sopt->sopt_data; 1393 *ovp = optval; 1394 sopt->sopt_size = sizeof(optval); 1395 } 1396 break; 1397 case SCTP_GET_ASOC_ID_LIST: 1398 { 1399 struct sctp_assoc_ids *ids; 1400 int cnt, at; 1401 u_int16_t orig; 1402 1403 if (sopt->sopt_size < sizeof(struct sctp_assoc_ids)) { 1404 error = EINVAL; 1405 break; 1406 } 1407 ids = sopt->sopt_data; 1408 cnt = 0; 1409 SCTP_INP_RLOCK(inp); 1410 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1411 if (stcb == NULL) { 1412 none_out_now: 1413 ids->asls_numb_present = 0; 1414 ids->asls_more_to_get = 0; 1415 SCTP_INP_RUNLOCK(inp); 1416 break; 1417 } 1418 orig = ids->asls_assoc_start; 1419 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1420 while( orig ) { 1421 stcb = LIST_NEXT(stcb , sctp_tcblist); 1422 orig--; 1423 cnt--; 1424 } 1425 if ( stcb == NULL) 1426 goto none_out_now; 1427 1428 at = 0; 1429 ids->asls_numb_present = 0; 1430 ids->asls_more_to_get = 1; 1431 while(at < MAX_ASOC_IDS_RET) { 1432 ids->asls_assoc_id[at] = sctp_get_associd(stcb); 1433 at++; 1434 ids->asls_numb_present++; 1435 stcb = LIST_NEXT(stcb , sctp_tcblist); 1436 if (stcb == NULL) { 1437 ids->asls_more_to_get = 0; 1438 break; 1439 } 1440 } 1441 SCTP_INP_RUNLOCK(inp); 1442 } 1443 break; 1444 case SCTP_GET_NONCE_VALUES: 1445 { 1446 struct sctp_get_nonce_values *gnv; 1447 if (sopt->sopt_size < sizeof(struct sctp_get_nonce_values)) { 1448 error = EINVAL; 1449 break; 1450 } 1451 gnv = sopt->sopt_data; 1452 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id); 1453 if (stcb == NULL) { 1454 error = ENOTCONN; 1455 } else { 1456 gnv->gn_peers_tag = stcb->asoc.peer_vtag; 1457 gnv->gn_local_tag = stcb->asoc.my_vtag; 1458 SCTP_TCB_UNLOCK(stcb); 1459 } 1460 1461 } 1462 break; 1463 case SCTP_PEER_PUBLIC_KEY: 1464 case SCTP_MY_PUBLIC_KEY: 1465 case SCTP_SET_AUTH_CHUNKS: 1466 case SCTP_SET_AUTH_SECRET: 1467 /* not supported yet and until we refine the draft */ 1468 error = EOPNOTSUPP; 1469 break; 1470 1471 case SCTP_DELAYED_ACK_TIME: 1472 { 1473 int32_t *tm; 1474 if (sopt->sopt_size < sizeof(int32_t)) { 1475 error = EINVAL; 1476 break; 1477 } 1478 tm = sopt->sopt_data; 1479 1480 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]); 1481 } 1482 break; 1483 1484 case SCTP_GET_SNDBUF_USE: 1485 if (sopt->sopt_size < sizeof(struct sctp_sockstat)) { 1486 error = EINVAL; 1487 } else { 1488 struct sctp_sockstat *ss; 1489 struct sctp_association *asoc; 1490 ss = sopt->sopt_data; 1491 stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id); 1492 if (stcb == NULL) { 1493 error = ENOTCONN; 1494 } else { 1495 asoc = &stcb->asoc; 1496 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size; 1497 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size; 1498 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue + 1499 asoc->size_on_reasm_queue + 1500 asoc->size_on_all_streams); 1501 SCTP_TCB_UNLOCK(stcb); 1502 error = 0; 1503 sopt->sopt_size = sizeof(struct sctp_sockstat); 1504 } 1505 } 1506 break; 1507 case SCTP_MAXBURST: 1508 { 1509 u_int8_t *burst; 1510 burst = sopt->sopt_data; 1511 SCTP_INP_RLOCK(inp); 1512 *burst = inp->sctp_ep.max_burst; 1513 SCTP_INP_RUNLOCK(inp); 1514 sopt->sopt_size = sizeof(u_int8_t); 1515 } 1516 break; 1517 case SCTP_MAXSEG: 1518 { 1519 u_int32_t *segsize; 1520 sctp_assoc_t *assoc_id; 1521 int ovh; 1522 1523 if (sopt->sopt_size < sizeof(u_int32_t)) { 1524 error = EINVAL; 1525 break; 1526 } 1527 if (sopt->sopt_size < sizeof(sctp_assoc_t)) { 1528 error = EINVAL; 1529 break; 1530 } 1531 assoc_id = sopt->sopt_data; 1532 segsize = sopt->sopt_data; 1533 sopt->sopt_size = sizeof(u_int32_t); 1534 1535 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1536 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) || 1537 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 1538 SCTP_INP_RLOCK(inp); 1539 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1540 if (stcb) { 1541 SCTP_TCB_LOCK(stcb); 1542 SCTP_INP_RUNLOCK(inp); 1543 *segsize = sctp_get_frag_point(stcb, &stcb->asoc); 1544 SCTP_TCB_UNLOCK(stcb); 1545 } else { 1546 SCTP_INP_RUNLOCK(inp); 1547 goto skipit; 1548 } 1549 } else { 1550 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id); 1551 if (stcb) { 1552 *segsize = sctp_get_frag_point(stcb, &stcb->asoc); 1553 SCTP_TCB_UNLOCK(stcb); 1554 break; 1555 } 1556 skipit: 1557 /* default is to get the max, if I 1558 * can't calculate from an existing association. 1559 */ 1560 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1561 ovh = SCTP_MED_OVERHEAD; 1562 } else { 1563 ovh = SCTP_MED_V4_OVERHEAD; 1564 } 1565 *segsize = inp->sctp_frag_point - ovh; 1566 } 1567 } 1568 break; 1569 1570 case SCTP_SET_DEBUG_LEVEL: 1571 #ifdef SCTP_DEBUG 1572 { 1573 u_int32_t *level; 1574 if (sopt->sopt_size < sizeof(u_int32_t)) { 1575 error = EINVAL; 1576 break; 1577 } 1578 level = sopt->sopt_data; 1579 error = 0; 1580 *level = sctp_debug_on; 1581 sopt->sopt_size = sizeof(u_int32_t); 1582 printf("Returning DEBUG LEVEL %x is set\n", 1583 (u_int)sctp_debug_on); 1584 } 1585 #else /* SCTP_DEBUG */ 1586 error = EOPNOTSUPP; 1587 #endif 1588 break; 1589 case SCTP_GET_STAT_LOG: 1590 #ifdef SCTP_STAT_LOGGING 1591 error = sctp_fill_stat_log(m); 1592 #else /* SCTP_DEBUG */ 1593 error = EOPNOTSUPP; 1594 #endif 1595 break; 1596 case SCTP_GET_PEGS: 1597 { 1598 u_int32_t *pt; 1599 if (sopt->sopt_size < sizeof(sctp_pegs)) { 1600 error = EINVAL; 1601 break; 1602 } 1603 pt = sopt->sopt_data; 1604 memcpy(pt, sctp_pegs, sizeof(sctp_pegs)); 1605 sopt->sopt_size = sizeof(sctp_pegs); 1606 } 1607 break; 1608 case SCTP_EVENTS: 1609 { 1610 struct sctp_event_subscribe *events; 1611 #ifdef SCTP_DEBUG 1612 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) { 1613 printf("get events\n"); 1614 } 1615 #endif /* SCTP_DEBUG */ 1616 if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) { 1617 #ifdef SCTP_DEBUG 1618 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) { 1619 printf("sopt->sopt_size is %d not %d\n", 1620 (int)sopt->sopt_size, 1621 (int)sizeof(struct sctp_event_subscribe)); 1622 } 1623 #endif /* SCTP_DEBUG */ 1624 error = EINVAL; 1625 break; 1626 } 1627 events = sopt->sopt_data; 1628 memset(events, 0, sopt->sopt_size); 1629 SCTP_INP_RLOCK(inp); 1630 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) 1631 events->sctp_data_io_event = 1; 1632 1633 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT) 1634 events->sctp_association_event = 1; 1635 1636 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT) 1637 events->sctp_address_event = 1; 1638 1639 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT) 1640 events->sctp_send_failure_event = 1; 1641 1642 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR) 1643 events->sctp_peer_error_event = 1; 1644 1645 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT) 1646 events->sctp_shutdown_event = 1; 1647 1648 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT) 1649 events->sctp_partial_delivery_event = 1; 1650 1651 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) 1652 events->sctp_adaption_layer_event = 1; 1653 1654 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT) 1655 events->sctp_stream_reset_events = 1; 1656 SCTP_INP_RUNLOCK(inp); 1657 sopt->sopt_size = sizeof(struct sctp_event_subscribe); 1658 1659 } 1660 break; 1661 1662 case SCTP_ADAPTION_LAYER: 1663 if (sopt->sopt_size < sizeof(int)) { 1664 error = EINVAL; 1665 break; 1666 } 1667 #ifdef SCTP_DEBUG 1668 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1669 printf("getadaption ind\n"); 1670 } 1671 #endif /* SCTP_DEBUG */ 1672 SCTP_INP_RLOCK(inp); 1673 ovp = sopt->sopt_data; 1674 *ovp = inp->sctp_ep.adaption_layer_indicator; 1675 SCTP_INP_RUNLOCK(inp); 1676 sopt->sopt_size = sizeof(int); 1677 break; 1678 case SCTP_SET_INITIAL_DBG_SEQ: 1679 if (sopt->sopt_size < sizeof(int)) { 1680 error = EINVAL; 1681 break; 1682 } 1683 #ifdef SCTP_DEBUG 1684 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1685 printf("get initial dbg seq\n"); 1686 } 1687 #endif /* SCTP_DEBUG */ 1688 SCTP_INP_RLOCK(inp); 1689 ovp = sopt->sopt_data; 1690 *ovp = inp->sctp_ep.initial_sequence_debug; 1691 SCTP_INP_RUNLOCK(inp); 1692 sopt->sopt_size = sizeof(int); 1693 break; 1694 case SCTP_GET_LOCAL_ADDR_SIZE: 1695 if (sopt->sopt_size < sizeof(int)) { 1696 error = EINVAL; 1697 break; 1698 } 1699 #ifdef SCTP_DEBUG 1700 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1701 printf("get local sizes\n"); 1702 } 1703 #endif /* SCTP_DEBUG */ 1704 SCTP_INP_RLOCK(inp); 1705 ovp = sopt->sopt_data; 1706 *ovp = sctp_count_max_addresses(inp); 1707 SCTP_INP_RUNLOCK(inp); 1708 sopt->sopt_size = sizeof(int); 1709 break; 1710 case SCTP_GET_REMOTE_ADDR_SIZE: 1711 { 1712 sctp_assoc_t *assoc_id; 1713 u_int32_t *val, sz; 1714 struct sctp_nets *net; 1715 #ifdef SCTP_DEBUG 1716 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1717 printf("get remote size\n"); 1718 } 1719 #endif /* SCTP_DEBUG */ 1720 if (sopt->sopt_size < sizeof(sctp_assoc_t)) { 1721 #ifdef SCTP_DEBUG 1722 printf("sopt->sopt_size:%zu not %zu\n", 1723 sopt->sopt_size, sizeof(sctp_assoc_t)); 1724 #endif /* SCTP_DEBUG */ 1725 error = EINVAL; 1726 break; 1727 } 1728 stcb = NULL; 1729 val = sopt->sopt_data; 1730 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1731 SCTP_INP_RLOCK(inp); 1732 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1733 if (stcb) { 1734 SCTP_TCB_LOCK(stcb); 1735 } 1736 SCTP_INP_RUNLOCK(inp); 1737 } 1738 if (stcb == NULL) { 1739 assoc_id = sopt->sopt_data; 1740 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id); 1741 } 1742 1743 if (stcb == NULL) { 1744 error = EINVAL; 1745 break; 1746 } 1747 *val = 0; 1748 sz = 0; 1749 /* Count the sizes */ 1750 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1751 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) || 1752 (rtcache_getdst(&net->ro)->sa_family == AF_INET6)) { 1753 sz += sizeof(struct sockaddr_in6); 1754 } else if (rtcache_getdst(&net->ro)->sa_family == AF_INET) { 1755 sz += sizeof(struct sockaddr_in); 1756 } else { 1757 /* huh */ 1758 break; 1759 } 1760 } 1761 SCTP_TCB_UNLOCK(stcb); 1762 *val = sz; 1763 sopt->sopt_size = sizeof(u_int32_t); 1764 } 1765 break; 1766 case SCTP_GET_PEER_ADDRESSES: 1767 /* 1768 * Get the address information, an array 1769 * is passed in to fill up we pack it. 1770 */ 1771 { 1772 int cpsz, left; 1773 struct sockaddr_storage *sas; 1774 struct sctp_nets *net; 1775 struct sctp_getaddresses *saddr; 1776 #ifdef SCTP_DEBUG 1777 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1778 printf("get peer addresses\n"); 1779 } 1780 #endif /* SCTP_DEBUG */ 1781 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) { 1782 error = EINVAL; 1783 break; 1784 } 1785 left = sopt->sopt_size - sizeof(struct sctp_getaddresses); 1786 saddr = sopt->sopt_data; 1787 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1788 SCTP_INP_RLOCK(inp); 1789 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1790 if (stcb) { 1791 SCTP_TCB_LOCK(stcb); 1792 } 1793 SCTP_INP_RUNLOCK(inp); 1794 } else 1795 stcb = sctp_findassociation_ep_asocid(inp, 1796 saddr->sget_assoc_id); 1797 if (stcb == NULL) { 1798 error = ENOENT; 1799 break; 1800 } 1801 sopt->sopt_size = sizeof(struct sctp_getaddresses); 1802 sas = (struct sockaddr_storage *)&saddr->addr[0]; 1803 1804 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1805 sa_family_t family; 1806 1807 family = rtcache_getdst(&net->ro)->sa_family; 1808 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) || 1809 (family == AF_INET6)) { 1810 cpsz = sizeof(struct sockaddr_in6); 1811 } else if (family == AF_INET) { 1812 cpsz = sizeof(struct sockaddr_in); 1813 } else { 1814 /* huh */ 1815 break; 1816 } 1817 if (left < cpsz) { 1818 /* not enough room. */ 1819 #ifdef SCTP_DEBUG 1820 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1821 printf("Out of room\n"); 1822 } 1823 #endif /* SCTP_DEBUG */ 1824 break; 1825 } 1826 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) && 1827 (family == AF_INET)) { 1828 /* Must map the address */ 1829 in6_sin_2_v4mapsin6((const struct sockaddr_in *) rtcache_getdst(&net->ro), 1830 (struct sockaddr_in6 *)sas); 1831 } else { 1832 memcpy(sas, rtcache_getdst(&net->ro), cpsz); 1833 } 1834 ((struct sockaddr_in *)sas)->sin_port = stcb->rport; 1835 1836 sas = (struct sockaddr_storage *)((vaddr_t)sas + cpsz); 1837 left -= cpsz; 1838 sopt->sopt_size += cpsz; 1839 #ifdef SCTP_DEBUG 1840 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) { 1841 printf("left now:%d mlen:%zu\n", 1842 left, sopt->sopt_size); 1843 } 1844 #endif /* SCTP_DEBUG */ 1845 } 1846 SCTP_TCB_UNLOCK(stcb); 1847 } 1848 #ifdef SCTP_DEBUG 1849 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1850 printf("All done\n"); 1851 } 1852 #endif /* SCTP_DEBUG */ 1853 break; 1854 case SCTP_GET_LOCAL_ADDRESSES: 1855 { 1856 int limit, actual; 1857 struct sockaddr_storage *sas; 1858 struct sctp_getaddresses *saddr; 1859 #ifdef SCTP_DEBUG 1860 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1861 printf("get local addresses\n"); 1862 } 1863 #endif /* SCTP_DEBUG */ 1864 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) { 1865 error = EINVAL; 1866 break; 1867 } 1868 saddr = sopt->sopt_data; 1869 1870 if (saddr->sget_assoc_id) { 1871 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1872 SCTP_INP_RLOCK(inp); 1873 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1874 if (stcb) { 1875 SCTP_TCB_LOCK(stcb); 1876 } 1877 SCTP_INP_RUNLOCK(inp); 1878 } else 1879 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id); 1880 1881 } else { 1882 stcb = NULL; 1883 } 1884 /* 1885 * assure that the TCP model does not need a assoc id 1886 * once connected. 1887 */ 1888 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 1889 (stcb == NULL) ) { 1890 SCTP_INP_RLOCK(inp); 1891 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1892 if (stcb) { 1893 SCTP_TCB_LOCK(stcb); 1894 } 1895 SCTP_INP_RUNLOCK(inp); 1896 } 1897 sas = (struct sockaddr_storage *)&saddr->addr[0]; 1898 limit = sopt->sopt_size - sizeof(sctp_assoc_t); 1899 actual = sctp_fill_up_addresses(inp, stcb, limit, sas); 1900 SCTP_TCB_UNLOCK(stcb); 1901 sopt->sopt_size = sizeof(struct sockaddr_storage) + actual; 1902 } 1903 break; 1904 case SCTP_PEER_ADDR_PARAMS: 1905 { 1906 struct sctp_paddrparams *paddrp; 1907 struct sctp_nets *net; 1908 1909 #ifdef SCTP_DEBUG 1910 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1911 printf("Getting peer_addr_params\n"); 1912 } 1913 #endif /* SCTP_DEBUG */ 1914 if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) { 1915 #ifdef SCTP_DEBUG 1916 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) { 1917 printf("Hmm m->m_len:%zu is to small\n", 1918 sopt->sopt_size); 1919 } 1920 #endif /* SCTP_DEBUG */ 1921 error = EINVAL; 1922 break; 1923 } 1924 paddrp = sopt->sopt_data; 1925 1926 net = NULL; 1927 if (paddrp->spp_assoc_id) { 1928 #ifdef SCTP_DEBUG 1929 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1930 printf("In spp_assoc_id find type\n"); 1931 } 1932 #endif /* SCTP_DEBUG */ 1933 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1934 SCTP_INP_RLOCK(inp); 1935 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1936 if (stcb) { 1937 SCTP_TCB_LOCK(stcb); 1938 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 1939 } 1940 SCTP_INP_RLOCK(inp); 1941 } else { 1942 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id); 1943 } 1944 if (stcb == NULL) { 1945 error = ENOENT; 1946 break; 1947 } 1948 } 1949 if ((stcb == NULL) && 1950 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) || 1951 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) { 1952 /* Lookup via address */ 1953 #ifdef SCTP_DEBUG 1954 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1955 printf("Ok we need to lookup a param\n"); 1956 } 1957 #endif /* SCTP_DEBUG */ 1958 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1959 SCTP_INP_RLOCK(inp); 1960 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1961 if (stcb) { 1962 SCTP_TCB_LOCK(stcb); 1963 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 1964 } 1965 SCTP_INP_RUNLOCK(inp); 1966 } else { 1967 SCTP_INP_WLOCK(inp); 1968 SCTP_INP_INCR_REF(inp); 1969 SCTP_INP_WUNLOCK(inp); 1970 stcb = sctp_findassociation_ep_addr(&inp, 1971 (struct sockaddr *)&paddrp->spp_address, 1972 &net, NULL, NULL); 1973 if (stcb == NULL) { 1974 SCTP_INP_WLOCK(inp); 1975 SCTP_INP_DECR_REF(inp); 1976 SCTP_INP_WUNLOCK(inp); 1977 } 1978 } 1979 1980 if (stcb == NULL) { 1981 error = ENOENT; 1982 break; 1983 } 1984 } else { 1985 /* Effects the Endpoint */ 1986 #ifdef SCTP_DEBUG 1987 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1988 printf("User wants EP level info\n"); 1989 } 1990 #endif /* SCTP_DEBUG */ 1991 stcb = NULL; 1992 } 1993 if (stcb) { 1994 /* Applys to the specific association */ 1995 #ifdef SCTP_DEBUG 1996 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 1997 printf("In TCB side\n"); 1998 } 1999 #endif /* SCTP_DEBUG */ 2000 if (net) { 2001 paddrp->spp_pathmaxrxt = net->failure_threshold; 2002 } else { 2003 /* No destination so return default value */ 2004 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure; 2005 } 2006 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay; 2007 paddrp->spp_assoc_id = sctp_get_associd(stcb); 2008 SCTP_TCB_UNLOCK(stcb); 2009 } else { 2010 /* Use endpoint defaults */ 2011 SCTP_INP_RLOCK(inp); 2012 #ifdef SCTP_DEBUG 2013 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2014 printf("In EP levle info\n"); 2015 } 2016 #endif /* SCTP_DEBUG */ 2017 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure; 2018 paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]; 2019 paddrp->spp_assoc_id = (sctp_assoc_t)0; 2020 SCTP_INP_RUNLOCK(inp); 2021 } 2022 sopt->sopt_size = sizeof(struct sctp_paddrparams); 2023 } 2024 break; 2025 case SCTP_GET_PEER_ADDR_INFO: 2026 { 2027 struct sctp_paddrinfo *paddri; 2028 struct sctp_nets *net; 2029 #ifdef SCTP_DEBUG 2030 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2031 printf("GetPEER ADDR_INFO\n"); 2032 } 2033 #endif /* SCTP_DEBUG */ 2034 if (sopt->sopt_size < sizeof(struct sctp_paddrinfo)) { 2035 error = EINVAL; 2036 break; 2037 } 2038 paddri = sopt->sopt_data; 2039 net = NULL; 2040 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) || 2041 (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) { 2042 /* Lookup via address */ 2043 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2044 SCTP_INP_RLOCK(inp); 2045 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2046 if (stcb) { 2047 SCTP_TCB_LOCK(stcb); 2048 net = sctp_findnet(stcb, 2049 (struct sockaddr *)&paddri->spinfo_address); 2050 } 2051 SCTP_INP_RUNLOCK(inp); 2052 } else { 2053 SCTP_INP_WLOCK(inp); 2054 SCTP_INP_INCR_REF(inp); 2055 SCTP_INP_WUNLOCK(inp); 2056 stcb = sctp_findassociation_ep_addr(&inp, 2057 (struct sockaddr *)&paddri->spinfo_address, 2058 &net, NULL, NULL); 2059 if (stcb == NULL) { 2060 SCTP_INP_WLOCK(inp); 2061 SCTP_INP_DECR_REF(inp); 2062 SCTP_INP_WUNLOCK(inp); 2063 } 2064 } 2065 2066 } else { 2067 stcb = NULL; 2068 } 2069 if ((stcb == NULL) || (net == NULL)) { 2070 error = ENOENT; 2071 break; 2072 } 2073 sopt->sopt_size = sizeof(struct sctp_paddrinfo); 2074 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB); 2075 paddri->spinfo_cwnd = net->cwnd; 2076 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1; 2077 paddri->spinfo_rto = net->RTO; 2078 paddri->spinfo_assoc_id = sctp_get_associd(stcb); 2079 SCTP_TCB_UNLOCK(stcb); 2080 } 2081 break; 2082 case SCTP_PCB_STATUS: 2083 { 2084 struct sctp_pcbinfo *spcb; 2085 #ifdef SCTP_DEBUG 2086 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2087 printf("PCB status\n"); 2088 } 2089 #endif /* SCTP_DEBUG */ 2090 if (sopt->sopt_size < sizeof(struct sctp_pcbinfo)) { 2091 error = EINVAL; 2092 break; 2093 } 2094 spcb = sopt->sopt_data; 2095 sctp_fill_pcbinfo(spcb); 2096 sopt->sopt_size = sizeof(struct sctp_pcbinfo); 2097 } 2098 break; 2099 case SCTP_STATUS: 2100 { 2101 struct sctp_nets *net; 2102 struct sctp_status *sstat; 2103 #ifdef SCTP_DEBUG 2104 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2105 printf("SCTP status\n"); 2106 } 2107 #endif /* SCTP_DEBUG */ 2108 2109 if (sopt->sopt_size < sizeof(struct sctp_status)) { 2110 error = EINVAL; 2111 break; 2112 } 2113 sstat = sopt->sopt_data; 2114 2115 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2116 SCTP_INP_RLOCK(inp); 2117 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2118 if (stcb) { 2119 SCTP_TCB_LOCK(stcb); 2120 } 2121 SCTP_INP_RUNLOCK(inp); 2122 } else 2123 stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id); 2124 2125 if (stcb == NULL) { 2126 printf("SCTP status, no stcb\n"); 2127 error = EINVAL; 2128 break; 2129 } 2130 /* 2131 * I think passing the state is fine since 2132 * sctp_constants.h will be available to the user 2133 * land. 2134 */ 2135 sstat->sstat_state = stcb->asoc.state; 2136 sstat->sstat_rwnd = stcb->asoc.peers_rwnd; 2137 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt; 2138 /* 2139 * We can't include chunks that have been passed 2140 * to the socket layer. Only things in queue. 2141 */ 2142 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue + 2143 stcb->asoc.cnt_on_reasm_queue + 2144 stcb->asoc.cnt_on_all_streams); 2145 2146 2147 sstat->sstat_instrms = stcb->asoc.streamincnt; 2148 sstat->sstat_outstrms = stcb->asoc.streamoutcnt; 2149 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc); 2150 memcpy(&sstat->sstat_primary.spinfo_address, 2151 rtcache_getdst(&stcb->asoc.primary_destination->ro), 2152 (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len); 2153 net = stcb->asoc.primary_destination; 2154 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport; 2155 /* 2156 * Again the user can get info from sctp_constants.h 2157 * for what the state of the network is. 2158 */ 2159 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK; 2160 sstat->sstat_primary.spinfo_cwnd = net->cwnd; 2161 sstat->sstat_primary.spinfo_srtt = net->lastsa; 2162 sstat->sstat_primary.spinfo_rto = net->RTO; 2163 sstat->sstat_primary.spinfo_mtu = net->mtu; 2164 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb); 2165 SCTP_TCB_UNLOCK(stcb); 2166 sopt->sopt_size = sizeof(*sstat); 2167 } 2168 break; 2169 case SCTP_RTOINFO: 2170 { 2171 struct sctp_rtoinfo *srto; 2172 #ifdef SCTP_DEBUG 2173 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2174 printf("RTO Info\n"); 2175 } 2176 #endif /* SCTP_DEBUG */ 2177 if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) { 2178 error = EINVAL; 2179 break; 2180 } 2181 srto = sopt->sopt_data; 2182 if (srto->srto_assoc_id == 0) { 2183 /* Endpoint only please */ 2184 SCTP_INP_RLOCK(inp); 2185 srto->srto_initial = inp->sctp_ep.initial_rto; 2186 srto->srto_max = inp->sctp_ep.sctp_maxrto; 2187 srto->srto_min = inp->sctp_ep.sctp_minrto; 2188 SCTP_INP_RUNLOCK(inp); 2189 break; 2190 } 2191 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2192 SCTP_INP_RLOCK(inp); 2193 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2194 if (stcb) { 2195 SCTP_TCB_LOCK(stcb); 2196 } 2197 SCTP_INP_RUNLOCK(inp); 2198 } else 2199 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id); 2200 2201 if (stcb == NULL) { 2202 error = EINVAL; 2203 break; 2204 } 2205 srto->srto_initial = stcb->asoc.initial_rto; 2206 srto->srto_max = stcb->asoc.maxrto; 2207 srto->srto_min = stcb->asoc.minrto; 2208 SCTP_TCB_UNLOCK(stcb); 2209 sopt->sopt_size = sizeof(*srto); 2210 } 2211 break; 2212 case SCTP_ASSOCINFO: 2213 { 2214 struct sctp_assocparams *sasoc; 2215 #ifdef SCTP_DEBUG 2216 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2217 printf("Associnfo\n"); 2218 } 2219 #endif /* SCTP_DEBUG */ 2220 if (sopt->sopt_size < sizeof(struct sctp_assocparams)) { 2221 error = EINVAL; 2222 break; 2223 } 2224 sasoc = sopt->sopt_data; 2225 stcb = NULL; 2226 2227 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2228 SCTP_INP_RLOCK(inp); 2229 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2230 if (stcb) { 2231 SCTP_TCB_LOCK(stcb); 2232 } 2233 SCTP_INP_RUNLOCK(inp); 2234 } 2235 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) { 2236 stcb = sctp_findassociation_ep_asocid(inp, 2237 sasoc->sasoc_assoc_id); 2238 if (stcb == NULL) { 2239 error = ENOENT; 2240 break; 2241 } 2242 } else { 2243 stcb = NULL; 2244 } 2245 2246 if (stcb) { 2247 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times; 2248 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets; 2249 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd; 2250 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd; 2251 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life; 2252 SCTP_TCB_UNLOCK(stcb); 2253 } else { 2254 SCTP_INP_RLOCK(inp); 2255 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times; 2256 sasoc->sasoc_number_peer_destinations = 0; 2257 sasoc->sasoc_peer_rwnd = 0; 2258 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv); 2259 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life; 2260 SCTP_INP_RUNLOCK(inp); 2261 } 2262 sopt->sopt_size = sizeof(*sasoc); 2263 } 2264 break; 2265 case SCTP_DEFAULT_SEND_PARAM: 2266 { 2267 struct sctp_sndrcvinfo *s_info; 2268 2269 if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) { 2270 error = EINVAL; 2271 break; 2272 } 2273 s_info = sopt->sopt_data; 2274 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2275 SCTP_INP_RLOCK(inp); 2276 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2277 if (stcb) { 2278 SCTP_TCB_LOCK(stcb); 2279 } 2280 SCTP_INP_RUNLOCK(inp); 2281 } else 2282 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id); 2283 2284 if (stcb == NULL) { 2285 error = ENOENT; 2286 break; 2287 } 2288 /* Copy it out */ 2289 *s_info = stcb->asoc.def_send; 2290 SCTP_TCB_UNLOCK(stcb); 2291 sopt->sopt_size = sizeof(*s_info); 2292 } 2293 case SCTP_INITMSG: 2294 { 2295 struct sctp_initmsg *sinit; 2296 #ifdef SCTP_DEBUG 2297 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2298 printf("initmsg\n"); 2299 } 2300 #endif /* SCTP_DEBUG */ 2301 if (sopt->sopt_size < sizeof(struct sctp_initmsg)) { 2302 error = EINVAL; 2303 break; 2304 } 2305 sinit = sopt->sopt_data; 2306 SCTP_INP_RLOCK(inp); 2307 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count; 2308 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome; 2309 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times; 2310 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max; 2311 SCTP_INP_RUNLOCK(inp); 2312 sopt->sopt_size = sizeof(*sinit); 2313 } 2314 break; 2315 case SCTP_PRIMARY_ADDR: 2316 /* we allow a "get" operation on this */ 2317 { 2318 struct sctp_setprim *ssp; 2319 2320 #ifdef SCTP_DEBUG 2321 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2322 printf("setprimary\n"); 2323 } 2324 #endif /* SCTP_DEBUG */ 2325 if (sopt->sopt_size < sizeof(struct sctp_setprim)) { 2326 error = EINVAL; 2327 break; 2328 } 2329 ssp = sopt->sopt_data; 2330 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2331 SCTP_INP_RLOCK(inp); 2332 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2333 if (stcb) { 2334 SCTP_TCB_LOCK(stcb); 2335 } 2336 SCTP_INP_RUNLOCK(inp); 2337 } else { 2338 stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id); 2339 if (stcb == NULL) { 2340 /* one last shot, try it by the address in */ 2341 struct sctp_nets *net; 2342 2343 SCTP_INP_WLOCK(inp); 2344 SCTP_INP_INCR_REF(inp); 2345 SCTP_INP_WUNLOCK(inp); 2346 stcb = sctp_findassociation_ep_addr(&inp, 2347 (struct sockaddr *)&ssp->ssp_addr, 2348 &net, NULL, NULL); 2349 if (stcb == NULL) { 2350 SCTP_INP_WLOCK(inp); 2351 SCTP_INP_DECR_REF(inp); 2352 SCTP_INP_WUNLOCK(inp); 2353 } 2354 } 2355 if (stcb == NULL) { 2356 error = EINVAL; 2357 break; 2358 } 2359 } 2360 /* simply copy out the sockaddr_storage... */ 2361 memcpy(&ssp->ssp_addr, 2362 rtcache_getdst(&stcb->asoc.primary_destination->ro), 2363 (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len); 2364 SCTP_TCB_UNLOCK(stcb); 2365 sopt->sopt_size = sizeof(*ssp); 2366 } 2367 break; 2368 default: 2369 error = ENOPROTOOPT; 2370 sopt->sopt_size = 0; 2371 break; 2372 } /* end switch (sopt->sopt_name) */ 2373 return (error); 2374 } 2375 2376 static int 2377 sctp_optsset(struct socket *so, struct sockopt *sopt) 2378 { 2379 int error, *mopt, set_opt; 2380 struct sctp_tcb *stcb = NULL; 2381 struct sctp_inpcb *inp; 2382 2383 if (sopt->sopt_data == NULL) { 2384 #ifdef SCTP_DEBUG 2385 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) { 2386 printf("optsset:MP is NULL EINVAL\n"); 2387 } 2388 #endif /* SCTP_DEBUG */ 2389 return (EINVAL); 2390 } 2391 inp = (struct sctp_inpcb *)so->so_pcb; 2392 if (inp == 0) 2393 return EINVAL; 2394 2395 error = 0; 2396 switch (sopt->sopt_name) { 2397 case SCTP_NODELAY: 2398 case SCTP_AUTOCLOSE: 2399 case SCTP_AUTO_ASCONF: 2400 case SCTP_DISABLE_FRAGMENTS: 2401 case SCTP_I_WANT_MAPPED_V4_ADDR: 2402 /* copy in the option value */ 2403 if (sopt->sopt_size < sizeof(int)) { 2404 error = EINVAL; 2405 break; 2406 } 2407 mopt = sopt->sopt_data; 2408 set_opt = 0; 2409 if (error) 2410 break; 2411 switch (sopt->sopt_name) { 2412 case SCTP_DISABLE_FRAGMENTS: 2413 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT; 2414 break; 2415 case SCTP_AUTO_ASCONF: 2416 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF; 2417 break; 2418 2419 case SCTP_I_WANT_MAPPED_V4_ADDR: 2420 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2421 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4; 2422 } else { 2423 return (EINVAL); 2424 } 2425 break; 2426 case SCTP_NODELAY: 2427 set_opt = SCTP_PCB_FLAGS_NODELAY; 2428 break; 2429 case SCTP_AUTOCLOSE: 2430 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE; 2431 /* 2432 * The value is in ticks. 2433 * Note this does not effect old associations, only 2434 * new ones. 2435 */ 2436 inp->sctp_ep.auto_close_time = (*mopt * hz); 2437 break; 2438 } 2439 SCTP_INP_WLOCK(inp); 2440 if (*mopt != 0) { 2441 inp->sctp_flags |= set_opt; 2442 } else { 2443 inp->sctp_flags &= ~set_opt; 2444 } 2445 SCTP_INP_WUNLOCK(inp); 2446 break; 2447 case SCTP_MY_PUBLIC_KEY: /* set my public key */ 2448 case SCTP_SET_AUTH_CHUNKS: /* set the authenticated chunks required */ 2449 case SCTP_SET_AUTH_SECRET: /* set the actual secret for the endpoint */ 2450 /* not supported yet and until we refine the draft */ 2451 error = EOPNOTSUPP; 2452 break; 2453 2454 case SCTP_CLR_STAT_LOG: 2455 #ifdef SCTP_STAT_LOGGING 2456 sctp_clr_stat_log(); 2457 #else 2458 error = EOPNOTSUPP; 2459 #endif 2460 break; 2461 case SCTP_DELAYED_ACK_TIME: 2462 { 2463 int32_t *tm; 2464 if (sopt->sopt_size < sizeof(int32_t)) { 2465 error = EINVAL; 2466 break; 2467 } 2468 tm = sopt->sopt_data; 2469 2470 if ((*tm < 10) || (*tm > 500)) { 2471 /* can't be smaller than 10ms */ 2472 /* MUST NOT be larger than 500ms */ 2473 error = EINVAL; 2474 break; 2475 } 2476 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm); 2477 } 2478 break; 2479 case SCTP_RESET_STREAMS: 2480 { 2481 struct sctp_stream_reset *strrst; 2482 uint8_t two_way, not_peer; 2483 2484 if (sopt->sopt_size < sizeof(struct sctp_stream_reset)) { 2485 error = EINVAL; 2486 break; 2487 } 2488 strrst = sopt->sopt_data; 2489 2490 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2491 SCTP_INP_RLOCK(inp); 2492 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2493 if (stcb) { 2494 SCTP_TCB_LOCK(stcb); 2495 } 2496 SCTP_INP_RUNLOCK(inp); 2497 } else 2498 stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id); 2499 if (stcb == NULL) { 2500 error = ENOENT; 2501 break; 2502 } 2503 if (stcb->asoc.peer_supports_strreset == 0) { 2504 /* Peer does not support it, 2505 * we return protocol not supported since 2506 * this is true for this feature and this 2507 * peer, not the socket request in general. 2508 */ 2509 error = EPROTONOSUPPORT; 2510 SCTP_TCB_UNLOCK(stcb); 2511 break; 2512 } 2513 2514 /* Having re-thought this code I added as I write the I-D there 2515 * is NO need for it. The peer, if we are requesting a stream-reset 2516 * will send a request to us but will itself do what we do, take 2517 * and copy off the "reset information" we send and queue TSN's 2518 * larger than the send-next in our response message. Thus they 2519 * will handle it. 2520 */ 2521 /* if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/ 2522 /* Must have all sending data ack'd before we 2523 * start this procedure. This is a bit restrictive 2524 * and we SHOULD work on changing this so ONLY the 2525 * streams being RESET get held up. So, a reset-all 2526 * would require this.. but a reset specific just 2527 * needs to be sure that the ones being reset have 2528 * nothing on the send_queue. For now we will 2529 * skip this more detailed method and do a course 2530 * way.. i.e. nothing pending ... for future FIX ME! 2531 */ 2532 /* error = EBUSY;*/ 2533 /* break;*/ 2534 /* }*/ 2535 2536 if (stcb->asoc.stream_reset_outstanding) { 2537 error = EALREADY; 2538 SCTP_TCB_UNLOCK(stcb); 2539 break; 2540 } 2541 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) { 2542 two_way = 0; 2543 not_peer = 0; 2544 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) { 2545 two_way = 1; 2546 not_peer = 1; 2547 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) { 2548 two_way = 1; 2549 not_peer = 0; 2550 } else { 2551 error = EINVAL; 2552 SCTP_TCB_UNLOCK(stcb); 2553 break; 2554 } 2555 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams, 2556 strrst->strrst_list, two_way, not_peer); 2557 sctp_chunk_output(inp, stcb, 12); 2558 SCTP_TCB_UNLOCK(stcb); 2559 2560 } 2561 break; 2562 case SCTP_RESET_PEGS: 2563 memset(sctp_pegs, 0, sizeof(sctp_pegs)); 2564 error = 0; 2565 break; 2566 case SCTP_CONNECT_X_COMPLETE: 2567 { 2568 struct sockaddr *sa; 2569 struct sctp_nets *net; 2570 if (sopt->sopt_size < sizeof(struct sockaddr_in)) { 2571 error = EINVAL; 2572 break; 2573 } 2574 sa = sopt->sopt_data; 2575 /* find tcb */ 2576 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2577 SCTP_INP_RLOCK(inp); 2578 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2579 if (stcb) { 2580 SCTP_TCB_LOCK(stcb); 2581 net = sctp_findnet(stcb, sa); 2582 } 2583 SCTP_INP_RUNLOCK(inp); 2584 } else { 2585 SCTP_INP_WLOCK(inp); 2586 SCTP_INP_INCR_REF(inp); 2587 SCTP_INP_WUNLOCK(inp); 2588 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL); 2589 if (stcb == NULL) { 2590 SCTP_INP_WLOCK(inp); 2591 SCTP_INP_DECR_REF(inp); 2592 SCTP_INP_WUNLOCK(inp); 2593 } 2594 } 2595 2596 if (stcb == NULL) { 2597 error = ENOENT; 2598 break; 2599 } 2600 if (stcb->asoc.delayed_connection == 1) { 2601 stcb->asoc.delayed_connection = 0; 2602 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 2603 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination); 2604 sctp_send_initiate(inp, stcb); 2605 } else { 2606 /* already expired or did not use delayed connectx */ 2607 error = EALREADY; 2608 } 2609 SCTP_TCB_UNLOCK(stcb); 2610 } 2611 break; 2612 case SCTP_MAXBURST: 2613 { 2614 u_int8_t *burst; 2615 SCTP_INP_WLOCK(inp); 2616 burst = sopt->sopt_data; 2617 if (*burst) { 2618 inp->sctp_ep.max_burst = *burst; 2619 } 2620 SCTP_INP_WUNLOCK(inp); 2621 } 2622 break; 2623 case SCTP_MAXSEG: 2624 { 2625 u_int32_t *segsize; 2626 int ovh; 2627 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2628 ovh = SCTP_MED_OVERHEAD; 2629 } else { 2630 ovh = SCTP_MED_V4_OVERHEAD; 2631 } 2632 segsize = sopt->sopt_data; 2633 if (*segsize < 1) { 2634 error = EINVAL; 2635 break; 2636 } 2637 SCTP_INP_WLOCK(inp); 2638 inp->sctp_frag_point = (*segsize+ovh); 2639 if (inp->sctp_frag_point < MHLEN) { 2640 inp->sctp_frag_point = MHLEN; 2641 } 2642 SCTP_INP_WUNLOCK(inp); 2643 } 2644 break; 2645 case SCTP_SET_DEBUG_LEVEL: 2646 #ifdef SCTP_DEBUG 2647 { 2648 u_int32_t *level; 2649 if (sopt->sopt_size < sizeof(u_int32_t)) { 2650 error = EINVAL; 2651 break; 2652 } 2653 level = sopt->sopt_data; 2654 error = 0; 2655 sctp_debug_on = (*level & (SCTP_DEBUG_ALL | 2656 SCTP_DEBUG_NOISY)); 2657 printf("SETTING DEBUG LEVEL to %x\n", 2658 (u_int)sctp_debug_on); 2659 2660 } 2661 #else 2662 error = EOPNOTSUPP; 2663 #endif /* SCTP_DEBUG */ 2664 break; 2665 case SCTP_EVENTS: 2666 { 2667 struct sctp_event_subscribe *events; 2668 if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) { 2669 error = EINVAL; 2670 break; 2671 } 2672 SCTP_INP_WLOCK(inp); 2673 events = sopt->sopt_data; 2674 if (events->sctp_data_io_event) { 2675 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT; 2676 } else { 2677 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT; 2678 } 2679 2680 if (events->sctp_association_event) { 2681 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT; 2682 } else { 2683 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT; 2684 } 2685 2686 if (events->sctp_address_event) { 2687 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT; 2688 } else { 2689 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT; 2690 } 2691 2692 if (events->sctp_send_failure_event) { 2693 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT; 2694 } else { 2695 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT; 2696 } 2697 2698 if (events->sctp_peer_error_event) { 2699 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR; 2700 } else { 2701 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR; 2702 } 2703 2704 if (events->sctp_shutdown_event) { 2705 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT; 2706 } else { 2707 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT; 2708 } 2709 2710 if (events->sctp_partial_delivery_event) { 2711 inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT; 2712 } else { 2713 inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT; 2714 } 2715 2716 if (events->sctp_adaption_layer_event) { 2717 inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT; 2718 } else { 2719 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT; 2720 } 2721 2722 if (events->sctp_stream_reset_events) { 2723 inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT; 2724 } else { 2725 inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT; 2726 } 2727 SCTP_INP_WUNLOCK(inp); 2728 } 2729 break; 2730 2731 case SCTP_ADAPTION_LAYER: 2732 { 2733 struct sctp_setadaption *adap_bits; 2734 if (sopt->sopt_size < sizeof(struct sctp_setadaption)) { 2735 error = EINVAL; 2736 break; 2737 } 2738 SCTP_INP_WLOCK(inp); 2739 adap_bits = sopt->sopt_data; 2740 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind; 2741 SCTP_INP_WUNLOCK(inp); 2742 } 2743 break; 2744 case SCTP_SET_INITIAL_DBG_SEQ: 2745 { 2746 u_int32_t *vvv; 2747 if (sopt->sopt_size < sizeof(u_int32_t)) { 2748 error = EINVAL; 2749 break; 2750 } 2751 SCTP_INP_WLOCK(inp); 2752 vvv = sopt->sopt_data; 2753 inp->sctp_ep.initial_sequence_debug = *vvv; 2754 SCTP_INP_WUNLOCK(inp); 2755 } 2756 break; 2757 case SCTP_DEFAULT_SEND_PARAM: 2758 { 2759 struct sctp_sndrcvinfo *s_info; 2760 2761 if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) { 2762 error = EINVAL; 2763 break; 2764 } 2765 s_info = sopt->sopt_data; 2766 2767 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2768 SCTP_INP_RLOCK(inp); 2769 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2770 if (stcb) { 2771 SCTP_TCB_LOCK(stcb); 2772 } 2773 SCTP_INP_RUNLOCK(inp); 2774 } else 2775 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id); 2776 2777 if (stcb == NULL) { 2778 error = ENOENT; 2779 break; 2780 } 2781 /* Validate things */ 2782 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) { 2783 SCTP_TCB_UNLOCK(stcb); 2784 error = EINVAL; 2785 break; 2786 } 2787 /* Mask off the flags that are allowed */ 2788 s_info->sinfo_flags = (s_info->sinfo_flags & 2789 (SCTP_UNORDERED | SCTP_ADDR_OVER | 2790 SCTP_PR_SCTP_TTL | SCTP_PR_SCTP_BUF)); 2791 /* Copy it in */ 2792 stcb->asoc.def_send = *s_info; 2793 SCTP_TCB_UNLOCK(stcb); 2794 } 2795 break; 2796 case SCTP_PEER_ADDR_PARAMS: 2797 { 2798 struct sctp_paddrparams *paddrp; 2799 struct sctp_nets *net; 2800 if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) { 2801 error = EINVAL; 2802 break; 2803 } 2804 paddrp = sopt->sopt_data; 2805 net = NULL; 2806 if (paddrp->spp_assoc_id) { 2807 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2808 SCTP_INP_RLOCK(inp); 2809 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2810 if (stcb) { 2811 SCTP_TCB_LOCK(stcb); 2812 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 2813 } 2814 SCTP_INP_RUNLOCK(inp); 2815 } else 2816 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id); 2817 if (stcb == NULL) { 2818 error = ENOENT; 2819 break; 2820 } 2821 2822 } 2823 if ((stcb == NULL) && 2824 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) || 2825 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) { 2826 /* Lookup via address */ 2827 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2828 SCTP_INP_RLOCK(inp); 2829 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2830 if (stcb) { 2831 SCTP_TCB_LOCK(stcb); 2832 net = sctp_findnet(stcb, 2833 (struct sockaddr *)&paddrp->spp_address); 2834 } 2835 SCTP_INP_RUNLOCK(inp); 2836 } else { 2837 SCTP_INP_WLOCK(inp); 2838 SCTP_INP_INCR_REF(inp); 2839 SCTP_INP_WUNLOCK(inp); 2840 stcb = sctp_findassociation_ep_addr(&inp, 2841 (struct sockaddr *)&paddrp->spp_address, 2842 &net, NULL, NULL); 2843 if (stcb == NULL) { 2844 SCTP_INP_WLOCK(inp); 2845 SCTP_INP_DECR_REF(inp); 2846 SCTP_INP_WUNLOCK(inp); 2847 } 2848 } 2849 } else { 2850 /* Effects the Endpoint */ 2851 stcb = NULL; 2852 } 2853 if (stcb) { 2854 /* Applies to the specific association */ 2855 if (paddrp->spp_pathmaxrxt) { 2856 if (net) { 2857 if (paddrp->spp_pathmaxrxt) 2858 net->failure_threshold = paddrp->spp_pathmaxrxt; 2859 } else { 2860 if (paddrp->spp_pathmaxrxt) 2861 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt; 2862 } 2863 } 2864 if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) { 2865 /* Just a set */ 2866 int old; 2867 if (net) { 2868 net->dest_state &= ~SCTP_ADDR_NOHB; 2869 } else { 2870 old = stcb->asoc.heart_beat_delay; 2871 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval; 2872 if (old == 0) { 2873 /* Turn back on the timer */ 2874 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 2875 } 2876 } 2877 } else if (paddrp->spp_hbinterval == 0xffffffff) { 2878 /* on demand HB */ 2879 sctp_send_hb(stcb, 1, net); 2880 } else { 2881 if (net == NULL) { 2882 /* off on association */ 2883 if (stcb->asoc.heart_beat_delay) { 2884 int cnt_of_unconf = 0; 2885 struct sctp_nets *lnet; 2886 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { 2887 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) { 2888 cnt_of_unconf++; 2889 } 2890 } 2891 /* stop the timer ONLY if we have no unconfirmed addresses 2892 */ 2893 if (cnt_of_unconf == 0) 2894 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 2895 } 2896 stcb->asoc.heart_beat_delay = 0; 2897 } else { 2898 net->dest_state |= SCTP_ADDR_NOHB; 2899 } 2900 } 2901 SCTP_TCB_UNLOCK(stcb); 2902 } else { 2903 /* Use endpoint defaults */ 2904 SCTP_INP_WLOCK(inp); 2905 if (paddrp->spp_pathmaxrxt) 2906 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt; 2907 if (paddrp->spp_hbinterval != SCTP_ISSUE_HB) 2908 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval; 2909 SCTP_INP_WUNLOCK(inp); 2910 } 2911 } 2912 break; 2913 case SCTP_RTOINFO: 2914 { 2915 struct sctp_rtoinfo *srto; 2916 if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) { 2917 error = EINVAL; 2918 break; 2919 } 2920 srto = sopt->sopt_data; 2921 if (srto->srto_assoc_id == 0) { 2922 SCTP_INP_WLOCK(inp); 2923 /* If we have a null asoc, its default for the endpoint */ 2924 if (srto->srto_initial > 10) 2925 inp->sctp_ep.initial_rto = srto->srto_initial; 2926 if (srto->srto_max > 10) 2927 inp->sctp_ep.sctp_maxrto = srto->srto_max; 2928 if (srto->srto_min > 10) 2929 inp->sctp_ep.sctp_minrto = srto->srto_min; 2930 SCTP_INP_WUNLOCK(inp); 2931 break; 2932 } 2933 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2934 SCTP_INP_RLOCK(inp); 2935 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2936 if (stcb) { 2937 SCTP_TCB_LOCK(stcb); 2938 } 2939 SCTP_INP_RUNLOCK(inp); 2940 } else 2941 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id); 2942 if (stcb == NULL) { 2943 error = EINVAL; 2944 break; 2945 } 2946 /* Set in ms we hope :-) */ 2947 if (srto->srto_initial > 10) 2948 stcb->asoc.initial_rto = srto->srto_initial; 2949 if (srto->srto_max > 10) 2950 stcb->asoc.maxrto = srto->srto_max; 2951 if (srto->srto_min > 10) 2952 stcb->asoc.minrto = srto->srto_min; 2953 SCTP_TCB_UNLOCK(stcb); 2954 } 2955 break; 2956 case SCTP_ASSOCINFO: 2957 { 2958 struct sctp_assocparams *sasoc; 2959 2960 if (sopt->sopt_size < sizeof(struct sctp_assocparams)) { 2961 error = EINVAL; 2962 break; 2963 } 2964 sasoc = sopt->sopt_data; 2965 if (sasoc->sasoc_assoc_id) { 2966 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2967 SCTP_INP_RLOCK(inp); 2968 stcb = LIST_FIRST(&inp->sctp_asoc_list); 2969 if (stcb) { 2970 SCTP_TCB_LOCK(stcb); 2971 } 2972 SCTP_INP_RUNLOCK(inp); 2973 } else 2974 stcb = sctp_findassociation_ep_asocid(inp, 2975 sasoc->sasoc_assoc_id); 2976 if (stcb == NULL) { 2977 error = ENOENT; 2978 break; 2979 } 2980 2981 } else { 2982 stcb = NULL; 2983 } 2984 if (stcb) { 2985 if (sasoc->sasoc_asocmaxrxt) 2986 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt; 2987 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets; 2988 sasoc->sasoc_peer_rwnd = 0; 2989 sasoc->sasoc_local_rwnd = 0; 2990 if (stcb->asoc.cookie_life) 2991 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life; 2992 SCTP_TCB_UNLOCK(stcb); 2993 } else { 2994 SCTP_INP_WLOCK(inp); 2995 if (sasoc->sasoc_asocmaxrxt) 2996 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt; 2997 sasoc->sasoc_number_peer_destinations = 0; 2998 sasoc->sasoc_peer_rwnd = 0; 2999 sasoc->sasoc_local_rwnd = 0; 3000 if (sasoc->sasoc_cookie_life) 3001 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life; 3002 SCTP_INP_WUNLOCK(inp); 3003 } 3004 } 3005 break; 3006 case SCTP_INITMSG: 3007 { 3008 struct sctp_initmsg *sinit; 3009 3010 if (sopt->sopt_size < sizeof(struct sctp_initmsg)) { 3011 error = EINVAL; 3012 break; 3013 } 3014 sinit = sopt->sopt_data; 3015 SCTP_INP_WLOCK(inp); 3016 if (sinit->sinit_num_ostreams) 3017 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams; 3018 3019 if (sinit->sinit_max_instreams) 3020 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams; 3021 3022 if (sinit->sinit_max_attempts) 3023 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts; 3024 3025 if (sinit->sinit_max_init_timeo > 10) 3026 /* We must be at least a 100ms (we set in ticks) */ 3027 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo; 3028 SCTP_INP_WUNLOCK(inp); 3029 } 3030 break; 3031 case SCTP_PRIMARY_ADDR: 3032 { 3033 struct sctp_setprim *spa; 3034 struct sctp_nets *net, *lnet; 3035 if (sopt->sopt_size < sizeof(struct sctp_setprim)) { 3036 error = EINVAL; 3037 break; 3038 } 3039 spa = sopt->sopt_data; 3040 3041 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 3042 SCTP_INP_RLOCK(inp); 3043 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3044 if (stcb) { 3045 SCTP_TCB_LOCK(stcb); 3046 } else { 3047 error = EINVAL; 3048 break; 3049 } 3050 SCTP_INP_RUNLOCK(inp); 3051 } else 3052 stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id); 3053 if (stcb == NULL) { 3054 /* One last shot */ 3055 SCTP_INP_WLOCK(inp); 3056 SCTP_INP_INCR_REF(inp); 3057 SCTP_INP_WUNLOCK(inp); 3058 stcb = sctp_findassociation_ep_addr(&inp, 3059 (struct sockaddr *)&spa->ssp_addr, 3060 &net, NULL, NULL); 3061 if (stcb == NULL) { 3062 SCTP_INP_WLOCK(inp); 3063 SCTP_INP_DECR_REF(inp); 3064 SCTP_INP_WUNLOCK(inp); 3065 error = EINVAL; 3066 break; 3067 } 3068 } else { 3069 /* find the net, associd or connected lookup type */ 3070 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr); 3071 if (net == NULL) { 3072 SCTP_TCB_UNLOCK(stcb); 3073 error = EINVAL; 3074 break; 3075 } 3076 } 3077 if ((net != stcb->asoc.primary_destination) && 3078 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) { 3079 /* Ok we need to set it */ 3080 lnet = stcb->asoc.primary_destination; 3081 lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq; 3082 if (sctp_set_primary_addr(stcb, 3083 (struct sockaddr *)NULL, 3084 net) == 0) { 3085 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) { 3086 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH; 3087 } 3088 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY; 3089 } 3090 } 3091 SCTP_TCB_UNLOCK(stcb); 3092 } 3093 break; 3094 3095 case SCTP_SET_PEER_PRIMARY_ADDR: 3096 { 3097 struct sctp_setpeerprim *sspp; 3098 if (sopt->sopt_size < sizeof(struct sctp_setpeerprim)) { 3099 error = EINVAL; 3100 break; 3101 } 3102 sspp = sopt->sopt_data; 3103 3104 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 3105 SCTP_INP_RLOCK(inp); 3106 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3107 if (stcb) { 3108 SCTP_TCB_UNLOCK(stcb); 3109 } 3110 SCTP_INP_RUNLOCK(inp); 3111 } else 3112 stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id); 3113 if (stcb == NULL) { 3114 error = EINVAL; 3115 break; 3116 } 3117 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) { 3118 error = EINVAL; 3119 } 3120 SCTP_TCB_UNLOCK(stcb); 3121 } 3122 break; 3123 case SCTP_BINDX_ADD_ADDR: 3124 { 3125 struct sctp_getaddresses *addrs; 3126 struct sockaddr *addr_touse; 3127 struct sockaddr_in sin; 3128 /* see if we're bound all already! */ 3129 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 3130 error = EINVAL; 3131 break; 3132 } 3133 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) { 3134 error = EINVAL; 3135 break; 3136 } 3137 addrs = sopt->sopt_data; 3138 addr_touse = addrs->addr; 3139 if (addrs->addr->sa_family == AF_INET6) { 3140 struct sockaddr_in6 *sin6; 3141 sin6 = (struct sockaddr_in6 *)addr_touse; 3142 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 3143 in6_sin6_2_sin(&sin, sin6); 3144 addr_touse = (struct sockaddr *)&sin; 3145 } 3146 } 3147 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 3148 error = sctp_inpcb_bind(so, addr_touse, curlwp); 3149 break; 3150 } 3151 /* No locks required here since bind and mgmt_ep_sa all 3152 * do their own locking. If we do something for the FIX: 3153 * below we may need to lock in that case. 3154 */ 3155 if (addrs->sget_assoc_id == 0) { 3156 /* add the address */ 3157 struct sctp_inpcb *lep; 3158 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport; 3159 lep = sctp_pcb_findep(addr_touse, 1, 0); 3160 if (lep != NULL) { 3161 /* We must decrement the refcount 3162 * since we have the ep already and 3163 * are binding. No remove going on 3164 * here. 3165 */ 3166 SCTP_INP_WLOCK(inp); 3167 SCTP_INP_DECR_REF(inp); 3168 SCTP_INP_WUNLOCK(inp); 3169 } 3170 if (lep == inp) { 3171 /* already bound to it.. ok */ 3172 break; 3173 } else if (lep == NULL) { 3174 ((struct sockaddr_in *)addr_touse)->sin_port = 0; 3175 error = sctp_addr_mgmt_ep_sa(inp, addr_touse, 3176 SCTP_ADD_IP_ADDRESS); 3177 } else { 3178 error = EADDRNOTAVAIL; 3179 } 3180 if (error) 3181 break; 3182 3183 } else { 3184 /* FIX: decide whether we allow assoc based bindx */ 3185 } 3186 } 3187 break; 3188 case SCTP_BINDX_REM_ADDR: 3189 { 3190 struct sctp_getaddresses *addrs; 3191 struct sockaddr *addr_touse; 3192 struct sockaddr_in sin; 3193 /* see if we're bound all already! */ 3194 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 3195 error = EINVAL; 3196 break; 3197 } 3198 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) { 3199 error = EINVAL; 3200 break; 3201 } 3202 addrs = sopt->sopt_data; 3203 addr_touse = addrs->addr; 3204 if (addrs->addr->sa_family == AF_INET6) { 3205 struct sockaddr_in6 *sin6; 3206 sin6 = (struct sockaddr_in6 *)addr_touse; 3207 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 3208 in6_sin6_2_sin(&sin, sin6); 3209 addr_touse = (struct sockaddr *)&sin; 3210 } 3211 } 3212 /* No lock required mgmt_ep_sa does its own locking. If 3213 * the FIX: below is ever changed we may need to 3214 * lock before calling association level binding. 3215 */ 3216 if (addrs->sget_assoc_id == 0) { 3217 /* delete the address */ 3218 sctp_addr_mgmt_ep_sa(inp, addr_touse, 3219 SCTP_DEL_IP_ADDRESS); 3220 } else { 3221 /* FIX: decide whether we allow assoc based bindx */ 3222 } 3223 } 3224 break; 3225 default: 3226 error = ENOPROTOOPT; 3227 break; 3228 } /* end switch (opt) */ 3229 return (error); 3230 } 3231 3232 int 3233 sctp_ctloutput(int op, struct socket *so, struct sockopt *sopt) 3234 { 3235 int s, error = 0; 3236 struct inpcb *inp; 3237 #ifdef INET6 3238 struct in6pcb *in6p; 3239 #endif 3240 int family; /* family of the socket */ 3241 3242 family = so->so_proto->pr_domain->dom_family; 3243 3244 s = splsoftnet(); 3245 switch (family) { 3246 case PF_INET: 3247 inp = sotoinpcb(so); 3248 #ifdef INET6 3249 in6p = NULL; 3250 #endif 3251 break; 3252 #ifdef INET6 3253 case PF_INET6: 3254 inp = NULL; 3255 in6p = sotoin6pcb(so); 3256 break; 3257 #endif 3258 default: 3259 splx(s); 3260 return EAFNOSUPPORT; 3261 } 3262 #ifndef INET6 3263 if (inp == NULL) 3264 #else 3265 if (inp == NULL && in6p == NULL) 3266 #endif 3267 { 3268 splx(s); 3269 return (ECONNRESET); 3270 } 3271 if (sopt->sopt_level != IPPROTO_SCTP) { 3272 switch (family) { 3273 case PF_INET: 3274 error = ip_ctloutput(op, so, sopt); 3275 break; 3276 #ifdef INET6 3277 case PF_INET6: 3278 error = ip6_ctloutput(op, so, sopt); 3279 break; 3280 #endif 3281 } 3282 splx(s); 3283 return (error); 3284 } 3285 /* Ok if we reach here it is a SCTP option we hope */ 3286 if (op == PRCO_SETOPT) { 3287 error = sctp_optsset(so, sopt); 3288 } else if (op == PRCO_GETOPT) { 3289 error = sctp_optsget(so, sopt); 3290 } else { 3291 error = EINVAL; 3292 } 3293 splx(s); 3294 return (error); 3295 } 3296 3297 static int 3298 sctp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 3299 { 3300 int error = 0; 3301 struct sctp_inpcb *inp; 3302 struct sctp_tcb *stcb; 3303 3304 KASSERT(solocked(so)); 3305 #ifdef SCTP_DEBUG 3306 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 3307 printf("Connect called in SCTP to "); 3308 sctp_print_address(nam); 3309 printf("Port %d\n", ntohs(((struct sockaddr_in *)nam)->sin_port)); 3310 } 3311 #endif /* SCTP_DEBUG */ 3312 inp = (struct sctp_inpcb *)so->so_pcb; 3313 if (inp == 0) { 3314 /* I made the same as TCP since we are not setup? */ 3315 return (ECONNRESET); 3316 } 3317 SCTP_ASOC_CREATE_LOCK(inp); 3318 #ifdef SCTP_DEBUG 3319 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 3320 printf("After ASOC lock\n"); 3321 } 3322 #endif /* SCTP_DEBUG */ 3323 SCTP_INP_WLOCK(inp); 3324 #ifdef SCTP_DEBUG 3325 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 3326 printf("After INP_WLOCK lock\n"); 3327 } 3328 #endif /* SCTP_DEBUG */ 3329 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 3330 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 3331 /* Should I really unlock ? */ 3332 SCTP_INP_WUNLOCK(inp); 3333 SCTP_ASOC_CREATE_UNLOCK(inp); 3334 return (EFAULT); 3335 } 3336 #ifdef INET6 3337 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 3338 (nam->sa_family == AF_INET6)) { 3339 SCTP_INP_WUNLOCK(inp); 3340 SCTP_ASOC_CREATE_UNLOCK(inp); 3341 return (EINVAL); 3342 } 3343 #endif /* INET6 */ 3344 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 3345 SCTP_PCB_FLAGS_UNBOUND) { 3346 /* Bind a ephemeral port */ 3347 SCTP_INP_WUNLOCK(inp); 3348 error = sctp_inpcb_bind(so, NULL, l); 3349 if (error) { 3350 SCTP_ASOC_CREATE_UNLOCK(inp); 3351 return (error); 3352 } 3353 SCTP_INP_WLOCK(inp); 3354 } 3355 #ifdef SCTP_DEBUG 3356 if (sctp_debug_on & SCTP_DEBUG_PCB1) { 3357 printf("After bind\n"); 3358 } 3359 #endif /* SCTP_DEBUG */ 3360 /* Now do we connect? */ 3361 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 3362 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 3363 /* We are already connected AND the TCP model */ 3364 SCTP_INP_WUNLOCK(inp); 3365 SCTP_ASOC_CREATE_UNLOCK(inp); 3366 return (EADDRINUSE); 3367 } 3368 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 3369 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3370 if (stcb) { 3371 SCTP_TCB_UNLOCK(stcb); 3372 } 3373 SCTP_INP_WUNLOCK(inp); 3374 } else { 3375 SCTP_INP_INCR_REF(inp); 3376 SCTP_INP_WUNLOCK(inp); 3377 stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL); 3378 if (stcb == NULL) { 3379 SCTP_INP_WLOCK(inp); 3380 SCTP_INP_DECR_REF(inp); 3381 SCTP_INP_WUNLOCK(inp); 3382 } 3383 } 3384 if (stcb != NULL) { 3385 /* Already have or am bring up an association */ 3386 SCTP_ASOC_CREATE_UNLOCK(inp); 3387 SCTP_TCB_UNLOCK(stcb); 3388 return (EALREADY); 3389 } 3390 /* We are GOOD to go */ 3391 stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0); 3392 if (stcb == NULL) { 3393 /* Gak! no memory */ 3394 return (error); 3395 } 3396 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 3397 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 3398 /* Set the connected flag so we can queue data */ 3399 soisconnecting(so); 3400 } 3401 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 3402 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 3403 sctp_send_initiate(inp, stcb); 3404 SCTP_ASOC_CREATE_UNLOCK(inp); 3405 SCTP_TCB_UNLOCK(stcb); 3406 return error; 3407 } 3408 3409 static int 3410 sctp_connect2(struct socket *so, struct socket *so2) 3411 { 3412 KASSERT(solocked(so)); 3413 3414 return EOPNOTSUPP; 3415 } 3416 3417 int 3418 sctp_rcvd(struct socket *so, int flags, struct lwp *l) 3419 { 3420 struct sctp_socket_q_list *sq=NULL; 3421 /* 3422 * The user has received some data, we may be able to stuff more 3423 * up the socket. And we need to possibly update the rwnd. 3424 */ 3425 struct sctp_inpcb *inp; 3426 struct sctp_tcb *stcb=NULL; 3427 3428 inp = (struct sctp_inpcb *)so->so_pcb; 3429 #ifdef SCTP_DEBUG 3430 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) 3431 printf("Read for so:%p inp:%p Flags:%x\n", 3432 so, inp, flags); 3433 #endif 3434 3435 if (inp == 0) { 3436 /* I made the same as TCP since we are not setup? */ 3437 #ifdef SCTP_DEBUG 3438 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) 3439 printf("Nope, connection reset\n"); 3440 #endif 3441 return (ECONNRESET); 3442 } 3443 /* 3444 * Grab the first one on the list. It will re-insert itself if 3445 * it runs out of room 3446 */ 3447 SCTP_INP_WLOCK(inp); 3448 if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) 3449 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 3450 /* Ok the other part of our grubby tracking 3451 * stuff for our horrible layer violation that 3452 * the tsvwg thinks is ok for sctp_peeloff.. gak! 3453 * We must update the next vtag pending on the 3454 * socket buffer (if any). 3455 */ 3456 inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so); 3457 sq = TAILQ_FIRST(&inp->sctp_queue_list); 3458 if (sq) { 3459 stcb = sq->tcb; 3460 } else { 3461 stcb = NULL; 3462 } 3463 } else { 3464 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3465 } 3466 if (stcb) { 3467 SCTP_TCB_LOCK(stcb); 3468 } 3469 if (stcb) { 3470 long incr; 3471 /* all code in normal stcb path assumes 3472 * that you have a tcb_lock only. Thus 3473 * we must release the inp write lock. 3474 */ 3475 if (flags & MSG_EOR) { 3476 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) 3477 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 3478 stcb = sctp_remove_from_socket_q(inp); 3479 } 3480 #ifdef SCTP_DEBUG 3481 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) 3482 printf("remove from socket queue for inp:%p tcbret:%p\n", 3483 inp, stcb); 3484 #endif 3485 3486 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len, 3487 sizeof(struct mbuf)); 3488 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) { 3489 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len, 3490 CMSG_LEN(sizeof(struct sctp_sndrcvinfo))); 3491 } 3492 } 3493 if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) || 3494 (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) { 3495 /* Deliver if there is something to be delivered */ 3496 sctp_service_queues(stcb, &stcb->asoc, 1); 3497 } 3498 sctp_set_rwnd(stcb, &stcb->asoc); 3499 /* if we increase by 1 or more MTU's (smallest MTUs of all 3500 * nets) we send a window update sack 3501 */ 3502 incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd; 3503 if (incr < 0) { 3504 incr = 0; 3505 } 3506 if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) || 3507 ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.sb_hiwat)) { 3508 if (callout_pending(&stcb->asoc.dack_timer.timer)) { 3509 /* If the timer is up, stop it */ 3510 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, 3511 stcb->sctp_ep, stcb, NULL); 3512 } 3513 /* Send the sack, with the new rwnd */ 3514 sctp_send_sack(stcb); 3515 /* Now do the output */ 3516 sctp_chunk_output(inp, stcb, 10); 3517 } 3518 } else { 3519 if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)) 3520 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 3521 stcb = sctp_remove_from_socket_q(inp); 3522 } 3523 } 3524 if ((so->so_rcv.sb_mb == NULL) && 3525 (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) { 3526 int sq_cnt=0; 3527 #ifdef SCTP_DEBUG 3528 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) 3529 printf("Something off, inp:%p so->so_rcv->sb_mb is empty and sockq is not.. cleaning\n", 3530 inp); 3531 #endif 3532 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) 3533 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 3534 int done_yet; 3535 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list); 3536 while (!done_yet) { 3537 sq_cnt++; 3538 (void)sctp_remove_from_socket_q(inp); 3539 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list); 3540 } 3541 } 3542 #ifdef SCTP_DEBUG 3543 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) 3544 printf("Cleaned up %d sockq's\n", sq_cnt); 3545 #endif 3546 } 3547 if (stcb) { 3548 SCTP_TCB_UNLOCK(stcb); 3549 } 3550 SCTP_INP_WUNLOCK(inp); 3551 return (0); 3552 } 3553 3554 int 3555 sctp_listen(struct socket *so, struct lwp *l) 3556 { 3557 /* 3558 * Note this module depends on the protocol processing being 3559 * called AFTER any socket level flags and backlog are applied 3560 * to the socket. The traditional way that the socket flags are 3561 * applied is AFTER protocol processing. We have made a change 3562 * to the sys/kern/uipc_socket.c module to reverse this but this 3563 * MUST be in place if the socket API for SCTP is to work properly. 3564 */ 3565 int error = 0; 3566 struct sctp_inpcb *inp; 3567 3568 inp = (struct sctp_inpcb *)so->so_pcb; 3569 if (inp == 0) { 3570 /* I made the same as TCP since we are not setup? */ 3571 return (ECONNRESET); 3572 } 3573 SCTP_INP_RLOCK(inp); 3574 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 3575 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 3576 /* We are already connected AND the TCP model */ 3577 SCTP_INP_RUNLOCK(inp); 3578 return (EADDRINUSE); 3579 } 3580 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 3581 /* We must do a bind. */ 3582 SCTP_INP_RUNLOCK(inp); 3583 if ((error = sctp_inpcb_bind(so, NULL, l))) { 3584 /* bind error, probably perm */ 3585 return (error); 3586 } 3587 } else { 3588 SCTP_INP_RUNLOCK(inp); 3589 } 3590 SCTP_INP_WLOCK(inp); 3591 if (inp->sctp_socket->so_qlimit) { 3592 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 3593 /* 3594 * For the UDP model we must TURN OFF the ACCEPT 3595 * flags since we do NOT allow the accept() call. 3596 * The TCP model (when present) will do accept which 3597 * then prohibits connect(). 3598 */ 3599 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN; 3600 } 3601 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING; 3602 } else { 3603 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) { 3604 /* 3605 * Turning off the listen flags if the backlog is 3606 * set to 0 (i.e. qlimit is 0). 3607 */ 3608 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING; 3609 } 3610 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN; 3611 } 3612 SCTP_INP_WUNLOCK(inp); 3613 return (error); 3614 } 3615 3616 int 3617 sctp_accept(struct socket *so, struct sockaddr *nam) 3618 { 3619 struct sctp_tcb *stcb; 3620 const struct sockaddr *prim; 3621 struct sctp_inpcb *inp; 3622 int error; 3623 3624 if (nam == NULL) { 3625 return EINVAL; 3626 } 3627 inp = (struct sctp_inpcb *)so->so_pcb; 3628 3629 if (inp == 0) { 3630 return ECONNRESET; 3631 } 3632 SCTP_INP_RLOCK(inp); 3633 if (so->so_state & SS_ISDISCONNECTED) { 3634 SCTP_INP_RUNLOCK(inp); 3635 return ECONNABORTED; 3636 } 3637 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3638 if (stcb == NULL) { 3639 SCTP_INP_RUNLOCK(inp); 3640 return ECONNRESET; 3641 } 3642 SCTP_TCB_LOCK(stcb); 3643 SCTP_INP_RUNLOCK(inp); 3644 prim = (const struct sockaddr *)rtcache_getdst(&stcb->asoc.primary_destination->ro); 3645 if (prim->sa_family == AF_INET) { 3646 struct sockaddr_in *sin; 3647 3648 sin = (struct sockaddr_in *)nam; 3649 memset((void *)sin, 0, sizeof (*sin)); 3650 3651 sin->sin_family = AF_INET; 3652 sin->sin_len = sizeof(*sin); 3653 sin->sin_port = ((const struct sockaddr_in *)prim)->sin_port; 3654 sin->sin_addr = ((const struct sockaddr_in *)prim)->sin_addr; 3655 } else { 3656 struct sockaddr_in6 *sin6; 3657 3658 sin6 = (struct sockaddr_in6 *)nam; 3659 memset((void *)sin6, 0, sizeof (*sin6)); 3660 sin6->sin6_family = AF_INET6; 3661 sin6->sin6_len = sizeof(*sin6); 3662 sin6->sin6_port = ((const struct sockaddr_in6 *)prim)->sin6_port; 3663 3664 sin6->sin6_addr = ((const struct sockaddr_in6 *)prim)->sin6_addr; 3665 if ((error = sa6_recoverscope(sin6)) != 0) 3666 return error; 3667 3668 } 3669 /* Wake any delayed sleep action */ 3670 SCTP_TCB_UNLOCK(stcb); 3671 SCTP_INP_WLOCK(inp); 3672 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) { 3673 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE; 3674 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) { 3675 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; 3676 if (sowritable(inp->sctp_socket)) 3677 sowwakeup(inp->sctp_socket); 3678 } 3679 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) { 3680 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; 3681 if (soreadable(inp->sctp_socket)) 3682 sorwakeup(inp->sctp_socket); 3683 } 3684 3685 } 3686 SCTP_INP_WUNLOCK(inp); 3687 return 0; 3688 } 3689 3690 static int 3691 sctp_stat(struct socket *so, struct stat *ub) 3692 { 3693 return 0; 3694 } 3695 3696 int 3697 sctp_sockaddr(struct socket *so, struct sockaddr *nam) 3698 { 3699 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 3700 struct sctp_inpcb *inp; 3701 3702 memset(sin, 0, sizeof(*sin)); 3703 sin->sin_family = AF_INET; 3704 sin->sin_len = sizeof(*sin); 3705 inp = (struct sctp_inpcb *)so->so_pcb; 3706 if (!inp) { 3707 return ECONNRESET; 3708 } 3709 SCTP_INP_RLOCK(inp); 3710 sin->sin_port = inp->sctp_lport; 3711 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 3712 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 3713 struct sctp_tcb *stcb; 3714 const struct sockaddr_in *sin_a; 3715 struct sctp_nets *net; 3716 int fnd; 3717 3718 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3719 if (stcb == NULL) { 3720 goto notConn; 3721 } 3722 fnd = 0; 3723 sin_a = NULL; 3724 SCTP_TCB_LOCK(stcb); 3725 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3726 sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro); 3727 if (sin_a->sin_family == AF_INET) { 3728 fnd = 1; 3729 break; 3730 } 3731 } 3732 if ((!fnd) || (sin_a == NULL)) { 3733 /* punt */ 3734 SCTP_TCB_UNLOCK(stcb); 3735 goto notConn; 3736 } 3737 sin->sin_addr = sctp_ipv4_source_address_selection(inp, 3738 stcb, (struct route *)&net->ro, net, 0); 3739 SCTP_TCB_UNLOCK(stcb); 3740 } else { 3741 /* For the bound all case you get back 0 */ 3742 notConn: 3743 sin->sin_addr.s_addr = 0; 3744 } 3745 3746 } else { 3747 /* Take the first IPv4 address in the list */ 3748 struct sctp_laddr *laddr; 3749 int fnd = 0; 3750 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 3751 if (laddr->ifa->ifa_addr->sa_family == AF_INET) { 3752 struct sockaddr_in *sin_a; 3753 sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr; 3754 sin->sin_addr = sin_a->sin_addr; 3755 fnd = 1; 3756 break; 3757 } 3758 } 3759 if (!fnd) { 3760 SCTP_INP_RUNLOCK(inp); 3761 return ENOENT; 3762 } 3763 } 3764 SCTP_INP_RUNLOCK(inp); 3765 return (0); 3766 } 3767 3768 int 3769 sctp_peeraddr(struct socket *so, struct sockaddr *nam) 3770 { 3771 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 3772 int fnd; 3773 const struct sockaddr_in *sin_a; 3774 struct sctp_inpcb *inp; 3775 struct sctp_tcb *stcb; 3776 struct sctp_nets *net; 3777 3778 /* Do the malloc first in case it blocks. */ 3779 inp = (struct sctp_inpcb *)so->so_pcb; 3780 if ((inp == NULL) || 3781 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 3782 /* UDP type and listeners will drop out here */ 3783 return (ENOTCONN); 3784 } 3785 3786 memset(sin, 0, sizeof(*sin)); 3787 sin->sin_family = AF_INET; 3788 sin->sin_len = sizeof(*sin); 3789 3790 /* We must recapture incase we blocked */ 3791 inp = (struct sctp_inpcb *)so->so_pcb; 3792 if (!inp) { 3793 return ECONNRESET; 3794 } 3795 SCTP_INP_RLOCK(inp); 3796 stcb = LIST_FIRST(&inp->sctp_asoc_list); 3797 if (stcb) { 3798 SCTP_TCB_LOCK(stcb); 3799 } 3800 SCTP_INP_RUNLOCK(inp); 3801 if (stcb == NULL) { 3802 return ECONNRESET; 3803 } 3804 fnd = 0; 3805 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3806 sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro); 3807 if (sin_a->sin_family == AF_INET) { 3808 fnd = 1; 3809 sin->sin_port = stcb->rport; 3810 sin->sin_addr = sin_a->sin_addr; 3811 break; 3812 } 3813 } 3814 SCTP_TCB_UNLOCK(stcb); 3815 if (!fnd) { 3816 /* No IPv4 address */ 3817 return ENOENT; 3818 } 3819 return (0); 3820 } 3821 3822 static int 3823 sctp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) 3824 { 3825 KASSERT(solocked(so)); 3826 3827 if (m) 3828 m_freem(m); 3829 if (control) 3830 m_freem(control); 3831 3832 return EOPNOTSUPP; 3833 } 3834 3835 static int 3836 sctp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 3837 { 3838 int error = 0; 3839 int family; 3840 3841 if (cmd == SIOCCONNECTX) { 3842 solock(so); 3843 error = sctp_do_connect_x(so, nam, curlwp, 0); 3844 sounlock(so); 3845 } else if (cmd == SIOCCONNECTXDEL) { 3846 solock(so); 3847 error = sctp_do_connect_x(so, nam, curlwp, 1); 3848 sounlock(so); 3849 } else { 3850 family = so->so_proto->pr_domain->dom_family; 3851 switch (family) { 3852 #ifdef INET 3853 case PF_INET: 3854 error = in_control(so, cmd, nam, ifp); 3855 break; 3856 #endif 3857 #ifdef INET6 3858 case PF_INET6: 3859 error = in6_control(so, cmd, nam, ifp); 3860 break; 3861 #endif 3862 default: 3863 error = EAFNOSUPPORT; 3864 } 3865 } 3866 return (error); 3867 } 3868 3869 static int 3870 sctp_purgeif(struct socket *so, struct ifnet *ifp) 3871 { 3872 struct ifaddr *ifa; 3873 IFADDR_READER_FOREACH(ifa, ifp) { 3874 if (ifa->ifa_addr->sa_family == PF_INET) { 3875 sctp_delete_ip_address(ifa); 3876 } 3877 } 3878 3879 mutex_enter(softnet_lock); 3880 in_purgeif(ifp); 3881 mutex_exit(softnet_lock); 3882 3883 return 0; 3884 } 3885 3886 /* 3887 * Sysctl for sctp variables. 3888 */ 3889 static void 3890 sysctl_net_inet_sctp_setup(struct sysctllog **clog) 3891 { 3892 3893 sysctl_createv(clog, 0, NULL, NULL, 3894 CTLFLAG_PERMANENT, 3895 CTLTYPE_NODE, "net", NULL, 3896 NULL, 0, NULL, 0, 3897 CTL_NET, CTL_EOL); 3898 sysctl_createv(clog, 0, NULL, NULL, 3899 CTLFLAG_PERMANENT, 3900 CTLTYPE_NODE, "inet", NULL, 3901 NULL, 0, NULL, 0, 3902 CTL_NET, PF_INET, CTL_EOL); 3903 sysctl_createv(clog, 0, NULL, NULL, 3904 CTLFLAG_PERMANENT, 3905 CTLTYPE_NODE, "sctp", 3906 SYSCTL_DESCR("sctp related settings"), 3907 NULL, 0, NULL, 0, 3908 CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL); 3909 3910 sysctl_createv(clog, 0, NULL, NULL, 3911 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3912 CTLTYPE_INT, "maxdgram", 3913 SYSCTL_DESCR("Maximum outgoing SCTP buffer size"), 3914 NULL, 0, &sctp_sendspace, 0, 3915 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM, 3916 CTL_EOL); 3917 3918 sysctl_createv(clog, 0, NULL, NULL, 3919 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3920 CTLTYPE_INT, "recvspace", 3921 SYSCTL_DESCR("Maximum incoming SCTP buffer size"), 3922 NULL, 0, &sctp_recvspace, 0, 3923 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE, 3924 CTL_EOL); 3925 3926 sysctl_createv(clog, 0, NULL, NULL, 3927 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3928 CTLTYPE_INT, "auto_asconf", 3929 SYSCTL_DESCR("Enable SCTP Auto-ASCONF"), 3930 NULL, 0, &sctp_auto_asconf, 0, 3931 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF, 3932 CTL_EOL); 3933 3934 sysctl_createv(clog, 0, NULL, NULL, 3935 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3936 CTLTYPE_INT, "ecn_enable", 3937 SYSCTL_DESCR("Enable SCTP ECN"), 3938 NULL, 0, &sctp_ecn, 0, 3939 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE, 3940 CTL_EOL); 3941 3942 sysctl_createv(clog, 0, NULL, NULL, 3943 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3944 CTLTYPE_INT, "ecn_nonce", 3945 SYSCTL_DESCR("Enable SCTP ECN Nonce"), 3946 NULL, 0, &sctp_ecn_nonce, 0, 3947 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE, 3948 CTL_EOL); 3949 3950 sysctl_createv(clog, 0, NULL, NULL, 3951 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3952 CTLTYPE_INT, "strict_sack", 3953 SYSCTL_DESCR("Enable SCTP Strict SACK checking"), 3954 NULL, 0, &sctp_strict_sacks, 0, 3955 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK, 3956 CTL_EOL); 3957 3958 sysctl_createv(clog, 0, NULL, NULL, 3959 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3960 CTLTYPE_INT, "loopback_nocsum", 3961 SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"), 3962 NULL, 0, &sctp_no_csum_on_loopback, 0, 3963 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO, 3964 CTL_EOL); 3965 3966 sysctl_createv(clog, 0, NULL, NULL, 3967 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3968 CTLTYPE_INT, "strict_init", 3969 SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"), 3970 NULL, 0, &sctp_strict_init, 0, 3971 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT, 3972 CTL_EOL); 3973 3974 sysctl_createv(clog, 0, NULL, NULL, 3975 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3976 CTLTYPE_INT, "peer_chkoh", 3977 SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"), 3978 NULL, 0, &sctp_peer_chunk_oh, 0, 3979 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH, 3980 CTL_EOL); 3981 3982 sysctl_createv(clog, 0, NULL, NULL, 3983 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3984 CTLTYPE_INT, "maxburst", 3985 SYSCTL_DESCR("Default max burst for sctp endpoints"), 3986 NULL, 0, &sctp_max_burst_default, 0, 3987 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST, 3988 CTL_EOL); 3989 3990 sysctl_createv(clog, 0, NULL, NULL, 3991 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3992 CTLTYPE_INT, "maxchunks", 3993 SYSCTL_DESCR("Default max chunks on queue per asoc"), 3994 NULL, 0, &sctp_max_chunks_on_queue, 0, 3995 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ, 3996 CTL_EOL); 3997 #ifdef SCTP_DEBUG 3998 sysctl_createv(clog, 0, NULL, NULL, 3999 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 4000 CTLTYPE_INT, "debug", 4001 SYSCTL_DESCR("Configure debug output"), 4002 NULL, 0, &sctp_debug_on, 0, 4003 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_DEBUG, 4004 CTL_EOL); 4005 #endif 4006 } 4007 4008 PR_WRAP_USRREQS(sctp) 4009 #define sctp_attach sctp_attach_wrapper 4010 #define sctp_detach sctp_detach_wrapper 4011 #define sctp_accept sctp_accept_wrapper 4012 #define sctp_bind sctp_bind_wrapper 4013 #define sctp_listen sctp_listen_wrapper 4014 #define sctp_connect sctp_connect_wrapper 4015 #define sctp_connect2 sctp_connect2_wrapper 4016 #define sctp_disconnect sctp_disconnect_wrapper 4017 #define sctp_shutdown sctp_shutdown_wrapper 4018 #define sctp_abort sctp_abort_wrapper 4019 #define sctp_ioctl sctp_ioctl_wrapper 4020 #define sctp_stat sctp_stat_wrapper 4021 #define sctp_peeraddr sctp_peeraddr_wrapper 4022 #define sctp_sockaddr sctp_sockaddr_wrapper 4023 #define sctp_rcvd sctp_rcvd_wrapper 4024 #define sctp_recvoob sctp_recvoob_wrapper 4025 #define sctp_send sctp_send_wrapper 4026 #define sctp_sendoob sctp_sendoob_wrapper 4027 #define sctp_purgeif sctp_purgeif_wrapper 4028 4029 const struct pr_usrreqs sctp_usrreqs = { 4030 .pr_attach = sctp_attach, 4031 .pr_detach = sctp_detach, 4032 .pr_accept = sctp_accept, 4033 .pr_bind = sctp_bind, 4034 .pr_listen = sctp_listen, 4035 .pr_connect = sctp_connect, 4036 .pr_connect2 = sctp_connect2, 4037 .pr_disconnect = sctp_disconnect, 4038 .pr_shutdown = sctp_shutdown, 4039 .pr_abort = sctp_abort, 4040 .pr_ioctl = sctp_ioctl, 4041 .pr_stat = sctp_stat, 4042 .pr_peeraddr = sctp_peeraddr, 4043 .pr_sockaddr = sctp_sockaddr, 4044 .pr_rcvd = sctp_rcvd, 4045 .pr_recvoob = sctp_recvoob, 4046 .pr_send = sctp_send, 4047 .pr_sendoob = sctp_sendoob, 4048 .pr_purgeif = sctp_purgeif, 4049 }; 4050