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