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