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