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