xref: /freebsd/sys/netinet/sctp_usrreq.c (revision 430f7286a566b1407c7b32ce13585caf5aa59b92)
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 	    inp->sctp_ep.port,
1478 	    (struct thread *)p
1479 	    );
1480 	if (stcb == NULL) {
1481 		/* Gak! no memory */
1482 		goto out_now;
1483 	}
1484 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1485 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1486 		/* Set the connected flag so we can queue data */
1487 		soisconnecting(so);
1488 	}
1489 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1490 	/* move to second address */
1491 	switch (sa->sa_family) {
1492 #ifdef INET
1493 	case AF_INET:
1494 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1495 		break;
1496 #endif
1497 #ifdef INET6
1498 	case AF_INET6:
1499 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1500 		break;
1501 #endif
1502 	default:
1503 		break;
1504 	}
1505 
1506 	error = 0;
1507 	sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1508 	/* Fill in the return id */
1509 	if (error) {
1510 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
1511 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1512 		goto out_now;
1513 	}
1514 	a_id = (sctp_assoc_t *) optval;
1515 	*a_id = sctp_get_associd(stcb);
1516 
1517 	/* initialize authentication parameters for the assoc */
1518 	sctp_initialize_auth_params(inp, stcb);
1519 
1520 	if (delay) {
1521 		/* doing delayed connection */
1522 		stcb->asoc.delayed_connection = 1;
1523 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1524 	} else {
1525 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1526 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1527 	}
1528 	SCTP_TCB_UNLOCK(stcb);
1529 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1530 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1531 		/* Set the connected flag so we can queue data */
1532 		soisconnecting(so);
1533 	}
1534 out_now:
1535 	if (creat_lock_on) {
1536 		SCTP_ASOC_CREATE_UNLOCK(inp);
1537 	}
1538 	SCTP_INP_DECR_REF(inp);
1539 	return (error);
1540 }
1541 
1542 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1543 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1544 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1545 		SCTP_INP_RLOCK(inp); \
1546 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1547 		if (stcb) { \
1548 			SCTP_TCB_LOCK(stcb); \
1549                 } \
1550 		SCTP_INP_RUNLOCK(inp); \
1551 	} else if (assoc_id > SCTP_ALL_ASSOC) { \
1552 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1553 		if (stcb == NULL) { \
1554 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1555 			error = ENOENT; \
1556 			break; \
1557 		} \
1558 	} else { \
1559 		stcb = NULL; \
1560         } \
1561   }
1562 
1563 
1564 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1565 	if (size < sizeof(type)) { \
1566 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1567 		error = EINVAL; \
1568 		break; \
1569 	} else { \
1570 		destp = (type *)srcp; \
1571 	} \
1572       }
1573 
1574 static int
1575 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1576     void *p)
1577 {
1578 	struct sctp_inpcb *inp = NULL;
1579 	int error, val = 0;
1580 	struct sctp_tcb *stcb = NULL;
1581 
1582 	if (optval == NULL) {
1583 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1584 		return (EINVAL);
1585 	}
1586 	inp = (struct sctp_inpcb *)so->so_pcb;
1587 	if (inp == NULL) {
1588 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1589 		return EINVAL;
1590 	}
1591 	error = 0;
1592 
1593 	switch (optname) {
1594 	case SCTP_NODELAY:
1595 	case SCTP_AUTOCLOSE:
1596 	case SCTP_EXPLICIT_EOR:
1597 	case SCTP_AUTO_ASCONF:
1598 	case SCTP_DISABLE_FRAGMENTS:
1599 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1600 	case SCTP_USE_EXT_RCVINFO:
1601 		SCTP_INP_RLOCK(inp);
1602 		switch (optname) {
1603 		case SCTP_DISABLE_FRAGMENTS:
1604 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1605 			break;
1606 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1607 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1608 			break;
1609 		case SCTP_AUTO_ASCONF:
1610 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1611 				/* only valid for bound all sockets */
1612 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1613 			} else {
1614 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1615 				error = EINVAL;
1616 				goto flags_out;
1617 			}
1618 			break;
1619 		case SCTP_EXPLICIT_EOR:
1620 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1621 			break;
1622 		case SCTP_NODELAY:
1623 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1624 			break;
1625 		case SCTP_USE_EXT_RCVINFO:
1626 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1627 			break;
1628 		case SCTP_AUTOCLOSE:
1629 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1630 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1631 			else
1632 				val = 0;
1633 			break;
1634 
1635 		default:
1636 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1637 			error = ENOPROTOOPT;
1638 		}		/* end switch (sopt->sopt_name) */
1639 		if (*optsize < sizeof(val)) {
1640 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1641 			error = EINVAL;
1642 		}
1643 flags_out:
1644 		SCTP_INP_RUNLOCK(inp);
1645 		if (error == 0) {
1646 			/* return the option value */
1647 			*(int *)optval = val;
1648 			*optsize = sizeof(val);
1649 		}
1650 		break;
1651 	case SCTP_GET_PACKET_LOG:
1652 		{
1653 #ifdef  SCTP_PACKET_LOGGING
1654 			uint8_t *target;
1655 			int ret;
1656 
1657 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1658 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1659 			*optsize = ret;
1660 #else
1661 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1662 			error = EOPNOTSUPP;
1663 #endif
1664 			break;
1665 		}
1666 	case SCTP_REUSE_PORT:
1667 		{
1668 			uint32_t *value;
1669 
1670 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1671 				/* Can't do this for a 1-m socket */
1672 				error = EINVAL;
1673 				break;
1674 			}
1675 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1676 			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1677 			*optsize = sizeof(uint32_t);
1678 			break;
1679 		}
1680 	case SCTP_PARTIAL_DELIVERY_POINT:
1681 		{
1682 			uint32_t *value;
1683 
1684 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1685 			*value = inp->partial_delivery_point;
1686 			*optsize = sizeof(uint32_t);
1687 			break;
1688 		}
1689 	case SCTP_FRAGMENT_INTERLEAVE:
1690 		{
1691 			uint32_t *value;
1692 
1693 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1694 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1695 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1696 					*value = SCTP_FRAG_LEVEL_2;
1697 				} else {
1698 					*value = SCTP_FRAG_LEVEL_1;
1699 				}
1700 			} else {
1701 				*value = SCTP_FRAG_LEVEL_0;
1702 			}
1703 			*optsize = sizeof(uint32_t);
1704 			break;
1705 		}
1706 	case SCTP_INTERLEAVING_SUPPORTED:
1707 		{
1708 			struct sctp_assoc_value *av;
1709 
1710 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1711 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1712 
1713 			if (stcb) {
1714 				av->assoc_value = stcb->asoc.idata_supported;
1715 				SCTP_TCB_UNLOCK(stcb);
1716 			} else {
1717 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1718 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1719 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1720 					SCTP_INP_RLOCK(inp);
1721 					if (inp->idata_supported) {
1722 						av->assoc_value = 1;
1723 					} else {
1724 						av->assoc_value = 0;
1725 					}
1726 					SCTP_INP_RUNLOCK(inp);
1727 				} else {
1728 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1729 					error = EINVAL;
1730 				}
1731 			}
1732 			if (error == 0) {
1733 				*optsize = sizeof(struct sctp_assoc_value);
1734 			}
1735 			break;
1736 		}
1737 	case SCTP_CMT_ON_OFF:
1738 		{
1739 			struct sctp_assoc_value *av;
1740 
1741 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1742 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1743 			if (stcb) {
1744 				av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1745 				SCTP_TCB_UNLOCK(stcb);
1746 			} else {
1747 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1748 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1749 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1750 					SCTP_INP_RLOCK(inp);
1751 					av->assoc_value = inp->sctp_cmt_on_off;
1752 					SCTP_INP_RUNLOCK(inp);
1753 				} else {
1754 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1755 					error = EINVAL;
1756 				}
1757 			}
1758 			if (error == 0) {
1759 				*optsize = sizeof(struct sctp_assoc_value);
1760 			}
1761 			break;
1762 		}
1763 	case SCTP_PLUGGABLE_CC:
1764 		{
1765 			struct sctp_assoc_value *av;
1766 
1767 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1768 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1769 			if (stcb) {
1770 				av->assoc_value = stcb->asoc.congestion_control_module;
1771 				SCTP_TCB_UNLOCK(stcb);
1772 			} else {
1773 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1774 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1775 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1776 					SCTP_INP_RLOCK(inp);
1777 					av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1778 					SCTP_INP_RUNLOCK(inp);
1779 				} else {
1780 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1781 					error = EINVAL;
1782 				}
1783 			}
1784 			if (error == 0) {
1785 				*optsize = sizeof(struct sctp_assoc_value);
1786 			}
1787 			break;
1788 		}
1789 	case SCTP_CC_OPTION:
1790 		{
1791 			struct sctp_cc_option *cc_opt;
1792 
1793 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1794 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1795 			if (stcb == NULL) {
1796 				error = EINVAL;
1797 			} else {
1798 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1799 					error = ENOTSUP;
1800 				} else {
1801 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1802 					*optsize = sizeof(struct sctp_cc_option);
1803 				}
1804 				SCTP_TCB_UNLOCK(stcb);
1805 			}
1806 			break;
1807 		}
1808 	case SCTP_PLUGGABLE_SS:
1809 		{
1810 			struct sctp_assoc_value *av;
1811 
1812 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1813 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1814 			if (stcb) {
1815 				av->assoc_value = stcb->asoc.stream_scheduling_module;
1816 				SCTP_TCB_UNLOCK(stcb);
1817 			} else {
1818 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1819 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1820 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1821 					SCTP_INP_RLOCK(inp);
1822 					av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1823 					SCTP_INP_RUNLOCK(inp);
1824 				} else {
1825 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1826 					error = EINVAL;
1827 				}
1828 			}
1829 			if (error == 0) {
1830 				*optsize = sizeof(struct sctp_assoc_value);
1831 			}
1832 			break;
1833 		}
1834 	case SCTP_SS_VALUE:
1835 		{
1836 			struct sctp_stream_value *av;
1837 
1838 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1839 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1840 			if (stcb) {
1841 				if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
1842 				    (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1843 				    &av->stream_value) < 0)) {
1844 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1845 					error = EINVAL;
1846 				} else {
1847 					*optsize = sizeof(struct sctp_stream_value);
1848 				}
1849 				SCTP_TCB_UNLOCK(stcb);
1850 			} else {
1851 				/*
1852 				 * Can't get stream value without
1853 				 * association
1854 				 */
1855 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1856 				error = EINVAL;
1857 			}
1858 			break;
1859 		}
1860 	case SCTP_GET_ADDR_LEN:
1861 		{
1862 			struct sctp_assoc_value *av;
1863 
1864 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1865 			error = EINVAL;
1866 #ifdef INET
1867 			if (av->assoc_value == AF_INET) {
1868 				av->assoc_value = sizeof(struct sockaddr_in);
1869 				error = 0;
1870 			}
1871 #endif
1872 #ifdef INET6
1873 			if (av->assoc_value == AF_INET6) {
1874 				av->assoc_value = sizeof(struct sockaddr_in6);
1875 				error = 0;
1876 			}
1877 #endif
1878 			if (error) {
1879 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1880 			} else {
1881 				*optsize = sizeof(struct sctp_assoc_value);
1882 			}
1883 			break;
1884 		}
1885 	case SCTP_GET_ASSOC_NUMBER:
1886 		{
1887 			uint32_t *value, cnt;
1888 
1889 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1890 			SCTP_INP_RLOCK(inp);
1891 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1892 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1893 				/* Can't do this for a 1-1 socket */
1894 				error = EINVAL;
1895 				SCTP_INP_RUNLOCK(inp);
1896 				break;
1897 			}
1898 			cnt = 0;
1899 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1900 				cnt++;
1901 			}
1902 			SCTP_INP_RUNLOCK(inp);
1903 			*value = cnt;
1904 			*optsize = sizeof(uint32_t);
1905 			break;
1906 		}
1907 	case SCTP_GET_ASSOC_ID_LIST:
1908 		{
1909 			struct sctp_assoc_ids *ids;
1910 			uint32_t at;
1911 			size_t limit;
1912 
1913 			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1914 			SCTP_INP_RLOCK(inp);
1915 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1916 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1917 				/* Can't do this for a 1-1 socket */
1918 				error = EINVAL;
1919 				SCTP_INP_RUNLOCK(inp);
1920 				break;
1921 			}
1922 			at = 0;
1923 			limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1924 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1925 				if (at < limit) {
1926 					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1927 					if (at == 0) {
1928 						error = EINVAL;
1929 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1930 						break;
1931 					}
1932 				} else {
1933 					error = EINVAL;
1934 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1935 					break;
1936 				}
1937 			}
1938 			SCTP_INP_RUNLOCK(inp);
1939 			if (error == 0) {
1940 				ids->gaids_number_of_ids = at;
1941 				*optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1942 			}
1943 			break;
1944 		}
1945 	case SCTP_CONTEXT:
1946 		{
1947 			struct sctp_assoc_value *av;
1948 
1949 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1950 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1951 
1952 			if (stcb) {
1953 				av->assoc_value = stcb->asoc.context;
1954 				SCTP_TCB_UNLOCK(stcb);
1955 			} else {
1956 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1957 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1958 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1959 					SCTP_INP_RLOCK(inp);
1960 					av->assoc_value = inp->sctp_context;
1961 					SCTP_INP_RUNLOCK(inp);
1962 				} else {
1963 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1964 					error = EINVAL;
1965 				}
1966 			}
1967 			if (error == 0) {
1968 				*optsize = sizeof(struct sctp_assoc_value);
1969 			}
1970 			break;
1971 		}
1972 	case SCTP_VRF_ID:
1973 		{
1974 			uint32_t *default_vrfid;
1975 
1976 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1977 			*default_vrfid = inp->def_vrf_id;
1978 			*optsize = sizeof(uint32_t);
1979 			break;
1980 		}
1981 	case SCTP_GET_ASOC_VRF:
1982 		{
1983 			struct sctp_assoc_value *id;
1984 
1985 			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1986 			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1987 			if (stcb == NULL) {
1988 				error = EINVAL;
1989 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1990 			} else {
1991 				id->assoc_value = stcb->asoc.vrf_id;
1992 				*optsize = sizeof(struct sctp_assoc_value);
1993 			}
1994 			break;
1995 		}
1996 	case SCTP_GET_VRF_IDS:
1997 		{
1998 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1999 			error = EOPNOTSUPP;
2000 			break;
2001 		}
2002 	case SCTP_GET_NONCE_VALUES:
2003 		{
2004 			struct sctp_get_nonce_values *gnv;
2005 
2006 			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
2007 			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
2008 
2009 			if (stcb) {
2010 				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
2011 				gnv->gn_local_tag = stcb->asoc.my_vtag;
2012 				SCTP_TCB_UNLOCK(stcb);
2013 				*optsize = sizeof(struct sctp_get_nonce_values);
2014 			} else {
2015 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2016 				error = ENOTCONN;
2017 			}
2018 			break;
2019 		}
2020 	case SCTP_DELAYED_SACK:
2021 		{
2022 			struct sctp_sack_info *sack;
2023 
2024 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
2025 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2026 			if (stcb) {
2027 				sack->sack_delay = stcb->asoc.delayed_ack;
2028 				sack->sack_freq = stcb->asoc.sack_freq;
2029 				SCTP_TCB_UNLOCK(stcb);
2030 			} else {
2031 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2032 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2033 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2034 					SCTP_INP_RLOCK(inp);
2035 					sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2036 					sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2037 					SCTP_INP_RUNLOCK(inp);
2038 				} else {
2039 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2040 					error = EINVAL;
2041 				}
2042 			}
2043 			if (error == 0) {
2044 				*optsize = sizeof(struct sctp_sack_info);
2045 			}
2046 			break;
2047 		}
2048 	case SCTP_GET_SNDBUF_USE:
2049 		{
2050 			struct sctp_sockstat *ss;
2051 
2052 			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2053 			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2054 
2055 			if (stcb) {
2056 				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2057 				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2058 				    stcb->asoc.size_on_all_streams);
2059 				SCTP_TCB_UNLOCK(stcb);
2060 				*optsize = sizeof(struct sctp_sockstat);
2061 			} else {
2062 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2063 				error = ENOTCONN;
2064 			}
2065 			break;
2066 		}
2067 	case SCTP_MAX_BURST:
2068 		{
2069 			struct sctp_assoc_value *av;
2070 
2071 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2072 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2073 
2074 			if (stcb) {
2075 				av->assoc_value = stcb->asoc.max_burst;
2076 				SCTP_TCB_UNLOCK(stcb);
2077 			} else {
2078 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2079 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2080 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2081 					SCTP_INP_RLOCK(inp);
2082 					av->assoc_value = inp->sctp_ep.max_burst;
2083 					SCTP_INP_RUNLOCK(inp);
2084 				} else {
2085 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2086 					error = EINVAL;
2087 				}
2088 			}
2089 			if (error == 0) {
2090 				*optsize = sizeof(struct sctp_assoc_value);
2091 			}
2092 			break;
2093 		}
2094 	case SCTP_MAXSEG:
2095 		{
2096 			struct sctp_assoc_value *av;
2097 			int ovh;
2098 
2099 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2100 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2101 
2102 			if (stcb) {
2103 				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2104 				SCTP_TCB_UNLOCK(stcb);
2105 			} else {
2106 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2107 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2108 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2109 					SCTP_INP_RLOCK(inp);
2110 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2111 						ovh = SCTP_MED_OVERHEAD;
2112 					} else {
2113 						ovh = SCTP_MED_V4_OVERHEAD;
2114 					}
2115 					if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2116 						av->assoc_value = 0;
2117 					else
2118 						av->assoc_value = inp->sctp_frag_point - ovh;
2119 					SCTP_INP_RUNLOCK(inp);
2120 				} else {
2121 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2122 					error = EINVAL;
2123 				}
2124 			}
2125 			if (error == 0) {
2126 				*optsize = sizeof(struct sctp_assoc_value);
2127 			}
2128 			break;
2129 		}
2130 	case SCTP_GET_STAT_LOG:
2131 		error = sctp_fill_stat_log(optval, optsize);
2132 		break;
2133 	case SCTP_EVENTS:
2134 		{
2135 			struct sctp_event_subscribe *events;
2136 
2137 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2138 			memset(events, 0, sizeof(struct sctp_event_subscribe));
2139 			SCTP_INP_RLOCK(inp);
2140 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2141 				events->sctp_data_io_event = 1;
2142 
2143 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2144 				events->sctp_association_event = 1;
2145 
2146 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2147 				events->sctp_address_event = 1;
2148 
2149 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2150 				events->sctp_send_failure_event = 1;
2151 
2152 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2153 				events->sctp_peer_error_event = 1;
2154 
2155 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2156 				events->sctp_shutdown_event = 1;
2157 
2158 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2159 				events->sctp_partial_delivery_event = 1;
2160 
2161 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2162 				events->sctp_adaptation_layer_event = 1;
2163 
2164 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2165 				events->sctp_authentication_event = 1;
2166 
2167 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2168 				events->sctp_sender_dry_event = 1;
2169 
2170 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2171 				events->sctp_stream_reset_event = 1;
2172 			SCTP_INP_RUNLOCK(inp);
2173 			*optsize = sizeof(struct sctp_event_subscribe);
2174 			break;
2175 		}
2176 	case SCTP_ADAPTATION_LAYER:
2177 		{
2178 			uint32_t *value;
2179 
2180 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2181 
2182 			SCTP_INP_RLOCK(inp);
2183 			*value = inp->sctp_ep.adaptation_layer_indicator;
2184 			SCTP_INP_RUNLOCK(inp);
2185 			*optsize = sizeof(uint32_t);
2186 			break;
2187 		}
2188 	case SCTP_SET_INITIAL_DBG_SEQ:
2189 		{
2190 			uint32_t *value;
2191 
2192 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2193 			SCTP_INP_RLOCK(inp);
2194 			*value = inp->sctp_ep.initial_sequence_debug;
2195 			SCTP_INP_RUNLOCK(inp);
2196 			*optsize = sizeof(uint32_t);
2197 			break;
2198 		}
2199 	case SCTP_GET_LOCAL_ADDR_SIZE:
2200 		{
2201 			uint32_t *value;
2202 
2203 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2204 			SCTP_INP_RLOCK(inp);
2205 			*value = sctp_count_max_addresses(inp);
2206 			SCTP_INP_RUNLOCK(inp);
2207 			*optsize = sizeof(uint32_t);
2208 			break;
2209 		}
2210 	case SCTP_GET_REMOTE_ADDR_SIZE:
2211 		{
2212 			uint32_t *value;
2213 			size_t size;
2214 			struct sctp_nets *net;
2215 
2216 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2217 			/* FIXME MT: change to sctp_assoc_value? */
2218 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2219 
2220 			if (stcb) {
2221 				size = 0;
2222 				/* Count the sizes */
2223 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2224 					switch (net->ro._l_addr.sa.sa_family) {
2225 #ifdef INET
2226 					case AF_INET:
2227 #ifdef INET6
2228 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2229 							size += sizeof(struct sockaddr_in6);
2230 						} else {
2231 							size += sizeof(struct sockaddr_in);
2232 						}
2233 #else
2234 						size += sizeof(struct sockaddr_in);
2235 #endif
2236 						break;
2237 #endif
2238 #ifdef INET6
2239 					case AF_INET6:
2240 						size += sizeof(struct sockaddr_in6);
2241 						break;
2242 #endif
2243 					default:
2244 						break;
2245 					}
2246 				}
2247 				SCTP_TCB_UNLOCK(stcb);
2248 				*value = (uint32_t) size;
2249 				*optsize = sizeof(uint32_t);
2250 			} else {
2251 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2252 				error = ENOTCONN;
2253 			}
2254 			break;
2255 		}
2256 	case SCTP_GET_PEER_ADDRESSES:
2257 		/*
2258 		 * Get the address information, an array is passed in to
2259 		 * fill up we pack it.
2260 		 */
2261 		{
2262 			size_t cpsz, left;
2263 			struct sockaddr_storage *sas;
2264 			struct sctp_nets *net;
2265 			struct sctp_getaddresses *saddr;
2266 
2267 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2268 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2269 
2270 			if (stcb) {
2271 				left = (*optsize) - sizeof(struct sctp_getaddresses);
2272 				*optsize = sizeof(struct sctp_getaddresses);
2273 				sas = (struct sockaddr_storage *)&saddr->addr[0];
2274 
2275 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2276 					switch (net->ro._l_addr.sa.sa_family) {
2277 #ifdef INET
2278 					case AF_INET:
2279 #ifdef INET6
2280 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2281 							cpsz = sizeof(struct sockaddr_in6);
2282 						} else {
2283 							cpsz = sizeof(struct sockaddr_in);
2284 						}
2285 #else
2286 						cpsz = sizeof(struct sockaddr_in);
2287 #endif
2288 						break;
2289 #endif
2290 #ifdef INET6
2291 					case AF_INET6:
2292 						cpsz = sizeof(struct sockaddr_in6);
2293 						break;
2294 #endif
2295 					default:
2296 						cpsz = 0;
2297 						break;
2298 					}
2299 					if (cpsz == 0) {
2300 						break;
2301 					}
2302 					if (left < cpsz) {
2303 						/* not enough room. */
2304 						break;
2305 					}
2306 #if defined(INET) && defined(INET6)
2307 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2308 					    (net->ro._l_addr.sa.sa_family == AF_INET)) {
2309 						/* Must map the address */
2310 						in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
2311 						    (struct sockaddr_in6 *)sas);
2312 					} else {
2313 						memcpy(sas, &net->ro._l_addr, cpsz);
2314 					}
2315 #else
2316 					memcpy(sas, &net->ro._l_addr, cpsz);
2317 #endif
2318 					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2319 
2320 					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2321 					left -= cpsz;
2322 					*optsize += cpsz;
2323 				}
2324 				SCTP_TCB_UNLOCK(stcb);
2325 			} else {
2326 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2327 				error = ENOENT;
2328 			}
2329 			break;
2330 		}
2331 	case SCTP_GET_LOCAL_ADDRESSES:
2332 		{
2333 			size_t limit, actual;
2334 			struct sockaddr_storage *sas;
2335 			struct sctp_getaddresses *saddr;
2336 
2337 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2338 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2339 
2340 			sas = (struct sockaddr_storage *)&saddr->addr[0];
2341 			limit = *optsize - sizeof(sctp_assoc_t);
2342 			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2343 			if (stcb) {
2344 				SCTP_TCB_UNLOCK(stcb);
2345 			}
2346 			*optsize = sizeof(struct sockaddr_storage) + actual;
2347 			break;
2348 		}
2349 	case SCTP_PEER_ADDR_PARAMS:
2350 		{
2351 			struct sctp_paddrparams *paddrp;
2352 			struct sctp_nets *net;
2353 			struct sockaddr *addr;
2354 
2355 #if defined(INET) && defined(INET6)
2356 			struct sockaddr_in sin_store;
2357 
2358 #endif
2359 
2360 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2361 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2362 
2363 #if defined(INET) && defined(INET6)
2364 			if (paddrp->spp_address.ss_family == AF_INET6) {
2365 				struct sockaddr_in6 *sin6;
2366 
2367 				sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
2368 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2369 					in6_sin6_2_sin(&sin_store, sin6);
2370 					addr = (struct sockaddr *)&sin_store;
2371 				} else {
2372 					addr = (struct sockaddr *)&paddrp->spp_address;
2373 				}
2374 			} else {
2375 				addr = (struct sockaddr *)&paddrp->spp_address;
2376 			}
2377 #else
2378 			addr = (struct sockaddr *)&paddrp->spp_address;
2379 #endif
2380 			if (stcb != NULL) {
2381 				net = sctp_findnet(stcb, addr);
2382 			} else {
2383 				/*
2384 				 * We increment here since
2385 				 * sctp_findassociation_ep_addr() wil do a
2386 				 * decrement if it finds the stcb as long as
2387 				 * the locked tcb (last argument) is NOT a
2388 				 * TCB.. aka NULL.
2389 				 */
2390 				net = NULL;
2391 				SCTP_INP_INCR_REF(inp);
2392 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2393 				if (stcb == NULL) {
2394 					SCTP_INP_DECR_REF(inp);
2395 				}
2396 			}
2397 			if ((stcb != NULL) && (net == NULL)) {
2398 #ifdef INET
2399 				if (addr->sa_family == AF_INET) {
2400 					struct sockaddr_in *sin;
2401 
2402 					sin = (struct sockaddr_in *)addr;
2403 					if (sin->sin_addr.s_addr != INADDR_ANY) {
2404 						error = EINVAL;
2405 						SCTP_TCB_UNLOCK(stcb);
2406 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2407 						break;
2408 					}
2409 				} else
2410 #endif
2411 #ifdef INET6
2412 				if (addr->sa_family == AF_INET6) {
2413 					struct sockaddr_in6 *sin6;
2414 
2415 					sin6 = (struct sockaddr_in6 *)addr;
2416 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2417 						error = EINVAL;
2418 						SCTP_TCB_UNLOCK(stcb);
2419 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2420 						break;
2421 					}
2422 				} else
2423 #endif
2424 				{
2425 					error = EAFNOSUPPORT;
2426 					SCTP_TCB_UNLOCK(stcb);
2427 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2428 					break;
2429 				}
2430 			}
2431 			if (stcb != NULL) {
2432 				/* Applies to the specific association */
2433 				paddrp->spp_flags = 0;
2434 				if (net != NULL) {
2435 					paddrp->spp_hbinterval = net->heart_beat_delay;
2436 					paddrp->spp_pathmaxrxt = net->failure_threshold;
2437 					paddrp->spp_pathmtu = net->mtu;
2438 					switch (net->ro._l_addr.sa.sa_family) {
2439 #ifdef INET
2440 					case AF_INET:
2441 						paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2442 						break;
2443 #endif
2444 #ifdef INET6
2445 					case AF_INET6:
2446 						paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2447 						break;
2448 #endif
2449 					default:
2450 						break;
2451 					}
2452 					/* get flags for HB */
2453 					if (net->dest_state & SCTP_ADDR_NOHB) {
2454 						paddrp->spp_flags |= SPP_HB_DISABLE;
2455 					} else {
2456 						paddrp->spp_flags |= SPP_HB_ENABLE;
2457 					}
2458 					/* get flags for PMTU */
2459 					if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2460 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2461 					} else {
2462 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2463 					}
2464 					if (net->dscp & 0x01) {
2465 						paddrp->spp_dscp = net->dscp & 0xfc;
2466 						paddrp->spp_flags |= SPP_DSCP;
2467 					}
2468 #ifdef INET6
2469 					if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2470 					    (net->flowlabel & 0x80000000)) {
2471 						paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2472 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2473 					}
2474 #endif
2475 				} else {
2476 					/*
2477 					 * No destination so return default
2478 					 * value
2479 					 */
2480 					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2481 					paddrp->spp_pathmtu = 0;
2482 					if (stcb->asoc.default_dscp & 0x01) {
2483 						paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2484 						paddrp->spp_flags |= SPP_DSCP;
2485 					}
2486 #ifdef INET6
2487 					if (stcb->asoc.default_flowlabel & 0x80000000) {
2488 						paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2489 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2490 					}
2491 #endif
2492 					/* default settings should be these */
2493 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2494 						paddrp->spp_flags |= SPP_HB_DISABLE;
2495 					} else {
2496 						paddrp->spp_flags |= SPP_HB_ENABLE;
2497 					}
2498 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2499 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2500 					} else {
2501 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2502 					}
2503 					paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2504 				}
2505 				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2506 				SCTP_TCB_UNLOCK(stcb);
2507 			} else {
2508 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2509 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2510 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2511 					/* Use endpoint defaults */
2512 					SCTP_INP_RLOCK(inp);
2513 					paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2514 					paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2515 					paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2516 					/* get inp's default */
2517 					if (inp->sctp_ep.default_dscp & 0x01) {
2518 						paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2519 						paddrp->spp_flags |= SPP_DSCP;
2520 					}
2521 #ifdef INET6
2522 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2523 					    (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2524 						paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2525 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2526 					}
2527 #endif
2528 					/* can't return this */
2529 					paddrp->spp_pathmtu = 0;
2530 
2531 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2532 						paddrp->spp_flags |= SPP_HB_ENABLE;
2533 					} else {
2534 						paddrp->spp_flags |= SPP_HB_DISABLE;
2535 					}
2536 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2537 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2538 					} else {
2539 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2540 					}
2541 					SCTP_INP_RUNLOCK(inp);
2542 				} else {
2543 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2544 					error = EINVAL;
2545 				}
2546 			}
2547 			if (error == 0) {
2548 				*optsize = sizeof(struct sctp_paddrparams);
2549 			}
2550 			break;
2551 		}
2552 	case SCTP_GET_PEER_ADDR_INFO:
2553 		{
2554 			struct sctp_paddrinfo *paddri;
2555 			struct sctp_nets *net;
2556 			struct sockaddr *addr;
2557 
2558 #if defined(INET) && defined(INET6)
2559 			struct sockaddr_in sin_store;
2560 
2561 #endif
2562 
2563 			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2564 			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2565 
2566 #if defined(INET) && defined(INET6)
2567 			if (paddri->spinfo_address.ss_family == AF_INET6) {
2568 				struct sockaddr_in6 *sin6;
2569 
2570 				sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
2571 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2572 					in6_sin6_2_sin(&sin_store, sin6);
2573 					addr = (struct sockaddr *)&sin_store;
2574 				} else {
2575 					addr = (struct sockaddr *)&paddri->spinfo_address;
2576 				}
2577 			} else {
2578 				addr = (struct sockaddr *)&paddri->spinfo_address;
2579 			}
2580 #else
2581 			addr = (struct sockaddr *)&paddri->spinfo_address;
2582 #endif
2583 			if (stcb != NULL) {
2584 				net = sctp_findnet(stcb, addr);
2585 			} else {
2586 				/*
2587 				 * We increment here since
2588 				 * sctp_findassociation_ep_addr() wil do a
2589 				 * decrement if it finds the stcb as long as
2590 				 * the locked tcb (last argument) is NOT a
2591 				 * TCB.. aka NULL.
2592 				 */
2593 				net = NULL;
2594 				SCTP_INP_INCR_REF(inp);
2595 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2596 				if (stcb == NULL) {
2597 					SCTP_INP_DECR_REF(inp);
2598 				}
2599 			}
2600 
2601 			if ((stcb != NULL) && (net != NULL)) {
2602 				if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2603 					/* It's unconfirmed */
2604 					paddri->spinfo_state = SCTP_UNCONFIRMED;
2605 				} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2606 					/* It's active */
2607 					paddri->spinfo_state = SCTP_ACTIVE;
2608 				} else {
2609 					/* It's inactive */
2610 					paddri->spinfo_state = SCTP_INACTIVE;
2611 				}
2612 				paddri->spinfo_cwnd = net->cwnd;
2613 				paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2614 				paddri->spinfo_rto = net->RTO;
2615 				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2616 				paddri->spinfo_mtu = net->mtu;
2617 				switch (addr->sa_family) {
2618 #if defined(INET)
2619 				case AF_INET:
2620 					paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2621 					break;
2622 #endif
2623 #if defined(INET6)
2624 				case AF_INET6:
2625 					paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
2626 					break;
2627 #endif
2628 				default:
2629 					break;
2630 				}
2631 				SCTP_TCB_UNLOCK(stcb);
2632 				*optsize = sizeof(struct sctp_paddrinfo);
2633 			} else {
2634 				if (stcb != NULL) {
2635 					SCTP_TCB_UNLOCK(stcb);
2636 				}
2637 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2638 				error = ENOENT;
2639 			}
2640 			break;
2641 		}
2642 	case SCTP_PCB_STATUS:
2643 		{
2644 			struct sctp_pcbinfo *spcb;
2645 
2646 			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2647 			sctp_fill_pcbinfo(spcb);
2648 			*optsize = sizeof(struct sctp_pcbinfo);
2649 			break;
2650 		}
2651 	case SCTP_STATUS:
2652 		{
2653 			struct sctp_nets *net;
2654 			struct sctp_status *sstat;
2655 
2656 			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2657 			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2658 
2659 			if (stcb == NULL) {
2660 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2661 				error = EINVAL;
2662 				break;
2663 			}
2664 			sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
2665 			sstat->sstat_assoc_id = sctp_get_associd(stcb);
2666 			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2667 			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2668 			/*
2669 			 * We can't include chunks that have been passed to
2670 			 * the socket layer. Only things in queue.
2671 			 */
2672 			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2673 			    stcb->asoc.cnt_on_all_streams);
2674 
2675 
2676 			sstat->sstat_instrms = stcb->asoc.streamincnt;
2677 			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2678 			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2679 			memcpy(&sstat->sstat_primary.spinfo_address,
2680 			    &stcb->asoc.primary_destination->ro._l_addr,
2681 			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2682 			net = stcb->asoc.primary_destination;
2683 			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2684 			/*
2685 			 * Again the user can get info from sctp_constants.h
2686 			 * for what the state of the network is.
2687 			 */
2688 			if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2689 				/* It's unconfirmed */
2690 				sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2691 			} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2692 				/* It's active */
2693 				sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2694 			} else {
2695 				/* It's inactive */
2696 				sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2697 			}
2698 			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2699 			sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2700 			sstat->sstat_primary.spinfo_rto = net->RTO;
2701 			sstat->sstat_primary.spinfo_mtu = net->mtu;
2702 			switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
2703 #if defined(INET)
2704 			case AF_INET:
2705 				sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2706 				break;
2707 #endif
2708 #if defined(INET6)
2709 			case AF_INET6:
2710 				sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
2711 				break;
2712 #endif
2713 			default:
2714 				break;
2715 			}
2716 			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2717 			SCTP_TCB_UNLOCK(stcb);
2718 			*optsize = sizeof(struct sctp_status);
2719 			break;
2720 		}
2721 	case SCTP_RTOINFO:
2722 		{
2723 			struct sctp_rtoinfo *srto;
2724 
2725 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2726 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2727 
2728 			if (stcb) {
2729 				srto->srto_initial = stcb->asoc.initial_rto;
2730 				srto->srto_max = stcb->asoc.maxrto;
2731 				srto->srto_min = stcb->asoc.minrto;
2732 				SCTP_TCB_UNLOCK(stcb);
2733 			} else {
2734 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2735 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2736 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2737 					SCTP_INP_RLOCK(inp);
2738 					srto->srto_initial = inp->sctp_ep.initial_rto;
2739 					srto->srto_max = inp->sctp_ep.sctp_maxrto;
2740 					srto->srto_min = inp->sctp_ep.sctp_minrto;
2741 					SCTP_INP_RUNLOCK(inp);
2742 				} else {
2743 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2744 					error = EINVAL;
2745 				}
2746 			}
2747 			if (error == 0) {
2748 				*optsize = sizeof(struct sctp_rtoinfo);
2749 			}
2750 			break;
2751 		}
2752 	case SCTP_TIMEOUTS:
2753 		{
2754 			struct sctp_timeouts *stimo;
2755 
2756 			SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2757 			SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2758 
2759 			if (stcb) {
2760 				stimo->stimo_init = stcb->asoc.timoinit;
2761 				stimo->stimo_data = stcb->asoc.timodata;
2762 				stimo->stimo_sack = stcb->asoc.timosack;
2763 				stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2764 				stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2765 				stimo->stimo_cookie = stcb->asoc.timocookie;
2766 				stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2767 				SCTP_TCB_UNLOCK(stcb);
2768 				*optsize = sizeof(struct sctp_timeouts);
2769 			} else {
2770 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2771 				error = EINVAL;
2772 			}
2773 			break;
2774 		}
2775 	case SCTP_ASSOCINFO:
2776 		{
2777 			struct sctp_assocparams *sasoc;
2778 
2779 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2780 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2781 
2782 			if (stcb) {
2783 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2784 				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2785 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2786 				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2787 				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2788 				SCTP_TCB_UNLOCK(stcb);
2789 			} else {
2790 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2791 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2792 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2793 					SCTP_INP_RLOCK(inp);
2794 					sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2795 					sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2796 					sasoc->sasoc_number_peer_destinations = 0;
2797 					sasoc->sasoc_peer_rwnd = 0;
2798 					sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2799 					SCTP_INP_RUNLOCK(inp);
2800 				} else {
2801 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2802 					error = EINVAL;
2803 				}
2804 			}
2805 			if (error == 0) {
2806 				*optsize = sizeof(struct sctp_assocparams);
2807 			}
2808 			break;
2809 		}
2810 	case SCTP_DEFAULT_SEND_PARAM:
2811 		{
2812 			struct sctp_sndrcvinfo *s_info;
2813 
2814 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2815 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2816 
2817 			if (stcb) {
2818 				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2819 				SCTP_TCB_UNLOCK(stcb);
2820 			} else {
2821 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2822 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2823 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2824 					SCTP_INP_RLOCK(inp);
2825 					memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2826 					SCTP_INP_RUNLOCK(inp);
2827 				} else {
2828 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2829 					error = EINVAL;
2830 				}
2831 			}
2832 			if (error == 0) {
2833 				*optsize = sizeof(struct sctp_sndrcvinfo);
2834 			}
2835 			break;
2836 		}
2837 	case SCTP_INITMSG:
2838 		{
2839 			struct sctp_initmsg *sinit;
2840 
2841 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2842 			SCTP_INP_RLOCK(inp);
2843 			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2844 			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2845 			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2846 			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2847 			SCTP_INP_RUNLOCK(inp);
2848 			*optsize = sizeof(struct sctp_initmsg);
2849 			break;
2850 		}
2851 	case SCTP_PRIMARY_ADDR:
2852 		/* we allow a "get" operation on this */
2853 		{
2854 			struct sctp_setprim *ssp;
2855 
2856 			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2857 			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2858 
2859 			if (stcb) {
2860 				union sctp_sockstore *addr;
2861 
2862 				addr = &stcb->asoc.primary_destination->ro._l_addr;
2863 				switch (addr->sa.sa_family) {
2864 #ifdef INET
2865 				case AF_INET:
2866 #ifdef INET6
2867 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2868 						in6_sin_2_v4mapsin6(&addr->sin,
2869 						    (struct sockaddr_in6 *)&ssp->ssp_addr);
2870 					} else {
2871 						memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2872 					}
2873 #else
2874 					memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2875 #endif
2876 					break;
2877 #endif
2878 #ifdef INET6
2879 				case AF_INET6:
2880 					memcpy(&ssp->ssp_addr, &addr->sin6, sizeof(struct sockaddr_in6));
2881 					break;
2882 #endif
2883 				default:
2884 					break;
2885 				}
2886 				SCTP_TCB_UNLOCK(stcb);
2887 				*optsize = sizeof(struct sctp_setprim);
2888 			} else {
2889 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2890 				error = EINVAL;
2891 			}
2892 			break;
2893 		}
2894 	case SCTP_HMAC_IDENT:
2895 		{
2896 			struct sctp_hmacalgo *shmac;
2897 			sctp_hmaclist_t *hmaclist;
2898 			uint32_t size;
2899 			int i;
2900 
2901 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2902 
2903 			SCTP_INP_RLOCK(inp);
2904 			hmaclist = inp->sctp_ep.local_hmacs;
2905 			if (hmaclist == NULL) {
2906 				/* no HMACs to return */
2907 				*optsize = sizeof(*shmac);
2908 				SCTP_INP_RUNLOCK(inp);
2909 				break;
2910 			}
2911 			/* is there room for all of the hmac ids? */
2912 			size = sizeof(*shmac) + (hmaclist->num_algo *
2913 			    sizeof(shmac->shmac_idents[0]));
2914 			if ((size_t)(*optsize) < size) {
2915 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2916 				error = EINVAL;
2917 				SCTP_INP_RUNLOCK(inp);
2918 				break;
2919 			}
2920 			/* copy in the list */
2921 			shmac->shmac_number_of_idents = hmaclist->num_algo;
2922 			for (i = 0; i < hmaclist->num_algo; i++) {
2923 				shmac->shmac_idents[i] = hmaclist->hmac[i];
2924 			}
2925 			SCTP_INP_RUNLOCK(inp);
2926 			*optsize = size;
2927 			break;
2928 		}
2929 	case SCTP_AUTH_ACTIVE_KEY:
2930 		{
2931 			struct sctp_authkeyid *scact;
2932 
2933 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2934 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2935 
2936 			if (stcb) {
2937 				/* get the active key on the assoc */
2938 				scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2939 				SCTP_TCB_UNLOCK(stcb);
2940 			} else {
2941 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2942 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2943 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2944 					/* get the endpoint active key */
2945 					SCTP_INP_RLOCK(inp);
2946 					scact->scact_keynumber = inp->sctp_ep.default_keyid;
2947 					SCTP_INP_RUNLOCK(inp);
2948 				} else {
2949 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2950 					error = EINVAL;
2951 				}
2952 			}
2953 			if (error == 0) {
2954 				*optsize = sizeof(struct sctp_authkeyid);
2955 			}
2956 			break;
2957 		}
2958 	case SCTP_LOCAL_AUTH_CHUNKS:
2959 		{
2960 			struct sctp_authchunks *sac;
2961 			sctp_auth_chklist_t *chklist = NULL;
2962 			size_t size = 0;
2963 
2964 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2965 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2966 
2967 			if (stcb) {
2968 				/* get off the assoc */
2969 				chklist = stcb->asoc.local_auth_chunks;
2970 				/* is there enough space? */
2971 				size = sctp_auth_get_chklist_size(chklist);
2972 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2973 					error = EINVAL;
2974 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2975 				} else {
2976 					/* copy in the chunks */
2977 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2978 					sac->gauth_number_of_chunks = (uint32_t) size;
2979 					*optsize = sizeof(struct sctp_authchunks) + size;
2980 				}
2981 				SCTP_TCB_UNLOCK(stcb);
2982 			} else {
2983 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2984 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2985 				    (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
2986 					/* get off the endpoint */
2987 					SCTP_INP_RLOCK(inp);
2988 					chklist = inp->sctp_ep.local_auth_chunks;
2989 					/* is there enough space? */
2990 					size = sctp_auth_get_chklist_size(chklist);
2991 					if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2992 						error = EINVAL;
2993 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2994 					} else {
2995 						/* copy in the chunks */
2996 						(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2997 						sac->gauth_number_of_chunks = (uint32_t) size;
2998 						*optsize = sizeof(struct sctp_authchunks) + size;
2999 					}
3000 					SCTP_INP_RUNLOCK(inp);
3001 				} else {
3002 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3003 					error = EINVAL;
3004 				}
3005 			}
3006 			break;
3007 		}
3008 	case SCTP_PEER_AUTH_CHUNKS:
3009 		{
3010 			struct sctp_authchunks *sac;
3011 			sctp_auth_chklist_t *chklist = NULL;
3012 			size_t size = 0;
3013 
3014 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
3015 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
3016 
3017 			if (stcb) {
3018 				/* get off the assoc */
3019 				chklist = stcb->asoc.peer_auth_chunks;
3020 				/* is there enough space? */
3021 				size = sctp_auth_get_chklist_size(chklist);
3022 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3023 					error = EINVAL;
3024 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3025 				} else {
3026 					/* copy in the chunks */
3027 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3028 					sac->gauth_number_of_chunks = (uint32_t) size;
3029 					*optsize = sizeof(struct sctp_authchunks) + size;
3030 				}
3031 				SCTP_TCB_UNLOCK(stcb);
3032 			} else {
3033 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3034 				error = ENOENT;
3035 			}
3036 			break;
3037 		}
3038 	case SCTP_EVENT:
3039 		{
3040 			struct sctp_event *event;
3041 			uint32_t event_type;
3042 
3043 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
3044 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
3045 
3046 			switch (event->se_type) {
3047 			case SCTP_ASSOC_CHANGE:
3048 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
3049 				break;
3050 			case SCTP_PEER_ADDR_CHANGE:
3051 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
3052 				break;
3053 			case SCTP_REMOTE_ERROR:
3054 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
3055 				break;
3056 			case SCTP_SEND_FAILED:
3057 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3058 				break;
3059 			case SCTP_SHUTDOWN_EVENT:
3060 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3061 				break;
3062 			case SCTP_ADAPTATION_INDICATION:
3063 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
3064 				break;
3065 			case SCTP_PARTIAL_DELIVERY_EVENT:
3066 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
3067 				break;
3068 			case SCTP_AUTHENTICATION_EVENT:
3069 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
3070 				break;
3071 			case SCTP_STREAM_RESET_EVENT:
3072 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3073 				break;
3074 			case SCTP_SENDER_DRY_EVENT:
3075 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
3076 				break;
3077 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
3078 				event_type = 0;
3079 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
3080 				error = ENOTSUP;
3081 				break;
3082 			case SCTP_ASSOC_RESET_EVENT:
3083 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
3084 				break;
3085 			case SCTP_STREAM_CHANGE_EVENT:
3086 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
3087 				break;
3088 			case SCTP_SEND_FAILED_EVENT:
3089 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
3090 				break;
3091 			default:
3092 				event_type = 0;
3093 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3094 				error = EINVAL;
3095 				break;
3096 			}
3097 			if (event_type > 0) {
3098 				if (stcb) {
3099 					event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
3100 					SCTP_TCB_UNLOCK(stcb);
3101 				} else {
3102 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3103 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3104 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3105 						SCTP_INP_RLOCK(inp);
3106 						event->se_on = sctp_is_feature_on(inp, event_type);
3107 						SCTP_INP_RUNLOCK(inp);
3108 					} else {
3109 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3110 						error = EINVAL;
3111 					}
3112 				}
3113 			}
3114 			if (error == 0) {
3115 				*optsize = sizeof(struct sctp_event);
3116 			}
3117 			break;
3118 		}
3119 	case SCTP_RECVRCVINFO:
3120 		{
3121 			int onoff;
3122 
3123 			if (*optsize < sizeof(int)) {
3124 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3125 				error = EINVAL;
3126 			} else {
3127 				SCTP_INP_RLOCK(inp);
3128 				onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3129 				SCTP_INP_RUNLOCK(inp);
3130 			}
3131 			if (error == 0) {
3132 				/* return the option value */
3133 				*(int *)optval = onoff;
3134 				*optsize = sizeof(int);
3135 			}
3136 			break;
3137 		}
3138 	case SCTP_RECVNXTINFO:
3139 		{
3140 			int onoff;
3141 
3142 			if (*optsize < sizeof(int)) {
3143 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3144 				error = EINVAL;
3145 			} else {
3146 				SCTP_INP_RLOCK(inp);
3147 				onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3148 				SCTP_INP_RUNLOCK(inp);
3149 			}
3150 			if (error == 0) {
3151 				/* return the option value */
3152 				*(int *)optval = onoff;
3153 				*optsize = sizeof(int);
3154 			}
3155 			break;
3156 		}
3157 	case SCTP_DEFAULT_SNDINFO:
3158 		{
3159 			struct sctp_sndinfo *info;
3160 
3161 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3162 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3163 
3164 			if (stcb) {
3165 				info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3166 				info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3167 				info->snd_flags &= 0xfff0;
3168 				info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3169 				info->snd_context = stcb->asoc.def_send.sinfo_context;
3170 				SCTP_TCB_UNLOCK(stcb);
3171 			} else {
3172 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3173 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3174 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3175 					SCTP_INP_RLOCK(inp);
3176 					info->snd_sid = inp->def_send.sinfo_stream;
3177 					info->snd_flags = inp->def_send.sinfo_flags;
3178 					info->snd_flags &= 0xfff0;
3179 					info->snd_ppid = inp->def_send.sinfo_ppid;
3180 					info->snd_context = inp->def_send.sinfo_context;
3181 					SCTP_INP_RUNLOCK(inp);
3182 				} else {
3183 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3184 					error = EINVAL;
3185 				}
3186 			}
3187 			if (error == 0) {
3188 				*optsize = sizeof(struct sctp_sndinfo);
3189 			}
3190 			break;
3191 		}
3192 	case SCTP_DEFAULT_PRINFO:
3193 		{
3194 			struct sctp_default_prinfo *info;
3195 
3196 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3197 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3198 
3199 			if (stcb) {
3200 				info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3201 				info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3202 				SCTP_TCB_UNLOCK(stcb);
3203 			} else {
3204 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3205 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3206 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3207 					SCTP_INP_RLOCK(inp);
3208 					info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3209 					info->pr_value = inp->def_send.sinfo_timetolive;
3210 					SCTP_INP_RUNLOCK(inp);
3211 				} else {
3212 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3213 					error = EINVAL;
3214 				}
3215 			}
3216 			if (error == 0) {
3217 				*optsize = sizeof(struct sctp_default_prinfo);
3218 			}
3219 			break;
3220 		}
3221 	case SCTP_PEER_ADDR_THLDS:
3222 		{
3223 			struct sctp_paddrthlds *thlds;
3224 			struct sctp_nets *net;
3225 			struct sockaddr *addr;
3226 
3227 #if defined(INET) && defined(INET6)
3228 			struct sockaddr_in sin_store;
3229 
3230 #endif
3231 
3232 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3233 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3234 
3235 #if defined(INET) && defined(INET6)
3236 			if (thlds->spt_address.ss_family == AF_INET6) {
3237 				struct sockaddr_in6 *sin6;
3238 
3239 				sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
3240 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3241 					in6_sin6_2_sin(&sin_store, sin6);
3242 					addr = (struct sockaddr *)&sin_store;
3243 				} else {
3244 					addr = (struct sockaddr *)&thlds->spt_address;
3245 				}
3246 			} else {
3247 				addr = (struct sockaddr *)&thlds->spt_address;
3248 			}
3249 #else
3250 			addr = (struct sockaddr *)&thlds->spt_address;
3251 #endif
3252 			if (stcb != NULL) {
3253 				net = sctp_findnet(stcb, addr);
3254 			} else {
3255 				/*
3256 				 * We increment here since
3257 				 * sctp_findassociation_ep_addr() wil do a
3258 				 * decrement if it finds the stcb as long as
3259 				 * the locked tcb (last argument) is NOT a
3260 				 * TCB.. aka NULL.
3261 				 */
3262 				net = NULL;
3263 				SCTP_INP_INCR_REF(inp);
3264 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3265 				if (stcb == NULL) {
3266 					SCTP_INP_DECR_REF(inp);
3267 				}
3268 			}
3269 			if ((stcb != NULL) && (net == NULL)) {
3270 #ifdef INET
3271 				if (addr->sa_family == AF_INET) {
3272 					struct sockaddr_in *sin;
3273 
3274 					sin = (struct sockaddr_in *)addr;
3275 					if (sin->sin_addr.s_addr != INADDR_ANY) {
3276 						error = EINVAL;
3277 						SCTP_TCB_UNLOCK(stcb);
3278 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3279 						break;
3280 					}
3281 				} else
3282 #endif
3283 #ifdef INET6
3284 				if (addr->sa_family == AF_INET6) {
3285 					struct sockaddr_in6 *sin6;
3286 
3287 					sin6 = (struct sockaddr_in6 *)addr;
3288 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3289 						error = EINVAL;
3290 						SCTP_TCB_UNLOCK(stcb);
3291 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3292 						break;
3293 					}
3294 				} else
3295 #endif
3296 				{
3297 					error = EAFNOSUPPORT;
3298 					SCTP_TCB_UNLOCK(stcb);
3299 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3300 					break;
3301 				}
3302 			}
3303 			if (stcb != NULL) {
3304 				if (net != NULL) {
3305 					thlds->spt_pathmaxrxt = net->failure_threshold;
3306 					thlds->spt_pathpfthld = net->pf_threshold;
3307 				} else {
3308 					thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3309 					thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3310 				}
3311 				thlds->spt_assoc_id = sctp_get_associd(stcb);
3312 				SCTP_TCB_UNLOCK(stcb);
3313 			} else {
3314 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3315 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3316 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3317 					/* Use endpoint defaults */
3318 					SCTP_INP_RLOCK(inp);
3319 					thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3320 					thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3321 					SCTP_INP_RUNLOCK(inp);
3322 				} else {
3323 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3324 					error = EINVAL;
3325 				}
3326 			}
3327 			if (error == 0) {
3328 				*optsize = sizeof(struct sctp_paddrthlds);
3329 			}
3330 			break;
3331 		}
3332 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
3333 		{
3334 			struct sctp_udpencaps *encaps;
3335 			struct sctp_nets *net;
3336 			struct sockaddr *addr;
3337 
3338 #if defined(INET) && defined(INET6)
3339 			struct sockaddr_in sin_store;
3340 
3341 #endif
3342 
3343 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3344 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3345 
3346 #if defined(INET) && defined(INET6)
3347 			if (encaps->sue_address.ss_family == AF_INET6) {
3348 				struct sockaddr_in6 *sin6;
3349 
3350 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
3351 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3352 					in6_sin6_2_sin(&sin_store, sin6);
3353 					addr = (struct sockaddr *)&sin_store;
3354 				} else {
3355 					addr = (struct sockaddr *)&encaps->sue_address;
3356 				}
3357 			} else {
3358 				addr = (struct sockaddr *)&encaps->sue_address;
3359 			}
3360 #else
3361 			addr = (struct sockaddr *)&encaps->sue_address;
3362 #endif
3363 			if (stcb) {
3364 				net = sctp_findnet(stcb, addr);
3365 			} else {
3366 				/*
3367 				 * We increment here since
3368 				 * sctp_findassociation_ep_addr() wil do a
3369 				 * decrement if it finds the stcb as long as
3370 				 * the locked tcb (last argument) is NOT a
3371 				 * TCB.. aka NULL.
3372 				 */
3373 				net = NULL;
3374 				SCTP_INP_INCR_REF(inp);
3375 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3376 				if (stcb == NULL) {
3377 					SCTP_INP_DECR_REF(inp);
3378 				}
3379 			}
3380 			if ((stcb != NULL) && (net == NULL)) {
3381 #ifdef INET
3382 				if (addr->sa_family == AF_INET) {
3383 					struct sockaddr_in *sin;
3384 
3385 					sin = (struct sockaddr_in *)addr;
3386 					if (sin->sin_addr.s_addr != INADDR_ANY) {
3387 						error = EINVAL;
3388 						SCTP_TCB_UNLOCK(stcb);
3389 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3390 						break;
3391 					}
3392 				} else
3393 #endif
3394 #ifdef INET6
3395 				if (addr->sa_family == AF_INET6) {
3396 					struct sockaddr_in6 *sin6;
3397 
3398 					sin6 = (struct sockaddr_in6 *)addr;
3399 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3400 						error = EINVAL;
3401 						SCTP_TCB_UNLOCK(stcb);
3402 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3403 						break;
3404 					}
3405 				} else
3406 #endif
3407 				{
3408 					error = EAFNOSUPPORT;
3409 					SCTP_TCB_UNLOCK(stcb);
3410 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3411 					break;
3412 				}
3413 			}
3414 			if (stcb != NULL) {
3415 				if (net) {
3416 					encaps->sue_port = net->port;
3417 				} else {
3418 					encaps->sue_port = stcb->asoc.port;
3419 				}
3420 				SCTP_TCB_UNLOCK(stcb);
3421 			} else {
3422 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3423 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3424 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3425 					SCTP_INP_RLOCK(inp);
3426 					encaps->sue_port = inp->sctp_ep.port;
3427 					SCTP_INP_RUNLOCK(inp);
3428 				} else {
3429 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3430 					error = EINVAL;
3431 				}
3432 			}
3433 			if (error == 0) {
3434 				*optsize = sizeof(struct sctp_udpencaps);
3435 			}
3436 			break;
3437 		}
3438 	case SCTP_ECN_SUPPORTED:
3439 		{
3440 			struct sctp_assoc_value *av;
3441 
3442 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3443 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3444 
3445 			if (stcb) {
3446 				av->assoc_value = stcb->asoc.ecn_supported;
3447 				SCTP_TCB_UNLOCK(stcb);
3448 			} else {
3449 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3450 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3451 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3452 					SCTP_INP_RLOCK(inp);
3453 					av->assoc_value = inp->ecn_supported;
3454 					SCTP_INP_RUNLOCK(inp);
3455 				} else {
3456 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3457 					error = EINVAL;
3458 				}
3459 			}
3460 			if (error == 0) {
3461 				*optsize = sizeof(struct sctp_assoc_value);
3462 			}
3463 			break;
3464 		}
3465 	case SCTP_PR_SUPPORTED:
3466 		{
3467 			struct sctp_assoc_value *av;
3468 
3469 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3470 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3471 
3472 			if (stcb) {
3473 				av->assoc_value = stcb->asoc.prsctp_supported;
3474 				SCTP_TCB_UNLOCK(stcb);
3475 			} else {
3476 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3477 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3478 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3479 					SCTP_INP_RLOCK(inp);
3480 					av->assoc_value = inp->prsctp_supported;
3481 					SCTP_INP_RUNLOCK(inp);
3482 				} else {
3483 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3484 					error = EINVAL;
3485 				}
3486 			}
3487 			if (error == 0) {
3488 				*optsize = sizeof(struct sctp_assoc_value);
3489 			}
3490 			break;
3491 		}
3492 	case SCTP_AUTH_SUPPORTED:
3493 		{
3494 			struct sctp_assoc_value *av;
3495 
3496 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3497 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3498 
3499 			if (stcb) {
3500 				av->assoc_value = stcb->asoc.auth_supported;
3501 				SCTP_TCB_UNLOCK(stcb);
3502 			} else {
3503 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3504 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3505 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3506 					SCTP_INP_RLOCK(inp);
3507 					av->assoc_value = inp->auth_supported;
3508 					SCTP_INP_RUNLOCK(inp);
3509 				} else {
3510 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3511 					error = EINVAL;
3512 				}
3513 			}
3514 			if (error == 0) {
3515 				*optsize = sizeof(struct sctp_assoc_value);
3516 			}
3517 			break;
3518 		}
3519 	case SCTP_ASCONF_SUPPORTED:
3520 		{
3521 			struct sctp_assoc_value *av;
3522 
3523 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3524 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3525 
3526 			if (stcb) {
3527 				av->assoc_value = stcb->asoc.asconf_supported;
3528 				SCTP_TCB_UNLOCK(stcb);
3529 			} else {
3530 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3531 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3532 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3533 					SCTP_INP_RLOCK(inp);
3534 					av->assoc_value = inp->asconf_supported;
3535 					SCTP_INP_RUNLOCK(inp);
3536 				} else {
3537 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3538 					error = EINVAL;
3539 				}
3540 			}
3541 			if (error == 0) {
3542 				*optsize = sizeof(struct sctp_assoc_value);
3543 			}
3544 			break;
3545 		}
3546 	case SCTP_RECONFIG_SUPPORTED:
3547 		{
3548 			struct sctp_assoc_value *av;
3549 
3550 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3551 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3552 
3553 			if (stcb) {
3554 				av->assoc_value = stcb->asoc.reconfig_supported;
3555 				SCTP_TCB_UNLOCK(stcb);
3556 			} else {
3557 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3558 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3559 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3560 					SCTP_INP_RLOCK(inp);
3561 					av->assoc_value = inp->reconfig_supported;
3562 					SCTP_INP_RUNLOCK(inp);
3563 				} else {
3564 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3565 					error = EINVAL;
3566 				}
3567 			}
3568 			if (error == 0) {
3569 				*optsize = sizeof(struct sctp_assoc_value);
3570 			}
3571 			break;
3572 		}
3573 	case SCTP_NRSACK_SUPPORTED:
3574 		{
3575 			struct sctp_assoc_value *av;
3576 
3577 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3578 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3579 
3580 			if (stcb) {
3581 				av->assoc_value = stcb->asoc.nrsack_supported;
3582 				SCTP_TCB_UNLOCK(stcb);
3583 			} else {
3584 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3585 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3586 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3587 					SCTP_INP_RLOCK(inp);
3588 					av->assoc_value = inp->nrsack_supported;
3589 					SCTP_INP_RUNLOCK(inp);
3590 				} else {
3591 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3592 					error = EINVAL;
3593 				}
3594 			}
3595 			if (error == 0) {
3596 				*optsize = sizeof(struct sctp_assoc_value);
3597 			}
3598 			break;
3599 		}
3600 	case SCTP_PKTDROP_SUPPORTED:
3601 		{
3602 			struct sctp_assoc_value *av;
3603 
3604 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3605 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3606 
3607 			if (stcb) {
3608 				av->assoc_value = stcb->asoc.pktdrop_supported;
3609 				SCTP_TCB_UNLOCK(stcb);
3610 			} else {
3611 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3612 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3613 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3614 					SCTP_INP_RLOCK(inp);
3615 					av->assoc_value = inp->pktdrop_supported;
3616 					SCTP_INP_RUNLOCK(inp);
3617 				} else {
3618 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3619 					error = EINVAL;
3620 				}
3621 			}
3622 			if (error == 0) {
3623 				*optsize = sizeof(struct sctp_assoc_value);
3624 			}
3625 			break;
3626 		}
3627 	case SCTP_ENABLE_STREAM_RESET:
3628 		{
3629 			struct sctp_assoc_value *av;
3630 
3631 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3632 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3633 
3634 			if (stcb) {
3635 				av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support;
3636 				SCTP_TCB_UNLOCK(stcb);
3637 			} else {
3638 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3639 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3640 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3641 					SCTP_INP_RLOCK(inp);
3642 					av->assoc_value = (uint32_t) inp->local_strreset_support;
3643 					SCTP_INP_RUNLOCK(inp);
3644 				} else {
3645 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3646 					error = EINVAL;
3647 				}
3648 			}
3649 			if (error == 0) {
3650 				*optsize = sizeof(struct sctp_assoc_value);
3651 			}
3652 			break;
3653 		}
3654 	case SCTP_PR_STREAM_STATUS:
3655 		{
3656 			struct sctp_prstatus *sprstat;
3657 			uint16_t sid;
3658 			uint16_t policy;
3659 
3660 			SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3661 			SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3662 
3663 			sid = sprstat->sprstat_sid;
3664 			policy = sprstat->sprstat_policy;
3665 #if defined(SCTP_DETAILED_STR_STATS)
3666 			if ((stcb != NULL) &&
3667 			    (sid < stcb->asoc.streamoutcnt) &&
3668 			    (policy != SCTP_PR_SCTP_NONE) &&
3669 			    ((policy <= SCTP_PR_SCTP_MAX) ||
3670 			    (policy == SCTP_PR_SCTP_ALL))) {
3671 				if (policy == SCTP_PR_SCTP_ALL) {
3672 					sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3673 					sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3674 				} else {
3675 					sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
3676 					sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
3677 				}
3678 #else
3679 			if ((stcb != NULL) &&
3680 			    (sid < stcb->asoc.streamoutcnt) &&
3681 			    (policy == SCTP_PR_SCTP_ALL)) {
3682 				sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3683 				sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3684 #endif
3685 				SCTP_TCB_UNLOCK(stcb);
3686 				*optsize = sizeof(struct sctp_prstatus);
3687 			} else {
3688 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3689 				error = EINVAL;
3690 			}
3691 			break;
3692 		}
3693 	case SCTP_PR_ASSOC_STATUS:
3694 		{
3695 			struct sctp_prstatus *sprstat;
3696 			uint16_t policy;
3697 
3698 			SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3699 			SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3700 
3701 			policy = sprstat->sprstat_policy;
3702 			if ((stcb != NULL) &&
3703 			    (policy != SCTP_PR_SCTP_NONE) &&
3704 			    ((policy <= SCTP_PR_SCTP_MAX) ||
3705 			    (policy == SCTP_PR_SCTP_ALL))) {
3706 				if (policy == SCTP_PR_SCTP_ALL) {
3707 					sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
3708 					sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
3709 				} else {
3710 					sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
3711 					sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
3712 				}
3713 				SCTP_TCB_UNLOCK(stcb);
3714 				*optsize = sizeof(struct sctp_prstatus);
3715 			} else {
3716 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3717 				error = EINVAL;
3718 			}
3719 			break;
3720 		}
3721 	case SCTP_MAX_CWND:
3722 		{
3723 			struct sctp_assoc_value *av;
3724 
3725 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3726 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3727 
3728 			if (stcb) {
3729 				av->assoc_value = stcb->asoc.max_cwnd;
3730 				SCTP_TCB_UNLOCK(stcb);
3731 			} else {
3732 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3733 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3734 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3735 					SCTP_INP_RLOCK(inp);
3736 					av->assoc_value = inp->max_cwnd;
3737 					SCTP_INP_RUNLOCK(inp);
3738 				} else {
3739 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3740 					error = EINVAL;
3741 				}
3742 			}
3743 			if (error == 0) {
3744 				*optsize = sizeof(struct sctp_assoc_value);
3745 			}
3746 			break;
3747 		}
3748 	default:
3749 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3750 		error = ENOPROTOOPT;
3751 		break;
3752 	}			/* end switch (sopt->sopt_name) */
3753 	if (error) {
3754 		*optsize = 0;
3755 	}
3756 	return (error);
3757 }
3758 
3759 static int
3760 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3761     void *p)
3762 {
3763 	int error, set_opt;
3764 	uint32_t *mopt;
3765 	struct sctp_tcb *stcb = NULL;
3766 	struct sctp_inpcb *inp = NULL;
3767 	uint32_t vrf_id;
3768 
3769 	if (optval == NULL) {
3770 		SCTP_PRINTF("optval is NULL\n");
3771 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3772 		return (EINVAL);
3773 	}
3774 	inp = (struct sctp_inpcb *)so->so_pcb;
3775 	if (inp == NULL) {
3776 		SCTP_PRINTF("inp is NULL?\n");
3777 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3778 		return (EINVAL);
3779 	}
3780 	vrf_id = inp->def_vrf_id;
3781 
3782 	error = 0;
3783 	switch (optname) {
3784 	case SCTP_NODELAY:
3785 	case SCTP_AUTOCLOSE:
3786 	case SCTP_AUTO_ASCONF:
3787 	case SCTP_EXPLICIT_EOR:
3788 	case SCTP_DISABLE_FRAGMENTS:
3789 	case SCTP_USE_EXT_RCVINFO:
3790 	case SCTP_I_WANT_MAPPED_V4_ADDR:
3791 		/* copy in the option value */
3792 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3793 		set_opt = 0;
3794 		if (error)
3795 			break;
3796 		switch (optname) {
3797 		case SCTP_DISABLE_FRAGMENTS:
3798 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3799 			break;
3800 		case SCTP_AUTO_ASCONF:
3801 			/*
3802 			 * NOTE: we don't really support this flag
3803 			 */
3804 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3805 				/* only valid for bound all sockets */
3806 				if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3807 				    (*mopt != 0)) {
3808 					/* forbidden by admin */
3809 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3810 					return (EPERM);
3811 				}
3812 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3813 			} else {
3814 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3815 				return (EINVAL);
3816 			}
3817 			break;
3818 		case SCTP_EXPLICIT_EOR:
3819 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3820 			break;
3821 		case SCTP_USE_EXT_RCVINFO:
3822 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3823 			break;
3824 		case SCTP_I_WANT_MAPPED_V4_ADDR:
3825 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3826 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3827 			} else {
3828 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3829 				return (EINVAL);
3830 			}
3831 			break;
3832 		case SCTP_NODELAY:
3833 			set_opt = SCTP_PCB_FLAGS_NODELAY;
3834 			break;
3835 		case SCTP_AUTOCLOSE:
3836 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3837 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3838 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3839 				return (EINVAL);
3840 			}
3841 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3842 			/*
3843 			 * The value is in ticks. Note this does not effect
3844 			 * old associations, only new ones.
3845 			 */
3846 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3847 			break;
3848 		}
3849 		SCTP_INP_WLOCK(inp);
3850 		if (*mopt != 0) {
3851 			sctp_feature_on(inp, set_opt);
3852 		} else {
3853 			sctp_feature_off(inp, set_opt);
3854 		}
3855 		SCTP_INP_WUNLOCK(inp);
3856 		break;
3857 	case SCTP_REUSE_PORT:
3858 		{
3859 			SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3860 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3861 				/* Can't set it after we are bound */
3862 				error = EINVAL;
3863 				break;
3864 			}
3865 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3866 				/* Can't do this for a 1-m socket */
3867 				error = EINVAL;
3868 				break;
3869 			}
3870 			if (optval)
3871 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3872 			else
3873 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3874 			break;
3875 		}
3876 	case SCTP_PARTIAL_DELIVERY_POINT:
3877 		{
3878 			uint32_t *value;
3879 
3880 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3881 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
3882 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3883 				error = EINVAL;
3884 				break;
3885 			}
3886 			inp->partial_delivery_point = *value;
3887 			break;
3888 		}
3889 	case SCTP_FRAGMENT_INTERLEAVE:
3890 		/* not yet until we re-write sctp_recvmsg() */
3891 		{
3892 			uint32_t *level;
3893 
3894 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3895 			if (*level == SCTP_FRAG_LEVEL_2) {
3896 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3897 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3898 			} else if (*level == SCTP_FRAG_LEVEL_1) {
3899 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3900 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3901 			} else if (*level == SCTP_FRAG_LEVEL_0) {
3902 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3903 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3904 
3905 			} else {
3906 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3907 				error = EINVAL;
3908 			}
3909 			break;
3910 		}
3911 	case SCTP_INTERLEAVING_SUPPORTED:
3912 		{
3913 			struct sctp_assoc_value *av;
3914 
3915 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3916 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3917 
3918 			if (stcb) {
3919 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3920 				error = EINVAL;
3921 				SCTP_TCB_UNLOCK(stcb);
3922 			} else {
3923 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3924 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3925 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3926 					SCTP_INP_WLOCK(inp);
3927 					if (av->assoc_value == 0) {
3928 						inp->idata_supported = 0;
3929 					} else {
3930 						if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
3931 						    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS))) {
3932 							inp->idata_supported = 1;
3933 						} else {
3934 							/*
3935 							 * Must have Frag
3936 							 * interleave and
3937 							 * stream interleave
3938 							 * on
3939 							 */
3940 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3941 							error = EINVAL;
3942 						}
3943 					}
3944 					SCTP_INP_WUNLOCK(inp);
3945 				} else {
3946 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3947 					error = EINVAL;
3948 				}
3949 			}
3950 			break;
3951 		}
3952 	case SCTP_CMT_ON_OFF:
3953 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3954 			struct sctp_assoc_value *av;
3955 
3956 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3957 			if (av->assoc_value > SCTP_CMT_MAX) {
3958 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3959 				error = EINVAL;
3960 				break;
3961 			}
3962 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3963 			if (stcb) {
3964 				stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3965 				SCTP_TCB_UNLOCK(stcb);
3966 			} else {
3967 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3968 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3969 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3970 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3971 					SCTP_INP_WLOCK(inp);
3972 					inp->sctp_cmt_on_off = av->assoc_value;
3973 					SCTP_INP_WUNLOCK(inp);
3974 				}
3975 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3976 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3977 					SCTP_INP_RLOCK(inp);
3978 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3979 						SCTP_TCB_LOCK(stcb);
3980 						stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3981 						SCTP_TCB_UNLOCK(stcb);
3982 					}
3983 					SCTP_INP_RUNLOCK(inp);
3984 				}
3985 			}
3986 		} else {
3987 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3988 			error = ENOPROTOOPT;
3989 		}
3990 		break;
3991 	case SCTP_PLUGGABLE_CC:
3992 		{
3993 			struct sctp_assoc_value *av;
3994 			struct sctp_nets *net;
3995 
3996 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3997 			if ((av->assoc_value != SCTP_CC_RFC2581) &&
3998 			    (av->assoc_value != SCTP_CC_HSTCP) &&
3999 			    (av->assoc_value != SCTP_CC_HTCP) &&
4000 			    (av->assoc_value != SCTP_CC_RTCC)) {
4001 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4002 				error = EINVAL;
4003 				break;
4004 			}
4005 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4006 			if (stcb) {
4007 				stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4008 				stcb->asoc.congestion_control_module = av->assoc_value;
4009 				if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4010 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4011 						stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4012 					}
4013 				}
4014 				SCTP_TCB_UNLOCK(stcb);
4015 			} else {
4016 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4017 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4018 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4019 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4020 					SCTP_INP_WLOCK(inp);
4021 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
4022 					SCTP_INP_WUNLOCK(inp);
4023 				}
4024 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4025 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4026 					SCTP_INP_RLOCK(inp);
4027 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4028 						SCTP_TCB_LOCK(stcb);
4029 						stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4030 						stcb->asoc.congestion_control_module = av->assoc_value;
4031 						if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4032 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4033 								stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4034 							}
4035 						}
4036 						SCTP_TCB_UNLOCK(stcb);
4037 					}
4038 					SCTP_INP_RUNLOCK(inp);
4039 				}
4040 			}
4041 			break;
4042 		}
4043 	case SCTP_CC_OPTION:
4044 		{
4045 			struct sctp_cc_option *cc_opt;
4046 
4047 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
4048 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
4049 			if (stcb == NULL) {
4050 				if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
4051 					SCTP_INP_RLOCK(inp);
4052 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4053 						SCTP_TCB_LOCK(stcb);
4054 						if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
4055 							(*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
4056 						}
4057 						SCTP_TCB_UNLOCK(stcb);
4058 					}
4059 					SCTP_INP_RUNLOCK(inp);
4060 				} else {
4061 					error = EINVAL;
4062 				}
4063 			} else {
4064 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4065 					error = ENOTSUP;
4066 				} else {
4067 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4068 					    cc_opt);
4069 				}
4070 				SCTP_TCB_UNLOCK(stcb);
4071 			}
4072 			break;
4073 		}
4074 	case SCTP_PLUGGABLE_SS:
4075 		{
4076 			struct sctp_assoc_value *av;
4077 
4078 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4079 			if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4080 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4081 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4082 			    (av->assoc_value != SCTP_SS_PRIORITY) &&
4083 			    (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4084 			    (av->assoc_value != SCTP_SS_FIRST_COME)) {
4085 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4086 				error = EINVAL;
4087 				break;
4088 			}
4089 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4090 			if (stcb) {
4091 				stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4092 				stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4093 				stcb->asoc.stream_scheduling_module = av->assoc_value;
4094 				stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4095 				SCTP_TCB_UNLOCK(stcb);
4096 			} else {
4097 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4098 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4099 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4100 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4101 					SCTP_INP_WLOCK(inp);
4102 					inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4103 					SCTP_INP_WUNLOCK(inp);
4104 				}
4105 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4106 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4107 					SCTP_INP_RLOCK(inp);
4108 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4109 						SCTP_TCB_LOCK(stcb);
4110 						stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4111 						stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4112 						stcb->asoc.stream_scheduling_module = av->assoc_value;
4113 						stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4114 						SCTP_TCB_UNLOCK(stcb);
4115 					}
4116 					SCTP_INP_RUNLOCK(inp);
4117 				}
4118 			}
4119 			break;
4120 		}
4121 	case SCTP_SS_VALUE:
4122 		{
4123 			struct sctp_stream_value *av;
4124 
4125 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4126 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4127 			if (stcb) {
4128 				if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4129 				    (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4130 				    av->stream_value) < 0)) {
4131 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4132 					error = EINVAL;
4133 				}
4134 				SCTP_TCB_UNLOCK(stcb);
4135 			} else {
4136 				if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4137 					SCTP_INP_RLOCK(inp);
4138 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4139 						SCTP_TCB_LOCK(stcb);
4140 						if (av->stream_id < stcb->asoc.streamoutcnt) {
4141 							stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4142 							    &stcb->asoc,
4143 							    &stcb->asoc.strmout[av->stream_id],
4144 							    av->stream_value);
4145 						}
4146 						SCTP_TCB_UNLOCK(stcb);
4147 					}
4148 					SCTP_INP_RUNLOCK(inp);
4149 				} else {
4150 					/*
4151 					 * Can't set stream value without
4152 					 * association
4153 					 */
4154 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4155 					error = EINVAL;
4156 				}
4157 			}
4158 			break;
4159 		}
4160 	case SCTP_CLR_STAT_LOG:
4161 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4162 		error = EOPNOTSUPP;
4163 		break;
4164 	case SCTP_CONTEXT:
4165 		{
4166 			struct sctp_assoc_value *av;
4167 
4168 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4169 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4170 
4171 			if (stcb) {
4172 				stcb->asoc.context = av->assoc_value;
4173 				SCTP_TCB_UNLOCK(stcb);
4174 			} else {
4175 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4176 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4177 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4178 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4179 					SCTP_INP_WLOCK(inp);
4180 					inp->sctp_context = av->assoc_value;
4181 					SCTP_INP_WUNLOCK(inp);
4182 				}
4183 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4184 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4185 					SCTP_INP_RLOCK(inp);
4186 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4187 						SCTP_TCB_LOCK(stcb);
4188 						stcb->asoc.context = av->assoc_value;
4189 						SCTP_TCB_UNLOCK(stcb);
4190 					}
4191 					SCTP_INP_RUNLOCK(inp);
4192 				}
4193 			}
4194 			break;
4195 		}
4196 	case SCTP_VRF_ID:
4197 		{
4198 			uint32_t *default_vrfid;
4199 
4200 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4201 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
4202 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4203 				error = EINVAL;
4204 				break;
4205 			}
4206 			inp->def_vrf_id = *default_vrfid;
4207 			break;
4208 		}
4209 	case SCTP_DEL_VRF_ID:
4210 		{
4211 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4212 			error = EOPNOTSUPP;
4213 			break;
4214 		}
4215 	case SCTP_ADD_VRF_ID:
4216 		{
4217 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4218 			error = EOPNOTSUPP;
4219 			break;
4220 		}
4221 	case SCTP_DELAYED_SACK:
4222 		{
4223 			struct sctp_sack_info *sack;
4224 
4225 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4226 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4227 			if (sack->sack_delay) {
4228 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4229 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
4230 				if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4231 					sack->sack_delay = TICKS_TO_MSEC(1);
4232 				}
4233 			}
4234 			if (stcb) {
4235 				if (sack->sack_delay) {
4236 					stcb->asoc.delayed_ack = sack->sack_delay;
4237 				}
4238 				if (sack->sack_freq) {
4239 					stcb->asoc.sack_freq = sack->sack_freq;
4240 				}
4241 				SCTP_TCB_UNLOCK(stcb);
4242 			} else {
4243 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4244 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4245 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4246 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4247 					SCTP_INP_WLOCK(inp);
4248 					if (sack->sack_delay) {
4249 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4250 					}
4251 					if (sack->sack_freq) {
4252 						inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4253 					}
4254 					SCTP_INP_WUNLOCK(inp);
4255 				}
4256 				if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4257 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4258 					SCTP_INP_RLOCK(inp);
4259 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4260 						SCTP_TCB_LOCK(stcb);
4261 						if (sack->sack_delay) {
4262 							stcb->asoc.delayed_ack = sack->sack_delay;
4263 						}
4264 						if (sack->sack_freq) {
4265 							stcb->asoc.sack_freq = sack->sack_freq;
4266 						}
4267 						SCTP_TCB_UNLOCK(stcb);
4268 					}
4269 					SCTP_INP_RUNLOCK(inp);
4270 				}
4271 			}
4272 			break;
4273 		}
4274 	case SCTP_AUTH_CHUNK:
4275 		{
4276 			struct sctp_authchunk *sauth;
4277 
4278 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4279 
4280 			SCTP_INP_WLOCK(inp);
4281 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4282 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4283 				error = EINVAL;
4284 			}
4285 			SCTP_INP_WUNLOCK(inp);
4286 			break;
4287 		}
4288 	case SCTP_AUTH_KEY:
4289 		{
4290 			struct sctp_authkey *sca;
4291 			struct sctp_keyhead *shared_keys;
4292 			sctp_sharedkey_t *shared_key;
4293 			sctp_key_t *key = NULL;
4294 			size_t size;
4295 
4296 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4297 			if (sca->sca_keylength == 0) {
4298 				size = optsize - sizeof(struct sctp_authkey);
4299 			} else {
4300 				if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4301 					size = sca->sca_keylength;
4302 				} else {
4303 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4304 					error = EINVAL;
4305 					break;
4306 				}
4307 			}
4308 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4309 
4310 			if (stcb) {
4311 				shared_keys = &stcb->asoc.shared_keys;
4312 				/* clear the cached keys for this key id */
4313 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4314 				/*
4315 				 * create the new shared key and
4316 				 * insert/replace it
4317 				 */
4318 				if (size > 0) {
4319 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
4320 					if (key == NULL) {
4321 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4322 						error = ENOMEM;
4323 						SCTP_TCB_UNLOCK(stcb);
4324 						break;
4325 					}
4326 				}
4327 				shared_key = sctp_alloc_sharedkey();
4328 				if (shared_key == NULL) {
4329 					sctp_free_key(key);
4330 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4331 					error = ENOMEM;
4332 					SCTP_TCB_UNLOCK(stcb);
4333 					break;
4334 				}
4335 				shared_key->key = key;
4336 				shared_key->keyid = sca->sca_keynumber;
4337 				error = sctp_insert_sharedkey(shared_keys, shared_key);
4338 				SCTP_TCB_UNLOCK(stcb);
4339 			} else {
4340 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4341 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4342 				    (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4343 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4344 					SCTP_INP_WLOCK(inp);
4345 					shared_keys = &inp->sctp_ep.shared_keys;
4346 					/*
4347 					 * clear the cached keys on all
4348 					 * assocs for this key id
4349 					 */
4350 					sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4351 					/*
4352 					 * create the new shared key and
4353 					 * insert/replace it
4354 					 */
4355 					if (size > 0) {
4356 						key = sctp_set_key(sca->sca_key, (uint32_t) size);
4357 						if (key == NULL) {
4358 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4359 							error = ENOMEM;
4360 							SCTP_INP_WUNLOCK(inp);
4361 							break;
4362 						}
4363 					}
4364 					shared_key = sctp_alloc_sharedkey();
4365 					if (shared_key == NULL) {
4366 						sctp_free_key(key);
4367 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4368 						error = ENOMEM;
4369 						SCTP_INP_WUNLOCK(inp);
4370 						break;
4371 					}
4372 					shared_key->key = key;
4373 					shared_key->keyid = sca->sca_keynumber;
4374 					error = sctp_insert_sharedkey(shared_keys, shared_key);
4375 					SCTP_INP_WUNLOCK(inp);
4376 				}
4377 				if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4378 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4379 					SCTP_INP_RLOCK(inp);
4380 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4381 						SCTP_TCB_LOCK(stcb);
4382 						shared_keys = &stcb->asoc.shared_keys;
4383 						/*
4384 						 * clear the cached keys for
4385 						 * this key id
4386 						 */
4387 						sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4388 						/*
4389 						 * create the new shared key
4390 						 * and insert/replace it
4391 						 */
4392 						if (size > 0) {
4393 							key = sctp_set_key(sca->sca_key, (uint32_t) size);
4394 							if (key == NULL) {
4395 								SCTP_TCB_UNLOCK(stcb);
4396 								continue;
4397 							}
4398 						}
4399 						shared_key = sctp_alloc_sharedkey();
4400 						if (shared_key == NULL) {
4401 							sctp_free_key(key);
4402 							SCTP_TCB_UNLOCK(stcb);
4403 							continue;
4404 						}
4405 						shared_key->key = key;
4406 						shared_key->keyid = sca->sca_keynumber;
4407 						error = sctp_insert_sharedkey(shared_keys, shared_key);
4408 						SCTP_TCB_UNLOCK(stcb);
4409 					}
4410 					SCTP_INP_RUNLOCK(inp);
4411 				}
4412 			}
4413 			break;
4414 		}
4415 	case SCTP_HMAC_IDENT:
4416 		{
4417 			struct sctp_hmacalgo *shmac;
4418 			sctp_hmaclist_t *hmaclist;
4419 			uint16_t hmacid;
4420 			uint32_t i;
4421 
4422 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4423 			if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4424 			    (shmac->shmac_number_of_idents > 0xffff)) {
4425 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4426 				error = EINVAL;
4427 				break;
4428 			}
4429 			hmaclist = sctp_alloc_hmaclist((uint16_t) shmac->shmac_number_of_idents);
4430 			if (hmaclist == NULL) {
4431 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4432 				error = ENOMEM;
4433 				break;
4434 			}
4435 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4436 				hmacid = shmac->shmac_idents[i];
4437 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4438 					 /* invalid HMACs were found */ ;
4439 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4440 					error = EINVAL;
4441 					sctp_free_hmaclist(hmaclist);
4442 					goto sctp_set_hmac_done;
4443 				}
4444 			}
4445 			for (i = 0; i < hmaclist->num_algo; i++) {
4446 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4447 					/* already in list */
4448 					break;
4449 				}
4450 			}
4451 			if (i == hmaclist->num_algo) {
4452 				/* not found in list */
4453 				sctp_free_hmaclist(hmaclist);
4454 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4455 				error = EINVAL;
4456 				break;
4457 			}
4458 			/* set it on the endpoint */
4459 			SCTP_INP_WLOCK(inp);
4460 			if (inp->sctp_ep.local_hmacs)
4461 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4462 			inp->sctp_ep.local_hmacs = hmaclist;
4463 			SCTP_INP_WUNLOCK(inp);
4464 	sctp_set_hmac_done:
4465 			break;
4466 		}
4467 	case SCTP_AUTH_ACTIVE_KEY:
4468 		{
4469 			struct sctp_authkeyid *scact;
4470 
4471 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4472 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4473 
4474 			/* set the active key on the right place */
4475 			if (stcb) {
4476 				/* set the active key on the assoc */
4477 				if (sctp_auth_setactivekey(stcb,
4478 				    scact->scact_keynumber)) {
4479 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4480 					    SCTP_FROM_SCTP_USRREQ,
4481 					    EINVAL);
4482 					error = EINVAL;
4483 				}
4484 				SCTP_TCB_UNLOCK(stcb);
4485 			} else {
4486 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4487 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4488 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4489 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4490 					SCTP_INP_WLOCK(inp);
4491 					if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4492 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4493 						error = EINVAL;
4494 					}
4495 					SCTP_INP_WUNLOCK(inp);
4496 				}
4497 				if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4498 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4499 					SCTP_INP_RLOCK(inp);
4500 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4501 						SCTP_TCB_LOCK(stcb);
4502 						sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4503 						SCTP_TCB_UNLOCK(stcb);
4504 					}
4505 					SCTP_INP_RUNLOCK(inp);
4506 				}
4507 			}
4508 			break;
4509 		}
4510 	case SCTP_AUTH_DELETE_KEY:
4511 		{
4512 			struct sctp_authkeyid *scdel;
4513 
4514 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4515 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4516 
4517 			/* delete the key from the right place */
4518 			if (stcb) {
4519 				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4520 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4521 					error = EINVAL;
4522 				}
4523 				SCTP_TCB_UNLOCK(stcb);
4524 			} else {
4525 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4526 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4527 				    (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4528 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4529 					SCTP_INP_WLOCK(inp);
4530 					if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4531 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4532 						error = EINVAL;
4533 					}
4534 					SCTP_INP_WUNLOCK(inp);
4535 				}
4536 				if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4537 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4538 					SCTP_INP_RLOCK(inp);
4539 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4540 						SCTP_TCB_LOCK(stcb);
4541 						sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4542 						SCTP_TCB_UNLOCK(stcb);
4543 					}
4544 					SCTP_INP_RUNLOCK(inp);
4545 				}
4546 			}
4547 			break;
4548 		}
4549 	case SCTP_AUTH_DEACTIVATE_KEY:
4550 		{
4551 			struct sctp_authkeyid *keyid;
4552 
4553 			SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4554 			SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4555 
4556 			/* deactivate the key from the right place */
4557 			if (stcb) {
4558 				if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4559 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4560 					error = EINVAL;
4561 				}
4562 				SCTP_TCB_UNLOCK(stcb);
4563 			} else {
4564 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4565 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4566 				    (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4567 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4568 					SCTP_INP_WLOCK(inp);
4569 					if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4570 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4571 						error = EINVAL;
4572 					}
4573 					SCTP_INP_WUNLOCK(inp);
4574 				}
4575 				if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4576 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4577 					SCTP_INP_RLOCK(inp);
4578 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4579 						SCTP_TCB_LOCK(stcb);
4580 						sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4581 						SCTP_TCB_UNLOCK(stcb);
4582 					}
4583 					SCTP_INP_RUNLOCK(inp);
4584 				}
4585 			}
4586 			break;
4587 		}
4588 	case SCTP_ENABLE_STREAM_RESET:
4589 		{
4590 			struct sctp_assoc_value *av;
4591 
4592 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4593 			if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4594 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4595 				error = EINVAL;
4596 				break;
4597 			}
4598 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4599 			if (stcb) {
4600 				stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4601 				SCTP_TCB_UNLOCK(stcb);
4602 			} else {
4603 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4604 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4605 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4606 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4607 					SCTP_INP_WLOCK(inp);
4608 					inp->local_strreset_support = (uint8_t) av->assoc_value;
4609 					SCTP_INP_WUNLOCK(inp);
4610 				}
4611 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4612 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4613 					SCTP_INP_RLOCK(inp);
4614 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4615 						SCTP_TCB_LOCK(stcb);
4616 						stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4617 						SCTP_TCB_UNLOCK(stcb);
4618 					}
4619 					SCTP_INP_RUNLOCK(inp);
4620 				}
4621 			}
4622 			break;
4623 		}
4624 	case SCTP_RESET_STREAMS:
4625 		{
4626 			struct sctp_reset_streams *strrst;
4627 			int i, send_out = 0;
4628 			int send_in = 0;
4629 
4630 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4631 			SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4632 			if (stcb == NULL) {
4633 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4634 				error = ENOENT;
4635 				break;
4636 			}
4637 			if (stcb->asoc.reconfig_supported == 0) {
4638 				/*
4639 				 * Peer does not support the chunk type.
4640 				 */
4641 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4642 				error = EOPNOTSUPP;
4643 				SCTP_TCB_UNLOCK(stcb);
4644 				break;
4645 			}
4646 			if (sizeof(struct sctp_reset_streams) +
4647 			    strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4648 				error = EINVAL;
4649 				SCTP_TCB_UNLOCK(stcb);
4650 				break;
4651 			}
4652 			if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4653 				send_in = 1;
4654 				if (stcb->asoc.stream_reset_outstanding) {
4655 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4656 					error = EALREADY;
4657 					SCTP_TCB_UNLOCK(stcb);
4658 					break;
4659 				}
4660 			}
4661 			if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4662 				send_out = 1;
4663 			}
4664 			if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4665 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4666 				error = ENOMEM;
4667 				SCTP_TCB_UNLOCK(stcb);
4668 				break;
4669 			}
4670 			if ((send_in == 0) && (send_out == 0)) {
4671 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4672 				error = EINVAL;
4673 				SCTP_TCB_UNLOCK(stcb);
4674 				break;
4675 			}
4676 			for (i = 0; i < strrst->srs_number_streams; i++) {
4677 				if ((send_in) &&
4678 				    (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4679 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4680 					error = EINVAL;
4681 					break;
4682 				}
4683 				if ((send_out) &&
4684 				    (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4685 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4686 					error = EINVAL;
4687 					break;
4688 				}
4689 			}
4690 			if (error) {
4691 				SCTP_TCB_UNLOCK(stcb);
4692 				break;
4693 			}
4694 			if (send_out) {
4695 				int cnt;
4696 				uint16_t strm;
4697 
4698 				if (strrst->srs_number_streams) {
4699 					for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4700 						strm = strrst->srs_stream_list[i];
4701 						if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4702 							stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4703 							cnt++;
4704 						}
4705 					}
4706 				} else {
4707 					/* Its all */
4708 					for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4709 						if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4710 							stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4711 							cnt++;
4712 						}
4713 					}
4714 				}
4715 			}
4716 			if (send_in) {
4717 				error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4718 				    strrst->srs_stream_list,
4719 				    send_in, 0, 0, 0, 0, 0);
4720 			} else {
4721 				error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4722 			}
4723 			if (error == 0) {
4724 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4725 			} else {
4726 				/*
4727 				 * For outgoing streams don't report any
4728 				 * problems in sending the request to the
4729 				 * application. XXX: Double check resetting
4730 				 * incoming streams.
4731 				 */
4732 				error = 0;
4733 			}
4734 			SCTP_TCB_UNLOCK(stcb);
4735 			break;
4736 		}
4737 	case SCTP_ADD_STREAMS:
4738 		{
4739 			struct sctp_add_streams *stradd;
4740 			uint8_t addstream = 0;
4741 			uint16_t add_o_strmcnt = 0;
4742 			uint16_t add_i_strmcnt = 0;
4743 
4744 			SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4745 			SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4746 			if (stcb == NULL) {
4747 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4748 				error = ENOENT;
4749 				break;
4750 			}
4751 			if (stcb->asoc.reconfig_supported == 0) {
4752 				/*
4753 				 * Peer does not support the chunk type.
4754 				 */
4755 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4756 				error = EOPNOTSUPP;
4757 				SCTP_TCB_UNLOCK(stcb);
4758 				break;
4759 			}
4760 			if (stcb->asoc.stream_reset_outstanding) {
4761 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4762 				error = EALREADY;
4763 				SCTP_TCB_UNLOCK(stcb);
4764 				break;
4765 			}
4766 			if ((stradd->sas_outstrms == 0) &&
4767 			    (stradd->sas_instrms == 0)) {
4768 				error = EINVAL;
4769 				goto skip_stuff;
4770 			}
4771 			if (stradd->sas_outstrms) {
4772 				addstream = 1;
4773 				/* We allocate here */
4774 				add_o_strmcnt = stradd->sas_outstrms;
4775 				if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4776 					/* You can't have more than 64k */
4777 					error = EINVAL;
4778 					goto skip_stuff;
4779 				}
4780 			}
4781 			if (stradd->sas_instrms) {
4782 				int cnt;
4783 
4784 				addstream |= 2;
4785 				/*
4786 				 * We allocate inside
4787 				 * sctp_send_str_reset_req()
4788 				 */
4789 				add_i_strmcnt = stradd->sas_instrms;
4790 				cnt = add_i_strmcnt;
4791 				cnt += stcb->asoc.streamincnt;
4792 				if (cnt > 0x0000ffff) {
4793 					/* You can't have more than 64k */
4794 					error = EINVAL;
4795 					goto skip_stuff;
4796 				}
4797 				if (cnt > (int)stcb->asoc.max_inbound_streams) {
4798 					/* More than you are allowed */
4799 					error = EINVAL;
4800 					goto skip_stuff;
4801 				}
4802 			}
4803 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4804 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4805 	skip_stuff:
4806 			SCTP_TCB_UNLOCK(stcb);
4807 			break;
4808 		}
4809 	case SCTP_RESET_ASSOC:
4810 		{
4811 			int i;
4812 			uint32_t *value;
4813 
4814 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4815 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
4816 			if (stcb == NULL) {
4817 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4818 				error = ENOENT;
4819 				break;
4820 			}
4821 			if (stcb->asoc.reconfig_supported == 0) {
4822 				/*
4823 				 * Peer does not support the chunk type.
4824 				 */
4825 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4826 				error = EOPNOTSUPP;
4827 				SCTP_TCB_UNLOCK(stcb);
4828 				break;
4829 			}
4830 			if (stcb->asoc.stream_reset_outstanding) {
4831 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4832 				error = EALREADY;
4833 				SCTP_TCB_UNLOCK(stcb);
4834 				break;
4835 			}
4836 			/*
4837 			 * Is there any data pending in the send or sent
4838 			 * queues?
4839 			 */
4840 			if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4841 			    !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4842 		busy_out:
4843 				error = EBUSY;
4844 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4845 				SCTP_TCB_UNLOCK(stcb);
4846 				break;
4847 			}
4848 			/* Do any streams have data queued? */
4849 			for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4850 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4851 					goto busy_out;
4852 				}
4853 			}
4854 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4855 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4856 			SCTP_TCB_UNLOCK(stcb);
4857 			break;
4858 		}
4859 	case SCTP_CONNECT_X:
4860 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4861 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4862 			error = EINVAL;
4863 			break;
4864 		}
4865 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4866 		break;
4867 	case SCTP_CONNECT_X_DELAYED:
4868 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4869 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4870 			error = EINVAL;
4871 			break;
4872 		}
4873 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4874 		break;
4875 	case SCTP_CONNECT_X_COMPLETE:
4876 		{
4877 			struct sockaddr *sa;
4878 
4879 			/* FIXME MT: check correct? */
4880 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4881 
4882 			/* find tcb */
4883 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4884 				SCTP_INP_RLOCK(inp);
4885 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
4886 				if (stcb) {
4887 					SCTP_TCB_LOCK(stcb);
4888 				}
4889 				SCTP_INP_RUNLOCK(inp);
4890 			} else {
4891 				/*
4892 				 * We increment here since
4893 				 * sctp_findassociation_ep_addr() wil do a
4894 				 * decrement if it finds the stcb as long as
4895 				 * the locked tcb (last argument) is NOT a
4896 				 * TCB.. aka NULL.
4897 				 */
4898 				SCTP_INP_INCR_REF(inp);
4899 				stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4900 				if (stcb == NULL) {
4901 					SCTP_INP_DECR_REF(inp);
4902 				}
4903 			}
4904 
4905 			if (stcb == NULL) {
4906 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4907 				error = ENOENT;
4908 				break;
4909 			}
4910 			if (stcb->asoc.delayed_connection == 1) {
4911 				stcb->asoc.delayed_connection = 0;
4912 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4913 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4914 				    stcb->asoc.primary_destination,
4915 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4916 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4917 			} else {
4918 				/*
4919 				 * already expired or did not use delayed
4920 				 * connectx
4921 				 */
4922 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4923 				error = EALREADY;
4924 			}
4925 			SCTP_TCB_UNLOCK(stcb);
4926 			break;
4927 		}
4928 	case SCTP_MAX_BURST:
4929 		{
4930 			struct sctp_assoc_value *av;
4931 
4932 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4933 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4934 
4935 			if (stcb) {
4936 				stcb->asoc.max_burst = av->assoc_value;
4937 				SCTP_TCB_UNLOCK(stcb);
4938 			} else {
4939 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4940 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4941 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4942 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4943 					SCTP_INP_WLOCK(inp);
4944 					inp->sctp_ep.max_burst = av->assoc_value;
4945 					SCTP_INP_WUNLOCK(inp);
4946 				}
4947 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4948 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4949 					SCTP_INP_RLOCK(inp);
4950 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4951 						SCTP_TCB_LOCK(stcb);
4952 						stcb->asoc.max_burst = av->assoc_value;
4953 						SCTP_TCB_UNLOCK(stcb);
4954 					}
4955 					SCTP_INP_RUNLOCK(inp);
4956 				}
4957 			}
4958 			break;
4959 		}
4960 	case SCTP_MAXSEG:
4961 		{
4962 			struct sctp_assoc_value *av;
4963 			int ovh;
4964 
4965 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4966 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4967 
4968 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4969 				ovh = SCTP_MED_OVERHEAD;
4970 			} else {
4971 				ovh = SCTP_MED_V4_OVERHEAD;
4972 			}
4973 			if (stcb) {
4974 				if (av->assoc_value) {
4975 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4976 				} else {
4977 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4978 				}
4979 				SCTP_TCB_UNLOCK(stcb);
4980 			} else {
4981 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4982 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4983 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4984 					SCTP_INP_WLOCK(inp);
4985 					/*
4986 					 * FIXME MT: I think this is not in
4987 					 * tune with the API ID
4988 					 */
4989 					if (av->assoc_value) {
4990 						inp->sctp_frag_point = (av->assoc_value + ovh);
4991 					} else {
4992 						inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4993 					}
4994 					SCTP_INP_WUNLOCK(inp);
4995 				} else {
4996 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4997 					error = EINVAL;
4998 				}
4999 			}
5000 			break;
5001 		}
5002 	case SCTP_EVENTS:
5003 		{
5004 			struct sctp_event_subscribe *events;
5005 
5006 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
5007 
5008 			SCTP_INP_WLOCK(inp);
5009 			if (events->sctp_data_io_event) {
5010 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5011 			} else {
5012 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5013 			}
5014 
5015 			if (events->sctp_association_event) {
5016 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5017 			} else {
5018 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5019 			}
5020 
5021 			if (events->sctp_address_event) {
5022 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5023 			} else {
5024 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5025 			}
5026 
5027 			if (events->sctp_send_failure_event) {
5028 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5029 			} else {
5030 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5031 			}
5032 
5033 			if (events->sctp_peer_error_event) {
5034 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5035 			} else {
5036 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5037 			}
5038 
5039 			if (events->sctp_shutdown_event) {
5040 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5041 			} else {
5042 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5043 			}
5044 
5045 			if (events->sctp_partial_delivery_event) {
5046 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5047 			} else {
5048 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5049 			}
5050 
5051 			if (events->sctp_adaptation_layer_event) {
5052 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5053 			} else {
5054 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5055 			}
5056 
5057 			if (events->sctp_authentication_event) {
5058 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5059 			} else {
5060 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5061 			}
5062 
5063 			if (events->sctp_sender_dry_event) {
5064 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5065 			} else {
5066 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5067 			}
5068 
5069 			if (events->sctp_stream_reset_event) {
5070 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5071 			} else {
5072 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5073 			}
5074 			SCTP_INP_WUNLOCK(inp);
5075 
5076 			SCTP_INP_RLOCK(inp);
5077 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5078 				SCTP_TCB_LOCK(stcb);
5079 				if (events->sctp_association_event) {
5080 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5081 				} else {
5082 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5083 				}
5084 				if (events->sctp_address_event) {
5085 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5086 				} else {
5087 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5088 				}
5089 				if (events->sctp_send_failure_event) {
5090 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5091 				} else {
5092 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5093 				}
5094 				if (events->sctp_peer_error_event) {
5095 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5096 				} else {
5097 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5098 				}
5099 				if (events->sctp_shutdown_event) {
5100 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5101 				} else {
5102 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5103 				}
5104 				if (events->sctp_partial_delivery_event) {
5105 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5106 				} else {
5107 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5108 				}
5109 				if (events->sctp_adaptation_layer_event) {
5110 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5111 				} else {
5112 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5113 				}
5114 				if (events->sctp_authentication_event) {
5115 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5116 				} else {
5117 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5118 				}
5119 				if (events->sctp_sender_dry_event) {
5120 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5121 				} else {
5122 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5123 				}
5124 				if (events->sctp_stream_reset_event) {
5125 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5126 				} else {
5127 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5128 				}
5129 				SCTP_TCB_UNLOCK(stcb);
5130 			}
5131 			/*
5132 			 * Send up the sender dry event only for 1-to-1
5133 			 * style sockets.
5134 			 */
5135 			if (events->sctp_sender_dry_event) {
5136 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5137 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5138 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
5139 					if (stcb) {
5140 						SCTP_TCB_LOCK(stcb);
5141 						if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5142 						    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5143 						    (stcb->asoc.stream_queue_cnt == 0)) {
5144 							sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5145 						}
5146 						SCTP_TCB_UNLOCK(stcb);
5147 					}
5148 				}
5149 			}
5150 			SCTP_INP_RUNLOCK(inp);
5151 			break;
5152 		}
5153 	case SCTP_ADAPTATION_LAYER:
5154 		{
5155 			struct sctp_setadaptation *adap_bits;
5156 
5157 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5158 			SCTP_INP_WLOCK(inp);
5159 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5160 			inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5161 			SCTP_INP_WUNLOCK(inp);
5162 			break;
5163 		}
5164 #ifdef SCTP_DEBUG
5165 	case SCTP_SET_INITIAL_DBG_SEQ:
5166 		{
5167 			uint32_t *vvv;
5168 
5169 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5170 			SCTP_INP_WLOCK(inp);
5171 			inp->sctp_ep.initial_sequence_debug = *vvv;
5172 			SCTP_INP_WUNLOCK(inp);
5173 			break;
5174 		}
5175 #endif
5176 	case SCTP_DEFAULT_SEND_PARAM:
5177 		{
5178 			struct sctp_sndrcvinfo *s_info;
5179 
5180 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5181 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5182 
5183 			if (stcb) {
5184 				if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5185 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5186 				} else {
5187 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5188 					error = EINVAL;
5189 				}
5190 				SCTP_TCB_UNLOCK(stcb);
5191 			} else {
5192 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5193 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5194 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5195 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5196 					SCTP_INP_WLOCK(inp);
5197 					memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5198 					SCTP_INP_WUNLOCK(inp);
5199 				}
5200 				if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5201 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5202 					SCTP_INP_RLOCK(inp);
5203 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5204 						SCTP_TCB_LOCK(stcb);
5205 						if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5206 							memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5207 						}
5208 						SCTP_TCB_UNLOCK(stcb);
5209 					}
5210 					SCTP_INP_RUNLOCK(inp);
5211 				}
5212 			}
5213 			break;
5214 		}
5215 	case SCTP_PEER_ADDR_PARAMS:
5216 		{
5217 			struct sctp_paddrparams *paddrp;
5218 			struct sctp_nets *net;
5219 			struct sockaddr *addr;
5220 
5221 #if defined(INET) && defined(INET6)
5222 			struct sockaddr_in sin_store;
5223 
5224 #endif
5225 
5226 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5227 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5228 
5229 #if defined(INET) && defined(INET6)
5230 			if (paddrp->spp_address.ss_family == AF_INET6) {
5231 				struct sockaddr_in6 *sin6;
5232 
5233 				sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5234 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5235 					in6_sin6_2_sin(&sin_store, sin6);
5236 					addr = (struct sockaddr *)&sin_store;
5237 				} else {
5238 					addr = (struct sockaddr *)&paddrp->spp_address;
5239 				}
5240 			} else {
5241 				addr = (struct sockaddr *)&paddrp->spp_address;
5242 			}
5243 #else
5244 			addr = (struct sockaddr *)&paddrp->spp_address;
5245 #endif
5246 			if (stcb != NULL) {
5247 				net = sctp_findnet(stcb, addr);
5248 			} else {
5249 				/*
5250 				 * We increment here since
5251 				 * sctp_findassociation_ep_addr() wil do a
5252 				 * decrement if it finds the stcb as long as
5253 				 * the locked tcb (last argument) is NOT a
5254 				 * TCB.. aka NULL.
5255 				 */
5256 				net = NULL;
5257 				SCTP_INP_INCR_REF(inp);
5258 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5259 				    &net, NULL, NULL);
5260 				if (stcb == NULL) {
5261 					SCTP_INP_DECR_REF(inp);
5262 				}
5263 			}
5264 			if ((stcb != NULL) && (net == NULL)) {
5265 #ifdef INET
5266 				if (addr->sa_family == AF_INET) {
5267 
5268 					struct sockaddr_in *sin;
5269 
5270 					sin = (struct sockaddr_in *)addr;
5271 					if (sin->sin_addr.s_addr != INADDR_ANY) {
5272 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5273 						SCTP_TCB_UNLOCK(stcb);
5274 						error = EINVAL;
5275 						break;
5276 					}
5277 				} else
5278 #endif
5279 #ifdef INET6
5280 				if (addr->sa_family == AF_INET6) {
5281 					struct sockaddr_in6 *sin6;
5282 
5283 					sin6 = (struct sockaddr_in6 *)addr;
5284 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5285 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5286 						SCTP_TCB_UNLOCK(stcb);
5287 						error = EINVAL;
5288 						break;
5289 					}
5290 				} else
5291 #endif
5292 				{
5293 					error = EAFNOSUPPORT;
5294 					SCTP_TCB_UNLOCK(stcb);
5295 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5296 					break;
5297 				}
5298 			}
5299 			/* sanity checks */
5300 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5301 				if (stcb)
5302 					SCTP_TCB_UNLOCK(stcb);
5303 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5304 				return (EINVAL);
5305 			}
5306 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5307 				if (stcb)
5308 					SCTP_TCB_UNLOCK(stcb);
5309 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5310 				return (EINVAL);
5311 			}
5312 			if (stcb != NULL) {
5313 				/************************TCB SPECIFIC SET ******************/
5314 				if (net != NULL) {
5315 					/************************NET SPECIFIC SET ******************/
5316 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5317 						if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5318 						    !(net->dest_state & SCTP_ADDR_NOHB)) {
5319 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5320 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5321 						}
5322 						net->dest_state |= SCTP_ADDR_NOHB;
5323 					}
5324 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5325 						if (paddrp->spp_hbinterval) {
5326 							net->heart_beat_delay = paddrp->spp_hbinterval;
5327 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5328 							net->heart_beat_delay = 0;
5329 						}
5330 						sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5331 						    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5332 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5333 						net->dest_state &= ~SCTP_ADDR_NOHB;
5334 					}
5335 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
5336 						/* on demand HB */
5337 						sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5338 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5339 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5340 					}
5341 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5342 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5343 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5344 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5345 						}
5346 						net->dest_state |= SCTP_ADDR_NO_PMTUD;
5347 						net->mtu = paddrp->spp_pathmtu;
5348 						switch (net->ro._l_addr.sa.sa_family) {
5349 #ifdef INET
5350 						case AF_INET:
5351 							net->mtu += SCTP_MIN_V4_OVERHEAD;
5352 							break;
5353 #endif
5354 #ifdef INET6
5355 						case AF_INET6:
5356 							net->mtu += SCTP_MIN_OVERHEAD;
5357 							break;
5358 #endif
5359 						default:
5360 							break;
5361 						}
5362 						if (net->mtu < stcb->asoc.smallest_mtu) {
5363 							sctp_pathmtu_adjustment(stcb, net->mtu);
5364 						}
5365 					}
5366 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5367 						if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5368 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5369 						}
5370 						net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5371 					}
5372 					if (paddrp->spp_pathmaxrxt) {
5373 						if (net->dest_state & SCTP_ADDR_PF) {
5374 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5375 								net->dest_state &= ~SCTP_ADDR_PF;
5376 							}
5377 						} else {
5378 							if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5379 							    (net->error_count > net->pf_threshold)) {
5380 								net->dest_state |= SCTP_ADDR_PF;
5381 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5382 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5383 								    stcb->sctp_ep, stcb, net,
5384 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5385 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5386 							}
5387 						}
5388 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
5389 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5390 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
5391 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5392 							}
5393 						} else {
5394 							if (net->error_count <= paddrp->spp_pathmaxrxt) {
5395 								net->dest_state |= SCTP_ADDR_REACHABLE;
5396 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5397 							}
5398 						}
5399 						net->failure_threshold = paddrp->spp_pathmaxrxt;
5400 					}
5401 					if (paddrp->spp_flags & SPP_DSCP) {
5402 						net->dscp = paddrp->spp_dscp & 0xfc;
5403 						net->dscp |= 0x01;
5404 					}
5405 #ifdef INET6
5406 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5407 						if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5408 							net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5409 							net->flowlabel |= 0x80000000;
5410 						}
5411 					}
5412 #endif
5413 				} else {
5414 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5415 					if (paddrp->spp_pathmaxrxt != 0) {
5416 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5417 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5418 							if (net->dest_state & SCTP_ADDR_PF) {
5419 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5420 									net->dest_state &= ~SCTP_ADDR_PF;
5421 								}
5422 							} else {
5423 								if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5424 								    (net->error_count > net->pf_threshold)) {
5425 									net->dest_state |= SCTP_ADDR_PF;
5426 									sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5427 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5428 									    stcb->sctp_ep, stcb, net,
5429 									    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5430 									sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5431 								}
5432 							}
5433 							if (net->dest_state & SCTP_ADDR_REACHABLE) {
5434 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5435 									net->dest_state &= ~SCTP_ADDR_REACHABLE;
5436 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5437 								}
5438 							} else {
5439 								if (net->error_count <= paddrp->spp_pathmaxrxt) {
5440 									net->dest_state |= SCTP_ADDR_REACHABLE;
5441 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5442 								}
5443 							}
5444 							net->failure_threshold = paddrp->spp_pathmaxrxt;
5445 						}
5446 					}
5447 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5448 						if (paddrp->spp_hbinterval != 0) {
5449 							stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5450 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5451 							stcb->asoc.heart_beat_delay = 0;
5452 						}
5453 						/* Turn back on the timer */
5454 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5455 							if (paddrp->spp_hbinterval != 0) {
5456 								net->heart_beat_delay = paddrp->spp_hbinterval;
5457 							} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5458 								net->heart_beat_delay = 0;
5459 							}
5460 							if (net->dest_state & SCTP_ADDR_NOHB) {
5461 								net->dest_state &= ~SCTP_ADDR_NOHB;
5462 							}
5463 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5464 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5465 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5466 						}
5467 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5468 					}
5469 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5470 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5471 							if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5472 								net->dest_state |= SCTP_ADDR_NOHB;
5473 								if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5474 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5475 									    inp, stcb, net,
5476 									    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5477 								}
5478 							}
5479 						}
5480 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5481 					}
5482 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5483 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5484 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5485 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5486 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5487 							}
5488 							net->dest_state |= SCTP_ADDR_NO_PMTUD;
5489 							net->mtu = paddrp->spp_pathmtu;
5490 							switch (net->ro._l_addr.sa.sa_family) {
5491 #ifdef INET
5492 							case AF_INET:
5493 								net->mtu += SCTP_MIN_V4_OVERHEAD;
5494 								break;
5495 #endif
5496 #ifdef INET6
5497 							case AF_INET6:
5498 								net->mtu += SCTP_MIN_OVERHEAD;
5499 								break;
5500 #endif
5501 							default:
5502 								break;
5503 							}
5504 							if (net->mtu < stcb->asoc.smallest_mtu) {
5505 								sctp_pathmtu_adjustment(stcb, net->mtu);
5506 							}
5507 						}
5508 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5509 					}
5510 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5511 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5512 							if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5513 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5514 							}
5515 							net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5516 						}
5517 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5518 					}
5519 					if (paddrp->spp_flags & SPP_DSCP) {
5520 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5521 							net->dscp = paddrp->spp_dscp & 0xfc;
5522 							net->dscp |= 0x01;
5523 						}
5524 						stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5525 						stcb->asoc.default_dscp |= 0x01;
5526 					}
5527 #ifdef INET6
5528 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5529 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5530 							if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5531 								net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5532 								net->flowlabel |= 0x80000000;
5533 							}
5534 						}
5535 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5536 						stcb->asoc.default_flowlabel |= 0x80000000;
5537 					}
5538 #endif
5539 				}
5540 				SCTP_TCB_UNLOCK(stcb);
5541 			} else {
5542 				/************************NO TCB, SET TO default stuff ******************/
5543 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5544 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5545 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5546 					SCTP_INP_WLOCK(inp);
5547 					/*
5548 					 * For the TOS/FLOWLABEL stuff you
5549 					 * set it with the options on the
5550 					 * socket
5551 					 */
5552 					if (paddrp->spp_pathmaxrxt != 0) {
5553 						inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5554 					}
5555 					if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5556 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5557 					else if (paddrp->spp_hbinterval != 0) {
5558 						if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5559 							paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5560 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5561 					}
5562 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5563 						if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5564 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5565 						} else if (paddrp->spp_hbinterval) {
5566 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5567 						}
5568 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5569 					} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5570 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5571 					}
5572 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5573 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5574 					} else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5575 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5576 					}
5577 					if (paddrp->spp_flags & SPP_DSCP) {
5578 						inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5579 						inp->sctp_ep.default_dscp |= 0x01;
5580 					}
5581 #ifdef INET6
5582 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5583 						if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5584 							inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5585 							inp->sctp_ep.default_flowlabel |= 0x80000000;
5586 						}
5587 					}
5588 #endif
5589 					SCTP_INP_WUNLOCK(inp);
5590 				} else {
5591 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5592 					error = EINVAL;
5593 				}
5594 			}
5595 			break;
5596 		}
5597 	case SCTP_RTOINFO:
5598 		{
5599 			struct sctp_rtoinfo *srto;
5600 			uint32_t new_init, new_min, new_max;
5601 
5602 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5603 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5604 
5605 			if (stcb) {
5606 				if (srto->srto_initial)
5607 					new_init = srto->srto_initial;
5608 				else
5609 					new_init = stcb->asoc.initial_rto;
5610 				if (srto->srto_max)
5611 					new_max = srto->srto_max;
5612 				else
5613 					new_max = stcb->asoc.maxrto;
5614 				if (srto->srto_min)
5615 					new_min = srto->srto_min;
5616 				else
5617 					new_min = stcb->asoc.minrto;
5618 				if ((new_min <= new_init) && (new_init <= new_max)) {
5619 					stcb->asoc.initial_rto = new_init;
5620 					stcb->asoc.maxrto = new_max;
5621 					stcb->asoc.minrto = new_min;
5622 				} else {
5623 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5624 					error = EINVAL;
5625 				}
5626 				SCTP_TCB_UNLOCK(stcb);
5627 			} else {
5628 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5629 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5630 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5631 					SCTP_INP_WLOCK(inp);
5632 					if (srto->srto_initial)
5633 						new_init = srto->srto_initial;
5634 					else
5635 						new_init = inp->sctp_ep.initial_rto;
5636 					if (srto->srto_max)
5637 						new_max = srto->srto_max;
5638 					else
5639 						new_max = inp->sctp_ep.sctp_maxrto;
5640 					if (srto->srto_min)
5641 						new_min = srto->srto_min;
5642 					else
5643 						new_min = inp->sctp_ep.sctp_minrto;
5644 					if ((new_min <= new_init) && (new_init <= new_max)) {
5645 						inp->sctp_ep.initial_rto = new_init;
5646 						inp->sctp_ep.sctp_maxrto = new_max;
5647 						inp->sctp_ep.sctp_minrto = new_min;
5648 					} else {
5649 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5650 						error = EINVAL;
5651 					}
5652 					SCTP_INP_WUNLOCK(inp);
5653 				} else {
5654 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5655 					error = EINVAL;
5656 				}
5657 			}
5658 			break;
5659 		}
5660 	case SCTP_ASSOCINFO:
5661 		{
5662 			struct sctp_assocparams *sasoc;
5663 
5664 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5665 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5666 			if (sasoc->sasoc_cookie_life) {
5667 				/* boundary check the cookie life */
5668 				if (sasoc->sasoc_cookie_life < 1000)
5669 					sasoc->sasoc_cookie_life = 1000;
5670 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5671 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5672 				}
5673 			}
5674 			if (stcb) {
5675 				if (sasoc->sasoc_asocmaxrxt)
5676 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5677 				if (sasoc->sasoc_cookie_life) {
5678 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5679 				}
5680 				SCTP_TCB_UNLOCK(stcb);
5681 			} else {
5682 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5683 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5684 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5685 					SCTP_INP_WLOCK(inp);
5686 					if (sasoc->sasoc_asocmaxrxt)
5687 						inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5688 					if (sasoc->sasoc_cookie_life) {
5689 						inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5690 					}
5691 					SCTP_INP_WUNLOCK(inp);
5692 				} else {
5693 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5694 					error = EINVAL;
5695 				}
5696 			}
5697 			break;
5698 		}
5699 	case SCTP_INITMSG:
5700 		{
5701 			struct sctp_initmsg *sinit;
5702 
5703 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5704 			SCTP_INP_WLOCK(inp);
5705 			if (sinit->sinit_num_ostreams)
5706 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5707 
5708 			if (sinit->sinit_max_instreams)
5709 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5710 
5711 			if (sinit->sinit_max_attempts)
5712 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5713 
5714 			if (sinit->sinit_max_init_timeo)
5715 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5716 			SCTP_INP_WUNLOCK(inp);
5717 			break;
5718 		}
5719 	case SCTP_PRIMARY_ADDR:
5720 		{
5721 			struct sctp_setprim *spa;
5722 			struct sctp_nets *net;
5723 			struct sockaddr *addr;
5724 
5725 #if defined(INET) && defined(INET6)
5726 			struct sockaddr_in sin_store;
5727 
5728 #endif
5729 
5730 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5731 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5732 
5733 #if defined(INET) && defined(INET6)
5734 			if (spa->ssp_addr.ss_family == AF_INET6) {
5735 				struct sockaddr_in6 *sin6;
5736 
5737 				sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5738 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5739 					in6_sin6_2_sin(&sin_store, sin6);
5740 					addr = (struct sockaddr *)&sin_store;
5741 				} else {
5742 					addr = (struct sockaddr *)&spa->ssp_addr;
5743 				}
5744 			} else {
5745 				addr = (struct sockaddr *)&spa->ssp_addr;
5746 			}
5747 #else
5748 			addr = (struct sockaddr *)&spa->ssp_addr;
5749 #endif
5750 			if (stcb != NULL) {
5751 				net = sctp_findnet(stcb, addr);
5752 			} else {
5753 				/*
5754 				 * We increment here since
5755 				 * sctp_findassociation_ep_addr() wil do a
5756 				 * decrement if it finds the stcb as long as
5757 				 * the locked tcb (last argument) is NOT a
5758 				 * TCB.. aka NULL.
5759 				 */
5760 				net = NULL;
5761 				SCTP_INP_INCR_REF(inp);
5762 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5763 				    &net, NULL, NULL);
5764 				if (stcb == NULL) {
5765 					SCTP_INP_DECR_REF(inp);
5766 				}
5767 			}
5768 
5769 			if ((stcb != NULL) && (net != NULL)) {
5770 				if (net != stcb->asoc.primary_destination) {
5771 					if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5772 						/* Ok we need to set it */
5773 						if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5774 							if ((stcb->asoc.alternate) &&
5775 							    (!(net->dest_state & SCTP_ADDR_PF)) &&
5776 							    (net->dest_state & SCTP_ADDR_REACHABLE)) {
5777 								sctp_free_remote_addr(stcb->asoc.alternate);
5778 								stcb->asoc.alternate = NULL;
5779 							}
5780 						} else {
5781 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5782 							error = EINVAL;
5783 						}
5784 					} else {
5785 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5786 						error = EINVAL;
5787 					}
5788 				}
5789 			} else {
5790 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5791 				error = EINVAL;
5792 			}
5793 			if (stcb != NULL) {
5794 				SCTP_TCB_UNLOCK(stcb);
5795 			}
5796 			break;
5797 		}
5798 	case SCTP_SET_DYNAMIC_PRIMARY:
5799 		{
5800 			union sctp_sockstore *ss;
5801 
5802 			error = priv_check(curthread,
5803 			    PRIV_NETINET_RESERVEDPORT);
5804 			if (error)
5805 				break;
5806 
5807 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5808 			/* SUPER USER CHECK? */
5809 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5810 			break;
5811 		}
5812 	case SCTP_SET_PEER_PRIMARY_ADDR:
5813 		{
5814 			struct sctp_setpeerprim *sspp;
5815 			struct sockaddr *addr;
5816 
5817 #if defined(INET) && defined(INET6)
5818 			struct sockaddr_in sin_store;
5819 
5820 #endif
5821 
5822 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5823 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5824 			if (stcb != NULL) {
5825 				struct sctp_ifa *ifa;
5826 
5827 #if defined(INET) && defined(INET6)
5828 				if (sspp->sspp_addr.ss_family == AF_INET6) {
5829 					struct sockaddr_in6 *sin6;
5830 
5831 					sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5832 					if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5833 						in6_sin6_2_sin(&sin_store, sin6);
5834 						addr = (struct sockaddr *)&sin_store;
5835 					} else {
5836 						addr = (struct sockaddr *)&sspp->sspp_addr;
5837 					}
5838 				} else {
5839 					addr = (struct sockaddr *)&sspp->sspp_addr;
5840 				}
5841 #else
5842 				addr = (struct sockaddr *)&sspp->sspp_addr;
5843 #endif
5844 				ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5845 				if (ifa == NULL) {
5846 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5847 					error = EINVAL;
5848 					goto out_of_it;
5849 				}
5850 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5851 					/*
5852 					 * Must validate the ifa found is in
5853 					 * our ep
5854 					 */
5855 					struct sctp_laddr *laddr;
5856 					int found = 0;
5857 
5858 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5859 						if (laddr->ifa == NULL) {
5860 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5861 							    __func__);
5862 							continue;
5863 						}
5864 						if ((sctp_is_addr_restricted(stcb, laddr->ifa)) &&
5865 						    (!sctp_is_addr_pending(stcb, laddr->ifa))) {
5866 							continue;
5867 						}
5868 						if (laddr->ifa == ifa) {
5869 							found = 1;
5870 							break;
5871 						}
5872 					}
5873 					if (!found) {
5874 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5875 						error = EINVAL;
5876 						goto out_of_it;
5877 					}
5878 				} else {
5879 					switch (addr->sa_family) {
5880 #ifdef INET
5881 					case AF_INET:
5882 						{
5883 							struct sockaddr_in *sin;
5884 
5885 							sin = (struct sockaddr_in *)addr;
5886 							if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5887 							    &sin->sin_addr) != 0) {
5888 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5889 								error = EINVAL;
5890 								goto out_of_it;
5891 							}
5892 							break;
5893 						}
5894 #endif
5895 #ifdef INET6
5896 					case AF_INET6:
5897 						{
5898 							struct sockaddr_in6 *sin6;
5899 
5900 							sin6 = (struct sockaddr_in6 *)addr;
5901 							if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5902 							    &sin6->sin6_addr) != 0) {
5903 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5904 								error = EINVAL;
5905 								goto out_of_it;
5906 							}
5907 							break;
5908 						}
5909 #endif
5910 					default:
5911 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5912 						error = EINVAL;
5913 						goto out_of_it;
5914 					}
5915 				}
5916 				if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5917 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5918 					error = EINVAL;
5919 				}
5920 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5921 		out_of_it:
5922 				SCTP_TCB_UNLOCK(stcb);
5923 			} else {
5924 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5925 				error = EINVAL;
5926 			}
5927 			break;
5928 		}
5929 	case SCTP_BINDX_ADD_ADDR:
5930 		{
5931 			struct sctp_getaddresses *addrs;
5932 			struct thread *td;
5933 
5934 			td = (struct thread *)p;
5935 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5936 			    optsize);
5937 #ifdef INET
5938 			if (addrs->addr->sa_family == AF_INET) {
5939 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5940 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5941 					error = EINVAL;
5942 					break;
5943 				}
5944 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5945 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5946 					break;
5947 				}
5948 			} else
5949 #endif
5950 #ifdef INET6
5951 			if (addrs->addr->sa_family == AF_INET6) {
5952 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5953 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5954 					error = EINVAL;
5955 					break;
5956 				}
5957 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5958 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5959 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5960 					break;
5961 				}
5962 			} else
5963 #endif
5964 			{
5965 				error = EAFNOSUPPORT;
5966 				break;
5967 			}
5968 			sctp_bindx_add_address(so, inp, addrs->addr,
5969 			    addrs->sget_assoc_id, vrf_id,
5970 			    &error, p);
5971 			break;
5972 		}
5973 	case SCTP_BINDX_REM_ADDR:
5974 		{
5975 			struct sctp_getaddresses *addrs;
5976 			struct thread *td;
5977 
5978 			td = (struct thread *)p;
5979 
5980 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5981 #ifdef INET
5982 			if (addrs->addr->sa_family == AF_INET) {
5983 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5984 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5985 					error = EINVAL;
5986 					break;
5987 				}
5988 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5989 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5990 					break;
5991 				}
5992 			} else
5993 #endif
5994 #ifdef INET6
5995 			if (addrs->addr->sa_family == AF_INET6) {
5996 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5997 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5998 					error = EINVAL;
5999 					break;
6000 				}
6001 				if (td != NULL &&
6002 				    (error = prison_local_ip6(td->td_ucred,
6003 				    &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
6004 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
6005 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6006 					break;
6007 				}
6008 			} else
6009 #endif
6010 			{
6011 				error = EAFNOSUPPORT;
6012 				break;
6013 			}
6014 			sctp_bindx_delete_address(inp, addrs->addr,
6015 			    addrs->sget_assoc_id, vrf_id,
6016 			    &error);
6017 			break;
6018 		}
6019 	case SCTP_EVENT:
6020 		{
6021 			struct sctp_event *event;
6022 			uint32_t event_type;
6023 
6024 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
6025 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
6026 			switch (event->se_type) {
6027 			case SCTP_ASSOC_CHANGE:
6028 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
6029 				break;
6030 			case SCTP_PEER_ADDR_CHANGE:
6031 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
6032 				break;
6033 			case SCTP_REMOTE_ERROR:
6034 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
6035 				break;
6036 			case SCTP_SEND_FAILED:
6037 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
6038 				break;
6039 			case SCTP_SHUTDOWN_EVENT:
6040 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
6041 				break;
6042 			case SCTP_ADAPTATION_INDICATION:
6043 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
6044 				break;
6045 			case SCTP_PARTIAL_DELIVERY_EVENT:
6046 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
6047 				break;
6048 			case SCTP_AUTHENTICATION_EVENT:
6049 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
6050 				break;
6051 			case SCTP_STREAM_RESET_EVENT:
6052 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
6053 				break;
6054 			case SCTP_SENDER_DRY_EVENT:
6055 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
6056 				break;
6057 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
6058 				event_type = 0;
6059 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6060 				error = ENOTSUP;
6061 				break;
6062 			case SCTP_ASSOC_RESET_EVENT:
6063 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
6064 				break;
6065 			case SCTP_STREAM_CHANGE_EVENT:
6066 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
6067 				break;
6068 			case SCTP_SEND_FAILED_EVENT:
6069 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6070 				break;
6071 			default:
6072 				event_type = 0;
6073 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6074 				error = EINVAL;
6075 				break;
6076 			}
6077 			if (event_type > 0) {
6078 				if (stcb) {
6079 					if (event->se_on) {
6080 						sctp_stcb_feature_on(inp, stcb, event_type);
6081 						if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6082 							if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6083 							    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6084 							    (stcb->asoc.stream_queue_cnt == 0)) {
6085 								sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6086 							}
6087 						}
6088 					} else {
6089 						sctp_stcb_feature_off(inp, stcb, event_type);
6090 					}
6091 					SCTP_TCB_UNLOCK(stcb);
6092 				} else {
6093 					/*
6094 					 * We don't want to send up a storm
6095 					 * of events, so return an error for
6096 					 * sender dry events
6097 					 */
6098 					if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6099 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6100 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6101 					    ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6102 					    (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6103 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6104 						error = ENOTSUP;
6105 						break;
6106 					}
6107 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6108 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6109 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6110 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6111 						SCTP_INP_WLOCK(inp);
6112 						if (event->se_on) {
6113 							sctp_feature_on(inp, event_type);
6114 						} else {
6115 							sctp_feature_off(inp, event_type);
6116 						}
6117 						SCTP_INP_WUNLOCK(inp);
6118 					}
6119 					if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6120 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6121 						SCTP_INP_RLOCK(inp);
6122 						LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6123 							SCTP_TCB_LOCK(stcb);
6124 							if (event->se_on) {
6125 								sctp_stcb_feature_on(inp, stcb, event_type);
6126 							} else {
6127 								sctp_stcb_feature_off(inp, stcb, event_type);
6128 							}
6129 							SCTP_TCB_UNLOCK(stcb);
6130 						}
6131 						SCTP_INP_RUNLOCK(inp);
6132 					}
6133 				}
6134 			}
6135 			break;
6136 		}
6137 	case SCTP_RECVRCVINFO:
6138 		{
6139 			int *onoff;
6140 
6141 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6142 			SCTP_INP_WLOCK(inp);
6143 			if (*onoff != 0) {
6144 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6145 			} else {
6146 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6147 			}
6148 			SCTP_INP_WUNLOCK(inp);
6149 			break;
6150 		}
6151 	case SCTP_RECVNXTINFO:
6152 		{
6153 			int *onoff;
6154 
6155 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6156 			SCTP_INP_WLOCK(inp);
6157 			if (*onoff != 0) {
6158 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6159 			} else {
6160 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6161 			}
6162 			SCTP_INP_WUNLOCK(inp);
6163 			break;
6164 		}
6165 	case SCTP_DEFAULT_SNDINFO:
6166 		{
6167 			struct sctp_sndinfo *info;
6168 			uint16_t policy;
6169 
6170 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6171 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6172 
6173 			if (stcb) {
6174 				if (info->snd_sid < stcb->asoc.streamoutcnt) {
6175 					stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6176 					policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6177 					stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6178 					stcb->asoc.def_send.sinfo_flags |= policy;
6179 					stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6180 					stcb->asoc.def_send.sinfo_context = info->snd_context;
6181 				} else {
6182 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6183 					error = EINVAL;
6184 				}
6185 				SCTP_TCB_UNLOCK(stcb);
6186 			} else {
6187 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6188 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6189 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6190 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6191 					SCTP_INP_WLOCK(inp);
6192 					inp->def_send.sinfo_stream = info->snd_sid;
6193 					policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6194 					inp->def_send.sinfo_flags = info->snd_flags;
6195 					inp->def_send.sinfo_flags |= policy;
6196 					inp->def_send.sinfo_ppid = info->snd_ppid;
6197 					inp->def_send.sinfo_context = info->snd_context;
6198 					SCTP_INP_WUNLOCK(inp);
6199 				}
6200 				if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6201 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6202 					SCTP_INP_RLOCK(inp);
6203 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6204 						SCTP_TCB_LOCK(stcb);
6205 						if (info->snd_sid < stcb->asoc.streamoutcnt) {
6206 							stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6207 							policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6208 							stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6209 							stcb->asoc.def_send.sinfo_flags |= policy;
6210 							stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6211 							stcb->asoc.def_send.sinfo_context = info->snd_context;
6212 						}
6213 						SCTP_TCB_UNLOCK(stcb);
6214 					}
6215 					SCTP_INP_RUNLOCK(inp);
6216 				}
6217 			}
6218 			break;
6219 		}
6220 	case SCTP_DEFAULT_PRINFO:
6221 		{
6222 			struct sctp_default_prinfo *info;
6223 
6224 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6225 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6226 
6227 			if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6228 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6229 				error = EINVAL;
6230 				break;
6231 			}
6232 			if (stcb) {
6233 				stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6234 				stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6235 				stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6236 				SCTP_TCB_UNLOCK(stcb);
6237 			} else {
6238 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6239 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6240 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6241 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6242 					SCTP_INP_WLOCK(inp);
6243 					inp->def_send.sinfo_flags &= 0xfff0;
6244 					inp->def_send.sinfo_flags |= info->pr_policy;
6245 					inp->def_send.sinfo_timetolive = info->pr_value;
6246 					SCTP_INP_WUNLOCK(inp);
6247 				}
6248 				if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6249 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6250 					SCTP_INP_RLOCK(inp);
6251 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6252 						SCTP_TCB_LOCK(stcb);
6253 						stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6254 						stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6255 						stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6256 						SCTP_TCB_UNLOCK(stcb);
6257 					}
6258 					SCTP_INP_RUNLOCK(inp);
6259 				}
6260 			}
6261 			break;
6262 		}
6263 	case SCTP_PEER_ADDR_THLDS:
6264 		/* Applies to the specific association */
6265 		{
6266 			struct sctp_paddrthlds *thlds;
6267 			struct sctp_nets *net;
6268 			struct sockaddr *addr;
6269 
6270 #if defined(INET) && defined(INET6)
6271 			struct sockaddr_in sin_store;
6272 
6273 #endif
6274 
6275 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6276 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6277 
6278 #if defined(INET) && defined(INET6)
6279 			if (thlds->spt_address.ss_family == AF_INET6) {
6280 				struct sockaddr_in6 *sin6;
6281 
6282 				sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6283 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6284 					in6_sin6_2_sin(&sin_store, sin6);
6285 					addr = (struct sockaddr *)&sin_store;
6286 				} else {
6287 					addr = (struct sockaddr *)&thlds->spt_address;
6288 				}
6289 			} else {
6290 				addr = (struct sockaddr *)&thlds->spt_address;
6291 			}
6292 #else
6293 			addr = (struct sockaddr *)&thlds->spt_address;
6294 #endif
6295 			if (stcb != NULL) {
6296 				net = sctp_findnet(stcb, addr);
6297 			} else {
6298 				/*
6299 				 * We increment here since
6300 				 * sctp_findassociation_ep_addr() wil do a
6301 				 * decrement if it finds the stcb as long as
6302 				 * the locked tcb (last argument) is NOT a
6303 				 * TCB.. aka NULL.
6304 				 */
6305 				net = NULL;
6306 				SCTP_INP_INCR_REF(inp);
6307 				stcb = sctp_findassociation_ep_addr(&inp, addr,
6308 				    &net, NULL, NULL);
6309 				if (stcb == NULL) {
6310 					SCTP_INP_DECR_REF(inp);
6311 				}
6312 			}
6313 			if ((stcb != NULL) && (net == NULL)) {
6314 #ifdef INET
6315 				if (addr->sa_family == AF_INET) {
6316 
6317 					struct sockaddr_in *sin;
6318 
6319 					sin = (struct sockaddr_in *)addr;
6320 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6321 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6322 						SCTP_TCB_UNLOCK(stcb);
6323 						error = EINVAL;
6324 						break;
6325 					}
6326 				} else
6327 #endif
6328 #ifdef INET6
6329 				if (addr->sa_family == AF_INET6) {
6330 					struct sockaddr_in6 *sin6;
6331 
6332 					sin6 = (struct sockaddr_in6 *)addr;
6333 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6334 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6335 						SCTP_TCB_UNLOCK(stcb);
6336 						error = EINVAL;
6337 						break;
6338 					}
6339 				} else
6340 #endif
6341 				{
6342 					error = EAFNOSUPPORT;
6343 					SCTP_TCB_UNLOCK(stcb);
6344 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6345 					break;
6346 				}
6347 			}
6348 			if (stcb != NULL) {
6349 				if (net != NULL) {
6350 					net->failure_threshold = thlds->spt_pathmaxrxt;
6351 					net->pf_threshold = thlds->spt_pathpfthld;
6352 					if (net->dest_state & SCTP_ADDR_PF) {
6353 						if ((net->error_count > net->failure_threshold) ||
6354 						    (net->error_count <= net->pf_threshold)) {
6355 							net->dest_state &= ~SCTP_ADDR_PF;
6356 						}
6357 					} else {
6358 						if ((net->error_count > net->pf_threshold) &&
6359 						    (net->error_count <= net->failure_threshold)) {
6360 							net->dest_state |= SCTP_ADDR_PF;
6361 							sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6362 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6363 							    stcb->sctp_ep, stcb, net,
6364 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6365 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6366 						}
6367 					}
6368 					if (net->dest_state & SCTP_ADDR_REACHABLE) {
6369 						if (net->error_count > net->failure_threshold) {
6370 							net->dest_state &= ~SCTP_ADDR_REACHABLE;
6371 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6372 						}
6373 					} else {
6374 						if (net->error_count <= net->failure_threshold) {
6375 							net->dest_state |= SCTP_ADDR_REACHABLE;
6376 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6377 						}
6378 					}
6379 				} else {
6380 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6381 						net->failure_threshold = thlds->spt_pathmaxrxt;
6382 						net->pf_threshold = thlds->spt_pathpfthld;
6383 						if (net->dest_state & SCTP_ADDR_PF) {
6384 							if ((net->error_count > net->failure_threshold) ||
6385 							    (net->error_count <= net->pf_threshold)) {
6386 								net->dest_state &= ~SCTP_ADDR_PF;
6387 							}
6388 						} else {
6389 							if ((net->error_count > net->pf_threshold) &&
6390 							    (net->error_count <= net->failure_threshold)) {
6391 								net->dest_state |= SCTP_ADDR_PF;
6392 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6393 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6394 								    stcb->sctp_ep, stcb, net,
6395 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
6396 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6397 							}
6398 						}
6399 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
6400 							if (net->error_count > net->failure_threshold) {
6401 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
6402 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6403 							}
6404 						} else {
6405 							if (net->error_count <= net->failure_threshold) {
6406 								net->dest_state |= SCTP_ADDR_REACHABLE;
6407 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6408 							}
6409 						}
6410 					}
6411 					stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6412 					stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6413 				}
6414 				SCTP_TCB_UNLOCK(stcb);
6415 			} else {
6416 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6417 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6418 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6419 					SCTP_INP_WLOCK(inp);
6420 					inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6421 					inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6422 					SCTP_INP_WUNLOCK(inp);
6423 				} else {
6424 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6425 					error = EINVAL;
6426 				}
6427 			}
6428 			break;
6429 		}
6430 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
6431 		{
6432 			struct sctp_udpencaps *encaps;
6433 			struct sctp_nets *net;
6434 			struct sockaddr *addr;
6435 
6436 #if defined(INET) && defined(INET6)
6437 			struct sockaddr_in sin_store;
6438 
6439 #endif
6440 
6441 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6442 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6443 
6444 #if defined(INET) && defined(INET6)
6445 			if (encaps->sue_address.ss_family == AF_INET6) {
6446 				struct sockaddr_in6 *sin6;
6447 
6448 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6449 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6450 					in6_sin6_2_sin(&sin_store, sin6);
6451 					addr = (struct sockaddr *)&sin_store;
6452 				} else {
6453 					addr = (struct sockaddr *)&encaps->sue_address;
6454 				}
6455 			} else {
6456 				addr = (struct sockaddr *)&encaps->sue_address;
6457 			}
6458 #else
6459 			addr = (struct sockaddr *)&encaps->sue_address;
6460 #endif
6461 			if (stcb != NULL) {
6462 				net = sctp_findnet(stcb, addr);
6463 			} else {
6464 				/*
6465 				 * We increment here since
6466 				 * sctp_findassociation_ep_addr() wil do a
6467 				 * decrement if it finds the stcb as long as
6468 				 * the locked tcb (last argument) is NOT a
6469 				 * TCB.. aka NULL.
6470 				 */
6471 				net = NULL;
6472 				SCTP_INP_INCR_REF(inp);
6473 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6474 				if (stcb == NULL) {
6475 					SCTP_INP_DECR_REF(inp);
6476 				}
6477 			}
6478 			if ((stcb != NULL) && (net == NULL)) {
6479 #ifdef INET
6480 				if (addr->sa_family == AF_INET) {
6481 
6482 					struct sockaddr_in *sin;
6483 
6484 					sin = (struct sockaddr_in *)addr;
6485 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6486 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6487 						SCTP_TCB_UNLOCK(stcb);
6488 						error = EINVAL;
6489 						break;
6490 					}
6491 				} else
6492 #endif
6493 #ifdef INET6
6494 				if (addr->sa_family == AF_INET6) {
6495 					struct sockaddr_in6 *sin6;
6496 
6497 					sin6 = (struct sockaddr_in6 *)addr;
6498 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6499 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6500 						SCTP_TCB_UNLOCK(stcb);
6501 						error = EINVAL;
6502 						break;
6503 					}
6504 				} else
6505 #endif
6506 				{
6507 					error = EAFNOSUPPORT;
6508 					SCTP_TCB_UNLOCK(stcb);
6509 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6510 					break;
6511 				}
6512 			}
6513 			if (stcb != NULL) {
6514 				if (net != NULL) {
6515 					net->port = encaps->sue_port;
6516 				} else {
6517 					stcb->asoc.port = encaps->sue_port;
6518 				}
6519 				SCTP_TCB_UNLOCK(stcb);
6520 			} else {
6521 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6522 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6523 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6524 					SCTP_INP_WLOCK(inp);
6525 					inp->sctp_ep.port = encaps->sue_port;
6526 					SCTP_INP_WUNLOCK(inp);
6527 				} else {
6528 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6529 					error = EINVAL;
6530 				}
6531 			}
6532 			break;
6533 		}
6534 	case SCTP_ECN_SUPPORTED:
6535 		{
6536 			struct sctp_assoc_value *av;
6537 
6538 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6539 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6540 
6541 			if (stcb) {
6542 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6543 				error = EINVAL;
6544 				SCTP_TCB_UNLOCK(stcb);
6545 			} else {
6546 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6547 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6548 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6549 					SCTP_INP_WLOCK(inp);
6550 					if (av->assoc_value == 0) {
6551 						inp->ecn_supported = 0;
6552 					} else {
6553 						inp->ecn_supported = 1;
6554 					}
6555 					SCTP_INP_WUNLOCK(inp);
6556 				} else {
6557 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6558 					error = EINVAL;
6559 				}
6560 			}
6561 			break;
6562 		}
6563 	case SCTP_PR_SUPPORTED:
6564 		{
6565 			struct sctp_assoc_value *av;
6566 
6567 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6568 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6569 
6570 			if (stcb) {
6571 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6572 				error = EINVAL;
6573 				SCTP_TCB_UNLOCK(stcb);
6574 			} else {
6575 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6576 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6577 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6578 					SCTP_INP_WLOCK(inp);
6579 					if (av->assoc_value == 0) {
6580 						inp->prsctp_supported = 0;
6581 					} else {
6582 						inp->prsctp_supported = 1;
6583 					}
6584 					SCTP_INP_WUNLOCK(inp);
6585 				} else {
6586 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6587 					error = EINVAL;
6588 				}
6589 			}
6590 			break;
6591 		}
6592 	case SCTP_AUTH_SUPPORTED:
6593 		{
6594 			struct sctp_assoc_value *av;
6595 
6596 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6597 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6598 
6599 			if (stcb) {
6600 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6601 				error = EINVAL;
6602 				SCTP_TCB_UNLOCK(stcb);
6603 			} else {
6604 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6605 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6606 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6607 					if ((av->assoc_value == 0) &&
6608 					    (inp->asconf_supported == 1)) {
6609 						/*
6610 						 * AUTH is required for
6611 						 * ASCONF
6612 						 */
6613 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6614 						error = EINVAL;
6615 					} else {
6616 						SCTP_INP_WLOCK(inp);
6617 						if (av->assoc_value == 0) {
6618 							inp->auth_supported = 0;
6619 						} else {
6620 							inp->auth_supported = 1;
6621 						}
6622 						SCTP_INP_WUNLOCK(inp);
6623 					}
6624 				} else {
6625 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6626 					error = EINVAL;
6627 				}
6628 			}
6629 			break;
6630 		}
6631 	case SCTP_ASCONF_SUPPORTED:
6632 		{
6633 			struct sctp_assoc_value *av;
6634 
6635 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6636 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6637 
6638 			if (stcb) {
6639 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6640 				error = EINVAL;
6641 				SCTP_TCB_UNLOCK(stcb);
6642 			} else {
6643 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6644 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6645 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6646 					if ((av->assoc_value != 0) &&
6647 					    (inp->auth_supported == 0)) {
6648 						/*
6649 						 * AUTH is required for
6650 						 * ASCONF
6651 						 */
6652 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6653 						error = EINVAL;
6654 					} else {
6655 						SCTP_INP_WLOCK(inp);
6656 						if (av->assoc_value == 0) {
6657 							inp->asconf_supported = 0;
6658 							sctp_auth_delete_chunk(SCTP_ASCONF,
6659 							    inp->sctp_ep.local_auth_chunks);
6660 							sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6661 							    inp->sctp_ep.local_auth_chunks);
6662 						} else {
6663 							inp->asconf_supported = 1;
6664 							sctp_auth_add_chunk(SCTP_ASCONF,
6665 							    inp->sctp_ep.local_auth_chunks);
6666 							sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6667 							    inp->sctp_ep.local_auth_chunks);
6668 						}
6669 						SCTP_INP_WUNLOCK(inp);
6670 					}
6671 				} else {
6672 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6673 					error = EINVAL;
6674 				}
6675 			}
6676 			break;
6677 		}
6678 	case SCTP_RECONFIG_SUPPORTED:
6679 		{
6680 			struct sctp_assoc_value *av;
6681 
6682 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6683 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6684 
6685 			if (stcb) {
6686 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6687 				error = EINVAL;
6688 				SCTP_TCB_UNLOCK(stcb);
6689 			} else {
6690 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6691 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6692 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6693 					SCTP_INP_WLOCK(inp);
6694 					if (av->assoc_value == 0) {
6695 						inp->reconfig_supported = 0;
6696 					} else {
6697 						inp->reconfig_supported = 1;
6698 					}
6699 					SCTP_INP_WUNLOCK(inp);
6700 				} else {
6701 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6702 					error = EINVAL;
6703 				}
6704 			}
6705 			break;
6706 		}
6707 	case SCTP_NRSACK_SUPPORTED:
6708 		{
6709 			struct sctp_assoc_value *av;
6710 
6711 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6712 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6713 
6714 			if (stcb) {
6715 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6716 				error = EINVAL;
6717 				SCTP_TCB_UNLOCK(stcb);
6718 			} else {
6719 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6720 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6721 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6722 					SCTP_INP_WLOCK(inp);
6723 					if (av->assoc_value == 0) {
6724 						inp->nrsack_supported = 0;
6725 					} else {
6726 						inp->nrsack_supported = 1;
6727 					}
6728 					SCTP_INP_WUNLOCK(inp);
6729 				} else {
6730 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6731 					error = EINVAL;
6732 				}
6733 			}
6734 			break;
6735 		}
6736 	case SCTP_PKTDROP_SUPPORTED:
6737 		{
6738 			struct sctp_assoc_value *av;
6739 
6740 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6741 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6742 
6743 			if (stcb) {
6744 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6745 				error = EINVAL;
6746 				SCTP_TCB_UNLOCK(stcb);
6747 			} else {
6748 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6749 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6750 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6751 					SCTP_INP_WLOCK(inp);
6752 					if (av->assoc_value == 0) {
6753 						inp->pktdrop_supported = 0;
6754 					} else {
6755 						inp->pktdrop_supported = 1;
6756 					}
6757 					SCTP_INP_WUNLOCK(inp);
6758 				} else {
6759 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6760 					error = EINVAL;
6761 				}
6762 			}
6763 			break;
6764 		}
6765 	case SCTP_MAX_CWND:
6766 		{
6767 			struct sctp_assoc_value *av;
6768 			struct sctp_nets *net;
6769 
6770 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6771 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6772 
6773 			if (stcb) {
6774 				stcb->asoc.max_cwnd = av->assoc_value;
6775 				if (stcb->asoc.max_cwnd > 0) {
6776 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6777 						if ((net->cwnd > stcb->asoc.max_cwnd) &&
6778 						    (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6779 							net->cwnd = stcb->asoc.max_cwnd;
6780 							if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6781 								net->cwnd = net->mtu - sizeof(struct sctphdr);
6782 							}
6783 						}
6784 					}
6785 				}
6786 				SCTP_TCB_UNLOCK(stcb);
6787 			} else {
6788 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6789 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6790 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6791 					SCTP_INP_WLOCK(inp);
6792 					inp->max_cwnd = av->assoc_value;
6793 					SCTP_INP_WUNLOCK(inp);
6794 				} else {
6795 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6796 					error = EINVAL;
6797 				}
6798 			}
6799 			break;
6800 		}
6801 	default:
6802 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6803 		error = ENOPROTOOPT;
6804 		break;
6805 	}			/* end switch (opt) */
6806 	return (error);
6807 }
6808 
6809 int
6810 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6811 {
6812 	void *optval = NULL;
6813 	size_t optsize = 0;
6814 	void *p;
6815 	int error = 0;
6816 	struct sctp_inpcb *inp;
6817 
6818 	if ((sopt->sopt_level == SOL_SOCKET) &&
6819 	    (sopt->sopt_name == SO_SETFIB)) {
6820 		inp = (struct sctp_inpcb *)so->so_pcb;
6821 		if (inp == NULL) {
6822 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6823 			return (EINVAL);
6824 		}
6825 		SCTP_INP_WLOCK(inp);
6826 		inp->fibnum = so->so_fibnum;
6827 		SCTP_INP_WUNLOCK(inp);
6828 		return (0);
6829 	}
6830 	if (sopt->sopt_level != IPPROTO_SCTP) {
6831 		/* wrong proto level... send back up to IP */
6832 #ifdef INET6
6833 		if (INP_CHECK_SOCKAF(so, AF_INET6))
6834 			error = ip6_ctloutput(so, sopt);
6835 #endif				/* INET6 */
6836 #if defined(INET) && defined(INET6)
6837 		else
6838 #endif
6839 #ifdef INET
6840 			error = ip_ctloutput(so, sopt);
6841 #endif
6842 		return (error);
6843 	}
6844 	optsize = sopt->sopt_valsize;
6845 	if (optsize) {
6846 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6847 		if (optval == NULL) {
6848 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6849 			return (ENOBUFS);
6850 		}
6851 		error = sooptcopyin(sopt, optval, optsize, optsize);
6852 		if (error) {
6853 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
6854 			goto out;
6855 		}
6856 	}
6857 	p = (void *)sopt->sopt_td;
6858 	if (sopt->sopt_dir == SOPT_SET) {
6859 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6860 	} else if (sopt->sopt_dir == SOPT_GET) {
6861 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6862 	} else {
6863 		SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6864 		error = EINVAL;
6865 	}
6866 	if ((error == 0) && (optval != NULL)) {
6867 		error = sooptcopyout(sopt, optval, optsize);
6868 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6869 	} else if (optval != NULL) {
6870 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6871 	}
6872 out:
6873 	return (error);
6874 }
6875 
6876 #ifdef INET
6877 static int
6878 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6879 {
6880 	int error = 0;
6881 	int create_lock_on = 0;
6882 	uint32_t vrf_id;
6883 	struct sctp_inpcb *inp;
6884 	struct sctp_tcb *stcb = NULL;
6885 
6886 	inp = (struct sctp_inpcb *)so->so_pcb;
6887 	if (inp == NULL) {
6888 		/* I made the same as TCP since we are not setup? */
6889 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6890 		return (ECONNRESET);
6891 	}
6892 	if (addr == NULL) {
6893 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6894 		return EINVAL;
6895 	}
6896 	switch (addr->sa_family) {
6897 #ifdef INET6
6898 	case AF_INET6:
6899 		{
6900 			struct sockaddr_in6 *sin6p;
6901 
6902 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6903 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6904 				return (EINVAL);
6905 			}
6906 			sin6p = (struct sockaddr_in6 *)addr;
6907 			if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6908 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6909 				return (error);
6910 			}
6911 			break;
6912 		}
6913 #endif
6914 #ifdef INET
6915 	case AF_INET:
6916 		{
6917 			struct sockaddr_in *sinp;
6918 
6919 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
6920 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6921 				return (EINVAL);
6922 			}
6923 			sinp = (struct sockaddr_in *)addr;
6924 			if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6925 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6926 				return (error);
6927 			}
6928 			break;
6929 		}
6930 #endif
6931 	default:
6932 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6933 		return (EAFNOSUPPORT);
6934 	}
6935 	SCTP_INP_INCR_REF(inp);
6936 	SCTP_ASOC_CREATE_LOCK(inp);
6937 	create_lock_on = 1;
6938 
6939 
6940 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6941 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6942 		/* Should I really unlock ? */
6943 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6944 		error = EFAULT;
6945 		goto out_now;
6946 	}
6947 #ifdef INET6
6948 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6949 	    (addr->sa_family == AF_INET6)) {
6950 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6951 		error = EINVAL;
6952 		goto out_now;
6953 	}
6954 #endif
6955 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6956 	    SCTP_PCB_FLAGS_UNBOUND) {
6957 		/* Bind a ephemeral port */
6958 		error = sctp_inpcb_bind(so, NULL, NULL, p);
6959 		if (error) {
6960 			goto out_now;
6961 		}
6962 	}
6963 	/* Now do we connect? */
6964 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6965 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6966 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6967 		error = EINVAL;
6968 		goto out_now;
6969 	}
6970 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6971 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6972 		/* We are already connected AND the TCP model */
6973 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6974 		error = EADDRINUSE;
6975 		goto out_now;
6976 	}
6977 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6978 		SCTP_INP_RLOCK(inp);
6979 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
6980 		SCTP_INP_RUNLOCK(inp);
6981 	} else {
6982 		/*
6983 		 * We increment here since sctp_findassociation_ep_addr()
6984 		 * will do a decrement if it finds the stcb as long as the
6985 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
6986 		 */
6987 		SCTP_INP_INCR_REF(inp);
6988 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6989 		if (stcb == NULL) {
6990 			SCTP_INP_DECR_REF(inp);
6991 		} else {
6992 			SCTP_TCB_UNLOCK(stcb);
6993 		}
6994 	}
6995 	if (stcb != NULL) {
6996 		/* Already have or am bring up an association */
6997 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
6998 		error = EALREADY;
6999 		goto out_now;
7000 	}
7001 	vrf_id = inp->def_vrf_id;
7002 	/* We are GOOD to go */
7003 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
7004 	    inp->sctp_ep.pre_open_stream_count,
7005 	    inp->sctp_ep.port, p);
7006 	if (stcb == NULL) {
7007 		/* Gak! no memory */
7008 		goto out_now;
7009 	}
7010 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
7011 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7012 		/* Set the connected flag so we can queue data */
7013 		soisconnecting(so);
7014 	}
7015 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
7016 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7017 
7018 	/* initialize authentication parameters for the assoc */
7019 	sctp_initialize_auth_params(inp, stcb);
7020 
7021 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7022 	SCTP_TCB_UNLOCK(stcb);
7023 out_now:
7024 	if (create_lock_on) {
7025 		SCTP_ASOC_CREATE_UNLOCK(inp);
7026 	}
7027 	SCTP_INP_DECR_REF(inp);
7028 	return (error);
7029 }
7030 
7031 #endif
7032 
7033 int
7034 sctp_listen(struct socket *so, int backlog, struct thread *p)
7035 {
7036 	/*
7037 	 * Note this module depends on the protocol processing being called
7038 	 * AFTER any socket level flags and backlog are applied to the
7039 	 * socket. The traditional way that the socket flags are applied is
7040 	 * AFTER protocol processing. We have made a change to the
7041 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
7042 	 * place if the socket API for SCTP is to work properly.
7043 	 */
7044 
7045 	int error = 0;
7046 	struct sctp_inpcb *inp;
7047 
7048 	inp = (struct sctp_inpcb *)so->so_pcb;
7049 	if (inp == NULL) {
7050 		/* I made the same as TCP since we are not setup? */
7051 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7052 		return (ECONNRESET);
7053 	}
7054 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
7055 		/* See if we have a listener */
7056 		struct sctp_inpcb *tinp;
7057 		union sctp_sockstore store;
7058 
7059 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
7060 			/* not bound all */
7061 			struct sctp_laddr *laddr;
7062 
7063 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7064 				memcpy(&store, &laddr->ifa->address, sizeof(store));
7065 				switch (store.sa.sa_family) {
7066 #ifdef INET
7067 				case AF_INET:
7068 					store.sin.sin_port = inp->sctp_lport;
7069 					break;
7070 #endif
7071 #ifdef INET6
7072 				case AF_INET6:
7073 					store.sin6.sin6_port = inp->sctp_lport;
7074 					break;
7075 #endif
7076 				default:
7077 					break;
7078 				}
7079 				tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7080 				if (tinp && (tinp != inp) &&
7081 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7082 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7083 				    (tinp->sctp_socket->so_qlimit)) {
7084 					/*
7085 					 * we have a listener already and
7086 					 * its not this inp.
7087 					 */
7088 					SCTP_INP_DECR_REF(tinp);
7089 					return (EADDRINUSE);
7090 				} else if (tinp) {
7091 					SCTP_INP_DECR_REF(tinp);
7092 				}
7093 			}
7094 		} else {
7095 			/* Setup a local addr bound all */
7096 			memset(&store, 0, sizeof(store));
7097 #ifdef INET6
7098 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7099 				store.sa.sa_family = AF_INET6;
7100 				store.sa.sa_len = sizeof(struct sockaddr_in6);
7101 			}
7102 #endif
7103 #ifdef INET
7104 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7105 				store.sa.sa_family = AF_INET;
7106 				store.sa.sa_len = sizeof(struct sockaddr_in);
7107 			}
7108 #endif
7109 			switch (store.sa.sa_family) {
7110 #ifdef INET
7111 			case AF_INET:
7112 				store.sin.sin_port = inp->sctp_lport;
7113 				break;
7114 #endif
7115 #ifdef INET6
7116 			case AF_INET6:
7117 				store.sin6.sin6_port = inp->sctp_lport;
7118 				break;
7119 #endif
7120 			default:
7121 				break;
7122 			}
7123 			tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7124 			if (tinp && (tinp != inp) &&
7125 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7126 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7127 			    (tinp->sctp_socket->so_qlimit)) {
7128 				/*
7129 				 * we have a listener already and its not
7130 				 * this inp.
7131 				 */
7132 				SCTP_INP_DECR_REF(tinp);
7133 				return (EADDRINUSE);
7134 			} else if (tinp) {
7135 				SCTP_INP_DECR_REF(tinp);
7136 			}
7137 		}
7138 	}
7139 	SCTP_INP_RLOCK(inp);
7140 #ifdef SCTP_LOCK_LOGGING
7141 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7142 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7143 	}
7144 #endif
7145 	SOCK_LOCK(so);
7146 	error = solisten_proto_check(so);
7147 	SOCK_UNLOCK(so);
7148 	if (error) {
7149 		SCTP_INP_RUNLOCK(inp);
7150 		return (error);
7151 	}
7152 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7153 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7154 		/*
7155 		 * The unlucky case - We are in the tcp pool with this guy.
7156 		 * - Someone else is in the main inp slot. - We must move
7157 		 * this guy (the listener) to the main slot - We must then
7158 		 * move the guy that was listener to the TCP Pool.
7159 		 */
7160 		if (sctp_swap_inpcb_for_listen(inp)) {
7161 			SCTP_INP_RUNLOCK(inp);
7162 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7163 			return (EADDRINUSE);
7164 		}
7165 	}
7166 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7167 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7168 		/* We are already connected AND the TCP model */
7169 		SCTP_INP_RUNLOCK(inp);
7170 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7171 		return (EADDRINUSE);
7172 	}
7173 	SCTP_INP_RUNLOCK(inp);
7174 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7175 		/* We must do a bind. */
7176 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7177 			/* bind error, probably perm */
7178 			return (error);
7179 		}
7180 	}
7181 	SOCK_LOCK(so);
7182 	/* It appears for 7.0 and on, we must always call this. */
7183 	solisten_proto(so, backlog);
7184 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7185 		/* remove the ACCEPTCONN flag for one-to-many sockets */
7186 		so->so_options &= ~SO_ACCEPTCONN;
7187 	}
7188 	if (backlog == 0) {
7189 		/* turning off listen */
7190 		so->so_options &= ~SO_ACCEPTCONN;
7191 	}
7192 	SOCK_UNLOCK(so);
7193 	return (error);
7194 }
7195 
7196 static int sctp_defered_wakeup_cnt = 0;
7197 
7198 int
7199 sctp_accept(struct socket *so, struct sockaddr **addr)
7200 {
7201 	struct sctp_tcb *stcb;
7202 	struct sctp_inpcb *inp;
7203 	union sctp_sockstore store;
7204 
7205 #ifdef INET6
7206 	int error;
7207 
7208 #endif
7209 	inp = (struct sctp_inpcb *)so->so_pcb;
7210 
7211 	if (inp == NULL) {
7212 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7213 		return (ECONNRESET);
7214 	}
7215 	SCTP_INP_RLOCK(inp);
7216 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7217 		SCTP_INP_RUNLOCK(inp);
7218 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7219 		return (EOPNOTSUPP);
7220 	}
7221 	if (so->so_state & SS_ISDISCONNECTED) {
7222 		SCTP_INP_RUNLOCK(inp);
7223 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7224 		return (ECONNABORTED);
7225 	}
7226 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7227 	if (stcb == NULL) {
7228 		SCTP_INP_RUNLOCK(inp);
7229 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7230 		return (ECONNRESET);
7231 	}
7232 	SCTP_TCB_LOCK(stcb);
7233 	SCTP_INP_RUNLOCK(inp);
7234 	store = stcb->asoc.primary_destination->ro._l_addr;
7235 	stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
7236 	SCTP_TCB_UNLOCK(stcb);
7237 	switch (store.sa.sa_family) {
7238 #ifdef INET
7239 	case AF_INET:
7240 		{
7241 			struct sockaddr_in *sin;
7242 
7243 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7244 			if (sin == NULL)
7245 				return (ENOMEM);
7246 			sin->sin_family = AF_INET;
7247 			sin->sin_len = sizeof(*sin);
7248 			sin->sin_port = store.sin.sin_port;
7249 			sin->sin_addr = store.sin.sin_addr;
7250 			*addr = (struct sockaddr *)sin;
7251 			break;
7252 		}
7253 #endif
7254 #ifdef INET6
7255 	case AF_INET6:
7256 		{
7257 			struct sockaddr_in6 *sin6;
7258 
7259 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7260 			if (sin6 == NULL)
7261 				return (ENOMEM);
7262 			sin6->sin6_family = AF_INET6;
7263 			sin6->sin6_len = sizeof(*sin6);
7264 			sin6->sin6_port = store.sin6.sin6_port;
7265 			sin6->sin6_addr = store.sin6.sin6_addr;
7266 			if ((error = sa6_recoverscope(sin6)) != 0) {
7267 				SCTP_FREE_SONAME(sin6);
7268 				return (error);
7269 			}
7270 			*addr = (struct sockaddr *)sin6;
7271 			break;
7272 		}
7273 #endif
7274 	default:
7275 		/* TSNH */
7276 		break;
7277 	}
7278 	/* Wake any delayed sleep action */
7279 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7280 		SCTP_INP_WLOCK(inp);
7281 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7282 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7283 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7284 			SCTP_INP_WUNLOCK(inp);
7285 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7286 			if (sowriteable(inp->sctp_socket)) {
7287 				sowwakeup_locked(inp->sctp_socket);
7288 			} else {
7289 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7290 			}
7291 			SCTP_INP_WLOCK(inp);
7292 		}
7293 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7294 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7295 			SCTP_INP_WUNLOCK(inp);
7296 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7297 			if (soreadable(inp->sctp_socket)) {
7298 				sctp_defered_wakeup_cnt++;
7299 				sorwakeup_locked(inp->sctp_socket);
7300 			} else {
7301 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7302 			}
7303 			SCTP_INP_WLOCK(inp);
7304 		}
7305 		SCTP_INP_WUNLOCK(inp);
7306 	}
7307 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7308 		SCTP_TCB_LOCK(stcb);
7309 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7310 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7311 	}
7312 	return (0);
7313 }
7314 
7315 #ifdef INET
7316 int
7317 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7318 {
7319 	struct sockaddr_in *sin;
7320 	uint32_t vrf_id;
7321 	struct sctp_inpcb *inp;
7322 	struct sctp_ifa *sctp_ifa;
7323 
7324 	/*
7325 	 * Do the malloc first in case it blocks.
7326 	 */
7327 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7328 	if (sin == NULL)
7329 		return (ENOMEM);
7330 	sin->sin_family = AF_INET;
7331 	sin->sin_len = sizeof(*sin);
7332 	inp = (struct sctp_inpcb *)so->so_pcb;
7333 	if (!inp) {
7334 		SCTP_FREE_SONAME(sin);
7335 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7336 		return (ECONNRESET);
7337 	}
7338 	SCTP_INP_RLOCK(inp);
7339 	sin->sin_port = inp->sctp_lport;
7340 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7341 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7342 			struct sctp_tcb *stcb;
7343 			struct sockaddr_in *sin_a;
7344 			struct sctp_nets *net;
7345 			int fnd;
7346 
7347 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
7348 			if (stcb == NULL) {
7349 				goto notConn;
7350 			}
7351 			fnd = 0;
7352 			sin_a = NULL;
7353 			SCTP_TCB_LOCK(stcb);
7354 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7355 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7356 				if (sin_a == NULL)
7357 					/* this will make coverity happy */
7358 					continue;
7359 
7360 				if (sin_a->sin_family == AF_INET) {
7361 					fnd = 1;
7362 					break;
7363 				}
7364 			}
7365 			if ((!fnd) || (sin_a == NULL)) {
7366 				/* punt */
7367 				SCTP_TCB_UNLOCK(stcb);
7368 				goto notConn;
7369 			}
7370 			vrf_id = inp->def_vrf_id;
7371 			sctp_ifa = sctp_source_address_selection(inp,
7372 			    stcb,
7373 			    (sctp_route_t *) & net->ro,
7374 			    net, 0, vrf_id);
7375 			if (sctp_ifa) {
7376 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7377 				sctp_free_ifa(sctp_ifa);
7378 			}
7379 			SCTP_TCB_UNLOCK(stcb);
7380 		} else {
7381 			/* For the bound all case you get back 0 */
7382 	notConn:
7383 			sin->sin_addr.s_addr = 0;
7384 		}
7385 
7386 	} else {
7387 		/* Take the first IPv4 address in the list */
7388 		struct sctp_laddr *laddr;
7389 		int fnd = 0;
7390 
7391 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7392 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
7393 				struct sockaddr_in *sin_a;
7394 
7395 				sin_a = &laddr->ifa->address.sin;
7396 				sin->sin_addr = sin_a->sin_addr;
7397 				fnd = 1;
7398 				break;
7399 			}
7400 		}
7401 		if (!fnd) {
7402 			SCTP_FREE_SONAME(sin);
7403 			SCTP_INP_RUNLOCK(inp);
7404 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7405 			return (ENOENT);
7406 		}
7407 	}
7408 	SCTP_INP_RUNLOCK(inp);
7409 	(*addr) = (struct sockaddr *)sin;
7410 	return (0);
7411 }
7412 
7413 int
7414 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7415 {
7416 	struct sockaddr_in *sin;
7417 	int fnd;
7418 	struct sockaddr_in *sin_a;
7419 	struct sctp_inpcb *inp;
7420 	struct sctp_tcb *stcb;
7421 	struct sctp_nets *net;
7422 
7423 	/* Do the malloc first in case it blocks. */
7424 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7425 	if (sin == NULL)
7426 		return (ENOMEM);
7427 	sin->sin_family = AF_INET;
7428 	sin->sin_len = sizeof(*sin);
7429 
7430 	inp = (struct sctp_inpcb *)so->so_pcb;
7431 	if ((inp == NULL) ||
7432 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7433 		/* UDP type and listeners will drop out here */
7434 		SCTP_FREE_SONAME(sin);
7435 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7436 		return (ENOTCONN);
7437 	}
7438 	SCTP_INP_RLOCK(inp);
7439 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7440 	if (stcb) {
7441 		SCTP_TCB_LOCK(stcb);
7442 	}
7443 	SCTP_INP_RUNLOCK(inp);
7444 	if (stcb == NULL) {
7445 		SCTP_FREE_SONAME(sin);
7446 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7447 		return (ECONNRESET);
7448 	}
7449 	fnd = 0;
7450 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7451 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7452 		if (sin_a->sin_family == AF_INET) {
7453 			fnd = 1;
7454 			sin->sin_port = stcb->rport;
7455 			sin->sin_addr = sin_a->sin_addr;
7456 			break;
7457 		}
7458 	}
7459 	SCTP_TCB_UNLOCK(stcb);
7460 	if (!fnd) {
7461 		/* No IPv4 address */
7462 		SCTP_FREE_SONAME(sin);
7463 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7464 		return (ENOENT);
7465 	}
7466 	(*addr) = (struct sockaddr *)sin;
7467 	return (0);
7468 }
7469 
7470 struct pr_usrreqs sctp_usrreqs = {
7471 	.pru_abort = sctp_abort,
7472 	.pru_accept = sctp_accept,
7473 	.pru_attach = sctp_attach,
7474 	.pru_bind = sctp_bind,
7475 	.pru_connect = sctp_connect,
7476 	.pru_control = in_control,
7477 	.pru_close = sctp_close,
7478 	.pru_detach = sctp_close,
7479 	.pru_sopoll = sopoll_generic,
7480 	.pru_flush = sctp_flush,
7481 	.pru_disconnect = sctp_disconnect,
7482 	.pru_listen = sctp_listen,
7483 	.pru_peeraddr = sctp_peeraddr,
7484 	.pru_send = sctp_sendm,
7485 	.pru_shutdown = sctp_shutdown,
7486 	.pru_sockaddr = sctp_ingetaddr,
7487 	.pru_sosend = sctp_sosend,
7488 	.pru_soreceive = sctp_soreceive
7489 };
7490 
7491 #endif
7492