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