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