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