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