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