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