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