xref: /freebsd/sys/netinet/sctp_usrreq.c (revision 74b8d63dcc17c07d8cb21e13f6db517698efd49f)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_pcb.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_var.h>
41 #ifdef INET6
42 #include <netinet6/sctp6_var.h>
43 #endif
44 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/udp.h>
54 
55 
56 
57 extern const struct sctp_cc_functions sctp_cc_functions[];
58 extern const struct sctp_ss_functions sctp_ss_functions[];
59 
60 void
61 sctp_init(void)
62 {
63 	u_long sb_max_adj;
64 
65 	/* Initialize and modify the sysctled variables */
66 	sctp_init_sysctls();
67 	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
68 		SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
69 	/*
70 	 * Allow a user to take no more than 1/2 the number of clusters or
71 	 * the SB_MAX whichever is smaller for the send window.
72 	 */
73 	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
74 	SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
75 	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76 	/*
77 	 * Now for the recv window, should we take the same amount? or
78 	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
79 	 * now I will just copy.
80 	 */
81 	SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
82 	SCTP_BASE_VAR(first_time) = 0;
83 	SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
84 	sctp_pcb_init();
85 #if defined(SCTP_PACKET_LOGGING)
86 	SCTP_BASE_VAR(packet_log_writers) = 0;
87 	SCTP_BASE_VAR(packet_log_end) = 0;
88 	bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
89 #endif
90 }
91 
92 void
93 sctp_finish(void)
94 {
95 	sctp_pcb_finish();
96 }
97 
98 
99 
100 void
101 sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz)
102 {
103 	struct sctp_tmit_chunk *chk;
104 	uint16_t overhead;
105 
106 	/* Adjust that too */
107 	stcb->asoc.smallest_mtu = nxtsz;
108 	/* now off to subtract IP_DF flag if needed */
109 	overhead = IP_HDR_SIZE;
110 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
111 		overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
112 	}
113 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
114 		if ((chk->send_size + overhead) > nxtsz) {
115 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
116 		}
117 	}
118 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
119 		if ((chk->send_size + overhead) > nxtsz) {
120 			/*
121 			 * For this guy we also mark for immediate resend
122 			 * since we sent to big of chunk
123 			 */
124 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
125 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
126 				sctp_flight_size_decrease(chk);
127 				sctp_total_flight_decrease(stcb, chk);
128 				chk->sent = SCTP_DATAGRAM_RESEND;
129 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130 				chk->rec.data.doing_fast_retransmit = 0;
131 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
132 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
133 					    chk->whoTo->flight_size,
134 					    chk->book_size,
135 					    (uint32_t) (uintptr_t) chk->whoTo,
136 					    chk->rec.data.TSN_seq);
137 				}
138 				/* Clear any time so NO RTT is being done */
139 				chk->do_rtt = 0;
140 			}
141 		}
142 	}
143 }
144 
145 #ifdef INET
146 static void
147 sctp_notify_mbuf(struct sctp_inpcb *inp,
148     struct sctp_tcb *stcb,
149     struct sctp_nets *net,
150     struct ip *ip,
151     struct sctphdr *sh)
152 {
153 	struct icmp *icmph;
154 	int totsz, tmr_stopped = 0;
155 	uint16_t nxtsz;
156 
157 	/* protection */
158 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
159 	    (ip == NULL) || (sh == NULL)) {
160 		if (stcb != NULL) {
161 			SCTP_TCB_UNLOCK(stcb);
162 		}
163 		return;
164 	}
165 	/* First job is to verify the vtag matches what I would send */
166 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
167 		SCTP_TCB_UNLOCK(stcb);
168 		return;
169 	}
170 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
171 	    sizeof(struct ip)));
172 	if (icmph->icmp_type != ICMP_UNREACH) {
173 		/* We only care about unreachable */
174 		SCTP_TCB_UNLOCK(stcb);
175 		return;
176 	}
177 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
178 		/* not a unreachable message due to frag. */
179 		SCTP_TCB_UNLOCK(stcb);
180 		return;
181 	}
182 	totsz = ntohs(ip->ip_len);
183 
184 	nxtsz = ntohs(icmph->icmp_nextmtu);
185 	if (nxtsz == 0) {
186 		/*
187 		 * old type router that does not tell us what the next size
188 		 * mtu is. Rats we will have to guess (in a educated fashion
189 		 * of course)
190 		 */
191 		nxtsz = sctp_get_prev_mtu(totsz);
192 	}
193 	/* Stop any PMTU timer */
194 	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
195 		tmr_stopped = 1;
196 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
197 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
198 	}
199 	/* Adjust destination size limit */
200 	if (net->mtu > nxtsz) {
201 		net->mtu = nxtsz;
202 		if (net->port) {
203 			net->mtu -= sizeof(struct udphdr);
204 		}
205 	}
206 	/* now what about the ep? */
207 	if (stcb->asoc.smallest_mtu > nxtsz) {
208 		sctp_pathmtu_adjustment(stcb, nxtsz);
209 	}
210 	if (tmr_stopped)
211 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
212 
213 	SCTP_TCB_UNLOCK(stcb);
214 }
215 
216 void
217 sctp_notify(struct sctp_inpcb *inp,
218     struct ip *ip,
219     struct sctphdr *sh,
220     struct sockaddr *to,
221     struct sctp_tcb *stcb,
222     struct sctp_nets *net)
223 {
224 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
225 	struct socket *so;
226 
227 #endif
228 	struct icmp *icmph;
229 
230 	/* protection */
231 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
232 	    (sh == NULL) || (to == NULL)) {
233 		if (stcb)
234 			SCTP_TCB_UNLOCK(stcb);
235 		return;
236 	}
237 	/* First job is to verify the vtag matches what I would send */
238 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
239 		SCTP_TCB_UNLOCK(stcb);
240 		return;
241 	}
242 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
243 	    sizeof(struct ip)));
244 	if (icmph->icmp_type != ICMP_UNREACH) {
245 		/* We only care about unreachable */
246 		SCTP_TCB_UNLOCK(stcb);
247 		return;
248 	}
249 	if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
250 	    (icmph->icmp_code == ICMP_UNREACH_HOST) ||
251 	    (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
252 	    (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
253 	    (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
254 	    (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
255 	    (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
256 	    (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
257 
258 		/*
259 		 * Hmm reachablity problems we must examine closely. If its
260 		 * not reachable, we may have lost a network. Or if there is
261 		 * NO protocol at the other end named SCTP. well we consider
262 		 * it a OOTB abort.
263 		 */
264 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
265 			/* Ok that destination is NOT reachable */
266 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
267 			net->dest_state &= ~SCTP_ADDR_PF;
268 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
269 			    stcb, 0,
270 			    (void *)net, SCTP_SO_NOT_LOCKED);
271 		}
272 		SCTP_TCB_UNLOCK(stcb);
273 	} else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
274 	    (icmph->icmp_code == ICMP_UNREACH_PORT)) {
275 		/*
276 		 * Here the peer is either playing tricks on us, including
277 		 * an address that belongs to someone who does not support
278 		 * SCTP OR was a userland implementation that shutdown and
279 		 * now is dead. In either case treat it like a OOTB abort
280 		 * with no TCB
281 		 */
282 		sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
283 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
284 		so = SCTP_INP_SO(inp);
285 		atomic_add_int(&stcb->asoc.refcnt, 1);
286 		SCTP_TCB_UNLOCK(stcb);
287 		SCTP_SOCKET_LOCK(so, 1);
288 		SCTP_TCB_LOCK(stcb);
289 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
290 #endif
291 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
292 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
293 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
294 		SCTP_SOCKET_UNLOCK(so, 1);
295 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
296 #endif
297 		/* no need to unlock here, since the TCB is gone */
298 	} else {
299 		SCTP_TCB_UNLOCK(stcb);
300 	}
301 }
302 
303 #endif
304 
305 #ifdef INET
306 void
307 sctp_ctlinput(cmd, sa, vip)
308 	int cmd;
309 	struct sockaddr *sa;
310 	void *vip;
311 {
312 	struct ip *ip = vip;
313 	struct sctphdr *sh;
314 	uint32_t vrf_id;
315 
316 	/* FIX, for non-bsd is this right? */
317 	vrf_id = SCTP_DEFAULT_VRFID;
318 	if (sa->sa_family != AF_INET ||
319 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
320 		return;
321 	}
322 	if (PRC_IS_REDIRECT(cmd)) {
323 		ip = 0;
324 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
325 		return;
326 	}
327 	if (ip) {
328 		struct sctp_inpcb *inp = NULL;
329 		struct sctp_tcb *stcb = NULL;
330 		struct sctp_nets *net = NULL;
331 		struct sockaddr_in to, from;
332 
333 		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
334 		bzero(&to, sizeof(to));
335 		bzero(&from, sizeof(from));
336 		from.sin_family = to.sin_family = AF_INET;
337 		from.sin_len = to.sin_len = sizeof(to);
338 		from.sin_port = sh->src_port;
339 		from.sin_addr = ip->ip_src;
340 		to.sin_port = sh->dest_port;
341 		to.sin_addr = ip->ip_dst;
342 
343 		/*
344 		 * 'to' holds the dest of the packet that failed to be sent.
345 		 * 'from' holds our local endpoint address. Thus we reverse
346 		 * the to and the from in the lookup.
347 		 */
348 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&to,
349 		    (struct sockaddr *)&from,
350 		    &inp, &net, 1, vrf_id);
351 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
352 			if (cmd != PRC_MSGSIZE) {
353 				sctp_notify(inp, ip, sh,
354 				    (struct sockaddr *)&to, stcb,
355 				    net);
356 			} else {
357 				/* handle possible ICMP size messages */
358 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
359 			}
360 		} else {
361 			if ((stcb == NULL) && (inp != NULL)) {
362 				/* reduce ref-count */
363 				SCTP_INP_WLOCK(inp);
364 				SCTP_INP_DECR_REF(inp);
365 				SCTP_INP_WUNLOCK(inp);
366 			}
367 			if (stcb) {
368 				SCTP_TCB_UNLOCK(stcb);
369 			}
370 		}
371 	}
372 	return;
373 }
374 
375 #endif
376 
377 static int
378 sctp_getcred(SYSCTL_HANDLER_ARGS)
379 {
380 	struct xucred xuc;
381 	struct sockaddr_in addrs[2];
382 	struct sctp_inpcb *inp;
383 	struct sctp_nets *net;
384 	struct sctp_tcb *stcb;
385 	int error;
386 	uint32_t vrf_id;
387 
388 	/* FIX, for non-bsd is this right? */
389 	vrf_id = SCTP_DEFAULT_VRFID;
390 
391 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
392 
393 	if (error)
394 		return (error);
395 
396 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
397 	if (error)
398 		return (error);
399 
400 	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[1]),
401 	    sintosa(&addrs[0]),
402 	    &inp, &net, 1, vrf_id);
403 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
404 		if ((inp != NULL) && (stcb == NULL)) {
405 			/* reduce ref-count */
406 			SCTP_INP_WLOCK(inp);
407 			SCTP_INP_DECR_REF(inp);
408 			goto cred_can_cont;
409 		}
410 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
411 		error = ENOENT;
412 		goto out;
413 	}
414 	SCTP_TCB_UNLOCK(stcb);
415 	/*
416 	 * We use the write lock here, only since in the error leg we need
417 	 * it. If we used RLOCK, then we would have to
418 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
419 	 * Better to use higher wlock.
420 	 */
421 	SCTP_INP_WLOCK(inp);
422 cred_can_cont:
423 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
424 	if (error) {
425 		SCTP_INP_WUNLOCK(inp);
426 		goto out;
427 	}
428 	cru2x(inp->sctp_socket->so_cred, &xuc);
429 	SCTP_INP_WUNLOCK(inp);
430 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
431 out:
432 	return (error);
433 }
434 
435 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
436     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
437 
438 
439 #ifdef INET
440 static void
441 sctp_abort(struct socket *so)
442 {
443 	struct sctp_inpcb *inp;
444 	uint32_t flags;
445 
446 	inp = (struct sctp_inpcb *)so->so_pcb;
447 	if (inp == NULL) {
448 		return;
449 	}
450 sctp_must_try_again:
451 	flags = inp->sctp_flags;
452 #ifdef SCTP_LOG_CLOSING
453 	sctp_log_closing(inp, NULL, 17);
454 #endif
455 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
456 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
457 #ifdef SCTP_LOG_CLOSING
458 		sctp_log_closing(inp, NULL, 16);
459 #endif
460 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
461 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
462 		SOCK_LOCK(so);
463 		SCTP_SB_CLEAR(so->so_snd);
464 		/*
465 		 * same for the rcv ones, they are only here for the
466 		 * accounting/select.
467 		 */
468 		SCTP_SB_CLEAR(so->so_rcv);
469 
470 		/* Now null out the reference, we are completely detached. */
471 		so->so_pcb = NULL;
472 		SOCK_UNLOCK(so);
473 	} else {
474 		flags = inp->sctp_flags;
475 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
476 			goto sctp_must_try_again;
477 		}
478 	}
479 	return;
480 }
481 
482 static int
483 sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
484 {
485 	struct sctp_inpcb *inp;
486 	struct inpcb *ip_inp;
487 	int error;
488 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
489 
490 	inp = (struct sctp_inpcb *)so->so_pcb;
491 	if (inp != 0) {
492 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
493 		return (EINVAL);
494 	}
495 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
496 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
497 		if (error) {
498 			return (error);
499 		}
500 	}
501 	error = sctp_inpcb_alloc(so, vrf_id);
502 	if (error) {
503 		return (error);
504 	}
505 	inp = (struct sctp_inpcb *)so->so_pcb;
506 	SCTP_INP_WLOCK(inp);
507 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
508 	ip_inp = &inp->ip_inp.inp;
509 	ip_inp->inp_vflag |= INP_IPV4;
510 	ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
511 	SCTP_INP_WUNLOCK(inp);
512 	return (0);
513 }
514 
515 static int
516 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
517 {
518 	struct sctp_inpcb *inp;
519 
520 	inp = (struct sctp_inpcb *)so->so_pcb;
521 	if (inp == NULL) {
522 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
523 		return (EINVAL);
524 	}
525 	if (addr != NULL) {
526 		if ((addr->sa_family != AF_INET) ||
527 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
528 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
529 			return (EINVAL);
530 		}
531 	}
532 	return (sctp_inpcb_bind(so, addr, NULL, p));
533 }
534 
535 #endif
536 void
537 sctp_close(struct socket *so)
538 {
539 	struct sctp_inpcb *inp;
540 	uint32_t flags;
541 
542 	inp = (struct sctp_inpcb *)so->so_pcb;
543 	if (inp == NULL)
544 		return;
545 
546 	/*
547 	 * Inform all the lower layer assoc that we are done.
548 	 */
549 sctp_must_try_again:
550 	flags = inp->sctp_flags;
551 #ifdef SCTP_LOG_CLOSING
552 	sctp_log_closing(inp, NULL, 17);
553 #endif
554 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
555 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
556 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
557 		    (so->so_rcv.sb_cc > 0)) {
558 #ifdef SCTP_LOG_CLOSING
559 			sctp_log_closing(inp, NULL, 13);
560 #endif
561 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
562 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
563 		} else {
564 #ifdef SCTP_LOG_CLOSING
565 			sctp_log_closing(inp, NULL, 14);
566 #endif
567 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
568 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
569 		}
570 		/*
571 		 * The socket is now detached, no matter what the state of
572 		 * the SCTP association.
573 		 */
574 		SOCK_LOCK(so);
575 		SCTP_SB_CLEAR(so->so_snd);
576 		/*
577 		 * same for the rcv ones, they are only here for the
578 		 * accounting/select.
579 		 */
580 		SCTP_SB_CLEAR(so->so_rcv);
581 
582 		/* Now null out the reference, we are completely detached. */
583 		so->so_pcb = NULL;
584 		SOCK_UNLOCK(so);
585 	} else {
586 		flags = inp->sctp_flags;
587 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
588 			goto sctp_must_try_again;
589 		}
590 	}
591 	return;
592 }
593 
594 
595 int
596 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
597     struct mbuf *control, struct thread *p);
598 
599 
600 int
601 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
602     struct mbuf *control, struct thread *p)
603 {
604 	struct sctp_inpcb *inp;
605 	int error;
606 
607 	inp = (struct sctp_inpcb *)so->so_pcb;
608 	if (inp == NULL) {
609 		if (control) {
610 			sctp_m_freem(control);
611 			control = NULL;
612 		}
613 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
614 		sctp_m_freem(m);
615 		return (EINVAL);
616 	}
617 	/* Got to have an to address if we are NOT a connected socket */
618 	if ((addr == NULL) &&
619 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
620 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))) {
621 		goto connected_type;
622 	} else if (addr == NULL) {
623 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
624 		error = EDESTADDRREQ;
625 		sctp_m_freem(m);
626 		if (control) {
627 			sctp_m_freem(control);
628 			control = NULL;
629 		}
630 		return (error);
631 	}
632 #ifdef INET6
633 	if (addr->sa_family != AF_INET) {
634 		/* must be a v4 address! */
635 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
636 		sctp_m_freem(m);
637 		if (control) {
638 			sctp_m_freem(control);
639 			control = NULL;
640 		}
641 		error = EDESTADDRREQ;
642 		return (error);
643 	}
644 #endif				/* INET6 */
645 connected_type:
646 	/* now what about control */
647 	if (control) {
648 		if (inp->control) {
649 			SCTP_PRINTF("huh? control set?\n");
650 			sctp_m_freem(inp->control);
651 			inp->control = NULL;
652 		}
653 		inp->control = control;
654 	}
655 	/* Place the data */
656 	if (inp->pkt) {
657 		SCTP_BUF_NEXT(inp->pkt_last) = m;
658 		inp->pkt_last = m;
659 	} else {
660 		inp->pkt_last = inp->pkt = m;
661 	}
662 	if (
663 	/* FreeBSD uses a flag passed */
664 	    ((flags & PRUS_MORETOCOME) == 0)
665 	    ) {
666 		/*
667 		 * note with the current version this code will only be used
668 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
669 		 * re-defining sosend to use the sctp_sosend. One can
670 		 * optionally switch back to this code (by changing back the
671 		 * definitions) but this is not advisable. This code is used
672 		 * by FreeBSD when sending a file with sendfile() though.
673 		 */
674 		int ret;
675 
676 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
677 		inp->pkt = NULL;
678 		inp->control = NULL;
679 		return (ret);
680 	} else {
681 		return (0);
682 	}
683 }
684 
685 int
686 sctp_disconnect(struct socket *so)
687 {
688 	struct sctp_inpcb *inp;
689 
690 	inp = (struct sctp_inpcb *)so->so_pcb;
691 	if (inp == NULL) {
692 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
693 		return (ENOTCONN);
694 	}
695 	SCTP_INP_RLOCK(inp);
696 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
697 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
698 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
699 			/* No connection */
700 			SCTP_INP_RUNLOCK(inp);
701 			return (0);
702 		} else {
703 			struct sctp_association *asoc;
704 			struct sctp_tcb *stcb;
705 
706 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
707 			if (stcb == NULL) {
708 				SCTP_INP_RUNLOCK(inp);
709 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
710 				return (EINVAL);
711 			}
712 			SCTP_TCB_LOCK(stcb);
713 			asoc = &stcb->asoc;
714 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
715 				/* We are about to be freed, out of here */
716 				SCTP_TCB_UNLOCK(stcb);
717 				SCTP_INP_RUNLOCK(inp);
718 				return (0);
719 			}
720 			if (((so->so_options & SO_LINGER) &&
721 			    (so->so_linger == 0)) ||
722 			    (so->so_rcv.sb_cc > 0)) {
723 				if (SCTP_GET_STATE(asoc) !=
724 				    SCTP_STATE_COOKIE_WAIT) {
725 					/* Left with Data unread */
726 					struct mbuf *err;
727 
728 					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
729 					if (err) {
730 						/*
731 						 * Fill in the user
732 						 * initiated abort
733 						 */
734 						struct sctp_paramhdr *ph;
735 
736 						ph = mtod(err, struct sctp_paramhdr *);
737 						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
738 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
739 						ph->param_length = htons(SCTP_BUF_LEN(err));
740 					}
741 					sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
742 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
743 				}
744 				SCTP_INP_RUNLOCK(inp);
745 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
746 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
747 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
748 				}
749 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
750 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
751 				/* No unlock tcb assoc is gone */
752 				return (0);
753 			}
754 			if (TAILQ_EMPTY(&asoc->send_queue) &&
755 			    TAILQ_EMPTY(&asoc->sent_queue) &&
756 			    (asoc->stream_queue_cnt == 0)) {
757 				/* there is nothing queued to send, so done */
758 				if (asoc->locked_on_sending) {
759 					goto abort_anyway;
760 				}
761 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
762 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
763 					/* only send SHUTDOWN 1st time thru */
764 					struct sctp_nets *netp;
765 
766 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
767 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
768 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
769 					}
770 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
771 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
772 					sctp_stop_timers_for_shutdown(stcb);
773 					if (stcb->asoc.alternate) {
774 						netp = stcb->asoc.alternate;
775 					} else {
776 						netp = stcb->asoc.primary_destination;
777 					}
778 					sctp_send_shutdown(stcb, netp);
779 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
780 					    stcb->sctp_ep, stcb, netp);
781 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
782 					    stcb->sctp_ep, stcb, netp);
783 					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
784 				}
785 			} else {
786 				/*
787 				 * we still got (or just got) data to send,
788 				 * so set SHUTDOWN_PENDING
789 				 */
790 				/*
791 				 * XXX sockets draft says that SCTP_EOF
792 				 * should be sent with no data. currently,
793 				 * we will allow user data to be sent first
794 				 * and move to SHUTDOWN-PENDING
795 				 */
796 				struct sctp_nets *netp;
797 
798 				if (stcb->asoc.alternate) {
799 					netp = stcb->asoc.alternate;
800 				} else {
801 					netp = stcb->asoc.primary_destination;
802 				}
803 
804 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
805 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
806 				    netp);
807 				if (asoc->locked_on_sending) {
808 					/* Locked to send out the data */
809 					struct sctp_stream_queue_pending *sp;
810 
811 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
812 					if (sp == NULL) {
813 						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
814 						    asoc->locked_on_sending->stream_no);
815 					} else {
816 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
817 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
818 					}
819 				}
820 				if (TAILQ_EMPTY(&asoc->send_queue) &&
821 				    TAILQ_EMPTY(&asoc->sent_queue) &&
822 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
823 					struct mbuf *op_err;
824 
825 			abort_anyway:
826 					op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
827 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
828 					sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
829 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
830 					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
831 					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
832 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
833 					}
834 					SCTP_INP_RUNLOCK(inp);
835 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
836 					    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
837 					return (0);
838 				} else {
839 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
840 				}
841 			}
842 			soisdisconnecting(so);
843 			SCTP_TCB_UNLOCK(stcb);
844 			SCTP_INP_RUNLOCK(inp);
845 			return (0);
846 		}
847 		/* not reached */
848 	} else {
849 		/* UDP model does not support this */
850 		SCTP_INP_RUNLOCK(inp);
851 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
852 		return (EOPNOTSUPP);
853 	}
854 }
855 
856 int
857 sctp_flush(struct socket *so, int how)
858 {
859 	/*
860 	 * We will just clear out the values and let subsequent close clear
861 	 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
862 	 * they will not be able to read the data, the socket will block
863 	 * that from happening.
864 	 */
865 	struct sctp_inpcb *inp;
866 
867 	inp = (struct sctp_inpcb *)so->so_pcb;
868 	if (inp == NULL) {
869 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
870 		return (EINVAL);
871 	}
872 	SCTP_INP_RLOCK(inp);
873 	/* For the 1 to many model this does nothing */
874 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
875 		SCTP_INP_RUNLOCK(inp);
876 		return (0);
877 	}
878 	SCTP_INP_RUNLOCK(inp);
879 	if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
880 		/*
881 		 * First make sure the sb will be happy, we don't use these
882 		 * except maybe the count
883 		 */
884 		SCTP_INP_WLOCK(inp);
885 		SCTP_INP_READ_LOCK(inp);
886 		inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
887 		SCTP_INP_READ_UNLOCK(inp);
888 		SCTP_INP_WUNLOCK(inp);
889 		so->so_rcv.sb_cc = 0;
890 		so->so_rcv.sb_mbcnt = 0;
891 		so->so_rcv.sb_mb = NULL;
892 	}
893 	if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
894 		/*
895 		 * First make sure the sb will be happy, we don't use these
896 		 * except maybe the count
897 		 */
898 		so->so_snd.sb_cc = 0;
899 		so->so_snd.sb_mbcnt = 0;
900 		so->so_snd.sb_mb = NULL;
901 
902 	}
903 	return (0);
904 }
905 
906 int
907 sctp_shutdown(struct socket *so)
908 {
909 	struct sctp_inpcb *inp;
910 
911 	inp = (struct sctp_inpcb *)so->so_pcb;
912 	if (inp == NULL) {
913 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
914 		return (EINVAL);
915 	}
916 	SCTP_INP_RLOCK(inp);
917 	/* For UDP model this is a invalid call */
918 	if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
919 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
920 		/* Restore the flags that the soshutdown took away. */
921 		SOCKBUF_LOCK(&so->so_rcv);
922 		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
923 		SOCKBUF_UNLOCK(&so->so_rcv);
924 		/* This proc will wakeup for read and do nothing (I hope) */
925 		SCTP_INP_RUNLOCK(inp);
926 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
927 		return (EOPNOTSUPP);
928 	} else {
929 		/*
930 		 * Ok, if we reach here its the TCP model and it is either a
931 		 * SHUT_WR or SHUT_RDWR. This means we put the shutdown flag
932 		 * against it.
933 		 */
934 		struct sctp_tcb *stcb;
935 		struct sctp_association *asoc;
936 		struct sctp_nets *netp;
937 
938 		if ((so->so_state &
939 		    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
940 			SCTP_INP_RUNLOCK(inp);
941 			return (ENOTCONN);
942 		}
943 		socantsendmore(so);
944 
945 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
946 		if (stcb == NULL) {
947 			/*
948 			 * Ok, we hit the case that the shutdown call was
949 			 * made after an abort or something. Nothing to do
950 			 * now.
951 			 */
952 			SCTP_INP_RUNLOCK(inp);
953 			return (0);
954 		}
955 		SCTP_TCB_LOCK(stcb);
956 		asoc = &stcb->asoc;
957 		if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
958 			SCTP_TCB_UNLOCK(stcb);
959 			SCTP_INP_RUNLOCK(inp);
960 			return (0);
961 		}
962 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
963 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED) &&
964 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN)) {
965 			/*
966 			 * If we are not in or before ESTABLISHED, there is
967 			 * no protocol action required.
968 			 */
969 			SCTP_TCB_UNLOCK(stcb);
970 			SCTP_INP_RUNLOCK(inp);
971 			return (0);
972 		}
973 		if (stcb->asoc.alternate) {
974 			netp = stcb->asoc.alternate;
975 		} else {
976 			netp = stcb->asoc.primary_destination;
977 		}
978 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
979 		    TAILQ_EMPTY(&asoc->send_queue) &&
980 		    TAILQ_EMPTY(&asoc->sent_queue) &&
981 		    (asoc->stream_queue_cnt == 0)) {
982 			if (asoc->locked_on_sending) {
983 				goto abort_anyway;
984 			}
985 			/* there is nothing queued to send, so I'm done... */
986 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
987 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
988 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
989 			sctp_stop_timers_for_shutdown(stcb);
990 			sctp_send_shutdown(stcb, netp);
991 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
992 			    stcb->sctp_ep, stcb, netp);
993 		} else {
994 			/*
995 			 * We still got (or just got) data to send, so set
996 			 * SHUTDOWN_PENDING.
997 			 */
998 			SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
999 			if (asoc->locked_on_sending) {
1000 				/* Locked to send out the data */
1001 				struct sctp_stream_queue_pending *sp;
1002 
1003 				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1004 				if (sp == NULL) {
1005 					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1006 					    asoc->locked_on_sending->stream_no);
1007 				} else {
1008 					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1009 						SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT);
1010 					}
1011 				}
1012 			}
1013 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1014 			    TAILQ_EMPTY(&asoc->sent_queue) &&
1015 			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1016 				struct mbuf *op_err;
1017 
1018 		abort_anyway:
1019 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
1020 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1021 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1022 				    op_err, SCTP_SO_LOCKED);
1023 				SCTP_INP_RUNLOCK(inp);
1024 				return (0);
1025 			}
1026 		}
1027 		sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, netp);
1028 		/*
1029 		 * XXX: Why do this in the case where we have still data
1030 		 * queued?
1031 		 */
1032 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1033 		SCTP_TCB_UNLOCK(stcb);
1034 		SCTP_INP_RUNLOCK(inp);
1035 		return (0);
1036 	}
1037 }
1038 
1039 /*
1040  * copies a "user" presentable address and removes embedded scope, etc.
1041  * returns 0 on success, 1 on error
1042  */
1043 static uint32_t
1044 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1045 {
1046 #ifdef INET6
1047 	struct sockaddr_in6 lsa6;
1048 
1049 	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1050 	    &lsa6);
1051 #endif
1052 	memcpy(ss, sa, sa->sa_len);
1053 	return (0);
1054 }
1055 
1056 
1057 
1058 /*
1059  * NOTE: assumes addr lock is held
1060  */
1061 static size_t
1062 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1063     struct sctp_tcb *stcb,
1064     size_t limit,
1065     struct sockaddr_storage *sas,
1066     uint32_t vrf_id)
1067 {
1068 	struct sctp_ifn *sctp_ifn;
1069 	struct sctp_ifa *sctp_ifa;
1070 	size_t actual;
1071 	int loopback_scope;
1072 
1073 #if defined(INET)
1074 	int ipv4_local_scope, ipv4_addr_legal;
1075 
1076 #endif
1077 #if defined(INET6)
1078 	int local_scope, site_scope, ipv6_addr_legal;
1079 
1080 #endif
1081 	struct sctp_vrf *vrf;
1082 
1083 	actual = 0;
1084 	if (limit <= 0)
1085 		return (actual);
1086 
1087 	if (stcb) {
1088 		/* Turn on all the appropriate scope */
1089 		loopback_scope = stcb->asoc.scope.loopback_scope;
1090 #if defined(INET)
1091 		ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
1092 		ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
1093 #endif
1094 #if defined(INET6)
1095 		local_scope = stcb->asoc.scope.local_scope;
1096 		site_scope = stcb->asoc.scope.site_scope;
1097 		ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
1098 #endif
1099 	} else {
1100 		/* Use generic values for endpoints. */
1101 		loopback_scope = 1;
1102 #if defined(INET)
1103 		ipv4_local_scope = 1;
1104 #endif
1105 #if defined(INET6)
1106 		local_scope = 1;
1107 		site_scope = 1;
1108 #endif
1109 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1110 #if defined(INET6)
1111 			ipv6_addr_legal = 1;
1112 #endif
1113 #if defined(INET)
1114 			if (SCTP_IPV6_V6ONLY(inp)) {
1115 				ipv4_addr_legal = 0;
1116 			} else {
1117 				ipv4_addr_legal = 1;
1118 			}
1119 #endif
1120 		} else {
1121 #if defined(INET6)
1122 			ipv6_addr_legal = 0;
1123 #endif
1124 #if defined(INET)
1125 			ipv4_addr_legal = 1;
1126 #endif
1127 		}
1128 	}
1129 	vrf = sctp_find_vrf(vrf_id);
1130 	if (vrf == NULL) {
1131 		return (0);
1132 	}
1133 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1134 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1135 			if ((loopback_scope == 0) &&
1136 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1137 				/* Skip loopback if loopback_scope not set */
1138 				continue;
1139 			}
1140 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1141 				if (stcb) {
1142 					/*
1143 					 * For the BOUND-ALL case, the list
1144 					 * associated with a TCB is Always
1145 					 * considered a reverse list.. i.e.
1146 					 * it lists addresses that are NOT
1147 					 * part of the association. If this
1148 					 * is one of those we must skip it.
1149 					 */
1150 					if (sctp_is_addr_restricted(stcb,
1151 					    sctp_ifa)) {
1152 						continue;
1153 					}
1154 				}
1155 				switch (sctp_ifa->address.sa.sa_family) {
1156 #ifdef INET
1157 				case AF_INET:
1158 					if (ipv4_addr_legal) {
1159 						struct sockaddr_in *sin;
1160 
1161 						sin = &sctp_ifa->address.sin;
1162 						if (sin->sin_addr.s_addr == 0) {
1163 							/*
1164 							 * we skip
1165 							 * unspecifed
1166 							 * addresses
1167 							 */
1168 							continue;
1169 						}
1170 						if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1171 						    &sin->sin_addr) != 0) {
1172 							continue;
1173 						}
1174 						if ((ipv4_local_scope == 0) &&
1175 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1176 							continue;
1177 						}
1178 #ifdef INET6
1179 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1180 							in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1181 							((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1182 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1183 							actual += sizeof(struct sockaddr_in6);
1184 						} else {
1185 #endif
1186 							memcpy(sas, sin, sizeof(*sin));
1187 							((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1188 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1189 							actual += sizeof(*sin);
1190 #ifdef INET6
1191 						}
1192 #endif
1193 						if (actual >= limit) {
1194 							return (actual);
1195 						}
1196 					} else {
1197 						continue;
1198 					}
1199 					break;
1200 #endif
1201 #ifdef INET6
1202 				case AF_INET6:
1203 					if (ipv6_addr_legal) {
1204 						struct sockaddr_in6 *sin6;
1205 
1206 						sin6 = &sctp_ifa->address.sin6;
1207 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1208 							/*
1209 							 * we skip
1210 							 * unspecifed
1211 							 * addresses
1212 							 */
1213 							continue;
1214 						}
1215 						if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1216 						    &sin6->sin6_addr) != 0) {
1217 							continue;
1218 						}
1219 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1220 							if (local_scope == 0)
1221 								continue;
1222 							if (sin6->sin6_scope_id == 0) {
1223 								if (sa6_recoverscope(sin6) != 0)
1224 									/*
1225 									 *
1226 									 * bad
1227 									 *
1228 									 * li
1229 									 * nk
1230 									 *
1231 									 * loc
1232 									 * al
1233 									 *
1234 									 * add
1235 									 * re
1236 									 * ss
1237 									 * */
1238 									continue;
1239 							}
1240 						}
1241 						if ((site_scope == 0) &&
1242 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1243 							continue;
1244 						}
1245 						memcpy(sas, sin6, sizeof(*sin6));
1246 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1247 						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1248 						actual += sizeof(*sin6);
1249 						if (actual >= limit) {
1250 							return (actual);
1251 						}
1252 					} else {
1253 						continue;
1254 					}
1255 					break;
1256 #endif
1257 				default:
1258 					/* TSNH */
1259 					break;
1260 				}
1261 			}
1262 		}
1263 	} else {
1264 		struct sctp_laddr *laddr;
1265 
1266 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1267 			if (stcb) {
1268 				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1269 					continue;
1270 				}
1271 			}
1272 			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1273 				continue;
1274 			switch (laddr->ifa->address.sa.sa_family) {
1275 #ifdef INET
1276 			case AF_INET:
1277 				((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1278 				break;
1279 #endif
1280 #ifdef INET6
1281 			case AF_INET6:
1282 				((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1283 				break;
1284 #endif
1285 			default:
1286 				/* TSNH */
1287 				break;
1288 			}
1289 			sas = (struct sockaddr_storage *)((caddr_t)sas +
1290 			    laddr->ifa->address.sa.sa_len);
1291 			actual += laddr->ifa->address.sa.sa_len;
1292 			if (actual >= limit) {
1293 				return (actual);
1294 			}
1295 		}
1296 	}
1297 	return (actual);
1298 }
1299 
1300 static size_t
1301 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1302     struct sctp_tcb *stcb,
1303     size_t limit,
1304     struct sockaddr_storage *sas)
1305 {
1306 	size_t size = 0;
1307 
1308 	SCTP_IPI_ADDR_RLOCK();
1309 	/* fill up addresses for the endpoint's default vrf */
1310 	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1311 	    inp->def_vrf_id);
1312 	SCTP_IPI_ADDR_RUNLOCK();
1313 	return (size);
1314 }
1315 
1316 /*
1317  * NOTE: assumes addr lock is held
1318  */
1319 static int
1320 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1321 {
1322 	int cnt = 0;
1323 	struct sctp_vrf *vrf = NULL;
1324 
1325 	/*
1326 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1327 	 * number of addresses that you COULD get. In reality the sub-set
1328 	 * bound may have an exclusion list for a given TCB OR in the
1329 	 * bound-all case a TCB may NOT include the loopback or other
1330 	 * addresses as well.
1331 	 */
1332 	vrf = sctp_find_vrf(vrf_id);
1333 	if (vrf == NULL) {
1334 		return (0);
1335 	}
1336 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1337 		struct sctp_ifn *sctp_ifn;
1338 		struct sctp_ifa *sctp_ifa;
1339 
1340 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1341 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1342 				/* Count them if they are the right type */
1343 				switch (sctp_ifa->address.sa.sa_family) {
1344 #ifdef INET
1345 				case AF_INET:
1346 #ifdef INET6
1347 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1348 						cnt += sizeof(struct sockaddr_in6);
1349 					else
1350 						cnt += sizeof(struct sockaddr_in);
1351 #else
1352 					cnt += sizeof(struct sockaddr_in);
1353 #endif
1354 					break;
1355 #endif
1356 #ifdef INET6
1357 				case AF_INET6:
1358 					cnt += sizeof(struct sockaddr_in6);
1359 					break;
1360 #endif
1361 				default:
1362 					break;
1363 				}
1364 			}
1365 		}
1366 	} else {
1367 		struct sctp_laddr *laddr;
1368 
1369 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1370 			switch (laddr->ifa->address.sa.sa_family) {
1371 #ifdef INET
1372 			case AF_INET:
1373 #ifdef INET6
1374 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1375 					cnt += sizeof(struct sockaddr_in6);
1376 				else
1377 					cnt += sizeof(struct sockaddr_in);
1378 #else
1379 				cnt += sizeof(struct sockaddr_in);
1380 #endif
1381 				break;
1382 #endif
1383 #ifdef INET6
1384 			case AF_INET6:
1385 				cnt += sizeof(struct sockaddr_in6);
1386 				break;
1387 #endif
1388 			default:
1389 				break;
1390 			}
1391 		}
1392 	}
1393 	return (cnt);
1394 }
1395 
1396 static int
1397 sctp_count_max_addresses(struct sctp_inpcb *inp)
1398 {
1399 	int cnt = 0;
1400 
1401 	SCTP_IPI_ADDR_RLOCK();
1402 	/* count addresses for the endpoint's default VRF */
1403 	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1404 	SCTP_IPI_ADDR_RUNLOCK();
1405 	return (cnt);
1406 }
1407 
1408 static int
1409 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1410     size_t optsize, void *p, int delay)
1411 {
1412 	int error = 0;
1413 	int creat_lock_on = 0;
1414 	struct sctp_tcb *stcb = NULL;
1415 	struct sockaddr *sa;
1416 	unsigned int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1417 	uint32_t vrf_id;
1418 	int bad_addresses = 0;
1419 	sctp_assoc_t *a_id;
1420 
1421 	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1422 
1423 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1424 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1425 		/* We are already connected AND the TCP model */
1426 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1427 		return (EADDRINUSE);
1428 	}
1429 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1430 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1431 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1432 		return (EINVAL);
1433 	}
1434 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1435 		SCTP_INP_RLOCK(inp);
1436 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1437 		SCTP_INP_RUNLOCK(inp);
1438 	}
1439 	if (stcb) {
1440 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1441 		return (EALREADY);
1442 	}
1443 	SCTP_INP_INCR_REF(inp);
1444 	SCTP_ASOC_CREATE_LOCK(inp);
1445 	creat_lock_on = 1;
1446 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1447 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1448 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1449 		error = EFAULT;
1450 		goto out_now;
1451 	}
1452 	totaddrp = (unsigned int *)optval;
1453 	totaddr = *totaddrp;
1454 	sa = (struct sockaddr *)(totaddrp + 1);
1455 	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (unsigned int)(optsize - sizeof(int)), &bad_addresses);
1456 	if ((stcb != NULL) || bad_addresses) {
1457 		/* Already have or am bring up an association */
1458 		SCTP_ASOC_CREATE_UNLOCK(inp);
1459 		creat_lock_on = 0;
1460 		if (stcb)
1461 			SCTP_TCB_UNLOCK(stcb);
1462 		if (bad_addresses == 0) {
1463 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1464 			error = EALREADY;
1465 		}
1466 		goto out_now;
1467 	}
1468 #ifdef INET6
1469 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1470 	    (num_v6 > 0)) {
1471 		error = EINVAL;
1472 		goto out_now;
1473 	}
1474 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1475 	    (num_v4 > 0)) {
1476 		struct in6pcb *inp6;
1477 
1478 		inp6 = (struct in6pcb *)inp;
1479 		if (SCTP_IPV6_V6ONLY(inp6)) {
1480 			/*
1481 			 * if IPV6_V6ONLY flag, ignore connections destined
1482 			 * to a v4 addr or v4-mapped addr
1483 			 */
1484 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1485 			error = EINVAL;
1486 			goto out_now;
1487 		}
1488 	}
1489 #endif				/* INET6 */
1490 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1491 	    SCTP_PCB_FLAGS_UNBOUND) {
1492 		/* Bind a ephemeral port */
1493 		error = sctp_inpcb_bind(so, NULL, NULL, p);
1494 		if (error) {
1495 			goto out_now;
1496 		}
1497 	}
1498 	/* FIX ME: do we want to pass in a vrf on the connect call? */
1499 	vrf_id = inp->def_vrf_id;
1500 
1501 
1502 	/* We are GOOD to go */
1503 	stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1504 	    inp->sctp_ep.pre_open_stream_count,
1505 	    (struct thread *)p
1506 	    );
1507 	if (stcb == NULL) {
1508 		/* Gak! no memory */
1509 		goto out_now;
1510 	}
1511 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1512 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1513 		/* Set the connected flag so we can queue data */
1514 		soisconnecting(so);
1515 	}
1516 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1517 	/* move to second address */
1518 	switch (sa->sa_family) {
1519 #ifdef INET
1520 	case AF_INET:
1521 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1522 		break;
1523 #endif
1524 #ifdef INET6
1525 	case AF_INET6:
1526 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1527 		break;
1528 #endif
1529 	default:
1530 		break;
1531 	}
1532 
1533 	error = 0;
1534 	sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1535 	/* Fill in the return id */
1536 	if (error) {
1537 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
1538 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1539 		goto out_now;
1540 	}
1541 	a_id = (sctp_assoc_t *) optval;
1542 	*a_id = sctp_get_associd(stcb);
1543 
1544 	/* initialize authentication parameters for the assoc */
1545 	sctp_initialize_auth_params(inp, stcb);
1546 
1547 	if (delay) {
1548 		/* doing delayed connection */
1549 		stcb->asoc.delayed_connection = 1;
1550 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1551 	} else {
1552 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1553 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1554 	}
1555 	SCTP_TCB_UNLOCK(stcb);
1556 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1557 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1558 		/* Set the connected flag so we can queue data */
1559 		soisconnecting(so);
1560 	}
1561 out_now:
1562 	if (creat_lock_on) {
1563 		SCTP_ASOC_CREATE_UNLOCK(inp);
1564 	}
1565 	SCTP_INP_DECR_REF(inp);
1566 	return (error);
1567 }
1568 
1569 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1570 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1571 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1572 		SCTP_INP_RLOCK(inp); \
1573 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1574 		if (stcb) { \
1575 			SCTP_TCB_LOCK(stcb); \
1576                 } \
1577 		SCTP_INP_RUNLOCK(inp); \
1578 	} else if (assoc_id > SCTP_ALL_ASSOC) { \
1579 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1580 		if (stcb == NULL) { \
1581 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1582 			error = ENOENT; \
1583 			break; \
1584 		} \
1585 	} else { \
1586 		stcb = NULL; \
1587         } \
1588   }
1589 
1590 
1591 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1592 	if (size < sizeof(type)) { \
1593 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1594 		error = EINVAL; \
1595 		break; \
1596 	} else { \
1597 		destp = (type *)srcp; \
1598 	} \
1599       }
1600 
1601 static int
1602 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1603     void *p)
1604 {
1605 	struct sctp_inpcb *inp = NULL;
1606 	int error, val = 0;
1607 	struct sctp_tcb *stcb = NULL;
1608 
1609 	if (optval == NULL) {
1610 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1611 		return (EINVAL);
1612 	}
1613 	inp = (struct sctp_inpcb *)so->so_pcb;
1614 	if (inp == NULL) {
1615 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1616 		return EINVAL;
1617 	}
1618 	error = 0;
1619 
1620 	switch (optname) {
1621 	case SCTP_NODELAY:
1622 	case SCTP_AUTOCLOSE:
1623 	case SCTP_EXPLICIT_EOR:
1624 	case SCTP_AUTO_ASCONF:
1625 	case SCTP_DISABLE_FRAGMENTS:
1626 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1627 	case SCTP_USE_EXT_RCVINFO:
1628 		SCTP_INP_RLOCK(inp);
1629 		switch (optname) {
1630 		case SCTP_DISABLE_FRAGMENTS:
1631 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1632 			break;
1633 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1634 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1635 			break;
1636 		case SCTP_AUTO_ASCONF:
1637 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1638 				/* only valid for bound all sockets */
1639 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1640 			} else {
1641 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1642 				error = EINVAL;
1643 				goto flags_out;
1644 			}
1645 			break;
1646 		case SCTP_EXPLICIT_EOR:
1647 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1648 			break;
1649 		case SCTP_NODELAY:
1650 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1651 			break;
1652 		case SCTP_USE_EXT_RCVINFO:
1653 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1654 			break;
1655 		case SCTP_AUTOCLOSE:
1656 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1657 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1658 			else
1659 				val = 0;
1660 			break;
1661 
1662 		default:
1663 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1664 			error = ENOPROTOOPT;
1665 		}		/* end switch (sopt->sopt_name) */
1666 		if (*optsize < sizeof(val)) {
1667 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1668 			error = EINVAL;
1669 		}
1670 flags_out:
1671 		SCTP_INP_RUNLOCK(inp);
1672 		if (error == 0) {
1673 			/* return the option value */
1674 			*(int *)optval = val;
1675 			*optsize = sizeof(val);
1676 		}
1677 		break;
1678 	case SCTP_GET_PACKET_LOG:
1679 		{
1680 #ifdef  SCTP_PACKET_LOGGING
1681 			uint8_t *target;
1682 			int ret;
1683 
1684 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1685 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1686 			*optsize = ret;
1687 #else
1688 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1689 			error = EOPNOTSUPP;
1690 #endif
1691 			break;
1692 		}
1693 	case SCTP_REUSE_PORT:
1694 		{
1695 			uint32_t *value;
1696 
1697 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1698 				/* Can't do this for a 1-m socket */
1699 				error = EINVAL;
1700 				break;
1701 			}
1702 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1703 			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1704 			*optsize = sizeof(uint32_t);
1705 			break;
1706 		}
1707 	case SCTP_PARTIAL_DELIVERY_POINT:
1708 		{
1709 			uint32_t *value;
1710 
1711 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1712 			*value = inp->partial_delivery_point;
1713 			*optsize = sizeof(uint32_t);
1714 			break;
1715 		}
1716 	case SCTP_FRAGMENT_INTERLEAVE:
1717 		{
1718 			uint32_t *value;
1719 
1720 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1721 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1722 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1723 					*value = SCTP_FRAG_LEVEL_2;
1724 				} else {
1725 					*value = SCTP_FRAG_LEVEL_1;
1726 				}
1727 			} else {
1728 				*value = SCTP_FRAG_LEVEL_0;
1729 			}
1730 			*optsize = sizeof(uint32_t);
1731 			break;
1732 		}
1733 	case SCTP_INTERLEAVING_SUPPORTED:
1734 		{
1735 			struct sctp_assoc_value *av;
1736 
1737 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1738 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1739 
1740 			if (stcb) {
1741 				av->assoc_value = stcb->asoc.idata_supported;
1742 				SCTP_TCB_UNLOCK(stcb);
1743 			} else {
1744 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1745 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1746 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1747 					SCTP_INP_RLOCK(inp);
1748 					if (inp->idata_supported) {
1749 						av->assoc_value = 1;
1750 					} else {
1751 						av->assoc_value = 0;
1752 					}
1753 					SCTP_INP_RUNLOCK(inp);
1754 				} else {
1755 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1756 					error = EINVAL;
1757 				}
1758 			}
1759 			if (error == 0) {
1760 				*optsize = sizeof(struct sctp_assoc_value);
1761 			}
1762 			break;
1763 		}
1764 	case SCTP_CMT_ON_OFF:
1765 		{
1766 			struct sctp_assoc_value *av;
1767 
1768 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1769 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1770 			if (stcb) {
1771 				av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1772 				SCTP_TCB_UNLOCK(stcb);
1773 			} else {
1774 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1775 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1776 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1777 					SCTP_INP_RLOCK(inp);
1778 					av->assoc_value = inp->sctp_cmt_on_off;
1779 					SCTP_INP_RUNLOCK(inp);
1780 				} else {
1781 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1782 					error = EINVAL;
1783 				}
1784 			}
1785 			if (error == 0) {
1786 				*optsize = sizeof(struct sctp_assoc_value);
1787 			}
1788 			break;
1789 		}
1790 	case SCTP_PLUGGABLE_CC:
1791 		{
1792 			struct sctp_assoc_value *av;
1793 
1794 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1795 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1796 			if (stcb) {
1797 				av->assoc_value = stcb->asoc.congestion_control_module;
1798 				SCTP_TCB_UNLOCK(stcb);
1799 			} else {
1800 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1801 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1802 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1803 					SCTP_INP_RLOCK(inp);
1804 					av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1805 					SCTP_INP_RUNLOCK(inp);
1806 				} else {
1807 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1808 					error = EINVAL;
1809 				}
1810 			}
1811 			if (error == 0) {
1812 				*optsize = sizeof(struct sctp_assoc_value);
1813 			}
1814 			break;
1815 		}
1816 	case SCTP_CC_OPTION:
1817 		{
1818 			struct sctp_cc_option *cc_opt;
1819 
1820 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1821 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1822 			if (stcb == NULL) {
1823 				error = EINVAL;
1824 			} else {
1825 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1826 					error = ENOTSUP;
1827 				} else {
1828 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1829 					*optsize = sizeof(struct sctp_cc_option);
1830 				}
1831 				SCTP_TCB_UNLOCK(stcb);
1832 			}
1833 			break;
1834 		}
1835 	case SCTP_PLUGGABLE_SS:
1836 		{
1837 			struct sctp_assoc_value *av;
1838 
1839 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1840 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1841 			if (stcb) {
1842 				av->assoc_value = stcb->asoc.stream_scheduling_module;
1843 				SCTP_TCB_UNLOCK(stcb);
1844 			} else {
1845 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1846 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1847 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1848 					SCTP_INP_RLOCK(inp);
1849 					av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1850 					SCTP_INP_RUNLOCK(inp);
1851 				} else {
1852 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1853 					error = EINVAL;
1854 				}
1855 			}
1856 			if (error == 0) {
1857 				*optsize = sizeof(struct sctp_assoc_value);
1858 			}
1859 			break;
1860 		}
1861 	case SCTP_SS_VALUE:
1862 		{
1863 			struct sctp_stream_value *av;
1864 
1865 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1866 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1867 			if (stcb) {
1868 				if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
1869 				    (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1870 				    &av->stream_value) < 0)) {
1871 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1872 					error = EINVAL;
1873 				} else {
1874 					*optsize = sizeof(struct sctp_stream_value);
1875 				}
1876 				SCTP_TCB_UNLOCK(stcb);
1877 			} else {
1878 				/*
1879 				 * Can't get stream value without
1880 				 * association
1881 				 */
1882 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1883 				error = EINVAL;
1884 			}
1885 			break;
1886 		}
1887 	case SCTP_GET_ADDR_LEN:
1888 		{
1889 			struct sctp_assoc_value *av;
1890 
1891 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1892 			error = EINVAL;
1893 #ifdef INET
1894 			if (av->assoc_value == AF_INET) {
1895 				av->assoc_value = sizeof(struct sockaddr_in);
1896 				error = 0;
1897 			}
1898 #endif
1899 #ifdef INET6
1900 			if (av->assoc_value == AF_INET6) {
1901 				av->assoc_value = sizeof(struct sockaddr_in6);
1902 				error = 0;
1903 			}
1904 #endif
1905 			if (error) {
1906 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1907 			} else {
1908 				*optsize = sizeof(struct sctp_assoc_value);
1909 			}
1910 			break;
1911 		}
1912 	case SCTP_GET_ASSOC_NUMBER:
1913 		{
1914 			uint32_t *value, cnt;
1915 
1916 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1917 			SCTP_INP_RLOCK(inp);
1918 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1919 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1920 				/* Can't do this for a 1-1 socket */
1921 				error = EINVAL;
1922 				SCTP_INP_RUNLOCK(inp);
1923 				break;
1924 			}
1925 			cnt = 0;
1926 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1927 				cnt++;
1928 			}
1929 			SCTP_INP_RUNLOCK(inp);
1930 			*value = cnt;
1931 			*optsize = sizeof(uint32_t);
1932 			break;
1933 		}
1934 	case SCTP_GET_ASSOC_ID_LIST:
1935 		{
1936 			struct sctp_assoc_ids *ids;
1937 			uint32_t at;
1938 			size_t limit;
1939 
1940 			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1941 			SCTP_INP_RLOCK(inp);
1942 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1943 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1944 				/* Can't do this for a 1-1 socket */
1945 				error = EINVAL;
1946 				SCTP_INP_RUNLOCK(inp);
1947 				break;
1948 			}
1949 			at = 0;
1950 			limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1951 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1952 				if (at < limit) {
1953 					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1954 					if (at == 0) {
1955 						error = EINVAL;
1956 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1957 						break;
1958 					}
1959 				} else {
1960 					error = EINVAL;
1961 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1962 					break;
1963 				}
1964 			}
1965 			SCTP_INP_RUNLOCK(inp);
1966 			if (error == 0) {
1967 				ids->gaids_number_of_ids = at;
1968 				*optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1969 			}
1970 			break;
1971 		}
1972 	case SCTP_CONTEXT:
1973 		{
1974 			struct sctp_assoc_value *av;
1975 
1976 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1977 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1978 
1979 			if (stcb) {
1980 				av->assoc_value = stcb->asoc.context;
1981 				SCTP_TCB_UNLOCK(stcb);
1982 			} else {
1983 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1984 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1985 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1986 					SCTP_INP_RLOCK(inp);
1987 					av->assoc_value = inp->sctp_context;
1988 					SCTP_INP_RUNLOCK(inp);
1989 				} else {
1990 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1991 					error = EINVAL;
1992 				}
1993 			}
1994 			if (error == 0) {
1995 				*optsize = sizeof(struct sctp_assoc_value);
1996 			}
1997 			break;
1998 		}
1999 	case SCTP_VRF_ID:
2000 		{
2001 			uint32_t *default_vrfid;
2002 
2003 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
2004 			*default_vrfid = inp->def_vrf_id;
2005 			*optsize = sizeof(uint32_t);
2006 			break;
2007 		}
2008 	case SCTP_GET_ASOC_VRF:
2009 		{
2010 			struct sctp_assoc_value *id;
2011 
2012 			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
2013 			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
2014 			if (stcb == NULL) {
2015 				error = EINVAL;
2016 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2017 			} else {
2018 				id->assoc_value = stcb->asoc.vrf_id;
2019 				*optsize = sizeof(struct sctp_assoc_value);
2020 			}
2021 			break;
2022 		}
2023 	case SCTP_GET_VRF_IDS:
2024 		{
2025 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2026 			error = EOPNOTSUPP;
2027 			break;
2028 		}
2029 	case SCTP_GET_NONCE_VALUES:
2030 		{
2031 			struct sctp_get_nonce_values *gnv;
2032 
2033 			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
2034 			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
2035 
2036 			if (stcb) {
2037 				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
2038 				gnv->gn_local_tag = stcb->asoc.my_vtag;
2039 				SCTP_TCB_UNLOCK(stcb);
2040 				*optsize = sizeof(struct sctp_get_nonce_values);
2041 			} else {
2042 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2043 				error = ENOTCONN;
2044 			}
2045 			break;
2046 		}
2047 	case SCTP_DELAYED_SACK:
2048 		{
2049 			struct sctp_sack_info *sack;
2050 
2051 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
2052 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2053 			if (stcb) {
2054 				sack->sack_delay = stcb->asoc.delayed_ack;
2055 				sack->sack_freq = stcb->asoc.sack_freq;
2056 				SCTP_TCB_UNLOCK(stcb);
2057 			} else {
2058 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2059 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2060 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2061 					SCTP_INP_RLOCK(inp);
2062 					sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2063 					sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2064 					SCTP_INP_RUNLOCK(inp);
2065 				} else {
2066 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2067 					error = EINVAL;
2068 				}
2069 			}
2070 			if (error == 0) {
2071 				*optsize = sizeof(struct sctp_sack_info);
2072 			}
2073 			break;
2074 		}
2075 	case SCTP_GET_SNDBUF_USE:
2076 		{
2077 			struct sctp_sockstat *ss;
2078 
2079 			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2080 			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2081 
2082 			if (stcb) {
2083 				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2084 				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2085 				    stcb->asoc.size_on_all_streams);
2086 				SCTP_TCB_UNLOCK(stcb);
2087 				*optsize = sizeof(struct sctp_sockstat);
2088 			} else {
2089 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2090 				error = ENOTCONN;
2091 			}
2092 			break;
2093 		}
2094 	case SCTP_MAX_BURST:
2095 		{
2096 			struct sctp_assoc_value *av;
2097 
2098 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2099 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2100 
2101 			if (stcb) {
2102 				av->assoc_value = stcb->asoc.max_burst;
2103 				SCTP_TCB_UNLOCK(stcb);
2104 			} else {
2105 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2106 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2107 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2108 					SCTP_INP_RLOCK(inp);
2109 					av->assoc_value = inp->sctp_ep.max_burst;
2110 					SCTP_INP_RUNLOCK(inp);
2111 				} else {
2112 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2113 					error = EINVAL;
2114 				}
2115 			}
2116 			if (error == 0) {
2117 				*optsize = sizeof(struct sctp_assoc_value);
2118 			}
2119 			break;
2120 		}
2121 	case SCTP_MAXSEG:
2122 		{
2123 			struct sctp_assoc_value *av;
2124 			int ovh;
2125 
2126 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2127 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2128 
2129 			if (stcb) {
2130 				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2131 				SCTP_TCB_UNLOCK(stcb);
2132 			} else {
2133 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2134 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2135 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2136 					SCTP_INP_RLOCK(inp);
2137 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2138 						ovh = SCTP_MED_OVERHEAD;
2139 					} else {
2140 						ovh = SCTP_MED_V4_OVERHEAD;
2141 					}
2142 					if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2143 						av->assoc_value = 0;
2144 					else
2145 						av->assoc_value = inp->sctp_frag_point - ovh;
2146 					SCTP_INP_RUNLOCK(inp);
2147 				} else {
2148 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2149 					error = EINVAL;
2150 				}
2151 			}
2152 			if (error == 0) {
2153 				*optsize = sizeof(struct sctp_assoc_value);
2154 			}
2155 			break;
2156 		}
2157 	case SCTP_GET_STAT_LOG:
2158 		error = sctp_fill_stat_log(optval, optsize);
2159 		break;
2160 	case SCTP_EVENTS:
2161 		{
2162 			struct sctp_event_subscribe *events;
2163 
2164 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2165 			memset(events, 0, sizeof(struct sctp_event_subscribe));
2166 			SCTP_INP_RLOCK(inp);
2167 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2168 				events->sctp_data_io_event = 1;
2169 
2170 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2171 				events->sctp_association_event = 1;
2172 
2173 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2174 				events->sctp_address_event = 1;
2175 
2176 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2177 				events->sctp_send_failure_event = 1;
2178 
2179 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2180 				events->sctp_peer_error_event = 1;
2181 
2182 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2183 				events->sctp_shutdown_event = 1;
2184 
2185 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2186 				events->sctp_partial_delivery_event = 1;
2187 
2188 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2189 				events->sctp_adaptation_layer_event = 1;
2190 
2191 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2192 				events->sctp_authentication_event = 1;
2193 
2194 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2195 				events->sctp_sender_dry_event = 1;
2196 
2197 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2198 				events->sctp_stream_reset_event = 1;
2199 			SCTP_INP_RUNLOCK(inp);
2200 			*optsize = sizeof(struct sctp_event_subscribe);
2201 			break;
2202 		}
2203 	case SCTP_ADAPTATION_LAYER:
2204 		{
2205 			uint32_t *value;
2206 
2207 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2208 
2209 			SCTP_INP_RLOCK(inp);
2210 			*value = inp->sctp_ep.adaptation_layer_indicator;
2211 			SCTP_INP_RUNLOCK(inp);
2212 			*optsize = sizeof(uint32_t);
2213 			break;
2214 		}
2215 	case SCTP_SET_INITIAL_DBG_SEQ:
2216 		{
2217 			uint32_t *value;
2218 
2219 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2220 			SCTP_INP_RLOCK(inp);
2221 			*value = inp->sctp_ep.initial_sequence_debug;
2222 			SCTP_INP_RUNLOCK(inp);
2223 			*optsize = sizeof(uint32_t);
2224 			break;
2225 		}
2226 	case SCTP_GET_LOCAL_ADDR_SIZE:
2227 		{
2228 			uint32_t *value;
2229 
2230 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2231 			SCTP_INP_RLOCK(inp);
2232 			*value = sctp_count_max_addresses(inp);
2233 			SCTP_INP_RUNLOCK(inp);
2234 			*optsize = sizeof(uint32_t);
2235 			break;
2236 		}
2237 	case SCTP_GET_REMOTE_ADDR_SIZE:
2238 		{
2239 			uint32_t *value;
2240 			size_t size;
2241 			struct sctp_nets *net;
2242 
2243 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2244 			/* FIXME MT: change to sctp_assoc_value? */
2245 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2246 
2247 			if (stcb) {
2248 				size = 0;
2249 				/* Count the sizes */
2250 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2251 					switch (net->ro._l_addr.sa.sa_family) {
2252 #ifdef INET
2253 					case AF_INET:
2254 #ifdef INET6
2255 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2256 							size += sizeof(struct sockaddr_in6);
2257 						} else {
2258 							size += sizeof(struct sockaddr_in);
2259 						}
2260 #else
2261 						size += sizeof(struct sockaddr_in);
2262 #endif
2263 						break;
2264 #endif
2265 #ifdef INET6
2266 					case AF_INET6:
2267 						size += sizeof(struct sockaddr_in6);
2268 						break;
2269 #endif
2270 					default:
2271 						break;
2272 					}
2273 				}
2274 				SCTP_TCB_UNLOCK(stcb);
2275 				*value = (uint32_t) size;
2276 				*optsize = sizeof(uint32_t);
2277 			} else {
2278 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2279 				error = ENOTCONN;
2280 			}
2281 			break;
2282 		}
2283 	case SCTP_GET_PEER_ADDRESSES:
2284 		/*
2285 		 * Get the address information, an array is passed in to
2286 		 * fill up we pack it.
2287 		 */
2288 		{
2289 			size_t cpsz, left;
2290 			struct sockaddr_storage *sas;
2291 			struct sctp_nets *net;
2292 			struct sctp_getaddresses *saddr;
2293 
2294 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2295 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2296 
2297 			if (stcb) {
2298 				left = (*optsize) - sizeof(struct sctp_getaddresses);
2299 				*optsize = sizeof(struct sctp_getaddresses);
2300 				sas = (struct sockaddr_storage *)&saddr->addr[0];
2301 
2302 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2303 					switch (net->ro._l_addr.sa.sa_family) {
2304 #ifdef INET
2305 					case AF_INET:
2306 #ifdef INET6
2307 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2308 							cpsz = sizeof(struct sockaddr_in6);
2309 						} else {
2310 							cpsz = sizeof(struct sockaddr_in);
2311 						}
2312 #else
2313 						cpsz = sizeof(struct sockaddr_in);
2314 #endif
2315 						break;
2316 #endif
2317 #ifdef INET6
2318 					case AF_INET6:
2319 						cpsz = sizeof(struct sockaddr_in6);
2320 						break;
2321 #endif
2322 					default:
2323 						cpsz = 0;
2324 						break;
2325 					}
2326 					if (cpsz == 0) {
2327 						break;
2328 					}
2329 					if (left < cpsz) {
2330 						/* not enough room. */
2331 						break;
2332 					}
2333 #if defined(INET) && defined(INET6)
2334 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2335 					    (net->ro._l_addr.sa.sa_family == AF_INET)) {
2336 						/* Must map the address */
2337 						in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
2338 						    (struct sockaddr_in6 *)sas);
2339 					} else {
2340 						memcpy(sas, &net->ro._l_addr, cpsz);
2341 					}
2342 #else
2343 					memcpy(sas, &net->ro._l_addr, cpsz);
2344 #endif
2345 					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2346 
2347 					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2348 					left -= cpsz;
2349 					*optsize += cpsz;
2350 				}
2351 				SCTP_TCB_UNLOCK(stcb);
2352 			} else {
2353 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2354 				error = ENOENT;
2355 			}
2356 			break;
2357 		}
2358 	case SCTP_GET_LOCAL_ADDRESSES:
2359 		{
2360 			size_t limit, actual;
2361 			struct sockaddr_storage *sas;
2362 			struct sctp_getaddresses *saddr;
2363 
2364 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2365 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2366 
2367 			sas = (struct sockaddr_storage *)&saddr->addr[0];
2368 			limit = *optsize - sizeof(sctp_assoc_t);
2369 			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2370 			if (stcb) {
2371 				SCTP_TCB_UNLOCK(stcb);
2372 			}
2373 			*optsize = sizeof(struct sockaddr_storage) + actual;
2374 			break;
2375 		}
2376 	case SCTP_PEER_ADDR_PARAMS:
2377 		{
2378 			struct sctp_paddrparams *paddrp;
2379 			struct sctp_nets *net;
2380 			struct sockaddr *addr;
2381 
2382 #if defined(INET) && defined(INET6)
2383 			struct sockaddr_in sin_store;
2384 
2385 #endif
2386 
2387 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2388 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2389 
2390 #if defined(INET) && defined(INET6)
2391 			if (paddrp->spp_address.ss_family == AF_INET6) {
2392 				struct sockaddr_in6 *sin6;
2393 
2394 				sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
2395 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2396 					in6_sin6_2_sin(&sin_store, sin6);
2397 					addr = (struct sockaddr *)&sin_store;
2398 				} else {
2399 					addr = (struct sockaddr *)&paddrp->spp_address;
2400 				}
2401 			} else {
2402 				addr = (struct sockaddr *)&paddrp->spp_address;
2403 			}
2404 #else
2405 			addr = (struct sockaddr *)&paddrp->spp_address;
2406 #endif
2407 			if (stcb != NULL) {
2408 				net = sctp_findnet(stcb, addr);
2409 			} else {
2410 				/*
2411 				 * We increment here since
2412 				 * sctp_findassociation_ep_addr() wil do a
2413 				 * decrement if it finds the stcb as long as
2414 				 * the locked tcb (last argument) is NOT a
2415 				 * TCB.. aka NULL.
2416 				 */
2417 				net = NULL;
2418 				SCTP_INP_INCR_REF(inp);
2419 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2420 				if (stcb == NULL) {
2421 					SCTP_INP_DECR_REF(inp);
2422 				}
2423 			}
2424 			if ((stcb != NULL) && (net == NULL)) {
2425 #ifdef INET
2426 				if (addr->sa_family == AF_INET) {
2427 					struct sockaddr_in *sin;
2428 
2429 					sin = (struct sockaddr_in *)addr;
2430 					if (sin->sin_addr.s_addr != INADDR_ANY) {
2431 						error = EINVAL;
2432 						SCTP_TCB_UNLOCK(stcb);
2433 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2434 						break;
2435 					}
2436 				} else
2437 #endif
2438 #ifdef INET6
2439 				if (addr->sa_family == AF_INET6) {
2440 					struct sockaddr_in6 *sin6;
2441 
2442 					sin6 = (struct sockaddr_in6 *)addr;
2443 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2444 						error = EINVAL;
2445 						SCTP_TCB_UNLOCK(stcb);
2446 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2447 						break;
2448 					}
2449 				} else
2450 #endif
2451 				{
2452 					error = EAFNOSUPPORT;
2453 					SCTP_TCB_UNLOCK(stcb);
2454 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2455 					break;
2456 				}
2457 			}
2458 			if (stcb != NULL) {
2459 				/* Applies to the specific association */
2460 				paddrp->spp_flags = 0;
2461 				if (net != NULL) {
2462 					paddrp->spp_hbinterval = net->heart_beat_delay;
2463 					paddrp->spp_pathmaxrxt = net->failure_threshold;
2464 					paddrp->spp_pathmtu = net->mtu;
2465 					switch (net->ro._l_addr.sa.sa_family) {
2466 #ifdef INET
2467 					case AF_INET:
2468 						paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2469 						break;
2470 #endif
2471 #ifdef INET6
2472 					case AF_INET6:
2473 						paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2474 						break;
2475 #endif
2476 					default:
2477 						break;
2478 					}
2479 					/* get flags for HB */
2480 					if (net->dest_state & SCTP_ADDR_NOHB) {
2481 						paddrp->spp_flags |= SPP_HB_DISABLE;
2482 					} else {
2483 						paddrp->spp_flags |= SPP_HB_ENABLE;
2484 					}
2485 					/* get flags for PMTU */
2486 					if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2487 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2488 					} else {
2489 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2490 					}
2491 					if (net->dscp & 0x01) {
2492 						paddrp->spp_dscp = net->dscp & 0xfc;
2493 						paddrp->spp_flags |= SPP_DSCP;
2494 					}
2495 #ifdef INET6
2496 					if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2497 					    (net->flowlabel & 0x80000000)) {
2498 						paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2499 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2500 					}
2501 #endif
2502 				} else {
2503 					/*
2504 					 * No destination so return default
2505 					 * value
2506 					 */
2507 					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2508 					paddrp->spp_pathmtu = 0;
2509 					if (stcb->asoc.default_dscp & 0x01) {
2510 						paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2511 						paddrp->spp_flags |= SPP_DSCP;
2512 					}
2513 #ifdef INET6
2514 					if (stcb->asoc.default_flowlabel & 0x80000000) {
2515 						paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2516 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2517 					}
2518 #endif
2519 					/* default settings should be these */
2520 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2521 						paddrp->spp_flags |= SPP_HB_DISABLE;
2522 					} else {
2523 						paddrp->spp_flags |= SPP_HB_ENABLE;
2524 					}
2525 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2526 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2527 					} else {
2528 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2529 					}
2530 					paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2531 				}
2532 				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2533 				SCTP_TCB_UNLOCK(stcb);
2534 			} else {
2535 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2536 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2537 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2538 					/* Use endpoint defaults */
2539 					SCTP_INP_RLOCK(inp);
2540 					paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2541 					paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2542 					paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2543 					/* get inp's default */
2544 					if (inp->sctp_ep.default_dscp & 0x01) {
2545 						paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2546 						paddrp->spp_flags |= SPP_DSCP;
2547 					}
2548 #ifdef INET6
2549 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2550 					    (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2551 						paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2552 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2553 					}
2554 #endif
2555 					/* can't return this */
2556 					paddrp->spp_pathmtu = 0;
2557 
2558 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2559 						paddrp->spp_flags |= SPP_HB_ENABLE;
2560 					} else {
2561 						paddrp->spp_flags |= SPP_HB_DISABLE;
2562 					}
2563 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2564 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2565 					} else {
2566 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2567 					}
2568 					SCTP_INP_RUNLOCK(inp);
2569 				} else {
2570 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2571 					error = EINVAL;
2572 				}
2573 			}
2574 			if (error == 0) {
2575 				*optsize = sizeof(struct sctp_paddrparams);
2576 			}
2577 			break;
2578 		}
2579 	case SCTP_GET_PEER_ADDR_INFO:
2580 		{
2581 			struct sctp_paddrinfo *paddri;
2582 			struct sctp_nets *net;
2583 			struct sockaddr *addr;
2584 
2585 #if defined(INET) && defined(INET6)
2586 			struct sockaddr_in sin_store;
2587 
2588 #endif
2589 
2590 			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2591 			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2592 
2593 #if defined(INET) && defined(INET6)
2594 			if (paddri->spinfo_address.ss_family == AF_INET6) {
2595 				struct sockaddr_in6 *sin6;
2596 
2597 				sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
2598 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2599 					in6_sin6_2_sin(&sin_store, sin6);
2600 					addr = (struct sockaddr *)&sin_store;
2601 				} else {
2602 					addr = (struct sockaddr *)&paddri->spinfo_address;
2603 				}
2604 			} else {
2605 				addr = (struct sockaddr *)&paddri->spinfo_address;
2606 			}
2607 #else
2608 			addr = (struct sockaddr *)&paddri->spinfo_address;
2609 #endif
2610 			if (stcb != NULL) {
2611 				net = sctp_findnet(stcb, addr);
2612 			} else {
2613 				/*
2614 				 * We increment here since
2615 				 * sctp_findassociation_ep_addr() wil do a
2616 				 * decrement if it finds the stcb as long as
2617 				 * the locked tcb (last argument) is NOT a
2618 				 * TCB.. aka NULL.
2619 				 */
2620 				net = NULL;
2621 				SCTP_INP_INCR_REF(inp);
2622 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2623 				if (stcb == NULL) {
2624 					SCTP_INP_DECR_REF(inp);
2625 				}
2626 			}
2627 
2628 			if ((stcb != NULL) && (net != NULL)) {
2629 				if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2630 					/* It's unconfirmed */
2631 					paddri->spinfo_state = SCTP_UNCONFIRMED;
2632 				} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2633 					/* It's active */
2634 					paddri->spinfo_state = SCTP_ACTIVE;
2635 				} else {
2636 					/* It's inactive */
2637 					paddri->spinfo_state = SCTP_INACTIVE;
2638 				}
2639 				paddri->spinfo_cwnd = net->cwnd;
2640 				paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2641 				paddri->spinfo_rto = net->RTO;
2642 				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2643 				paddri->spinfo_mtu = net->mtu;
2644 				switch (addr->sa_family) {
2645 #if defined(INET)
2646 				case AF_INET:
2647 					paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2648 					break;
2649 #endif
2650 #if defined(INET6)
2651 				case AF_INET6:
2652 					paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
2653 					break;
2654 #endif
2655 				default:
2656 					break;
2657 				}
2658 				SCTP_TCB_UNLOCK(stcb);
2659 				*optsize = sizeof(struct sctp_paddrinfo);
2660 			} else {
2661 				if (stcb != NULL) {
2662 					SCTP_TCB_UNLOCK(stcb);
2663 				}
2664 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2665 				error = ENOENT;
2666 			}
2667 			break;
2668 		}
2669 	case SCTP_PCB_STATUS:
2670 		{
2671 			struct sctp_pcbinfo *spcb;
2672 
2673 			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2674 			sctp_fill_pcbinfo(spcb);
2675 			*optsize = sizeof(struct sctp_pcbinfo);
2676 			break;
2677 		}
2678 	case SCTP_STATUS:
2679 		{
2680 			struct sctp_nets *net;
2681 			struct sctp_status *sstat;
2682 
2683 			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2684 			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2685 
2686 			if (stcb == NULL) {
2687 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2688 				error = EINVAL;
2689 				break;
2690 			}
2691 			sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
2692 			sstat->sstat_assoc_id = sctp_get_associd(stcb);
2693 			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2694 			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2695 			/*
2696 			 * We can't include chunks that have been passed to
2697 			 * the socket layer. Only things in queue.
2698 			 */
2699 			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2700 			    stcb->asoc.cnt_on_all_streams);
2701 
2702 
2703 			sstat->sstat_instrms = stcb->asoc.streamincnt;
2704 			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2705 			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2706 			memcpy(&sstat->sstat_primary.spinfo_address,
2707 			    &stcb->asoc.primary_destination->ro._l_addr,
2708 			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2709 			net = stcb->asoc.primary_destination;
2710 			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2711 			/*
2712 			 * Again the user can get info from sctp_constants.h
2713 			 * for what the state of the network is.
2714 			 */
2715 			if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2716 				/* It's unconfirmed */
2717 				sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2718 			} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2719 				/* It's active */
2720 				sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2721 			} else {
2722 				/* It's inactive */
2723 				sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2724 			}
2725 			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2726 			sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2727 			sstat->sstat_primary.spinfo_rto = net->RTO;
2728 			sstat->sstat_primary.spinfo_mtu = net->mtu;
2729 			switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
2730 #if defined(INET)
2731 			case AF_INET:
2732 				sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2733 				break;
2734 #endif
2735 #if defined(INET6)
2736 			case AF_INET6:
2737 				sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
2738 				break;
2739 #endif
2740 			default:
2741 				break;
2742 			}
2743 			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2744 			SCTP_TCB_UNLOCK(stcb);
2745 			*optsize = sizeof(struct sctp_status);
2746 			break;
2747 		}
2748 	case SCTP_RTOINFO:
2749 		{
2750 			struct sctp_rtoinfo *srto;
2751 
2752 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2753 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2754 
2755 			if (stcb) {
2756 				srto->srto_initial = stcb->asoc.initial_rto;
2757 				srto->srto_max = stcb->asoc.maxrto;
2758 				srto->srto_min = stcb->asoc.minrto;
2759 				SCTP_TCB_UNLOCK(stcb);
2760 			} else {
2761 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2762 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2763 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2764 					SCTP_INP_RLOCK(inp);
2765 					srto->srto_initial = inp->sctp_ep.initial_rto;
2766 					srto->srto_max = inp->sctp_ep.sctp_maxrto;
2767 					srto->srto_min = inp->sctp_ep.sctp_minrto;
2768 					SCTP_INP_RUNLOCK(inp);
2769 				} else {
2770 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2771 					error = EINVAL;
2772 				}
2773 			}
2774 			if (error == 0) {
2775 				*optsize = sizeof(struct sctp_rtoinfo);
2776 			}
2777 			break;
2778 		}
2779 	case SCTP_TIMEOUTS:
2780 		{
2781 			struct sctp_timeouts *stimo;
2782 
2783 			SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2784 			SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2785 
2786 			if (stcb) {
2787 				stimo->stimo_init = stcb->asoc.timoinit;
2788 				stimo->stimo_data = stcb->asoc.timodata;
2789 				stimo->stimo_sack = stcb->asoc.timosack;
2790 				stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2791 				stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2792 				stimo->stimo_cookie = stcb->asoc.timocookie;
2793 				stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2794 				SCTP_TCB_UNLOCK(stcb);
2795 				*optsize = sizeof(struct sctp_timeouts);
2796 			} else {
2797 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2798 				error = EINVAL;
2799 			}
2800 			break;
2801 		}
2802 	case SCTP_ASSOCINFO:
2803 		{
2804 			struct sctp_assocparams *sasoc;
2805 
2806 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2807 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2808 
2809 			if (stcb) {
2810 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2811 				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2812 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2813 				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2814 				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2815 				SCTP_TCB_UNLOCK(stcb);
2816 			} else {
2817 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2818 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2819 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2820 					SCTP_INP_RLOCK(inp);
2821 					sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2822 					sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2823 					sasoc->sasoc_number_peer_destinations = 0;
2824 					sasoc->sasoc_peer_rwnd = 0;
2825 					sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2826 					SCTP_INP_RUNLOCK(inp);
2827 				} else {
2828 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2829 					error = EINVAL;
2830 				}
2831 			}
2832 			if (error == 0) {
2833 				*optsize = sizeof(struct sctp_assocparams);
2834 			}
2835 			break;
2836 		}
2837 	case SCTP_DEFAULT_SEND_PARAM:
2838 		{
2839 			struct sctp_sndrcvinfo *s_info;
2840 
2841 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2842 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2843 
2844 			if (stcb) {
2845 				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2846 				SCTP_TCB_UNLOCK(stcb);
2847 			} else {
2848 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2849 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2850 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2851 					SCTP_INP_RLOCK(inp);
2852 					memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2853 					SCTP_INP_RUNLOCK(inp);
2854 				} else {
2855 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2856 					error = EINVAL;
2857 				}
2858 			}
2859 			if (error == 0) {
2860 				*optsize = sizeof(struct sctp_sndrcvinfo);
2861 			}
2862 			break;
2863 		}
2864 	case SCTP_INITMSG:
2865 		{
2866 			struct sctp_initmsg *sinit;
2867 
2868 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2869 			SCTP_INP_RLOCK(inp);
2870 			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2871 			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2872 			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2873 			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2874 			SCTP_INP_RUNLOCK(inp);
2875 			*optsize = sizeof(struct sctp_initmsg);
2876 			break;
2877 		}
2878 	case SCTP_PRIMARY_ADDR:
2879 		/* we allow a "get" operation on this */
2880 		{
2881 			struct sctp_setprim *ssp;
2882 
2883 			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2884 			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2885 
2886 			if (stcb) {
2887 				union sctp_sockstore *addr;
2888 
2889 				addr = &stcb->asoc.primary_destination->ro._l_addr;
2890 				switch (addr->sa.sa_family) {
2891 #ifdef INET
2892 				case AF_INET:
2893 #ifdef INET6
2894 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2895 						in6_sin_2_v4mapsin6(&addr->sin,
2896 						    (struct sockaddr_in6 *)&ssp->ssp_addr);
2897 					} else {
2898 						memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2899 					}
2900 #else
2901 					memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2902 #endif
2903 					break;
2904 #endif
2905 #ifdef INET6
2906 				case AF_INET6:
2907 					memcpy(&ssp->ssp_addr, &addr->sin6, sizeof(struct sockaddr_in6));
2908 					break;
2909 #endif
2910 				default:
2911 					break;
2912 				}
2913 				SCTP_TCB_UNLOCK(stcb);
2914 				*optsize = sizeof(struct sctp_setprim);
2915 			} else {
2916 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2917 				error = EINVAL;
2918 			}
2919 			break;
2920 		}
2921 	case SCTP_HMAC_IDENT:
2922 		{
2923 			struct sctp_hmacalgo *shmac;
2924 			sctp_hmaclist_t *hmaclist;
2925 			uint32_t size;
2926 			int i;
2927 
2928 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2929 
2930 			SCTP_INP_RLOCK(inp);
2931 			hmaclist = inp->sctp_ep.local_hmacs;
2932 			if (hmaclist == NULL) {
2933 				/* no HMACs to return */
2934 				*optsize = sizeof(*shmac);
2935 				SCTP_INP_RUNLOCK(inp);
2936 				break;
2937 			}
2938 			/* is there room for all of the hmac ids? */
2939 			size = sizeof(*shmac) + (hmaclist->num_algo *
2940 			    sizeof(shmac->shmac_idents[0]));
2941 			if ((size_t)(*optsize) < size) {
2942 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2943 				error = EINVAL;
2944 				SCTP_INP_RUNLOCK(inp);
2945 				break;
2946 			}
2947 			/* copy in the list */
2948 			shmac->shmac_number_of_idents = hmaclist->num_algo;
2949 			for (i = 0; i < hmaclist->num_algo; i++) {
2950 				shmac->shmac_idents[i] = hmaclist->hmac[i];
2951 			}
2952 			SCTP_INP_RUNLOCK(inp);
2953 			*optsize = size;
2954 			break;
2955 		}
2956 	case SCTP_AUTH_ACTIVE_KEY:
2957 		{
2958 			struct sctp_authkeyid *scact;
2959 
2960 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2961 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2962 
2963 			if (stcb) {
2964 				/* get the active key on the assoc */
2965 				scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2966 				SCTP_TCB_UNLOCK(stcb);
2967 			} else {
2968 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2969 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2970 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2971 					/* get the endpoint active key */
2972 					SCTP_INP_RLOCK(inp);
2973 					scact->scact_keynumber = inp->sctp_ep.default_keyid;
2974 					SCTP_INP_RUNLOCK(inp);
2975 				} else {
2976 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2977 					error = EINVAL;
2978 				}
2979 			}
2980 			if (error == 0) {
2981 				*optsize = sizeof(struct sctp_authkeyid);
2982 			}
2983 			break;
2984 		}
2985 	case SCTP_LOCAL_AUTH_CHUNKS:
2986 		{
2987 			struct sctp_authchunks *sac;
2988 			sctp_auth_chklist_t *chklist = NULL;
2989 			size_t size = 0;
2990 
2991 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2992 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2993 
2994 			if (stcb) {
2995 				/* get off the assoc */
2996 				chklist = stcb->asoc.local_auth_chunks;
2997 				/* is there enough space? */
2998 				size = sctp_auth_get_chklist_size(chklist);
2999 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3000 					error = EINVAL;
3001 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3002 				} else {
3003 					/* copy in the chunks */
3004 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3005 					sac->gauth_number_of_chunks = (uint32_t) size;
3006 					*optsize = sizeof(struct sctp_authchunks) + size;
3007 				}
3008 				SCTP_TCB_UNLOCK(stcb);
3009 			} else {
3010 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3011 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3012 				    (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
3013 					/* get off the endpoint */
3014 					SCTP_INP_RLOCK(inp);
3015 					chklist = inp->sctp_ep.local_auth_chunks;
3016 					/* is there enough space? */
3017 					size = sctp_auth_get_chklist_size(chklist);
3018 					if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3019 						error = EINVAL;
3020 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3021 					} else {
3022 						/* copy in the chunks */
3023 						(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3024 						sac->gauth_number_of_chunks = (uint32_t) size;
3025 						*optsize = sizeof(struct sctp_authchunks) + size;
3026 					}
3027 					SCTP_INP_RUNLOCK(inp);
3028 				} else {
3029 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3030 					error = EINVAL;
3031 				}
3032 			}
3033 			break;
3034 		}
3035 	case SCTP_PEER_AUTH_CHUNKS:
3036 		{
3037 			struct sctp_authchunks *sac;
3038 			sctp_auth_chklist_t *chklist = NULL;
3039 			size_t size = 0;
3040 
3041 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
3042 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
3043 
3044 			if (stcb) {
3045 				/* get off the assoc */
3046 				chklist = stcb->asoc.peer_auth_chunks;
3047 				/* is there enough space? */
3048 				size = sctp_auth_get_chklist_size(chklist);
3049 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3050 					error = EINVAL;
3051 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3052 				} else {
3053 					/* copy in the chunks */
3054 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3055 					sac->gauth_number_of_chunks = (uint32_t) size;
3056 					*optsize = sizeof(struct sctp_authchunks) + size;
3057 				}
3058 				SCTP_TCB_UNLOCK(stcb);
3059 			} else {
3060 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3061 				error = ENOENT;
3062 			}
3063 			break;
3064 		}
3065 	case SCTP_EVENT:
3066 		{
3067 			struct sctp_event *event;
3068 			uint32_t event_type;
3069 
3070 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
3071 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
3072 
3073 			switch (event->se_type) {
3074 			case SCTP_ASSOC_CHANGE:
3075 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
3076 				break;
3077 			case SCTP_PEER_ADDR_CHANGE:
3078 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
3079 				break;
3080 			case SCTP_REMOTE_ERROR:
3081 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
3082 				break;
3083 			case SCTP_SEND_FAILED:
3084 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3085 				break;
3086 			case SCTP_SHUTDOWN_EVENT:
3087 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3088 				break;
3089 			case SCTP_ADAPTATION_INDICATION:
3090 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
3091 				break;
3092 			case SCTP_PARTIAL_DELIVERY_EVENT:
3093 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
3094 				break;
3095 			case SCTP_AUTHENTICATION_EVENT:
3096 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
3097 				break;
3098 			case SCTP_STREAM_RESET_EVENT:
3099 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3100 				break;
3101 			case SCTP_SENDER_DRY_EVENT:
3102 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
3103 				break;
3104 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
3105 				event_type = 0;
3106 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
3107 				error = ENOTSUP;
3108 				break;
3109 			case SCTP_ASSOC_RESET_EVENT:
3110 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
3111 				break;
3112 			case SCTP_STREAM_CHANGE_EVENT:
3113 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
3114 				break;
3115 			case SCTP_SEND_FAILED_EVENT:
3116 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
3117 				break;
3118 			default:
3119 				event_type = 0;
3120 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3121 				error = EINVAL;
3122 				break;
3123 			}
3124 			if (event_type > 0) {
3125 				if (stcb) {
3126 					event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
3127 					SCTP_TCB_UNLOCK(stcb);
3128 				} else {
3129 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3130 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3131 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3132 						SCTP_INP_RLOCK(inp);
3133 						event->se_on = sctp_is_feature_on(inp, event_type);
3134 						SCTP_INP_RUNLOCK(inp);
3135 					} else {
3136 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3137 						error = EINVAL;
3138 					}
3139 				}
3140 			}
3141 			if (error == 0) {
3142 				*optsize = sizeof(struct sctp_event);
3143 			}
3144 			break;
3145 		}
3146 	case SCTP_RECVRCVINFO:
3147 		{
3148 			int onoff;
3149 
3150 			if (*optsize < sizeof(int)) {
3151 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3152 				error = EINVAL;
3153 			} else {
3154 				SCTP_INP_RLOCK(inp);
3155 				onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3156 				SCTP_INP_RUNLOCK(inp);
3157 			}
3158 			if (error == 0) {
3159 				/* return the option value */
3160 				*(int *)optval = onoff;
3161 				*optsize = sizeof(int);
3162 			}
3163 			break;
3164 		}
3165 	case SCTP_RECVNXTINFO:
3166 		{
3167 			int onoff;
3168 
3169 			if (*optsize < sizeof(int)) {
3170 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3171 				error = EINVAL;
3172 			} else {
3173 				SCTP_INP_RLOCK(inp);
3174 				onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3175 				SCTP_INP_RUNLOCK(inp);
3176 			}
3177 			if (error == 0) {
3178 				/* return the option value */
3179 				*(int *)optval = onoff;
3180 				*optsize = sizeof(int);
3181 			}
3182 			break;
3183 		}
3184 	case SCTP_DEFAULT_SNDINFO:
3185 		{
3186 			struct sctp_sndinfo *info;
3187 
3188 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3189 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3190 
3191 			if (stcb) {
3192 				info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3193 				info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3194 				info->snd_flags &= 0xfff0;
3195 				info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3196 				info->snd_context = stcb->asoc.def_send.sinfo_context;
3197 				SCTP_TCB_UNLOCK(stcb);
3198 			} else {
3199 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3200 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3201 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3202 					SCTP_INP_RLOCK(inp);
3203 					info->snd_sid = inp->def_send.sinfo_stream;
3204 					info->snd_flags = inp->def_send.sinfo_flags;
3205 					info->snd_flags &= 0xfff0;
3206 					info->snd_ppid = inp->def_send.sinfo_ppid;
3207 					info->snd_context = inp->def_send.sinfo_context;
3208 					SCTP_INP_RUNLOCK(inp);
3209 				} else {
3210 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3211 					error = EINVAL;
3212 				}
3213 			}
3214 			if (error == 0) {
3215 				*optsize = sizeof(struct sctp_sndinfo);
3216 			}
3217 			break;
3218 		}
3219 	case SCTP_DEFAULT_PRINFO:
3220 		{
3221 			struct sctp_default_prinfo *info;
3222 
3223 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3224 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3225 
3226 			if (stcb) {
3227 				info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3228 				info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3229 				SCTP_TCB_UNLOCK(stcb);
3230 			} else {
3231 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3232 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3233 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3234 					SCTP_INP_RLOCK(inp);
3235 					info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3236 					info->pr_value = inp->def_send.sinfo_timetolive;
3237 					SCTP_INP_RUNLOCK(inp);
3238 				} else {
3239 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3240 					error = EINVAL;
3241 				}
3242 			}
3243 			if (error == 0) {
3244 				*optsize = sizeof(struct sctp_default_prinfo);
3245 			}
3246 			break;
3247 		}
3248 	case SCTP_PEER_ADDR_THLDS:
3249 		{
3250 			struct sctp_paddrthlds *thlds;
3251 			struct sctp_nets *net;
3252 			struct sockaddr *addr;
3253 
3254 #if defined(INET) && defined(INET6)
3255 			struct sockaddr_in sin_store;
3256 
3257 #endif
3258 
3259 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3260 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3261 
3262 #if defined(INET) && defined(INET6)
3263 			if (thlds->spt_address.ss_family == AF_INET6) {
3264 				struct sockaddr_in6 *sin6;
3265 
3266 				sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
3267 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3268 					in6_sin6_2_sin(&sin_store, sin6);
3269 					addr = (struct sockaddr *)&sin_store;
3270 				} else {
3271 					addr = (struct sockaddr *)&thlds->spt_address;
3272 				}
3273 			} else {
3274 				addr = (struct sockaddr *)&thlds->spt_address;
3275 			}
3276 #else
3277 			addr = (struct sockaddr *)&thlds->spt_address;
3278 #endif
3279 			if (stcb != NULL) {
3280 				net = sctp_findnet(stcb, addr);
3281 			} else {
3282 				/*
3283 				 * We increment here since
3284 				 * sctp_findassociation_ep_addr() wil do a
3285 				 * decrement if it finds the stcb as long as
3286 				 * the locked tcb (last argument) is NOT a
3287 				 * TCB.. aka NULL.
3288 				 */
3289 				net = NULL;
3290 				SCTP_INP_INCR_REF(inp);
3291 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3292 				if (stcb == NULL) {
3293 					SCTP_INP_DECR_REF(inp);
3294 				}
3295 			}
3296 			if ((stcb != NULL) && (net == NULL)) {
3297 #ifdef INET
3298 				if (addr->sa_family == AF_INET) {
3299 					struct sockaddr_in *sin;
3300 
3301 					sin = (struct sockaddr_in *)addr;
3302 					if (sin->sin_addr.s_addr != INADDR_ANY) {
3303 						error = EINVAL;
3304 						SCTP_TCB_UNLOCK(stcb);
3305 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3306 						break;
3307 					}
3308 				} else
3309 #endif
3310 #ifdef INET6
3311 				if (addr->sa_family == AF_INET6) {
3312 					struct sockaddr_in6 *sin6;
3313 
3314 					sin6 = (struct sockaddr_in6 *)addr;
3315 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3316 						error = EINVAL;
3317 						SCTP_TCB_UNLOCK(stcb);
3318 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3319 						break;
3320 					}
3321 				} else
3322 #endif
3323 				{
3324 					error = EAFNOSUPPORT;
3325 					SCTP_TCB_UNLOCK(stcb);
3326 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3327 					break;
3328 				}
3329 			}
3330 			if (stcb != NULL) {
3331 				if (net != NULL) {
3332 					thlds->spt_pathmaxrxt = net->failure_threshold;
3333 					thlds->spt_pathpfthld = net->pf_threshold;
3334 				} else {
3335 					thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3336 					thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3337 				}
3338 				thlds->spt_assoc_id = sctp_get_associd(stcb);
3339 				SCTP_TCB_UNLOCK(stcb);
3340 			} else {
3341 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3342 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3343 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3344 					/* Use endpoint defaults */
3345 					SCTP_INP_RLOCK(inp);
3346 					thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3347 					thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3348 					SCTP_INP_RUNLOCK(inp);
3349 				} else {
3350 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3351 					error = EINVAL;
3352 				}
3353 			}
3354 			if (error == 0) {
3355 				*optsize = sizeof(struct sctp_paddrthlds);
3356 			}
3357 			break;
3358 		}
3359 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
3360 		{
3361 			struct sctp_udpencaps *encaps;
3362 			struct sctp_nets *net;
3363 			struct sockaddr *addr;
3364 
3365 #if defined(INET) && defined(INET6)
3366 			struct sockaddr_in sin_store;
3367 
3368 #endif
3369 
3370 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3371 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3372 
3373 #if defined(INET) && defined(INET6)
3374 			if (encaps->sue_address.ss_family == AF_INET6) {
3375 				struct sockaddr_in6 *sin6;
3376 
3377 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
3378 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3379 					in6_sin6_2_sin(&sin_store, sin6);
3380 					addr = (struct sockaddr *)&sin_store;
3381 				} else {
3382 					addr = (struct sockaddr *)&encaps->sue_address;
3383 				}
3384 			} else {
3385 				addr = (struct sockaddr *)&encaps->sue_address;
3386 			}
3387 #else
3388 			addr = (struct sockaddr *)&encaps->sue_address;
3389 #endif
3390 			if (stcb) {
3391 				net = sctp_findnet(stcb, addr);
3392 			} else {
3393 				/*
3394 				 * We increment here since
3395 				 * sctp_findassociation_ep_addr() wil do a
3396 				 * decrement if it finds the stcb as long as
3397 				 * the locked tcb (last argument) is NOT a
3398 				 * TCB.. aka NULL.
3399 				 */
3400 				net = NULL;
3401 				SCTP_INP_INCR_REF(inp);
3402 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3403 				if (stcb == NULL) {
3404 					SCTP_INP_DECR_REF(inp);
3405 				}
3406 			}
3407 			if ((stcb != NULL) && (net == NULL)) {
3408 #ifdef INET
3409 				if (addr->sa_family == AF_INET) {
3410 					struct sockaddr_in *sin;
3411 
3412 					sin = (struct sockaddr_in *)addr;
3413 					if (sin->sin_addr.s_addr != INADDR_ANY) {
3414 						error = EINVAL;
3415 						SCTP_TCB_UNLOCK(stcb);
3416 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3417 						break;
3418 					}
3419 				} else
3420 #endif
3421 #ifdef INET6
3422 				if (addr->sa_family == AF_INET6) {
3423 					struct sockaddr_in6 *sin6;
3424 
3425 					sin6 = (struct sockaddr_in6 *)addr;
3426 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3427 						error = EINVAL;
3428 						SCTP_TCB_UNLOCK(stcb);
3429 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3430 						break;
3431 					}
3432 				} else
3433 #endif
3434 				{
3435 					error = EAFNOSUPPORT;
3436 					SCTP_TCB_UNLOCK(stcb);
3437 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3438 					break;
3439 				}
3440 			}
3441 			if (stcb != NULL) {
3442 				if (net) {
3443 					encaps->sue_port = net->port;
3444 				} else {
3445 					encaps->sue_port = stcb->asoc.port;
3446 				}
3447 				SCTP_TCB_UNLOCK(stcb);
3448 			} else {
3449 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3450 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3451 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3452 					SCTP_INP_RLOCK(inp);
3453 					encaps->sue_port = inp->sctp_ep.port;
3454 					SCTP_INP_RUNLOCK(inp);
3455 				} else {
3456 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3457 					error = EINVAL;
3458 				}
3459 			}
3460 			if (error == 0) {
3461 				*optsize = sizeof(struct sctp_udpencaps);
3462 			}
3463 			break;
3464 		}
3465 	case SCTP_ECN_SUPPORTED:
3466 		{
3467 			struct sctp_assoc_value *av;
3468 
3469 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3470 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3471 
3472 			if (stcb) {
3473 				av->assoc_value = stcb->asoc.ecn_supported;
3474 				SCTP_TCB_UNLOCK(stcb);
3475 			} else {
3476 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3477 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3478 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3479 					SCTP_INP_RLOCK(inp);
3480 					av->assoc_value = inp->ecn_supported;
3481 					SCTP_INP_RUNLOCK(inp);
3482 				} else {
3483 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3484 					error = EINVAL;
3485 				}
3486 			}
3487 			if (error == 0) {
3488 				*optsize = sizeof(struct sctp_assoc_value);
3489 			}
3490 			break;
3491 		}
3492 	case SCTP_PR_SUPPORTED:
3493 		{
3494 			struct sctp_assoc_value *av;
3495 
3496 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3497 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3498 
3499 			if (stcb) {
3500 				av->assoc_value = stcb->asoc.prsctp_supported;
3501 				SCTP_TCB_UNLOCK(stcb);
3502 			} else {
3503 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3504 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3505 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3506 					SCTP_INP_RLOCK(inp);
3507 					av->assoc_value = inp->prsctp_supported;
3508 					SCTP_INP_RUNLOCK(inp);
3509 				} else {
3510 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3511 					error = EINVAL;
3512 				}
3513 			}
3514 			if (error == 0) {
3515 				*optsize = sizeof(struct sctp_assoc_value);
3516 			}
3517 			break;
3518 		}
3519 	case SCTP_AUTH_SUPPORTED:
3520 		{
3521 			struct sctp_assoc_value *av;
3522 
3523 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3524 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3525 
3526 			if (stcb) {
3527 				av->assoc_value = stcb->asoc.auth_supported;
3528 				SCTP_TCB_UNLOCK(stcb);
3529 			} else {
3530 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3531 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3532 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3533 					SCTP_INP_RLOCK(inp);
3534 					av->assoc_value = inp->auth_supported;
3535 					SCTP_INP_RUNLOCK(inp);
3536 				} else {
3537 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3538 					error = EINVAL;
3539 				}
3540 			}
3541 			if (error == 0) {
3542 				*optsize = sizeof(struct sctp_assoc_value);
3543 			}
3544 			break;
3545 		}
3546 	case SCTP_ASCONF_SUPPORTED:
3547 		{
3548 			struct sctp_assoc_value *av;
3549 
3550 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3551 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3552 
3553 			if (stcb) {
3554 				av->assoc_value = stcb->asoc.asconf_supported;
3555 				SCTP_TCB_UNLOCK(stcb);
3556 			} else {
3557 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3558 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3559 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3560 					SCTP_INP_RLOCK(inp);
3561 					av->assoc_value = inp->asconf_supported;
3562 					SCTP_INP_RUNLOCK(inp);
3563 				} else {
3564 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3565 					error = EINVAL;
3566 				}
3567 			}
3568 			if (error == 0) {
3569 				*optsize = sizeof(struct sctp_assoc_value);
3570 			}
3571 			break;
3572 		}
3573 	case SCTP_RECONFIG_SUPPORTED:
3574 		{
3575 			struct sctp_assoc_value *av;
3576 
3577 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3578 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3579 
3580 			if (stcb) {
3581 				av->assoc_value = stcb->asoc.reconfig_supported;
3582 				SCTP_TCB_UNLOCK(stcb);
3583 			} else {
3584 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3585 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3586 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3587 					SCTP_INP_RLOCK(inp);
3588 					av->assoc_value = inp->reconfig_supported;
3589 					SCTP_INP_RUNLOCK(inp);
3590 				} else {
3591 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3592 					error = EINVAL;
3593 				}
3594 			}
3595 			if (error == 0) {
3596 				*optsize = sizeof(struct sctp_assoc_value);
3597 			}
3598 			break;
3599 		}
3600 	case SCTP_NRSACK_SUPPORTED:
3601 		{
3602 			struct sctp_assoc_value *av;
3603 
3604 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3605 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3606 
3607 			if (stcb) {
3608 				av->assoc_value = stcb->asoc.nrsack_supported;
3609 				SCTP_TCB_UNLOCK(stcb);
3610 			} else {
3611 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3612 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3613 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3614 					SCTP_INP_RLOCK(inp);
3615 					av->assoc_value = inp->nrsack_supported;
3616 					SCTP_INP_RUNLOCK(inp);
3617 				} else {
3618 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3619 					error = EINVAL;
3620 				}
3621 			}
3622 			if (error == 0) {
3623 				*optsize = sizeof(struct sctp_assoc_value);
3624 			}
3625 			break;
3626 		}
3627 	case SCTP_PKTDROP_SUPPORTED:
3628 		{
3629 			struct sctp_assoc_value *av;
3630 
3631 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3632 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3633 
3634 			if (stcb) {
3635 				av->assoc_value = stcb->asoc.pktdrop_supported;
3636 				SCTP_TCB_UNLOCK(stcb);
3637 			} else {
3638 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3639 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3640 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3641 					SCTP_INP_RLOCK(inp);
3642 					av->assoc_value = inp->pktdrop_supported;
3643 					SCTP_INP_RUNLOCK(inp);
3644 				} else {
3645 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3646 					error = EINVAL;
3647 				}
3648 			}
3649 			if (error == 0) {
3650 				*optsize = sizeof(struct sctp_assoc_value);
3651 			}
3652 			break;
3653 		}
3654 	case SCTP_ENABLE_STREAM_RESET:
3655 		{
3656 			struct sctp_assoc_value *av;
3657 
3658 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3659 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3660 
3661 			if (stcb) {
3662 				av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support;
3663 				SCTP_TCB_UNLOCK(stcb);
3664 			} else {
3665 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3666 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3667 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3668 					SCTP_INP_RLOCK(inp);
3669 					av->assoc_value = (uint32_t) inp->local_strreset_support;
3670 					SCTP_INP_RUNLOCK(inp);
3671 				} else {
3672 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3673 					error = EINVAL;
3674 				}
3675 			}
3676 			if (error == 0) {
3677 				*optsize = sizeof(struct sctp_assoc_value);
3678 			}
3679 			break;
3680 		}
3681 	case SCTP_PR_STREAM_STATUS:
3682 		{
3683 			struct sctp_prstatus *sprstat;
3684 			uint16_t sid;
3685 			uint16_t policy;
3686 
3687 			SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3688 			SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3689 
3690 			sid = sprstat->sprstat_sid;
3691 			policy = sprstat->sprstat_policy;
3692 #if defined(SCTP_DETAILED_STR_STATS)
3693 			if ((stcb != NULL) &&
3694 			    (sid < stcb->asoc.streamoutcnt) &&
3695 			    (policy != SCTP_PR_SCTP_NONE) &&
3696 			    ((policy <= SCTP_PR_SCTP_MAX) ||
3697 			    (policy == SCTP_PR_SCTP_ALL))) {
3698 				if (policy == SCTP_PR_SCTP_ALL) {
3699 					sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3700 					sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3701 				} else {
3702 					sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
3703 					sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
3704 				}
3705 #else
3706 			if ((stcb != NULL) &&
3707 			    (sid < stcb->asoc.streamoutcnt) &&
3708 			    (policy == SCTP_PR_SCTP_ALL)) {
3709 				sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3710 				sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3711 #endif
3712 				SCTP_TCB_UNLOCK(stcb);
3713 				*optsize = sizeof(struct sctp_prstatus);
3714 			} else {
3715 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3716 				error = EINVAL;
3717 			}
3718 			break;
3719 		}
3720 	case SCTP_PR_ASSOC_STATUS:
3721 		{
3722 			struct sctp_prstatus *sprstat;
3723 			uint16_t policy;
3724 
3725 			SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3726 			SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3727 
3728 			policy = sprstat->sprstat_policy;
3729 			if ((stcb != NULL) &&
3730 			    (policy != SCTP_PR_SCTP_NONE) &&
3731 			    ((policy <= SCTP_PR_SCTP_MAX) ||
3732 			    (policy == SCTP_PR_SCTP_ALL))) {
3733 				if (policy == SCTP_PR_SCTP_ALL) {
3734 					sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
3735 					sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
3736 				} else {
3737 					sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
3738 					sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
3739 				}
3740 				SCTP_TCB_UNLOCK(stcb);
3741 				*optsize = sizeof(struct sctp_prstatus);
3742 			} else {
3743 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3744 				error = EINVAL;
3745 			}
3746 			break;
3747 		}
3748 	case SCTP_MAX_CWND:
3749 		{
3750 			struct sctp_assoc_value *av;
3751 
3752 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3753 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3754 
3755 			if (stcb) {
3756 				av->assoc_value = stcb->asoc.max_cwnd;
3757 				SCTP_TCB_UNLOCK(stcb);
3758 			} else {
3759 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3760 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3761 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3762 					SCTP_INP_RLOCK(inp);
3763 					av->assoc_value = inp->max_cwnd;
3764 					SCTP_INP_RUNLOCK(inp);
3765 				} else {
3766 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3767 					error = EINVAL;
3768 				}
3769 			}
3770 			if (error == 0) {
3771 				*optsize = sizeof(struct sctp_assoc_value);
3772 			}
3773 			break;
3774 		}
3775 	default:
3776 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3777 		error = ENOPROTOOPT;
3778 		break;
3779 	}			/* end switch (sopt->sopt_name) */
3780 	if (error) {
3781 		*optsize = 0;
3782 	}
3783 	return (error);
3784 }
3785 
3786 static int
3787 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3788     void *p)
3789 {
3790 	int error, set_opt;
3791 	uint32_t *mopt;
3792 	struct sctp_tcb *stcb = NULL;
3793 	struct sctp_inpcb *inp = NULL;
3794 	uint32_t vrf_id;
3795 
3796 	if (optval == NULL) {
3797 		SCTP_PRINTF("optval is NULL\n");
3798 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3799 		return (EINVAL);
3800 	}
3801 	inp = (struct sctp_inpcb *)so->so_pcb;
3802 	if (inp == NULL) {
3803 		SCTP_PRINTF("inp is NULL?\n");
3804 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3805 		return (EINVAL);
3806 	}
3807 	vrf_id = inp->def_vrf_id;
3808 
3809 	error = 0;
3810 	switch (optname) {
3811 	case SCTP_NODELAY:
3812 	case SCTP_AUTOCLOSE:
3813 	case SCTP_AUTO_ASCONF:
3814 	case SCTP_EXPLICIT_EOR:
3815 	case SCTP_DISABLE_FRAGMENTS:
3816 	case SCTP_USE_EXT_RCVINFO:
3817 	case SCTP_I_WANT_MAPPED_V4_ADDR:
3818 		/* copy in the option value */
3819 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3820 		set_opt = 0;
3821 		if (error)
3822 			break;
3823 		switch (optname) {
3824 		case SCTP_DISABLE_FRAGMENTS:
3825 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3826 			break;
3827 		case SCTP_AUTO_ASCONF:
3828 			/*
3829 			 * NOTE: we don't really support this flag
3830 			 */
3831 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3832 				/* only valid for bound all sockets */
3833 				if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3834 				    (*mopt != 0)) {
3835 					/* forbidden by admin */
3836 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3837 					return (EPERM);
3838 				}
3839 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3840 			} else {
3841 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3842 				return (EINVAL);
3843 			}
3844 			break;
3845 		case SCTP_EXPLICIT_EOR:
3846 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3847 			break;
3848 		case SCTP_USE_EXT_RCVINFO:
3849 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3850 			break;
3851 		case SCTP_I_WANT_MAPPED_V4_ADDR:
3852 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3853 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3854 			} else {
3855 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3856 				return (EINVAL);
3857 			}
3858 			break;
3859 		case SCTP_NODELAY:
3860 			set_opt = SCTP_PCB_FLAGS_NODELAY;
3861 			break;
3862 		case SCTP_AUTOCLOSE:
3863 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3864 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3865 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3866 				return (EINVAL);
3867 			}
3868 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3869 			/*
3870 			 * The value is in ticks. Note this does not effect
3871 			 * old associations, only new ones.
3872 			 */
3873 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3874 			break;
3875 		}
3876 		SCTP_INP_WLOCK(inp);
3877 		if (*mopt != 0) {
3878 			sctp_feature_on(inp, set_opt);
3879 		} else {
3880 			sctp_feature_off(inp, set_opt);
3881 		}
3882 		SCTP_INP_WUNLOCK(inp);
3883 		break;
3884 	case SCTP_REUSE_PORT:
3885 		{
3886 			SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3887 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3888 				/* Can't set it after we are bound */
3889 				error = EINVAL;
3890 				break;
3891 			}
3892 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3893 				/* Can't do this for a 1-m socket */
3894 				error = EINVAL;
3895 				break;
3896 			}
3897 			if (optval)
3898 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3899 			else
3900 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3901 			break;
3902 		}
3903 	case SCTP_PARTIAL_DELIVERY_POINT:
3904 		{
3905 			uint32_t *value;
3906 
3907 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3908 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
3909 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3910 				error = EINVAL;
3911 				break;
3912 			}
3913 			inp->partial_delivery_point = *value;
3914 			break;
3915 		}
3916 	case SCTP_FRAGMENT_INTERLEAVE:
3917 		/* not yet until we re-write sctp_recvmsg() */
3918 		{
3919 			uint32_t *level;
3920 
3921 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3922 			if (*level == SCTP_FRAG_LEVEL_2) {
3923 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3924 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3925 			} else if (*level == SCTP_FRAG_LEVEL_1) {
3926 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3927 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3928 			} else if (*level == SCTP_FRAG_LEVEL_0) {
3929 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3930 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3931 
3932 			} else {
3933 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3934 				error = EINVAL;
3935 			}
3936 			break;
3937 		}
3938 	case SCTP_INTERLEAVING_SUPPORTED:
3939 		{
3940 			struct sctp_assoc_value *av;
3941 
3942 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3943 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3944 
3945 			if (stcb) {
3946 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3947 				error = EINVAL;
3948 				SCTP_TCB_UNLOCK(stcb);
3949 			} else {
3950 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3951 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3952 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3953 					SCTP_INP_WLOCK(inp);
3954 					if (av->assoc_value == 0) {
3955 						inp->idata_supported = 0;
3956 					} else {
3957 						if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
3958 						    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS))) {
3959 							inp->idata_supported = 1;
3960 						} else {
3961 							/*
3962 							 * Must have Frag
3963 							 * interleave and
3964 							 * stream interleave
3965 							 * on
3966 							 */
3967 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3968 							error = EINVAL;
3969 						}
3970 					}
3971 					SCTP_INP_WUNLOCK(inp);
3972 				} else {
3973 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3974 					error = EINVAL;
3975 				}
3976 			}
3977 			break;
3978 		}
3979 	case SCTP_CMT_ON_OFF:
3980 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3981 			struct sctp_assoc_value *av;
3982 
3983 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3984 			if (av->assoc_value > SCTP_CMT_MAX) {
3985 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3986 				error = EINVAL;
3987 				break;
3988 			}
3989 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3990 			if (stcb) {
3991 				stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3992 				SCTP_TCB_UNLOCK(stcb);
3993 			} else {
3994 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3995 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3996 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3997 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3998 					SCTP_INP_WLOCK(inp);
3999 					inp->sctp_cmt_on_off = av->assoc_value;
4000 					SCTP_INP_WUNLOCK(inp);
4001 				}
4002 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4003 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4004 					SCTP_INP_RLOCK(inp);
4005 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4006 						SCTP_TCB_LOCK(stcb);
4007 						stcb->asoc.sctp_cmt_on_off = av->assoc_value;
4008 						SCTP_TCB_UNLOCK(stcb);
4009 					}
4010 					SCTP_INP_RUNLOCK(inp);
4011 				}
4012 			}
4013 		} else {
4014 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4015 			error = ENOPROTOOPT;
4016 		}
4017 		break;
4018 	case SCTP_PLUGGABLE_CC:
4019 		{
4020 			struct sctp_assoc_value *av;
4021 			struct sctp_nets *net;
4022 
4023 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4024 			if ((av->assoc_value != SCTP_CC_RFC2581) &&
4025 			    (av->assoc_value != SCTP_CC_HSTCP) &&
4026 			    (av->assoc_value != SCTP_CC_HTCP) &&
4027 			    (av->assoc_value != SCTP_CC_RTCC)) {
4028 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4029 				error = EINVAL;
4030 				break;
4031 			}
4032 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4033 			if (stcb) {
4034 				stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4035 				stcb->asoc.congestion_control_module = av->assoc_value;
4036 				if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4037 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4038 						stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4039 					}
4040 				}
4041 				SCTP_TCB_UNLOCK(stcb);
4042 			} else {
4043 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4044 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4045 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4046 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4047 					SCTP_INP_WLOCK(inp);
4048 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
4049 					SCTP_INP_WUNLOCK(inp);
4050 				}
4051 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4052 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4053 					SCTP_INP_RLOCK(inp);
4054 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4055 						SCTP_TCB_LOCK(stcb);
4056 						stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4057 						stcb->asoc.congestion_control_module = av->assoc_value;
4058 						if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4059 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4060 								stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4061 							}
4062 						}
4063 						SCTP_TCB_UNLOCK(stcb);
4064 					}
4065 					SCTP_INP_RUNLOCK(inp);
4066 				}
4067 			}
4068 			break;
4069 		}
4070 	case SCTP_CC_OPTION:
4071 		{
4072 			struct sctp_cc_option *cc_opt;
4073 
4074 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
4075 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
4076 			if (stcb == NULL) {
4077 				if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
4078 					SCTP_INP_RLOCK(inp);
4079 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4080 						SCTP_TCB_LOCK(stcb);
4081 						if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
4082 							(*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
4083 						}
4084 						SCTP_TCB_UNLOCK(stcb);
4085 					}
4086 					SCTP_INP_RUNLOCK(inp);
4087 				} else {
4088 					error = EINVAL;
4089 				}
4090 			} else {
4091 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4092 					error = ENOTSUP;
4093 				} else {
4094 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4095 					    cc_opt);
4096 				}
4097 				SCTP_TCB_UNLOCK(stcb);
4098 			}
4099 			break;
4100 		}
4101 	case SCTP_PLUGGABLE_SS:
4102 		{
4103 			struct sctp_assoc_value *av;
4104 
4105 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4106 			if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4107 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4108 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4109 			    (av->assoc_value != SCTP_SS_PRIORITY) &&
4110 			    (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4111 			    (av->assoc_value != SCTP_SS_FIRST_COME)) {
4112 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4113 				error = EINVAL;
4114 				break;
4115 			}
4116 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4117 			if (stcb) {
4118 				stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4119 				stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4120 				stcb->asoc.stream_scheduling_module = av->assoc_value;
4121 				stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4122 				SCTP_TCB_UNLOCK(stcb);
4123 			} else {
4124 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4125 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4126 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4127 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4128 					SCTP_INP_WLOCK(inp);
4129 					inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4130 					SCTP_INP_WUNLOCK(inp);
4131 				}
4132 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4133 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4134 					SCTP_INP_RLOCK(inp);
4135 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4136 						SCTP_TCB_LOCK(stcb);
4137 						stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4138 						stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4139 						stcb->asoc.stream_scheduling_module = av->assoc_value;
4140 						stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4141 						SCTP_TCB_UNLOCK(stcb);
4142 					}
4143 					SCTP_INP_RUNLOCK(inp);
4144 				}
4145 			}
4146 			break;
4147 		}
4148 	case SCTP_SS_VALUE:
4149 		{
4150 			struct sctp_stream_value *av;
4151 
4152 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4153 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4154 			if (stcb) {
4155 				if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4156 				    (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4157 				    av->stream_value) < 0)) {
4158 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4159 					error = EINVAL;
4160 				}
4161 				SCTP_TCB_UNLOCK(stcb);
4162 			} else {
4163 				if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4164 					SCTP_INP_RLOCK(inp);
4165 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4166 						SCTP_TCB_LOCK(stcb);
4167 						if (av->stream_id < stcb->asoc.streamoutcnt) {
4168 							stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4169 							    &stcb->asoc,
4170 							    &stcb->asoc.strmout[av->stream_id],
4171 							    av->stream_value);
4172 						}
4173 						SCTP_TCB_UNLOCK(stcb);
4174 					}
4175 					SCTP_INP_RUNLOCK(inp);
4176 				} else {
4177 					/*
4178 					 * Can't set stream value without
4179 					 * association
4180 					 */
4181 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4182 					error = EINVAL;
4183 				}
4184 			}
4185 			break;
4186 		}
4187 	case SCTP_CLR_STAT_LOG:
4188 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4189 		error = EOPNOTSUPP;
4190 		break;
4191 	case SCTP_CONTEXT:
4192 		{
4193 			struct sctp_assoc_value *av;
4194 
4195 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4196 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4197 
4198 			if (stcb) {
4199 				stcb->asoc.context = av->assoc_value;
4200 				SCTP_TCB_UNLOCK(stcb);
4201 			} else {
4202 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4203 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4204 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4205 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4206 					SCTP_INP_WLOCK(inp);
4207 					inp->sctp_context = av->assoc_value;
4208 					SCTP_INP_WUNLOCK(inp);
4209 				}
4210 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4211 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4212 					SCTP_INP_RLOCK(inp);
4213 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4214 						SCTP_TCB_LOCK(stcb);
4215 						stcb->asoc.context = av->assoc_value;
4216 						SCTP_TCB_UNLOCK(stcb);
4217 					}
4218 					SCTP_INP_RUNLOCK(inp);
4219 				}
4220 			}
4221 			break;
4222 		}
4223 	case SCTP_VRF_ID:
4224 		{
4225 			uint32_t *default_vrfid;
4226 
4227 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4228 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
4229 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4230 				error = EINVAL;
4231 				break;
4232 			}
4233 			inp->def_vrf_id = *default_vrfid;
4234 			break;
4235 		}
4236 	case SCTP_DEL_VRF_ID:
4237 		{
4238 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4239 			error = EOPNOTSUPP;
4240 			break;
4241 		}
4242 	case SCTP_ADD_VRF_ID:
4243 		{
4244 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4245 			error = EOPNOTSUPP;
4246 			break;
4247 		}
4248 	case SCTP_DELAYED_SACK:
4249 		{
4250 			struct sctp_sack_info *sack;
4251 
4252 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4253 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4254 			if (sack->sack_delay) {
4255 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4256 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
4257 				if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4258 					sack->sack_delay = TICKS_TO_MSEC(1);
4259 				}
4260 			}
4261 			if (stcb) {
4262 				if (sack->sack_delay) {
4263 					stcb->asoc.delayed_ack = sack->sack_delay;
4264 				}
4265 				if (sack->sack_freq) {
4266 					stcb->asoc.sack_freq = sack->sack_freq;
4267 				}
4268 				SCTP_TCB_UNLOCK(stcb);
4269 			} else {
4270 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4271 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4272 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4273 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4274 					SCTP_INP_WLOCK(inp);
4275 					if (sack->sack_delay) {
4276 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4277 					}
4278 					if (sack->sack_freq) {
4279 						inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4280 					}
4281 					SCTP_INP_WUNLOCK(inp);
4282 				}
4283 				if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4284 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4285 					SCTP_INP_RLOCK(inp);
4286 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4287 						SCTP_TCB_LOCK(stcb);
4288 						if (sack->sack_delay) {
4289 							stcb->asoc.delayed_ack = sack->sack_delay;
4290 						}
4291 						if (sack->sack_freq) {
4292 							stcb->asoc.sack_freq = sack->sack_freq;
4293 						}
4294 						SCTP_TCB_UNLOCK(stcb);
4295 					}
4296 					SCTP_INP_RUNLOCK(inp);
4297 				}
4298 			}
4299 			break;
4300 		}
4301 	case SCTP_AUTH_CHUNK:
4302 		{
4303 			struct sctp_authchunk *sauth;
4304 
4305 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4306 
4307 			SCTP_INP_WLOCK(inp);
4308 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4309 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4310 				error = EINVAL;
4311 			}
4312 			SCTP_INP_WUNLOCK(inp);
4313 			break;
4314 		}
4315 	case SCTP_AUTH_KEY:
4316 		{
4317 			struct sctp_authkey *sca;
4318 			struct sctp_keyhead *shared_keys;
4319 			sctp_sharedkey_t *shared_key;
4320 			sctp_key_t *key = NULL;
4321 			size_t size;
4322 
4323 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4324 			if (sca->sca_keylength == 0) {
4325 				size = optsize - sizeof(struct sctp_authkey);
4326 			} else {
4327 				if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4328 					size = sca->sca_keylength;
4329 				} else {
4330 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4331 					error = EINVAL;
4332 					break;
4333 				}
4334 			}
4335 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4336 
4337 			if (stcb) {
4338 				shared_keys = &stcb->asoc.shared_keys;
4339 				/* clear the cached keys for this key id */
4340 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4341 				/*
4342 				 * create the new shared key and
4343 				 * insert/replace it
4344 				 */
4345 				if (size > 0) {
4346 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
4347 					if (key == NULL) {
4348 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4349 						error = ENOMEM;
4350 						SCTP_TCB_UNLOCK(stcb);
4351 						break;
4352 					}
4353 				}
4354 				shared_key = sctp_alloc_sharedkey();
4355 				if (shared_key == NULL) {
4356 					sctp_free_key(key);
4357 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4358 					error = ENOMEM;
4359 					SCTP_TCB_UNLOCK(stcb);
4360 					break;
4361 				}
4362 				shared_key->key = key;
4363 				shared_key->keyid = sca->sca_keynumber;
4364 				error = sctp_insert_sharedkey(shared_keys, shared_key);
4365 				SCTP_TCB_UNLOCK(stcb);
4366 			} else {
4367 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4368 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4369 				    (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4370 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4371 					SCTP_INP_WLOCK(inp);
4372 					shared_keys = &inp->sctp_ep.shared_keys;
4373 					/*
4374 					 * clear the cached keys on all
4375 					 * assocs for this key id
4376 					 */
4377 					sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4378 					/*
4379 					 * create the new shared key and
4380 					 * insert/replace it
4381 					 */
4382 					if (size > 0) {
4383 						key = sctp_set_key(sca->sca_key, (uint32_t) size);
4384 						if (key == NULL) {
4385 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4386 							error = ENOMEM;
4387 							SCTP_INP_WUNLOCK(inp);
4388 							break;
4389 						}
4390 					}
4391 					shared_key = sctp_alloc_sharedkey();
4392 					if (shared_key == NULL) {
4393 						sctp_free_key(key);
4394 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4395 						error = ENOMEM;
4396 						SCTP_INP_WUNLOCK(inp);
4397 						break;
4398 					}
4399 					shared_key->key = key;
4400 					shared_key->keyid = sca->sca_keynumber;
4401 					error = sctp_insert_sharedkey(shared_keys, shared_key);
4402 					SCTP_INP_WUNLOCK(inp);
4403 				}
4404 				if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4405 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4406 					SCTP_INP_RLOCK(inp);
4407 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4408 						SCTP_TCB_LOCK(stcb);
4409 						shared_keys = &stcb->asoc.shared_keys;
4410 						/*
4411 						 * clear the cached keys for
4412 						 * this key id
4413 						 */
4414 						sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4415 						/*
4416 						 * create the new shared key
4417 						 * and insert/replace it
4418 						 */
4419 						if (size > 0) {
4420 							key = sctp_set_key(sca->sca_key, (uint32_t) size);
4421 							if (key == NULL) {
4422 								SCTP_TCB_UNLOCK(stcb);
4423 								continue;
4424 							}
4425 						}
4426 						shared_key = sctp_alloc_sharedkey();
4427 						if (shared_key == NULL) {
4428 							sctp_free_key(key);
4429 							SCTP_TCB_UNLOCK(stcb);
4430 							continue;
4431 						}
4432 						shared_key->key = key;
4433 						shared_key->keyid = sca->sca_keynumber;
4434 						error = sctp_insert_sharedkey(shared_keys, shared_key);
4435 						SCTP_TCB_UNLOCK(stcb);
4436 					}
4437 					SCTP_INP_RUNLOCK(inp);
4438 				}
4439 			}
4440 			break;
4441 		}
4442 	case SCTP_HMAC_IDENT:
4443 		{
4444 			struct sctp_hmacalgo *shmac;
4445 			sctp_hmaclist_t *hmaclist;
4446 			uint16_t hmacid;
4447 			uint32_t i;
4448 
4449 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4450 			if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4451 			    (shmac->shmac_number_of_idents > 0xffff)) {
4452 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4453 				error = EINVAL;
4454 				break;
4455 			}
4456 			hmaclist = sctp_alloc_hmaclist((uint16_t) shmac->shmac_number_of_idents);
4457 			if (hmaclist == NULL) {
4458 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4459 				error = ENOMEM;
4460 				break;
4461 			}
4462 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4463 				hmacid = shmac->shmac_idents[i];
4464 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4465 					 /* invalid HMACs were found */ ;
4466 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4467 					error = EINVAL;
4468 					sctp_free_hmaclist(hmaclist);
4469 					goto sctp_set_hmac_done;
4470 				}
4471 			}
4472 			for (i = 0; i < hmaclist->num_algo; i++) {
4473 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4474 					/* already in list */
4475 					break;
4476 				}
4477 			}
4478 			if (i == hmaclist->num_algo) {
4479 				/* not found in list */
4480 				sctp_free_hmaclist(hmaclist);
4481 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4482 				error = EINVAL;
4483 				break;
4484 			}
4485 			/* set it on the endpoint */
4486 			SCTP_INP_WLOCK(inp);
4487 			if (inp->sctp_ep.local_hmacs)
4488 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4489 			inp->sctp_ep.local_hmacs = hmaclist;
4490 			SCTP_INP_WUNLOCK(inp);
4491 	sctp_set_hmac_done:
4492 			break;
4493 		}
4494 	case SCTP_AUTH_ACTIVE_KEY:
4495 		{
4496 			struct sctp_authkeyid *scact;
4497 
4498 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4499 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4500 
4501 			/* set the active key on the right place */
4502 			if (stcb) {
4503 				/* set the active key on the assoc */
4504 				if (sctp_auth_setactivekey(stcb,
4505 				    scact->scact_keynumber)) {
4506 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4507 					    SCTP_FROM_SCTP_USRREQ,
4508 					    EINVAL);
4509 					error = EINVAL;
4510 				}
4511 				SCTP_TCB_UNLOCK(stcb);
4512 			} else {
4513 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4514 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4515 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4516 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4517 					SCTP_INP_WLOCK(inp);
4518 					if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4519 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4520 						error = EINVAL;
4521 					}
4522 					SCTP_INP_WUNLOCK(inp);
4523 				}
4524 				if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4525 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4526 					SCTP_INP_RLOCK(inp);
4527 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4528 						SCTP_TCB_LOCK(stcb);
4529 						sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4530 						SCTP_TCB_UNLOCK(stcb);
4531 					}
4532 					SCTP_INP_RUNLOCK(inp);
4533 				}
4534 			}
4535 			break;
4536 		}
4537 	case SCTP_AUTH_DELETE_KEY:
4538 		{
4539 			struct sctp_authkeyid *scdel;
4540 
4541 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4542 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4543 
4544 			/* delete the key from the right place */
4545 			if (stcb) {
4546 				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4547 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4548 					error = EINVAL;
4549 				}
4550 				SCTP_TCB_UNLOCK(stcb);
4551 			} else {
4552 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4553 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4554 				    (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4555 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4556 					SCTP_INP_WLOCK(inp);
4557 					if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4558 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4559 						error = EINVAL;
4560 					}
4561 					SCTP_INP_WUNLOCK(inp);
4562 				}
4563 				if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4564 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4565 					SCTP_INP_RLOCK(inp);
4566 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4567 						SCTP_TCB_LOCK(stcb);
4568 						sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4569 						SCTP_TCB_UNLOCK(stcb);
4570 					}
4571 					SCTP_INP_RUNLOCK(inp);
4572 				}
4573 			}
4574 			break;
4575 		}
4576 	case SCTP_AUTH_DEACTIVATE_KEY:
4577 		{
4578 			struct sctp_authkeyid *keyid;
4579 
4580 			SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4581 			SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4582 
4583 			/* deactivate the key from the right place */
4584 			if (stcb) {
4585 				if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4586 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4587 					error = EINVAL;
4588 				}
4589 				SCTP_TCB_UNLOCK(stcb);
4590 			} else {
4591 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4592 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4593 				    (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4594 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4595 					SCTP_INP_WLOCK(inp);
4596 					if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4597 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4598 						error = EINVAL;
4599 					}
4600 					SCTP_INP_WUNLOCK(inp);
4601 				}
4602 				if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4603 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4604 					SCTP_INP_RLOCK(inp);
4605 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4606 						SCTP_TCB_LOCK(stcb);
4607 						sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4608 						SCTP_TCB_UNLOCK(stcb);
4609 					}
4610 					SCTP_INP_RUNLOCK(inp);
4611 				}
4612 			}
4613 			break;
4614 		}
4615 	case SCTP_ENABLE_STREAM_RESET:
4616 		{
4617 			struct sctp_assoc_value *av;
4618 
4619 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4620 			if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4621 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4622 				error = EINVAL;
4623 				break;
4624 			}
4625 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4626 			if (stcb) {
4627 				stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4628 				SCTP_TCB_UNLOCK(stcb);
4629 			} else {
4630 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4631 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4632 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4633 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4634 					SCTP_INP_WLOCK(inp);
4635 					inp->local_strreset_support = (uint8_t) av->assoc_value;
4636 					SCTP_INP_WUNLOCK(inp);
4637 				}
4638 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4639 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4640 					SCTP_INP_RLOCK(inp);
4641 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4642 						SCTP_TCB_LOCK(stcb);
4643 						stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4644 						SCTP_TCB_UNLOCK(stcb);
4645 					}
4646 					SCTP_INP_RUNLOCK(inp);
4647 				}
4648 			}
4649 			break;
4650 		}
4651 	case SCTP_RESET_STREAMS:
4652 		{
4653 			struct sctp_reset_streams *strrst;
4654 			int i, send_out = 0;
4655 			int send_in = 0;
4656 
4657 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4658 			SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4659 			if (stcb == NULL) {
4660 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4661 				error = ENOENT;
4662 				break;
4663 			}
4664 			if (stcb->asoc.reconfig_supported == 0) {
4665 				/*
4666 				 * Peer does not support the chunk type.
4667 				 */
4668 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4669 				error = EOPNOTSUPP;
4670 				SCTP_TCB_UNLOCK(stcb);
4671 				break;
4672 			}
4673 			if (sizeof(struct sctp_reset_streams) +
4674 			    strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4675 				error = EINVAL;
4676 				SCTP_TCB_UNLOCK(stcb);
4677 				break;
4678 			}
4679 			if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4680 				send_in = 1;
4681 				if (stcb->asoc.stream_reset_outstanding) {
4682 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4683 					error = EALREADY;
4684 					SCTP_TCB_UNLOCK(stcb);
4685 					break;
4686 				}
4687 			}
4688 			if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4689 				send_out = 1;
4690 			}
4691 			if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4692 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4693 				error = ENOMEM;
4694 				SCTP_TCB_UNLOCK(stcb);
4695 				break;
4696 			}
4697 			if ((send_in == 0) && (send_out == 0)) {
4698 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4699 				error = EINVAL;
4700 				SCTP_TCB_UNLOCK(stcb);
4701 				break;
4702 			}
4703 			for (i = 0; i < strrst->srs_number_streams; i++) {
4704 				if ((send_in) &&
4705 				    (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4706 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4707 					error = EINVAL;
4708 					break;
4709 				}
4710 				if ((send_out) &&
4711 				    (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4712 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4713 					error = EINVAL;
4714 					break;
4715 				}
4716 			}
4717 			if (error) {
4718 				SCTP_TCB_UNLOCK(stcb);
4719 				break;
4720 			}
4721 			if (send_out) {
4722 				int cnt;
4723 				uint16_t strm;
4724 
4725 				if (strrst->srs_number_streams) {
4726 					for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4727 						strm = strrst->srs_stream_list[i];
4728 						if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4729 							stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4730 							cnt++;
4731 						}
4732 					}
4733 				} else {
4734 					/* Its all */
4735 					for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4736 						if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4737 							stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4738 							cnt++;
4739 						}
4740 					}
4741 				}
4742 			}
4743 			if (send_in) {
4744 				error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4745 				    strrst->srs_stream_list,
4746 				    send_in, 0, 0, 0, 0, 0);
4747 			} else {
4748 				error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4749 			}
4750 			if (error == 0) {
4751 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4752 			} else {
4753 				/*
4754 				 * For outgoing streams don't report any
4755 				 * problems in sending the request to the
4756 				 * application. XXX: Double check resetting
4757 				 * incoming streams.
4758 				 */
4759 				error = 0;
4760 			}
4761 			SCTP_TCB_UNLOCK(stcb);
4762 			break;
4763 		}
4764 	case SCTP_ADD_STREAMS:
4765 		{
4766 			struct sctp_add_streams *stradd;
4767 			uint8_t addstream = 0;
4768 			uint16_t add_o_strmcnt = 0;
4769 			uint16_t add_i_strmcnt = 0;
4770 
4771 			SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4772 			SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4773 			if (stcb == NULL) {
4774 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4775 				error = ENOENT;
4776 				break;
4777 			}
4778 			if (stcb->asoc.reconfig_supported == 0) {
4779 				/*
4780 				 * Peer does not support the chunk type.
4781 				 */
4782 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4783 				error = EOPNOTSUPP;
4784 				SCTP_TCB_UNLOCK(stcb);
4785 				break;
4786 			}
4787 			if (stcb->asoc.stream_reset_outstanding) {
4788 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4789 				error = EALREADY;
4790 				SCTP_TCB_UNLOCK(stcb);
4791 				break;
4792 			}
4793 			if ((stradd->sas_outstrms == 0) &&
4794 			    (stradd->sas_instrms == 0)) {
4795 				error = EINVAL;
4796 				goto skip_stuff;
4797 			}
4798 			if (stradd->sas_outstrms) {
4799 				addstream = 1;
4800 				/* We allocate here */
4801 				add_o_strmcnt = stradd->sas_outstrms;
4802 				if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4803 					/* You can't have more than 64k */
4804 					error = EINVAL;
4805 					goto skip_stuff;
4806 				}
4807 			}
4808 			if (stradd->sas_instrms) {
4809 				int cnt;
4810 
4811 				addstream |= 2;
4812 				/*
4813 				 * We allocate inside
4814 				 * sctp_send_str_reset_req()
4815 				 */
4816 				add_i_strmcnt = stradd->sas_instrms;
4817 				cnt = add_i_strmcnt;
4818 				cnt += stcb->asoc.streamincnt;
4819 				if (cnt > 0x0000ffff) {
4820 					/* You can't have more than 64k */
4821 					error = EINVAL;
4822 					goto skip_stuff;
4823 				}
4824 				if (cnt > (int)stcb->asoc.max_inbound_streams) {
4825 					/* More than you are allowed */
4826 					error = EINVAL;
4827 					goto skip_stuff;
4828 				}
4829 			}
4830 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4831 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4832 	skip_stuff:
4833 			SCTP_TCB_UNLOCK(stcb);
4834 			break;
4835 		}
4836 	case SCTP_RESET_ASSOC:
4837 		{
4838 			int i;
4839 			uint32_t *value;
4840 
4841 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4842 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
4843 			if (stcb == NULL) {
4844 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4845 				error = ENOENT;
4846 				break;
4847 			}
4848 			if (stcb->asoc.reconfig_supported == 0) {
4849 				/*
4850 				 * Peer does not support the chunk type.
4851 				 */
4852 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4853 				error = EOPNOTSUPP;
4854 				SCTP_TCB_UNLOCK(stcb);
4855 				break;
4856 			}
4857 			if (stcb->asoc.stream_reset_outstanding) {
4858 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4859 				error = EALREADY;
4860 				SCTP_TCB_UNLOCK(stcb);
4861 				break;
4862 			}
4863 			/*
4864 			 * Is there any data pending in the send or sent
4865 			 * queues?
4866 			 */
4867 			if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4868 			    !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4869 		busy_out:
4870 				error = EBUSY;
4871 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4872 				SCTP_TCB_UNLOCK(stcb);
4873 				break;
4874 			}
4875 			/* Do any streams have data queued? */
4876 			for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4877 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4878 					goto busy_out;
4879 				}
4880 			}
4881 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4882 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4883 			SCTP_TCB_UNLOCK(stcb);
4884 			break;
4885 		}
4886 	case SCTP_CONNECT_X:
4887 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4888 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4889 			error = EINVAL;
4890 			break;
4891 		}
4892 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4893 		break;
4894 	case SCTP_CONNECT_X_DELAYED:
4895 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4896 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4897 			error = EINVAL;
4898 			break;
4899 		}
4900 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4901 		break;
4902 	case SCTP_CONNECT_X_COMPLETE:
4903 		{
4904 			struct sockaddr *sa;
4905 
4906 			/* FIXME MT: check correct? */
4907 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4908 
4909 			/* find tcb */
4910 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4911 				SCTP_INP_RLOCK(inp);
4912 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
4913 				if (stcb) {
4914 					SCTP_TCB_LOCK(stcb);
4915 				}
4916 				SCTP_INP_RUNLOCK(inp);
4917 			} else {
4918 				/*
4919 				 * We increment here since
4920 				 * sctp_findassociation_ep_addr() wil do a
4921 				 * decrement if it finds the stcb as long as
4922 				 * the locked tcb (last argument) is NOT a
4923 				 * TCB.. aka NULL.
4924 				 */
4925 				SCTP_INP_INCR_REF(inp);
4926 				stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4927 				if (stcb == NULL) {
4928 					SCTP_INP_DECR_REF(inp);
4929 				}
4930 			}
4931 
4932 			if (stcb == NULL) {
4933 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4934 				error = ENOENT;
4935 				break;
4936 			}
4937 			if (stcb->asoc.delayed_connection == 1) {
4938 				stcb->asoc.delayed_connection = 0;
4939 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4940 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4941 				    stcb->asoc.primary_destination,
4942 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4943 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4944 			} else {
4945 				/*
4946 				 * already expired or did not use delayed
4947 				 * connectx
4948 				 */
4949 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4950 				error = EALREADY;
4951 			}
4952 			SCTP_TCB_UNLOCK(stcb);
4953 			break;
4954 		}
4955 	case SCTP_MAX_BURST:
4956 		{
4957 			struct sctp_assoc_value *av;
4958 
4959 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4960 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4961 
4962 			if (stcb) {
4963 				stcb->asoc.max_burst = av->assoc_value;
4964 				SCTP_TCB_UNLOCK(stcb);
4965 			} else {
4966 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4967 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4968 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4969 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4970 					SCTP_INP_WLOCK(inp);
4971 					inp->sctp_ep.max_burst = av->assoc_value;
4972 					SCTP_INP_WUNLOCK(inp);
4973 				}
4974 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4975 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4976 					SCTP_INP_RLOCK(inp);
4977 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4978 						SCTP_TCB_LOCK(stcb);
4979 						stcb->asoc.max_burst = av->assoc_value;
4980 						SCTP_TCB_UNLOCK(stcb);
4981 					}
4982 					SCTP_INP_RUNLOCK(inp);
4983 				}
4984 			}
4985 			break;
4986 		}
4987 	case SCTP_MAXSEG:
4988 		{
4989 			struct sctp_assoc_value *av;
4990 			int ovh;
4991 
4992 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4993 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4994 
4995 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4996 				ovh = SCTP_MED_OVERHEAD;
4997 			} else {
4998 				ovh = SCTP_MED_V4_OVERHEAD;
4999 			}
5000 			if (stcb) {
5001 				if (av->assoc_value) {
5002 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
5003 				} else {
5004 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
5005 				}
5006 				SCTP_TCB_UNLOCK(stcb);
5007 			} else {
5008 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5009 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5010 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
5011 					SCTP_INP_WLOCK(inp);
5012 					/*
5013 					 * FIXME MT: I think this is not in
5014 					 * tune with the API ID
5015 					 */
5016 					if (av->assoc_value) {
5017 						inp->sctp_frag_point = (av->assoc_value + ovh);
5018 					} else {
5019 						inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
5020 					}
5021 					SCTP_INP_WUNLOCK(inp);
5022 				} else {
5023 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5024 					error = EINVAL;
5025 				}
5026 			}
5027 			break;
5028 		}
5029 	case SCTP_EVENTS:
5030 		{
5031 			struct sctp_event_subscribe *events;
5032 
5033 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
5034 
5035 			SCTP_INP_WLOCK(inp);
5036 			if (events->sctp_data_io_event) {
5037 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5038 			} else {
5039 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5040 			}
5041 
5042 			if (events->sctp_association_event) {
5043 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5044 			} else {
5045 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5046 			}
5047 
5048 			if (events->sctp_address_event) {
5049 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5050 			} else {
5051 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5052 			}
5053 
5054 			if (events->sctp_send_failure_event) {
5055 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5056 			} else {
5057 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5058 			}
5059 
5060 			if (events->sctp_peer_error_event) {
5061 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5062 			} else {
5063 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5064 			}
5065 
5066 			if (events->sctp_shutdown_event) {
5067 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5068 			} else {
5069 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5070 			}
5071 
5072 			if (events->sctp_partial_delivery_event) {
5073 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5074 			} else {
5075 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5076 			}
5077 
5078 			if (events->sctp_adaptation_layer_event) {
5079 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5080 			} else {
5081 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5082 			}
5083 
5084 			if (events->sctp_authentication_event) {
5085 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5086 			} else {
5087 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5088 			}
5089 
5090 			if (events->sctp_sender_dry_event) {
5091 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5092 			} else {
5093 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5094 			}
5095 
5096 			if (events->sctp_stream_reset_event) {
5097 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5098 			} else {
5099 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5100 			}
5101 			SCTP_INP_WUNLOCK(inp);
5102 
5103 			SCTP_INP_RLOCK(inp);
5104 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5105 				SCTP_TCB_LOCK(stcb);
5106 				if (events->sctp_association_event) {
5107 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5108 				} else {
5109 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5110 				}
5111 				if (events->sctp_address_event) {
5112 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5113 				} else {
5114 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5115 				}
5116 				if (events->sctp_send_failure_event) {
5117 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5118 				} else {
5119 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5120 				}
5121 				if (events->sctp_peer_error_event) {
5122 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5123 				} else {
5124 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5125 				}
5126 				if (events->sctp_shutdown_event) {
5127 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5128 				} else {
5129 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5130 				}
5131 				if (events->sctp_partial_delivery_event) {
5132 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5133 				} else {
5134 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5135 				}
5136 				if (events->sctp_adaptation_layer_event) {
5137 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5138 				} else {
5139 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5140 				}
5141 				if (events->sctp_authentication_event) {
5142 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5143 				} else {
5144 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5145 				}
5146 				if (events->sctp_sender_dry_event) {
5147 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5148 				} else {
5149 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5150 				}
5151 				if (events->sctp_stream_reset_event) {
5152 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5153 				} else {
5154 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5155 				}
5156 				SCTP_TCB_UNLOCK(stcb);
5157 			}
5158 			/*
5159 			 * Send up the sender dry event only for 1-to-1
5160 			 * style sockets.
5161 			 */
5162 			if (events->sctp_sender_dry_event) {
5163 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5164 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5165 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
5166 					if (stcb) {
5167 						SCTP_TCB_LOCK(stcb);
5168 						if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5169 						    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5170 						    (stcb->asoc.stream_queue_cnt == 0)) {
5171 							sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5172 						}
5173 						SCTP_TCB_UNLOCK(stcb);
5174 					}
5175 				}
5176 			}
5177 			SCTP_INP_RUNLOCK(inp);
5178 			break;
5179 		}
5180 	case SCTP_ADAPTATION_LAYER:
5181 		{
5182 			struct sctp_setadaptation *adap_bits;
5183 
5184 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5185 			SCTP_INP_WLOCK(inp);
5186 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5187 			inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5188 			SCTP_INP_WUNLOCK(inp);
5189 			break;
5190 		}
5191 #ifdef SCTP_DEBUG
5192 	case SCTP_SET_INITIAL_DBG_SEQ:
5193 		{
5194 			uint32_t *vvv;
5195 
5196 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5197 			SCTP_INP_WLOCK(inp);
5198 			inp->sctp_ep.initial_sequence_debug = *vvv;
5199 			SCTP_INP_WUNLOCK(inp);
5200 			break;
5201 		}
5202 #endif
5203 	case SCTP_DEFAULT_SEND_PARAM:
5204 		{
5205 			struct sctp_sndrcvinfo *s_info;
5206 
5207 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5208 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5209 
5210 			if (stcb) {
5211 				if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5212 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5213 				} else {
5214 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5215 					error = EINVAL;
5216 				}
5217 				SCTP_TCB_UNLOCK(stcb);
5218 			} else {
5219 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5220 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5221 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5222 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5223 					SCTP_INP_WLOCK(inp);
5224 					memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5225 					SCTP_INP_WUNLOCK(inp);
5226 				}
5227 				if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5228 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5229 					SCTP_INP_RLOCK(inp);
5230 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5231 						SCTP_TCB_LOCK(stcb);
5232 						if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5233 							memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5234 						}
5235 						SCTP_TCB_UNLOCK(stcb);
5236 					}
5237 					SCTP_INP_RUNLOCK(inp);
5238 				}
5239 			}
5240 			break;
5241 		}
5242 	case SCTP_PEER_ADDR_PARAMS:
5243 		{
5244 			struct sctp_paddrparams *paddrp;
5245 			struct sctp_nets *net;
5246 			struct sockaddr *addr;
5247 
5248 #if defined(INET) && defined(INET6)
5249 			struct sockaddr_in sin_store;
5250 
5251 #endif
5252 
5253 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5254 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5255 
5256 #if defined(INET) && defined(INET6)
5257 			if (paddrp->spp_address.ss_family == AF_INET6) {
5258 				struct sockaddr_in6 *sin6;
5259 
5260 				sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5261 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5262 					in6_sin6_2_sin(&sin_store, sin6);
5263 					addr = (struct sockaddr *)&sin_store;
5264 				} else {
5265 					addr = (struct sockaddr *)&paddrp->spp_address;
5266 				}
5267 			} else {
5268 				addr = (struct sockaddr *)&paddrp->spp_address;
5269 			}
5270 #else
5271 			addr = (struct sockaddr *)&paddrp->spp_address;
5272 #endif
5273 			if (stcb != NULL) {
5274 				net = sctp_findnet(stcb, addr);
5275 			} else {
5276 				/*
5277 				 * We increment here since
5278 				 * sctp_findassociation_ep_addr() wil do a
5279 				 * decrement if it finds the stcb as long as
5280 				 * the locked tcb (last argument) is NOT a
5281 				 * TCB.. aka NULL.
5282 				 */
5283 				net = NULL;
5284 				SCTP_INP_INCR_REF(inp);
5285 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5286 				    &net, NULL, NULL);
5287 				if (stcb == NULL) {
5288 					SCTP_INP_DECR_REF(inp);
5289 				}
5290 			}
5291 			if ((stcb != NULL) && (net == NULL)) {
5292 #ifdef INET
5293 				if (addr->sa_family == AF_INET) {
5294 
5295 					struct sockaddr_in *sin;
5296 
5297 					sin = (struct sockaddr_in *)addr;
5298 					if (sin->sin_addr.s_addr != INADDR_ANY) {
5299 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5300 						SCTP_TCB_UNLOCK(stcb);
5301 						error = EINVAL;
5302 						break;
5303 					}
5304 				} else
5305 #endif
5306 #ifdef INET6
5307 				if (addr->sa_family == AF_INET6) {
5308 					struct sockaddr_in6 *sin6;
5309 
5310 					sin6 = (struct sockaddr_in6 *)addr;
5311 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5312 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5313 						SCTP_TCB_UNLOCK(stcb);
5314 						error = EINVAL;
5315 						break;
5316 					}
5317 				} else
5318 #endif
5319 				{
5320 					error = EAFNOSUPPORT;
5321 					SCTP_TCB_UNLOCK(stcb);
5322 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5323 					break;
5324 				}
5325 			}
5326 			/* sanity checks */
5327 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5328 				if (stcb)
5329 					SCTP_TCB_UNLOCK(stcb);
5330 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5331 				return (EINVAL);
5332 			}
5333 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5334 				if (stcb)
5335 					SCTP_TCB_UNLOCK(stcb);
5336 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5337 				return (EINVAL);
5338 			}
5339 			if (stcb != NULL) {
5340 				/************************TCB SPECIFIC SET ******************/
5341 				if (net != NULL) {
5342 					/************************NET SPECIFIC SET ******************/
5343 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5344 						if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5345 						    !(net->dest_state & SCTP_ADDR_NOHB)) {
5346 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5347 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5348 						}
5349 						net->dest_state |= SCTP_ADDR_NOHB;
5350 					}
5351 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5352 						if (paddrp->spp_hbinterval) {
5353 							net->heart_beat_delay = paddrp->spp_hbinterval;
5354 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5355 							net->heart_beat_delay = 0;
5356 						}
5357 						sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5358 						    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5359 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5360 						net->dest_state &= ~SCTP_ADDR_NOHB;
5361 					}
5362 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
5363 						/* on demand HB */
5364 						sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5365 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5366 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5367 					}
5368 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5369 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5370 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5371 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5372 						}
5373 						net->dest_state |= SCTP_ADDR_NO_PMTUD;
5374 						net->mtu = paddrp->spp_pathmtu;
5375 						switch (net->ro._l_addr.sa.sa_family) {
5376 #ifdef INET
5377 						case AF_INET:
5378 							net->mtu += SCTP_MIN_V4_OVERHEAD;
5379 							break;
5380 #endif
5381 #ifdef INET6
5382 						case AF_INET6:
5383 							net->mtu += SCTP_MIN_OVERHEAD;
5384 							break;
5385 #endif
5386 						default:
5387 							break;
5388 						}
5389 						if (net->mtu < stcb->asoc.smallest_mtu) {
5390 							sctp_pathmtu_adjustment(stcb, net->mtu);
5391 						}
5392 					}
5393 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5394 						if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5395 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5396 						}
5397 						net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5398 					}
5399 					if (paddrp->spp_pathmaxrxt) {
5400 						if (net->dest_state & SCTP_ADDR_PF) {
5401 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5402 								net->dest_state &= ~SCTP_ADDR_PF;
5403 							}
5404 						} else {
5405 							if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5406 							    (net->error_count > net->pf_threshold)) {
5407 								net->dest_state |= SCTP_ADDR_PF;
5408 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5409 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5410 								    stcb->sctp_ep, stcb, net,
5411 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5412 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5413 							}
5414 						}
5415 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
5416 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5417 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
5418 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5419 							}
5420 						} else {
5421 							if (net->error_count <= paddrp->spp_pathmaxrxt) {
5422 								net->dest_state |= SCTP_ADDR_REACHABLE;
5423 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5424 							}
5425 						}
5426 						net->failure_threshold = paddrp->spp_pathmaxrxt;
5427 					}
5428 					if (paddrp->spp_flags & SPP_DSCP) {
5429 						net->dscp = paddrp->spp_dscp & 0xfc;
5430 						net->dscp |= 0x01;
5431 					}
5432 #ifdef INET6
5433 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5434 						if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5435 							net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5436 							net->flowlabel |= 0x80000000;
5437 						}
5438 					}
5439 #endif
5440 				} else {
5441 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5442 					if (paddrp->spp_pathmaxrxt != 0) {
5443 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5444 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5445 							if (net->dest_state & SCTP_ADDR_PF) {
5446 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5447 									net->dest_state &= ~SCTP_ADDR_PF;
5448 								}
5449 							} else {
5450 								if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5451 								    (net->error_count > net->pf_threshold)) {
5452 									net->dest_state |= SCTP_ADDR_PF;
5453 									sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5454 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5455 									    stcb->sctp_ep, stcb, net,
5456 									    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5457 									sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5458 								}
5459 							}
5460 							if (net->dest_state & SCTP_ADDR_REACHABLE) {
5461 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5462 									net->dest_state &= ~SCTP_ADDR_REACHABLE;
5463 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5464 								}
5465 							} else {
5466 								if (net->error_count <= paddrp->spp_pathmaxrxt) {
5467 									net->dest_state |= SCTP_ADDR_REACHABLE;
5468 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5469 								}
5470 							}
5471 							net->failure_threshold = paddrp->spp_pathmaxrxt;
5472 						}
5473 					}
5474 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5475 						if (paddrp->spp_hbinterval != 0) {
5476 							stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5477 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5478 							stcb->asoc.heart_beat_delay = 0;
5479 						}
5480 						/* Turn back on the timer */
5481 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5482 							if (paddrp->spp_hbinterval != 0) {
5483 								net->heart_beat_delay = paddrp->spp_hbinterval;
5484 							} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5485 								net->heart_beat_delay = 0;
5486 							}
5487 							if (net->dest_state & SCTP_ADDR_NOHB) {
5488 								net->dest_state &= ~SCTP_ADDR_NOHB;
5489 							}
5490 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5491 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5492 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5493 						}
5494 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5495 					}
5496 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5497 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5498 							if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5499 								net->dest_state |= SCTP_ADDR_NOHB;
5500 								if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5501 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5502 									    inp, stcb, net,
5503 									    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5504 								}
5505 							}
5506 						}
5507 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5508 					}
5509 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5510 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5511 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5512 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5513 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5514 							}
5515 							net->dest_state |= SCTP_ADDR_NO_PMTUD;
5516 							net->mtu = paddrp->spp_pathmtu;
5517 							switch (net->ro._l_addr.sa.sa_family) {
5518 #ifdef INET
5519 							case AF_INET:
5520 								net->mtu += SCTP_MIN_V4_OVERHEAD;
5521 								break;
5522 #endif
5523 #ifdef INET6
5524 							case AF_INET6:
5525 								net->mtu += SCTP_MIN_OVERHEAD;
5526 								break;
5527 #endif
5528 							default:
5529 								break;
5530 							}
5531 							if (net->mtu < stcb->asoc.smallest_mtu) {
5532 								sctp_pathmtu_adjustment(stcb, net->mtu);
5533 							}
5534 						}
5535 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5536 					}
5537 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5538 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5539 							if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5540 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5541 							}
5542 							net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5543 						}
5544 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5545 					}
5546 					if (paddrp->spp_flags & SPP_DSCP) {
5547 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5548 							net->dscp = paddrp->spp_dscp & 0xfc;
5549 							net->dscp |= 0x01;
5550 						}
5551 						stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5552 						stcb->asoc.default_dscp |= 0x01;
5553 					}
5554 #ifdef INET6
5555 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5556 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5557 							if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5558 								net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5559 								net->flowlabel |= 0x80000000;
5560 							}
5561 						}
5562 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5563 						stcb->asoc.default_flowlabel |= 0x80000000;
5564 					}
5565 #endif
5566 				}
5567 				SCTP_TCB_UNLOCK(stcb);
5568 			} else {
5569 				/************************NO TCB, SET TO default stuff ******************/
5570 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5571 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5572 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5573 					SCTP_INP_WLOCK(inp);
5574 					/*
5575 					 * For the TOS/FLOWLABEL stuff you
5576 					 * set it with the options on the
5577 					 * socket
5578 					 */
5579 					if (paddrp->spp_pathmaxrxt != 0) {
5580 						inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5581 					}
5582 					if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5583 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5584 					else if (paddrp->spp_hbinterval != 0) {
5585 						if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5586 							paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5587 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5588 					}
5589 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5590 						if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5591 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5592 						} else if (paddrp->spp_hbinterval) {
5593 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5594 						}
5595 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5596 					} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5597 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5598 					}
5599 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5600 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5601 					} else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5602 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5603 					}
5604 					if (paddrp->spp_flags & SPP_DSCP) {
5605 						inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5606 						inp->sctp_ep.default_dscp |= 0x01;
5607 					}
5608 #ifdef INET6
5609 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5610 						if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5611 							inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5612 							inp->sctp_ep.default_flowlabel |= 0x80000000;
5613 						}
5614 					}
5615 #endif
5616 					SCTP_INP_WUNLOCK(inp);
5617 				} else {
5618 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5619 					error = EINVAL;
5620 				}
5621 			}
5622 			break;
5623 		}
5624 	case SCTP_RTOINFO:
5625 		{
5626 			struct sctp_rtoinfo *srto;
5627 			uint32_t new_init, new_min, new_max;
5628 
5629 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5630 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5631 
5632 			if (stcb) {
5633 				if (srto->srto_initial)
5634 					new_init = srto->srto_initial;
5635 				else
5636 					new_init = stcb->asoc.initial_rto;
5637 				if (srto->srto_max)
5638 					new_max = srto->srto_max;
5639 				else
5640 					new_max = stcb->asoc.maxrto;
5641 				if (srto->srto_min)
5642 					new_min = srto->srto_min;
5643 				else
5644 					new_min = stcb->asoc.minrto;
5645 				if ((new_min <= new_init) && (new_init <= new_max)) {
5646 					stcb->asoc.initial_rto = new_init;
5647 					stcb->asoc.maxrto = new_max;
5648 					stcb->asoc.minrto = new_min;
5649 				} else {
5650 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5651 					error = EINVAL;
5652 				}
5653 				SCTP_TCB_UNLOCK(stcb);
5654 			} else {
5655 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5656 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5657 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5658 					SCTP_INP_WLOCK(inp);
5659 					if (srto->srto_initial)
5660 						new_init = srto->srto_initial;
5661 					else
5662 						new_init = inp->sctp_ep.initial_rto;
5663 					if (srto->srto_max)
5664 						new_max = srto->srto_max;
5665 					else
5666 						new_max = inp->sctp_ep.sctp_maxrto;
5667 					if (srto->srto_min)
5668 						new_min = srto->srto_min;
5669 					else
5670 						new_min = inp->sctp_ep.sctp_minrto;
5671 					if ((new_min <= new_init) && (new_init <= new_max)) {
5672 						inp->sctp_ep.initial_rto = new_init;
5673 						inp->sctp_ep.sctp_maxrto = new_max;
5674 						inp->sctp_ep.sctp_minrto = new_min;
5675 					} else {
5676 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5677 						error = EINVAL;
5678 					}
5679 					SCTP_INP_WUNLOCK(inp);
5680 				} else {
5681 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5682 					error = EINVAL;
5683 				}
5684 			}
5685 			break;
5686 		}
5687 	case SCTP_ASSOCINFO:
5688 		{
5689 			struct sctp_assocparams *sasoc;
5690 
5691 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5692 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5693 			if (sasoc->sasoc_cookie_life) {
5694 				/* boundary check the cookie life */
5695 				if (sasoc->sasoc_cookie_life < 1000)
5696 					sasoc->sasoc_cookie_life = 1000;
5697 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5698 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5699 				}
5700 			}
5701 			if (stcb) {
5702 				if (sasoc->sasoc_asocmaxrxt)
5703 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5704 				if (sasoc->sasoc_cookie_life) {
5705 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5706 				}
5707 				SCTP_TCB_UNLOCK(stcb);
5708 			} else {
5709 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5710 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5711 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5712 					SCTP_INP_WLOCK(inp);
5713 					if (sasoc->sasoc_asocmaxrxt)
5714 						inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5715 					if (sasoc->sasoc_cookie_life) {
5716 						inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5717 					}
5718 					SCTP_INP_WUNLOCK(inp);
5719 				} else {
5720 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5721 					error = EINVAL;
5722 				}
5723 			}
5724 			break;
5725 		}
5726 	case SCTP_INITMSG:
5727 		{
5728 			struct sctp_initmsg *sinit;
5729 
5730 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5731 			SCTP_INP_WLOCK(inp);
5732 			if (sinit->sinit_num_ostreams)
5733 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5734 
5735 			if (sinit->sinit_max_instreams)
5736 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5737 
5738 			if (sinit->sinit_max_attempts)
5739 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5740 
5741 			if (sinit->sinit_max_init_timeo)
5742 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5743 			SCTP_INP_WUNLOCK(inp);
5744 			break;
5745 		}
5746 	case SCTP_PRIMARY_ADDR:
5747 		{
5748 			struct sctp_setprim *spa;
5749 			struct sctp_nets *net;
5750 			struct sockaddr *addr;
5751 
5752 #if defined(INET) && defined(INET6)
5753 			struct sockaddr_in sin_store;
5754 
5755 #endif
5756 
5757 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5758 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5759 
5760 #if defined(INET) && defined(INET6)
5761 			if (spa->ssp_addr.ss_family == AF_INET6) {
5762 				struct sockaddr_in6 *sin6;
5763 
5764 				sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5765 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5766 					in6_sin6_2_sin(&sin_store, sin6);
5767 					addr = (struct sockaddr *)&sin_store;
5768 				} else {
5769 					addr = (struct sockaddr *)&spa->ssp_addr;
5770 				}
5771 			} else {
5772 				addr = (struct sockaddr *)&spa->ssp_addr;
5773 			}
5774 #else
5775 			addr = (struct sockaddr *)&spa->ssp_addr;
5776 #endif
5777 			if (stcb != NULL) {
5778 				net = sctp_findnet(stcb, addr);
5779 			} else {
5780 				/*
5781 				 * We increment here since
5782 				 * sctp_findassociation_ep_addr() wil do a
5783 				 * decrement if it finds the stcb as long as
5784 				 * the locked tcb (last argument) is NOT a
5785 				 * TCB.. aka NULL.
5786 				 */
5787 				net = NULL;
5788 				SCTP_INP_INCR_REF(inp);
5789 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5790 				    &net, NULL, NULL);
5791 				if (stcb == NULL) {
5792 					SCTP_INP_DECR_REF(inp);
5793 				}
5794 			}
5795 
5796 			if ((stcb != NULL) && (net != NULL)) {
5797 				if (net != stcb->asoc.primary_destination) {
5798 					if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5799 						/* Ok we need to set it */
5800 						if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5801 							if ((stcb->asoc.alternate) &&
5802 							    (!(net->dest_state & SCTP_ADDR_PF)) &&
5803 							    (net->dest_state & SCTP_ADDR_REACHABLE)) {
5804 								sctp_free_remote_addr(stcb->asoc.alternate);
5805 								stcb->asoc.alternate = NULL;
5806 							}
5807 						} else {
5808 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5809 							error = EINVAL;
5810 						}
5811 					} else {
5812 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5813 						error = EINVAL;
5814 					}
5815 				}
5816 			} else {
5817 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5818 				error = EINVAL;
5819 			}
5820 			if (stcb != NULL) {
5821 				SCTP_TCB_UNLOCK(stcb);
5822 			}
5823 			break;
5824 		}
5825 	case SCTP_SET_DYNAMIC_PRIMARY:
5826 		{
5827 			union sctp_sockstore *ss;
5828 
5829 			error = priv_check(curthread,
5830 			    PRIV_NETINET_RESERVEDPORT);
5831 			if (error)
5832 				break;
5833 
5834 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5835 			/* SUPER USER CHECK? */
5836 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5837 			break;
5838 		}
5839 	case SCTP_SET_PEER_PRIMARY_ADDR:
5840 		{
5841 			struct sctp_setpeerprim *sspp;
5842 			struct sockaddr *addr;
5843 
5844 #if defined(INET) && defined(INET6)
5845 			struct sockaddr_in sin_store;
5846 
5847 #endif
5848 
5849 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5850 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5851 			if (stcb != NULL) {
5852 				struct sctp_ifa *ifa;
5853 
5854 #if defined(INET) && defined(INET6)
5855 				if (sspp->sspp_addr.ss_family == AF_INET6) {
5856 					struct sockaddr_in6 *sin6;
5857 
5858 					sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5859 					if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5860 						in6_sin6_2_sin(&sin_store, sin6);
5861 						addr = (struct sockaddr *)&sin_store;
5862 					} else {
5863 						addr = (struct sockaddr *)&sspp->sspp_addr;
5864 					}
5865 				} else {
5866 					addr = (struct sockaddr *)&sspp->sspp_addr;
5867 				}
5868 #else
5869 				addr = (struct sockaddr *)&sspp->sspp_addr;
5870 #endif
5871 				ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5872 				if (ifa == NULL) {
5873 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5874 					error = EINVAL;
5875 					goto out_of_it;
5876 				}
5877 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5878 					/*
5879 					 * Must validate the ifa found is in
5880 					 * our ep
5881 					 */
5882 					struct sctp_laddr *laddr;
5883 					int found = 0;
5884 
5885 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5886 						if (laddr->ifa == NULL) {
5887 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5888 							    __func__);
5889 							continue;
5890 						}
5891 						if ((sctp_is_addr_restricted(stcb, laddr->ifa)) &&
5892 						    (!sctp_is_addr_pending(stcb, laddr->ifa))) {
5893 							continue;
5894 						}
5895 						if (laddr->ifa == ifa) {
5896 							found = 1;
5897 							break;
5898 						}
5899 					}
5900 					if (!found) {
5901 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5902 						error = EINVAL;
5903 						goto out_of_it;
5904 					}
5905 				} else {
5906 					switch (addr->sa_family) {
5907 #ifdef INET
5908 					case AF_INET:
5909 						{
5910 							struct sockaddr_in *sin;
5911 
5912 							sin = (struct sockaddr_in *)addr;
5913 							if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5914 							    &sin->sin_addr) != 0) {
5915 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5916 								error = EINVAL;
5917 								goto out_of_it;
5918 							}
5919 							break;
5920 						}
5921 #endif
5922 #ifdef INET6
5923 					case AF_INET6:
5924 						{
5925 							struct sockaddr_in6 *sin6;
5926 
5927 							sin6 = (struct sockaddr_in6 *)addr;
5928 							if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5929 							    &sin6->sin6_addr) != 0) {
5930 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5931 								error = EINVAL;
5932 								goto out_of_it;
5933 							}
5934 							break;
5935 						}
5936 #endif
5937 					default:
5938 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5939 						error = EINVAL;
5940 						goto out_of_it;
5941 					}
5942 				}
5943 				if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5944 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5945 					error = EINVAL;
5946 				}
5947 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5948 		out_of_it:
5949 				SCTP_TCB_UNLOCK(stcb);
5950 			} else {
5951 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5952 				error = EINVAL;
5953 			}
5954 			break;
5955 		}
5956 	case SCTP_BINDX_ADD_ADDR:
5957 		{
5958 			struct sctp_getaddresses *addrs;
5959 			struct thread *td;
5960 
5961 			td = (struct thread *)p;
5962 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5963 			    optsize);
5964 #ifdef INET
5965 			if (addrs->addr->sa_family == AF_INET) {
5966 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5967 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5968 					error = EINVAL;
5969 					break;
5970 				}
5971 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5972 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5973 					break;
5974 				}
5975 			} else
5976 #endif
5977 #ifdef INET6
5978 			if (addrs->addr->sa_family == AF_INET6) {
5979 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5980 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5981 					error = EINVAL;
5982 					break;
5983 				}
5984 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5985 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5986 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5987 					break;
5988 				}
5989 			} else
5990 #endif
5991 			{
5992 				error = EAFNOSUPPORT;
5993 				break;
5994 			}
5995 			sctp_bindx_add_address(so, inp, addrs->addr,
5996 			    addrs->sget_assoc_id, vrf_id,
5997 			    &error, p);
5998 			break;
5999 		}
6000 	case SCTP_BINDX_REM_ADDR:
6001 		{
6002 			struct sctp_getaddresses *addrs;
6003 			struct thread *td;
6004 
6005 			td = (struct thread *)p;
6006 
6007 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
6008 #ifdef INET
6009 			if (addrs->addr->sa_family == AF_INET) {
6010 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
6011 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6012 					error = EINVAL;
6013 					break;
6014 				}
6015 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
6016 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6017 					break;
6018 				}
6019 			} else
6020 #endif
6021 #ifdef INET6
6022 			if (addrs->addr->sa_family == AF_INET6) {
6023 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
6024 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6025 					error = EINVAL;
6026 					break;
6027 				}
6028 				if (td != NULL &&
6029 				    (error = prison_local_ip6(td->td_ucred,
6030 				    &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
6031 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
6032 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6033 					break;
6034 				}
6035 			} else
6036 #endif
6037 			{
6038 				error = EAFNOSUPPORT;
6039 				break;
6040 			}
6041 			sctp_bindx_delete_address(inp, addrs->addr,
6042 			    addrs->sget_assoc_id, vrf_id,
6043 			    &error);
6044 			break;
6045 		}
6046 	case SCTP_EVENT:
6047 		{
6048 			struct sctp_event *event;
6049 			uint32_t event_type;
6050 
6051 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
6052 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
6053 			switch (event->se_type) {
6054 			case SCTP_ASSOC_CHANGE:
6055 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
6056 				break;
6057 			case SCTP_PEER_ADDR_CHANGE:
6058 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
6059 				break;
6060 			case SCTP_REMOTE_ERROR:
6061 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
6062 				break;
6063 			case SCTP_SEND_FAILED:
6064 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
6065 				break;
6066 			case SCTP_SHUTDOWN_EVENT:
6067 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
6068 				break;
6069 			case SCTP_ADAPTATION_INDICATION:
6070 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
6071 				break;
6072 			case SCTP_PARTIAL_DELIVERY_EVENT:
6073 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
6074 				break;
6075 			case SCTP_AUTHENTICATION_EVENT:
6076 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
6077 				break;
6078 			case SCTP_STREAM_RESET_EVENT:
6079 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
6080 				break;
6081 			case SCTP_SENDER_DRY_EVENT:
6082 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
6083 				break;
6084 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
6085 				event_type = 0;
6086 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6087 				error = ENOTSUP;
6088 				break;
6089 			case SCTP_ASSOC_RESET_EVENT:
6090 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
6091 				break;
6092 			case SCTP_STREAM_CHANGE_EVENT:
6093 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
6094 				break;
6095 			case SCTP_SEND_FAILED_EVENT:
6096 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6097 				break;
6098 			default:
6099 				event_type = 0;
6100 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6101 				error = EINVAL;
6102 				break;
6103 			}
6104 			if (event_type > 0) {
6105 				if (stcb) {
6106 					if (event->se_on) {
6107 						sctp_stcb_feature_on(inp, stcb, event_type);
6108 						if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6109 							if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6110 							    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6111 							    (stcb->asoc.stream_queue_cnt == 0)) {
6112 								sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6113 							}
6114 						}
6115 					} else {
6116 						sctp_stcb_feature_off(inp, stcb, event_type);
6117 					}
6118 					SCTP_TCB_UNLOCK(stcb);
6119 				} else {
6120 					/*
6121 					 * We don't want to send up a storm
6122 					 * of events, so return an error for
6123 					 * sender dry events
6124 					 */
6125 					if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6126 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6127 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6128 					    ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6129 					    (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6130 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6131 						error = ENOTSUP;
6132 						break;
6133 					}
6134 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6135 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6136 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6137 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6138 						SCTP_INP_WLOCK(inp);
6139 						if (event->se_on) {
6140 							sctp_feature_on(inp, event_type);
6141 						} else {
6142 							sctp_feature_off(inp, event_type);
6143 						}
6144 						SCTP_INP_WUNLOCK(inp);
6145 					}
6146 					if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6147 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6148 						SCTP_INP_RLOCK(inp);
6149 						LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6150 							SCTP_TCB_LOCK(stcb);
6151 							if (event->se_on) {
6152 								sctp_stcb_feature_on(inp, stcb, event_type);
6153 							} else {
6154 								sctp_stcb_feature_off(inp, stcb, event_type);
6155 							}
6156 							SCTP_TCB_UNLOCK(stcb);
6157 						}
6158 						SCTP_INP_RUNLOCK(inp);
6159 					}
6160 				}
6161 			}
6162 			break;
6163 		}
6164 	case SCTP_RECVRCVINFO:
6165 		{
6166 			int *onoff;
6167 
6168 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6169 			SCTP_INP_WLOCK(inp);
6170 			if (*onoff != 0) {
6171 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6172 			} else {
6173 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6174 			}
6175 			SCTP_INP_WUNLOCK(inp);
6176 			break;
6177 		}
6178 	case SCTP_RECVNXTINFO:
6179 		{
6180 			int *onoff;
6181 
6182 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6183 			SCTP_INP_WLOCK(inp);
6184 			if (*onoff != 0) {
6185 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6186 			} else {
6187 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6188 			}
6189 			SCTP_INP_WUNLOCK(inp);
6190 			break;
6191 		}
6192 	case SCTP_DEFAULT_SNDINFO:
6193 		{
6194 			struct sctp_sndinfo *info;
6195 			uint16_t policy;
6196 
6197 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6198 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6199 
6200 			if (stcb) {
6201 				if (info->snd_sid < stcb->asoc.streamoutcnt) {
6202 					stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6203 					policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6204 					stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6205 					stcb->asoc.def_send.sinfo_flags |= policy;
6206 					stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6207 					stcb->asoc.def_send.sinfo_context = info->snd_context;
6208 				} else {
6209 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6210 					error = EINVAL;
6211 				}
6212 				SCTP_TCB_UNLOCK(stcb);
6213 			} else {
6214 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6215 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6216 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6217 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6218 					SCTP_INP_WLOCK(inp);
6219 					inp->def_send.sinfo_stream = info->snd_sid;
6220 					policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6221 					inp->def_send.sinfo_flags = info->snd_flags;
6222 					inp->def_send.sinfo_flags |= policy;
6223 					inp->def_send.sinfo_ppid = info->snd_ppid;
6224 					inp->def_send.sinfo_context = info->snd_context;
6225 					SCTP_INP_WUNLOCK(inp);
6226 				}
6227 				if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6228 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6229 					SCTP_INP_RLOCK(inp);
6230 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6231 						SCTP_TCB_LOCK(stcb);
6232 						if (info->snd_sid < stcb->asoc.streamoutcnt) {
6233 							stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6234 							policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6235 							stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6236 							stcb->asoc.def_send.sinfo_flags |= policy;
6237 							stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6238 							stcb->asoc.def_send.sinfo_context = info->snd_context;
6239 						}
6240 						SCTP_TCB_UNLOCK(stcb);
6241 					}
6242 					SCTP_INP_RUNLOCK(inp);
6243 				}
6244 			}
6245 			break;
6246 		}
6247 	case SCTP_DEFAULT_PRINFO:
6248 		{
6249 			struct sctp_default_prinfo *info;
6250 
6251 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6252 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6253 
6254 			if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6255 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6256 				error = EINVAL;
6257 				break;
6258 			}
6259 			if (stcb) {
6260 				stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6261 				stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6262 				stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6263 				SCTP_TCB_UNLOCK(stcb);
6264 			} else {
6265 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6266 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6267 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6268 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6269 					SCTP_INP_WLOCK(inp);
6270 					inp->def_send.sinfo_flags &= 0xfff0;
6271 					inp->def_send.sinfo_flags |= info->pr_policy;
6272 					inp->def_send.sinfo_timetolive = info->pr_value;
6273 					SCTP_INP_WUNLOCK(inp);
6274 				}
6275 				if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6276 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6277 					SCTP_INP_RLOCK(inp);
6278 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6279 						SCTP_TCB_LOCK(stcb);
6280 						stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6281 						stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6282 						stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6283 						SCTP_TCB_UNLOCK(stcb);
6284 					}
6285 					SCTP_INP_RUNLOCK(inp);
6286 				}
6287 			}
6288 			break;
6289 		}
6290 	case SCTP_PEER_ADDR_THLDS:
6291 		/* Applies to the specific association */
6292 		{
6293 			struct sctp_paddrthlds *thlds;
6294 			struct sctp_nets *net;
6295 			struct sockaddr *addr;
6296 
6297 #if defined(INET) && defined(INET6)
6298 			struct sockaddr_in sin_store;
6299 
6300 #endif
6301 
6302 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6303 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6304 
6305 #if defined(INET) && defined(INET6)
6306 			if (thlds->spt_address.ss_family == AF_INET6) {
6307 				struct sockaddr_in6 *sin6;
6308 
6309 				sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6310 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6311 					in6_sin6_2_sin(&sin_store, sin6);
6312 					addr = (struct sockaddr *)&sin_store;
6313 				} else {
6314 					addr = (struct sockaddr *)&thlds->spt_address;
6315 				}
6316 			} else {
6317 				addr = (struct sockaddr *)&thlds->spt_address;
6318 			}
6319 #else
6320 			addr = (struct sockaddr *)&thlds->spt_address;
6321 #endif
6322 			if (stcb != NULL) {
6323 				net = sctp_findnet(stcb, addr);
6324 			} else {
6325 				/*
6326 				 * We increment here since
6327 				 * sctp_findassociation_ep_addr() wil do a
6328 				 * decrement if it finds the stcb as long as
6329 				 * the locked tcb (last argument) is NOT a
6330 				 * TCB.. aka NULL.
6331 				 */
6332 				net = NULL;
6333 				SCTP_INP_INCR_REF(inp);
6334 				stcb = sctp_findassociation_ep_addr(&inp, addr,
6335 				    &net, NULL, NULL);
6336 				if (stcb == NULL) {
6337 					SCTP_INP_DECR_REF(inp);
6338 				}
6339 			}
6340 			if ((stcb != NULL) && (net == NULL)) {
6341 #ifdef INET
6342 				if (addr->sa_family == AF_INET) {
6343 
6344 					struct sockaddr_in *sin;
6345 
6346 					sin = (struct sockaddr_in *)addr;
6347 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6348 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6349 						SCTP_TCB_UNLOCK(stcb);
6350 						error = EINVAL;
6351 						break;
6352 					}
6353 				} else
6354 #endif
6355 #ifdef INET6
6356 				if (addr->sa_family == AF_INET6) {
6357 					struct sockaddr_in6 *sin6;
6358 
6359 					sin6 = (struct sockaddr_in6 *)addr;
6360 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6361 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6362 						SCTP_TCB_UNLOCK(stcb);
6363 						error = EINVAL;
6364 						break;
6365 					}
6366 				} else
6367 #endif
6368 				{
6369 					error = EAFNOSUPPORT;
6370 					SCTP_TCB_UNLOCK(stcb);
6371 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6372 					break;
6373 				}
6374 			}
6375 			if (stcb != NULL) {
6376 				if (net != NULL) {
6377 					net->failure_threshold = thlds->spt_pathmaxrxt;
6378 					net->pf_threshold = thlds->spt_pathpfthld;
6379 					if (net->dest_state & SCTP_ADDR_PF) {
6380 						if ((net->error_count > net->failure_threshold) ||
6381 						    (net->error_count <= net->pf_threshold)) {
6382 							net->dest_state &= ~SCTP_ADDR_PF;
6383 						}
6384 					} else {
6385 						if ((net->error_count > net->pf_threshold) &&
6386 						    (net->error_count <= net->failure_threshold)) {
6387 							net->dest_state |= SCTP_ADDR_PF;
6388 							sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6389 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6390 							    stcb->sctp_ep, stcb, net,
6391 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6392 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6393 						}
6394 					}
6395 					if (net->dest_state & SCTP_ADDR_REACHABLE) {
6396 						if (net->error_count > net->failure_threshold) {
6397 							net->dest_state &= ~SCTP_ADDR_REACHABLE;
6398 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6399 						}
6400 					} else {
6401 						if (net->error_count <= net->failure_threshold) {
6402 							net->dest_state |= SCTP_ADDR_REACHABLE;
6403 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6404 						}
6405 					}
6406 				} else {
6407 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6408 						net->failure_threshold = thlds->spt_pathmaxrxt;
6409 						net->pf_threshold = thlds->spt_pathpfthld;
6410 						if (net->dest_state & SCTP_ADDR_PF) {
6411 							if ((net->error_count > net->failure_threshold) ||
6412 							    (net->error_count <= net->pf_threshold)) {
6413 								net->dest_state &= ~SCTP_ADDR_PF;
6414 							}
6415 						} else {
6416 							if ((net->error_count > net->pf_threshold) &&
6417 							    (net->error_count <= net->failure_threshold)) {
6418 								net->dest_state |= SCTP_ADDR_PF;
6419 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6420 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6421 								    stcb->sctp_ep, stcb, net,
6422 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
6423 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6424 							}
6425 						}
6426 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
6427 							if (net->error_count > net->failure_threshold) {
6428 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
6429 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6430 							}
6431 						} else {
6432 							if (net->error_count <= net->failure_threshold) {
6433 								net->dest_state |= SCTP_ADDR_REACHABLE;
6434 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6435 							}
6436 						}
6437 					}
6438 					stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6439 					stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6440 				}
6441 				SCTP_TCB_UNLOCK(stcb);
6442 			} else {
6443 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6444 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6445 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6446 					SCTP_INP_WLOCK(inp);
6447 					inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6448 					inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6449 					SCTP_INP_WUNLOCK(inp);
6450 				} else {
6451 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6452 					error = EINVAL;
6453 				}
6454 			}
6455 			break;
6456 		}
6457 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
6458 		{
6459 			struct sctp_udpencaps *encaps;
6460 			struct sctp_nets *net;
6461 			struct sockaddr *addr;
6462 
6463 #if defined(INET) && defined(INET6)
6464 			struct sockaddr_in sin_store;
6465 
6466 #endif
6467 
6468 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6469 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6470 
6471 #if defined(INET) && defined(INET6)
6472 			if (encaps->sue_address.ss_family == AF_INET6) {
6473 				struct sockaddr_in6 *sin6;
6474 
6475 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6476 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6477 					in6_sin6_2_sin(&sin_store, sin6);
6478 					addr = (struct sockaddr *)&sin_store;
6479 				} else {
6480 					addr = (struct sockaddr *)&encaps->sue_address;
6481 				}
6482 			} else {
6483 				addr = (struct sockaddr *)&encaps->sue_address;
6484 			}
6485 #else
6486 			addr = (struct sockaddr *)&encaps->sue_address;
6487 #endif
6488 			if (stcb != NULL) {
6489 				net = sctp_findnet(stcb, addr);
6490 			} else {
6491 				/*
6492 				 * We increment here since
6493 				 * sctp_findassociation_ep_addr() wil do a
6494 				 * decrement if it finds the stcb as long as
6495 				 * the locked tcb (last argument) is NOT a
6496 				 * TCB.. aka NULL.
6497 				 */
6498 				net = NULL;
6499 				SCTP_INP_INCR_REF(inp);
6500 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6501 				if (stcb == NULL) {
6502 					SCTP_INP_DECR_REF(inp);
6503 				}
6504 			}
6505 			if ((stcb != NULL) && (net == NULL)) {
6506 #ifdef INET
6507 				if (addr->sa_family == AF_INET) {
6508 
6509 					struct sockaddr_in *sin;
6510 
6511 					sin = (struct sockaddr_in *)addr;
6512 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6513 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6514 						SCTP_TCB_UNLOCK(stcb);
6515 						error = EINVAL;
6516 						break;
6517 					}
6518 				} else
6519 #endif
6520 #ifdef INET6
6521 				if (addr->sa_family == AF_INET6) {
6522 					struct sockaddr_in6 *sin6;
6523 
6524 					sin6 = (struct sockaddr_in6 *)addr;
6525 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6526 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6527 						SCTP_TCB_UNLOCK(stcb);
6528 						error = EINVAL;
6529 						break;
6530 					}
6531 				} else
6532 #endif
6533 				{
6534 					error = EAFNOSUPPORT;
6535 					SCTP_TCB_UNLOCK(stcb);
6536 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6537 					break;
6538 				}
6539 			}
6540 			if (stcb != NULL) {
6541 				if (net != NULL) {
6542 					net->port = encaps->sue_port;
6543 				} else {
6544 					stcb->asoc.port = encaps->sue_port;
6545 				}
6546 				SCTP_TCB_UNLOCK(stcb);
6547 			} else {
6548 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6549 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6550 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6551 					SCTP_INP_WLOCK(inp);
6552 					inp->sctp_ep.port = encaps->sue_port;
6553 					SCTP_INP_WUNLOCK(inp);
6554 				} else {
6555 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6556 					error = EINVAL;
6557 				}
6558 			}
6559 			break;
6560 		}
6561 	case SCTP_ECN_SUPPORTED:
6562 		{
6563 			struct sctp_assoc_value *av;
6564 
6565 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6566 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6567 
6568 			if (stcb) {
6569 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6570 				error = EINVAL;
6571 				SCTP_TCB_UNLOCK(stcb);
6572 			} else {
6573 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6574 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6575 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6576 					SCTP_INP_WLOCK(inp);
6577 					if (av->assoc_value == 0) {
6578 						inp->ecn_supported = 0;
6579 					} else {
6580 						inp->ecn_supported = 1;
6581 					}
6582 					SCTP_INP_WUNLOCK(inp);
6583 				} else {
6584 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6585 					error = EINVAL;
6586 				}
6587 			}
6588 			break;
6589 		}
6590 	case SCTP_PR_SUPPORTED:
6591 		{
6592 			struct sctp_assoc_value *av;
6593 
6594 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6595 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6596 
6597 			if (stcb) {
6598 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6599 				error = EINVAL;
6600 				SCTP_TCB_UNLOCK(stcb);
6601 			} else {
6602 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6603 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6604 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6605 					SCTP_INP_WLOCK(inp);
6606 					if (av->assoc_value == 0) {
6607 						inp->prsctp_supported = 0;
6608 					} else {
6609 						inp->prsctp_supported = 1;
6610 					}
6611 					SCTP_INP_WUNLOCK(inp);
6612 				} else {
6613 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6614 					error = EINVAL;
6615 				}
6616 			}
6617 			break;
6618 		}
6619 	case SCTP_AUTH_SUPPORTED:
6620 		{
6621 			struct sctp_assoc_value *av;
6622 
6623 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6624 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6625 
6626 			if (stcb) {
6627 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6628 				error = EINVAL;
6629 				SCTP_TCB_UNLOCK(stcb);
6630 			} else {
6631 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6632 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6633 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6634 					if ((av->assoc_value == 0) &&
6635 					    (inp->asconf_supported == 1)) {
6636 						/*
6637 						 * AUTH is required for
6638 						 * ASCONF
6639 						 */
6640 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6641 						error = EINVAL;
6642 					} else {
6643 						SCTP_INP_WLOCK(inp);
6644 						if (av->assoc_value == 0) {
6645 							inp->auth_supported = 0;
6646 						} else {
6647 							inp->auth_supported = 1;
6648 						}
6649 						SCTP_INP_WUNLOCK(inp);
6650 					}
6651 				} else {
6652 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6653 					error = EINVAL;
6654 				}
6655 			}
6656 			break;
6657 		}
6658 	case SCTP_ASCONF_SUPPORTED:
6659 		{
6660 			struct sctp_assoc_value *av;
6661 
6662 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6663 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6664 
6665 			if (stcb) {
6666 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6667 				error = EINVAL;
6668 				SCTP_TCB_UNLOCK(stcb);
6669 			} else {
6670 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6671 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6672 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6673 					if ((av->assoc_value != 0) &&
6674 					    (inp->auth_supported == 0)) {
6675 						/*
6676 						 * AUTH is required for
6677 						 * ASCONF
6678 						 */
6679 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6680 						error = EINVAL;
6681 					} else {
6682 						SCTP_INP_WLOCK(inp);
6683 						if (av->assoc_value == 0) {
6684 							inp->asconf_supported = 0;
6685 							sctp_auth_delete_chunk(SCTP_ASCONF,
6686 							    inp->sctp_ep.local_auth_chunks);
6687 							sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6688 							    inp->sctp_ep.local_auth_chunks);
6689 						} else {
6690 							inp->asconf_supported = 1;
6691 							sctp_auth_add_chunk(SCTP_ASCONF,
6692 							    inp->sctp_ep.local_auth_chunks);
6693 							sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6694 							    inp->sctp_ep.local_auth_chunks);
6695 						}
6696 						SCTP_INP_WUNLOCK(inp);
6697 					}
6698 				} else {
6699 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6700 					error = EINVAL;
6701 				}
6702 			}
6703 			break;
6704 		}
6705 	case SCTP_RECONFIG_SUPPORTED:
6706 		{
6707 			struct sctp_assoc_value *av;
6708 
6709 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6710 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6711 
6712 			if (stcb) {
6713 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6714 				error = EINVAL;
6715 				SCTP_TCB_UNLOCK(stcb);
6716 			} else {
6717 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6718 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6719 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6720 					SCTP_INP_WLOCK(inp);
6721 					if (av->assoc_value == 0) {
6722 						inp->reconfig_supported = 0;
6723 					} else {
6724 						inp->reconfig_supported = 1;
6725 					}
6726 					SCTP_INP_WUNLOCK(inp);
6727 				} else {
6728 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6729 					error = EINVAL;
6730 				}
6731 			}
6732 			break;
6733 		}
6734 	case SCTP_NRSACK_SUPPORTED:
6735 		{
6736 			struct sctp_assoc_value *av;
6737 
6738 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6739 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6740 
6741 			if (stcb) {
6742 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6743 				error = EINVAL;
6744 				SCTP_TCB_UNLOCK(stcb);
6745 			} else {
6746 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6747 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6748 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6749 					SCTP_INP_WLOCK(inp);
6750 					if (av->assoc_value == 0) {
6751 						inp->nrsack_supported = 0;
6752 					} else {
6753 						inp->nrsack_supported = 1;
6754 					}
6755 					SCTP_INP_WUNLOCK(inp);
6756 				} else {
6757 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6758 					error = EINVAL;
6759 				}
6760 			}
6761 			break;
6762 		}
6763 	case SCTP_PKTDROP_SUPPORTED:
6764 		{
6765 			struct sctp_assoc_value *av;
6766 
6767 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6768 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6769 
6770 			if (stcb) {
6771 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6772 				error = EINVAL;
6773 				SCTP_TCB_UNLOCK(stcb);
6774 			} else {
6775 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6776 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6777 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6778 					SCTP_INP_WLOCK(inp);
6779 					if (av->assoc_value == 0) {
6780 						inp->pktdrop_supported = 0;
6781 					} else {
6782 						inp->pktdrop_supported = 1;
6783 					}
6784 					SCTP_INP_WUNLOCK(inp);
6785 				} else {
6786 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6787 					error = EINVAL;
6788 				}
6789 			}
6790 			break;
6791 		}
6792 	case SCTP_MAX_CWND:
6793 		{
6794 			struct sctp_assoc_value *av;
6795 			struct sctp_nets *net;
6796 
6797 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6798 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6799 
6800 			if (stcb) {
6801 				stcb->asoc.max_cwnd = av->assoc_value;
6802 				if (stcb->asoc.max_cwnd > 0) {
6803 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6804 						if ((net->cwnd > stcb->asoc.max_cwnd) &&
6805 						    (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6806 							net->cwnd = stcb->asoc.max_cwnd;
6807 							if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6808 								net->cwnd = net->mtu - sizeof(struct sctphdr);
6809 							}
6810 						}
6811 					}
6812 				}
6813 				SCTP_TCB_UNLOCK(stcb);
6814 			} else {
6815 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6816 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6817 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6818 					SCTP_INP_WLOCK(inp);
6819 					inp->max_cwnd = av->assoc_value;
6820 					SCTP_INP_WUNLOCK(inp);
6821 				} else {
6822 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6823 					error = EINVAL;
6824 				}
6825 			}
6826 			break;
6827 		}
6828 	default:
6829 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6830 		error = ENOPROTOOPT;
6831 		break;
6832 	}			/* end switch (opt) */
6833 	return (error);
6834 }
6835 
6836 int
6837 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6838 {
6839 	void *optval = NULL;
6840 	size_t optsize = 0;
6841 	void *p;
6842 	int error = 0;
6843 	struct sctp_inpcb *inp;
6844 
6845 	if ((sopt->sopt_level == SOL_SOCKET) &&
6846 	    (sopt->sopt_name == SO_SETFIB)) {
6847 		inp = (struct sctp_inpcb *)so->so_pcb;
6848 		if (inp == NULL) {
6849 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6850 			return (EINVAL);
6851 		}
6852 		SCTP_INP_WLOCK(inp);
6853 		inp->fibnum = so->so_fibnum;
6854 		SCTP_INP_WUNLOCK(inp);
6855 		return (0);
6856 	}
6857 	if (sopt->sopt_level != IPPROTO_SCTP) {
6858 		/* wrong proto level... send back up to IP */
6859 #ifdef INET6
6860 		if (INP_CHECK_SOCKAF(so, AF_INET6))
6861 			error = ip6_ctloutput(so, sopt);
6862 #endif				/* INET6 */
6863 #if defined(INET) && defined(INET6)
6864 		else
6865 #endif
6866 #ifdef INET
6867 			error = ip_ctloutput(so, sopt);
6868 #endif
6869 		return (error);
6870 	}
6871 	optsize = sopt->sopt_valsize;
6872 	if (optsize) {
6873 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6874 		if (optval == NULL) {
6875 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6876 			return (ENOBUFS);
6877 		}
6878 		error = sooptcopyin(sopt, optval, optsize, optsize);
6879 		if (error) {
6880 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
6881 			goto out;
6882 		}
6883 	}
6884 	p = (void *)sopt->sopt_td;
6885 	if (sopt->sopt_dir == SOPT_SET) {
6886 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6887 	} else if (sopt->sopt_dir == SOPT_GET) {
6888 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6889 	} else {
6890 		SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6891 		error = EINVAL;
6892 	}
6893 	if ((error == 0) && (optval != NULL)) {
6894 		error = sooptcopyout(sopt, optval, optsize);
6895 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6896 	} else if (optval != NULL) {
6897 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6898 	}
6899 out:
6900 	return (error);
6901 }
6902 
6903 #ifdef INET
6904 static int
6905 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6906 {
6907 	int error = 0;
6908 	int create_lock_on = 0;
6909 	uint32_t vrf_id;
6910 	struct sctp_inpcb *inp;
6911 	struct sctp_tcb *stcb = NULL;
6912 
6913 	inp = (struct sctp_inpcb *)so->so_pcb;
6914 	if (inp == NULL) {
6915 		/* I made the same as TCP since we are not setup? */
6916 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6917 		return (ECONNRESET);
6918 	}
6919 	if (addr == NULL) {
6920 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6921 		return EINVAL;
6922 	}
6923 	switch (addr->sa_family) {
6924 #ifdef INET6
6925 	case AF_INET6:
6926 		{
6927 			struct sockaddr_in6 *sin6p;
6928 
6929 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6930 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6931 				return (EINVAL);
6932 			}
6933 			sin6p = (struct sockaddr_in6 *)addr;
6934 			if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6935 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6936 				return (error);
6937 			}
6938 			break;
6939 		}
6940 #endif
6941 #ifdef INET
6942 	case AF_INET:
6943 		{
6944 			struct sockaddr_in *sinp;
6945 
6946 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
6947 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6948 				return (EINVAL);
6949 			}
6950 			sinp = (struct sockaddr_in *)addr;
6951 			if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6952 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6953 				return (error);
6954 			}
6955 			break;
6956 		}
6957 #endif
6958 	default:
6959 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6960 		return (EAFNOSUPPORT);
6961 	}
6962 	SCTP_INP_INCR_REF(inp);
6963 	SCTP_ASOC_CREATE_LOCK(inp);
6964 	create_lock_on = 1;
6965 
6966 
6967 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6968 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6969 		/* Should I really unlock ? */
6970 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6971 		error = EFAULT;
6972 		goto out_now;
6973 	}
6974 #ifdef INET6
6975 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6976 	    (addr->sa_family == AF_INET6)) {
6977 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6978 		error = EINVAL;
6979 		goto out_now;
6980 	}
6981 #endif
6982 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6983 	    SCTP_PCB_FLAGS_UNBOUND) {
6984 		/* Bind a ephemeral port */
6985 		error = sctp_inpcb_bind(so, NULL, NULL, p);
6986 		if (error) {
6987 			goto out_now;
6988 		}
6989 	}
6990 	/* Now do we connect? */
6991 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6992 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6993 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6994 		error = EINVAL;
6995 		goto out_now;
6996 	}
6997 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6998 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6999 		/* We are already connected AND the TCP model */
7000 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7001 		error = EADDRINUSE;
7002 		goto out_now;
7003 	}
7004 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7005 		SCTP_INP_RLOCK(inp);
7006 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
7007 		SCTP_INP_RUNLOCK(inp);
7008 	} else {
7009 		/*
7010 		 * We increment here since sctp_findassociation_ep_addr()
7011 		 * will do a decrement if it finds the stcb as long as the
7012 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
7013 		 */
7014 		SCTP_INP_INCR_REF(inp);
7015 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
7016 		if (stcb == NULL) {
7017 			SCTP_INP_DECR_REF(inp);
7018 		} else {
7019 			SCTP_TCB_UNLOCK(stcb);
7020 		}
7021 	}
7022 	if (stcb != NULL) {
7023 		/* Already have or am bring up an association */
7024 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
7025 		error = EALREADY;
7026 		goto out_now;
7027 	}
7028 	vrf_id = inp->def_vrf_id;
7029 	/* We are GOOD to go */
7030 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, inp->sctp_ep.pre_open_stream_count, p);
7031 	if (stcb == NULL) {
7032 		/* Gak! no memory */
7033 		goto out_now;
7034 	}
7035 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
7036 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7037 		/* Set the connected flag so we can queue data */
7038 		soisconnecting(so);
7039 	}
7040 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
7041 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7042 
7043 	/* initialize authentication parameters for the assoc */
7044 	sctp_initialize_auth_params(inp, stcb);
7045 
7046 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7047 	SCTP_TCB_UNLOCK(stcb);
7048 out_now:
7049 	if (create_lock_on) {
7050 		SCTP_ASOC_CREATE_UNLOCK(inp);
7051 	}
7052 	SCTP_INP_DECR_REF(inp);
7053 	return (error);
7054 }
7055 
7056 #endif
7057 
7058 int
7059 sctp_listen(struct socket *so, int backlog, struct thread *p)
7060 {
7061 	/*
7062 	 * Note this module depends on the protocol processing being called
7063 	 * AFTER any socket level flags and backlog are applied to the
7064 	 * socket. The traditional way that the socket flags are applied is
7065 	 * AFTER protocol processing. We have made a change to the
7066 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
7067 	 * place if the socket API for SCTP is to work properly.
7068 	 */
7069 
7070 	int error = 0;
7071 	struct sctp_inpcb *inp;
7072 
7073 	inp = (struct sctp_inpcb *)so->so_pcb;
7074 	if (inp == NULL) {
7075 		/* I made the same as TCP since we are not setup? */
7076 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7077 		return (ECONNRESET);
7078 	}
7079 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
7080 		/* See if we have a listener */
7081 		struct sctp_inpcb *tinp;
7082 		union sctp_sockstore store;
7083 
7084 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
7085 			/* not bound all */
7086 			struct sctp_laddr *laddr;
7087 
7088 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7089 				memcpy(&store, &laddr->ifa->address, sizeof(store));
7090 				switch (store.sa.sa_family) {
7091 #ifdef INET
7092 				case AF_INET:
7093 					store.sin.sin_port = inp->sctp_lport;
7094 					break;
7095 #endif
7096 #ifdef INET6
7097 				case AF_INET6:
7098 					store.sin6.sin6_port = inp->sctp_lport;
7099 					break;
7100 #endif
7101 				default:
7102 					break;
7103 				}
7104 				tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7105 				if (tinp && (tinp != inp) &&
7106 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7107 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7108 				    (tinp->sctp_socket->so_qlimit)) {
7109 					/*
7110 					 * we have a listener already and
7111 					 * its not this inp.
7112 					 */
7113 					SCTP_INP_DECR_REF(tinp);
7114 					return (EADDRINUSE);
7115 				} else if (tinp) {
7116 					SCTP_INP_DECR_REF(tinp);
7117 				}
7118 			}
7119 		} else {
7120 			/* Setup a local addr bound all */
7121 			memset(&store, 0, sizeof(store));
7122 #ifdef INET6
7123 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7124 				store.sa.sa_family = AF_INET6;
7125 				store.sa.sa_len = sizeof(struct sockaddr_in6);
7126 			}
7127 #endif
7128 #ifdef INET
7129 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7130 				store.sa.sa_family = AF_INET;
7131 				store.sa.sa_len = sizeof(struct sockaddr_in);
7132 			}
7133 #endif
7134 			switch (store.sa.sa_family) {
7135 #ifdef INET
7136 			case AF_INET:
7137 				store.sin.sin_port = inp->sctp_lport;
7138 				break;
7139 #endif
7140 #ifdef INET6
7141 			case AF_INET6:
7142 				store.sin6.sin6_port = inp->sctp_lport;
7143 				break;
7144 #endif
7145 			default:
7146 				break;
7147 			}
7148 			tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7149 			if (tinp && (tinp != inp) &&
7150 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7151 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7152 			    (tinp->sctp_socket->so_qlimit)) {
7153 				/*
7154 				 * we have a listener already and its not
7155 				 * this inp.
7156 				 */
7157 				SCTP_INP_DECR_REF(tinp);
7158 				return (EADDRINUSE);
7159 			} else if (tinp) {
7160 				SCTP_INP_DECR_REF(tinp);
7161 			}
7162 		}
7163 	}
7164 	SCTP_INP_RLOCK(inp);
7165 #ifdef SCTP_LOCK_LOGGING
7166 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7167 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7168 	}
7169 #endif
7170 	SOCK_LOCK(so);
7171 	error = solisten_proto_check(so);
7172 	SOCK_UNLOCK(so);
7173 	if (error) {
7174 		SCTP_INP_RUNLOCK(inp);
7175 		return (error);
7176 	}
7177 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7178 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7179 		/*
7180 		 * The unlucky case - We are in the tcp pool with this guy.
7181 		 * - Someone else is in the main inp slot. - We must move
7182 		 * this guy (the listener) to the main slot - We must then
7183 		 * move the guy that was listener to the TCP Pool.
7184 		 */
7185 		if (sctp_swap_inpcb_for_listen(inp)) {
7186 			SCTP_INP_RUNLOCK(inp);
7187 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7188 			return (EADDRINUSE);
7189 		}
7190 	}
7191 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7192 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7193 		/* We are already connected AND the TCP model */
7194 		SCTP_INP_RUNLOCK(inp);
7195 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7196 		return (EADDRINUSE);
7197 	}
7198 	SCTP_INP_RUNLOCK(inp);
7199 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7200 		/* We must do a bind. */
7201 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7202 			/* bind error, probably perm */
7203 			return (error);
7204 		}
7205 	}
7206 	SOCK_LOCK(so);
7207 	/* It appears for 7.0 and on, we must always call this. */
7208 	solisten_proto(so, backlog);
7209 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7210 		/* remove the ACCEPTCONN flag for one-to-many sockets */
7211 		so->so_options &= ~SO_ACCEPTCONN;
7212 	}
7213 	if (backlog == 0) {
7214 		/* turning off listen */
7215 		so->so_options &= ~SO_ACCEPTCONN;
7216 	}
7217 	SOCK_UNLOCK(so);
7218 	return (error);
7219 }
7220 
7221 static int sctp_defered_wakeup_cnt = 0;
7222 
7223 int
7224 sctp_accept(struct socket *so, struct sockaddr **addr)
7225 {
7226 	struct sctp_tcb *stcb;
7227 	struct sctp_inpcb *inp;
7228 	union sctp_sockstore store;
7229 
7230 #ifdef INET6
7231 	int error;
7232 
7233 #endif
7234 	inp = (struct sctp_inpcb *)so->so_pcb;
7235 
7236 	if (inp == NULL) {
7237 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7238 		return (ECONNRESET);
7239 	}
7240 	SCTP_INP_RLOCK(inp);
7241 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7242 		SCTP_INP_RUNLOCK(inp);
7243 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7244 		return (EOPNOTSUPP);
7245 	}
7246 	if (so->so_state & SS_ISDISCONNECTED) {
7247 		SCTP_INP_RUNLOCK(inp);
7248 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7249 		return (ECONNABORTED);
7250 	}
7251 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7252 	if (stcb == NULL) {
7253 		SCTP_INP_RUNLOCK(inp);
7254 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7255 		return (ECONNRESET);
7256 	}
7257 	SCTP_TCB_LOCK(stcb);
7258 	SCTP_INP_RUNLOCK(inp);
7259 	store = stcb->asoc.primary_destination->ro._l_addr;
7260 	stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
7261 	SCTP_TCB_UNLOCK(stcb);
7262 	switch (store.sa.sa_family) {
7263 #ifdef INET
7264 	case AF_INET:
7265 		{
7266 			struct sockaddr_in *sin;
7267 
7268 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7269 			if (sin == NULL)
7270 				return (ENOMEM);
7271 			sin->sin_family = AF_INET;
7272 			sin->sin_len = sizeof(*sin);
7273 			sin->sin_port = store.sin.sin_port;
7274 			sin->sin_addr = store.sin.sin_addr;
7275 			*addr = (struct sockaddr *)sin;
7276 			break;
7277 		}
7278 #endif
7279 #ifdef INET6
7280 	case AF_INET6:
7281 		{
7282 			struct sockaddr_in6 *sin6;
7283 
7284 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7285 			if (sin6 == NULL)
7286 				return (ENOMEM);
7287 			sin6->sin6_family = AF_INET6;
7288 			sin6->sin6_len = sizeof(*sin6);
7289 			sin6->sin6_port = store.sin6.sin6_port;
7290 			sin6->sin6_addr = store.sin6.sin6_addr;
7291 			if ((error = sa6_recoverscope(sin6)) != 0) {
7292 				SCTP_FREE_SONAME(sin6);
7293 				return (error);
7294 			}
7295 			*addr = (struct sockaddr *)sin6;
7296 			break;
7297 		}
7298 #endif
7299 	default:
7300 		/* TSNH */
7301 		break;
7302 	}
7303 	/* Wake any delayed sleep action */
7304 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7305 		SCTP_INP_WLOCK(inp);
7306 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7307 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7308 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7309 			SCTP_INP_WUNLOCK(inp);
7310 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7311 			if (sowriteable(inp->sctp_socket)) {
7312 				sowwakeup_locked(inp->sctp_socket);
7313 			} else {
7314 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7315 			}
7316 			SCTP_INP_WLOCK(inp);
7317 		}
7318 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7319 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7320 			SCTP_INP_WUNLOCK(inp);
7321 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7322 			if (soreadable(inp->sctp_socket)) {
7323 				sctp_defered_wakeup_cnt++;
7324 				sorwakeup_locked(inp->sctp_socket);
7325 			} else {
7326 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7327 			}
7328 			SCTP_INP_WLOCK(inp);
7329 		}
7330 		SCTP_INP_WUNLOCK(inp);
7331 	}
7332 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7333 		SCTP_TCB_LOCK(stcb);
7334 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7335 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7336 	}
7337 	return (0);
7338 }
7339 
7340 #ifdef INET
7341 int
7342 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7343 {
7344 	struct sockaddr_in *sin;
7345 	uint32_t vrf_id;
7346 	struct sctp_inpcb *inp;
7347 	struct sctp_ifa *sctp_ifa;
7348 
7349 	/*
7350 	 * Do the malloc first in case it blocks.
7351 	 */
7352 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7353 	if (sin == NULL)
7354 		return (ENOMEM);
7355 	sin->sin_family = AF_INET;
7356 	sin->sin_len = sizeof(*sin);
7357 	inp = (struct sctp_inpcb *)so->so_pcb;
7358 	if (!inp) {
7359 		SCTP_FREE_SONAME(sin);
7360 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7361 		return (ECONNRESET);
7362 	}
7363 	SCTP_INP_RLOCK(inp);
7364 	sin->sin_port = inp->sctp_lport;
7365 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7366 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7367 			struct sctp_tcb *stcb;
7368 			struct sockaddr_in *sin_a;
7369 			struct sctp_nets *net;
7370 			int fnd;
7371 
7372 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
7373 			if (stcb == NULL) {
7374 				goto notConn;
7375 			}
7376 			fnd = 0;
7377 			sin_a = NULL;
7378 			SCTP_TCB_LOCK(stcb);
7379 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7380 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7381 				if (sin_a == NULL)
7382 					/* this will make coverity happy */
7383 					continue;
7384 
7385 				if (sin_a->sin_family == AF_INET) {
7386 					fnd = 1;
7387 					break;
7388 				}
7389 			}
7390 			if ((!fnd) || (sin_a == NULL)) {
7391 				/* punt */
7392 				SCTP_TCB_UNLOCK(stcb);
7393 				goto notConn;
7394 			}
7395 			vrf_id = inp->def_vrf_id;
7396 			sctp_ifa = sctp_source_address_selection(inp,
7397 			    stcb,
7398 			    (sctp_route_t *) & net->ro,
7399 			    net, 0, vrf_id);
7400 			if (sctp_ifa) {
7401 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7402 				sctp_free_ifa(sctp_ifa);
7403 			}
7404 			SCTP_TCB_UNLOCK(stcb);
7405 		} else {
7406 			/* For the bound all case you get back 0 */
7407 	notConn:
7408 			sin->sin_addr.s_addr = 0;
7409 		}
7410 
7411 	} else {
7412 		/* Take the first IPv4 address in the list */
7413 		struct sctp_laddr *laddr;
7414 		int fnd = 0;
7415 
7416 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7417 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
7418 				struct sockaddr_in *sin_a;
7419 
7420 				sin_a = &laddr->ifa->address.sin;
7421 				sin->sin_addr = sin_a->sin_addr;
7422 				fnd = 1;
7423 				break;
7424 			}
7425 		}
7426 		if (!fnd) {
7427 			SCTP_FREE_SONAME(sin);
7428 			SCTP_INP_RUNLOCK(inp);
7429 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7430 			return (ENOENT);
7431 		}
7432 	}
7433 	SCTP_INP_RUNLOCK(inp);
7434 	(*addr) = (struct sockaddr *)sin;
7435 	return (0);
7436 }
7437 
7438 int
7439 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7440 {
7441 	struct sockaddr_in *sin;
7442 	int fnd;
7443 	struct sockaddr_in *sin_a;
7444 	struct sctp_inpcb *inp;
7445 	struct sctp_tcb *stcb;
7446 	struct sctp_nets *net;
7447 
7448 	/* Do the malloc first in case it blocks. */
7449 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7450 	if (sin == NULL)
7451 		return (ENOMEM);
7452 	sin->sin_family = AF_INET;
7453 	sin->sin_len = sizeof(*sin);
7454 
7455 	inp = (struct sctp_inpcb *)so->so_pcb;
7456 	if ((inp == NULL) ||
7457 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7458 		/* UDP type and listeners will drop out here */
7459 		SCTP_FREE_SONAME(sin);
7460 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7461 		return (ENOTCONN);
7462 	}
7463 	SCTP_INP_RLOCK(inp);
7464 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7465 	if (stcb) {
7466 		SCTP_TCB_LOCK(stcb);
7467 	}
7468 	SCTP_INP_RUNLOCK(inp);
7469 	if (stcb == NULL) {
7470 		SCTP_FREE_SONAME(sin);
7471 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7472 		return (ECONNRESET);
7473 	}
7474 	fnd = 0;
7475 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7476 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7477 		if (sin_a->sin_family == AF_INET) {
7478 			fnd = 1;
7479 			sin->sin_port = stcb->rport;
7480 			sin->sin_addr = sin_a->sin_addr;
7481 			break;
7482 		}
7483 	}
7484 	SCTP_TCB_UNLOCK(stcb);
7485 	if (!fnd) {
7486 		/* No IPv4 address */
7487 		SCTP_FREE_SONAME(sin);
7488 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7489 		return (ENOENT);
7490 	}
7491 	(*addr) = (struct sockaddr *)sin;
7492 	return (0);
7493 }
7494 
7495 struct pr_usrreqs sctp_usrreqs = {
7496 	.pru_abort = sctp_abort,
7497 	.pru_accept = sctp_accept,
7498 	.pru_attach = sctp_attach,
7499 	.pru_bind = sctp_bind,
7500 	.pru_connect = sctp_connect,
7501 	.pru_control = in_control,
7502 	.pru_close = sctp_close,
7503 	.pru_detach = sctp_close,
7504 	.pru_sopoll = sopoll_generic,
7505 	.pru_flush = sctp_flush,
7506 	.pru_disconnect = sctp_disconnect,
7507 	.pru_listen = sctp_listen,
7508 	.pru_peeraddr = sctp_peeraddr,
7509 	.pru_send = sctp_sendm,
7510 	.pru_shutdown = sctp_shutdown,
7511 	.pru_sockaddr = sctp_ingetaddr,
7512 	.pru_sosend = sctp_sosend,
7513 	.pru_soreceive = sctp_soreceive
7514 };
7515 
7516 #endif
7517