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