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