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