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