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