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