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

Cache object: a492bf8f14fcd8855a3124a85c0ecfe6


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