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