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