xref: /freebsd/sys/netinet/sctp_usrreq.c (revision 282e23f07bf49b4e37aabdcc1c513a788db36d10)
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 #ifdef VIMAGE
93 static void
94 sctp_finish(void *unused __unused)
95 {
96 	sctp_pcb_finish();
97 }
98 VNET_SYSUNINIT(sctp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, sctp_finish, NULL);
99 #endif
100 
101 void
102 sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz)
103 {
104 	struct sctp_tmit_chunk *chk;
105 	uint16_t overhead;
106 
107 	/* Adjust that too */
108 	stcb->asoc.smallest_mtu = nxtsz;
109 	/* now off to subtract IP_DF flag if needed */
110 	overhead = IP_HDR_SIZE;
111 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
112 		overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
113 	}
114 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
115 		if ((chk->send_size + overhead) > nxtsz) {
116 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
117 		}
118 	}
119 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
120 		if ((chk->send_size + overhead) > nxtsz) {
121 			/*
122 			 * For this guy we also mark for immediate resend
123 			 * since we sent to big of chunk
124 			 */
125 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
126 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
127 				sctp_flight_size_decrease(chk);
128 				sctp_total_flight_decrease(stcb, chk);
129 				chk->sent = SCTP_DATAGRAM_RESEND;
130 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
131 				chk->rec.data.doing_fast_retransmit = 0;
132 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
133 					sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
134 					    chk->whoTo->flight_size,
135 					    chk->book_size,
136 					    (uint32_t) (uintptr_t) chk->whoTo,
137 					    chk->rec.data.TSN_seq);
138 				}
139 				/* Clear any time so NO RTT is being done */
140 				chk->do_rtt = 0;
141 			}
142 		}
143 	}
144 }
145 
146 #ifdef INET
147 void
148 sctp_notify(struct sctp_inpcb *inp,
149     struct sctp_tcb *stcb,
150     struct sctp_nets *net,
151     uint8_t icmp_type,
152     uint8_t icmp_code,
153     uint16_t ip_len,
154     uint16_t next_mtu)
155 {
156 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
157 	struct socket *so;
158 
159 #endif
160 	int timer_stopped;
161 
162 	if (icmp_type != ICMP_UNREACH) {
163 		/* We only care about unreachable */
164 		SCTP_TCB_UNLOCK(stcb);
165 		return;
166 	}
167 	if ((icmp_code == ICMP_UNREACH_NET) ||
168 	    (icmp_code == ICMP_UNREACH_HOST) ||
169 	    (icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
170 	    (icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
171 	    (icmp_code == ICMP_UNREACH_ISOLATED) ||
172 	    (icmp_code == ICMP_UNREACH_NET_PROHIB) ||
173 	    (icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
174 	    (icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
175 		/* Mark the net unreachable. */
176 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
177 			/* OK, that destination is NOT reachable. */
178 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
179 			net->dest_state &= ~SCTP_ADDR_PF;
180 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
181 			    stcb, 0,
182 			    (void *)net, SCTP_SO_NOT_LOCKED);
183 		}
184 		SCTP_TCB_UNLOCK(stcb);
185 	} else if ((icmp_code == ICMP_UNREACH_PROTOCOL) ||
186 	    (icmp_code == ICMP_UNREACH_PORT)) {
187 		/* Treat it like an ABORT. */
188 		sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
189 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
190 		so = SCTP_INP_SO(inp);
191 		atomic_add_int(&stcb->asoc.refcnt, 1);
192 		SCTP_TCB_UNLOCK(stcb);
193 		SCTP_SOCKET_LOCK(so, 1);
194 		SCTP_TCB_LOCK(stcb);
195 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
196 #endif
197 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
198 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
199 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
200 		SCTP_SOCKET_UNLOCK(so, 1);
201 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
202 #endif
203 		/* no need to unlock here, since the TCB is gone */
204 	} else if (icmp_code == ICMP_UNREACH_NEEDFRAG) {
205 		/* Find the next (smaller) MTU */
206 		if (next_mtu == 0) {
207 			/*
208 			 * Old type router that does not tell us what the
209 			 * next MTU is. Rats we will have to guess (in a
210 			 * educated fashion of course).
211 			 */
212 			next_mtu = sctp_get_prev_mtu(ip_len);
213 		}
214 		/* Stop the PMTU timer. */
215 		if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
216 			timer_stopped = 1;
217 			sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
218 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
219 		} else {
220 			timer_stopped = 0;
221 		}
222 		/* Update the path MTU. */
223 		if (net->mtu > next_mtu) {
224 			net->mtu = next_mtu;
225 			if (net->port) {
226 				net->mtu -= sizeof(struct udphdr);
227 			}
228 		}
229 		/* Update the association MTU */
230 		if (stcb->asoc.smallest_mtu > next_mtu) {
231 			sctp_pathmtu_adjustment(stcb, next_mtu);
232 		}
233 		/* Finally, start the PMTU timer if it was running before. */
234 		if (timer_stopped) {
235 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
236 		}
237 		SCTP_TCB_UNLOCK(stcb);
238 	} else {
239 		SCTP_TCB_UNLOCK(stcb);
240 	}
241 }
242 
243 void
244 sctp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
245 {
246 	struct ip *outer_ip;
247 	struct ip *inner_ip;
248 	struct sctphdr *sh;
249 	struct icmp *icmp;
250 	struct sctp_inpcb *inp;
251 	struct sctp_tcb *stcb;
252 	struct sctp_nets *net;
253 	struct sctp_init_chunk *ch;
254 	struct sockaddr_in src, dst;
255 
256 	if (sa->sa_family != AF_INET ||
257 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
258 		return;
259 	}
260 	if (PRC_IS_REDIRECT(cmd)) {
261 		vip = NULL;
262 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
263 		return;
264 	}
265 	if (vip != NULL) {
266 		inner_ip = (struct ip *)vip;
267 		icmp = (struct icmp *)((caddr_t)inner_ip -
268 		    (sizeof(struct icmp) - sizeof(struct ip)));
269 		outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
270 		sh = (struct sctphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
271 		memset(&src, 0, sizeof(struct sockaddr_in));
272 		src.sin_family = AF_INET;
273 		src.sin_len = sizeof(struct sockaddr_in);
274 		src.sin_port = sh->src_port;
275 		src.sin_addr = inner_ip->ip_src;
276 		memset(&dst, 0, sizeof(struct sockaddr_in));
277 		dst.sin_family = AF_INET;
278 		dst.sin_len = sizeof(struct sockaddr_in);
279 		dst.sin_port = sh->dest_port;
280 		dst.sin_addr = inner_ip->ip_dst;
281 		/*
282 		 * 'dst' holds the dest of the packet that failed to be
283 		 * sent. 'src' holds our local endpoint address. Thus we
284 		 * reverse the dst and the src in the lookup.
285 		 */
286 		inp = NULL;
287 		net = NULL;
288 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
289 		    (struct sockaddr *)&src,
290 		    &inp, &net, 1,
291 		    SCTP_DEFAULT_VRFID);
292 		if ((stcb != NULL) &&
293 		    (net != NULL) &&
294 		    (inp != 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 					thlds->spt_pathcpthld = 0xffff;
3308 				} else {
3309 					thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3310 					thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3311 					thlds->spt_pathcpthld = 0xffff;
3312 				}
3313 				thlds->spt_assoc_id = sctp_get_associd(stcb);
3314 				SCTP_TCB_UNLOCK(stcb);
3315 			} else {
3316 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3317 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3318 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3319 					/* Use endpoint defaults */
3320 					SCTP_INP_RLOCK(inp);
3321 					thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3322 					thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3323 					thlds->spt_pathcpthld = 0xffff;
3324 					SCTP_INP_RUNLOCK(inp);
3325 				} else {
3326 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3327 					error = EINVAL;
3328 				}
3329 			}
3330 			if (error == 0) {
3331 				*optsize = sizeof(struct sctp_paddrthlds);
3332 			}
3333 			break;
3334 		}
3335 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
3336 		{
3337 			struct sctp_udpencaps *encaps;
3338 			struct sctp_nets *net;
3339 			struct sockaddr *addr;
3340 
3341 #if defined(INET) && defined(INET6)
3342 			struct sockaddr_in sin_store;
3343 
3344 #endif
3345 
3346 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3347 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3348 
3349 #if defined(INET) && defined(INET6)
3350 			if (encaps->sue_address.ss_family == AF_INET6) {
3351 				struct sockaddr_in6 *sin6;
3352 
3353 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
3354 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3355 					in6_sin6_2_sin(&sin_store, sin6);
3356 					addr = (struct sockaddr *)&sin_store;
3357 				} else {
3358 					addr = (struct sockaddr *)&encaps->sue_address;
3359 				}
3360 			} else {
3361 				addr = (struct sockaddr *)&encaps->sue_address;
3362 			}
3363 #else
3364 			addr = (struct sockaddr *)&encaps->sue_address;
3365 #endif
3366 			if (stcb) {
3367 				net = sctp_findnet(stcb, addr);
3368 			} else {
3369 				/*
3370 				 * We increment here since
3371 				 * sctp_findassociation_ep_addr() wil do a
3372 				 * decrement if it finds the stcb as long as
3373 				 * the locked tcb (last argument) is NOT a
3374 				 * TCB.. aka NULL.
3375 				 */
3376 				net = NULL;
3377 				SCTP_INP_INCR_REF(inp);
3378 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3379 				if (stcb == NULL) {
3380 					SCTP_INP_DECR_REF(inp);
3381 				}
3382 			}
3383 			if ((stcb != NULL) && (net == NULL)) {
3384 #ifdef INET
3385 				if (addr->sa_family == AF_INET) {
3386 					struct sockaddr_in *sin;
3387 
3388 					sin = (struct sockaddr_in *)addr;
3389 					if (sin->sin_addr.s_addr != INADDR_ANY) {
3390 						error = EINVAL;
3391 						SCTP_TCB_UNLOCK(stcb);
3392 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3393 						break;
3394 					}
3395 				} else
3396 #endif
3397 #ifdef INET6
3398 				if (addr->sa_family == AF_INET6) {
3399 					struct sockaddr_in6 *sin6;
3400 
3401 					sin6 = (struct sockaddr_in6 *)addr;
3402 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3403 						error = EINVAL;
3404 						SCTP_TCB_UNLOCK(stcb);
3405 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3406 						break;
3407 					}
3408 				} else
3409 #endif
3410 				{
3411 					error = EAFNOSUPPORT;
3412 					SCTP_TCB_UNLOCK(stcb);
3413 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3414 					break;
3415 				}
3416 			}
3417 			if (stcb != NULL) {
3418 				if (net) {
3419 					encaps->sue_port = net->port;
3420 				} else {
3421 					encaps->sue_port = stcb->asoc.port;
3422 				}
3423 				SCTP_TCB_UNLOCK(stcb);
3424 			} else {
3425 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3426 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3427 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3428 					SCTP_INP_RLOCK(inp);
3429 					encaps->sue_port = inp->sctp_ep.port;
3430 					SCTP_INP_RUNLOCK(inp);
3431 				} else {
3432 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3433 					error = EINVAL;
3434 				}
3435 			}
3436 			if (error == 0) {
3437 				*optsize = sizeof(struct sctp_udpencaps);
3438 			}
3439 			break;
3440 		}
3441 	case SCTP_ECN_SUPPORTED:
3442 		{
3443 			struct sctp_assoc_value *av;
3444 
3445 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3446 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3447 
3448 			if (stcb) {
3449 				av->assoc_value = stcb->asoc.ecn_supported;
3450 				SCTP_TCB_UNLOCK(stcb);
3451 			} else {
3452 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3453 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3454 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3455 					SCTP_INP_RLOCK(inp);
3456 					av->assoc_value = inp->ecn_supported;
3457 					SCTP_INP_RUNLOCK(inp);
3458 				} else {
3459 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3460 					error = EINVAL;
3461 				}
3462 			}
3463 			if (error == 0) {
3464 				*optsize = sizeof(struct sctp_assoc_value);
3465 			}
3466 			break;
3467 		}
3468 	case SCTP_PR_SUPPORTED:
3469 		{
3470 			struct sctp_assoc_value *av;
3471 
3472 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3473 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3474 
3475 			if (stcb) {
3476 				av->assoc_value = stcb->asoc.prsctp_supported;
3477 				SCTP_TCB_UNLOCK(stcb);
3478 			} else {
3479 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3480 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3481 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3482 					SCTP_INP_RLOCK(inp);
3483 					av->assoc_value = inp->prsctp_supported;
3484 					SCTP_INP_RUNLOCK(inp);
3485 				} else {
3486 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3487 					error = EINVAL;
3488 				}
3489 			}
3490 			if (error == 0) {
3491 				*optsize = sizeof(struct sctp_assoc_value);
3492 			}
3493 			break;
3494 		}
3495 	case SCTP_AUTH_SUPPORTED:
3496 		{
3497 			struct sctp_assoc_value *av;
3498 
3499 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3500 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3501 
3502 			if (stcb) {
3503 				av->assoc_value = stcb->asoc.auth_supported;
3504 				SCTP_TCB_UNLOCK(stcb);
3505 			} else {
3506 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3507 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3508 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3509 					SCTP_INP_RLOCK(inp);
3510 					av->assoc_value = inp->auth_supported;
3511 					SCTP_INP_RUNLOCK(inp);
3512 				} else {
3513 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3514 					error = EINVAL;
3515 				}
3516 			}
3517 			if (error == 0) {
3518 				*optsize = sizeof(struct sctp_assoc_value);
3519 			}
3520 			break;
3521 		}
3522 	case SCTP_ASCONF_SUPPORTED:
3523 		{
3524 			struct sctp_assoc_value *av;
3525 
3526 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3527 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3528 
3529 			if (stcb) {
3530 				av->assoc_value = stcb->asoc.asconf_supported;
3531 				SCTP_TCB_UNLOCK(stcb);
3532 			} else {
3533 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3534 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3535 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3536 					SCTP_INP_RLOCK(inp);
3537 					av->assoc_value = inp->asconf_supported;
3538 					SCTP_INP_RUNLOCK(inp);
3539 				} else {
3540 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3541 					error = EINVAL;
3542 				}
3543 			}
3544 			if (error == 0) {
3545 				*optsize = sizeof(struct sctp_assoc_value);
3546 			}
3547 			break;
3548 		}
3549 	case SCTP_RECONFIG_SUPPORTED:
3550 		{
3551 			struct sctp_assoc_value *av;
3552 
3553 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3554 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3555 
3556 			if (stcb) {
3557 				av->assoc_value = stcb->asoc.reconfig_supported;
3558 				SCTP_TCB_UNLOCK(stcb);
3559 			} else {
3560 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3561 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3562 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3563 					SCTP_INP_RLOCK(inp);
3564 					av->assoc_value = inp->reconfig_supported;
3565 					SCTP_INP_RUNLOCK(inp);
3566 				} else {
3567 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3568 					error = EINVAL;
3569 				}
3570 			}
3571 			if (error == 0) {
3572 				*optsize = sizeof(struct sctp_assoc_value);
3573 			}
3574 			break;
3575 		}
3576 	case SCTP_NRSACK_SUPPORTED:
3577 		{
3578 			struct sctp_assoc_value *av;
3579 
3580 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3581 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3582 
3583 			if (stcb) {
3584 				av->assoc_value = stcb->asoc.nrsack_supported;
3585 				SCTP_TCB_UNLOCK(stcb);
3586 			} else {
3587 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3588 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3589 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3590 					SCTP_INP_RLOCK(inp);
3591 					av->assoc_value = inp->nrsack_supported;
3592 					SCTP_INP_RUNLOCK(inp);
3593 				} else {
3594 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3595 					error = EINVAL;
3596 				}
3597 			}
3598 			if (error == 0) {
3599 				*optsize = sizeof(struct sctp_assoc_value);
3600 			}
3601 			break;
3602 		}
3603 	case SCTP_PKTDROP_SUPPORTED:
3604 		{
3605 			struct sctp_assoc_value *av;
3606 
3607 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3608 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3609 
3610 			if (stcb) {
3611 				av->assoc_value = stcb->asoc.pktdrop_supported;
3612 				SCTP_TCB_UNLOCK(stcb);
3613 			} else {
3614 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3615 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3616 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3617 					SCTP_INP_RLOCK(inp);
3618 					av->assoc_value = inp->pktdrop_supported;
3619 					SCTP_INP_RUNLOCK(inp);
3620 				} else {
3621 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3622 					error = EINVAL;
3623 				}
3624 			}
3625 			if (error == 0) {
3626 				*optsize = sizeof(struct sctp_assoc_value);
3627 			}
3628 			break;
3629 		}
3630 	case SCTP_ENABLE_STREAM_RESET:
3631 		{
3632 			struct sctp_assoc_value *av;
3633 
3634 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3635 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3636 
3637 			if (stcb) {
3638 				av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support;
3639 				SCTP_TCB_UNLOCK(stcb);
3640 			} else {
3641 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3642 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3643 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3644 					SCTP_INP_RLOCK(inp);
3645 					av->assoc_value = (uint32_t) inp->local_strreset_support;
3646 					SCTP_INP_RUNLOCK(inp);
3647 				} else {
3648 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3649 					error = EINVAL;
3650 				}
3651 			}
3652 			if (error == 0) {
3653 				*optsize = sizeof(struct sctp_assoc_value);
3654 			}
3655 			break;
3656 		}
3657 	case SCTP_PR_STREAM_STATUS:
3658 		{
3659 			struct sctp_prstatus *sprstat;
3660 			uint16_t sid;
3661 			uint16_t policy;
3662 
3663 			SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3664 			SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3665 
3666 			sid = sprstat->sprstat_sid;
3667 			policy = sprstat->sprstat_policy;
3668 #if defined(SCTP_DETAILED_STR_STATS)
3669 			if ((stcb != NULL) &&
3670 			    (sid < stcb->asoc.streamoutcnt) &&
3671 			    (policy != SCTP_PR_SCTP_NONE) &&
3672 			    ((policy <= SCTP_PR_SCTP_MAX) ||
3673 			    (policy == SCTP_PR_SCTP_ALL))) {
3674 				if (policy == SCTP_PR_SCTP_ALL) {
3675 					sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3676 					sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3677 				} else {
3678 					sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
3679 					sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
3680 				}
3681 #else
3682 			if ((stcb != NULL) &&
3683 			    (sid < stcb->asoc.streamoutcnt) &&
3684 			    (policy == SCTP_PR_SCTP_ALL)) {
3685 				sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3686 				sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3687 #endif
3688 				SCTP_TCB_UNLOCK(stcb);
3689 				*optsize = sizeof(struct sctp_prstatus);
3690 			} else {
3691 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3692 				error = EINVAL;
3693 			}
3694 			break;
3695 		}
3696 	case SCTP_PR_ASSOC_STATUS:
3697 		{
3698 			struct sctp_prstatus *sprstat;
3699 			uint16_t policy;
3700 
3701 			SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3702 			SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3703 
3704 			policy = sprstat->sprstat_policy;
3705 			if ((stcb != NULL) &&
3706 			    (policy != SCTP_PR_SCTP_NONE) &&
3707 			    ((policy <= SCTP_PR_SCTP_MAX) ||
3708 			    (policy == SCTP_PR_SCTP_ALL))) {
3709 				if (policy == SCTP_PR_SCTP_ALL) {
3710 					sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
3711 					sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
3712 				} else {
3713 					sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
3714 					sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
3715 				}
3716 				SCTP_TCB_UNLOCK(stcb);
3717 				*optsize = sizeof(struct sctp_prstatus);
3718 			} else {
3719 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3720 				error = EINVAL;
3721 			}
3722 			break;
3723 		}
3724 	case SCTP_MAX_CWND:
3725 		{
3726 			struct sctp_assoc_value *av;
3727 
3728 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3729 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3730 
3731 			if (stcb) {
3732 				av->assoc_value = stcb->asoc.max_cwnd;
3733 				SCTP_TCB_UNLOCK(stcb);
3734 			} else {
3735 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3736 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3737 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3738 					SCTP_INP_RLOCK(inp);
3739 					av->assoc_value = inp->max_cwnd;
3740 					SCTP_INP_RUNLOCK(inp);
3741 				} else {
3742 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3743 					error = EINVAL;
3744 				}
3745 			}
3746 			if (error == 0) {
3747 				*optsize = sizeof(struct sctp_assoc_value);
3748 			}
3749 			break;
3750 		}
3751 	default:
3752 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3753 		error = ENOPROTOOPT;
3754 		break;
3755 	}			/* end switch (sopt->sopt_name) */
3756 	if (error) {
3757 		*optsize = 0;
3758 	}
3759 	return (error);
3760 }
3761 
3762 static int
3763 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3764     void *p)
3765 {
3766 	int error, set_opt;
3767 	uint32_t *mopt;
3768 	struct sctp_tcb *stcb = NULL;
3769 	struct sctp_inpcb *inp = NULL;
3770 	uint32_t vrf_id;
3771 
3772 	if (optval == NULL) {
3773 		SCTP_PRINTF("optval is NULL\n");
3774 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3775 		return (EINVAL);
3776 	}
3777 	inp = (struct sctp_inpcb *)so->so_pcb;
3778 	if (inp == NULL) {
3779 		SCTP_PRINTF("inp is NULL?\n");
3780 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3781 		return (EINVAL);
3782 	}
3783 	vrf_id = inp->def_vrf_id;
3784 
3785 	error = 0;
3786 	switch (optname) {
3787 	case SCTP_NODELAY:
3788 	case SCTP_AUTOCLOSE:
3789 	case SCTP_AUTO_ASCONF:
3790 	case SCTP_EXPLICIT_EOR:
3791 	case SCTP_DISABLE_FRAGMENTS:
3792 	case SCTP_USE_EXT_RCVINFO:
3793 	case SCTP_I_WANT_MAPPED_V4_ADDR:
3794 		/* copy in the option value */
3795 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3796 		set_opt = 0;
3797 		if (error)
3798 			break;
3799 		switch (optname) {
3800 		case SCTP_DISABLE_FRAGMENTS:
3801 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3802 			break;
3803 		case SCTP_AUTO_ASCONF:
3804 			/*
3805 			 * NOTE: we don't really support this flag
3806 			 */
3807 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3808 				/* only valid for bound all sockets */
3809 				if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3810 				    (*mopt != 0)) {
3811 					/* forbidden by admin */
3812 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3813 					return (EPERM);
3814 				}
3815 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3816 			} else {
3817 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3818 				return (EINVAL);
3819 			}
3820 			break;
3821 		case SCTP_EXPLICIT_EOR:
3822 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3823 			break;
3824 		case SCTP_USE_EXT_RCVINFO:
3825 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3826 			break;
3827 		case SCTP_I_WANT_MAPPED_V4_ADDR:
3828 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3829 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3830 			} else {
3831 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3832 				return (EINVAL);
3833 			}
3834 			break;
3835 		case SCTP_NODELAY:
3836 			set_opt = SCTP_PCB_FLAGS_NODELAY;
3837 			break;
3838 		case SCTP_AUTOCLOSE:
3839 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3840 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3841 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3842 				return (EINVAL);
3843 			}
3844 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3845 			/*
3846 			 * The value is in ticks. Note this does not effect
3847 			 * old associations, only new ones.
3848 			 */
3849 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3850 			break;
3851 		}
3852 		SCTP_INP_WLOCK(inp);
3853 		if (*mopt != 0) {
3854 			sctp_feature_on(inp, set_opt);
3855 		} else {
3856 			sctp_feature_off(inp, set_opt);
3857 		}
3858 		SCTP_INP_WUNLOCK(inp);
3859 		break;
3860 	case SCTP_REUSE_PORT:
3861 		{
3862 			SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3863 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3864 				/* Can't set it after we are bound */
3865 				error = EINVAL;
3866 				break;
3867 			}
3868 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3869 				/* Can't do this for a 1-m socket */
3870 				error = EINVAL;
3871 				break;
3872 			}
3873 			if (optval)
3874 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3875 			else
3876 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3877 			break;
3878 		}
3879 	case SCTP_PARTIAL_DELIVERY_POINT:
3880 		{
3881 			uint32_t *value;
3882 
3883 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3884 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
3885 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3886 				error = EINVAL;
3887 				break;
3888 			}
3889 			inp->partial_delivery_point = *value;
3890 			break;
3891 		}
3892 	case SCTP_FRAGMENT_INTERLEAVE:
3893 		/* not yet until we re-write sctp_recvmsg() */
3894 		{
3895 			uint32_t *level;
3896 
3897 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3898 			if (*level == SCTP_FRAG_LEVEL_2) {
3899 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3900 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3901 			} else if (*level == SCTP_FRAG_LEVEL_1) {
3902 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3903 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3904 			} else if (*level == SCTP_FRAG_LEVEL_0) {
3905 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3906 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3907 
3908 			} else {
3909 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3910 				error = EINVAL;
3911 			}
3912 			break;
3913 		}
3914 	case SCTP_INTERLEAVING_SUPPORTED:
3915 		{
3916 			struct sctp_assoc_value *av;
3917 
3918 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3919 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3920 
3921 			if (stcb) {
3922 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3923 				error = EINVAL;
3924 				SCTP_TCB_UNLOCK(stcb);
3925 			} else {
3926 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3927 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3928 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3929 					SCTP_INP_WLOCK(inp);
3930 					if (av->assoc_value == 0) {
3931 						inp->idata_supported = 0;
3932 					} else {
3933 						if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
3934 						    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS))) {
3935 							inp->idata_supported = 1;
3936 						} else {
3937 							/*
3938 							 * Must have Frag
3939 							 * interleave and
3940 							 * stream interleave
3941 							 * on
3942 							 */
3943 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3944 							error = EINVAL;
3945 						}
3946 					}
3947 					SCTP_INP_WUNLOCK(inp);
3948 				} else {
3949 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3950 					error = EINVAL;
3951 				}
3952 			}
3953 			break;
3954 		}
3955 	case SCTP_CMT_ON_OFF:
3956 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3957 			struct sctp_assoc_value *av;
3958 
3959 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3960 			if (av->assoc_value > SCTP_CMT_MAX) {
3961 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3962 				error = EINVAL;
3963 				break;
3964 			}
3965 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3966 			if (stcb) {
3967 				stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3968 				SCTP_TCB_UNLOCK(stcb);
3969 			} else {
3970 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3971 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3972 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3973 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3974 					SCTP_INP_WLOCK(inp);
3975 					inp->sctp_cmt_on_off = av->assoc_value;
3976 					SCTP_INP_WUNLOCK(inp);
3977 				}
3978 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3979 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3980 					SCTP_INP_RLOCK(inp);
3981 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3982 						SCTP_TCB_LOCK(stcb);
3983 						stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3984 						SCTP_TCB_UNLOCK(stcb);
3985 					}
3986 					SCTP_INP_RUNLOCK(inp);
3987 				}
3988 			}
3989 		} else {
3990 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3991 			error = ENOPROTOOPT;
3992 		}
3993 		break;
3994 	case SCTP_PLUGGABLE_CC:
3995 		{
3996 			struct sctp_assoc_value *av;
3997 			struct sctp_nets *net;
3998 
3999 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4000 			if ((av->assoc_value != SCTP_CC_RFC2581) &&
4001 			    (av->assoc_value != SCTP_CC_HSTCP) &&
4002 			    (av->assoc_value != SCTP_CC_HTCP) &&
4003 			    (av->assoc_value != SCTP_CC_RTCC)) {
4004 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4005 				error = EINVAL;
4006 				break;
4007 			}
4008 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4009 			if (stcb) {
4010 				stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4011 				stcb->asoc.congestion_control_module = av->assoc_value;
4012 				if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4013 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4014 						stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4015 					}
4016 				}
4017 				SCTP_TCB_UNLOCK(stcb);
4018 			} else {
4019 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4020 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4021 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4022 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4023 					SCTP_INP_WLOCK(inp);
4024 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
4025 					SCTP_INP_WUNLOCK(inp);
4026 				}
4027 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4028 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4029 					SCTP_INP_RLOCK(inp);
4030 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4031 						SCTP_TCB_LOCK(stcb);
4032 						stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4033 						stcb->asoc.congestion_control_module = av->assoc_value;
4034 						if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4035 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4036 								stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4037 							}
4038 						}
4039 						SCTP_TCB_UNLOCK(stcb);
4040 					}
4041 					SCTP_INP_RUNLOCK(inp);
4042 				}
4043 			}
4044 			break;
4045 		}
4046 	case SCTP_CC_OPTION:
4047 		{
4048 			struct sctp_cc_option *cc_opt;
4049 
4050 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
4051 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
4052 			if (stcb == NULL) {
4053 				if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
4054 					SCTP_INP_RLOCK(inp);
4055 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4056 						SCTP_TCB_LOCK(stcb);
4057 						if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
4058 							(*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
4059 						}
4060 						SCTP_TCB_UNLOCK(stcb);
4061 					}
4062 					SCTP_INP_RUNLOCK(inp);
4063 				} else {
4064 					error = EINVAL;
4065 				}
4066 			} else {
4067 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4068 					error = ENOTSUP;
4069 				} else {
4070 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4071 					    cc_opt);
4072 				}
4073 				SCTP_TCB_UNLOCK(stcb);
4074 			}
4075 			break;
4076 		}
4077 	case SCTP_PLUGGABLE_SS:
4078 		{
4079 			struct sctp_assoc_value *av;
4080 
4081 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4082 			if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4083 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4084 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4085 			    (av->assoc_value != SCTP_SS_PRIORITY) &&
4086 			    (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4087 			    (av->assoc_value != SCTP_SS_FIRST_COME)) {
4088 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4089 				error = EINVAL;
4090 				break;
4091 			}
4092 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4093 			if (stcb) {
4094 				stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4095 				stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4096 				stcb->asoc.stream_scheduling_module = av->assoc_value;
4097 				stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4098 				SCTP_TCB_UNLOCK(stcb);
4099 			} else {
4100 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4101 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4102 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4103 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4104 					SCTP_INP_WLOCK(inp);
4105 					inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4106 					SCTP_INP_WUNLOCK(inp);
4107 				}
4108 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4109 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4110 					SCTP_INP_RLOCK(inp);
4111 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4112 						SCTP_TCB_LOCK(stcb);
4113 						stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4114 						stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4115 						stcb->asoc.stream_scheduling_module = av->assoc_value;
4116 						stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4117 						SCTP_TCB_UNLOCK(stcb);
4118 					}
4119 					SCTP_INP_RUNLOCK(inp);
4120 				}
4121 			}
4122 			break;
4123 		}
4124 	case SCTP_SS_VALUE:
4125 		{
4126 			struct sctp_stream_value *av;
4127 
4128 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4129 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4130 			if (stcb) {
4131 				if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4132 				    (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4133 				    av->stream_value) < 0)) {
4134 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4135 					error = EINVAL;
4136 				}
4137 				SCTP_TCB_UNLOCK(stcb);
4138 			} else {
4139 				if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4140 					SCTP_INP_RLOCK(inp);
4141 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4142 						SCTP_TCB_LOCK(stcb);
4143 						if (av->stream_id < stcb->asoc.streamoutcnt) {
4144 							stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4145 							    &stcb->asoc,
4146 							    &stcb->asoc.strmout[av->stream_id],
4147 							    av->stream_value);
4148 						}
4149 						SCTP_TCB_UNLOCK(stcb);
4150 					}
4151 					SCTP_INP_RUNLOCK(inp);
4152 				} else {
4153 					/*
4154 					 * Can't set stream value without
4155 					 * association
4156 					 */
4157 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4158 					error = EINVAL;
4159 				}
4160 			}
4161 			break;
4162 		}
4163 	case SCTP_CLR_STAT_LOG:
4164 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4165 		error = EOPNOTSUPP;
4166 		break;
4167 	case SCTP_CONTEXT:
4168 		{
4169 			struct sctp_assoc_value *av;
4170 
4171 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4172 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4173 
4174 			if (stcb) {
4175 				stcb->asoc.context = av->assoc_value;
4176 				SCTP_TCB_UNLOCK(stcb);
4177 			} else {
4178 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4179 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4180 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4181 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4182 					SCTP_INP_WLOCK(inp);
4183 					inp->sctp_context = av->assoc_value;
4184 					SCTP_INP_WUNLOCK(inp);
4185 				}
4186 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4187 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4188 					SCTP_INP_RLOCK(inp);
4189 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4190 						SCTP_TCB_LOCK(stcb);
4191 						stcb->asoc.context = av->assoc_value;
4192 						SCTP_TCB_UNLOCK(stcb);
4193 					}
4194 					SCTP_INP_RUNLOCK(inp);
4195 				}
4196 			}
4197 			break;
4198 		}
4199 	case SCTP_VRF_ID:
4200 		{
4201 			uint32_t *default_vrfid;
4202 
4203 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4204 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
4205 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4206 				error = EINVAL;
4207 				break;
4208 			}
4209 			inp->def_vrf_id = *default_vrfid;
4210 			break;
4211 		}
4212 	case SCTP_DEL_VRF_ID:
4213 		{
4214 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4215 			error = EOPNOTSUPP;
4216 			break;
4217 		}
4218 	case SCTP_ADD_VRF_ID:
4219 		{
4220 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4221 			error = EOPNOTSUPP;
4222 			break;
4223 		}
4224 	case SCTP_DELAYED_SACK:
4225 		{
4226 			struct sctp_sack_info *sack;
4227 
4228 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4229 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4230 			if (sack->sack_delay) {
4231 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4232 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
4233 				if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4234 					sack->sack_delay = TICKS_TO_MSEC(1);
4235 				}
4236 			}
4237 			if (stcb) {
4238 				if (sack->sack_delay) {
4239 					stcb->asoc.delayed_ack = sack->sack_delay;
4240 				}
4241 				if (sack->sack_freq) {
4242 					stcb->asoc.sack_freq = sack->sack_freq;
4243 				}
4244 				SCTP_TCB_UNLOCK(stcb);
4245 			} else {
4246 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4247 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4248 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4249 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4250 					SCTP_INP_WLOCK(inp);
4251 					if (sack->sack_delay) {
4252 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4253 					}
4254 					if (sack->sack_freq) {
4255 						inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4256 					}
4257 					SCTP_INP_WUNLOCK(inp);
4258 				}
4259 				if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4260 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4261 					SCTP_INP_RLOCK(inp);
4262 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4263 						SCTP_TCB_LOCK(stcb);
4264 						if (sack->sack_delay) {
4265 							stcb->asoc.delayed_ack = sack->sack_delay;
4266 						}
4267 						if (sack->sack_freq) {
4268 							stcb->asoc.sack_freq = sack->sack_freq;
4269 						}
4270 						SCTP_TCB_UNLOCK(stcb);
4271 					}
4272 					SCTP_INP_RUNLOCK(inp);
4273 				}
4274 			}
4275 			break;
4276 		}
4277 	case SCTP_AUTH_CHUNK:
4278 		{
4279 			struct sctp_authchunk *sauth;
4280 
4281 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4282 
4283 			SCTP_INP_WLOCK(inp);
4284 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4285 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4286 				error = EINVAL;
4287 			}
4288 			SCTP_INP_WUNLOCK(inp);
4289 			break;
4290 		}
4291 	case SCTP_AUTH_KEY:
4292 		{
4293 			struct sctp_authkey *sca;
4294 			struct sctp_keyhead *shared_keys;
4295 			sctp_sharedkey_t *shared_key;
4296 			sctp_key_t *key = NULL;
4297 			size_t size;
4298 
4299 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4300 			if (sca->sca_keylength == 0) {
4301 				size = optsize - sizeof(struct sctp_authkey);
4302 			} else {
4303 				if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4304 					size = sca->sca_keylength;
4305 				} else {
4306 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4307 					error = EINVAL;
4308 					break;
4309 				}
4310 			}
4311 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4312 
4313 			if (stcb) {
4314 				shared_keys = &stcb->asoc.shared_keys;
4315 				/* clear the cached keys for this key id */
4316 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4317 				/*
4318 				 * create the new shared key and
4319 				 * insert/replace it
4320 				 */
4321 				if (size > 0) {
4322 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
4323 					if (key == NULL) {
4324 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4325 						error = ENOMEM;
4326 						SCTP_TCB_UNLOCK(stcb);
4327 						break;
4328 					}
4329 				}
4330 				shared_key = sctp_alloc_sharedkey();
4331 				if (shared_key == NULL) {
4332 					sctp_free_key(key);
4333 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4334 					error = ENOMEM;
4335 					SCTP_TCB_UNLOCK(stcb);
4336 					break;
4337 				}
4338 				shared_key->key = key;
4339 				shared_key->keyid = sca->sca_keynumber;
4340 				error = sctp_insert_sharedkey(shared_keys, shared_key);
4341 				SCTP_TCB_UNLOCK(stcb);
4342 			} else {
4343 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4344 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4345 				    (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4346 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4347 					SCTP_INP_WLOCK(inp);
4348 					shared_keys = &inp->sctp_ep.shared_keys;
4349 					/*
4350 					 * clear the cached keys on all
4351 					 * assocs for this key id
4352 					 */
4353 					sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4354 					/*
4355 					 * create the new shared key and
4356 					 * insert/replace it
4357 					 */
4358 					if (size > 0) {
4359 						key = sctp_set_key(sca->sca_key, (uint32_t) size);
4360 						if (key == NULL) {
4361 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4362 							error = ENOMEM;
4363 							SCTP_INP_WUNLOCK(inp);
4364 							break;
4365 						}
4366 					}
4367 					shared_key = sctp_alloc_sharedkey();
4368 					if (shared_key == NULL) {
4369 						sctp_free_key(key);
4370 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4371 						error = ENOMEM;
4372 						SCTP_INP_WUNLOCK(inp);
4373 						break;
4374 					}
4375 					shared_key->key = key;
4376 					shared_key->keyid = sca->sca_keynumber;
4377 					error = sctp_insert_sharedkey(shared_keys, shared_key);
4378 					SCTP_INP_WUNLOCK(inp);
4379 				}
4380 				if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4381 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4382 					SCTP_INP_RLOCK(inp);
4383 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4384 						SCTP_TCB_LOCK(stcb);
4385 						shared_keys = &stcb->asoc.shared_keys;
4386 						/*
4387 						 * clear the cached keys for
4388 						 * this key id
4389 						 */
4390 						sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4391 						/*
4392 						 * create the new shared key
4393 						 * and insert/replace it
4394 						 */
4395 						if (size > 0) {
4396 							key = sctp_set_key(sca->sca_key, (uint32_t) size);
4397 							if (key == NULL) {
4398 								SCTP_TCB_UNLOCK(stcb);
4399 								continue;
4400 							}
4401 						}
4402 						shared_key = sctp_alloc_sharedkey();
4403 						if (shared_key == NULL) {
4404 							sctp_free_key(key);
4405 							SCTP_TCB_UNLOCK(stcb);
4406 							continue;
4407 						}
4408 						shared_key->key = key;
4409 						shared_key->keyid = sca->sca_keynumber;
4410 						error = sctp_insert_sharedkey(shared_keys, shared_key);
4411 						SCTP_TCB_UNLOCK(stcb);
4412 					}
4413 					SCTP_INP_RUNLOCK(inp);
4414 				}
4415 			}
4416 			break;
4417 		}
4418 	case SCTP_HMAC_IDENT:
4419 		{
4420 			struct sctp_hmacalgo *shmac;
4421 			sctp_hmaclist_t *hmaclist;
4422 			uint16_t hmacid;
4423 			uint32_t i;
4424 
4425 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4426 			if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4427 			    (shmac->shmac_number_of_idents > 0xffff)) {
4428 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4429 				error = EINVAL;
4430 				break;
4431 			}
4432 			hmaclist = sctp_alloc_hmaclist((uint16_t) shmac->shmac_number_of_idents);
4433 			if (hmaclist == NULL) {
4434 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4435 				error = ENOMEM;
4436 				break;
4437 			}
4438 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4439 				hmacid = shmac->shmac_idents[i];
4440 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4441 					 /* invalid HMACs were found */ ;
4442 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4443 					error = EINVAL;
4444 					sctp_free_hmaclist(hmaclist);
4445 					goto sctp_set_hmac_done;
4446 				}
4447 			}
4448 			for (i = 0; i < hmaclist->num_algo; i++) {
4449 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4450 					/* already in list */
4451 					break;
4452 				}
4453 			}
4454 			if (i == hmaclist->num_algo) {
4455 				/* not found in list */
4456 				sctp_free_hmaclist(hmaclist);
4457 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4458 				error = EINVAL;
4459 				break;
4460 			}
4461 			/* set it on the endpoint */
4462 			SCTP_INP_WLOCK(inp);
4463 			if (inp->sctp_ep.local_hmacs)
4464 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4465 			inp->sctp_ep.local_hmacs = hmaclist;
4466 			SCTP_INP_WUNLOCK(inp);
4467 	sctp_set_hmac_done:
4468 			break;
4469 		}
4470 	case SCTP_AUTH_ACTIVE_KEY:
4471 		{
4472 			struct sctp_authkeyid *scact;
4473 
4474 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4475 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4476 
4477 			/* set the active key on the right place */
4478 			if (stcb) {
4479 				/* set the active key on the assoc */
4480 				if (sctp_auth_setactivekey(stcb,
4481 				    scact->scact_keynumber)) {
4482 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4483 					    SCTP_FROM_SCTP_USRREQ,
4484 					    EINVAL);
4485 					error = EINVAL;
4486 				}
4487 				SCTP_TCB_UNLOCK(stcb);
4488 			} else {
4489 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4490 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4491 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4492 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4493 					SCTP_INP_WLOCK(inp);
4494 					if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4495 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4496 						error = EINVAL;
4497 					}
4498 					SCTP_INP_WUNLOCK(inp);
4499 				}
4500 				if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4501 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4502 					SCTP_INP_RLOCK(inp);
4503 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4504 						SCTP_TCB_LOCK(stcb);
4505 						sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4506 						SCTP_TCB_UNLOCK(stcb);
4507 					}
4508 					SCTP_INP_RUNLOCK(inp);
4509 				}
4510 			}
4511 			break;
4512 		}
4513 	case SCTP_AUTH_DELETE_KEY:
4514 		{
4515 			struct sctp_authkeyid *scdel;
4516 
4517 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4518 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4519 
4520 			/* delete the key from the right place */
4521 			if (stcb) {
4522 				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4523 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4524 					error = EINVAL;
4525 				}
4526 				SCTP_TCB_UNLOCK(stcb);
4527 			} else {
4528 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4529 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4530 				    (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4531 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4532 					SCTP_INP_WLOCK(inp);
4533 					if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4534 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4535 						error = EINVAL;
4536 					}
4537 					SCTP_INP_WUNLOCK(inp);
4538 				}
4539 				if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4540 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4541 					SCTP_INP_RLOCK(inp);
4542 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4543 						SCTP_TCB_LOCK(stcb);
4544 						sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4545 						SCTP_TCB_UNLOCK(stcb);
4546 					}
4547 					SCTP_INP_RUNLOCK(inp);
4548 				}
4549 			}
4550 			break;
4551 		}
4552 	case SCTP_AUTH_DEACTIVATE_KEY:
4553 		{
4554 			struct sctp_authkeyid *keyid;
4555 
4556 			SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4557 			SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4558 
4559 			/* deactivate the key from the right place */
4560 			if (stcb) {
4561 				if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4562 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4563 					error = EINVAL;
4564 				}
4565 				SCTP_TCB_UNLOCK(stcb);
4566 			} else {
4567 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4568 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4569 				    (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4570 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4571 					SCTP_INP_WLOCK(inp);
4572 					if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4573 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4574 						error = EINVAL;
4575 					}
4576 					SCTP_INP_WUNLOCK(inp);
4577 				}
4578 				if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4579 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4580 					SCTP_INP_RLOCK(inp);
4581 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4582 						SCTP_TCB_LOCK(stcb);
4583 						sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4584 						SCTP_TCB_UNLOCK(stcb);
4585 					}
4586 					SCTP_INP_RUNLOCK(inp);
4587 				}
4588 			}
4589 			break;
4590 		}
4591 	case SCTP_ENABLE_STREAM_RESET:
4592 		{
4593 			struct sctp_assoc_value *av;
4594 
4595 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4596 			if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4597 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4598 				error = EINVAL;
4599 				break;
4600 			}
4601 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4602 			if (stcb) {
4603 				stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4604 				SCTP_TCB_UNLOCK(stcb);
4605 			} else {
4606 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4607 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4608 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4609 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4610 					SCTP_INP_WLOCK(inp);
4611 					inp->local_strreset_support = (uint8_t) av->assoc_value;
4612 					SCTP_INP_WUNLOCK(inp);
4613 				}
4614 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4615 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4616 					SCTP_INP_RLOCK(inp);
4617 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4618 						SCTP_TCB_LOCK(stcb);
4619 						stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4620 						SCTP_TCB_UNLOCK(stcb);
4621 					}
4622 					SCTP_INP_RUNLOCK(inp);
4623 				}
4624 			}
4625 			break;
4626 		}
4627 	case SCTP_RESET_STREAMS:
4628 		{
4629 			struct sctp_reset_streams *strrst;
4630 			int i, send_out = 0;
4631 			int send_in = 0;
4632 
4633 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4634 			SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4635 			if (stcb == NULL) {
4636 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4637 				error = ENOENT;
4638 				break;
4639 			}
4640 			if (stcb->asoc.reconfig_supported == 0) {
4641 				/*
4642 				 * Peer does not support the chunk type.
4643 				 */
4644 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4645 				error = EOPNOTSUPP;
4646 				SCTP_TCB_UNLOCK(stcb);
4647 				break;
4648 			}
4649 			if (sizeof(struct sctp_reset_streams) +
4650 			    strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4651 				error = EINVAL;
4652 				SCTP_TCB_UNLOCK(stcb);
4653 				break;
4654 			}
4655 			if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4656 				send_in = 1;
4657 				if (stcb->asoc.stream_reset_outstanding) {
4658 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4659 					error = EALREADY;
4660 					SCTP_TCB_UNLOCK(stcb);
4661 					break;
4662 				}
4663 			}
4664 			if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4665 				send_out = 1;
4666 			}
4667 			if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4668 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4669 				error = ENOMEM;
4670 				SCTP_TCB_UNLOCK(stcb);
4671 				break;
4672 			}
4673 			if ((send_in == 0) && (send_out == 0)) {
4674 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4675 				error = EINVAL;
4676 				SCTP_TCB_UNLOCK(stcb);
4677 				break;
4678 			}
4679 			for (i = 0; i < strrst->srs_number_streams; i++) {
4680 				if ((send_in) &&
4681 				    (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4682 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4683 					error = EINVAL;
4684 					break;
4685 				}
4686 				if ((send_out) &&
4687 				    (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4688 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4689 					error = EINVAL;
4690 					break;
4691 				}
4692 			}
4693 			if (error) {
4694 				SCTP_TCB_UNLOCK(stcb);
4695 				break;
4696 			}
4697 			if (send_out) {
4698 				int cnt;
4699 				uint16_t strm;
4700 
4701 				if (strrst->srs_number_streams) {
4702 					for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4703 						strm = strrst->srs_stream_list[i];
4704 						if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4705 							stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4706 							cnt++;
4707 						}
4708 					}
4709 				} else {
4710 					/* Its all */
4711 					for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4712 						if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4713 							stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4714 							cnt++;
4715 						}
4716 					}
4717 				}
4718 			}
4719 			if (send_in) {
4720 				error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4721 				    strrst->srs_stream_list,
4722 				    send_in, 0, 0, 0, 0, 0);
4723 			} else {
4724 				error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4725 			}
4726 			if (error == 0) {
4727 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4728 			} else {
4729 				/*
4730 				 * For outgoing streams don't report any
4731 				 * problems in sending the request to the
4732 				 * application. XXX: Double check resetting
4733 				 * incoming streams.
4734 				 */
4735 				error = 0;
4736 			}
4737 			SCTP_TCB_UNLOCK(stcb);
4738 			break;
4739 		}
4740 	case SCTP_ADD_STREAMS:
4741 		{
4742 			struct sctp_add_streams *stradd;
4743 			uint8_t addstream = 0;
4744 			uint16_t add_o_strmcnt = 0;
4745 			uint16_t add_i_strmcnt = 0;
4746 
4747 			SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4748 			SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4749 			if (stcb == NULL) {
4750 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4751 				error = ENOENT;
4752 				break;
4753 			}
4754 			if (stcb->asoc.reconfig_supported == 0) {
4755 				/*
4756 				 * Peer does not support the chunk type.
4757 				 */
4758 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4759 				error = EOPNOTSUPP;
4760 				SCTP_TCB_UNLOCK(stcb);
4761 				break;
4762 			}
4763 			if (stcb->asoc.stream_reset_outstanding) {
4764 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4765 				error = EALREADY;
4766 				SCTP_TCB_UNLOCK(stcb);
4767 				break;
4768 			}
4769 			if ((stradd->sas_outstrms == 0) &&
4770 			    (stradd->sas_instrms == 0)) {
4771 				error = EINVAL;
4772 				goto skip_stuff;
4773 			}
4774 			if (stradd->sas_outstrms) {
4775 				addstream = 1;
4776 				/* We allocate here */
4777 				add_o_strmcnt = stradd->sas_outstrms;
4778 				if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4779 					/* You can't have more than 64k */
4780 					error = EINVAL;
4781 					goto skip_stuff;
4782 				}
4783 			}
4784 			if (stradd->sas_instrms) {
4785 				int cnt;
4786 
4787 				addstream |= 2;
4788 				/*
4789 				 * We allocate inside
4790 				 * sctp_send_str_reset_req()
4791 				 */
4792 				add_i_strmcnt = stradd->sas_instrms;
4793 				cnt = add_i_strmcnt;
4794 				cnt += stcb->asoc.streamincnt;
4795 				if (cnt > 0x0000ffff) {
4796 					/* You can't have more than 64k */
4797 					error = EINVAL;
4798 					goto skip_stuff;
4799 				}
4800 				if (cnt > (int)stcb->asoc.max_inbound_streams) {
4801 					/* More than you are allowed */
4802 					error = EINVAL;
4803 					goto skip_stuff;
4804 				}
4805 			}
4806 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4807 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4808 	skip_stuff:
4809 			SCTP_TCB_UNLOCK(stcb);
4810 			break;
4811 		}
4812 	case SCTP_RESET_ASSOC:
4813 		{
4814 			int i;
4815 			uint32_t *value;
4816 
4817 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4818 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
4819 			if (stcb == NULL) {
4820 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4821 				error = ENOENT;
4822 				break;
4823 			}
4824 			if (stcb->asoc.reconfig_supported == 0) {
4825 				/*
4826 				 * Peer does not support the chunk type.
4827 				 */
4828 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4829 				error = EOPNOTSUPP;
4830 				SCTP_TCB_UNLOCK(stcb);
4831 				break;
4832 			}
4833 			if (stcb->asoc.stream_reset_outstanding) {
4834 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4835 				error = EALREADY;
4836 				SCTP_TCB_UNLOCK(stcb);
4837 				break;
4838 			}
4839 			/*
4840 			 * Is there any data pending in the send or sent
4841 			 * queues?
4842 			 */
4843 			if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4844 			    !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4845 		busy_out:
4846 				error = EBUSY;
4847 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4848 				SCTP_TCB_UNLOCK(stcb);
4849 				break;
4850 			}
4851 			/* Do any streams have data queued? */
4852 			for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4853 				if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4854 					goto busy_out;
4855 				}
4856 			}
4857 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4858 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4859 			SCTP_TCB_UNLOCK(stcb);
4860 			break;
4861 		}
4862 	case SCTP_CONNECT_X:
4863 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4864 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4865 			error = EINVAL;
4866 			break;
4867 		}
4868 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4869 		break;
4870 	case SCTP_CONNECT_X_DELAYED:
4871 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4872 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4873 			error = EINVAL;
4874 			break;
4875 		}
4876 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4877 		break;
4878 	case SCTP_CONNECT_X_COMPLETE:
4879 		{
4880 			struct sockaddr *sa;
4881 
4882 			/* FIXME MT: check correct? */
4883 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4884 
4885 			/* find tcb */
4886 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4887 				SCTP_INP_RLOCK(inp);
4888 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
4889 				if (stcb) {
4890 					SCTP_TCB_LOCK(stcb);
4891 				}
4892 				SCTP_INP_RUNLOCK(inp);
4893 			} else {
4894 				/*
4895 				 * We increment here since
4896 				 * sctp_findassociation_ep_addr() wil do a
4897 				 * decrement if it finds the stcb as long as
4898 				 * the locked tcb (last argument) is NOT a
4899 				 * TCB.. aka NULL.
4900 				 */
4901 				SCTP_INP_INCR_REF(inp);
4902 				stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4903 				if (stcb == NULL) {
4904 					SCTP_INP_DECR_REF(inp);
4905 				}
4906 			}
4907 
4908 			if (stcb == NULL) {
4909 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4910 				error = ENOENT;
4911 				break;
4912 			}
4913 			if (stcb->asoc.delayed_connection == 1) {
4914 				stcb->asoc.delayed_connection = 0;
4915 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4916 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4917 				    stcb->asoc.primary_destination,
4918 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4919 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4920 			} else {
4921 				/*
4922 				 * already expired or did not use delayed
4923 				 * connectx
4924 				 */
4925 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4926 				error = EALREADY;
4927 			}
4928 			SCTP_TCB_UNLOCK(stcb);
4929 			break;
4930 		}
4931 	case SCTP_MAX_BURST:
4932 		{
4933 			struct sctp_assoc_value *av;
4934 
4935 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4936 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4937 
4938 			if (stcb) {
4939 				stcb->asoc.max_burst = av->assoc_value;
4940 				SCTP_TCB_UNLOCK(stcb);
4941 			} else {
4942 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4943 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4944 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4945 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4946 					SCTP_INP_WLOCK(inp);
4947 					inp->sctp_ep.max_burst = av->assoc_value;
4948 					SCTP_INP_WUNLOCK(inp);
4949 				}
4950 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4951 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4952 					SCTP_INP_RLOCK(inp);
4953 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4954 						SCTP_TCB_LOCK(stcb);
4955 						stcb->asoc.max_burst = av->assoc_value;
4956 						SCTP_TCB_UNLOCK(stcb);
4957 					}
4958 					SCTP_INP_RUNLOCK(inp);
4959 				}
4960 			}
4961 			break;
4962 		}
4963 	case SCTP_MAXSEG:
4964 		{
4965 			struct sctp_assoc_value *av;
4966 			int ovh;
4967 
4968 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4969 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4970 
4971 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4972 				ovh = SCTP_MED_OVERHEAD;
4973 			} else {
4974 				ovh = SCTP_MED_V4_OVERHEAD;
4975 			}
4976 			if (stcb) {
4977 				if (av->assoc_value) {
4978 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4979 				} else {
4980 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4981 				}
4982 				SCTP_TCB_UNLOCK(stcb);
4983 			} else {
4984 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4985 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4986 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4987 					SCTP_INP_WLOCK(inp);
4988 					/*
4989 					 * FIXME MT: I think this is not in
4990 					 * tune with the API ID
4991 					 */
4992 					if (av->assoc_value) {
4993 						inp->sctp_frag_point = (av->assoc_value + ovh);
4994 					} else {
4995 						inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4996 					}
4997 					SCTP_INP_WUNLOCK(inp);
4998 				} else {
4999 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5000 					error = EINVAL;
5001 				}
5002 			}
5003 			break;
5004 		}
5005 	case SCTP_EVENTS:
5006 		{
5007 			struct sctp_event_subscribe *events;
5008 
5009 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
5010 
5011 			SCTP_INP_WLOCK(inp);
5012 			if (events->sctp_data_io_event) {
5013 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5014 			} else {
5015 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5016 			}
5017 
5018 			if (events->sctp_association_event) {
5019 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5020 			} else {
5021 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5022 			}
5023 
5024 			if (events->sctp_address_event) {
5025 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5026 			} else {
5027 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5028 			}
5029 
5030 			if (events->sctp_send_failure_event) {
5031 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5032 			} else {
5033 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5034 			}
5035 
5036 			if (events->sctp_peer_error_event) {
5037 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5038 			} else {
5039 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5040 			}
5041 
5042 			if (events->sctp_shutdown_event) {
5043 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5044 			} else {
5045 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5046 			}
5047 
5048 			if (events->sctp_partial_delivery_event) {
5049 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5050 			} else {
5051 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5052 			}
5053 
5054 			if (events->sctp_adaptation_layer_event) {
5055 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5056 			} else {
5057 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5058 			}
5059 
5060 			if (events->sctp_authentication_event) {
5061 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5062 			} else {
5063 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5064 			}
5065 
5066 			if (events->sctp_sender_dry_event) {
5067 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5068 			} else {
5069 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5070 			}
5071 
5072 			if (events->sctp_stream_reset_event) {
5073 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5074 			} else {
5075 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5076 			}
5077 			SCTP_INP_WUNLOCK(inp);
5078 
5079 			SCTP_INP_RLOCK(inp);
5080 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5081 				SCTP_TCB_LOCK(stcb);
5082 				if (events->sctp_association_event) {
5083 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5084 				} else {
5085 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5086 				}
5087 				if (events->sctp_address_event) {
5088 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5089 				} else {
5090 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5091 				}
5092 				if (events->sctp_send_failure_event) {
5093 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5094 				} else {
5095 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5096 				}
5097 				if (events->sctp_peer_error_event) {
5098 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5099 				} else {
5100 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5101 				}
5102 				if (events->sctp_shutdown_event) {
5103 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5104 				} else {
5105 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5106 				}
5107 				if (events->sctp_partial_delivery_event) {
5108 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5109 				} else {
5110 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5111 				}
5112 				if (events->sctp_adaptation_layer_event) {
5113 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5114 				} else {
5115 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5116 				}
5117 				if (events->sctp_authentication_event) {
5118 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5119 				} else {
5120 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5121 				}
5122 				if (events->sctp_sender_dry_event) {
5123 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5124 				} else {
5125 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5126 				}
5127 				if (events->sctp_stream_reset_event) {
5128 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5129 				} else {
5130 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5131 				}
5132 				SCTP_TCB_UNLOCK(stcb);
5133 			}
5134 			/*
5135 			 * Send up the sender dry event only for 1-to-1
5136 			 * style sockets.
5137 			 */
5138 			if (events->sctp_sender_dry_event) {
5139 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5140 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5141 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
5142 					if (stcb) {
5143 						SCTP_TCB_LOCK(stcb);
5144 						if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5145 						    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5146 						    (stcb->asoc.stream_queue_cnt == 0)) {
5147 							sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5148 						}
5149 						SCTP_TCB_UNLOCK(stcb);
5150 					}
5151 				}
5152 			}
5153 			SCTP_INP_RUNLOCK(inp);
5154 			break;
5155 		}
5156 	case SCTP_ADAPTATION_LAYER:
5157 		{
5158 			struct sctp_setadaptation *adap_bits;
5159 
5160 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5161 			SCTP_INP_WLOCK(inp);
5162 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5163 			inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5164 			SCTP_INP_WUNLOCK(inp);
5165 			break;
5166 		}
5167 #ifdef SCTP_DEBUG
5168 	case SCTP_SET_INITIAL_DBG_SEQ:
5169 		{
5170 			uint32_t *vvv;
5171 
5172 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5173 			SCTP_INP_WLOCK(inp);
5174 			inp->sctp_ep.initial_sequence_debug = *vvv;
5175 			SCTP_INP_WUNLOCK(inp);
5176 			break;
5177 		}
5178 #endif
5179 	case SCTP_DEFAULT_SEND_PARAM:
5180 		{
5181 			struct sctp_sndrcvinfo *s_info;
5182 
5183 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5184 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5185 
5186 			if (stcb) {
5187 				if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5188 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5189 				} else {
5190 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5191 					error = EINVAL;
5192 				}
5193 				SCTP_TCB_UNLOCK(stcb);
5194 			} else {
5195 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5196 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5197 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5198 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5199 					SCTP_INP_WLOCK(inp);
5200 					memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5201 					SCTP_INP_WUNLOCK(inp);
5202 				}
5203 				if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5204 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5205 					SCTP_INP_RLOCK(inp);
5206 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5207 						SCTP_TCB_LOCK(stcb);
5208 						if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5209 							memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5210 						}
5211 						SCTP_TCB_UNLOCK(stcb);
5212 					}
5213 					SCTP_INP_RUNLOCK(inp);
5214 				}
5215 			}
5216 			break;
5217 		}
5218 	case SCTP_PEER_ADDR_PARAMS:
5219 		{
5220 			struct sctp_paddrparams *paddrp;
5221 			struct sctp_nets *net;
5222 			struct sockaddr *addr;
5223 
5224 #if defined(INET) && defined(INET6)
5225 			struct sockaddr_in sin_store;
5226 
5227 #endif
5228 
5229 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5230 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5231 
5232 #if defined(INET) && defined(INET6)
5233 			if (paddrp->spp_address.ss_family == AF_INET6) {
5234 				struct sockaddr_in6 *sin6;
5235 
5236 				sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5237 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5238 					in6_sin6_2_sin(&sin_store, sin6);
5239 					addr = (struct sockaddr *)&sin_store;
5240 				} else {
5241 					addr = (struct sockaddr *)&paddrp->spp_address;
5242 				}
5243 			} else {
5244 				addr = (struct sockaddr *)&paddrp->spp_address;
5245 			}
5246 #else
5247 			addr = (struct sockaddr *)&paddrp->spp_address;
5248 #endif
5249 			if (stcb != NULL) {
5250 				net = sctp_findnet(stcb, addr);
5251 			} else {
5252 				/*
5253 				 * We increment here since
5254 				 * sctp_findassociation_ep_addr() wil do a
5255 				 * decrement if it finds the stcb as long as
5256 				 * the locked tcb (last argument) is NOT a
5257 				 * TCB.. aka NULL.
5258 				 */
5259 				net = NULL;
5260 				SCTP_INP_INCR_REF(inp);
5261 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5262 				    &net, NULL, NULL);
5263 				if (stcb == NULL) {
5264 					SCTP_INP_DECR_REF(inp);
5265 				}
5266 			}
5267 			if ((stcb != NULL) && (net == NULL)) {
5268 #ifdef INET
5269 				if (addr->sa_family == AF_INET) {
5270 
5271 					struct sockaddr_in *sin;
5272 
5273 					sin = (struct sockaddr_in *)addr;
5274 					if (sin->sin_addr.s_addr != INADDR_ANY) {
5275 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5276 						SCTP_TCB_UNLOCK(stcb);
5277 						error = EINVAL;
5278 						break;
5279 					}
5280 				} else
5281 #endif
5282 #ifdef INET6
5283 				if (addr->sa_family == AF_INET6) {
5284 					struct sockaddr_in6 *sin6;
5285 
5286 					sin6 = (struct sockaddr_in6 *)addr;
5287 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5288 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5289 						SCTP_TCB_UNLOCK(stcb);
5290 						error = EINVAL;
5291 						break;
5292 					}
5293 				} else
5294 #endif
5295 				{
5296 					error = EAFNOSUPPORT;
5297 					SCTP_TCB_UNLOCK(stcb);
5298 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5299 					break;
5300 				}
5301 			}
5302 			/* sanity checks */
5303 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5304 				if (stcb)
5305 					SCTP_TCB_UNLOCK(stcb);
5306 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5307 				return (EINVAL);
5308 			}
5309 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5310 				if (stcb)
5311 					SCTP_TCB_UNLOCK(stcb);
5312 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5313 				return (EINVAL);
5314 			}
5315 			if (stcb != NULL) {
5316 				/************************TCB SPECIFIC SET ******************/
5317 				if (net != NULL) {
5318 					/************************NET SPECIFIC SET ******************/
5319 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5320 						if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5321 						    !(net->dest_state & SCTP_ADDR_NOHB)) {
5322 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5323 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5324 						}
5325 						net->dest_state |= SCTP_ADDR_NOHB;
5326 					}
5327 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5328 						if (paddrp->spp_hbinterval) {
5329 							net->heart_beat_delay = paddrp->spp_hbinterval;
5330 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5331 							net->heart_beat_delay = 0;
5332 						}
5333 						sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5334 						    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5335 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5336 						net->dest_state &= ~SCTP_ADDR_NOHB;
5337 					}
5338 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
5339 						/* on demand HB */
5340 						sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5341 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5342 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5343 					}
5344 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5345 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5346 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5347 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5348 						}
5349 						net->dest_state |= SCTP_ADDR_NO_PMTUD;
5350 						net->mtu = paddrp->spp_pathmtu;
5351 						switch (net->ro._l_addr.sa.sa_family) {
5352 #ifdef INET
5353 						case AF_INET:
5354 							net->mtu += SCTP_MIN_V4_OVERHEAD;
5355 							break;
5356 #endif
5357 #ifdef INET6
5358 						case AF_INET6:
5359 							net->mtu += SCTP_MIN_OVERHEAD;
5360 							break;
5361 #endif
5362 						default:
5363 							break;
5364 						}
5365 						if (net->mtu < stcb->asoc.smallest_mtu) {
5366 							sctp_pathmtu_adjustment(stcb, net->mtu);
5367 						}
5368 					}
5369 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5370 						if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5371 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5372 						}
5373 						net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5374 					}
5375 					if (paddrp->spp_pathmaxrxt) {
5376 						if (net->dest_state & SCTP_ADDR_PF) {
5377 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5378 								net->dest_state &= ~SCTP_ADDR_PF;
5379 							}
5380 						} else {
5381 							if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5382 							    (net->error_count > net->pf_threshold)) {
5383 								net->dest_state |= SCTP_ADDR_PF;
5384 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5385 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5386 								    stcb->sctp_ep, stcb, net,
5387 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5388 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5389 							}
5390 						}
5391 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
5392 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5393 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
5394 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5395 							}
5396 						} else {
5397 							if (net->error_count <= paddrp->spp_pathmaxrxt) {
5398 								net->dest_state |= SCTP_ADDR_REACHABLE;
5399 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5400 							}
5401 						}
5402 						net->failure_threshold = paddrp->spp_pathmaxrxt;
5403 					}
5404 					if (paddrp->spp_flags & SPP_DSCP) {
5405 						net->dscp = paddrp->spp_dscp & 0xfc;
5406 						net->dscp |= 0x01;
5407 					}
5408 #ifdef INET6
5409 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5410 						if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5411 							net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5412 							net->flowlabel |= 0x80000000;
5413 						}
5414 					}
5415 #endif
5416 				} else {
5417 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5418 					if (paddrp->spp_pathmaxrxt != 0) {
5419 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5420 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5421 							if (net->dest_state & SCTP_ADDR_PF) {
5422 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5423 									net->dest_state &= ~SCTP_ADDR_PF;
5424 								}
5425 							} else {
5426 								if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5427 								    (net->error_count > net->pf_threshold)) {
5428 									net->dest_state |= SCTP_ADDR_PF;
5429 									sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5430 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5431 									    stcb->sctp_ep, stcb, net,
5432 									    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5433 									sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5434 								}
5435 							}
5436 							if (net->dest_state & SCTP_ADDR_REACHABLE) {
5437 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5438 									net->dest_state &= ~SCTP_ADDR_REACHABLE;
5439 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5440 								}
5441 							} else {
5442 								if (net->error_count <= paddrp->spp_pathmaxrxt) {
5443 									net->dest_state |= SCTP_ADDR_REACHABLE;
5444 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5445 								}
5446 							}
5447 							net->failure_threshold = paddrp->spp_pathmaxrxt;
5448 						}
5449 					}
5450 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5451 						if (paddrp->spp_hbinterval != 0) {
5452 							stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5453 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5454 							stcb->asoc.heart_beat_delay = 0;
5455 						}
5456 						/* Turn back on the timer */
5457 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5458 							if (paddrp->spp_hbinterval != 0) {
5459 								net->heart_beat_delay = paddrp->spp_hbinterval;
5460 							} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5461 								net->heart_beat_delay = 0;
5462 							}
5463 							if (net->dest_state & SCTP_ADDR_NOHB) {
5464 								net->dest_state &= ~SCTP_ADDR_NOHB;
5465 							}
5466 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5467 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5468 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5469 						}
5470 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5471 					}
5472 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5473 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5474 							if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5475 								net->dest_state |= SCTP_ADDR_NOHB;
5476 								if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5477 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5478 									    inp, stcb, net,
5479 									    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5480 								}
5481 							}
5482 						}
5483 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5484 					}
5485 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5486 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5487 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5488 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5489 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5490 							}
5491 							net->dest_state |= SCTP_ADDR_NO_PMTUD;
5492 							net->mtu = paddrp->spp_pathmtu;
5493 							switch (net->ro._l_addr.sa.sa_family) {
5494 #ifdef INET
5495 							case AF_INET:
5496 								net->mtu += SCTP_MIN_V4_OVERHEAD;
5497 								break;
5498 #endif
5499 #ifdef INET6
5500 							case AF_INET6:
5501 								net->mtu += SCTP_MIN_OVERHEAD;
5502 								break;
5503 #endif
5504 							default:
5505 								break;
5506 							}
5507 							if (net->mtu < stcb->asoc.smallest_mtu) {
5508 								sctp_pathmtu_adjustment(stcb, net->mtu);
5509 							}
5510 						}
5511 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5512 					}
5513 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5514 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5515 							if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5516 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5517 							}
5518 							net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5519 						}
5520 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5521 					}
5522 					if (paddrp->spp_flags & SPP_DSCP) {
5523 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5524 							net->dscp = paddrp->spp_dscp & 0xfc;
5525 							net->dscp |= 0x01;
5526 						}
5527 						stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5528 						stcb->asoc.default_dscp |= 0x01;
5529 					}
5530 #ifdef INET6
5531 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5532 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5533 							if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5534 								net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5535 								net->flowlabel |= 0x80000000;
5536 							}
5537 						}
5538 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5539 						stcb->asoc.default_flowlabel |= 0x80000000;
5540 					}
5541 #endif
5542 				}
5543 				SCTP_TCB_UNLOCK(stcb);
5544 			} else {
5545 				/************************NO TCB, SET TO default stuff ******************/
5546 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5547 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5548 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5549 					SCTP_INP_WLOCK(inp);
5550 					/*
5551 					 * For the TOS/FLOWLABEL stuff you
5552 					 * set it with the options on the
5553 					 * socket
5554 					 */
5555 					if (paddrp->spp_pathmaxrxt != 0) {
5556 						inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5557 					}
5558 					if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5559 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5560 					else if (paddrp->spp_hbinterval != 0) {
5561 						if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5562 							paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5563 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5564 					}
5565 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5566 						if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5567 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5568 						} else if (paddrp->spp_hbinterval) {
5569 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5570 						}
5571 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5572 					} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5573 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5574 					}
5575 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5576 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5577 					} else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5578 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5579 					}
5580 					if (paddrp->spp_flags & SPP_DSCP) {
5581 						inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5582 						inp->sctp_ep.default_dscp |= 0x01;
5583 					}
5584 #ifdef INET6
5585 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5586 						if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5587 							inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5588 							inp->sctp_ep.default_flowlabel |= 0x80000000;
5589 						}
5590 					}
5591 #endif
5592 					SCTP_INP_WUNLOCK(inp);
5593 				} else {
5594 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5595 					error = EINVAL;
5596 				}
5597 			}
5598 			break;
5599 		}
5600 	case SCTP_RTOINFO:
5601 		{
5602 			struct sctp_rtoinfo *srto;
5603 			uint32_t new_init, new_min, new_max;
5604 
5605 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5606 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5607 
5608 			if (stcb) {
5609 				if (srto->srto_initial)
5610 					new_init = srto->srto_initial;
5611 				else
5612 					new_init = stcb->asoc.initial_rto;
5613 				if (srto->srto_max)
5614 					new_max = srto->srto_max;
5615 				else
5616 					new_max = stcb->asoc.maxrto;
5617 				if (srto->srto_min)
5618 					new_min = srto->srto_min;
5619 				else
5620 					new_min = stcb->asoc.minrto;
5621 				if ((new_min <= new_init) && (new_init <= new_max)) {
5622 					stcb->asoc.initial_rto = new_init;
5623 					stcb->asoc.maxrto = new_max;
5624 					stcb->asoc.minrto = new_min;
5625 				} else {
5626 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5627 					error = EINVAL;
5628 				}
5629 				SCTP_TCB_UNLOCK(stcb);
5630 			} else {
5631 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5632 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5633 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5634 					SCTP_INP_WLOCK(inp);
5635 					if (srto->srto_initial)
5636 						new_init = srto->srto_initial;
5637 					else
5638 						new_init = inp->sctp_ep.initial_rto;
5639 					if (srto->srto_max)
5640 						new_max = srto->srto_max;
5641 					else
5642 						new_max = inp->sctp_ep.sctp_maxrto;
5643 					if (srto->srto_min)
5644 						new_min = srto->srto_min;
5645 					else
5646 						new_min = inp->sctp_ep.sctp_minrto;
5647 					if ((new_min <= new_init) && (new_init <= new_max)) {
5648 						inp->sctp_ep.initial_rto = new_init;
5649 						inp->sctp_ep.sctp_maxrto = new_max;
5650 						inp->sctp_ep.sctp_minrto = new_min;
5651 					} else {
5652 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5653 						error = EINVAL;
5654 					}
5655 					SCTP_INP_WUNLOCK(inp);
5656 				} else {
5657 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5658 					error = EINVAL;
5659 				}
5660 			}
5661 			break;
5662 		}
5663 	case SCTP_ASSOCINFO:
5664 		{
5665 			struct sctp_assocparams *sasoc;
5666 
5667 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5668 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5669 			if (sasoc->sasoc_cookie_life) {
5670 				/* boundary check the cookie life */
5671 				if (sasoc->sasoc_cookie_life < 1000)
5672 					sasoc->sasoc_cookie_life = 1000;
5673 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5674 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5675 				}
5676 			}
5677 			if (stcb) {
5678 				if (sasoc->sasoc_asocmaxrxt)
5679 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5680 				if (sasoc->sasoc_cookie_life) {
5681 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5682 				}
5683 				SCTP_TCB_UNLOCK(stcb);
5684 			} else {
5685 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5686 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5687 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5688 					SCTP_INP_WLOCK(inp);
5689 					if (sasoc->sasoc_asocmaxrxt)
5690 						inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5691 					if (sasoc->sasoc_cookie_life) {
5692 						inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5693 					}
5694 					SCTP_INP_WUNLOCK(inp);
5695 				} else {
5696 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5697 					error = EINVAL;
5698 				}
5699 			}
5700 			break;
5701 		}
5702 	case SCTP_INITMSG:
5703 		{
5704 			struct sctp_initmsg *sinit;
5705 
5706 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5707 			SCTP_INP_WLOCK(inp);
5708 			if (sinit->sinit_num_ostreams)
5709 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5710 
5711 			if (sinit->sinit_max_instreams)
5712 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5713 
5714 			if (sinit->sinit_max_attempts)
5715 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5716 
5717 			if (sinit->sinit_max_init_timeo)
5718 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5719 			SCTP_INP_WUNLOCK(inp);
5720 			break;
5721 		}
5722 	case SCTP_PRIMARY_ADDR:
5723 		{
5724 			struct sctp_setprim *spa;
5725 			struct sctp_nets *net;
5726 			struct sockaddr *addr;
5727 
5728 #if defined(INET) && defined(INET6)
5729 			struct sockaddr_in sin_store;
5730 
5731 #endif
5732 
5733 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5734 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5735 
5736 #if defined(INET) && defined(INET6)
5737 			if (spa->ssp_addr.ss_family == AF_INET6) {
5738 				struct sockaddr_in6 *sin6;
5739 
5740 				sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5741 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5742 					in6_sin6_2_sin(&sin_store, sin6);
5743 					addr = (struct sockaddr *)&sin_store;
5744 				} else {
5745 					addr = (struct sockaddr *)&spa->ssp_addr;
5746 				}
5747 			} else {
5748 				addr = (struct sockaddr *)&spa->ssp_addr;
5749 			}
5750 #else
5751 			addr = (struct sockaddr *)&spa->ssp_addr;
5752 #endif
5753 			if (stcb != NULL) {
5754 				net = sctp_findnet(stcb, addr);
5755 			} else {
5756 				/*
5757 				 * We increment here since
5758 				 * sctp_findassociation_ep_addr() wil do a
5759 				 * decrement if it finds the stcb as long as
5760 				 * the locked tcb (last argument) is NOT a
5761 				 * TCB.. aka NULL.
5762 				 */
5763 				net = NULL;
5764 				SCTP_INP_INCR_REF(inp);
5765 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5766 				    &net, NULL, NULL);
5767 				if (stcb == NULL) {
5768 					SCTP_INP_DECR_REF(inp);
5769 				}
5770 			}
5771 
5772 			if ((stcb != NULL) && (net != NULL)) {
5773 				if (net != stcb->asoc.primary_destination) {
5774 					if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5775 						/* Ok we need to set it */
5776 						if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5777 							if ((stcb->asoc.alternate) &&
5778 							    (!(net->dest_state & SCTP_ADDR_PF)) &&
5779 							    (net->dest_state & SCTP_ADDR_REACHABLE)) {
5780 								sctp_free_remote_addr(stcb->asoc.alternate);
5781 								stcb->asoc.alternate = NULL;
5782 							}
5783 						} else {
5784 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5785 							error = EINVAL;
5786 						}
5787 					} else {
5788 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5789 						error = EINVAL;
5790 					}
5791 				}
5792 			} else {
5793 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5794 				error = EINVAL;
5795 			}
5796 			if (stcb != NULL) {
5797 				SCTP_TCB_UNLOCK(stcb);
5798 			}
5799 			break;
5800 		}
5801 	case SCTP_SET_DYNAMIC_PRIMARY:
5802 		{
5803 			union sctp_sockstore *ss;
5804 
5805 			error = priv_check(curthread,
5806 			    PRIV_NETINET_RESERVEDPORT);
5807 			if (error)
5808 				break;
5809 
5810 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5811 			/* SUPER USER CHECK? */
5812 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5813 			break;
5814 		}
5815 	case SCTP_SET_PEER_PRIMARY_ADDR:
5816 		{
5817 			struct sctp_setpeerprim *sspp;
5818 			struct sockaddr *addr;
5819 
5820 #if defined(INET) && defined(INET6)
5821 			struct sockaddr_in sin_store;
5822 
5823 #endif
5824 
5825 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5826 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5827 			if (stcb != NULL) {
5828 				struct sctp_ifa *ifa;
5829 
5830 #if defined(INET) && defined(INET6)
5831 				if (sspp->sspp_addr.ss_family == AF_INET6) {
5832 					struct sockaddr_in6 *sin6;
5833 
5834 					sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5835 					if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5836 						in6_sin6_2_sin(&sin_store, sin6);
5837 						addr = (struct sockaddr *)&sin_store;
5838 					} else {
5839 						addr = (struct sockaddr *)&sspp->sspp_addr;
5840 					}
5841 				} else {
5842 					addr = (struct sockaddr *)&sspp->sspp_addr;
5843 				}
5844 #else
5845 				addr = (struct sockaddr *)&sspp->sspp_addr;
5846 #endif
5847 				ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5848 				if (ifa == NULL) {
5849 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5850 					error = EINVAL;
5851 					goto out_of_it;
5852 				}
5853 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5854 					/*
5855 					 * Must validate the ifa found is in
5856 					 * our ep
5857 					 */
5858 					struct sctp_laddr *laddr;
5859 					int found = 0;
5860 
5861 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5862 						if (laddr->ifa == NULL) {
5863 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5864 							    __func__);
5865 							continue;
5866 						}
5867 						if ((sctp_is_addr_restricted(stcb, laddr->ifa)) &&
5868 						    (!sctp_is_addr_pending(stcb, laddr->ifa))) {
5869 							continue;
5870 						}
5871 						if (laddr->ifa == ifa) {
5872 							found = 1;
5873 							break;
5874 						}
5875 					}
5876 					if (!found) {
5877 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5878 						error = EINVAL;
5879 						goto out_of_it;
5880 					}
5881 				} else {
5882 					switch (addr->sa_family) {
5883 #ifdef INET
5884 					case AF_INET:
5885 						{
5886 							struct sockaddr_in *sin;
5887 
5888 							sin = (struct sockaddr_in *)addr;
5889 							if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5890 							    &sin->sin_addr) != 0) {
5891 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5892 								error = EINVAL;
5893 								goto out_of_it;
5894 							}
5895 							break;
5896 						}
5897 #endif
5898 #ifdef INET6
5899 					case AF_INET6:
5900 						{
5901 							struct sockaddr_in6 *sin6;
5902 
5903 							sin6 = (struct sockaddr_in6 *)addr;
5904 							if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5905 							    &sin6->sin6_addr) != 0) {
5906 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5907 								error = EINVAL;
5908 								goto out_of_it;
5909 							}
5910 							break;
5911 						}
5912 #endif
5913 					default:
5914 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5915 						error = EINVAL;
5916 						goto out_of_it;
5917 					}
5918 				}
5919 				if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5920 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5921 					error = EINVAL;
5922 				}
5923 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5924 		out_of_it:
5925 				SCTP_TCB_UNLOCK(stcb);
5926 			} else {
5927 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5928 				error = EINVAL;
5929 			}
5930 			break;
5931 		}
5932 	case SCTP_BINDX_ADD_ADDR:
5933 		{
5934 			struct sctp_getaddresses *addrs;
5935 			struct thread *td;
5936 
5937 			td = (struct thread *)p;
5938 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5939 			    optsize);
5940 #ifdef INET
5941 			if (addrs->addr->sa_family == AF_INET) {
5942 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5943 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5944 					error = EINVAL;
5945 					break;
5946 				}
5947 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5948 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5949 					break;
5950 				}
5951 			} else
5952 #endif
5953 #ifdef INET6
5954 			if (addrs->addr->sa_family == AF_INET6) {
5955 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5956 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5957 					error = EINVAL;
5958 					break;
5959 				}
5960 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5961 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5962 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5963 					break;
5964 				}
5965 			} else
5966 #endif
5967 			{
5968 				error = EAFNOSUPPORT;
5969 				break;
5970 			}
5971 			sctp_bindx_add_address(so, inp, addrs->addr,
5972 			    addrs->sget_assoc_id, vrf_id,
5973 			    &error, p);
5974 			break;
5975 		}
5976 	case SCTP_BINDX_REM_ADDR:
5977 		{
5978 			struct sctp_getaddresses *addrs;
5979 			struct thread *td;
5980 
5981 			td = (struct thread *)p;
5982 
5983 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5984 #ifdef INET
5985 			if (addrs->addr->sa_family == AF_INET) {
5986 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5987 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5988 					error = EINVAL;
5989 					break;
5990 				}
5991 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5992 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5993 					break;
5994 				}
5995 			} else
5996 #endif
5997 #ifdef INET6
5998 			if (addrs->addr->sa_family == AF_INET6) {
5999 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
6000 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6001 					error = EINVAL;
6002 					break;
6003 				}
6004 				if (td != NULL &&
6005 				    (error = prison_local_ip6(td->td_ucred,
6006 				    &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
6007 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
6008 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6009 					break;
6010 				}
6011 			} else
6012 #endif
6013 			{
6014 				error = EAFNOSUPPORT;
6015 				break;
6016 			}
6017 			sctp_bindx_delete_address(inp, addrs->addr,
6018 			    addrs->sget_assoc_id, vrf_id,
6019 			    &error);
6020 			break;
6021 		}
6022 	case SCTP_EVENT:
6023 		{
6024 			struct sctp_event *event;
6025 			uint32_t event_type;
6026 
6027 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
6028 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
6029 			switch (event->se_type) {
6030 			case SCTP_ASSOC_CHANGE:
6031 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
6032 				break;
6033 			case SCTP_PEER_ADDR_CHANGE:
6034 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
6035 				break;
6036 			case SCTP_REMOTE_ERROR:
6037 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
6038 				break;
6039 			case SCTP_SEND_FAILED:
6040 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
6041 				break;
6042 			case SCTP_SHUTDOWN_EVENT:
6043 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
6044 				break;
6045 			case SCTP_ADAPTATION_INDICATION:
6046 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
6047 				break;
6048 			case SCTP_PARTIAL_DELIVERY_EVENT:
6049 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
6050 				break;
6051 			case SCTP_AUTHENTICATION_EVENT:
6052 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
6053 				break;
6054 			case SCTP_STREAM_RESET_EVENT:
6055 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
6056 				break;
6057 			case SCTP_SENDER_DRY_EVENT:
6058 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
6059 				break;
6060 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
6061 				event_type = 0;
6062 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6063 				error = ENOTSUP;
6064 				break;
6065 			case SCTP_ASSOC_RESET_EVENT:
6066 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
6067 				break;
6068 			case SCTP_STREAM_CHANGE_EVENT:
6069 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
6070 				break;
6071 			case SCTP_SEND_FAILED_EVENT:
6072 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6073 				break;
6074 			default:
6075 				event_type = 0;
6076 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6077 				error = EINVAL;
6078 				break;
6079 			}
6080 			if (event_type > 0) {
6081 				if (stcb) {
6082 					if (event->se_on) {
6083 						sctp_stcb_feature_on(inp, stcb, event_type);
6084 						if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6085 							if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6086 							    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6087 							    (stcb->asoc.stream_queue_cnt == 0)) {
6088 								sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6089 							}
6090 						}
6091 					} else {
6092 						sctp_stcb_feature_off(inp, stcb, event_type);
6093 					}
6094 					SCTP_TCB_UNLOCK(stcb);
6095 				} else {
6096 					/*
6097 					 * We don't want to send up a storm
6098 					 * of events, so return an error for
6099 					 * sender dry events
6100 					 */
6101 					if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6102 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6103 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6104 					    ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6105 					    (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6106 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6107 						error = ENOTSUP;
6108 						break;
6109 					}
6110 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6111 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6112 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6113 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6114 						SCTP_INP_WLOCK(inp);
6115 						if (event->se_on) {
6116 							sctp_feature_on(inp, event_type);
6117 						} else {
6118 							sctp_feature_off(inp, event_type);
6119 						}
6120 						SCTP_INP_WUNLOCK(inp);
6121 					}
6122 					if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6123 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6124 						SCTP_INP_RLOCK(inp);
6125 						LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6126 							SCTP_TCB_LOCK(stcb);
6127 							if (event->se_on) {
6128 								sctp_stcb_feature_on(inp, stcb, event_type);
6129 							} else {
6130 								sctp_stcb_feature_off(inp, stcb, event_type);
6131 							}
6132 							SCTP_TCB_UNLOCK(stcb);
6133 						}
6134 						SCTP_INP_RUNLOCK(inp);
6135 					}
6136 				}
6137 			}
6138 			break;
6139 		}
6140 	case SCTP_RECVRCVINFO:
6141 		{
6142 			int *onoff;
6143 
6144 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6145 			SCTP_INP_WLOCK(inp);
6146 			if (*onoff != 0) {
6147 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6148 			} else {
6149 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6150 			}
6151 			SCTP_INP_WUNLOCK(inp);
6152 			break;
6153 		}
6154 	case SCTP_RECVNXTINFO:
6155 		{
6156 			int *onoff;
6157 
6158 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6159 			SCTP_INP_WLOCK(inp);
6160 			if (*onoff != 0) {
6161 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6162 			} else {
6163 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6164 			}
6165 			SCTP_INP_WUNLOCK(inp);
6166 			break;
6167 		}
6168 	case SCTP_DEFAULT_SNDINFO:
6169 		{
6170 			struct sctp_sndinfo *info;
6171 			uint16_t policy;
6172 
6173 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6174 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6175 
6176 			if (stcb) {
6177 				if (info->snd_sid < stcb->asoc.streamoutcnt) {
6178 					stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6179 					policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6180 					stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6181 					stcb->asoc.def_send.sinfo_flags |= policy;
6182 					stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6183 					stcb->asoc.def_send.sinfo_context = info->snd_context;
6184 				} else {
6185 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6186 					error = EINVAL;
6187 				}
6188 				SCTP_TCB_UNLOCK(stcb);
6189 			} else {
6190 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6191 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6192 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6193 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6194 					SCTP_INP_WLOCK(inp);
6195 					inp->def_send.sinfo_stream = info->snd_sid;
6196 					policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6197 					inp->def_send.sinfo_flags = info->snd_flags;
6198 					inp->def_send.sinfo_flags |= policy;
6199 					inp->def_send.sinfo_ppid = info->snd_ppid;
6200 					inp->def_send.sinfo_context = info->snd_context;
6201 					SCTP_INP_WUNLOCK(inp);
6202 				}
6203 				if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6204 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6205 					SCTP_INP_RLOCK(inp);
6206 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6207 						SCTP_TCB_LOCK(stcb);
6208 						if (info->snd_sid < stcb->asoc.streamoutcnt) {
6209 							stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6210 							policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6211 							stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6212 							stcb->asoc.def_send.sinfo_flags |= policy;
6213 							stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6214 							stcb->asoc.def_send.sinfo_context = info->snd_context;
6215 						}
6216 						SCTP_TCB_UNLOCK(stcb);
6217 					}
6218 					SCTP_INP_RUNLOCK(inp);
6219 				}
6220 			}
6221 			break;
6222 		}
6223 	case SCTP_DEFAULT_PRINFO:
6224 		{
6225 			struct sctp_default_prinfo *info;
6226 
6227 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6228 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6229 
6230 			if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6231 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6232 				error = EINVAL;
6233 				break;
6234 			}
6235 			if (stcb) {
6236 				stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6237 				stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6238 				stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6239 				SCTP_TCB_UNLOCK(stcb);
6240 			} else {
6241 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6242 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6243 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6244 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6245 					SCTP_INP_WLOCK(inp);
6246 					inp->def_send.sinfo_flags &= 0xfff0;
6247 					inp->def_send.sinfo_flags |= info->pr_policy;
6248 					inp->def_send.sinfo_timetolive = info->pr_value;
6249 					SCTP_INP_WUNLOCK(inp);
6250 				}
6251 				if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6252 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6253 					SCTP_INP_RLOCK(inp);
6254 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6255 						SCTP_TCB_LOCK(stcb);
6256 						stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6257 						stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6258 						stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6259 						SCTP_TCB_UNLOCK(stcb);
6260 					}
6261 					SCTP_INP_RUNLOCK(inp);
6262 				}
6263 			}
6264 			break;
6265 		}
6266 	case SCTP_PEER_ADDR_THLDS:
6267 		/* Applies to the specific association */
6268 		{
6269 			struct sctp_paddrthlds *thlds;
6270 			struct sctp_nets *net;
6271 			struct sockaddr *addr;
6272 
6273 #if defined(INET) && defined(INET6)
6274 			struct sockaddr_in sin_store;
6275 
6276 #endif
6277 
6278 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6279 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6280 
6281 #if defined(INET) && defined(INET6)
6282 			if (thlds->spt_address.ss_family == AF_INET6) {
6283 				struct sockaddr_in6 *sin6;
6284 
6285 				sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6286 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6287 					in6_sin6_2_sin(&sin_store, sin6);
6288 					addr = (struct sockaddr *)&sin_store;
6289 				} else {
6290 					addr = (struct sockaddr *)&thlds->spt_address;
6291 				}
6292 			} else {
6293 				addr = (struct sockaddr *)&thlds->spt_address;
6294 			}
6295 #else
6296 			addr = (struct sockaddr *)&thlds->spt_address;
6297 #endif
6298 			if (stcb != NULL) {
6299 				net = sctp_findnet(stcb, addr);
6300 			} else {
6301 				/*
6302 				 * We increment here since
6303 				 * sctp_findassociation_ep_addr() wil do a
6304 				 * decrement if it finds the stcb as long as
6305 				 * the locked tcb (last argument) is NOT a
6306 				 * TCB.. aka NULL.
6307 				 */
6308 				net = NULL;
6309 				SCTP_INP_INCR_REF(inp);
6310 				stcb = sctp_findassociation_ep_addr(&inp, addr,
6311 				    &net, NULL, NULL);
6312 				if (stcb == NULL) {
6313 					SCTP_INP_DECR_REF(inp);
6314 				}
6315 			}
6316 			if ((stcb != NULL) && (net == NULL)) {
6317 #ifdef INET
6318 				if (addr->sa_family == AF_INET) {
6319 
6320 					struct sockaddr_in *sin;
6321 
6322 					sin = (struct sockaddr_in *)addr;
6323 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6324 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6325 						SCTP_TCB_UNLOCK(stcb);
6326 						error = EINVAL;
6327 						break;
6328 					}
6329 				} else
6330 #endif
6331 #ifdef INET6
6332 				if (addr->sa_family == AF_INET6) {
6333 					struct sockaddr_in6 *sin6;
6334 
6335 					sin6 = (struct sockaddr_in6 *)addr;
6336 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6337 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6338 						SCTP_TCB_UNLOCK(stcb);
6339 						error = EINVAL;
6340 						break;
6341 					}
6342 				} else
6343 #endif
6344 				{
6345 					error = EAFNOSUPPORT;
6346 					SCTP_TCB_UNLOCK(stcb);
6347 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6348 					break;
6349 				}
6350 			}
6351 			if (thlds->spt_pathcpthld != 0xffff) {
6352 				error = EINVAL;
6353 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6354 				break;
6355 			}
6356 			if (stcb != NULL) {
6357 				if (net != NULL) {
6358 					net->failure_threshold = thlds->spt_pathmaxrxt;
6359 					net->pf_threshold = thlds->spt_pathpfthld;
6360 					if (net->dest_state & SCTP_ADDR_PF) {
6361 						if ((net->error_count > net->failure_threshold) ||
6362 						    (net->error_count <= net->pf_threshold)) {
6363 							net->dest_state &= ~SCTP_ADDR_PF;
6364 						}
6365 					} else {
6366 						if ((net->error_count > net->pf_threshold) &&
6367 						    (net->error_count <= net->failure_threshold)) {
6368 							net->dest_state |= SCTP_ADDR_PF;
6369 							sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6370 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6371 							    stcb->sctp_ep, stcb, net,
6372 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6373 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6374 						}
6375 					}
6376 					if (net->dest_state & SCTP_ADDR_REACHABLE) {
6377 						if (net->error_count > net->failure_threshold) {
6378 							net->dest_state &= ~SCTP_ADDR_REACHABLE;
6379 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6380 						}
6381 					} else {
6382 						if (net->error_count <= net->failure_threshold) {
6383 							net->dest_state |= SCTP_ADDR_REACHABLE;
6384 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6385 						}
6386 					}
6387 				} else {
6388 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6389 						net->failure_threshold = thlds->spt_pathmaxrxt;
6390 						net->pf_threshold = thlds->spt_pathpfthld;
6391 						if (net->dest_state & SCTP_ADDR_PF) {
6392 							if ((net->error_count > net->failure_threshold) ||
6393 							    (net->error_count <= net->pf_threshold)) {
6394 								net->dest_state &= ~SCTP_ADDR_PF;
6395 							}
6396 						} else {
6397 							if ((net->error_count > net->pf_threshold) &&
6398 							    (net->error_count <= net->failure_threshold)) {
6399 								net->dest_state |= SCTP_ADDR_PF;
6400 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6401 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6402 								    stcb->sctp_ep, stcb, net,
6403 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
6404 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6405 							}
6406 						}
6407 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
6408 							if (net->error_count > net->failure_threshold) {
6409 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
6410 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6411 							}
6412 						} else {
6413 							if (net->error_count <= net->failure_threshold) {
6414 								net->dest_state |= SCTP_ADDR_REACHABLE;
6415 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6416 							}
6417 						}
6418 					}
6419 					stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6420 					stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6421 				}
6422 				SCTP_TCB_UNLOCK(stcb);
6423 			} else {
6424 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6425 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6426 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6427 					SCTP_INP_WLOCK(inp);
6428 					inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6429 					inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6430 					SCTP_INP_WUNLOCK(inp);
6431 				} else {
6432 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6433 					error = EINVAL;
6434 				}
6435 			}
6436 			break;
6437 		}
6438 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
6439 		{
6440 			struct sctp_udpencaps *encaps;
6441 			struct sctp_nets *net;
6442 			struct sockaddr *addr;
6443 
6444 #if defined(INET) && defined(INET6)
6445 			struct sockaddr_in sin_store;
6446 
6447 #endif
6448 
6449 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6450 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6451 
6452 #if defined(INET) && defined(INET6)
6453 			if (encaps->sue_address.ss_family == AF_INET6) {
6454 				struct sockaddr_in6 *sin6;
6455 
6456 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6457 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6458 					in6_sin6_2_sin(&sin_store, sin6);
6459 					addr = (struct sockaddr *)&sin_store;
6460 				} else {
6461 					addr = (struct sockaddr *)&encaps->sue_address;
6462 				}
6463 			} else {
6464 				addr = (struct sockaddr *)&encaps->sue_address;
6465 			}
6466 #else
6467 			addr = (struct sockaddr *)&encaps->sue_address;
6468 #endif
6469 			if (stcb != NULL) {
6470 				net = sctp_findnet(stcb, addr);
6471 			} else {
6472 				/*
6473 				 * We increment here since
6474 				 * sctp_findassociation_ep_addr() wil do a
6475 				 * decrement if it finds the stcb as long as
6476 				 * the locked tcb (last argument) is NOT a
6477 				 * TCB.. aka NULL.
6478 				 */
6479 				net = NULL;
6480 				SCTP_INP_INCR_REF(inp);
6481 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6482 				if (stcb == NULL) {
6483 					SCTP_INP_DECR_REF(inp);
6484 				}
6485 			}
6486 			if ((stcb != NULL) && (net == NULL)) {
6487 #ifdef INET
6488 				if (addr->sa_family == AF_INET) {
6489 
6490 					struct sockaddr_in *sin;
6491 
6492 					sin = (struct sockaddr_in *)addr;
6493 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6494 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6495 						SCTP_TCB_UNLOCK(stcb);
6496 						error = EINVAL;
6497 						break;
6498 					}
6499 				} else
6500 #endif
6501 #ifdef INET6
6502 				if (addr->sa_family == AF_INET6) {
6503 					struct sockaddr_in6 *sin6;
6504 
6505 					sin6 = (struct sockaddr_in6 *)addr;
6506 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6507 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6508 						SCTP_TCB_UNLOCK(stcb);
6509 						error = EINVAL;
6510 						break;
6511 					}
6512 				} else
6513 #endif
6514 				{
6515 					error = EAFNOSUPPORT;
6516 					SCTP_TCB_UNLOCK(stcb);
6517 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6518 					break;
6519 				}
6520 			}
6521 			if (stcb != NULL) {
6522 				if (net != NULL) {
6523 					net->port = encaps->sue_port;
6524 				} else {
6525 					stcb->asoc.port = encaps->sue_port;
6526 				}
6527 				SCTP_TCB_UNLOCK(stcb);
6528 			} else {
6529 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6530 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6531 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6532 					SCTP_INP_WLOCK(inp);
6533 					inp->sctp_ep.port = encaps->sue_port;
6534 					SCTP_INP_WUNLOCK(inp);
6535 				} else {
6536 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6537 					error = EINVAL;
6538 				}
6539 			}
6540 			break;
6541 		}
6542 	case SCTP_ECN_SUPPORTED:
6543 		{
6544 			struct sctp_assoc_value *av;
6545 
6546 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6547 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6548 
6549 			if (stcb) {
6550 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6551 				error = EINVAL;
6552 				SCTP_TCB_UNLOCK(stcb);
6553 			} else {
6554 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6555 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6556 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6557 					SCTP_INP_WLOCK(inp);
6558 					if (av->assoc_value == 0) {
6559 						inp->ecn_supported = 0;
6560 					} else {
6561 						inp->ecn_supported = 1;
6562 					}
6563 					SCTP_INP_WUNLOCK(inp);
6564 				} else {
6565 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6566 					error = EINVAL;
6567 				}
6568 			}
6569 			break;
6570 		}
6571 	case SCTP_PR_SUPPORTED:
6572 		{
6573 			struct sctp_assoc_value *av;
6574 
6575 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6576 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6577 
6578 			if (stcb) {
6579 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6580 				error = EINVAL;
6581 				SCTP_TCB_UNLOCK(stcb);
6582 			} else {
6583 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6584 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6585 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6586 					SCTP_INP_WLOCK(inp);
6587 					if (av->assoc_value == 0) {
6588 						inp->prsctp_supported = 0;
6589 					} else {
6590 						inp->prsctp_supported = 1;
6591 					}
6592 					SCTP_INP_WUNLOCK(inp);
6593 				} else {
6594 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6595 					error = EINVAL;
6596 				}
6597 			}
6598 			break;
6599 		}
6600 	case SCTP_AUTH_SUPPORTED:
6601 		{
6602 			struct sctp_assoc_value *av;
6603 
6604 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6605 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6606 
6607 			if (stcb) {
6608 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6609 				error = EINVAL;
6610 				SCTP_TCB_UNLOCK(stcb);
6611 			} else {
6612 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6613 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6614 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6615 					if ((av->assoc_value == 0) &&
6616 					    (inp->asconf_supported == 1)) {
6617 						/*
6618 						 * AUTH is required for
6619 						 * ASCONF
6620 						 */
6621 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6622 						error = EINVAL;
6623 					} else {
6624 						SCTP_INP_WLOCK(inp);
6625 						if (av->assoc_value == 0) {
6626 							inp->auth_supported = 0;
6627 						} else {
6628 							inp->auth_supported = 1;
6629 						}
6630 						SCTP_INP_WUNLOCK(inp);
6631 					}
6632 				} else {
6633 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6634 					error = EINVAL;
6635 				}
6636 			}
6637 			break;
6638 		}
6639 	case SCTP_ASCONF_SUPPORTED:
6640 		{
6641 			struct sctp_assoc_value *av;
6642 
6643 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6644 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6645 
6646 			if (stcb) {
6647 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6648 				error = EINVAL;
6649 				SCTP_TCB_UNLOCK(stcb);
6650 			} else {
6651 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6652 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6653 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6654 					if ((av->assoc_value != 0) &&
6655 					    (inp->auth_supported == 0)) {
6656 						/*
6657 						 * AUTH is required for
6658 						 * ASCONF
6659 						 */
6660 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6661 						error = EINVAL;
6662 					} else {
6663 						SCTP_INP_WLOCK(inp);
6664 						if (av->assoc_value == 0) {
6665 							inp->asconf_supported = 0;
6666 							sctp_auth_delete_chunk(SCTP_ASCONF,
6667 							    inp->sctp_ep.local_auth_chunks);
6668 							sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6669 							    inp->sctp_ep.local_auth_chunks);
6670 						} else {
6671 							inp->asconf_supported = 1;
6672 							sctp_auth_add_chunk(SCTP_ASCONF,
6673 							    inp->sctp_ep.local_auth_chunks);
6674 							sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6675 							    inp->sctp_ep.local_auth_chunks);
6676 						}
6677 						SCTP_INP_WUNLOCK(inp);
6678 					}
6679 				} else {
6680 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6681 					error = EINVAL;
6682 				}
6683 			}
6684 			break;
6685 		}
6686 	case SCTP_RECONFIG_SUPPORTED:
6687 		{
6688 			struct sctp_assoc_value *av;
6689 
6690 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6691 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6692 
6693 			if (stcb) {
6694 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6695 				error = EINVAL;
6696 				SCTP_TCB_UNLOCK(stcb);
6697 			} else {
6698 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6699 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6700 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6701 					SCTP_INP_WLOCK(inp);
6702 					if (av->assoc_value == 0) {
6703 						inp->reconfig_supported = 0;
6704 					} else {
6705 						inp->reconfig_supported = 1;
6706 					}
6707 					SCTP_INP_WUNLOCK(inp);
6708 				} else {
6709 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6710 					error = EINVAL;
6711 				}
6712 			}
6713 			break;
6714 		}
6715 	case SCTP_NRSACK_SUPPORTED:
6716 		{
6717 			struct sctp_assoc_value *av;
6718 
6719 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6720 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6721 
6722 			if (stcb) {
6723 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6724 				error = EINVAL;
6725 				SCTP_TCB_UNLOCK(stcb);
6726 			} else {
6727 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6728 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6729 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6730 					SCTP_INP_WLOCK(inp);
6731 					if (av->assoc_value == 0) {
6732 						inp->nrsack_supported = 0;
6733 					} else {
6734 						inp->nrsack_supported = 1;
6735 					}
6736 					SCTP_INP_WUNLOCK(inp);
6737 				} else {
6738 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6739 					error = EINVAL;
6740 				}
6741 			}
6742 			break;
6743 		}
6744 	case SCTP_PKTDROP_SUPPORTED:
6745 		{
6746 			struct sctp_assoc_value *av;
6747 
6748 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6749 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6750 
6751 			if (stcb) {
6752 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6753 				error = EINVAL;
6754 				SCTP_TCB_UNLOCK(stcb);
6755 			} else {
6756 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6757 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6758 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6759 					SCTP_INP_WLOCK(inp);
6760 					if (av->assoc_value == 0) {
6761 						inp->pktdrop_supported = 0;
6762 					} else {
6763 						inp->pktdrop_supported = 1;
6764 					}
6765 					SCTP_INP_WUNLOCK(inp);
6766 				} else {
6767 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6768 					error = EINVAL;
6769 				}
6770 			}
6771 			break;
6772 		}
6773 	case SCTP_MAX_CWND:
6774 		{
6775 			struct sctp_assoc_value *av;
6776 			struct sctp_nets *net;
6777 
6778 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6779 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6780 
6781 			if (stcb) {
6782 				stcb->asoc.max_cwnd = av->assoc_value;
6783 				if (stcb->asoc.max_cwnd > 0) {
6784 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6785 						if ((net->cwnd > stcb->asoc.max_cwnd) &&
6786 						    (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6787 							net->cwnd = stcb->asoc.max_cwnd;
6788 							if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6789 								net->cwnd = net->mtu - sizeof(struct sctphdr);
6790 							}
6791 						}
6792 					}
6793 				}
6794 				SCTP_TCB_UNLOCK(stcb);
6795 			} else {
6796 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6797 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6798 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6799 					SCTP_INP_WLOCK(inp);
6800 					inp->max_cwnd = av->assoc_value;
6801 					SCTP_INP_WUNLOCK(inp);
6802 				} else {
6803 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6804 					error = EINVAL;
6805 				}
6806 			}
6807 			break;
6808 		}
6809 	default:
6810 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6811 		error = ENOPROTOOPT;
6812 		break;
6813 	}			/* end switch (opt) */
6814 	return (error);
6815 }
6816 
6817 int
6818 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6819 {
6820 	void *optval = NULL;
6821 	size_t optsize = 0;
6822 	void *p;
6823 	int error = 0;
6824 	struct sctp_inpcb *inp;
6825 
6826 	if ((sopt->sopt_level == SOL_SOCKET) &&
6827 	    (sopt->sopt_name == SO_SETFIB)) {
6828 		inp = (struct sctp_inpcb *)so->so_pcb;
6829 		if (inp == NULL) {
6830 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6831 			return (EINVAL);
6832 		}
6833 		SCTP_INP_WLOCK(inp);
6834 		inp->fibnum = so->so_fibnum;
6835 		SCTP_INP_WUNLOCK(inp);
6836 		return (0);
6837 	}
6838 	if (sopt->sopt_level != IPPROTO_SCTP) {
6839 		/* wrong proto level... send back up to IP */
6840 #ifdef INET6
6841 		if (INP_CHECK_SOCKAF(so, AF_INET6))
6842 			error = ip6_ctloutput(so, sopt);
6843 #endif				/* INET6 */
6844 #if defined(INET) && defined(INET6)
6845 		else
6846 #endif
6847 #ifdef INET
6848 			error = ip_ctloutput(so, sopt);
6849 #endif
6850 		return (error);
6851 	}
6852 	optsize = sopt->sopt_valsize;
6853 	if (optsize) {
6854 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6855 		if (optval == NULL) {
6856 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6857 			return (ENOBUFS);
6858 		}
6859 		error = sooptcopyin(sopt, optval, optsize, optsize);
6860 		if (error) {
6861 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
6862 			goto out;
6863 		}
6864 	}
6865 	p = (void *)sopt->sopt_td;
6866 	if (sopt->sopt_dir == SOPT_SET) {
6867 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6868 	} else if (sopt->sopt_dir == SOPT_GET) {
6869 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6870 	} else {
6871 		SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6872 		error = EINVAL;
6873 	}
6874 	if ((error == 0) && (optval != NULL)) {
6875 		error = sooptcopyout(sopt, optval, optsize);
6876 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6877 	} else if (optval != NULL) {
6878 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6879 	}
6880 out:
6881 	return (error);
6882 }
6883 
6884 #ifdef INET
6885 static int
6886 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6887 {
6888 	int error = 0;
6889 	int create_lock_on = 0;
6890 	uint32_t vrf_id;
6891 	struct sctp_inpcb *inp;
6892 	struct sctp_tcb *stcb = NULL;
6893 
6894 	inp = (struct sctp_inpcb *)so->so_pcb;
6895 	if (inp == NULL) {
6896 		/* I made the same as TCP since we are not setup? */
6897 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6898 		return (ECONNRESET);
6899 	}
6900 	if (addr == NULL) {
6901 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6902 		return EINVAL;
6903 	}
6904 	switch (addr->sa_family) {
6905 #ifdef INET6
6906 	case AF_INET6:
6907 		{
6908 			struct sockaddr_in6 *sin6p;
6909 
6910 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6911 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6912 				return (EINVAL);
6913 			}
6914 			sin6p = (struct sockaddr_in6 *)addr;
6915 			if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6916 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6917 				return (error);
6918 			}
6919 			break;
6920 		}
6921 #endif
6922 #ifdef INET
6923 	case AF_INET:
6924 		{
6925 			struct sockaddr_in *sinp;
6926 
6927 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
6928 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6929 				return (EINVAL);
6930 			}
6931 			sinp = (struct sockaddr_in *)addr;
6932 			if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6933 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6934 				return (error);
6935 			}
6936 			break;
6937 		}
6938 #endif
6939 	default:
6940 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6941 		return (EAFNOSUPPORT);
6942 	}
6943 	SCTP_INP_INCR_REF(inp);
6944 	SCTP_ASOC_CREATE_LOCK(inp);
6945 	create_lock_on = 1;
6946 
6947 
6948 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6949 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6950 		/* Should I really unlock ? */
6951 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6952 		error = EFAULT;
6953 		goto out_now;
6954 	}
6955 #ifdef INET6
6956 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6957 	    (addr->sa_family == AF_INET6)) {
6958 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6959 		error = EINVAL;
6960 		goto out_now;
6961 	}
6962 #endif
6963 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6964 	    SCTP_PCB_FLAGS_UNBOUND) {
6965 		/* Bind a ephemeral port */
6966 		error = sctp_inpcb_bind(so, NULL, NULL, p);
6967 		if (error) {
6968 			goto out_now;
6969 		}
6970 	}
6971 	/* Now do we connect? */
6972 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6973 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6974 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6975 		error = EINVAL;
6976 		goto out_now;
6977 	}
6978 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6979 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6980 		/* We are already connected AND the TCP model */
6981 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6982 		error = EADDRINUSE;
6983 		goto out_now;
6984 	}
6985 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6986 		SCTP_INP_RLOCK(inp);
6987 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
6988 		SCTP_INP_RUNLOCK(inp);
6989 	} else {
6990 		/*
6991 		 * We increment here since sctp_findassociation_ep_addr()
6992 		 * will do a decrement if it finds the stcb as long as the
6993 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
6994 		 */
6995 		SCTP_INP_INCR_REF(inp);
6996 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6997 		if (stcb == NULL) {
6998 			SCTP_INP_DECR_REF(inp);
6999 		} else {
7000 			SCTP_TCB_UNLOCK(stcb);
7001 		}
7002 	}
7003 	if (stcb != NULL) {
7004 		/* Already have or am bring up an association */
7005 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
7006 		error = EALREADY;
7007 		goto out_now;
7008 	}
7009 	vrf_id = inp->def_vrf_id;
7010 	/* We are GOOD to go */
7011 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
7012 	    inp->sctp_ep.pre_open_stream_count,
7013 	    inp->sctp_ep.port, p);
7014 	if (stcb == NULL) {
7015 		/* Gak! no memory */
7016 		goto out_now;
7017 	}
7018 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
7019 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7020 		/* Set the connected flag so we can queue data */
7021 		soisconnecting(so);
7022 	}
7023 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
7024 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7025 
7026 	/* initialize authentication parameters for the assoc */
7027 	sctp_initialize_auth_params(inp, stcb);
7028 
7029 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7030 	SCTP_TCB_UNLOCK(stcb);
7031 out_now:
7032 	if (create_lock_on) {
7033 		SCTP_ASOC_CREATE_UNLOCK(inp);
7034 	}
7035 	SCTP_INP_DECR_REF(inp);
7036 	return (error);
7037 }
7038 
7039 #endif
7040 
7041 int
7042 sctp_listen(struct socket *so, int backlog, struct thread *p)
7043 {
7044 	/*
7045 	 * Note this module depends on the protocol processing being called
7046 	 * AFTER any socket level flags and backlog are applied to the
7047 	 * socket. The traditional way that the socket flags are applied is
7048 	 * AFTER protocol processing. We have made a change to the
7049 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
7050 	 * place if the socket API for SCTP is to work properly.
7051 	 */
7052 
7053 	int error = 0;
7054 	struct sctp_inpcb *inp;
7055 
7056 	inp = (struct sctp_inpcb *)so->so_pcb;
7057 	if (inp == NULL) {
7058 		/* I made the same as TCP since we are not setup? */
7059 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7060 		return (ECONNRESET);
7061 	}
7062 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
7063 		/* See if we have a listener */
7064 		struct sctp_inpcb *tinp;
7065 		union sctp_sockstore store;
7066 
7067 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
7068 			/* not bound all */
7069 			struct sctp_laddr *laddr;
7070 
7071 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7072 				memcpy(&store, &laddr->ifa->address, sizeof(store));
7073 				switch (store.sa.sa_family) {
7074 #ifdef INET
7075 				case AF_INET:
7076 					store.sin.sin_port = inp->sctp_lport;
7077 					break;
7078 #endif
7079 #ifdef INET6
7080 				case AF_INET6:
7081 					store.sin6.sin6_port = inp->sctp_lport;
7082 					break;
7083 #endif
7084 				default:
7085 					break;
7086 				}
7087 				tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7088 				if (tinp && (tinp != inp) &&
7089 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7090 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7091 				    (tinp->sctp_socket->so_qlimit)) {
7092 					/*
7093 					 * we have a listener already and
7094 					 * its not this inp.
7095 					 */
7096 					SCTP_INP_DECR_REF(tinp);
7097 					return (EADDRINUSE);
7098 				} else if (tinp) {
7099 					SCTP_INP_DECR_REF(tinp);
7100 				}
7101 			}
7102 		} else {
7103 			/* Setup a local addr bound all */
7104 			memset(&store, 0, sizeof(store));
7105 #ifdef INET6
7106 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7107 				store.sa.sa_family = AF_INET6;
7108 				store.sa.sa_len = sizeof(struct sockaddr_in6);
7109 			}
7110 #endif
7111 #ifdef INET
7112 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7113 				store.sa.sa_family = AF_INET;
7114 				store.sa.sa_len = sizeof(struct sockaddr_in);
7115 			}
7116 #endif
7117 			switch (store.sa.sa_family) {
7118 #ifdef INET
7119 			case AF_INET:
7120 				store.sin.sin_port = inp->sctp_lport;
7121 				break;
7122 #endif
7123 #ifdef INET6
7124 			case AF_INET6:
7125 				store.sin6.sin6_port = inp->sctp_lport;
7126 				break;
7127 #endif
7128 			default:
7129 				break;
7130 			}
7131 			tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7132 			if (tinp && (tinp != inp) &&
7133 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7134 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7135 			    (tinp->sctp_socket->so_qlimit)) {
7136 				/*
7137 				 * we have a listener already and its not
7138 				 * this inp.
7139 				 */
7140 				SCTP_INP_DECR_REF(tinp);
7141 				return (EADDRINUSE);
7142 			} else if (tinp) {
7143 				SCTP_INP_DECR_REF(tinp);
7144 			}
7145 		}
7146 	}
7147 	SCTP_INP_RLOCK(inp);
7148 #ifdef SCTP_LOCK_LOGGING
7149 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7150 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7151 	}
7152 #endif
7153 	SOCK_LOCK(so);
7154 	error = solisten_proto_check(so);
7155 	SOCK_UNLOCK(so);
7156 	if (error) {
7157 		SCTP_INP_RUNLOCK(inp);
7158 		return (error);
7159 	}
7160 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7161 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7162 		/*
7163 		 * The unlucky case - We are in the tcp pool with this guy.
7164 		 * - Someone else is in the main inp slot. - We must move
7165 		 * this guy (the listener) to the main slot - We must then
7166 		 * move the guy that was listener to the TCP Pool.
7167 		 */
7168 		if (sctp_swap_inpcb_for_listen(inp)) {
7169 			SCTP_INP_RUNLOCK(inp);
7170 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7171 			return (EADDRINUSE);
7172 		}
7173 	}
7174 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7175 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7176 		/* We are already connected AND the TCP model */
7177 		SCTP_INP_RUNLOCK(inp);
7178 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7179 		return (EADDRINUSE);
7180 	}
7181 	SCTP_INP_RUNLOCK(inp);
7182 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7183 		/* We must do a bind. */
7184 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7185 			/* bind error, probably perm */
7186 			return (error);
7187 		}
7188 	}
7189 	SOCK_LOCK(so);
7190 	/* It appears for 7.0 and on, we must always call this. */
7191 	solisten_proto(so, backlog);
7192 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7193 		/* remove the ACCEPTCONN flag for one-to-many sockets */
7194 		so->so_options &= ~SO_ACCEPTCONN;
7195 	}
7196 	if (backlog == 0) {
7197 		/* turning off listen */
7198 		so->so_options &= ~SO_ACCEPTCONN;
7199 	}
7200 	SOCK_UNLOCK(so);
7201 	return (error);
7202 }
7203 
7204 static int sctp_defered_wakeup_cnt = 0;
7205 
7206 int
7207 sctp_accept(struct socket *so, struct sockaddr **addr)
7208 {
7209 	struct sctp_tcb *stcb;
7210 	struct sctp_inpcb *inp;
7211 	union sctp_sockstore store;
7212 
7213 #ifdef INET6
7214 	int error;
7215 
7216 #endif
7217 	inp = (struct sctp_inpcb *)so->so_pcb;
7218 
7219 	if (inp == NULL) {
7220 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7221 		return (ECONNRESET);
7222 	}
7223 	SCTP_INP_RLOCK(inp);
7224 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7225 		SCTP_INP_RUNLOCK(inp);
7226 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7227 		return (EOPNOTSUPP);
7228 	}
7229 	if (so->so_state & SS_ISDISCONNECTED) {
7230 		SCTP_INP_RUNLOCK(inp);
7231 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7232 		return (ECONNABORTED);
7233 	}
7234 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7235 	if (stcb == NULL) {
7236 		SCTP_INP_RUNLOCK(inp);
7237 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7238 		return (ECONNRESET);
7239 	}
7240 	SCTP_TCB_LOCK(stcb);
7241 	SCTP_INP_RUNLOCK(inp);
7242 	store = stcb->asoc.primary_destination->ro._l_addr;
7243 	stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
7244 	SCTP_TCB_UNLOCK(stcb);
7245 	switch (store.sa.sa_family) {
7246 #ifdef INET
7247 	case AF_INET:
7248 		{
7249 			struct sockaddr_in *sin;
7250 
7251 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7252 			if (sin == NULL)
7253 				return (ENOMEM);
7254 			sin->sin_family = AF_INET;
7255 			sin->sin_len = sizeof(*sin);
7256 			sin->sin_port = store.sin.sin_port;
7257 			sin->sin_addr = store.sin.sin_addr;
7258 			*addr = (struct sockaddr *)sin;
7259 			break;
7260 		}
7261 #endif
7262 #ifdef INET6
7263 	case AF_INET6:
7264 		{
7265 			struct sockaddr_in6 *sin6;
7266 
7267 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7268 			if (sin6 == NULL)
7269 				return (ENOMEM);
7270 			sin6->sin6_family = AF_INET6;
7271 			sin6->sin6_len = sizeof(*sin6);
7272 			sin6->sin6_port = store.sin6.sin6_port;
7273 			sin6->sin6_addr = store.sin6.sin6_addr;
7274 			if ((error = sa6_recoverscope(sin6)) != 0) {
7275 				SCTP_FREE_SONAME(sin6);
7276 				return (error);
7277 			}
7278 			*addr = (struct sockaddr *)sin6;
7279 			break;
7280 		}
7281 #endif
7282 	default:
7283 		/* TSNH */
7284 		break;
7285 	}
7286 	/* Wake any delayed sleep action */
7287 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7288 		SCTP_INP_WLOCK(inp);
7289 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7290 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7291 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7292 			SCTP_INP_WUNLOCK(inp);
7293 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7294 			if (sowriteable(inp->sctp_socket)) {
7295 				sowwakeup_locked(inp->sctp_socket);
7296 			} else {
7297 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7298 			}
7299 			SCTP_INP_WLOCK(inp);
7300 		}
7301 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7302 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7303 			SCTP_INP_WUNLOCK(inp);
7304 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7305 			if (soreadable(inp->sctp_socket)) {
7306 				sctp_defered_wakeup_cnt++;
7307 				sorwakeup_locked(inp->sctp_socket);
7308 			} else {
7309 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7310 			}
7311 			SCTP_INP_WLOCK(inp);
7312 		}
7313 		SCTP_INP_WUNLOCK(inp);
7314 	}
7315 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7316 		SCTP_TCB_LOCK(stcb);
7317 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7318 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7319 	}
7320 	return (0);
7321 }
7322 
7323 #ifdef INET
7324 int
7325 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7326 {
7327 	struct sockaddr_in *sin;
7328 	uint32_t vrf_id;
7329 	struct sctp_inpcb *inp;
7330 	struct sctp_ifa *sctp_ifa;
7331 
7332 	/*
7333 	 * Do the malloc first in case it blocks.
7334 	 */
7335 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7336 	if (sin == NULL)
7337 		return (ENOMEM);
7338 	sin->sin_family = AF_INET;
7339 	sin->sin_len = sizeof(*sin);
7340 	inp = (struct sctp_inpcb *)so->so_pcb;
7341 	if (!inp) {
7342 		SCTP_FREE_SONAME(sin);
7343 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7344 		return (ECONNRESET);
7345 	}
7346 	SCTP_INP_RLOCK(inp);
7347 	sin->sin_port = inp->sctp_lport;
7348 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7349 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7350 			struct sctp_tcb *stcb;
7351 			struct sockaddr_in *sin_a;
7352 			struct sctp_nets *net;
7353 			int fnd;
7354 
7355 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
7356 			if (stcb == NULL) {
7357 				goto notConn;
7358 			}
7359 			fnd = 0;
7360 			sin_a = NULL;
7361 			SCTP_TCB_LOCK(stcb);
7362 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7363 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7364 				if (sin_a == NULL)
7365 					/* this will make coverity happy */
7366 					continue;
7367 
7368 				if (sin_a->sin_family == AF_INET) {
7369 					fnd = 1;
7370 					break;
7371 				}
7372 			}
7373 			if ((!fnd) || (sin_a == NULL)) {
7374 				/* punt */
7375 				SCTP_TCB_UNLOCK(stcb);
7376 				goto notConn;
7377 			}
7378 			vrf_id = inp->def_vrf_id;
7379 			sctp_ifa = sctp_source_address_selection(inp,
7380 			    stcb,
7381 			    (sctp_route_t *) & net->ro,
7382 			    net, 0, vrf_id);
7383 			if (sctp_ifa) {
7384 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7385 				sctp_free_ifa(sctp_ifa);
7386 			}
7387 			SCTP_TCB_UNLOCK(stcb);
7388 		} else {
7389 			/* For the bound all case you get back 0 */
7390 	notConn:
7391 			sin->sin_addr.s_addr = 0;
7392 		}
7393 
7394 	} else {
7395 		/* Take the first IPv4 address in the list */
7396 		struct sctp_laddr *laddr;
7397 		int fnd = 0;
7398 
7399 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7400 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
7401 				struct sockaddr_in *sin_a;
7402 
7403 				sin_a = &laddr->ifa->address.sin;
7404 				sin->sin_addr = sin_a->sin_addr;
7405 				fnd = 1;
7406 				break;
7407 			}
7408 		}
7409 		if (!fnd) {
7410 			SCTP_FREE_SONAME(sin);
7411 			SCTP_INP_RUNLOCK(inp);
7412 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7413 			return (ENOENT);
7414 		}
7415 	}
7416 	SCTP_INP_RUNLOCK(inp);
7417 	(*addr) = (struct sockaddr *)sin;
7418 	return (0);
7419 }
7420 
7421 int
7422 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7423 {
7424 	struct sockaddr_in *sin;
7425 	int fnd;
7426 	struct sockaddr_in *sin_a;
7427 	struct sctp_inpcb *inp;
7428 	struct sctp_tcb *stcb;
7429 	struct sctp_nets *net;
7430 
7431 	/* Do the malloc first in case it blocks. */
7432 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7433 	if (sin == NULL)
7434 		return (ENOMEM);
7435 	sin->sin_family = AF_INET;
7436 	sin->sin_len = sizeof(*sin);
7437 
7438 	inp = (struct sctp_inpcb *)so->so_pcb;
7439 	if ((inp == NULL) ||
7440 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7441 		/* UDP type and listeners will drop out here */
7442 		SCTP_FREE_SONAME(sin);
7443 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7444 		return (ENOTCONN);
7445 	}
7446 	SCTP_INP_RLOCK(inp);
7447 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7448 	if (stcb) {
7449 		SCTP_TCB_LOCK(stcb);
7450 	}
7451 	SCTP_INP_RUNLOCK(inp);
7452 	if (stcb == NULL) {
7453 		SCTP_FREE_SONAME(sin);
7454 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7455 		return (ECONNRESET);
7456 	}
7457 	fnd = 0;
7458 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7459 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7460 		if (sin_a->sin_family == AF_INET) {
7461 			fnd = 1;
7462 			sin->sin_port = stcb->rport;
7463 			sin->sin_addr = sin_a->sin_addr;
7464 			break;
7465 		}
7466 	}
7467 	SCTP_TCB_UNLOCK(stcb);
7468 	if (!fnd) {
7469 		/* No IPv4 address */
7470 		SCTP_FREE_SONAME(sin);
7471 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7472 		return (ENOENT);
7473 	}
7474 	(*addr) = (struct sockaddr *)sin;
7475 	return (0);
7476 }
7477 
7478 struct pr_usrreqs sctp_usrreqs = {
7479 	.pru_abort = sctp_abort,
7480 	.pru_accept = sctp_accept,
7481 	.pru_attach = sctp_attach,
7482 	.pru_bind = sctp_bind,
7483 	.pru_connect = sctp_connect,
7484 	.pru_control = in_control,
7485 	.pru_close = sctp_close,
7486 	.pru_detach = sctp_close,
7487 	.pru_sopoll = sopoll_generic,
7488 	.pru_flush = sctp_flush,
7489 	.pru_disconnect = sctp_disconnect,
7490 	.pru_listen = sctp_listen,
7491 	.pru_peeraddr = sctp_peeraddr,
7492 	.pru_send = sctp_sendm,
7493 	.pru_shutdown = sctp_shutdown,
7494 	.pru_sockaddr = sctp_ingetaddr,
7495 	.pru_sosend = sctp_sosend,
7496 	.pru_soreceive = sctp_soreceive
7497 };
7498 
7499 #endif
7500