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