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