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  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions are met:
    6  *
    7  * a) Redistributions of source code must retain the above copyright notice,
    8  *   this list of conditions and the following disclaimer.
    9  *
   10  * b) Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in
   12  *   the documentation and/or other materials provided with the distribution.
   13  *
   14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
   15  *    contributors may be used to endorse or promote products derived
   16  *    from this software without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   28  * THE POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 
   31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/8.1/sys/netinet/sctp_usrreq.c 209067 2010-06-11 20:38:20Z tuexen $");
   35 #include <netinet/sctp_os.h>
   36 #include <sys/proc.h>
   37 #include <netinet/sctp_pcb.h>
   38 #include <netinet/sctp_header.h>
   39 #include <netinet/sctp_var.h>
   40 #if defined(INET6)
   41 #endif
   42 #include <netinet/sctp_sysctl.h>
   43 #include <netinet/sctp_output.h>
   44 #include <netinet/sctp_uio.h>
   45 #include <netinet/sctp_asconf.h>
   46 #include <netinet/sctputil.h>
   47 #include <netinet/sctp_indata.h>
   48 #include <netinet/sctp_timer.h>
   49 #include <netinet/sctp_auth.h>
   50 #include <netinet/sctp_bsd_addr.h>
   51 #include <netinet/sctp_cc_functions.h>
   52 #include <netinet/udp.h>
   53 
   54 
   55 
   56 
   57 void
   58 sctp_init(void)
   59 {
   60         u_long sb_max_adj;
   61 
   62         bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
   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 
   82         SCTP_BASE_VAR(first_time) = 0;
   83         SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
   84         sctp_pcb_init();
   85 #if defined(SCTP_PACKET_LOGGING)
   86         SCTP_BASE_VAR(packet_log_writers) = 0;
   87         SCTP_BASE_VAR(packet_log_end) = 0;
   88         bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
   89 #endif
   90 
   91 
   92 }
   93 
   94 void
   95 sctp_finish(void)
   96 {
   97         sctp_pcb_finish();
   98 }
   99 
  100 
  101 
  102 void
  103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
  104     struct sctp_tcb *stcb,
  105     struct sctp_nets *net,
  106     uint16_t nxtsz)
  107 {
  108         struct sctp_tmit_chunk *chk;
  109         uint16_t overhead;
  110 
  111         /* Adjust that too */
  112         stcb->asoc.smallest_mtu = nxtsz;
  113         /* now off to subtract IP_DF flag if needed */
  114 #ifdef SCTP_PRINT_FOR_B_AND_M
  115         SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
  116             inp, stcb, net, nxtsz);
  117 #endif
  118         overhead = IP_HDR_SIZE;
  119         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
  120                 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  121         }
  122         TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
  123                 if ((chk->send_size + overhead) > nxtsz) {
  124                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  125                 }
  126         }
  127         TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  128                 if ((chk->send_size + overhead) > nxtsz) {
  129                         /*
  130                          * For this guy we also mark for immediate resend
  131                          * since we sent to big of chunk
  132                          */
  133                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  134                         if (chk->sent < SCTP_DATAGRAM_RESEND) {
  135                                 sctp_flight_size_decrease(chk);
  136                                 sctp_total_flight_decrease(stcb, chk);
  137                         }
  138                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
  139                                 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
  140                         }
  141                         chk->sent = SCTP_DATAGRAM_RESEND;
  142                         chk->rec.data.doing_fast_retransmit = 0;
  143                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
  144                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
  145                                     chk->whoTo->flight_size,
  146                                     chk->book_size,
  147                                     (uintptr_t) chk->whoTo,
  148                                     chk->rec.data.TSN_seq);
  149                         }
  150                         /* Clear any time so NO RTT is being done */
  151                         chk->do_rtt = 0;
  152                 }
  153         }
  154 }
  155 
  156 static void
  157 sctp_notify_mbuf(struct sctp_inpcb *inp,
  158     struct sctp_tcb *stcb,
  159     struct sctp_nets *net,
  160     struct ip *ip,
  161     struct sctphdr *sh)
  162 {
  163         struct icmp *icmph;
  164         int totsz, tmr_stopped = 0;
  165         uint16_t nxtsz;
  166 
  167         /* protection */
  168         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
  169             (ip == NULL) || (sh == NULL)) {
  170                 if (stcb != NULL) {
  171                         SCTP_TCB_UNLOCK(stcb);
  172                 }
  173                 return;
  174         }
  175         /* First job is to verify the vtag matches what I would send */
  176         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
  177                 SCTP_TCB_UNLOCK(stcb);
  178                 return;
  179         }
  180         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
  181             sizeof(struct ip)));
  182         if (icmph->icmp_type != ICMP_UNREACH) {
  183                 /* We only care about unreachable */
  184                 SCTP_TCB_UNLOCK(stcb);
  185                 return;
  186         }
  187         if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
  188                 /* not a unreachable message due to frag. */
  189                 SCTP_TCB_UNLOCK(stcb);
  190                 return;
  191         }
  192         totsz = ip->ip_len;
  193 
  194         nxtsz = ntohs(icmph->icmp_nextmtu);
  195         if (nxtsz == 0) {
  196                 /*
  197                  * old type router that does not tell us what the next size
  198                  * mtu is. Rats we will have to guess (in a educated fashion
  199                  * of course)
  200                  */
  201                 nxtsz = find_next_best_mtu(totsz);
  202         }
  203         /* Stop any PMTU timer */
  204         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
  205                 tmr_stopped = 1;
  206                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
  207                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
  208         }
  209         /* Adjust destination size limit */
  210         if (net->mtu > nxtsz) {
  211                 net->mtu = nxtsz;
  212                 if (net->port) {
  213                         net->mtu -= sizeof(struct udphdr);
  214                 }
  215         }
  216         /* now what about the ep? */
  217         if (stcb->asoc.smallest_mtu > nxtsz) {
  218 #ifdef SCTP_PRINT_FOR_B_AND_M
  219                 SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
  220                     nxtsz);
  221 #endif
  222                 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
  223         }
  224         if (tmr_stopped)
  225                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
  226 
  227         SCTP_TCB_UNLOCK(stcb);
  228 }
  229 
  230 
  231 void
  232 sctp_notify(struct sctp_inpcb *inp,
  233     struct ip *ip,
  234     struct sctphdr *sh,
  235     struct sockaddr *to,
  236     struct sctp_tcb *stcb,
  237     struct sctp_nets *net)
  238 {
  239 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  240         struct socket *so;
  241 
  242 #endif
  243         /* protection */
  244         int reason;
  245         struct icmp *icmph;
  246 
  247 
  248         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
  249             (sh == NULL) || (to == NULL)) {
  250                 if (stcb)
  251                         SCTP_TCB_UNLOCK(stcb);
  252                 return;
  253         }
  254         /* First job is to verify the vtag matches what I would send */
  255         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
  256                 SCTP_TCB_UNLOCK(stcb);
  257                 return;
  258         }
  259         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
  260             sizeof(struct ip)));
  261         if (icmph->icmp_type != ICMP_UNREACH) {
  262                 /* We only care about unreachable */
  263                 SCTP_TCB_UNLOCK(stcb);
  264                 return;
  265         }
  266         if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
  267             (icmph->icmp_code == ICMP_UNREACH_HOST) ||
  268             (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
  269             (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
  270             (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
  271             (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
  272             (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
  273             (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
  274 
  275                 /*
  276                  * Hmm reachablity problems we must examine closely. If its
  277                  * not reachable, we may have lost a network. Or if there is
  278                  * NO protocol at the other end named SCTP. well we consider
  279                  * it a OOTB abort.
  280                  */
  281                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
  282                         /* Ok that destination is NOT reachable */
  283                         SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
  284                             net->error_count,
  285                             net->failure_threshold,
  286                             net);
  287 
  288                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
  289                         net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
  290                         /*
  291                          * JRS 5/14/07 - If a destination is unreachable,
  292                          * the PF bit is turned off.  This allows an
  293                          * unambiguous use of the PF bit for destinations
  294                          * that are reachable but potentially failed. If the
  295                          * destination is set to the unreachable state, also
  296                          * set the destination to the PF state.
  297                          */
  298                         /*
  299                          * Add debug message here if destination is not in
  300                          * PF state.
  301                          */
  302                         /* Stop any running T3 timers here? */
  303                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
  304                                 net->dest_state &= ~SCTP_ADDR_PF;
  305                                 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
  306                                     net);
  307                         }
  308                         net->error_count = net->failure_threshold + 1;
  309                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
  310                             stcb, SCTP_FAILED_THRESHOLD,
  311                             (void *)net, SCTP_SO_NOT_LOCKED);
  312                 }
  313                 SCTP_TCB_UNLOCK(stcb);
  314         } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
  315             (icmph->icmp_code == ICMP_UNREACH_PORT)) {
  316                 /*
  317                  * Here the peer is either playing tricks on us, including
  318                  * an address that belongs to someone who does not support
  319                  * SCTP OR was a userland implementation that shutdown and
  320                  * now is dead. In either case treat it like a OOTB abort
  321                  * with no TCB
  322                  */
  323                 reason = SCTP_PEER_FAULTY;
  324                 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
  325 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  326                 so = SCTP_INP_SO(inp);
  327                 atomic_add_int(&stcb->asoc.refcnt, 1);
  328                 SCTP_TCB_UNLOCK(stcb);
  329                 SCTP_SOCKET_LOCK(so, 1);
  330                 SCTP_TCB_LOCK(stcb);
  331                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
  332 #endif
  333                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
  334 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  335                 SCTP_SOCKET_UNLOCK(so, 1);
  336                 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
  337 #endif
  338                 /* no need to unlock here, since the TCB is gone */
  339         } else {
  340                 SCTP_TCB_UNLOCK(stcb);
  341         }
  342 }
  343 
  344 void
  345 sctp_ctlinput(cmd, sa, vip)
  346         int cmd;
  347         struct sockaddr *sa;
  348         void *vip;
  349 {
  350         struct ip *ip = vip;
  351         struct sctphdr *sh;
  352         uint32_t vrf_id;
  353 
  354         /* FIX, for non-bsd is this right? */
  355         vrf_id = SCTP_DEFAULT_VRFID;
  356         if (sa->sa_family != AF_INET ||
  357             ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
  358                 return;
  359         }
  360         if (PRC_IS_REDIRECT(cmd)) {
  361                 ip = 0;
  362         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
  363                 return;
  364         }
  365         if (ip) {
  366                 struct sctp_inpcb *inp = NULL;
  367                 struct sctp_tcb *stcb = NULL;
  368                 struct sctp_nets *net = NULL;
  369                 struct sockaddr_in to, from;
  370 
  371                 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  372                 bzero(&to, sizeof(to));
  373                 bzero(&from, sizeof(from));
  374                 from.sin_family = to.sin_family = AF_INET;
  375                 from.sin_len = to.sin_len = sizeof(to);
  376                 from.sin_port = sh->src_port;
  377                 from.sin_addr = ip->ip_src;
  378                 to.sin_port = sh->dest_port;
  379                 to.sin_addr = ip->ip_dst;
  380 
  381                 /*
  382                  * 'to' holds the dest of the packet that failed to be sent.
  383                  * 'from' holds our local endpoint address. Thus we reverse
  384                  * the to and the from in the lookup.
  385                  */
  386                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
  387                     (struct sockaddr *)&to,
  388                     &inp, &net, 1, vrf_id);
  389                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
  390                         if (cmd != PRC_MSGSIZE) {
  391                                 sctp_notify(inp, ip, sh,
  392                                     (struct sockaddr *)&to, stcb,
  393                                     net);
  394                         } else {
  395                                 /* handle possible ICMP size messages */
  396                                 sctp_notify_mbuf(inp, stcb, net, ip, sh);
  397                         }
  398                 } else {
  399                         if ((stcb == NULL) && (inp != NULL)) {
  400                                 /* reduce ref-count */
  401                                 SCTP_INP_WLOCK(inp);
  402                                 SCTP_INP_DECR_REF(inp);
  403                                 SCTP_INP_WUNLOCK(inp);
  404                         }
  405                         if (stcb) {
  406                                 SCTP_TCB_UNLOCK(stcb);
  407                         }
  408                 }
  409         }
  410         return;
  411 }
  412 
  413 static int
  414 sctp_getcred(SYSCTL_HANDLER_ARGS)
  415 {
  416         struct xucred xuc;
  417         struct sockaddr_in addrs[2];
  418         struct sctp_inpcb *inp;
  419         struct sctp_nets *net;
  420         struct sctp_tcb *stcb;
  421         int error;
  422         uint32_t vrf_id;
  423 
  424         /* FIX, for non-bsd is this right? */
  425         vrf_id = SCTP_DEFAULT_VRFID;
  426 
  427         error = priv_check(req->td, PRIV_NETINET_GETCRED);
  428 
  429         if (error)
  430                 return (error);
  431 
  432         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  433         if (error)
  434                 return (error);
  435 
  436         stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
  437             sintosa(&addrs[1]),
  438             &inp, &net, 1, vrf_id);
  439         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
  440                 if ((inp != NULL) && (stcb == NULL)) {
  441                         /* reduce ref-count */
  442                         SCTP_INP_WLOCK(inp);
  443                         SCTP_INP_DECR_REF(inp);
  444                         goto cred_can_cont;
  445                 }
  446                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
  447                 error = ENOENT;
  448                 goto out;
  449         }
  450         SCTP_TCB_UNLOCK(stcb);
  451         /*
  452          * We use the write lock here, only since in the error leg we need
  453          * it. If we used RLOCK, then we would have to
  454          * wlock/decr/unlock/rlock. Which in theory could create a hole.
  455          * Better to use higher wlock.
  456          */
  457         SCTP_INP_WLOCK(inp);
  458 cred_can_cont:
  459         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
  460         if (error) {
  461                 SCTP_INP_WUNLOCK(inp);
  462                 goto out;
  463         }
  464         cru2x(inp->sctp_socket->so_cred, &xuc);
  465         SCTP_INP_WUNLOCK(inp);
  466         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  467 out:
  468         return (error);
  469 }
  470 
  471 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
  472     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
  473 
  474 
  475 static void
  476 sctp_abort(struct socket *so)
  477 {
  478         struct sctp_inpcb *inp;
  479         uint32_t flags;
  480 
  481         inp = (struct sctp_inpcb *)so->so_pcb;
  482         if (inp == 0) {
  483                 return;
  484         }
  485 sctp_must_try_again:
  486         flags = inp->sctp_flags;
  487 #ifdef SCTP_LOG_CLOSING
  488         sctp_log_closing(inp, NULL, 17);
  489 #endif
  490         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  491             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
  492 #ifdef SCTP_LOG_CLOSING
  493                 sctp_log_closing(inp, NULL, 16);
  494 #endif
  495                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  496                     SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
  497                 SOCK_LOCK(so);
  498                 SCTP_SB_CLEAR(so->so_snd);
  499                 /*
  500                  * same for the rcv ones, they are only here for the
  501                  * accounting/select.
  502                  */
  503                 SCTP_SB_CLEAR(so->so_rcv);
  504 
  505                 /* Now null out the reference, we are completely detached. */
  506                 so->so_pcb = NULL;
  507                 SOCK_UNLOCK(so);
  508         } else {
  509                 flags = inp->sctp_flags;
  510                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
  511                         goto sctp_must_try_again;
  512                 }
  513         }
  514         return;
  515 }
  516 
  517 static int
  518 sctp_attach(struct socket *so, int proto, struct thread *p)
  519 {
  520         struct sctp_inpcb *inp;
  521         struct inpcb *ip_inp;
  522         int error;
  523         uint32_t vrf_id = SCTP_DEFAULT_VRFID;
  524 
  525 #ifdef IPSEC
  526         uint32_t flags;
  527 
  528 #endif
  529 
  530         inp = (struct sctp_inpcb *)so->so_pcb;
  531         if (inp != 0) {
  532                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  533                 return EINVAL;
  534         }
  535         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
  536                 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
  537                 if (error) {
  538                         return error;
  539                 }
  540         }
  541         error = sctp_inpcb_alloc(so, vrf_id);
  542         if (error) {
  543                 return error;
  544         }
  545         inp = (struct sctp_inpcb *)so->so_pcb;
  546         SCTP_INP_WLOCK(inp);
  547         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;    /* I'm not v6! */
  548         ip_inp = &inp->ip_inp.inp;
  549         ip_inp->inp_vflag |= INP_IPV4;
  550         ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
  551 #ifdef IPSEC
  552         error = ipsec_init_policy(so, &ip_inp->inp_sp);
  553 #ifdef SCTP_LOG_CLOSING
  554         sctp_log_closing(inp, NULL, 17);
  555 #endif
  556         if (error != 0) {
  557 try_again:
  558                 flags = inp->sctp_flags;
  559                 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  560                     (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
  561 #ifdef SCTP_LOG_CLOSING
  562                         sctp_log_closing(inp, NULL, 15);
  563 #endif
  564                         SCTP_INP_WUNLOCK(inp);
  565                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  566                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
  567                 } else {
  568                         flags = inp->sctp_flags;
  569                         if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
  570                                 goto try_again;
  571                         } else {
  572                                 SCTP_INP_WUNLOCK(inp);
  573                         }
  574                 }
  575                 return error;
  576         }
  577 #endif                          /* IPSEC */
  578         SCTP_INP_WUNLOCK(inp);
  579         return 0;
  580 }
  581 
  582 static int
  583 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
  584 {
  585         struct sctp_inpcb *inp = NULL;
  586         int error;
  587 
  588 #ifdef INET6
  589         if (addr && addr->sa_family != AF_INET) {
  590                 /* must be a v4 address! */
  591                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  592                 return EINVAL;
  593         }
  594 #endif                          /* INET6 */
  595         if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
  596                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  597                 return EINVAL;
  598         }
  599         inp = (struct sctp_inpcb *)so->so_pcb;
  600         if (inp == 0) {
  601                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  602                 return EINVAL;
  603         }
  604         error = sctp_inpcb_bind(so, addr, NULL, p);
  605         return error;
  606 }
  607 
  608 void
  609 sctp_close(struct socket *so)
  610 {
  611         struct sctp_inpcb *inp;
  612         uint32_t flags;
  613 
  614         inp = (struct sctp_inpcb *)so->so_pcb;
  615         if (inp == 0)
  616                 return;
  617 
  618         /*
  619          * Inform all the lower layer assoc that we are done.
  620          */
  621 sctp_must_try_again:
  622         flags = inp->sctp_flags;
  623 #ifdef SCTP_LOG_CLOSING
  624         sctp_log_closing(inp, NULL, 17);
  625 #endif
  626         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  627             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
  628                 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
  629                     (so->so_rcv.sb_cc > 0)) {
  630 #ifdef SCTP_LOG_CLOSING
  631                         sctp_log_closing(inp, NULL, 13);
  632 #endif
  633                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  634                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
  635                 } else {
  636 #ifdef SCTP_LOG_CLOSING
  637                         sctp_log_closing(inp, NULL, 14);
  638 #endif
  639                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
  640                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
  641                 }
  642                 /*
  643                  * The socket is now detached, no matter what the state of
  644                  * the SCTP association.
  645                  */
  646                 SOCK_LOCK(so);
  647                 SCTP_SB_CLEAR(so->so_snd);
  648                 /*
  649                  * same for the rcv ones, they are only here for the
  650                  * accounting/select.
  651                  */
  652                 SCTP_SB_CLEAR(so->so_rcv);
  653 
  654                 /* Now null out the reference, we are completely detached. */
  655                 so->so_pcb = NULL;
  656                 SOCK_UNLOCK(so);
  657         } else {
  658                 flags = inp->sctp_flags;
  659                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
  660                         goto sctp_must_try_again;
  661                 }
  662         }
  663         return;
  664 }
  665 
  666 
  667 int
  668 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  669     struct mbuf *control, struct thread *p);
  670 
  671 
  672 int
  673 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  674     struct mbuf *control, struct thread *p)
  675 {
  676         struct sctp_inpcb *inp;
  677         int error;
  678 
  679         inp = (struct sctp_inpcb *)so->so_pcb;
  680         if (inp == 0) {
  681                 if (control) {
  682                         sctp_m_freem(control);
  683                         control = NULL;
  684                 }
  685                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  686                 sctp_m_freem(m);
  687                 return EINVAL;
  688         }
  689         /* Got to have an to address if we are NOT a connected socket */
  690         if ((addr == NULL) &&
  691             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
  692             (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
  693             ) {
  694                 goto connected_type;
  695         } else if (addr == NULL) {
  696                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
  697                 error = EDESTADDRREQ;
  698                 sctp_m_freem(m);
  699                 if (control) {
  700                         sctp_m_freem(control);
  701                         control = NULL;
  702                 }
  703                 return (error);
  704         }
  705 #ifdef INET6
  706         if (addr->sa_family != AF_INET) {
  707                 /* must be a v4 address! */
  708                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
  709                 sctp_m_freem(m);
  710                 if (control) {
  711                         sctp_m_freem(control);
  712                         control = NULL;
  713                 }
  714                 error = EDESTADDRREQ;
  715                 return EDESTADDRREQ;
  716         }
  717 #endif                          /* INET6 */
  718 connected_type:
  719         /* now what about control */
  720         if (control) {
  721                 if (inp->control) {
  722                         SCTP_PRINTF("huh? control set?\n");
  723                         sctp_m_freem(inp->control);
  724                         inp->control = NULL;
  725                 }
  726                 inp->control = control;
  727         }
  728         /* Place the data */
  729         if (inp->pkt) {
  730                 SCTP_BUF_NEXT(inp->pkt_last) = m;
  731                 inp->pkt_last = m;
  732         } else {
  733                 inp->pkt_last = inp->pkt = m;
  734         }
  735         if (
  736         /* FreeBSD uses a flag passed */
  737             ((flags & PRUS_MORETOCOME) == 0)
  738             ) {
  739                 /*
  740                  * note with the current version this code will only be used
  741                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
  742                  * re-defining sosend to use the sctp_sosend. One can
  743                  * optionally switch back to this code (by changing back the
  744                  * definitions) but this is not advisable. This code is used
  745                  * by FreeBSD when sending a file with sendfile() though.
  746                  */
  747                 int ret;
  748 
  749                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
  750                 inp->pkt = NULL;
  751                 inp->control = NULL;
  752                 return (ret);
  753         } else {
  754                 return (0);
  755         }
  756 }
  757 
  758 int
  759 sctp_disconnect(struct socket *so)
  760 {
  761         struct sctp_inpcb *inp;
  762 
  763         inp = (struct sctp_inpcb *)so->so_pcb;
  764         if (inp == NULL) {
  765                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
  766                 return (ENOTCONN);
  767         }
  768         SCTP_INP_RLOCK(inp);
  769         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  770             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
  771                 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
  772                         /* No connection */
  773                         SCTP_INP_RUNLOCK(inp);
  774                         return (0);
  775                 } else {
  776                         struct sctp_association *asoc;
  777                         struct sctp_tcb *stcb;
  778 
  779                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
  780                         if (stcb == NULL) {
  781                                 SCTP_INP_RUNLOCK(inp);
  782                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  783                                 return (EINVAL);
  784                         }
  785                         SCTP_TCB_LOCK(stcb);
  786                         asoc = &stcb->asoc;
  787                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  788                                 /* We are about to be freed, out of here */
  789                                 SCTP_TCB_UNLOCK(stcb);
  790                                 SCTP_INP_RUNLOCK(inp);
  791                                 return (0);
  792                         }
  793                         if (((so->so_options & SO_LINGER) &&
  794                             (so->so_linger == 0)) ||
  795                             (so->so_rcv.sb_cc > 0)) {
  796                                 if (SCTP_GET_STATE(asoc) !=
  797                                     SCTP_STATE_COOKIE_WAIT) {
  798                                         /* Left with Data unread */
  799                                         struct mbuf *err;
  800 
  801                                         err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
  802                                         if (err) {
  803                                                 /*
  804                                                  * Fill in the user
  805                                                  * initiated abort
  806                                                  */
  807                                                 struct sctp_paramhdr *ph;
  808 
  809                                                 ph = mtod(err, struct sctp_paramhdr *);
  810                                                 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
  811                                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
  812                                                 ph->param_length = htons(SCTP_BUF_LEN(err));
  813                                         }
  814 #if defined(SCTP_PANIC_ON_ABORT)
  815                                         panic("disconnect does an abort");
  816 #endif
  817                                         sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
  818                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  819                                 }
  820                                 SCTP_INP_RUNLOCK(inp);
  821                                 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
  822                                     (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  823                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  824                                 }
  825                                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
  826                                 /* No unlock tcb assoc is gone */
  827                                 return (0);
  828                         }
  829                         if (TAILQ_EMPTY(&asoc->send_queue) &&
  830                             TAILQ_EMPTY(&asoc->sent_queue) &&
  831                             (asoc->stream_queue_cnt == 0)) {
  832                                 /* there is nothing queued to send, so done */
  833                                 if (asoc->locked_on_sending) {
  834                                         goto abort_anyway;
  835                                 }
  836                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
  837                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  838                                         /* only send SHUTDOWN 1st time thru */
  839                                         sctp_stop_timers_for_shutdown(stcb);
  840                                         sctp_send_shutdown(stcb,
  841                                             stcb->asoc.primary_destination);
  842                                         sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
  843                                         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
  844                                             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  845                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  846                                         }
  847                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
  848                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
  849                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
  850                                             stcb->sctp_ep, stcb,
  851                                             asoc->primary_destination);
  852                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
  853                                             stcb->sctp_ep, stcb,
  854                                             asoc->primary_destination);
  855                                 }
  856                         } else {
  857                                 /*
  858                                  * we still got (or just got) data to send,
  859                                  * so set SHUTDOWN_PENDING
  860                                  */
  861                                 /*
  862                                  * XXX sockets draft says that SCTP_EOF
  863                                  * should be sent with no data. currently,
  864                                  * we will allow user data to be sent first
  865                                  * and move to SHUTDOWN-PENDING
  866                                  */
  867                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
  868                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
  869                                     asoc->primary_destination);
  870                                 if (asoc->locked_on_sending) {
  871                                         /* Locked to send out the data */
  872                                         struct sctp_stream_queue_pending *sp;
  873 
  874                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
  875                                         if (sp == NULL) {
  876                                                 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
  877                                                     asoc->locked_on_sending->stream_no);
  878                                         } else {
  879                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
  880                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
  881                                         }
  882                                 }
  883                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
  884                                     TAILQ_EMPTY(&asoc->sent_queue) &&
  885                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
  886                                         struct mbuf *op_err;
  887 
  888                         abort_anyway:
  889                                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
  890                                             0, M_DONTWAIT, 1, MT_DATA);
  891                                         if (op_err) {
  892                                                 /*
  893                                                  * Fill in the user
  894                                                  * initiated abort
  895                                                  */
  896                                                 struct sctp_paramhdr *ph;
  897                                                 uint32_t *ippp;
  898 
  899                                                 SCTP_BUF_LEN(op_err) =
  900                                                     (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
  901                                                 ph = mtod(op_err,
  902                                                     struct sctp_paramhdr *);
  903                                                 ph->param_type = htons(
  904                                                     SCTP_CAUSE_USER_INITIATED_ABT);
  905                                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
  906                                                 ippp = (uint32_t *) (ph + 1);
  907                                                 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
  908                                         }
  909 #if defined(SCTP_PANIC_ON_ABORT)
  910                                         panic("disconnect does an abort");
  911 #endif
  912 
  913                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
  914                                         sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
  915                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  916                                         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
  917                                             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  918                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  919                                         }
  920                                         SCTP_INP_RUNLOCK(inp);
  921                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
  922                                         return (0);
  923                                 } else {
  924                                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
  925                                 }
  926                         }
  927                         soisdisconnecting(so);
  928                         SCTP_TCB_UNLOCK(stcb);
  929                         SCTP_INP_RUNLOCK(inp);
  930                         return (0);
  931                 }
  932                 /* not reached */
  933         } else {
  934                 /* UDP model does not support this */
  935                 SCTP_INP_RUNLOCK(inp);
  936                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
  937                 return EOPNOTSUPP;
  938         }
  939 }
  940 
  941 int
  942 sctp_flush(struct socket *so, int how)
  943 {
  944         /*
  945          * We will just clear out the values and let subsequent close clear
  946          * out the data, if any. Note if the user did a shutdown(SHUT_RD)
  947          * they will not be able to read the data, the socket will block
  948          * that from happening.
  949          */
  950         if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
  951                 /*
  952                  * First make sure the sb will be happy, we don't use these
  953                  * except maybe the count
  954                  */
  955                 so->so_rcv.sb_cc = 0;
  956                 so->so_rcv.sb_mbcnt = 0;
  957                 so->so_rcv.sb_mb = NULL;
  958         }
  959         if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
  960                 /*
  961                  * First make sure the sb will be happy, we don't use these
  962                  * except maybe the count
  963                  */
  964                 so->so_snd.sb_cc = 0;
  965                 so->so_snd.sb_mbcnt = 0;
  966                 so->so_snd.sb_mb = NULL;
  967 
  968         }
  969         return (0);
  970 }
  971 
  972 int
  973 sctp_shutdown(struct socket *so)
  974 {
  975         struct sctp_inpcb *inp;
  976 
  977         inp = (struct sctp_inpcb *)so->so_pcb;
  978         if (inp == 0) {
  979                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
  980                 return EINVAL;
  981         }
  982         SCTP_INP_RLOCK(inp);
  983         /* For UDP model this is a invalid call */
  984         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
  985                 /* Restore the flags that the soshutdown took away. */
  986                 SOCKBUF_LOCK(&so->so_rcv);
  987                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
  988                 SOCKBUF_UNLOCK(&so->so_rcv);
  989                 /* This proc will wakeup for read and do nothing (I hope) */
  990                 SCTP_INP_RUNLOCK(inp);
  991                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
  992                 return (EOPNOTSUPP);
  993         }
  994         /*
  995          * Ok if we reach here its the TCP model and it is either a SHUT_WR
  996          * or SHUT_RDWR. This means we put the shutdown flag against it.
  997          */
  998         {
  999                 struct sctp_tcb *stcb;
 1000                 struct sctp_association *asoc;
 1001 
 1002                 if ((so->so_state &
 1003                     (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
 1004                         SCTP_INP_RUNLOCK(inp);
 1005                         return (ENOTCONN);
 1006                 }
 1007                 socantsendmore(so);
 1008 
 1009                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
 1010                 if (stcb == NULL) {
 1011                         /*
 1012                          * Ok we hit the case that the shutdown call was
 1013                          * made after an abort or something. Nothing to do
 1014                          * now.
 1015                          */
 1016                         SCTP_INP_RUNLOCK(inp);
 1017                         return (0);
 1018                 }
 1019                 SCTP_TCB_LOCK(stcb);
 1020                 asoc = &stcb->asoc;
 1021                 if (TAILQ_EMPTY(&asoc->send_queue) &&
 1022                     TAILQ_EMPTY(&asoc->sent_queue) &&
 1023                     (asoc->stream_queue_cnt == 0)) {
 1024                         if (asoc->locked_on_sending) {
 1025                                 goto abort_anyway;
 1026                         }
 1027                         /* there is nothing queued to send, so I'm done... */
 1028                         if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
 1029                                 /* only send SHUTDOWN the first time through */
 1030                                 sctp_stop_timers_for_shutdown(stcb);
 1031                                 sctp_send_shutdown(stcb,
 1032                                     stcb->asoc.primary_destination);
 1033                                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
 1034                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
 1035                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
 1036                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
 1037                                 }
 1038                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
 1039                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
 1040                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
 1041                                     stcb->sctp_ep, stcb,
 1042                                     asoc->primary_destination);
 1043                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
 1044                                     stcb->sctp_ep, stcb,
 1045                                     asoc->primary_destination);
 1046                         }
 1047                 } else {
 1048                         /*
 1049                          * we still got (or just got) data to send, so set
 1050                          * SHUTDOWN_PENDING
 1051                          */
 1052                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
 1053                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
 1054                             asoc->primary_destination);
 1055 
 1056                         if (asoc->locked_on_sending) {
 1057                                 /* Locked to send out the data */
 1058                                 struct sctp_stream_queue_pending *sp;
 1059 
 1060                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
 1061                                 if (sp == NULL) {
 1062                                         SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
 1063                                             asoc->locked_on_sending->stream_no);
 1064                                 } else {
 1065                                         if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
 1066                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
 1067                                         }
 1068                                 }
 1069                         }
 1070                         if (TAILQ_EMPTY(&asoc->send_queue) &&
 1071                             TAILQ_EMPTY(&asoc->sent_queue) &&
 1072                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
 1073                                 struct mbuf *op_err;
 1074 
 1075                 abort_anyway:
 1076                                 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
 1077                                     0, M_DONTWAIT, 1, MT_DATA);
 1078                                 if (op_err) {
 1079                                         /* Fill in the user initiated abort */
 1080                                         struct sctp_paramhdr *ph;
 1081                                         uint32_t *ippp;
 1082 
 1083                                         SCTP_BUF_LEN(op_err) =
 1084                                             sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
 1085                                         ph = mtod(op_err,
 1086                                             struct sctp_paramhdr *);
 1087                                         ph->param_type = htons(
 1088                                             SCTP_CAUSE_USER_INITIATED_ABT);
 1089                                         ph->param_length = htons(SCTP_BUF_LEN(op_err));
 1090                                         ippp = (uint32_t *) (ph + 1);
 1091                                         *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
 1092                                 }
 1093 #if defined(SCTP_PANIC_ON_ABORT)
 1094                                 panic("shutdown does an abort");
 1095 #endif
 1096                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
 1097                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
 1098                                     SCTP_RESPONSE_TO_USER_REQ,
 1099                                     op_err, SCTP_SO_LOCKED);
 1100                                 goto skip_unlock;
 1101                         } else {
 1102                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
 1103                         }
 1104                 }
 1105                 SCTP_TCB_UNLOCK(stcb);
 1106         }
 1107 skip_unlock:
 1108         SCTP_INP_RUNLOCK(inp);
 1109         return 0;
 1110 }
 1111 
 1112 /*
 1113  * copies a "user" presentable address and removes embedded scope, etc.
 1114  * returns 0 on success, 1 on error
 1115  */
 1116 static uint32_t
 1117 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
 1118 {
 1119 #ifdef INET6
 1120         struct sockaddr_in6 lsa6;
 1121 
 1122         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
 1123             &lsa6);
 1124 #endif
 1125         memcpy(ss, sa, sa->sa_len);
 1126         return (0);
 1127 }
 1128 
 1129 
 1130 
 1131 /*
 1132  * NOTE: assumes addr lock is held
 1133  */
 1134 static size_t
 1135 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
 1136     struct sctp_tcb *stcb,
 1137     size_t limit,
 1138     struct sockaddr_storage *sas,
 1139     uint32_t vrf_id)
 1140 {
 1141         struct sctp_ifn *sctp_ifn;
 1142         struct sctp_ifa *sctp_ifa;
 1143         int loopback_scope, ipv4_local_scope, local_scope, site_scope;
 1144         size_t actual;
 1145         int ipv4_addr_legal, ipv6_addr_legal;
 1146         struct sctp_vrf *vrf;
 1147 
 1148         actual = 0;
 1149         if (limit <= 0)
 1150                 return (actual);
 1151 
 1152         if (stcb) {
 1153                 /* Turn on all the appropriate scope */
 1154                 loopback_scope = stcb->asoc.loopback_scope;
 1155                 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
 1156                 local_scope = stcb->asoc.local_scope;
 1157                 site_scope = stcb->asoc.site_scope;
 1158         } else {
 1159                 /* Turn on ALL scope, since we look at the EP */
 1160                 loopback_scope = ipv4_local_scope = local_scope =
 1161                     site_scope = 1;
 1162         }
 1163         ipv4_addr_legal = ipv6_addr_legal = 0;
 1164         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 1165                 ipv6_addr_legal = 1;
 1166                 if (SCTP_IPV6_V6ONLY(inp) == 0) {
 1167                         ipv4_addr_legal = 1;
 1168                 }
 1169         } else {
 1170                 ipv4_addr_legal = 1;
 1171         }
 1172         vrf = sctp_find_vrf(vrf_id);
 1173         if (vrf == NULL) {
 1174                 return (0);
 1175         }
 1176         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 1177                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 1178                         if ((loopback_scope == 0) &&
 1179                             SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
 1180                                 /* Skip loopback if loopback_scope not set */
 1181                                 continue;
 1182                         }
 1183                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 1184                                 if (stcb) {
 1185                                         /*
 1186                                          * For the BOUND-ALL case, the list
 1187                                          * associated with a TCB is Always
 1188                                          * considered a reverse list.. i.e.
 1189                                          * it lists addresses that are NOT
 1190                                          * part of the association. If this
 1191                                          * is one of those we must skip it.
 1192                                          */
 1193                                         if (sctp_is_addr_restricted(stcb,
 1194                                             sctp_ifa)) {
 1195                                                 continue;
 1196                                         }
 1197                                 }
 1198                                 switch (sctp_ifa->address.sa.sa_family) {
 1199                                 case AF_INET:
 1200                                         if (ipv4_addr_legal) {
 1201                                                 struct sockaddr_in *sin;
 1202 
 1203                                                 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
 1204                                                 if (sin->sin_addr.s_addr == 0) {
 1205                                                         /*
 1206                                                          * we skip
 1207                                                          * unspecifed
 1208                                                          * addresses
 1209                                                          */
 1210                                                         continue;
 1211                                                 }
 1212                                                 if ((ipv4_local_scope == 0) &&
 1213                                                     (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
 1214                                                         continue;
 1215                                                 }
 1216 #ifdef INET6
 1217                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
 1218                                                         in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
 1219                                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
 1220                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
 1221                                                         actual += sizeof(struct sockaddr_in6);
 1222                                                 } else {
 1223 #endif
 1224                                                         memcpy(sas, sin, sizeof(*sin));
 1225                                                         ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
 1226                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
 1227                                                         actual += sizeof(*sin);
 1228 #ifdef INET6
 1229                                                 }
 1230 #endif
 1231                                                 if (actual >= limit) {
 1232                                                         return (actual);
 1233                                                 }
 1234                                         } else {
 1235                                                 continue;
 1236                                         }
 1237                                         break;
 1238 #ifdef INET6
 1239                                 case AF_INET6:
 1240                                         if (ipv6_addr_legal) {
 1241                                                 struct sockaddr_in6 *sin6;
 1242 
 1243                                                 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
 1244                                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 1245                                                         /*
 1246                                                          * we skip
 1247                                                          * unspecifed
 1248                                                          * addresses
 1249                                                          */
 1250                                                         continue;
 1251                                                 }
 1252                                                 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
 1253                                                         if (local_scope == 0)
 1254                                                                 continue;
 1255                                                         if (sin6->sin6_scope_id == 0) {
 1256                                                                 if (sa6_recoverscope(sin6) != 0)
 1257                                                                         /*
 1258                                                                          * 
 1259                                                                          * bad
 1260                                                                          * 
 1261                                                                          * li
 1262                                                                          * nk
 1263                                                                          * 
 1264                                                                          * loc
 1265                                                                          * al
 1266                                                                          * 
 1267                                                                          * add
 1268                                                                          * re
 1269                                                                          * ss
 1270                                                                          * */
 1271                                                                         continue;
 1272                                                         }
 1273                                                 }
 1274                                                 if ((site_scope == 0) &&
 1275                                                     (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
 1276                                                         continue;
 1277                                                 }
 1278                                                 memcpy(sas, sin6, sizeof(*sin6));
 1279                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
 1280                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
 1281                                                 actual += sizeof(*sin6);
 1282                                                 if (actual >= limit) {
 1283                                                         return (actual);
 1284                                                 }
 1285                                         } else {
 1286                                                 continue;
 1287                                         }
 1288                                         break;
 1289 #endif
 1290                                 default:
 1291                                         /* TSNH */
 1292                                         break;
 1293                                 }
 1294                         }
 1295                 }
 1296         } else {
 1297                 struct sctp_laddr *laddr;
 1298 
 1299                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 1300                         if (stcb) {
 1301                                 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
 1302                                         continue;
 1303                                 }
 1304                         }
 1305                         if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
 1306                                 continue;
 1307 
 1308                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
 1309                         sas = (struct sockaddr_storage *)((caddr_t)sas +
 1310                             laddr->ifa->address.sa.sa_len);
 1311                         actual += laddr->ifa->address.sa.sa_len;
 1312                         if (actual >= limit) {
 1313                                 return (actual);
 1314                         }
 1315                 }
 1316         }
 1317         return (actual);
 1318 }
 1319 
 1320 static size_t
 1321 sctp_fill_up_addresses(struct sctp_inpcb *inp,
 1322     struct sctp_tcb *stcb,
 1323     size_t limit,
 1324     struct sockaddr_storage *sas)
 1325 {
 1326         size_t size = 0;
 1327 
 1328         SCTP_IPI_ADDR_RLOCK();
 1329         /* fill up addresses for the endpoint's default vrf */
 1330         size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
 1331             inp->def_vrf_id);
 1332         SCTP_IPI_ADDR_RUNLOCK();
 1333         return (size);
 1334 }
 1335 
 1336 /*
 1337  * NOTE: assumes addr lock is held
 1338  */
 1339 static int
 1340 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
 1341 {
 1342         int cnt = 0;
 1343         struct sctp_vrf *vrf = NULL;
 1344 
 1345         /*
 1346          * In both sub-set bound an bound_all cases we return the MAXIMUM
 1347          * number of addresses that you COULD get. In reality the sub-set
 1348          * bound may have an exclusion list for a given TCB OR in the
 1349          * bound-all case a TCB may NOT include the loopback or other
 1350          * addresses as well.
 1351          */
 1352         vrf = sctp_find_vrf(vrf_id);
 1353         if (vrf == NULL) {
 1354                 return (0);
 1355         }
 1356         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 1357                 struct sctp_ifn *sctp_ifn;
 1358                 struct sctp_ifa *sctp_ifa;
 1359 
 1360                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
 1361                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
 1362                                 /* Count them if they are the right type */
 1363                                 if (sctp_ifa->address.sa.sa_family == AF_INET) {
 1364                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
 1365                                                 cnt += sizeof(struct sockaddr_in6);
 1366                                         else
 1367                                                 cnt += sizeof(struct sockaddr_in);
 1368 
 1369                                 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
 1370                                         cnt += sizeof(struct sockaddr_in6);
 1371                         }
 1372                 }
 1373         } else {
 1374                 struct sctp_laddr *laddr;
 1375 
 1376                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 1377                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
 1378                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
 1379                                         cnt += sizeof(struct sockaddr_in6);
 1380                                 else
 1381                                         cnt += sizeof(struct sockaddr_in);
 1382 
 1383                         } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
 1384                                 cnt += sizeof(struct sockaddr_in6);
 1385                 }
 1386         }
 1387         return (cnt);
 1388 }
 1389 
 1390 static int
 1391 sctp_count_max_addresses(struct sctp_inpcb *inp)
 1392 {
 1393         int cnt = 0;
 1394 
 1395         SCTP_IPI_ADDR_RLOCK();
 1396         /* count addresses for the endpoint's default VRF */
 1397         cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
 1398         SCTP_IPI_ADDR_RUNLOCK();
 1399         return (cnt);
 1400 }
 1401 
 1402 static int
 1403 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
 1404     size_t optsize, void *p, int delay)
 1405 {
 1406         int error = 0;
 1407         int creat_lock_on = 0;
 1408         struct sctp_tcb *stcb = NULL;
 1409         struct sockaddr *sa;
 1410         int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
 1411         int added = 0;
 1412         uint32_t vrf_id;
 1413         int bad_addresses = 0;
 1414         sctp_assoc_t *a_id;
 1415 
 1416         SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
 1417 
 1418         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
 1419             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
 1420                 /* We are already connected AND the TCP model */
 1421                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
 1422                 return (EADDRINUSE);
 1423         }
 1424         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
 1425             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
 1426                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 1427                 return (EINVAL);
 1428         }
 1429         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
 1430                 SCTP_INP_RLOCK(inp);
 1431                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
 1432                 SCTP_INP_RUNLOCK(inp);
 1433         }
 1434         if (stcb) {
 1435                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
 1436                 return (EALREADY);
 1437         }
 1438         SCTP_INP_INCR_REF(inp);
 1439         SCTP_ASOC_CREATE_LOCK(inp);
 1440         creat_lock_on = 1;
 1441         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
 1442             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
 1443                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
 1444                 error = EFAULT;
 1445                 goto out_now;
 1446         }
 1447         totaddrp = (int *)optval;
 1448         totaddr = *totaddrp;
 1449         sa = (struct sockaddr *)(totaddrp + 1);
 1450         stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
 1451         if ((stcb != NULL) || bad_addresses) {
 1452                 /* Already have or am bring up an association */
 1453                 SCTP_ASOC_CREATE_UNLOCK(inp);
 1454                 creat_lock_on = 0;
 1455                 if (stcb)
 1456                         SCTP_TCB_UNLOCK(stcb);
 1457                 if (bad_addresses == 0) {
 1458                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
 1459                         error = EALREADY;
 1460                 }
 1461                 goto out_now;
 1462         }
 1463 #ifdef INET6
 1464         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
 1465             (num_v6 > 0)) {
 1466                 error = EINVAL;
 1467                 goto out_now;
 1468         }
 1469         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
 1470             (num_v4 > 0)) {
 1471                 struct in6pcb *inp6;
 1472 
 1473                 inp6 = (struct in6pcb *)inp;
 1474                 if (SCTP_IPV6_V6ONLY(inp6)) {
 1475                         /*
 1476                          * if IPV6_V6ONLY flag, ignore connections destined
 1477                          * to a v4 addr or v4-mapped addr
 1478                          */
 1479                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 1480                         error = EINVAL;
 1481                         goto out_now;
 1482                 }
 1483         }
 1484 #endif                          /* INET6 */
 1485         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
 1486             SCTP_PCB_FLAGS_UNBOUND) {
 1487                 /* Bind a ephemeral port */
 1488                 error = sctp_inpcb_bind(so, NULL, NULL, p);
 1489                 if (error) {
 1490                         goto out_now;
 1491                 }
 1492         }
 1493         /* FIX ME: do we want to pass in a vrf on the connect call? */
 1494         vrf_id = inp->def_vrf_id;
 1495 
 1496 
 1497         /* We are GOOD to go */
 1498         stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
 1499             (struct thread *)p
 1500             );
 1501         if (stcb == NULL) {
 1502                 /* Gak! no memory */
 1503                 goto out_now;
 1504         }
 1505         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
 1506         /* move to second address */
 1507         if (sa->sa_family == AF_INET)
 1508                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
 1509         else
 1510                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
 1511 
 1512         error = 0;
 1513         added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
 1514         /* Fill in the return id */
 1515         if (error) {
 1516                 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
 1517                 goto out_now;
 1518         }
 1519         a_id = (sctp_assoc_t *) optval;
 1520         *a_id = sctp_get_associd(stcb);
 1521 
 1522         /* initialize authentication parameters for the assoc */
 1523         sctp_initialize_auth_params(inp, stcb);
 1524 
 1525         if (delay) {
 1526                 /* doing delayed connection */
 1527                 stcb->asoc.delayed_connection = 1;
 1528                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
 1529         } else {
 1530                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
 1531                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
 1532         }
 1533         SCTP_TCB_UNLOCK(stcb);
 1534         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
 1535                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
 1536                 /* Set the connected flag so we can queue data */
 1537                 soisconnecting(so);
 1538         }
 1539 out_now:
 1540         if (creat_lock_on) {
 1541                 SCTP_ASOC_CREATE_UNLOCK(inp);
 1542         }
 1543         SCTP_INP_DECR_REF(inp);
 1544         return error;
 1545 }
 1546 
 1547 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
 1548         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
 1549             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
 1550                 SCTP_INP_RLOCK(inp); \
 1551                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
 1552                 if (stcb) { \
 1553                         SCTP_TCB_LOCK(stcb); \
 1554                 } \
 1555                 SCTP_INP_RUNLOCK(inp); \
 1556         } else if (assoc_id != 0) { \
 1557                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
 1558                 if (stcb == NULL) { \
 1559                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
 1560                         error = ENOENT; \
 1561                         break; \
 1562                 } \
 1563         } else { \
 1564                 stcb = NULL; \
 1565         } \
 1566   }
 1567 
 1568 
 1569 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
 1570         if (size < sizeof(type)) { \
 1571                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
 1572                 error = EINVAL; \
 1573                 break; \
 1574         } else { \
 1575                 destp = (type *)srcp; \
 1576         } \
 1577       }
 1578 
 1579 static int
 1580 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
 1581     void *p)
 1582 {
 1583         struct sctp_inpcb *inp = NULL;
 1584         int error, val = 0;
 1585         struct sctp_tcb *stcb = NULL;
 1586 
 1587         if (optval == NULL) {
 1588                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 1589                 return (EINVAL);
 1590         }
 1591         inp = (struct sctp_inpcb *)so->so_pcb;
 1592         if (inp == 0) {
 1593                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 1594                 return EINVAL;
 1595         }
 1596         error = 0;
 1597 
 1598         switch (optname) {
 1599         case SCTP_NODELAY:
 1600         case SCTP_AUTOCLOSE:
 1601         case SCTP_EXPLICIT_EOR:
 1602         case SCTP_AUTO_ASCONF:
 1603         case SCTP_DISABLE_FRAGMENTS:
 1604         case SCTP_I_WANT_MAPPED_V4_ADDR:
 1605         case SCTP_USE_EXT_RCVINFO:
 1606                 SCTP_INP_RLOCK(inp);
 1607                 switch (optname) {
 1608                 case SCTP_DISABLE_FRAGMENTS:
 1609                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
 1610                         break;
 1611                 case SCTP_I_WANT_MAPPED_V4_ADDR:
 1612                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
 1613                         break;
 1614                 case SCTP_AUTO_ASCONF:
 1615                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 1616                                 /* only valid for bound all sockets */
 1617                                 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
 1618                         } else {
 1619                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 1620                                 error = EINVAL;
 1621                                 goto flags_out;
 1622                         }
 1623                         break;
 1624                 case SCTP_EXPLICIT_EOR:
 1625                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
 1626                         break;
 1627                 case SCTP_NODELAY:
 1628                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
 1629                         break;
 1630                 case SCTP_USE_EXT_RCVINFO:
 1631                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
 1632                         break;
 1633                 case SCTP_AUTOCLOSE:
 1634                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
 1635                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
 1636                         else
 1637                                 val = 0;
 1638                         break;
 1639 
 1640                 default:
 1641                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 1642                         error = ENOPROTOOPT;
 1643                 }               /* end switch (sopt->sopt_name) */
 1644                 if (optname != SCTP_AUTOCLOSE) {
 1645                         /* make it an "on/off" value */
 1646                         val = (val != 0);
 1647                 }
 1648                 if (*optsize < sizeof(val)) {
 1649                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 1650                         error = EINVAL;
 1651                 }
 1652 flags_out:
 1653                 SCTP_INP_RUNLOCK(inp);
 1654                 if (error == 0) {
 1655                         /* return the option value */
 1656                         *(int *)optval = val;
 1657                         *optsize = sizeof(val);
 1658                 }
 1659                 break;
 1660         case SCTP_GET_PACKET_LOG:
 1661                 {
 1662 #ifdef  SCTP_PACKET_LOGGING
 1663                         uint8_t *target;
 1664                         int ret;
 1665 
 1666                         SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
 1667                         ret = sctp_copy_out_packet_log(target, (int)*optsize);
 1668                         *optsize = ret;
 1669 #else
 1670                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
 1671                         error = EOPNOTSUPP;
 1672 #endif
 1673                         break;
 1674                 }
 1675         case SCTP_REUSE_PORT:
 1676                 {
 1677                         uint32_t *value;
 1678 
 1679                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
 1680                                 /* Can't do this for a 1-m socket */
 1681                                 error = EINVAL;
 1682                                 break;
 1683                         }
 1684                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 1685                         *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
 1686                         *optsize = sizeof(uint32_t);
 1687                 }
 1688                 break;
 1689         case SCTP_PARTIAL_DELIVERY_POINT:
 1690                 {
 1691                         uint32_t *value;
 1692 
 1693                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 1694                         *value = inp->partial_delivery_point;
 1695                         *optsize = sizeof(uint32_t);
 1696                 }
 1697                 break;
 1698         case SCTP_FRAGMENT_INTERLEAVE:
 1699                 {
 1700                         uint32_t *value;
 1701 
 1702                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 1703                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
 1704                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
 1705                                         *value = SCTP_FRAG_LEVEL_2;
 1706                                 } else {
 1707                                         *value = SCTP_FRAG_LEVEL_1;
 1708                                 }
 1709                         } else {
 1710                                 *value = SCTP_FRAG_LEVEL_0;
 1711                         }
 1712                         *optsize = sizeof(uint32_t);
 1713                 }
 1714                 break;
 1715         case SCTP_CMT_ON_OFF:
 1716                 {
 1717                         struct sctp_assoc_value *av;
 1718 
 1719                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
 1720                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
 1721                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 1722                                 if (stcb) {
 1723                                         av->assoc_value = stcb->asoc.sctp_cmt_on_off;
 1724                                         SCTP_TCB_UNLOCK(stcb);
 1725 
 1726                                 } else {
 1727                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 1728                                         error = ENOTCONN;
 1729                                 }
 1730                         } else {
 1731                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 1732                                 error = ENOPROTOOPT;
 1733                         }
 1734                         *optsize = sizeof(*av);
 1735                 }
 1736                 break;
 1737                 /* EY - set socket option for nr_sacks  */
 1738         case SCTP_NR_SACK_ON_OFF:
 1739                 {
 1740                         struct sctp_assoc_value *av;
 1741 
 1742                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
 1743                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
 1744                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 1745                                 if (stcb) {
 1746                                         av->assoc_value = stcb->asoc.sctp_nr_sack_on_off;
 1747                                         SCTP_TCB_UNLOCK(stcb);
 1748 
 1749                                 } else {
 1750                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 1751                                         error = ENOTCONN;
 1752                                 }
 1753                         } else {
 1754                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 1755                                 error = ENOPROTOOPT;
 1756                         }
 1757                         *optsize = sizeof(*av);
 1758                 }
 1759                 break;
 1760                 /* JRS - Get socket option for pluggable congestion control */
 1761         case SCTP_PLUGGABLE_CC:
 1762                 {
 1763                         struct sctp_assoc_value *av;
 1764 
 1765                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
 1766                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 1767                         if (stcb) {
 1768                                 av->assoc_value = stcb->asoc.congestion_control_module;
 1769                                 SCTP_TCB_UNLOCK(stcb);
 1770                         } else {
 1771                                 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
 1772                         }
 1773                         *optsize = sizeof(*av);
 1774                 }
 1775                 break;
 1776         case SCTP_GET_ADDR_LEN:
 1777                 {
 1778                         struct sctp_assoc_value *av;
 1779 
 1780                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
 1781                         error = EINVAL;
 1782 #ifdef INET
 1783                         if (av->assoc_value == AF_INET) {
 1784                                 av->assoc_value = sizeof(struct sockaddr_in);
 1785                                 error = 0;
 1786                         }
 1787 #endif
 1788 #ifdef INET6
 1789                         if (av->assoc_value == AF_INET6) {
 1790                                 av->assoc_value = sizeof(struct sockaddr_in6);
 1791                                 error = 0;
 1792                         }
 1793 #endif
 1794                         if (error) {
 1795                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 1796                         }
 1797                         *optsize = sizeof(*av);
 1798                 }
 1799                 break;
 1800         case SCTP_GET_ASSOC_NUMBER:
 1801                 {
 1802                         uint32_t *value, cnt;
 1803 
 1804                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 1805                         cnt = 0;
 1806                         SCTP_INP_RLOCK(inp);
 1807                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
 1808                                 cnt++;
 1809                         }
 1810                         SCTP_INP_RUNLOCK(inp);
 1811                         *value = cnt;
 1812                         *optsize = sizeof(uint32_t);
 1813                 }
 1814                 break;
 1815 
 1816         case SCTP_GET_ASSOC_ID_LIST:
 1817                 {
 1818                         struct sctp_assoc_ids *ids;
 1819                         unsigned int at, limit;
 1820 
 1821                         SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
 1822                         at = 0;
 1823                         limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
 1824                         SCTP_INP_RLOCK(inp);
 1825                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
 1826                                 if (at < limit) {
 1827                                         ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
 1828                                 } else {
 1829                                         error = EINVAL;
 1830                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 1831                                         break;
 1832                                 }
 1833                         }
 1834                         SCTP_INP_RUNLOCK(inp);
 1835                         ids->gaids_number_of_ids = at;
 1836                         *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
 1837                 }
 1838                 break;
 1839         case SCTP_CONTEXT:
 1840                 {
 1841                         struct sctp_assoc_value *av;
 1842 
 1843                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
 1844                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 1845 
 1846                         if (stcb) {
 1847                                 av->assoc_value = stcb->asoc.context;
 1848                                 SCTP_TCB_UNLOCK(stcb);
 1849                         } else {
 1850                                 SCTP_INP_RLOCK(inp);
 1851                                 av->assoc_value = inp->sctp_context;
 1852                                 SCTP_INP_RUNLOCK(inp);
 1853                         }
 1854                         *optsize = sizeof(*av);
 1855                 }
 1856                 break;
 1857         case SCTP_VRF_ID:
 1858                 {
 1859                         uint32_t *default_vrfid;
 1860 
 1861                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
 1862                         *default_vrfid = inp->def_vrf_id;
 1863                         break;
 1864                 }
 1865         case SCTP_GET_ASOC_VRF:
 1866                 {
 1867                         struct sctp_assoc_value *id;
 1868 
 1869                         SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
 1870                         SCTP_FIND_STCB(inp, stcb, id->assoc_id);
 1871                         if (stcb == NULL) {
 1872                                 error = EINVAL;
 1873                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 1874                                 break;
 1875                         }
 1876                         id->assoc_value = stcb->asoc.vrf_id;
 1877                         break;
 1878                 }
 1879         case SCTP_GET_VRF_IDS:
 1880                 {
 1881                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
 1882                         error = EOPNOTSUPP;
 1883                         break;
 1884                 }
 1885         case SCTP_GET_NONCE_VALUES:
 1886                 {
 1887                         struct sctp_get_nonce_values *gnv;
 1888 
 1889                         SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
 1890                         SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
 1891 
 1892                         if (stcb) {
 1893                                 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
 1894                                 gnv->gn_local_tag = stcb->asoc.my_vtag;
 1895                                 SCTP_TCB_UNLOCK(stcb);
 1896                         } else {
 1897                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 1898                                 error = ENOTCONN;
 1899                         }
 1900                         *optsize = sizeof(*gnv);
 1901                 }
 1902                 break;
 1903         case SCTP_DELAYED_SACK:
 1904                 {
 1905                         struct sctp_sack_info *sack;
 1906 
 1907                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
 1908                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
 1909                         if (stcb) {
 1910                                 sack->sack_delay = stcb->asoc.delayed_ack;
 1911                                 sack->sack_freq = stcb->asoc.sack_freq;
 1912                                 SCTP_TCB_UNLOCK(stcb);
 1913                         } else {
 1914                                 SCTP_INP_RLOCK(inp);
 1915                                 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
 1916                                 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
 1917                                 SCTP_INP_RUNLOCK(inp);
 1918                         }
 1919                         *optsize = sizeof(*sack);
 1920                 }
 1921                 break;
 1922 
 1923         case SCTP_GET_SNDBUF_USE:
 1924                 {
 1925                         struct sctp_sockstat *ss;
 1926 
 1927                         SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
 1928                         SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
 1929 
 1930                         if (stcb) {
 1931                                 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
 1932                                 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
 1933                                     stcb->asoc.size_on_all_streams);
 1934                                 SCTP_TCB_UNLOCK(stcb);
 1935                         } else {
 1936                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 1937                                 error = ENOTCONN;
 1938                         }
 1939                         *optsize = sizeof(struct sctp_sockstat);
 1940                 }
 1941                 break;
 1942         case SCTP_MAX_BURST:
 1943                 {
 1944                         uint8_t *value;
 1945 
 1946                         SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
 1947 
 1948                         SCTP_INP_RLOCK(inp);
 1949                         *value = inp->sctp_ep.max_burst;
 1950                         SCTP_INP_RUNLOCK(inp);
 1951                         *optsize = sizeof(uint8_t);
 1952                 }
 1953                 break;
 1954         case SCTP_MAXSEG:
 1955                 {
 1956                         struct sctp_assoc_value *av;
 1957                         int ovh;
 1958 
 1959                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
 1960                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 1961 
 1962                         if (stcb) {
 1963                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
 1964                                 SCTP_TCB_UNLOCK(stcb);
 1965                         } else {
 1966                                 SCTP_INP_RLOCK(inp);
 1967                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 1968                                         ovh = SCTP_MED_OVERHEAD;
 1969                                 } else {
 1970                                         ovh = SCTP_MED_V4_OVERHEAD;
 1971                                 }
 1972                                 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
 1973                                         av->assoc_value = 0;
 1974                                 else
 1975                                         av->assoc_value = inp->sctp_frag_point - ovh;
 1976                                 SCTP_INP_RUNLOCK(inp);
 1977                         }
 1978                         *optsize = sizeof(struct sctp_assoc_value);
 1979                 }
 1980                 break;
 1981         case SCTP_GET_STAT_LOG:
 1982                 error = sctp_fill_stat_log(optval, optsize);
 1983                 break;
 1984         case SCTP_EVENTS:
 1985                 {
 1986                         struct sctp_event_subscribe *events;
 1987 
 1988                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
 1989                         memset(events, 0, sizeof(*events));
 1990                         SCTP_INP_RLOCK(inp);
 1991                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
 1992                                 events->sctp_data_io_event = 1;
 1993 
 1994                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
 1995                                 events->sctp_association_event = 1;
 1996 
 1997                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
 1998                                 events->sctp_address_event = 1;
 1999 
 2000                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
 2001                                 events->sctp_send_failure_event = 1;
 2002 
 2003                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
 2004                                 events->sctp_peer_error_event = 1;
 2005 
 2006                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
 2007                                 events->sctp_shutdown_event = 1;
 2008 
 2009                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
 2010                                 events->sctp_partial_delivery_event = 1;
 2011 
 2012                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
 2013                                 events->sctp_adaptation_layer_event = 1;
 2014 
 2015                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
 2016                                 events->sctp_authentication_event = 1;
 2017 
 2018                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
 2019                                 events->sctp_sender_dry_event = 1;
 2020 
 2021                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
 2022                                 events->sctp_stream_reset_event = 1;
 2023                         SCTP_INP_RUNLOCK(inp);
 2024                         *optsize = sizeof(struct sctp_event_subscribe);
 2025                 }
 2026                 break;
 2027 
 2028         case SCTP_ADAPTATION_LAYER:
 2029                 {
 2030                         uint32_t *value;
 2031 
 2032                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 2033 
 2034                         SCTP_INP_RLOCK(inp);
 2035                         *value = inp->sctp_ep.adaptation_layer_indicator;
 2036                         SCTP_INP_RUNLOCK(inp);
 2037                         *optsize = sizeof(uint32_t);
 2038                 }
 2039                 break;
 2040         case SCTP_SET_INITIAL_DBG_SEQ:
 2041                 {
 2042                         uint32_t *value;
 2043 
 2044                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 2045                         SCTP_INP_RLOCK(inp);
 2046                         *value = inp->sctp_ep.initial_sequence_debug;
 2047                         SCTP_INP_RUNLOCK(inp);
 2048                         *optsize = sizeof(uint32_t);
 2049                 }
 2050                 break;
 2051         case SCTP_GET_LOCAL_ADDR_SIZE:
 2052                 {
 2053                         uint32_t *value;
 2054 
 2055                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 2056                         SCTP_INP_RLOCK(inp);
 2057                         *value = sctp_count_max_addresses(inp);
 2058                         SCTP_INP_RUNLOCK(inp);
 2059                         *optsize = sizeof(uint32_t);
 2060                 }
 2061                 break;
 2062         case SCTP_GET_REMOTE_ADDR_SIZE:
 2063                 {
 2064                         uint32_t *value;
 2065                         size_t size;
 2066                         struct sctp_nets *net;
 2067 
 2068                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
 2069                         /* FIXME MT: change to sctp_assoc_value? */
 2070                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
 2071 
 2072                         if (stcb) {
 2073                                 size = 0;
 2074                                 /* Count the sizes */
 2075                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 2076                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
 2077                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
 2078                                                 size += sizeof(struct sockaddr_in6);
 2079                                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
 2080                                                 size += sizeof(struct sockaddr_in);
 2081                                         } else {
 2082                                                 /* huh */
 2083                                                 break;
 2084                                         }
 2085                                 }
 2086                                 SCTP_TCB_UNLOCK(stcb);
 2087                                 *value = (uint32_t) size;
 2088                         } else {
 2089                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 2090                                 error = ENOTCONN;
 2091                         }
 2092                         *optsize = sizeof(uint32_t);
 2093                 }
 2094                 break;
 2095         case SCTP_GET_PEER_ADDRESSES:
 2096                 /*
 2097                  * Get the address information, an array is passed in to
 2098                  * fill up we pack it.
 2099                  */
 2100                 {
 2101                         size_t cpsz, left;
 2102                         struct sockaddr_storage *sas;
 2103                         struct sctp_nets *net;
 2104                         struct sctp_getaddresses *saddr;
 2105 
 2106                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
 2107                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
 2108 
 2109                         if (stcb) {
 2110                                 left = (*optsize) - sizeof(struct sctp_getaddresses);
 2111                                 *optsize = sizeof(struct sctp_getaddresses);
 2112                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
 2113 
 2114                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 2115                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
 2116                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
 2117                                                 cpsz = sizeof(struct sockaddr_in6);
 2118                                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
 2119                                                 cpsz = sizeof(struct sockaddr_in);
 2120                                         } else {
 2121                                                 /* huh */
 2122                                                 break;
 2123                                         }
 2124                                         if (left < cpsz) {
 2125                                                 /* not enough room. */
 2126                                                 break;
 2127                                         }
 2128 #ifdef INET6
 2129                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
 2130                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
 2131                                                 /* Must map the address */
 2132                                                 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
 2133                                                     (struct sockaddr_in6 *)sas);
 2134                                         } else {
 2135 #endif
 2136                                                 memcpy(sas, &net->ro._l_addr, cpsz);
 2137 #ifdef INET6
 2138                                         }
 2139 #endif
 2140                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
 2141 
 2142                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
 2143                                         left -= cpsz;
 2144                                         *optsize += cpsz;
 2145                                 }
 2146                                 SCTP_TCB_UNLOCK(stcb);
 2147                         } else {
 2148                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 2149                                 error = ENOENT;
 2150                         }
 2151                 }
 2152                 break;
 2153         case SCTP_GET_LOCAL_ADDRESSES:
 2154                 {
 2155                         size_t limit, actual;
 2156                         struct sockaddr_storage *sas;
 2157                         struct sctp_getaddresses *saddr;
 2158 
 2159                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
 2160                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
 2161 
 2162                         sas = (struct sockaddr_storage *)&saddr->addr[0];
 2163                         limit = *optsize - sizeof(sctp_assoc_t);
 2164                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
 2165                         if (stcb) {
 2166                                 SCTP_TCB_UNLOCK(stcb);
 2167                         }
 2168                         *optsize = sizeof(struct sockaddr_storage) + actual;
 2169                 }
 2170                 break;
 2171         case SCTP_PEER_ADDR_PARAMS:
 2172                 {
 2173                         struct sctp_paddrparams *paddrp;
 2174                         struct sctp_nets *net;
 2175 
 2176                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
 2177                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
 2178 
 2179                         net = NULL;
 2180                         if (stcb) {
 2181                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
 2182                         } else {
 2183                                 /*
 2184                                  * We increment here since
 2185                                  * sctp_findassociation_ep_addr() wil do a
 2186                                  * decrement if it finds the stcb as long as
 2187                                  * the locked tcb (last argument) is NOT a
 2188                                  * TCB.. aka NULL.
 2189                                  */
 2190                                 SCTP_INP_INCR_REF(inp);
 2191                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
 2192                                 if (stcb == NULL) {
 2193                                         SCTP_INP_DECR_REF(inp);
 2194                                 }
 2195                         }
 2196                         if (stcb && (net == NULL)) {
 2197                                 struct sockaddr *sa;
 2198 
 2199                                 sa = (struct sockaddr *)&paddrp->spp_address;
 2200                                 if (sa->sa_family == AF_INET) {
 2201                                         struct sockaddr_in *sin;
 2202 
 2203                                         sin = (struct sockaddr_in *)sa;
 2204                                         if (sin->sin_addr.s_addr) {
 2205                                                 error = EINVAL;
 2206                                                 SCTP_TCB_UNLOCK(stcb);
 2207                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2208                                                 break;
 2209                                         }
 2210                                 } else if (sa->sa_family == AF_INET6) {
 2211                                         struct sockaddr_in6 *sin6;
 2212 
 2213                                         sin6 = (struct sockaddr_in6 *)sa;
 2214                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 2215                                                 error = EINVAL;
 2216                                                 SCTP_TCB_UNLOCK(stcb);
 2217                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2218                                                 break;
 2219                                         }
 2220                                 } else {
 2221                                         error = EAFNOSUPPORT;
 2222                                         SCTP_TCB_UNLOCK(stcb);
 2223                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2224                                         break;
 2225                                 }
 2226                         }
 2227                         if (stcb) {
 2228                                 /* Applys to the specific association */
 2229                                 paddrp->spp_flags = 0;
 2230                                 if (net) {
 2231                                         int ovh;
 2232 
 2233                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 2234                                                 ovh = SCTP_MED_OVERHEAD;
 2235                                         } else {
 2236                                                 ovh = SCTP_MED_V4_OVERHEAD;
 2237                                         }
 2238 
 2239 
 2240                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
 2241                                         paddrp->spp_pathmtu = net->mtu - ovh;
 2242                                         /* get flags for HB */
 2243                                         if (net->dest_state & SCTP_ADDR_NOHB)
 2244                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
 2245                                         else
 2246                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
 2247                                         /* get flags for PMTU */
 2248                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
 2249                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
 2250                                         } else {
 2251                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
 2252                                         }
 2253 #ifdef INET
 2254                                         if (net->ro._l_addr.sin.sin_family == AF_INET) {
 2255                                                 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
 2256                                                 paddrp->spp_flags |= SPP_IPV4_TOS;
 2257                                         }
 2258 #endif
 2259 #ifdef INET6
 2260                                         if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
 2261                                                 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
 2262                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
 2263                                         }
 2264 #endif
 2265                                 } else {
 2266                                         /*
 2267                                          * No destination so return default
 2268                                          * value
 2269                                          */
 2270                                         int cnt = 0;
 2271 
 2272                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
 2273                                         paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
 2274 #ifdef INET
 2275                                         paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
 2276                                         paddrp->spp_flags |= SPP_IPV4_TOS;
 2277 #endif
 2278 #ifdef INET6
 2279                                         paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
 2280                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
 2281 #endif
 2282                                         /* default settings should be these */
 2283                                         if (stcb->asoc.hb_is_disabled == 0) {
 2284                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
 2285                                         } else {
 2286                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
 2287                                         }
 2288                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 2289                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
 2290                                                         cnt++;
 2291                                                 }
 2292                                         }
 2293                                         if (cnt) {
 2294                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
 2295                                         }
 2296                                 }
 2297                                 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
 2298                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
 2299                                 SCTP_TCB_UNLOCK(stcb);
 2300                         } else {
 2301                                 /* Use endpoint defaults */
 2302                                 SCTP_INP_RLOCK(inp);
 2303                                 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
 2304                                 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
 2305                                 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
 2306                                 /* get inp's default */
 2307 #ifdef INET
 2308                                 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
 2309                                 paddrp->spp_flags |= SPP_IPV4_TOS;
 2310 #endif
 2311 #ifdef INET6
 2312                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 2313                                         paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
 2314                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
 2315                                 }
 2316 #endif
 2317                                 /* can't return this */
 2318                                 paddrp->spp_pathmtu = 0;
 2319 
 2320                                 /* default behavior, no stcb */
 2321                                 paddrp->spp_flags = SPP_PMTUD_ENABLE;
 2322 
 2323                                 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
 2324                                         paddrp->spp_flags |= SPP_HB_ENABLE;
 2325                                 } else {
 2326                                         paddrp->spp_flags |= SPP_HB_DISABLE;
 2327                                 }
 2328                                 SCTP_INP_RUNLOCK(inp);
 2329                         }
 2330                         *optsize = sizeof(struct sctp_paddrparams);
 2331                 }
 2332                 break;
 2333         case SCTP_GET_PEER_ADDR_INFO:
 2334                 {
 2335                         struct sctp_paddrinfo *paddri;
 2336                         struct sctp_nets *net;
 2337 
 2338                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
 2339                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
 2340 
 2341                         net = NULL;
 2342                         if (stcb) {
 2343                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
 2344                         } else {
 2345                                 /*
 2346                                  * We increment here since
 2347                                  * sctp_findassociation_ep_addr() wil do a
 2348                                  * decrement if it finds the stcb as long as
 2349                                  * the locked tcb (last argument) is NOT a
 2350                                  * TCB.. aka NULL.
 2351                                  */
 2352                                 SCTP_INP_INCR_REF(inp);
 2353                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
 2354                                 if (stcb == NULL) {
 2355                                         SCTP_INP_DECR_REF(inp);
 2356                                 }
 2357                         }
 2358 
 2359                         if ((stcb) && (net)) {
 2360                                 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
 2361                                 paddri->spinfo_cwnd = net->cwnd;
 2362                                 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
 2363                                 paddri->spinfo_rto = net->RTO;
 2364                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
 2365                                 SCTP_TCB_UNLOCK(stcb);
 2366                         } else {
 2367                                 if (stcb) {
 2368                                         SCTP_TCB_UNLOCK(stcb);
 2369                                 }
 2370                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 2371                                 error = ENOENT;
 2372                         }
 2373                         *optsize = sizeof(struct sctp_paddrinfo);
 2374                 }
 2375                 break;
 2376         case SCTP_PCB_STATUS:
 2377                 {
 2378                         struct sctp_pcbinfo *spcb;
 2379 
 2380                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
 2381                         sctp_fill_pcbinfo(spcb);
 2382                         *optsize = sizeof(struct sctp_pcbinfo);
 2383                 }
 2384                 break;
 2385 
 2386         case SCTP_STATUS:
 2387                 {
 2388                         struct sctp_nets *net;
 2389                         struct sctp_status *sstat;
 2390 
 2391                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
 2392                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
 2393 
 2394                         if (stcb == NULL) {
 2395                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2396                                 error = EINVAL;
 2397                                 break;
 2398                         }
 2399                         /*
 2400                          * I think passing the state is fine since
 2401                          * sctp_constants.h will be available to the user
 2402                          * land.
 2403                          */
 2404                         sstat->sstat_state = stcb->asoc.state;
 2405                         sstat->sstat_assoc_id = sctp_get_associd(stcb);
 2406                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
 2407                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
 2408                         /*
 2409                          * We can't include chunks that have been passed to
 2410                          * the socket layer. Only things in queue.
 2411                          */
 2412                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
 2413                             stcb->asoc.cnt_on_all_streams);
 2414 
 2415 
 2416                         sstat->sstat_instrms = stcb->asoc.streamincnt;
 2417                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
 2418                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
 2419                         memcpy(&sstat->sstat_primary.spinfo_address,
 2420                             &stcb->asoc.primary_destination->ro._l_addr,
 2421                             ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
 2422                         net = stcb->asoc.primary_destination;
 2423                         ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
 2424                         /*
 2425                          * Again the user can get info from sctp_constants.h
 2426                          * for what the state of the network is.
 2427                          */
 2428                         sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
 2429                         sstat->sstat_primary.spinfo_cwnd = net->cwnd;
 2430                         sstat->sstat_primary.spinfo_srtt = net->lastsa;
 2431                         sstat->sstat_primary.spinfo_rto = net->RTO;
 2432                         sstat->sstat_primary.spinfo_mtu = net->mtu;
 2433                         sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
 2434                         SCTP_TCB_UNLOCK(stcb);
 2435                         *optsize = sizeof(*sstat);
 2436                 }
 2437                 break;
 2438         case SCTP_RTOINFO:
 2439                 {
 2440                         struct sctp_rtoinfo *srto;
 2441 
 2442                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
 2443                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
 2444 
 2445                         if (stcb) {
 2446                                 srto->srto_initial = stcb->asoc.initial_rto;
 2447                                 srto->srto_max = stcb->asoc.maxrto;
 2448                                 srto->srto_min = stcb->asoc.minrto;
 2449                                 SCTP_TCB_UNLOCK(stcb);
 2450                         } else {
 2451                                 SCTP_INP_RLOCK(inp);
 2452                                 srto->srto_initial = inp->sctp_ep.initial_rto;
 2453                                 srto->srto_max = inp->sctp_ep.sctp_maxrto;
 2454                                 srto->srto_min = inp->sctp_ep.sctp_minrto;
 2455                                 SCTP_INP_RUNLOCK(inp);
 2456                         }
 2457                         *optsize = sizeof(*srto);
 2458                 }
 2459                 break;
 2460         case SCTP_ASSOCINFO:
 2461                 {
 2462                         struct sctp_assocparams *sasoc;
 2463                         uint32_t oldval;
 2464 
 2465                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
 2466                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
 2467 
 2468                         if (stcb) {
 2469                                 oldval = sasoc->sasoc_cookie_life;
 2470                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
 2471                                 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
 2472                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
 2473                                 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
 2474                                 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
 2475                                 SCTP_TCB_UNLOCK(stcb);
 2476                         } else {
 2477                                 SCTP_INP_RLOCK(inp);
 2478                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
 2479                                 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
 2480                                 sasoc->sasoc_number_peer_destinations = 0;
 2481                                 sasoc->sasoc_peer_rwnd = 0;
 2482                                 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
 2483                                 SCTP_INP_RUNLOCK(inp);
 2484                         }
 2485                         *optsize = sizeof(*sasoc);
 2486                 }
 2487                 break;
 2488         case SCTP_DEFAULT_SEND_PARAM:
 2489                 {
 2490                         struct sctp_sndrcvinfo *s_info;
 2491 
 2492                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
 2493                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
 2494 
 2495                         if (stcb) {
 2496                                 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
 2497                                 SCTP_TCB_UNLOCK(stcb);
 2498                         } else {
 2499                                 SCTP_INP_RLOCK(inp);
 2500                                 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
 2501                                 SCTP_INP_RUNLOCK(inp);
 2502                         }
 2503                         *optsize = sizeof(*s_info);
 2504                 }
 2505                 break;
 2506         case SCTP_INITMSG:
 2507                 {
 2508                         struct sctp_initmsg *sinit;
 2509 
 2510                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
 2511                         SCTP_INP_RLOCK(inp);
 2512                         sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
 2513                         sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
 2514                         sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
 2515                         sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
 2516                         SCTP_INP_RUNLOCK(inp);
 2517                         *optsize = sizeof(*sinit);
 2518                 }
 2519                 break;
 2520         case SCTP_PRIMARY_ADDR:
 2521                 /* we allow a "get" operation on this */
 2522                 {
 2523                         struct sctp_setprim *ssp;
 2524 
 2525                         SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
 2526                         SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
 2527 
 2528                         if (stcb) {
 2529                                 /* simply copy out the sockaddr_storage... */
 2530                                 int len;
 2531 
 2532                                 len = *optsize;
 2533                                 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
 2534                                         len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
 2535 
 2536                                 memcpy(&ssp->ssp_addr,
 2537                                     &stcb->asoc.primary_destination->ro._l_addr,
 2538                                     len);
 2539                                 SCTP_TCB_UNLOCK(stcb);
 2540                         } else {
 2541                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2542                                 error = EINVAL;
 2543                         }
 2544                         *optsize = sizeof(*ssp);
 2545                 }
 2546                 break;
 2547 
 2548         case SCTP_HMAC_IDENT:
 2549                 {
 2550                         struct sctp_hmacalgo *shmac;
 2551                         sctp_hmaclist_t *hmaclist;
 2552                         uint32_t size;
 2553                         int i;
 2554 
 2555                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
 2556 
 2557                         SCTP_INP_RLOCK(inp);
 2558                         hmaclist = inp->sctp_ep.local_hmacs;
 2559                         if (hmaclist == NULL) {
 2560                                 /* no HMACs to return */
 2561                                 *optsize = sizeof(*shmac);
 2562                                 SCTP_INP_RUNLOCK(inp);
 2563                                 break;
 2564                         }
 2565                         /* is there room for all of the hmac ids? */
 2566                         size = sizeof(*shmac) + (hmaclist->num_algo *
 2567                             sizeof(shmac->shmac_idents[0]));
 2568                         if ((size_t)(*optsize) < size) {
 2569                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2570                                 error = EINVAL;
 2571                                 SCTP_INP_RUNLOCK(inp);
 2572                                 break;
 2573                         }
 2574                         /* copy in the list */
 2575                         shmac->shmac_number_of_idents = hmaclist->num_algo;
 2576                         for (i = 0; i < hmaclist->num_algo; i++) {
 2577                                 shmac->shmac_idents[i] = hmaclist->hmac[i];
 2578                         }
 2579                         SCTP_INP_RUNLOCK(inp);
 2580                         *optsize = size;
 2581                         break;
 2582                 }
 2583         case SCTP_AUTH_ACTIVE_KEY:
 2584                 {
 2585                         struct sctp_authkeyid *scact;
 2586 
 2587                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
 2588                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
 2589 
 2590                         if (stcb) {
 2591                                 /* get the active key on the assoc */
 2592                                 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
 2593                                 SCTP_TCB_UNLOCK(stcb);
 2594                         } else {
 2595                                 /* get the endpoint active key */
 2596                                 SCTP_INP_RLOCK(inp);
 2597                                 scact->scact_keynumber = inp->sctp_ep.default_keyid;
 2598                                 SCTP_INP_RUNLOCK(inp);
 2599                         }
 2600                         *optsize = sizeof(*scact);
 2601                         break;
 2602                 }
 2603         case SCTP_LOCAL_AUTH_CHUNKS:
 2604                 {
 2605                         struct sctp_authchunks *sac;
 2606                         sctp_auth_chklist_t *chklist = NULL;
 2607                         size_t size = 0;
 2608 
 2609                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
 2610                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
 2611 
 2612                         if (stcb) {
 2613                                 /* get off the assoc */
 2614                                 chklist = stcb->asoc.local_auth_chunks;
 2615                                 /* is there enough space? */
 2616                                 size = sctp_auth_get_chklist_size(chklist);
 2617                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
 2618                                         error = EINVAL;
 2619                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2620                                 } else {
 2621                                         /* copy in the chunks */
 2622                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
 2623                                 }
 2624                                 SCTP_TCB_UNLOCK(stcb);
 2625                         } else {
 2626                                 /* get off the endpoint */
 2627                                 SCTP_INP_RLOCK(inp);
 2628                                 chklist = inp->sctp_ep.local_auth_chunks;
 2629                                 /* is there enough space? */
 2630                                 size = sctp_auth_get_chklist_size(chklist);
 2631                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
 2632                                         error = EINVAL;
 2633                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2634                                 } else {
 2635                                         /* copy in the chunks */
 2636                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
 2637                                 }
 2638                                 SCTP_INP_RUNLOCK(inp);
 2639                         }
 2640                         *optsize = sizeof(struct sctp_authchunks) + size;
 2641                         break;
 2642                 }
 2643         case SCTP_PEER_AUTH_CHUNKS:
 2644                 {
 2645                         struct sctp_authchunks *sac;
 2646                         sctp_auth_chklist_t *chklist = NULL;
 2647                         size_t size = 0;
 2648 
 2649                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
 2650                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
 2651 
 2652                         if (stcb) {
 2653                                 /* get off the assoc */
 2654                                 chklist = stcb->asoc.peer_auth_chunks;
 2655                                 /* is there enough space? */
 2656                                 size = sctp_auth_get_chklist_size(chklist);
 2657                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
 2658                                         error = EINVAL;
 2659                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 2660                                 } else {
 2661                                         /* copy in the chunks */
 2662                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
 2663                                 }
 2664                                 SCTP_TCB_UNLOCK(stcb);
 2665                         } else {
 2666                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 2667                                 error = ENOENT;
 2668                         }
 2669                         *optsize = sizeof(struct sctp_authchunks) + size;
 2670                         break;
 2671                 }
 2672 
 2673 
 2674         default:
 2675                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 2676                 error = ENOPROTOOPT;
 2677                 *optsize = 0;
 2678                 break;
 2679         }                       /* end switch (sopt->sopt_name) */
 2680         return (error);
 2681 }
 2682 
 2683 static int
 2684 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
 2685     void *p)
 2686 {
 2687         int error, set_opt;
 2688         uint32_t *mopt;
 2689         struct sctp_tcb *stcb = NULL;
 2690         struct sctp_inpcb *inp = NULL;
 2691         uint32_t vrf_id;
 2692 
 2693         if (optval == NULL) {
 2694                 SCTP_PRINTF("optval is NULL\n");
 2695                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2696                 return (EINVAL);
 2697         }
 2698         inp = (struct sctp_inpcb *)so->so_pcb;
 2699         if (inp == 0) {
 2700                 SCTP_PRINTF("inp is NULL?\n");
 2701                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2702                 return EINVAL;
 2703         }
 2704         vrf_id = inp->def_vrf_id;
 2705 
 2706         error = 0;
 2707         switch (optname) {
 2708         case SCTP_NODELAY:
 2709         case SCTP_AUTOCLOSE:
 2710         case SCTP_AUTO_ASCONF:
 2711         case SCTP_EXPLICIT_EOR:
 2712         case SCTP_DISABLE_FRAGMENTS:
 2713         case SCTP_USE_EXT_RCVINFO:
 2714         case SCTP_I_WANT_MAPPED_V4_ADDR:
 2715                 /* copy in the option value */
 2716                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
 2717                 set_opt = 0;
 2718                 if (error)
 2719                         break;
 2720                 switch (optname) {
 2721                 case SCTP_DISABLE_FRAGMENTS:
 2722                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
 2723                         break;
 2724                 case SCTP_AUTO_ASCONF:
 2725                         /*
 2726                          * NOTE: we don't really support this flag
 2727                          */
 2728                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 2729                                 /* only valid for bound all sockets */
 2730                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
 2731                         } else {
 2732                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2733                                 return (EINVAL);
 2734                         }
 2735                         break;
 2736                 case SCTP_EXPLICIT_EOR:
 2737                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
 2738                         break;
 2739                 case SCTP_USE_EXT_RCVINFO:
 2740                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
 2741                         break;
 2742                 case SCTP_I_WANT_MAPPED_V4_ADDR:
 2743                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 2744                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
 2745                         } else {
 2746                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2747                                 return (EINVAL);
 2748                         }
 2749                         break;
 2750                 case SCTP_NODELAY:
 2751                         set_opt = SCTP_PCB_FLAGS_NODELAY;
 2752                         break;
 2753                 case SCTP_AUTOCLOSE:
 2754                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 2755                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
 2756                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2757                                 return (EINVAL);
 2758                         }
 2759                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
 2760                         /*
 2761                          * The value is in ticks. Note this does not effect
 2762                          * old associations, only new ones.
 2763                          */
 2764                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
 2765                         break;
 2766                 }
 2767                 SCTP_INP_WLOCK(inp);
 2768                 if (*mopt != 0) {
 2769                         sctp_feature_on(inp, set_opt);
 2770                 } else {
 2771                         sctp_feature_off(inp, set_opt);
 2772                 }
 2773                 SCTP_INP_WUNLOCK(inp);
 2774                 break;
 2775         case SCTP_REUSE_PORT:
 2776                 {
 2777                         SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
 2778                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
 2779                                 /* Can't set it after we are bound */
 2780                                 error = EINVAL;
 2781                                 break;
 2782                         }
 2783                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
 2784                                 /* Can't do this for a 1-m socket */
 2785                                 error = EINVAL;
 2786                                 break;
 2787                         }
 2788                         if (optval)
 2789                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
 2790                         else
 2791                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
 2792                 }
 2793                 break;
 2794         case SCTP_PARTIAL_DELIVERY_POINT:
 2795                 {
 2796                         uint32_t *value;
 2797 
 2798                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
 2799                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
 2800                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2801                                 error = EINVAL;
 2802                                 break;
 2803                         }
 2804                         inp->partial_delivery_point = *value;
 2805                 }
 2806                 break;
 2807         case SCTP_FRAGMENT_INTERLEAVE:
 2808                 /* not yet until we re-write sctp_recvmsg() */
 2809                 {
 2810                         uint32_t *level;
 2811 
 2812                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
 2813                         if (*level == SCTP_FRAG_LEVEL_2) {
 2814                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
 2815                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
 2816                         } else if (*level == SCTP_FRAG_LEVEL_1) {
 2817                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
 2818                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
 2819                         } else if (*level == SCTP_FRAG_LEVEL_0) {
 2820                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
 2821                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
 2822 
 2823                         } else {
 2824                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2825                                 error = EINVAL;
 2826                         }
 2827                 }
 2828                 break;
 2829         case SCTP_CMT_ON_OFF:
 2830                 {
 2831                         struct sctp_assoc_value *av;
 2832 
 2833                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
 2834                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
 2835                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 2836                                 if (stcb) {
 2837                                         stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
 2838                                         SCTP_TCB_UNLOCK(stcb);
 2839                                 } else {
 2840                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 2841                                         error = ENOTCONN;
 2842                                 }
 2843                         } else {
 2844                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 2845                                 error = ENOPROTOOPT;
 2846                         }
 2847                 }
 2848                 break;
 2849                 /* EY nr_sack_on_off socket option */
 2850         case SCTP_NR_SACK_ON_OFF:
 2851                 {
 2852                         struct sctp_assoc_value *av;
 2853 
 2854                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
 2855                         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
 2856                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 2857                                 if (stcb) {
 2858                                         stcb->asoc.sctp_nr_sack_on_off = (uint8_t) av->assoc_value;
 2859                                         SCTP_TCB_UNLOCK(stcb);
 2860                                 } else {
 2861                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 2862                                         error = ENOTCONN;
 2863                                 }
 2864                         } else {
 2865                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 2866                                 error = ENOPROTOOPT;
 2867                         }
 2868                 }
 2869                 break;
 2870                 /* JRS - Set socket option for pluggable congestion control */
 2871         case SCTP_PLUGGABLE_CC:
 2872                 {
 2873                         struct sctp_assoc_value *av;
 2874 
 2875                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
 2876                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 2877                         if (stcb) {
 2878                                 switch (av->assoc_value) {
 2879                                         /*
 2880                                          * JRS - Standard TCP congestion
 2881                                          * control
 2882                                          */
 2883                                 case SCTP_CC_RFC2581:
 2884                                         {
 2885                                                 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
 2886                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
 2887                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
 2888                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
 2889                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
 2890                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
 2891                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
 2892                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
 2893                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
 2894                                                 SCTP_TCB_UNLOCK(stcb);
 2895                                                 break;
 2896                                         }
 2897                                         /*
 2898                                          * JRS - High Speed TCP congestion
 2899                                          * control (Floyd)
 2900                                          */
 2901                                 case SCTP_CC_HSTCP:
 2902                                         {
 2903                                                 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
 2904                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
 2905                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
 2906                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
 2907                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
 2908                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
 2909                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
 2910                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
 2911                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
 2912                                                 SCTP_TCB_UNLOCK(stcb);
 2913                                                 break;
 2914                                         }
 2915                                         /* JRS - HTCP congestion control */
 2916                                 case SCTP_CC_HTCP:
 2917                                         {
 2918                                                 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
 2919                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
 2920                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
 2921                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
 2922                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
 2923                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
 2924                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
 2925                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
 2926                                                 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
 2927                                                 SCTP_TCB_UNLOCK(stcb);
 2928                                                 break;
 2929                                         }
 2930                                         /*
 2931                                          * JRS - All other values are
 2932                                          * invalid
 2933                                          */
 2934                                 default:
 2935                                         {
 2936                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2937                                                 error = EINVAL;
 2938                                                 SCTP_TCB_UNLOCK(stcb);
 2939                                                 break;
 2940                                         }
 2941                                 }
 2942                         } else {
 2943                                 switch (av->assoc_value) {
 2944                                 case SCTP_CC_RFC2581:
 2945                                 case SCTP_CC_HSTCP:
 2946                                 case SCTP_CC_HTCP:
 2947                                         inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
 2948                                         break;
 2949                                 default:
 2950                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2951                                         error = EINVAL;
 2952                                         break;
 2953                                 };
 2954                         }
 2955                 }
 2956                 break;
 2957         case SCTP_CLR_STAT_LOG:
 2958                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
 2959                 error = EOPNOTSUPP;
 2960                 break;
 2961         case SCTP_CONTEXT:
 2962                 {
 2963                         struct sctp_assoc_value *av;
 2964 
 2965                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
 2966                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 2967 
 2968                         if (stcb) {
 2969                                 stcb->asoc.context = av->assoc_value;
 2970                                 SCTP_TCB_UNLOCK(stcb);
 2971                         } else {
 2972                                 SCTP_INP_WLOCK(inp);
 2973                                 inp->sctp_context = av->assoc_value;
 2974                                 SCTP_INP_WUNLOCK(inp);
 2975                         }
 2976                 }
 2977                 break;
 2978         case SCTP_VRF_ID:
 2979                 {
 2980                         uint32_t *default_vrfid;
 2981 
 2982                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
 2983                         if (*default_vrfid > SCTP_MAX_VRF_ID) {
 2984                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 2985                                 error = EINVAL;
 2986                                 break;
 2987                         }
 2988                         inp->def_vrf_id = *default_vrfid;
 2989                         break;
 2990                 }
 2991         case SCTP_DEL_VRF_ID:
 2992                 {
 2993                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
 2994                         error = EOPNOTSUPP;
 2995                         break;
 2996                 }
 2997         case SCTP_ADD_VRF_ID:
 2998                 {
 2999                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
 3000                         error = EOPNOTSUPP;
 3001                         break;
 3002                 }
 3003         case SCTP_DELAYED_SACK:
 3004                 {
 3005                         struct sctp_sack_info *sack;
 3006 
 3007                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
 3008                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
 3009                         if (sack->sack_delay) {
 3010                                 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
 3011                                         sack->sack_delay = SCTP_MAX_SACK_DELAY;
 3012                         }
 3013                         if (stcb) {
 3014                                 if (sack->sack_delay) {
 3015                                         if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
 3016                                                 sack->sack_delay = TICKS_TO_MSEC(1);
 3017                                         }
 3018                                         stcb->asoc.delayed_ack = sack->sack_delay;
 3019                                 }
 3020                                 if (sack->sack_freq) {
 3021                                         stcb->asoc.sack_freq = sack->sack_freq;
 3022                                 }
 3023                                 SCTP_TCB_UNLOCK(stcb);
 3024                         } else {
 3025                                 SCTP_INP_WLOCK(inp);
 3026                                 if (sack->sack_delay) {
 3027                                         if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
 3028                                                 sack->sack_delay = TICKS_TO_MSEC(1);
 3029                                         }
 3030                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
 3031                                 }
 3032                                 if (sack->sack_freq) {
 3033                                         inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
 3034                                 }
 3035                                 SCTP_INP_WUNLOCK(inp);
 3036                         }
 3037                         break;
 3038                 }
 3039         case SCTP_AUTH_CHUNK:
 3040                 {
 3041                         struct sctp_authchunk *sauth;
 3042 
 3043                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
 3044 
 3045                         SCTP_INP_WLOCK(inp);
 3046                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
 3047                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3048                                 error = EINVAL;
 3049                         }
 3050                         SCTP_INP_WUNLOCK(inp);
 3051                         break;
 3052                 }
 3053         case SCTP_AUTH_KEY:
 3054                 {
 3055                         struct sctp_authkey *sca;
 3056                         struct sctp_keyhead *shared_keys;
 3057                         sctp_sharedkey_t *shared_key;
 3058                         sctp_key_t *key = NULL;
 3059                         size_t size;
 3060 
 3061                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
 3062                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
 3063                         size = optsize - sizeof(*sca);
 3064 
 3065                         if (stcb) {
 3066                                 /* set it on the assoc */
 3067                                 shared_keys = &stcb->asoc.shared_keys;
 3068                                 /* clear the cached keys for this key id */
 3069                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
 3070                                 /*
 3071                                  * create the new shared key and
 3072                                  * insert/replace it
 3073                                  */
 3074                                 if (size > 0) {
 3075                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
 3076                                         if (key == NULL) {
 3077                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
 3078                                                 error = ENOMEM;
 3079                                                 SCTP_TCB_UNLOCK(stcb);
 3080                                                 break;
 3081                                         }
 3082                                 }
 3083                                 shared_key = sctp_alloc_sharedkey();
 3084                                 if (shared_key == NULL) {
 3085                                         sctp_free_key(key);
 3086                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
 3087                                         error = ENOMEM;
 3088                                         SCTP_TCB_UNLOCK(stcb);
 3089                                         break;
 3090                                 }
 3091                                 shared_key->key = key;
 3092                                 shared_key->keyid = sca->sca_keynumber;
 3093                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
 3094                                 SCTP_TCB_UNLOCK(stcb);
 3095                         } else {
 3096                                 /* set it on the endpoint */
 3097                                 SCTP_INP_WLOCK(inp);
 3098                                 shared_keys = &inp->sctp_ep.shared_keys;
 3099                                 /*
 3100                                  * clear the cached keys on all assocs for
 3101                                  * this key id
 3102                                  */
 3103                                 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
 3104                                 /*
 3105                                  * create the new shared key and
 3106                                  * insert/replace it
 3107                                  */
 3108                                 if (size > 0) {
 3109                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
 3110                                         if (key == NULL) {
 3111                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
 3112                                                 error = ENOMEM;
 3113                                                 SCTP_INP_WUNLOCK(inp);
 3114                                                 break;
 3115                                         }
 3116                                 }
 3117                                 shared_key = sctp_alloc_sharedkey();
 3118                                 if (shared_key == NULL) {
 3119                                         sctp_free_key(key);
 3120                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
 3121                                         error = ENOMEM;
 3122                                         SCTP_INP_WUNLOCK(inp);
 3123                                         break;
 3124                                 }
 3125                                 shared_key->key = key;
 3126                                 shared_key->keyid = sca->sca_keynumber;
 3127                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
 3128                                 SCTP_INP_WUNLOCK(inp);
 3129                         }
 3130                         break;
 3131                 }
 3132         case SCTP_HMAC_IDENT:
 3133                 {
 3134                         struct sctp_hmacalgo *shmac;
 3135                         sctp_hmaclist_t *hmaclist;
 3136                         uint16_t hmacid;
 3137                         uint32_t i;
 3138 
 3139                         size_t found;
 3140 
 3141                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
 3142                         if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
 3143                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3144                                 error = EINVAL;
 3145                                 break;
 3146                         }
 3147                         hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
 3148                         if (hmaclist == NULL) {
 3149                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
 3150                                 error = ENOMEM;
 3151                                 break;
 3152                         }
 3153                         for (i = 0; i < shmac->shmac_number_of_idents; i++) {
 3154                                 hmacid = shmac->shmac_idents[i];
 3155                                 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
 3156                                          /* invalid HMACs were found */ ;
 3157                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3158                                         error = EINVAL;
 3159                                         sctp_free_hmaclist(hmaclist);
 3160                                         goto sctp_set_hmac_done;
 3161                                 }
 3162                         }
 3163                         found = 0;
 3164                         for (i = 0; i < hmaclist->num_algo; i++) {
 3165                                 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
 3166                                         /* already in list */
 3167                                         found = 1;
 3168                                 }
 3169                         }
 3170                         if (!found) {
 3171                                 sctp_free_hmaclist(hmaclist);
 3172                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3173                                 error = EINVAL;
 3174                                 break;
 3175                         }
 3176                         /* set it on the endpoint */
 3177                         SCTP_INP_WLOCK(inp);
 3178                         if (inp->sctp_ep.local_hmacs)
 3179                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
 3180                         inp->sctp_ep.local_hmacs = hmaclist;
 3181                         SCTP_INP_WUNLOCK(inp);
 3182         sctp_set_hmac_done:
 3183                         break;
 3184                 }
 3185         case SCTP_AUTH_ACTIVE_KEY:
 3186                 {
 3187                         struct sctp_authkeyid *scact;
 3188 
 3189                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
 3190                             optsize);
 3191                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
 3192 
 3193                         /* set the active key on the right place */
 3194                         if (stcb) {
 3195                                 /* set the active key on the assoc */
 3196                                 if (sctp_auth_setactivekey(stcb,
 3197                                     scact->scact_keynumber)) {
 3198                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
 3199                                             SCTP_FROM_SCTP_USRREQ,
 3200                                             EINVAL);
 3201                                         error = EINVAL;
 3202                                 }
 3203                                 SCTP_TCB_UNLOCK(stcb);
 3204                         } else {
 3205                                 /* set the active key on the endpoint */
 3206                                 SCTP_INP_WLOCK(inp);
 3207                                 if (sctp_auth_setactivekey_ep(inp,
 3208                                     scact->scact_keynumber)) {
 3209                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
 3210                                             SCTP_FROM_SCTP_USRREQ,
 3211                                             EINVAL);
 3212                                         error = EINVAL;
 3213                                 }
 3214                                 SCTP_INP_WUNLOCK(inp);
 3215                         }
 3216                         break;
 3217                 }
 3218         case SCTP_AUTH_DELETE_KEY:
 3219                 {
 3220                         struct sctp_authkeyid *scdel;
 3221 
 3222                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
 3223                             optsize);
 3224                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
 3225 
 3226                         /* delete the key from the right place */
 3227                         if (stcb) {
 3228                                 if (sctp_delete_sharedkey(stcb,
 3229                                     scdel->scact_keynumber)) {
 3230                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
 3231                                             SCTP_FROM_SCTP_USRREQ,
 3232                                             EINVAL);
 3233                                         error = EINVAL;
 3234                                 }
 3235                                 SCTP_TCB_UNLOCK(stcb);
 3236                         } else {
 3237                                 SCTP_INP_WLOCK(inp);
 3238                                 if (sctp_delete_sharedkey_ep(inp,
 3239                                     scdel->scact_keynumber)) {
 3240                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
 3241                                             SCTP_FROM_SCTP_USRREQ,
 3242                                             EINVAL);
 3243                                         error = EINVAL;
 3244                                 }
 3245                                 SCTP_INP_WUNLOCK(inp);
 3246                         }
 3247                         break;
 3248                 }
 3249         case SCTP_AUTH_DEACTIVATE_KEY:
 3250                 {
 3251                         struct sctp_authkeyid *keyid;
 3252 
 3253                         SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
 3254                             optsize);
 3255                         SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
 3256 
 3257                         /* deactivate the key from the right place */
 3258                         if (stcb) {
 3259                                 if (sctp_deact_sharedkey(stcb,
 3260                                     keyid->scact_keynumber)) {
 3261                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
 3262                                             SCTP_FROM_SCTP_USRREQ,
 3263                                             EINVAL);
 3264                                         error = EINVAL;
 3265                                 }
 3266                                 SCTP_TCB_UNLOCK(stcb);
 3267                         } else {
 3268                                 SCTP_INP_WLOCK(inp);
 3269                                 if (sctp_deact_sharedkey_ep(inp,
 3270                                     keyid->scact_keynumber)) {
 3271                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
 3272                                             SCTP_FROM_SCTP_USRREQ,
 3273                                             EINVAL);
 3274                                         error = EINVAL;
 3275                                 }
 3276                                 SCTP_INP_WUNLOCK(inp);
 3277                         }
 3278                         break;
 3279                 }
 3280 
 3281         case SCTP_RESET_STREAMS:
 3282                 {
 3283                         struct sctp_stream_reset *strrst;
 3284                         uint8_t send_in = 0, send_tsn = 0, send_out = 0,
 3285                                 addstream = 0;
 3286                         uint16_t addstrmcnt = 0;
 3287                         int i;
 3288 
 3289                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
 3290                         SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
 3291 
 3292                         if (stcb == NULL) {
 3293                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 3294                                 error = ENOENT;
 3295                                 break;
 3296                         }
 3297                         if (stcb->asoc.peer_supports_strreset == 0) {
 3298                                 /*
 3299                                  * Peer does not support it, we return
 3300                                  * protocol not supported since this is true
 3301                                  * for this feature and this peer, not the
 3302                                  * socket request in general.
 3303                                  */
 3304                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
 3305                                 error = EPROTONOSUPPORT;
 3306                                 SCTP_TCB_UNLOCK(stcb);
 3307                                 break;
 3308                         }
 3309                         if (stcb->asoc.stream_reset_outstanding) {
 3310                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
 3311                                 error = EALREADY;
 3312                                 SCTP_TCB_UNLOCK(stcb);
 3313                                 break;
 3314                         }
 3315                         if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
 3316                                 send_in = 1;
 3317                         } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
 3318                                 send_out = 1;
 3319                         } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
 3320                                 send_in = 1;
 3321                                 send_out = 1;
 3322                         } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
 3323                                 send_tsn = 1;
 3324                         } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
 3325                                 if (send_tsn ||
 3326                                     send_in ||
 3327                                     send_out) {
 3328                                         /* We can't do that and add streams */
 3329                                         error = EINVAL;
 3330                                         goto skip_stuff;
 3331                                 }
 3332                                 if (stcb->asoc.stream_reset_outstanding) {
 3333                                         error = EBUSY;
 3334                                         goto skip_stuff;
 3335                                 }
 3336                                 addstream = 1;
 3337                                 /* We allocate here */
 3338                                 addstrmcnt = strrst->strrst_num_streams;
 3339                                 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
 3340                                         /* You can't have more than 64k */
 3341                                         error = EINVAL;
 3342                                         goto skip_stuff;
 3343                                 }
 3344                                 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
 3345                                         /* Need to allocate more */
 3346                                         struct sctp_stream_out *oldstream;
 3347                                         struct sctp_stream_queue_pending *sp;
 3348                                         int removed;
 3349 
 3350                                         oldstream = stcb->asoc.strmout;
 3351                                         /* get some more */
 3352                                         SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
 3353                                             ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
 3354                                             SCTP_M_STRMO);
 3355                                         if (stcb->asoc.strmout == NULL) {
 3356                                                 stcb->asoc.strmout = oldstream;
 3357                                                 error = ENOMEM;
 3358                                                 goto skip_stuff;
 3359                                         }
 3360                                         /*
 3361                                          * Ok now we proceed with copying
 3362                                          * the old out stuff and
 3363                                          * initializing the new stuff.
 3364                                          */
 3365                                         SCTP_TCB_SEND_LOCK(stcb);
 3366                                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
 3367                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
 3368                                                 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
 3369                                                 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
 3370                                                 stcb->asoc.strmout[i].stream_no = i;
 3371                                                 if (oldstream[i].next_spoke.tqe_next) {
 3372                                                         sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
 3373                                                         stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
 3374                                                         stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
 3375                                                         removed = 1;
 3376                                                 } else {
 3377                                                         /* not on out wheel */
 3378                                                         stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
 3379                                                         stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
 3380                                                         removed = 0;
 3381                                                 }
 3382                                                 /*
 3383                                                  * now anything on those
 3384                                                  * queues?
 3385                                                  */
 3386                                                 while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
 3387                                                         sp = TAILQ_FIRST(&oldstream[i].outqueue);
 3388                                                         TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
 3389                                                         TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
 3390                                                 }
 3391                                                 /* Did we disrupt the wheel? */
 3392                                                 if (removed) {
 3393                                                         sctp_insert_on_wheel(stcb,
 3394                                                             &stcb->asoc,
 3395                                                             &stcb->asoc.strmout[i],
 3396                                                             1);
 3397                                                 }
 3398                                                 /*
 3399                                                  * Now move assoc pointers
 3400                                                  * too
 3401                                                  */
 3402                                                 if (stcb->asoc.last_out_stream == &oldstream[i]) {
 3403                                                         stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
 3404                                                 }
 3405                                                 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
 3406                                                         stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
 3407                                                 }
 3408                                         }
 3409                                         /* now the new streams */
 3410                                         for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
 3411                                                 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
 3412                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
 3413                                                 stcb->asoc.strmout[i].stream_no = i;
 3414                                                 stcb->asoc.strmout[i].last_msg_incomplete = 0;
 3415                                                 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
 3416                                                 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
 3417                                         }
 3418                                         stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
 3419                                         SCTP_FREE(oldstream, SCTP_M_STRMO);
 3420                                 }
 3421                                 SCTP_TCB_SEND_UNLOCK(stcb);
 3422                                 goto skip_stuff;
 3423                         } else {
 3424                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3425                                 error = EINVAL;
 3426                                 SCTP_TCB_UNLOCK(stcb);
 3427                                 break;
 3428                         }
 3429                         for (i = 0; i < strrst->strrst_num_streams; i++) {
 3430                                 if ((send_in) &&
 3431 
 3432                                     (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
 3433                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3434                                         error = EINVAL;
 3435                                         goto get_out;
 3436                                 }
 3437                                 if ((send_out) &&
 3438                                     (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
 3439                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3440                                         error = EINVAL;
 3441                                         goto get_out;
 3442                                 }
 3443                         }
 3444         skip_stuff:
 3445                         if (error) {
 3446                 get_out:
 3447                                 SCTP_TCB_UNLOCK(stcb);
 3448                                 break;
 3449                         }
 3450                         error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
 3451                             strrst->strrst_list,
 3452                             send_out, (stcb->asoc.str_reset_seq_in - 3),
 3453                             send_in, send_tsn, addstream, addstrmcnt);
 3454 
 3455                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
 3456                         SCTP_TCB_UNLOCK(stcb);
 3457                 }
 3458                 break;
 3459 
 3460         case SCTP_CONNECT_X:
 3461                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
 3462                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3463                         error = EINVAL;
 3464                         break;
 3465                 }
 3466                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
 3467                 break;
 3468 
 3469         case SCTP_CONNECT_X_DELAYED:
 3470                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
 3471                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3472                         error = EINVAL;
 3473                         break;
 3474                 }
 3475                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
 3476                 break;
 3477 
 3478         case SCTP_CONNECT_X_COMPLETE:
 3479                 {
 3480                         struct sockaddr *sa;
 3481                         struct sctp_nets *net;
 3482 
 3483                         /* FIXME MT: check correct? */
 3484                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
 3485 
 3486                         /* find tcb */
 3487                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
 3488                                 SCTP_INP_RLOCK(inp);
 3489                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
 3490                                 if (stcb) {
 3491                                         SCTP_TCB_LOCK(stcb);
 3492                                         net = sctp_findnet(stcb, sa);
 3493                                 }
 3494                                 SCTP_INP_RUNLOCK(inp);
 3495                         } else {
 3496                                 /*
 3497                                  * We increment here since
 3498                                  * sctp_findassociation_ep_addr() wil do a
 3499                                  * decrement if it finds the stcb as long as
 3500                                  * the locked tcb (last argument) is NOT a
 3501                                  * TCB.. aka NULL.
 3502                                  */
 3503                                 SCTP_INP_INCR_REF(inp);
 3504                                 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
 3505                                 if (stcb == NULL) {
 3506                                         SCTP_INP_DECR_REF(inp);
 3507                                 }
 3508                         }
 3509 
 3510                         if (stcb == NULL) {
 3511                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 3512                                 error = ENOENT;
 3513                                 break;
 3514                         }
 3515                         if (stcb->asoc.delayed_connection == 1) {
 3516                                 stcb->asoc.delayed_connection = 0;
 3517                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
 3518                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
 3519                                     stcb->asoc.primary_destination,
 3520                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
 3521                                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
 3522                         } else {
 3523                                 /*
 3524                                  * already expired or did not use delayed
 3525                                  * connectx
 3526                                  */
 3527                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
 3528                                 error = EALREADY;
 3529                         }
 3530                         SCTP_TCB_UNLOCK(stcb);
 3531                 }
 3532                 break;
 3533         case SCTP_MAX_BURST:
 3534                 {
 3535                         uint8_t *burst;
 3536 
 3537                         SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
 3538 
 3539                         SCTP_INP_WLOCK(inp);
 3540                         if (*burst) {
 3541                                 inp->sctp_ep.max_burst = *burst;
 3542                         }
 3543                         SCTP_INP_WUNLOCK(inp);
 3544                 }
 3545                 break;
 3546         case SCTP_MAXSEG:
 3547                 {
 3548                         struct sctp_assoc_value *av;
 3549                         int ovh;
 3550 
 3551                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
 3552                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
 3553 
 3554                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 3555                                 ovh = SCTP_MED_OVERHEAD;
 3556                         } else {
 3557                                 ovh = SCTP_MED_V4_OVERHEAD;
 3558                         }
 3559                         if (stcb) {
 3560                                 if (av->assoc_value) {
 3561                                         stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
 3562                                 } else {
 3563                                         stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
 3564                                 }
 3565                                 SCTP_TCB_UNLOCK(stcb);
 3566                         } else {
 3567                                 SCTP_INP_WLOCK(inp);
 3568                                 /*
 3569                                  * FIXME MT: I think this is not in tune
 3570                                  * with the API ID
 3571                                  */
 3572                                 if (av->assoc_value) {
 3573                                         inp->sctp_frag_point = (av->assoc_value + ovh);
 3574                                 } else {
 3575                                         inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
 3576                                 }
 3577                                 SCTP_INP_WUNLOCK(inp);
 3578                         }
 3579                 }
 3580                 break;
 3581         case SCTP_EVENTS:
 3582                 {
 3583                         struct sctp_event_subscribe *events;
 3584 
 3585                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
 3586 
 3587                         SCTP_INP_WLOCK(inp);
 3588                         if (events->sctp_data_io_event) {
 3589                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
 3590                         } else {
 3591                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
 3592                         }
 3593 
 3594                         if (events->sctp_association_event) {
 3595                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
 3596                         } else {
 3597                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
 3598                         }
 3599 
 3600                         if (events->sctp_address_event) {
 3601                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
 3602                         } else {
 3603                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
 3604                         }
 3605 
 3606                         if (events->sctp_send_failure_event) {
 3607                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
 3608                         } else {
 3609                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
 3610                         }
 3611 
 3612                         if (events->sctp_peer_error_event) {
 3613                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
 3614                         } else {
 3615                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
 3616                         }
 3617 
 3618                         if (events->sctp_shutdown_event) {
 3619                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
 3620                         } else {
 3621                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
 3622                         }
 3623 
 3624                         if (events->sctp_partial_delivery_event) {
 3625                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
 3626                         } else {
 3627                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
 3628                         }
 3629 
 3630                         if (events->sctp_adaptation_layer_event) {
 3631                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
 3632                         } else {
 3633                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
 3634                         }
 3635 
 3636                         if (events->sctp_authentication_event) {
 3637                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
 3638                         } else {
 3639                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
 3640                         }
 3641 
 3642                         if (events->sctp_sender_dry_event) {
 3643                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
 3644                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 3645                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
 3646                                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
 3647                                         if (stcb) {
 3648                                                 SCTP_TCB_LOCK(stcb);
 3649                                         }
 3650                                         if (stcb &&
 3651                                             TAILQ_EMPTY(&stcb->asoc.send_queue) &&
 3652                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
 3653                                             (stcb->asoc.stream_queue_cnt == 0)) {
 3654                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
 3655                                         }
 3656                                         if (stcb) {
 3657                                                 SCTP_TCB_UNLOCK(stcb);
 3658                                         }
 3659                                 }
 3660                         } else {
 3661                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
 3662                         }
 3663 
 3664                         if (events->sctp_stream_reset_event) {
 3665                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
 3666                         } else {
 3667                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
 3668                         }
 3669                         SCTP_INP_WUNLOCK(inp);
 3670                 }
 3671                 break;
 3672 
 3673         case SCTP_ADAPTATION_LAYER:
 3674                 {
 3675                         struct sctp_setadaptation *adap_bits;
 3676 
 3677                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
 3678                         SCTP_INP_WLOCK(inp);
 3679                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
 3680                         SCTP_INP_WUNLOCK(inp);
 3681                 }
 3682                 break;
 3683 #ifdef SCTP_DEBUG
 3684         case SCTP_SET_INITIAL_DBG_SEQ:
 3685                 {
 3686                         uint32_t *vvv;
 3687 
 3688                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
 3689                         SCTP_INP_WLOCK(inp);
 3690                         inp->sctp_ep.initial_sequence_debug = *vvv;
 3691                         SCTP_INP_WUNLOCK(inp);
 3692                 }
 3693                 break;
 3694 #endif
 3695         case SCTP_DEFAULT_SEND_PARAM:
 3696                 {
 3697                         struct sctp_sndrcvinfo *s_info;
 3698 
 3699                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
 3700                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
 3701 
 3702                         if (stcb) {
 3703                                 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
 3704                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
 3705                                 } else {
 3706                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3707                                         error = EINVAL;
 3708                                 }
 3709                                 SCTP_TCB_UNLOCK(stcb);
 3710                         } else {
 3711                                 SCTP_INP_WLOCK(inp);
 3712                                 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
 3713                                 SCTP_INP_WUNLOCK(inp);
 3714                         }
 3715                 }
 3716                 break;
 3717         case SCTP_PEER_ADDR_PARAMS:
 3718                 /* Applys to the specific association */
 3719                 {
 3720                         struct sctp_paddrparams *paddrp;
 3721                         struct sctp_nets *net;
 3722 
 3723                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
 3724                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
 3725                         net = NULL;
 3726                         if (stcb) {
 3727                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
 3728                         } else {
 3729                                 /*
 3730                                  * We increment here since
 3731                                  * sctp_findassociation_ep_addr() wil do a
 3732                                  * decrement if it finds the stcb as long as
 3733                                  * the locked tcb (last argument) is NOT a
 3734                                  * TCB.. aka NULL.
 3735                                  */
 3736                                 SCTP_INP_INCR_REF(inp);
 3737                                 stcb = sctp_findassociation_ep_addr(&inp,
 3738                                     (struct sockaddr *)&paddrp->spp_address,
 3739                                     &net, NULL, NULL);
 3740                                 if (stcb == NULL) {
 3741                                         SCTP_INP_DECR_REF(inp);
 3742                                 }
 3743                         }
 3744                         if (stcb && (net == NULL)) {
 3745                                 struct sockaddr *sa;
 3746 
 3747                                 sa = (struct sockaddr *)&paddrp->spp_address;
 3748                                 if (sa->sa_family == AF_INET) {
 3749                                         struct sockaddr_in *sin;
 3750 
 3751                                         sin = (struct sockaddr_in *)sa;
 3752                                         if (sin->sin_addr.s_addr) {
 3753                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3754                                                 SCTP_TCB_UNLOCK(stcb);
 3755                                                 error = EINVAL;
 3756                                                 break;
 3757                                         }
 3758                                 } else if (sa->sa_family == AF_INET6) {
 3759                                         struct sockaddr_in6 *sin6;
 3760 
 3761                                         sin6 = (struct sockaddr_in6 *)sa;
 3762                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 3763                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3764                                                 SCTP_TCB_UNLOCK(stcb);
 3765                                                 error = EINVAL;
 3766                                                 break;
 3767                                         }
 3768                                 } else {
 3769                                         error = EAFNOSUPPORT;
 3770                                         SCTP_TCB_UNLOCK(stcb);
 3771                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 3772                                         break;
 3773                                 }
 3774                         }
 3775                         /* sanity checks */
 3776                         if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
 3777                                 if (stcb)
 3778                                         SCTP_TCB_UNLOCK(stcb);
 3779                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3780                                 return (EINVAL);
 3781                         }
 3782                         if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
 3783                                 if (stcb)
 3784                                         SCTP_TCB_UNLOCK(stcb);
 3785                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3786                                 return (EINVAL);
 3787                         }
 3788                         if (stcb) {
 3789                                 /************************TCB SPECIFIC SET ******************/
 3790                                 /*
 3791                                  * do we change the timer for HB, we run
 3792                                  * only one?
 3793                                  */
 3794                                 int ovh = 0;
 3795 
 3796                                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 3797                                         ovh = SCTP_MED_OVERHEAD;
 3798                                 } else {
 3799                                         ovh = SCTP_MED_V4_OVERHEAD;
 3800                                 }
 3801 
 3802                                 if (paddrp->spp_hbinterval)
 3803                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
 3804                                 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
 3805                                         stcb->asoc.heart_beat_delay = 0;
 3806 
 3807                                 /* network sets ? */
 3808                                 if (net) {
 3809                                         /************************NET SPECIFIC SET ******************/
 3810                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
 3811                                                 /* on demand HB */
 3812                                                 if (sctp_send_hb(stcb, 1, net) < 0) {
 3813                                                         /* asoc destroyed */
 3814                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3815                                                         error = EINVAL;
 3816                                                         break;
 3817                                                 }
 3818                                         }
 3819                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
 3820                                                 net->dest_state |= SCTP_ADDR_NOHB;
 3821                                         }
 3822                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
 3823                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
 3824                                         }
 3825                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
 3826                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
 3827                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
 3828                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
 3829                                                 }
 3830                                                 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
 3831                                                         net->mtu = paddrp->spp_pathmtu + ovh;
 3832                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
 3833 #ifdef SCTP_PRINT_FOR_B_AND_M
 3834                                                                 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
 3835                                                                     net->mtu);
 3836 #endif
 3837                                                                 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
 3838                                                         }
 3839                                                 }
 3840                                         }
 3841                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
 3842                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
 3843                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
 3844                                                 }
 3845                                         }
 3846                                         if (paddrp->spp_pathmaxrxt)
 3847                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
 3848 #ifdef INET
 3849                                         if (paddrp->spp_flags & SPP_IPV4_TOS) {
 3850                                                 if (net->ro._l_addr.sin.sin_family == AF_INET) {
 3851                                                         net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
 3852                                                 }
 3853                                         }
 3854 #endif
 3855 #ifdef INET6
 3856                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
 3857                                                 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
 3858                                                         net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
 3859                                                 }
 3860                                         }
 3861 #endif
 3862                                 } else {
 3863                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
 3864                                         if (paddrp->spp_pathmaxrxt)
 3865                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
 3866 
 3867                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
 3868                                                 /* Turn back on the timer */
 3869                                                 stcb->asoc.hb_is_disabled = 0;
 3870                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
 3871                                         }
 3872                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
 3873                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 3874                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
 3875                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
 3876                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
 3877                                                         }
 3878                                                         if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
 3879                                                                 net->mtu = paddrp->spp_pathmtu + ovh;
 3880                                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
 3881 #ifdef SCTP_PRINT_FOR_B_AND_M
 3882                                                                         SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
 3883                                                                             net->mtu);
 3884 #endif
 3885                                                                         sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
 3886                                                                 }
 3887                                                         }
 3888                                                 }
 3889                                         }
 3890                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
 3891                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 3892                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
 3893                                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
 3894                                                         }
 3895                                                 }
 3896                                         }
 3897                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
 3898                                                 int cnt_of_unconf = 0;
 3899                                                 struct sctp_nets *lnet;
 3900 
 3901                                                 stcb->asoc.hb_is_disabled = 1;
 3902                                                 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
 3903                                                         if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
 3904                                                                 cnt_of_unconf++;
 3905                                                         }
 3906                                                 }
 3907                                                 /*
 3908                                                  * stop the timer ONLY if we
 3909                                                  * have no unconfirmed
 3910                                                  * addresses
 3911                                                  */
 3912                                                 if (cnt_of_unconf == 0) {
 3913                                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 3914                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
 3915                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
 3916                                                         }
 3917                                                 }
 3918                                         }
 3919                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
 3920                                                 /* start up the timer. */
 3921                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 3922                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
 3923                                                 }
 3924                                         }
 3925 #ifdef INET
 3926                                         if (paddrp->spp_flags & SPP_IPV4_TOS)
 3927                                                 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
 3928 #endif
 3929 #ifdef INET6
 3930                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
 3931                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
 3932 #endif
 3933 
 3934                                 }
 3935                                 SCTP_TCB_UNLOCK(stcb);
 3936                         } else {
 3937                                 /************************NO TCB, SET TO default stuff ******************/
 3938                                 SCTP_INP_WLOCK(inp);
 3939                                 /*
 3940                                  * For the TOS/FLOWLABEL stuff you set it
 3941                                  * with the options on the socket
 3942                                  */
 3943                                 if (paddrp->spp_pathmaxrxt) {
 3944                                         inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
 3945                                 }
 3946                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
 3947                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
 3948                                 else if (paddrp->spp_hbinterval) {
 3949                                         if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
 3950                                                 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
 3951                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
 3952                                 }
 3953                                 if (paddrp->spp_flags & SPP_HB_ENABLE) {
 3954                                         sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
 3955 
 3956                                 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
 3957                                         sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
 3958                                 }
 3959                                 SCTP_INP_WUNLOCK(inp);
 3960                         }
 3961                 }
 3962                 break;
 3963         case SCTP_RTOINFO:
 3964                 {
 3965                         struct sctp_rtoinfo *srto;
 3966                         uint32_t new_init, new_min, new_max;
 3967 
 3968                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
 3969                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
 3970 
 3971                         if (stcb) {
 3972                                 if (srto->srto_initial)
 3973                                         new_init = srto->srto_initial;
 3974                                 else
 3975                                         new_init = stcb->asoc.initial_rto;
 3976                                 if (srto->srto_max)
 3977                                         new_max = srto->srto_max;
 3978                                 else
 3979                                         new_max = stcb->asoc.maxrto;
 3980                                 if (srto->srto_min)
 3981                                         new_min = srto->srto_min;
 3982                                 else
 3983                                         new_min = stcb->asoc.minrto;
 3984                                 if ((new_min <= new_init) && (new_init <= new_max)) {
 3985                                         stcb->asoc.initial_rto = new_init;
 3986                                         stcb->asoc.maxrto = new_max;
 3987                                         stcb->asoc.minrto = new_min;
 3988                                 } else {
 3989                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 3990                                         error = EINVAL;
 3991                                 }
 3992                                 SCTP_TCB_UNLOCK(stcb);
 3993                         } else {
 3994                                 SCTP_INP_WLOCK(inp);
 3995                                 if (srto->srto_initial)
 3996                                         new_init = srto->srto_initial;
 3997                                 else
 3998                                         new_init = inp->sctp_ep.initial_rto;
 3999                                 if (srto->srto_max)
 4000                                         new_max = srto->srto_max;
 4001                                 else
 4002                                         new_max = inp->sctp_ep.sctp_maxrto;
 4003                                 if (srto->srto_min)
 4004                                         new_min = srto->srto_min;
 4005                                 else
 4006                                         new_min = inp->sctp_ep.sctp_minrto;
 4007                                 if ((new_min <= new_init) && (new_init <= new_max)) {
 4008                                         inp->sctp_ep.initial_rto = new_init;
 4009                                         inp->sctp_ep.sctp_maxrto = new_max;
 4010                                         inp->sctp_ep.sctp_minrto = new_min;
 4011                                 } else {
 4012                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4013                                         error = EINVAL;
 4014                                 }
 4015                                 SCTP_INP_WUNLOCK(inp);
 4016                         }
 4017                 }
 4018                 break;
 4019         case SCTP_ASSOCINFO:
 4020                 {
 4021                         struct sctp_assocparams *sasoc;
 4022 
 4023                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
 4024                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
 4025                         if (sasoc->sasoc_cookie_life) {
 4026                                 /* boundary check the cookie life */
 4027                                 if (sasoc->sasoc_cookie_life < 1000)
 4028                                         sasoc->sasoc_cookie_life = 1000;
 4029                                 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
 4030                                         sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
 4031                                 }
 4032                         }
 4033                         if (stcb) {
 4034                                 if (sasoc->sasoc_asocmaxrxt)
 4035                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
 4036                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
 4037                                 sasoc->sasoc_peer_rwnd = 0;
 4038                                 sasoc->sasoc_local_rwnd = 0;
 4039                                 if (sasoc->sasoc_cookie_life) {
 4040                                         stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
 4041                                 }
 4042                                 SCTP_TCB_UNLOCK(stcb);
 4043                         } else {
 4044                                 SCTP_INP_WLOCK(inp);
 4045                                 if (sasoc->sasoc_asocmaxrxt)
 4046                                         inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
 4047                                 sasoc->sasoc_number_peer_destinations = 0;
 4048                                 sasoc->sasoc_peer_rwnd = 0;
 4049                                 sasoc->sasoc_local_rwnd = 0;
 4050                                 if (sasoc->sasoc_cookie_life) {
 4051                                         inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
 4052                                 }
 4053                                 SCTP_INP_WUNLOCK(inp);
 4054                         }
 4055                 }
 4056                 break;
 4057         case SCTP_INITMSG:
 4058                 {
 4059                         struct sctp_initmsg *sinit;
 4060 
 4061                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
 4062                         SCTP_INP_WLOCK(inp);
 4063                         if (sinit->sinit_num_ostreams)
 4064                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
 4065 
 4066                         if (sinit->sinit_max_instreams)
 4067                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
 4068 
 4069                         if (sinit->sinit_max_attempts)
 4070                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
 4071 
 4072                         if (sinit->sinit_max_init_timeo)
 4073                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
 4074                         SCTP_INP_WUNLOCK(inp);
 4075                 }
 4076                 break;
 4077         case SCTP_PRIMARY_ADDR:
 4078                 {
 4079                         struct sctp_setprim *spa;
 4080                         struct sctp_nets *net, *lnet;
 4081 
 4082                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
 4083                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
 4084 
 4085                         net = NULL;
 4086                         if (stcb) {
 4087                                 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
 4088                         } else {
 4089                                 /*
 4090                                  * We increment here since
 4091                                  * sctp_findassociation_ep_addr() wil do a
 4092                                  * decrement if it finds the stcb as long as
 4093                                  * the locked tcb (last argument) is NOT a
 4094                                  * TCB.. aka NULL.
 4095                                  */
 4096                                 SCTP_INP_INCR_REF(inp);
 4097                                 stcb = sctp_findassociation_ep_addr(&inp,
 4098                                     (struct sockaddr *)&spa->ssp_addr,
 4099                                     &net, NULL, NULL);
 4100                                 if (stcb == NULL) {
 4101                                         SCTP_INP_DECR_REF(inp);
 4102                                 }
 4103                         }
 4104 
 4105                         if ((stcb) && (net)) {
 4106                                 if ((net != stcb->asoc.primary_destination) &&
 4107                                     (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
 4108                                         /* Ok we need to set it */
 4109                                         lnet = stcb->asoc.primary_destination;
 4110                                         if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
 4111                                                 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
 4112                                                         net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
 4113                                                 }
 4114                                                 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
 4115                                         }
 4116                                 }
 4117                         } else {
 4118                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4119                                 error = EINVAL;
 4120                         }
 4121                         if (stcb) {
 4122                                 SCTP_TCB_UNLOCK(stcb);
 4123                         }
 4124                 }
 4125                 break;
 4126         case SCTP_SET_DYNAMIC_PRIMARY:
 4127                 {
 4128                         union sctp_sockstore *ss;
 4129 
 4130                         error = priv_check(curthread,
 4131                             PRIV_NETINET_RESERVEDPORT);
 4132                         if (error)
 4133                                 break;
 4134 
 4135                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
 4136                         /* SUPER USER CHECK? */
 4137                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
 4138                 }
 4139                 break;
 4140         case SCTP_SET_PEER_PRIMARY_ADDR:
 4141                 {
 4142                         struct sctp_setpeerprim *sspp;
 4143 
 4144                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
 4145                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
 4146                         if (stcb != NULL) {
 4147                                 struct sctp_ifa *ifa;
 4148 
 4149                                 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
 4150                                     stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
 4151                                 if (ifa == NULL) {
 4152                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4153                                         error = EINVAL;
 4154                                         goto out_of_it;
 4155                                 }
 4156                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
 4157                                         /*
 4158                                          * Must validate the ifa found is in
 4159                                          * our ep
 4160                                          */
 4161                                         struct sctp_laddr *laddr;
 4162                                         int found = 0;
 4163 
 4164                                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 4165                                                 if (laddr->ifa == NULL) {
 4166                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
 4167                                                             __FUNCTION__);
 4168                                                         continue;
 4169                                                 }
 4170                                                 if (laddr->ifa == ifa) {
 4171                                                         found = 1;
 4172                                                         break;
 4173                                                 }
 4174                                         }
 4175                                         if (!found) {
 4176                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4177                                                 error = EINVAL;
 4178                                                 goto out_of_it;
 4179                                         }
 4180                                 }
 4181                                 if (sctp_set_primary_ip_address_sa(stcb,
 4182                                     (struct sockaddr *)&sspp->sspp_addr) != 0) {
 4183                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4184                                         error = EINVAL;
 4185                                 }
 4186                 out_of_it:
 4187                                 SCTP_TCB_UNLOCK(stcb);
 4188                         } else {
 4189                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4190                                 error = EINVAL;
 4191                         }
 4192 
 4193                 }
 4194                 break;
 4195         case SCTP_BINDX_ADD_ADDR:
 4196                 {
 4197                         struct sctp_getaddresses *addrs;
 4198                         size_t sz;
 4199                         struct thread *td;
 4200 
 4201                         td = (struct thread *)p;
 4202                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
 4203                             optsize);
 4204                         if (addrs->addr->sa_family == AF_INET) {
 4205                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
 4206                                 if (optsize < sz) {
 4207                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4208                                         error = EINVAL;
 4209                                         break;
 4210                                 }
 4211                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
 4212                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
 4213                                         break;
 4214                                 }
 4215 #ifdef INET6
 4216                         } else if (addrs->addr->sa_family == AF_INET6) {
 4217                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
 4218                                 if (optsize < sz) {
 4219                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4220                                         error = EINVAL;
 4221                                         break;
 4222                                 }
 4223                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
 4224                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
 4225                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
 4226                                         break;
 4227                                 }
 4228 #endif
 4229                         } else {
 4230                                 error = EAFNOSUPPORT;
 4231                                 break;
 4232                         }
 4233                         sctp_bindx_add_address(so, inp, addrs->addr,
 4234                             addrs->sget_assoc_id, vrf_id,
 4235                             &error, p);
 4236                 }
 4237                 break;
 4238         case SCTP_BINDX_REM_ADDR:
 4239                 {
 4240                         struct sctp_getaddresses *addrs;
 4241                         size_t sz;
 4242                         struct thread *td;
 4243 
 4244                         td = (struct thread *)p;
 4245 
 4246                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
 4247                         if (addrs->addr->sa_family == AF_INET) {
 4248                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
 4249                                 if (optsize < sz) {
 4250                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4251                                         error = EINVAL;
 4252                                         break;
 4253                                 }
 4254                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
 4255                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
 4256                                         break;
 4257                                 }
 4258 #ifdef INET6
 4259                         } else if (addrs->addr->sa_family == AF_INET6) {
 4260                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
 4261                                 if (optsize < sz) {
 4262                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4263                                         error = EINVAL;
 4264                                         break;
 4265                                 }
 4266                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
 4267                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
 4268                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
 4269                                         break;
 4270                                 }
 4271 #endif
 4272                         } else {
 4273                                 error = EAFNOSUPPORT;
 4274                                 break;
 4275                         }
 4276                         sctp_bindx_delete_address(so, inp, addrs->addr,
 4277                             addrs->sget_assoc_id, vrf_id,
 4278                             &error);
 4279                 }
 4280                 break;
 4281         default:
 4282                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
 4283                 error = ENOPROTOOPT;
 4284                 break;
 4285         }                       /* end switch (opt) */
 4286         return (error);
 4287 }
 4288 
 4289 int
 4290 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
 4291 {
 4292         void *optval = NULL;
 4293         size_t optsize = 0;
 4294         struct sctp_inpcb *inp;
 4295         void *p;
 4296         int error = 0;
 4297 
 4298         inp = (struct sctp_inpcb *)so->so_pcb;
 4299         if (inp == 0) {
 4300                 /* I made the same as TCP since we are not setup? */
 4301                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4302                 return (ECONNRESET);
 4303         }
 4304         if (sopt->sopt_level != IPPROTO_SCTP) {
 4305                 /* wrong proto level... send back up to IP */
 4306 #ifdef INET6
 4307                 if (INP_CHECK_SOCKAF(so, AF_INET6))
 4308                         error = ip6_ctloutput(so, sopt);
 4309                 else
 4310 #endif                          /* INET6 */
 4311                         error = ip_ctloutput(so, sopt);
 4312                 return (error);
 4313         }
 4314         optsize = sopt->sopt_valsize;
 4315         if (optsize) {
 4316                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
 4317                 if (optval == NULL) {
 4318                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
 4319                         return (ENOBUFS);
 4320                 }
 4321                 error = sooptcopyin(sopt, optval, optsize, optsize);
 4322                 if (error) {
 4323                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
 4324                         goto out;
 4325                 }
 4326         }
 4327         p = (void *)sopt->sopt_td;
 4328         if (sopt->sopt_dir == SOPT_SET) {
 4329                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
 4330         } else if (sopt->sopt_dir == SOPT_GET) {
 4331                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
 4332         } else {
 4333                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4334                 error = EINVAL;
 4335         }
 4336         if ((error == 0) && (optval != NULL)) {
 4337                 error = sooptcopyout(sopt, optval, optsize);
 4338                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
 4339         } else if (optval != NULL) {
 4340                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
 4341         }
 4342 out:
 4343         return (error);
 4344 }
 4345 
 4346 
 4347 static int
 4348 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
 4349 {
 4350         int error = 0;
 4351         int create_lock_on = 0;
 4352         uint32_t vrf_id;
 4353         struct sctp_inpcb *inp;
 4354         struct sctp_tcb *stcb = NULL;
 4355 
 4356         inp = (struct sctp_inpcb *)so->so_pcb;
 4357         if (inp == 0) {
 4358                 /* I made the same as TCP since we are not setup? */
 4359                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4360                 return (ECONNRESET);
 4361         }
 4362         if (addr == NULL) {
 4363                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4364                 return EINVAL;
 4365         }
 4366 #ifdef INET6
 4367         if (addr->sa_family == AF_INET6) {
 4368                 struct sockaddr_in6 *sin6p;
 4369 
 4370                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
 4371                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4372                         return (EINVAL);
 4373                 }
 4374                 sin6p = (struct sockaddr_in6 *)addr;
 4375                 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
 4376                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 4377                         return (error);
 4378                 }
 4379         } else
 4380 #endif
 4381         if (addr->sa_family == AF_INET) {
 4382                 struct sockaddr_in *sinp;
 4383 
 4384                 if (addr->sa_len != sizeof(struct sockaddr_in)) {
 4385                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4386                         return (EINVAL);
 4387                 }
 4388                 sinp = (struct sockaddr_in *)addr;
 4389                 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
 4390                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
 4391                         return (error);
 4392                 }
 4393         } else {
 4394                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
 4395                 return (EAFNOSUPPORT);
 4396         }
 4397         SCTP_INP_INCR_REF(inp);
 4398         SCTP_ASOC_CREATE_LOCK(inp);
 4399         create_lock_on = 1;
 4400 
 4401 
 4402         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
 4403             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
 4404                 /* Should I really unlock ? */
 4405                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
 4406                 error = EFAULT;
 4407                 goto out_now;
 4408         }
 4409 #ifdef INET6
 4410         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
 4411             (addr->sa_family == AF_INET6)) {
 4412                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4413                 error = EINVAL;
 4414                 goto out_now;
 4415         }
 4416 #endif                          /* INET6 */
 4417         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
 4418             SCTP_PCB_FLAGS_UNBOUND) {
 4419                 /* Bind a ephemeral port */
 4420                 error = sctp_inpcb_bind(so, NULL, NULL, p);
 4421                 if (error) {
 4422                         goto out_now;
 4423                 }
 4424         }
 4425         /* Now do we connect? */
 4426         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
 4427             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
 4428                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4429                 error = EINVAL;
 4430                 goto out_now;
 4431         }
 4432         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
 4433             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
 4434                 /* We are already connected AND the TCP model */
 4435                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
 4436                 error = EADDRINUSE;
 4437                 goto out_now;
 4438         }
 4439         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
 4440                 SCTP_INP_RLOCK(inp);
 4441                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
 4442                 SCTP_INP_RUNLOCK(inp);
 4443         } else {
 4444                 /*
 4445                  * We increment here since sctp_findassociation_ep_addr()
 4446                  * will do a decrement if it finds the stcb as long as the
 4447                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
 4448                  */
 4449                 SCTP_INP_INCR_REF(inp);
 4450                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
 4451                 if (stcb == NULL) {
 4452                         SCTP_INP_DECR_REF(inp);
 4453                 } else {
 4454                         SCTP_TCB_UNLOCK(stcb);
 4455                 }
 4456         }
 4457         if (stcb != NULL) {
 4458                 /* Already have or am bring up an association */
 4459                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
 4460                 error = EALREADY;
 4461                 goto out_now;
 4462         }
 4463         vrf_id = inp->def_vrf_id;
 4464         /* We are GOOD to go */
 4465         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
 4466         if (stcb == NULL) {
 4467                 /* Gak! no memory */
 4468                 goto out_now;
 4469         }
 4470         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
 4471                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
 4472                 /* Set the connected flag so we can queue data */
 4473                 SOCKBUF_LOCK(&so->so_rcv);
 4474                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
 4475                 SOCKBUF_UNLOCK(&so->so_rcv);
 4476                 SOCKBUF_LOCK(&so->so_snd);
 4477                 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
 4478                 SOCKBUF_UNLOCK(&so->so_snd);
 4479                 SOCK_LOCK(so);
 4480                 so->so_state &= ~SS_ISDISCONNECTING;
 4481                 SOCK_UNLOCK(so);
 4482                 soisconnecting(so);
 4483         }
 4484         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
 4485         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
 4486 
 4487         /* initialize authentication parameters for the assoc */
 4488         sctp_initialize_auth_params(inp, stcb);
 4489 
 4490         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
 4491         SCTP_TCB_UNLOCK(stcb);
 4492 out_now:
 4493         if (create_lock_on) {
 4494                 SCTP_ASOC_CREATE_UNLOCK(inp);
 4495         }
 4496         SCTP_INP_DECR_REF(inp);
 4497         return error;
 4498 }
 4499 
 4500 int
 4501 sctp_listen(struct socket *so, int backlog, struct thread *p)
 4502 {
 4503         /*
 4504          * Note this module depends on the protocol processing being called
 4505          * AFTER any socket level flags and backlog are applied to the
 4506          * socket. The traditional way that the socket flags are applied is
 4507          * AFTER protocol processing. We have made a change to the
 4508          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
 4509          * place if the socket API for SCTP is to work properly.
 4510          */
 4511 
 4512         int error = 0;
 4513         struct sctp_inpcb *inp;
 4514 
 4515         inp = (struct sctp_inpcb *)so->so_pcb;
 4516         if (inp == 0) {
 4517                 /* I made the same as TCP since we are not setup? */
 4518                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4519                 return (ECONNRESET);
 4520         }
 4521         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
 4522                 /* See if we have a listener */
 4523                 struct sctp_inpcb *tinp;
 4524                 union sctp_sockstore store, *sp;
 4525 
 4526                 sp = &store;
 4527                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
 4528                         /* not bound all */
 4529                         struct sctp_laddr *laddr;
 4530 
 4531                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 4532                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
 4533                                 sp->sin.sin_port = inp->sctp_lport;
 4534                                 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
 4535                                 if (tinp && (tinp != inp) &&
 4536                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
 4537                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
 4538                                     (tinp->sctp_socket->so_qlimit)) {
 4539                                         /*
 4540                                          * we have a listener already and
 4541                                          * its not this inp.
 4542                                          */
 4543                                         SCTP_INP_DECR_REF(tinp);
 4544                                         return (EADDRINUSE);
 4545                                 } else if (tinp) {
 4546                                         SCTP_INP_DECR_REF(tinp);
 4547                                 }
 4548                         }
 4549                 } else {
 4550                         /* Setup a local addr bound all */
 4551                         memset(&store, 0, sizeof(store));
 4552                         store.sin.sin_port = inp->sctp_lport;
 4553 #ifdef INET6
 4554                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
 4555                                 store.sa.sa_family = AF_INET6;
 4556                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
 4557                         }
 4558 #endif
 4559                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
 4560                                 store.sa.sa_family = AF_INET;
 4561                                 store.sa.sa_len = sizeof(struct sockaddr_in);
 4562                         }
 4563                         tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
 4564                         if (tinp && (tinp != inp) &&
 4565                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
 4566                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
 4567                             (tinp->sctp_socket->so_qlimit)) {
 4568                                 /*
 4569                                  * we have a listener already and its not
 4570                                  * this inp.
 4571                                  */
 4572                                 SCTP_INP_DECR_REF(tinp);
 4573                                 return (EADDRINUSE);
 4574                         } else if (tinp) {
 4575                                 SCTP_INP_DECR_REF(inp);
 4576                         }
 4577                 }
 4578         }
 4579         SCTP_INP_RLOCK(inp);
 4580 #ifdef SCTP_LOCK_LOGGING
 4581         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
 4582                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
 4583         }
 4584 #endif
 4585         SOCK_LOCK(so);
 4586         error = solisten_proto_check(so);
 4587         if (error) {
 4588                 SOCK_UNLOCK(so);
 4589                 SCTP_INP_RUNLOCK(inp);
 4590                 return (error);
 4591         }
 4592         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
 4593             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
 4594                 /*
 4595                  * The unlucky case - We are in the tcp pool with this guy.
 4596                  * - Someone else is in the main inp slot. - We must move
 4597                  * this guy (the listener) to the main slot - We must then
 4598                  * move the guy that was listener to the TCP Pool.
 4599                  */
 4600                 if (sctp_swap_inpcb_for_listen(inp)) {
 4601                         goto in_use;
 4602                 }
 4603         }
 4604         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
 4605             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
 4606                 /* We are already connected AND the TCP model */
 4607 in_use:
 4608                 SCTP_INP_RUNLOCK(inp);
 4609                 SOCK_UNLOCK(so);
 4610                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
 4611                 return (EADDRINUSE);
 4612         }
 4613         SCTP_INP_RUNLOCK(inp);
 4614         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
 4615                 /* We must do a bind. */
 4616                 SOCK_UNLOCK(so);
 4617                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
 4618                         /* bind error, probably perm */
 4619                         return (error);
 4620                 }
 4621                 SOCK_LOCK(so);
 4622         }
 4623         /* It appears for 7.0 and on, we must always call this. */
 4624         solisten_proto(so, backlog);
 4625         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
 4626                 /* remove the ACCEPTCONN flag for one-to-many sockets */
 4627                 so->so_options &= ~SO_ACCEPTCONN;
 4628         }
 4629         if (backlog == 0) {
 4630                 /* turning off listen */
 4631                 so->so_options &= ~SO_ACCEPTCONN;
 4632         }
 4633         SOCK_UNLOCK(so);
 4634         return (error);
 4635 }
 4636 
 4637 static int sctp_defered_wakeup_cnt = 0;
 4638 
 4639 int
 4640 sctp_accept(struct socket *so, struct sockaddr **addr)
 4641 {
 4642         struct sctp_tcb *stcb;
 4643         struct sctp_inpcb *inp;
 4644         union sctp_sockstore store;
 4645 
 4646 #ifdef INET6
 4647         int error;
 4648 
 4649 #endif
 4650         inp = (struct sctp_inpcb *)so->so_pcb;
 4651 
 4652         if (inp == 0) {
 4653                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4654                 return (ECONNRESET);
 4655         }
 4656         SCTP_INP_RLOCK(inp);
 4657         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
 4658                 SCTP_INP_RUNLOCK(inp);
 4659                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
 4660                 return (EOPNOTSUPP);
 4661         }
 4662         if (so->so_state & SS_ISDISCONNECTED) {
 4663                 SCTP_INP_RUNLOCK(inp);
 4664                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
 4665                 return (ECONNABORTED);
 4666         }
 4667         stcb = LIST_FIRST(&inp->sctp_asoc_list);
 4668         if (stcb == NULL) {
 4669                 SCTP_INP_RUNLOCK(inp);
 4670                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4671                 return (ECONNRESET);
 4672         }
 4673         SCTP_TCB_LOCK(stcb);
 4674         SCTP_INP_RUNLOCK(inp);
 4675         store = stcb->asoc.primary_destination->ro._l_addr;
 4676         stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
 4677         SCTP_TCB_UNLOCK(stcb);
 4678         switch (store.sa.sa_family) {
 4679         case AF_INET:
 4680                 {
 4681                         struct sockaddr_in *sin;
 4682 
 4683                         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
 4684                         if (sin == NULL)
 4685                                 return (ENOMEM);
 4686                         sin->sin_family = AF_INET;
 4687                         sin->sin_len = sizeof(*sin);
 4688                         sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
 4689                         sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
 4690                         *addr = (struct sockaddr *)sin;
 4691                         break;
 4692                 }
 4693 #ifdef INET6
 4694         case AF_INET6:
 4695                 {
 4696                         struct sockaddr_in6 *sin6;
 4697 
 4698                         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
 4699                         if (sin6 == NULL)
 4700                                 return (ENOMEM);
 4701                         sin6->sin6_family = AF_INET6;
 4702                         sin6->sin6_len = sizeof(*sin6);
 4703                         sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
 4704 
 4705                         sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
 4706                         if ((error = sa6_recoverscope(sin6)) != 0) {
 4707                                 SCTP_FREE_SONAME(sin6);
 4708                                 return (error);
 4709                         }
 4710                         *addr = (struct sockaddr *)sin6;
 4711                         break;
 4712                 }
 4713 #endif
 4714         default:
 4715                 /* TSNH */
 4716                 break;
 4717         }
 4718         /* Wake any delayed sleep action */
 4719         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
 4720                 SCTP_INP_WLOCK(inp);
 4721                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
 4722                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
 4723                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
 4724                         SCTP_INP_WUNLOCK(inp);
 4725                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
 4726                         if (sowriteable(inp->sctp_socket)) {
 4727                                 sowwakeup_locked(inp->sctp_socket);
 4728                         } else {
 4729                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
 4730                         }
 4731                         SCTP_INP_WLOCK(inp);
 4732                 }
 4733                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
 4734                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
 4735                         SCTP_INP_WUNLOCK(inp);
 4736                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
 4737                         if (soreadable(inp->sctp_socket)) {
 4738                                 sctp_defered_wakeup_cnt++;
 4739                                 sorwakeup_locked(inp->sctp_socket);
 4740                         } else {
 4741                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
 4742                         }
 4743                         SCTP_INP_WLOCK(inp);
 4744                 }
 4745                 SCTP_INP_WUNLOCK(inp);
 4746         }
 4747         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
 4748                 SCTP_TCB_LOCK(stcb);
 4749                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
 4750         }
 4751         return (0);
 4752 }
 4753 
 4754 int
 4755 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
 4756 {
 4757         struct sockaddr_in *sin;
 4758         uint32_t vrf_id;
 4759         struct sctp_inpcb *inp;
 4760         struct sctp_ifa *sctp_ifa;
 4761 
 4762         /*
 4763          * Do the malloc first in case it blocks.
 4764          */
 4765         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
 4766         if (sin == NULL)
 4767                 return (ENOMEM);
 4768         sin->sin_family = AF_INET;
 4769         sin->sin_len = sizeof(*sin);
 4770         inp = (struct sctp_inpcb *)so->so_pcb;
 4771         if (!inp) {
 4772                 SCTP_FREE_SONAME(sin);
 4773                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4774                 return ECONNRESET;
 4775         }
 4776         SCTP_INP_RLOCK(inp);
 4777         sin->sin_port = inp->sctp_lport;
 4778         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
 4779                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
 4780                         struct sctp_tcb *stcb;
 4781                         struct sockaddr_in *sin_a;
 4782                         struct sctp_nets *net;
 4783                         int fnd;
 4784 
 4785                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
 4786                         if (stcb == NULL) {
 4787                                 goto notConn;
 4788                         }
 4789                         fnd = 0;
 4790                         sin_a = NULL;
 4791                         SCTP_TCB_LOCK(stcb);
 4792                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 4793                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
 4794                                 if (sin_a == NULL)
 4795                                         /* this will make coverity happy */
 4796                                         continue;
 4797 
 4798                                 if (sin_a->sin_family == AF_INET) {
 4799                                         fnd = 1;
 4800                                         break;
 4801                                 }
 4802                         }
 4803                         if ((!fnd) || (sin_a == NULL)) {
 4804                                 /* punt */
 4805                                 SCTP_TCB_UNLOCK(stcb);
 4806                                 goto notConn;
 4807                         }
 4808                         vrf_id = inp->def_vrf_id;
 4809                         sctp_ifa = sctp_source_address_selection(inp,
 4810                             stcb,
 4811                             (sctp_route_t *) & net->ro,
 4812                             net, 0, vrf_id);
 4813                         if (sctp_ifa) {
 4814                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
 4815                                 sctp_free_ifa(sctp_ifa);
 4816                         }
 4817                         SCTP_TCB_UNLOCK(stcb);
 4818                 } else {
 4819                         /* For the bound all case you get back 0 */
 4820         notConn:
 4821                         sin->sin_addr.s_addr = 0;
 4822                 }
 4823 
 4824         } else {
 4825                 /* Take the first IPv4 address in the list */
 4826                 struct sctp_laddr *laddr;
 4827                 int fnd = 0;
 4828 
 4829                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 4830                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
 4831                                 struct sockaddr_in *sin_a;
 4832 
 4833                                 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
 4834                                 sin->sin_addr = sin_a->sin_addr;
 4835                                 fnd = 1;
 4836                                 break;
 4837                         }
 4838                 }
 4839                 if (!fnd) {
 4840                         SCTP_FREE_SONAME(sin);
 4841                         SCTP_INP_RUNLOCK(inp);
 4842                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 4843                         return ENOENT;
 4844                 }
 4845         }
 4846         SCTP_INP_RUNLOCK(inp);
 4847         (*addr) = (struct sockaddr *)sin;
 4848         return (0);
 4849 }
 4850 
 4851 int
 4852 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
 4853 {
 4854         struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
 4855         int fnd;
 4856         struct sockaddr_in *sin_a;
 4857         struct sctp_inpcb *inp;
 4858         struct sctp_tcb *stcb;
 4859         struct sctp_nets *net;
 4860 
 4861         /* Do the malloc first in case it blocks. */
 4862         inp = (struct sctp_inpcb *)so->so_pcb;
 4863         if ((inp == NULL) ||
 4864             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
 4865                 /* UDP type and listeners will drop out here */
 4866                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
 4867                 return (ENOTCONN);
 4868         }
 4869         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
 4870         if (sin == NULL)
 4871                 return (ENOMEM);
 4872         sin->sin_family = AF_INET;
 4873         sin->sin_len = sizeof(*sin);
 4874 
 4875         /* We must recapture incase we blocked */
 4876         inp = (struct sctp_inpcb *)so->so_pcb;
 4877         if (!inp) {
 4878                 SCTP_FREE_SONAME(sin);
 4879                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4880                 return ECONNRESET;
 4881         }
 4882         SCTP_INP_RLOCK(inp);
 4883         stcb = LIST_FIRST(&inp->sctp_asoc_list);
 4884         if (stcb) {
 4885                 SCTP_TCB_LOCK(stcb);
 4886         }
 4887         SCTP_INP_RUNLOCK(inp);
 4888         if (stcb == NULL) {
 4889                 SCTP_FREE_SONAME(sin);
 4890                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
 4891                 return ECONNRESET;
 4892         }
 4893         fnd = 0;
 4894         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 4895                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
 4896                 if (sin_a->sin_family == AF_INET) {
 4897                         fnd = 1;
 4898                         sin->sin_port = stcb->rport;
 4899                         sin->sin_addr = sin_a->sin_addr;
 4900                         break;
 4901                 }
 4902         }
 4903         SCTP_TCB_UNLOCK(stcb);
 4904         if (!fnd) {
 4905                 /* No IPv4 address */
 4906                 SCTP_FREE_SONAME(sin);
 4907                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
 4908                 return ENOENT;
 4909         }
 4910         (*addr) = (struct sockaddr *)sin;
 4911         return (0);
 4912 }
 4913 
 4914 struct pr_usrreqs sctp_usrreqs = {
 4915         .pru_abort = sctp_abort,
 4916         .pru_accept = sctp_accept,
 4917         .pru_attach = sctp_attach,
 4918         .pru_bind = sctp_bind,
 4919         .pru_connect = sctp_connect,
 4920         .pru_control = in_control,
 4921         .pru_close = sctp_close,
 4922         .pru_detach = sctp_close,
 4923         .pru_sopoll = sopoll_generic,
 4924         .pru_flush = sctp_flush,
 4925         .pru_disconnect = sctp_disconnect,
 4926         .pru_listen = sctp_listen,
 4927         .pru_peeraddr = sctp_peeraddr,
 4928         .pru_send = sctp_sendm,
 4929         .pru_shutdown = sctp_shutdown,
 4930         .pru_sockaddr = sctp_ingetaddr,
 4931         .pru_sosend = sctp_sosend,
 4932         .pru_soreceive = sctp_soreceive
 4933 };

Cache object: 8709bfed6bff11e0bb32d27dee9028e0


[ 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.