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