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