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