xref: /freebsd/sys/netinet/sctp_usrreq.c (revision fa030de01267cc324a0b57479715e4a53356b665)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
36 #include <sys/proc.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
40 #if defined(INET6)
41 #include <netinet6/sctp6_var.h>
42 #endif
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_bsd_addr.h>
52 #include <netinet/sctp_cc_functions.h>
53 
54 
55 
56 
57 void
58 sctp_init(void)
59 {
60 	/* Init the SCTP pcb in sctp_pcb.c */
61 	u_long sb_max_adj;
62 
63 	sctp_pcb_init();
64 
65 
66 	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 		sctp_max_chunks_on_queue = (nmbclusters / 8);
68 	/*
69 	 * Allow a user to take no more than 1/2 the number of clusters or
70 	 * the SB_MAX whichever is smaller for the send window.
71 	 */
72 	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
74 	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
75 	/*
76 	 * Now for the recv window, should we take the same amount? or
77 	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 	 * now I will just copy.
79 	 */
80 	sctp_recvspace = sctp_sendspace;
81 
82 }
83 
84 
85 
86 /*
87  * cleanup of the sctppcbinfo structure.
88  * Assumes that the sctppcbinfo lock is held.
89  */
90 void
91 sctp_pcbinfo_cleanup(void)
92 {
93 	/* free the hash tables */
94 	if (sctppcbinfo.sctp_asochash != NULL)
95 		SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
96 	if (sctppcbinfo.sctp_ephash != NULL)
97 		SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
98 	if (sctppcbinfo.sctp_tcpephash != NULL)
99 		SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
100 	if (sctppcbinfo.sctp_restarthash != NULL)
101 		SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
102 }
103 
104 
105 static void
106 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
107     struct sctp_tcb *stcb,
108     struct sctp_nets *net,
109     uint16_t nxtsz)
110 {
111 	struct sctp_tmit_chunk *chk;
112 
113 	/* Adjust that too */
114 	stcb->asoc.smallest_mtu = nxtsz;
115 	/* now off to subtract IP_DF flag if needed */
116 #ifdef SCTP_PRINT_FOR_B_AND_M
117 	SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
118 	    inp, stcb, net, nxtsz);
119 #endif
120 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
121 		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
123 		}
124 	}
125 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
126 		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
127 			/*
128 			 * For this guy we also mark for immediate resend
129 			 * since we sent to big of chunk
130 			 */
131 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
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_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 			sctp_flight_size_decrease(chk);
147 			sctp_total_flight_decrease(stcb, chk);
148 		}
149 	}
150 }
151 
152 static void
153 sctp_notify_mbuf(struct sctp_inpcb *inp,
154     struct sctp_tcb *stcb,
155     struct sctp_nets *net,
156     struct ip *ip,
157     struct sctphdr *sh)
158 {
159 	struct icmp *icmph;
160 	int totsz, tmr_stopped = 0;
161 	uint16_t nxtsz;
162 
163 	/* protection */
164 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165 	    (ip == NULL) || (sh == NULL)) {
166 		if (stcb != NULL) {
167 			SCTP_TCB_UNLOCK(stcb);
168 		}
169 		return;
170 	}
171 	/* First job is to verify the vtag matches what I would send */
172 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173 		SCTP_TCB_UNLOCK(stcb);
174 		return;
175 	}
176 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
177 	    sizeof(struct ip)));
178 	if (icmph->icmp_type != ICMP_UNREACH) {
179 		/* We only care about unreachable */
180 		SCTP_TCB_UNLOCK(stcb);
181 		return;
182 	}
183 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184 		/* not a unreachable message due to frag. */
185 		SCTP_TCB_UNLOCK(stcb);
186 		return;
187 	}
188 	totsz = ip->ip_len;
189 
190 	nxtsz = ntohs(icmph->icmp_nextmtu);
191 	if (nxtsz == 0) {
192 		/*
193 		 * old type router that does not tell us what the next size
194 		 * mtu is. Rats we will have to guess (in a educated fashion
195 		 * of course)
196 		 */
197 		nxtsz = find_next_best_mtu(totsz);
198 	}
199 	/* Stop any PMTU timer */
200 	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
201 		tmr_stopped = 1;
202 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
203 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
204 	}
205 	/* Adjust destination size limit */
206 	if (net->mtu > nxtsz) {
207 		net->mtu = nxtsz;
208 	}
209 	/* now what about the ep? */
210 	if (stcb->asoc.smallest_mtu > nxtsz) {
211 #ifdef SCTP_PRINT_FOR_B_AND_M
212 		SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
213 		    nxtsz);
214 #endif
215 		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
216 	}
217 	if (tmr_stopped)
218 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
219 
220 	SCTP_TCB_UNLOCK(stcb);
221 }
222 
223 
224 void
225 sctp_notify(struct sctp_inpcb *inp,
226     struct ip *ip,
227     struct sctphdr *sh,
228     struct sockaddr *to,
229     struct sctp_tcb *stcb,
230     struct sctp_nets *net)
231 {
232 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
233 	struct socket *so;
234 
235 #endif
236 	/* protection */
237 	int reason;
238 	struct icmp *icmph;
239 
240 
241 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
242 	    (sh == NULL) || (to == NULL)) {
243 		if (stcb)
244 			SCTP_TCB_UNLOCK(stcb);
245 		return;
246 	}
247 	/* First job is to verify the vtag matches what I would send */
248 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
249 		SCTP_TCB_UNLOCK(stcb);
250 		return;
251 	}
252 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
253 	    sizeof(struct ip)));
254 	if (icmph->icmp_type != ICMP_UNREACH) {
255 		/* We only care about unreachable */
256 		SCTP_TCB_UNLOCK(stcb);
257 		return;
258 	}
259 	if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
260 	    (icmph->icmp_code == ICMP_UNREACH_HOST) ||
261 	    (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
262 	    (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
263 	    (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
264 	    (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
265 	    (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
266 	    (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
267 
268 		/*
269 		 * Hmm reachablity problems we must examine closely. If its
270 		 * not reachable, we may have lost a network. Or if there is
271 		 * NO protocol at the other end named SCTP. well we consider
272 		 * it a OOTB abort.
273 		 */
274 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
275 			/* Ok that destination is NOT reachable */
276 			SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
277 			    net->error_count,
278 			    net->failure_threshold,
279 			    net);
280 
281 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
282 			net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
283 			/*
284 			 * JRS 5/14/07 - If a destination is unreachable,
285 			 * the PF bit is turned off.  This allows an
286 			 * unambiguous use of the PF bit for destinations
287 			 * that are reachable but potentially failed. If the
288 			 * destination is set to the unreachable state, also
289 			 * set the destination to the PF state.
290 			 */
291 			/*
292 			 * Add debug message here if destination is not in
293 			 * PF state.
294 			 */
295 			/* Stop any running T3 timers here? */
296 			if (sctp_cmt_on_off && sctp_cmt_pf) {
297 				net->dest_state &= ~SCTP_ADDR_PF;
298 				SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
299 				    net);
300 			}
301 			net->error_count = net->failure_threshold + 1;
302 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
303 			    stcb, SCTP_FAILED_THRESHOLD,
304 			    (void *)net, SCTP_SO_NOT_LOCKED);
305 		}
306 		SCTP_TCB_UNLOCK(stcb);
307 	} else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
308 	    (icmph->icmp_code == ICMP_UNREACH_PORT)) {
309 		/*
310 		 * Here the peer is either playing tricks on us, including
311 		 * an address that belongs to someone who does not support
312 		 * SCTP OR was a userland implementation that shutdown and
313 		 * now is dead. In either case treat it like a OOTB abort
314 		 * with no TCB
315 		 */
316 		reason = SCTP_PEER_FAULTY;
317 		sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
318 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
319 		so = SCTP_INP_SO(inp);
320 		atomic_add_int(&stcb->asoc.refcnt, 1);
321 		SCTP_TCB_UNLOCK(stcb);
322 		SCTP_SOCKET_LOCK(so, 1);
323 		SCTP_TCB_LOCK(stcb);
324 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
325 #endif
326 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
327 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
328 		SCTP_SOCKET_UNLOCK(so, 1);
329 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
330 #endif
331 		/* no need to unlock here, since the TCB is gone */
332 	} else {
333 		SCTP_TCB_UNLOCK(stcb);
334 	}
335 }
336 
337 void
338 sctp_ctlinput(cmd, sa, vip)
339 	int cmd;
340 	struct sockaddr *sa;
341 	void *vip;
342 {
343 	struct ip *ip = vip;
344 	struct sctphdr *sh;
345 	uint32_t vrf_id;
346 
347 	/* FIX, for non-bsd is this right? */
348 	vrf_id = SCTP_DEFAULT_VRFID;
349 	if (sa->sa_family != AF_INET ||
350 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
351 		return;
352 	}
353 	if (PRC_IS_REDIRECT(cmd)) {
354 		ip = 0;
355 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
356 		return;
357 	}
358 	if (ip) {
359 		struct sctp_inpcb *inp = NULL;
360 		struct sctp_tcb *stcb = NULL;
361 		struct sctp_nets *net = NULL;
362 		struct sockaddr_in to, from;
363 
364 		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
365 		bzero(&to, sizeof(to));
366 		bzero(&from, sizeof(from));
367 		from.sin_family = to.sin_family = AF_INET;
368 		from.sin_len = to.sin_len = sizeof(to);
369 		from.sin_port = sh->src_port;
370 		from.sin_addr = ip->ip_src;
371 		to.sin_port = sh->dest_port;
372 		to.sin_addr = ip->ip_dst;
373 
374 		/*
375 		 * 'to' holds the dest of the packet that failed to be sent.
376 		 * 'from' holds our local endpoint address. Thus we reverse
377 		 * the to and the from in the lookup.
378 		 */
379 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
380 		    (struct sockaddr *)&to,
381 		    &inp, &net, 1, vrf_id);
382 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
383 			if (cmd != PRC_MSGSIZE) {
384 				sctp_notify(inp, ip, sh,
385 				    (struct sockaddr *)&to, stcb,
386 				    net);
387 			} else {
388 				/* handle possible ICMP size messages */
389 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
390 			}
391 		} else {
392 			if ((stcb == NULL) && (inp != NULL)) {
393 				/* reduce ref-count */
394 				SCTP_INP_WLOCK(inp);
395 				SCTP_INP_DECR_REF(inp);
396 				SCTP_INP_WUNLOCK(inp);
397 			}
398 		}
399 	}
400 	return;
401 }
402 
403 static int
404 sctp_getcred(SYSCTL_HANDLER_ARGS)
405 {
406 	struct xucred xuc;
407 	struct sockaddr_in addrs[2];
408 	struct sctp_inpcb *inp;
409 	struct sctp_nets *net;
410 	struct sctp_tcb *stcb;
411 	int error;
412 	uint32_t vrf_id;
413 
414 	/* FIX, for non-bsd is this right? */
415 	vrf_id = SCTP_DEFAULT_VRFID;
416 
417 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
418 
419 	if (error)
420 		return (error);
421 
422 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
423 	if (error)
424 		return (error);
425 
426 	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
427 	    sintosa(&addrs[1]),
428 	    &inp, &net, 1, vrf_id);
429 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
430 		if ((inp != NULL) && (stcb == NULL)) {
431 			/* reduce ref-count */
432 			SCTP_INP_WLOCK(inp);
433 			SCTP_INP_DECR_REF(inp);
434 			goto cred_can_cont;
435 		}
436 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
437 		error = ENOENT;
438 		goto out;
439 	}
440 	SCTP_TCB_UNLOCK(stcb);
441 	/*
442 	 * We use the write lock here, only since in the error leg we need
443 	 * it. If we used RLOCK, then we would have to
444 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
445 	 * Better to use higher wlock.
446 	 */
447 	SCTP_INP_WLOCK(inp);
448 cred_can_cont:
449 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
450 	if (error) {
451 		SCTP_INP_WUNLOCK(inp);
452 		goto out;
453 	}
454 	cru2x(inp->sctp_socket->so_cred, &xuc);
455 	SCTP_INP_WUNLOCK(inp);
456 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
457 out:
458 	return (error);
459 }
460 
461 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
462     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
463 
464 
465 static void
466 sctp_abort(struct socket *so)
467 {
468 	struct sctp_inpcb *inp;
469 	uint32_t flags;
470 
471 	inp = (struct sctp_inpcb *)so->so_pcb;
472 	if (inp == 0) {
473 		return;
474 	}
475 sctp_must_try_again:
476 	flags = inp->sctp_flags;
477 #ifdef SCTP_LOG_CLOSING
478 	sctp_log_closing(inp, NULL, 17);
479 #endif
480 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
481 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
482 #ifdef SCTP_LOG_CLOSING
483 		sctp_log_closing(inp, NULL, 16);
484 #endif
485 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
486 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
487 		SOCK_LOCK(so);
488 		SCTP_SB_CLEAR(so->so_snd);
489 		/*
490 		 * same for the rcv ones, they are only here for the
491 		 * accounting/select.
492 		 */
493 		SCTP_SB_CLEAR(so->so_rcv);
494 
495 		/* Now null out the reference, we are completely detached. */
496 		so->so_pcb = NULL;
497 		SOCK_UNLOCK(so);
498 	} else {
499 		flags = inp->sctp_flags;
500 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
501 			goto sctp_must_try_again;
502 		}
503 	}
504 	return;
505 }
506 
507 static int
508 sctp_attach(struct socket *so, int proto, struct thread *p)
509 {
510 	struct sctp_inpcb *inp;
511 	struct inpcb *ip_inp;
512 	int error;
513 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
514 
515 #ifdef IPSEC
516 	uint32_t flags;
517 
518 #endif
519 	inp = (struct sctp_inpcb *)so->so_pcb;
520 	if (inp != 0) {
521 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
522 		return EINVAL;
523 	}
524 	error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
525 	if (error) {
526 		return error;
527 	}
528 	error = sctp_inpcb_alloc(so, vrf_id);
529 	if (error) {
530 		return error;
531 	}
532 	inp = (struct sctp_inpcb *)so->so_pcb;
533 	SCTP_INP_WLOCK(inp);
534 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
535 	ip_inp = &inp->ip_inp.inp;
536 	ip_inp->inp_vflag |= INP_IPV4;
537 	ip_inp->inp_ip_ttl = ip_defttl;
538 #ifdef IPSEC
539 	error = ipsec_init_policy(so, &ip_inp->inp_sp);
540 #ifdef SCTP_LOG_CLOSING
541 	sctp_log_closing(inp, NULL, 17);
542 #endif
543 	if (error != 0) {
544 		flags = inp->sctp_flags;
545 		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
546 		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
547 #ifdef SCTP_LOG_CLOSING
548 			sctp_log_closing(inp, NULL, 15);
549 #endif
550 			SCTP_INP_WUNLOCK(inp);
551 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
552 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
553 		} else {
554 			SCTP_INP_WUNLOCK(inp);
555 		}
556 		return error;
557 	}
558 #endif				/* IPSEC */
559 	SCTP_INP_WUNLOCK(inp);
560 	return 0;
561 }
562 
563 static int
564 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
565 {
566 	struct sctp_inpcb *inp = NULL;
567 	int error;
568 
569 #ifdef INET6
570 	if (addr && addr->sa_family != AF_INET) {
571 		/* must be a v4 address! */
572 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
573 		return EINVAL;
574 	}
575 #endif				/* INET6 */
576 	if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
577 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
578 		return EINVAL;
579 	}
580 	inp = (struct sctp_inpcb *)so->so_pcb;
581 	if (inp == 0) {
582 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
583 		return EINVAL;
584 	}
585 	error = sctp_inpcb_bind(so, addr, NULL, p);
586 	return error;
587 }
588 
589 void
590 sctp_close(struct socket *so)
591 {
592 	struct sctp_inpcb *inp;
593 	uint32_t flags;
594 
595 	inp = (struct sctp_inpcb *)so->so_pcb;
596 	if (inp == 0)
597 		return;
598 
599 	/*
600 	 * Inform all the lower layer assoc that we are done.
601 	 */
602 sctp_must_try_again:
603 	flags = inp->sctp_flags;
604 #ifdef SCTP_LOG_CLOSING
605 	sctp_log_closing(inp, NULL, 17);
606 #endif
607 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
608 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
609 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
610 		    (so->so_rcv.sb_cc > 0)) {
611 #ifdef SCTP_LOG_CLOSING
612 			sctp_log_closing(inp, NULL, 13);
613 #endif
614 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
615 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
616 		} else {
617 #ifdef SCTP_LOG_CLOSING
618 			sctp_log_closing(inp, NULL, 14);
619 #endif
620 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
621 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
622 		}
623 		/*
624 		 * The socket is now detached, no matter what the state of
625 		 * the SCTP association.
626 		 */
627 		SOCK_LOCK(so);
628 		SCTP_SB_CLEAR(so->so_snd);
629 		/*
630 		 * same for the rcv ones, they are only here for the
631 		 * accounting/select.
632 		 */
633 		SCTP_SB_CLEAR(so->so_rcv);
634 
635 		/* Now null out the reference, we are completely detached. */
636 		so->so_pcb = NULL;
637 		SOCK_UNLOCK(so);
638 	} else {
639 		flags = inp->sctp_flags;
640 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
641 			goto sctp_must_try_again;
642 		}
643 	}
644 	return;
645 }
646 
647 
648 int
649 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
650     struct mbuf *control, struct thread *p);
651 
652 
653 int
654 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
655     struct mbuf *control, struct thread *p)
656 {
657 	struct sctp_inpcb *inp;
658 	int error;
659 
660 	inp = (struct sctp_inpcb *)so->so_pcb;
661 	if (inp == 0) {
662 		if (control) {
663 			sctp_m_freem(control);
664 			control = NULL;
665 		}
666 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
667 		sctp_m_freem(m);
668 		return EINVAL;
669 	}
670 	/* Got to have an to address if we are NOT a connected socket */
671 	if ((addr == NULL) &&
672 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
673 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
674 	    ) {
675 		goto connected_type;
676 	} else if (addr == NULL) {
677 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
678 		error = EDESTADDRREQ;
679 		sctp_m_freem(m);
680 		if (control) {
681 			sctp_m_freem(control);
682 			control = NULL;
683 		}
684 		return (error);
685 	}
686 #ifdef INET6
687 	if (addr->sa_family != AF_INET) {
688 		/* must be a v4 address! */
689 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
690 		sctp_m_freem(m);
691 		if (control) {
692 			sctp_m_freem(control);
693 			control = NULL;
694 		}
695 		error = EDESTADDRREQ;
696 		return EDESTADDRREQ;
697 	}
698 #endif				/* INET6 */
699 connected_type:
700 	/* now what about control */
701 	if (control) {
702 		if (inp->control) {
703 			SCTP_PRINTF("huh? control set?\n");
704 			sctp_m_freem(inp->control);
705 			inp->control = NULL;
706 		}
707 		inp->control = control;
708 	}
709 	/* Place the data */
710 	if (inp->pkt) {
711 		SCTP_BUF_NEXT(inp->pkt_last) = m;
712 		inp->pkt_last = m;
713 	} else {
714 		inp->pkt_last = inp->pkt = m;
715 	}
716 	if (
717 	/* FreeBSD uses a flag passed */
718 	    ((flags & PRUS_MORETOCOME) == 0)
719 	    ) {
720 		/*
721 		 * note with the current version this code will only be used
722 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
723 		 * re-defining sosend to use the sctp_sosend. One can
724 		 * optionally switch back to this code (by changing back the
725 		 * definitions) but this is not advisable. This code is used
726 		 * by FreeBSD when sending a file with sendfile() though.
727 		 */
728 		int ret;
729 
730 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
731 		inp->pkt = NULL;
732 		inp->control = NULL;
733 		return (ret);
734 	} else {
735 		return (0);
736 	}
737 }
738 
739 int
740 sctp_disconnect(struct socket *so)
741 {
742 	struct sctp_inpcb *inp;
743 
744 	inp = (struct sctp_inpcb *)so->so_pcb;
745 	if (inp == NULL) {
746 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
747 		return (ENOTCONN);
748 	}
749 	SCTP_INP_RLOCK(inp);
750 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
751 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
752 		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
753 			/* No connection */
754 			SCTP_INP_RUNLOCK(inp);
755 			return (0);
756 		} else {
757 			struct sctp_association *asoc;
758 			struct sctp_tcb *stcb;
759 
760 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
761 			if (stcb == NULL) {
762 				SCTP_INP_RUNLOCK(inp);
763 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
764 				return (EINVAL);
765 			}
766 			SCTP_TCB_LOCK(stcb);
767 			asoc = &stcb->asoc;
768 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
769 				/* We are about to be freed, out of here */
770 				SCTP_TCB_UNLOCK(stcb);
771 				SCTP_INP_RUNLOCK(inp);
772 				return (0);
773 			}
774 			if (((so->so_options & SO_LINGER) &&
775 			    (so->so_linger == 0)) ||
776 			    (so->so_rcv.sb_cc > 0)) {
777 				if (SCTP_GET_STATE(asoc) !=
778 				    SCTP_STATE_COOKIE_WAIT) {
779 					/* Left with Data unread */
780 					struct mbuf *err;
781 
782 					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
783 					if (err) {
784 						/*
785 						 * Fill in the user
786 						 * initiated abort
787 						 */
788 						struct sctp_paramhdr *ph;
789 
790 						ph = mtod(err, struct sctp_paramhdr *);
791 						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
792 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
793 						ph->param_length = htons(SCTP_BUF_LEN(err));
794 					}
795 #if defined(SCTP_PANIC_ON_ABORT)
796 					panic("disconnect does an abort");
797 #endif
798 					sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
799 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
800 				}
801 				SCTP_INP_RUNLOCK(inp);
802 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
803 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
804 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
805 				}
806 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
807 				/* No unlock tcb assoc is gone */
808 				return (0);
809 			}
810 			if (TAILQ_EMPTY(&asoc->send_queue) &&
811 			    TAILQ_EMPTY(&asoc->sent_queue) &&
812 			    (asoc->stream_queue_cnt == 0)) {
813 				/* there is nothing queued to send, so done */
814 				if (asoc->locked_on_sending) {
815 					goto abort_anyway;
816 				}
817 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
818 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
819 					/* only send SHUTDOWN 1st time thru */
820 					sctp_stop_timers_for_shutdown(stcb);
821 					sctp_send_shutdown(stcb,
822 					    stcb->asoc.primary_destination);
823 					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
824 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
825 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
826 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
827 					}
828 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
829 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
830 					    stcb->sctp_ep, stcb,
831 					    asoc->primary_destination);
832 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
833 					    stcb->sctp_ep, stcb,
834 					    asoc->primary_destination);
835 				}
836 			} else {
837 				/*
838 				 * we still got (or just got) data to send,
839 				 * so set SHUTDOWN_PENDING
840 				 */
841 				/*
842 				 * XXX sockets draft says that SCTP_EOF
843 				 * should be sent with no data. currently,
844 				 * we will allow user data to be sent first
845 				 * and move to SHUTDOWN-PENDING
846 				 */
847 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
848 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
849 				    asoc->primary_destination);
850 				if (asoc->locked_on_sending) {
851 					/* Locked to send out the data */
852 					struct sctp_stream_queue_pending *sp;
853 
854 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
855 					if (sp == NULL) {
856 						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
857 						    asoc->locked_on_sending->stream_no);
858 					} else {
859 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
860 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
861 					}
862 				}
863 				if (TAILQ_EMPTY(&asoc->send_queue) &&
864 				    TAILQ_EMPTY(&asoc->sent_queue) &&
865 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
866 					struct mbuf *op_err;
867 
868 			abort_anyway:
869 					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
870 					    0, M_DONTWAIT, 1, MT_DATA);
871 					if (op_err) {
872 						/*
873 						 * Fill in the user
874 						 * initiated abort
875 						 */
876 						struct sctp_paramhdr *ph;
877 						uint32_t *ippp;
878 
879 						SCTP_BUF_LEN(op_err) =
880 						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
881 						ph = mtod(op_err,
882 						    struct sctp_paramhdr *);
883 						ph->param_type = htons(
884 						    SCTP_CAUSE_USER_INITIATED_ABT);
885 						ph->param_length = htons(SCTP_BUF_LEN(op_err));
886 						ippp = (uint32_t *) (ph + 1);
887 						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
888 					}
889 #if defined(SCTP_PANIC_ON_ABORT)
890 					panic("disconnect does an abort");
891 #endif
892 
893 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
894 					sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
895 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
896 					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
897 					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
898 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
899 					}
900 					SCTP_INP_RUNLOCK(inp);
901 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
902 					return (0);
903 				} else {
904 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
905 				}
906 			}
907 			SCTP_TCB_UNLOCK(stcb);
908 			SCTP_INP_RUNLOCK(inp);
909 			return (0);
910 		}
911 		/* not reached */
912 	} else {
913 		/* UDP model does not support this */
914 		SCTP_INP_RUNLOCK(inp);
915 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
916 		return EOPNOTSUPP;
917 	}
918 }
919 
920 int
921 sctp_shutdown(struct socket *so)
922 {
923 	struct sctp_inpcb *inp;
924 
925 	inp = (struct sctp_inpcb *)so->so_pcb;
926 	if (inp == 0) {
927 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
928 		return EINVAL;
929 	}
930 	SCTP_INP_RLOCK(inp);
931 	/* For UDP model this is a invalid call */
932 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
933 		/* Restore the flags that the soshutdown took away. */
934 		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
935 		/* This proc will wakeup for read and do nothing (I hope) */
936 		SCTP_INP_RUNLOCK(inp);
937 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
938 		return (EOPNOTSUPP);
939 	}
940 	/*
941 	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
942 	 * or SHUT_RDWR. This means we put the shutdown flag against it.
943 	 */
944 	{
945 		struct sctp_tcb *stcb;
946 		struct sctp_association *asoc;
947 
948 		socantsendmore(so);
949 
950 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
951 		if (stcb == NULL) {
952 			/*
953 			 * Ok we hit the case that the shutdown call was
954 			 * made after an abort or something. Nothing to do
955 			 * now.
956 			 */
957 			SCTP_INP_RUNLOCK(inp);
958 			return (0);
959 		}
960 		SCTP_TCB_LOCK(stcb);
961 		asoc = &stcb->asoc;
962 		if (TAILQ_EMPTY(&asoc->send_queue) &&
963 		    TAILQ_EMPTY(&asoc->sent_queue) &&
964 		    (asoc->stream_queue_cnt == 0)) {
965 			if (asoc->locked_on_sending) {
966 				goto abort_anyway;
967 			}
968 			/* there is nothing queued to send, so I'm done... */
969 			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
970 				/* only send SHUTDOWN the first time through */
971 				sctp_stop_timers_for_shutdown(stcb);
972 				sctp_send_shutdown(stcb,
973 				    stcb->asoc.primary_destination);
974 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
975 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
976 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
977 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
978 				}
979 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
980 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
981 				    stcb->sctp_ep, stcb,
982 				    asoc->primary_destination);
983 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
984 				    stcb->sctp_ep, stcb,
985 				    asoc->primary_destination);
986 			}
987 		} else {
988 			/*
989 			 * we still got (or just got) data to send, so set
990 			 * SHUTDOWN_PENDING
991 			 */
992 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
993 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
994 			    asoc->primary_destination);
995 
996 			if (asoc->locked_on_sending) {
997 				/* Locked to send out the data */
998 				struct sctp_stream_queue_pending *sp;
999 
1000 				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1001 				if (sp == NULL) {
1002 					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1003 					    asoc->locked_on_sending->stream_no);
1004 				} else {
1005 					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1006 						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1007 					}
1008 				}
1009 			}
1010 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1011 			    TAILQ_EMPTY(&asoc->sent_queue) &&
1012 			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1013 				struct mbuf *op_err;
1014 
1015 		abort_anyway:
1016 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1017 				    0, M_DONTWAIT, 1, MT_DATA);
1018 				if (op_err) {
1019 					/* Fill in the user initiated abort */
1020 					struct sctp_paramhdr *ph;
1021 					uint32_t *ippp;
1022 
1023 					SCTP_BUF_LEN(op_err) =
1024 					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1025 					ph = mtod(op_err,
1026 					    struct sctp_paramhdr *);
1027 					ph->param_type = htons(
1028 					    SCTP_CAUSE_USER_INITIATED_ABT);
1029 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
1030 					ippp = (uint32_t *) (ph + 1);
1031 					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1032 				}
1033 #if defined(SCTP_PANIC_ON_ABORT)
1034 				panic("shutdown does an abort");
1035 #endif
1036 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1037 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1038 				    SCTP_RESPONSE_TO_USER_REQ,
1039 				    op_err, SCTP_SO_LOCKED);
1040 				goto skip_unlock;
1041 			} else {
1042 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1043 			}
1044 		}
1045 		SCTP_TCB_UNLOCK(stcb);
1046 	}
1047 skip_unlock:
1048 	SCTP_INP_RUNLOCK(inp);
1049 	return 0;
1050 }
1051 
1052 /*
1053  * copies a "user" presentable address and removes embedded scope, etc.
1054  * returns 0 on success, 1 on error
1055  */
1056 static uint32_t
1057 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1058 {
1059 	struct sockaddr_in6 lsa6;
1060 
1061 	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1062 	    &lsa6);
1063 	memcpy(ss, sa, sa->sa_len);
1064 	return (0);
1065 }
1066 
1067 
1068 
1069 /*
1070  * NOTE: assumes addr lock is held
1071  */
1072 static size_t
1073 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1074     struct sctp_tcb *stcb,
1075     size_t limit,
1076     struct sockaddr_storage *sas,
1077     uint32_t vrf_id)
1078 {
1079 	struct sctp_ifn *sctp_ifn;
1080 	struct sctp_ifa *sctp_ifa;
1081 	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1082 	size_t actual;
1083 	int ipv4_addr_legal, ipv6_addr_legal;
1084 	struct sctp_vrf *vrf;
1085 
1086 	actual = 0;
1087 	if (limit <= 0)
1088 		return (actual);
1089 
1090 	if (stcb) {
1091 		/* Turn on all the appropriate scope */
1092 		loopback_scope = stcb->asoc.loopback_scope;
1093 		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1094 		local_scope = stcb->asoc.local_scope;
1095 		site_scope = stcb->asoc.site_scope;
1096 	} else {
1097 		/* Turn on ALL scope, since we look at the EP */
1098 		loopback_scope = ipv4_local_scope = local_scope =
1099 		    site_scope = 1;
1100 	}
1101 	ipv4_addr_legal = ipv6_addr_legal = 0;
1102 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1103 		ipv6_addr_legal = 1;
1104 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1105 			ipv4_addr_legal = 1;
1106 		}
1107 	} else {
1108 		ipv4_addr_legal = 1;
1109 	}
1110 	vrf = sctp_find_vrf(vrf_id);
1111 	if (vrf == NULL) {
1112 		return (0);
1113 	}
1114 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1115 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1116 			if ((loopback_scope == 0) &&
1117 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1118 				/* Skip loopback if loopback_scope not set */
1119 				continue;
1120 			}
1121 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1122 				if (stcb) {
1123 					/*
1124 					 * For the BOUND-ALL case, the list
1125 					 * associated with a TCB is Always
1126 					 * considered a reverse list.. i.e.
1127 					 * it lists addresses that are NOT
1128 					 * part of the association. If this
1129 					 * is one of those we must skip it.
1130 					 */
1131 					if (sctp_is_addr_restricted(stcb,
1132 					    sctp_ifa)) {
1133 						continue;
1134 					}
1135 				}
1136 				if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1137 				    (ipv4_addr_legal)) {
1138 					struct sockaddr_in *sin;
1139 
1140 					sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1141 					if (sin->sin_addr.s_addr == 0) {
1142 						/*
1143 						 * we skip unspecifed
1144 						 * addresses
1145 						 */
1146 						continue;
1147 					}
1148 					if ((ipv4_local_scope == 0) &&
1149 					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1150 						continue;
1151 					}
1152 					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1153 						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1154 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1155 						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1156 						actual += sizeof(struct sockaddr_in6);
1157 					} else {
1158 						memcpy(sas, sin, sizeof(*sin));
1159 						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1160 						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1161 						actual += sizeof(*sin);
1162 					}
1163 					if (actual >= limit) {
1164 						return (actual);
1165 					}
1166 				} else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1167 				    (ipv6_addr_legal)) {
1168 					struct sockaddr_in6 *sin6;
1169 
1170 					sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1171 					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1172 						/*
1173 						 * we skip unspecifed
1174 						 * addresses
1175 						 */
1176 						continue;
1177 					}
1178 					if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1179 						if (local_scope == 0)
1180 							continue;
1181 						if (sin6->sin6_scope_id == 0) {
1182 							if (sa6_recoverscope(sin6) != 0)
1183 								/*
1184 								 * bad link
1185 								 * local
1186 								 * address
1187 								 */
1188 								continue;
1189 						}
1190 					}
1191 					if ((site_scope == 0) &&
1192 					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1193 						continue;
1194 					}
1195 					memcpy(sas, sin6, sizeof(*sin6));
1196 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1197 					sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1198 					actual += sizeof(*sin6);
1199 					if (actual >= limit) {
1200 						return (actual);
1201 					}
1202 				}
1203 			}
1204 		}
1205 	} else {
1206 		struct sctp_laddr *laddr;
1207 
1208 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1209 			if (stcb) {
1210 				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1211 					continue;
1212 				}
1213 			}
1214 			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1215 				continue;
1216 
1217 			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1218 			sas = (struct sockaddr_storage *)((caddr_t)sas +
1219 			    laddr->ifa->address.sa.sa_len);
1220 			actual += laddr->ifa->address.sa.sa_len;
1221 			if (actual >= limit) {
1222 				return (actual);
1223 			}
1224 		}
1225 	}
1226 	return (actual);
1227 }
1228 
1229 static size_t
1230 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1231     struct sctp_tcb *stcb,
1232     size_t limit,
1233     struct sockaddr_storage *sas)
1234 {
1235 	size_t size = 0;
1236 
1237 	SCTP_IPI_ADDR_RLOCK();
1238 	/* fill up addresses for the endpoint's default vrf */
1239 	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1240 	    inp->def_vrf_id);
1241 	SCTP_IPI_ADDR_RUNLOCK();
1242 	return (size);
1243 }
1244 
1245 /*
1246  * NOTE: assumes addr lock is held
1247  */
1248 static int
1249 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1250 {
1251 	int cnt = 0;
1252 	struct sctp_vrf *vrf = NULL;
1253 
1254 	/*
1255 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1256 	 * number of addresses that you COULD get. In reality the sub-set
1257 	 * bound may have an exclusion list for a given TCB OR in the
1258 	 * bound-all case a TCB may NOT include the loopback or other
1259 	 * addresses as well.
1260 	 */
1261 	vrf = sctp_find_vrf(vrf_id);
1262 	if (vrf == NULL) {
1263 		return (0);
1264 	}
1265 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1266 		struct sctp_ifn *sctp_ifn;
1267 		struct sctp_ifa *sctp_ifa;
1268 
1269 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1270 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1271 				/* Count them if they are the right type */
1272 				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1273 					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1274 						cnt += sizeof(struct sockaddr_in6);
1275 					else
1276 						cnt += sizeof(struct sockaddr_in);
1277 
1278 				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1279 					cnt += sizeof(struct sockaddr_in6);
1280 			}
1281 		}
1282 	} else {
1283 		struct sctp_laddr *laddr;
1284 
1285 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1286 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1287 				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1288 					cnt += sizeof(struct sockaddr_in6);
1289 				else
1290 					cnt += sizeof(struct sockaddr_in);
1291 
1292 			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1293 				cnt += sizeof(struct sockaddr_in6);
1294 		}
1295 	}
1296 	return (cnt);
1297 }
1298 
1299 static int
1300 sctp_count_max_addresses(struct sctp_inpcb *inp)
1301 {
1302 	int cnt = 0;
1303 
1304 	SCTP_IPI_ADDR_RLOCK();
1305 	/* count addresses for the endpoint's default VRF */
1306 	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1307 	SCTP_IPI_ADDR_RUNLOCK();
1308 	return (cnt);
1309 }
1310 
1311 static int
1312 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1313     size_t optsize, void *p, int delay)
1314 {
1315 	int error = 0;
1316 	int creat_lock_on = 0;
1317 	struct sctp_tcb *stcb = NULL;
1318 	struct sockaddr *sa;
1319 	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1320 	int added = 0;
1321 	uint32_t vrf_id;
1322 	int bad_addresses = 0;
1323 	sctp_assoc_t *a_id;
1324 
1325 	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1326 
1327 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1328 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1329 		/* We are already connected AND the TCP model */
1330 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1331 		return (EADDRINUSE);
1332 	}
1333 	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1334 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1335 		return (EINVAL);
1336 	}
1337 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1338 		SCTP_INP_RLOCK(inp);
1339 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1340 		SCTP_INP_RUNLOCK(inp);
1341 	}
1342 	if (stcb) {
1343 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1344 		return (EALREADY);
1345 	}
1346 	SCTP_INP_INCR_REF(inp);
1347 	SCTP_ASOC_CREATE_LOCK(inp);
1348 	creat_lock_on = 1;
1349 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1350 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1351 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1352 		error = EFAULT;
1353 		goto out_now;
1354 	}
1355 	totaddrp = (int *)optval;
1356 	totaddr = *totaddrp;
1357 	sa = (struct sockaddr *)(totaddrp + 1);
1358 	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1359 	if ((stcb != NULL) || bad_addresses) {
1360 		/* Already have or am bring up an association */
1361 		SCTP_ASOC_CREATE_UNLOCK(inp);
1362 		creat_lock_on = 0;
1363 		if (stcb)
1364 			SCTP_TCB_UNLOCK(stcb);
1365 		if (bad_addresses == 0) {
1366 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1367 			error = EALREADY;
1368 		}
1369 		goto out_now;
1370 	}
1371 #ifdef INET6
1372 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1373 	    (num_v6 > 0)) {
1374 		error = EINVAL;
1375 		goto out_now;
1376 	}
1377 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1378 	    (num_v4 > 0)) {
1379 		struct in6pcb *inp6;
1380 
1381 		inp6 = (struct in6pcb *)inp;
1382 		if (SCTP_IPV6_V6ONLY(inp6)) {
1383 			/*
1384 			 * if IPV6_V6ONLY flag, ignore connections destined
1385 			 * to a v4 addr or v4-mapped addr
1386 			 */
1387 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1388 			error = EINVAL;
1389 			goto out_now;
1390 		}
1391 	}
1392 #endif				/* INET6 */
1393 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1394 	    SCTP_PCB_FLAGS_UNBOUND) {
1395 		/* Bind a ephemeral port */
1396 		error = sctp_inpcb_bind(so, NULL, NULL, p);
1397 		if (error) {
1398 			goto out_now;
1399 		}
1400 	}
1401 	/* FIX ME: do we want to pass in a vrf on the connect call? */
1402 	vrf_id = inp->def_vrf_id;
1403 
1404 	/* We are GOOD to go */
1405 	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1406 	    (struct thread *)p
1407 	    );
1408 	if (stcb == NULL) {
1409 		/* Gak! no memory */
1410 		goto out_now;
1411 	}
1412 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1413 	/* move to second address */
1414 	if (sa->sa_family == AF_INET)
1415 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1416 	else
1417 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1418 
1419 	error = 0;
1420 	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1421 	/* Fill in the return id */
1422 	if (error) {
1423 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1424 		goto out_now;
1425 	}
1426 	a_id = (sctp_assoc_t *) optval;
1427 	*a_id = sctp_get_associd(stcb);
1428 
1429 	/* initialize authentication parameters for the assoc */
1430 	sctp_initialize_auth_params(inp, stcb);
1431 
1432 	if (delay) {
1433 		/* doing delayed connection */
1434 		stcb->asoc.delayed_connection = 1;
1435 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1436 	} else {
1437 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1438 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1439 	}
1440 	SCTP_TCB_UNLOCK(stcb);
1441 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1442 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1443 		/* Set the connected flag so we can queue data */
1444 		soisconnecting(so);
1445 	}
1446 out_now:
1447 	if (creat_lock_on) {
1448 		SCTP_ASOC_CREATE_UNLOCK(inp);
1449 	}
1450 	SCTP_INP_DECR_REF(inp);
1451 	return error;
1452 }
1453 
1454 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1455 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1456 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1457 		SCTP_INP_RLOCK(inp); \
1458 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1459 		if (stcb) { \
1460 			SCTP_TCB_LOCK(stcb); \
1461                 } \
1462 		SCTP_INP_RUNLOCK(inp); \
1463 	} else if (assoc_id != 0) { \
1464 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1465 		if (stcb == NULL) { \
1466 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1467 			error = ENOENT; \
1468 			break; \
1469 		} \
1470 	} else { \
1471 		stcb = NULL; \
1472         } \
1473   }
1474 
1475 
1476 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
1477 	if (size < sizeof(type)) { \
1478 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1479 		error = EINVAL; \
1480 		break; \
1481 	} else { \
1482 		destp = (type *)srcp; \
1483 	} \
1484       }
1485 
1486 static int
1487 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1488     void *p)
1489 {
1490 	struct sctp_inpcb *inp = NULL;
1491 	int error, val = 0;
1492 	struct sctp_tcb *stcb = NULL;
1493 
1494 	if (optval == NULL) {
1495 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1496 		return (EINVAL);
1497 	}
1498 	inp = (struct sctp_inpcb *)so->so_pcb;
1499 	if (inp == 0) {
1500 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1501 		return EINVAL;
1502 	}
1503 	error = 0;
1504 
1505 	switch (optname) {
1506 	case SCTP_NODELAY:
1507 	case SCTP_AUTOCLOSE:
1508 	case SCTP_EXPLICIT_EOR:
1509 	case SCTP_AUTO_ASCONF:
1510 	case SCTP_DISABLE_FRAGMENTS:
1511 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1512 	case SCTP_USE_EXT_RCVINFO:
1513 		SCTP_INP_RLOCK(inp);
1514 		switch (optname) {
1515 		case SCTP_DISABLE_FRAGMENTS:
1516 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1517 			break;
1518 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1519 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1520 			break;
1521 		case SCTP_AUTO_ASCONF:
1522 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1523 				/* only valid for bound all sockets */
1524 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1525 			} else {
1526 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1527 				error = EINVAL;
1528 				goto flags_out;
1529 			}
1530 			break;
1531 		case SCTP_EXPLICIT_EOR:
1532 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1533 			break;
1534 		case SCTP_NODELAY:
1535 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1536 			break;
1537 		case SCTP_USE_EXT_RCVINFO:
1538 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1539 			break;
1540 		case SCTP_AUTOCLOSE:
1541 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1542 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1543 			else
1544 				val = 0;
1545 			break;
1546 
1547 		default:
1548 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1549 			error = ENOPROTOOPT;
1550 		}		/* end switch (sopt->sopt_name) */
1551 		if (optname != SCTP_AUTOCLOSE) {
1552 			/* make it an "on/off" value */
1553 			val = (val != 0);
1554 		}
1555 		if (*optsize < sizeof(val)) {
1556 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1557 			error = EINVAL;
1558 		}
1559 flags_out:
1560 		SCTP_INP_RUNLOCK(inp);
1561 		if (error == 0) {
1562 			/* return the option value */
1563 			*(int *)optval = val;
1564 			*optsize = sizeof(val);
1565 		}
1566 		break;
1567 	case SCTP_GET_PACKET_LOG:
1568 		{
1569 #ifdef  SCTP_PACKET_LOGGING
1570 			uint8_t *target;
1571 			int ret;
1572 
1573 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1574 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1575 			*optsize = ret;
1576 #else
1577 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1578 			error = EOPNOTSUPP;
1579 #endif
1580 			break;
1581 		}
1582 	case SCTP_PARTIAL_DELIVERY_POINT:
1583 		{
1584 			uint32_t *value;
1585 
1586 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1587 			*value = inp->partial_delivery_point;
1588 			*optsize = sizeof(uint32_t);
1589 		}
1590 		break;
1591 	case SCTP_FRAGMENT_INTERLEAVE:
1592 		{
1593 			uint32_t *value;
1594 
1595 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1596 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1597 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1598 					*value = SCTP_FRAG_LEVEL_2;
1599 				} else {
1600 					*value = SCTP_FRAG_LEVEL_1;
1601 				}
1602 			} else {
1603 				*value = SCTP_FRAG_LEVEL_0;
1604 			}
1605 			*optsize = sizeof(uint32_t);
1606 		}
1607 		break;
1608 	case SCTP_CMT_ON_OFF:
1609 		{
1610 			struct sctp_assoc_value *av;
1611 
1612 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1613 			if (sctp_cmt_on_off) {
1614 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1615 				if (stcb) {
1616 					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1617 					SCTP_TCB_UNLOCK(stcb);
1618 
1619 				} else {
1620 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1621 					error = ENOTCONN;
1622 				}
1623 			} else {
1624 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1625 				error = ENOPROTOOPT;
1626 			}
1627 			*optsize = sizeof(*av);
1628 		}
1629 		break;
1630 		/* JRS - Get socket option for pluggable congestion control */
1631 	case SCTP_PLUGGABLE_CC:
1632 		{
1633 			struct sctp_assoc_value *av;
1634 
1635 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1636 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1637 			if (stcb) {
1638 				av->assoc_value = stcb->asoc.congestion_control_module;
1639 				SCTP_TCB_UNLOCK(stcb);
1640 			} else {
1641 				av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1642 			}
1643 			*optsize = sizeof(*av);
1644 		}
1645 		break;
1646 	case SCTP_GET_ADDR_LEN:
1647 		{
1648 			struct sctp_assoc_value *av;
1649 
1650 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1651 			error = EINVAL;
1652 #ifdef INET
1653 			if (av->assoc_value == AF_INET) {
1654 				av->assoc_value = sizeof(struct sockaddr_in);
1655 				error = 0;
1656 			}
1657 #endif
1658 #ifdef INET6
1659 			if (av->assoc_value == AF_INET6) {
1660 				av->assoc_value = sizeof(struct sockaddr_in6);
1661 				error = 0;
1662 			}
1663 #endif
1664 			if (error) {
1665 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1666 			}
1667 			*optsize = sizeof(*av);
1668 		}
1669 		break;
1670 	case SCTP_GET_ASSOC_NUMBER:
1671 		{
1672 			uint32_t *value, cnt;
1673 
1674 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1675 			cnt = 0;
1676 			SCTP_INP_RLOCK(inp);
1677 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1678 				cnt++;
1679 			}
1680 			SCTP_INP_RUNLOCK(inp);
1681 			*value = cnt;
1682 			*optsize = sizeof(uint32_t);
1683 		}
1684 		break;
1685 
1686 	case SCTP_GET_ASSOC_ID_LIST:
1687 		{
1688 			struct sctp_assoc_ids *ids;
1689 			unsigned int at, limit;
1690 
1691 			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1692 			at = 0;
1693 			limit = *optsize / sizeof(sctp_assoc_t);
1694 			SCTP_INP_RLOCK(inp);
1695 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1696 				if (at < limit) {
1697 					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1698 				} else {
1699 					error = EINVAL;
1700 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1701 					break;
1702 				}
1703 			}
1704 			SCTP_INP_RUNLOCK(inp);
1705 			*optsize = at * sizeof(sctp_assoc_t);
1706 		}
1707 		break;
1708 	case SCTP_CONTEXT:
1709 		{
1710 			struct sctp_assoc_value *av;
1711 
1712 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1713 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1714 
1715 			if (stcb) {
1716 				av->assoc_value = stcb->asoc.context;
1717 				SCTP_TCB_UNLOCK(stcb);
1718 			} else {
1719 				SCTP_INP_RLOCK(inp);
1720 				av->assoc_value = inp->sctp_context;
1721 				SCTP_INP_RUNLOCK(inp);
1722 			}
1723 			*optsize = sizeof(*av);
1724 		}
1725 		break;
1726 	case SCTP_VRF_ID:
1727 		{
1728 			uint32_t *default_vrfid;
1729 
1730 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1731 			*default_vrfid = inp->def_vrf_id;
1732 			break;
1733 		}
1734 	case SCTP_GET_ASOC_VRF:
1735 		{
1736 			struct sctp_assoc_value *id;
1737 
1738 			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1739 			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1740 			if (stcb == NULL) {
1741 				error = EINVAL;
1742 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1743 				break;
1744 			}
1745 			id->assoc_value = stcb->asoc.vrf_id;
1746 			break;
1747 		}
1748 	case SCTP_GET_VRF_IDS:
1749 		{
1750 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1751 			error = EOPNOTSUPP;
1752 			break;
1753 		}
1754 	case SCTP_GET_NONCE_VALUES:
1755 		{
1756 			struct sctp_get_nonce_values *gnv;
1757 
1758 			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1759 			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1760 
1761 			if (stcb) {
1762 				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1763 				gnv->gn_local_tag = stcb->asoc.my_vtag;
1764 				SCTP_TCB_UNLOCK(stcb);
1765 			} else {
1766 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1767 				error = ENOTCONN;
1768 			}
1769 			*optsize = sizeof(*gnv);
1770 		}
1771 		break;
1772 	case SCTP_DELAYED_SACK:
1773 		{
1774 			struct sctp_sack_info *sack;
1775 
1776 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1777 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1778 			if (stcb) {
1779 				sack->sack_delay = stcb->asoc.delayed_ack;
1780 				sack->sack_freq = stcb->asoc.sack_freq;
1781 				SCTP_TCB_UNLOCK(stcb);
1782 			} else {
1783 				SCTP_INP_RLOCK(inp);
1784 				sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1785 				sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1786 				SCTP_INP_RUNLOCK(inp);
1787 			}
1788 			*optsize = sizeof(*sack);
1789 		}
1790 		break;
1791 
1792 	case SCTP_GET_SNDBUF_USE:
1793 		{
1794 			struct sctp_sockstat *ss;
1795 
1796 			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1797 			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1798 
1799 			if (stcb) {
1800 				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1801 				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1802 				    stcb->asoc.size_on_all_streams);
1803 				SCTP_TCB_UNLOCK(stcb);
1804 			} else {
1805 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1806 				error = ENOTCONN;
1807 			}
1808 			*optsize = sizeof(struct sctp_sockstat);
1809 		}
1810 		break;
1811 	case SCTP_MAX_BURST:
1812 		{
1813 			uint8_t *value;
1814 
1815 			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1816 
1817 			SCTP_INP_RLOCK(inp);
1818 			*value = inp->sctp_ep.max_burst;
1819 			SCTP_INP_RUNLOCK(inp);
1820 			*optsize = sizeof(uint8_t);
1821 		}
1822 		break;
1823 	case SCTP_MAXSEG:
1824 		{
1825 			struct sctp_assoc_value *av;
1826 			int ovh;
1827 
1828 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1829 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1830 
1831 			if (stcb) {
1832 				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1833 				SCTP_TCB_UNLOCK(stcb);
1834 			} else {
1835 				SCTP_INP_RLOCK(inp);
1836 				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1837 					ovh = SCTP_MED_OVERHEAD;
1838 				} else {
1839 					ovh = SCTP_MED_V4_OVERHEAD;
1840 				}
1841 				if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1842 					av->assoc_value = 0;
1843 				else
1844 					av->assoc_value = inp->sctp_frag_point - ovh;
1845 				SCTP_INP_RUNLOCK(inp);
1846 			}
1847 			*optsize = sizeof(struct sctp_assoc_value);
1848 		}
1849 		break;
1850 	case SCTP_GET_STAT_LOG:
1851 		error = sctp_fill_stat_log(optval, optsize);
1852 		break;
1853 	case SCTP_EVENTS:
1854 		{
1855 			struct sctp_event_subscribe *events;
1856 
1857 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1858 			memset(events, 0, sizeof(*events));
1859 			SCTP_INP_RLOCK(inp);
1860 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1861 				events->sctp_data_io_event = 1;
1862 
1863 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1864 				events->sctp_association_event = 1;
1865 
1866 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1867 				events->sctp_address_event = 1;
1868 
1869 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1870 				events->sctp_send_failure_event = 1;
1871 
1872 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1873 				events->sctp_peer_error_event = 1;
1874 
1875 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1876 				events->sctp_shutdown_event = 1;
1877 
1878 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1879 				events->sctp_partial_delivery_event = 1;
1880 
1881 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1882 				events->sctp_adaptation_layer_event = 1;
1883 
1884 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1885 				events->sctp_authentication_event = 1;
1886 
1887 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1888 				events->sctp_stream_reset_events = 1;
1889 			SCTP_INP_RUNLOCK(inp);
1890 			*optsize = sizeof(struct sctp_event_subscribe);
1891 		}
1892 		break;
1893 
1894 	case SCTP_ADAPTATION_LAYER:
1895 		{
1896 			uint32_t *value;
1897 
1898 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1899 
1900 			SCTP_INP_RLOCK(inp);
1901 			*value = inp->sctp_ep.adaptation_layer_indicator;
1902 			SCTP_INP_RUNLOCK(inp);
1903 			*optsize = sizeof(uint32_t);
1904 		}
1905 		break;
1906 	case SCTP_SET_INITIAL_DBG_SEQ:
1907 		{
1908 			uint32_t *value;
1909 
1910 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1911 			SCTP_INP_RLOCK(inp);
1912 			*value = inp->sctp_ep.initial_sequence_debug;
1913 			SCTP_INP_RUNLOCK(inp);
1914 			*optsize = sizeof(uint32_t);
1915 		}
1916 		break;
1917 	case SCTP_GET_LOCAL_ADDR_SIZE:
1918 		{
1919 			uint32_t *value;
1920 
1921 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1922 			SCTP_INP_RLOCK(inp);
1923 			*value = sctp_count_max_addresses(inp);
1924 			SCTP_INP_RUNLOCK(inp);
1925 			*optsize = sizeof(uint32_t);
1926 		}
1927 		break;
1928 	case SCTP_GET_REMOTE_ADDR_SIZE:
1929 		{
1930 			uint32_t *value;
1931 			size_t size;
1932 			struct sctp_nets *net;
1933 
1934 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1935 			/* FIXME MT: change to sctp_assoc_value? */
1936 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1937 
1938 			if (stcb) {
1939 				size = 0;
1940 				/* Count the sizes */
1941 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1942 					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1943 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1944 						size += sizeof(struct sockaddr_in6);
1945 					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1946 						size += sizeof(struct sockaddr_in);
1947 					} else {
1948 						/* huh */
1949 						break;
1950 					}
1951 				}
1952 				SCTP_TCB_UNLOCK(stcb);
1953 				*value = (uint32_t) size;
1954 			} else {
1955 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1956 				error = ENOTCONN;
1957 			}
1958 			*optsize = sizeof(uint32_t);
1959 		}
1960 		break;
1961 	case SCTP_GET_PEER_ADDRESSES:
1962 		/*
1963 		 * Get the address information, an array is passed in to
1964 		 * fill up we pack it.
1965 		 */
1966 		{
1967 			size_t cpsz, left;
1968 			struct sockaddr_storage *sas;
1969 			struct sctp_nets *net;
1970 			struct sctp_getaddresses *saddr;
1971 
1972 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1973 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1974 
1975 			if (stcb) {
1976 				left = (*optsize) - sizeof(struct sctp_getaddresses);
1977 				*optsize = sizeof(struct sctp_getaddresses);
1978 				sas = (struct sockaddr_storage *)&saddr->addr[0];
1979 
1980 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1981 					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1982 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1983 						cpsz = sizeof(struct sockaddr_in6);
1984 					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1985 						cpsz = sizeof(struct sockaddr_in);
1986 					} else {
1987 						/* huh */
1988 						break;
1989 					}
1990 					if (left < cpsz) {
1991 						/* not enough room. */
1992 						break;
1993 					}
1994 					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1995 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1996 						/* Must map the address */
1997 						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1998 						    (struct sockaddr_in6 *)sas);
1999 					} else {
2000 						memcpy(sas, &net->ro._l_addr, cpsz);
2001 					}
2002 					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2003 
2004 					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2005 					left -= cpsz;
2006 					*optsize += cpsz;
2007 				}
2008 				SCTP_TCB_UNLOCK(stcb);
2009 			} else {
2010 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2011 				error = ENOENT;
2012 			}
2013 		}
2014 		break;
2015 	case SCTP_GET_LOCAL_ADDRESSES:
2016 		{
2017 			size_t limit, actual;
2018 			struct sockaddr_storage *sas;
2019 			struct sctp_getaddresses *saddr;
2020 
2021 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2022 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2023 
2024 			sas = (struct sockaddr_storage *)&saddr->addr[0];
2025 			limit = *optsize - sizeof(sctp_assoc_t);
2026 			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2027 			if (stcb) {
2028 				SCTP_TCB_UNLOCK(stcb);
2029 			}
2030 			*optsize = sizeof(struct sockaddr_storage) + actual;
2031 		}
2032 		break;
2033 	case SCTP_PEER_ADDR_PARAMS:
2034 		{
2035 			struct sctp_paddrparams *paddrp;
2036 			struct sctp_nets *net;
2037 
2038 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2039 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2040 
2041 			net = NULL;
2042 			if (stcb) {
2043 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2044 			} else {
2045 				/*
2046 				 * We increment here since
2047 				 * sctp_findassociation_ep_addr() wil do a
2048 				 * decrement if it finds the stcb as long as
2049 				 * the locked tcb (last argument) is NOT a
2050 				 * TCB.. aka NULL.
2051 				 */
2052 				SCTP_INP_INCR_REF(inp);
2053 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2054 				if (stcb == NULL) {
2055 					SCTP_INP_DECR_REF(inp);
2056 				}
2057 			}
2058 			if (stcb && (net == NULL)) {
2059 				struct sockaddr *sa;
2060 
2061 				sa = (struct sockaddr *)&paddrp->spp_address;
2062 				if (sa->sa_family == AF_INET) {
2063 					struct sockaddr_in *sin;
2064 
2065 					sin = (struct sockaddr_in *)sa;
2066 					if (sin->sin_addr.s_addr) {
2067 						error = EINVAL;
2068 						SCTP_TCB_UNLOCK(stcb);
2069 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2070 						break;
2071 					}
2072 				} else if (sa->sa_family == AF_INET6) {
2073 					struct sockaddr_in6 *sin6;
2074 
2075 					sin6 = (struct sockaddr_in6 *)sa;
2076 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2077 						error = EINVAL;
2078 						SCTP_TCB_UNLOCK(stcb);
2079 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2080 						break;
2081 					}
2082 				} else {
2083 					error = EAFNOSUPPORT;
2084 					SCTP_TCB_UNLOCK(stcb);
2085 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2086 					break;
2087 				}
2088 			}
2089 			if (stcb) {
2090 				/* Applys to the specific association */
2091 				paddrp->spp_flags = 0;
2092 				if (net) {
2093 					int ovh;
2094 
2095 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2096 						ovh = SCTP_MED_OVERHEAD;
2097 					} else {
2098 						ovh = SCTP_MED_V4_OVERHEAD;
2099 					}
2100 
2101 
2102 					paddrp->spp_pathmaxrxt = net->failure_threshold;
2103 					paddrp->spp_pathmtu = net->mtu - ovh;
2104 					/* get flags for HB */
2105 					if (net->dest_state & SCTP_ADDR_NOHB)
2106 						paddrp->spp_flags |= SPP_HB_DISABLE;
2107 					else
2108 						paddrp->spp_flags |= SPP_HB_ENABLE;
2109 					/* get flags for PMTU */
2110 					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2111 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2112 					} else {
2113 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2114 					}
2115 #ifdef INET
2116 					if (net->ro._l_addr.sin.sin_family == AF_INET) {
2117 						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2118 						paddrp->spp_flags |= SPP_IPV4_TOS;
2119 					}
2120 #endif
2121 #ifdef INET6
2122 					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2123 						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2124 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2125 					}
2126 #endif
2127 				} else {
2128 					/*
2129 					 * No destination so return default
2130 					 * value
2131 					 */
2132 					int cnt = 0;
2133 
2134 					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2135 					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2136 #ifdef INET
2137 					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2138 					paddrp->spp_flags |= SPP_IPV4_TOS;
2139 #endif
2140 #ifdef INET6
2141 					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2142 					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2143 #endif
2144 					/* default settings should be these */
2145 					if (stcb->asoc.hb_is_disabled == 0) {
2146 						paddrp->spp_flags |= SPP_HB_ENABLE;
2147 					} else {
2148 						paddrp->spp_flags |= SPP_HB_DISABLE;
2149 					}
2150 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2151 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2152 							cnt++;
2153 						}
2154 					}
2155 					if (cnt) {
2156 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2157 					}
2158 				}
2159 				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2160 				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2161 				SCTP_TCB_UNLOCK(stcb);
2162 			} else {
2163 				/* Use endpoint defaults */
2164 				SCTP_INP_RLOCK(inp);
2165 				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2166 				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2167 				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2168 				/* get inp's default */
2169 #ifdef INET
2170 				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2171 				paddrp->spp_flags |= SPP_IPV4_TOS;
2172 #endif
2173 #ifdef INET6
2174 				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2175 					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2176 					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2177 				}
2178 #endif
2179 				/* can't return this */
2180 				paddrp->spp_pathmtu = 0;
2181 
2182 				/* default behavior, no stcb */
2183 				paddrp->spp_flags = SPP_PMTUD_ENABLE;
2184 
2185 				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2186 					paddrp->spp_flags |= SPP_HB_ENABLE;
2187 				} else {
2188 					paddrp->spp_flags |= SPP_HB_DISABLE;
2189 				}
2190 				SCTP_INP_RUNLOCK(inp);
2191 			}
2192 			*optsize = sizeof(struct sctp_paddrparams);
2193 		}
2194 		break;
2195 	case SCTP_GET_PEER_ADDR_INFO:
2196 		{
2197 			struct sctp_paddrinfo *paddri;
2198 			struct sctp_nets *net;
2199 
2200 			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2201 			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2202 
2203 			net = NULL;
2204 			if (stcb) {
2205 				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2206 			} else {
2207 				/*
2208 				 * We increment here since
2209 				 * sctp_findassociation_ep_addr() wil do a
2210 				 * decrement if it finds the stcb as long as
2211 				 * the locked tcb (last argument) is NOT a
2212 				 * TCB.. aka NULL.
2213 				 */
2214 				SCTP_INP_INCR_REF(inp);
2215 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2216 				if (stcb == NULL) {
2217 					SCTP_INP_DECR_REF(inp);
2218 				}
2219 			}
2220 
2221 			if ((stcb) && (net)) {
2222 				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2223 				paddri->spinfo_cwnd = net->cwnd;
2224 				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2225 				paddri->spinfo_rto = net->RTO;
2226 				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2227 				SCTP_TCB_UNLOCK(stcb);
2228 			} else {
2229 				if (stcb) {
2230 					SCTP_TCB_UNLOCK(stcb);
2231 				}
2232 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2233 				error = ENOENT;
2234 			}
2235 			*optsize = sizeof(struct sctp_paddrinfo);
2236 		}
2237 		break;
2238 	case SCTP_PCB_STATUS:
2239 		{
2240 			struct sctp_pcbinfo *spcb;
2241 
2242 			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2243 			sctp_fill_pcbinfo(spcb);
2244 			*optsize = sizeof(struct sctp_pcbinfo);
2245 		}
2246 		break;
2247 
2248 	case SCTP_STATUS:
2249 		{
2250 			struct sctp_nets *net;
2251 			struct sctp_status *sstat;
2252 
2253 			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2254 			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2255 
2256 			if (stcb == NULL) {
2257 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2258 				error = EINVAL;
2259 				break;
2260 			}
2261 			/*
2262 			 * I think passing the state is fine since
2263 			 * sctp_constants.h will be available to the user
2264 			 * land.
2265 			 */
2266 			sstat->sstat_state = stcb->asoc.state;
2267 			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2268 			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2269 			/*
2270 			 * We can't include chunks that have been passed to
2271 			 * the socket layer. Only things in queue.
2272 			 */
2273 			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2274 			    stcb->asoc.cnt_on_all_streams);
2275 
2276 
2277 			sstat->sstat_instrms = stcb->asoc.streamincnt;
2278 			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2279 			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2280 			memcpy(&sstat->sstat_primary.spinfo_address,
2281 			    &stcb->asoc.primary_destination->ro._l_addr,
2282 			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2283 			net = stcb->asoc.primary_destination;
2284 			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2285 			/*
2286 			 * Again the user can get info from sctp_constants.h
2287 			 * for what the state of the network is.
2288 			 */
2289 			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2290 			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2291 			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2292 			sstat->sstat_primary.spinfo_rto = net->RTO;
2293 			sstat->sstat_primary.spinfo_mtu = net->mtu;
2294 			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2295 			SCTP_TCB_UNLOCK(stcb);
2296 			*optsize = sizeof(*sstat);
2297 		}
2298 		break;
2299 	case SCTP_RTOINFO:
2300 		{
2301 			struct sctp_rtoinfo *srto;
2302 
2303 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2304 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2305 
2306 			if (stcb) {
2307 				srto->srto_initial = stcb->asoc.initial_rto;
2308 				srto->srto_max = stcb->asoc.maxrto;
2309 				srto->srto_min = stcb->asoc.minrto;
2310 				SCTP_TCB_UNLOCK(stcb);
2311 			} else {
2312 				SCTP_INP_RLOCK(inp);
2313 				srto->srto_initial = inp->sctp_ep.initial_rto;
2314 				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2315 				srto->srto_min = inp->sctp_ep.sctp_minrto;
2316 				SCTP_INP_RUNLOCK(inp);
2317 			}
2318 			*optsize = sizeof(*srto);
2319 		}
2320 		break;
2321 	case SCTP_ASSOCINFO:
2322 		{
2323 			struct sctp_assocparams *sasoc;
2324 			uint32_t oldval;
2325 
2326 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2327 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2328 
2329 			if (stcb) {
2330 				oldval = sasoc->sasoc_cookie_life;
2331 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2332 				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2333 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2334 				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2335 				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2336 				SCTP_TCB_UNLOCK(stcb);
2337 			} else {
2338 				SCTP_INP_RLOCK(inp);
2339 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2340 				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2341 				sasoc->sasoc_number_peer_destinations = 0;
2342 				sasoc->sasoc_peer_rwnd = 0;
2343 				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2344 				SCTP_INP_RUNLOCK(inp);
2345 			}
2346 			*optsize = sizeof(*sasoc);
2347 		}
2348 		break;
2349 	case SCTP_DEFAULT_SEND_PARAM:
2350 		{
2351 			struct sctp_sndrcvinfo *s_info;
2352 
2353 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2354 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2355 
2356 			if (stcb) {
2357 				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2358 				SCTP_TCB_UNLOCK(stcb);
2359 			} else {
2360 				SCTP_INP_RLOCK(inp);
2361 				memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2362 				SCTP_INP_RUNLOCK(inp);
2363 			}
2364 			*optsize = sizeof(*s_info);
2365 		}
2366 		break;
2367 	case SCTP_INITMSG:
2368 		{
2369 			struct sctp_initmsg *sinit;
2370 
2371 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2372 			SCTP_INP_RLOCK(inp);
2373 			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2374 			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2375 			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2376 			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2377 			SCTP_INP_RUNLOCK(inp);
2378 			*optsize = sizeof(*sinit);
2379 		}
2380 		break;
2381 	case SCTP_PRIMARY_ADDR:
2382 		/* we allow a "get" operation on this */
2383 		{
2384 			struct sctp_setprim *ssp;
2385 
2386 			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2387 			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2388 
2389 			if (stcb) {
2390 				/* simply copy out the sockaddr_storage... */
2391 				int len;
2392 
2393 				len = *optsize;
2394 				if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2395 					len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2396 
2397 				memcpy(&ssp->ssp_addr,
2398 				    &stcb->asoc.primary_destination->ro._l_addr,
2399 				    len);
2400 				SCTP_TCB_UNLOCK(stcb);
2401 			} else {
2402 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2403 				error = EINVAL;
2404 			}
2405 			*optsize = sizeof(*ssp);
2406 		}
2407 		break;
2408 
2409 	case SCTP_HMAC_IDENT:
2410 		{
2411 			struct sctp_hmacalgo *shmac;
2412 			sctp_hmaclist_t *hmaclist;
2413 			uint32_t size;
2414 			int i;
2415 
2416 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2417 
2418 			SCTP_INP_RLOCK(inp);
2419 			hmaclist = inp->sctp_ep.local_hmacs;
2420 			if (hmaclist == NULL) {
2421 				/* no HMACs to return */
2422 				*optsize = sizeof(*shmac);
2423 				SCTP_INP_RUNLOCK(inp);
2424 				break;
2425 			}
2426 			/* is there room for all of the hmac ids? */
2427 			size = sizeof(*shmac) + (hmaclist->num_algo *
2428 			    sizeof(shmac->shmac_idents[0]));
2429 			if ((size_t)(*optsize) < size) {
2430 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2431 				error = EINVAL;
2432 				SCTP_INP_RUNLOCK(inp);
2433 				break;
2434 			}
2435 			/* copy in the list */
2436 			for (i = 0; i < hmaclist->num_algo; i++)
2437 				shmac->shmac_idents[i] = hmaclist->hmac[i];
2438 			SCTP_INP_RUNLOCK(inp);
2439 			*optsize = size;
2440 			break;
2441 		}
2442 	case SCTP_AUTH_ACTIVE_KEY:
2443 		{
2444 			struct sctp_authkeyid *scact;
2445 
2446 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2447 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2448 
2449 			if (stcb) {
2450 				/* get the active key on the assoc */
2451 				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2452 				SCTP_TCB_UNLOCK(stcb);
2453 			} else {
2454 				/* get the endpoint active key */
2455 				SCTP_INP_RLOCK(inp);
2456 				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2457 				SCTP_INP_RUNLOCK(inp);
2458 			}
2459 			*optsize = sizeof(*scact);
2460 			break;
2461 		}
2462 	case SCTP_LOCAL_AUTH_CHUNKS:
2463 		{
2464 			struct sctp_authchunks *sac;
2465 			sctp_auth_chklist_t *chklist = NULL;
2466 			size_t size = 0;
2467 
2468 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2469 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2470 
2471 			if (stcb) {
2472 				/* get off the assoc */
2473 				chklist = stcb->asoc.local_auth_chunks;
2474 				/* is there enough space? */
2475 				size = sctp_auth_get_chklist_size(chklist);
2476 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2477 					error = EINVAL;
2478 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2479 				} else {
2480 					/* copy in the chunks */
2481 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2482 				}
2483 				SCTP_TCB_UNLOCK(stcb);
2484 			} else {
2485 				/* get off the endpoint */
2486 				SCTP_INP_RLOCK(inp);
2487 				chklist = inp->sctp_ep.local_auth_chunks;
2488 				/* is there enough space? */
2489 				size = sctp_auth_get_chklist_size(chklist);
2490 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2491 					error = EINVAL;
2492 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2493 				} else {
2494 					/* copy in the chunks */
2495 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2496 				}
2497 				SCTP_INP_RUNLOCK(inp);
2498 			}
2499 			*optsize = sizeof(struct sctp_authchunks) + size;
2500 			break;
2501 		}
2502 	case SCTP_PEER_AUTH_CHUNKS:
2503 		{
2504 			struct sctp_authchunks *sac;
2505 			sctp_auth_chklist_t *chklist = NULL;
2506 			size_t size = 0;
2507 
2508 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2509 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2510 
2511 			if (stcb) {
2512 				/* get off the assoc */
2513 				chklist = stcb->asoc.peer_auth_chunks;
2514 				/* is there enough space? */
2515 				size = sctp_auth_get_chklist_size(chklist);
2516 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2517 					error = EINVAL;
2518 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2519 				} else {
2520 					/* copy in the chunks */
2521 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2522 				}
2523 				SCTP_TCB_UNLOCK(stcb);
2524 			} else {
2525 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2526 				error = ENOENT;
2527 			}
2528 			*optsize = sizeof(struct sctp_authchunks) + size;
2529 			break;
2530 		}
2531 
2532 
2533 	default:
2534 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2535 		error = ENOPROTOOPT;
2536 		*optsize = 0;
2537 		break;
2538 	}			/* end switch (sopt->sopt_name) */
2539 	return (error);
2540 }
2541 
2542 static int
2543 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2544     void *p)
2545 {
2546 	int error, set_opt;
2547 	uint32_t *mopt;
2548 	struct sctp_tcb *stcb = NULL;
2549 	struct sctp_inpcb *inp = NULL;
2550 	uint32_t vrf_id;
2551 
2552 	if (optval == NULL) {
2553 		SCTP_PRINTF("optval is NULL\n");
2554 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2555 		return (EINVAL);
2556 	}
2557 	inp = (struct sctp_inpcb *)so->so_pcb;
2558 	if (inp == 0) {
2559 		SCTP_PRINTF("inp is NULL?\n");
2560 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2561 		return EINVAL;
2562 	}
2563 	vrf_id = inp->def_vrf_id;
2564 
2565 	error = 0;
2566 	switch (optname) {
2567 	case SCTP_NODELAY:
2568 	case SCTP_AUTOCLOSE:
2569 	case SCTP_AUTO_ASCONF:
2570 	case SCTP_EXPLICIT_EOR:
2571 	case SCTP_DISABLE_FRAGMENTS:
2572 	case SCTP_USE_EXT_RCVINFO:
2573 	case SCTP_I_WANT_MAPPED_V4_ADDR:
2574 		/* copy in the option value */
2575 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2576 		set_opt = 0;
2577 		if (error)
2578 			break;
2579 		switch (optname) {
2580 		case SCTP_DISABLE_FRAGMENTS:
2581 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2582 			break;
2583 		case SCTP_AUTO_ASCONF:
2584 			/*
2585 			 * NOTE: we don't really support this flag
2586 			 */
2587 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2588 				/* only valid for bound all sockets */
2589 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2590 			} else {
2591 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2592 				return (EINVAL);
2593 			}
2594 			break;
2595 		case SCTP_EXPLICIT_EOR:
2596 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2597 			break;
2598 		case SCTP_USE_EXT_RCVINFO:
2599 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2600 			break;
2601 		case SCTP_I_WANT_MAPPED_V4_ADDR:
2602 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2603 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2604 			} else {
2605 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2606 				return (EINVAL);
2607 			}
2608 			break;
2609 		case SCTP_NODELAY:
2610 			set_opt = SCTP_PCB_FLAGS_NODELAY;
2611 			break;
2612 		case SCTP_AUTOCLOSE:
2613 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2614 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2615 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2616 				return (EINVAL);
2617 			}
2618 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2619 			/*
2620 			 * The value is in ticks. Note this does not effect
2621 			 * old associations, only new ones.
2622 			 */
2623 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2624 			break;
2625 		}
2626 		SCTP_INP_WLOCK(inp);
2627 		if (*mopt != 0) {
2628 			sctp_feature_on(inp, set_opt);
2629 		} else {
2630 			sctp_feature_off(inp, set_opt);
2631 		}
2632 		SCTP_INP_WUNLOCK(inp);
2633 		break;
2634 	case SCTP_PARTIAL_DELIVERY_POINT:
2635 		{
2636 			uint32_t *value;
2637 
2638 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2639 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2640 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2641 				error = EINVAL;
2642 				break;
2643 			}
2644 			inp->partial_delivery_point = *value;
2645 		}
2646 		break;
2647 	case SCTP_FRAGMENT_INTERLEAVE:
2648 		/* not yet until we re-write sctp_recvmsg() */
2649 		{
2650 			uint32_t *level;
2651 
2652 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2653 			if (*level == SCTP_FRAG_LEVEL_2) {
2654 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2655 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2656 			} else if (*level == SCTP_FRAG_LEVEL_1) {
2657 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2658 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2659 			} else if (*level == SCTP_FRAG_LEVEL_0) {
2660 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2661 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2662 
2663 			} else {
2664 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2665 				error = EINVAL;
2666 			}
2667 		}
2668 		break;
2669 	case SCTP_CMT_ON_OFF:
2670 		{
2671 			struct sctp_assoc_value *av;
2672 
2673 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2674 			if (sctp_cmt_on_off) {
2675 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2676 				if (stcb) {
2677 					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2678 					SCTP_TCB_UNLOCK(stcb);
2679 				} else {
2680 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2681 					error = ENOTCONN;
2682 				}
2683 			} else {
2684 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2685 				error = ENOPROTOOPT;
2686 			}
2687 		}
2688 		break;
2689 		/* JRS - Set socket option for pluggable congestion control */
2690 	case SCTP_PLUGGABLE_CC:
2691 		{
2692 			struct sctp_assoc_value *av;
2693 
2694 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2695 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2696 			if (stcb) {
2697 				switch (av->assoc_value) {
2698 					/*
2699 					 * JRS - Standard TCP congestion
2700 					 * control
2701 					 */
2702 				case SCTP_CC_RFC2581:
2703 					{
2704 						stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2705 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2706 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2707 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2708 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2709 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2710 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2711 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2712 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2713 						SCTP_TCB_UNLOCK(stcb);
2714 						break;
2715 					}
2716 					/*
2717 					 * JRS - High Speed TCP congestion
2718 					 * control (Floyd)
2719 					 */
2720 				case SCTP_CC_HSTCP:
2721 					{
2722 						stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2723 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2724 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2725 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2726 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2727 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2728 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2729 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2730 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2731 						SCTP_TCB_UNLOCK(stcb);
2732 						break;
2733 					}
2734 					/* JRS - HTCP congestion control */
2735 				case SCTP_CC_HTCP:
2736 					{
2737 						stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2738 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2739 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2740 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2741 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2742 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2743 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2744 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2745 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2746 						SCTP_TCB_UNLOCK(stcb);
2747 						break;
2748 					}
2749 					/*
2750 					 * JRS - All other values are
2751 					 * invalid
2752 					 */
2753 				default:
2754 					{
2755 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2756 						error = EINVAL;
2757 						SCTP_TCB_UNLOCK(stcb);
2758 						break;
2759 					}
2760 				}
2761 			} else {
2762 				switch (av->assoc_value) {
2763 				case SCTP_CC_RFC2581:
2764 				case SCTP_CC_HSTCP:
2765 				case SCTP_CC_HTCP:
2766 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2767 					break;
2768 				default:
2769 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2770 					error = EINVAL;
2771 					break;
2772 				};
2773 			}
2774 		}
2775 		break;
2776 	case SCTP_CLR_STAT_LOG:
2777 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2778 		error = EOPNOTSUPP;
2779 		break;
2780 	case SCTP_CONTEXT:
2781 		{
2782 			struct sctp_assoc_value *av;
2783 
2784 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2785 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2786 
2787 			if (stcb) {
2788 				stcb->asoc.context = av->assoc_value;
2789 				SCTP_TCB_UNLOCK(stcb);
2790 			} else {
2791 				SCTP_INP_WLOCK(inp);
2792 				inp->sctp_context = av->assoc_value;
2793 				SCTP_INP_WUNLOCK(inp);
2794 			}
2795 		}
2796 		break;
2797 	case SCTP_VRF_ID:
2798 		{
2799 			uint32_t *default_vrfid;
2800 
2801 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2802 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
2803 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2804 				error = EINVAL;
2805 				break;
2806 			}
2807 			inp->def_vrf_id = *default_vrfid;
2808 			break;
2809 		}
2810 	case SCTP_DEL_VRF_ID:
2811 		{
2812 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2813 			error = EOPNOTSUPP;
2814 			break;
2815 		}
2816 	case SCTP_ADD_VRF_ID:
2817 		{
2818 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2819 			error = EOPNOTSUPP;
2820 			break;
2821 		}
2822 	case SCTP_DELAYED_SACK:
2823 		{
2824 			struct sctp_sack_info *sack;
2825 
2826 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2827 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2828 			if (sack->sack_delay) {
2829 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2830 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
2831 			}
2832 			if (stcb) {
2833 				if (sack->sack_delay) {
2834 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2835 						sack->sack_delay = TICKS_TO_MSEC(1);
2836 					}
2837 					stcb->asoc.delayed_ack = sack->sack_delay;
2838 				}
2839 				if (sack->sack_freq) {
2840 					stcb->asoc.sack_freq = sack->sack_freq;
2841 				}
2842 				SCTP_TCB_UNLOCK(stcb);
2843 			} else {
2844 				SCTP_INP_WLOCK(inp);
2845 				if (sack->sack_delay) {
2846 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2847 						sack->sack_delay = TICKS_TO_MSEC(1);
2848 					}
2849 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2850 				}
2851 				if (sack->sack_freq) {
2852 					inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2853 				}
2854 				SCTP_INP_WUNLOCK(inp);
2855 			}
2856 			break;
2857 		}
2858 	case SCTP_AUTH_CHUNK:
2859 		{
2860 			struct sctp_authchunk *sauth;
2861 
2862 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2863 
2864 			SCTP_INP_WLOCK(inp);
2865 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
2866 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2867 				error = EINVAL;
2868 			}
2869 			SCTP_INP_WUNLOCK(inp);
2870 			break;
2871 		}
2872 	case SCTP_AUTH_KEY:
2873 		{
2874 			struct sctp_authkey *sca;
2875 			struct sctp_keyhead *shared_keys;
2876 			sctp_sharedkey_t *shared_key;
2877 			sctp_key_t *key = NULL;
2878 			size_t size;
2879 
2880 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2881 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2882 			size = optsize - sizeof(*sca);
2883 
2884 			if (stcb) {
2885 				/* set it on the assoc */
2886 				shared_keys = &stcb->asoc.shared_keys;
2887 				/* clear the cached keys for this key id */
2888 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2889 				/*
2890 				 * create the new shared key and
2891 				 * insert/replace it
2892 				 */
2893 				if (size > 0) {
2894 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2895 					if (key == NULL) {
2896 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2897 						error = ENOMEM;
2898 						SCTP_TCB_UNLOCK(stcb);
2899 						break;
2900 					}
2901 				}
2902 				shared_key = sctp_alloc_sharedkey();
2903 				if (shared_key == NULL) {
2904 					sctp_free_key(key);
2905 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2906 					error = ENOMEM;
2907 					SCTP_TCB_UNLOCK(stcb);
2908 					break;
2909 				}
2910 				shared_key->key = key;
2911 				shared_key->keyid = sca->sca_keynumber;
2912 				sctp_insert_sharedkey(shared_keys, shared_key);
2913 				SCTP_TCB_UNLOCK(stcb);
2914 			} else {
2915 				/* set it on the endpoint */
2916 				SCTP_INP_WLOCK(inp);
2917 				shared_keys = &inp->sctp_ep.shared_keys;
2918 				/*
2919 				 * clear the cached keys on all assocs for
2920 				 * this key id
2921 				 */
2922 				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2923 				/*
2924 				 * create the new shared key and
2925 				 * insert/replace it
2926 				 */
2927 				if (size > 0) {
2928 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2929 					if (key == NULL) {
2930 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2931 						error = ENOMEM;
2932 						SCTP_INP_WUNLOCK(inp);
2933 						break;
2934 					}
2935 				}
2936 				shared_key = sctp_alloc_sharedkey();
2937 				if (shared_key == NULL) {
2938 					sctp_free_key(key);
2939 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2940 					error = ENOMEM;
2941 					SCTP_INP_WUNLOCK(inp);
2942 					break;
2943 				}
2944 				shared_key->key = key;
2945 				shared_key->keyid = sca->sca_keynumber;
2946 				sctp_insert_sharedkey(shared_keys, shared_key);
2947 				SCTP_INP_WUNLOCK(inp);
2948 			}
2949 			break;
2950 		}
2951 	case SCTP_HMAC_IDENT:
2952 		{
2953 			struct sctp_hmacalgo *shmac;
2954 			sctp_hmaclist_t *hmaclist;
2955 			uint32_t hmacid;
2956 			size_t size, i, found;
2957 
2958 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2959 			size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2960 			hmaclist = sctp_alloc_hmaclist(size);
2961 			if (hmaclist == NULL) {
2962 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2963 				error = ENOMEM;
2964 				break;
2965 			}
2966 			for (i = 0; i < size; i++) {
2967 				hmacid = shmac->shmac_idents[i];
2968 				if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2969 					 /* invalid HMACs were found */ ;
2970 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2971 					error = EINVAL;
2972 					sctp_free_hmaclist(hmaclist);
2973 					goto sctp_set_hmac_done;
2974 				}
2975 			}
2976 			found = 0;
2977 			for (i = 0; i < hmaclist->num_algo; i++) {
2978 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
2979 					/* already in list */
2980 					found = 1;
2981 				}
2982 			}
2983 			if (!found) {
2984 				sctp_free_hmaclist(hmaclist);
2985 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2986 				error = EINVAL;
2987 				break;
2988 			}
2989 			/* set it on the endpoint */
2990 			SCTP_INP_WLOCK(inp);
2991 			if (inp->sctp_ep.local_hmacs)
2992 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2993 			inp->sctp_ep.local_hmacs = hmaclist;
2994 			SCTP_INP_WUNLOCK(inp);
2995 	sctp_set_hmac_done:
2996 			break;
2997 		}
2998 	case SCTP_AUTH_ACTIVE_KEY:
2999 		{
3000 			struct sctp_authkeyid *scact;
3001 
3002 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
3003 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3004 
3005 			/* set the active key on the right place */
3006 			if (stcb) {
3007 				/* set the active key on the assoc */
3008 				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber)) {
3009 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3010 					error = EINVAL;
3011 				}
3012 				SCTP_TCB_UNLOCK(stcb);
3013 			} else {
3014 				/* set the active key on the endpoint */
3015 				SCTP_INP_WLOCK(inp);
3016 				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
3017 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3018 					error = EINVAL;
3019 				}
3020 				SCTP_INP_WUNLOCK(inp);
3021 			}
3022 			break;
3023 		}
3024 	case SCTP_AUTH_DELETE_KEY:
3025 		{
3026 			struct sctp_authkeyid *scdel;
3027 
3028 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
3029 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3030 
3031 			/* delete the key from the right place */
3032 			if (stcb) {
3033 				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
3034 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3035 					error = EINVAL;
3036 				}
3037 				SCTP_TCB_UNLOCK(stcb);
3038 			} else {
3039 				SCTP_INP_WLOCK(inp);
3040 				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
3041 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3042 					error = EINVAL;
3043 				}
3044 				SCTP_INP_WUNLOCK(inp);
3045 			}
3046 			break;
3047 		}
3048 
3049 	case SCTP_RESET_STREAMS:
3050 		{
3051 			struct sctp_stream_reset *strrst;
3052 			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
3053 			int i;
3054 
3055 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3056 			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3057 
3058 			if (stcb == NULL) {
3059 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3060 				error = ENOENT;
3061 				break;
3062 			}
3063 			if (stcb->asoc.peer_supports_strreset == 0) {
3064 				/*
3065 				 * Peer does not support it, we return
3066 				 * protocol not supported since this is true
3067 				 * for this feature and this peer, not the
3068 				 * socket request in general.
3069 				 */
3070 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3071 				error = EPROTONOSUPPORT;
3072 				SCTP_TCB_UNLOCK(stcb);
3073 				break;
3074 			}
3075 			if (stcb->asoc.stream_reset_outstanding) {
3076 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3077 				error = EALREADY;
3078 				SCTP_TCB_UNLOCK(stcb);
3079 				break;
3080 			}
3081 			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3082 				send_in = 1;
3083 			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3084 				send_out = 1;
3085 			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3086 				send_in = 1;
3087 				send_out = 1;
3088 			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3089 				send_tsn = 1;
3090 			} else {
3091 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3092 				error = EINVAL;
3093 				SCTP_TCB_UNLOCK(stcb);
3094 				break;
3095 			}
3096 			for (i = 0; i < strrst->strrst_num_streams; i++) {
3097 				if ((send_in) &&
3098 
3099 				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3100 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3101 					error = EINVAL;
3102 					goto get_out;
3103 				}
3104 				if ((send_out) &&
3105 				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3106 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3107 					error = EINVAL;
3108 					goto get_out;
3109 				}
3110 			}
3111 			if (error) {
3112 		get_out:
3113 				SCTP_TCB_UNLOCK(stcb);
3114 				break;
3115 			}
3116 			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3117 			    strrst->strrst_list,
3118 			    send_out, (stcb->asoc.str_reset_seq_in - 3),
3119 			    send_in, send_tsn);
3120 
3121 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3122 			SCTP_TCB_UNLOCK(stcb);
3123 		}
3124 		break;
3125 
3126 	case SCTP_CONNECT_X:
3127 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3128 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3129 			error = EINVAL;
3130 			break;
3131 		}
3132 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3133 		break;
3134 
3135 	case SCTP_CONNECT_X_DELAYED:
3136 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3137 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3138 			error = EINVAL;
3139 			break;
3140 		}
3141 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3142 		break;
3143 
3144 	case SCTP_CONNECT_X_COMPLETE:
3145 		{
3146 			struct sockaddr *sa;
3147 			struct sctp_nets *net;
3148 
3149 			/* FIXME MT: check correct? */
3150 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3151 
3152 			/* find tcb */
3153 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3154 				SCTP_INP_RLOCK(inp);
3155 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
3156 				if (stcb) {
3157 					SCTP_TCB_LOCK(stcb);
3158 					net = sctp_findnet(stcb, sa);
3159 				}
3160 				SCTP_INP_RUNLOCK(inp);
3161 			} else {
3162 				/*
3163 				 * We increment here since
3164 				 * sctp_findassociation_ep_addr() wil do a
3165 				 * decrement if it finds the stcb as long as
3166 				 * the locked tcb (last argument) is NOT a
3167 				 * TCB.. aka NULL.
3168 				 */
3169 				SCTP_INP_INCR_REF(inp);
3170 				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3171 				if (stcb == NULL) {
3172 					SCTP_INP_DECR_REF(inp);
3173 				}
3174 			}
3175 
3176 			if (stcb == NULL) {
3177 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3178 				error = ENOENT;
3179 				break;
3180 			}
3181 			if (stcb->asoc.delayed_connection == 1) {
3182 				stcb->asoc.delayed_connection = 0;
3183 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3184 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3185 				    stcb->asoc.primary_destination,
3186 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3187 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3188 			} else {
3189 				/*
3190 				 * already expired or did not use delayed
3191 				 * connectx
3192 				 */
3193 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3194 				error = EALREADY;
3195 			}
3196 			SCTP_TCB_UNLOCK(stcb);
3197 		}
3198 		break;
3199 	case SCTP_MAX_BURST:
3200 		{
3201 			uint8_t *burst;
3202 
3203 			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3204 
3205 			SCTP_INP_WLOCK(inp);
3206 			if (*burst) {
3207 				inp->sctp_ep.max_burst = *burst;
3208 			}
3209 			SCTP_INP_WUNLOCK(inp);
3210 		}
3211 		break;
3212 	case SCTP_MAXSEG:
3213 		{
3214 			struct sctp_assoc_value *av;
3215 			int ovh;
3216 
3217 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3218 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3219 
3220 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3221 				ovh = SCTP_MED_OVERHEAD;
3222 			} else {
3223 				ovh = SCTP_MED_V4_OVERHEAD;
3224 			}
3225 			if (stcb) {
3226 				if (av->assoc_value) {
3227 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3228 				} else {
3229 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3230 				}
3231 				SCTP_TCB_UNLOCK(stcb);
3232 			} else {
3233 				SCTP_INP_WLOCK(inp);
3234 				/*
3235 				 * FIXME MT: I think this is not in tune
3236 				 * with the API ID
3237 				 */
3238 				if (av->assoc_value) {
3239 					inp->sctp_frag_point = (av->assoc_value + ovh);
3240 				} else {
3241 					inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3242 				}
3243 				SCTP_INP_WUNLOCK(inp);
3244 			}
3245 		}
3246 		break;
3247 	case SCTP_EVENTS:
3248 		{
3249 			struct sctp_event_subscribe *events;
3250 
3251 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3252 
3253 			SCTP_INP_WLOCK(inp);
3254 			if (events->sctp_data_io_event) {
3255 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3256 			} else {
3257 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3258 			}
3259 
3260 			if (events->sctp_association_event) {
3261 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3262 			} else {
3263 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3264 			}
3265 
3266 			if (events->sctp_address_event) {
3267 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3268 			} else {
3269 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3270 			}
3271 
3272 			if (events->sctp_send_failure_event) {
3273 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3274 			} else {
3275 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3276 			}
3277 
3278 			if (events->sctp_peer_error_event) {
3279 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3280 			} else {
3281 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3282 			}
3283 
3284 			if (events->sctp_shutdown_event) {
3285 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3286 			} else {
3287 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3288 			}
3289 
3290 			if (events->sctp_partial_delivery_event) {
3291 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3292 			} else {
3293 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3294 			}
3295 
3296 			if (events->sctp_adaptation_layer_event) {
3297 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3298 			} else {
3299 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3300 			}
3301 
3302 			if (events->sctp_authentication_event) {
3303 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3304 			} else {
3305 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3306 			}
3307 
3308 			if (events->sctp_stream_reset_events) {
3309 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3310 			} else {
3311 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3312 			}
3313 			SCTP_INP_WUNLOCK(inp);
3314 		}
3315 		break;
3316 
3317 	case SCTP_ADAPTATION_LAYER:
3318 		{
3319 			struct sctp_setadaptation *adap_bits;
3320 
3321 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3322 			SCTP_INP_WLOCK(inp);
3323 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3324 			SCTP_INP_WUNLOCK(inp);
3325 		}
3326 		break;
3327 #ifdef SCTP_DEBUG
3328 	case SCTP_SET_INITIAL_DBG_SEQ:
3329 		{
3330 			uint32_t *vvv;
3331 
3332 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3333 			SCTP_INP_WLOCK(inp);
3334 			inp->sctp_ep.initial_sequence_debug = *vvv;
3335 			SCTP_INP_WUNLOCK(inp);
3336 		}
3337 		break;
3338 #endif
3339 	case SCTP_DEFAULT_SEND_PARAM:
3340 		{
3341 			struct sctp_sndrcvinfo *s_info;
3342 
3343 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3344 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3345 
3346 			if (stcb) {
3347 				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3348 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3349 				} else {
3350 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3351 					error = EINVAL;
3352 				}
3353 				SCTP_TCB_UNLOCK(stcb);
3354 			} else {
3355 				SCTP_INP_WLOCK(inp);
3356 				memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3357 				SCTP_INP_WUNLOCK(inp);
3358 			}
3359 		}
3360 		break;
3361 	case SCTP_PEER_ADDR_PARAMS:
3362 		/* Applys to the specific association */
3363 		{
3364 			struct sctp_paddrparams *paddrp;
3365 			struct sctp_nets *net;
3366 
3367 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3368 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3369 			net = NULL;
3370 			if (stcb) {
3371 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3372 			} else {
3373 				/*
3374 				 * We increment here since
3375 				 * sctp_findassociation_ep_addr() wil do a
3376 				 * decrement if it finds the stcb as long as
3377 				 * the locked tcb (last argument) is NOT a
3378 				 * TCB.. aka NULL.
3379 				 */
3380 				SCTP_INP_INCR_REF(inp);
3381 				stcb = sctp_findassociation_ep_addr(&inp,
3382 				    (struct sockaddr *)&paddrp->spp_address,
3383 				    &net, NULL, NULL);
3384 				if (stcb == NULL) {
3385 					SCTP_INP_DECR_REF(inp);
3386 				}
3387 			}
3388 			if (stcb && (net == NULL)) {
3389 				struct sockaddr *sa;
3390 
3391 				sa = (struct sockaddr *)&paddrp->spp_address;
3392 				if (sa->sa_family == AF_INET) {
3393 					struct sockaddr_in *sin;
3394 
3395 					sin = (struct sockaddr_in *)sa;
3396 					if (sin->sin_addr.s_addr) {
3397 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3398 						SCTP_TCB_UNLOCK(stcb);
3399 						error = EINVAL;
3400 						break;
3401 					}
3402 				} else if (sa->sa_family == AF_INET6) {
3403 					struct sockaddr_in6 *sin6;
3404 
3405 					sin6 = (struct sockaddr_in6 *)sa;
3406 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3407 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3408 						SCTP_TCB_UNLOCK(stcb);
3409 						error = EINVAL;
3410 						break;
3411 					}
3412 				} else {
3413 					error = EAFNOSUPPORT;
3414 					SCTP_TCB_UNLOCK(stcb);
3415 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3416 					break;
3417 				}
3418 			}
3419 			/* sanity checks */
3420 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3421 				if (stcb)
3422 					SCTP_TCB_UNLOCK(stcb);
3423 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3424 				return (EINVAL);
3425 			}
3426 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3427 				if (stcb)
3428 					SCTP_TCB_UNLOCK(stcb);
3429 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3430 				return (EINVAL);
3431 			}
3432 			if (stcb) {
3433 				/************************TCB SPECIFIC SET ******************/
3434 				/*
3435 				 * do we change the timer for HB, we run
3436 				 * only one?
3437 				 */
3438 				int ovh = 0;
3439 
3440 				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3441 					ovh = SCTP_MED_OVERHEAD;
3442 				} else {
3443 					ovh = SCTP_MED_V4_OVERHEAD;
3444 				}
3445 
3446 				if (paddrp->spp_hbinterval)
3447 					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3448 				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3449 					stcb->asoc.heart_beat_delay = 0;
3450 
3451 				/* network sets ? */
3452 				if (net) {
3453 					/************************NET SPECIFIC SET ******************/
3454 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3455 						/* on demand HB */
3456 						if (sctp_send_hb(stcb, 1, net) < 0) {
3457 							/* asoc destroyed */
3458 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3459 							error = EINVAL;
3460 							break;
3461 						}
3462 					}
3463 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3464 						net->dest_state |= SCTP_ADDR_NOHB;
3465 					}
3466 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3467 						net->dest_state &= ~SCTP_ADDR_NOHB;
3468 					}
3469 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3470 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3471 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3472 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3473 						}
3474 						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3475 							net->mtu = paddrp->spp_pathmtu + ovh;
3476 							if (net->mtu < stcb->asoc.smallest_mtu) {
3477 #ifdef SCTP_PRINT_FOR_B_AND_M
3478 								SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3479 								    net->mtu);
3480 #endif
3481 								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3482 							}
3483 						}
3484 					}
3485 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3486 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3487 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3488 						}
3489 					}
3490 					if (paddrp->spp_pathmaxrxt)
3491 						net->failure_threshold = paddrp->spp_pathmaxrxt;
3492 #ifdef INET
3493 					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3494 						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3495 							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3496 						}
3497 					}
3498 #endif
3499 #ifdef INET6
3500 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3501 						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3502 							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3503 						}
3504 					}
3505 #endif
3506 				} else {
3507 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3508 					if (paddrp->spp_pathmaxrxt)
3509 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3510 
3511 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3512 						/* Turn back on the timer */
3513 						stcb->asoc.hb_is_disabled = 0;
3514 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3515 					}
3516 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3517 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3518 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3519 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3520 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3521 							}
3522 							if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3523 								net->mtu = paddrp->spp_pathmtu + ovh;
3524 								if (net->mtu < stcb->asoc.smallest_mtu) {
3525 #ifdef SCTP_PRINT_FOR_B_AND_M
3526 									SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3527 									    net->mtu);
3528 #endif
3529 									sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3530 								}
3531 							}
3532 						}
3533 					}
3534 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3535 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3536 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3537 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3538 							}
3539 						}
3540 					}
3541 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3542 						int cnt_of_unconf = 0;
3543 						struct sctp_nets *lnet;
3544 
3545 						stcb->asoc.hb_is_disabled = 1;
3546 						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3547 							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3548 								cnt_of_unconf++;
3549 							}
3550 						}
3551 						/*
3552 						 * stop the timer ONLY if we
3553 						 * have no unconfirmed
3554 						 * addresses
3555 						 */
3556 						if (cnt_of_unconf == 0) {
3557 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3558 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3559 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3560 							}
3561 						}
3562 					}
3563 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3564 						/* start up the timer. */
3565 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3566 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3567 						}
3568 					}
3569 #ifdef INET
3570 					if (paddrp->spp_flags & SPP_IPV4_TOS)
3571 						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3572 #endif
3573 #ifdef INET6
3574 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3575 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3576 #endif
3577 
3578 				}
3579 				SCTP_TCB_UNLOCK(stcb);
3580 			} else {
3581 				/************************NO TCB, SET TO default stuff ******************/
3582 				SCTP_INP_WLOCK(inp);
3583 				/*
3584 				 * For the TOS/FLOWLABEL stuff you set it
3585 				 * with the options on the socket
3586 				 */
3587 				if (paddrp->spp_pathmaxrxt) {
3588 					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3589 				}
3590 				if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3591 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3592 				else if (paddrp->spp_hbinterval) {
3593 					if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3594 						paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3595 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3596 				}
3597 				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3598 					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3599 
3600 				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3601 					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3602 				}
3603 				SCTP_INP_WUNLOCK(inp);
3604 			}
3605 		}
3606 		break;
3607 	case SCTP_RTOINFO:
3608 		{
3609 			struct sctp_rtoinfo *srto;
3610 			uint32_t new_init, new_min, new_max;
3611 
3612 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3613 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3614 
3615 			if (stcb) {
3616 				if (srto->srto_initial)
3617 					new_init = srto->srto_initial;
3618 				else
3619 					new_init = stcb->asoc.initial_rto;
3620 				if (srto->srto_max)
3621 					new_max = srto->srto_max;
3622 				else
3623 					new_max = stcb->asoc.maxrto;
3624 				if (srto->srto_min)
3625 					new_min = srto->srto_min;
3626 				else
3627 					new_min = stcb->asoc.minrto;
3628 				if ((new_min <= new_init) && (new_init <= new_max)) {
3629 					stcb->asoc.initial_rto = new_init;
3630 					stcb->asoc.maxrto = new_max;
3631 					stcb->asoc.minrto = new_min;
3632 				} else {
3633 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDOM);
3634 					error = EDOM;
3635 				}
3636 				SCTP_TCB_UNLOCK(stcb);
3637 			} else {
3638 				SCTP_INP_WLOCK(inp);
3639 				if (srto->srto_initial)
3640 					new_init = srto->srto_initial;
3641 				else
3642 					new_init = inp->sctp_ep.initial_rto;
3643 				if (srto->srto_max)
3644 					new_max = srto->srto_max;
3645 				else
3646 					new_max = inp->sctp_ep.sctp_maxrto;
3647 				if (srto->srto_min)
3648 					new_min = srto->srto_min;
3649 				else
3650 					new_min = inp->sctp_ep.sctp_minrto;
3651 				if ((new_min <= new_init) && (new_init <= new_max)) {
3652 					inp->sctp_ep.initial_rto = new_init;
3653 					inp->sctp_ep.sctp_maxrto = new_max;
3654 					inp->sctp_ep.sctp_minrto = new_min;
3655 				} else {
3656 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDOM);
3657 					error = EDOM;
3658 				}
3659 				SCTP_INP_WUNLOCK(inp);
3660 			}
3661 		}
3662 		break;
3663 	case SCTP_ASSOCINFO:
3664 		{
3665 			struct sctp_assocparams *sasoc;
3666 
3667 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3668 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3669 			if (sasoc->sasoc_cookie_life) {
3670 				/* boundary check the cookie life */
3671 				if (sasoc->sasoc_cookie_life < 1000)
3672 					sasoc->sasoc_cookie_life = 1000;
3673 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3674 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3675 				}
3676 			}
3677 			if (stcb) {
3678 				if (sasoc->sasoc_asocmaxrxt)
3679 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3680 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3681 				sasoc->sasoc_peer_rwnd = 0;
3682 				sasoc->sasoc_local_rwnd = 0;
3683 				if (sasoc->sasoc_cookie_life) {
3684 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3685 				}
3686 				SCTP_TCB_UNLOCK(stcb);
3687 			} else {
3688 				SCTP_INP_WLOCK(inp);
3689 				if (sasoc->sasoc_asocmaxrxt)
3690 					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3691 				sasoc->sasoc_number_peer_destinations = 0;
3692 				sasoc->sasoc_peer_rwnd = 0;
3693 				sasoc->sasoc_local_rwnd = 0;
3694 				if (sasoc->sasoc_cookie_life) {
3695 					inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3696 				}
3697 				SCTP_INP_WUNLOCK(inp);
3698 			}
3699 		}
3700 		break;
3701 	case SCTP_INITMSG:
3702 		{
3703 			struct sctp_initmsg *sinit;
3704 
3705 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3706 			SCTP_INP_WLOCK(inp);
3707 			if (sinit->sinit_num_ostreams)
3708 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3709 
3710 			if (sinit->sinit_max_instreams)
3711 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3712 
3713 			if (sinit->sinit_max_attempts)
3714 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3715 
3716 			if (sinit->sinit_max_init_timeo)
3717 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3718 			SCTP_INP_WUNLOCK(inp);
3719 		}
3720 		break;
3721 	case SCTP_PRIMARY_ADDR:
3722 		{
3723 			struct sctp_setprim *spa;
3724 			struct sctp_nets *net, *lnet;
3725 
3726 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3727 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3728 
3729 			net = NULL;
3730 			if (stcb) {
3731 				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3732 			} else {
3733 				/*
3734 				 * We increment here since
3735 				 * sctp_findassociation_ep_addr() wil do a
3736 				 * decrement if it finds the stcb as long as
3737 				 * the locked tcb (last argument) is NOT a
3738 				 * TCB.. aka NULL.
3739 				 */
3740 				SCTP_INP_INCR_REF(inp);
3741 				stcb = sctp_findassociation_ep_addr(&inp,
3742 				    (struct sockaddr *)&spa->ssp_addr,
3743 				    &net, NULL, NULL);
3744 				if (stcb == NULL) {
3745 					SCTP_INP_DECR_REF(inp);
3746 				}
3747 			}
3748 
3749 			if ((stcb) && (net)) {
3750 				if ((net != stcb->asoc.primary_destination) &&
3751 				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3752 					/* Ok we need to set it */
3753 					lnet = stcb->asoc.primary_destination;
3754 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3755 						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3756 							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3757 						}
3758 						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3759 					}
3760 				}
3761 			} else {
3762 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3763 				error = EINVAL;
3764 			}
3765 			if (stcb) {
3766 				SCTP_TCB_UNLOCK(stcb);
3767 			}
3768 		}
3769 		break;
3770 	case SCTP_SET_DYNAMIC_PRIMARY:
3771 		{
3772 			union sctp_sockstore *ss;
3773 
3774 			error = priv_check(curthread,
3775 			    PRIV_NETINET_RESERVEDPORT);
3776 			if (error)
3777 				break;
3778 
3779 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3780 			/* SUPER USER CHECK? */
3781 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3782 		}
3783 		break;
3784 	case SCTP_SET_PEER_PRIMARY_ADDR:
3785 		{
3786 			struct sctp_setpeerprim *sspp;
3787 
3788 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3789 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3790 			if (stcb != NULL) {
3791 				struct sctp_ifa *ifa;
3792 
3793 				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3794 				    stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
3795 				if (ifa == NULL) {
3796 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3797 					error = EINVAL;
3798 					goto out_of_it;
3799 				}
3800 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3801 					/*
3802 					 * Must validate the ifa found is in
3803 					 * our ep
3804 					 */
3805 					struct sctp_laddr *laddr;
3806 					int found = 0;
3807 
3808 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3809 						if (laddr->ifa == NULL) {
3810 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3811 							    __FUNCTION__);
3812 							continue;
3813 						}
3814 						if (laddr->ifa == ifa) {
3815 							found = 1;
3816 							break;
3817 						}
3818 					}
3819 					if (!found) {
3820 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3821 						error = EINVAL;
3822 						goto out_of_it;
3823 					}
3824 				}
3825 				if (sctp_set_primary_ip_address_sa(stcb,
3826 				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
3827 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3828 					error = EINVAL;
3829 				}
3830 		out_of_it:
3831 				SCTP_TCB_UNLOCK(stcb);
3832 			} else {
3833 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3834 				error = EINVAL;
3835 			}
3836 
3837 		}
3838 		break;
3839 	case SCTP_BINDX_ADD_ADDR:
3840 		{
3841 			struct sctp_getaddresses *addrs;
3842 			size_t sz;
3843 			struct thread *td;
3844 			int prison = 0;
3845 
3846 			td = (struct thread *)p;
3847 			if (jailed(td->td_ucred)) {
3848 				prison = 1;
3849 			}
3850 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3851 			    optsize);
3852 			if (addrs->addr->sa_family == AF_INET) {
3853 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3854 				if (optsize < sz) {
3855 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3856 					error = EINVAL;
3857 					break;
3858 				}
3859 				if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3860 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3861 					error = EADDRNOTAVAIL;
3862 				}
3863 			} else if (addrs->addr->sa_family == AF_INET6) {
3864 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3865 				if (optsize < sz) {
3866 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3867 					error = EINVAL;
3868 					break;
3869 				}
3870 				/* JAIL XXXX Add else here for V6 */
3871 			}
3872 			sctp_bindx_add_address(so, inp, addrs->addr,
3873 			    addrs->sget_assoc_id, vrf_id,
3874 			    &error, p);
3875 		}
3876 		break;
3877 	case SCTP_BINDX_REM_ADDR:
3878 		{
3879 			struct sctp_getaddresses *addrs;
3880 			size_t sz;
3881 			struct thread *td;
3882 			int prison = 0;
3883 
3884 			td = (struct thread *)p;
3885 			if (jailed(td->td_ucred)) {
3886 				prison = 1;
3887 			}
3888 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3889 			if (addrs->addr->sa_family == AF_INET) {
3890 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3891 				if (optsize < sz) {
3892 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3893 					error = EINVAL;
3894 					break;
3895 				}
3896 				if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3897 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3898 					error = EADDRNOTAVAIL;
3899 				}
3900 			} else if (addrs->addr->sa_family == AF_INET6) {
3901 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3902 				if (optsize < sz) {
3903 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3904 					error = EINVAL;
3905 					break;
3906 				}
3907 				/* JAIL XXXX Add else here for V6 */
3908 			}
3909 			sctp_bindx_delete_address(so, inp, addrs->addr,
3910 			    addrs->sget_assoc_id, vrf_id,
3911 			    &error);
3912 		}
3913 		break;
3914 	default:
3915 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3916 		error = ENOPROTOOPT;
3917 		break;
3918 	}			/* end switch (opt) */
3919 	return (error);
3920 }
3921 
3922 
3923 int
3924 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3925 {
3926 	void *optval = NULL;
3927 	size_t optsize = 0;
3928 	struct sctp_inpcb *inp;
3929 	void *p;
3930 	int error = 0;
3931 
3932 	inp = (struct sctp_inpcb *)so->so_pcb;
3933 	if (inp == 0) {
3934 		/* I made the same as TCP since we are not setup? */
3935 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3936 		return (ECONNRESET);
3937 	}
3938 	if (sopt->sopt_level != IPPROTO_SCTP) {
3939 		/* wrong proto level... send back up to IP */
3940 #ifdef INET6
3941 		if (INP_CHECK_SOCKAF(so, AF_INET6))
3942 			error = ip6_ctloutput(so, sopt);
3943 		else
3944 #endif				/* INET6 */
3945 			error = ip_ctloutput(so, sopt);
3946 		return (error);
3947 	}
3948 	optsize = sopt->sopt_valsize;
3949 	if (optsize) {
3950 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
3951 		if (optval == NULL) {
3952 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
3953 			return (ENOBUFS);
3954 		}
3955 		error = sooptcopyin(sopt, optval, optsize, optsize);
3956 		if (error) {
3957 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
3958 			goto out;
3959 		}
3960 	}
3961 	p = (void *)sopt->sopt_td;
3962 	if (sopt->sopt_dir == SOPT_SET) {
3963 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3964 	} else if (sopt->sopt_dir == SOPT_GET) {
3965 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3966 	} else {
3967 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3968 		error = EINVAL;
3969 	}
3970 	if ((error == 0) && (optval != NULL)) {
3971 		error = sooptcopyout(sopt, optval, optsize);
3972 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
3973 	} else if (optval != NULL) {
3974 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
3975 	}
3976 out:
3977 	return (error);
3978 }
3979 
3980 
3981 static int
3982 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3983 {
3984 	int error = 0;
3985 	int create_lock_on = 0;
3986 	uint32_t vrf_id;
3987 	struct sctp_inpcb *inp;
3988 	struct sctp_tcb *stcb = NULL;
3989 
3990 	inp = (struct sctp_inpcb *)so->so_pcb;
3991 	if (inp == 0) {
3992 		/* I made the same as TCP since we are not setup? */
3993 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3994 		return (ECONNRESET);
3995 	}
3996 	if (addr == NULL) {
3997 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3998 		return EINVAL;
3999 	}
4000 	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
4001 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4002 		return (EINVAL);
4003 	}
4004 	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
4005 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4006 		return (EINVAL);
4007 	}
4008 	SCTP_ASOC_CREATE_LOCK(inp);
4009 	create_lock_on = 1;
4010 
4011 	SCTP_INP_INCR_REF(inp);
4012 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4013 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4014 		/* Should I really unlock ? */
4015 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4016 		error = EFAULT;
4017 		goto out_now;
4018 	}
4019 #ifdef INET6
4020 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4021 	    (addr->sa_family == AF_INET6)) {
4022 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4023 		error = EINVAL;
4024 		goto out_now;
4025 	}
4026 #endif				/* INET6 */
4027 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4028 	    SCTP_PCB_FLAGS_UNBOUND) {
4029 		/* Bind a ephemeral port */
4030 		error = sctp_inpcb_bind(so, NULL, NULL, p);
4031 		if (error) {
4032 			goto out_now;
4033 		}
4034 	}
4035 	/* Now do we connect? */
4036 	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
4037 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4038 		error = EINVAL;
4039 		goto out_now;
4040 	}
4041 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4042 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4043 		/* We are already connected AND the TCP model */
4044 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4045 		error = EADDRINUSE;
4046 		goto out_now;
4047 	}
4048 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4049 		SCTP_INP_RLOCK(inp);
4050 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
4051 		SCTP_INP_RUNLOCK(inp);
4052 	} else {
4053 		/*
4054 		 * We increment here since sctp_findassociation_ep_addr()
4055 		 * wil do a decrement if it finds the stcb as long as the
4056 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4057 		 */
4058 		SCTP_INP_INCR_REF(inp);
4059 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4060 		if (stcb == NULL) {
4061 			SCTP_INP_DECR_REF(inp);
4062 		} else {
4063 			SCTP_TCB_LOCK(stcb);
4064 		}
4065 	}
4066 	if (stcb != NULL) {
4067 		/* Already have or am bring up an association */
4068 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4069 		error = EALREADY;
4070 		goto out_now;
4071 	}
4072 	vrf_id = inp->def_vrf_id;
4073 	/* We are GOOD to go */
4074 	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4075 	if (stcb == NULL) {
4076 		/* Gak! no memory */
4077 		goto out_now;
4078 	}
4079 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4080 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4081 		/* Set the connected flag so we can queue data */
4082 		soisconnecting(so);
4083 	}
4084 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4085 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4086 
4087 	/* initialize authentication parameters for the assoc */
4088 	sctp_initialize_auth_params(inp, stcb);
4089 
4090 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4091 	SCTP_TCB_UNLOCK(stcb);
4092 out_now:
4093 	if (create_lock_on) {
4094 		SCTP_ASOC_CREATE_UNLOCK(inp);
4095 	}
4096 	SCTP_INP_DECR_REF(inp);
4097 	return error;
4098 }
4099 
4100 int
4101 sctp_listen(struct socket *so, int backlog, struct thread *p)
4102 {
4103 	/*
4104 	 * Note this module depends on the protocol processing being called
4105 	 * AFTER any socket level flags and backlog are applied to the
4106 	 * socket. The traditional way that the socket flags are applied is
4107 	 * AFTER protocol processing. We have made a change to the
4108 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4109 	 * place if the socket API for SCTP is to work properly.
4110 	 */
4111 
4112 	int error = 0;
4113 	struct sctp_inpcb *inp;
4114 
4115 	inp = (struct sctp_inpcb *)so->so_pcb;
4116 	if (inp == 0) {
4117 		/* I made the same as TCP since we are not setup? */
4118 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4119 		return (ECONNRESET);
4120 	}
4121 	SCTP_INP_RLOCK(inp);
4122 #ifdef SCTP_LOCK_LOGGING
4123 	if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
4124 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4125 	}
4126 #endif
4127 	SOCK_LOCK(so);
4128 	error = solisten_proto_check(so);
4129 	if (error) {
4130 		SOCK_UNLOCK(so);
4131 		SCTP_INP_RUNLOCK(inp);
4132 		return (error);
4133 	}
4134 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4135 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4136 		/* We are already connected AND the TCP model */
4137 		SCTP_INP_RUNLOCK(inp);
4138 		SOCK_UNLOCK(so);
4139 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4140 		return (EADDRINUSE);
4141 	}
4142 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4143 		/* We must do a bind. */
4144 		SOCK_UNLOCK(so);
4145 		SCTP_INP_RUNLOCK(inp);
4146 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4147 			/* bind error, probably perm */
4148 			return (error);
4149 		}
4150 		SOCK_LOCK(so);
4151 	} else {
4152 		SCTP_INP_RUNLOCK(inp);
4153 	}
4154 	/* It appears for 7.0 and on, we must always call this. */
4155 	solisten_proto(so, backlog);
4156 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4157 		/* remove the ACCEPTCONN flag for one-to-many sockets */
4158 		so->so_options &= ~SO_ACCEPTCONN;
4159 	}
4160 	if (backlog == 0) {
4161 		/* turning off listen */
4162 		so->so_options &= ~SO_ACCEPTCONN;
4163 	}
4164 	SOCK_UNLOCK(so);
4165 	return (error);
4166 }
4167 
4168 static int sctp_defered_wakeup_cnt = 0;
4169 
4170 int
4171 sctp_accept(struct socket *so, struct sockaddr **addr)
4172 {
4173 	struct sctp_tcb *stcb;
4174 	struct sctp_inpcb *inp;
4175 	union sctp_sockstore store;
4176 
4177 	int error;
4178 
4179 	inp = (struct sctp_inpcb *)so->so_pcb;
4180 
4181 	if (inp == 0) {
4182 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4183 		return (ECONNRESET);
4184 	}
4185 	SCTP_INP_RLOCK(inp);
4186 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4187 		SCTP_INP_RUNLOCK(inp);
4188 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4189 		return (EOPNOTSUPP);
4190 	}
4191 	if (so->so_state & SS_ISDISCONNECTED) {
4192 		SCTP_INP_RUNLOCK(inp);
4193 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4194 		return (ECONNABORTED);
4195 	}
4196 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4197 	if (stcb == NULL) {
4198 		SCTP_INP_RUNLOCK(inp);
4199 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4200 		return (ECONNRESET);
4201 	}
4202 	SCTP_TCB_LOCK(stcb);
4203 	SCTP_INP_RUNLOCK(inp);
4204 	store = stcb->asoc.primary_destination->ro._l_addr;
4205 	SCTP_TCB_UNLOCK(stcb);
4206 	if (store.sa.sa_family == AF_INET) {
4207 		struct sockaddr_in *sin;
4208 
4209 		SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4210 		sin->sin_family = AF_INET;
4211 		sin->sin_len = sizeof(*sin);
4212 		sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4213 		sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4214 		*addr = (struct sockaddr *)sin;
4215 	} else {
4216 		struct sockaddr_in6 *sin6;
4217 
4218 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4219 		sin6->sin6_family = AF_INET6;
4220 		sin6->sin6_len = sizeof(*sin6);
4221 		sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4222 
4223 		sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4224 		if ((error = sa6_recoverscope(sin6)) != 0) {
4225 			SCTP_FREE_SONAME(sin6);
4226 			return (error);
4227 		}
4228 		*addr = (struct sockaddr *)sin6;
4229 	}
4230 	/* Wake any delayed sleep action */
4231 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4232 		SCTP_INP_WLOCK(inp);
4233 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4234 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4235 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4236 			SCTP_INP_WUNLOCK(inp);
4237 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4238 			if (sowriteable(inp->sctp_socket)) {
4239 				sowwakeup_locked(inp->sctp_socket);
4240 			} else {
4241 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4242 			}
4243 			SCTP_INP_WLOCK(inp);
4244 		}
4245 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4246 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4247 			SCTP_INP_WUNLOCK(inp);
4248 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4249 			if (soreadable(inp->sctp_socket)) {
4250 				sctp_defered_wakeup_cnt++;
4251 				sorwakeup_locked(inp->sctp_socket);
4252 			} else {
4253 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4254 			}
4255 			SCTP_INP_WLOCK(inp);
4256 		}
4257 		SCTP_INP_WUNLOCK(inp);
4258 	}
4259 	return (0);
4260 }
4261 
4262 int
4263 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4264 {
4265 	struct sockaddr_in *sin;
4266 	uint32_t vrf_id;
4267 	struct sctp_inpcb *inp;
4268 	struct sctp_ifa *sctp_ifa;
4269 
4270 	/*
4271 	 * Do the malloc first in case it blocks.
4272 	 */
4273 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4274 	sin->sin_family = AF_INET;
4275 	sin->sin_len = sizeof(*sin);
4276 	inp = (struct sctp_inpcb *)so->so_pcb;
4277 	if (!inp) {
4278 		SCTP_FREE_SONAME(sin);
4279 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4280 		return ECONNRESET;
4281 	}
4282 	SCTP_INP_RLOCK(inp);
4283 	sin->sin_port = inp->sctp_lport;
4284 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4285 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4286 			struct sctp_tcb *stcb;
4287 			struct sockaddr_in *sin_a;
4288 			struct sctp_nets *net;
4289 			int fnd;
4290 
4291 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
4292 			if (stcb == NULL) {
4293 				goto notConn;
4294 			}
4295 			fnd = 0;
4296 			sin_a = NULL;
4297 			SCTP_TCB_LOCK(stcb);
4298 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4299 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4300 				if (sin_a == NULL)
4301 					/* this will make coverity happy */
4302 					continue;
4303 
4304 				if (sin_a->sin_family == AF_INET) {
4305 					fnd = 1;
4306 					break;
4307 				}
4308 			}
4309 			if ((!fnd) || (sin_a == NULL)) {
4310 				/* punt */
4311 				SCTP_TCB_UNLOCK(stcb);
4312 				goto notConn;
4313 			}
4314 			vrf_id = inp->def_vrf_id;
4315 			sctp_ifa = sctp_source_address_selection(inp,
4316 			    stcb,
4317 			    (sctp_route_t *) & net->ro,
4318 			    net, 0, vrf_id);
4319 			if (sctp_ifa) {
4320 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4321 				sctp_free_ifa(sctp_ifa);
4322 			}
4323 			SCTP_TCB_UNLOCK(stcb);
4324 		} else {
4325 			/* For the bound all case you get back 0 */
4326 	notConn:
4327 			sin->sin_addr.s_addr = 0;
4328 		}
4329 
4330 	} else {
4331 		/* Take the first IPv4 address in the list */
4332 		struct sctp_laddr *laddr;
4333 		int fnd = 0;
4334 
4335 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4336 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
4337 				struct sockaddr_in *sin_a;
4338 
4339 				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4340 				sin->sin_addr = sin_a->sin_addr;
4341 				fnd = 1;
4342 				break;
4343 			}
4344 		}
4345 		if (!fnd) {
4346 			SCTP_FREE_SONAME(sin);
4347 			SCTP_INP_RUNLOCK(inp);
4348 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4349 			return ENOENT;
4350 		}
4351 	}
4352 	SCTP_INP_RUNLOCK(inp);
4353 	(*addr) = (struct sockaddr *)sin;
4354 	return (0);
4355 }
4356 
4357 int
4358 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4359 {
4360 	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4361 	int fnd;
4362 	struct sockaddr_in *sin_a;
4363 	struct sctp_inpcb *inp;
4364 	struct sctp_tcb *stcb;
4365 	struct sctp_nets *net;
4366 
4367 	/* Do the malloc first in case it blocks. */
4368 	inp = (struct sctp_inpcb *)so->so_pcb;
4369 	if ((inp == NULL) ||
4370 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4371 		/* UDP type and listeners will drop out here */
4372 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4373 		return (ENOTCONN);
4374 	}
4375 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4376 	sin->sin_family = AF_INET;
4377 	sin->sin_len = sizeof(*sin);
4378 
4379 	/* We must recapture incase we blocked */
4380 	inp = (struct sctp_inpcb *)so->so_pcb;
4381 	if (!inp) {
4382 		SCTP_FREE_SONAME(sin);
4383 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4384 		return ECONNRESET;
4385 	}
4386 	SCTP_INP_RLOCK(inp);
4387 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4388 	if (stcb) {
4389 		SCTP_TCB_LOCK(stcb);
4390 	}
4391 	SCTP_INP_RUNLOCK(inp);
4392 	if (stcb == NULL) {
4393 		SCTP_FREE_SONAME(sin);
4394 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4395 		return ECONNRESET;
4396 	}
4397 	fnd = 0;
4398 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4399 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4400 		if (sin_a->sin_family == AF_INET) {
4401 			fnd = 1;
4402 			sin->sin_port = stcb->rport;
4403 			sin->sin_addr = sin_a->sin_addr;
4404 			break;
4405 		}
4406 	}
4407 	SCTP_TCB_UNLOCK(stcb);
4408 	if (!fnd) {
4409 		/* No IPv4 address */
4410 		SCTP_FREE_SONAME(sin);
4411 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4412 		return ENOENT;
4413 	}
4414 	(*addr) = (struct sockaddr *)sin;
4415 	return (0);
4416 }
4417 
4418 struct pr_usrreqs sctp_usrreqs = {
4419 	.pru_abort = sctp_abort,
4420 	.pru_accept = sctp_accept,
4421 	.pru_attach = sctp_attach,
4422 	.pru_bind = sctp_bind,
4423 	.pru_connect = sctp_connect,
4424 	.pru_control = in_control,
4425 	.pru_close = sctp_close,
4426 	.pru_detach = sctp_close,
4427 	.pru_sopoll = sopoll_generic,
4428 	.pru_disconnect = sctp_disconnect,
4429 	.pru_listen = sctp_listen,
4430 	.pru_peeraddr = sctp_peeraddr,
4431 	.pru_send = sctp_sendm,
4432 	.pru_shutdown = sctp_shutdown,
4433 	.pru_sockaddr = sctp_ingetaddr,
4434 	.pru_sosend = sctp_sosend,
4435 	.pru_soreceive = sctp_soreceive
4436 };
4437