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