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