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/netinet6/sctp6_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-2007, by Cisco Systems, Inc. All rights reserved.
    3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
    4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions are met:
    8  *
    9  * a) Redistributions of source code must retain the above copyright notice,
   10  *    this list of conditions and the following disclaimer.
   11  *
   12  * b) Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in
   14  *    the documentation and/or other materials provided with the distribution.
   15  *
   16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
   17  *    contributors may be used to endorse or promote products derived
   18  *    from this software without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   30  * THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/11.1/sys/netinet6/sctp6_usrreq.c 338980 2018-09-27 18:34:42Z gordon $");
   35 
   36 #include <netinet/sctp_os.h>
   37 #ifdef INET6
   38 #include <sys/proc.h>
   39 #include <netinet/sctp_pcb.h>
   40 #include <netinet/sctp_header.h>
   41 #include <netinet/sctp_var.h>
   42 #include <netinet6/sctp6_var.h>
   43 #include <netinet/sctp_sysctl.h>
   44 #include <netinet/sctp_output.h>
   45 #include <netinet/sctp_uio.h>
   46 #include <netinet/sctp_asconf.h>
   47 #include <netinet/sctputil.h>
   48 #include <netinet/sctp_indata.h>
   49 #include <netinet/sctp_timer.h>
   50 #include <netinet/sctp_auth.h>
   51 #include <netinet/sctp_input.h>
   52 #include <netinet/sctp_output.h>
   53 #include <netinet/sctp_bsd_addr.h>
   54 #include <netinet/sctp_crc32.h>
   55 #include <netinet/icmp6.h>
   56 #include <netinet/udp.h>
   57 
   58 extern struct protosw inetsw[];
   59 
   60 int
   61 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
   62 {
   63         struct mbuf *m;
   64         int iphlen;
   65         uint32_t vrf_id;
   66         uint8_t ecn_bits;
   67         struct sockaddr_in6 src, dst;
   68         struct ip6_hdr *ip6;
   69         struct sctphdr *sh;
   70         struct sctp_chunkhdr *ch;
   71         int length, offset;
   72 #if !defined(SCTP_WITH_NO_CSUM)
   73         uint8_t compute_crc;
   74 #endif
   75         uint32_t mflowid;
   76         uint8_t mflowtype;
   77         uint16_t fibnum;
   78 
   79         iphlen = *offp;
   80         if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
   81                 SCTP_RELEASE_PKT(*i_pak);
   82                 return (IPPROTO_DONE);
   83         }
   84         m = SCTP_HEADER_TO_CHAIN(*i_pak);
   85 #ifdef SCTP_MBUF_LOGGING
   86         /* Log in any input mbufs */
   87         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
   88                 sctp_log_mbc(m, SCTP_MBUF_INPUT);
   89         }
   90 #endif
   91 #ifdef SCTP_PACKET_LOGGING
   92         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
   93                 sctp_packet_log(m);
   94         }
   95 #endif
   96         SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
   97             "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
   98             m->m_pkthdr.len,
   99             if_name(m->m_pkthdr.rcvif),
  100             (int)m->m_pkthdr.csum_flags, CSUM_BITS);
  101         mflowid = m->m_pkthdr.flowid;
  102         mflowtype = M_HASHTYPE_GET(m);
  103         fibnum = M_GETFIB(m);
  104         SCTP_STAT_INCR(sctps_recvpackets);
  105         SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
  106         /* Get IP, SCTP, and first chunk header together in the first mbuf. */
  107         offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
  108         ip6 = mtod(m, struct ip6_hdr *);
  109         IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
  110             (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
  111         if (sh == NULL) {
  112                 SCTP_STAT_INCR(sctps_hdrops);
  113                 return (IPPROTO_DONE);
  114         }
  115         ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
  116         offset -= sizeof(struct sctp_chunkhdr);
  117         memset(&src, 0, sizeof(struct sockaddr_in6));
  118         src.sin6_family = AF_INET6;
  119         src.sin6_len = sizeof(struct sockaddr_in6);
  120         src.sin6_port = sh->src_port;
  121         src.sin6_addr = ip6->ip6_src;
  122         if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
  123                 goto out;
  124         }
  125         memset(&dst, 0, sizeof(struct sockaddr_in6));
  126         dst.sin6_family = AF_INET6;
  127         dst.sin6_len = sizeof(struct sockaddr_in6);
  128         dst.sin6_port = sh->dest_port;
  129         dst.sin6_addr = ip6->ip6_dst;
  130         if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
  131                 goto out;
  132         }
  133         length = ntohs(ip6->ip6_plen) + iphlen;
  134         /* Validate mbuf chain length with IP payload length. */
  135         if (SCTP_HEADER_LEN(m) != length) {
  136                 SCTPDBG(SCTP_DEBUG_INPUT1,
  137                     "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
  138                 SCTP_STAT_INCR(sctps_hdrops);
  139                 goto out;
  140         }
  141         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  142                 goto out;
  143         }
  144         ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
  145 #if defined(SCTP_WITH_NO_CSUM)
  146         SCTP_STAT_INCR(sctps_recvnocrc);
  147 #else
  148         if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
  149                 SCTP_STAT_INCR(sctps_recvhwcrc);
  150                 compute_crc = 0;
  151         } else {
  152                 SCTP_STAT_INCR(sctps_recvswcrc);
  153                 compute_crc = 1;
  154         }
  155 #endif
  156         sctp_common_input_processing(&m, iphlen, offset, length,
  157             (struct sockaddr *)&src,
  158             (struct sockaddr *)&dst,
  159             sh, ch,
  160 #if !defined(SCTP_WITH_NO_CSUM)
  161             compute_crc,
  162 #endif
  163             ecn_bits,
  164             mflowtype, mflowid, fibnum,
  165             vrf_id, port);
  166 out:
  167         if (m) {
  168                 sctp_m_freem(m);
  169         }
  170         return (IPPROTO_DONE);
  171 }
  172 
  173 
  174 int
  175 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
  176 {
  177         return (sctp6_input_with_port(i_pak, offp, 0));
  178 }
  179 
  180 void
  181 sctp6_notify(struct sctp_inpcb *inp,
  182     struct sctp_tcb *stcb,
  183     struct sctp_nets *net,
  184     uint8_t icmp6_type,
  185     uint8_t icmp6_code,
  186     uint32_t next_mtu)
  187 {
  188 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  189         struct socket *so;
  190 #endif
  191         int timer_stopped;
  192 
  193         switch (icmp6_type) {
  194         case ICMP6_DST_UNREACH:
  195                 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
  196                     (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
  197                     (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
  198                     (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
  199                         /* Mark the net unreachable. */
  200                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
  201                                 /* Ok that destination is not reachable */
  202                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
  203                                 net->dest_state &= ~SCTP_ADDR_PF;
  204                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
  205                                     stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
  206                         }
  207                 }
  208                 SCTP_TCB_UNLOCK(stcb);
  209                 break;
  210         case ICMP6_PARAM_PROB:
  211                 /* Treat it like an ABORT. */
  212                 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
  213                         sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
  214 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  215                         so = SCTP_INP_SO(inp);
  216                         atomic_add_int(&stcb->asoc.refcnt, 1);
  217                         SCTP_TCB_UNLOCK(stcb);
  218                         SCTP_SOCKET_LOCK(so, 1);
  219                         SCTP_TCB_LOCK(stcb);
  220                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
  221 #endif
  222                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  223                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
  224 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
  225                         SCTP_SOCKET_UNLOCK(so, 1);
  226 #endif
  227                 } else {
  228                         SCTP_TCB_UNLOCK(stcb);
  229                 }
  230                 break;
  231         case ICMP6_PACKET_TOO_BIG:
  232                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
  233                         timer_stopped = 1;
  234                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
  235                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
  236                 } else {
  237                         timer_stopped = 0;
  238                 }
  239                 /* Update the path MTU. */
  240                 if (net->port) {
  241                         next_mtu -= sizeof(struct udphdr);
  242                 }
  243                 if (net->mtu > next_mtu) {
  244                         net->mtu = next_mtu;
  245                 }
  246                 /* Update the association MTU */
  247                 if (stcb->asoc.smallest_mtu > next_mtu) {
  248                         sctp_pathmtu_adjustment(stcb, next_mtu);
  249                 }
  250                 /* Finally, start the PMTU timer if it was running before. */
  251                 if (timer_stopped) {
  252                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
  253                 }
  254                 SCTP_TCB_UNLOCK(stcb);
  255                 break;
  256         default:
  257                 SCTP_TCB_UNLOCK(stcb);
  258                 break;
  259         }
  260 }
  261 
  262 void
  263 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
  264 {
  265         struct ip6ctlparam *ip6cp;
  266         struct sctp_inpcb *inp;
  267         struct sctp_tcb *stcb;
  268         struct sctp_nets *net;
  269         struct sctphdr sh;
  270         struct sockaddr_in6 src, dst;
  271 
  272         if (pktdst->sa_family != AF_INET6 ||
  273             pktdst->sa_len != sizeof(struct sockaddr_in6)) {
  274                 return;
  275         }
  276         if ((unsigned)cmd >= PRC_NCMDS) {
  277                 return;
  278         }
  279         if (PRC_IS_REDIRECT(cmd)) {
  280                 d = NULL;
  281         } else if (inet6ctlerrmap[cmd] == 0) {
  282                 return;
  283         }
  284         /* If the parameter is from icmp6, decode it. */
  285         if (d != NULL) {
  286                 ip6cp = (struct ip6ctlparam *)d;
  287         } else {
  288                 ip6cp = (struct ip6ctlparam *)NULL;
  289         }
  290 
  291         if (ip6cp != NULL) {
  292                 /*
  293                  * XXX: We assume that when IPV6 is non NULL, M and OFF are
  294                  * valid.
  295                  */
  296                 if (ip6cp->ip6c_m == NULL) {
  297                         return;
  298                 }
  299                 /*
  300                  * Check if we can safely examine the ports and the
  301                  * verification tag of the SCTP common header.
  302                  */
  303                 if (ip6cp->ip6c_m->m_pkthdr.len <
  304                     (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
  305                         return;
  306                 }
  307                 /* Copy out the port numbers and the verification tag. */
  308                 bzero(&sh, sizeof(sh));
  309                 m_copydata(ip6cp->ip6c_m,
  310                     ip6cp->ip6c_off,
  311                     sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
  312                     (caddr_t)&sh);
  313                 memset(&src, 0, sizeof(struct sockaddr_in6));
  314                 src.sin6_family = AF_INET6;
  315                 src.sin6_len = sizeof(struct sockaddr_in6);
  316                 src.sin6_port = sh.src_port;
  317                 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
  318                 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
  319                         return;
  320                 }
  321                 memset(&dst, 0, sizeof(struct sockaddr_in6));
  322                 dst.sin6_family = AF_INET6;
  323                 dst.sin6_len = sizeof(struct sockaddr_in6);
  324                 dst.sin6_port = sh.dest_port;
  325                 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
  326                 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
  327                         return;
  328                 }
  329                 inp = NULL;
  330                 net = NULL;
  331                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
  332                     (struct sockaddr *)&src,
  333                     &inp, &net, 1, SCTP_DEFAULT_VRFID);
  334                 if ((stcb != NULL) &&
  335                     (net != NULL) &&
  336                     (inp != NULL)) {
  337                         /* Check the verification tag */
  338                         if (ntohl(sh.v_tag) != 0) {
  339                                 /*
  340                                  * This must be the verification tag used
  341                                  * for sending out packets. We don't
  342                                  * consider packets reflecting the
  343                                  * verification tag.
  344                                  */
  345                                 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
  346                                         SCTP_TCB_UNLOCK(stcb);
  347                                         return;
  348                                 }
  349                         } else {
  350                                 if (ip6cp->ip6c_m->m_pkthdr.len >=
  351                                     ip6cp->ip6c_off + sizeof(struct sctphdr) +
  352                                     sizeof(struct sctp_chunkhdr) +
  353                                     offsetof(struct sctp_init, a_rwnd)) {
  354                                         /*
  355                                          * In this case we can check if we
  356                                          * got an INIT chunk and if the
  357                                          * initiate tag matches.
  358                                          */
  359                                         uint32_t initiate_tag;
  360                                         uint8_t chunk_type;
  361 
  362                                         m_copydata(ip6cp->ip6c_m,
  363                                             ip6cp->ip6c_off +
  364                                             sizeof(struct sctphdr),
  365                                             sizeof(uint8_t),
  366                                             (caddr_t)&chunk_type);
  367                                         m_copydata(ip6cp->ip6c_m,
  368                                             ip6cp->ip6c_off +
  369                                             sizeof(struct sctphdr) +
  370                                             sizeof(struct sctp_chunkhdr),
  371                                             sizeof(uint32_t),
  372                                             (caddr_t)&initiate_tag);
  373                                         if ((chunk_type != SCTP_INITIATION) ||
  374                                             (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
  375                                                 SCTP_TCB_UNLOCK(stcb);
  376                                                 return;
  377                                         }
  378                                 } else {
  379                                         SCTP_TCB_UNLOCK(stcb);
  380                                         return;
  381                                 }
  382                         }
  383                         sctp6_notify(inp, stcb, net,
  384                             ip6cp->ip6c_icmp6->icmp6_type,
  385                             ip6cp->ip6c_icmp6->icmp6_code,
  386                             ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
  387                 } else {
  388                         if ((stcb == NULL) && (inp != NULL)) {
  389                                 /* reduce inp's ref-count */
  390                                 SCTP_INP_WLOCK(inp);
  391                                 SCTP_INP_DECR_REF(inp);
  392                                 SCTP_INP_WUNLOCK(inp);
  393                         }
  394                         if (stcb) {
  395                                 SCTP_TCB_UNLOCK(stcb);
  396                         }
  397                 }
  398         }
  399 }
  400 
  401 /*
  402  * this routine can probably be collasped into the one in sctp_userreq.c
  403  * since they do the same thing and now we lookup with a sockaddr
  404  */
  405 static int
  406 sctp6_getcred(SYSCTL_HANDLER_ARGS)
  407 {
  408         struct xucred xuc;
  409         struct sockaddr_in6 addrs[2];
  410         struct sctp_inpcb *inp;
  411         struct sctp_nets *net;
  412         struct sctp_tcb *stcb;
  413         int error;
  414         uint32_t vrf_id;
  415 
  416         vrf_id = SCTP_DEFAULT_VRFID;
  417 
  418         error = priv_check(req->td, PRIV_NETINET_GETCRED);
  419         if (error)
  420                 return (error);
  421 
  422         if (req->newlen != sizeof(addrs)) {
  423                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  424                 return (EINVAL);
  425         }
  426         if (req->oldlen != sizeof(struct ucred)) {
  427                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  428                 return (EINVAL);
  429         }
  430         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  431         if (error)
  432                 return (error);
  433 
  434         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
  435             sin6tosa(&addrs[0]),
  436             &inp, &net, 1, vrf_id);
  437         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
  438                 if ((inp != NULL) && (stcb == NULL)) {
  439                         /* reduce ref-count */
  440                         SCTP_INP_WLOCK(inp);
  441                         SCTP_INP_DECR_REF(inp);
  442                         goto cred_can_cont;
  443                 }
  444                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
  445                 error = ENOENT;
  446                 goto out;
  447         }
  448         SCTP_TCB_UNLOCK(stcb);
  449         /*
  450          * We use the write lock here, only since in the error leg we need
  451          * it. If we used RLOCK, then we would have to
  452          * wlock/decr/unlock/rlock. Which in theory could create a hole.
  453          * Better to use higher wlock.
  454          */
  455         SCTP_INP_WLOCK(inp);
  456 cred_can_cont:
  457         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
  458         if (error) {
  459                 SCTP_INP_WUNLOCK(inp);
  460                 goto out;
  461         }
  462         cru2x(inp->sctp_socket->so_cred, &xuc);
  463         SCTP_INP_WUNLOCK(inp);
  464         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  465 out:
  466         return (error);
  467 }
  468 
  469 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
  470     0, 0,
  471     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
  472 
  473 
  474 /* This is the same as the sctp_abort() could be made common */
  475 static void
  476 sctp6_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 == NULL) {
  483                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  484                 return;
  485         }
  486 sctp_must_try_again:
  487         flags = inp->sctp_flags;
  488 #ifdef SCTP_LOG_CLOSING
  489         sctp_log_closing(inp, NULL, 17);
  490 #endif
  491         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  492             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
  493 #ifdef SCTP_LOG_CLOSING
  494                 sctp_log_closing(inp, NULL, 16);
  495 #endif
  496                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  497                     SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
  498                 SOCK_LOCK(so);
  499                 SCTP_SB_CLEAR(so->so_snd);
  500                 /*
  501                  * same for the rcv ones, they are only here for the
  502                  * accounting/select.
  503                  */
  504                 SCTP_SB_CLEAR(so->so_rcv);
  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 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
  519 {
  520         struct in6pcb *inp6;
  521         int error;
  522         struct sctp_inpcb *inp;
  523         uint32_t vrf_id = SCTP_DEFAULT_VRFID;
  524 
  525         inp = (struct sctp_inpcb *)so->so_pcb;
  526         if (inp != NULL) {
  527                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  528                 return (EINVAL);
  529         }
  530         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
  531                 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
  532                 if (error)
  533                         return (error);
  534         }
  535         error = sctp_inpcb_alloc(so, vrf_id);
  536         if (error)
  537                 return (error);
  538         inp = (struct sctp_inpcb *)so->so_pcb;
  539         SCTP_INP_WLOCK(inp);
  540         inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;     /* I'm v6! */
  541         inp6 = (struct in6pcb *)inp;
  542 
  543         inp6->inp_vflag |= INP_IPV6;
  544         inp6->in6p_hops = -1;   /* use kernel default */
  545         inp6->in6p_cksum = -1;  /* just to be sure */
  546 #ifdef INET
  547         /*
  548          * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
  549          * socket as well, because the socket may be bound to an IPv6
  550          * wildcard address, which may match an IPv4-mapped IPv6 address.
  551          */
  552         inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
  553 #endif
  554         SCTP_INP_WUNLOCK(inp);
  555         return (0);
  556 }
  557 
  558 static int
  559 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
  560 {
  561         struct sctp_inpcb *inp;
  562         struct in6pcb *inp6;
  563         int error;
  564         u_char vflagsav;
  565 
  566         inp = (struct sctp_inpcb *)so->so_pcb;
  567         if (inp == NULL) {
  568                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  569                 return (EINVAL);
  570         }
  571         if (addr) {
  572                 switch (addr->sa_family) {
  573 #ifdef INET
  574                 case AF_INET:
  575                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
  576                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  577                                 return (EINVAL);
  578                         }
  579                         break;
  580 #endif
  581 #ifdef INET6
  582                 case AF_INET6:
  583                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
  584                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  585                                 return (EINVAL);
  586                         }
  587                         break;
  588 #endif
  589                 default:
  590                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  591                         return (EINVAL);
  592                 }
  593         }
  594         inp6 = (struct in6pcb *)inp;
  595         vflagsav = inp6->inp_vflag;
  596         inp6->inp_vflag &= ~INP_IPV4;
  597         inp6->inp_vflag |= INP_IPV6;
  598         if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
  599                 switch (addr->sa_family) {
  600 #ifdef INET
  601                 case AF_INET:
  602                         /* binding v4 addr to v6 socket, so reset flags */
  603                         inp6->inp_vflag |= INP_IPV4;
  604                         inp6->inp_vflag &= ~INP_IPV6;
  605                         break;
  606 #endif
  607 #ifdef INET6
  608                 case AF_INET6:
  609                         {
  610                                 struct sockaddr_in6 *sin6_p;
  611 
  612                                 sin6_p = (struct sockaddr_in6 *)addr;
  613 
  614                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
  615                                         inp6->inp_vflag |= INP_IPV4;
  616                                 }
  617 #ifdef INET
  618                                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
  619                                         struct sockaddr_in sin;
  620 
  621                                         in6_sin6_2_sin(&sin, sin6_p);
  622                                         inp6->inp_vflag |= INP_IPV4;
  623                                         inp6->inp_vflag &= ~INP_IPV6;
  624                                         error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
  625                                         goto out;
  626                                 }
  627 #endif
  628                                 break;
  629                         }
  630 #endif
  631                 default:
  632                         break;
  633                 }
  634         } else if (addr != NULL) {
  635                 struct sockaddr_in6 *sin6_p;
  636 
  637                 /* IPV6_V6ONLY socket */
  638 #ifdef INET
  639                 if (addr->sa_family == AF_INET) {
  640                         /* can't bind v4 addr to v6 only socket! */
  641                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  642                         error = EINVAL;
  643                         goto out;
  644                 }
  645 #endif
  646                 sin6_p = (struct sockaddr_in6 *)addr;
  647 
  648                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
  649                         /* can't bind v4-mapped addrs either! */
  650                         /* NOTE: we don't support SIIT */
  651                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  652                         error = EINVAL;
  653                         goto out;
  654                 }
  655         }
  656         error = sctp_inpcb_bind(so, addr, NULL, p);
  657 out:
  658         if (error != 0)
  659                 inp6->inp_vflag = vflagsav;
  660         return (error);
  661 }
  662 
  663 
  664 static void
  665 sctp6_close(struct socket *so)
  666 {
  667         sctp_close(so);
  668 }
  669 
  670 /* This could be made common with sctp_detach() since they are identical */
  671 
  672 static
  673 int
  674 sctp6_disconnect(struct socket *so)
  675 {
  676         return (sctp_disconnect(so));
  677 }
  678 
  679 
  680 int
  681 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  682     struct mbuf *control, struct thread *p);
  683 
  684 
  685 static int
  686 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  687     struct mbuf *control, struct thread *p)
  688 {
  689         struct sctp_inpcb *inp;
  690         struct in6pcb *inp6;
  691 
  692 #ifdef INET
  693         struct sockaddr_in6 *sin6;
  694 #endif                          /* INET */
  695         /* No SPL needed since sctp_output does this */
  696 
  697         inp = (struct sctp_inpcb *)so->so_pcb;
  698         if (inp == NULL) {
  699                 if (control) {
  700                         SCTP_RELEASE_PKT(control);
  701                         control = NULL;
  702                 }
  703                 SCTP_RELEASE_PKT(m);
  704                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  705                 return (EINVAL);
  706         }
  707         inp6 = (struct in6pcb *)inp;
  708         /*
  709          * For the TCP model we may get a NULL addr, if we are a connected
  710          * socket thats ok.
  711          */
  712         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
  713             (addr == NULL)) {
  714                 goto connected_type;
  715         }
  716         if (addr == NULL) {
  717                 SCTP_RELEASE_PKT(m);
  718                 if (control) {
  719                         SCTP_RELEASE_PKT(control);
  720                         control = NULL;
  721                 }
  722                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
  723                 return (EDESTADDRREQ);
  724         }
  725 #ifdef INET
  726         sin6 = (struct sockaddr_in6 *)addr;
  727         if (SCTP_IPV6_V6ONLY(inp6)) {
  728                 /*
  729                  * if IPV6_V6ONLY flag, we discard datagrams destined to a
  730                  * v4 addr or v4-mapped addr
  731                  */
  732                 if (addr->sa_family == AF_INET) {
  733                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  734                         return (EINVAL);
  735                 }
  736                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  737                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  738                         return (EINVAL);
  739                 }
  740         }
  741         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  742                 struct sockaddr_in sin;
  743 
  744                 /* convert v4-mapped into v4 addr and send */
  745                 in6_sin6_2_sin(&sin, sin6);
  746                 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
  747         }
  748 #endif                          /* INET */
  749 connected_type:
  750         /* now what about control */
  751         if (control) {
  752                 if (inp->control) {
  753                         SCTP_PRINTF("huh? control set?\n");
  754                         SCTP_RELEASE_PKT(inp->control);
  755                         inp->control = NULL;
  756                 }
  757                 inp->control = control;
  758         }
  759         /* Place the data */
  760         if (inp->pkt) {
  761                 SCTP_BUF_NEXT(inp->pkt_last) = m;
  762                 inp->pkt_last = m;
  763         } else {
  764                 inp->pkt_last = inp->pkt = m;
  765         }
  766         if (
  767         /* FreeBSD and MacOSX uses a flag passed */
  768             ((flags & PRUS_MORETOCOME) == 0)
  769             ) {
  770                 /*
  771                  * note with the current version this code will only be used
  772                  * by OpenBSD, NetBSD and FreeBSD have methods for
  773                  * re-defining sosend() to use sctp_sosend().  One can
  774                  * optionaly switch back to this code (by changing back the
  775                  * defininitions but this is not advisable.
  776                  */
  777                 int ret;
  778 
  779                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
  780                 inp->pkt = NULL;
  781                 inp->control = NULL;
  782                 return (ret);
  783         } else {
  784                 return (0);
  785         }
  786 }
  787 
  788 static int
  789 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
  790 {
  791         uint32_t vrf_id;
  792         int error = 0;
  793         struct sctp_inpcb *inp;
  794         struct sctp_tcb *stcb;
  795 #ifdef INET
  796         struct in6pcb *inp6;
  797         struct sockaddr_in6 *sin6;
  798         union sctp_sockstore store;
  799 #endif
  800 
  801 #ifdef INET
  802         inp6 = (struct in6pcb *)so->so_pcb;
  803 #endif
  804         inp = (struct sctp_inpcb *)so->so_pcb;
  805         if (inp == NULL) {
  806                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
  807                 return (ECONNRESET);    /* I made the same as TCP since we are
  808                                          * not setup? */
  809         }
  810         if (addr == NULL) {
  811                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  812                 return (EINVAL);
  813         }
  814         switch (addr->sa_family) {
  815 #ifdef INET
  816         case AF_INET:
  817                 if (addr->sa_len != sizeof(struct sockaddr_in)) {
  818                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  819                         return (EINVAL);
  820                 }
  821                 break;
  822 #endif
  823 #ifdef INET6
  824         case AF_INET6:
  825                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
  826                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  827                         return (EINVAL);
  828                 }
  829                 break;
  830 #endif
  831         default:
  832                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  833                 return (EINVAL);
  834         }
  835 
  836         vrf_id = inp->def_vrf_id;
  837         SCTP_ASOC_CREATE_LOCK(inp);
  838         SCTP_INP_RLOCK(inp);
  839         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
  840             SCTP_PCB_FLAGS_UNBOUND) {
  841                 /* Bind a ephemeral port */
  842                 SCTP_INP_RUNLOCK(inp);
  843                 error = sctp6_bind(so, NULL, p);
  844                 if (error) {
  845                         SCTP_ASOC_CREATE_UNLOCK(inp);
  846 
  847                         return (error);
  848                 }
  849                 SCTP_INP_RLOCK(inp);
  850         }
  851         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
  852             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
  853                 /* We are already connected AND the TCP model */
  854                 SCTP_INP_RUNLOCK(inp);
  855                 SCTP_ASOC_CREATE_UNLOCK(inp);
  856                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
  857                 return (EADDRINUSE);
  858         }
  859 #ifdef INET
  860         sin6 = (struct sockaddr_in6 *)addr;
  861         if (SCTP_IPV6_V6ONLY(inp6)) {
  862                 /*
  863                  * if IPV6_V6ONLY flag, ignore connections destined to a v4
  864                  * addr or v4-mapped addr
  865                  */
  866                 if (addr->sa_family == AF_INET) {
  867                         SCTP_INP_RUNLOCK(inp);
  868                         SCTP_ASOC_CREATE_UNLOCK(inp);
  869                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  870                         return (EINVAL);
  871                 }
  872                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  873                         SCTP_INP_RUNLOCK(inp);
  874                         SCTP_ASOC_CREATE_UNLOCK(inp);
  875                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
  876                         return (EINVAL);
  877                 }
  878         }
  879         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  880                 /* convert v4-mapped into v4 addr */
  881                 in6_sin6_2_sin(&store.sin, sin6);
  882                 addr = &store.sa;
  883         }
  884 #endif                          /* INET */
  885         /* Now do we connect? */
  886         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
  887                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
  888                 if (stcb) {
  889                         SCTP_TCB_UNLOCK(stcb);
  890                 }
  891                 SCTP_INP_RUNLOCK(inp);
  892         } else {
  893                 SCTP_INP_RUNLOCK(inp);
  894                 SCTP_INP_WLOCK(inp);
  895                 SCTP_INP_INCR_REF(inp);
  896                 SCTP_INP_WUNLOCK(inp);
  897                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
  898                 if (stcb == NULL) {
  899                         SCTP_INP_WLOCK(inp);
  900                         SCTP_INP_DECR_REF(inp);
  901                         SCTP_INP_WUNLOCK(inp);
  902                 }
  903         }
  904 
  905         if (stcb != NULL) {
  906                 /* Already have or am bring up an association */
  907                 SCTP_ASOC_CREATE_UNLOCK(inp);
  908                 SCTP_TCB_UNLOCK(stcb);
  909                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
  910                 return (EALREADY);
  911         }
  912         /* We are GOOD to go */
  913         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
  914             inp->sctp_ep.pre_open_stream_count,
  915             inp->sctp_ep.port, p);
  916         SCTP_ASOC_CREATE_UNLOCK(inp);
  917         if (stcb == NULL) {
  918                 /* Gak! no memory */
  919                 return (error);
  920         }
  921         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
  922                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
  923                 /* Set the connected flag so we can queue data */
  924                 soisconnecting(so);
  925         }
  926         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
  927         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
  928 
  929         /* initialize authentication parameters for the assoc */
  930         sctp_initialize_auth_params(inp, stcb);
  931 
  932         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
  933         SCTP_TCB_UNLOCK(stcb);
  934         return (error);
  935 }
  936 
  937 static int
  938 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
  939 {
  940         struct sockaddr_in6 *sin6;
  941         struct sctp_inpcb *inp;
  942         uint32_t vrf_id;
  943         struct sctp_ifa *sctp_ifa;
  944 
  945         int error;
  946 
  947         /*
  948          * Do the malloc first in case it blocks.
  949          */
  950         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
  951         if (sin6 == NULL)
  952                 return (ENOMEM);
  953         sin6->sin6_family = AF_INET6;
  954         sin6->sin6_len = sizeof(*sin6);
  955 
  956         inp = (struct sctp_inpcb *)so->so_pcb;
  957         if (inp == NULL) {
  958                 SCTP_FREE_SONAME(sin6);
  959                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
  960                 return (ECONNRESET);
  961         }
  962         SCTP_INP_RLOCK(inp);
  963         sin6->sin6_port = inp->sctp_lport;
  964         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  965                 /* For the bound all case you get back 0 */
  966                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
  967                         struct sctp_tcb *stcb;
  968                         struct sockaddr_in6 *sin_a6;
  969                         struct sctp_nets *net;
  970                         int fnd;
  971 
  972                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
  973                         if (stcb == NULL) {
  974                                 SCTP_INP_RUNLOCK(inp);
  975                                 SCTP_FREE_SONAME(sin6);
  976                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
  977                                 return (ENOENT);
  978                         }
  979                         fnd = 0;
  980                         sin_a6 = NULL;
  981                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  982                                 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  983                                 if (sin_a6 == NULL)
  984                                         /* this will make coverity happy */
  985                                         continue;
  986 
  987                                 if (sin_a6->sin6_family == AF_INET6) {
  988                                         fnd = 1;
  989                                         break;
  990                                 }
  991                         }
  992                         if ((!fnd) || (sin_a6 == NULL)) {
  993                                 /* punt */
  994                                 SCTP_INP_RUNLOCK(inp);
  995                                 SCTP_FREE_SONAME(sin6);
  996                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
  997                                 return (ENOENT);
  998                         }
  999                         vrf_id = inp->def_vrf_id;
 1000                         sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
 1001                         if (sctp_ifa) {
 1002                                 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
 1003                         }
 1004                 } else {
 1005                         /* For the bound all case you get back 0 */
 1006                         memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
 1007                 }
 1008         } else {
 1009                 /* Take the first IPv6 address in the list */
 1010                 struct sctp_laddr *laddr;
 1011                 int fnd = 0;
 1012 
 1013                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
 1014                         if (laddr->ifa->address.sa.sa_family == AF_INET6) {
 1015                                 struct sockaddr_in6 *sin_a;
 1016 
 1017                                 sin_a = &laddr->ifa->address.sin6;
 1018                                 sin6->sin6_addr = sin_a->sin6_addr;
 1019                                 fnd = 1;
 1020                                 break;
 1021                         }
 1022                 }
 1023                 if (!fnd) {
 1024                         SCTP_FREE_SONAME(sin6);
 1025                         SCTP_INP_RUNLOCK(inp);
 1026                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
 1027                         return (ENOENT);
 1028                 }
 1029         }
 1030         SCTP_INP_RUNLOCK(inp);
 1031         /* Scoping things for v6 */
 1032         if ((error = sa6_recoverscope(sin6)) != 0) {
 1033                 SCTP_FREE_SONAME(sin6);
 1034                 return (error);
 1035         }
 1036         (*addr) = (struct sockaddr *)sin6;
 1037         return (0);
 1038 }
 1039 
 1040 static int
 1041 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
 1042 {
 1043         struct sockaddr_in6 *sin6;
 1044         int fnd;
 1045         struct sockaddr_in6 *sin_a6;
 1046         struct sctp_inpcb *inp;
 1047         struct sctp_tcb *stcb;
 1048         struct sctp_nets *net;
 1049         int error;
 1050 
 1051         /* Do the malloc first in case it blocks. */
 1052         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
 1053         if (sin6 == NULL)
 1054                 return (ENOMEM);
 1055         sin6->sin6_family = AF_INET6;
 1056         sin6->sin6_len = sizeof(*sin6);
 1057 
 1058         inp = (struct sctp_inpcb *)so->so_pcb;
 1059         if ((inp == NULL) ||
 1060             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
 1061                 /* UDP type and listeners will drop out here */
 1062                 SCTP_FREE_SONAME(sin6);
 1063                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
 1064                 return (ENOTCONN);
 1065         }
 1066         SCTP_INP_RLOCK(inp);
 1067         stcb = LIST_FIRST(&inp->sctp_asoc_list);
 1068         if (stcb) {
 1069                 SCTP_TCB_LOCK(stcb);
 1070         }
 1071         SCTP_INP_RUNLOCK(inp);
 1072         if (stcb == NULL) {
 1073                 SCTP_FREE_SONAME(sin6);
 1074                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
 1075                 return (ECONNRESET);
 1076         }
 1077         fnd = 0;
 1078         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
 1079                 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
 1080                 if (sin_a6->sin6_family == AF_INET6) {
 1081                         fnd = 1;
 1082                         sin6->sin6_port = stcb->rport;
 1083                         sin6->sin6_addr = sin_a6->sin6_addr;
 1084                         break;
 1085                 }
 1086         }
 1087         SCTP_TCB_UNLOCK(stcb);
 1088         if (!fnd) {
 1089                 /* No IPv4 address */
 1090                 SCTP_FREE_SONAME(sin6);
 1091                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
 1092                 return (ENOENT);
 1093         }
 1094         if ((error = sa6_recoverscope(sin6)) != 0) {
 1095                 SCTP_FREE_SONAME(sin6);
 1096                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
 1097                 return (error);
 1098         }
 1099         *addr = (struct sockaddr *)sin6;
 1100         return (0);
 1101 }
 1102 
 1103 static int
 1104 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
 1105 {
 1106         struct in6pcb *inp6 = sotoin6pcb(so);
 1107         int error;
 1108 
 1109         if (inp6 == NULL) {
 1110                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
 1111                 return (EINVAL);
 1112         }
 1113         /* allow v6 addresses precedence */
 1114         error = sctp6_getaddr(so, nam);
 1115 #ifdef INET
 1116         if (error) {
 1117                 struct sockaddr_in6 *sin6;
 1118 
 1119                 /* try v4 next if v6 failed */
 1120                 error = sctp_ingetaddr(so, nam);
 1121                 if (error) {
 1122                         return (error);
 1123                 }
 1124                 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
 1125                 if (sin6 == NULL) {
 1126                         SCTP_FREE_SONAME(*nam);
 1127                         return (ENOMEM);
 1128                 }
 1129                 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
 1130                 SCTP_FREE_SONAME(*nam);
 1131                 *nam = (struct sockaddr *)sin6;
 1132         }
 1133 #endif
 1134         return (error);
 1135 }
 1136 
 1137 
 1138 static int
 1139 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
 1140 {
 1141         struct in6pcb *inp6 = sotoin6pcb(so);
 1142         int error;
 1143 
 1144         if (inp6 == NULL) {
 1145                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
 1146                 return (EINVAL);
 1147         }
 1148         /* allow v6 addresses precedence */
 1149         error = sctp6_peeraddr(so, nam);
 1150 #ifdef INET
 1151         if (error) {
 1152                 struct sockaddr_in6 *sin6;
 1153 
 1154                 /* try v4 next if v6 failed */
 1155                 error = sctp_peeraddr(so, nam);
 1156                 if (error) {
 1157                         return (error);
 1158                 }
 1159                 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
 1160                 if (sin6 == NULL) {
 1161                         SCTP_FREE_SONAME(*nam);
 1162                         return (ENOMEM);
 1163                 }
 1164                 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
 1165                 SCTP_FREE_SONAME(*nam);
 1166                 *nam = (struct sockaddr *)sin6;
 1167         }
 1168 #endif
 1169         return (error);
 1170 }
 1171 
 1172 struct pr_usrreqs sctp6_usrreqs = {
 1173         .pru_abort = sctp6_abort,
 1174         .pru_accept = sctp_accept,
 1175         .pru_attach = sctp6_attach,
 1176         .pru_bind = sctp6_bind,
 1177         .pru_connect = sctp6_connect,
 1178         .pru_control = in6_control,
 1179         .pru_close = sctp6_close,
 1180         .pru_detach = sctp6_close,
 1181         .pru_sopoll = sopoll_generic,
 1182         .pru_flush = sctp_flush,
 1183         .pru_disconnect = sctp6_disconnect,
 1184         .pru_listen = sctp_listen,
 1185         .pru_peeraddr = sctp6_getpeeraddr,
 1186         .pru_send = sctp6_send,
 1187         .pru_shutdown = sctp_shutdown,
 1188         .pru_sockaddr = sctp6_in6getaddr,
 1189         .pru_sosend = sctp_sosend,
 1190         .pru_soreceive = sctp_soreceive
 1191 };
 1192 
 1193 #endif

Cache object: fd43c2155b6f7db5fe1f650107e9f688


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