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