sctp_usrreq.c revision 168124
1163953Srrs/*-
2166675Srrs * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs/* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $	 */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 168124 2007-03-31 11:47:30Z rrs $");
35166086Srrs#include <netinet/sctp_os.h>
36163953Srrs#include <sys/proc.h>
37163953Srrs#include <netinet/sctp_pcb.h>
38163953Srrs#include <netinet/sctp_header.h>
39163953Srrs#include <netinet/sctp_var.h>
40167695Srrs#if defined(INET6)
41167695Srrs#include <netinet6/sctp6_var.h>
42167695Srrs#endif
43167598Srrs#include <netinet/sctp_sysctl.h>
44163953Srrs#include <netinet/sctp_output.h>
45163953Srrs#include <netinet/sctp_bsd_addr.h>
46163953Srrs#include <netinet/sctp_uio.h>
47163953Srrs#include <netinet/sctp_asconf.h>
48163953Srrs#include <netinet/sctputil.h>
49163953Srrs#include <netinet/sctp_indata.h>
50163953Srrs#include <netinet/sctp_timer.h>
51163953Srrs#include <netinet/sctp_auth.h>
52164085Srrs
53163953Srrs
54163953Srrs
55163953Srrsvoid
56163953Srrssctp_init(void)
57163953Srrs{
58163953Srrs	/* Init the SCTP pcb in sctp_pcb.c */
59163953Srrs	u_long sb_max_adj;
60163953Srrs
61163953Srrs	sctp_pcb_init();
62163953Srrs
63163953Srrs	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
64163953Srrs		sctp_max_chunks_on_queue = (nmbclusters / 8);
65163953Srrs	/*
66163953Srrs	 * Allow a user to take no more than 1/2 the number of clusters or
67163953Srrs	 * the SB_MAX whichever is smaller for the send window.
68163953Srrs	 */
69163953Srrs	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
70163953Srrs	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
71163953Srrs	    ((nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
72163953Srrs	/*
73163953Srrs	 * Now for the recv window, should we take the same amount? or
74163953Srrs	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
75163953Srrs	 * now I will just copy.
76163953Srrs	 */
77163953Srrs	sctp_recvspace = sctp_sendspace;
78163953Srrs
79163953Srrs
80163953Srrs}
81163953Srrs
82163953Srrs
83166023Srrs
84166023Srrs/*
85166023Srrs * cleanup of the sctppcbinfo structure.
86166023Srrs * Assumes that the sctppcbinfo lock is held.
87166023Srrs */
88166023Srrsvoid
89166023Srrssctp_pcbinfo_cleanup(void)
90166023Srrs{
91166023Srrs	/* free the hash tables */
92166023Srrs	if (sctppcbinfo.sctp_asochash != NULL)
93166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
94166023Srrs	if (sctppcbinfo.sctp_ephash != NULL)
95166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
96166023Srrs	if (sctppcbinfo.sctp_tcpephash != NULL)
97166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
98166023Srrs	if (sctppcbinfo.sctp_restarthash != NULL)
99166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
100166023Srrs}
101166023Srrs
102163953Srrs
103163953Srrsstatic void
104167695Srrssctp_pathmtu_adjustment(struct sctp_inpcb *inp,
105163953Srrs    struct sctp_tcb *stcb,
106163953Srrs    struct sctp_nets *net,
107163953Srrs    uint16_t nxtsz)
108163953Srrs{
109163953Srrs	struct sctp_tmit_chunk *chk;
110163953Srrs
111163953Srrs	/* Adjust that too */
112163953Srrs	stcb->asoc.smallest_mtu = nxtsz;
113163953Srrs	/* now off to subtract IP_DF flag if needed */
114163953Srrs
115163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
116163953Srrs		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
117163953Srrs			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
118163953Srrs		}
119163953Srrs	}
120163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
121163953Srrs		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122163953Srrs			/*
123163953Srrs			 * For this guy we also mark for immediate resend
124163953Srrs			 * since we sent to big of chunk
125163953Srrs			 */
126163953Srrs			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
127163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
128163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
129163953Srrs			}
130163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
131163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
132163953Srrs
133163953Srrs			/* Clear any time so NO RTT is being done */
134163953Srrs			chk->do_rtt = 0;
135163953Srrs			if (stcb->asoc.total_flight >= chk->book_size)
136163953Srrs				stcb->asoc.total_flight -= chk->book_size;
137163953Srrs			else
138163953Srrs				stcb->asoc.total_flight = 0;
139163953Srrs			if (stcb->asoc.total_flight_count > 0)
140163953Srrs				stcb->asoc.total_flight_count--;
141163953Srrs			if (net->flight_size >= chk->book_size)
142163953Srrs				net->flight_size -= chk->book_size;
143163953Srrs			else
144163953Srrs				net->flight_size = 0;
145163953Srrs		}
146163953Srrs	}
147163953Srrs}
148163953Srrs
149163953Srrsstatic void
150163953Srrssctp_notify_mbuf(struct sctp_inpcb *inp,
151163953Srrs    struct sctp_tcb *stcb,
152163953Srrs    struct sctp_nets *net,
153163953Srrs    struct ip *ip,
154163953Srrs    struct sctphdr *sh)
155163953Srrs{
156163953Srrs	struct icmp *icmph;
157163953Srrs	int totsz, tmr_stopped = 0;
158163953Srrs	uint16_t nxtsz;
159163953Srrs
160163953Srrs	/* protection */
161163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
162163953Srrs	    (ip == NULL) || (sh == NULL)) {
163163953Srrs		if (stcb != NULL)
164163953Srrs			SCTP_TCB_UNLOCK(stcb);
165163953Srrs		return;
166163953Srrs	}
167163953Srrs	/* First job is to verify the vtag matches what I would send */
168163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
169163953Srrs		SCTP_TCB_UNLOCK(stcb);
170163953Srrs		return;
171163953Srrs	}
172163953Srrs	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
173163953Srrs	    sizeof(struct ip)));
174163953Srrs	if (icmph->icmp_type != ICMP_UNREACH) {
175163953Srrs		/* We only care about unreachable */
176163953Srrs		SCTP_TCB_UNLOCK(stcb);
177163953Srrs		return;
178163953Srrs	}
179163953Srrs	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
180163953Srrs		/* not a unreachable message due to frag. */
181163953Srrs		SCTP_TCB_UNLOCK(stcb);
182163953Srrs		return;
183163953Srrs	}
184163953Srrs	totsz = ip->ip_len;
185163953Srrs
186163953Srrs	nxtsz = ntohs(icmph->icmp_seq);
187163953Srrs	if (nxtsz == 0) {
188163953Srrs		/*
189163953Srrs		 * old type router that does not tell us what the next size
190163953Srrs		 * mtu is. Rats we will have to guess (in a educated fashion
191163953Srrs		 * of course)
192163953Srrs		 */
193163953Srrs		nxtsz = find_next_best_mtu(totsz);
194163953Srrs	}
195163953Srrs	/* Stop any PMTU timer */
196165647Srrs	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
197163953Srrs		tmr_stopped = 1;
198165220Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
199165220Srrs		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
200163953Srrs	}
201163953Srrs	/* Adjust destination size limit */
202163953Srrs	if (net->mtu > nxtsz) {
203163953Srrs		net->mtu = nxtsz;
204163953Srrs	}
205163953Srrs	/* now what about the ep? */
206163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
207167695Srrs		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
208163953Srrs	}
209163953Srrs	if (tmr_stopped)
210163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
211163953Srrs
212163953Srrs	SCTP_TCB_UNLOCK(stcb);
213163953Srrs}
214163953Srrs
215163953Srrs
216163953Srrsvoid
217163953Srrssctp_notify(struct sctp_inpcb *inp,
218167695Srrs    int error,
219163953Srrs    struct sctphdr *sh,
220163953Srrs    struct sockaddr *to,
221163953Srrs    struct sctp_tcb *stcb,
222163953Srrs    struct sctp_nets *net)
223163953Srrs{
224163953Srrs	/* protection */
225163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
226163953Srrs	    (sh == NULL) || (to == NULL)) {
227163953Srrs		return;
228163953Srrs	}
229163953Srrs	/* First job is to verify the vtag matches what I would send */
230163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
231163953Srrs		return;
232163953Srrs	}
233163953Srrs	/* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
234163953Srrs
235167695Srrs	if ((error == EHOSTUNREACH) ||	/* Host is not reachable */
236167695Srrs	    (error == EHOSTDOWN) ||	/* Host is down */
237167695Srrs	    (error == ECONNREFUSED) ||	/* Host refused the connection, (not
238163953Srrs					 * an abort?) */
239167695Srrs	    (error == ENOPROTOOPT)	/* SCTP is not present on host */
240163953Srrs	    ) {
241163953Srrs		/*
242163953Srrs		 * Hmm reachablity problems we must examine closely. If its
243163953Srrs		 * not reachable, we may have lost a network. Or if there is
244163953Srrs		 * NO protocol at the other end named SCTP. well we consider
245163953Srrs		 * it a OOTB abort.
246163953Srrs		 */
247167695Srrs		if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
248163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
249163953Srrs				/* Ok that destination is NOT reachable */
250167598Srrs				printf("ICMP (thresh %d/%d) takes interface %p down\n",
251167598Srrs				    net->error_count,
252167598Srrs				    net->failure_threshold,
253167598Srrs				    net);
254167598Srrs
255163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
256163953Srrs				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
257163953Srrs				net->error_count = net->failure_threshold + 1;
258163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
259163953Srrs				    stcb, SCTP_FAILED_THRESHOLD,
260163953Srrs				    (void *)net);
261163953Srrs			}
262163953Srrs			if (stcb)
263163953Srrs				SCTP_TCB_UNLOCK(stcb);
264163953Srrs		} else {
265163953Srrs			/*
266163953Srrs			 * Here the peer is either playing tricks on us,
267163953Srrs			 * including an address that belongs to someone who
268163953Srrs			 * does not support SCTP OR was a userland
269163953Srrs			 * implementation that shutdown and now is dead. In
270163953Srrs			 * either case treat it like a OOTB abort with no
271163953Srrs			 * TCB
272163953Srrs			 */
273163953Srrs			sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
274165220Srrs			sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
275163953Srrs			/* no need to unlock here, since the TCB is gone */
276163953Srrs		}
277163953Srrs	} else {
278163953Srrs		/* Send all others to the app */
279163953Srrs		if (stcb)
280163953Srrs			SCTP_TCB_UNLOCK(stcb);
281163953Srrs
282163953Srrs
283163953Srrs		if (inp->sctp_socket) {
284163953Srrs#ifdef SCTP_LOCK_LOGGING
285163953Srrs			sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
286163953Srrs#endif
287163953Srrs			SOCK_LOCK(inp->sctp_socket);
288167695Srrs			inp->sctp_socket->so_error = error;
289163953Srrs			sctp_sowwakeup(inp, inp->sctp_socket);
290163953Srrs			SOCK_UNLOCK(inp->sctp_socket);
291163953Srrs		}
292163953Srrs	}
293163953Srrs}
294163953Srrs
295163953Srrsvoid
296163953Srrssctp_ctlinput(cmd, sa, vip)
297163953Srrs	int cmd;
298163953Srrs	struct sockaddr *sa;
299163953Srrs	void *vip;
300163953Srrs{
301163953Srrs	struct ip *ip = vip;
302163953Srrs	struct sctphdr *sh;
303167598Srrs	uint32_t vrf_id;
304163953Srrs
305167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
306163953Srrs	if (sa->sa_family != AF_INET ||
307163953Srrs	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
308163953Srrs		return;
309163953Srrs	}
310163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
311163953Srrs		ip = 0;
312163953Srrs	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
313163953Srrs		return;
314163953Srrs	}
315163953Srrs	if (ip) {
316163953Srrs		struct sctp_inpcb *inp = NULL;
317163953Srrs		struct sctp_tcb *stcb = NULL;
318163953Srrs		struct sctp_nets *net = NULL;
319163953Srrs		struct sockaddr_in to, from;
320163953Srrs
321163953Srrs		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
322163953Srrs		bzero(&to, sizeof(to));
323163953Srrs		bzero(&from, sizeof(from));
324163953Srrs		from.sin_family = to.sin_family = AF_INET;
325163953Srrs		from.sin_len = to.sin_len = sizeof(to);
326163953Srrs		from.sin_port = sh->src_port;
327163953Srrs		from.sin_addr = ip->ip_src;
328163953Srrs		to.sin_port = sh->dest_port;
329163953Srrs		to.sin_addr = ip->ip_dst;
330163953Srrs
331163953Srrs		/*
332163953Srrs		 * 'to' holds the dest of the packet that failed to be sent.
333163953Srrs		 * 'from' holds our local endpoint address. Thus we reverse
334163953Srrs		 * the to and the from in the lookup.
335163953Srrs		 */
336163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
337163953Srrs		    (struct sockaddr *)&to,
338167598Srrs		    &inp, &net, 1, vrf_id);
339163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
340163953Srrs			if (cmd != PRC_MSGSIZE) {
341163953Srrs				int cm;
342163953Srrs
343163953Srrs				if (cmd == PRC_HOSTDEAD) {
344163953Srrs					cm = EHOSTUNREACH;
345163953Srrs				} else {
346163953Srrs					cm = inetctlerrmap[cmd];
347163953Srrs				}
348163953Srrs				sctp_notify(inp, cm, sh,
349163953Srrs				    (struct sockaddr *)&to, stcb,
350163953Srrs				    net);
351163953Srrs			} else {
352163953Srrs				/* handle possible ICMP size messages */
353163953Srrs				sctp_notify_mbuf(inp, stcb, net, ip, sh);
354163953Srrs			}
355163953Srrs		} else {
356163953Srrs			if ((stcb == NULL) && (inp != NULL)) {
357163953Srrs				/* reduce ref-count */
358163953Srrs				SCTP_INP_WLOCK(inp);
359163953Srrs				SCTP_INP_DECR_REF(inp);
360163953Srrs				SCTP_INP_WUNLOCK(inp);
361163953Srrs			}
362163953Srrs		}
363163953Srrs	}
364163953Srrs	return;
365163953Srrs}
366163953Srrs
367163953Srrsstatic int
368163953Srrssctp_getcred(SYSCTL_HANDLER_ARGS)
369163953Srrs{
370164085Srrs	struct xucred xuc;
371163953Srrs	struct sockaddr_in addrs[2];
372163953Srrs	struct sctp_inpcb *inp;
373163953Srrs	struct sctp_nets *net;
374163953Srrs	struct sctp_tcb *stcb;
375164085Srrs	int error;
376167598Srrs	uint32_t vrf_id;
377163953Srrs
378167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
379164039Srwatson	/*
380164039Srwatson	 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
381164039Srwatson	 * visibility is scoped using cr_canseesocket(), which it is not
382164039Srwatson	 * here.
383164039Srwatson	 */
384167598Srrs	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
385167598Srrs	    SUSER_ALLOWJAIL);
386163953Srrs	if (error)
387163953Srrs		return (error);
388164039Srwatson
389163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
390163953Srrs	if (error)
391163953Srrs		return (error);
392163953Srrs
393163953Srrs	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
394163953Srrs	    sintosa(&addrs[1]),
395167598Srrs	    &inp, &net, 1, vrf_id);
396163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
397163953Srrs		if ((inp != NULL) && (stcb == NULL)) {
398163953Srrs			/* reduce ref-count */
399163953Srrs			SCTP_INP_WLOCK(inp);
400163953Srrs			SCTP_INP_DECR_REF(inp);
401164085Srrs			goto cred_can_cont;
402163953Srrs		}
403163953Srrs		error = ENOENT;
404163953Srrs		goto out;
405163953Srrs	}
406163953Srrs	SCTP_TCB_UNLOCK(stcb);
407164085Srrs	/*
408164085Srrs	 * We use the write lock here, only since in the error leg we need
409164085Srrs	 * it. If we used RLOCK, then we would have to
410164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
411164085Srrs	 * Better to use higher wlock.
412164085Srrs	 */
413164085Srrs	SCTP_INP_WLOCK(inp);
414164085Srrscred_can_cont:
415164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
416164085Srrs	if (error) {
417164085Srrs		SCTP_INP_WUNLOCK(inp);
418164085Srrs		goto out;
419164085Srrs	}
420164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
421164085Srrs	SCTP_INP_WUNLOCK(inp);
422164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
423163953Srrsout:
424163953Srrs	return (error);
425163953Srrs}
426163953Srrs
427163953SrrsSYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
428163953Srrs    0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
429163953Srrs
430163953Srrs
431163953Srrsstatic void
432163953Srrssctp_abort(struct socket *so)
433163953Srrs{
434163953Srrs	struct sctp_inpcb *inp;
435163953Srrs	uint32_t flags;
436163953Srrs
437163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
438163953Srrs	if (inp == 0)
439163953Srrs		return;
440163953Srrs
441163953Srrssctp_must_try_again:
442163953Srrs	flags = inp->sctp_flags;
443163953Srrs#ifdef SCTP_LOG_CLOSING
444163953Srrs	sctp_log_closing(inp, NULL, 17);
445163953Srrs#endif
446163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
447163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
448163953Srrs#ifdef SCTP_LOG_CLOSING
449163953Srrs		sctp_log_closing(inp, NULL, 16);
450163953Srrs#endif
451163953Srrs		sctp_inpcb_free(inp, 1, 0);
452163953Srrs		SOCK_LOCK(so);
453167695Srrs		SCTP_SB_CLEAR(so->so_snd);
454163953Srrs		/*
455163953Srrs		 * same for the rcv ones, they are only here for the
456163953Srrs		 * accounting/select.
457163953Srrs		 */
458167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
459167695Srrs
460167695Srrs		/* Now null out the reference, we are completely detached. */
461163953Srrs		so->so_pcb = NULL;
462163953Srrs		SOCK_UNLOCK(so);
463163953Srrs	} else {
464163953Srrs		flags = inp->sctp_flags;
465163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
466163953Srrs			goto sctp_must_try_again;
467163953Srrs		}
468163953Srrs	}
469163953Srrs	return;
470163953Srrs}
471163953Srrs
472163953Srrsstatic int
473163953Srrssctp_attach(struct socket *so, int proto, struct thread *p)
474163953Srrs{
475163953Srrs	struct sctp_inpcb *inp;
476163953Srrs	struct inpcb *ip_inp;
477166086Srrs	int error;
478163953Srrs
479163953Srrs#ifdef IPSEC
480163953Srrs	uint32_t flags;
481163953Srrs
482163953Srrs#endif
483163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
484163953Srrs	if (inp != 0) {
485163953Srrs		return EINVAL;
486163953Srrs	}
487167695Srrs	error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
488163953Srrs	if (error) {
489163953Srrs		return error;
490163953Srrs	}
491163953Srrs	error = sctp_inpcb_alloc(so);
492163953Srrs	if (error) {
493163953Srrs		return error;
494163953Srrs	}
495163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
496163953Srrs	SCTP_INP_WLOCK(inp);
497163953Srrs
498163953Srrs	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
499163953Srrs	ip_inp = &inp->ip_inp.inp;
500163953Srrs	ip_inp->inp_vflag |= INP_IPV4;
501163953Srrs	ip_inp->inp_ip_ttl = ip_defttl;
502163953Srrs
503163953Srrs#ifdef IPSEC
504163953Srrs	error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
505163953Srrs#ifdef SCTP_LOG_CLOSING
506163953Srrs	sctp_log_closing(inp, NULL, 17);
507163953Srrs#endif
508163953Srrs	if (error != 0) {
509163953Srrs		flags = inp->sctp_flags;
510163953Srrs		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
511163953Srrs		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
512163953Srrs#ifdef SCTP_LOG_CLOSING
513163953Srrs			sctp_log_closing(inp, NULL, 15);
514163953Srrs#endif
515163953Srrs			sctp_inpcb_free(inp, 1, 0);
516163953Srrs		}
517163953Srrs		return error;
518163953Srrs	}
519163953Srrs#endif				/* IPSEC */
520163953Srrs	SCTP_INP_WUNLOCK(inp);
521163953Srrs	return 0;
522163953Srrs}
523163953Srrs
524163953Srrsstatic int
525163953Srrssctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
526163953Srrs{
527163953Srrs	struct sctp_inpcb *inp;
528166086Srrs	int error;
529163953Srrs
530163953Srrs#ifdef INET6
531163953Srrs	if (addr && addr->sa_family != AF_INET)
532163953Srrs		/* must be a v4 address! */
533163953Srrs		return EINVAL;
534163953Srrs#endif				/* INET6 */
535163953Srrs
536163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
537163953Srrs	if (inp == 0)
538163953Srrs		return EINVAL;
539163953Srrs
540163953Srrs	error = sctp_inpcb_bind(so, addr, p);
541163953Srrs	return error;
542163953Srrs}
543163953Srrs
544163953Srrsstatic void
545163953Srrssctp_close(struct socket *so)
546163953Srrs{
547163953Srrs	struct sctp_inpcb *inp;
548163953Srrs	uint32_t flags;
549163953Srrs
550163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
551163953Srrs	if (inp == 0)
552163953Srrs		return;
553163953Srrs
554163953Srrs	/*
555163953Srrs	 * Inform all the lower layer assoc that we are done.
556163953Srrs	 */
557163953Srrssctp_must_try_again:
558163953Srrs	flags = inp->sctp_flags;
559163953Srrs#ifdef SCTP_LOG_CLOSING
560163953Srrs	sctp_log_closing(inp, NULL, 17);
561163953Srrs#endif
562163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
563163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
564163953Srrs		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
565163953Srrs		    (so->so_rcv.sb_cc > 0)) {
566163953Srrs#ifdef SCTP_LOG_CLOSING
567163953Srrs			sctp_log_closing(inp, NULL, 13);
568163953Srrs#endif
569163953Srrs			sctp_inpcb_free(inp, 1, 1);
570163953Srrs		} else {
571163953Srrs#ifdef SCTP_LOG_CLOSING
572163953Srrs			sctp_log_closing(inp, NULL, 14);
573163953Srrs#endif
574163953Srrs			sctp_inpcb_free(inp, 0, 1);
575163953Srrs		}
576163953Srrs		/*
577163953Srrs		 * The socket is now detached, no matter what the state of
578163953Srrs		 * the SCTP association.
579163953Srrs		 */
580163953Srrs		SOCK_LOCK(so);
581167695Srrs		SCTP_SB_CLEAR(so->so_snd);
582163953Srrs		/*
583163953Srrs		 * same for the rcv ones, they are only here for the
584163953Srrs		 * accounting/select.
585163953Srrs		 */
586167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
587167695Srrs
588167695Srrs		/* Now null out the reference, we are completely detached. */
589163953Srrs		so->so_pcb = NULL;
590163953Srrs		SOCK_UNLOCK(so);
591163953Srrs	} else {
592163953Srrs		flags = inp->sctp_flags;
593163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
594163953Srrs			goto sctp_must_try_again;
595163953Srrs		}
596163953Srrs	}
597163953Srrs	return;
598163953Srrs}
599163953Srrs
600163953Srrs
601163953Srrsint
602163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
603163953Srrs    struct mbuf *control, struct thread *p);
604163953Srrs
605163953Srrs
606163953Srrsint
607163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
608163953Srrs    struct mbuf *control, struct thread *p)
609163953Srrs{
610163953Srrs	struct sctp_inpcb *inp;
611163953Srrs	int error;
612163953Srrs
613163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
614163953Srrs	if (inp == 0) {
615163953Srrs		if (control) {
616163953Srrs			sctp_m_freem(control);
617163953Srrs			control = NULL;
618163953Srrs		}
619163953Srrs		sctp_m_freem(m);
620163953Srrs		return EINVAL;
621163953Srrs	}
622163953Srrs	/* Got to have an to address if we are NOT a connected socket */
623163953Srrs	if ((addr == NULL) &&
624163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
625163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
626163953Srrs	    ) {
627163953Srrs		goto connected_type;
628163953Srrs	} else if (addr == NULL) {
629163953Srrs		error = EDESTADDRREQ;
630163953Srrs		sctp_m_freem(m);
631163953Srrs		if (control) {
632163953Srrs			sctp_m_freem(control);
633163953Srrs			control = NULL;
634163953Srrs		}
635163953Srrs		return (error);
636163953Srrs	}
637163953Srrs#ifdef INET6
638163953Srrs	if (addr->sa_family != AF_INET) {
639163953Srrs		/* must be a v4 address! */
640163953Srrs		sctp_m_freem(m);
641163953Srrs		if (control) {
642163953Srrs			sctp_m_freem(control);
643163953Srrs			control = NULL;
644163953Srrs		}
645163953Srrs		error = EDESTADDRREQ;
646163953Srrs		return EINVAL;
647163953Srrs	}
648163953Srrs#endif				/* INET6 */
649163953Srrsconnected_type:
650163953Srrs	/* now what about control */
651163953Srrs	if (control) {
652163953Srrs		if (inp->control) {
653163953Srrs			printf("huh? control set?\n");
654163953Srrs			sctp_m_freem(inp->control);
655163953Srrs			inp->control = NULL;
656163953Srrs		}
657163953Srrs		inp->control = control;
658163953Srrs	}
659163953Srrs	/* Place the data */
660163953Srrs	if (inp->pkt) {
661165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
662163953Srrs		inp->pkt_last = m;
663163953Srrs	} else {
664163953Srrs		inp->pkt_last = inp->pkt = m;
665163953Srrs	}
666163953Srrs	if (
667163953Srrs	/* FreeBSD uses a flag passed */
668163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
669163953Srrs	    ) {
670163953Srrs		/*
671163953Srrs		 * note with the current version this code will only be used
672163953Srrs		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
673163953Srrs		 * re-defining sosend to use the sctp_sosend. One can
674163953Srrs		 * optionally switch back to this code (by changing back the
675163953Srrs		 * definitions) but this is not advisable. This code is used
676163953Srrs		 * by FreeBSD when sending a file with sendfile() though.
677163953Srrs		 */
678163953Srrs		int ret;
679163953Srrs
680163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
681163953Srrs		inp->pkt = NULL;
682163953Srrs		inp->control = NULL;
683163953Srrs		return (ret);
684163953Srrs	} else {
685163953Srrs		return (0);
686163953Srrs	}
687163953Srrs}
688163953Srrs
689163953Srrsstatic int
690163953Srrssctp_disconnect(struct socket *so)
691163953Srrs{
692163953Srrs	struct sctp_inpcb *inp;
693163953Srrs
694163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
695163953Srrs	if (inp == NULL) {
696163953Srrs		return (ENOTCONN);
697163953Srrs	}
698163953Srrs	SCTP_INP_RLOCK(inp);
699163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
700166675Srrs		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
701163953Srrs			/* No connection */
702163953Srrs			SCTP_INP_RUNLOCK(inp);
703163953Srrs			return (0);
704163953Srrs		} else {
705163953Srrs			struct sctp_association *asoc;
706163953Srrs			struct sctp_tcb *stcb;
707163953Srrs
708163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
709163953Srrs			if (stcb == NULL) {
710163953Srrs				SCTP_INP_RUNLOCK(inp);
711163953Srrs				return (EINVAL);
712163953Srrs			}
713163953Srrs			SCTP_TCB_LOCK(stcb);
714163953Srrs			asoc = &stcb->asoc;
715163953Srrs			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
716163953Srrs				/* We are about to be freed, out of here */
717163953Srrs				SCTP_TCB_UNLOCK(stcb);
718163953Srrs				SCTP_INP_RUNLOCK(inp);
719163953Srrs				return (0);
720163953Srrs			}
721163953Srrs			if (((so->so_options & SO_LINGER) &&
722163953Srrs			    (so->so_linger == 0)) ||
723163953Srrs			    (so->so_rcv.sb_cc > 0)) {
724163953Srrs				if (SCTP_GET_STATE(asoc) !=
725163953Srrs				    SCTP_STATE_COOKIE_WAIT) {
726163953Srrs					/* Left with Data unread */
727163953Srrs					struct mbuf *err;
728163953Srrs
729163953Srrs					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
730163953Srrs					if (err) {
731163953Srrs						/*
732163953Srrs						 * Fill in the user
733163953Srrs						 * initiated abort
734163953Srrs						 */
735163953Srrs						struct sctp_paramhdr *ph;
736163953Srrs
737163953Srrs						ph = mtod(err, struct sctp_paramhdr *);
738165647Srrs						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
739163953Srrs						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
740165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(err));
741163953Srrs					}
742163953Srrs					sctp_send_abort_tcb(stcb, err);
743163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
744163953Srrs				}
745163953Srrs				SCTP_INP_RUNLOCK(inp);
746163953Srrs				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
747163953Srrs				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
748163953Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
749163953Srrs				}
750165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
751163953Srrs				/* No unlock tcb assoc is gone */
752163953Srrs				return (0);
753163953Srrs			}
754163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
755163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
756163953Srrs			    (asoc->stream_queue_cnt == 0)) {
757163953Srrs				/* there is nothing queued to send, so done */
758163953Srrs				if (asoc->locked_on_sending) {
759163953Srrs					goto abort_anyway;
760163953Srrs				}
761166675Srrs				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
762166675Srrs				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
763163953Srrs					/* only send SHUTDOWN 1st time thru */
764163953Srrs					sctp_stop_timers_for_shutdown(stcb);
765163953Srrs					sctp_send_shutdown(stcb,
766163953Srrs					    stcb->asoc.primary_destination);
767163953Srrs					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
768166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
769166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
770166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
771166675Srrs					}
772163953Srrs					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
773163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
774163953Srrs					    stcb->sctp_ep, stcb,
775163953Srrs					    asoc->primary_destination);
776163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
777163953Srrs					    stcb->sctp_ep, stcb,
778163953Srrs					    asoc->primary_destination);
779163953Srrs				}
780163953Srrs			} else {
781163953Srrs				/*
782163953Srrs				 * we still got (or just got) data to send,
783163953Srrs				 * so set SHUTDOWN_PENDING
784163953Srrs				 */
785163953Srrs				/*
786163953Srrs				 * XXX sockets draft says that SCTP_EOF
787163953Srrs				 * should be sent with no data. currently,
788163953Srrs				 * we will allow user data to be sent first
789163953Srrs				 * and move to SHUTDOWN-PENDING
790163953Srrs				 */
791163953Srrs				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
792163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
793163953Srrs				    asoc->primary_destination);
794163953Srrs				if (asoc->locked_on_sending) {
795163953Srrs					/* Locked to send out the data */
796163953Srrs					struct sctp_stream_queue_pending *sp;
797163953Srrs
798163953Srrs					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
799163953Srrs					if (sp == NULL) {
800163959Srrs						printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
801163953Srrs						    asoc->locked_on_sending->stream_no);
802163953Srrs					} else {
803163953Srrs						if ((sp->length == 0) && (sp->msg_is_complete == 0))
804163953Srrs							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
805163953Srrs					}
806163953Srrs				}
807163953Srrs				if (TAILQ_EMPTY(&asoc->send_queue) &&
808163953Srrs				    TAILQ_EMPTY(&asoc->sent_queue) &&
809163953Srrs				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
810163953Srrs					struct mbuf *op_err;
811163953Srrs
812163953Srrs			abort_anyway:
813163953Srrs					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
814163953Srrs					    0, M_DONTWAIT, 1, MT_DATA);
815163953Srrs					if (op_err) {
816163953Srrs						/*
817163953Srrs						 * Fill in the user
818163953Srrs						 * initiated abort
819163953Srrs						 */
820163953Srrs						struct sctp_paramhdr *ph;
821163953Srrs						uint32_t *ippp;
822163953Srrs
823165647Srrs						SCTP_BUF_LEN(op_err) =
824163953Srrs						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
825163953Srrs						ph = mtod(op_err,
826163953Srrs						    struct sctp_paramhdr *);
827163953Srrs						ph->param_type = htons(
828163953Srrs						    SCTP_CAUSE_USER_INITIATED_ABT);
829165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(op_err));
830163953Srrs						ippp = (uint32_t *) (ph + 1);
831165220Srrs						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
832163953Srrs					}
833165220Srrs					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
834163953Srrs					sctp_send_abort_tcb(stcb, op_err);
835163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
836163953Srrs					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
837163953Srrs					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
838163953Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
839163953Srrs					}
840163953Srrs					SCTP_INP_RUNLOCK(inp);
841165220Srrs					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
842163953Srrs					return (0);
843163953Srrs				}
844163953Srrs			}
845163953Srrs			SCTP_TCB_UNLOCK(stcb);
846163953Srrs			SCTP_INP_RUNLOCK(inp);
847163953Srrs			return (0);
848163953Srrs		}
849163953Srrs		/* not reached */
850163953Srrs	} else {
851163953Srrs		/* UDP model does not support this */
852163953Srrs		SCTP_INP_RUNLOCK(inp);
853163953Srrs		return EOPNOTSUPP;
854163953Srrs	}
855163953Srrs}
856163953Srrs
857163953Srrsint
858163953Srrssctp_shutdown(struct socket *so)
859163953Srrs{
860163953Srrs	struct sctp_inpcb *inp;
861163953Srrs
862163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
863163953Srrs	if (inp == 0) {
864163953Srrs		return EINVAL;
865163953Srrs	}
866163953Srrs	SCTP_INP_RLOCK(inp);
867163953Srrs	/* For UDP model this is a invalid call */
868163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
869163953Srrs		/* Restore the flags that the soshutdown took away. */
870163953Srrs		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
871163953Srrs		/* This proc will wakeup for read and do nothing (I hope) */
872163953Srrs		SCTP_INP_RUNLOCK(inp);
873163953Srrs		return (EOPNOTSUPP);
874163953Srrs	}
875163953Srrs	/*
876163953Srrs	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
877163953Srrs	 * or SHUT_RDWR. This means we put the shutdown flag against it.
878163953Srrs	 */
879163953Srrs	{
880163953Srrs		struct sctp_tcb *stcb;
881163953Srrs		struct sctp_association *asoc;
882163953Srrs
883163953Srrs		socantsendmore(so);
884163953Srrs
885163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
886163953Srrs		if (stcb == NULL) {
887163953Srrs			/*
888163953Srrs			 * Ok we hit the case that the shutdown call was
889163953Srrs			 * made after an abort or something. Nothing to do
890163953Srrs			 * now.
891163953Srrs			 */
892163953Srrs			return (0);
893163953Srrs		}
894163953Srrs		SCTP_TCB_LOCK(stcb);
895163953Srrs		asoc = &stcb->asoc;
896163953Srrs		if (TAILQ_EMPTY(&asoc->send_queue) &&
897163953Srrs		    TAILQ_EMPTY(&asoc->sent_queue) &&
898163953Srrs		    (asoc->stream_queue_cnt == 0)) {
899163953Srrs			if (asoc->locked_on_sending) {
900163953Srrs				goto abort_anyway;
901163953Srrs			}
902163953Srrs			/* there is nothing queued to send, so I'm done... */
903163953Srrs			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
904163953Srrs				/* only send SHUTDOWN the first time through */
905163953Srrs				sctp_stop_timers_for_shutdown(stcb);
906163953Srrs				sctp_send_shutdown(stcb,
907163953Srrs				    stcb->asoc.primary_destination);
908163953Srrs				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
909166675Srrs				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
910166675Srrs				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
911166675Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
912166675Srrs				}
913163953Srrs				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
914163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
915163953Srrs				    stcb->sctp_ep, stcb,
916163953Srrs				    asoc->primary_destination);
917163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
918163953Srrs				    stcb->sctp_ep, stcb,
919163953Srrs				    asoc->primary_destination);
920163953Srrs			}
921163953Srrs		} else {
922163953Srrs			/*
923163953Srrs			 * we still got (or just got) data to send, so set
924163953Srrs			 * SHUTDOWN_PENDING
925163953Srrs			 */
926163953Srrs			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
927163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
928163953Srrs			    asoc->primary_destination);
929163953Srrs
930163953Srrs			if (asoc->locked_on_sending) {
931163953Srrs				/* Locked to send out the data */
932163953Srrs				struct sctp_stream_queue_pending *sp;
933163953Srrs
934163953Srrs				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
935163953Srrs				if (sp == NULL) {
936163959Srrs					printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
937163953Srrs					    asoc->locked_on_sending->stream_no);
938163953Srrs				} else {
939163953Srrs					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
940163953Srrs						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
941163953Srrs					}
942163953Srrs				}
943163953Srrs			}
944163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
945163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
946163953Srrs			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
947163953Srrs				struct mbuf *op_err;
948163953Srrs
949163953Srrs		abort_anyway:
950163953Srrs				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
951163953Srrs				    0, M_DONTWAIT, 1, MT_DATA);
952163953Srrs				if (op_err) {
953163953Srrs					/* Fill in the user initiated abort */
954163953Srrs					struct sctp_paramhdr *ph;
955163953Srrs					uint32_t *ippp;
956163953Srrs
957165647Srrs					SCTP_BUF_LEN(op_err) =
958163953Srrs					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
959163953Srrs					ph = mtod(op_err,
960163953Srrs					    struct sctp_paramhdr *);
961163953Srrs					ph->param_type = htons(
962163953Srrs					    SCTP_CAUSE_USER_INITIATED_ABT);
963165647Srrs					ph->param_length = htons(SCTP_BUF_LEN(op_err));
964163953Srrs					ippp = (uint32_t *) (ph + 1);
965165220Srrs					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
966163953Srrs				}
967165220Srrs				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
968163953Srrs				sctp_abort_an_association(stcb->sctp_ep, stcb,
969163953Srrs				    SCTP_RESPONSE_TO_USER_REQ,
970163953Srrs				    op_err);
971163953Srrs				goto skip_unlock;
972163953Srrs			}
973163953Srrs		}
974163953Srrs		SCTP_TCB_UNLOCK(stcb);
975163953Srrs	}
976163953Srrsskip_unlock:
977163953Srrs	SCTP_INP_RUNLOCK(inp);
978163953Srrs	return 0;
979163953Srrs}
980163953Srrs
981163953Srrs/*
982163953Srrs * copies a "user" presentable address and removes embedded scope, etc.
983163953Srrs * returns 0 on success, 1 on error
984163953Srrs */
985163953Srrsstatic uint32_t
986163953Srrssctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
987163953Srrs{
988163953Srrs	struct sockaddr_in6 lsa6;
989163953Srrs
990163953Srrs	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
991163953Srrs	    &lsa6);
992163953Srrs	memcpy(ss, sa, sa->sa_len);
993163953Srrs	return (0);
994163953Srrs}
995163953Srrs
996163953Srrs
997163953Srrs
998166675Srrsstatic size_t
999168124Srrssctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1000163953Srrs    struct sctp_tcb *stcb,
1001166675Srrs    size_t limit,
1002167598Srrs    struct sockaddr_storage *sas,
1003167598Srrs    uint32_t vrf_id)
1004163953Srrs{
1005167598Srrs	struct sctp_ifn *sctp_ifn;
1006167598Srrs	struct sctp_ifa *sctp_ifa;
1007166675Srrs	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1008166675Srrs	size_t actual;
1009163953Srrs	int ipv4_addr_legal, ipv6_addr_legal;
1010167598Srrs	struct sctp_vrf *vrf;
1011163953Srrs
1012163953Srrs	actual = 0;
1013163953Srrs	if (limit <= 0)
1014163953Srrs		return (actual);
1015163953Srrs
1016163953Srrs	if (stcb) {
1017163953Srrs		/* Turn on all the appropriate scope */
1018163953Srrs		loopback_scope = stcb->asoc.loopback_scope;
1019163953Srrs		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1020163953Srrs		local_scope = stcb->asoc.local_scope;
1021163953Srrs		site_scope = stcb->asoc.site_scope;
1022163953Srrs	} else {
1023163953Srrs		/* Turn on ALL scope, since we look at the EP */
1024163953Srrs		loopback_scope = ipv4_local_scope = local_scope =
1025163953Srrs		    site_scope = 1;
1026163953Srrs	}
1027163953Srrs	ipv4_addr_legal = ipv6_addr_legal = 0;
1028163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1029163953Srrs		ipv6_addr_legal = 1;
1030166023Srrs		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1031163953Srrs			ipv4_addr_legal = 1;
1032163953Srrs		}
1033163953Srrs	} else {
1034163953Srrs		ipv4_addr_legal = 1;
1035163953Srrs	}
1036167598Srrs	vrf = sctp_find_vrf(vrf_id);
1037167598Srrs	if (vrf == NULL) {
1038167598Srrs		return (0);
1039167598Srrs	}
1040163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1041167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1042163953Srrs			if ((loopback_scope == 0) &&
1043167598Srrs			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1044163953Srrs				/* Skip loopback if loopback_scope not set */
1045163953Srrs				continue;
1046163953Srrs			}
1047167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1048163953Srrs				if (stcb) {
1049163953Srrs					/*
1050163953Srrs					 * For the BOUND-ALL case, the list
1051163953Srrs					 * associated with a TCB is Always
1052163953Srrs					 * considered a reverse list.. i.e.
1053163953Srrs					 * it lists addresses that are NOT
1054163953Srrs					 * part of the association. If this
1055163953Srrs					 * is one of those we must skip it.
1056163953Srrs					 */
1057163953Srrs					if (sctp_is_addr_restricted(stcb,
1058167598Srrs					    sctp_ifa)) {
1059163953Srrs						continue;
1060163953Srrs					}
1061163953Srrs				}
1062167598Srrs				if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1063163953Srrs				    (ipv4_addr_legal)) {
1064163953Srrs					struct sockaddr_in *sin;
1065163953Srrs
1066167598Srrs					sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1067163953Srrs					if (sin->sin_addr.s_addr == 0) {
1068163953Srrs						/*
1069163953Srrs						 * we skip unspecifed
1070163953Srrs						 * addresses
1071163953Srrs						 */
1072163953Srrs						continue;
1073163953Srrs					}
1074163953Srrs					if ((ipv4_local_scope == 0) &&
1075163953Srrs					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1076163953Srrs						continue;
1077163953Srrs					}
1078163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1079163953Srrs						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1080163953Srrs						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1081163953Srrs						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1082163953Srrs						actual += sizeof(sizeof(struct sockaddr_in6));
1083163953Srrs					} else {
1084163953Srrs						memcpy(sas, sin, sizeof(*sin));
1085163953Srrs						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1086163953Srrs						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1087163953Srrs						actual += sizeof(*sin);
1088163953Srrs					}
1089163953Srrs					if (actual >= limit) {
1090163953Srrs						return (actual);
1091163953Srrs					}
1092167598Srrs				} else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1093163953Srrs				    (ipv6_addr_legal)) {
1094163953Srrs					struct sockaddr_in6 *sin6;
1095163953Srrs
1096167598Srrs					sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1097163953Srrs					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1098163953Srrs						/*
1099163953Srrs						 * we skip unspecifed
1100163953Srrs						 * addresses
1101163953Srrs						 */
1102163953Srrs						continue;
1103163953Srrs					}
1104163953Srrs					if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1105163953Srrs						if (local_scope == 0)
1106163953Srrs							continue;
1107163953Srrs						if (sin6->sin6_scope_id == 0) {
1108163953Srrs							if (sa6_recoverscope(sin6) != 0)
1109163953Srrs								/*
1110163953Srrs								 * bad link
1111163953Srrs								 * local
1112163953Srrs								 * address
1113163953Srrs								 */
1114163953Srrs								continue;
1115163953Srrs						}
1116163953Srrs					}
1117163953Srrs					if ((site_scope == 0) &&
1118163953Srrs					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1119163953Srrs						continue;
1120163953Srrs					}
1121163953Srrs					memcpy(sas, sin6, sizeof(*sin6));
1122163953Srrs					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1123163953Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1124163953Srrs					actual += sizeof(*sin6);
1125163953Srrs					if (actual >= limit) {
1126163953Srrs						return (actual);
1127163953Srrs					}
1128163953Srrs				}
1129163953Srrs			}
1130163953Srrs		}
1131163953Srrs	} else {
1132163953Srrs		struct sctp_laddr *laddr;
1133163953Srrs
1134167598Srrs		/* The list is a NEGATIVE list */
1135167598Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1136167598Srrs			if (stcb) {
1137167598Srrs				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1138163953Srrs					continue;
1139163953Srrs				}
1140163953Srrs			}
1141167598Srrs			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1142167598Srrs				continue;
1143167598Srrs
1144167598Srrs			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1145167598Srrs			sas = (struct sockaddr_storage *)((caddr_t)sas +
1146167598Srrs			    laddr->ifa->address.sa.sa_len);
1147167598Srrs			actual += laddr->ifa->address.sa.sa_len;
1148167598Srrs			if (actual >= limit) {
1149167598Srrs				return (actual);
1150163953Srrs			}
1151163953Srrs		}
1152163953Srrs	}
1153163953Srrs	return (actual);
1154163953Srrs}
1155163953Srrs
1156168124Srrsstatic size_t
1157168124Srrssctp_fill_up_addresses(struct sctp_inpcb *inp,
1158168124Srrs    struct sctp_tcb *stcb,
1159168124Srrs    size_t limit,
1160168124Srrs    struct sockaddr_storage *sas)
1161168124Srrs{
1162168124Srrs	size_t size = 0;
1163168124Srrs
1164168124Srrs	/* fill up addresses for the endpoint's default vrf */
1165168124Srrs	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1166168124Srrs	    inp->def_vrf_id);
1167168124Srrs	return (size);
1168168124Srrs}
1169168124Srrs
1170163953Srrsstatic int
1171168124Srrssctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1172163953Srrs{
1173163953Srrs	int cnt = 0;
1174167598Srrs	struct sctp_vrf *vrf = NULL;
1175163953Srrs
1176163953Srrs	/*
1177163953Srrs	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1178163953Srrs	 * number of addresses that you COULD get. In reality the sub-set
1179163953Srrs	 * bound may have an exclusion list for a given TCB OR in the
1180163953Srrs	 * bound-all case a TCB may NOT include the loopback or other
1181163953Srrs	 * addresses as well.
1182163953Srrs	 */
1183167598Srrs	vrf = sctp_find_vrf(vrf_id);
1184167598Srrs	if (vrf == NULL) {
1185167598Srrs		return (0);
1186167598Srrs	}
1187163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1188167598Srrs		struct sctp_ifn *sctp_ifn;
1189167598Srrs		struct sctp_ifa *sctp_ifa;
1190163953Srrs
1191167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1192167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1193163953Srrs				/* Count them if they are the right type */
1194167598Srrs				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1195163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1196163953Srrs						cnt += sizeof(struct sockaddr_in6);
1197163953Srrs					else
1198163953Srrs						cnt += sizeof(struct sockaddr_in);
1199163953Srrs
1200167598Srrs				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1201163953Srrs					cnt += sizeof(struct sockaddr_in6);
1202163953Srrs			}
1203163953Srrs		}
1204163953Srrs	} else {
1205163953Srrs		struct sctp_laddr *laddr;
1206163953Srrs
1207163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1208167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1209163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1210163953Srrs					cnt += sizeof(struct sockaddr_in6);
1211163953Srrs				else
1212163953Srrs					cnt += sizeof(struct sockaddr_in);
1213163953Srrs
1214167598Srrs			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1215163953Srrs				cnt += sizeof(struct sockaddr_in6);
1216163953Srrs		}
1217163953Srrs	}
1218163953Srrs	return (cnt);
1219163953Srrs}
1220163953Srrs
1221168124Srrsstatic int
1222168124Srrssctp_count_max_addresses(struct sctp_inpcb *inp)
1223168124Srrs{
1224168124Srrs	int cnt = 0;
1225166675Srrs
1226168124Srrs	/* count addresses for the endpoint's default VRF */
1227168124Srrs	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1228168124Srrs	return (cnt);
1229168124Srrs}
1230168124Srrs
1231168124Srrs
1232163953Srrsstatic int
1233166675Srrssctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1234166675Srrs    size_t optsize, void *p, int delay)
1235163953Srrs{
1236163953Srrs	int error = 0;
1237163953Srrs	int creat_lock_on = 0;
1238163953Srrs	struct sctp_tcb *stcb = NULL;
1239163953Srrs	struct sockaddr *sa;
1240166675Srrs	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr, i;
1241166675Srrs	size_t incr, at;
1242167598Srrs	uint32_t vrf_id;
1243167598Srrs	sctp_assoc_t *a_id;
1244163953Srrs
1245163953Srrs#ifdef SCTP_DEBUG
1246163953Srrs	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1247163953Srrs		printf("Connectx called\n");
1248163953Srrs	}
1249163953Srrs#endif				/* SCTP_DEBUG */
1250163953Srrs
1251163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1252163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1253163953Srrs		/* We are already connected AND the TCP model */
1254163953Srrs		return (EADDRINUSE);
1255163953Srrs	}
1256163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1257163953Srrs		return (EINVAL);
1258163953Srrs	}
1259163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1260163953Srrs		SCTP_INP_RLOCK(inp);
1261163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1262163953Srrs		SCTP_INP_RUNLOCK(inp);
1263163953Srrs	}
1264163953Srrs	if (stcb) {
1265163953Srrs		return (EALREADY);
1266163953Srrs	}
1267163953Srrs	SCTP_INP_INCR_REF(inp);
1268163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
1269163953Srrs	creat_lock_on = 1;
1270163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1271163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1272163953Srrs		error = EFAULT;
1273163953Srrs		goto out_now;
1274163953Srrs	}
1275166675Srrs	totaddrp = (int *)optval;
1276163953Srrs	totaddr = *totaddrp;
1277163953Srrs	sa = (struct sockaddr *)(totaddrp + 1);
1278163953Srrs	at = incr = 0;
1279163953Srrs	/* account and validate addresses */
1280163953Srrs	for (i = 0; i < totaddr; i++) {
1281163953Srrs		if (sa->sa_family == AF_INET) {
1282163953Srrs			num_v4++;
1283163953Srrs			incr = sizeof(struct sockaddr_in);
1284163953Srrs		} else if (sa->sa_family == AF_INET6) {
1285163953Srrs			struct sockaddr_in6 *sin6;
1286163953Srrs
1287163953Srrs			sin6 = (struct sockaddr_in6 *)sa;
1288163953Srrs			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1289163953Srrs				/* Must be non-mapped for connectx */
1290163953Srrs				error = EINVAL;
1291163953Srrs				goto out_now;
1292163953Srrs			}
1293163953Srrs			num_v6++;
1294163953Srrs			incr = sizeof(struct sockaddr_in6);
1295163953Srrs		} else {
1296163953Srrs			totaddr = i;
1297163953Srrs			break;
1298163953Srrs		}
1299163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1300163953Srrs		if (stcb != NULL) {
1301163953Srrs			/* Already have or am bring up an association */
1302163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1303163953Srrs			creat_lock_on = 0;
1304163953Srrs			SCTP_TCB_UNLOCK(stcb);
1305163953Srrs			error = EALREADY;
1306163953Srrs			goto out_now;
1307163953Srrs		}
1308166675Srrs		if ((at + incr) > optsize) {
1309163953Srrs			totaddr = i;
1310163953Srrs			break;
1311163953Srrs		}
1312163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + incr);
1313163953Srrs	}
1314163953Srrs	sa = (struct sockaddr *)(totaddrp + 1);
1315163953Srrs#ifdef INET6
1316163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1317163953Srrs	    (num_v6 > 0)) {
1318163953Srrs		error = EINVAL;
1319163953Srrs		goto out_now;
1320163953Srrs	}
1321163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1322163953Srrs	    (num_v4 > 0)) {
1323163953Srrs		struct in6pcb *inp6;
1324163953Srrs
1325163953Srrs		inp6 = (struct in6pcb *)inp;
1326166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1327163953Srrs			/*
1328163953Srrs			 * if IPV6_V6ONLY flag, ignore connections destined
1329163953Srrs			 * to a v4 addr or v4-mapped addr
1330163953Srrs			 */
1331163953Srrs			error = EINVAL;
1332163953Srrs			goto out_now;
1333163953Srrs		}
1334163953Srrs	}
1335163953Srrs#endif				/* INET6 */
1336163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1337163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
1338163953Srrs		/* Bind a ephemeral port */
1339163953Srrs		SCTP_INP_WUNLOCK(inp);
1340163953Srrs		error = sctp_inpcb_bind(so, NULL, p);
1341163953Srrs		if (error) {
1342163953Srrs			goto out_now;
1343163953Srrs		}
1344163953Srrs	} else {
1345163953Srrs		SCTP_INP_WUNLOCK(inp);
1346163953Srrs	}
1347163953Srrs
1348167695Srrs	/* FIX ME: do we want to pass in a vrf on the connect call? */
1349167695Srrs	vrf_id = inp->def_vrf_id;
1350167695Srrs
1351163953Srrs	/* We are GOOD to go */
1352167598Srrs	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1353163953Srrs	if (stcb == NULL) {
1354163953Srrs		/* Gak! no memory */
1355163953Srrs		goto out_now;
1356163953Srrs	}
1357163953Srrs	/* move to second address */
1358163953Srrs	if (sa->sa_family == AF_INET)
1359163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1360163953Srrs	else
1361163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1362163953Srrs
1363163953Srrs	for (i = 1; i < totaddr; i++) {
1364163953Srrs		if (sa->sa_family == AF_INET) {
1365163953Srrs			incr = sizeof(struct sockaddr_in);
1366165220Srrs			if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
1367163953Srrs				/* assoc gone no un-lock */
1368165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1369163953Srrs				error = ENOBUFS;
1370163953Srrs				goto out_now;
1371163953Srrs			}
1372163953Srrs		} else if (sa->sa_family == AF_INET6) {
1373163953Srrs			incr = sizeof(struct sockaddr_in6);
1374165220Srrs			if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
1375163953Srrs				/* assoc gone no un-lock */
1376165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
1377163953Srrs				error = ENOBUFS;
1378163953Srrs				goto out_now;
1379163953Srrs			}
1380163953Srrs		}
1381163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + incr);
1382163953Srrs	}
1383163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1384167598Srrs	/* Fill in the return id */
1385167598Srrs	a_id = (sctp_assoc_t *) optval;
1386167598Srrs	*a_id = sctp_get_associd(stcb);
1387163953Srrs
1388163953Srrs	/* initialize authentication parameters for the assoc */
1389163953Srrs	sctp_initialize_auth_params(inp, stcb);
1390163953Srrs
1391163953Srrs	if (delay) {
1392163953Srrs		/* doing delayed connection */
1393163953Srrs		stcb->asoc.delayed_connection = 1;
1394163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1395163953Srrs	} else {
1396163953Srrs		SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1397163953Srrs		sctp_send_initiate(inp, stcb);
1398163953Srrs	}
1399163953Srrs	SCTP_TCB_UNLOCK(stcb);
1400163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1401163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1402163953Srrs		/* Set the connected flag so we can queue data */
1403163953Srrs		soisconnecting(so);
1404163953Srrs	}
1405163953Srrsout_now:
1406163953Srrs	if (creat_lock_on)
1407163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1408163953Srrs	SCTP_INP_DECR_REF(inp);
1409163953Srrs	return error;
1410163953Srrs}
1411163953Srrs
1412166675Srrs#define SCTP_FIND_STCB(inp, stcb, assoc_id) \
1413166675Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { \
1414166675Srrs		SCTP_INP_RLOCK(inp); \
1415166675Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1416166675Srrs		if (stcb) \
1417166675Srrs			SCTP_TCB_LOCK(stcb); \
1418166675Srrs		SCTP_INP_RUNLOCK(inp); \
1419166675Srrs	} else if (assoc_id != 0) { \
1420166675Srrs		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1421166675Srrs		if (stcb == NULL) { \
1422166675Srrs			error = ENOENT; \
1423166675Srrs			break; \
1424166675Srrs		} \
1425166675Srrs	} else { \
1426166675Srrs		stcb = NULL; \
1427166675Srrs	}
1428163953Srrs
1429166675Srrs#define SCTP_CHECK_AND_CAST(destp, srcp, type, size) \
1430166675Srrs	if (size < sizeof(type)) { \
1431166675Srrs		error = EINVAL; \
1432166675Srrs		break; \
1433166675Srrs	} else { \
1434166675Srrs		destp = (type *)srcp; \
1435166675Srrs	}
1436163953Srrs
1437163953Srrsstatic int
1438166675Srrssctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1439166675Srrs    void *p)
1440163953Srrs{
1441163953Srrs	struct sctp_inpcb *inp;
1442166675Srrs	int error, val = 0;
1443163953Srrs	struct sctp_tcb *stcb = NULL;
1444163953Srrs
1445166675Srrs	if (optval == NULL) {
1446166675Srrs		return (EINVAL);
1447166675Srrs	}
1448163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1449163953Srrs	if (inp == 0)
1450163953Srrs		return EINVAL;
1451163953Srrs	error = 0;
1452163953Srrs
1453166675Srrs	switch (optname) {
1454163953Srrs	case SCTP_NODELAY:
1455163953Srrs	case SCTP_AUTOCLOSE:
1456163953Srrs	case SCTP_EXPLICIT_EOR:
1457163953Srrs	case SCTP_AUTO_ASCONF:
1458163953Srrs	case SCTP_DISABLE_FRAGMENTS:
1459163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
1460163953Srrs	case SCTP_USE_EXT_RCVINFO:
1461163953Srrs		SCTP_INP_RLOCK(inp);
1462166675Srrs		switch (optname) {
1463163953Srrs		case SCTP_DISABLE_FRAGMENTS:
1464166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1465163953Srrs			break;
1466163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
1467166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1468163953Srrs			break;
1469163953Srrs		case SCTP_AUTO_ASCONF:
1470166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1471163953Srrs			break;
1472163953Srrs		case SCTP_EXPLICIT_EOR:
1473166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1474163953Srrs			break;
1475163953Srrs		case SCTP_NODELAY:
1476166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1477163953Srrs			break;
1478163953Srrs		case SCTP_USE_EXT_RCVINFO:
1479166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1480163953Srrs			break;
1481163953Srrs		case SCTP_AUTOCLOSE:
1482163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1483166675Srrs				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1484163953Srrs			else
1485166675Srrs				val = 0;
1486163953Srrs			break;
1487163953Srrs
1488163953Srrs		default:
1489163953Srrs			error = ENOPROTOOPT;
1490163953Srrs		}		/* end switch (sopt->sopt_name) */
1491166675Srrs		if (optname != SCTP_AUTOCLOSE) {
1492163953Srrs			/* make it an "on/off" value */
1493166675Srrs			val = (val != 0);
1494163953Srrs		}
1495166675Srrs		if (*optsize < sizeof(val)) {
1496163953Srrs			error = EINVAL;
1497163953Srrs		}
1498163953Srrs		SCTP_INP_RUNLOCK(inp);
1499163953Srrs		if (error == 0) {
1500163953Srrs			/* return the option value */
1501166675Srrs			*(int *)optval = val;
1502166675Srrs			*optsize = sizeof(val);
1503163953Srrs		}
1504163953Srrs		break;
1505167598Srrs
1506163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
1507163953Srrs		{
1508166675Srrs			uint32_t *value;
1509166675Srrs
1510166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1511166675Srrs			*value = inp->partial_delivery_point;
1512166675Srrs			*optsize = sizeof(uint32_t);
1513163953Srrs		}
1514163953Srrs		break;
1515163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
1516163953Srrs		{
1517166675Srrs			uint32_t *value;
1518166675Srrs
1519166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1520166675Srrs			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
1521166675Srrs			*optsize = sizeof(uint32_t);
1522163953Srrs		}
1523163953Srrs		break;
1524163953Srrs	case SCTP_CMT_ON_OFF:
1525163953Srrs		{
1526166675Srrs			struct sctp_assoc_value *av;
1527166675Srrs
1528166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1529166675Srrs			if (sctp_cmt_on_off) {
1530166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1531166675Srrs				if (stcb) {
1532166675Srrs					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1533166675Srrs					SCTP_TCB_UNLOCK(stcb);
1534166675Srrs
1535166675Srrs				} else {
1536166675Srrs					error = ENOTCONN;
1537166675Srrs				}
1538166675Srrs			} else {
1539166675Srrs				error = ENOPROTOOPT;
1540163953Srrs			}
1541166675Srrs			*optsize = sizeof(*av);
1542163953Srrs		}
1543163953Srrs		break;
1544163953Srrs	case SCTP_GET_ADDR_LEN:
1545163953Srrs		{
1546163953Srrs			struct sctp_assoc_value *av;
1547163953Srrs
1548166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1549163953Srrs			error = EINVAL;
1550167598Srrs#ifdef INET
1551163953Srrs			if (av->assoc_value == AF_INET) {
1552163953Srrs				av->assoc_value = sizeof(struct sockaddr_in);
1553163953Srrs				error = 0;
1554163953Srrs			}
1555163953Srrs#endif
1556167598Srrs#ifdef INET6
1557163953Srrs			if (av->assoc_value == AF_INET6) {
1558163953Srrs				av->assoc_value = sizeof(struct sockaddr_in6);
1559163953Srrs				error = 0;
1560163953Srrs			}
1561163953Srrs#endif
1562166675Srrs			*optsize = sizeof(*av);
1563163953Srrs		}
1564163953Srrs		break;
1565163953Srrs	case SCTP_GET_ASOC_ID_LIST:
1566163953Srrs		{
1567163953Srrs			struct sctp_assoc_ids *ids;
1568163953Srrs			int cnt, at;
1569163953Srrs			uint16_t orig;
1570163953Srrs
1571166675Srrs			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1572163953Srrs			cnt = 0;
1573163953Srrs			SCTP_INP_RLOCK(inp);
1574163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1575163953Srrs			if (stcb == NULL) {
1576163953Srrs		none_out_now:
1577163953Srrs				ids->asls_numb_present = 0;
1578163953Srrs				ids->asls_more_to_get = 0;
1579163953Srrs				SCTP_INP_RUNLOCK(inp);
1580163953Srrs				break;
1581163953Srrs			}
1582163953Srrs			orig = ids->asls_assoc_start;
1583163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1584163953Srrs			while (orig) {
1585163953Srrs				stcb = LIST_NEXT(stcb, sctp_tcblist);
1586163953Srrs				orig--;
1587163953Srrs				cnt--;
1588163953Srrs				if (stcb == NULL)
1589163953Srrs					goto none_out_now;
1590163953Srrs			}
1591163953Srrs			if (stcb == NULL)
1592163953Srrs				goto none_out_now;
1593163953Srrs
1594163953Srrs			at = 0;
1595163953Srrs			ids->asls_numb_present = 0;
1596163953Srrs			ids->asls_more_to_get = 1;
1597163953Srrs			while (at < MAX_ASOC_IDS_RET) {
1598163953Srrs				ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1599163953Srrs				at++;
1600163953Srrs				ids->asls_numb_present++;
1601163953Srrs				stcb = LIST_NEXT(stcb, sctp_tcblist);
1602163953Srrs				if (stcb == NULL) {
1603163953Srrs					ids->asls_more_to_get = 0;
1604163953Srrs					break;
1605163953Srrs				}
1606163953Srrs			}
1607163953Srrs			SCTP_INP_RUNLOCK(inp);
1608163953Srrs		}
1609163953Srrs		break;
1610163953Srrs	case SCTP_CONTEXT:
1611163953Srrs		{
1612163953Srrs			struct sctp_assoc_value *av;
1613163953Srrs
1614166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1615166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1616166675Srrs
1617166675Srrs			if (stcb) {
1618166675Srrs				av->assoc_value = stcb->asoc.context;
1619166675Srrs				SCTP_TCB_UNLOCK(stcb);
1620163953Srrs			} else {
1621166675Srrs				SCTP_INP_RLOCK(inp);
1622163953Srrs				av->assoc_value = inp->sctp_context;
1623166675Srrs				SCTP_INP_RUNLOCK(inp);
1624163953Srrs			}
1625166675Srrs			*optsize = sizeof(*av);
1626163953Srrs		}
1627163953Srrs		break;
1628167598Srrs	case SCTP_VRF_ID:
1629167598Srrs		{
1630167598Srrs			uint32_t *vrf_id;
1631167598Srrs
1632167598Srrs			SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, *optsize);
1633167598Srrs			*vrf_id = inp->def_vrf_id;
1634167598Srrs			break;
1635167598Srrs		}
1636167598Srrs	case SCTP_GET_ASOC_VRF:
1637167598Srrs		{
1638167598Srrs			struct sctp_assoc_value *id;
1639167598Srrs
1640167598Srrs			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1641167598Srrs			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1642167598Srrs			if (stcb == NULL) {
1643167598Srrs				error = EINVAL;
1644167598Srrs				break;
1645167598Srrs			}
1646167598Srrs			id->assoc_value = stcb->asoc.vrf_id;
1647167598Srrs			break;
1648167598Srrs		}
1649167598Srrs	case SCTP_GET_VRF_IDS:
1650167598Srrs		{
1651167598Srrs			error = EOPNOTSUPP;
1652167598Srrs			break;
1653167598Srrs		}
1654163953Srrs	case SCTP_GET_NONCE_VALUES:
1655163953Srrs		{
1656163953Srrs			struct sctp_get_nonce_values *gnv;
1657163953Srrs
1658166675Srrs			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1659166675Srrs			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1660166675Srrs
1661166675Srrs			if (stcb) {
1662163953Srrs				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1663163953Srrs				gnv->gn_local_tag = stcb->asoc.my_vtag;
1664163953Srrs				SCTP_TCB_UNLOCK(stcb);
1665166675Srrs			} else {
1666166675Srrs				error = ENOTCONN;
1667163953Srrs			}
1668166675Srrs			*optsize = sizeof(*gnv);
1669163953Srrs		}
1670163953Srrs		break;
1671163953Srrs	case SCTP_DELAYED_ACK_TIME:
1672163953Srrs		{
1673163953Srrs			struct sctp_assoc_value *tm;
1674163953Srrs
1675166675Srrs			SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, *optsize);
1676166675Srrs			SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
1677163953Srrs
1678166675Srrs			if (stcb) {
1679166675Srrs				tm->assoc_value = stcb->asoc.delayed_ack;
1680166675Srrs				SCTP_TCB_UNLOCK(stcb);
1681166675Srrs			} else {
1682163953Srrs				SCTP_INP_RLOCK(inp);
1683166675Srrs				tm->assoc_value = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1684163953Srrs				SCTP_INP_RUNLOCK(inp);
1685163953Srrs			}
1686166675Srrs			*optsize = sizeof(*tm);
1687163953Srrs		}
1688163953Srrs		break;
1689163953Srrs
1690163953Srrs	case SCTP_GET_SNDBUF_USE:
1691166675Srrs		{
1692163953Srrs			struct sctp_sockstat *ss;
1693163953Srrs
1694166675Srrs			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1695166675Srrs			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1696166675Srrs
1697166675Srrs			if (stcb) {
1698166675Srrs				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1699166675Srrs				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1700166675Srrs				    stcb->asoc.size_on_all_streams);
1701166675Srrs				SCTP_TCB_UNLOCK(stcb);
1702166675Srrs			} else {
1703163953Srrs				error = ENOTCONN;
1704163953Srrs			}
1705166675Srrs			*optsize = sizeof(struct sctp_sockstat);
1706163953Srrs		}
1707163953Srrs		break;
1708163953Srrs	case SCTP_MAXBURST:
1709163953Srrs		{
1710166675Srrs			uint8_t *value;
1711163953Srrs
1712166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1713166675Srrs
1714163953Srrs			SCTP_INP_RLOCK(inp);
1715166675Srrs			*value = inp->sctp_ep.max_burst;
1716163953Srrs			SCTP_INP_RUNLOCK(inp);
1717166675Srrs			*optsize = sizeof(uint8_t);
1718163953Srrs		}
1719163953Srrs		break;
1720163953Srrs	case SCTP_MAXSEG:
1721163953Srrs		{
1722167598Srrs			struct sctp_assoc_value *av;
1723163953Srrs			int ovh;
1724163953Srrs
1725167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1726167598Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1727163953Srrs
1728167598Srrs			if (stcb) {
1729167598Srrs				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1730167598Srrs				SCTP_TCB_UNLOCK(stcb);
1731163953Srrs			} else {
1732167598Srrs				SCTP_INP_RLOCK(inp);
1733167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1734167598Srrs					ovh = SCTP_MED_OVERHEAD;
1735167598Srrs				} else {
1736167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
1737167598Srrs				}
1738167598Srrs				av->assoc_value = inp->sctp_frag_point - ovh;
1739167598Srrs				SCTP_INP_RUNLOCK(inp);
1740163953Srrs			}
1741167598Srrs			*optsize = sizeof(struct sctp_assoc_value);
1742163953Srrs		}
1743163953Srrs		break;
1744163953Srrs	case SCTP_GET_STAT_LOG:
1745163953Srrs#ifdef SCTP_STAT_LOGGING
1746167598Srrs		error = sctp_fill_stat_log(optval, optsize);
1747167598Srrs#else
1748163953Srrs		error = EOPNOTSUPP;
1749163953Srrs#endif
1750163953Srrs		break;
1751163953Srrs	case SCTP_EVENTS:
1752163953Srrs		{
1753163953Srrs			struct sctp_event_subscribe *events;
1754163953Srrs
1755166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1756163953Srrs			memset(events, 0, sizeof(*events));
1757163953Srrs			SCTP_INP_RLOCK(inp);
1758163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1759163953Srrs				events->sctp_data_io_event = 1;
1760163953Srrs
1761163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1762163953Srrs				events->sctp_association_event = 1;
1763163953Srrs
1764163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1765163953Srrs				events->sctp_address_event = 1;
1766163953Srrs
1767163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1768163953Srrs				events->sctp_send_failure_event = 1;
1769163953Srrs
1770163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1771163953Srrs				events->sctp_peer_error_event = 1;
1772163953Srrs
1773163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1774163953Srrs				events->sctp_shutdown_event = 1;
1775163953Srrs
1776163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1777163953Srrs				events->sctp_partial_delivery_event = 1;
1778163953Srrs
1779163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1780163953Srrs				events->sctp_adaptation_layer_event = 1;
1781163953Srrs
1782163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1783163953Srrs				events->sctp_authentication_event = 1;
1784163953Srrs
1785163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1786163953Srrs				events->sctp_stream_reset_events = 1;
1787163953Srrs			SCTP_INP_RUNLOCK(inp);
1788166675Srrs			*optsize = sizeof(struct sctp_event_subscribe);
1789163953Srrs		}
1790163953Srrs		break;
1791163953Srrs
1792163953Srrs	case SCTP_ADAPTATION_LAYER:
1793166675Srrs		{
1794166675Srrs			uint32_t *value;
1795166675Srrs
1796166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1797166675Srrs
1798166675Srrs			SCTP_INP_RLOCK(inp);
1799166675Srrs			*value = inp->sctp_ep.adaptation_layer_indicator;
1800166675Srrs			SCTP_INP_RUNLOCK(inp);
1801166675Srrs			*optsize = sizeof(uint32_t);
1802163953Srrs		}
1803163953Srrs		break;
1804163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
1805166675Srrs		{
1806166675Srrs			uint32_t *value;
1807166675Srrs
1808166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1809166675Srrs			SCTP_INP_RLOCK(inp);
1810166675Srrs			*value = inp->sctp_ep.initial_sequence_debug;
1811166675Srrs			SCTP_INP_RUNLOCK(inp);
1812166675Srrs			*optsize = sizeof(uint32_t);
1813163953Srrs		}
1814163953Srrs		break;
1815163953Srrs	case SCTP_GET_LOCAL_ADDR_SIZE:
1816166675Srrs		{
1817166675Srrs			uint32_t *value;
1818166675Srrs
1819166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1820166675Srrs			SCTP_INP_RLOCK(inp);
1821168124Srrs			*value = sctp_count_max_addresses(inp);
1822166675Srrs			SCTP_INP_RUNLOCK(inp);
1823166675Srrs			*optsize = sizeof(uint32_t);
1824163953Srrs		}
1825163953Srrs		break;
1826163953Srrs	case SCTP_GET_REMOTE_ADDR_SIZE:
1827163953Srrs		{
1828166675Srrs			uint32_t *value;
1829166675Srrs			size_t size;
1830163953Srrs			struct sctp_nets *net;
1831163953Srrs
1832166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1833166675Srrs			/* FIXME MT: change to sctp_assoc_value? */
1834166675Srrs			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1835166675Srrs
1836166675Srrs			if (stcb) {
1837166675Srrs				size = 0;
1838166675Srrs				/* Count the sizes */
1839166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1840166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1841166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1842166675Srrs						size += sizeof(struct sockaddr_in6);
1843166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1844166675Srrs						size += sizeof(struct sockaddr_in);
1845166675Srrs					} else {
1846166675Srrs						/* huh */
1847166675Srrs						break;
1848166675Srrs					}
1849163953Srrs				}
1850166675Srrs				SCTP_TCB_UNLOCK(stcb);
1851166675Srrs				*value = (uint32_t) size;
1852166675Srrs			} else {
1853166675Srrs				error = ENOTCONN;
1854163953Srrs			}
1855166675Srrs			*optsize = sizeof(uint32_t);
1856163953Srrs		}
1857163953Srrs		break;
1858163953Srrs	case SCTP_GET_PEER_ADDRESSES:
1859163953Srrs		/*
1860163953Srrs		 * Get the address information, an array is passed in to
1861163953Srrs		 * fill up we pack it.
1862163953Srrs		 */
1863163953Srrs		{
1864166675Srrs			size_t cpsz, left;
1865163953Srrs			struct sockaddr_storage *sas;
1866163953Srrs			struct sctp_nets *net;
1867163953Srrs			struct sctp_getaddresses *saddr;
1868163953Srrs
1869166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1870166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1871163953Srrs
1872166675Srrs			if (stcb) {
1873166675Srrs				left = (*optsize) - sizeof(struct sctp_getaddresses);
1874166675Srrs				*optsize = sizeof(struct sctp_getaddresses);
1875166675Srrs				sas = (struct sockaddr_storage *)&saddr->addr[0];
1876166675Srrs
1877166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1878166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1879166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1880166675Srrs						cpsz = sizeof(struct sockaddr_in6);
1881166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1882166675Srrs						cpsz = sizeof(struct sockaddr_in);
1883166675Srrs					} else {
1884166675Srrs						/* huh */
1885166675Srrs						break;
1886166675Srrs					}
1887166675Srrs					if (left < cpsz) {
1888166675Srrs						/* not enough room. */
1889166675Srrs						break;
1890166675Srrs					}
1891166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1892166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1893166675Srrs						/* Must map the address */
1894166675Srrs						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1895166675Srrs						    (struct sockaddr_in6 *)sas);
1896166675Srrs					} else {
1897166675Srrs						memcpy(sas, &net->ro._l_addr, cpsz);
1898166675Srrs					}
1899166675Srrs					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1900166675Srrs
1901166675Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1902166675Srrs					left -= cpsz;
1903166675Srrs					*optsize += cpsz;
1904163953Srrs				}
1905166675Srrs				SCTP_TCB_UNLOCK(stcb);
1906166675Srrs			} else {
1907166675Srrs				error = ENOENT;
1908163953Srrs			}
1909163953Srrs		}
1910163953Srrs		break;
1911163953Srrs	case SCTP_GET_LOCAL_ADDRESSES:
1912163953Srrs		{
1913166675Srrs			size_t limit, actual;
1914163953Srrs			struct sockaddr_storage *sas;
1915163953Srrs			struct sctp_getaddresses *saddr;
1916163953Srrs
1917166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1918166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1919163953Srrs
1920163953Srrs			sas = (struct sockaddr_storage *)&saddr->addr[0];
1921166675Srrs			limit = *optsize - sizeof(sctp_assoc_t);
1922168124Srrs			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1923163953Srrs			if (stcb)
1924163953Srrs				SCTP_TCB_UNLOCK(stcb);
1925166675Srrs			*optsize = sizeof(struct sockaddr_storage) + actual;
1926163953Srrs		}
1927163953Srrs		break;
1928163953Srrs	case SCTP_PEER_ADDR_PARAMS:
1929163953Srrs		{
1930163953Srrs			struct sctp_paddrparams *paddrp;
1931163953Srrs			struct sctp_nets *net;
1932163953Srrs
1933166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1934166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1935163953Srrs
1936163953Srrs			net = NULL;
1937166675Srrs			if (stcb) {
1938166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1939166675Srrs			} else {
1940166675Srrs				/*
1941166675Srrs				 * We increment here since
1942166675Srrs				 * sctp_findassociation_ep_addr() wil do a
1943166675Srrs				 * decrement if it finds the stcb as long as
1944166675Srrs				 * the locked tcb (last argument) is NOT a
1945166675Srrs				 * TCB.. aka NULL.
1946166675Srrs				 */
1947166675Srrs				SCTP_INP_INCR_REF(inp);
1948166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1949163953Srrs				if (stcb == NULL) {
1950166675Srrs					SCTP_INP_DECR_REF(inp);
1951163953Srrs				}
1952163953Srrs			}
1953163953Srrs
1954163953Srrs			if (stcb) {
1955163953Srrs				/* Applys to the specific association */
1956163953Srrs				paddrp->spp_flags = 0;
1957163953Srrs				if (net) {
1958163953Srrs					paddrp->spp_pathmaxrxt = net->failure_threshold;
1959163953Srrs					paddrp->spp_pathmtu = net->mtu;
1960163953Srrs					/* get flags for HB */
1961163953Srrs					if (net->dest_state & SCTP_ADDR_NOHB)
1962163953Srrs						paddrp->spp_flags |= SPP_HB_DISABLE;
1963163953Srrs					else
1964163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
1965163953Srrs					/* get flags for PMTU */
1966165647Srrs					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
1967163953Srrs						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
1968163953Srrs					} else {
1969163953Srrs						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
1970163953Srrs					}
1971167598Srrs#ifdef INET
1972163953Srrs					if (net->ro._l_addr.sin.sin_family == AF_INET) {
1973163953Srrs						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
1974163953Srrs						paddrp->spp_flags |= SPP_IPV4_TOS;
1975163953Srrs					}
1976163953Srrs#endif
1977167598Srrs#ifdef INET6
1978163953Srrs					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
1979163953Srrs						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
1980163953Srrs						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1981163953Srrs					}
1982163953Srrs#endif
1983163953Srrs				} else {
1984163953Srrs					/*
1985163953Srrs					 * No destination so return default
1986163953Srrs					 * value
1987163953Srrs					 */
1988163953Srrs					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
1989163953Srrs					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
1990167598Srrs#ifdef INET
1991163953Srrs					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
1992163953Srrs					paddrp->spp_flags |= SPP_IPV4_TOS;
1993163953Srrs#endif
1994167598Srrs#ifdef INET6
1995163953Srrs					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
1996163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1997163953Srrs#endif
1998163953Srrs					/* default settings should be these */
1999163953Srrs					if (sctp_is_hb_timer_running(stcb)) {
2000163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
2001163953Srrs					}
2002163953Srrs				}
2003163953Srrs				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2004163953Srrs				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2005163953Srrs				SCTP_TCB_UNLOCK(stcb);
2006163953Srrs			} else {
2007163953Srrs				/* Use endpoint defaults */
2008163953Srrs				SCTP_INP_RLOCK(inp);
2009163953Srrs				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2010163953Srrs				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2011163953Srrs				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2012163953Srrs				/* get inp's default */
2013167598Srrs#ifdef INET
2014163953Srrs				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2015163953Srrs				paddrp->spp_flags |= SPP_IPV4_TOS;
2016163953Srrs#endif
2017167598Srrs#ifdef INET6
2018163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2019163953Srrs					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2020163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2021163953Srrs				}
2022163953Srrs#endif
2023163953Srrs				/* can't return this */
2024163953Srrs				paddrp->spp_pathmaxrxt = 0;
2025163953Srrs				paddrp->spp_pathmtu = 0;
2026163953Srrs				/* default behavior, no stcb */
2027167598Srrs				paddrp->spp_flags = SPP_HB_ENABLE | SPP_PMTUD_ENABLE;
2028163953Srrs
2029163953Srrs				SCTP_INP_RUNLOCK(inp);
2030163953Srrs			}
2031166675Srrs			*optsize = sizeof(struct sctp_paddrparams);
2032163953Srrs		}
2033163953Srrs		break;
2034163953Srrs	case SCTP_GET_PEER_ADDR_INFO:
2035163953Srrs		{
2036163953Srrs			struct sctp_paddrinfo *paddri;
2037163953Srrs			struct sctp_nets *net;
2038163953Srrs
2039166675Srrs			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2040166675Srrs			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2041166675Srrs
2042163953Srrs			net = NULL;
2043166675Srrs			if (stcb) {
2044166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2045166675Srrs			} else {
2046166675Srrs				/*
2047166675Srrs				 * We increment here since
2048166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2049166675Srrs				 * decrement if it finds the stcb as long as
2050166675Srrs				 * the locked tcb (last argument) is NOT a
2051166675Srrs				 * TCB.. aka NULL.
2052166675Srrs				 */
2053166675Srrs				SCTP_INP_INCR_REF(inp);
2054166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2055166675Srrs				if (stcb == NULL) {
2056166675Srrs					SCTP_INP_DECR_REF(inp);
2057163953Srrs				}
2058166675Srrs			}
2059163953Srrs
2060166675Srrs			if ((stcb) && (net)) {
2061166675Srrs				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2062166675Srrs				paddri->spinfo_cwnd = net->cwnd;
2063166675Srrs				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2064166675Srrs				paddri->spinfo_rto = net->RTO;
2065166675Srrs				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2066166675Srrs				SCTP_TCB_UNLOCK(stcb);
2067163953Srrs			} else {
2068163953Srrs				if (stcb) {
2069163953Srrs					SCTP_TCB_UNLOCK(stcb);
2070163953Srrs				}
2071163953Srrs				error = ENOENT;
2072163953Srrs			}
2073166675Srrs			*optsize = sizeof(struct sctp_paddrinfo);
2074163953Srrs		}
2075163953Srrs		break;
2076163953Srrs	case SCTP_PCB_STATUS:
2077163953Srrs		{
2078163953Srrs			struct sctp_pcbinfo *spcb;
2079163953Srrs
2080166675Srrs			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2081163953Srrs			sctp_fill_pcbinfo(spcb);
2082166675Srrs			*optsize = sizeof(struct sctp_pcbinfo);
2083163953Srrs		}
2084163953Srrs		break;
2085167598Srrs
2086163953Srrs	case SCTP_STATUS:
2087163953Srrs		{
2088163953Srrs			struct sctp_nets *net;
2089163953Srrs			struct sctp_status *sstat;
2090163953Srrs
2091166675Srrs			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2092166675Srrs			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2093163953Srrs
2094163953Srrs			if (stcb == NULL) {
2095163953Srrs				error = EINVAL;
2096163953Srrs				break;
2097163953Srrs			}
2098163953Srrs			/*
2099163953Srrs			 * I think passing the state is fine since
2100163953Srrs			 * sctp_constants.h will be available to the user
2101163953Srrs			 * land.
2102163953Srrs			 */
2103163953Srrs			sstat->sstat_state = stcb->asoc.state;
2104163953Srrs			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2105163953Srrs			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2106163953Srrs			/*
2107163953Srrs			 * We can't include chunks that have been passed to
2108163953Srrs			 * the socket layer. Only things in queue.
2109163953Srrs			 */
2110163953Srrs			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2111163953Srrs			    stcb->asoc.cnt_on_all_streams);
2112163953Srrs
2113163953Srrs
2114163953Srrs			sstat->sstat_instrms = stcb->asoc.streamincnt;
2115163953Srrs			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2116163953Srrs			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2117163953Srrs			memcpy(&sstat->sstat_primary.spinfo_address,
2118163953Srrs			    &stcb->asoc.primary_destination->ro._l_addr,
2119163953Srrs			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2120163953Srrs			net = stcb->asoc.primary_destination;
2121163953Srrs			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2122163953Srrs			/*
2123163953Srrs			 * Again the user can get info from sctp_constants.h
2124163953Srrs			 * for what the state of the network is.
2125163953Srrs			 */
2126163953Srrs			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2127163953Srrs			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2128163953Srrs			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2129163953Srrs			sstat->sstat_primary.spinfo_rto = net->RTO;
2130163953Srrs			sstat->sstat_primary.spinfo_mtu = net->mtu;
2131163953Srrs			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2132163953Srrs			SCTP_TCB_UNLOCK(stcb);
2133166675Srrs			*optsize = sizeof(*sstat);
2134163953Srrs		}
2135163953Srrs		break;
2136163953Srrs	case SCTP_RTOINFO:
2137163953Srrs		{
2138163953Srrs			struct sctp_rtoinfo *srto;
2139163953Srrs
2140166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2141166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2142166675Srrs
2143166675Srrs			if (stcb) {
2144166675Srrs				srto->srto_initial = stcb->asoc.initial_rto;
2145166675Srrs				srto->srto_max = stcb->asoc.maxrto;
2146166675Srrs				srto->srto_min = stcb->asoc.minrto;
2147166675Srrs				SCTP_TCB_UNLOCK(stcb);
2148166675Srrs			} else {
2149163953Srrs				SCTP_INP_RLOCK(inp);
2150163953Srrs				srto->srto_initial = inp->sctp_ep.initial_rto;
2151163953Srrs				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2152163953Srrs				srto->srto_min = inp->sctp_ep.sctp_minrto;
2153163953Srrs				SCTP_INP_RUNLOCK(inp);
2154163953Srrs			}
2155166675Srrs			*optsize = sizeof(*srto);
2156163953Srrs		}
2157163953Srrs		break;
2158163953Srrs	case SCTP_ASSOCINFO:
2159163953Srrs		{
2160163953Srrs			struct sctp_assocparams *sasoc;
2161163953Srrs
2162166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2163166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2164163953Srrs
2165163953Srrs			if (stcb) {
2166163953Srrs				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2167163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2168163953Srrs				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2169163953Srrs				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2170163953Srrs				sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2171167598Srrs				sasoc->sasoc_sack_delay = stcb->asoc.delayed_ack;
2172167598Srrs				sasoc->sasoc_sack_freq = stcb->asoc.sack_freq;
2173163953Srrs				SCTP_TCB_UNLOCK(stcb);
2174163953Srrs			} else {
2175163953Srrs				SCTP_INP_RLOCK(inp);
2176163953Srrs				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2177163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
2178163953Srrs				sasoc->sasoc_peer_rwnd = 0;
2179163953Srrs				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2180163953Srrs				sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2181167598Srrs				sasoc->sasoc_sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2182167598Srrs				sasoc->sasoc_sack_freq = inp->sctp_ep.sctp_sack_freq;
2183163953Srrs				SCTP_INP_RUNLOCK(inp);
2184163953Srrs			}
2185166675Srrs			*optsize = sizeof(*sasoc);
2186163953Srrs		}
2187163953Srrs		break;
2188163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2189163953Srrs		{
2190163953Srrs			struct sctp_sndrcvinfo *s_info;
2191163953Srrs
2192166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2193166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2194166675Srrs
2195166675Srrs			if (stcb) {
2196166675Srrs				*s_info = stcb->asoc.def_send;
2197166675Srrs				SCTP_TCB_UNLOCK(stcb);
2198166675Srrs			} else {
2199163953Srrs				SCTP_INP_RLOCK(inp);
2200166675Srrs				*s_info = inp->def_send;
2201163953Srrs				SCTP_INP_RUNLOCK(inp);
2202163953Srrs			}
2203166675Srrs			*optsize = sizeof(*s_info);
2204163953Srrs		}
2205163953Srrs		break;
2206163953Srrs	case SCTP_INITMSG:
2207163953Srrs		{
2208163953Srrs			struct sctp_initmsg *sinit;
2209163953Srrs
2210166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2211163953Srrs			SCTP_INP_RLOCK(inp);
2212163953Srrs			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2213163953Srrs			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2214163953Srrs			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2215163953Srrs			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2216163953Srrs			SCTP_INP_RUNLOCK(inp);
2217166675Srrs			*optsize = sizeof(*sinit);
2218163953Srrs		}
2219163953Srrs		break;
2220163953Srrs	case SCTP_PRIMARY_ADDR:
2221163953Srrs		/* we allow a "get" operation on this */
2222163953Srrs		{
2223163953Srrs			struct sctp_setprim *ssp;
2224163953Srrs
2225166675Srrs			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2226166675Srrs			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2227166675Srrs
2228166675Srrs			if (stcb) {
2229166675Srrs				/* simply copy out the sockaddr_storage... */
2230166675Srrs				memcpy(&ssp->ssp_addr, &stcb->asoc.primary_destination->ro._l_addr,
2231166675Srrs				    ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2232166675Srrs				SCTP_TCB_UNLOCK(stcb);
2233166675Srrs			} else {
2234163953Srrs				error = EINVAL;
2235163953Srrs			}
2236166675Srrs			*optsize = sizeof(*ssp);
2237163953Srrs		}
2238163953Srrs		break;
2239163953Srrs
2240163953Srrs	case SCTP_HMAC_IDENT:
2241163953Srrs		{
2242163953Srrs			struct sctp_hmacalgo *shmac;
2243163953Srrs			sctp_hmaclist_t *hmaclist;
2244163953Srrs			uint32_t size;
2245163953Srrs			int i;
2246163953Srrs
2247166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2248166675Srrs
2249163953Srrs			SCTP_INP_RLOCK(inp);
2250163953Srrs			hmaclist = inp->sctp_ep.local_hmacs;
2251163953Srrs			if (hmaclist == NULL) {
2252163953Srrs				/* no HMACs to return */
2253166675Srrs				*optsize = sizeof(*shmac);
2254163953Srrs				break;
2255163953Srrs			}
2256163953Srrs			/* is there room for all of the hmac ids? */
2257163953Srrs			size = sizeof(*shmac) + (hmaclist->num_algo *
2258163953Srrs			    sizeof(shmac->shmac_idents[0]));
2259166675Srrs			if ((size_t)(*optsize) < size) {
2260163953Srrs				error = EINVAL;
2261163953Srrs				SCTP_INP_RUNLOCK(inp);
2262163953Srrs				break;
2263163953Srrs			}
2264163953Srrs			/* copy in the list */
2265163953Srrs			for (i = 0; i < hmaclist->num_algo; i++)
2266163953Srrs				shmac->shmac_idents[i] = hmaclist->hmac[i];
2267163953Srrs			SCTP_INP_RUNLOCK(inp);
2268166675Srrs			*optsize = size;
2269163953Srrs			break;
2270163953Srrs		}
2271163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2272163953Srrs		{
2273163953Srrs			struct sctp_authkeyid *scact;
2274163953Srrs
2275166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2276166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2277166675Srrs
2278166675Srrs			if (stcb) {
2279163953Srrs				/* get the active key on the assoc */
2280163953Srrs				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2281163953Srrs				SCTP_TCB_UNLOCK(stcb);
2282163953Srrs			} else {
2283163953Srrs				/* get the endpoint active key */
2284163953Srrs				SCTP_INP_RLOCK(inp);
2285163953Srrs				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2286163953Srrs				SCTP_INP_RUNLOCK(inp);
2287163953Srrs			}
2288166675Srrs			*optsize = sizeof(*scact);
2289163953Srrs			break;
2290163953Srrs		}
2291163953Srrs	case SCTP_LOCAL_AUTH_CHUNKS:
2292163953Srrs		{
2293163953Srrs			struct sctp_authchunks *sac;
2294163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2295166675Srrs			size_t size = 0;
2296163953Srrs
2297166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2298166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2299166675Srrs
2300166675Srrs			if (stcb) {
2301163953Srrs				/* get off the assoc */
2302163953Srrs				chklist = stcb->asoc.local_auth_chunks;
2303163953Srrs				/* is there enough space? */
2304163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2305166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2306163953Srrs					error = EINVAL;
2307166675Srrs				} else {
2308166675Srrs					/* copy in the chunks */
2309166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2310163953Srrs				}
2311163953Srrs				SCTP_TCB_UNLOCK(stcb);
2312163953Srrs			} else {
2313163953Srrs				/* get off the endpoint */
2314163953Srrs				SCTP_INP_RLOCK(inp);
2315163953Srrs				chklist = inp->sctp_ep.local_auth_chunks;
2316163953Srrs				/* is there enough space? */
2317163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2318166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2319163953Srrs					error = EINVAL;
2320166675Srrs				} else {
2321166675Srrs					/* copy in the chunks */
2322166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2323163953Srrs				}
2324163953Srrs				SCTP_INP_RUNLOCK(inp);
2325163953Srrs			}
2326166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2327163953Srrs			break;
2328163953Srrs		}
2329163953Srrs	case SCTP_PEER_AUTH_CHUNKS:
2330163953Srrs		{
2331163953Srrs			struct sctp_authchunks *sac;
2332163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2333166675Srrs			size_t size = 0;
2334163953Srrs
2335166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2336166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2337166675Srrs
2338166675Srrs			if (stcb) {
2339166675Srrs				/* get off the assoc */
2340166675Srrs				chklist = stcb->asoc.peer_auth_chunks;
2341166675Srrs				/* is there enough space? */
2342166675Srrs				size = sctp_auth_get_chklist_size(chklist);
2343166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2344166675Srrs					error = EINVAL;
2345166675Srrs				} else {
2346166675Srrs					/* copy in the chunks */
2347166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2348166675Srrs				}
2349166675Srrs				SCTP_TCB_UNLOCK(stcb);
2350166675Srrs			} else {
2351163953Srrs				error = ENOENT;
2352163953Srrs			}
2353166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2354163953Srrs			break;
2355163953Srrs		}
2356163953Srrs
2357163953Srrs
2358163953Srrs	default:
2359163953Srrs		error = ENOPROTOOPT;
2360166675Srrs		*optsize = 0;
2361163953Srrs		break;
2362163953Srrs	}			/* end switch (sopt->sopt_name) */
2363163953Srrs	return (error);
2364163953Srrs}
2365163953Srrs
2366163953Srrsstatic int
2367166675Srrssctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2368166675Srrs    void *p)
2369163953Srrs{
2370166675Srrs	int error, set_opt;
2371166675Srrs	uint32_t *mopt;
2372163953Srrs	struct sctp_tcb *stcb = NULL;
2373163953Srrs	struct sctp_inpcb *inp;
2374167598Srrs	uint32_t vrf_id;
2375163953Srrs
2376166675Srrs	if (optval == NULL) {
2377167598Srrs		printf("optval is NULL\n");
2378163953Srrs		return (EINVAL);
2379163953Srrs	}
2380163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
2381167598Srrs	if (inp == 0) {
2382167598Srrs		printf("inp is NULL?\n");
2383163953Srrs		return EINVAL;
2384167598Srrs	}
2385167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
2386163953Srrs
2387163953Srrs	error = 0;
2388166675Srrs	switch (optname) {
2389163953Srrs	case SCTP_NODELAY:
2390163953Srrs	case SCTP_AUTOCLOSE:
2391163953Srrs	case SCTP_AUTO_ASCONF:
2392163953Srrs	case SCTP_EXPLICIT_EOR:
2393163953Srrs	case SCTP_DISABLE_FRAGMENTS:
2394163953Srrs	case SCTP_USE_EXT_RCVINFO:
2395163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
2396163953Srrs		/* copy in the option value */
2397166675Srrs		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2398163953Srrs		set_opt = 0;
2399163953Srrs		if (error)
2400163953Srrs			break;
2401166675Srrs		switch (optname) {
2402163953Srrs		case SCTP_DISABLE_FRAGMENTS:
2403163953Srrs			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2404163953Srrs			break;
2405163953Srrs		case SCTP_AUTO_ASCONF:
2406163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2407163953Srrs			break;
2408163953Srrs		case SCTP_EXPLICIT_EOR:
2409163953Srrs			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2410163953Srrs			break;
2411163953Srrs		case SCTP_USE_EXT_RCVINFO:
2412163953Srrs			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2413163953Srrs			break;
2414163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
2415163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2416163953Srrs				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2417163953Srrs			} else {
2418163953Srrs				return (EINVAL);
2419163953Srrs			}
2420163953Srrs			break;
2421163953Srrs		case SCTP_NODELAY:
2422163953Srrs			set_opt = SCTP_PCB_FLAGS_NODELAY;
2423163953Srrs			break;
2424163953Srrs		case SCTP_AUTOCLOSE:
2425163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2426163953Srrs			/*
2427163953Srrs			 * The value is in ticks. Note this does not effect
2428163953Srrs			 * old associations, only new ones.
2429163953Srrs			 */
2430163953Srrs			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2431163953Srrs			break;
2432163953Srrs		}
2433163953Srrs		SCTP_INP_WLOCK(inp);
2434163953Srrs		if (*mopt != 0) {
2435163953Srrs			sctp_feature_on(inp, set_opt);
2436163953Srrs		} else {
2437163953Srrs			sctp_feature_off(inp, set_opt);
2438163953Srrs		}
2439163953Srrs		SCTP_INP_WUNLOCK(inp);
2440163953Srrs		break;
2441163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
2442163953Srrs		{
2443166675Srrs			uint32_t *value;
2444166675Srrs
2445166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2446167736Srrs			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2447167736Srrs				error = EINVAL;
2448167736Srrs				break;
2449167736Srrs			}
2450166675Srrs			inp->partial_delivery_point = *value;
2451163953Srrs		}
2452163953Srrs		break;
2453163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
2454163953Srrs		/* not yet until we re-write sctp_recvmsg() */
2455163953Srrs		{
2456166675Srrs			uint32_t *on_off;
2457163953Srrs
2458166675Srrs			SCTP_CHECK_AND_CAST(on_off, optval, uint32_t, optsize);
2459166675Srrs			if (*on_off) {
2460163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2461163953Srrs			} else {
2462163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2463163953Srrs			}
2464163953Srrs		}
2465163953Srrs		break;
2466163953Srrs	case SCTP_CMT_ON_OFF:
2467163953Srrs		{
2468163953Srrs			struct sctp_assoc_value *av;
2469163953Srrs
2470166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2471166675Srrs			if (sctp_cmt_on_off) {
2472166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2473166675Srrs				if (stcb) {
2474163953Srrs					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2475166675Srrs					SCTP_TCB_UNLOCK(stcb);
2476163953Srrs				} else {
2477166675Srrs					error = ENOTCONN;
2478163953Srrs				}
2479166675Srrs			} else {
2480166675Srrs				error = ENOPROTOOPT;
2481163953Srrs			}
2482163953Srrs		}
2483163953Srrs		break;
2484163953Srrs	case SCTP_CLR_STAT_LOG:
2485163953Srrs#ifdef SCTP_STAT_LOGGING
2486163953Srrs		sctp_clr_stat_log();
2487163953Srrs#else
2488163953Srrs		error = EOPNOTSUPP;
2489163953Srrs#endif
2490163953Srrs		break;
2491163953Srrs	case SCTP_CONTEXT:
2492163953Srrs		{
2493163953Srrs			struct sctp_assoc_value *av;
2494163953Srrs
2495166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2496166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2497166675Srrs
2498166675Srrs			if (stcb) {
2499166675Srrs				stcb->asoc.context = av->assoc_value;
2500166675Srrs				SCTP_TCB_UNLOCK(stcb);
2501163953Srrs			} else {
2502166675Srrs				SCTP_INP_WLOCK(inp);
2503163953Srrs				inp->sctp_context = av->assoc_value;
2504166675Srrs				SCTP_INP_WUNLOCK(inp);
2505163953Srrs			}
2506163953Srrs		}
2507163953Srrs		break;
2508167598Srrs	case SCTP_VRF_ID:
2509167598Srrs		{
2510167598Srrs			uint32_t *vrf_id;
2511167598Srrs
2512167598Srrs			SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, optsize);
2513167598Srrs			if (*vrf_id > SCTP_MAX_VRF_ID) {
2514167598Srrs				error = EINVAL;
2515167598Srrs				break;
2516167598Srrs			}
2517167598Srrs			inp->def_vrf_id = *vrf_id;
2518167598Srrs			break;
2519167598Srrs		}
2520167598Srrs	case SCTP_DEL_VRF_ID:
2521167598Srrs		{
2522167598Srrs			error = EOPNOTSUPP;
2523167598Srrs			break;
2524167598Srrs		}
2525167598Srrs	case SCTP_ADD_VRF_ID:
2526167598Srrs		{
2527167598Srrs			error = EOPNOTSUPP;
2528167598Srrs			break;
2529167598Srrs		}
2530167598Srrs
2531163953Srrs	case SCTP_DELAYED_ACK_TIME:
2532163953Srrs		{
2533163953Srrs			struct sctp_assoc_value *tm;
2534163953Srrs
2535166675Srrs			SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, optsize);
2536166675Srrs			SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
2537163953Srrs
2538166675Srrs			if (stcb) {
2539166675Srrs				stcb->asoc.delayed_ack = tm->assoc_value;
2540166675Srrs				SCTP_TCB_UNLOCK(stcb);
2541166675Srrs			} else {
2542163953Srrs				SCTP_INP_WLOCK(inp);
2543166675Srrs				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(tm->assoc_value);
2544163953Srrs				SCTP_INP_WUNLOCK(inp);
2545163953Srrs			}
2546166675Srrs			break;
2547163953Srrs		}
2548163953Srrs	case SCTP_AUTH_CHUNK:
2549163953Srrs		{
2550163953Srrs			struct sctp_authchunk *sauth;
2551163953Srrs
2552166675Srrs			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2553166675Srrs
2554166675Srrs			SCTP_INP_WLOCK(inp);
2555166675Srrs			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2556163953Srrs				error = EINVAL;
2557166675Srrs			SCTP_INP_WUNLOCK(inp);
2558163953Srrs			break;
2559163953Srrs		}
2560163953Srrs	case SCTP_AUTH_KEY:
2561163953Srrs		{
2562163953Srrs			struct sctp_authkey *sca;
2563163953Srrs			struct sctp_keyhead *shared_keys;
2564163953Srrs			sctp_sharedkey_t *shared_key;
2565163953Srrs			sctp_key_t *key = NULL;
2566166675Srrs			size_t size;
2567163953Srrs
2568166675Srrs			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2569166675Srrs			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id)
2570166675Srrs			    size = optsize - sizeof(*sca);
2571166675Srrs
2572166675Srrs			if (stcb) {
2573163953Srrs				/* set it on the assoc */
2574163953Srrs				shared_keys = &stcb->asoc.shared_keys;
2575163953Srrs				/* clear the cached keys for this key id */
2576163953Srrs				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2577163953Srrs				/*
2578163953Srrs				 * create the new shared key and
2579163953Srrs				 * insert/replace it
2580163953Srrs				 */
2581163953Srrs				if (size > 0) {
2582163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2583163953Srrs					if (key == NULL) {
2584163953Srrs						error = ENOMEM;
2585163953Srrs						SCTP_TCB_UNLOCK(stcb);
2586163953Srrs						break;
2587163953Srrs					}
2588163953Srrs				}
2589163953Srrs				shared_key = sctp_alloc_sharedkey();
2590163953Srrs				if (shared_key == NULL) {
2591163953Srrs					sctp_free_key(key);
2592163953Srrs					error = ENOMEM;
2593163953Srrs					SCTP_TCB_UNLOCK(stcb);
2594163953Srrs					break;
2595163953Srrs				}
2596163953Srrs				shared_key->key = key;
2597163953Srrs				shared_key->keyid = sca->sca_keynumber;
2598163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2599163953Srrs				SCTP_TCB_UNLOCK(stcb);
2600163953Srrs			} else {
2601166675Srrs				/* set it on the endpoint */
2602163953Srrs				SCTP_INP_WLOCK(inp);
2603163953Srrs				shared_keys = &inp->sctp_ep.shared_keys;
2604163953Srrs				/*
2605163953Srrs				 * clear the cached keys on all assocs for
2606163953Srrs				 * this key id
2607163953Srrs				 */
2608163953Srrs				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2609163953Srrs				/*
2610163953Srrs				 * create the new shared key and
2611163953Srrs				 * insert/replace it
2612163953Srrs				 */
2613163953Srrs				if (size > 0) {
2614163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2615163953Srrs					if (key == NULL) {
2616163953Srrs						error = ENOMEM;
2617163953Srrs						SCTP_INP_WUNLOCK(inp);
2618163953Srrs						break;
2619163953Srrs					}
2620163953Srrs				}
2621163953Srrs				shared_key = sctp_alloc_sharedkey();
2622163953Srrs				if (shared_key == NULL) {
2623163953Srrs					sctp_free_key(key);
2624163953Srrs					error = ENOMEM;
2625163953Srrs					SCTP_INP_WUNLOCK(inp);
2626163953Srrs					break;
2627163953Srrs				}
2628163953Srrs				shared_key->key = key;
2629163953Srrs				shared_key->keyid = sca->sca_keynumber;
2630163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2631163953Srrs				SCTP_INP_WUNLOCK(inp);
2632163953Srrs			}
2633163953Srrs			break;
2634163953Srrs		}
2635163953Srrs	case SCTP_HMAC_IDENT:
2636163953Srrs		{
2637163953Srrs			struct sctp_hmacalgo *shmac;
2638163953Srrs			sctp_hmaclist_t *hmaclist;
2639163953Srrs			uint32_t hmacid;
2640166675Srrs			size_t size, i;
2641163953Srrs
2642166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2643166675Srrs			size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2644163953Srrs			hmaclist = sctp_alloc_hmaclist(size);
2645163953Srrs			if (hmaclist == NULL) {
2646163953Srrs				error = ENOMEM;
2647163953Srrs				break;
2648163953Srrs			}
2649163953Srrs			for (i = 0; i < size; i++) {
2650163953Srrs				hmacid = shmac->shmac_idents[i];
2651163953Srrs				if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2652163953Srrs					 /* invalid HMACs were found */ ;
2653163953Srrs					error = EINVAL;
2654164085Srrs					sctp_free_hmaclist(hmaclist);
2655163953Srrs					goto sctp_set_hmac_done;
2656163953Srrs				}
2657163953Srrs			}
2658163953Srrs			/* set it on the endpoint */
2659163953Srrs			SCTP_INP_WLOCK(inp);
2660163953Srrs			if (inp->sctp_ep.local_hmacs)
2661163953Srrs				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2662163953Srrs			inp->sctp_ep.local_hmacs = hmaclist;
2663163953Srrs			SCTP_INP_WUNLOCK(inp);
2664163953Srrs	sctp_set_hmac_done:
2665163953Srrs			break;
2666163953Srrs		}
2667163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2668163953Srrs		{
2669163953Srrs			struct sctp_authkeyid *scact;
2670163953Srrs
2671166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2672166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2673166675Srrs
2674163953Srrs			/* set the active key on the right place */
2675166675Srrs			if (stcb) {
2676163953Srrs				/* set the active key on the assoc */
2677163953Srrs				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2678163953Srrs					error = EINVAL;
2679163953Srrs				SCTP_TCB_UNLOCK(stcb);
2680163953Srrs			} else {
2681163953Srrs				/* set the active key on the endpoint */
2682163953Srrs				SCTP_INP_WLOCK(inp);
2683163953Srrs				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2684163953Srrs					error = EINVAL;
2685163953Srrs				SCTP_INP_WUNLOCK(inp);
2686163953Srrs			}
2687163953Srrs			break;
2688163953Srrs		}
2689163953Srrs	case SCTP_AUTH_DELETE_KEY:
2690163953Srrs		{
2691163953Srrs			struct sctp_authkeyid *scdel;
2692163953Srrs
2693166675Srrs			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2694166675Srrs			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2695166675Srrs
2696163953Srrs			/* delete the key from the right place */
2697166675Srrs			if (stcb) {
2698163953Srrs				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2699163953Srrs					error = EINVAL;
2700163953Srrs				SCTP_TCB_UNLOCK(stcb);
2701163953Srrs			} else {
2702163953Srrs				SCTP_INP_WLOCK(inp);
2703163953Srrs				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2704163953Srrs					error = EINVAL;
2705163953Srrs				SCTP_INP_WUNLOCK(inp);
2706163953Srrs			}
2707163953Srrs			break;
2708163953Srrs		}
2709163953Srrs
2710163953Srrs	case SCTP_RESET_STREAMS:
2711163953Srrs		{
2712163953Srrs			struct sctp_stream_reset *strrst;
2713163953Srrs			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2714163953Srrs			int i;
2715163953Srrs
2716166675Srrs			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2717166675Srrs			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2718163953Srrs
2719163953Srrs			if (stcb == NULL) {
2720163953Srrs				error = ENOENT;
2721163953Srrs				break;
2722163953Srrs			}
2723163953Srrs			if (stcb->asoc.peer_supports_strreset == 0) {
2724163953Srrs				/*
2725163953Srrs				 * Peer does not support it, we return
2726163953Srrs				 * protocol not supported since this is true
2727163953Srrs				 * for this feature and this peer, not the
2728163953Srrs				 * socket request in general.
2729163953Srrs				 */
2730163953Srrs				error = EPROTONOSUPPORT;
2731163953Srrs				SCTP_TCB_UNLOCK(stcb);
2732163953Srrs				break;
2733163953Srrs			}
2734163953Srrs			if (stcb->asoc.stream_reset_outstanding) {
2735163953Srrs				error = EALREADY;
2736163953Srrs				SCTP_TCB_UNLOCK(stcb);
2737163953Srrs				break;
2738163953Srrs			}
2739163953Srrs			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2740163953Srrs				send_in = 1;
2741163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2742163953Srrs				send_out = 1;
2743163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2744163953Srrs				send_in = 1;
2745163953Srrs				send_out = 1;
2746163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2747163953Srrs				send_tsn = 1;
2748163953Srrs			} else {
2749163953Srrs				error = EINVAL;
2750163953Srrs				SCTP_TCB_UNLOCK(stcb);
2751163953Srrs				break;
2752163953Srrs			}
2753163953Srrs			for (i = 0; i < strrst->strrst_num_streams; i++) {
2754163953Srrs				if ((send_in) &&
2755163953Srrs
2756163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2757163953Srrs					error = EINVAL;
2758163953Srrs					goto get_out;
2759163953Srrs				}
2760163953Srrs				if ((send_out) &&
2761163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2762163953Srrs					error = EINVAL;
2763163953Srrs					goto get_out;
2764163953Srrs				}
2765163953Srrs			}
2766163953Srrs			if (error) {
2767163953Srrs		get_out:
2768163953Srrs				SCTP_TCB_UNLOCK(stcb);
2769163953Srrs				break;
2770163953Srrs			}
2771163953Srrs			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2772163953Srrs			    strrst->strrst_list,
2773163953Srrs			    send_out, (stcb->asoc.str_reset_seq_in - 3),
2774163953Srrs			    send_in, send_tsn);
2775163953Srrs
2776163953Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2777163953Srrs			SCTP_TCB_UNLOCK(stcb);
2778163953Srrs		}
2779163953Srrs		break;
2780166675Srrs
2781163953Srrs	case SCTP_CONNECT_X:
2782166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2783163953Srrs			error = EINVAL;
2784163953Srrs			break;
2785163953Srrs		}
2786166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2787163953Srrs		break;
2788163953Srrs
2789163953Srrs	case SCTP_CONNECT_X_DELAYED:
2790166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2791163953Srrs			error = EINVAL;
2792163953Srrs			break;
2793163953Srrs		}
2794166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2795163953Srrs		break;
2796163953Srrs
2797163953Srrs	case SCTP_CONNECT_X_COMPLETE:
2798163953Srrs		{
2799163953Srrs			struct sockaddr *sa;
2800163953Srrs			struct sctp_nets *net;
2801163953Srrs
2802166675Srrs			/* FIXME MT: check correct? */
2803166675Srrs			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2804166675Srrs
2805163953Srrs			/* find tcb */
2806163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2807163953Srrs				SCTP_INP_RLOCK(inp);
2808163953Srrs				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2809163953Srrs				if (stcb) {
2810163953Srrs					SCTP_TCB_LOCK(stcb);
2811163953Srrs					net = sctp_findnet(stcb, sa);
2812163953Srrs				}
2813163953Srrs				SCTP_INP_RUNLOCK(inp);
2814163953Srrs			} else {
2815166675Srrs				/*
2816166675Srrs				 * We increment here since
2817166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2818166675Srrs				 * decrement if it finds the stcb as long as
2819166675Srrs				 * the locked tcb (last argument) is NOT a
2820166675Srrs				 * TCB.. aka NULL.
2821166675Srrs				 */
2822163953Srrs				SCTP_INP_INCR_REF(inp);
2823163953Srrs				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2824163953Srrs				if (stcb == NULL) {
2825163953Srrs					SCTP_INP_DECR_REF(inp);
2826163953Srrs				}
2827163953Srrs			}
2828163953Srrs
2829163953Srrs			if (stcb == NULL) {
2830163953Srrs				error = ENOENT;
2831163953Srrs				break;
2832163953Srrs			}
2833163953Srrs			if (stcb->asoc.delayed_connection == 1) {
2834163953Srrs				stcb->asoc.delayed_connection = 0;
2835163953Srrs				SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2836165220Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
2837165220Srrs				    stcb->asoc.primary_destination,
2838165220Srrs				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
2839163953Srrs				sctp_send_initiate(inp, stcb);
2840163953Srrs			} else {
2841163953Srrs				/*
2842163953Srrs				 * already expired or did not use delayed
2843163953Srrs				 * connectx
2844163953Srrs				 */
2845163953Srrs				error = EALREADY;
2846163953Srrs			}
2847163953Srrs			SCTP_TCB_UNLOCK(stcb);
2848163953Srrs		}
2849163953Srrs		break;
2850163953Srrs	case SCTP_MAXBURST:
2851163953Srrs		{
2852163953Srrs			uint8_t *burst;
2853163953Srrs
2854166675Srrs			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
2855166675Srrs
2856163953Srrs			SCTP_INP_WLOCK(inp);
2857163953Srrs			if (*burst) {
2858163953Srrs				inp->sctp_ep.max_burst = *burst;
2859163953Srrs			}
2860163953Srrs			SCTP_INP_WUNLOCK(inp);
2861163953Srrs		}
2862163953Srrs		break;
2863163953Srrs	case SCTP_MAXSEG:
2864163953Srrs		{
2865167598Srrs			struct sctp_assoc_value *av;
2866163953Srrs			int ovh;
2867163953Srrs
2868167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2869167598Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2870166675Srrs
2871167598Srrs			if (stcb) {
2872167598Srrs				error = EINVAL;
2873167598Srrs				SCTP_TCB_UNLOCK(stcb);
2874163953Srrs			} else {
2875167598Srrs				SCTP_INP_WLOCK(inp);
2876167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2877167598Srrs					ovh = SCTP_MED_OVERHEAD;
2878167598Srrs				} else {
2879167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
2880167598Srrs				}
2881167598Srrs				/*
2882167598Srrs				 * FIXME MT: I think this is not in tune
2883167598Srrs				 * with the API ID
2884167598Srrs				 */
2885167598Srrs				if (av->assoc_value) {
2886167598Srrs					inp->sctp_frag_point = (av->assoc_value + ovh);
2887167598Srrs				} else {
2888167598Srrs					error = EINVAL;
2889167598Srrs				}
2890167598Srrs				SCTP_INP_WUNLOCK(inp);
2891163953Srrs			}
2892163953Srrs		}
2893163953Srrs		break;
2894163953Srrs	case SCTP_EVENTS:
2895163953Srrs		{
2896163953Srrs			struct sctp_event_subscribe *events;
2897163953Srrs
2898166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
2899166675Srrs
2900163953Srrs			SCTP_INP_WLOCK(inp);
2901163953Srrs			if (events->sctp_data_io_event) {
2902163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2903163953Srrs			} else {
2904163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2905163953Srrs			}
2906163953Srrs
2907163953Srrs			if (events->sctp_association_event) {
2908163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2909163953Srrs			} else {
2910163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2911163953Srrs			}
2912163953Srrs
2913163953Srrs			if (events->sctp_address_event) {
2914163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2915163953Srrs			} else {
2916163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2917163953Srrs			}
2918163953Srrs
2919163953Srrs			if (events->sctp_send_failure_event) {
2920163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2921163953Srrs			} else {
2922163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2923163953Srrs			}
2924163953Srrs
2925163953Srrs			if (events->sctp_peer_error_event) {
2926163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2927163953Srrs			} else {
2928163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2929163953Srrs			}
2930163953Srrs
2931163953Srrs			if (events->sctp_shutdown_event) {
2932163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2933163953Srrs			} else {
2934163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2935163953Srrs			}
2936163953Srrs
2937163953Srrs			if (events->sctp_partial_delivery_event) {
2938163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2939163953Srrs			} else {
2940163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2941163953Srrs			}
2942163953Srrs
2943163953Srrs			if (events->sctp_adaptation_layer_event) {
2944163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2945163953Srrs			} else {
2946163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2947163953Srrs			}
2948163953Srrs
2949163953Srrs			if (events->sctp_authentication_event) {
2950163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2951163953Srrs			} else {
2952163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2953163953Srrs			}
2954163953Srrs
2955163953Srrs			if (events->sctp_stream_reset_events) {
2956163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2957163953Srrs			} else {
2958163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2959163953Srrs			}
2960163953Srrs			SCTP_INP_WUNLOCK(inp);
2961163953Srrs		}
2962163953Srrs		break;
2963163953Srrs
2964163953Srrs	case SCTP_ADAPTATION_LAYER:
2965163953Srrs		{
2966163953Srrs			struct sctp_setadaptation *adap_bits;
2967163953Srrs
2968166675Srrs			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
2969163953Srrs			SCTP_INP_WLOCK(inp);
2970163953Srrs			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
2971163953Srrs			SCTP_INP_WUNLOCK(inp);
2972163953Srrs		}
2973163953Srrs		break;
2974166675Srrs#ifdef SCTP_DEBUG
2975163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
2976163953Srrs		{
2977163953Srrs			uint32_t *vvv;
2978163953Srrs
2979166675Srrs			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
2980163953Srrs			SCTP_INP_WLOCK(inp);
2981163953Srrs			inp->sctp_ep.initial_sequence_debug = *vvv;
2982163953Srrs			SCTP_INP_WUNLOCK(inp);
2983163953Srrs		}
2984163953Srrs		break;
2985166675Srrs#endif
2986163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2987163953Srrs		{
2988163953Srrs			struct sctp_sndrcvinfo *s_info;
2989163953Srrs
2990166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
2991166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2992163953Srrs
2993166675Srrs			if (stcb) {
2994166675Srrs				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
2995166675Srrs					stcb->asoc.def_send = *s_info;
2996163953Srrs				} else {
2997166675Srrs					error = EINVAL;
2998163953Srrs				}
2999166675Srrs				SCTP_TCB_UNLOCK(stcb);
3000166675Srrs			} else {
3001166675Srrs				SCTP_INP_WLOCK(inp);
3002163953Srrs				inp->def_send = *s_info;
3003166675Srrs				SCTP_INP_WUNLOCK(inp);
3004163953Srrs			}
3005163953Srrs		}
3006163953Srrs		break;
3007163953Srrs	case SCTP_PEER_ADDR_PARAMS:
3008163953Srrs		/* Applys to the specific association */
3009163953Srrs		{
3010163953Srrs			struct sctp_paddrparams *paddrp;
3011163953Srrs			struct sctp_nets *net;
3012163953Srrs
3013166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3014166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3015163953Srrs			net = NULL;
3016166675Srrs			if (stcb) {
3017166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3018166675Srrs			} else {
3019166675Srrs				/*
3020166675Srrs				 * We increment here since
3021166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3022166675Srrs				 * decrement if it finds the stcb as long as
3023166675Srrs				 * the locked tcb (last argument) is NOT a
3024166675Srrs				 * TCB.. aka NULL.
3025166675Srrs				 */
3026166675Srrs				SCTP_INP_INCR_REF(inp);
3027166675Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3028166675Srrs				    (struct sockaddr *)&paddrp->spp_address,
3029166675Srrs				    &net, NULL, NULL);
3030163953Srrs				if (stcb == NULL) {
3031166675Srrs					SCTP_INP_DECR_REF(inp);
3032163953Srrs				}
3033163953Srrs			}
3034166675Srrs
3035166675Srrs
3036163953Srrs			if (stcb) {
3037163953Srrs				/************************TCB SPECIFIC SET ******************/
3038163953Srrs				/*
3039163953Srrs				 * do we change the timer for HB, we run
3040163953Srrs				 * only one?
3041163953Srrs				 */
3042163953Srrs				if (paddrp->spp_hbinterval)
3043163953Srrs					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3044163953Srrs				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3045163953Srrs					stcb->asoc.heart_beat_delay = 0;
3046163953Srrs
3047163953Srrs				/* network sets ? */
3048163953Srrs				if (net) {
3049163953Srrs					/************************NET SPECIFIC SET ******************/
3050163953Srrs					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3051163953Srrs						/* on demand HB */
3052163953Srrs						sctp_send_hb(stcb, 1, net);
3053163953Srrs					}
3054163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3055163953Srrs						net->dest_state |= SCTP_ADDR_NOHB;
3056163953Srrs					}
3057163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3058163953Srrs						net->dest_state &= ~SCTP_ADDR_NOHB;
3059163953Srrs					}
3060163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
3061165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3062165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3063165220Srrs							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3064163953Srrs						}
3065163953Srrs						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3066163953Srrs							net->mtu = paddrp->spp_pathmtu;
3067163953Srrs							if (net->mtu < stcb->asoc.smallest_mtu)
3068167695Srrs								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3069163953Srrs						}
3070163953Srrs					}
3071163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3072165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3073163953Srrs							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3074163953Srrs						}
3075163953Srrs					}
3076163953Srrs					if (paddrp->spp_pathmaxrxt)
3077163953Srrs						net->failure_threshold = paddrp->spp_pathmaxrxt;
3078167598Srrs#ifdef INET
3079163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3080163953Srrs						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3081163953Srrs							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3082163953Srrs						}
3083163953Srrs					}
3084163953Srrs#endif
3085167598Srrs#ifdef INET6
3086163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3087163953Srrs						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3088163953Srrs							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3089163953Srrs						}
3090163953Srrs					}
3091163953Srrs#endif
3092163953Srrs				} else {
3093163953Srrs					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3094163953Srrs					if (paddrp->spp_pathmaxrxt)
3095163953Srrs						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3096163953Srrs
3097163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3098163953Srrs						/* Turn back on the timer */
3099163953Srrs						stcb->asoc.hb_is_disabled = 0;
3100163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3101163953Srrs					}
3102163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3103163953Srrs						int cnt_of_unconf = 0;
3104163953Srrs						struct sctp_nets *lnet;
3105163953Srrs
3106163953Srrs						stcb->asoc.hb_is_disabled = 1;
3107163953Srrs						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3108163953Srrs							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3109163953Srrs								cnt_of_unconf++;
3110163953Srrs							}
3111163953Srrs						}
3112163953Srrs						/*
3113163953Srrs						 * stop the timer ONLY if we
3114163953Srrs						 * have no unconfirmed
3115163953Srrs						 * addresses
3116163953Srrs						 */
3117163953Srrs						if (cnt_of_unconf == 0) {
3118165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3119163953Srrs						}
3120163953Srrs					}
3121163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3122163953Srrs						/* start up the timer. */
3123163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3124163953Srrs					}
3125167598Srrs#ifdef INET
3126163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS)
3127163953Srrs						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3128163953Srrs#endif
3129167598Srrs#ifdef INET6
3130163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3131163953Srrs						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3132163953Srrs#endif
3133163953Srrs
3134163953Srrs				}
3135163953Srrs				SCTP_TCB_UNLOCK(stcb);
3136163953Srrs			} else {
3137163953Srrs				/************************NO TCB, SET TO default stuff ******************/
3138163953Srrs				SCTP_INP_WLOCK(inp);
3139163953Srrs				/*
3140163953Srrs				 * For the TOS/FLOWLABEL stuff you set it
3141163953Srrs				 * with the options on the socket
3142163953Srrs				 */
3143163953Srrs				if (paddrp->spp_pathmaxrxt) {
3144163953Srrs					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3145163953Srrs				}
3146163953Srrs				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3147163953Srrs					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3148163953Srrs					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3149163953Srrs				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3150163953Srrs					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3151163953Srrs				}
3152163953Srrs				SCTP_INP_WUNLOCK(inp);
3153163953Srrs			}
3154163953Srrs		}
3155163953Srrs		break;
3156163953Srrs	case SCTP_RTOINFO:
3157163953Srrs		{
3158163953Srrs			struct sctp_rtoinfo *srto;
3159163953Srrs
3160166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3161166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3162166675Srrs
3163166675Srrs			if (stcb) {
3164166675Srrs				/* Set in ms we hope :-) */
3165167598Srrs				if (srto->srto_initial)
3166166675Srrs					stcb->asoc.initial_rto = srto->srto_initial;
3167167598Srrs				if (srto->srto_max)
3168166675Srrs					stcb->asoc.maxrto = srto->srto_max;
3169167598Srrs				if (srto->srto_min)
3170166675Srrs					stcb->asoc.minrto = srto->srto_min;
3171166675Srrs				SCTP_TCB_UNLOCK(stcb);
3172166675Srrs			} else {
3173163953Srrs				SCTP_INP_WLOCK(inp);
3174163953Srrs				/*
3175163953Srrs				 * If we have a null asoc, its default for
3176163953Srrs				 * the endpoint
3177163953Srrs				 */
3178167598Srrs				if (srto->srto_initial)
3179163953Srrs					inp->sctp_ep.initial_rto = srto->srto_initial;
3180167598Srrs				if (srto->srto_max)
3181163953Srrs					inp->sctp_ep.sctp_maxrto = srto->srto_max;
3182167598Srrs				if (srto->srto_min)
3183163953Srrs					inp->sctp_ep.sctp_minrto = srto->srto_min;
3184163953Srrs				SCTP_INP_WUNLOCK(inp);
3185163953Srrs			}
3186163953Srrs		}
3187163953Srrs		break;
3188163953Srrs	case SCTP_ASSOCINFO:
3189163953Srrs		{
3190163953Srrs			struct sctp_assocparams *sasoc;
3191163953Srrs
3192166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3193166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3194166675Srrs
3195163953Srrs			if (stcb) {
3196163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3197163953Srrs					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3198163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3199163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3200163953Srrs				sasoc->sasoc_local_rwnd = 0;
3201163953Srrs				if (stcb->asoc.cookie_life)
3202163953Srrs					stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3203167598Srrs				stcb->asoc.delayed_ack = sasoc->sasoc_sack_delay;
3204167598Srrs				if (sasoc->sasoc_sack_freq) {
3205167598Srrs					stcb->asoc.sack_freq = sasoc->sasoc_sack_freq;
3206167598Srrs				}
3207163953Srrs				SCTP_TCB_UNLOCK(stcb);
3208163953Srrs			} else {
3209163953Srrs				SCTP_INP_WLOCK(inp);
3210163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3211163953Srrs					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3212163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
3213163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3214163953Srrs				sasoc->sasoc_local_rwnd = 0;
3215163953Srrs				if (sasoc->sasoc_cookie_life)
3216163953Srrs					inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3217167598Srrs				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sasoc->sasoc_sack_delay);
3218167598Srrs				if (sasoc->sasoc_sack_freq) {
3219167598Srrs					inp->sctp_ep.sctp_sack_freq = sasoc->sasoc_sack_freq;
3220167598Srrs				}
3221163953Srrs				SCTP_INP_WUNLOCK(inp);
3222163953Srrs			}
3223163953Srrs		}
3224163953Srrs		break;
3225163953Srrs	case SCTP_INITMSG:
3226163953Srrs		{
3227163953Srrs			struct sctp_initmsg *sinit;
3228163953Srrs
3229166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3230163953Srrs			SCTP_INP_WLOCK(inp);
3231163953Srrs			if (sinit->sinit_num_ostreams)
3232163953Srrs				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3233163953Srrs
3234163953Srrs			if (sinit->sinit_max_instreams)
3235163953Srrs				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3236163953Srrs
3237163953Srrs			if (sinit->sinit_max_attempts)
3238163953Srrs				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3239163953Srrs
3240167598Srrs			if (sinit->sinit_max_init_timeo)
3241163953Srrs				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3242163953Srrs			SCTP_INP_WUNLOCK(inp);
3243163953Srrs		}
3244163953Srrs		break;
3245163953Srrs	case SCTP_PRIMARY_ADDR:
3246163953Srrs		{
3247163953Srrs			struct sctp_setprim *spa;
3248163953Srrs			struct sctp_nets *net, *lnet;
3249163953Srrs
3250166675Srrs			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3251166675Srrs			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3252163953Srrs
3253166675Srrs			net = NULL;
3254166675Srrs			if (stcb) {
3255166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3256166675Srrs			} else {
3257166675Srrs				/*
3258166675Srrs				 * We increment here since
3259166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3260166675Srrs				 * decrement if it finds the stcb as long as
3261166675Srrs				 * the locked tcb (last argument) is NOT a
3262166675Srrs				 * TCB.. aka NULL.
3263166675Srrs				 */
3264163953Srrs				SCTP_INP_INCR_REF(inp);
3265163953Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3266163953Srrs				    (struct sockaddr *)&spa->ssp_addr,
3267163953Srrs				    &net, NULL, NULL);
3268163953Srrs				if (stcb == NULL) {
3269163953Srrs					SCTP_INP_DECR_REF(inp);
3270163953Srrs				}
3271163953Srrs			}
3272166675Srrs
3273166675Srrs			if ((stcb) && (net)) {
3274166675Srrs				if ((net != stcb->asoc.primary_destination) &&
3275166675Srrs				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3276166675Srrs					/* Ok we need to set it */
3277166675Srrs					lnet = stcb->asoc.primary_destination;
3278166675Srrs					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3279166675Srrs						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3280166675Srrs							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3281166675Srrs						}
3282166675Srrs						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3283163953Srrs					}
3284163953Srrs				}
3285166675Srrs			} else {
3286166675Srrs				error = EINVAL;
3287163953Srrs			}
3288166675Srrs			if (stcb) {
3289166675Srrs				SCTP_TCB_UNLOCK(stcb);
3290166675Srrs			}
3291163953Srrs		}
3292163953Srrs		break;
3293167598Srrs	case SCTP_SET_DYNAMIC_PRIMARY:
3294167598Srrs		{
3295167598Srrs			union sctp_sockstore *ss;
3296163953Srrs
3297167598Srrs			error = priv_check_cred(curthread->td_ucred,
3298167598Srrs			    PRIV_NETINET_RESERVEDPORT,
3299167598Srrs			    SUSER_ALLOWJAIL);
3300167598Srrs			if (error)
3301167598Srrs				break;
3302167598Srrs
3303167598Srrs			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3304167598Srrs			/* SUPER USER CHECK? */
3305167598Srrs			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3306167598Srrs		}
3307167598Srrs		break;
3308163953Srrs	case SCTP_SET_PEER_PRIMARY_ADDR:
3309163953Srrs		{
3310163953Srrs			struct sctp_setpeerprim *sspp;
3311163953Srrs
3312166675Srrs			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3313166675Srrs			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3314163953Srrs
3315166675Srrs			if (stcb) {
3316166675Srrs				if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3317166675Srrs					error = EINVAL;
3318166675Srrs				}
3319166675Srrs			} else {
3320163953Srrs				error = EINVAL;
3321163953Srrs			}
3322163953Srrs			SCTP_TCB_UNLOCK(stcb);
3323163953Srrs		}
3324163953Srrs		break;
3325163953Srrs	case SCTP_BINDX_ADD_ADDR:
3326163953Srrs		{
3327163953Srrs			struct sctp_getaddresses *addrs;
3328163953Srrs			struct sockaddr *addr_touse;
3329163953Srrs			struct sockaddr_in sin;
3330163953Srrs
3331166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3332166675Srrs
3333163953Srrs			/* see if we're bound all already! */
3334163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3335163953Srrs				error = EINVAL;
3336163953Srrs				break;
3337163953Srrs			}
3338167598Srrs			/* Is the VRF one we have */
3339163953Srrs			addr_touse = addrs->addr;
3340167695Srrs#if defined(INET6)
3341163953Srrs			if (addrs->addr->sa_family == AF_INET6) {
3342163953Srrs				struct sockaddr_in6 *sin6;
3343163953Srrs
3344163953Srrs				sin6 = (struct sockaddr_in6 *)addr_touse;
3345163953Srrs				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3346163953Srrs					in6_sin6_2_sin(&sin, sin6);
3347163953Srrs					addr_touse = (struct sockaddr *)&sin;
3348163953Srrs				}
3349163953Srrs			}
3350167695Srrs#endif
3351163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3352163953Srrs				if (p == NULL) {
3353163953Srrs					/* Can't get proc for Net/Open BSD */
3354163953Srrs					error = EINVAL;
3355163953Srrs					break;
3356163953Srrs				}
3357163953Srrs				error = sctp_inpcb_bind(so, addr_touse, p);
3358163953Srrs				break;
3359163953Srrs			}
3360163953Srrs			/*
3361163953Srrs			 * No locks required here since bind and mgmt_ep_sa
3362163953Srrs			 * all do their own locking. If we do something for
3363163953Srrs			 * the FIX: below we may need to lock in that case.
3364163953Srrs			 */
3365163953Srrs			if (addrs->sget_assoc_id == 0) {
3366163953Srrs				/* add the address */
3367163953Srrs				struct sctp_inpcb *lep;
3368163953Srrs
3369163953Srrs				((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3370167598Srrs				lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
3371163953Srrs				if (lep != NULL) {
3372163953Srrs					/*
3373163953Srrs					 * We must decrement the refcount
3374163953Srrs					 * since we have the ep already and
3375163953Srrs					 * are binding. No remove going on
3376163953Srrs					 * here.
3377163953Srrs					 */
3378163953Srrs					SCTP_INP_DECR_REF(inp);
3379163953Srrs				}
3380163953Srrs				if (lep == inp) {
3381163953Srrs					/* already bound to it.. ok */
3382163953Srrs					break;
3383163953Srrs				} else if (lep == NULL) {
3384163953Srrs					((struct sockaddr_in *)addr_touse)->sin_port = 0;
3385163953Srrs					error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3386167598Srrs					    SCTP_ADD_IP_ADDRESS, vrf_id);
3387163953Srrs				} else {
3388163953Srrs					error = EADDRNOTAVAIL;
3389163953Srrs				}
3390163953Srrs				if (error)
3391163953Srrs					break;
3392163953Srrs
3393163953Srrs			} else {
3394163953Srrs				/*
3395163953Srrs				 * FIX: decide whether we allow assoc based
3396163953Srrs				 * bindx
3397163953Srrs				 */
3398163953Srrs			}
3399163953Srrs		}
3400163953Srrs		break;
3401163953Srrs	case SCTP_BINDX_REM_ADDR:
3402163953Srrs		{
3403163953Srrs			struct sctp_getaddresses *addrs;
3404163953Srrs			struct sockaddr *addr_touse;
3405163953Srrs			struct sockaddr_in sin;
3406163953Srrs
3407166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3408163953Srrs			/* see if we're bound all already! */
3409163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3410163953Srrs				error = EINVAL;
3411163953Srrs				break;
3412163953Srrs			}
3413163953Srrs			addr_touse = addrs->addr;
3414167695Srrs#if defined(INET6)
3415163953Srrs			if (addrs->addr->sa_family == AF_INET6) {
3416163953Srrs				struct sockaddr_in6 *sin6;
3417163953Srrs
3418163953Srrs				sin6 = (struct sockaddr_in6 *)addr_touse;
3419163953Srrs				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3420163953Srrs					in6_sin6_2_sin(&sin, sin6);
3421163953Srrs					addr_touse = (struct sockaddr *)&sin;
3422163953Srrs				}
3423163953Srrs			}
3424167695Srrs#endif
3425163953Srrs			/*
3426163953Srrs			 * No lock required mgmt_ep_sa does its own locking.
3427163953Srrs			 * If the FIX: below is ever changed we may need to
3428163953Srrs			 * lock before calling association level binding.
3429163953Srrs			 */
3430163953Srrs			if (addrs->sget_assoc_id == 0) {
3431163953Srrs				/* delete the address */
3432163953Srrs				sctp_addr_mgmt_ep_sa(inp, addr_touse,
3433167598Srrs				    SCTP_DEL_IP_ADDRESS, vrf_id);
3434163953Srrs			} else {
3435163953Srrs				/*
3436163953Srrs				 * FIX: decide whether we allow assoc based
3437163953Srrs				 * bindx
3438163953Srrs				 */
3439163953Srrs			}
3440163953Srrs		}
3441163953Srrs		break;
3442163953Srrs	default:
3443163953Srrs		error = ENOPROTOOPT;
3444163953Srrs		break;
3445163953Srrs	}			/* end switch (opt) */
3446163953Srrs	return (error);
3447163953Srrs}
3448163953Srrs
3449163953Srrs
3450163953Srrsint
3451163953Srrssctp_ctloutput(struct socket *so, struct sockopt *sopt)
3452163953Srrs{
3453166675Srrs	void *optval = NULL;
3454166675Srrs	size_t optsize = 0;
3455163953Srrs	struct sctp_inpcb *inp;
3456166675Srrs	void *p;
3457166675Srrs	int error = 0;
3458163953Srrs
3459163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3460163953Srrs	if (inp == 0) {
3461163953Srrs		/* I made the same as TCP since we are not setup? */
3462163953Srrs		return (ECONNRESET);
3463163953Srrs	}
3464163953Srrs	if (sopt->sopt_level != IPPROTO_SCTP) {
3465163953Srrs		/* wrong proto level... send back up to IP */
3466163953Srrs#ifdef INET6
3467163953Srrs		if (INP_CHECK_SOCKAF(so, AF_INET6))
3468163953Srrs			error = ip6_ctloutput(so, sopt);
3469163953Srrs		else
3470163953Srrs#endif				/* INET6 */
3471163953Srrs			error = ip_ctloutput(so, sopt);
3472163953Srrs		return (error);
3473163953Srrs	}
3474166675Srrs	optsize = sopt->sopt_valsize;
3475166675Srrs	if (optsize) {
3476166675Srrs		SCTP_MALLOC(optval, void *, optsize, "SCTPSockOpt");
3477166675Srrs		if (optval == NULL) {
3478163953Srrs			return (ENOBUFS);
3479163953Srrs		}
3480166675Srrs		error = sooptcopyin(sopt, optval, optsize, optsize);
3481163953Srrs		if (error) {
3482166675Srrs			SCTP_FREE(optval);
3483163953Srrs			goto out;
3484163953Srrs		}
3485163953Srrs	}
3486166675Srrs	p = (void *)sopt->sopt_td;
3487163953Srrs	if (sopt->sopt_dir == SOPT_SET) {
3488166675Srrs		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3489163953Srrs	} else if (sopt->sopt_dir == SOPT_GET) {
3490166675Srrs		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3491163953Srrs	} else {
3492163953Srrs		error = EINVAL;
3493163953Srrs	}
3494166675Srrs	if ((error == 0) && (optval != NULL)) {
3495166675Srrs		error = sooptcopyout(sopt, optval, optsize);
3496166675Srrs		SCTP_FREE(optval);
3497166675Srrs	} else if (optval != NULL) {
3498166675Srrs		SCTP_FREE(optval);
3499163953Srrs	}
3500163953Srrsout:
3501163953Srrs	return (error);
3502163953Srrs}
3503163953Srrs
3504163953Srrs
3505163953Srrsstatic int
3506163953Srrssctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3507163953Srrs{
3508163953Srrs	int error = 0;
3509163953Srrs	int create_lock_on = 0;
3510167598Srrs	uint32_t vrf_id;
3511163953Srrs	struct sctp_inpcb *inp;
3512163953Srrs	struct sctp_tcb *stcb = NULL;
3513163953Srrs
3514163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3515163953Srrs	if (inp == 0) {
3516163953Srrs		/* I made the same as TCP since we are not setup? */
3517163953Srrs		return (ECONNRESET);
3518163953Srrs	}
3519163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
3520163953Srrs	create_lock_on = 1;
3521163953Srrs
3522163953Srrs	SCTP_INP_INCR_REF(inp);
3523163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3524163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3525163953Srrs		/* Should I really unlock ? */
3526163953Srrs		error = EFAULT;
3527163953Srrs		goto out_now;
3528163953Srrs	}
3529163953Srrs#ifdef INET6
3530163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3531163953Srrs	    (addr->sa_family == AF_INET6)) {
3532163953Srrs		error = EINVAL;
3533163953Srrs		goto out_now;
3534163953Srrs	}
3535163953Srrs#endif				/* INET6 */
3536163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3537163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
3538163953Srrs		/* Bind a ephemeral port */
3539163953Srrs		error = sctp_inpcb_bind(so, NULL, p);
3540163953Srrs		if (error) {
3541163953Srrs			goto out_now;
3542163953Srrs		}
3543163953Srrs	}
3544163953Srrs	/* Now do we connect? */
3545163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3546163953Srrs		error = EINVAL;
3547163953Srrs		goto out_now;
3548163953Srrs	}
3549163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3550163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3551163953Srrs		/* We are already connected AND the TCP model */
3552163953Srrs		error = EADDRINUSE;
3553163953Srrs		goto out_now;
3554163953Srrs	}
3555163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3556163953Srrs		SCTP_INP_RLOCK(inp);
3557163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3558163953Srrs		if (stcb)
3559163953Srrs			SCTP_TCB_UNLOCK(stcb);
3560163953Srrs		SCTP_INP_RUNLOCK(inp);
3561163953Srrs	} else {
3562163953Srrs		/*
3563166675Srrs		 * We increment here since sctp_findassociation_ep_addr()
3564166675Srrs		 * wil do a decrement if it finds the stcb as long as the
3565166675Srrs		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3566163953Srrs		 */
3567163953Srrs		SCTP_INP_INCR_REF(inp);
3568163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3569163953Srrs		if (stcb == NULL) {
3570163953Srrs			SCTP_INP_DECR_REF(inp);
3571163953Srrs		}
3572163953Srrs	}
3573163953Srrs	if (stcb != NULL) {
3574163953Srrs		/* Already have or am bring up an association */
3575163953Srrs		error = EALREADY;
3576163953Srrs		goto out_now;
3577163953Srrs	}
3578167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
3579163953Srrs	/* We are GOOD to go */
3580167598Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3581163953Srrs	if (stcb == NULL) {
3582163953Srrs		/* Gak! no memory */
3583167598Srrs		goto out_now;
3584163953Srrs	}
3585163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3586163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3587163953Srrs		/* Set the connected flag so we can queue data */
3588163953Srrs		soisconnecting(so);
3589163953Srrs	}
3590163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3591163953Srrs	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3592163953Srrs
3593163953Srrs	/* initialize authentication parameters for the assoc */
3594163953Srrs	sctp_initialize_auth_params(inp, stcb);
3595163953Srrs
3596163953Srrs	sctp_send_initiate(inp, stcb);
3597163953Srrsout_now:
3598163953Srrs	if (create_lock_on)
3599163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
3600163953Srrs
3601163953Srrs	if (stcb)
3602163953Srrs		SCTP_TCB_UNLOCK(stcb);
3603163953Srrs	SCTP_INP_DECR_REF(inp);
3604163953Srrs	return error;
3605163953Srrs}
3606163953Srrs
3607163953Srrsint
3608163953Srrssctp_listen(struct socket *so, int backlog, struct thread *p)
3609163953Srrs{
3610163953Srrs	/*
3611163953Srrs	 * Note this module depends on the protocol processing being called
3612163953Srrs	 * AFTER any socket level flags and backlog are applied to the
3613163953Srrs	 * socket. The traditional way that the socket flags are applied is
3614163953Srrs	 * AFTER protocol processing. We have made a change to the
3615163953Srrs	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3616163953Srrs	 * place if the socket API for SCTP is to work properly.
3617163953Srrs	 */
3618163953Srrs
3619163953Srrs	int error = 0;
3620163953Srrs	struct sctp_inpcb *inp;
3621163953Srrs
3622163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3623163953Srrs	if (inp == 0) {
3624163953Srrs		/* I made the same as TCP since we are not setup? */
3625163953Srrs		return (ECONNRESET);
3626163953Srrs	}
3627163953Srrs	SCTP_INP_RLOCK(inp);
3628163953Srrs#ifdef SCTP_LOCK_LOGGING
3629163953Srrs	sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3630163953Srrs#endif
3631163953Srrs	SOCK_LOCK(so);
3632163953Srrs	error = solisten_proto_check(so);
3633163953Srrs	if (error) {
3634163953Srrs		SOCK_UNLOCK(so);
3635163953Srrs		return (error);
3636163953Srrs	}
3637163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3638163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3639163953Srrs		/* We are already connected AND the TCP model */
3640163953Srrs		SCTP_INP_RUNLOCK(inp);
3641163953Srrs		SOCK_UNLOCK(so);
3642163953Srrs		return (EADDRINUSE);
3643163953Srrs	}
3644163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3645163953Srrs		/* We must do a bind. */
3646166675Srrs		SOCK_UNLOCK(so);
3647163953Srrs		SCTP_INP_RUNLOCK(inp);
3648163953Srrs		if ((error = sctp_inpcb_bind(so, NULL, p))) {
3649163953Srrs			/* bind error, probably perm */
3650163953Srrs			return (error);
3651163953Srrs		}
3652166675Srrs		SOCK_LOCK(so);
3653163953Srrs	} else {
3654163953Srrs		SCTP_INP_RUNLOCK(inp);
3655163953Srrs	}
3656163953Srrs	/* It appears for 7.0 and on, we must always call this. */
3657163953Srrs	solisten_proto(so, backlog);
3658163953Srrs
3659163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3660163953Srrs		/* remove the ACCEPTCONN flag for one-to-many sockets */
3661163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3662163953Srrs	}
3663163953Srrs	if (backlog == 0) {
3664163953Srrs		/* turning off listen */
3665163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3666163953Srrs	}
3667163953Srrs	SOCK_UNLOCK(so);
3668163953Srrs	return (error);
3669163953Srrs}
3670163953Srrs
3671163953Srrsstatic int sctp_defered_wakeup_cnt = 0;
3672163953Srrs
3673163953Srrsint
3674163953Srrssctp_accept(struct socket *so, struct sockaddr **addr)
3675163953Srrs{
3676163953Srrs	struct sctp_tcb *stcb;
3677163953Srrs	struct sctp_inpcb *inp;
3678163953Srrs	union sctp_sockstore store;
3679163953Srrs
3680163953Srrs	int error;
3681163953Srrs
3682163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3683163953Srrs
3684163953Srrs	if (inp == 0) {
3685163953Srrs		return (ECONNRESET);
3686163953Srrs	}
3687163953Srrs	SCTP_INP_RLOCK(inp);
3688163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3689163953Srrs		return (ENOTSUP);
3690163953Srrs	}
3691163953Srrs	if (so->so_state & SS_ISDISCONNECTED) {
3692163953Srrs		SCTP_INP_RUNLOCK(inp);
3693163953Srrs		return (ECONNABORTED);
3694163953Srrs	}
3695163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3696163953Srrs	if (stcb == NULL) {
3697163953Srrs		SCTP_INP_RUNLOCK(inp);
3698163953Srrs		return (ECONNRESET);
3699163953Srrs	}
3700163953Srrs	SCTP_TCB_LOCK(stcb);
3701163953Srrs	SCTP_INP_RUNLOCK(inp);
3702163953Srrs	store = stcb->asoc.primary_destination->ro._l_addr;
3703163953Srrs	SCTP_TCB_UNLOCK(stcb);
3704163953Srrs	if (store.sa.sa_family == AF_INET) {
3705163953Srrs		struct sockaddr_in *sin;
3706163953Srrs
3707163953Srrs		SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3708163953Srrs		sin->sin_family = AF_INET;
3709163953Srrs		sin->sin_len = sizeof(*sin);
3710163953Srrs		sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3711163953Srrs		sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3712163953Srrs		*addr = (struct sockaddr *)sin;
3713163953Srrs	} else {
3714163953Srrs		struct sockaddr_in6 *sin6;
3715163953Srrs
3716163953Srrs		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3717163953Srrs		sin6->sin6_family = AF_INET6;
3718163953Srrs		sin6->sin6_len = sizeof(*sin6);
3719163953Srrs		sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3720163953Srrs
3721163953Srrs		sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3722164085Srrs		if ((error = sa6_recoverscope(sin6)) != 0) {
3723164085Srrs			SCTP_FREE_SONAME(sin6);
3724163953Srrs			return (error);
3725164085Srrs		}
3726163953Srrs		*addr = (struct sockaddr *)sin6;
3727163953Srrs	}
3728163953Srrs	/* Wake any delayed sleep action */
3729163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3730166086Srrs		SCTP_INP_WLOCK(inp);
3731163953Srrs		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3732163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3733163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3734166086Srrs			SCTP_INP_WUNLOCK(inp);
3735163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3736163953Srrs			if (sowriteable(inp->sctp_socket)) {
3737163953Srrs				sowwakeup_locked(inp->sctp_socket);
3738163953Srrs			} else {
3739163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3740163953Srrs			}
3741166086Srrs			SCTP_INP_WLOCK(inp);
3742163953Srrs		}
3743163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3744163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3745166086Srrs			SCTP_INP_WUNLOCK(inp);
3746163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3747163953Srrs			if (soreadable(inp->sctp_socket)) {
3748163953Srrs				sctp_defered_wakeup_cnt++;
3749163953Srrs				sorwakeup_locked(inp->sctp_socket);
3750163953Srrs			} else {
3751163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3752163953Srrs			}
3753166086Srrs			SCTP_INP_WLOCK(inp);
3754163953Srrs		}
3755166086Srrs		SCTP_INP_WUNLOCK(inp);
3756163953Srrs	}
3757163953Srrs	return (0);
3758163953Srrs}
3759163953Srrs
3760163953Srrsint
3761163953Srrssctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3762163953Srrs{
3763163953Srrs	struct sockaddr_in *sin;
3764167598Srrs	uint32_t vrf_id;
3765163953Srrs	struct sctp_inpcb *inp;
3766167695Srrs	struct sctp_ifa *sctp_ifa;
3767163953Srrs
3768163953Srrs	/*
3769163953Srrs	 * Do the malloc first in case it blocks.
3770163953Srrs	 */
3771163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3772163953Srrs	sin->sin_family = AF_INET;
3773163953Srrs	sin->sin_len = sizeof(*sin);
3774163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3775163953Srrs	if (!inp) {
3776163953Srrs		SCTP_FREE_SONAME(sin);
3777163953Srrs		return ECONNRESET;
3778163953Srrs	}
3779163953Srrs	SCTP_INP_RLOCK(inp);
3780163953Srrs	sin->sin_port = inp->sctp_lport;
3781163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3782163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3783163953Srrs			struct sctp_tcb *stcb;
3784163953Srrs			struct sockaddr_in *sin_a;
3785163953Srrs			struct sctp_nets *net;
3786163953Srrs			int fnd;
3787163953Srrs
3788163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3789163953Srrs			if (stcb == NULL) {
3790163953Srrs				goto notConn;
3791163953Srrs			}
3792163953Srrs			fnd = 0;
3793163953Srrs			sin_a = NULL;
3794163953Srrs			SCTP_TCB_LOCK(stcb);
3795163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3796163953Srrs				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3797164085Srrs				if (sin_a == NULL)
3798164085Srrs					/* this will make coverity happy */
3799164085Srrs					continue;
3800164085Srrs
3801163953Srrs				if (sin_a->sin_family == AF_INET) {
3802163953Srrs					fnd = 1;
3803163953Srrs					break;
3804163953Srrs				}
3805163953Srrs			}
3806163953Srrs			if ((!fnd) || (sin_a == NULL)) {
3807163953Srrs				/* punt */
3808163953Srrs				SCTP_TCB_UNLOCK(stcb);
3809163953Srrs				goto notConn;
3810163953Srrs			}
3811167598Srrs			vrf_id = SCTP_DEFAULT_VRFID;
3812167598Srrs
3813167598Srrs			sctp_ifa = sctp_source_address_selection(inp,
3814167598Srrs			    stcb,
3815167598Srrs			    (struct route *)&net->ro,
3816167598Srrs			    net, 0, vrf_id);
3817167598Srrs			if (sctp_ifa) {
3818167598Srrs				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
3819167598Srrs				sctp_free_ifa(sctp_ifa);
3820167598Srrs			}
3821163953Srrs			SCTP_TCB_UNLOCK(stcb);
3822163953Srrs		} else {
3823163953Srrs			/* For the bound all case you get back 0 */
3824163953Srrs	notConn:
3825163953Srrs			sin->sin_addr.s_addr = 0;
3826163953Srrs		}
3827163953Srrs
3828163953Srrs	} else {
3829163953Srrs		/* Take the first IPv4 address in the list */
3830163953Srrs		struct sctp_laddr *laddr;
3831163953Srrs		int fnd = 0;
3832163953Srrs
3833163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3834167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
3835163953Srrs				struct sockaddr_in *sin_a;
3836163953Srrs
3837167598Srrs				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
3838163953Srrs				sin->sin_addr = sin_a->sin_addr;
3839163953Srrs				fnd = 1;
3840163953Srrs				break;
3841163953Srrs			}
3842163953Srrs		}
3843163953Srrs		if (!fnd) {
3844163953Srrs			SCTP_FREE_SONAME(sin);
3845163953Srrs			SCTP_INP_RUNLOCK(inp);
3846163953Srrs			return ENOENT;
3847163953Srrs		}
3848163953Srrs	}
3849163953Srrs	SCTP_INP_RUNLOCK(inp);
3850163953Srrs	(*addr) = (struct sockaddr *)sin;
3851163953Srrs	return (0);
3852163953Srrs}
3853163953Srrs
3854163953Srrsint
3855163953Srrssctp_peeraddr(struct socket *so, struct sockaddr **addr)
3856163953Srrs{
3857163953Srrs	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
3858166086Srrs	int fnd;
3859163953Srrs	struct sockaddr_in *sin_a;
3860163953Srrs	struct sctp_inpcb *inp;
3861163953Srrs	struct sctp_tcb *stcb;
3862163953Srrs	struct sctp_nets *net;
3863163953Srrs
3864163953Srrs	/* Do the malloc first in case it blocks. */
3865163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3866163953Srrs	if ((inp == NULL) ||
3867163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3868163953Srrs		/* UDP type and listeners will drop out here */
3869163953Srrs		return (ENOTCONN);
3870163953Srrs	}
3871163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3872163953Srrs	sin->sin_family = AF_INET;
3873163953Srrs	sin->sin_len = sizeof(*sin);
3874163953Srrs
3875163953Srrs	/* We must recapture incase we blocked */
3876163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3877163953Srrs	if (!inp) {
3878163953Srrs		SCTP_FREE_SONAME(sin);
3879163953Srrs		return ECONNRESET;
3880163953Srrs	}
3881163953Srrs	SCTP_INP_RLOCK(inp);
3882163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3883163953Srrs	if (stcb)
3884163953Srrs		SCTP_TCB_LOCK(stcb);
3885163953Srrs	SCTP_INP_RUNLOCK(inp);
3886163953Srrs	if (stcb == NULL) {
3887163953Srrs		SCTP_FREE_SONAME(sin);
3888163953Srrs		return ECONNRESET;
3889163953Srrs	}
3890163953Srrs	fnd = 0;
3891163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3892163953Srrs		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3893163953Srrs		if (sin_a->sin_family == AF_INET) {
3894163953Srrs			fnd = 1;
3895163953Srrs			sin->sin_port = stcb->rport;
3896163953Srrs			sin->sin_addr = sin_a->sin_addr;
3897163953Srrs			break;
3898163953Srrs		}
3899163953Srrs	}
3900163953Srrs	SCTP_TCB_UNLOCK(stcb);
3901163953Srrs	if (!fnd) {
3902163953Srrs		/* No IPv4 address */
3903163953Srrs		SCTP_FREE_SONAME(sin);
3904163953Srrs		return ENOENT;
3905163953Srrs	}
3906163953Srrs	(*addr) = (struct sockaddr *)sin;
3907163953Srrs	return (0);
3908163953Srrs}
3909163953Srrs
3910163953Srrsstruct pr_usrreqs sctp_usrreqs = {
3911163953Srrs	.pru_abort = sctp_abort,
3912163953Srrs	.pru_accept = sctp_accept,
3913163953Srrs	.pru_attach = sctp_attach,
3914163953Srrs	.pru_bind = sctp_bind,
3915163953Srrs	.pru_connect = sctp_connect,
3916163953Srrs	.pru_control = in_control,
3917163953Srrs	.pru_close = sctp_close,
3918163953Srrs	.pru_detach = sctp_close,
3919163953Srrs	.pru_sopoll = sopoll_generic,
3920163953Srrs	.pru_disconnect = sctp_disconnect,
3921163953Srrs	.pru_listen = sctp_listen,
3922163953Srrs	.pru_peeraddr = sctp_peeraddr,
3923163953Srrs	.pru_send = sctp_sendm,
3924163953Srrs	.pru_shutdown = sctp_shutdown,
3925163953Srrs	.pru_sockaddr = sctp_ingetaddr,
3926163953Srrs	.pru_sosend = sctp_sosend,
3927163953Srrs	.pru_soreceive = sctp_soreceive
3928163953Srrs};
3929