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