Home | History | Annotate | Line # | Download | only in netinet6
sctp6_usrreq.c revision 1.13.4.1
      1       1.1       rjs /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
      2  1.13.4.1   msaitoh /* $NetBSD: sctp6_usrreq.c,v 1.13.4.1 2019/01/29 07:04:09 msaitoh Exp $ */
      3       1.1       rjs 
      4       1.1       rjs /*
      5       1.1       rjs  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
      6       1.1       rjs  * All rights reserved.
      7       1.1       rjs  *
      8       1.1       rjs  * Redistribution and use in source and binary forms, with or without
      9       1.1       rjs  * modification, are permitted provided that the following conditions
     10       1.1       rjs  * are met:
     11       1.1       rjs  * 1. Redistributions of source code must retain the above copyright
     12       1.1       rjs  *    notice, this list of conditions and the following disclaimer.
     13       1.1       rjs  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       rjs  *    notice, this list of conditions and the following disclaimer in the
     15       1.1       rjs  *    documentation and/or other materials provided with the distribution.
     16       1.1       rjs  * 3. All advertising materials mentioning features or use of this software
     17       1.1       rjs  *    must display the following acknowledgement:
     18       1.1       rjs  *      This product includes software developed by Cisco Systems, Inc.
     19       1.1       rjs  * 4. Neither the name of the project nor the names of its contributors
     20       1.1       rjs  *    may be used to endorse or promote products derived from this software
     21       1.1       rjs  *    without specific prior written permission.
     22       1.1       rjs  *
     23       1.1       rjs  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       rjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       rjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       rjs  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
     27       1.1       rjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       rjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       rjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       rjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       rjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       rjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       rjs  * SUCH DAMAGE.
     34       1.1       rjs  */
     35       1.1       rjs #include <sys/cdefs.h>
     36  1.13.4.1   msaitoh __KERNEL_RCSID(0, "$NetBSD: sctp6_usrreq.c,v 1.13.4.1 2019/01/29 07:04:09 msaitoh Exp $");
     37       1.1       rjs 
     38       1.1       rjs #ifdef _KERNEL_OPT
     39       1.1       rjs #include "opt_inet.h"
     40       1.1       rjs #include "opt_ipsec.h"
     41       1.1       rjs #include "opt_sctp.h"
     42       1.9  knakahar #include "opt_net_mpsafe.h"
     43       1.1       rjs #endif /* _KERNEL_OPT */
     44       1.1       rjs 
     45       1.1       rjs #include <sys/param.h>
     46       1.1       rjs #include <sys/kernel.h>
     47       1.1       rjs #include <sys/mbuf.h>
     48       1.1       rjs #include <sys/domain.h>
     49       1.1       rjs #include <sys/protosw.h>
     50       1.1       rjs #include <sys/socket.h>
     51       1.1       rjs #include <sys/malloc.h>
     52       1.1       rjs #include <sys/socketvar.h>
     53       1.1       rjs #include <sys/sysctl.h>
     54       1.1       rjs #include <sys/errno.h>
     55       1.1       rjs #include <sys/stat.h>
     56       1.1       rjs #include <sys/systm.h>
     57       1.1       rjs #include <sys/syslog.h>
     58       1.1       rjs #include <sys/proc.h>
     59       1.1       rjs #include <net/if.h>
     60       1.1       rjs #include <net/route.h>
     61       1.1       rjs #include <net/if_types.h>
     62       1.1       rjs #include <netinet/in.h>
     63       1.1       rjs #include <netinet/in_systm.h>
     64       1.1       rjs #include <netinet/ip.h>
     65       1.1       rjs #include <netinet/in_pcb.h>
     66       1.1       rjs #include <netinet/in_var.h>
     67       1.1       rjs #include <netinet/ip_var.h>
     68       1.1       rjs #include <netinet/sctp_pcb.h>
     69       1.1       rjs #include <netinet/sctp_header.h>
     70       1.1       rjs #include <netinet/sctp_var.h>
     71       1.1       rjs #include <netinet/sctputil.h>
     72       1.1       rjs #include <netinet/sctp_output.h>
     73       1.1       rjs #include <netinet/sctp_input.h>
     74       1.1       rjs #include <netinet/sctp_asconf.h>
     75       1.1       rjs #include <netinet6/ip6_var.h>
     76       1.1       rjs #include <netinet6/scope6_var.h>
     77       1.1       rjs #include <netinet/ip6.h>
     78       1.1       rjs #include <netinet6/in6_pcb.h>
     79       1.1       rjs #include <netinet/icmp6.h>
     80       1.1       rjs #include <netinet6/sctp6_var.h>
     81       1.1       rjs #include <netinet6/ip6protosw.h>
     82       1.1       rjs 
     83       1.1       rjs #ifdef IPSEC
     84       1.4       rjs #include <netipsec/ipsec.h>
     85       1.4       rjs #include <netipsec/ipsec6.h>
     86       1.1       rjs #endif /*IPSEC*/
     87       1.1       rjs 
     88       1.1       rjs #if defined(NFAITH) && NFAITH > 0
     89       1.1       rjs #include <net/if_faith.h>
     90       1.1       rjs #endif
     91       1.1       rjs 
     92       1.1       rjs #include <net/net_osdep.h>
     93       1.1       rjs 
     94       1.1       rjs #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
     95       1.1       rjs #ifndef in6pcb
     96       1.1       rjs #define in6pcb		inpcb
     97       1.1       rjs #endif
     98       1.1       rjs #ifndef sotoin6pcb
     99       1.1       rjs #define sotoin6pcb      sotoinpcb
    100       1.1       rjs #endif
    101       1.1       rjs #endif
    102       1.1       rjs 
    103       1.1       rjs #ifdef SCTP_DEBUG
    104       1.1       rjs extern u_int32_t sctp_debug_on;
    105       1.1       rjs #endif
    106       1.1       rjs 
    107       1.1       rjs static	int sctp6_detach(struct socket *so);
    108       1.1       rjs 
    109       1.1       rjs extern int sctp_no_csum_on_loopback;
    110       1.1       rjs 
    111       1.1       rjs int
    112       1.1       rjs sctp6_input(struct mbuf **mp, int *offp, int proto)
    113       1.1       rjs {
    114       1.1       rjs 	struct mbuf *m = *mp;
    115       1.1       rjs 	struct ip6_hdr *ip6;
    116       1.1       rjs 	struct sctphdr *sh;
    117       1.1       rjs 	struct sctp_inpcb *in6p = NULL;
    118       1.1       rjs 	struct sctp_nets *net;
    119       1.1       rjs 	int refcount_up = 0;
    120       1.1       rjs 	u_int32_t check, calc_check;
    121       1.1       rjs 	struct inpcb *in6p_ip;
    122       1.1       rjs 	struct sctp_chunkhdr *ch;
    123       1.1       rjs 	struct mbuf *opts = NULL;
    124       1.1       rjs 	int length, mlen, offset, iphlen;
    125       1.1       rjs 	u_int8_t ecn_bits;
    126       1.1       rjs 	struct sctp_tcb *stcb = NULL;
    127       1.1       rjs 	int off = *offp;
    128       1.1       rjs 	int s;
    129       1.1       rjs 
    130       1.1       rjs 	ip6 = mtod(m, struct ip6_hdr *);
    131       1.1       rjs 	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
    132       1.1       rjs 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
    133       1.1       rjs 	if (sh == NULL) {
    134       1.1       rjs 		sctp_pegs[SCTP_HDR_DROPS]++;
    135       1.1       rjs 		return IPPROTO_DONE;
    136       1.1       rjs 	}
    137       1.1       rjs 	ch = (struct sctp_chunkhdr *)((vaddr_t)sh + sizeof(struct sctphdr));
    138       1.1       rjs 
    139       1.1       rjs 	iphlen = off;
    140       1.1       rjs 	offset = iphlen + sizeof(*sh) + sizeof(*ch);
    141       1.1       rjs 
    142       1.1       rjs #if defined(NFAITH) && NFAITH > 0
    143       1.1       rjs 	if (faithprefix(&ip6->ip6_dst))
    144       1.1       rjs 		goto bad;
    145       1.1       rjs #endif /* NFAITH defined and > 0 */
    146       1.1       rjs 	sctp_pegs[SCTP_INPKTS]++;
    147       1.1       rjs #ifdef SCTP_DEBUG
    148       1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
    149       1.1       rjs 		printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
    150       1.1       rjs 	}
    151       1.1       rjs #endif
    152       1.1       rjs  	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    153       1.1       rjs 		/* No multi-cast support in SCTP */
    154       1.1       rjs 		sctp_pegs[SCTP_IN_MCAST]++;
    155       1.1       rjs 		goto bad;
    156       1.1       rjs 	}
    157       1.1       rjs 	/* destination port of 0 is illegal, based on RFC2960. */
    158       1.1       rjs 	if (sh->dest_port == 0)
    159       1.1       rjs 		goto bad;
    160       1.1       rjs 	if ((sctp_no_csum_on_loopback == 0) ||
    161       1.5     ozaki 	   (m_get_rcvif_NOMPSAFE(m) == NULL) ||
    162       1.5     ozaki 	   (m_get_rcvif_NOMPSAFE(m)->if_type != IFT_LOOP)) {
    163       1.1       rjs 		/* we do NOT validate things from the loopback if the
    164       1.1       rjs 		 * sysctl is set to 1.
    165       1.1       rjs 		 */
    166       1.1       rjs 		check = sh->checksum;		/* save incoming checksum */
    167       1.1       rjs 		if ((check == 0) && (sctp_no_csum_on_loopback)) {
    168       1.1       rjs 			/* special hook for where we got a local address
    169       1.1       rjs 			 * somehow routed across a non IFT_LOOP type interface
    170       1.1       rjs 			 */
    171       1.1       rjs 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
    172       1.1       rjs 				goto sctp_skip_csum;
    173       1.1       rjs 		}
    174       1.1       rjs 		sh->checksum = 0;		/* prepare for calc */
    175       1.1       rjs 		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
    176       1.1       rjs 		if (calc_check != check) {
    177       1.1       rjs #ifdef SCTP_DEBUG
    178       1.1       rjs 			if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
    179       1.1       rjs 				printf("Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
    180       1.1       rjs 				       calc_check, check, m,
    181       1.1       rjs 				       mlen, iphlen);
    182       1.1       rjs 			}
    183       1.1       rjs #endif
    184       1.1       rjs 			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
    185       1.1       rjs 							 sh, ch, &in6p, &net);
    186       1.1       rjs 			/* in6p's ref-count increased && stcb locked */
    187       1.1       rjs 			if ((in6p) && (stcb)) {
    188       1.1       rjs 				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
    189       1.1       rjs 				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
    190       1.1       rjs 			}  else if ((in6p != NULL) && (stcb == NULL)) {
    191       1.1       rjs 				refcount_up = 1;
    192       1.1       rjs 			}
    193       1.1       rjs 			sctp_pegs[SCTP_BAD_CSUM]++;
    194       1.1       rjs 			goto bad;
    195       1.1       rjs 		}
    196       1.1       rjs 		sh->checksum = calc_check;
    197       1.1       rjs 	} else {
    198       1.1       rjs sctp_skip_csum:
    199       1.1       rjs 		mlen = m->m_pkthdr.len;
    200       1.1       rjs 	}
    201       1.1       rjs 	net = NULL;
    202       1.1       rjs 	/*
    203       1.1       rjs 	 * Locate pcb and tcb for datagram
    204       1.1       rjs 	 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
    205       1.1       rjs 	 */
    206       1.1       rjs #ifdef SCTP_DEBUG
    207       1.1       rjs 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
    208       1.1       rjs 		printf("V6 Find the association\n");
    209       1.1       rjs 	}
    210       1.1       rjs #endif
    211       1.1       rjs 	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
    212       1.1       rjs 	    sh, ch, &in6p, &net);
    213       1.1       rjs 	/* in6p's ref-count increased */
    214       1.1       rjs 	if (in6p == NULL) {
    215       1.1       rjs 		struct sctp_init_chunk *init_chk, chunk_buf;
    216       1.1       rjs 
    217       1.1       rjs 		sctp_pegs[SCTP_NOPORTS]++;
    218       1.1       rjs 		if (ch->chunk_type == SCTP_INITIATION) {
    219       1.1       rjs 			/* we do a trick here to get the INIT tag,
    220       1.1       rjs 			 * dig in and get the tag from the INIT and
    221       1.1       rjs 			 * put it in the common header.
    222       1.1       rjs 			 */
    223       1.1       rjs 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
    224       1.1       rjs 			    iphlen + sizeof(*sh), sizeof(*init_chk),
    225       1.1       rjs 			    (u_int8_t *)&chunk_buf);
    226       1.1       rjs 			sh->v_tag = init_chk->init.initiate_tag;
    227       1.1       rjs 		}
    228       1.1       rjs 		sctp_send_abort(m, iphlen, sh, 0, NULL);
    229       1.1       rjs 		goto bad;
    230       1.1       rjs 	} else if (stcb == NULL) {
    231       1.1       rjs 		refcount_up = 1;
    232       1.1       rjs 	}
    233       1.1       rjs 	in6p_ip = (struct inpcb *)in6p;
    234       1.1       rjs #ifdef IPSEC
    235       1.1       rjs 	/*
    236       1.1       rjs 	 * Check AH/ESP integrity.
    237       1.1       rjs 	 */
    238      1.13     ozaki 	if (ipsec_used && ipsec6_in_reject(m, (struct in6pcb *)in6p_ip)) {
    239       1.1       rjs /* XXX */
    240       1.4       rjs #if 0
    241       1.4       rjs 		/* FIX ME: need to find right stat */
    242       1.1       rjs 		ipsec6stat.in_polvio++;
    243       1.1       rjs #endif
    244       1.1       rjs 		goto bad;
    245       1.1       rjs 	}
    246       1.1       rjs #endif /*IPSEC*/
    247       1.1       rjs 
    248       1.1       rjs 	/*
    249       1.1       rjs 	 * Construct sockaddr format source address.
    250       1.1       rjs 	 * Stuff source address and datagram in user buffer.
    251       1.1       rjs 	 */
    252       1.1       rjs 	if ((in6p->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
    253       1.1       rjs #ifndef __OpenBSD__
    254       1.1       rjs 	    || (in6p->sctp_socket->so_options & SO_TIMESTAMP)
    255       1.1       rjs #endif
    256       1.1       rjs 	    ) {
    257       1.1       rjs #if defined(__FreeBSD__) || defined(__APPLE__)
    258       1.1       rjs #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || defined(__APPLE__)
    259       1.1       rjs 		ip6_savecontrol(in6p_ip, &opts, ip6, m);
    260       1.1       rjs #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
    261       1.1       rjs 		ip6_savecontrol(in6p_ip, m, &opts);
    262       1.1       rjs #else
    263       1.1       rjs 		ip6_savecontrol(in6p_ip, m, &opts, NULL);
    264       1.1       rjs #endif
    265       1.1       rjs #elif defined(__NetBSD__)
    266       1.1       rjs 		ip6_savecontrol((struct in6pcb *)in6p_ip, &opts, ip6, m);
    267       1.1       rjs #else
    268       1.1       rjs 		ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
    269       1.1       rjs #endif
    270       1.1       rjs 	}
    271       1.1       rjs 
    272       1.1       rjs 	/*
    273       1.1       rjs 	 * CONTROL chunk processing
    274       1.1       rjs 	 */
    275       1.1       rjs 	length = ntohs(ip6->ip6_plen) + iphlen;
    276       1.1       rjs 	offset -= sizeof(*ch);
    277       1.1       rjs 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
    278       1.1       rjs 	s = splsoftnet();
    279       1.1       rjs 	(void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
    280       1.1       rjs 	    in6p, stcb, net, ecn_bits);
    281       1.1       rjs 	/* inp's ref-count reduced && stcb unlocked */
    282       1.1       rjs 	splx(s);
    283       1.1       rjs 	/* XXX this stuff below gets moved to appropriate parts later... */
    284       1.1       rjs 	if (m)
    285       1.1       rjs 		m_freem(m);
    286       1.1       rjs 	if (opts)
    287       1.1       rjs 		m_freem(opts);
    288       1.1       rjs 
    289       1.1       rjs 	if ((in6p) && refcount_up){
    290       1.1       rjs 		/* reduce ref-count */
    291       1.1       rjs 		SCTP_INP_WLOCK(in6p);
    292       1.1       rjs 		SCTP_INP_DECR_REF(in6p);
    293       1.1       rjs 		SCTP_INP_WUNLOCK(in6p);
    294       1.1       rjs 	}
    295       1.1       rjs 
    296       1.1       rjs 	return IPPROTO_DONE;
    297       1.1       rjs 
    298       1.1       rjs bad:
    299       1.1       rjs 	if (stcb) {
    300       1.1       rjs 		SCTP_TCB_UNLOCK(stcb);
    301       1.1       rjs 	}
    302       1.1       rjs 
    303       1.1       rjs 	if ((in6p) && refcount_up){
    304       1.1       rjs 		/* reduce ref-count */
    305       1.1       rjs 		SCTP_INP_WLOCK(in6p);
    306       1.1       rjs 		SCTP_INP_DECR_REF(in6p);
    307       1.1       rjs 		SCTP_INP_WUNLOCK(in6p);
    308       1.1       rjs 	}
    309       1.1       rjs 	if (m) {
    310       1.1       rjs 		m_freem(m);
    311       1.1       rjs 	}
    312       1.1       rjs 	if (opts) {
    313       1.1       rjs 		m_freem(opts);
    314       1.1       rjs 	}
    315       1.1       rjs 	return IPPROTO_DONE;
    316       1.1       rjs }
    317       1.1       rjs 
    318       1.1       rjs 
    319       1.1       rjs static void
    320       1.1       rjs sctp6_notify_mbuf(struct sctp_inpcb *inp,
    321       1.1       rjs 		  struct icmp6_hdr *icmp6,
    322       1.1       rjs 		  struct sctphdr *sh,
    323       1.1       rjs 		  struct sctp_tcb *stcb,
    324       1.1       rjs 		  struct sctp_nets *net)
    325       1.1       rjs {
    326       1.1       rjs 	unsigned int nxtsz;
    327       1.1       rjs 
    328       1.1       rjs 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
    329       1.1       rjs 	    (icmp6 == NULL) || (sh == NULL)) {
    330       1.1       rjs 		goto out;
    331       1.1       rjs 	}
    332       1.1       rjs 
    333       1.1       rjs 	/* First do we even look at it? */
    334       1.1       rjs 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
    335       1.1       rjs 		goto out;
    336       1.1       rjs 
    337       1.1       rjs 	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
    338       1.1       rjs 		/* not PACKET TO BIG */
    339       1.1       rjs 		goto out;
    340       1.1       rjs 	}
    341       1.1       rjs 	/*
    342       1.1       rjs 	 * ok we need to look closely. We could even get smarter and
    343       1.1       rjs 	 * look at anyone that we sent to in case we get a different
    344       1.1       rjs 	 * ICMP that tells us there is no way to reach a host, but for
    345       1.1       rjs 	 * this impl, all we care about is MTU discovery.
    346       1.1       rjs 	 */
    347       1.1       rjs 	nxtsz = ntohl(icmp6->icmp6_mtu);
    348       1.1       rjs 	/* Stop any PMTU timer */
    349       1.1       rjs 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
    350       1.1       rjs 
    351       1.1       rjs 	/* Adjust destination size limit */
    352       1.1       rjs 	if (net->mtu > nxtsz) {
    353       1.1       rjs 		net->mtu = nxtsz;
    354       1.1       rjs 	}
    355       1.1       rjs 	/* now what about the ep? */
    356       1.1       rjs 	if (stcb->asoc.smallest_mtu > nxtsz) {
    357       1.1       rjs 		struct sctp_tmit_chunk *chk;
    358       1.1       rjs 		struct sctp_stream_out *strm;
    359       1.1       rjs 		/* Adjust that too */
    360       1.1       rjs 		stcb->asoc.smallest_mtu = nxtsz;
    361       1.1       rjs 		/* now off to subtract IP_DF flag if needed */
    362       1.1       rjs 
    363       1.1       rjs 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
    364       1.1       rjs 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    365       1.1       rjs 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    366       1.1       rjs 			}
    367       1.1       rjs 		}
    368       1.1       rjs 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
    369       1.1       rjs 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    370       1.1       rjs 				/*
    371       1.1       rjs 				 * For this guy we also mark for immediate
    372       1.1       rjs 				 * resend since we sent to big of chunk
    373       1.1       rjs 				 */
    374       1.1       rjs 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    375       1.1       rjs 				if (chk->sent != SCTP_DATAGRAM_RESEND)
    376       1.1       rjs 					stcb->asoc.sent_queue_retran_cnt++;
    377       1.1       rjs 				chk->sent = SCTP_DATAGRAM_RESEND;
    378       1.1       rjs 				chk->rec.data.doing_fast_retransmit = 0;
    379       1.1       rjs 
    380       1.1       rjs 				chk->sent = SCTP_DATAGRAM_RESEND;
    381       1.1       rjs 				/* Clear any time so NO RTT is being done */
    382       1.1       rjs 				chk->sent_rcv_time.tv_sec = 0;
    383       1.1       rjs 				chk->sent_rcv_time.tv_usec = 0;
    384       1.1       rjs 				stcb->asoc.total_flight -= chk->send_size;
    385       1.1       rjs 				net->flight_size -= chk->send_size;
    386       1.1       rjs 			}
    387       1.1       rjs 		}
    388       1.1       rjs 		TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
    389       1.1       rjs 			TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
    390       1.1       rjs 				if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    391       1.1       rjs 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    392       1.1       rjs 				}
    393       1.1       rjs 			}
    394       1.1       rjs 		}
    395       1.1       rjs 	}
    396       1.1       rjs 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
    397       1.1       rjs out:
    398       1.1       rjs 	if (inp) {
    399       1.1       rjs 		/* reduce inp's ref-count */
    400       1.1       rjs 		SCTP_INP_WLOCK(inp);
    401       1.1       rjs 		SCTP_INP_DECR_REF(inp);
    402       1.1       rjs 		SCTP_INP_WUNLOCK(inp);
    403       1.1       rjs 	}
    404       1.1       rjs 	if (stcb) {
    405       1.1       rjs 		SCTP_TCB_UNLOCK(stcb);
    406       1.1       rjs 	}
    407       1.1       rjs }
    408       1.1       rjs 
    409       1.1       rjs 
    410       1.1       rjs void *
    411       1.1       rjs sctp6_ctlinput(int cmd, const struct sockaddr *pktdst, void *d)
    412       1.1       rjs {
    413       1.1       rjs 	struct sctphdr sh;
    414       1.1       rjs 	struct ip6ctlparam *ip6cp = NULL;
    415       1.1       rjs 	int s, cm;
    416       1.1       rjs 
    417       1.1       rjs 	if (pktdst->sa_family != AF_INET6 ||
    418       1.1       rjs 	    pktdst->sa_len != sizeof(struct sockaddr_in6))
    419       1.1       rjs 		return NULL;
    420       1.1       rjs 
    421       1.1       rjs 	if ((unsigned)cmd >= PRC_NCMDS)
    422       1.1       rjs 		return NULL;
    423       1.1       rjs 	if (PRC_IS_REDIRECT(cmd)) {
    424       1.1       rjs 		d = NULL;
    425       1.1       rjs 	} else if (inet6ctlerrmap[cmd] == 0) {
    426       1.1       rjs 		return NULL;
    427       1.1       rjs 	}
    428       1.1       rjs 
    429       1.1       rjs 	/* if the parameter is from icmp6, decode it. */
    430       1.1       rjs 	if (d != NULL) {
    431       1.1       rjs 		ip6cp = (struct ip6ctlparam *)d;
    432       1.1       rjs 	} else {
    433       1.1       rjs 		ip6cp = (struct ip6ctlparam *)NULL;
    434       1.1       rjs 	}
    435       1.1       rjs 
    436       1.1       rjs 	if (ip6cp) {
    437       1.1       rjs 		/*
    438       1.1       rjs 		 * XXX: We assume that when IPV6 is non NULL,
    439       1.1       rjs 		 * M and OFF are valid.
    440       1.1       rjs 		 */
    441       1.1       rjs 		/* check if we can safely examine src and dst ports */
    442       1.1       rjs 		struct sctp_inpcb *inp;
    443       1.1       rjs 		struct sctp_tcb *stcb;
    444       1.1       rjs 		struct sctp_nets *net;
    445       1.1       rjs 		struct sockaddr_in6 final;
    446       1.1       rjs 
    447       1.1       rjs 		if (ip6cp->ip6c_m == NULL ||
    448       1.1       rjs 		    (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
    449       1.1       rjs 			return NULL;
    450       1.1       rjs 
    451       1.1       rjs 		memset(&sh, 0, sizeof(sh));
    452       1.1       rjs 		memset(&final, 0, sizeof(final));
    453       1.1       rjs 		inp = NULL;
    454       1.1       rjs 		net = NULL;
    455       1.1       rjs 		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
    456       1.1       rjs 		    (void *)&sh);
    457       1.1       rjs 		ip6cp->ip6c_src->sin6_port = sh.src_port;
    458       1.1       rjs 		final.sin6_len = sizeof(final);
    459       1.1       rjs 		final.sin6_family = AF_INET6;
    460       1.1       rjs 		final.sin6_addr = ((const struct sockaddr_in6 *)pktdst)->sin6_addr;
    461       1.1       rjs 		final.sin6_port = sh.dest_port;
    462       1.1       rjs 		s = splsoftnet();
    463       1.7     ozaki 		stcb = sctp_findassociation_addr_sa(sin6tosa(ip6cp->ip6c_src),
    464       1.7     ozaki 						    sin6tosa(&final),
    465       1.1       rjs 						    &inp, &net, 1);
    466       1.1       rjs 		/* inp's ref-count increased && stcb locked */
    467       1.1       rjs 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
    468       1.1       rjs 			if (cmd == PRC_MSGSIZE) {
    469       1.1       rjs 				sctp6_notify_mbuf(inp,
    470       1.1       rjs 						  ip6cp->ip6c_icmp6,
    471       1.1       rjs 						  &sh,
    472       1.1       rjs 						  stcb,
    473       1.1       rjs 						  net);
    474       1.1       rjs 				/* inp's ref-count reduced && stcb unlocked */
    475       1.1       rjs 			} else {
    476       1.1       rjs 				if (cmd == PRC_HOSTDEAD) {
    477       1.1       rjs 					cm = EHOSTUNREACH;
    478       1.1       rjs 				} else {
    479       1.1       rjs 					cm = inet6ctlerrmap[cmd];
    480       1.1       rjs 				}
    481       1.7     ozaki 				sctp_notify(inp, cm, &sh, sin6tosa(&final),
    482       1.1       rjs 					    stcb, net);
    483       1.1       rjs 				/* inp's ref-count reduced && stcb unlocked */
    484       1.1       rjs 			}
    485       1.1       rjs 		} else {
    486       1.1       rjs 			if (PRC_IS_REDIRECT(cmd) && inp) {
    487       1.1       rjs 				in6_rtchange((struct in6pcb *)inp,
    488       1.1       rjs 					     inet6ctlerrmap[cmd]);
    489       1.1       rjs 			}
    490       1.1       rjs 			if (inp) {
    491       1.1       rjs 				/* reduce inp's ref-count */
    492       1.1       rjs 				SCTP_INP_WLOCK(inp);
    493       1.1       rjs 				SCTP_INP_DECR_REF(inp);
    494       1.1       rjs 				SCTP_INP_WUNLOCK(inp);
    495       1.1       rjs 			}
    496       1.1       rjs 			if (stcb) {
    497       1.1       rjs 				SCTP_TCB_UNLOCK(stcb);
    498       1.1       rjs 			}
    499       1.1       rjs 		}
    500       1.1       rjs 		splx(s);
    501       1.1       rjs 	}
    502       1.1       rjs 	return NULL;
    503       1.1       rjs }
    504       1.1       rjs 
    505       1.1       rjs /*
    506       1.1       rjs  * this routine can probably be collasped into the one in sctp_userreq.c
    507       1.1       rjs  * since they do the same thing and now we lookup with a sockaddr
    508       1.1       rjs  */
    509       1.1       rjs #ifdef __FreeBSD__
    510       1.1       rjs static int
    511       1.1       rjs sctp6_getcred(SYSCTL_HANDLER_ARGS)
    512       1.1       rjs {
    513       1.1       rjs 	struct sockaddr_in6 addrs[2];
    514       1.1       rjs 	struct sctp_inpcb *inp;
    515       1.1       rjs 	struct sctp_nets *net;
    516       1.1       rjs 	struct sctp_tcb *stcb;
    517       1.1       rjs 	int error, s;
    518       1.1       rjs 
    519       1.1       rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    520       1.1       rjs 	error = suser(req->td);
    521       1.1       rjs #else
    522       1.1       rjs 	error = suser(req->p);
    523       1.1       rjs #endif
    524       1.1       rjs 	if (error)
    525       1.1       rjs 		return (error);
    526       1.1       rjs 
    527       1.1       rjs 	if (req->newlen != sizeof(addrs))
    528       1.1       rjs 		return (EINVAL);
    529       1.1       rjs 	if (req->oldlen != sizeof(struct ucred))
    530       1.1       rjs 		return (EINVAL);
    531       1.1       rjs 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
    532       1.1       rjs 	if (error)
    533       1.1       rjs 		return (error);
    534       1.1       rjs 	s = splsoftnet();
    535       1.1       rjs 
    536       1.1       rjs         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
    537       1.1       rjs                                            sin6tosa(&addrs[1]),
    538       1.1       rjs                                            &inp, &net, 1);
    539       1.1       rjs 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
    540       1.1       rjs 		error = ENOENT;
    541       1.1       rjs 		if (inp) {
    542       1.1       rjs 			SCTP_INP_WLOCK(inp);
    543       1.1       rjs 			SCTP_INP_DECR_REF(inp);
    544       1.1       rjs 			SCTP_INP_WUNLOCK(inp);
    545       1.1       rjs 		}
    546       1.1       rjs 		goto out;
    547       1.1       rjs 	}
    548       1.1       rjs 	error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
    549       1.1       rjs 			   sizeof(struct ucred));
    550       1.1       rjs 
    551       1.1       rjs 	SCTP_TCB_UNLOCK (stcb);
    552       1.1       rjs  out:
    553       1.1       rjs 	splx(s);
    554       1.1       rjs 	return (error);
    555       1.1       rjs }
    556       1.1       rjs 
    557       1.1       rjs SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
    558       1.1       rjs 	    0, 0,
    559       1.1       rjs 	    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
    560       1.1       rjs 
    561       1.1       rjs #endif
    562       1.1       rjs 
    563       1.1       rjs /* This is the same as the sctp_abort() could be made common */
    564       1.1       rjs static int
    565       1.1       rjs sctp6_abort(struct socket *so)
    566       1.1       rjs {
    567       1.1       rjs 	int s;
    568       1.1       rjs 	struct sctp_inpcb *inp;
    569       1.1       rjs 
    570       1.1       rjs 	KASSERT(solocked(so));
    571       1.1       rjs 
    572       1.1       rjs 	s = splsoftnet();
    573       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    574       1.1       rjs 	if (inp == 0)
    575       1.1       rjs 		return EINVAL;	/* ??? possible? panic instead? */
    576       1.1       rjs 	soisdisconnected(so);
    577       1.1       rjs 	sctp_inpcb_free(inp, 1);
    578       1.1       rjs 	splx(s);
    579       1.1       rjs 	return 0;
    580       1.1       rjs }
    581       1.1       rjs 
    582       1.1       rjs static int
    583       1.1       rjs sctp6_attach(struct socket *so, int proto)
    584       1.1       rjs {
    585       1.1       rjs 	struct in6pcb *inp6;
    586       1.1       rjs 	int error;
    587       1.1       rjs 	struct sctp_inpcb *inp;
    588       1.1       rjs 
    589       1.1       rjs 	sosetlock(so);
    590       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    591       1.1       rjs 	if (inp != NULL)
    592       1.1       rjs 		return EINVAL;
    593       1.1       rjs 
    594       1.1       rjs 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    595       1.1       rjs 		error = soreserve(so, sctp_sendspace, sctp_recvspace);
    596       1.1       rjs 		if (error)
    597       1.1       rjs 			return error;
    598       1.1       rjs 	}
    599       1.1       rjs 	error = sctp_inpcb_alloc(so);
    600       1.1       rjs 	if (error)
    601       1.1       rjs 		return error;
    602       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    603       1.1       rjs 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
    604       1.1       rjs 	inp6 = (struct in6pcb *)inp;
    605       1.1       rjs 
    606       1.1       rjs 	inp->inp_vflag |=  INP_IPV6;
    607       1.1       rjs 	if (ip6_v6only) {
    608       1.1       rjs 		inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
    609       1.1       rjs 	}
    610       1.1       rjs 	so->so_send = sctp_sosend;
    611       1.1       rjs 
    612       1.1       rjs 	inp6->in6p_hops = -1;	        /* use kernel default */
    613       1.1       rjs 	inp6->in6p_cksum = -1;	/* just to be sure */
    614       1.1       rjs #ifdef INET
    615       1.1       rjs 	/*
    616       1.1       rjs 	 * XXX: ugly!!
    617       1.1       rjs 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
    618       1.1       rjs 	 * because the socket may be bound to an IPv6 wildcard address,
    619       1.1       rjs 	 * which may match an IPv4-mapped IPv6 address.
    620       1.1       rjs 	 */
    621       1.1       rjs 	inp->inp_ip_ttl = ip_defttl;
    622       1.1       rjs #endif
    623       1.1       rjs 	/*
    624       1.1       rjs 	 * Hmm what about the IPSEC stuff that is missing here but
    625       1.1       rjs 	 * in sctp_attach()?
    626       1.1       rjs 	 */
    627       1.1       rjs 	return 0;
    628       1.1       rjs }
    629       1.1       rjs 
    630       1.1       rjs static int
    631       1.1       rjs sctp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    632       1.1       rjs {
    633       1.1       rjs 	struct sctp_inpcb *inp;
    634       1.1       rjs 	struct in6pcb *inp6;
    635       1.1       rjs 	int error;
    636       1.1       rjs 
    637       1.1       rjs 	KASSERT(solocked(so));
    638       1.1       rjs 
    639       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    640       1.1       rjs 	if (inp == 0)
    641       1.1       rjs 		return EINVAL;
    642       1.1       rjs 
    643       1.1       rjs 	inp6 = (struct in6pcb *)inp;
    644       1.1       rjs 	inp->inp_vflag &= ~INP_IPV4;
    645       1.1       rjs 	inp->inp_vflag |= INP_IPV6;
    646       1.1       rjs 
    647       1.1       rjs 	if (nam != NULL && (inp6->in6p_flags & IN6P_IPV6_V6ONLY) == 0) {
    648       1.1       rjs 		if (nam->sa_family == AF_INET) {
    649       1.1       rjs 			/* binding v4 addr to v6 socket, so reset flags */
    650       1.1       rjs 			inp->inp_vflag |= INP_IPV4;
    651       1.1       rjs 			inp->inp_vflag &= ~INP_IPV6;
    652       1.1       rjs 		} else {
    653       1.1       rjs 			struct sockaddr_in6 *sin6_p;
    654       1.1       rjs 			sin6_p = (struct sockaddr_in6 *)nam;
    655       1.1       rjs 
    656       1.1       rjs 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
    657       1.1       rjs 			  inp->inp_vflag |= INP_IPV4;
    658       1.1       rjs 			}
    659       1.1       rjs 			else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
    660       1.1       rjs 				struct sockaddr_in sin;
    661       1.1       rjs 				in6_sin6_2_sin(&sin, sin6_p);
    662       1.1       rjs 				inp->inp_vflag |= INP_IPV4;
    663       1.1       rjs 				inp->inp_vflag &= ~INP_IPV6;
    664       1.1       rjs 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, l);
    665       1.1       rjs 				return error;
    666       1.1       rjs 			}
    667       1.1       rjs 		}
    668       1.1       rjs 	} else if (nam != NULL) {
    669       1.1       rjs 		/* IPV6_V6ONLY socket */
    670       1.1       rjs 		if (nam->sa_family == AF_INET) {
    671       1.1       rjs 			/* can't bind v4 addr to v6 only socket! */
    672       1.1       rjs 			return EINVAL;
    673       1.1       rjs 		} else {
    674       1.1       rjs 			struct sockaddr_in6 *sin6_p;
    675       1.1       rjs 			sin6_p = (struct sockaddr_in6 *)nam;
    676       1.1       rjs 
    677       1.1       rjs 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
    678       1.1       rjs 				/* can't bind v4-mapped addrs either! */
    679       1.1       rjs 				/* NOTE: we don't support SIIT */
    680       1.1       rjs 				return EINVAL;
    681       1.1       rjs 		}
    682       1.1       rjs 	}
    683       1.1       rjs 	error = sctp_inpcb_bind(so, nam, l);
    684       1.1       rjs 	return error;
    685       1.1       rjs }
    686       1.1       rjs 
    687       1.1       rjs /*This could be made common with sctp_detach() since they are identical */
    688       1.1       rjs static int
    689       1.1       rjs sctp6_detach(struct socket *so)
    690       1.1       rjs {
    691       1.1       rjs 	struct sctp_inpcb *inp;
    692       1.1       rjs 
    693       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    694       1.1       rjs 	if (inp == 0)
    695       1.1       rjs 		return EINVAL;
    696       1.1       rjs 
    697       1.1       rjs 	if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
    698       1.1       rjs 	    (so->so_rcv.sb_cc > 0))
    699       1.1       rjs 		sctp_inpcb_free(inp, 1);
    700       1.1       rjs 	else
    701       1.1       rjs 		sctp_inpcb_free(inp, 0);
    702       1.1       rjs 	return 0;
    703       1.1       rjs }
    704       1.1       rjs 
    705       1.1       rjs static int
    706       1.1       rjs sctp6_disconnect(struct socket *so)
    707       1.1       rjs {
    708       1.1       rjs 	struct sctp_inpcb *inp;
    709       1.1       rjs 
    710       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    711       1.1       rjs 	if (inp == NULL) {
    712       1.1       rjs 		return (ENOTCONN);
    713       1.1       rjs 	}
    714       1.1       rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
    715       1.1       rjs 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
    716       1.1       rjs 			/* No connection */
    717       1.1       rjs 			return (ENOTCONN);
    718       1.1       rjs 		} else {
    719       1.1       rjs 			int some_on_streamwheel = 0;
    720       1.1       rjs 			struct sctp_association *asoc;
    721       1.1       rjs 			struct sctp_tcb *stcb;
    722       1.1       rjs 
    723       1.1       rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
    724       1.1       rjs 			if (stcb == NULL) {
    725       1.1       rjs 				return (EINVAL);
    726       1.1       rjs 			}
    727       1.1       rjs 			asoc = &stcb->asoc;
    728       1.1       rjs 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
    729       1.1       rjs 				/* Check to see if some data queued */
    730       1.1       rjs 				struct sctp_stream_out *outs;
    731       1.1       rjs 				TAILQ_FOREACH(outs, &asoc->out_wheel,
    732       1.1       rjs 					      next_spoke) {
    733       1.1       rjs 					if (!TAILQ_EMPTY(&outs->outqueue)) {
    734       1.1       rjs 						some_on_streamwheel = 1;
    735       1.1       rjs 						break;
    736       1.1       rjs 					}
    737       1.1       rjs 				}
    738       1.1       rjs 			}
    739       1.1       rjs 
    740       1.1       rjs 			if (TAILQ_EMPTY(&asoc->send_queue) &&
    741       1.1       rjs 			    TAILQ_EMPTY(&asoc->sent_queue) &&
    742       1.1       rjs 			    (some_on_streamwheel == 0)) {
    743       1.1       rjs 				/* nothing queued to send, so I'm done... */
    744       1.1       rjs 				if ((SCTP_GET_STATE(asoc) !=
    745       1.1       rjs 				     SCTP_STATE_SHUTDOWN_SENT) &&
    746       1.1       rjs 				    (SCTP_GET_STATE(asoc) !=
    747       1.1       rjs 				     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
    748       1.1       rjs 					/* only send SHUTDOWN the first time */
    749       1.1       rjs #ifdef SCTP_DEBUG
    750       1.1       rjs 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    751       1.1       rjs 						printf("%s:%d sends a shutdown\n",
    752       1.1       rjs 						       __FILE__,
    753       1.1       rjs 						       __LINE__
    754       1.1       rjs 							);
    755       1.1       rjs 					}
    756       1.1       rjs #endif
    757       1.1       rjs 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
    758       1.1       rjs 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
    759       1.1       rjs 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
    760       1.1       rjs 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
    761       1.1       rjs 							 stcb->sctp_ep, stcb,
    762       1.1       rjs 							 asoc->primary_destination);
    763       1.1       rjs 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
    764       1.1       rjs 							 stcb->sctp_ep, stcb,
    765       1.1       rjs 							 asoc->primary_destination);
    766       1.1       rjs 				}
    767       1.1       rjs 			} else {
    768       1.1       rjs 				/*
    769       1.1       rjs 				 * we still got (or just got) data to send,
    770       1.1       rjs 				 * so set SHUTDOWN_PENDING
    771       1.1       rjs 				 */
    772       1.1       rjs 				/*
    773       1.1       rjs 				 * XXX sockets draft says that MSG_EOF should
    774       1.1       rjs 				 * be sent with no data.  currently, we will
    775       1.1       rjs 				 * allow user data to be sent first and move
    776       1.1       rjs 				 * to SHUTDOWN-PENDING
    777       1.1       rjs 				 */
    778       1.1       rjs 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
    779       1.1       rjs 			}
    780       1.1       rjs 			return (0);
    781       1.1       rjs 		}
    782       1.1       rjs 	} else {
    783       1.1       rjs 		/* UDP model does not support this */
    784       1.1       rjs 		return EOPNOTSUPP;
    785       1.1       rjs 	}
    786       1.1       rjs }
    787       1.1       rjs 
    788       1.1       rjs static int
    789       1.1       rjs sctp6_recvoob(struct socket *so, struct mbuf *m, int flags)
    790       1.1       rjs {
    791       1.1       rjs 	KASSERT(solocked(so));
    792       1.1       rjs 
    793       1.1       rjs 	return EOPNOTSUPP;
    794       1.1       rjs }
    795       1.1       rjs 
    796       1.1       rjs static int
    797       1.1       rjs sctp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    798       1.1       rjs 	   struct mbuf *control, struct lwp *l)
    799       1.1       rjs {
    800       1.1       rjs 	struct sctp_inpcb *inp;
    801       1.1       rjs 	struct in6pcb *inp6;
    802       1.1       rjs #ifdef INET
    803       1.1       rjs 	struct sockaddr_in6 *sin6;
    804       1.1       rjs #endif /* INET */
    805       1.1       rjs 	/* No SPL needed since sctp_output does this */
    806       1.1       rjs 
    807       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    808       1.1       rjs 	if (inp == NULL) {
    809       1.1       rjs 	        if (control) {
    810       1.1       rjs 			m_freem(control);
    811       1.1       rjs 			control = NULL;
    812       1.1       rjs 		}
    813       1.1       rjs 		m_freem(m);
    814       1.1       rjs 		return EINVAL;
    815       1.1       rjs 	}
    816       1.1       rjs 	inp6 = (struct in6pcb *)inp;
    817       1.1       rjs 	/* For the TCP model we may get a NULL addr, if we
    818       1.1       rjs 	 * are a connected socket thats ok.
    819       1.1       rjs 	 */
    820       1.1       rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
    821       1.1       rjs 	    (nam == NULL)) {
    822       1.1       rjs 	        goto connected_type;
    823       1.1       rjs 	}
    824       1.1       rjs 	if (nam == NULL) {
    825       1.1       rjs 		m_freem(m);
    826       1.1       rjs 		if (control) {
    827       1.1       rjs 			m_freem(control);
    828       1.1       rjs 			control = NULL;
    829       1.1       rjs 		}
    830       1.1       rjs 		return (EDESTADDRREQ);
    831       1.1       rjs 	}
    832       1.1       rjs 
    833       1.1       rjs #ifdef INET
    834       1.1       rjs 	sin6 = (struct sockaddr_in6 *)nam;
    835       1.1       rjs 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
    836       1.1       rjs 		/*
    837       1.1       rjs 		 * if IPV6_V6ONLY flag, we discard datagrams
    838       1.1       rjs 		 * destined to a v4 addr or v4-mapped addr
    839       1.1       rjs 		 */
    840       1.1       rjs 		if (nam->sa_family == AF_INET) {
    841       1.1       rjs 			return EINVAL;
    842       1.1       rjs 		}
    843       1.1       rjs 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    844       1.1       rjs 			return EINVAL;
    845       1.1       rjs 		}
    846       1.1       rjs 	}
    847       1.1       rjs 
    848       1.1       rjs 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    849       1.1       rjs 		if (!ip6_v6only) {
    850       1.1       rjs 			struct sockaddr_in sin;
    851       1.1       rjs 			/* convert v4-mapped into v4 addr and send */
    852       1.1       rjs 			in6_sin6_2_sin(&sin, sin6);
    853       1.1       rjs 			return sctp_send(so, m, (struct sockaddr *)&sin,
    854       1.1       rjs 			    control, l);
    855       1.1       rjs 		} else {
    856       1.1       rjs 			/* mapped addresses aren't enabled */
    857       1.1       rjs 			return EINVAL;
    858       1.1       rjs 		}
    859       1.1       rjs 	}
    860       1.1       rjs #endif /* INET */
    861       1.1       rjs  connected_type:
    862       1.1       rjs 	/* now what about control */
    863       1.1       rjs 	if (control) {
    864       1.1       rjs 		if (inp->control) {
    865       1.1       rjs 			printf("huh? control set?\n");
    866       1.1       rjs 			m_freem(inp->control);
    867       1.1       rjs 			inp->control = NULL;
    868       1.1       rjs 		}
    869       1.1       rjs 		inp->control = control;
    870       1.1       rjs 	}
    871       1.1       rjs 	/* add it in possibly */
    872       1.1       rjs 	if ((inp->pkt) &&
    873       1.1       rjs 	    (inp->pkt->m_flags & M_PKTHDR)) {
    874       1.1       rjs 		struct mbuf *x;
    875       1.1       rjs 		int c_len;
    876       1.1       rjs 
    877       1.1       rjs 		c_len = 0;
    878       1.1       rjs 		/* How big is it */
    879       1.1       rjs 		for (x=m;x;x = x->m_next) {
    880       1.1       rjs 			c_len += x->m_len;
    881       1.1       rjs 		}
    882       1.1       rjs 		inp->pkt->m_pkthdr.len += c_len;
    883       1.1       rjs 	}
    884       1.1       rjs 	/* Place the data */
    885       1.1       rjs 	if (inp->pkt) {
    886       1.1       rjs 		inp->pkt_last->m_next = m;
    887       1.1       rjs 		inp->pkt_last = m;
    888       1.1       rjs 	} else {
    889       1.1       rjs 		inp->pkt_last = inp->pkt = m;
    890       1.1       rjs 	}
    891       1.1       rjs 	if ((so->so_state & SS_MORETOCOME) == 0) {
    892       1.1       rjs 		/*
    893       1.1       rjs 		 * note with the current version this code will only be
    894       1.1       rjs 		 * used by OpenBSD, NetBSD and FreeBSD have methods for
    895       1.1       rjs 		 * re-defining sosend() to use sctp_sosend().  One can
    896       1.1       rjs 		 * optionaly switch back to this code (by changing back
    897       1.1       rjs 		 * the defininitions but this is not advisable.
    898       1.1       rjs 		 */
    899       1.1       rjs 		int ret;
    900       1.1       rjs 		ret = sctp_output(inp, inp->pkt , nam, inp->control, l, 0);
    901       1.1       rjs 		inp->pkt = NULL;
    902       1.1       rjs 		inp->control = NULL;
    903       1.1       rjs 		return (ret);
    904       1.1       rjs 	} else {
    905       1.1       rjs 		return (0);
    906       1.1       rjs 	}
    907       1.1       rjs }
    908       1.1       rjs 
    909       1.1       rjs static int
    910       1.1       rjs sctp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    911       1.1       rjs {
    912       1.1       rjs 	KASSERT(solocked(so));
    913       1.1       rjs 
    914  1.13.4.1   msaitoh 	m_freem(m);
    915  1.13.4.1   msaitoh 	m_freem(control);
    916       1.1       rjs 
    917       1.1       rjs 	return EOPNOTSUPP;
    918       1.1       rjs }
    919       1.1       rjs 
    920       1.1       rjs static int
    921       1.1       rjs sctp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
    922       1.1       rjs {
    923       1.1       rjs 	int error = 0;
    924       1.1       rjs 	struct sctp_inpcb *inp;
    925       1.1       rjs 	struct in6pcb *inp6;
    926       1.1       rjs 	struct sctp_tcb *stcb;
    927       1.1       rjs #ifdef INET
    928       1.1       rjs 	struct sockaddr_in6 *sin6;
    929       1.1       rjs 	struct sockaddr_storage ss;
    930       1.1       rjs #endif /* INET */
    931       1.1       rjs 
    932       1.1       rjs 	inp6 = (struct in6pcb *)so->so_pcb;
    933       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    934       1.1       rjs 	if (inp == 0) {
    935       1.1       rjs 		return (ECONNRESET);	/* I made the same as TCP since
    936       1.1       rjs 					 * we are not setup? */
    937       1.1       rjs 	}
    938       1.1       rjs 	SCTP_ASOC_CREATE_LOCK(inp);
    939       1.1       rjs 	SCTP_INP_RLOCK(inp);
    940       1.1       rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
    941       1.1       rjs 	    SCTP_PCB_FLAGS_UNBOUND) {
    942       1.1       rjs 		/* Bind a ephemeral port */
    943       1.1       rjs 		SCTP_INP_RUNLOCK(inp);
    944       1.1       rjs 		error = sctp6_bind(so, NULL, l);
    945       1.1       rjs 		if (error) {
    946       1.1       rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
    947       1.1       rjs 
    948       1.1       rjs 			return (error);
    949       1.1       rjs 		}
    950       1.1       rjs 		SCTP_INP_RLOCK(inp);
    951       1.1       rjs 	}
    952       1.1       rjs 
    953       1.1       rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
    954       1.1       rjs 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
    955       1.1       rjs 		/* We are already connected AND the TCP model */
    956       1.1       rjs 		SCTP_INP_RUNLOCK(inp);
    957       1.1       rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
    958       1.1       rjs 		return (EADDRINUSE);
    959       1.1       rjs 	}
    960       1.1       rjs 
    961       1.1       rjs #ifdef INET
    962       1.1       rjs 	sin6 = (struct sockaddr_in6 *)nam;
    963       1.1       rjs 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
    964       1.1       rjs 		/*
    965       1.1       rjs 		 * if IPV6_V6ONLY flag, ignore connections
    966       1.1       rjs 		 * destined to a v4 addr or v4-mapped addr
    967       1.1       rjs 		 */
    968       1.1       rjs 		if (nam->sa_family == AF_INET) {
    969       1.1       rjs 			SCTP_INP_RUNLOCK(inp);
    970       1.1       rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
    971       1.1       rjs 			return EINVAL;
    972       1.1       rjs 		}
    973       1.1       rjs 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    974       1.1       rjs 			SCTP_INP_RUNLOCK(inp);
    975       1.1       rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
    976       1.1       rjs 			return EINVAL;
    977       1.1       rjs 		}
    978       1.1       rjs 	}
    979       1.1       rjs 
    980       1.1       rjs 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    981       1.1       rjs 		if (!ip6_v6only) {
    982       1.1       rjs 			/* convert v4-mapped into v4 addr */
    983       1.1       rjs 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
    984       1.1       rjs 			nam = (struct sockaddr *)&ss;
    985       1.1       rjs 		} else {
    986       1.1       rjs 			/* mapped addresses aren't enabled */
    987       1.1       rjs 			SCTP_INP_RUNLOCK(inp);
    988       1.1       rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
    989       1.1       rjs 			return EINVAL;
    990       1.1       rjs 		}
    991       1.1       rjs 	}
    992       1.1       rjs #endif /* INET */
    993       1.1       rjs 
    994       1.1       rjs 	/* Now do we connect? */
    995       1.1       rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
    996       1.1       rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
    997       1.1       rjs 		if (stcb) {
    998       1.1       rjs 			SCTP_TCB_UNLOCK (stcb);
    999       1.1       rjs 		}
   1000       1.1       rjs 		SCTP_INP_RUNLOCK(inp);
   1001       1.1       rjs 	} else {
   1002       1.1       rjs 		SCTP_INP_RUNLOCK(inp);
   1003       1.1       rjs 		SCTP_INP_WLOCK(inp);
   1004       1.1       rjs 		SCTP_INP_INCR_REF(inp);
   1005       1.1       rjs 		SCTP_INP_WUNLOCK(inp);
   1006       1.1       rjs 		stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
   1007       1.1       rjs 		if (stcb == NULL) {
   1008       1.1       rjs 			SCTP_INP_WLOCK(inp);
   1009       1.1       rjs 			SCTP_INP_DECR_REF(inp);
   1010       1.1       rjs 			SCTP_INP_WUNLOCK(inp);
   1011       1.1       rjs 		}
   1012       1.1       rjs 	}
   1013       1.1       rjs 
   1014       1.1       rjs 	if (stcb != NULL) {
   1015       1.1       rjs 		/* Already have or am bring up an association */
   1016       1.1       rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   1017       1.1       rjs 		SCTP_TCB_UNLOCK (stcb);
   1018       1.1       rjs 		return (EALREADY);
   1019       1.1       rjs 	}
   1020       1.1       rjs 	/* We are GOOD to go */
   1021       1.1       rjs 	stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
   1022       1.1       rjs 	SCTP_ASOC_CREATE_UNLOCK(inp);
   1023       1.1       rjs 	if (stcb == NULL) {
   1024       1.1       rjs 		/* Gak! no memory */
   1025       1.1       rjs 		return (error);
   1026       1.1       rjs 	}
   1027       1.1       rjs 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
   1028       1.1       rjs 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
   1029       1.1       rjs 		/* Set the connected flag so we can queue data */
   1030       1.1       rjs 		soisconnecting(so);
   1031       1.1       rjs 	}
   1032       1.1       rjs 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
   1033       1.1       rjs 	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
   1034       1.1       rjs 	sctp_send_initiate(inp, stcb);
   1035       1.1       rjs 	SCTP_TCB_UNLOCK (stcb);
   1036       1.1       rjs 	return error;
   1037       1.1       rjs }
   1038       1.1       rjs 
   1039       1.1       rjs static int
   1040       1.1       rjs sctp6_connect2(struct socket *so, struct socket *so2)
   1041       1.1       rjs {
   1042       1.1       rjs 	KASSERT(solocked(so));
   1043       1.1       rjs 
   1044       1.1       rjs 	return EOPNOTSUPP;
   1045       1.1       rjs }
   1046       1.1       rjs 
   1047       1.1       rjs static int
   1048       1.1       rjs sctp6_getaddr(struct socket *so, struct sockaddr *nam)
   1049       1.1       rjs {
   1050       1.1       rjs 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1051       1.1       rjs 	struct sctp_inpcb *inp;
   1052       1.1       rjs 	int error;
   1053       1.1       rjs 
   1054       1.1       rjs 	/*
   1055       1.1       rjs 	 * Do the malloc first in case it blocks.
   1056       1.1       rjs 	 */
   1057       1.1       rjs 	memset(sin6, 0, sizeof(*sin6));
   1058       1.1       rjs 	sin6->sin6_family = AF_INET6;
   1059       1.1       rjs 	sin6->sin6_len = sizeof(*sin6);
   1060       1.1       rjs 
   1061       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   1062       1.1       rjs 	if (inp == NULL) {
   1063       1.1       rjs 		return ECONNRESET;
   1064       1.1       rjs 	}
   1065       1.1       rjs 
   1066       1.1       rjs 	sin6->sin6_port = inp->sctp_lport;
   1067       1.1       rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1068       1.1       rjs 		/* For the bound all case you get back 0 */
   1069       1.1       rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1070       1.1       rjs 			struct sctp_tcb *stcb;
   1071       1.1       rjs 			const struct sockaddr_in6 *sin_a6;
   1072       1.1       rjs 			struct sctp_nets *net;
   1073       1.1       rjs 			int fnd;
   1074       1.1       rjs 
   1075       1.1       rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1076       1.1       rjs 			if (stcb == NULL) {
   1077       1.1       rjs 				goto notConn6;
   1078       1.1       rjs 			}
   1079       1.1       rjs 			fnd = 0;
   1080       1.1       rjs 			sin_a6 = NULL;
   1081       1.1       rjs 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1082       1.1       rjs 				sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1083       1.1       rjs 				if (sin_a6->sin6_family == AF_INET6) {
   1084       1.1       rjs 					fnd = 1;
   1085       1.1       rjs 					break;
   1086       1.1       rjs 				}
   1087       1.1       rjs 			}
   1088       1.1       rjs 			if ((!fnd) || (sin_a6 == NULL)) {
   1089       1.1       rjs 				/* punt */
   1090       1.1       rjs 				goto notConn6;
   1091       1.1       rjs 			}
   1092       1.1       rjs 			sin6->sin6_addr = sctp_ipv6_source_address_selection(
   1093       1.1       rjs 			    inp, stcb, &net->ro, net, 0);
   1094       1.1       rjs 
   1095       1.1       rjs 		} else {
   1096       1.1       rjs 			/* For the bound all case you get back 0 */
   1097       1.1       rjs 		notConn6:
   1098       1.1       rjs 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
   1099       1.1       rjs 		}
   1100       1.1       rjs 	} else {
   1101       1.1       rjs 		/* Take the first IPv6 address in the list */
   1102       1.1       rjs 		struct sctp_laddr *laddr;
   1103       1.1       rjs 		int fnd = 0;
   1104       1.1       rjs 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   1105       1.1       rjs 			if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
   1106       1.1       rjs 				struct sockaddr_in6 *sin_a;
   1107       1.1       rjs 				sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1108       1.1       rjs 				sin6->sin6_addr = sin_a->sin6_addr;
   1109       1.1       rjs 				fnd = 1;
   1110       1.1       rjs 				break;
   1111       1.1       rjs 			}
   1112       1.1       rjs 		}
   1113       1.1       rjs 		if (!fnd) {
   1114       1.1       rjs 			return ENOENT;
   1115       1.1       rjs 		}
   1116       1.1       rjs 	}
   1117       1.1       rjs 	/* Scoping things for v6 */
   1118       1.1       rjs 	if ((error = sa6_recoverscope(sin6)) != 0)
   1119       1.1       rjs 		return (error);
   1120       1.1       rjs 
   1121       1.1       rjs 	return (0);
   1122       1.1       rjs }
   1123       1.1       rjs 
   1124       1.1       rjs static int
   1125       1.1       rjs sctp6_peeraddr(struct socket *so, struct sockaddr *nam)
   1126       1.1       rjs {
   1127       1.1       rjs 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1128       1.1       rjs 	int fnd, error;
   1129       1.1       rjs 	const struct sockaddr_in6 *sin_a6;
   1130       1.1       rjs 	struct sctp_inpcb *inp;
   1131       1.1       rjs 	struct sctp_tcb *stcb;
   1132       1.1       rjs 	struct sctp_nets *net;
   1133       1.1       rjs 	/*
   1134       1.1       rjs 	 * Do the malloc first in case it blocks.
   1135       1.1       rjs 	 */
   1136       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   1137       1.1       rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
   1138       1.1       rjs 		/* UDP type and listeners will drop out here */
   1139       1.1       rjs 		return (ENOTCONN);
   1140       1.1       rjs 	}
   1141       1.1       rjs 	memset(sin6, 0, sizeof(*sin6));
   1142       1.1       rjs 	sin6->sin6_family = AF_INET6;
   1143       1.1       rjs 	sin6->sin6_len = sizeof(*sin6);
   1144       1.1       rjs 
   1145       1.1       rjs 	/* We must recapture incase we blocked */
   1146       1.1       rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   1147       1.1       rjs 	if (inp == NULL) {
   1148       1.1       rjs 		return ECONNRESET;
   1149       1.1       rjs 	}
   1150       1.1       rjs 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1151       1.1       rjs 	if (stcb == NULL) {
   1152       1.1       rjs 		return ECONNRESET;
   1153       1.1       rjs 	}
   1154       1.1       rjs 	fnd = 0;
   1155       1.1       rjs 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1156       1.1       rjs 		sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1157       1.1       rjs 		if (sin_a6->sin6_family == AF_INET6) {
   1158       1.1       rjs 			fnd = 1;
   1159       1.1       rjs 			sin6->sin6_port = stcb->rport;
   1160       1.1       rjs 			sin6->sin6_addr = sin_a6->sin6_addr;
   1161       1.1       rjs 			break;
   1162       1.1       rjs 		}
   1163       1.1       rjs 	}
   1164       1.1       rjs 	if (!fnd) {
   1165       1.1       rjs 		/* No IPv4 address */
   1166       1.1       rjs 		return ENOENT;
   1167       1.1       rjs 	}
   1168       1.1       rjs 	if ((error = sa6_recoverscope(sin6)) != 0)
   1169       1.1       rjs 		return (error);
   1170       1.1       rjs 
   1171       1.1       rjs 	return (0);
   1172       1.1       rjs }
   1173       1.1       rjs 
   1174       1.1       rjs static int
   1175       1.1       rjs sctp6_sockaddr(struct socket *so, struct sockaddr *nam)
   1176       1.1       rjs {
   1177       1.1       rjs 	struct in6pcb *inp6 = sotoin6pcb(so);
   1178       1.1       rjs 	int	error;
   1179       1.1       rjs 
   1180       1.1       rjs 	if (inp6 == NULL)
   1181       1.1       rjs 		return EINVAL;
   1182       1.1       rjs 
   1183       1.1       rjs 	/* allow v6 addresses precedence */
   1184       1.1       rjs 	error = sctp6_getaddr(so, nam);
   1185       1.1       rjs 	if (error) {
   1186       1.1       rjs 		/* try v4 next if v6 failed */
   1187       1.1       rjs 		error = sctp_sockaddr(so, nam);
   1188       1.1       rjs 		if (error) {
   1189       1.1       rjs 			return (error);
   1190       1.1       rjs 		}
   1191       1.1       rjs 
   1192       1.1       rjs 		/* if I'm V6ONLY, convert it to v4-mapped */
   1193       1.1       rjs 		if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
   1194       1.1       rjs 			struct sockaddr_in6 sin6;
   1195       1.1       rjs 			in6_sin_2_v4mapsin6((struct sockaddr_in *)nam, &sin6);
   1196       1.1       rjs 			memcpy(nam, &sin6, sizeof(struct sockaddr_in6));
   1197       1.1       rjs 		}
   1198       1.1       rjs 	}
   1199       1.1       rjs 	return (error);
   1200       1.1       rjs }
   1201       1.1       rjs 
   1202       1.1       rjs #if 0
   1203       1.1       rjs static int
   1204       1.1       rjs sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam)
   1205       1.1       rjs {
   1206       1.1       rjs 	struct in6pcb *inp6 = sotoin6pcb(so);
   1207       1.1       rjs 	int	error;
   1208       1.1       rjs 
   1209       1.1       rjs 	if (inp6 == NULL)
   1210       1.1       rjs 		return EINVAL;
   1211       1.1       rjs 
   1212       1.1       rjs 	/* allow v6 addresses precedence */
   1213       1.1       rjs 	error = sctp6_peeraddr(so, nam);
   1214       1.1       rjs 	if (error) {
   1215       1.1       rjs 		/* try v4 next if v6 failed */
   1216       1.1       rjs 		error = sctp_peeraddr(so, nam);
   1217       1.1       rjs 		if (error) {
   1218       1.1       rjs 			return (error);
   1219       1.1       rjs 		}
   1220       1.1       rjs 		/* if I'm V6ONLY, convert it to v4-mapped */
   1221       1.1       rjs 		if ((inp6->in6p_flags & IN6P_IPV6_V6ONLY)) {
   1222       1.1       rjs 			struct sockaddr_in6 sin6;
   1223       1.1       rjs 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
   1224       1.1       rjs 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
   1225       1.1       rjs 		}
   1226       1.1       rjs 	}
   1227       1.1       rjs 	return error;
   1228       1.1       rjs }
   1229       1.1       rjs #endif
   1230       1.1       rjs 
   1231       1.1       rjs static int
   1232       1.1       rjs sctp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
   1233       1.1       rjs {
   1234       1.1       rjs 	int error = 0;
   1235       1.1       rjs 	int family;
   1236       1.1       rjs 
   1237       1.1       rjs 	family = so->so_proto->pr_domain->dom_family;
   1238       1.1       rjs 	switch (family) {
   1239       1.1       rjs #ifdef INET
   1240       1.1       rjs 	case PF_INET:
   1241       1.1       rjs 		error = in_control(so, cmd, nam, ifp);
   1242       1.1       rjs 		break;
   1243       1.1       rjs #endif
   1244       1.1       rjs #ifdef INET6
   1245       1.1       rjs 	case PF_INET6:
   1246       1.1       rjs 		error = in6_control(so, cmd, nam, ifp);
   1247       1.1       rjs 		break;
   1248       1.1       rjs #endif
   1249       1.1       rjs 	default:
   1250       1.1       rjs 		error = EAFNOSUPPORT;
   1251       1.1       rjs 	}
   1252       1.1       rjs 	return (error);
   1253       1.1       rjs }
   1254       1.1       rjs 
   1255       1.1       rjs static int
   1256       1.1       rjs sctp6_accept(struct socket *so, struct sockaddr *nam)
   1257       1.1       rjs {
   1258       1.1       rjs 	KASSERT(solocked(so));
   1259       1.1       rjs 
   1260       1.1       rjs 	return EOPNOTSUPP;
   1261       1.1       rjs }
   1262       1.1       rjs 
   1263       1.1       rjs static int
   1264       1.1       rjs sctp6_stat(struct socket *so, struct stat *ub)
   1265       1.1       rjs {
   1266       1.1       rjs 	return 0;
   1267       1.1       rjs }
   1268       1.1       rjs 
   1269       1.1       rjs static int
   1270       1.1       rjs sctp6_listen(struct socket *so, struct lwp *l)
   1271       1.1       rjs {
   1272       1.1       rjs 	return sctp_listen(so, l);
   1273       1.1       rjs }
   1274       1.1       rjs 
   1275       1.1       rjs static int
   1276       1.1       rjs sctp6_shutdown(struct socket *so)
   1277       1.1       rjs {
   1278       1.1       rjs 	return sctp_shutdown(so);
   1279       1.1       rjs }
   1280       1.1       rjs 
   1281       1.1       rjs static int
   1282       1.1       rjs sctp6_rcvd(struct socket *so, int flags, struct lwp *l)
   1283       1.1       rjs {
   1284       1.1       rjs 	KASSERT(solocked(so));
   1285       1.1       rjs 
   1286       1.1       rjs 	return sctp_rcvd(so, flags, l);
   1287       1.1       rjs }
   1288       1.1       rjs 
   1289       1.1       rjs static int
   1290       1.1       rjs sctp6_purgeif(struct socket *so, struct ifnet *ifp)
   1291       1.1       rjs {
   1292       1.1       rjs 	struct ifaddr *ifa;
   1293       1.8     ozaki 	/* FIXME NOMPSAFE */
   1294       1.6     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   1295       1.1       rjs 		if (ifa->ifa_addr->sa_family == PF_INET6) {
   1296       1.1       rjs 			sctp_delete_ip_address(ifa);
   1297       1.1       rjs 		}
   1298       1.1       rjs 	}
   1299       1.1       rjs 
   1300       1.9  knakahar #ifndef NET_MPSAFE
   1301       1.1       rjs 	mutex_enter(softnet_lock);
   1302       1.9  knakahar #endif
   1303       1.1       rjs 	in6_purgeif(ifp);
   1304       1.9  knakahar #ifndef NET_MPSAFE
   1305       1.1       rjs 	mutex_exit(softnet_lock);
   1306       1.9  knakahar #endif
   1307       1.1       rjs 
   1308       1.1       rjs 	return 0;
   1309       1.1       rjs }
   1310       1.1       rjs 
   1311       1.1       rjs PR_WRAP_USRREQS(sctp6)
   1312       1.1       rjs #define	sctp6_attach	sctp6_attach_wrapper
   1313       1.1       rjs #define	sctp6_detach	sctp6_detach_wrapper
   1314       1.1       rjs #define sctp6_accept	sctp6_accept_wrapper
   1315       1.1       rjs #define	sctp6_bind	sctp6_bind_wrapper
   1316       1.1       rjs #define	sctp6_listen	sctp6_listen_wrapper
   1317       1.1       rjs #define	sctp6_connect	sctp6_connect_wrapper
   1318       1.1       rjs #define	sctp6_connect2	sctp6_connect2_wrapper
   1319       1.1       rjs #define sctp6_disconnect	sctp6_disconnect_wrapper
   1320       1.1       rjs #define sctp6_shutdown	sctp6_shutdown_wrapper
   1321       1.1       rjs #define sctp6_abort	sctp6_abort_wrapper
   1322       1.1       rjs #define	sctp6_ioctl	sctp6_ioctl_wrapper
   1323       1.1       rjs #define	sctp6_stat	sctp6_stat_wrapper
   1324       1.1       rjs #define	sctp6_peeraddr	sctp6_peeraddr_wrapper
   1325       1.1       rjs #define sctp6_sockaddr	sctp6_sockaddr_wrapper
   1326       1.1       rjs #define sctp6_rcvd	sctp6_rcvd_wrapper
   1327       1.1       rjs #define sctp6_recvoob	sctp6_recvoob_wrapper
   1328       1.1       rjs #define sctp6_send	sctp6_send_wrapper
   1329       1.1       rjs #define sctp6_sendoob	sctp6_sendoob_wrapper
   1330       1.1       rjs #define sctp6_purgeif	sctp6_purgeif_wrapper
   1331       1.1       rjs 
   1332       1.1       rjs const struct pr_usrreqs sctp6_usrreqs = {
   1333       1.1       rjs 	.pr_attach	= sctp6_attach,
   1334       1.1       rjs 	.pr_detach	= sctp6_detach,
   1335       1.1       rjs 	.pr_accept	= sctp6_accept,
   1336       1.1       rjs 	.pr_bind	= sctp6_bind,
   1337       1.1       rjs 	.pr_listen	= sctp6_listen,
   1338       1.1       rjs 	.pr_connect	= sctp6_connect,
   1339       1.1       rjs 	.pr_connect2	= sctp6_connect2,
   1340       1.1       rjs 	.pr_disconnect	= sctp6_disconnect,
   1341       1.1       rjs 	.pr_shutdown	= sctp6_shutdown,
   1342       1.1       rjs 	.pr_abort	= sctp6_abort,
   1343       1.1       rjs 	.pr_ioctl	= sctp6_ioctl,
   1344       1.1       rjs 	.pr_stat	= sctp6_stat,
   1345       1.1       rjs 	.pr_peeraddr	= sctp6_peeraddr,
   1346       1.1       rjs 	.pr_sockaddr	= sctp6_sockaddr,
   1347       1.1       rjs 	.pr_rcvd	= sctp6_rcvd,
   1348       1.1       rjs 	.pr_recvoob	= sctp6_recvoob,
   1349       1.1       rjs 	.pr_send	= sctp6_send,
   1350       1.1       rjs 	.pr_sendoob	= sctp6_sendoob,
   1351       1.1       rjs 	.pr_purgeif	= sctp6_purgeif,
   1352       1.1       rjs };
   1353