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

Cache object: 4365d5b48d25350942a37294614eb837


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