The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/sctp_usrreq.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: d774d2ef6bd91ccc136f5a756963be14


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.