Home | History | Annotate | Line # | Download | only in netinet
sctp_usrreq.c revision 1.1
      1  1.1  rjs /*	$KAME: sctp_usrreq.c,v 1.50 2005/06/16 20:45:29 jinmei Exp $	*/
      2  1.1  rjs /*	$NetBSD: sctp_usrreq.c,v 1.1 2015/10/13 21:28:35 rjs 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.1  rjs __KERNEL_RCSID(0, "$NetBSD: sctp_usrreq.c,v 1.1 2015/10/13 21:28:35 rjs 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_sctp.h"
     41  1.1  rjs #endif /* _KERNEL_OPT */
     42  1.1  rjs 
     43  1.1  rjs #include <sys/param.h>
     44  1.1  rjs #include <sys/systm.h>
     45  1.1  rjs #include <sys/kernel.h>
     46  1.1  rjs #include <sys/malloc.h>
     47  1.1  rjs #include <sys/mbuf.h>
     48  1.1  rjs #include <sys/domain.h>
     49  1.1  rjs #include <sys/proc.h>
     50  1.1  rjs #include <sys/protosw.h>
     51  1.1  rjs #include <sys/socket.h>
     52  1.1  rjs #include <sys/socketvar.h>
     53  1.1  rjs #include <sys/sysctl.h>
     54  1.1  rjs #include <sys/syslog.h>
     55  1.1  rjs #include <net/if.h>
     56  1.1  rjs #include <net/if_types.h>
     57  1.1  rjs #include <net/route.h>
     58  1.1  rjs #include <netinet/in.h>
     59  1.1  rjs #include <netinet/in_systm.h>
     60  1.1  rjs #include <netinet/ip.h>
     61  1.1  rjs #include <netinet/ip6.h>
     62  1.1  rjs #include <netinet/in_pcb.h>
     63  1.1  rjs #include <netinet/in_var.h>
     64  1.1  rjs #include <netinet/ip_var.h>
     65  1.1  rjs #include <netinet6/ip6_var.h>
     66  1.1  rjs #include <netinet6/in6_var.h>
     67  1.1  rjs #include <netinet6/scope6_var.h>
     68  1.1  rjs 
     69  1.1  rjs #include <netinet/ip_icmp.h>
     70  1.1  rjs #include <netinet/icmp_var.h>
     71  1.1  rjs #include <netinet/sctp_pcb.h>
     72  1.1  rjs #include <netinet/sctp_header.h>
     73  1.1  rjs #include <netinet/sctp_var.h>
     74  1.1  rjs #include <netinet/sctp_output.h>
     75  1.1  rjs #include <netinet/sctp_uio.h>
     76  1.1  rjs #include <netinet/sctp_asconf.h>
     77  1.1  rjs #include <netinet/sctputil.h>
     78  1.1  rjs #include <netinet/sctp_indata.h>
     79  1.1  rjs #include <netinet/sctp_asconf.h>
     80  1.1  rjs #ifdef IPSEC
     81  1.1  rjs #include <netinet6/ipsec.h>
     82  1.1  rjs #include <netkey/key.h>
     83  1.1  rjs #endif /* IPSEC */
     84  1.1  rjs 
     85  1.1  rjs #include <net/net_osdep.h>
     86  1.1  rjs 
     87  1.1  rjs #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
     88  1.1  rjs #ifndef in6pcb
     89  1.1  rjs #define in6pcb		inpcb
     90  1.1  rjs #endif
     91  1.1  rjs #ifndef sotoin6pcb
     92  1.1  rjs #define sotoin6pcb      sotoinpcb
     93  1.1  rjs #endif
     94  1.1  rjs #endif
     95  1.1  rjs 
     96  1.1  rjs #ifdef SCTP_DEBUG
     97  1.1  rjs extern u_int32_t sctp_debug_on;
     98  1.1  rjs #endif /* SCTP_DEBUG */
     99  1.1  rjs 
    100  1.1  rjs /*
    101  1.1  rjs  * sysctl tunable variables
    102  1.1  rjs  */
    103  1.1  rjs int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
    104  1.1  rjs int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
    105  1.1  rjs int sctp_peer_chunk_oh = sizeof(struct mbuf);
    106  1.1  rjs int sctp_strict_init = 1;
    107  1.1  rjs int sctp_no_csum_on_loopback = 1;
    108  1.1  rjs unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
    109  1.1  rjs int sctp_sendspace = (128 * 1024);
    110  1.1  rjs int sctp_recvspace = 128 * (1024 +
    111  1.1  rjs #ifdef INET6
    112  1.1  rjs 				sizeof(struct sockaddr_in6)
    113  1.1  rjs #else
    114  1.1  rjs 				sizeof(struct sockaddr_in)
    115  1.1  rjs #endif
    116  1.1  rjs 	);
    117  1.1  rjs int sctp_strict_sacks = 0;
    118  1.1  rjs int sctp_ecn = 1;
    119  1.1  rjs int sctp_ecn_nonce = 0;
    120  1.1  rjs 
    121  1.1  rjs unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
    122  1.1  rjs unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
    123  1.1  rjs unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
    124  1.1  rjs unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
    125  1.1  rjs unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
    126  1.1  rjs unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
    127  1.1  rjs unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
    128  1.1  rjs unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
    129  1.1  rjs unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
    130  1.1  rjs unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
    131  1.1  rjs unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
    132  1.1  rjs unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
    133  1.1  rjs unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
    134  1.1  rjs unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
    135  1.1  rjs 
    136  1.1  rjs void
    137  1.1  rjs sctp_init(void)
    138  1.1  rjs {
    139  1.1  rjs 	/* Init the SCTP pcb in sctp_pcb.c */
    140  1.1  rjs 	u_long sb_max_adj;
    141  1.1  rjs 
    142  1.1  rjs 	sctp_pcb_init();
    143  1.1  rjs 
    144  1.1  rjs 	if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
    145  1.1  rjs 		sctp_max_chunks_on_queue = nmbclusters;
    146  1.1  rjs 	/*
    147  1.1  rjs 	 * Allow a user to take no more than 1/2 the number of clusters
    148  1.1  rjs 	 * or the SB_MAX whichever is smaller for the send window.
    149  1.1  rjs 	 */
    150  1.1  rjs 	sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
    151  1.1  rjs 	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
    152  1.1  rjs 			     ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
    153  1.1  rjs 	/*
    154  1.1  rjs 	 * Now for the recv window, should we take the same amount?
    155  1.1  rjs 	 * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
    156  1.1  rjs 	 * For now I will just copy.
    157  1.1  rjs 	 */
    158  1.1  rjs 	sctp_recvspace = sctp_sendspace;
    159  1.1  rjs }
    160  1.1  rjs 
    161  1.1  rjs #ifdef INET6
    162  1.1  rjs void
    163  1.1  rjs ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
    164  1.1  rjs {
    165  1.1  rjs 	memset(ip6, 0, sizeof(*ip6));
    166  1.1  rjs 
    167  1.1  rjs 	ip6->ip6_vfc = IPV6_VERSION;
    168  1.1  rjs 	ip6->ip6_plen = ip->ip_len;
    169  1.1  rjs 	ip6->ip6_nxt = ip->ip_p;
    170  1.1  rjs 	ip6->ip6_hlim = ip->ip_ttl;
    171  1.1  rjs 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
    172  1.1  rjs 		IPV6_ADDR_INT32_SMP;
    173  1.1  rjs 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
    174  1.1  rjs 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
    175  1.1  rjs }
    176  1.1  rjs #endif /* INET6 */
    177  1.1  rjs 
    178  1.1  rjs static void
    179  1.1  rjs sctp_split_chunks(struct sctp_association *asoc,
    180  1.1  rjs 		  struct sctp_stream_out *strm,
    181  1.1  rjs 		  struct sctp_tmit_chunk *chk)
    182  1.1  rjs {
    183  1.1  rjs 	struct sctp_tmit_chunk *new_chk;
    184  1.1  rjs 
    185  1.1  rjs 	/* First we need a chunk */
    186  1.1  rjs 	new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
    187  1.1  rjs 	if (new_chk == NULL) {
    188  1.1  rjs 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    189  1.1  rjs 		return;
    190  1.1  rjs 	}
    191  1.1  rjs 	sctppcbinfo.ipi_count_chunk++;
    192  1.1  rjs 	sctppcbinfo.ipi_gencnt_chunk++;
    193  1.1  rjs 	/* Copy it all */
    194  1.1  rjs 	*new_chk = *chk;
    195  1.1  rjs 	/*  split the data */
    196  1.1  rjs 	new_chk->data = m_split(chk->data, (chk->send_size>>1), M_DONTWAIT);
    197  1.1  rjs 	if (new_chk->data == NULL) {
    198  1.1  rjs 		/* Can't split */
    199  1.1  rjs 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    200  1.1  rjs 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
    201  1.1  rjs 		sctppcbinfo.ipi_count_chunk--;
    202  1.1  rjs 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
    203  1.1  rjs 			panic("Chunk count is negative");
    204  1.1  rjs 		}
    205  1.1  rjs 		sctppcbinfo.ipi_gencnt_chunk++;
    206  1.1  rjs 		return;
    207  1.1  rjs 
    208  1.1  rjs 	}
    209  1.1  rjs 	/* Data is now split adjust sizes */
    210  1.1  rjs 	chk->send_size >>= 1;
    211  1.1  rjs 	new_chk->send_size >>= 1;
    212  1.1  rjs 
    213  1.1  rjs 	chk->book_size >>= 1;
    214  1.1  rjs 	new_chk->book_size >>= 1;
    215  1.1  rjs 
    216  1.1  rjs 	/* now adjust the marks */
    217  1.1  rjs 	chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
    218  1.1  rjs 	chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
    219  1.1  rjs 
    220  1.1  rjs 	new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
    221  1.1  rjs 	new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
    222  1.1  rjs 
    223  1.1  rjs 	/* Increase ref count if dest is set */
    224  1.1  rjs 	if (chk->whoTo) {
    225  1.1  rjs 		new_chk->whoTo->ref_count++;
    226  1.1  rjs 	}
    227  1.1  rjs 	/* now drop it on the end of the list*/
    228  1.1  rjs 	asoc->stream_queue_cnt++;
    229  1.1  rjs 	TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
    230  1.1  rjs }
    231  1.1  rjs 
    232  1.1  rjs static void
    233  1.1  rjs sctp_notify_mbuf(struct sctp_inpcb *inp,
    234  1.1  rjs 		 struct sctp_tcb *stcb,
    235  1.1  rjs 		 struct sctp_nets *net,
    236  1.1  rjs 		 struct ip *ip,
    237  1.1  rjs 		 struct sctphdr *sh)
    238  1.1  rjs 
    239  1.1  rjs {
    240  1.1  rjs 	struct icmp *icmph;
    241  1.1  rjs 	int totsz;
    242  1.1  rjs 	uint16_t nxtsz;
    243  1.1  rjs 
    244  1.1  rjs 	/* protection */
    245  1.1  rjs 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
    246  1.1  rjs 	    (ip == NULL) || (sh == NULL)) {
    247  1.1  rjs 		if (stcb != NULL) {
    248  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
    249  1.1  rjs 		}
    250  1.1  rjs 		return;
    251  1.1  rjs 	}
    252  1.1  rjs 	/* First job is to verify the vtag matches what I would send */
    253  1.1  rjs 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
    254  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
    255  1.1  rjs 		return;
    256  1.1  rjs 	}
    257  1.1  rjs 	icmph = (struct icmp *)((vaddr_t)ip - (sizeof(struct icmp) -
    258  1.1  rjs 					       sizeof(struct ip)));
    259  1.1  rjs 	if (icmph->icmp_type != ICMP_UNREACH) {
    260  1.1  rjs 		/* We only care about unreachable */
    261  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
    262  1.1  rjs 		return;
    263  1.1  rjs 	}
    264  1.1  rjs 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
    265  1.1  rjs 		/* not a unreachable message due to frag. */
    266  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
    267  1.1  rjs 		return;
    268  1.1  rjs 	}
    269  1.1  rjs 	totsz = ip->ip_len;
    270  1.1  rjs 	nxtsz = ntohs(icmph->icmp_seq);
    271  1.1  rjs 	if (nxtsz == 0) {
    272  1.1  rjs 		/*
    273  1.1  rjs 		 * old type router that does not tell us what the next size
    274  1.1  rjs 		 * mtu is. Rats we will have to guess (in a educated fashion
    275  1.1  rjs 		 * of course)
    276  1.1  rjs 		 */
    277  1.1  rjs 		nxtsz = find_next_best_mtu(totsz);
    278  1.1  rjs 	}
    279  1.1  rjs 
    280  1.1  rjs 	/* Stop any PMTU timer */
    281  1.1  rjs 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
    282  1.1  rjs 
    283  1.1  rjs 	/* Adjust destination size limit */
    284  1.1  rjs 	if (net->mtu > nxtsz) {
    285  1.1  rjs 		net->mtu = nxtsz;
    286  1.1  rjs 	}
    287  1.1  rjs 	/* now what about the ep? */
    288  1.1  rjs 	if (stcb->asoc.smallest_mtu > nxtsz) {
    289  1.1  rjs 		struct sctp_tmit_chunk *chk, *nchk;
    290  1.1  rjs 		struct sctp_stream_out *strm;
    291  1.1  rjs 		/* Adjust that too */
    292  1.1  rjs 		stcb->asoc.smallest_mtu = nxtsz;
    293  1.1  rjs 		/* now off to subtract IP_DF flag if needed */
    294  1.1  rjs 
    295  1.1  rjs 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
    296  1.1  rjs 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    297  1.1  rjs 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    298  1.1  rjs 			}
    299  1.1  rjs 		}
    300  1.1  rjs 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
    301  1.1  rjs 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    302  1.1  rjs 				/*
    303  1.1  rjs 				 * For this guy we also mark for immediate
    304  1.1  rjs 				 * resend since we sent to big of chunk
    305  1.1  rjs 				 */
    306  1.1  rjs 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    307  1.1  rjs 				if (chk->sent != SCTP_DATAGRAM_RESEND) {
    308  1.1  rjs 					stcb->asoc.sent_queue_retran_cnt++;
    309  1.1  rjs 				}
    310  1.1  rjs 				chk->sent = SCTP_DATAGRAM_RESEND;
    311  1.1  rjs 				chk->rec.data.doing_fast_retransmit = 0;
    312  1.1  rjs 
    313  1.1  rjs 				/* Clear any time so NO RTT is being done */
    314  1.1  rjs 				chk->do_rtt = 0;
    315  1.1  rjs 				sctp_total_flight_decrease(stcb, chk);
    316  1.1  rjs 				if (net->flight_size >= chk->book_size) {
    317  1.1  rjs 					net->flight_size -= chk->book_size;
    318  1.1  rjs 				} else {
    319  1.1  rjs 					net->flight_size = 0;
    320  1.1  rjs 				}
    321  1.1  rjs 			}
    322  1.1  rjs 		}
    323  1.1  rjs 		TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
    324  1.1  rjs 			chk = TAILQ_FIRST(&strm->outqueue);
    325  1.1  rjs 			while (chk) {
    326  1.1  rjs 				nchk = TAILQ_NEXT(chk, sctp_next);
    327  1.1  rjs 				if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
    328  1.1  rjs 					sctp_split_chunks(&stcb->asoc, strm, chk);
    329  1.1  rjs 				}
    330  1.1  rjs 				chk = nchk;
    331  1.1  rjs 			}
    332  1.1  rjs 		}
    333  1.1  rjs 	}
    334  1.1  rjs 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
    335  1.1  rjs 	SCTP_TCB_UNLOCK(stcb);
    336  1.1  rjs }
    337  1.1  rjs 
    338  1.1  rjs 
    339  1.1  rjs void
    340  1.1  rjs sctp_notify(struct sctp_inpcb *inp,
    341  1.1  rjs 	    int errno,
    342  1.1  rjs 	    struct sctphdr *sh,
    343  1.1  rjs 	    struct sockaddr *to,
    344  1.1  rjs 	    struct sctp_tcb *stcb,
    345  1.1  rjs 	    struct sctp_nets *net)
    346  1.1  rjs {
    347  1.1  rjs 	/* protection */
    348  1.1  rjs 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
    349  1.1  rjs 	    (sh == NULL) || (to == NULL)) {
    350  1.1  rjs #ifdef SCTP_DEBUG
    351  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
    352  1.1  rjs 			printf("sctp-notify, bad call\n");
    353  1.1  rjs 		}
    354  1.1  rjs #endif /* SCTP_DEBUG */
    355  1.1  rjs 		return;
    356  1.1  rjs 	}
    357  1.1  rjs 	/* First job is to verify the vtag matches what I would send */
    358  1.1  rjs 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
    359  1.1  rjs 		return;
    360  1.1  rjs 	}
    361  1.1  rjs 
    362  1.1  rjs /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
    363  1.1  rjs 
    364  1.1  rjs 	if ((errno == EHOSTUNREACH) ||  /* Host is not reachable */
    365  1.1  rjs 	    (errno == EHOSTDOWN) ||	/* Host is down */
    366  1.1  rjs 	    (errno == ECONNREFUSED) ||	/* Host refused the connection, (not an abort?) */
    367  1.1  rjs 	    (errno == ENOPROTOOPT)	/* SCTP is not present on host */
    368  1.1  rjs 		) {
    369  1.1  rjs 		/*
    370  1.1  rjs 		 * Hmm reachablity problems we must examine closely.
    371  1.1  rjs 		 * If its not reachable, we may have lost a network.
    372  1.1  rjs 		 * Or if there is NO protocol at the other end named SCTP.
    373  1.1  rjs 		 * well we consider it a OOTB abort.
    374  1.1  rjs 		 */
    375  1.1  rjs 		if ((errno == EHOSTUNREACH) || (errno == EHOSTDOWN)) {
    376  1.1  rjs 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
    377  1.1  rjs 				/* Ok that destination is NOT reachable */
    378  1.1  rjs 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
    379  1.1  rjs 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
    380  1.1  rjs 				net->error_count = net->failure_threshold + 1;
    381  1.1  rjs 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
    382  1.1  rjs 						stcb, SCTP_FAILED_THRESHOLD,
    383  1.1  rjs 						(void *)net);
    384  1.1  rjs 			}
    385  1.1  rjs 			if (stcb) {
    386  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
    387  1.1  rjs 			}
    388  1.1  rjs 		} else {
    389  1.1  rjs 			/*
    390  1.1  rjs 			 * Here the peer is either playing tricks on us,
    391  1.1  rjs 			 * including an address that belongs to someone who
    392  1.1  rjs 			 * does not support SCTP OR was a userland
    393  1.1  rjs 			 * implementation that shutdown and now is dead. In
    394  1.1  rjs 			 * either case treat it like a OOTB abort with no TCB
    395  1.1  rjs 			 */
    396  1.1  rjs 			sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
    397  1.1  rjs 			sctp_free_assoc(inp, stcb);
    398  1.1  rjs 			/* no need to unlock here, since the TCB is gone */
    399  1.1  rjs 		}
    400  1.1  rjs 	} else {
    401  1.1  rjs 		/* Send all others to the app */
    402  1.1  rjs 		if (inp->sctp_socket) {
    403  1.1  rjs 			inp->sctp_socket->so_error = errno;
    404  1.1  rjs 			sctp_sowwakeup(inp, inp->sctp_socket);
    405  1.1  rjs 		}
    406  1.1  rjs 	        if (stcb) {
    407  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
    408  1.1  rjs 		}
    409  1.1  rjs 	}
    410  1.1  rjs }
    411  1.1  rjs 
    412  1.1  rjs void *
    413  1.1  rjs sctp_ctlinput(int cmd, const struct sockaddr *sa, void *vip)
    414  1.1  rjs {
    415  1.1  rjs 	struct ip *ip = vip;
    416  1.1  rjs 	struct sctphdr *sh;
    417  1.1  rjs 	int s;
    418  1.1  rjs 
    419  1.1  rjs 	if (sa->sa_family != AF_INET ||
    420  1.1  rjs 	    ((const struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
    421  1.1  rjs 		return (NULL);
    422  1.1  rjs 	}
    423  1.1  rjs 
    424  1.1  rjs 	if (PRC_IS_REDIRECT(cmd)) {
    425  1.1  rjs 		ip = 0;
    426  1.1  rjs 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
    427  1.1  rjs 		return (NULL);
    428  1.1  rjs 	}
    429  1.1  rjs 	if (ip) {
    430  1.1  rjs 		struct sctp_inpcb *inp;
    431  1.1  rjs 		struct sctp_tcb *stcb;
    432  1.1  rjs 		struct sctp_nets *net;
    433  1.1  rjs 		struct sockaddr_in to, from;
    434  1.1  rjs 
    435  1.1  rjs 		sh = (struct sctphdr *)((vaddr_t)ip + (ip->ip_hl << 2));
    436  1.1  rjs 		memset(&to, 0, sizeof(to));
    437  1.1  rjs 		memset(&from, 0, sizeof(from));
    438  1.1  rjs 		from.sin_family = to.sin_family = AF_INET;
    439  1.1  rjs 		from.sin_len = to.sin_len = sizeof(to);
    440  1.1  rjs 		from.sin_port = sh->src_port;
    441  1.1  rjs 		from.sin_addr = ip->ip_src;
    442  1.1  rjs 		to.sin_port = sh->dest_port;
    443  1.1  rjs 		to.sin_addr = ip->ip_dst;
    444  1.1  rjs 
    445  1.1  rjs 		/*
    446  1.1  rjs 		 * 'to' holds the dest of the packet that failed to be sent.
    447  1.1  rjs 		 * 'from' holds our local endpoint address.
    448  1.1  rjs 		 * Thus we reverse the to and the from in the lookup.
    449  1.1  rjs 		 */
    450  1.1  rjs 		s = splsoftnet();
    451  1.1  rjs 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
    452  1.1  rjs 						    (struct sockaddr *)&to,
    453  1.1  rjs 						    &inp, &net, 1);
    454  1.1  rjs 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
    455  1.1  rjs 			if (cmd != PRC_MSGSIZE) {
    456  1.1  rjs 				int cm;
    457  1.1  rjs 				if (cmd == PRC_HOSTDEAD) {
    458  1.1  rjs 					cm = EHOSTUNREACH;
    459  1.1  rjs 				} else {
    460  1.1  rjs 					cm = inetctlerrmap[cmd];
    461  1.1  rjs 				}
    462  1.1  rjs 				sctp_notify(inp, cm, sh,
    463  1.1  rjs 					    (struct sockaddr *)&to, stcb,
    464  1.1  rjs 					    net);
    465  1.1  rjs 			} else {
    466  1.1  rjs 				/* handle possible ICMP size messages */
    467  1.1  rjs 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
    468  1.1  rjs 			}
    469  1.1  rjs 		} else {
    470  1.1  rjs #if defined(__FreeBSD__) && __FreeBSD_version < 500000
    471  1.1  rjs                         /* XXX must be fixed for 5.x and higher, leave for 4.x */
    472  1.1  rjs 			if (PRC_IS_REDIRECT(cmd) && inp) {
    473  1.1  rjs 				in_rtchange((struct inpcb *)inp,
    474  1.1  rjs 					    inetctlerrmap[cmd]);
    475  1.1  rjs 			}
    476  1.1  rjs #endif
    477  1.1  rjs 			if ((stcb == NULL) && (inp != NULL)) {
    478  1.1  rjs 				/* reduce ref-count */
    479  1.1  rjs 				SCTP_INP_WLOCK(inp);
    480  1.1  rjs 				SCTP_INP_DECR_REF(inp);
    481  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
    482  1.1  rjs 			}
    483  1.1  rjs 
    484  1.1  rjs 		}
    485  1.1  rjs 		splx(s);
    486  1.1  rjs 	}
    487  1.1  rjs 	return (NULL);
    488  1.1  rjs }
    489  1.1  rjs 
    490  1.1  rjs static int
    491  1.1  rjs sctp_abort(struct socket *so)
    492  1.1  rjs {
    493  1.1  rjs 	struct sctp_inpcb *inp;
    494  1.1  rjs 
    495  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    496  1.1  rjs 	if (inp == 0)
    497  1.1  rjs 		return EINVAL;	/* ??? possible? panic instead? */
    498  1.1  rjs 
    499  1.1  rjs 	sctp_inpcb_free(inp, 1);
    500  1.1  rjs 	return 0;
    501  1.1  rjs }
    502  1.1  rjs 
    503  1.1  rjs static int
    504  1.1  rjs sctp_attach(struct socket *so, int proto)
    505  1.1  rjs {
    506  1.1  rjs 	struct sctp_inpcb *inp;
    507  1.1  rjs #ifdef IPSEC
    508  1.1  rjs 	struct inpcb *ip_inp;
    509  1.1  rjs #endif
    510  1.1  rjs 	int error;
    511  1.1  rjs 
    512  1.1  rjs 	sosetlock(so);
    513  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    514  1.1  rjs 	if (inp != 0) {
    515  1.1  rjs 		return EINVAL;
    516  1.1  rjs 	}
    517  1.1  rjs 	error = soreserve(so, sctp_sendspace, sctp_recvspace);
    518  1.1  rjs 	if (error) {
    519  1.1  rjs 		return error;
    520  1.1  rjs 	}
    521  1.1  rjs 	error = sctp_inpcb_alloc(so);
    522  1.1  rjs 	if (error) {
    523  1.1  rjs 		return error;
    524  1.1  rjs 	}
    525  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    526  1.1  rjs 	SCTP_INP_WLOCK(inp);
    527  1.1  rjs 
    528  1.1  rjs 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
    529  1.1  rjs #ifdef IPSEC
    530  1.1  rjs 	ip_inp = &inp->ip_inp.inp;
    531  1.1  rjs #endif
    532  1.1  rjs 	inp->inp_vflag |= INP_IPV4;
    533  1.1  rjs 	inp->inp_ip_ttl = ip_defttl;
    534  1.1  rjs 
    535  1.1  rjs #ifdef IPSEC
    536  1.1  rjs 	error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
    537  1.1  rjs 	if (error != 0) {
    538  1.1  rjs 		sctp_inpcb_free(inp, 1);
    539  1.1  rjs 		return error;
    540  1.1  rjs 	}
    541  1.1  rjs #endif /*IPSEC*/
    542  1.1  rjs 	SCTP_INP_WUNLOCK(inp);
    543  1.1  rjs 	so->so_send = sctp_sosend;
    544  1.1  rjs 	return 0;
    545  1.1  rjs }
    546  1.1  rjs 
    547  1.1  rjs static int
    548  1.1  rjs sctp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    549  1.1  rjs {
    550  1.1  rjs 	struct sctp_inpcb *inp;
    551  1.1  rjs 	int error;
    552  1.1  rjs 
    553  1.1  rjs 	KASSERT(solocked(so));
    554  1.1  rjs 
    555  1.1  rjs #ifdef INET6
    556  1.1  rjs 	if (nam && nam->sa_family != AF_INET)
    557  1.1  rjs 		/* must be a v4 address! */
    558  1.1  rjs 		return EINVAL;
    559  1.1  rjs #endif /* INET6 */
    560  1.1  rjs 
    561  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    562  1.1  rjs 	if (inp == 0)
    563  1.1  rjs 		return EINVAL;
    564  1.1  rjs 
    565  1.1  rjs 	error = sctp_inpcb_bind(so, nam, l);
    566  1.1  rjs 	return error;
    567  1.1  rjs }
    568  1.1  rjs 
    569  1.1  rjs 
    570  1.1  rjs static int
    571  1.1  rjs sctp_detach(struct socket *so)
    572  1.1  rjs {
    573  1.1  rjs 	struct sctp_inpcb *inp;
    574  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    575  1.1  rjs 	if (inp == 0)
    576  1.1  rjs 		return EINVAL;
    577  1.1  rjs 
    578  1.1  rjs 	if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
    579  1.1  rjs 	    (so->so_rcv.sb_cc > 0)) {
    580  1.1  rjs 		sctp_inpcb_free(inp, 1);
    581  1.1  rjs 	} else {
    582  1.1  rjs 		sctp_inpcb_free(inp, 0);
    583  1.1  rjs 	}
    584  1.1  rjs 	return 0;
    585  1.1  rjs }
    586  1.1  rjs 
    587  1.1  rjs static int
    588  1.1  rjs sctp_recvoob(struct socket *so, struct mbuf *m, int flags)
    589  1.1  rjs {
    590  1.1  rjs 	KASSERT(solocked(so));
    591  1.1  rjs 
    592  1.1  rjs 	return EOPNOTSUPP;
    593  1.1  rjs }
    594  1.1  rjs 
    595  1.1  rjs int
    596  1.1  rjs sctp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr,
    597  1.1  rjs 	  struct mbuf *control, struct lwp *l)
    598  1.1  rjs {
    599  1.1  rjs 	struct sctp_inpcb *inp;
    600  1.1  rjs 	int error;
    601  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    602  1.1  rjs 	if (inp == 0) {
    603  1.1  rjs 		if (control) {
    604  1.1  rjs 			sctp_m_freem(control);
    605  1.1  rjs 			control = NULL;
    606  1.1  rjs 		}
    607  1.1  rjs 		sctp_m_freem(m);
    608  1.1  rjs 		return EINVAL;
    609  1.1  rjs 	}
    610  1.1  rjs 	/* Got to have an to address if we are NOT a connected socket */
    611  1.1  rjs 	if ((addr == NULL) &&
    612  1.1  rjs 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
    613  1.1  rjs 	     (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
    614  1.1  rjs 		) {
    615  1.1  rjs 		goto connected_type;
    616  1.1  rjs 	} else if (addr == NULL) {
    617  1.1  rjs 		error = EDESTADDRREQ;
    618  1.1  rjs 		sctp_m_freem(m);
    619  1.1  rjs 		if (control) {
    620  1.1  rjs 			sctp_m_freem(control);
    621  1.1  rjs 			control = NULL;
    622  1.1  rjs 		}
    623  1.1  rjs 		return (error);
    624  1.1  rjs 	}
    625  1.1  rjs #ifdef INET6
    626  1.1  rjs 	if (addr->sa_family != AF_INET) {
    627  1.1  rjs 		/* must be a v4 address! */
    628  1.1  rjs 		sctp_m_freem(m);
    629  1.1  rjs 		if (control) {
    630  1.1  rjs 			sctp_m_freem(control);
    631  1.1  rjs 			control = NULL;
    632  1.1  rjs 		}
    633  1.1  rjs 		error = EDESTADDRREQ;
    634  1.1  rjs 		return EINVAL;
    635  1.1  rjs 	}
    636  1.1  rjs #endif /* INET6 */
    637  1.1  rjs  connected_type:
    638  1.1  rjs 	/* now what about control */
    639  1.1  rjs 	if (control) {
    640  1.1  rjs 		if (inp->control) {
    641  1.1  rjs 			printf("huh? control set?\n");
    642  1.1  rjs 			sctp_m_freem(inp->control);
    643  1.1  rjs 			inp->control = NULL;
    644  1.1  rjs 		}
    645  1.1  rjs 		inp->control = control;
    646  1.1  rjs 	}
    647  1.1  rjs 	/* add it in possibly */
    648  1.1  rjs 	if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
    649  1.1  rjs 		struct mbuf *x;
    650  1.1  rjs 		int c_len;
    651  1.1  rjs 
    652  1.1  rjs 		c_len = 0;
    653  1.1  rjs 		/* How big is it */
    654  1.1  rjs 		for (x=m;x;x = x->m_next) {
    655  1.1  rjs 			c_len += x->m_len;
    656  1.1  rjs 		}
    657  1.1  rjs 		inp->pkt->m_pkthdr.len += c_len;
    658  1.1  rjs 	}
    659  1.1  rjs 	/* Place the data */
    660  1.1  rjs 	if (inp->pkt) {
    661  1.1  rjs 		inp->pkt_last->m_next = m;
    662  1.1  rjs 		inp->pkt_last = m;
    663  1.1  rjs 	} else {
    664  1.1  rjs 		inp->pkt_last = inp->pkt = m;
    665  1.1  rjs 	}
    666  1.1  rjs 	if ((so->so_state & SS_MORETOCOME) == 0) {
    667  1.1  rjs 		/*
    668  1.1  rjs 		 * note with the current version this code will only be used
    669  1.1  rjs 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
    670  1.1  rjs 		 * re-defining sosend to use the sctp_sosend. One can
    671  1.1  rjs 		 * optionally switch back to this code (by changing back the
    672  1.1  rjs 		 * definitions) but this is not advisable.
    673  1.1  rjs 	     */
    674  1.1  rjs 		int ret;
    675  1.1  rjs 		ret = sctp_output(inp, inp->pkt, addr, inp->control, l, 0);
    676  1.1  rjs 		inp->pkt = NULL;
    677  1.1  rjs 		inp->control = NULL;
    678  1.1  rjs 		return (ret);
    679  1.1  rjs 	} else {
    680  1.1  rjs 		return (0);
    681  1.1  rjs 	}
    682  1.1  rjs }
    683  1.1  rjs 
    684  1.1  rjs static int
    685  1.1  rjs sctp_disconnect(struct socket *so)
    686  1.1  rjs {
    687  1.1  rjs 	struct sctp_inpcb *inp;
    688  1.1  rjs 
    689  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    690  1.1  rjs 	if (inp == NULL) {
    691  1.1  rjs 		return (ENOTCONN);
    692  1.1  rjs 	}
    693  1.1  rjs 	SCTP_INP_RLOCK(inp);
    694  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
    695  1.1  rjs 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
    696  1.1  rjs 			/* No connection */
    697  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
    698  1.1  rjs 			return (0);
    699  1.1  rjs 		} else {
    700  1.1  rjs 			int some_on_streamwheel = 0;
    701  1.1  rjs 			struct sctp_association *asoc;
    702  1.1  rjs 			struct sctp_tcb *stcb;
    703  1.1  rjs 
    704  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
    705  1.1  rjs 			if (stcb == NULL) {
    706  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
    707  1.1  rjs 				return (EINVAL);
    708  1.1  rjs 			}
    709  1.1  rjs 			asoc = &stcb->asoc;
    710  1.1  rjs 			SCTP_TCB_LOCK(stcb);
    711  1.1  rjs 			if (((so->so_options & SO_LINGER) &&
    712  1.1  rjs 			     (so->so_linger == 0)) ||
    713  1.1  rjs 			    (so->so_rcv.sb_cc > 0)) {
    714  1.1  rjs 				if (SCTP_GET_STATE(asoc) !=
    715  1.1  rjs 				    SCTP_STATE_COOKIE_WAIT) {
    716  1.1  rjs 					/* Left with Data unread */
    717  1.1  rjs 					struct mbuf *err;
    718  1.1  rjs 					err = NULL;
    719  1.1  rjs 					MGET(err, M_DONTWAIT, MT_DATA);
    720  1.1  rjs 					if (err) {
    721  1.1  rjs 						/* Fill in the user initiated abort */
    722  1.1  rjs 						struct sctp_paramhdr *ph;
    723  1.1  rjs 						ph = mtod(err, struct sctp_paramhdr *);
    724  1.1  rjs 						err->m_len = sizeof(struct sctp_paramhdr);
    725  1.1  rjs 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
    726  1.1  rjs 						ph->param_length = htons(err->m_len);
    727  1.1  rjs 					}
    728  1.1  rjs 					sctp_send_abort_tcb(stcb, err);
    729  1.1  rjs 				}
    730  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
    731  1.1  rjs 				sctp_free_assoc(inp, stcb);
    732  1.1  rjs 				/* No unlock tcb assoc is gone */
    733  1.1  rjs 				return (0);
    734  1.1  rjs 			}
    735  1.1  rjs 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
    736  1.1  rjs 				/* Check to see if some data queued */
    737  1.1  rjs 				struct sctp_stream_out *outs;
    738  1.1  rjs 				TAILQ_FOREACH(outs, &asoc->out_wheel,
    739  1.1  rjs 					      next_spoke) {
    740  1.1  rjs 					if (!TAILQ_EMPTY(&outs->outqueue)) {
    741  1.1  rjs 						some_on_streamwheel = 1;
    742  1.1  rjs 						break;
    743  1.1  rjs 					}
    744  1.1  rjs 				}
    745  1.1  rjs 			}
    746  1.1  rjs 
    747  1.1  rjs 			if (TAILQ_EMPTY(&asoc->send_queue) &&
    748  1.1  rjs 			    TAILQ_EMPTY(&asoc->sent_queue) &&
    749  1.1  rjs 			    (some_on_streamwheel == 0)) {
    750  1.1  rjs 				/* there is nothing queued to send, so done */
    751  1.1  rjs 				if ((SCTP_GET_STATE(asoc) !=
    752  1.1  rjs 				     SCTP_STATE_SHUTDOWN_SENT) &&
    753  1.1  rjs 				    (SCTP_GET_STATE(asoc) !=
    754  1.1  rjs 				     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
    755  1.1  rjs 					/* only send SHUTDOWN 1st time thru */
    756  1.1  rjs #ifdef SCTP_DEBUG
    757  1.1  rjs 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    758  1.1  rjs 						printf("%s:%d sends a shutdown\n",
    759  1.1  rjs 						       __FILE__,
    760  1.1  rjs 						       __LINE__
    761  1.1  rjs 							);
    762  1.1  rjs 					}
    763  1.1  rjs #endif
    764  1.1  rjs 					sctp_send_shutdown(stcb,
    765  1.1  rjs 							   stcb->asoc.primary_destination);
    766  1.1  rjs 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
    767  1.1  rjs 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
    768  1.1  rjs 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
    769  1.1  rjs 							 stcb->sctp_ep, stcb,
    770  1.1  rjs 							 asoc->primary_destination);
    771  1.1  rjs 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
    772  1.1  rjs 							 stcb->sctp_ep, stcb,
    773  1.1  rjs 							 asoc->primary_destination);
    774  1.1  rjs 				}
    775  1.1  rjs 			} else {
    776  1.1  rjs 				/*
    777  1.1  rjs 				 * we still got (or just got) data to send,
    778  1.1  rjs 				 * so set SHUTDOWN_PENDING
    779  1.1  rjs 				 */
    780  1.1  rjs 				/*
    781  1.1  rjs 				 * XXX sockets draft says that MSG_EOF should
    782  1.1  rjs 				 * be sent with no data.
    783  1.1  rjs 				 * currently, we will allow user data to be
    784  1.1  rjs 				 * sent first and move to SHUTDOWN-PENDING
    785  1.1  rjs 				 */
    786  1.1  rjs 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
    787  1.1  rjs 			}
    788  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
    789  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
    790  1.1  rjs 			return (0);
    791  1.1  rjs 		}
    792  1.1  rjs 		/* not reached */
    793  1.1  rjs 	} else {
    794  1.1  rjs 		/* UDP model does not support this */
    795  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
    796  1.1  rjs 		return EOPNOTSUPP;
    797  1.1  rjs 	}
    798  1.1  rjs }
    799  1.1  rjs 
    800  1.1  rjs int
    801  1.1  rjs sctp_shutdown(struct socket *so)
    802  1.1  rjs {
    803  1.1  rjs 	struct sctp_inpcb *inp;
    804  1.1  rjs 
    805  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
    806  1.1  rjs 	if (inp == 0) {
    807  1.1  rjs 		return EINVAL;
    808  1.1  rjs 	}
    809  1.1  rjs 	SCTP_INP_RLOCK(inp);
    810  1.1  rjs 	/* For UDP model this is a invalid call */
    811  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
    812  1.1  rjs 		/* Restore the flags that the soshutdown took away. */
    813  1.1  rjs 		so->so_state &= ~SS_CANTRCVMORE;
    814  1.1  rjs 		/* This proc will wakeup for read and do nothing (I hope) */
    815  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
    816  1.1  rjs 		return (EOPNOTSUPP);
    817  1.1  rjs 	}
    818  1.1  rjs 	/*
    819  1.1  rjs 	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
    820  1.1  rjs 	 * or SHUT_RDWR. This means we put the shutdown flag against it.
    821  1.1  rjs 	 */
    822  1.1  rjs 	{
    823  1.1  rjs 		int some_on_streamwheel = 0;
    824  1.1  rjs 		struct sctp_tcb *stcb;
    825  1.1  rjs 		struct sctp_association *asoc;
    826  1.1  rjs 		socantsendmore(so);
    827  1.1  rjs 
    828  1.1  rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
    829  1.1  rjs 		if (stcb == NULL) {
    830  1.1  rjs 			/*
    831  1.1  rjs 			 * Ok we hit the case that the shutdown call was made
    832  1.1  rjs 			 * after an abort or something. Nothing to do now.
    833  1.1  rjs 			 */
    834  1.1  rjs 			return (0);
    835  1.1  rjs 		}
    836  1.1  rjs 		SCTP_TCB_LOCK(stcb);
    837  1.1  rjs 		asoc = &stcb->asoc;
    838  1.1  rjs 
    839  1.1  rjs 		if (!TAILQ_EMPTY(&asoc->out_wheel)) {
    840  1.1  rjs 			/* Check to see if some data queued */
    841  1.1  rjs 			struct sctp_stream_out *outs;
    842  1.1  rjs 			TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
    843  1.1  rjs 				if (!TAILQ_EMPTY(&outs->outqueue)) {
    844  1.1  rjs 					some_on_streamwheel = 1;
    845  1.1  rjs 					break;
    846  1.1  rjs 				}
    847  1.1  rjs 			}
    848  1.1  rjs 		}
    849  1.1  rjs 		if (TAILQ_EMPTY(&asoc->send_queue) &&
    850  1.1  rjs 		    TAILQ_EMPTY(&asoc->sent_queue) &&
    851  1.1  rjs 		    (some_on_streamwheel == 0)) {
    852  1.1  rjs 			/* there is nothing queued to send, so I'm done... */
    853  1.1  rjs 			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
    854  1.1  rjs 				/* only send SHUTDOWN the first time through */
    855  1.1  rjs #ifdef SCTP_DEBUG
    856  1.1  rjs 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    857  1.1  rjs 					printf("%s:%d sends a shutdown\n",
    858  1.1  rjs 					       __FILE__,
    859  1.1  rjs 					       __LINE__
    860  1.1  rjs 						);
    861  1.1  rjs 				}
    862  1.1  rjs #endif
    863  1.1  rjs 				sctp_send_shutdown(stcb,
    864  1.1  rjs 						   stcb->asoc.primary_destination);
    865  1.1  rjs 				sctp_chunk_output(stcb->sctp_ep, stcb, 1);
    866  1.1  rjs 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
    867  1.1  rjs 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
    868  1.1  rjs 						 stcb->sctp_ep, stcb,
    869  1.1  rjs 						 asoc->primary_destination);
    870  1.1  rjs 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
    871  1.1  rjs 						 stcb->sctp_ep, stcb,
    872  1.1  rjs 						 asoc->primary_destination);
    873  1.1  rjs 			}
    874  1.1  rjs 		} else {
    875  1.1  rjs 			/*
    876  1.1  rjs 			 * we still got (or just got) data to send, so
    877  1.1  rjs 			 * set SHUTDOWN_PENDING
    878  1.1  rjs 			 */
    879  1.1  rjs 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
    880  1.1  rjs 		}
    881  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
    882  1.1  rjs 	}
    883  1.1  rjs 	SCTP_INP_RUNLOCK(inp);
    884  1.1  rjs 	return 0;
    885  1.1  rjs }
    886  1.1  rjs 
    887  1.1  rjs /*
    888  1.1  rjs  * copies a "user" presentable address and removes embedded scope, etc.
    889  1.1  rjs  * returns 0 on success, 1 on error
    890  1.1  rjs  */
    891  1.1  rjs static uint32_t
    892  1.1  rjs sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
    893  1.1  rjs {
    894  1.1  rjs 	struct sockaddr_in6 lsa6;
    895  1.1  rjs 
    896  1.1  rjs 	sctp_recover_scope((struct sockaddr_in6 *)sa, &lsa6);
    897  1.1  rjs 	memcpy(ss, sa, sa->sa_len);
    898  1.1  rjs 	return (0);
    899  1.1  rjs }
    900  1.1  rjs 
    901  1.1  rjs 
    902  1.1  rjs static int
    903  1.1  rjs sctp_fill_up_addresses(struct sctp_inpcb *inp,
    904  1.1  rjs 		       struct sctp_tcb *stcb,
    905  1.1  rjs 		       int limit,
    906  1.1  rjs 		       struct sockaddr_storage *sas)
    907  1.1  rjs {
    908  1.1  rjs 	struct ifnet *ifn;
    909  1.1  rjs 	struct ifaddr *ifa;
    910  1.1  rjs 	int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
    911  1.1  rjs 	int ipv4_addr_legal, ipv6_addr_legal;
    912  1.1  rjs 	actual = 0;
    913  1.1  rjs 	if (limit <= 0)
    914  1.1  rjs 		return (actual);
    915  1.1  rjs 
    916  1.1  rjs 	if (stcb) {
    917  1.1  rjs 		/* Turn on all the appropriate scope */
    918  1.1  rjs 		loopback_scope = stcb->asoc.loopback_scope;
    919  1.1  rjs 		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
    920  1.1  rjs 		local_scope = stcb->asoc.local_scope;
    921  1.1  rjs 		site_scope = stcb->asoc.site_scope;
    922  1.1  rjs 	} else {
    923  1.1  rjs 		/* Turn on ALL scope, since we look at the EP */
    924  1.1  rjs 		loopback_scope = ipv4_local_scope = local_scope =
    925  1.1  rjs 			site_scope = 1;
    926  1.1  rjs 	}
    927  1.1  rjs 	ipv4_addr_legal = ipv6_addr_legal = 0;
    928  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
    929  1.1  rjs 		ipv6_addr_legal = 1;
    930  1.1  rjs 		if (
    931  1.1  rjs #if defined(__OpenBSD__)
    932  1.1  rjs 		(0) /* we always do dual bind */
    933  1.1  rjs #elif defined (__NetBSD__)
    934  1.1  rjs 		(((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
    935  1.1  rjs #else
    936  1.1  rjs 		(((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
    937  1.1  rjs #endif
    938  1.1  rjs 		== 0) {
    939  1.1  rjs 			ipv4_addr_legal = 1;
    940  1.1  rjs 		}
    941  1.1  rjs 	} else {
    942  1.1  rjs 		ipv4_addr_legal = 1;
    943  1.1  rjs 	}
    944  1.1  rjs 
    945  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
    946  1.1  rjs 		TAILQ_FOREACH(ifn, &ifnet_list, if_list) {
    947  1.1  rjs 			if ((loopback_scope == 0) &&
    948  1.1  rjs 			    (ifn->if_type == IFT_LOOP)) {
    949  1.1  rjs 				/* Skip loopback if loopback_scope not set */
    950  1.1  rjs 				continue;
    951  1.1  rjs 			}
    952  1.1  rjs 			TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
    953  1.1  rjs 				if (stcb) {
    954  1.1  rjs 				/*
    955  1.1  rjs 				 * For the BOUND-ALL case, the list
    956  1.1  rjs 				 * associated with a TCB is Always
    957  1.1  rjs 				 * considered a reverse list.. i.e.
    958  1.1  rjs 				 * it lists addresses that are NOT
    959  1.1  rjs 				 * part of the association. If this
    960  1.1  rjs 				 * is one of those we must skip it.
    961  1.1  rjs 				 */
    962  1.1  rjs 					if (sctp_is_addr_restricted(stcb,
    963  1.1  rjs 								    ifa->ifa_addr)) {
    964  1.1  rjs 						continue;
    965  1.1  rjs 					}
    966  1.1  rjs 				}
    967  1.1  rjs 				if ((ifa->ifa_addr->sa_family == AF_INET) &&
    968  1.1  rjs 				    (ipv4_addr_legal)) {
    969  1.1  rjs 					struct sockaddr_in *sin;
    970  1.1  rjs 					sin = (struct sockaddr_in *)ifa->ifa_addr;
    971  1.1  rjs 					if (sin->sin_addr.s_addr == 0) {
    972  1.1  rjs 						/* we skip unspecifed addresses */
    973  1.1  rjs 						continue;
    974  1.1  rjs 					}
    975  1.1  rjs 					if ((ipv4_local_scope == 0) &&
    976  1.1  rjs 					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
    977  1.1  rjs 						continue;
    978  1.1  rjs 					}
    979  1.1  rjs 					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
    980  1.1  rjs 						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
    981  1.1  rjs 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
    982  1.1  rjs 						sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(struct sockaddr_in6));
    983  1.1  rjs 						actual += sizeof(sizeof(struct sockaddr_in6));
    984  1.1  rjs 					} else {
    985  1.1  rjs 						memcpy(sas, sin, sizeof(*sin));
    986  1.1  rjs 						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
    987  1.1  rjs 						sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin));
    988  1.1  rjs 						actual += sizeof(*sin);
    989  1.1  rjs 					}
    990  1.1  rjs 					if (actual >= limit) {
    991  1.1  rjs 						return (actual);
    992  1.1  rjs 					}
    993  1.1  rjs 				} else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
    994  1.1  rjs 					   (ipv6_addr_legal)) {
    995  1.1  rjs 					struct sockaddr_in6 *sin6;
    996  1.1  rjs 					sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    997  1.1  rjs 					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    998  1.1  rjs 						/*
    999  1.1  rjs 						 * we skip unspecified
   1000  1.1  rjs 						 * addresses
   1001  1.1  rjs 						 */
   1002  1.1  rjs 						continue;
   1003  1.1  rjs 					}
   1004  1.1  rjs 					if ((site_scope == 0) &&
   1005  1.1  rjs 					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
   1006  1.1  rjs 						continue;
   1007  1.1  rjs 					}
   1008  1.1  rjs 					memcpy(sas, sin6, sizeof(*sin6));
   1009  1.1  rjs 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
   1010  1.1  rjs 					sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin6));
   1011  1.1  rjs 					actual += sizeof(*sin6);
   1012  1.1  rjs 					if (actual >= limit) {
   1013  1.1  rjs 						return (actual);
   1014  1.1  rjs 					}
   1015  1.1  rjs 				}
   1016  1.1  rjs 			}
   1017  1.1  rjs 		}
   1018  1.1  rjs 	} else {
   1019  1.1  rjs 		struct sctp_laddr *laddr;
   1020  1.1  rjs 		/*
   1021  1.1  rjs 		 * If we have a TCB and we do NOT support ASCONF (it's
   1022  1.1  rjs 		 * turned off or otherwise) then the list is always the
   1023  1.1  rjs 		 * true list of addresses (the else case below).  Otherwise
   1024  1.1  rjs 		 * the list on the association is a list of addresses that
   1025  1.1  rjs 		 * are NOT part of the association.
   1026  1.1  rjs 		 */
   1027  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
   1028  1.1  rjs 			/* The list is a NEGATIVE list */
   1029  1.1  rjs 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   1030  1.1  rjs 				if (stcb) {
   1031  1.1  rjs 					if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
   1032  1.1  rjs 						continue;
   1033  1.1  rjs 					}
   1034  1.1  rjs 				}
   1035  1.1  rjs 				if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
   1036  1.1  rjs 					continue;
   1037  1.1  rjs 
   1038  1.1  rjs 				((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
   1039  1.1  rjs 				sas = (struct sockaddr_storage *)((vaddr_t)sas +
   1040  1.1  rjs 								  laddr->ifa->ifa_addr->sa_len);
   1041  1.1  rjs 				actual += laddr->ifa->ifa_addr->sa_len;
   1042  1.1  rjs 				if (actual >= limit) {
   1043  1.1  rjs 					return (actual);
   1044  1.1  rjs 				}
   1045  1.1  rjs 			}
   1046  1.1  rjs 		} else {
   1047  1.1  rjs 			/* The list is a positive list if present */
   1048  1.1  rjs 			if (stcb) {
   1049  1.1  rjs 				/* Must use the specific association list */
   1050  1.1  rjs 				LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1051  1.1  rjs 					     sctp_nxt_addr) {
   1052  1.1  rjs 					if (sctp_fill_user_address(sas,
   1053  1.1  rjs 								   laddr->ifa->ifa_addr))
   1054  1.1  rjs 						continue;
   1055  1.1  rjs 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
   1056  1.1  rjs 					sas = (struct sockaddr_storage *)((vaddr_t)sas +
   1057  1.1  rjs 									  laddr->ifa->ifa_addr->sa_len);
   1058  1.1  rjs 					actual += laddr->ifa->ifa_addr->sa_len;
   1059  1.1  rjs 					if (actual >= limit) {
   1060  1.1  rjs 						return (actual);
   1061  1.1  rjs 					}
   1062  1.1  rjs 				}
   1063  1.1  rjs 			} else {
   1064  1.1  rjs 				/* No endpoint so use the endpoints individual list */
   1065  1.1  rjs 				LIST_FOREACH(laddr, &inp->sctp_addr_list,
   1066  1.1  rjs 					     sctp_nxt_addr) {
   1067  1.1  rjs 					if (sctp_fill_user_address(sas,
   1068  1.1  rjs 								   laddr->ifa->ifa_addr))
   1069  1.1  rjs 						continue;
   1070  1.1  rjs 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
   1071  1.1  rjs 					sas = (struct sockaddr_storage *)((vaddr_t)sas +
   1072  1.1  rjs 									  laddr->ifa->ifa_addr->sa_len);
   1073  1.1  rjs 					actual += laddr->ifa->ifa_addr->sa_len;
   1074  1.1  rjs 					if (actual >= limit) {
   1075  1.1  rjs 						return (actual);
   1076  1.1  rjs 					}
   1077  1.1  rjs 				}
   1078  1.1  rjs 			}
   1079  1.1  rjs 		}
   1080  1.1  rjs 	}
   1081  1.1  rjs 	return (actual);
   1082  1.1  rjs }
   1083  1.1  rjs 
   1084  1.1  rjs static int
   1085  1.1  rjs sctp_count_max_addresses(struct sctp_inpcb *inp)
   1086  1.1  rjs {
   1087  1.1  rjs 	int cnt = 0;
   1088  1.1  rjs 	/*
   1089  1.1  rjs 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
   1090  1.1  rjs 	 * number of addresses that you COULD get. In reality the sub-set
   1091  1.1  rjs 	 * bound may have an exclusion list for a given TCB OR in the
   1092  1.1  rjs 	 * bound-all case a TCB may NOT include the loopback or other
   1093  1.1  rjs 	 * addresses as well.
   1094  1.1  rjs 	 */
   1095  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1096  1.1  rjs 		struct ifnet *ifn;
   1097  1.1  rjs 		struct ifaddr *ifa;
   1098  1.1  rjs 
   1099  1.1  rjs 		TAILQ_FOREACH(ifn, &ifnet_list, if_list) {
   1100  1.1  rjs 			TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
   1101  1.1  rjs 				/* Count them if they are the right type */
   1102  1.1  rjs 				if (ifa->ifa_addr->sa_family == AF_INET) {
   1103  1.1  rjs 					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
   1104  1.1  rjs 						cnt += sizeof(struct sockaddr_in6);
   1105  1.1  rjs 					else
   1106  1.1  rjs 						cnt += sizeof(struct sockaddr_in);
   1107  1.1  rjs 
   1108  1.1  rjs 				} else if (ifa->ifa_addr->sa_family == AF_INET6)
   1109  1.1  rjs 					cnt += sizeof(struct sockaddr_in6);
   1110  1.1  rjs 			}
   1111  1.1  rjs 		}
   1112  1.1  rjs 	} else {
   1113  1.1  rjs 		struct sctp_laddr *laddr;
   1114  1.1  rjs 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   1115  1.1  rjs 			if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
   1116  1.1  rjs 				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
   1117  1.1  rjs 					cnt += sizeof(struct sockaddr_in6);
   1118  1.1  rjs 				else
   1119  1.1  rjs 					cnt += sizeof(struct sockaddr_in);
   1120  1.1  rjs 
   1121  1.1  rjs 			} else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
   1122  1.1  rjs 				cnt += sizeof(struct sockaddr_in6);
   1123  1.1  rjs 		}
   1124  1.1  rjs 	}
   1125  1.1  rjs 	return (cnt);
   1126  1.1  rjs }
   1127  1.1  rjs 
   1128  1.1  rjs static int
   1129  1.1  rjs sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, struct mbuf *m,
   1130  1.1  rjs 		  struct lwp *l, int delay)
   1131  1.1  rjs {
   1132  1.1  rjs         int error = 0;
   1133  1.1  rjs 	struct sctp_tcb *stcb = NULL;
   1134  1.1  rjs 	struct sockaddr *sa;
   1135  1.1  rjs 	int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
   1136  1.1  rjs #ifdef SCTP_DEBUG
   1137  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   1138  1.1  rjs 		printf("Connectx called\n");
   1139  1.1  rjs 	}
   1140  1.1  rjs #endif /* SCTP_DEBUG */
   1141  1.1  rjs 
   1142  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   1143  1.1  rjs 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
   1144  1.1  rjs 		/* We are already connected AND the TCP model */
   1145  1.1  rjs 		return (EADDRINUSE);
   1146  1.1  rjs 	}
   1147  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1148  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1149  1.1  rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1150  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1151  1.1  rjs 	}
   1152  1.1  rjs 	if (stcb) {
   1153  1.1  rjs 		return (EALREADY);
   1154  1.1  rjs 
   1155  1.1  rjs 	}
   1156  1.1  rjs 	SCTP_ASOC_CREATE_LOCK(inp);
   1157  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   1158  1.1  rjs 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   1159  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   1160  1.1  rjs 		return (EFAULT);
   1161  1.1  rjs 	}
   1162  1.1  rjs 
   1163  1.1  rjs 	totaddrp = mtod(m, int *);
   1164  1.1  rjs 	totaddr = *totaddrp;
   1165  1.1  rjs 	sa = (struct sockaddr *)(totaddrp + 1);
   1166  1.1  rjs 	at = incr = 0;
   1167  1.1  rjs 	/* account and validate addresses */
   1168  1.1  rjs 	SCTP_INP_WLOCK(inp);
   1169  1.1  rjs 	SCTP_INP_INCR_REF(inp);
   1170  1.1  rjs 	SCTP_INP_WUNLOCK(inp);
   1171  1.1  rjs 	for (i = 0; i < totaddr; i++) {
   1172  1.1  rjs 		if (sa->sa_family == AF_INET) {
   1173  1.1  rjs 			num_v4++;
   1174  1.1  rjs 			incr = sizeof(struct sockaddr_in);
   1175  1.1  rjs 		} else if (sa->sa_family == AF_INET6) {
   1176  1.1  rjs 			struct sockaddr_in6 *sin6;
   1177  1.1  rjs 			sin6 = (struct sockaddr_in6 *)sa;
   1178  1.1  rjs 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
   1179  1.1  rjs 				/* Must be non-mapped for connectx */
   1180  1.1  rjs 				SCTP_ASOC_CREATE_UNLOCK(inp);
   1181  1.1  rjs 				return EINVAL;
   1182  1.1  rjs 			}
   1183  1.1  rjs 			num_v6++;
   1184  1.1  rjs 			incr = sizeof(struct sockaddr_in6);
   1185  1.1  rjs 		} else {
   1186  1.1  rjs 			totaddr = i;
   1187  1.1  rjs 			break;
   1188  1.1  rjs 		}
   1189  1.1  rjs 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
   1190  1.1  rjs 		if (stcb != NULL) {
   1191  1.1  rjs 			/* Already have or am bring up an association */
   1192  1.1  rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
   1193  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   1194  1.1  rjs 			return (EALREADY);
   1195  1.1  rjs 		}
   1196  1.1  rjs 		if ((at + incr) > m->m_len) {
   1197  1.1  rjs 			totaddr = i;
   1198  1.1  rjs 			break;
   1199  1.1  rjs 		}
   1200  1.1  rjs 		sa = (struct sockaddr *)((vaddr_t)sa + incr);
   1201  1.1  rjs 	}
   1202  1.1  rjs 	sa = (struct sockaddr *)(totaddrp + 1);
   1203  1.1  rjs 	SCTP_INP_WLOCK(inp);
   1204  1.1  rjs 	SCTP_INP_DECR_REF(inp);
   1205  1.1  rjs 	SCTP_INP_WUNLOCK(inp);
   1206  1.1  rjs #ifdef INET6
   1207  1.1  rjs 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
   1208  1.1  rjs 	    (num_v6 > 0)) {
   1209  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   1210  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   1211  1.1  rjs 		return (EINVAL);
   1212  1.1  rjs 	}
   1213  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
   1214  1.1  rjs 	    (num_v4 > 0)) {
   1215  1.1  rjs 		struct in6pcb *inp6;
   1216  1.1  rjs 		inp6 = (struct in6pcb *)inp;
   1217  1.1  rjs 		if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
   1218  1.1  rjs 			/*
   1219  1.1  rjs 			 * if IPV6_V6ONLY flag, ignore connections
   1220  1.1  rjs 			 * destined to a v4 addr or v4-mapped addr
   1221  1.1  rjs 			 */
   1222  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   1223  1.1  rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
   1224  1.1  rjs 			return EINVAL;
   1225  1.1  rjs 		}
   1226  1.1  rjs 	}
   1227  1.1  rjs #endif /* INET6 */
   1228  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
   1229  1.1  rjs 	    SCTP_PCB_FLAGS_UNBOUND) {
   1230  1.1  rjs 		/* Bind a ephemeral port */
   1231  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   1232  1.1  rjs 		error = sctp_inpcb_bind(so, NULL, l);
   1233  1.1  rjs 		if (error) {
   1234  1.1  rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
   1235  1.1  rjs 			return (error);
   1236  1.1  rjs 		}
   1237  1.1  rjs 	} else {
   1238  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   1239  1.1  rjs 	}
   1240  1.1  rjs         /* We are GOOD to go */
   1241  1.1  rjs 	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
   1242  1.1  rjs 	if (stcb == NULL) {
   1243  1.1  rjs 		/* Gak! no memory */
   1244  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   1245  1.1  rjs 		return (error);
   1246  1.1  rjs 	}
   1247  1.1  rjs 	/* move to second address */
   1248  1.1  rjs 	if (sa->sa_family == AF_INET)
   1249  1.1  rjs 		sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in));
   1250  1.1  rjs 	else
   1251  1.1  rjs 		sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in6));
   1252  1.1  rjs 
   1253  1.1  rjs 	for (i = 1; i < totaddr; i++) {
   1254  1.1  rjs 		if (sa->sa_family == AF_INET) {
   1255  1.1  rjs 			incr = sizeof(struct sockaddr_in);
   1256  1.1  rjs 			if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
   1257  1.1  rjs 				/* assoc gone no un-lock */
   1258  1.1  rjs 				sctp_free_assoc(inp, stcb);
   1259  1.1  rjs 				SCTP_ASOC_CREATE_UNLOCK(inp);
   1260  1.1  rjs 				return (ENOBUFS);
   1261  1.1  rjs 			}
   1262  1.1  rjs 
   1263  1.1  rjs 		} else if (sa->sa_family == AF_INET6) {
   1264  1.1  rjs 			incr = sizeof(struct sockaddr_in6);
   1265  1.1  rjs 			if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
   1266  1.1  rjs 				/* assoc gone no un-lock */
   1267  1.1  rjs 				sctp_free_assoc(inp, stcb);
   1268  1.1  rjs 				SCTP_ASOC_CREATE_UNLOCK(inp);
   1269  1.1  rjs 				return (ENOBUFS);
   1270  1.1  rjs 			}
   1271  1.1  rjs 		}
   1272  1.1  rjs 		sa = (struct sockaddr *)((vaddr_t)sa + incr);
   1273  1.1  rjs 	}
   1274  1.1  rjs 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
   1275  1.1  rjs 	if (delay) {
   1276  1.1  rjs 		/* doing delayed connection */
   1277  1.1  rjs 		stcb->asoc.delayed_connection = 1;
   1278  1.1  rjs 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
   1279  1.1  rjs 	} else {
   1280  1.1  rjs 		SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
   1281  1.1  rjs 		sctp_send_initiate(inp, stcb);
   1282  1.1  rjs 	}
   1283  1.1  rjs 	SCTP_TCB_UNLOCK(stcb);
   1284  1.1  rjs 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
   1285  1.1  rjs 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
   1286  1.1  rjs 		/* Set the connected flag so we can queue data */
   1287  1.1  rjs 		soisconnecting(so);
   1288  1.1  rjs 	}
   1289  1.1  rjs 	SCTP_ASOC_CREATE_UNLOCK(inp);
   1290  1.1  rjs 	return error;
   1291  1.1  rjs }
   1292  1.1  rjs 
   1293  1.1  rjs 
   1294  1.1  rjs static int
   1295  1.1  rjs sctp_optsget(struct socket *so, struct sockopt *sopt)
   1296  1.1  rjs {
   1297  1.1  rjs 	struct sctp_inpcb *inp;
   1298  1.1  rjs 	int error, optval=0;
   1299  1.1  rjs 	int *ovp;
   1300  1.1  rjs 	struct sctp_tcb *stcb = NULL;
   1301  1.1  rjs 
   1302  1.1  rjs         inp = (struct sctp_inpcb *)so->so_pcb;
   1303  1.1  rjs 	if (inp == 0)
   1304  1.1  rjs 		return EINVAL;
   1305  1.1  rjs 	error = 0;
   1306  1.1  rjs 
   1307  1.1  rjs #ifdef SCTP_DEBUG
   1308  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
   1309  1.1  rjs 		printf("optsget opt:%x sz:%zu\n", sopt->sopt_name,
   1310  1.1  rjs 		       sopt->sopt_size);
   1311  1.1  rjs 	}
   1312  1.1  rjs #endif /* SCTP_DEBUG */
   1313  1.1  rjs 
   1314  1.1  rjs 	switch (sopt->sopt_name) {
   1315  1.1  rjs 	case SCTP_NODELAY:
   1316  1.1  rjs 	case SCTP_AUTOCLOSE:
   1317  1.1  rjs 	case SCTP_AUTO_ASCONF:
   1318  1.1  rjs 	case SCTP_DISABLE_FRAGMENTS:
   1319  1.1  rjs 	case SCTP_I_WANT_MAPPED_V4_ADDR:
   1320  1.1  rjs #ifdef SCTP_DEBUG
   1321  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
   1322  1.1  rjs 			printf("other stuff\n");
   1323  1.1  rjs 		}
   1324  1.1  rjs #endif /* SCTP_DEBUG */
   1325  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1326  1.1  rjs 		switch (sopt->sopt_name) {
   1327  1.1  rjs 		case SCTP_DISABLE_FRAGMENTS:
   1328  1.1  rjs 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
   1329  1.1  rjs 			break;
   1330  1.1  rjs 		case SCTP_I_WANT_MAPPED_V4_ADDR:
   1331  1.1  rjs 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
   1332  1.1  rjs 			break;
   1333  1.1  rjs 		case SCTP_AUTO_ASCONF:
   1334  1.1  rjs 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
   1335  1.1  rjs 			break;
   1336  1.1  rjs 		case SCTP_NODELAY:
   1337  1.1  rjs 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
   1338  1.1  rjs 			break;
   1339  1.1  rjs 		case SCTP_AUTOCLOSE:
   1340  1.1  rjs 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
   1341  1.1  rjs 			    SCTP_PCB_FLAGS_AUTOCLOSE)
   1342  1.1  rjs 				optval = inp->sctp_ep.auto_close_time;
   1343  1.1  rjs 			else
   1344  1.1  rjs 				optval = 0;
   1345  1.1  rjs 			break;
   1346  1.1  rjs 
   1347  1.1  rjs 		default:
   1348  1.1  rjs 			error = ENOPROTOOPT;
   1349  1.1  rjs 		} /* end switch (sopt->sopt_name) */
   1350  1.1  rjs 		if (sopt->sopt_name != SCTP_AUTOCLOSE) {
   1351  1.1  rjs 			/* make it an "on/off" value */
   1352  1.1  rjs 			optval = (optval != 0);
   1353  1.1  rjs 		}
   1354  1.1  rjs 		if (sopt->sopt_size < sizeof(int)) {
   1355  1.1  rjs 			error = EINVAL;
   1356  1.1  rjs 		}
   1357  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1358  1.1  rjs 		if (error == 0) {
   1359  1.1  rjs 			/* return the option value */
   1360  1.1  rjs 			ovp = sopt->sopt_data;
   1361  1.1  rjs 			*ovp = optval;
   1362  1.1  rjs 			sopt->sopt_size = sizeof(optval);
   1363  1.1  rjs 		}
   1364  1.1  rjs 		break;
   1365  1.1  rjs 	case SCTP_GET_ASOC_ID_LIST:
   1366  1.1  rjs 	{
   1367  1.1  rjs 		struct sctp_assoc_ids *ids;
   1368  1.1  rjs 		int cnt, at;
   1369  1.1  rjs 		u_int16_t orig;
   1370  1.1  rjs 
   1371  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_assoc_ids)) {
   1372  1.1  rjs 			error = EINVAL;
   1373  1.1  rjs 			break;
   1374  1.1  rjs 		}
   1375  1.1  rjs 		ids = sopt->sopt_data;
   1376  1.1  rjs 		cnt = 0;
   1377  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1378  1.1  rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1379  1.1  rjs 		if (stcb == NULL) {
   1380  1.1  rjs 		none_out_now:
   1381  1.1  rjs 			ids->asls_numb_present = 0;
   1382  1.1  rjs 			ids->asls_more_to_get = 0;
   1383  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   1384  1.1  rjs 			break;
   1385  1.1  rjs 		}
   1386  1.1  rjs 		orig = ids->asls_assoc_start;
   1387  1.1  rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1388  1.1  rjs 		while( orig ) {
   1389  1.1  rjs 			stcb = LIST_NEXT(stcb , sctp_tcblist);
   1390  1.1  rjs 			orig--;
   1391  1.1  rjs 			cnt--;
   1392  1.1  rjs 		}
   1393  1.1  rjs 		if ( stcb == NULL)
   1394  1.1  rjs 			goto none_out_now;
   1395  1.1  rjs 
   1396  1.1  rjs 		at = 0;
   1397  1.1  rjs 		ids->asls_numb_present = 0;
   1398  1.1  rjs 		ids->asls_more_to_get = 1;
   1399  1.1  rjs 		while(at < MAX_ASOC_IDS_RET) {
   1400  1.1  rjs 			ids->asls_assoc_id[at] = sctp_get_associd(stcb);
   1401  1.1  rjs 			at++;
   1402  1.1  rjs 			ids->asls_numb_present++;
   1403  1.1  rjs 			stcb = LIST_NEXT(stcb , sctp_tcblist);
   1404  1.1  rjs 			if (stcb == NULL) {
   1405  1.1  rjs 				ids->asls_more_to_get = 0;
   1406  1.1  rjs 				break;
   1407  1.1  rjs 			}
   1408  1.1  rjs 		}
   1409  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1410  1.1  rjs 	}
   1411  1.1  rjs 	break;
   1412  1.1  rjs 	case SCTP_GET_NONCE_VALUES:
   1413  1.1  rjs 	{
   1414  1.1  rjs 		struct sctp_get_nonce_values *gnv;
   1415  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_get_nonce_values)) {
   1416  1.1  rjs 			error = EINVAL;
   1417  1.1  rjs 			break;
   1418  1.1  rjs 		}
   1419  1.1  rjs 		gnv = sopt->sopt_data;
   1420  1.1  rjs 		stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
   1421  1.1  rjs 		if (stcb == NULL) {
   1422  1.1  rjs 			error = ENOTCONN;
   1423  1.1  rjs 		} else {
   1424  1.1  rjs 			gnv->gn_peers_tag = stcb->asoc.peer_vtag;
   1425  1.1  rjs 			gnv->gn_local_tag = stcb->asoc.my_vtag;
   1426  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   1427  1.1  rjs 		}
   1428  1.1  rjs 
   1429  1.1  rjs 	}
   1430  1.1  rjs 	break;
   1431  1.1  rjs 	case SCTP_PEER_PUBLIC_KEY:
   1432  1.1  rjs 	case SCTP_MY_PUBLIC_KEY:
   1433  1.1  rjs 	case SCTP_SET_AUTH_CHUNKS:
   1434  1.1  rjs 	case SCTP_SET_AUTH_SECRET:
   1435  1.1  rjs 		/* not supported yet and until we refine the draft */
   1436  1.1  rjs 		error = EOPNOTSUPP;
   1437  1.1  rjs 		break;
   1438  1.1  rjs 
   1439  1.1  rjs 	case SCTP_DELAYED_ACK_TIME:
   1440  1.1  rjs 	{
   1441  1.1  rjs 		int32_t *tm;
   1442  1.1  rjs 		if (sopt->sopt_size < sizeof(int32_t)) {
   1443  1.1  rjs 			error = EINVAL;
   1444  1.1  rjs 			break;
   1445  1.1  rjs 		}
   1446  1.1  rjs 		tm = sopt->sopt_data;
   1447  1.1  rjs 
   1448  1.1  rjs 		*tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
   1449  1.1  rjs 	}
   1450  1.1  rjs 	break;
   1451  1.1  rjs 
   1452  1.1  rjs 	case SCTP_GET_SNDBUF_USE:
   1453  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_sockstat)) {
   1454  1.1  rjs 			error = EINVAL;
   1455  1.1  rjs 		} else {
   1456  1.1  rjs 			struct sctp_sockstat *ss;
   1457  1.1  rjs 			struct sctp_association *asoc;
   1458  1.1  rjs 			ss = sopt->sopt_data;
   1459  1.1  rjs    		        stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
   1460  1.1  rjs 			if (stcb == NULL) {
   1461  1.1  rjs 				error = ENOTCONN;
   1462  1.1  rjs 			} else {
   1463  1.1  rjs 				asoc = &stcb->asoc;
   1464  1.1  rjs 				ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
   1465  1.1  rjs 				ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
   1466  1.1  rjs 				ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
   1467  1.1  rjs 								    asoc->size_on_reasm_queue +
   1468  1.1  rjs 								    asoc->size_on_all_streams);
   1469  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
   1470  1.1  rjs 				error = 0;
   1471  1.1  rjs 				sopt->sopt_size = sizeof(struct sctp_sockstat);
   1472  1.1  rjs 			}
   1473  1.1  rjs 		}
   1474  1.1  rjs 		break;
   1475  1.1  rjs 	case SCTP_MAXBURST:
   1476  1.1  rjs 	{
   1477  1.1  rjs 		u_int8_t *burst;
   1478  1.1  rjs 		burst = sopt->sopt_data;
   1479  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1480  1.1  rjs 		*burst = inp->sctp_ep.max_burst;
   1481  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1482  1.1  rjs 		sopt->sopt_size = sizeof(u_int8_t);
   1483  1.1  rjs 	}
   1484  1.1  rjs 	break;
   1485  1.1  rjs 	case SCTP_MAXSEG:
   1486  1.1  rjs 	{
   1487  1.1  rjs 		u_int32_t *segsize;
   1488  1.1  rjs 		sctp_assoc_t *assoc_id;
   1489  1.1  rjs 		int ovh;
   1490  1.1  rjs 
   1491  1.1  rjs 		if (sopt->sopt_size < sizeof(u_int32_t)) {
   1492  1.1  rjs 			error = EINVAL;
   1493  1.1  rjs 			break;
   1494  1.1  rjs 		}
   1495  1.1  rjs 		if (sopt->sopt_size < sizeof(sctp_assoc_t)) {
   1496  1.1  rjs 			error = EINVAL;
   1497  1.1  rjs 			break;
   1498  1.1  rjs 		}
   1499  1.1  rjs 		assoc_id = sopt->sopt_data;
   1500  1.1  rjs 		segsize = sopt->sopt_data;
   1501  1.1  rjs 		sopt->sopt_size = sizeof(u_int32_t);
   1502  1.1  rjs 
   1503  1.1  rjs 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   1504  1.1  rjs 		     (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
   1505  1.1  rjs 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   1506  1.1  rjs 			SCTP_INP_RLOCK(inp);
   1507  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1508  1.1  rjs 			if (stcb) {
   1509  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   1510  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   1511  1.1  rjs 				*segsize = sctp_get_frag_point(stcb, &stcb->asoc);
   1512  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
   1513  1.1  rjs 			} else {
   1514  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   1515  1.1  rjs 				goto skipit;
   1516  1.1  rjs 			}
   1517  1.1  rjs 		} else {
   1518  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
   1519  1.1  rjs 			if (stcb) {
   1520  1.1  rjs 				*segsize = sctp_get_frag_point(stcb, &stcb->asoc);
   1521  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
   1522  1.1  rjs 				break;
   1523  1.1  rjs 			}
   1524  1.1  rjs 		skipit:
   1525  1.1  rjs 			/* default is to get the max, if I
   1526  1.1  rjs 			 * can't calculate from an existing association.
   1527  1.1  rjs 			 */
   1528  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   1529  1.1  rjs 				ovh = SCTP_MED_OVERHEAD;
   1530  1.1  rjs 			} else {
   1531  1.1  rjs 				ovh = SCTP_MED_V4_OVERHEAD;
   1532  1.1  rjs 			}
   1533  1.1  rjs 			*segsize = inp->sctp_frag_point - ovh;
   1534  1.1  rjs 		}
   1535  1.1  rjs 	}
   1536  1.1  rjs 	break;
   1537  1.1  rjs 
   1538  1.1  rjs 	case SCTP_SET_DEBUG_LEVEL:
   1539  1.1  rjs #ifdef SCTP_DEBUG
   1540  1.1  rjs 	{
   1541  1.1  rjs 		u_int32_t *level;
   1542  1.1  rjs 		if (sopt->sopt_size < sizeof(u_int32_t)) {
   1543  1.1  rjs 			error = EINVAL;
   1544  1.1  rjs 			break;
   1545  1.1  rjs 		}
   1546  1.1  rjs 		level = sopt->sopt_data;
   1547  1.1  rjs 		error = 0;
   1548  1.1  rjs 		*level = sctp_debug_on;
   1549  1.1  rjs 		sopt->sopt_size = sizeof(u_int32_t);
   1550  1.1  rjs 		printf("Returning DEBUG LEVEL %x is set\n",
   1551  1.1  rjs 		       (u_int)sctp_debug_on);
   1552  1.1  rjs 	}
   1553  1.1  rjs #else /* SCTP_DEBUG */
   1554  1.1  rjs 	error = EOPNOTSUPP;
   1555  1.1  rjs #endif
   1556  1.1  rjs 	break;
   1557  1.1  rjs 	case SCTP_GET_STAT_LOG:
   1558  1.1  rjs #ifdef SCTP_STAT_LOGGING
   1559  1.1  rjs 		error = sctp_fill_stat_log(m);
   1560  1.1  rjs #else /* SCTP_DEBUG */
   1561  1.1  rjs 		error = EOPNOTSUPP;
   1562  1.1  rjs #endif
   1563  1.1  rjs 		break;
   1564  1.1  rjs 	case SCTP_GET_PEGS:
   1565  1.1  rjs 	{
   1566  1.1  rjs 		u_int32_t *pt;
   1567  1.1  rjs 		if (sopt->sopt_size < sizeof(sctp_pegs)) {
   1568  1.1  rjs 			error = EINVAL;
   1569  1.1  rjs 			break;
   1570  1.1  rjs 		}
   1571  1.1  rjs 		pt = sopt->sopt_data;
   1572  1.1  rjs 		memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
   1573  1.1  rjs 		sopt->sopt_size = sizeof(sctp_pegs);
   1574  1.1  rjs 	}
   1575  1.1  rjs 	break;
   1576  1.1  rjs 	case SCTP_EVENTS:
   1577  1.1  rjs 	{
   1578  1.1  rjs 		struct sctp_event_subscribe *events;
   1579  1.1  rjs #ifdef SCTP_DEBUG
   1580  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
   1581  1.1  rjs 			printf("get events\n");
   1582  1.1  rjs 		}
   1583  1.1  rjs #endif /* SCTP_DEBUG */
   1584  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) {
   1585  1.1  rjs #ifdef SCTP_DEBUG
   1586  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
   1587  1.1  rjs 				printf("sopt->sopt_size is %d not %d\n",
   1588  1.1  rjs 				       (int)sopt->sopt_size,
   1589  1.1  rjs 				       (int)sizeof(struct sctp_event_subscribe));
   1590  1.1  rjs 			}
   1591  1.1  rjs #endif /* SCTP_DEBUG */
   1592  1.1  rjs 			error = EINVAL;
   1593  1.1  rjs 			break;
   1594  1.1  rjs 		}
   1595  1.1  rjs 		events = sopt->sopt_data;
   1596  1.1  rjs 		memset(events, 0, sopt->sopt_size);
   1597  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1598  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
   1599  1.1  rjs 			events->sctp_data_io_event = 1;
   1600  1.1  rjs 
   1601  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
   1602  1.1  rjs 			events->sctp_association_event = 1;
   1603  1.1  rjs 
   1604  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
   1605  1.1  rjs 			events->sctp_address_event = 1;
   1606  1.1  rjs 
   1607  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
   1608  1.1  rjs 			events->sctp_send_failure_event = 1;
   1609  1.1  rjs 
   1610  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
   1611  1.1  rjs 			events->sctp_peer_error_event = 1;
   1612  1.1  rjs 
   1613  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
   1614  1.1  rjs 			events->sctp_shutdown_event = 1;
   1615  1.1  rjs 
   1616  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
   1617  1.1  rjs 			events->sctp_partial_delivery_event = 1;
   1618  1.1  rjs 
   1619  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
   1620  1.1  rjs 			events->sctp_adaption_layer_event = 1;
   1621  1.1  rjs 
   1622  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
   1623  1.1  rjs 			events->sctp_stream_reset_events = 1;
   1624  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1625  1.1  rjs 		sopt->sopt_size = sizeof(struct sctp_event_subscribe);
   1626  1.1  rjs 
   1627  1.1  rjs 	}
   1628  1.1  rjs 	break;
   1629  1.1  rjs 
   1630  1.1  rjs 	case SCTP_ADAPTION_LAYER:
   1631  1.1  rjs 		if (sopt->sopt_size < sizeof(int)) {
   1632  1.1  rjs 			error = EINVAL;
   1633  1.1  rjs 			break;
   1634  1.1  rjs 		}
   1635  1.1  rjs #ifdef SCTP_DEBUG
   1636  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1637  1.1  rjs 			printf("getadaption ind\n");
   1638  1.1  rjs 		}
   1639  1.1  rjs #endif /* SCTP_DEBUG */
   1640  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1641  1.1  rjs 		ovp = sopt->sopt_data;
   1642  1.1  rjs 		*ovp = inp->sctp_ep.adaption_layer_indicator;
   1643  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1644  1.1  rjs 		sopt->sopt_size = sizeof(int);
   1645  1.1  rjs 		break;
   1646  1.1  rjs 	case SCTP_SET_INITIAL_DBG_SEQ:
   1647  1.1  rjs 		if (sopt->sopt_size < sizeof(int)) {
   1648  1.1  rjs 			error = EINVAL;
   1649  1.1  rjs 			break;
   1650  1.1  rjs 		}
   1651  1.1  rjs #ifdef SCTP_DEBUG
   1652  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1653  1.1  rjs 			printf("get initial dbg seq\n");
   1654  1.1  rjs 		}
   1655  1.1  rjs #endif /* SCTP_DEBUG */
   1656  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1657  1.1  rjs 		ovp = sopt->sopt_data;
   1658  1.1  rjs 		*ovp = inp->sctp_ep.initial_sequence_debug;
   1659  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1660  1.1  rjs 		sopt->sopt_size = sizeof(int);
   1661  1.1  rjs 		break;
   1662  1.1  rjs 	case SCTP_GET_LOCAL_ADDR_SIZE:
   1663  1.1  rjs 		if (sopt->sopt_size < sizeof(int)) {
   1664  1.1  rjs 			error = EINVAL;
   1665  1.1  rjs 			break;
   1666  1.1  rjs 		}
   1667  1.1  rjs #ifdef SCTP_DEBUG
   1668  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1669  1.1  rjs 			printf("get local sizes\n");
   1670  1.1  rjs 		}
   1671  1.1  rjs #endif /* SCTP_DEBUG */
   1672  1.1  rjs 		SCTP_INP_RLOCK(inp);
   1673  1.1  rjs 		ovp = sopt->sopt_data;
   1674  1.1  rjs 		*ovp = sctp_count_max_addresses(inp);
   1675  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   1676  1.1  rjs 		sopt->sopt_size = sizeof(int);
   1677  1.1  rjs 		break;
   1678  1.1  rjs 	case SCTP_GET_REMOTE_ADDR_SIZE:
   1679  1.1  rjs 	{
   1680  1.1  rjs 		sctp_assoc_t *assoc_id;
   1681  1.1  rjs 		u_int32_t *val, sz;
   1682  1.1  rjs 		struct sctp_nets *net;
   1683  1.1  rjs #ifdef SCTP_DEBUG
   1684  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1685  1.1  rjs 			printf("get remote size\n");
   1686  1.1  rjs 		}
   1687  1.1  rjs #endif /* SCTP_DEBUG */
   1688  1.1  rjs 		if (sopt->sopt_size < sizeof(sctp_assoc_t)) {
   1689  1.1  rjs #ifdef SCTP_DEBUG
   1690  1.1  rjs 			printf("sopt->sopt_size:%zu not %zu\n",
   1691  1.1  rjs 			       sopt->sopt_size, sizeof(sctp_assoc_t));
   1692  1.1  rjs #endif /* SCTP_DEBUG */
   1693  1.1  rjs 			error = EINVAL;
   1694  1.1  rjs 			break;
   1695  1.1  rjs 		}
   1696  1.1  rjs 		stcb = NULL;
   1697  1.1  rjs 		val = sopt->sopt_data;
   1698  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1699  1.1  rjs 			SCTP_INP_RLOCK(inp);
   1700  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1701  1.1  rjs 			if (stcb) {
   1702  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   1703  1.1  rjs 			}
   1704  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   1705  1.1  rjs 		}
   1706  1.1  rjs 		if (stcb == NULL) {
   1707  1.1  rjs 			assoc_id = sopt->sopt_data;
   1708  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
   1709  1.1  rjs 		}
   1710  1.1  rjs 
   1711  1.1  rjs 		if (stcb == NULL) {
   1712  1.1  rjs 			error = EINVAL;
   1713  1.1  rjs 			break;
   1714  1.1  rjs 		}
   1715  1.1  rjs 		*val = 0;
   1716  1.1  rjs 		sz = 0;
   1717  1.1  rjs 		/* Count the sizes */
   1718  1.1  rjs 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1719  1.1  rjs 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
   1720  1.1  rjs 			    (rtcache_getdst(&net->ro)->sa_family == AF_INET6)) {
   1721  1.1  rjs 				sz += sizeof(struct sockaddr_in6);
   1722  1.1  rjs 			} else if (rtcache_getdst(&net->ro)->sa_family == AF_INET) {
   1723  1.1  rjs 				sz += sizeof(struct sockaddr_in);
   1724  1.1  rjs 			} else {
   1725  1.1  rjs 				/* huh */
   1726  1.1  rjs 				break;
   1727  1.1  rjs 			}
   1728  1.1  rjs 		}
   1729  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   1730  1.1  rjs 		*val = sz;
   1731  1.1  rjs 		sopt->sopt_size = sizeof(u_int32_t);
   1732  1.1  rjs 	}
   1733  1.1  rjs 	break;
   1734  1.1  rjs 	case SCTP_GET_PEER_ADDRESSES:
   1735  1.1  rjs 		/*
   1736  1.1  rjs 		 * Get the address information, an array
   1737  1.1  rjs 		 * is passed in to fill up we pack it.
   1738  1.1  rjs 		 */
   1739  1.1  rjs 	{
   1740  1.1  rjs 		int cpsz, left;
   1741  1.1  rjs 		struct sockaddr_storage *sas;
   1742  1.1  rjs 		struct sctp_nets *net;
   1743  1.1  rjs 		struct sctp_getaddresses *saddr;
   1744  1.1  rjs #ifdef SCTP_DEBUG
   1745  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1746  1.1  rjs 			printf("get peer addresses\n");
   1747  1.1  rjs 		}
   1748  1.1  rjs #endif /* SCTP_DEBUG */
   1749  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
   1750  1.1  rjs 			error = EINVAL;
   1751  1.1  rjs 			break;
   1752  1.1  rjs 		}
   1753  1.1  rjs 		left = sopt->sopt_size - sizeof(struct sctp_getaddresses);
   1754  1.1  rjs 		saddr = sopt->sopt_data;
   1755  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1756  1.1  rjs 			SCTP_INP_RLOCK(inp);
   1757  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1758  1.1  rjs 			if (stcb) {
   1759  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   1760  1.1  rjs 			}
   1761  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   1762  1.1  rjs 		} else
   1763  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp,
   1764  1.1  rjs 							      saddr->sget_assoc_id);
   1765  1.1  rjs 		if (stcb == NULL) {
   1766  1.1  rjs 			error = ENOENT;
   1767  1.1  rjs 			break;
   1768  1.1  rjs 		}
   1769  1.1  rjs 		sopt->sopt_size = sizeof(struct sctp_getaddresses);
   1770  1.1  rjs 		sas = (struct sockaddr_storage *)&saddr->addr[0];
   1771  1.1  rjs 
   1772  1.1  rjs 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1773  1.1  rjs 			sa_family_t family;
   1774  1.1  rjs 
   1775  1.1  rjs 			family = rtcache_getdst(&net->ro)->sa_family;
   1776  1.1  rjs 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
   1777  1.1  rjs 			    (family == AF_INET6)) {
   1778  1.1  rjs 				cpsz = sizeof(struct sockaddr_in6);
   1779  1.1  rjs 			} else if (family == AF_INET) {
   1780  1.1  rjs 				cpsz = sizeof(struct sockaddr_in);
   1781  1.1  rjs 			} else {
   1782  1.1  rjs 				/* huh */
   1783  1.1  rjs 				break;
   1784  1.1  rjs 			}
   1785  1.1  rjs 			if (left < cpsz) {
   1786  1.1  rjs 				/* not enough room. */
   1787  1.1  rjs #ifdef SCTP_DEBUG
   1788  1.1  rjs 				if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1789  1.1  rjs 					printf("Out of room\n");
   1790  1.1  rjs 				}
   1791  1.1  rjs #endif /* SCTP_DEBUG */
   1792  1.1  rjs 				break;
   1793  1.1  rjs 			}
   1794  1.1  rjs 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
   1795  1.1  rjs 			    (family == AF_INET)) {
   1796  1.1  rjs 				/* Must map the address */
   1797  1.1  rjs 				in6_sin_2_v4mapsin6((const struct sockaddr_in *) rtcache_getdst(&net->ro),
   1798  1.1  rjs 						    (struct sockaddr_in6 *)sas);
   1799  1.1  rjs 			} else {
   1800  1.1  rjs 				memcpy(sas, rtcache_getdst(&net->ro), cpsz);
   1801  1.1  rjs 			}
   1802  1.1  rjs 			((struct sockaddr_in *)sas)->sin_port = stcb->rport;
   1803  1.1  rjs 
   1804  1.1  rjs 			sas = (struct sockaddr_storage *)((vaddr_t)sas + cpsz);
   1805  1.1  rjs 			left -= cpsz;
   1806  1.1  rjs 			sopt->sopt_size += cpsz;
   1807  1.1  rjs #ifdef SCTP_DEBUG
   1808  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
   1809  1.1  rjs 				printf("left now:%d mlen:%zu\n",
   1810  1.1  rjs 				       left, sopt->sopt_size);
   1811  1.1  rjs 			}
   1812  1.1  rjs #endif /* SCTP_DEBUG */
   1813  1.1  rjs 		}
   1814  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   1815  1.1  rjs 	}
   1816  1.1  rjs #ifdef SCTP_DEBUG
   1817  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1818  1.1  rjs 		printf("All done\n");
   1819  1.1  rjs 	}
   1820  1.1  rjs #endif /* SCTP_DEBUG */
   1821  1.1  rjs 	break;
   1822  1.1  rjs 	case SCTP_GET_LOCAL_ADDRESSES:
   1823  1.1  rjs 	{
   1824  1.1  rjs 		int limit, actual;
   1825  1.1  rjs 		struct sockaddr_storage *sas;
   1826  1.1  rjs 		struct sctp_getaddresses *saddr;
   1827  1.1  rjs #ifdef SCTP_DEBUG
   1828  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1829  1.1  rjs 			printf("get local addresses\n");
   1830  1.1  rjs 		}
   1831  1.1  rjs #endif /* SCTP_DEBUG */
   1832  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
   1833  1.1  rjs 			error = EINVAL;
   1834  1.1  rjs 			break;
   1835  1.1  rjs 		}
   1836  1.1  rjs 		saddr = sopt->sopt_data;
   1837  1.1  rjs 
   1838  1.1  rjs 		if (saddr->sget_assoc_id) {
   1839  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1840  1.1  rjs 				SCTP_INP_RLOCK(inp);
   1841  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1842  1.1  rjs 				if (stcb) {
   1843  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   1844  1.1  rjs 				}
   1845  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   1846  1.1  rjs 			} else
   1847  1.1  rjs 				stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
   1848  1.1  rjs 
   1849  1.1  rjs 		} else {
   1850  1.1  rjs 			stcb = NULL;
   1851  1.1  rjs 		}
   1852  1.1  rjs 		/*
   1853  1.1  rjs 		 * assure that the TCP model does not need a assoc id
   1854  1.1  rjs 		 * once connected.
   1855  1.1  rjs 		 */
   1856  1.1  rjs 		if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
   1857  1.1  rjs 		     (stcb == NULL) ) {
   1858  1.1  rjs 			SCTP_INP_RLOCK(inp);
   1859  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1860  1.1  rjs 			if (stcb) {
   1861  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   1862  1.1  rjs 			}
   1863  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   1864  1.1  rjs 		}
   1865  1.1  rjs 		sas = (struct sockaddr_storage *)&saddr->addr[0];
   1866  1.1  rjs 		limit = sopt->sopt_size - sizeof(sctp_assoc_t);
   1867  1.1  rjs 		actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
   1868  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   1869  1.1  rjs 		sopt->sopt_size = sizeof(struct sockaddr_storage) + actual;
   1870  1.1  rjs 	}
   1871  1.1  rjs 	break;
   1872  1.1  rjs 	case SCTP_PEER_ADDR_PARAMS:
   1873  1.1  rjs 	{
   1874  1.1  rjs 		struct sctp_paddrparams *paddrp;
   1875  1.1  rjs 		struct sctp_nets *net;
   1876  1.1  rjs 
   1877  1.1  rjs #ifdef SCTP_DEBUG
   1878  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1879  1.1  rjs 			printf("Getting peer_addr_params\n");
   1880  1.1  rjs 		}
   1881  1.1  rjs #endif /* SCTP_DEBUG */
   1882  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) {
   1883  1.1  rjs #ifdef SCTP_DEBUG
   1884  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
   1885  1.1  rjs 				printf("Hmm m->m_len:%zu is to small\n",
   1886  1.1  rjs 				       sopt->sopt_size);
   1887  1.1  rjs 			}
   1888  1.1  rjs #endif /* SCTP_DEBUG */
   1889  1.1  rjs 			error = EINVAL;
   1890  1.1  rjs 			break;
   1891  1.1  rjs 		}
   1892  1.1  rjs 		paddrp = sopt->sopt_data;
   1893  1.1  rjs 
   1894  1.1  rjs 		net = NULL;
   1895  1.1  rjs 		if (paddrp->spp_assoc_id) {
   1896  1.1  rjs #ifdef SCTP_DEBUG
   1897  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1898  1.1  rjs 				printf("In spp_assoc_id find type\n");
   1899  1.1  rjs 			}
   1900  1.1  rjs #endif /* SCTP_DEBUG */
   1901  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1902  1.1  rjs 				SCTP_INP_RLOCK(inp);
   1903  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1904  1.1  rjs 				if (stcb) {
   1905  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   1906  1.1  rjs 					net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
   1907  1.1  rjs 				}
   1908  1.1  rjs 				SCTP_INP_RLOCK(inp);
   1909  1.1  rjs 			} else {
   1910  1.1  rjs 				stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
   1911  1.1  rjs 			}
   1912  1.1  rjs 			if (stcb == NULL) {
   1913  1.1  rjs 				error = ENOENT;
   1914  1.1  rjs 				break;
   1915  1.1  rjs 			}
   1916  1.1  rjs 		}
   1917  1.1  rjs 		if (	(stcb == NULL) &&
   1918  1.1  rjs 			((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
   1919  1.1  rjs 			 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
   1920  1.1  rjs 			/* Lookup via address */
   1921  1.1  rjs #ifdef SCTP_DEBUG
   1922  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1923  1.1  rjs 				printf("Ok we need to lookup a param\n");
   1924  1.1  rjs 			}
   1925  1.1  rjs #endif /* SCTP_DEBUG */
   1926  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1927  1.1  rjs 				SCTP_INP_RLOCK(inp);
   1928  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1929  1.1  rjs 				if (stcb) {
   1930  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   1931  1.1  rjs 					net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
   1932  1.1  rjs 				}
   1933  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   1934  1.1  rjs 			} else {
   1935  1.1  rjs 				SCTP_INP_WLOCK(inp);
   1936  1.1  rjs 				SCTP_INP_INCR_REF(inp);
   1937  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   1938  1.1  rjs 				stcb = sctp_findassociation_ep_addr(&inp,
   1939  1.1  rjs 								    (struct sockaddr *)&paddrp->spp_address,
   1940  1.1  rjs 								    &net, NULL, NULL);
   1941  1.1  rjs 				if (stcb == NULL) {
   1942  1.1  rjs 					SCTP_INP_WLOCK(inp);
   1943  1.1  rjs 					SCTP_INP_DECR_REF(inp);
   1944  1.1  rjs 					SCTP_INP_WUNLOCK(inp);
   1945  1.1  rjs 				}
   1946  1.1  rjs 			}
   1947  1.1  rjs 
   1948  1.1  rjs 			if (stcb == NULL) {
   1949  1.1  rjs 				error = ENOENT;
   1950  1.1  rjs 				break;
   1951  1.1  rjs 			}
   1952  1.1  rjs 		} else {
   1953  1.1  rjs 			/* Effects the Endpoint */
   1954  1.1  rjs #ifdef SCTP_DEBUG
   1955  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1956  1.1  rjs 				printf("User wants EP level info\n");
   1957  1.1  rjs 			}
   1958  1.1  rjs #endif /* SCTP_DEBUG */
   1959  1.1  rjs 			stcb = NULL;
   1960  1.1  rjs 		}
   1961  1.1  rjs 		if (stcb) {
   1962  1.1  rjs 			/* Applys to the specific association */
   1963  1.1  rjs #ifdef SCTP_DEBUG
   1964  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1965  1.1  rjs 				printf("In TCB side\n");
   1966  1.1  rjs 			}
   1967  1.1  rjs #endif /* SCTP_DEBUG */
   1968  1.1  rjs 			if (net) {
   1969  1.1  rjs 				paddrp->spp_pathmaxrxt = net->failure_threshold;
   1970  1.1  rjs 			} else {
   1971  1.1  rjs 				/* No destination so return default value */
   1972  1.1  rjs 				paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
   1973  1.1  rjs 			}
   1974  1.1  rjs 			paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
   1975  1.1  rjs 			paddrp->spp_assoc_id = sctp_get_associd(stcb);
   1976  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   1977  1.1  rjs 		} else {
   1978  1.1  rjs 			/* Use endpoint defaults */
   1979  1.1  rjs 			SCTP_INP_RLOCK(inp);
   1980  1.1  rjs #ifdef SCTP_DEBUG
   1981  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1982  1.1  rjs 				printf("In EP levle info\n");
   1983  1.1  rjs 			}
   1984  1.1  rjs #endif /* SCTP_DEBUG */
   1985  1.1  rjs 			paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
   1986  1.1  rjs 			paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
   1987  1.1  rjs 			paddrp->spp_assoc_id = (sctp_assoc_t)0;
   1988  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   1989  1.1  rjs 		}
   1990  1.1  rjs 		sopt->sopt_size = sizeof(struct sctp_paddrparams);
   1991  1.1  rjs 	}
   1992  1.1  rjs 	break;
   1993  1.1  rjs 	case SCTP_GET_PEER_ADDR_INFO:
   1994  1.1  rjs 	{
   1995  1.1  rjs 		struct sctp_paddrinfo *paddri;
   1996  1.1  rjs 		struct sctp_nets *net;
   1997  1.1  rjs #ifdef SCTP_DEBUG
   1998  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   1999  1.1  rjs 			printf("GetPEER ADDR_INFO\n");
   2000  1.1  rjs 		}
   2001  1.1  rjs #endif /* SCTP_DEBUG */
   2002  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_paddrinfo)) {
   2003  1.1  rjs 			error = EINVAL;
   2004  1.1  rjs 			break;
   2005  1.1  rjs 		}
   2006  1.1  rjs 		paddri = sopt->sopt_data;
   2007  1.1  rjs 		net = NULL;
   2008  1.1  rjs 		if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
   2009  1.1  rjs 		    (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
   2010  1.1  rjs 			/* Lookup via address */
   2011  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2012  1.1  rjs 				SCTP_INP_RLOCK(inp);
   2013  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2014  1.1  rjs 				if (stcb) {
   2015  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   2016  1.1  rjs 					net = sctp_findnet(stcb,
   2017  1.1  rjs 							    (struct sockaddr *)&paddri->spinfo_address);
   2018  1.1  rjs 				}
   2019  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   2020  1.1  rjs 			} else {
   2021  1.1  rjs 				SCTP_INP_WLOCK(inp);
   2022  1.1  rjs 				SCTP_INP_INCR_REF(inp);
   2023  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   2024  1.1  rjs 				stcb = sctp_findassociation_ep_addr(&inp,
   2025  1.1  rjs 				    (struct sockaddr *)&paddri->spinfo_address,
   2026  1.1  rjs 				    &net, NULL, NULL);
   2027  1.1  rjs 				if (stcb == NULL) {
   2028  1.1  rjs 					SCTP_INP_WLOCK(inp);
   2029  1.1  rjs 					SCTP_INP_DECR_REF(inp);
   2030  1.1  rjs 					SCTP_INP_WUNLOCK(inp);
   2031  1.1  rjs 				}
   2032  1.1  rjs 			}
   2033  1.1  rjs 
   2034  1.1  rjs 		} else {
   2035  1.1  rjs 			stcb = NULL;
   2036  1.1  rjs 		}
   2037  1.1  rjs 		if ((stcb == NULL) || (net == NULL)) {
   2038  1.1  rjs 			error = ENOENT;
   2039  1.1  rjs 			break;
   2040  1.1  rjs 		}
   2041  1.1  rjs 		sopt->sopt_size = sizeof(struct sctp_paddrinfo);
   2042  1.1  rjs 		paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
   2043  1.1  rjs 		paddri->spinfo_cwnd = net->cwnd;
   2044  1.1  rjs 		paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
   2045  1.1  rjs 		paddri->spinfo_rto = net->RTO;
   2046  1.1  rjs 		paddri->spinfo_assoc_id = sctp_get_associd(stcb);
   2047  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2048  1.1  rjs 	}
   2049  1.1  rjs 	break;
   2050  1.1  rjs 	case SCTP_PCB_STATUS:
   2051  1.1  rjs 	{
   2052  1.1  rjs 		struct sctp_pcbinfo *spcb;
   2053  1.1  rjs #ifdef SCTP_DEBUG
   2054  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2055  1.1  rjs 			printf("PCB status\n");
   2056  1.1  rjs 		}
   2057  1.1  rjs #endif /* SCTP_DEBUG */
   2058  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_pcbinfo)) {
   2059  1.1  rjs 			error = EINVAL;
   2060  1.1  rjs 			break;
   2061  1.1  rjs 		}
   2062  1.1  rjs 		spcb = sopt->sopt_data;
   2063  1.1  rjs 		sctp_fill_pcbinfo(spcb);
   2064  1.1  rjs 		sopt->sopt_size = sizeof(struct sctp_pcbinfo);
   2065  1.1  rjs 	}
   2066  1.1  rjs 	break;
   2067  1.1  rjs 	case SCTP_STATUS:
   2068  1.1  rjs 	{
   2069  1.1  rjs 		struct sctp_nets *net;
   2070  1.1  rjs 		struct sctp_status *sstat;
   2071  1.1  rjs #ifdef SCTP_DEBUG
   2072  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2073  1.1  rjs 			printf("SCTP status\n");
   2074  1.1  rjs 		}
   2075  1.1  rjs #endif /* SCTP_DEBUG */
   2076  1.1  rjs 
   2077  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_status)) {
   2078  1.1  rjs 			error = EINVAL;
   2079  1.1  rjs 			break;
   2080  1.1  rjs 		}
   2081  1.1  rjs 		sstat = sopt->sopt_data;
   2082  1.1  rjs 
   2083  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2084  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2085  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2086  1.1  rjs 			if (stcb) {
   2087  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2088  1.1  rjs 			}
   2089  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2090  1.1  rjs 		} else
   2091  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
   2092  1.1  rjs 
   2093  1.1  rjs 		if (stcb == NULL) {
   2094  1.1  rjs 			error = EINVAL;
   2095  1.1  rjs 			break;
   2096  1.1  rjs 		}
   2097  1.1  rjs 		/*
   2098  1.1  rjs 		 * I think passing the state is fine since
   2099  1.1  rjs 		 * sctp_constants.h will be available to the user
   2100  1.1  rjs 		 * land.
   2101  1.1  rjs 		 */
   2102  1.1  rjs 		sstat->sstat_state = stcb->asoc.state;
   2103  1.1  rjs 		sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
   2104  1.1  rjs 		sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
   2105  1.1  rjs 		/*
   2106  1.1  rjs 		 * We can't include chunks that have been passed
   2107  1.1  rjs 		 * to the socket layer. Only things in queue.
   2108  1.1  rjs 		 */
   2109  1.1  rjs 		sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
   2110  1.1  rjs 					 stcb->asoc.cnt_on_reasm_queue +
   2111  1.1  rjs 					 stcb->asoc.cnt_on_all_streams);
   2112  1.1  rjs 
   2113  1.1  rjs 
   2114  1.1  rjs 		sstat->sstat_instrms = stcb->asoc.streamincnt;
   2115  1.1  rjs 		sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
   2116  1.1  rjs 		sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
   2117  1.1  rjs 		memcpy(&sstat->sstat_primary.spinfo_address,
   2118  1.1  rjs 		       rtcache_getdst(&stcb->asoc.primary_destination->ro),
   2119  1.1  rjs 		       (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len);
   2120  1.1  rjs 		net = stcb->asoc.primary_destination;
   2121  1.1  rjs 		((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
   2122  1.1  rjs 		/*
   2123  1.1  rjs 		 * Again the user can get info from sctp_constants.h
   2124  1.1  rjs 		 * for what the state of the network is.
   2125  1.1  rjs 		 */
   2126  1.1  rjs 		sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
   2127  1.1  rjs 		sstat->sstat_primary.spinfo_cwnd = net->cwnd;
   2128  1.1  rjs 		sstat->sstat_primary.spinfo_srtt = net->lastsa;
   2129  1.1  rjs 		sstat->sstat_primary.spinfo_rto = net->RTO;
   2130  1.1  rjs 		sstat->sstat_primary.spinfo_mtu = net->mtu;
   2131  1.1  rjs 		sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
   2132  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2133  1.1  rjs 		sopt->sopt_size = sizeof(*sstat);
   2134  1.1  rjs 	}
   2135  1.1  rjs 	break;
   2136  1.1  rjs 	case SCTP_RTOINFO:
   2137  1.1  rjs 	{
   2138  1.1  rjs 		struct sctp_rtoinfo *srto;
   2139  1.1  rjs #ifdef SCTP_DEBUG
   2140  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2141  1.1  rjs 			printf("RTO Info\n");
   2142  1.1  rjs 		}
   2143  1.1  rjs #endif /* SCTP_DEBUG */
   2144  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) {
   2145  1.1  rjs 			error = EINVAL;
   2146  1.1  rjs 			break;
   2147  1.1  rjs 		}
   2148  1.1  rjs 		srto = sopt->sopt_data;
   2149  1.1  rjs 		if (srto->srto_assoc_id == 0) {
   2150  1.1  rjs 			/* Endpoint only please */
   2151  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2152  1.1  rjs 			srto->srto_initial = inp->sctp_ep.initial_rto;
   2153  1.1  rjs 			srto->srto_max = inp->sctp_ep.sctp_maxrto;
   2154  1.1  rjs 			srto->srto_min = inp->sctp_ep.sctp_minrto;
   2155  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2156  1.1  rjs 			break;
   2157  1.1  rjs 		}
   2158  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2159  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2160  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2161  1.1  rjs 			if (stcb) {
   2162  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2163  1.1  rjs 			}
   2164  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2165  1.1  rjs 		} else
   2166  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
   2167  1.1  rjs 
   2168  1.1  rjs 		if (stcb == NULL) {
   2169  1.1  rjs 			error = EINVAL;
   2170  1.1  rjs 			break;
   2171  1.1  rjs 		}
   2172  1.1  rjs 		srto->srto_initial = stcb->asoc.initial_rto;
   2173  1.1  rjs 		srto->srto_max = stcb->asoc.maxrto;
   2174  1.1  rjs 		srto->srto_min = stcb->asoc.minrto;
   2175  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2176  1.1  rjs 		sopt->sopt_size = sizeof(*srto);
   2177  1.1  rjs 	}
   2178  1.1  rjs 	break;
   2179  1.1  rjs 	case SCTP_ASSOCINFO:
   2180  1.1  rjs 	{
   2181  1.1  rjs 		struct sctp_assocparams *sasoc;
   2182  1.1  rjs #ifdef SCTP_DEBUG
   2183  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2184  1.1  rjs 			printf("Associnfo\n");
   2185  1.1  rjs 		}
   2186  1.1  rjs #endif /* SCTP_DEBUG */
   2187  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_assocparams)) {
   2188  1.1  rjs 			error = EINVAL;
   2189  1.1  rjs 			break;
   2190  1.1  rjs 		}
   2191  1.1  rjs 		sasoc = sopt->sopt_data;
   2192  1.1  rjs 		stcb = NULL;
   2193  1.1  rjs 
   2194  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2195  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2196  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2197  1.1  rjs 			if (stcb) {
   2198  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2199  1.1  rjs 			}
   2200  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2201  1.1  rjs 		}
   2202  1.1  rjs 		if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
   2203  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp,
   2204  1.1  rjs 							     sasoc->sasoc_assoc_id);
   2205  1.1  rjs 			if (stcb == NULL) {
   2206  1.1  rjs 				error = ENOENT;
   2207  1.1  rjs 				break;
   2208  1.1  rjs 			}
   2209  1.1  rjs 		} else {
   2210  1.1  rjs 			stcb = NULL;
   2211  1.1  rjs 		}
   2212  1.1  rjs 
   2213  1.1  rjs 		if (stcb) {
   2214  1.1  rjs 			sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
   2215  1.1  rjs 			sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
   2216  1.1  rjs 			sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
   2217  1.1  rjs 			sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
   2218  1.1  rjs 			sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
   2219  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2220  1.1  rjs 		} else {
   2221  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2222  1.1  rjs 			sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
   2223  1.1  rjs 			sasoc->sasoc_number_peer_destinations = 0;
   2224  1.1  rjs 			sasoc->sasoc_peer_rwnd = 0;
   2225  1.1  rjs 			sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
   2226  1.1  rjs 			sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
   2227  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2228  1.1  rjs 		}
   2229  1.1  rjs 		sopt->sopt_size = sizeof(*sasoc);
   2230  1.1  rjs 	}
   2231  1.1  rjs 	break;
   2232  1.1  rjs 	case SCTP_DEFAULT_SEND_PARAM:
   2233  1.1  rjs 	{
   2234  1.1  rjs 		struct sctp_sndrcvinfo *s_info;
   2235  1.1  rjs 
   2236  1.1  rjs 		if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) {
   2237  1.1  rjs 			error = EINVAL;
   2238  1.1  rjs 			break;
   2239  1.1  rjs 		}
   2240  1.1  rjs 		s_info = sopt->sopt_data;
   2241  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2242  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2243  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2244  1.1  rjs 			if (stcb) {
   2245  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2246  1.1  rjs 			}
   2247  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2248  1.1  rjs 		} else
   2249  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
   2250  1.1  rjs 
   2251  1.1  rjs 		if (stcb == NULL) {
   2252  1.1  rjs 			error = ENOENT;
   2253  1.1  rjs 			break;
   2254  1.1  rjs 		}
   2255  1.1  rjs 		/* Copy it out */
   2256  1.1  rjs 		*s_info = stcb->asoc.def_send;
   2257  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2258  1.1  rjs 		sopt->sopt_size = sizeof(*s_info);
   2259  1.1  rjs 	}
   2260  1.1  rjs 	case SCTP_INITMSG:
   2261  1.1  rjs 	{
   2262  1.1  rjs 		struct sctp_initmsg *sinit;
   2263  1.1  rjs #ifdef SCTP_DEBUG
   2264  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2265  1.1  rjs 			printf("initmsg\n");
   2266  1.1  rjs 		}
   2267  1.1  rjs #endif /* SCTP_DEBUG */
   2268  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_initmsg)) {
   2269  1.1  rjs 			error = EINVAL;
   2270  1.1  rjs 			break;
   2271  1.1  rjs 		}
   2272  1.1  rjs 		sinit = sopt->sopt_data;
   2273  1.1  rjs 		SCTP_INP_RLOCK(inp);
   2274  1.1  rjs 		sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
   2275  1.1  rjs 		sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
   2276  1.1  rjs 		sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
   2277  1.1  rjs 		sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
   2278  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   2279  1.1  rjs 		sopt->sopt_size = sizeof(*sinit);
   2280  1.1  rjs 	}
   2281  1.1  rjs 	break;
   2282  1.1  rjs 	case SCTP_PRIMARY_ADDR:
   2283  1.1  rjs 		/* we allow a "get" operation on this */
   2284  1.1  rjs 	{
   2285  1.1  rjs 		struct sctp_setprim *ssp;
   2286  1.1  rjs 
   2287  1.1  rjs #ifdef SCTP_DEBUG
   2288  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2289  1.1  rjs 			printf("setprimary\n");
   2290  1.1  rjs 		}
   2291  1.1  rjs #endif /* SCTP_DEBUG */
   2292  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_setprim)) {
   2293  1.1  rjs 			error = EINVAL;
   2294  1.1  rjs 			break;
   2295  1.1  rjs 		}
   2296  1.1  rjs 		ssp = sopt->sopt_data;
   2297  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2298  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2299  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2300  1.1  rjs 			if (stcb) {
   2301  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2302  1.1  rjs 			}
   2303  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2304  1.1  rjs 		} else {
   2305  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
   2306  1.1  rjs 			if (stcb == NULL) {
   2307  1.1  rjs 				/* one last shot, try it by the address in */
   2308  1.1  rjs 				struct sctp_nets *net;
   2309  1.1  rjs 
   2310  1.1  rjs 				SCTP_INP_WLOCK(inp);
   2311  1.1  rjs 				SCTP_INP_INCR_REF(inp);
   2312  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   2313  1.1  rjs 				stcb = sctp_findassociation_ep_addr(&inp,
   2314  1.1  rjs 							    (struct sockaddr *)&ssp->ssp_addr,
   2315  1.1  rjs 							    &net, NULL, NULL);
   2316  1.1  rjs 				if (stcb == NULL) {
   2317  1.1  rjs 					SCTP_INP_WLOCK(inp);
   2318  1.1  rjs 					SCTP_INP_DECR_REF(inp);
   2319  1.1  rjs 					SCTP_INP_WUNLOCK(inp);
   2320  1.1  rjs 				}
   2321  1.1  rjs 			}
   2322  1.1  rjs 			if (stcb == NULL) {
   2323  1.1  rjs 				error = EINVAL;
   2324  1.1  rjs 				break;
   2325  1.1  rjs 			}
   2326  1.1  rjs 		}
   2327  1.1  rjs 		/* simply copy out the sockaddr_storage... */
   2328  1.1  rjs 		memcpy(&ssp->ssp_addr,
   2329  1.1  rjs 		       rtcache_getdst(&stcb->asoc.primary_destination->ro),
   2330  1.1  rjs 		       (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len);
   2331  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2332  1.1  rjs 		sopt->sopt_size = sizeof(*ssp);
   2333  1.1  rjs 	}
   2334  1.1  rjs 	break;
   2335  1.1  rjs 	default:
   2336  1.1  rjs 		error = ENOPROTOOPT;
   2337  1.1  rjs 		sopt->sopt_size = 0;
   2338  1.1  rjs 		break;
   2339  1.1  rjs 	} /* end switch (sopt->sopt_name) */
   2340  1.1  rjs         return (error);
   2341  1.1  rjs }
   2342  1.1  rjs 
   2343  1.1  rjs static int
   2344  1.1  rjs sctp_optsset(struct socket *so, struct sockopt *sopt)
   2345  1.1  rjs {
   2346  1.1  rjs 	int error, *mopt, set_opt;
   2347  1.1  rjs 	struct sctp_tcb *stcb = NULL;
   2348  1.1  rjs         struct sctp_inpcb *inp;
   2349  1.1  rjs 
   2350  1.1  rjs 	if (sopt->sopt_data == NULL) {
   2351  1.1  rjs #ifdef SCTP_DEBUG
   2352  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
   2353  1.1  rjs 			printf("optsset:MP is NULL EINVAL\n");
   2354  1.1  rjs 		}
   2355  1.1  rjs #endif /* SCTP_DEBUG */
   2356  1.1  rjs 		return (EINVAL);
   2357  1.1  rjs 	}
   2358  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   2359  1.1  rjs 	if (inp == 0)
   2360  1.1  rjs 		return EINVAL;
   2361  1.1  rjs 
   2362  1.1  rjs 	error = 0;
   2363  1.1  rjs 	switch (sopt->sopt_name) {
   2364  1.1  rjs 	case SCTP_NODELAY:
   2365  1.1  rjs 	case SCTP_AUTOCLOSE:
   2366  1.1  rjs 	case SCTP_AUTO_ASCONF:
   2367  1.1  rjs 	case SCTP_DISABLE_FRAGMENTS:
   2368  1.1  rjs 	case SCTP_I_WANT_MAPPED_V4_ADDR:
   2369  1.1  rjs 		/* copy in the option value */
   2370  1.1  rjs 		if (sopt->sopt_size < sizeof(int)) {
   2371  1.1  rjs 			error = EINVAL;
   2372  1.1  rjs 			break;
   2373  1.1  rjs 		}
   2374  1.1  rjs 		mopt = sopt->sopt_data;
   2375  1.1  rjs 		set_opt = 0;
   2376  1.1  rjs 		if (error)
   2377  1.1  rjs 			break;
   2378  1.1  rjs 		switch (sopt->sopt_name) {
   2379  1.1  rjs 		case SCTP_DISABLE_FRAGMENTS:
   2380  1.1  rjs 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
   2381  1.1  rjs 			break;
   2382  1.1  rjs 		case SCTP_AUTO_ASCONF:
   2383  1.1  rjs 			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
   2384  1.1  rjs 			break;
   2385  1.1  rjs 
   2386  1.1  rjs 		case SCTP_I_WANT_MAPPED_V4_ADDR:
   2387  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   2388  1.1  rjs 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
   2389  1.1  rjs 			} else {
   2390  1.1  rjs 				return (EINVAL);
   2391  1.1  rjs 			}
   2392  1.1  rjs 			break;
   2393  1.1  rjs 		case SCTP_NODELAY:
   2394  1.1  rjs 			set_opt = SCTP_PCB_FLAGS_NODELAY;
   2395  1.1  rjs 			break;
   2396  1.1  rjs 		case SCTP_AUTOCLOSE:
   2397  1.1  rjs 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
   2398  1.1  rjs 			/*
   2399  1.1  rjs 			 * The value is in ticks.
   2400  1.1  rjs 			 * Note this does not effect old associations, only
   2401  1.1  rjs 			 * new ones.
   2402  1.1  rjs 			 */
   2403  1.1  rjs 			inp->sctp_ep.auto_close_time = (*mopt * hz);
   2404  1.1  rjs 			break;
   2405  1.1  rjs 		}
   2406  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2407  1.1  rjs 		if (*mopt != 0) {
   2408  1.1  rjs 			inp->sctp_flags |= set_opt;
   2409  1.1  rjs 		} else {
   2410  1.1  rjs 			inp->sctp_flags &= ~set_opt;
   2411  1.1  rjs 		}
   2412  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   2413  1.1  rjs 		break;
   2414  1.1  rjs 	case SCTP_MY_PUBLIC_KEY:    /* set my public key */
   2415  1.1  rjs 	case SCTP_SET_AUTH_CHUNKS:  /* set the authenticated chunks required */
   2416  1.1  rjs 	case SCTP_SET_AUTH_SECRET:  /* set the actual secret for the endpoint */
   2417  1.1  rjs 		/* not supported yet and until we refine the draft */
   2418  1.1  rjs 		error = EOPNOTSUPP;
   2419  1.1  rjs 		break;
   2420  1.1  rjs 
   2421  1.1  rjs 	case SCTP_CLR_STAT_LOG:
   2422  1.1  rjs #ifdef SCTP_STAT_LOGGING
   2423  1.1  rjs 		sctp_clr_stat_log();
   2424  1.1  rjs #else
   2425  1.1  rjs 		error = EOPNOTSUPP;
   2426  1.1  rjs #endif
   2427  1.1  rjs 		break;
   2428  1.1  rjs 	case SCTP_DELAYED_ACK_TIME:
   2429  1.1  rjs 	{
   2430  1.1  rjs 		int32_t *tm;
   2431  1.1  rjs 		if (sopt->sopt_size < sizeof(int32_t)) {
   2432  1.1  rjs 			error = EINVAL;
   2433  1.1  rjs 			break;
   2434  1.1  rjs 		}
   2435  1.1  rjs 		tm = sopt->sopt_data;
   2436  1.1  rjs 
   2437  1.1  rjs 		if ((*tm < 10) || (*tm > 500)) {
   2438  1.1  rjs 			/* can't be smaller than 10ms */
   2439  1.1  rjs 			/* MUST NOT be larger than 500ms */
   2440  1.1  rjs 			error = EINVAL;
   2441  1.1  rjs 			break;
   2442  1.1  rjs 		}
   2443  1.1  rjs 		inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
   2444  1.1  rjs 	}
   2445  1.1  rjs 		break;
   2446  1.1  rjs 	case SCTP_RESET_STREAMS:
   2447  1.1  rjs 	{
   2448  1.1  rjs 		struct sctp_stream_reset *strrst;
   2449  1.1  rjs 		uint8_t two_way, not_peer;
   2450  1.1  rjs 
   2451  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_stream_reset)) {
   2452  1.1  rjs 			error = EINVAL;
   2453  1.1  rjs 			break;
   2454  1.1  rjs 		}
   2455  1.1  rjs 		strrst = sopt->sopt_data;
   2456  1.1  rjs 
   2457  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2458  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2459  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2460  1.1  rjs 			if (stcb) {
   2461  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2462  1.1  rjs 			}
   2463  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2464  1.1  rjs 		} else
   2465  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
   2466  1.1  rjs 		if (stcb == NULL) {
   2467  1.1  rjs 			error = ENOENT;
   2468  1.1  rjs 			break;
   2469  1.1  rjs 		}
   2470  1.1  rjs 		if (stcb->asoc.peer_supports_strreset == 0) {
   2471  1.1  rjs 			/* Peer does not support it,
   2472  1.1  rjs 			 * we return protocol not supported since
   2473  1.1  rjs 			 * this is true for this feature and this
   2474  1.1  rjs 			 * peer, not the socket request in general.
   2475  1.1  rjs 			 */
   2476  1.1  rjs 			error = EPROTONOSUPPORT;
   2477  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2478  1.1  rjs 			break;
   2479  1.1  rjs 		}
   2480  1.1  rjs 
   2481  1.1  rjs /* Having re-thought this code I added as I write the I-D there
   2482  1.1  rjs  * is NO need for it. The peer, if we are requesting a stream-reset
   2483  1.1  rjs  * will send a request to us but will itself do what we do, take
   2484  1.1  rjs  * and copy off the "reset information" we send and queue TSN's
   2485  1.1  rjs  * larger than the send-next in our response message. Thus they
   2486  1.1  rjs  * will handle it.
   2487  1.1  rjs  */
   2488  1.1  rjs /*		if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
   2489  1.1  rjs 		/* Must have all sending data ack'd before we
   2490  1.1  rjs 		 * start this procedure. This is a bit restrictive
   2491  1.1  rjs 		 * and we SHOULD work on changing this so ONLY the
   2492  1.1  rjs 		 * streams being RESET get held up. So, a reset-all
   2493  1.1  rjs 		 * would require this.. but a reset specific just
   2494  1.1  rjs 		 * needs to be sure that the ones being reset have
   2495  1.1  rjs 		 * nothing on the send_queue. For now we will
   2496  1.1  rjs 		 * skip this more detailed method and do a course
   2497  1.1  rjs 		 * way.. i.e. nothing pending ... for future FIX ME!
   2498  1.1  rjs 		 */
   2499  1.1  rjs /*			error = EBUSY;*/
   2500  1.1  rjs /*			break;*/
   2501  1.1  rjs /*		}*/
   2502  1.1  rjs 
   2503  1.1  rjs 		if (stcb->asoc.stream_reset_outstanding) {
   2504  1.1  rjs 			error = EALREADY;
   2505  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2506  1.1  rjs 			break;
   2507  1.1  rjs 		}
   2508  1.1  rjs 		if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
   2509  1.1  rjs 			two_way = 0;
   2510  1.1  rjs 			not_peer = 0;
   2511  1.1  rjs 		} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
   2512  1.1  rjs 			two_way = 1;
   2513  1.1  rjs 			not_peer = 1;
   2514  1.1  rjs 		} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
   2515  1.1  rjs 			two_way = 1;
   2516  1.1  rjs 			not_peer = 0;
   2517  1.1  rjs 		} else {
   2518  1.1  rjs 			error = EINVAL;
   2519  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2520  1.1  rjs 			break;
   2521  1.1  rjs 		}
   2522  1.1  rjs 		sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
   2523  1.1  rjs 					strrst->strrst_list, two_way, not_peer);
   2524  1.1  rjs 		sctp_chunk_output(inp, stcb, 12);
   2525  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2526  1.1  rjs 
   2527  1.1  rjs 	}
   2528  1.1  rjs 	break;
   2529  1.1  rjs 	case SCTP_RESET_PEGS:
   2530  1.1  rjs 		memset(sctp_pegs, 0, sizeof(sctp_pegs));
   2531  1.1  rjs 		error = 0;
   2532  1.1  rjs 		break;
   2533  1.1  rjs 	case SCTP_CONNECT_X:
   2534  1.1  rjs 		if (sopt->sopt_size < (sizeof(int) + sizeof(struct sockaddr_in))) {
   2535  1.1  rjs 			error = EINVAL;
   2536  1.1  rjs 			break;
   2537  1.1  rjs 		}
   2538  1.1  rjs 		error = sctp_do_connect_x(so, inp, sopt->sopt_data, curlwp, 0);
   2539  1.1  rjs 		break;
   2540  1.1  rjs 
   2541  1.1  rjs 	case SCTP_CONNECT_X_DELAYED:
   2542  1.1  rjs 		if (sopt->sopt_size < (sizeof(int) + sizeof(struct sockaddr_in))) {
   2543  1.1  rjs 			error = EINVAL;
   2544  1.1  rjs 			break;
   2545  1.1  rjs 		}
   2546  1.1  rjs 		error = sctp_do_connect_x(so, inp, sopt->sopt_data, curlwp, 1);
   2547  1.1  rjs 		break;
   2548  1.1  rjs 
   2549  1.1  rjs 	case SCTP_CONNECT_X_COMPLETE:
   2550  1.1  rjs 	{
   2551  1.1  rjs 		struct sockaddr *sa;
   2552  1.1  rjs 		struct sctp_nets *net;
   2553  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sockaddr_in)) {
   2554  1.1  rjs 			error = EINVAL;
   2555  1.1  rjs 			break;
   2556  1.1  rjs 		}
   2557  1.1  rjs 		sa = sopt->sopt_data;
   2558  1.1  rjs 		/* find tcb */
   2559  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2560  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2561  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2562  1.1  rjs 			if (stcb) {
   2563  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2564  1.1  rjs 				net = sctp_findnet(stcb, sa);
   2565  1.1  rjs 			}
   2566  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2567  1.1  rjs 		} else {
   2568  1.1  rjs 			SCTP_INP_WLOCK(inp);
   2569  1.1  rjs 			SCTP_INP_INCR_REF(inp);
   2570  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   2571  1.1  rjs 			stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
   2572  1.1  rjs 			if (stcb == NULL) {
   2573  1.1  rjs 				SCTP_INP_WLOCK(inp);
   2574  1.1  rjs 				SCTP_INP_DECR_REF(inp);
   2575  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   2576  1.1  rjs 			}
   2577  1.1  rjs 		}
   2578  1.1  rjs 
   2579  1.1  rjs 		if (stcb == NULL) {
   2580  1.1  rjs 			error = ENOENT;
   2581  1.1  rjs 			break;
   2582  1.1  rjs 		}
   2583  1.1  rjs 		if (stcb->asoc.delayed_connection == 1) {
   2584  1.1  rjs 			stcb->asoc.delayed_connection = 0;
   2585  1.1  rjs 			SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
   2586  1.1  rjs 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
   2587  1.1  rjs 			sctp_send_initiate(inp, stcb);
   2588  1.1  rjs 		} else {
   2589  1.1  rjs 			/* already expired or did not use delayed connectx */
   2590  1.1  rjs 			error = EALREADY;
   2591  1.1  rjs 		}
   2592  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2593  1.1  rjs 	}
   2594  1.1  rjs 	break;
   2595  1.1  rjs 	case SCTP_MAXBURST:
   2596  1.1  rjs 	{
   2597  1.1  rjs 		u_int8_t *burst;
   2598  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2599  1.1  rjs 		burst = sopt->sopt_data;
   2600  1.1  rjs 		if (*burst) {
   2601  1.1  rjs 			inp->sctp_ep.max_burst = *burst;
   2602  1.1  rjs 		}
   2603  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   2604  1.1  rjs 	}
   2605  1.1  rjs 	break;
   2606  1.1  rjs 	case SCTP_MAXSEG:
   2607  1.1  rjs 	{
   2608  1.1  rjs 		u_int32_t *segsize;
   2609  1.1  rjs 		int ovh;
   2610  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   2611  1.1  rjs 			ovh = SCTP_MED_OVERHEAD;
   2612  1.1  rjs 		} else {
   2613  1.1  rjs 			ovh = SCTP_MED_V4_OVERHEAD;
   2614  1.1  rjs 		}
   2615  1.1  rjs 		segsize = sopt->sopt_data;
   2616  1.1  rjs 		if (*segsize < 1) {
   2617  1.1  rjs 			error = EINVAL;
   2618  1.1  rjs 			break;
   2619  1.1  rjs 		}
   2620  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2621  1.1  rjs 		inp->sctp_frag_point = (*segsize+ovh);
   2622  1.1  rjs 		if (inp->sctp_frag_point < MHLEN) {
   2623  1.1  rjs 			inp->sctp_frag_point = MHLEN;
   2624  1.1  rjs 		}
   2625  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   2626  1.1  rjs 	}
   2627  1.1  rjs 	break;
   2628  1.1  rjs 	case SCTP_SET_DEBUG_LEVEL:
   2629  1.1  rjs #ifdef SCTP_DEBUG
   2630  1.1  rjs 	{
   2631  1.1  rjs 		u_int32_t *level;
   2632  1.1  rjs 		if (sopt->sopt_size < sizeof(u_int32_t)) {
   2633  1.1  rjs 			error = EINVAL;
   2634  1.1  rjs 			break;
   2635  1.1  rjs 		}
   2636  1.1  rjs 		level = sopt->sopt_data;
   2637  1.1  rjs 		error = 0;
   2638  1.1  rjs 		sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
   2639  1.1  rjs 					   SCTP_DEBUG_NOISY));
   2640  1.1  rjs 		printf("SETTING DEBUG LEVEL to %x\n",
   2641  1.1  rjs 		       (u_int)sctp_debug_on);
   2642  1.1  rjs 
   2643  1.1  rjs 	}
   2644  1.1  rjs #else
   2645  1.1  rjs 	error = EOPNOTSUPP;
   2646  1.1  rjs #endif /* SCTP_DEBUG */
   2647  1.1  rjs 	break;
   2648  1.1  rjs 	case SCTP_EVENTS:
   2649  1.1  rjs 	{
   2650  1.1  rjs 		struct sctp_event_subscribe *events;
   2651  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) {
   2652  1.1  rjs 			error = EINVAL;
   2653  1.1  rjs 			break;
   2654  1.1  rjs 		}
   2655  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2656  1.1  rjs 		events = sopt->sopt_data;
   2657  1.1  rjs 		if (events->sctp_data_io_event) {
   2658  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
   2659  1.1  rjs 		} else {
   2660  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
   2661  1.1  rjs 		}
   2662  1.1  rjs 
   2663  1.1  rjs 		if (events->sctp_association_event) {
   2664  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
   2665  1.1  rjs 		} else {
   2666  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
   2667  1.1  rjs 		}
   2668  1.1  rjs 
   2669  1.1  rjs 		if (events->sctp_address_event) {
   2670  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
   2671  1.1  rjs 		} else {
   2672  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
   2673  1.1  rjs 		}
   2674  1.1  rjs 
   2675  1.1  rjs 		if (events->sctp_send_failure_event) {
   2676  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
   2677  1.1  rjs 		} else {
   2678  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
   2679  1.1  rjs 		}
   2680  1.1  rjs 
   2681  1.1  rjs 		if (events->sctp_peer_error_event) {
   2682  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
   2683  1.1  rjs 		} else {
   2684  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
   2685  1.1  rjs 		}
   2686  1.1  rjs 
   2687  1.1  rjs 		if (events->sctp_shutdown_event) {
   2688  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
   2689  1.1  rjs 		} else {
   2690  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
   2691  1.1  rjs 		}
   2692  1.1  rjs 
   2693  1.1  rjs 		if (events->sctp_partial_delivery_event) {
   2694  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
   2695  1.1  rjs 		} else {
   2696  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
   2697  1.1  rjs 		}
   2698  1.1  rjs 
   2699  1.1  rjs 		if (events->sctp_adaption_layer_event) {
   2700  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
   2701  1.1  rjs 		} else {
   2702  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
   2703  1.1  rjs 		}
   2704  1.1  rjs 
   2705  1.1  rjs 		if (events->sctp_stream_reset_events) {
   2706  1.1  rjs 			inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
   2707  1.1  rjs 		} else {
   2708  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
   2709  1.1  rjs 		}
   2710  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   2711  1.1  rjs 	}
   2712  1.1  rjs 	break;
   2713  1.1  rjs 
   2714  1.1  rjs 	case SCTP_ADAPTION_LAYER:
   2715  1.1  rjs 	{
   2716  1.1  rjs 		struct sctp_setadaption *adap_bits;
   2717  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_setadaption)) {
   2718  1.1  rjs 			error = EINVAL;
   2719  1.1  rjs 			break;
   2720  1.1  rjs 		}
   2721  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2722  1.1  rjs 		adap_bits = sopt->sopt_data;
   2723  1.1  rjs 		inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
   2724  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   2725  1.1  rjs 	}
   2726  1.1  rjs 	break;
   2727  1.1  rjs 	case SCTP_SET_INITIAL_DBG_SEQ:
   2728  1.1  rjs 	{
   2729  1.1  rjs 		u_int32_t *vvv;
   2730  1.1  rjs 		if (sopt->sopt_size < sizeof(u_int32_t)) {
   2731  1.1  rjs 			error = EINVAL;
   2732  1.1  rjs 			break;
   2733  1.1  rjs 		}
   2734  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2735  1.1  rjs 		vvv = sopt->sopt_data;
   2736  1.1  rjs 		inp->sctp_ep.initial_sequence_debug = *vvv;
   2737  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   2738  1.1  rjs 	}
   2739  1.1  rjs 	break;
   2740  1.1  rjs 	case SCTP_DEFAULT_SEND_PARAM:
   2741  1.1  rjs 	{
   2742  1.1  rjs 		struct sctp_sndrcvinfo *s_info;
   2743  1.1  rjs 
   2744  1.1  rjs 		if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) {
   2745  1.1  rjs 			error = EINVAL;
   2746  1.1  rjs 			break;
   2747  1.1  rjs 		}
   2748  1.1  rjs 		s_info = sopt->sopt_data;
   2749  1.1  rjs 
   2750  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2751  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2752  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2753  1.1  rjs 			if (stcb) {
   2754  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2755  1.1  rjs 			}
   2756  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2757  1.1  rjs 		} else
   2758  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
   2759  1.1  rjs 
   2760  1.1  rjs 		if (stcb == NULL) {
   2761  1.1  rjs 			error = ENOENT;
   2762  1.1  rjs 			break;
   2763  1.1  rjs 		}
   2764  1.1  rjs 		/* Validate things */
   2765  1.1  rjs 		if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
   2766  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2767  1.1  rjs 			error = EINVAL;
   2768  1.1  rjs 			break;
   2769  1.1  rjs 		}
   2770  1.1  rjs 		/* Mask off the flags that are allowed */
   2771  1.1  rjs 		s_info->sinfo_flags = (s_info->sinfo_flags &
   2772  1.1  rjs 				       (MSG_UNORDERED | MSG_ADDR_OVER |
   2773  1.1  rjs 					MSG_PR_SCTP_TTL | MSG_PR_SCTP_BUF));
   2774  1.1  rjs 		/* Copy it in */
   2775  1.1  rjs 		stcb->asoc.def_send = *s_info;
   2776  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2777  1.1  rjs 	}
   2778  1.1  rjs 	break;
   2779  1.1  rjs 	case SCTP_PEER_ADDR_PARAMS:
   2780  1.1  rjs 	{
   2781  1.1  rjs 		struct sctp_paddrparams *paddrp;
   2782  1.1  rjs 		struct sctp_nets *net;
   2783  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) {
   2784  1.1  rjs 			error = EINVAL;
   2785  1.1  rjs 			break;
   2786  1.1  rjs 		}
   2787  1.1  rjs 		paddrp = sopt->sopt_data;
   2788  1.1  rjs 		net = NULL;
   2789  1.1  rjs 		if (paddrp->spp_assoc_id) {
   2790  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2791  1.1  rjs 				SCTP_INP_RLOCK(inp);
   2792  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2793  1.1  rjs 				if (stcb) {
   2794  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   2795  1.1  rjs 					net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
   2796  1.1  rjs 				}
   2797  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   2798  1.1  rjs 			} else
   2799  1.1  rjs 				stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
   2800  1.1  rjs 			if (stcb == NULL) {
   2801  1.1  rjs 				error = ENOENT;
   2802  1.1  rjs 				break;
   2803  1.1  rjs 			}
   2804  1.1  rjs 
   2805  1.1  rjs 		}
   2806  1.1  rjs 		if ((stcb == NULL) &&
   2807  1.1  rjs 		    ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
   2808  1.1  rjs 		     (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
   2809  1.1  rjs 			/* Lookup via address */
   2810  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2811  1.1  rjs 				SCTP_INP_RLOCK(inp);
   2812  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2813  1.1  rjs 				if (stcb) {
   2814  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   2815  1.1  rjs 					net = sctp_findnet(stcb,
   2816  1.1  rjs 							   (struct sockaddr *)&paddrp->spp_address);
   2817  1.1  rjs 				}
   2818  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   2819  1.1  rjs 			} else {
   2820  1.1  rjs 				SCTP_INP_WLOCK(inp);
   2821  1.1  rjs 				SCTP_INP_INCR_REF(inp);
   2822  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   2823  1.1  rjs 				stcb = sctp_findassociation_ep_addr(&inp,
   2824  1.1  rjs 								    (struct sockaddr *)&paddrp->spp_address,
   2825  1.1  rjs 								    &net, NULL, NULL);
   2826  1.1  rjs 				if (stcb == NULL) {
   2827  1.1  rjs 					SCTP_INP_WLOCK(inp);
   2828  1.1  rjs 					SCTP_INP_DECR_REF(inp);
   2829  1.1  rjs 					SCTP_INP_WUNLOCK(inp);
   2830  1.1  rjs 				}
   2831  1.1  rjs 			}
   2832  1.1  rjs 		} else {
   2833  1.1  rjs 			/* Effects the Endpoint */
   2834  1.1  rjs 			stcb = NULL;
   2835  1.1  rjs 		}
   2836  1.1  rjs 		if (stcb) {
   2837  1.1  rjs 			/* Applies to the specific association */
   2838  1.1  rjs 			if (paddrp->spp_pathmaxrxt) {
   2839  1.1  rjs 				if (net) {
   2840  1.1  rjs 					if (paddrp->spp_pathmaxrxt)
   2841  1.1  rjs 						net->failure_threshold = paddrp->spp_pathmaxrxt;
   2842  1.1  rjs 				} else {
   2843  1.1  rjs 					if (paddrp->spp_pathmaxrxt)
   2844  1.1  rjs 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
   2845  1.1  rjs 				}
   2846  1.1  rjs 			}
   2847  1.1  rjs 			if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
   2848  1.1  rjs 				/* Just a set */
   2849  1.1  rjs 				int old;
   2850  1.1  rjs 				if (net) {
   2851  1.1  rjs 					net->dest_state &= ~SCTP_ADDR_NOHB;
   2852  1.1  rjs 				} else {
   2853  1.1  rjs 					old = stcb->asoc.heart_beat_delay;
   2854  1.1  rjs 					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
   2855  1.1  rjs 					if (old == 0) {
   2856  1.1  rjs 						/* Turn back on the timer */
   2857  1.1  rjs 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
   2858  1.1  rjs 					}
   2859  1.1  rjs 				}
   2860  1.1  rjs 			} else if (paddrp->spp_hbinterval == 0xffffffff) {
   2861  1.1  rjs 				/* on demand HB */
   2862  1.1  rjs 				sctp_send_hb(stcb, 1, net);
   2863  1.1  rjs 			} else {
   2864  1.1  rjs 				if (net == NULL) {
   2865  1.1  rjs 					/* off on association */
   2866  1.1  rjs 					if (stcb->asoc.heart_beat_delay) {
   2867  1.1  rjs 						int cnt_of_unconf = 0;
   2868  1.1  rjs 						struct sctp_nets *lnet;
   2869  1.1  rjs 						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
   2870  1.1  rjs 							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
   2871  1.1  rjs 								cnt_of_unconf++;
   2872  1.1  rjs 							}
   2873  1.1  rjs 						}
   2874  1.1  rjs 						/* stop the timer ONLY if we have no unconfirmed addresses
   2875  1.1  rjs 						 */
   2876  1.1  rjs 						if (cnt_of_unconf == 0)
   2877  1.1  rjs 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
   2878  1.1  rjs 					}
   2879  1.1  rjs 					stcb->asoc.heart_beat_delay = 0;
   2880  1.1  rjs 				} else {
   2881  1.1  rjs 					net->dest_state |= SCTP_ADDR_NOHB;
   2882  1.1  rjs 				}
   2883  1.1  rjs 			}
   2884  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2885  1.1  rjs 		} else {
   2886  1.1  rjs 			/* Use endpoint defaults */
   2887  1.1  rjs 			SCTP_INP_WLOCK(inp);
   2888  1.1  rjs 			if (paddrp->spp_pathmaxrxt)
   2889  1.1  rjs 				inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
   2890  1.1  rjs 			if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
   2891  1.1  rjs 				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
   2892  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   2893  1.1  rjs 		}
   2894  1.1  rjs 	}
   2895  1.1  rjs 	break;
   2896  1.1  rjs 	case SCTP_RTOINFO:
   2897  1.1  rjs 	{
   2898  1.1  rjs 		struct sctp_rtoinfo *srto;
   2899  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) {
   2900  1.1  rjs 			error = EINVAL;
   2901  1.1  rjs 			break;
   2902  1.1  rjs 		}
   2903  1.1  rjs 		srto = sopt->sopt_data;
   2904  1.1  rjs 		if (srto->srto_assoc_id == 0) {
   2905  1.1  rjs 			SCTP_INP_WLOCK(inp);
   2906  1.1  rjs 			/* If we have a null asoc, its default for the endpoint */
   2907  1.1  rjs 			if (srto->srto_initial > 10)
   2908  1.1  rjs 				inp->sctp_ep.initial_rto = srto->srto_initial;
   2909  1.1  rjs 			if (srto->srto_max > 10)
   2910  1.1  rjs 				inp->sctp_ep.sctp_maxrto = srto->srto_max;
   2911  1.1  rjs 			if (srto->srto_min > 10)
   2912  1.1  rjs 				inp->sctp_ep.sctp_minrto = srto->srto_min;
   2913  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   2914  1.1  rjs 			break;
   2915  1.1  rjs 		}
   2916  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2917  1.1  rjs 			SCTP_INP_RLOCK(inp);
   2918  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2919  1.1  rjs 			if (stcb) {
   2920  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   2921  1.1  rjs 			}
   2922  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   2923  1.1  rjs 		} else
   2924  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
   2925  1.1  rjs 		if (stcb == NULL) {
   2926  1.1  rjs 			error = EINVAL;
   2927  1.1  rjs 			break;
   2928  1.1  rjs 		}
   2929  1.1  rjs 		/* Set in ms we hope :-) */
   2930  1.1  rjs 		if (srto->srto_initial > 10)
   2931  1.1  rjs 			stcb->asoc.initial_rto = srto->srto_initial;
   2932  1.1  rjs 		if (srto->srto_max > 10)
   2933  1.1  rjs 			stcb->asoc.maxrto = srto->srto_max;
   2934  1.1  rjs 		if (srto->srto_min > 10)
   2935  1.1  rjs 			stcb->asoc.minrto = srto->srto_min;
   2936  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   2937  1.1  rjs 	}
   2938  1.1  rjs 	break;
   2939  1.1  rjs 	case SCTP_ASSOCINFO:
   2940  1.1  rjs 	{
   2941  1.1  rjs 		struct sctp_assocparams *sasoc;
   2942  1.1  rjs 
   2943  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_assocparams)) {
   2944  1.1  rjs 			error = EINVAL;
   2945  1.1  rjs 			break;
   2946  1.1  rjs 		}
   2947  1.1  rjs 		sasoc = sopt->sopt_data;
   2948  1.1  rjs 		if (sasoc->sasoc_assoc_id) {
   2949  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   2950  1.1  rjs 				SCTP_INP_RLOCK(inp);
   2951  1.1  rjs 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
   2952  1.1  rjs 				if (stcb) {
   2953  1.1  rjs 					SCTP_TCB_LOCK(stcb);
   2954  1.1  rjs 				}
   2955  1.1  rjs 				SCTP_INP_RUNLOCK(inp);
   2956  1.1  rjs 			} else
   2957  1.1  rjs 				stcb = sctp_findassociation_ep_asocid(inp,
   2958  1.1  rjs 								      sasoc->sasoc_assoc_id);
   2959  1.1  rjs 			if (stcb == NULL) {
   2960  1.1  rjs 				error = ENOENT;
   2961  1.1  rjs 				break;
   2962  1.1  rjs 			}
   2963  1.1  rjs 
   2964  1.1  rjs 		} else {
   2965  1.1  rjs 			stcb = NULL;
   2966  1.1  rjs 		}
   2967  1.1  rjs 		if (stcb) {
   2968  1.1  rjs 			if (sasoc->sasoc_asocmaxrxt)
   2969  1.1  rjs 				stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
   2970  1.1  rjs 			sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
   2971  1.1  rjs 			sasoc->sasoc_peer_rwnd = 0;
   2972  1.1  rjs 			sasoc->sasoc_local_rwnd = 0;
   2973  1.1  rjs 			if (stcb->asoc.cookie_life)
   2974  1.1  rjs 				stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
   2975  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   2976  1.1  rjs 		} else {
   2977  1.1  rjs 			SCTP_INP_WLOCK(inp);
   2978  1.1  rjs                         if (sasoc->sasoc_asocmaxrxt)
   2979  1.1  rjs 				inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
   2980  1.1  rjs 			sasoc->sasoc_number_peer_destinations = 0;
   2981  1.1  rjs 			sasoc->sasoc_peer_rwnd = 0;
   2982  1.1  rjs 			sasoc->sasoc_local_rwnd = 0;
   2983  1.1  rjs 			if (sasoc->sasoc_cookie_life)
   2984  1.1  rjs 				inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
   2985  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   2986  1.1  rjs 		}
   2987  1.1  rjs 	}
   2988  1.1  rjs 	break;
   2989  1.1  rjs 	case SCTP_INITMSG:
   2990  1.1  rjs 	{
   2991  1.1  rjs                 struct sctp_initmsg *sinit;
   2992  1.1  rjs 
   2993  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_initmsg)) {
   2994  1.1  rjs 			error = EINVAL;
   2995  1.1  rjs 			break;
   2996  1.1  rjs 		}
   2997  1.1  rjs 		sinit = sopt->sopt_data;
   2998  1.1  rjs 		SCTP_INP_WLOCK(inp);
   2999  1.1  rjs 		if (sinit->sinit_num_ostreams)
   3000  1.1  rjs 			inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
   3001  1.1  rjs 
   3002  1.1  rjs 		if (sinit->sinit_max_instreams)
   3003  1.1  rjs 			inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
   3004  1.1  rjs 
   3005  1.1  rjs 		if (sinit->sinit_max_attempts)
   3006  1.1  rjs 			inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
   3007  1.1  rjs 
   3008  1.1  rjs 		if (sinit->sinit_max_init_timeo > 10)
   3009  1.1  rjs 			/* We must be at least a 100ms (we set in ticks) */
   3010  1.1  rjs 			inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
   3011  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3012  1.1  rjs 	}
   3013  1.1  rjs 	break;
   3014  1.1  rjs 	case SCTP_PRIMARY_ADDR:
   3015  1.1  rjs 	{
   3016  1.1  rjs 		struct sctp_setprim *spa;
   3017  1.1  rjs 		struct sctp_nets *net, *lnet;
   3018  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_setprim)) {
   3019  1.1  rjs 			error = EINVAL;
   3020  1.1  rjs 			break;
   3021  1.1  rjs 		}
   3022  1.1  rjs 		spa = sopt->sopt_data;
   3023  1.1  rjs 
   3024  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   3025  1.1  rjs  			SCTP_INP_RLOCK(inp);
   3026  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3027  1.1  rjs 			if (stcb) {
   3028  1.1  rjs 				SCTP_TCB_LOCK(stcb);
   3029  1.1  rjs 			} else {
   3030  1.1  rjs 				error = EINVAL;
   3031  1.1  rjs 				break;
   3032  1.1  rjs 			}
   3033  1.1  rjs  			SCTP_INP_RUNLOCK(inp);
   3034  1.1  rjs 		} else
   3035  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
   3036  1.1  rjs 		if (stcb == NULL) {
   3037  1.1  rjs 			/* One last shot */
   3038  1.1  rjs 			SCTP_INP_WLOCK(inp);
   3039  1.1  rjs 			SCTP_INP_INCR_REF(inp);
   3040  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   3041  1.1  rjs 			stcb = sctp_findassociation_ep_addr(&inp,
   3042  1.1  rjs 							    (struct sockaddr *)&spa->ssp_addr,
   3043  1.1  rjs 							    &net, NULL, NULL);
   3044  1.1  rjs 			if (stcb == NULL) {
   3045  1.1  rjs 				SCTP_INP_WLOCK(inp);
   3046  1.1  rjs 				SCTP_INP_DECR_REF(inp);
   3047  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   3048  1.1  rjs 				error = EINVAL;
   3049  1.1  rjs 				break;
   3050  1.1  rjs 			}
   3051  1.1  rjs 		} else {
   3052  1.1  rjs 			/* find the net, associd or connected lookup type */
   3053  1.1  rjs 			net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
   3054  1.1  rjs 			if (net == NULL) {
   3055  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
   3056  1.1  rjs 				error = EINVAL;
   3057  1.1  rjs 				break;
   3058  1.1  rjs 			}
   3059  1.1  rjs                 }
   3060  1.1  rjs                 if ((net != stcb->asoc.primary_destination) &&
   3061  1.1  rjs   		    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
   3062  1.1  rjs 			/* Ok we need to set it */
   3063  1.1  rjs 			lnet = stcb->asoc.primary_destination;
   3064  1.1  rjs                         lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
   3065  1.1  rjs 		        if (sctp_set_primary_addr(stcb,
   3066  1.1  rjs 						  (struct sockaddr *)NULL,
   3067  1.1  rjs 						  net) == 0) {
   3068  1.1  rjs 			        if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
   3069  1.1  rjs    				        net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
   3070  1.1  rjs                                 }
   3071  1.1  rjs                                 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
   3072  1.1  rjs                         }
   3073  1.1  rjs 		}
   3074  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   3075  1.1  rjs         }
   3076  1.1  rjs 	break;
   3077  1.1  rjs 
   3078  1.1  rjs 	case SCTP_SET_PEER_PRIMARY_ADDR:
   3079  1.1  rjs 	{
   3080  1.1  rjs 		struct sctp_setpeerprim *sspp;
   3081  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_setpeerprim)) {
   3082  1.1  rjs 			error = EINVAL;
   3083  1.1  rjs 			break;
   3084  1.1  rjs 		}
   3085  1.1  rjs 		sspp = sopt->sopt_data;
   3086  1.1  rjs 
   3087  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   3088  1.1  rjs 			SCTP_INP_RLOCK(inp);
   3089  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3090  1.1  rjs 			if (stcb) {
   3091  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
   3092  1.1  rjs 			}
   3093  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   3094  1.1  rjs 		} else
   3095  1.1  rjs 			stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
   3096  1.1  rjs 		if (stcb == NULL) {
   3097  1.1  rjs 			error = EINVAL;
   3098  1.1  rjs 			break;
   3099  1.1  rjs 		}
   3100  1.1  rjs 		if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
   3101  1.1  rjs 			error = EINVAL;
   3102  1.1  rjs 		}
   3103  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   3104  1.1  rjs 	}
   3105  1.1  rjs 	break;
   3106  1.1  rjs 	case SCTP_BINDX_ADD_ADDR:
   3107  1.1  rjs 	{
   3108  1.1  rjs 		struct sctp_getaddresses *addrs;
   3109  1.1  rjs 		struct sockaddr *addr_touse;
   3110  1.1  rjs 		struct sockaddr_in sin;
   3111  1.1  rjs 		/* see if we're bound all already! */
   3112  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3113  1.1  rjs 			error = EINVAL;
   3114  1.1  rjs 			break;
   3115  1.1  rjs 		}
   3116  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
   3117  1.1  rjs 			error = EINVAL;
   3118  1.1  rjs 			break;
   3119  1.1  rjs 		}
   3120  1.1  rjs 		addrs = sopt->sopt_data;
   3121  1.1  rjs 		addr_touse = addrs->addr;
   3122  1.1  rjs 		if (addrs->addr->sa_family == AF_INET6) {
   3123  1.1  rjs 			struct sockaddr_in6 *sin6;
   3124  1.1  rjs 			sin6 = (struct sockaddr_in6 *)addr_touse;
   3125  1.1  rjs 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
   3126  1.1  rjs 				in6_sin6_2_sin(&sin, sin6);
   3127  1.1  rjs 				addr_touse = (struct sockaddr *)&sin;
   3128  1.1  rjs 			}
   3129  1.1  rjs 		}
   3130  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
   3131  1.1  rjs 			error = sctp_inpcb_bind(so, addr_touse, curlwp);
   3132  1.1  rjs 			break;
   3133  1.1  rjs 		}
   3134  1.1  rjs 		/* No locks required here since bind and mgmt_ep_sa all
   3135  1.1  rjs 		 * do their own locking. If we do something for the FIX:
   3136  1.1  rjs 		 * below we may need to lock in that case.
   3137  1.1  rjs 		 */
   3138  1.1  rjs 		if (addrs->sget_assoc_id == 0) {
   3139  1.1  rjs 			/* add the address */
   3140  1.1  rjs 			struct sctp_inpcb  *lep;
   3141  1.1  rjs 			((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
   3142  1.1  rjs 			lep = sctp_pcb_findep(addr_touse, 1, 0);
   3143  1.1  rjs 			if (lep != NULL) {
   3144  1.1  rjs 				/* We must decrement the refcount
   3145  1.1  rjs 				 * since we have the ep already and
   3146  1.1  rjs 				 * are binding. No remove going on
   3147  1.1  rjs 				 * here.
   3148  1.1  rjs 				 */
   3149  1.1  rjs 				SCTP_INP_WLOCK(inp);
   3150  1.1  rjs 				SCTP_INP_DECR_REF(inp);
   3151  1.1  rjs 				SCTP_INP_WUNLOCK(inp);
   3152  1.1  rjs 			}
   3153  1.1  rjs 			if (lep == inp) {
   3154  1.1  rjs 				/* already bound to it.. ok */
   3155  1.1  rjs 				break;
   3156  1.1  rjs 			} else if (lep == NULL) {
   3157  1.1  rjs 				((struct sockaddr_in *)addr_touse)->sin_port = 0;
   3158  1.1  rjs 				error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
   3159  1.1  rjs 							     SCTP_ADD_IP_ADDRESS);
   3160  1.1  rjs 			} else {
   3161  1.1  rjs 				error = EADDRNOTAVAIL;
   3162  1.1  rjs 			}
   3163  1.1  rjs 			if (error)
   3164  1.1  rjs 				break;
   3165  1.1  rjs 
   3166  1.1  rjs 		} else {
   3167  1.1  rjs 			/* FIX: decide whether we allow assoc based bindx */
   3168  1.1  rjs 		}
   3169  1.1  rjs 	}
   3170  1.1  rjs 	break;
   3171  1.1  rjs 	case SCTP_BINDX_REM_ADDR:
   3172  1.1  rjs 	{
   3173  1.1  rjs 		struct sctp_getaddresses *addrs;
   3174  1.1  rjs 		struct sockaddr *addr_touse;
   3175  1.1  rjs 		struct sockaddr_in sin;
   3176  1.1  rjs 		/* see if we're bound all already! */
   3177  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3178  1.1  rjs 			error = EINVAL;
   3179  1.1  rjs 			break;
   3180  1.1  rjs 		}
   3181  1.1  rjs 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
   3182  1.1  rjs 			error = EINVAL;
   3183  1.1  rjs 			break;
   3184  1.1  rjs 		}
   3185  1.1  rjs 		addrs = sopt->sopt_data;
   3186  1.1  rjs 		addr_touse = addrs->addr;
   3187  1.1  rjs 		if (addrs->addr->sa_family == AF_INET6) {
   3188  1.1  rjs 			struct sockaddr_in6 *sin6;
   3189  1.1  rjs 			sin6 = (struct sockaddr_in6 *)addr_touse;
   3190  1.1  rjs 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
   3191  1.1  rjs 				in6_sin6_2_sin(&sin, sin6);
   3192  1.1  rjs 				addr_touse = (struct sockaddr *)&sin;
   3193  1.1  rjs 			}
   3194  1.1  rjs 		}
   3195  1.1  rjs                 /* No lock required mgmt_ep_sa does its own locking. If
   3196  1.1  rjs 		 * the FIX: below is ever changed we may need to
   3197  1.1  rjs 		 * lock before calling association level binding.
   3198  1.1  rjs 		 */
   3199  1.1  rjs 		if (addrs->sget_assoc_id == 0) {
   3200  1.1  rjs 			/* delete the address */
   3201  1.1  rjs 			sctp_addr_mgmt_ep_sa(inp, addr_touse,
   3202  1.1  rjs 					     SCTP_DEL_IP_ADDRESS);
   3203  1.1  rjs 		} else {
   3204  1.1  rjs 			/* FIX: decide whether we allow assoc based bindx */
   3205  1.1  rjs 		}
   3206  1.1  rjs 	}
   3207  1.1  rjs 	break;
   3208  1.1  rjs 	default:
   3209  1.1  rjs 		error = ENOPROTOOPT;
   3210  1.1  rjs 		break;
   3211  1.1  rjs 	} /* end switch (opt) */
   3212  1.1  rjs 	return (error);
   3213  1.1  rjs }
   3214  1.1  rjs 
   3215  1.1  rjs int
   3216  1.1  rjs sctp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   3217  1.1  rjs {
   3218  1.1  rjs 	int s, error = 0;
   3219  1.1  rjs 	struct inpcb *inp;
   3220  1.1  rjs #ifdef INET6
   3221  1.1  rjs 	struct in6pcb *in6p;
   3222  1.1  rjs #endif
   3223  1.1  rjs 	int family;	/* family of the socket */
   3224  1.1  rjs 
   3225  1.1  rjs 	family = so->so_proto->pr_domain->dom_family;
   3226  1.1  rjs 
   3227  1.1  rjs 	s = splsoftnet();
   3228  1.1  rjs 	switch (family) {
   3229  1.1  rjs 	case PF_INET:
   3230  1.1  rjs 		inp = sotoinpcb(so);
   3231  1.1  rjs #ifdef INET6
   3232  1.1  rjs 		in6p = NULL;
   3233  1.1  rjs #endif
   3234  1.1  rjs 		break;
   3235  1.1  rjs #ifdef INET6
   3236  1.1  rjs 	case PF_INET6:
   3237  1.1  rjs 		inp = NULL;
   3238  1.1  rjs 		in6p = sotoin6pcb(so);
   3239  1.1  rjs 		break;
   3240  1.1  rjs #endif
   3241  1.1  rjs 	default:
   3242  1.1  rjs 		splx(s);
   3243  1.1  rjs 		return EAFNOSUPPORT;
   3244  1.1  rjs 	}
   3245  1.1  rjs #ifndef INET6
   3246  1.1  rjs 	if (inp == NULL)
   3247  1.1  rjs #else
   3248  1.1  rjs 	if (inp == NULL && in6p == NULL)
   3249  1.1  rjs #endif
   3250  1.1  rjs 	{
   3251  1.1  rjs 		splx(s);
   3252  1.1  rjs 		return (ECONNRESET);
   3253  1.1  rjs 	}
   3254  1.1  rjs 	if (sopt->sopt_level != IPPROTO_SCTP) {
   3255  1.1  rjs 		switch (family) {
   3256  1.1  rjs 		case PF_INET:
   3257  1.1  rjs 			error = ip_ctloutput(op, so, sopt);
   3258  1.1  rjs 			break;
   3259  1.1  rjs #ifdef INET6
   3260  1.1  rjs 		case PF_INET6:
   3261  1.1  rjs 			error = ip6_ctloutput(op, so, sopt);
   3262  1.1  rjs 			break;
   3263  1.1  rjs #endif
   3264  1.1  rjs 		}
   3265  1.1  rjs 		splx(s);
   3266  1.1  rjs 		return (error);
   3267  1.1  rjs 	}
   3268  1.1  rjs 	/* Ok if we reach here it is a SCTP option we hope */
   3269  1.1  rjs 	if (op == PRCO_SETOPT) {
   3270  1.1  rjs 		error = sctp_optsset(so, sopt);
   3271  1.1  rjs 	} else if (op ==  PRCO_GETOPT) {
   3272  1.1  rjs 		error = sctp_optsget(so, sopt);
   3273  1.1  rjs 	} else {
   3274  1.1  rjs 		error = EINVAL;
   3275  1.1  rjs 	}
   3276  1.1  rjs 	splx(s);
   3277  1.1  rjs 	return (error);
   3278  1.1  rjs }
   3279  1.1  rjs 
   3280  1.1  rjs static int
   3281  1.1  rjs sctp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
   3282  1.1  rjs {
   3283  1.1  rjs 	int error = 0;
   3284  1.1  rjs 	struct sctp_inpcb *inp;
   3285  1.1  rjs 	struct sctp_tcb *stcb;
   3286  1.1  rjs 
   3287  1.1  rjs 	KASSERT(solocked(so));
   3288  1.1  rjs #ifdef SCTP_DEBUG
   3289  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   3290  1.1  rjs 		printf("Connect called in SCTP to ");
   3291  1.1  rjs 		sctp_print_address(nam);
   3292  1.1  rjs 		printf("Port %d\n", ntohs(((struct sockaddr_in *)nam)->sin_port));
   3293  1.1  rjs 	}
   3294  1.1  rjs #endif /* SCTP_DEBUG */
   3295  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3296  1.1  rjs 	if (inp == 0) {
   3297  1.1  rjs 		/* I made the same as TCP since we are not setup? */
   3298  1.1  rjs 		return (ECONNRESET);
   3299  1.1  rjs 	}
   3300  1.1  rjs 	SCTP_ASOC_CREATE_LOCK(inp);
   3301  1.1  rjs #ifdef SCTP_DEBUG
   3302  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   3303  1.1  rjs 		printf("After ASOC lock\n");
   3304  1.1  rjs 	}
   3305  1.1  rjs #endif /* SCTP_DEBUG */
   3306  1.1  rjs 	SCTP_INP_WLOCK(inp);
   3307  1.1  rjs #ifdef SCTP_DEBUG
   3308  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   3309  1.1  rjs 		printf("After INP_WLOCK lock\n");
   3310  1.1  rjs 	}
   3311  1.1  rjs #endif /* SCTP_DEBUG */
   3312  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   3313  1.1  rjs 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   3314  1.1  rjs 		/* Should I really unlock ? */
   3315  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3316  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   3317  1.1  rjs 		return (EFAULT);
   3318  1.1  rjs 	}
   3319  1.1  rjs #ifdef INET6
   3320  1.1  rjs 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
   3321  1.1  rjs 	    (nam->sa_family == AF_INET6)) {
   3322  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3323  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   3324  1.1  rjs 		return (EINVAL);
   3325  1.1  rjs 	}
   3326  1.1  rjs #endif /* INET6 */
   3327  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
   3328  1.1  rjs 	    SCTP_PCB_FLAGS_UNBOUND) {
   3329  1.1  rjs 		/* Bind a ephemeral port */
   3330  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3331  1.1  rjs 		error = sctp_inpcb_bind(so, NULL, l);
   3332  1.1  rjs 		if (error) {
   3333  1.1  rjs 			SCTP_ASOC_CREATE_UNLOCK(inp);
   3334  1.1  rjs 			return (error);
   3335  1.1  rjs 		}
   3336  1.1  rjs 		SCTP_INP_WLOCK(inp);
   3337  1.1  rjs 	}
   3338  1.1  rjs #ifdef SCTP_DEBUG
   3339  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
   3340  1.1  rjs 		printf("After bind\n");
   3341  1.1  rjs 	}
   3342  1.1  rjs #endif /* SCTP_DEBUG */
   3343  1.1  rjs 	/* Now do we connect? */
   3344  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   3345  1.1  rjs 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
   3346  1.1  rjs 		/* We are already connected AND the TCP model */
   3347  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3348  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   3349  1.1  rjs 		return (EADDRINUSE);
   3350  1.1  rjs 	}
   3351  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   3352  1.1  rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3353  1.1  rjs 		if (stcb) {
   3354  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   3355  1.1  rjs 		}
   3356  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3357  1.1  rjs 	} else {
   3358  1.1  rjs 		SCTP_INP_INCR_REF(inp);
   3359  1.1  rjs 		SCTP_INP_WUNLOCK(inp);
   3360  1.1  rjs 		stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
   3361  1.1  rjs 		if (stcb == NULL) {
   3362  1.1  rjs 			SCTP_INP_WLOCK(inp);
   3363  1.1  rjs 			SCTP_INP_DECR_REF(inp);
   3364  1.1  rjs 			SCTP_INP_WUNLOCK(inp);
   3365  1.1  rjs 		}
   3366  1.1  rjs 	}
   3367  1.1  rjs 	if (stcb != NULL) {
   3368  1.1  rjs 		/* Already have or am bring up an association */
   3369  1.1  rjs 		SCTP_ASOC_CREATE_UNLOCK(inp);
   3370  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   3371  1.1  rjs 		return (EALREADY);
   3372  1.1  rjs 	}
   3373  1.1  rjs 	/* We are GOOD to go */
   3374  1.1  rjs 	stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
   3375  1.1  rjs 	if (stcb == NULL) {
   3376  1.1  rjs 		/* Gak! no memory */
   3377  1.1  rjs 		return (error);
   3378  1.1  rjs 	}
   3379  1.1  rjs 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
   3380  1.1  rjs 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
   3381  1.1  rjs 		/* Set the connected flag so we can queue data */
   3382  1.1  rjs 		soisconnecting(so);
   3383  1.1  rjs 	}
   3384  1.1  rjs 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
   3385  1.1  rjs 	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
   3386  1.1  rjs 	sctp_send_initiate(inp, stcb);
   3387  1.1  rjs 	SCTP_ASOC_CREATE_UNLOCK(inp);
   3388  1.1  rjs 	SCTP_TCB_UNLOCK(stcb);
   3389  1.1  rjs 	return error;
   3390  1.1  rjs }
   3391  1.1  rjs 
   3392  1.1  rjs static int
   3393  1.1  rjs sctp_connect2(struct socket *so, struct socket *so2)
   3394  1.1  rjs {
   3395  1.1  rjs 	KASSERT(solocked(so));
   3396  1.1  rjs 
   3397  1.1  rjs 	return EOPNOTSUPP;
   3398  1.1  rjs }
   3399  1.1  rjs 
   3400  1.1  rjs int
   3401  1.1  rjs sctp_rcvd(struct socket *so, int flags, struct lwp *l)
   3402  1.1  rjs {
   3403  1.1  rjs 	struct sctp_socket_q_list *sq=NULL;
   3404  1.1  rjs 	/*
   3405  1.1  rjs 	 * The user has received some data, we may be able to stuff more
   3406  1.1  rjs 	 * up the socket. And we need to possibly update the rwnd.
   3407  1.1  rjs 	 */
   3408  1.1  rjs 	struct sctp_inpcb *inp;
   3409  1.1  rjs 	struct sctp_tcb *stcb=NULL;
   3410  1.1  rjs 
   3411  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3412  1.1  rjs #ifdef SCTP_DEBUG
   3413  1.1  rjs 	if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
   3414  1.1  rjs 		printf("Read for so:%p inp:%p Flags:%x\n",
   3415  1.1  rjs 		       so, inp, flags);
   3416  1.1  rjs #endif
   3417  1.1  rjs 
   3418  1.1  rjs 	if (inp == 0) {
   3419  1.1  rjs 		/* I made the same as TCP since we are not setup? */
   3420  1.1  rjs #ifdef SCTP_DEBUG
   3421  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
   3422  1.1  rjs 			printf("Nope, connection reset\n");
   3423  1.1  rjs #endif
   3424  1.1  rjs 		return (ECONNRESET);
   3425  1.1  rjs 	}
   3426  1.1  rjs 	/*
   3427  1.1  rjs 	 * Grab the first one on the list. It will re-insert itself if
   3428  1.1  rjs 	 * it runs out of room
   3429  1.1  rjs 	 */
   3430  1.1  rjs 	SCTP_INP_WLOCK(inp);
   3431  1.1  rjs 	if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
   3432  1.1  rjs 	    && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
   3433  1.1  rjs 		/* Ok the other part of our grubby tracking
   3434  1.1  rjs 		 * stuff for our horrible layer violation that
   3435  1.1  rjs 		 * the tsvwg thinks is ok for sctp_peeloff.. gak!
   3436  1.1  rjs 		 * We must update the next vtag pending on the
   3437  1.1  rjs 		 * socket buffer (if any).
   3438  1.1  rjs 		 */
   3439  1.1  rjs 		inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so);
   3440  1.1  rjs 		sq = TAILQ_FIRST(&inp->sctp_queue_list);
   3441  1.1  rjs 		if (sq) {
   3442  1.1  rjs 			stcb = sq->tcb;
   3443  1.1  rjs 		} else {
   3444  1.1  rjs 			stcb = NULL;
   3445  1.1  rjs 		}
   3446  1.1  rjs 	} else {
   3447  1.1  rjs 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3448  1.1  rjs 	}
   3449  1.1  rjs 	if (stcb) {
   3450  1.1  rjs 		SCTP_TCB_LOCK(stcb);
   3451  1.1  rjs 	}
   3452  1.1  rjs 	if (stcb) {
   3453  1.1  rjs 		long incr;
   3454  1.1  rjs 		/* all code in normal stcb path assumes
   3455  1.1  rjs 		 * that you have a tcb_lock only. Thus
   3456  1.1  rjs 		 * we must release the inp write lock.
   3457  1.1  rjs 		 */
   3458  1.1  rjs 		if (flags & MSG_EOR) {
   3459  1.1  rjs 			if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
   3460  1.1  rjs 			   && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
   3461  1.1  rjs 				stcb = sctp_remove_from_socket_q(inp);
   3462  1.1  rjs 			}
   3463  1.1  rjs #ifdef SCTP_DEBUG
   3464  1.1  rjs 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
   3465  1.1  rjs 				printf("remove from socket queue for inp:%p tcbret:%p\n",
   3466  1.1  rjs 				       inp, stcb);
   3467  1.1  rjs #endif
   3468  1.1  rjs 
   3469  1.1  rjs  			stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
   3470  1.1  rjs  									  sizeof(struct mbuf));
   3471  1.1  rjs 			if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) {
   3472  1.1  rjs  				stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
   3473  1.1  rjs  										  CMSG_LEN(sizeof(struct sctp_sndrcvinfo)));
   3474  1.1  rjs 			}
   3475  1.1  rjs 		}
   3476  1.1  rjs 		if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) ||
   3477  1.1  rjs 		    (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) {
   3478  1.1  rjs 			/* Deliver if there is something to be delivered */
   3479  1.1  rjs 			sctp_service_queues(stcb, &stcb->asoc, 1);
   3480  1.1  rjs 		}
   3481  1.1  rjs 		sctp_set_rwnd(stcb, &stcb->asoc);
   3482  1.1  rjs 		/* if we increase by 1 or more MTU's (smallest MTUs of all
   3483  1.1  rjs 		 * nets) we send a window update sack
   3484  1.1  rjs 		 */
   3485  1.1  rjs 		incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd;
   3486  1.1  rjs 		if (incr < 0) {
   3487  1.1  rjs 			incr = 0;
   3488  1.1  rjs 		}
   3489  1.1  rjs 		if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) ||
   3490  1.1  rjs 		    ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.sb_hiwat)) {
   3491  1.1  rjs 			if (callout_pending(&stcb->asoc.dack_timer.timer)) {
   3492  1.1  rjs 				/* If the timer is up, stop it */
   3493  1.1  rjs 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   3494  1.1  rjs 						stcb->sctp_ep, stcb, NULL);
   3495  1.1  rjs 			}
   3496  1.1  rjs 			/* Send the sack, with the new rwnd */
   3497  1.1  rjs 			sctp_send_sack(stcb);
   3498  1.1  rjs 			/* Now do the output */
   3499  1.1  rjs 			sctp_chunk_output(inp, stcb, 10);
   3500  1.1  rjs 		}
   3501  1.1  rjs 	} else {
   3502  1.1  rjs 		if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0))
   3503  1.1  rjs 		    && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
   3504  1.1  rjs 			stcb = sctp_remove_from_socket_q(inp);
   3505  1.1  rjs 		}
   3506  1.1  rjs 	}
   3507  1.1  rjs 	if ((so->so_rcv.sb_mb == NULL) &&
   3508  1.1  rjs 	    (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) {
   3509  1.1  rjs 		int sq_cnt=0;
   3510  1.1  rjs #ifdef SCTP_DEBUG
   3511  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
   3512  1.1  rjs 			printf("Something off, inp:%p so->so_rcv->sb_mb is empty and sockq is not.. cleaning\n",
   3513  1.1  rjs 			       inp);
   3514  1.1  rjs #endif
   3515  1.1  rjs 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
   3516  1.1  rjs 		   && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
   3517  1.1  rjs 			int done_yet;
   3518  1.1  rjs 			done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
   3519  1.1  rjs 			while (!done_yet) {
   3520  1.1  rjs 				sq_cnt++;
   3521  1.1  rjs 				(void)sctp_remove_from_socket_q(inp);
   3522  1.1  rjs 				done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
   3523  1.1  rjs 			}
   3524  1.1  rjs 		}
   3525  1.1  rjs #ifdef SCTP_DEBUG
   3526  1.1  rjs 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
   3527  1.1  rjs 			printf("Cleaned up %d sockq's\n", sq_cnt);
   3528  1.1  rjs #endif
   3529  1.1  rjs 	}
   3530  1.1  rjs 	if (stcb) {
   3531  1.1  rjs 		SCTP_TCB_UNLOCK(stcb);
   3532  1.1  rjs 	}
   3533  1.1  rjs 	SCTP_INP_WUNLOCK(inp);
   3534  1.1  rjs 	return (0);
   3535  1.1  rjs }
   3536  1.1  rjs 
   3537  1.1  rjs int
   3538  1.1  rjs sctp_listen(struct socket *so, struct lwp *l)
   3539  1.1  rjs {
   3540  1.1  rjs 	/*
   3541  1.1  rjs 	 * Note this module depends on the protocol processing being
   3542  1.1  rjs 	 * called AFTER any socket level flags and backlog are applied
   3543  1.1  rjs 	 * to the socket. The traditional way that the socket flags are
   3544  1.1  rjs 	 * applied is AFTER protocol processing. We have made a change
   3545  1.1  rjs 	 * to the sys/kern/uipc_socket.c module to reverse this but this
   3546  1.1  rjs 	 * MUST be in place if the socket API for SCTP is to work properly.
   3547  1.1  rjs 	 */
   3548  1.1  rjs 	int error = 0;
   3549  1.1  rjs 	struct sctp_inpcb *inp;
   3550  1.1  rjs 
   3551  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3552  1.1  rjs 	if (inp == 0) {
   3553  1.1  rjs 		/* I made the same as TCP since we are not setup? */
   3554  1.1  rjs 		return (ECONNRESET);
   3555  1.1  rjs 	}
   3556  1.1  rjs 	SCTP_INP_RLOCK(inp);
   3557  1.1  rjs 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   3558  1.1  rjs 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
   3559  1.1  rjs 		/* We are already connected AND the TCP model */
   3560  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   3561  1.1  rjs 		return (EADDRINUSE);
   3562  1.1  rjs 	}
   3563  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
   3564  1.1  rjs 		/* We must do a bind. */
   3565  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   3566  1.1  rjs 		if ((error = sctp_inpcb_bind(so, NULL, l))) {
   3567  1.1  rjs 			/* bind error, probably perm */
   3568  1.1  rjs 			return (error);
   3569  1.1  rjs 		}
   3570  1.1  rjs 	} else {
   3571  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   3572  1.1  rjs 	}
   3573  1.1  rjs 	SCTP_INP_WLOCK(inp);
   3574  1.1  rjs 	if (inp->sctp_socket->so_qlimit) {
   3575  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
   3576  1.1  rjs 			/*
   3577  1.1  rjs 			 * For the UDP model we must TURN OFF the ACCEPT
   3578  1.1  rjs 			 * flags since we do NOT allow the accept() call.
   3579  1.1  rjs 			 * The TCP model (when present) will do accept which
   3580  1.1  rjs 			 * then prohibits connect().
   3581  1.1  rjs 			 */
   3582  1.1  rjs 			inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
   3583  1.1  rjs 		}
   3584  1.1  rjs 		inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
   3585  1.1  rjs 	} else {
   3586  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
   3587  1.1  rjs 			/*
   3588  1.1  rjs 			 * Turning off the listen flags if the backlog is
   3589  1.1  rjs 			 * set to 0 (i.e. qlimit is 0).
   3590  1.1  rjs 			 */
   3591  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
   3592  1.1  rjs 		}
   3593  1.1  rjs 		inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
   3594  1.1  rjs 	}
   3595  1.1  rjs 	SCTP_INP_WUNLOCK(inp);
   3596  1.1  rjs 	return (error);
   3597  1.1  rjs }
   3598  1.1  rjs 
   3599  1.1  rjs int
   3600  1.1  rjs sctp_accept(struct socket *so, struct sockaddr *nam)
   3601  1.1  rjs {
   3602  1.1  rjs 	struct sctp_tcb *stcb;
   3603  1.1  rjs 	const struct sockaddr *prim;
   3604  1.1  rjs 	struct sctp_inpcb *inp;
   3605  1.1  rjs 	int error;
   3606  1.1  rjs 
   3607  1.1  rjs 	if (nam == NULL) {
   3608  1.1  rjs 		return EINVAL;
   3609  1.1  rjs 	}
   3610  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3611  1.1  rjs 
   3612  1.1  rjs 	if (inp == 0) {
   3613  1.1  rjs 		return ECONNRESET;
   3614  1.1  rjs 	}
   3615  1.1  rjs 	SCTP_INP_RLOCK(inp);
   3616  1.1  rjs 	if (so->so_state & SS_ISDISCONNECTED) {
   3617  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   3618  1.1  rjs 		return ECONNABORTED;
   3619  1.1  rjs 	}
   3620  1.1  rjs 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3621  1.1  rjs 	if (stcb == NULL) {
   3622  1.1  rjs 		SCTP_INP_RUNLOCK(inp);
   3623  1.1  rjs 		return ECONNRESET;
   3624  1.1  rjs 	}
   3625  1.1  rjs 	SCTP_TCB_LOCK(stcb);
   3626  1.1  rjs 	SCTP_INP_RUNLOCK(inp);
   3627  1.1  rjs 	prim = (const struct sockaddr *)rtcache_getdst(&stcb->asoc.primary_destination->ro);
   3628  1.1  rjs 	if (prim->sa_family == AF_INET) {
   3629  1.1  rjs 		struct sockaddr_in *sin;
   3630  1.1  rjs 
   3631  1.1  rjs 		sin = (struct sockaddr_in *)nam;
   3632  1.1  rjs 		memset((void *)sin, 0, sizeof (*sin));
   3633  1.1  rjs 
   3634  1.1  rjs 		sin->sin_family = AF_INET;
   3635  1.1  rjs 		sin->sin_len = sizeof(*sin);
   3636  1.1  rjs 		sin->sin_port = ((const struct sockaddr_in *)prim)->sin_port;
   3637  1.1  rjs 		sin->sin_addr = ((const struct sockaddr_in *)prim)->sin_addr;
   3638  1.1  rjs 	} else {
   3639  1.1  rjs 		struct sockaddr_in6 *sin6;
   3640  1.1  rjs 
   3641  1.1  rjs 		sin6 = (struct sockaddr_in6 *)nam;
   3642  1.1  rjs 		memset((void *)sin6, 0, sizeof (*sin6));
   3643  1.1  rjs 		sin6->sin6_family = AF_INET6;
   3644  1.1  rjs 		sin6->sin6_len = sizeof(*sin6);
   3645  1.1  rjs 		sin6->sin6_port = ((const struct sockaddr_in6 *)prim)->sin6_port;
   3646  1.1  rjs 
   3647  1.1  rjs 		sin6->sin6_addr = ((const struct sockaddr_in6 *)prim)->sin6_addr;
   3648  1.1  rjs 		if ((error = sa6_recoverscope(sin6)) != 0)
   3649  1.1  rjs 			return error;
   3650  1.1  rjs 
   3651  1.1  rjs 	}
   3652  1.1  rjs 	/* Wake any delayed sleep action */
   3653  1.1  rjs 	SCTP_TCB_UNLOCK(stcb);
   3654  1.1  rjs 	SCTP_INP_WLOCK(inp);
   3655  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
   3656  1.1  rjs 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
   3657  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
   3658  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
   3659  1.1  rjs 			if (sowritable(inp->sctp_socket))
   3660  1.1  rjs 				sowwakeup(inp->sctp_socket);
   3661  1.1  rjs 		}
   3662  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
   3663  1.1  rjs 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
   3664  1.1  rjs 			if (soreadable(inp->sctp_socket))
   3665  1.1  rjs 				sorwakeup(inp->sctp_socket);
   3666  1.1  rjs 		}
   3667  1.1  rjs 
   3668  1.1  rjs 	}
   3669  1.1  rjs 	SCTP_INP_WUNLOCK(inp);
   3670  1.1  rjs 	return 0;
   3671  1.1  rjs }
   3672  1.1  rjs 
   3673  1.1  rjs static int
   3674  1.1  rjs sctp_stat(struct socket *so, struct stat *ub)
   3675  1.1  rjs {
   3676  1.1  rjs 	return 0;
   3677  1.1  rjs }
   3678  1.1  rjs 
   3679  1.1  rjs int
   3680  1.1  rjs sctp_sockaddr(struct socket *so, struct sockaddr *nam)
   3681  1.1  rjs {
   3682  1.1  rjs 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
   3683  1.1  rjs 	struct sctp_inpcb *inp;
   3684  1.1  rjs 
   3685  1.1  rjs 	memset(sin, 0, sizeof(*sin));
   3686  1.1  rjs 	sin->sin_family = AF_INET;
   3687  1.1  rjs 	sin->sin_len = sizeof(*sin);
   3688  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3689  1.1  rjs 	if (!inp) {
   3690  1.1  rjs 		return ECONNRESET;
   3691  1.1  rjs 	}
   3692  1.1  rjs 	SCTP_INP_RLOCK(inp);
   3693  1.1  rjs 	sin->sin_port = inp->sctp_lport;
   3694  1.1  rjs 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3695  1.1  rjs 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   3696  1.1  rjs 			struct sctp_tcb *stcb;
   3697  1.1  rjs 			const struct sockaddr_in *sin_a;
   3698  1.1  rjs 			struct sctp_nets *net;
   3699  1.1  rjs 			int fnd;
   3700  1.1  rjs 
   3701  1.1  rjs 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3702  1.1  rjs 			if (stcb == NULL) {
   3703  1.1  rjs 				goto notConn;
   3704  1.1  rjs 			}
   3705  1.1  rjs 			fnd = 0;
   3706  1.1  rjs 			sin_a = NULL;
   3707  1.1  rjs 			SCTP_TCB_LOCK(stcb);
   3708  1.1  rjs 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   3709  1.1  rjs 				sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro);
   3710  1.1  rjs 				if (sin_a->sin_family == AF_INET) {
   3711  1.1  rjs 					fnd = 1;
   3712  1.1  rjs 					break;
   3713  1.1  rjs 				}
   3714  1.1  rjs 			}
   3715  1.1  rjs 			if ((!fnd) || (sin_a == NULL)) {
   3716  1.1  rjs 				/* punt */
   3717  1.1  rjs 				SCTP_TCB_UNLOCK(stcb);
   3718  1.1  rjs 				goto notConn;
   3719  1.1  rjs 			}
   3720  1.1  rjs 			sin->sin_addr = sctp_ipv4_source_address_selection(inp,
   3721  1.1  rjs 			    stcb, (struct route *)&net->ro, net, 0);
   3722  1.1  rjs 			SCTP_TCB_UNLOCK(stcb);
   3723  1.1  rjs 		} else {
   3724  1.1  rjs 			/* For the bound all case you get back 0 */
   3725  1.1  rjs 		notConn:
   3726  1.1  rjs 			sin->sin_addr.s_addr = 0;
   3727  1.1  rjs 		}
   3728  1.1  rjs 
   3729  1.1  rjs 	} else {
   3730  1.1  rjs 		/* Take the first IPv4 address in the list */
   3731  1.1  rjs 		struct sctp_laddr *laddr;
   3732  1.1  rjs 		int fnd = 0;
   3733  1.1  rjs 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3734  1.1  rjs 			if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
   3735  1.1  rjs 				struct sockaddr_in *sin_a;
   3736  1.1  rjs 				sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr;
   3737  1.1  rjs 				sin->sin_addr = sin_a->sin_addr;
   3738  1.1  rjs 				fnd = 1;
   3739  1.1  rjs 				break;
   3740  1.1  rjs 			}
   3741  1.1  rjs 		}
   3742  1.1  rjs 		if (!fnd) {
   3743  1.1  rjs 			SCTP_INP_RUNLOCK(inp);
   3744  1.1  rjs 			return ENOENT;
   3745  1.1  rjs 		}
   3746  1.1  rjs 	}
   3747  1.1  rjs 	SCTP_INP_RUNLOCK(inp);
   3748  1.1  rjs 	return (0);
   3749  1.1  rjs }
   3750  1.1  rjs 
   3751  1.1  rjs int
   3752  1.1  rjs sctp_peeraddr(struct socket *so, struct sockaddr *nam)
   3753  1.1  rjs {
   3754  1.1  rjs 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
   3755  1.1  rjs 	int fnd;
   3756  1.1  rjs 	const struct sockaddr_in *sin_a;
   3757  1.1  rjs 	struct sctp_inpcb *inp;
   3758  1.1  rjs 	struct sctp_tcb *stcb;
   3759  1.1  rjs 	struct sctp_nets *net;
   3760  1.1  rjs 
   3761  1.1  rjs 	/* Do the malloc first in case it blocks. */
   3762  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3763  1.1  rjs 	if ((inp == NULL) ||
   3764  1.1  rjs 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
   3765  1.1  rjs 		/* UDP type and listeners will drop out here */
   3766  1.1  rjs 		return (ENOTCONN);
   3767  1.1  rjs 	}
   3768  1.1  rjs 
   3769  1.1  rjs 	memset(sin, 0, sizeof(*sin));
   3770  1.1  rjs 	sin->sin_family = AF_INET;
   3771  1.1  rjs 	sin->sin_len = sizeof(*sin);
   3772  1.1  rjs 
   3773  1.1  rjs 	/* We must recapture incase we blocked */
   3774  1.1  rjs 	inp = (struct sctp_inpcb *)so->so_pcb;
   3775  1.1  rjs 	if (!inp) {
   3776  1.1  rjs 		return ECONNRESET;
   3777  1.1  rjs 	}
   3778  1.1  rjs 	SCTP_INP_RLOCK(inp);
   3779  1.1  rjs 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
   3780  1.1  rjs 	if (stcb) {
   3781  1.1  rjs 		SCTP_TCB_LOCK(stcb);
   3782  1.1  rjs 	}
   3783  1.1  rjs 	SCTP_INP_RUNLOCK(inp);
   3784  1.1  rjs 	if (stcb == NULL) {
   3785  1.1  rjs 		return ECONNRESET;
   3786  1.1  rjs 	}
   3787  1.1  rjs 	fnd = 0;
   3788  1.1  rjs 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   3789  1.1  rjs 		sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro);
   3790  1.1  rjs 		if (sin_a->sin_family == AF_INET) {
   3791  1.1  rjs 			fnd = 1;
   3792  1.1  rjs 			sin->sin_port = stcb->rport;
   3793  1.1  rjs 			sin->sin_addr = sin_a->sin_addr;
   3794  1.1  rjs 			break;
   3795  1.1  rjs 		}
   3796  1.1  rjs 	}
   3797  1.1  rjs 	SCTP_TCB_UNLOCK(stcb);
   3798  1.1  rjs 	if (!fnd) {
   3799  1.1  rjs 		/* No IPv4 address */
   3800  1.1  rjs 		return ENOENT;
   3801  1.1  rjs 	}
   3802  1.1  rjs 	return (0);
   3803  1.1  rjs }
   3804  1.1  rjs 
   3805  1.1  rjs static int
   3806  1.1  rjs sctp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
   3807  1.1  rjs {
   3808  1.1  rjs 	KASSERT(solocked(so));
   3809  1.1  rjs 
   3810  1.1  rjs 	if (m)
   3811  1.1  rjs 		m_freem(m);
   3812  1.1  rjs 	if (control)
   3813  1.1  rjs 		m_freem(control);
   3814  1.1  rjs 
   3815  1.1  rjs 	return EOPNOTSUPP;
   3816  1.1  rjs }
   3817  1.1  rjs 
   3818  1.1  rjs static int
   3819  1.1  rjs sctp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
   3820  1.1  rjs {
   3821  1.1  rjs 	int error = 0;
   3822  1.1  rjs 	int family;
   3823  1.1  rjs 
   3824  1.1  rjs 	family = so->so_proto->pr_domain->dom_family;
   3825  1.1  rjs 	switch (family) {
   3826  1.1  rjs #ifdef INET
   3827  1.1  rjs 	case PF_INET:
   3828  1.1  rjs 		error = in_control(so, cmd, nam, ifp);
   3829  1.1  rjs 		break;
   3830  1.1  rjs #endif
   3831  1.1  rjs #ifdef INET6
   3832  1.1  rjs 	case PF_INET6:
   3833  1.1  rjs 		error = in6_control(so, cmd, nam, ifp);
   3834  1.1  rjs 		break;
   3835  1.1  rjs #endif
   3836  1.1  rjs 	default:
   3837  1.1  rjs 		error =  EAFNOSUPPORT;
   3838  1.1  rjs 	}
   3839  1.1  rjs 	return (error);
   3840  1.1  rjs }
   3841  1.1  rjs 
   3842  1.1  rjs static int
   3843  1.1  rjs sctp_purgeif(struct socket *so, struct ifnet *ifp)
   3844  1.1  rjs {
   3845  1.1  rjs 	struct ifaddr *ifa;
   3846  1.1  rjs 	IFADDR_FOREACH(ifa, ifp) {
   3847  1.1  rjs 		if (ifa->ifa_addr->sa_family == PF_INET) {
   3848  1.1  rjs 			sctp_delete_ip_address(ifa);
   3849  1.1  rjs 		}
   3850  1.1  rjs 	}
   3851  1.1  rjs 
   3852  1.1  rjs 	mutex_enter(softnet_lock);
   3853  1.1  rjs 	in_purgeif(ifp);
   3854  1.1  rjs 	mutex_exit(softnet_lock);
   3855  1.1  rjs 
   3856  1.1  rjs 	return 0;
   3857  1.1  rjs }
   3858  1.1  rjs 
   3859  1.1  rjs /*
   3860  1.1  rjs  * Sysctl for sctp variables.
   3861  1.1  rjs  */
   3862  1.1  rjs SYSCTL_SETUP(sysctl_net_inet_sctp_setup, "sysctl net.inet.sctp subtree setup")
   3863  1.1  rjs {
   3864  1.1  rjs 
   3865  1.1  rjs 	sysctl_createv(clog, 0, NULL, NULL,
   3866  1.1  rjs 		       CTLFLAG_PERMANENT,
   3867  1.1  rjs 	               CTLTYPE_NODE, "net", NULL,
   3868  1.1  rjs                        NULL, 0, NULL, 0,
   3869  1.1  rjs                        CTL_NET, CTL_EOL);
   3870  1.1  rjs         sysctl_createv(clog, 0, NULL, NULL,
   3871  1.1  rjs                        CTLFLAG_PERMANENT,
   3872  1.1  rjs                        CTLTYPE_NODE, "inet", NULL,
   3873  1.1  rjs                        NULL, 0, NULL, 0,
   3874  1.1  rjs                        CTL_NET, PF_INET, CTL_EOL);
   3875  1.1  rjs         sysctl_createv(clog, 0, NULL, NULL,
   3876  1.1  rjs                        CTLFLAG_PERMANENT,
   3877  1.1  rjs                        CTLTYPE_NODE, "sctp",
   3878  1.1  rjs                        SYSCTL_DESCR("sctp related settings"),
   3879  1.1  rjs                        NULL, 0, NULL, 0,
   3880  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL);
   3881  1.1  rjs 
   3882  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3883  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3884  1.1  rjs                        CTLTYPE_INT, "maxdgram",
   3885  1.1  rjs                        SYSCTL_DESCR("Maximum outgoing SCTP buffer size"),
   3886  1.1  rjs                        NULL, 0, &sctp_sendspace, 0,
   3887  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM,
   3888  1.1  rjs                        CTL_EOL);
   3889  1.1  rjs 
   3890  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3891  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3892  1.1  rjs                        CTLTYPE_INT, "recvspace",
   3893  1.1  rjs                        SYSCTL_DESCR("Maximum incoming SCTP buffer size"),
   3894  1.1  rjs                        NULL, 0, &sctp_recvspace, 0,
   3895  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE,
   3896  1.1  rjs                        CTL_EOL);
   3897  1.1  rjs 
   3898  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3899  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3900  1.1  rjs                        CTLTYPE_INT, "autoasconf",
   3901  1.1  rjs                        SYSCTL_DESCR("Enable SCTP Auto-ASCONF"),
   3902  1.1  rjs                        NULL, 0, &sctp_auto_asconf, 0,
   3903  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF,
   3904  1.1  rjs                        CTL_EOL);
   3905  1.1  rjs 
   3906  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3907  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3908  1.1  rjs                        CTLTYPE_INT, "ecn_enable",
   3909  1.1  rjs                        SYSCTL_DESCR("Enable SCTP ECN"),
   3910  1.1  rjs                        NULL, 0, &sctp_ecn, 0,
   3911  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE,
   3912  1.1  rjs                        CTL_EOL);
   3913  1.1  rjs 
   3914  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3915  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3916  1.1  rjs                        CTLTYPE_INT, "ecn_nonce",
   3917  1.1  rjs                        SYSCTL_DESCR("Enable SCTP ECN Nonce"),
   3918  1.1  rjs                        NULL, 0, &sctp_ecn_nonce, 0,
   3919  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE,
   3920  1.1  rjs                        CTL_EOL);
   3921  1.1  rjs 
   3922  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3923  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3924  1.1  rjs                        CTLTYPE_INT, "strict_sack",
   3925  1.1  rjs                        SYSCTL_DESCR("Enable SCTP Strict SACK checking"),
   3926  1.1  rjs                        NULL, 0, &sctp_strict_sacks, 0,
   3927  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK,
   3928  1.1  rjs                        CTL_EOL);
   3929  1.1  rjs 
   3930  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3931  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3932  1.1  rjs                        CTLTYPE_INT, "loopback_nocsum",
   3933  1.1  rjs                        SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"),
   3934  1.1  rjs                        NULL, 0, &sctp_no_csum_on_loopback, 0,
   3935  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO,
   3936  1.1  rjs                        CTL_EOL);
   3937  1.1  rjs 
   3938  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3939  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3940  1.1  rjs                        CTLTYPE_INT, "strict_init",
   3941  1.1  rjs                        SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"),
   3942  1.1  rjs                        NULL, 0, &sctp_strict_init, 0,
   3943  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT,
   3944  1.1  rjs                        CTL_EOL);
   3945  1.1  rjs 
   3946  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3947  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3948  1.1  rjs                        CTLTYPE_INT, "peer_chkoh",
   3949  1.1  rjs                        SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"),
   3950  1.1  rjs                        NULL, 0, &sctp_peer_chunk_oh, 0,
   3951  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH,
   3952  1.1  rjs                        CTL_EOL);
   3953  1.1  rjs 
   3954  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3955  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3956  1.1  rjs                        CTLTYPE_INT, "maxburst",
   3957  1.1  rjs                        SYSCTL_DESCR("Default max burst for sctp endpoints"),
   3958  1.1  rjs                        NULL, 0, &sctp_max_burst_default, 0,
   3959  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST,
   3960  1.1  rjs                        CTL_EOL);
   3961  1.1  rjs 
   3962  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3963  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3964  1.1  rjs                        CTLTYPE_INT, "maxchunks",
   3965  1.1  rjs                        SYSCTL_DESCR("Default max chunks on queue per asoc"),
   3966  1.1  rjs                        NULL, 0, &sctp_max_chunks_on_queue, 0,
   3967  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ,
   3968  1.1  rjs                        CTL_EOL);
   3969  1.1  rjs #ifdef SCTP_DEBUG
   3970  1.1  rjs        sysctl_createv(clog, 0, NULL, NULL,
   3971  1.1  rjs                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3972  1.1  rjs                        CTLTYPE_INT, "debug",
   3973  1.1  rjs                        SYSCTL_DESCR("Configure debug output"),
   3974  1.1  rjs                        NULL, 0, &sctp_debug_on, 0,
   3975  1.1  rjs                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_DEBUG,
   3976  1.1  rjs                        CTL_EOL);
   3977  1.1  rjs #endif
   3978  1.1  rjs }
   3979  1.1  rjs 
   3980  1.1  rjs PR_WRAP_USRREQS(sctp)
   3981  1.1  rjs #define	sctp_attach	sctp_attach_wrapper
   3982  1.1  rjs #define	sctp_detach	sctp_detach_wrapper
   3983  1.1  rjs #define sctp_accept	sctp_accept_wrapper
   3984  1.1  rjs #define sctp_bind	sctp_bind_wrapper
   3985  1.1  rjs #define sctp_listen	sctp_listen_wrapper
   3986  1.1  rjs #define sctp_connect	sctp_connect_wrapper
   3987  1.1  rjs #define sctp_connect2	sctp_connect2_wrapper
   3988  1.1  rjs #define sctp_disconnect	sctp_disconnect_wrapper
   3989  1.1  rjs #define sctp_shutdown	sctp_shutdown_wrapper
   3990  1.1  rjs #define sctp_abort	sctp_abort_wrapper
   3991  1.1  rjs #define	sctp_ioctl	sctp_ioctl_wrapper
   3992  1.1  rjs #define	sctp_stat	sctp_stat_wrapper
   3993  1.1  rjs #define sctp_peeraddr	sctp_peeraddr_wrapper
   3994  1.1  rjs #define sctp_sockaddr	sctp_sockaddr_wrapper
   3995  1.1  rjs #define sctp_rcvd	sctp_rcvd_wrapper
   3996  1.1  rjs #define sctp_recvoob	sctp_recvoob_wrapper
   3997  1.1  rjs #define sctp_send	sctp_send_wrapper
   3998  1.1  rjs #define sctp_sendoob	sctp_sendoob_wrapper
   3999  1.1  rjs #define sctp_purgeif	sctp_purgeif_wrapper
   4000  1.1  rjs 
   4001  1.1  rjs const struct pr_usrreqs sctp_usrreqs = {
   4002  1.1  rjs 	.pr_attach	= sctp_attach,
   4003  1.1  rjs 	.pr_detach	= sctp_detach,
   4004  1.1  rjs 	.pr_accept	= sctp_accept,
   4005  1.1  rjs 	.pr_bind	= sctp_bind,
   4006  1.1  rjs 	.pr_listen	= sctp_listen,
   4007  1.1  rjs 	.pr_connect	= sctp_connect,
   4008  1.1  rjs 	.pr_connect2	= sctp_connect2,
   4009  1.1  rjs 	.pr_disconnect	= sctp_disconnect,
   4010  1.1  rjs 	.pr_shutdown	= sctp_shutdown,
   4011  1.1  rjs 	.pr_abort	= sctp_abort,
   4012  1.1  rjs 	.pr_ioctl	= sctp_ioctl,
   4013  1.1  rjs 	.pr_stat	= sctp_stat,
   4014  1.1  rjs 	.pr_peeraddr	= sctp_peeraddr,
   4015  1.1  rjs 	.pr_sockaddr	= sctp_sockaddr,
   4016  1.1  rjs 	.pr_rcvd	= sctp_rcvd,
   4017  1.1  rjs 	.pr_recvoob	= sctp_recvoob,
   4018  1.1  rjs 	.pr_send	= sctp_send,
   4019  1.1  rjs 	.pr_sendoob	= sctp_sendoob,
   4020  1.1  rjs 	.pr_purgeif	= sctp_purgeif,
   4021  1.1  rjs };
   4022