sctp_usrreq.c revision 167695
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 167695 2007-03-19 06:53:02Z 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
999163953Srrssctp_fill_up_addresses(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
1156163953Srrsstatic int
1157167598Srrssctp_count_max_addresses(struct sctp_inpcb *inp, uint32_t vrf_id)
1158163953Srrs{
1159163953Srrs	int cnt = 0;
1160167598Srrs	struct sctp_vrf *vrf = NULL;
1161163953Srrs
1162163953Srrs	/*
1163163953Srrs	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1164163953Srrs	 * number of addresses that you COULD get. In reality the sub-set
1165163953Srrs	 * bound may have an exclusion list for a given TCB OR in the
1166163953Srrs	 * bound-all case a TCB may NOT include the loopback or other
1167163953Srrs	 * addresses as well.
1168163953Srrs	 */
1169167598Srrs	vrf = sctp_find_vrf(vrf_id);
1170167598Srrs	if (vrf == NULL) {
1171167598Srrs		return (0);
1172167598Srrs	}
1173163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1174167598Srrs		struct sctp_ifn *sctp_ifn;
1175167598Srrs		struct sctp_ifa *sctp_ifa;
1176163953Srrs
1177167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1178167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1179163953Srrs				/* Count them if they are the right type */
1180167598Srrs				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1181163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1182163953Srrs						cnt += sizeof(struct sockaddr_in6);
1183163953Srrs					else
1184163953Srrs						cnt += sizeof(struct sockaddr_in);
1185163953Srrs
1186167598Srrs				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1187163953Srrs					cnt += sizeof(struct sockaddr_in6);
1188163953Srrs			}
1189163953Srrs		}
1190163953Srrs	} else {
1191163953Srrs		struct sctp_laddr *laddr;
1192163953Srrs
1193163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1194167598Srrs			if (laddr->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 (laddr->ifa->address.sa.sa_family == AF_INET6)
1201163953Srrs				cnt += sizeof(struct sockaddr_in6);
1202163953Srrs		}
1203163953Srrs	}
1204163953Srrs	return (cnt);
1205163953Srrs}
1206163953Srrs
1207166675Srrs
1208163953Srrsstatic int
1209166675Srrssctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1210166675Srrs    size_t optsize, void *p, int delay)
1211163953Srrs{
1212163953Srrs	int error = 0;
1213163953Srrs	int creat_lock_on = 0;
1214163953Srrs	struct sctp_tcb *stcb = NULL;
1215163953Srrs	struct sockaddr *sa;
1216166675Srrs	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr, i;
1217166675Srrs	size_t incr, at;
1218167598Srrs	uint32_t vrf_id;
1219167598Srrs	sctp_assoc_t *a_id;
1220163953Srrs
1221163953Srrs#ifdef SCTP_DEBUG
1222163953Srrs	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1223163953Srrs		printf("Connectx called\n");
1224163953Srrs	}
1225163953Srrs#endif				/* SCTP_DEBUG */
1226163953Srrs
1227163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1228163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1229163953Srrs		/* We are already connected AND the TCP model */
1230163953Srrs		return (EADDRINUSE);
1231163953Srrs	}
1232163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1233163953Srrs		return (EINVAL);
1234163953Srrs	}
1235163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1236163953Srrs		SCTP_INP_RLOCK(inp);
1237163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1238163953Srrs		SCTP_INP_RUNLOCK(inp);
1239163953Srrs	}
1240163953Srrs	if (stcb) {
1241163953Srrs		return (EALREADY);
1242163953Srrs	}
1243163953Srrs	SCTP_INP_INCR_REF(inp);
1244163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
1245163953Srrs	creat_lock_on = 1;
1246163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1247163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1248163953Srrs		error = EFAULT;
1249163953Srrs		goto out_now;
1250163953Srrs	}
1251166675Srrs	totaddrp = (int *)optval;
1252163953Srrs	totaddr = *totaddrp;
1253163953Srrs	sa = (struct sockaddr *)(totaddrp + 1);
1254163953Srrs	at = incr = 0;
1255163953Srrs	/* account and validate addresses */
1256163953Srrs	for (i = 0; i < totaddr; i++) {
1257163953Srrs		if (sa->sa_family == AF_INET) {
1258163953Srrs			num_v4++;
1259163953Srrs			incr = sizeof(struct sockaddr_in);
1260163953Srrs		} else if (sa->sa_family == AF_INET6) {
1261163953Srrs			struct sockaddr_in6 *sin6;
1262163953Srrs
1263163953Srrs			sin6 = (struct sockaddr_in6 *)sa;
1264163953Srrs			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1265163953Srrs				/* Must be non-mapped for connectx */
1266163953Srrs				error = EINVAL;
1267163953Srrs				goto out_now;
1268163953Srrs			}
1269163953Srrs			num_v6++;
1270163953Srrs			incr = sizeof(struct sockaddr_in6);
1271163953Srrs		} else {
1272163953Srrs			totaddr = i;
1273163953Srrs			break;
1274163953Srrs		}
1275163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1276163953Srrs		if (stcb != NULL) {
1277163953Srrs			/* Already have or am bring up an association */
1278163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1279163953Srrs			creat_lock_on = 0;
1280163953Srrs			SCTP_TCB_UNLOCK(stcb);
1281163953Srrs			error = EALREADY;
1282163953Srrs			goto out_now;
1283163953Srrs		}
1284166675Srrs		if ((at + incr) > optsize) {
1285163953Srrs			totaddr = i;
1286163953Srrs			break;
1287163953Srrs		}
1288163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + incr);
1289163953Srrs	}
1290163953Srrs	sa = (struct sockaddr *)(totaddrp + 1);
1291163953Srrs#ifdef INET6
1292163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1293163953Srrs	    (num_v6 > 0)) {
1294163953Srrs		error = EINVAL;
1295163953Srrs		goto out_now;
1296163953Srrs	}
1297163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1298163953Srrs	    (num_v4 > 0)) {
1299163953Srrs		struct in6pcb *inp6;
1300163953Srrs
1301163953Srrs		inp6 = (struct in6pcb *)inp;
1302166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1303163953Srrs			/*
1304163953Srrs			 * if IPV6_V6ONLY flag, ignore connections destined
1305163953Srrs			 * to a v4 addr or v4-mapped addr
1306163953Srrs			 */
1307163953Srrs			error = EINVAL;
1308163953Srrs			goto out_now;
1309163953Srrs		}
1310163953Srrs	}
1311163953Srrs#endif				/* INET6 */
1312163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1313163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
1314163953Srrs		/* Bind a ephemeral port */
1315163953Srrs		SCTP_INP_WUNLOCK(inp);
1316163953Srrs		error = sctp_inpcb_bind(so, NULL, p);
1317163953Srrs		if (error) {
1318163953Srrs			goto out_now;
1319163953Srrs		}
1320163953Srrs	} else {
1321163953Srrs		SCTP_INP_WUNLOCK(inp);
1322163953Srrs	}
1323163953Srrs
1324167695Srrs	/* FIX ME: do we want to pass in a vrf on the connect call? */
1325167695Srrs	vrf_id = inp->def_vrf_id;
1326167695Srrs
1327163953Srrs	/* We are GOOD to go */
1328167598Srrs	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1329163953Srrs	if (stcb == NULL) {
1330163953Srrs		/* Gak! no memory */
1331163953Srrs		goto out_now;
1332163953Srrs	}
1333163953Srrs	/* move to second address */
1334163953Srrs	if (sa->sa_family == AF_INET)
1335163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1336163953Srrs	else
1337163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1338163953Srrs
1339163953Srrs	for (i = 1; i < totaddr; i++) {
1340163953Srrs		if (sa->sa_family == AF_INET) {
1341163953Srrs			incr = sizeof(struct sockaddr_in);
1342165220Srrs			if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
1343163953Srrs				/* assoc gone no un-lock */
1344165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1345163953Srrs				error = ENOBUFS;
1346163953Srrs				goto out_now;
1347163953Srrs			}
1348163953Srrs		} else if (sa->sa_family == AF_INET6) {
1349163953Srrs			incr = sizeof(struct sockaddr_in6);
1350165220Srrs			if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
1351163953Srrs				/* assoc gone no un-lock */
1352165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
1353163953Srrs				error = ENOBUFS;
1354163953Srrs				goto out_now;
1355163953Srrs			}
1356163953Srrs		}
1357163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + incr);
1358163953Srrs	}
1359163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1360167598Srrs	/* Fill in the return id */
1361167598Srrs	a_id = (sctp_assoc_t *) optval;
1362167598Srrs	*a_id = sctp_get_associd(stcb);
1363163953Srrs
1364163953Srrs	/* initialize authentication parameters for the assoc */
1365163953Srrs	sctp_initialize_auth_params(inp, stcb);
1366163953Srrs
1367163953Srrs	if (delay) {
1368163953Srrs		/* doing delayed connection */
1369163953Srrs		stcb->asoc.delayed_connection = 1;
1370163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1371163953Srrs	} else {
1372163953Srrs		SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1373163953Srrs		sctp_send_initiate(inp, stcb);
1374163953Srrs	}
1375163953Srrs	SCTP_TCB_UNLOCK(stcb);
1376163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1377163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1378163953Srrs		/* Set the connected flag so we can queue data */
1379163953Srrs		soisconnecting(so);
1380163953Srrs	}
1381163953Srrsout_now:
1382163953Srrs	if (creat_lock_on)
1383163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1384163953Srrs	SCTP_INP_DECR_REF(inp);
1385163953Srrs	return error;
1386163953Srrs}
1387163953Srrs
1388166675Srrs#define SCTP_FIND_STCB(inp, stcb, assoc_id) \
1389166675Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { \
1390166675Srrs		SCTP_INP_RLOCK(inp); \
1391166675Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1392166675Srrs		if (stcb) \
1393166675Srrs			SCTP_TCB_LOCK(stcb); \
1394166675Srrs		SCTP_INP_RUNLOCK(inp); \
1395166675Srrs	} else if (assoc_id != 0) { \
1396166675Srrs		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1397166675Srrs		if (stcb == NULL) { \
1398166675Srrs			error = ENOENT; \
1399166675Srrs			break; \
1400166675Srrs		} \
1401166675Srrs	} else { \
1402166675Srrs		stcb = NULL; \
1403166675Srrs	}
1404163953Srrs
1405166675Srrs#define SCTP_CHECK_AND_CAST(destp, srcp, type, size) \
1406166675Srrs	if (size < sizeof(type)) { \
1407166675Srrs		error = EINVAL; \
1408166675Srrs		break; \
1409166675Srrs	} else { \
1410166675Srrs		destp = (type *)srcp; \
1411166675Srrs	}
1412163953Srrs
1413163953Srrsstatic int
1414166675Srrssctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1415166675Srrs    void *p)
1416163953Srrs{
1417163953Srrs	struct sctp_inpcb *inp;
1418166675Srrs	int error, val = 0;
1419167598Srrs	uint32_t vrf_id;
1420163953Srrs	struct sctp_tcb *stcb = NULL;
1421163953Srrs
1422166675Srrs	if (optval == NULL) {
1423166675Srrs		return (EINVAL);
1424166675Srrs	}
1425163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1426163953Srrs	if (inp == 0)
1427163953Srrs		return EINVAL;
1428167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
1429167598Srrs
1430163953Srrs	error = 0;
1431163953Srrs
1432166675Srrs	switch (optname) {
1433163953Srrs	case SCTP_NODELAY:
1434163953Srrs	case SCTP_AUTOCLOSE:
1435163953Srrs	case SCTP_EXPLICIT_EOR:
1436163953Srrs	case SCTP_AUTO_ASCONF:
1437163953Srrs	case SCTP_DISABLE_FRAGMENTS:
1438163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
1439163953Srrs	case SCTP_USE_EXT_RCVINFO:
1440163953Srrs		SCTP_INP_RLOCK(inp);
1441166675Srrs		switch (optname) {
1442163953Srrs		case SCTP_DISABLE_FRAGMENTS:
1443166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1444163953Srrs			break;
1445163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
1446166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1447163953Srrs			break;
1448163953Srrs		case SCTP_AUTO_ASCONF:
1449166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1450163953Srrs			break;
1451163953Srrs		case SCTP_EXPLICIT_EOR:
1452166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1453163953Srrs			break;
1454163953Srrs		case SCTP_NODELAY:
1455166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1456163953Srrs			break;
1457163953Srrs		case SCTP_USE_EXT_RCVINFO:
1458166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1459163953Srrs			break;
1460163953Srrs		case SCTP_AUTOCLOSE:
1461163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1462166675Srrs				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1463163953Srrs			else
1464166675Srrs				val = 0;
1465163953Srrs			break;
1466163953Srrs
1467163953Srrs		default:
1468163953Srrs			error = ENOPROTOOPT;
1469163953Srrs		}		/* end switch (sopt->sopt_name) */
1470166675Srrs		if (optname != SCTP_AUTOCLOSE) {
1471163953Srrs			/* make it an "on/off" value */
1472166675Srrs			val = (val != 0);
1473163953Srrs		}
1474166675Srrs		if (*optsize < sizeof(val)) {
1475163953Srrs			error = EINVAL;
1476163953Srrs		}
1477163953Srrs		SCTP_INP_RUNLOCK(inp);
1478163953Srrs		if (error == 0) {
1479163953Srrs			/* return the option value */
1480166675Srrs			*(int *)optval = val;
1481166675Srrs			*optsize = sizeof(val);
1482163953Srrs		}
1483163953Srrs		break;
1484167598Srrs
1485163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
1486163953Srrs		{
1487166675Srrs			uint32_t *value;
1488166675Srrs
1489166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1490166675Srrs			*value = inp->partial_delivery_point;
1491166675Srrs			*optsize = sizeof(uint32_t);
1492163953Srrs		}
1493163953Srrs		break;
1494163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
1495163953Srrs		{
1496166675Srrs			uint32_t *value;
1497166675Srrs
1498166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1499166675Srrs			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
1500166675Srrs			*optsize = sizeof(uint32_t);
1501163953Srrs		}
1502163953Srrs		break;
1503163953Srrs	case SCTP_CMT_ON_OFF:
1504163953Srrs		{
1505166675Srrs			struct sctp_assoc_value *av;
1506166675Srrs
1507166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1508166675Srrs			if (sctp_cmt_on_off) {
1509166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1510166675Srrs				if (stcb) {
1511166675Srrs					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1512166675Srrs					SCTP_TCB_UNLOCK(stcb);
1513166675Srrs
1514166675Srrs				} else {
1515166675Srrs					error = ENOTCONN;
1516166675Srrs				}
1517166675Srrs			} else {
1518166675Srrs				error = ENOPROTOOPT;
1519163953Srrs			}
1520166675Srrs			*optsize = sizeof(*av);
1521163953Srrs		}
1522163953Srrs		break;
1523163953Srrs	case SCTP_GET_ADDR_LEN:
1524163953Srrs		{
1525163953Srrs			struct sctp_assoc_value *av;
1526163953Srrs
1527166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1528163953Srrs			error = EINVAL;
1529167598Srrs#ifdef INET
1530163953Srrs			if (av->assoc_value == AF_INET) {
1531163953Srrs				av->assoc_value = sizeof(struct sockaddr_in);
1532163953Srrs				error = 0;
1533163953Srrs			}
1534163953Srrs#endif
1535167598Srrs#ifdef INET6
1536163953Srrs			if (av->assoc_value == AF_INET6) {
1537163953Srrs				av->assoc_value = sizeof(struct sockaddr_in6);
1538163953Srrs				error = 0;
1539163953Srrs			}
1540163953Srrs#endif
1541166675Srrs			*optsize = sizeof(*av);
1542163953Srrs		}
1543163953Srrs		break;
1544163953Srrs	case SCTP_GET_ASOC_ID_LIST:
1545163953Srrs		{
1546163953Srrs			struct sctp_assoc_ids *ids;
1547163953Srrs			int cnt, at;
1548163953Srrs			uint16_t orig;
1549163953Srrs
1550166675Srrs			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1551163953Srrs			cnt = 0;
1552163953Srrs			SCTP_INP_RLOCK(inp);
1553163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1554163953Srrs			if (stcb == NULL) {
1555163953Srrs		none_out_now:
1556163953Srrs				ids->asls_numb_present = 0;
1557163953Srrs				ids->asls_more_to_get = 0;
1558163953Srrs				SCTP_INP_RUNLOCK(inp);
1559163953Srrs				break;
1560163953Srrs			}
1561163953Srrs			orig = ids->asls_assoc_start;
1562163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1563163953Srrs			while (orig) {
1564163953Srrs				stcb = LIST_NEXT(stcb, sctp_tcblist);
1565163953Srrs				orig--;
1566163953Srrs				cnt--;
1567163953Srrs				if (stcb == NULL)
1568163953Srrs					goto none_out_now;
1569163953Srrs			}
1570163953Srrs			if (stcb == NULL)
1571163953Srrs				goto none_out_now;
1572163953Srrs
1573163953Srrs			at = 0;
1574163953Srrs			ids->asls_numb_present = 0;
1575163953Srrs			ids->asls_more_to_get = 1;
1576163953Srrs			while (at < MAX_ASOC_IDS_RET) {
1577163953Srrs				ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1578163953Srrs				at++;
1579163953Srrs				ids->asls_numb_present++;
1580163953Srrs				stcb = LIST_NEXT(stcb, sctp_tcblist);
1581163953Srrs				if (stcb == NULL) {
1582163953Srrs					ids->asls_more_to_get = 0;
1583163953Srrs					break;
1584163953Srrs				}
1585163953Srrs			}
1586163953Srrs			SCTP_INP_RUNLOCK(inp);
1587163953Srrs		}
1588163953Srrs		break;
1589163953Srrs	case SCTP_CONTEXT:
1590163953Srrs		{
1591163953Srrs			struct sctp_assoc_value *av;
1592163953Srrs
1593166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1594166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1595166675Srrs
1596166675Srrs			if (stcb) {
1597166675Srrs				av->assoc_value = stcb->asoc.context;
1598166675Srrs				SCTP_TCB_UNLOCK(stcb);
1599163953Srrs			} else {
1600166675Srrs				SCTP_INP_RLOCK(inp);
1601163953Srrs				av->assoc_value = inp->sctp_context;
1602166675Srrs				SCTP_INP_RUNLOCK(inp);
1603163953Srrs			}
1604166675Srrs			*optsize = sizeof(*av);
1605163953Srrs		}
1606163953Srrs		break;
1607167598Srrs	case SCTP_VRF_ID:
1608167598Srrs		{
1609167598Srrs			uint32_t *vrf_id;
1610167598Srrs
1611167598Srrs			SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, *optsize);
1612167598Srrs			*vrf_id = inp->def_vrf_id;
1613167598Srrs			break;
1614167598Srrs		}
1615167598Srrs	case SCTP_GET_ASOC_VRF:
1616167598Srrs		{
1617167598Srrs			struct sctp_assoc_value *id;
1618167598Srrs
1619167598Srrs			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1620167598Srrs			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1621167598Srrs			if (stcb == NULL) {
1622167598Srrs				error = EINVAL;
1623167598Srrs				break;
1624167598Srrs			}
1625167598Srrs			id->assoc_value = stcb->asoc.vrf_id;
1626167598Srrs			break;
1627167598Srrs		}
1628167598Srrs	case SCTP_GET_VRF_IDS:
1629167598Srrs		{
1630167598Srrs			error = EOPNOTSUPP;
1631167598Srrs			break;
1632167598Srrs		}
1633163953Srrs	case SCTP_GET_NONCE_VALUES:
1634163953Srrs		{
1635163953Srrs			struct sctp_get_nonce_values *gnv;
1636163953Srrs
1637166675Srrs			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1638166675Srrs			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1639166675Srrs
1640166675Srrs			if (stcb) {
1641163953Srrs				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1642163953Srrs				gnv->gn_local_tag = stcb->asoc.my_vtag;
1643163953Srrs				SCTP_TCB_UNLOCK(stcb);
1644166675Srrs			} else {
1645166675Srrs				error = ENOTCONN;
1646163953Srrs			}
1647166675Srrs			*optsize = sizeof(*gnv);
1648163953Srrs		}
1649163953Srrs		break;
1650163953Srrs	case SCTP_DELAYED_ACK_TIME:
1651163953Srrs		{
1652163953Srrs			struct sctp_assoc_value *tm;
1653163953Srrs
1654166675Srrs			SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, *optsize);
1655166675Srrs			SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
1656163953Srrs
1657166675Srrs			if (stcb) {
1658166675Srrs				tm->assoc_value = stcb->asoc.delayed_ack;
1659166675Srrs				SCTP_TCB_UNLOCK(stcb);
1660166675Srrs			} else {
1661163953Srrs				SCTP_INP_RLOCK(inp);
1662166675Srrs				tm->assoc_value = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1663163953Srrs				SCTP_INP_RUNLOCK(inp);
1664163953Srrs			}
1665166675Srrs			*optsize = sizeof(*tm);
1666163953Srrs		}
1667163953Srrs		break;
1668163953Srrs
1669163953Srrs	case SCTP_GET_SNDBUF_USE:
1670166675Srrs		{
1671163953Srrs			struct sctp_sockstat *ss;
1672163953Srrs
1673166675Srrs			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1674166675Srrs			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1675166675Srrs
1676166675Srrs			if (stcb) {
1677166675Srrs				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1678166675Srrs				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1679166675Srrs				    stcb->asoc.size_on_all_streams);
1680166675Srrs				SCTP_TCB_UNLOCK(stcb);
1681166675Srrs			} else {
1682163953Srrs				error = ENOTCONN;
1683163953Srrs			}
1684166675Srrs			*optsize = sizeof(struct sctp_sockstat);
1685163953Srrs		}
1686163953Srrs		break;
1687163953Srrs	case SCTP_MAXBURST:
1688163953Srrs		{
1689166675Srrs			uint8_t *value;
1690163953Srrs
1691166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1692166675Srrs
1693163953Srrs			SCTP_INP_RLOCK(inp);
1694166675Srrs			*value = inp->sctp_ep.max_burst;
1695163953Srrs			SCTP_INP_RUNLOCK(inp);
1696166675Srrs			*optsize = sizeof(uint8_t);
1697163953Srrs		}
1698163953Srrs		break;
1699163953Srrs	case SCTP_MAXSEG:
1700163953Srrs		{
1701167598Srrs			struct sctp_assoc_value *av;
1702163953Srrs			int ovh;
1703163953Srrs
1704167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1705167598Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1706163953Srrs
1707167598Srrs			if (stcb) {
1708167598Srrs				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1709167598Srrs				SCTP_TCB_UNLOCK(stcb);
1710163953Srrs			} else {
1711167598Srrs				SCTP_INP_RLOCK(inp);
1712167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1713167598Srrs					ovh = SCTP_MED_OVERHEAD;
1714167598Srrs				} else {
1715167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
1716167598Srrs				}
1717167598Srrs				av->assoc_value = inp->sctp_frag_point - ovh;
1718167598Srrs				SCTP_INP_RUNLOCK(inp);
1719163953Srrs			}
1720167598Srrs			*optsize = sizeof(struct sctp_assoc_value);
1721163953Srrs		}
1722163953Srrs		break;
1723163953Srrs	case SCTP_GET_STAT_LOG:
1724163953Srrs#ifdef SCTP_STAT_LOGGING
1725167598Srrs		error = sctp_fill_stat_log(optval, optsize);
1726167598Srrs#else
1727163953Srrs		error = EOPNOTSUPP;
1728163953Srrs#endif
1729163953Srrs		break;
1730163953Srrs	case SCTP_EVENTS:
1731163953Srrs		{
1732163953Srrs			struct sctp_event_subscribe *events;
1733163953Srrs
1734166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1735163953Srrs			memset(events, 0, sizeof(*events));
1736163953Srrs			SCTP_INP_RLOCK(inp);
1737163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1738163953Srrs				events->sctp_data_io_event = 1;
1739163953Srrs
1740163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1741163953Srrs				events->sctp_association_event = 1;
1742163953Srrs
1743163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1744163953Srrs				events->sctp_address_event = 1;
1745163953Srrs
1746163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1747163953Srrs				events->sctp_send_failure_event = 1;
1748163953Srrs
1749163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1750163953Srrs				events->sctp_peer_error_event = 1;
1751163953Srrs
1752163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1753163953Srrs				events->sctp_shutdown_event = 1;
1754163953Srrs
1755163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1756163953Srrs				events->sctp_partial_delivery_event = 1;
1757163953Srrs
1758163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1759163953Srrs				events->sctp_adaptation_layer_event = 1;
1760163953Srrs
1761163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1762163953Srrs				events->sctp_authentication_event = 1;
1763163953Srrs
1764163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1765163953Srrs				events->sctp_stream_reset_events = 1;
1766163953Srrs			SCTP_INP_RUNLOCK(inp);
1767166675Srrs			*optsize = sizeof(struct sctp_event_subscribe);
1768163953Srrs		}
1769163953Srrs		break;
1770163953Srrs
1771163953Srrs	case SCTP_ADAPTATION_LAYER:
1772166675Srrs		{
1773166675Srrs			uint32_t *value;
1774166675Srrs
1775166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1776166675Srrs
1777166675Srrs			SCTP_INP_RLOCK(inp);
1778166675Srrs			*value = inp->sctp_ep.adaptation_layer_indicator;
1779166675Srrs			SCTP_INP_RUNLOCK(inp);
1780166675Srrs			*optsize = sizeof(uint32_t);
1781163953Srrs		}
1782163953Srrs		break;
1783163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
1784166675Srrs		{
1785166675Srrs			uint32_t *value;
1786166675Srrs
1787166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1788166675Srrs			SCTP_INP_RLOCK(inp);
1789166675Srrs			*value = inp->sctp_ep.initial_sequence_debug;
1790166675Srrs			SCTP_INP_RUNLOCK(inp);
1791166675Srrs			*optsize = sizeof(uint32_t);
1792163953Srrs		}
1793163953Srrs		break;
1794163953Srrs	case SCTP_GET_LOCAL_ADDR_SIZE:
1795166675Srrs		{
1796166675Srrs			uint32_t *value;
1797166675Srrs
1798166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1799166675Srrs			SCTP_INP_RLOCK(inp);
1800167598Srrs			*value = sctp_count_max_addresses(inp, vrf_id);
1801166675Srrs			SCTP_INP_RUNLOCK(inp);
1802166675Srrs			*optsize = sizeof(uint32_t);
1803163953Srrs		}
1804163953Srrs		break;
1805163953Srrs	case SCTP_GET_REMOTE_ADDR_SIZE:
1806163953Srrs		{
1807166675Srrs			uint32_t *value;
1808166675Srrs			size_t size;
1809163953Srrs			struct sctp_nets *net;
1810163953Srrs
1811166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1812166675Srrs			/* FIXME MT: change to sctp_assoc_value? */
1813166675Srrs			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1814166675Srrs
1815166675Srrs			if (stcb) {
1816166675Srrs				size = 0;
1817166675Srrs				/* Count the sizes */
1818166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1819166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1820166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1821166675Srrs						size += sizeof(struct sockaddr_in6);
1822166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1823166675Srrs						size += sizeof(struct sockaddr_in);
1824166675Srrs					} else {
1825166675Srrs						/* huh */
1826166675Srrs						break;
1827166675Srrs					}
1828163953Srrs				}
1829166675Srrs				SCTP_TCB_UNLOCK(stcb);
1830166675Srrs				*value = (uint32_t) size;
1831166675Srrs			} else {
1832166675Srrs				error = ENOTCONN;
1833163953Srrs			}
1834166675Srrs			*optsize = sizeof(uint32_t);
1835163953Srrs		}
1836163953Srrs		break;
1837163953Srrs	case SCTP_GET_PEER_ADDRESSES:
1838163953Srrs		/*
1839163953Srrs		 * Get the address information, an array is passed in to
1840163953Srrs		 * fill up we pack it.
1841163953Srrs		 */
1842163953Srrs		{
1843166675Srrs			size_t cpsz, left;
1844163953Srrs			struct sockaddr_storage *sas;
1845163953Srrs			struct sctp_nets *net;
1846163953Srrs			struct sctp_getaddresses *saddr;
1847163953Srrs
1848166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1849166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1850163953Srrs
1851166675Srrs			if (stcb) {
1852166675Srrs				left = (*optsize) - sizeof(struct sctp_getaddresses);
1853166675Srrs				*optsize = sizeof(struct sctp_getaddresses);
1854166675Srrs				sas = (struct sockaddr_storage *)&saddr->addr[0];
1855166675Srrs
1856166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1857166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1858166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1859166675Srrs						cpsz = sizeof(struct sockaddr_in6);
1860166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1861166675Srrs						cpsz = sizeof(struct sockaddr_in);
1862166675Srrs					} else {
1863166675Srrs						/* huh */
1864166675Srrs						break;
1865166675Srrs					}
1866166675Srrs					if (left < cpsz) {
1867166675Srrs						/* not enough room. */
1868166675Srrs						break;
1869166675Srrs					}
1870166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1871166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1872166675Srrs						/* Must map the address */
1873166675Srrs						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1874166675Srrs						    (struct sockaddr_in6 *)sas);
1875166675Srrs					} else {
1876166675Srrs						memcpy(sas, &net->ro._l_addr, cpsz);
1877166675Srrs					}
1878166675Srrs					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1879166675Srrs
1880166675Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1881166675Srrs					left -= cpsz;
1882166675Srrs					*optsize += cpsz;
1883163953Srrs				}
1884166675Srrs				SCTP_TCB_UNLOCK(stcb);
1885166675Srrs			} else {
1886166675Srrs				error = ENOENT;
1887163953Srrs			}
1888163953Srrs		}
1889163953Srrs		break;
1890163953Srrs	case SCTP_GET_LOCAL_ADDRESSES:
1891163953Srrs		{
1892166675Srrs			size_t limit, actual;
1893163953Srrs			struct sockaddr_storage *sas;
1894163953Srrs			struct sctp_getaddresses *saddr;
1895163953Srrs
1896166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1897166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1898163953Srrs
1899163953Srrs			sas = (struct sockaddr_storage *)&saddr->addr[0];
1900166675Srrs			limit = *optsize - sizeof(sctp_assoc_t);
1901167598Srrs			actual = sctp_fill_up_addresses(inp, stcb, limit, sas, vrf_id);
1902163953Srrs			if (stcb)
1903163953Srrs				SCTP_TCB_UNLOCK(stcb);
1904166675Srrs			*optsize = sizeof(struct sockaddr_storage) + actual;
1905163953Srrs		}
1906163953Srrs		break;
1907163953Srrs	case SCTP_PEER_ADDR_PARAMS:
1908163953Srrs		{
1909163953Srrs			struct sctp_paddrparams *paddrp;
1910163953Srrs			struct sctp_nets *net;
1911163953Srrs
1912166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1913166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1914163953Srrs
1915163953Srrs			net = NULL;
1916166675Srrs			if (stcb) {
1917166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1918166675Srrs			} else {
1919166675Srrs				/*
1920166675Srrs				 * We increment here since
1921166675Srrs				 * sctp_findassociation_ep_addr() wil do a
1922166675Srrs				 * decrement if it finds the stcb as long as
1923166675Srrs				 * the locked tcb (last argument) is NOT a
1924166675Srrs				 * TCB.. aka NULL.
1925166675Srrs				 */
1926166675Srrs				SCTP_INP_INCR_REF(inp);
1927166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1928163953Srrs				if (stcb == NULL) {
1929166675Srrs					SCTP_INP_DECR_REF(inp);
1930163953Srrs				}
1931163953Srrs			}
1932163953Srrs
1933163953Srrs			if (stcb) {
1934163953Srrs				/* Applys to the specific association */
1935163953Srrs				paddrp->spp_flags = 0;
1936163953Srrs				if (net) {
1937163953Srrs					paddrp->spp_pathmaxrxt = net->failure_threshold;
1938163953Srrs					paddrp->spp_pathmtu = net->mtu;
1939163953Srrs					/* get flags for HB */
1940163953Srrs					if (net->dest_state & SCTP_ADDR_NOHB)
1941163953Srrs						paddrp->spp_flags |= SPP_HB_DISABLE;
1942163953Srrs					else
1943163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
1944163953Srrs					/* get flags for PMTU */
1945165647Srrs					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
1946163953Srrs						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
1947163953Srrs					} else {
1948163953Srrs						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
1949163953Srrs					}
1950167598Srrs#ifdef INET
1951163953Srrs					if (net->ro._l_addr.sin.sin_family == AF_INET) {
1952163953Srrs						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
1953163953Srrs						paddrp->spp_flags |= SPP_IPV4_TOS;
1954163953Srrs					}
1955163953Srrs#endif
1956167598Srrs#ifdef INET6
1957163953Srrs					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
1958163953Srrs						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
1959163953Srrs						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1960163953Srrs					}
1961163953Srrs#endif
1962163953Srrs				} else {
1963163953Srrs					/*
1964163953Srrs					 * No destination so return default
1965163953Srrs					 * value
1966163953Srrs					 */
1967163953Srrs					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
1968163953Srrs					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
1969167598Srrs#ifdef INET
1970163953Srrs					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
1971163953Srrs					paddrp->spp_flags |= SPP_IPV4_TOS;
1972163953Srrs#endif
1973167598Srrs#ifdef INET6
1974163953Srrs					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
1975163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1976163953Srrs#endif
1977163953Srrs					/* default settings should be these */
1978163953Srrs					if (sctp_is_hb_timer_running(stcb)) {
1979163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
1980163953Srrs					}
1981163953Srrs				}
1982163953Srrs				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
1983163953Srrs				paddrp->spp_assoc_id = sctp_get_associd(stcb);
1984163953Srrs				SCTP_TCB_UNLOCK(stcb);
1985163953Srrs			} else {
1986163953Srrs				/* Use endpoint defaults */
1987163953Srrs				SCTP_INP_RLOCK(inp);
1988163953Srrs				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
1989163953Srrs				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1990163953Srrs				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
1991163953Srrs				/* get inp's default */
1992167598Srrs#ifdef INET
1993163953Srrs				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
1994163953Srrs				paddrp->spp_flags |= SPP_IPV4_TOS;
1995163953Srrs#endif
1996167598Srrs#ifdef INET6
1997163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1998163953Srrs					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
1999163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2000163953Srrs				}
2001163953Srrs#endif
2002163953Srrs				/* can't return this */
2003163953Srrs				paddrp->spp_pathmaxrxt = 0;
2004163953Srrs				paddrp->spp_pathmtu = 0;
2005163953Srrs				/* default behavior, no stcb */
2006167598Srrs				paddrp->spp_flags = SPP_HB_ENABLE | SPP_PMTUD_ENABLE;
2007163953Srrs
2008163953Srrs				SCTP_INP_RUNLOCK(inp);
2009163953Srrs			}
2010166675Srrs			*optsize = sizeof(struct sctp_paddrparams);
2011163953Srrs		}
2012163953Srrs		break;
2013163953Srrs	case SCTP_GET_PEER_ADDR_INFO:
2014163953Srrs		{
2015163953Srrs			struct sctp_paddrinfo *paddri;
2016163953Srrs			struct sctp_nets *net;
2017163953Srrs
2018166675Srrs			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2019166675Srrs			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2020166675Srrs
2021163953Srrs			net = NULL;
2022166675Srrs			if (stcb) {
2023166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2024166675Srrs			} else {
2025166675Srrs				/*
2026166675Srrs				 * We increment here since
2027166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2028166675Srrs				 * decrement if it finds the stcb as long as
2029166675Srrs				 * the locked tcb (last argument) is NOT a
2030166675Srrs				 * TCB.. aka NULL.
2031166675Srrs				 */
2032166675Srrs				SCTP_INP_INCR_REF(inp);
2033166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2034166675Srrs				if (stcb == NULL) {
2035166675Srrs					SCTP_INP_DECR_REF(inp);
2036163953Srrs				}
2037166675Srrs			}
2038163953Srrs
2039166675Srrs			if ((stcb) && (net)) {
2040166675Srrs				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2041166675Srrs				paddri->spinfo_cwnd = net->cwnd;
2042166675Srrs				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2043166675Srrs				paddri->spinfo_rto = net->RTO;
2044166675Srrs				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2045166675Srrs				SCTP_TCB_UNLOCK(stcb);
2046163953Srrs			} else {
2047163953Srrs				if (stcb) {
2048163953Srrs					SCTP_TCB_UNLOCK(stcb);
2049163953Srrs				}
2050163953Srrs				error = ENOENT;
2051163953Srrs			}
2052166675Srrs			*optsize = sizeof(struct sctp_paddrinfo);
2053163953Srrs		}
2054163953Srrs		break;
2055163953Srrs	case SCTP_PCB_STATUS:
2056163953Srrs		{
2057163953Srrs			struct sctp_pcbinfo *spcb;
2058163953Srrs
2059166675Srrs			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2060163953Srrs			sctp_fill_pcbinfo(spcb);
2061166675Srrs			*optsize = sizeof(struct sctp_pcbinfo);
2062163953Srrs		}
2063163953Srrs		break;
2064167598Srrs
2065163953Srrs	case SCTP_STATUS:
2066163953Srrs		{
2067163953Srrs			struct sctp_nets *net;
2068163953Srrs			struct sctp_status *sstat;
2069163953Srrs
2070166675Srrs			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2071166675Srrs			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2072163953Srrs
2073163953Srrs			if (stcb == NULL) {
2074163953Srrs				error = EINVAL;
2075163953Srrs				break;
2076163953Srrs			}
2077163953Srrs			/*
2078163953Srrs			 * I think passing the state is fine since
2079163953Srrs			 * sctp_constants.h will be available to the user
2080163953Srrs			 * land.
2081163953Srrs			 */
2082163953Srrs			sstat->sstat_state = stcb->asoc.state;
2083163953Srrs			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2084163953Srrs			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2085163953Srrs			/*
2086163953Srrs			 * We can't include chunks that have been passed to
2087163953Srrs			 * the socket layer. Only things in queue.
2088163953Srrs			 */
2089163953Srrs			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2090163953Srrs			    stcb->asoc.cnt_on_all_streams);
2091163953Srrs
2092163953Srrs
2093163953Srrs			sstat->sstat_instrms = stcb->asoc.streamincnt;
2094163953Srrs			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2095163953Srrs			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2096163953Srrs			memcpy(&sstat->sstat_primary.spinfo_address,
2097163953Srrs			    &stcb->asoc.primary_destination->ro._l_addr,
2098163953Srrs			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2099163953Srrs			net = stcb->asoc.primary_destination;
2100163953Srrs			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2101163953Srrs			/*
2102163953Srrs			 * Again the user can get info from sctp_constants.h
2103163953Srrs			 * for what the state of the network is.
2104163953Srrs			 */
2105163953Srrs			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2106163953Srrs			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2107163953Srrs			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2108163953Srrs			sstat->sstat_primary.spinfo_rto = net->RTO;
2109163953Srrs			sstat->sstat_primary.spinfo_mtu = net->mtu;
2110163953Srrs			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2111163953Srrs			SCTP_TCB_UNLOCK(stcb);
2112166675Srrs			*optsize = sizeof(*sstat);
2113163953Srrs		}
2114163953Srrs		break;
2115163953Srrs	case SCTP_RTOINFO:
2116163953Srrs		{
2117163953Srrs			struct sctp_rtoinfo *srto;
2118163953Srrs
2119166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2120166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2121166675Srrs
2122166675Srrs			if (stcb) {
2123166675Srrs				srto->srto_initial = stcb->asoc.initial_rto;
2124166675Srrs				srto->srto_max = stcb->asoc.maxrto;
2125166675Srrs				srto->srto_min = stcb->asoc.minrto;
2126166675Srrs				SCTP_TCB_UNLOCK(stcb);
2127166675Srrs			} else {
2128163953Srrs				SCTP_INP_RLOCK(inp);
2129163953Srrs				srto->srto_initial = inp->sctp_ep.initial_rto;
2130163953Srrs				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2131163953Srrs				srto->srto_min = inp->sctp_ep.sctp_minrto;
2132163953Srrs				SCTP_INP_RUNLOCK(inp);
2133163953Srrs			}
2134166675Srrs			*optsize = sizeof(*srto);
2135163953Srrs		}
2136163953Srrs		break;
2137163953Srrs	case SCTP_ASSOCINFO:
2138163953Srrs		{
2139163953Srrs			struct sctp_assocparams *sasoc;
2140163953Srrs
2141166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2142166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2143163953Srrs
2144163953Srrs			if (stcb) {
2145163953Srrs				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2146163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2147163953Srrs				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2148163953Srrs				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2149163953Srrs				sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2150167598Srrs				sasoc->sasoc_sack_delay = stcb->asoc.delayed_ack;
2151167598Srrs				sasoc->sasoc_sack_freq = stcb->asoc.sack_freq;
2152163953Srrs				SCTP_TCB_UNLOCK(stcb);
2153163953Srrs			} else {
2154163953Srrs				SCTP_INP_RLOCK(inp);
2155163953Srrs				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2156163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
2157163953Srrs				sasoc->sasoc_peer_rwnd = 0;
2158163953Srrs				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2159163953Srrs				sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2160167598Srrs				sasoc->sasoc_sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2161167598Srrs				sasoc->sasoc_sack_freq = inp->sctp_ep.sctp_sack_freq;
2162163953Srrs				SCTP_INP_RUNLOCK(inp);
2163163953Srrs			}
2164166675Srrs			*optsize = sizeof(*sasoc);
2165163953Srrs		}
2166163953Srrs		break;
2167163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2168163953Srrs		{
2169163953Srrs			struct sctp_sndrcvinfo *s_info;
2170163953Srrs
2171166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2172166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2173166675Srrs
2174166675Srrs			if (stcb) {
2175166675Srrs				*s_info = stcb->asoc.def_send;
2176166675Srrs				SCTP_TCB_UNLOCK(stcb);
2177166675Srrs			} else {
2178163953Srrs				SCTP_INP_RLOCK(inp);
2179166675Srrs				*s_info = inp->def_send;
2180163953Srrs				SCTP_INP_RUNLOCK(inp);
2181163953Srrs			}
2182166675Srrs			*optsize = sizeof(*s_info);
2183163953Srrs		}
2184163953Srrs		break;
2185163953Srrs	case SCTP_INITMSG:
2186163953Srrs		{
2187163953Srrs			struct sctp_initmsg *sinit;
2188163953Srrs
2189166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2190163953Srrs			SCTP_INP_RLOCK(inp);
2191163953Srrs			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2192163953Srrs			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2193163953Srrs			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2194163953Srrs			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2195163953Srrs			SCTP_INP_RUNLOCK(inp);
2196166675Srrs			*optsize = sizeof(*sinit);
2197163953Srrs		}
2198163953Srrs		break;
2199163953Srrs	case SCTP_PRIMARY_ADDR:
2200163953Srrs		/* we allow a "get" operation on this */
2201163953Srrs		{
2202163953Srrs			struct sctp_setprim *ssp;
2203163953Srrs
2204166675Srrs			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2205166675Srrs			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2206166675Srrs
2207166675Srrs			if (stcb) {
2208166675Srrs				/* simply copy out the sockaddr_storage... */
2209166675Srrs				memcpy(&ssp->ssp_addr, &stcb->asoc.primary_destination->ro._l_addr,
2210166675Srrs				    ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2211166675Srrs				SCTP_TCB_UNLOCK(stcb);
2212166675Srrs			} else {
2213163953Srrs				error = EINVAL;
2214163953Srrs			}
2215166675Srrs			*optsize = sizeof(*ssp);
2216163953Srrs		}
2217163953Srrs		break;
2218163953Srrs
2219163953Srrs	case SCTP_HMAC_IDENT:
2220163953Srrs		{
2221163953Srrs			struct sctp_hmacalgo *shmac;
2222163953Srrs			sctp_hmaclist_t *hmaclist;
2223163953Srrs			uint32_t size;
2224163953Srrs			int i;
2225163953Srrs
2226166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2227166675Srrs
2228163953Srrs			SCTP_INP_RLOCK(inp);
2229163953Srrs			hmaclist = inp->sctp_ep.local_hmacs;
2230163953Srrs			if (hmaclist == NULL) {
2231163953Srrs				/* no HMACs to return */
2232166675Srrs				*optsize = sizeof(*shmac);
2233163953Srrs				break;
2234163953Srrs			}
2235163953Srrs			/* is there room for all of the hmac ids? */
2236163953Srrs			size = sizeof(*shmac) + (hmaclist->num_algo *
2237163953Srrs			    sizeof(shmac->shmac_idents[0]));
2238166675Srrs			if ((size_t)(*optsize) < size) {
2239163953Srrs				error = EINVAL;
2240163953Srrs				SCTP_INP_RUNLOCK(inp);
2241163953Srrs				break;
2242163953Srrs			}
2243163953Srrs			/* copy in the list */
2244163953Srrs			for (i = 0; i < hmaclist->num_algo; i++)
2245163953Srrs				shmac->shmac_idents[i] = hmaclist->hmac[i];
2246163953Srrs			SCTP_INP_RUNLOCK(inp);
2247166675Srrs			*optsize = size;
2248163953Srrs			break;
2249163953Srrs		}
2250163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2251163953Srrs		{
2252163953Srrs			struct sctp_authkeyid *scact;
2253163953Srrs
2254166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2255166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2256166675Srrs
2257166675Srrs			if (stcb) {
2258163953Srrs				/* get the active key on the assoc */
2259163953Srrs				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2260163953Srrs				SCTP_TCB_UNLOCK(stcb);
2261163953Srrs			} else {
2262163953Srrs				/* get the endpoint active key */
2263163953Srrs				SCTP_INP_RLOCK(inp);
2264163953Srrs				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2265163953Srrs				SCTP_INP_RUNLOCK(inp);
2266163953Srrs			}
2267166675Srrs			*optsize = sizeof(*scact);
2268163953Srrs			break;
2269163953Srrs		}
2270163953Srrs	case SCTP_LOCAL_AUTH_CHUNKS:
2271163953Srrs		{
2272163953Srrs			struct sctp_authchunks *sac;
2273163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2274166675Srrs			size_t size = 0;
2275163953Srrs
2276166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2277166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2278166675Srrs
2279166675Srrs			if (stcb) {
2280163953Srrs				/* get off the assoc */
2281163953Srrs				chklist = stcb->asoc.local_auth_chunks;
2282163953Srrs				/* is there enough space? */
2283163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2284166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2285163953Srrs					error = EINVAL;
2286166675Srrs				} else {
2287166675Srrs					/* copy in the chunks */
2288166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2289163953Srrs				}
2290163953Srrs				SCTP_TCB_UNLOCK(stcb);
2291163953Srrs			} else {
2292163953Srrs				/* get off the endpoint */
2293163953Srrs				SCTP_INP_RLOCK(inp);
2294163953Srrs				chklist = inp->sctp_ep.local_auth_chunks;
2295163953Srrs				/* is there enough space? */
2296163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2297166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2298163953Srrs					error = EINVAL;
2299166675Srrs				} else {
2300166675Srrs					/* copy in the chunks */
2301166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2302163953Srrs				}
2303163953Srrs				SCTP_INP_RUNLOCK(inp);
2304163953Srrs			}
2305166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2306163953Srrs			break;
2307163953Srrs		}
2308163953Srrs	case SCTP_PEER_AUTH_CHUNKS:
2309163953Srrs		{
2310163953Srrs			struct sctp_authchunks *sac;
2311163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2312166675Srrs			size_t size = 0;
2313163953Srrs
2314166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2315166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2316166675Srrs
2317166675Srrs			if (stcb) {
2318166675Srrs				/* get off the assoc */
2319166675Srrs				chklist = stcb->asoc.peer_auth_chunks;
2320166675Srrs				/* is there enough space? */
2321166675Srrs				size = sctp_auth_get_chklist_size(chklist);
2322166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2323166675Srrs					error = EINVAL;
2324166675Srrs				} else {
2325166675Srrs					/* copy in the chunks */
2326166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2327166675Srrs				}
2328166675Srrs				SCTP_TCB_UNLOCK(stcb);
2329166675Srrs			} else {
2330163953Srrs				error = ENOENT;
2331163953Srrs			}
2332166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2333163953Srrs			break;
2334163953Srrs		}
2335163953Srrs
2336163953Srrs
2337163953Srrs	default:
2338163953Srrs		error = ENOPROTOOPT;
2339166675Srrs		*optsize = 0;
2340163953Srrs		break;
2341163953Srrs	}			/* end switch (sopt->sopt_name) */
2342163953Srrs	return (error);
2343163953Srrs}
2344163953Srrs
2345163953Srrsstatic int
2346166675Srrssctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2347166675Srrs    void *p)
2348163953Srrs{
2349166675Srrs	int error, set_opt;
2350166675Srrs	uint32_t *mopt;
2351163953Srrs	struct sctp_tcb *stcb = NULL;
2352163953Srrs	struct sctp_inpcb *inp;
2353167598Srrs	uint32_t vrf_id;
2354163953Srrs
2355166675Srrs	if (optval == NULL) {
2356167598Srrs		printf("optval is NULL\n");
2357163953Srrs		return (EINVAL);
2358163953Srrs	}
2359163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
2360167598Srrs	if (inp == 0) {
2361167598Srrs		printf("inp is NULL?\n");
2362163953Srrs		return EINVAL;
2363167598Srrs	}
2364167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
2365163953Srrs
2366163953Srrs	error = 0;
2367166675Srrs	switch (optname) {
2368163953Srrs	case SCTP_NODELAY:
2369163953Srrs	case SCTP_AUTOCLOSE:
2370163953Srrs	case SCTP_AUTO_ASCONF:
2371163953Srrs	case SCTP_EXPLICIT_EOR:
2372163953Srrs	case SCTP_DISABLE_FRAGMENTS:
2373163953Srrs	case SCTP_USE_EXT_RCVINFO:
2374163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
2375163953Srrs		/* copy in the option value */
2376166675Srrs		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2377163953Srrs		set_opt = 0;
2378163953Srrs		if (error)
2379163953Srrs			break;
2380166675Srrs		switch (optname) {
2381163953Srrs		case SCTP_DISABLE_FRAGMENTS:
2382163953Srrs			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2383163953Srrs			break;
2384163953Srrs		case SCTP_AUTO_ASCONF:
2385163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2386163953Srrs			break;
2387163953Srrs		case SCTP_EXPLICIT_EOR:
2388163953Srrs			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2389163953Srrs			break;
2390163953Srrs		case SCTP_USE_EXT_RCVINFO:
2391163953Srrs			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2392163953Srrs			break;
2393163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
2394163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2395163953Srrs				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2396163953Srrs			} else {
2397163953Srrs				return (EINVAL);
2398163953Srrs			}
2399163953Srrs			break;
2400163953Srrs		case SCTP_NODELAY:
2401163953Srrs			set_opt = SCTP_PCB_FLAGS_NODELAY;
2402163953Srrs			break;
2403163953Srrs		case SCTP_AUTOCLOSE:
2404163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2405163953Srrs			/*
2406163953Srrs			 * The value is in ticks. Note this does not effect
2407163953Srrs			 * old associations, only new ones.
2408163953Srrs			 */
2409163953Srrs			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2410163953Srrs			break;
2411163953Srrs		}
2412163953Srrs		SCTP_INP_WLOCK(inp);
2413163953Srrs		if (*mopt != 0) {
2414163953Srrs			sctp_feature_on(inp, set_opt);
2415163953Srrs		} else {
2416163953Srrs			sctp_feature_off(inp, set_opt);
2417163953Srrs		}
2418163953Srrs		SCTP_INP_WUNLOCK(inp);
2419163953Srrs		break;
2420163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
2421163953Srrs		{
2422166675Srrs			uint32_t *value;
2423166675Srrs
2424166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2425166675Srrs			inp->partial_delivery_point = *value;
2426163953Srrs		}
2427163953Srrs		break;
2428163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
2429163953Srrs		/* not yet until we re-write sctp_recvmsg() */
2430163953Srrs		{
2431166675Srrs			uint32_t *on_off;
2432163953Srrs
2433166675Srrs			SCTP_CHECK_AND_CAST(on_off, optval, uint32_t, optsize);
2434166675Srrs			if (*on_off) {
2435163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2436163953Srrs			} else {
2437163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2438163953Srrs			}
2439163953Srrs		}
2440163953Srrs		break;
2441163953Srrs	case SCTP_CMT_ON_OFF:
2442163953Srrs		{
2443163953Srrs			struct sctp_assoc_value *av;
2444163953Srrs
2445166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2446166675Srrs			if (sctp_cmt_on_off) {
2447166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2448166675Srrs				if (stcb) {
2449163953Srrs					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2450166675Srrs					SCTP_TCB_UNLOCK(stcb);
2451163953Srrs				} else {
2452166675Srrs					error = ENOTCONN;
2453163953Srrs				}
2454166675Srrs			} else {
2455166675Srrs				error = ENOPROTOOPT;
2456163953Srrs			}
2457163953Srrs		}
2458163953Srrs		break;
2459163953Srrs	case SCTP_CLR_STAT_LOG:
2460163953Srrs#ifdef SCTP_STAT_LOGGING
2461163953Srrs		sctp_clr_stat_log();
2462163953Srrs#else
2463163953Srrs		error = EOPNOTSUPP;
2464163953Srrs#endif
2465163953Srrs		break;
2466163953Srrs	case SCTP_CONTEXT:
2467163953Srrs		{
2468163953Srrs			struct sctp_assoc_value *av;
2469163953Srrs
2470166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2471166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2472166675Srrs
2473166675Srrs			if (stcb) {
2474166675Srrs				stcb->asoc.context = av->assoc_value;
2475166675Srrs				SCTP_TCB_UNLOCK(stcb);
2476163953Srrs			} else {
2477166675Srrs				SCTP_INP_WLOCK(inp);
2478163953Srrs				inp->sctp_context = av->assoc_value;
2479166675Srrs				SCTP_INP_WUNLOCK(inp);
2480163953Srrs			}
2481163953Srrs		}
2482163953Srrs		break;
2483167598Srrs	case SCTP_VRF_ID:
2484167598Srrs		{
2485167598Srrs			uint32_t *vrf_id;
2486167598Srrs
2487167598Srrs			SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, optsize);
2488167598Srrs			if (*vrf_id > SCTP_MAX_VRF_ID) {
2489167598Srrs				error = EINVAL;
2490167598Srrs				break;
2491167598Srrs			}
2492167598Srrs			inp->def_vrf_id = *vrf_id;
2493167598Srrs			break;
2494167598Srrs		}
2495167598Srrs	case SCTP_DEL_VRF_ID:
2496167598Srrs		{
2497167598Srrs			error = EOPNOTSUPP;
2498167598Srrs			break;
2499167598Srrs		}
2500167598Srrs	case SCTP_ADD_VRF_ID:
2501167598Srrs		{
2502167598Srrs			error = EOPNOTSUPP;
2503167598Srrs			break;
2504167598Srrs		}
2505167598Srrs
2506163953Srrs	case SCTP_DELAYED_ACK_TIME:
2507163953Srrs		{
2508163953Srrs			struct sctp_assoc_value *tm;
2509163953Srrs
2510166675Srrs			SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, optsize);
2511166675Srrs			SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
2512163953Srrs
2513166675Srrs			if (stcb) {
2514166675Srrs				stcb->asoc.delayed_ack = tm->assoc_value;
2515166675Srrs				SCTP_TCB_UNLOCK(stcb);
2516166675Srrs			} else {
2517163953Srrs				SCTP_INP_WLOCK(inp);
2518166675Srrs				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(tm->assoc_value);
2519163953Srrs				SCTP_INP_WUNLOCK(inp);
2520163953Srrs			}
2521166675Srrs			break;
2522163953Srrs		}
2523163953Srrs	case SCTP_AUTH_CHUNK:
2524163953Srrs		{
2525163953Srrs			struct sctp_authchunk *sauth;
2526163953Srrs
2527166675Srrs			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2528166675Srrs
2529166675Srrs			SCTP_INP_WLOCK(inp);
2530166675Srrs			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2531163953Srrs				error = EINVAL;
2532166675Srrs			SCTP_INP_WUNLOCK(inp);
2533163953Srrs			break;
2534163953Srrs		}
2535163953Srrs	case SCTP_AUTH_KEY:
2536163953Srrs		{
2537163953Srrs			struct sctp_authkey *sca;
2538163953Srrs			struct sctp_keyhead *shared_keys;
2539163953Srrs			sctp_sharedkey_t *shared_key;
2540163953Srrs			sctp_key_t *key = NULL;
2541166675Srrs			size_t size;
2542163953Srrs
2543166675Srrs			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2544166675Srrs			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id)
2545166675Srrs			    size = optsize - sizeof(*sca);
2546166675Srrs
2547166675Srrs			if (stcb) {
2548163953Srrs				/* set it on the assoc */
2549163953Srrs				shared_keys = &stcb->asoc.shared_keys;
2550163953Srrs				/* clear the cached keys for this key id */
2551163953Srrs				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2552163953Srrs				/*
2553163953Srrs				 * create the new shared key and
2554163953Srrs				 * insert/replace it
2555163953Srrs				 */
2556163953Srrs				if (size > 0) {
2557163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2558163953Srrs					if (key == NULL) {
2559163953Srrs						error = ENOMEM;
2560163953Srrs						SCTP_TCB_UNLOCK(stcb);
2561163953Srrs						break;
2562163953Srrs					}
2563163953Srrs				}
2564163953Srrs				shared_key = sctp_alloc_sharedkey();
2565163953Srrs				if (shared_key == NULL) {
2566163953Srrs					sctp_free_key(key);
2567163953Srrs					error = ENOMEM;
2568163953Srrs					SCTP_TCB_UNLOCK(stcb);
2569163953Srrs					break;
2570163953Srrs				}
2571163953Srrs				shared_key->key = key;
2572163953Srrs				shared_key->keyid = sca->sca_keynumber;
2573163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2574163953Srrs				SCTP_TCB_UNLOCK(stcb);
2575163953Srrs			} else {
2576166675Srrs				/* set it on the endpoint */
2577163953Srrs				SCTP_INP_WLOCK(inp);
2578163953Srrs				shared_keys = &inp->sctp_ep.shared_keys;
2579163953Srrs				/*
2580163953Srrs				 * clear the cached keys on all assocs for
2581163953Srrs				 * this key id
2582163953Srrs				 */
2583163953Srrs				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2584163953Srrs				/*
2585163953Srrs				 * create the new shared key and
2586163953Srrs				 * insert/replace it
2587163953Srrs				 */
2588163953Srrs				if (size > 0) {
2589163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2590163953Srrs					if (key == NULL) {
2591163953Srrs						error = ENOMEM;
2592163953Srrs						SCTP_INP_WUNLOCK(inp);
2593163953Srrs						break;
2594163953Srrs					}
2595163953Srrs				}
2596163953Srrs				shared_key = sctp_alloc_sharedkey();
2597163953Srrs				if (shared_key == NULL) {
2598163953Srrs					sctp_free_key(key);
2599163953Srrs					error = ENOMEM;
2600163953Srrs					SCTP_INP_WUNLOCK(inp);
2601163953Srrs					break;
2602163953Srrs				}
2603163953Srrs				shared_key->key = key;
2604163953Srrs				shared_key->keyid = sca->sca_keynumber;
2605163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2606163953Srrs				SCTP_INP_WUNLOCK(inp);
2607163953Srrs			}
2608163953Srrs			break;
2609163953Srrs		}
2610163953Srrs	case SCTP_HMAC_IDENT:
2611163953Srrs		{
2612163953Srrs			struct sctp_hmacalgo *shmac;
2613163953Srrs			sctp_hmaclist_t *hmaclist;
2614163953Srrs			uint32_t hmacid;
2615166675Srrs			size_t size, i;
2616163953Srrs
2617166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2618166675Srrs			size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2619163953Srrs			hmaclist = sctp_alloc_hmaclist(size);
2620163953Srrs			if (hmaclist == NULL) {
2621163953Srrs				error = ENOMEM;
2622163953Srrs				break;
2623163953Srrs			}
2624163953Srrs			for (i = 0; i < size; i++) {
2625163953Srrs				hmacid = shmac->shmac_idents[i];
2626163953Srrs				if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2627163953Srrs					 /* invalid HMACs were found */ ;
2628163953Srrs					error = EINVAL;
2629164085Srrs					sctp_free_hmaclist(hmaclist);
2630163953Srrs					goto sctp_set_hmac_done;
2631163953Srrs				}
2632163953Srrs			}
2633163953Srrs			/* set it on the endpoint */
2634163953Srrs			SCTP_INP_WLOCK(inp);
2635163953Srrs			if (inp->sctp_ep.local_hmacs)
2636163953Srrs				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2637163953Srrs			inp->sctp_ep.local_hmacs = hmaclist;
2638163953Srrs			SCTP_INP_WUNLOCK(inp);
2639163953Srrs	sctp_set_hmac_done:
2640163953Srrs			break;
2641163953Srrs		}
2642163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2643163953Srrs		{
2644163953Srrs			struct sctp_authkeyid *scact;
2645163953Srrs
2646166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2647166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2648166675Srrs
2649163953Srrs			/* set the active key on the right place */
2650166675Srrs			if (stcb) {
2651163953Srrs				/* set the active key on the assoc */
2652163953Srrs				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2653163953Srrs					error = EINVAL;
2654163953Srrs				SCTP_TCB_UNLOCK(stcb);
2655163953Srrs			} else {
2656163953Srrs				/* set the active key on the endpoint */
2657163953Srrs				SCTP_INP_WLOCK(inp);
2658163953Srrs				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2659163953Srrs					error = EINVAL;
2660163953Srrs				SCTP_INP_WUNLOCK(inp);
2661163953Srrs			}
2662163953Srrs			break;
2663163953Srrs		}
2664163953Srrs	case SCTP_AUTH_DELETE_KEY:
2665163953Srrs		{
2666163953Srrs			struct sctp_authkeyid *scdel;
2667163953Srrs
2668166675Srrs			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2669166675Srrs			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2670166675Srrs
2671163953Srrs			/* delete the key from the right place */
2672166675Srrs			if (stcb) {
2673163953Srrs				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2674163953Srrs					error = EINVAL;
2675163953Srrs				SCTP_TCB_UNLOCK(stcb);
2676163953Srrs			} else {
2677163953Srrs				SCTP_INP_WLOCK(inp);
2678163953Srrs				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2679163953Srrs					error = EINVAL;
2680163953Srrs				SCTP_INP_WUNLOCK(inp);
2681163953Srrs			}
2682163953Srrs			break;
2683163953Srrs		}
2684163953Srrs
2685163953Srrs	case SCTP_RESET_STREAMS:
2686163953Srrs		{
2687163953Srrs			struct sctp_stream_reset *strrst;
2688163953Srrs			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2689163953Srrs			int i;
2690163953Srrs
2691166675Srrs			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2692166675Srrs			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2693163953Srrs
2694163953Srrs			if (stcb == NULL) {
2695163953Srrs				error = ENOENT;
2696163953Srrs				break;
2697163953Srrs			}
2698163953Srrs			if (stcb->asoc.peer_supports_strreset == 0) {
2699163953Srrs				/*
2700163953Srrs				 * Peer does not support it, we return
2701163953Srrs				 * protocol not supported since this is true
2702163953Srrs				 * for this feature and this peer, not the
2703163953Srrs				 * socket request in general.
2704163953Srrs				 */
2705163953Srrs				error = EPROTONOSUPPORT;
2706163953Srrs				SCTP_TCB_UNLOCK(stcb);
2707163953Srrs				break;
2708163953Srrs			}
2709163953Srrs			if (stcb->asoc.stream_reset_outstanding) {
2710163953Srrs				error = EALREADY;
2711163953Srrs				SCTP_TCB_UNLOCK(stcb);
2712163953Srrs				break;
2713163953Srrs			}
2714163953Srrs			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2715163953Srrs				send_in = 1;
2716163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2717163953Srrs				send_out = 1;
2718163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2719163953Srrs				send_in = 1;
2720163953Srrs				send_out = 1;
2721163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2722163953Srrs				send_tsn = 1;
2723163953Srrs			} else {
2724163953Srrs				error = EINVAL;
2725163953Srrs				SCTP_TCB_UNLOCK(stcb);
2726163953Srrs				break;
2727163953Srrs			}
2728163953Srrs			for (i = 0; i < strrst->strrst_num_streams; i++) {
2729163953Srrs				if ((send_in) &&
2730163953Srrs
2731163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2732163953Srrs					error = EINVAL;
2733163953Srrs					goto get_out;
2734163953Srrs				}
2735163953Srrs				if ((send_out) &&
2736163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2737163953Srrs					error = EINVAL;
2738163953Srrs					goto get_out;
2739163953Srrs				}
2740163953Srrs			}
2741163953Srrs			if (error) {
2742163953Srrs		get_out:
2743163953Srrs				SCTP_TCB_UNLOCK(stcb);
2744163953Srrs				break;
2745163953Srrs			}
2746163953Srrs			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2747163953Srrs			    strrst->strrst_list,
2748163953Srrs			    send_out, (stcb->asoc.str_reset_seq_in - 3),
2749163953Srrs			    send_in, send_tsn);
2750163953Srrs
2751163953Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2752163953Srrs			SCTP_TCB_UNLOCK(stcb);
2753163953Srrs		}
2754163953Srrs		break;
2755166675Srrs
2756163953Srrs	case SCTP_CONNECT_X:
2757166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2758163953Srrs			error = EINVAL;
2759163953Srrs			break;
2760163953Srrs		}
2761166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2762163953Srrs		break;
2763163953Srrs
2764163953Srrs	case SCTP_CONNECT_X_DELAYED:
2765166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2766163953Srrs			error = EINVAL;
2767163953Srrs			break;
2768163953Srrs		}
2769166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2770163953Srrs		break;
2771163953Srrs
2772163953Srrs	case SCTP_CONNECT_X_COMPLETE:
2773163953Srrs		{
2774163953Srrs			struct sockaddr *sa;
2775163953Srrs			struct sctp_nets *net;
2776163953Srrs
2777166675Srrs			/* FIXME MT: check correct? */
2778166675Srrs			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2779166675Srrs
2780163953Srrs			/* find tcb */
2781163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2782163953Srrs				SCTP_INP_RLOCK(inp);
2783163953Srrs				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2784163953Srrs				if (stcb) {
2785163953Srrs					SCTP_TCB_LOCK(stcb);
2786163953Srrs					net = sctp_findnet(stcb, sa);
2787163953Srrs				}
2788163953Srrs				SCTP_INP_RUNLOCK(inp);
2789163953Srrs			} else {
2790166675Srrs				/*
2791166675Srrs				 * We increment here since
2792166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2793166675Srrs				 * decrement if it finds the stcb as long as
2794166675Srrs				 * the locked tcb (last argument) is NOT a
2795166675Srrs				 * TCB.. aka NULL.
2796166675Srrs				 */
2797163953Srrs				SCTP_INP_INCR_REF(inp);
2798163953Srrs				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2799163953Srrs				if (stcb == NULL) {
2800163953Srrs					SCTP_INP_DECR_REF(inp);
2801163953Srrs				}
2802163953Srrs			}
2803163953Srrs
2804163953Srrs			if (stcb == NULL) {
2805163953Srrs				error = ENOENT;
2806163953Srrs				break;
2807163953Srrs			}
2808163953Srrs			if (stcb->asoc.delayed_connection == 1) {
2809163953Srrs				stcb->asoc.delayed_connection = 0;
2810163953Srrs				SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2811165220Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
2812165220Srrs				    stcb->asoc.primary_destination,
2813165220Srrs				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
2814163953Srrs				sctp_send_initiate(inp, stcb);
2815163953Srrs			} else {
2816163953Srrs				/*
2817163953Srrs				 * already expired or did not use delayed
2818163953Srrs				 * connectx
2819163953Srrs				 */
2820163953Srrs				error = EALREADY;
2821163953Srrs			}
2822163953Srrs			SCTP_TCB_UNLOCK(stcb);
2823163953Srrs		}
2824163953Srrs		break;
2825163953Srrs	case SCTP_MAXBURST:
2826163953Srrs		{
2827163953Srrs			uint8_t *burst;
2828163953Srrs
2829166675Srrs			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
2830166675Srrs
2831163953Srrs			SCTP_INP_WLOCK(inp);
2832163953Srrs			if (*burst) {
2833163953Srrs				inp->sctp_ep.max_burst = *burst;
2834163953Srrs			}
2835163953Srrs			SCTP_INP_WUNLOCK(inp);
2836163953Srrs		}
2837163953Srrs		break;
2838163953Srrs	case SCTP_MAXSEG:
2839163953Srrs		{
2840167598Srrs			struct sctp_assoc_value *av;
2841163953Srrs			int ovh;
2842163953Srrs
2843167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2844167598Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2845166675Srrs
2846167598Srrs			if (stcb) {
2847167598Srrs				error = EINVAL;
2848167598Srrs				SCTP_TCB_UNLOCK(stcb);
2849163953Srrs			} else {
2850167598Srrs				SCTP_INP_WLOCK(inp);
2851167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2852167598Srrs					ovh = SCTP_MED_OVERHEAD;
2853167598Srrs				} else {
2854167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
2855167598Srrs				}
2856167598Srrs				/*
2857167598Srrs				 * FIXME MT: I think this is not in tune
2858167598Srrs				 * with the API ID
2859167598Srrs				 */
2860167598Srrs				if (av->assoc_value) {
2861167598Srrs					inp->sctp_frag_point = (av->assoc_value + ovh);
2862167598Srrs				} else {
2863167598Srrs					error = EINVAL;
2864167598Srrs				}
2865167598Srrs				SCTP_INP_WUNLOCK(inp);
2866163953Srrs			}
2867163953Srrs		}
2868163953Srrs		break;
2869163953Srrs	case SCTP_EVENTS:
2870163953Srrs		{
2871163953Srrs			struct sctp_event_subscribe *events;
2872163953Srrs
2873166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
2874166675Srrs
2875163953Srrs			SCTP_INP_WLOCK(inp);
2876163953Srrs			if (events->sctp_data_io_event) {
2877163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2878163953Srrs			} else {
2879163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2880163953Srrs			}
2881163953Srrs
2882163953Srrs			if (events->sctp_association_event) {
2883163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2884163953Srrs			} else {
2885163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2886163953Srrs			}
2887163953Srrs
2888163953Srrs			if (events->sctp_address_event) {
2889163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2890163953Srrs			} else {
2891163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2892163953Srrs			}
2893163953Srrs
2894163953Srrs			if (events->sctp_send_failure_event) {
2895163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2896163953Srrs			} else {
2897163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2898163953Srrs			}
2899163953Srrs
2900163953Srrs			if (events->sctp_peer_error_event) {
2901163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2902163953Srrs			} else {
2903163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2904163953Srrs			}
2905163953Srrs
2906163953Srrs			if (events->sctp_shutdown_event) {
2907163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2908163953Srrs			} else {
2909163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2910163953Srrs			}
2911163953Srrs
2912163953Srrs			if (events->sctp_partial_delivery_event) {
2913163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2914163953Srrs			} else {
2915163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2916163953Srrs			}
2917163953Srrs
2918163953Srrs			if (events->sctp_adaptation_layer_event) {
2919163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2920163953Srrs			} else {
2921163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2922163953Srrs			}
2923163953Srrs
2924163953Srrs			if (events->sctp_authentication_event) {
2925163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2926163953Srrs			} else {
2927163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2928163953Srrs			}
2929163953Srrs
2930163953Srrs			if (events->sctp_stream_reset_events) {
2931163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2932163953Srrs			} else {
2933163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2934163953Srrs			}
2935163953Srrs			SCTP_INP_WUNLOCK(inp);
2936163953Srrs		}
2937163953Srrs		break;
2938163953Srrs
2939163953Srrs	case SCTP_ADAPTATION_LAYER:
2940163953Srrs		{
2941163953Srrs			struct sctp_setadaptation *adap_bits;
2942163953Srrs
2943166675Srrs			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
2944163953Srrs			SCTP_INP_WLOCK(inp);
2945163953Srrs			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
2946163953Srrs			SCTP_INP_WUNLOCK(inp);
2947163953Srrs		}
2948163953Srrs		break;
2949166675Srrs#ifdef SCTP_DEBUG
2950163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
2951163953Srrs		{
2952163953Srrs			uint32_t *vvv;
2953163953Srrs
2954166675Srrs			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
2955163953Srrs			SCTP_INP_WLOCK(inp);
2956163953Srrs			inp->sctp_ep.initial_sequence_debug = *vvv;
2957163953Srrs			SCTP_INP_WUNLOCK(inp);
2958163953Srrs		}
2959163953Srrs		break;
2960166675Srrs#endif
2961163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2962163953Srrs		{
2963163953Srrs			struct sctp_sndrcvinfo *s_info;
2964163953Srrs
2965166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
2966166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2967163953Srrs
2968166675Srrs			if (stcb) {
2969166675Srrs				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
2970166675Srrs					stcb->asoc.def_send = *s_info;
2971163953Srrs				} else {
2972166675Srrs					error = EINVAL;
2973163953Srrs				}
2974166675Srrs				SCTP_TCB_UNLOCK(stcb);
2975166675Srrs			} else {
2976166675Srrs				SCTP_INP_WLOCK(inp);
2977163953Srrs				inp->def_send = *s_info;
2978166675Srrs				SCTP_INP_WUNLOCK(inp);
2979163953Srrs			}
2980163953Srrs		}
2981163953Srrs		break;
2982163953Srrs	case SCTP_PEER_ADDR_PARAMS:
2983163953Srrs		/* Applys to the specific association */
2984163953Srrs		{
2985163953Srrs			struct sctp_paddrparams *paddrp;
2986163953Srrs			struct sctp_nets *net;
2987163953Srrs
2988166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
2989166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2990163953Srrs			net = NULL;
2991166675Srrs			if (stcb) {
2992166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2993166675Srrs			} else {
2994166675Srrs				/*
2995166675Srrs				 * We increment here since
2996166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2997166675Srrs				 * decrement if it finds the stcb as long as
2998166675Srrs				 * the locked tcb (last argument) is NOT a
2999166675Srrs				 * TCB.. aka NULL.
3000166675Srrs				 */
3001166675Srrs				SCTP_INP_INCR_REF(inp);
3002166675Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3003166675Srrs				    (struct sockaddr *)&paddrp->spp_address,
3004166675Srrs				    &net, NULL, NULL);
3005163953Srrs				if (stcb == NULL) {
3006166675Srrs					SCTP_INP_DECR_REF(inp);
3007163953Srrs				}
3008163953Srrs			}
3009166675Srrs
3010166675Srrs
3011163953Srrs			if (stcb) {
3012163953Srrs				/************************TCB SPECIFIC SET ******************/
3013163953Srrs				/*
3014163953Srrs				 * do we change the timer for HB, we run
3015163953Srrs				 * only one?
3016163953Srrs				 */
3017163953Srrs				if (paddrp->spp_hbinterval)
3018163953Srrs					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3019163953Srrs				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3020163953Srrs					stcb->asoc.heart_beat_delay = 0;
3021163953Srrs
3022163953Srrs				/* network sets ? */
3023163953Srrs				if (net) {
3024163953Srrs					/************************NET SPECIFIC SET ******************/
3025163953Srrs					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3026163953Srrs						/* on demand HB */
3027163953Srrs						sctp_send_hb(stcb, 1, net);
3028163953Srrs					}
3029163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3030163953Srrs						net->dest_state |= SCTP_ADDR_NOHB;
3031163953Srrs					}
3032163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3033163953Srrs						net->dest_state &= ~SCTP_ADDR_NOHB;
3034163953Srrs					}
3035163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
3036165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3037165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3038165220Srrs							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3039163953Srrs						}
3040163953Srrs						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3041163953Srrs							net->mtu = paddrp->spp_pathmtu;
3042163953Srrs							if (net->mtu < stcb->asoc.smallest_mtu)
3043167695Srrs								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3044163953Srrs						}
3045163953Srrs					}
3046163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3047165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3048163953Srrs							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3049163953Srrs						}
3050163953Srrs					}
3051163953Srrs					if (paddrp->spp_pathmaxrxt)
3052163953Srrs						net->failure_threshold = paddrp->spp_pathmaxrxt;
3053167598Srrs#ifdef INET
3054163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3055163953Srrs						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3056163953Srrs							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3057163953Srrs						}
3058163953Srrs					}
3059163953Srrs#endif
3060167598Srrs#ifdef INET6
3061163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3062163953Srrs						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3063163953Srrs							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3064163953Srrs						}
3065163953Srrs					}
3066163953Srrs#endif
3067163953Srrs				} else {
3068163953Srrs					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3069163953Srrs					if (paddrp->spp_pathmaxrxt)
3070163953Srrs						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3071163953Srrs
3072163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3073163953Srrs						/* Turn back on the timer */
3074163953Srrs						stcb->asoc.hb_is_disabled = 0;
3075163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3076163953Srrs					}
3077163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3078163953Srrs						int cnt_of_unconf = 0;
3079163953Srrs						struct sctp_nets *lnet;
3080163953Srrs
3081163953Srrs						stcb->asoc.hb_is_disabled = 1;
3082163953Srrs						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3083163953Srrs							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3084163953Srrs								cnt_of_unconf++;
3085163953Srrs							}
3086163953Srrs						}
3087163953Srrs						/*
3088163953Srrs						 * stop the timer ONLY if we
3089163953Srrs						 * have no unconfirmed
3090163953Srrs						 * addresses
3091163953Srrs						 */
3092163953Srrs						if (cnt_of_unconf == 0) {
3093165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3094163953Srrs						}
3095163953Srrs					}
3096163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3097163953Srrs						/* start up the timer. */
3098163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3099163953Srrs					}
3100167598Srrs#ifdef INET
3101163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS)
3102163953Srrs						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3103163953Srrs#endif
3104167598Srrs#ifdef INET6
3105163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3106163953Srrs						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3107163953Srrs#endif
3108163953Srrs
3109163953Srrs				}
3110163953Srrs				SCTP_TCB_UNLOCK(stcb);
3111163953Srrs			} else {
3112163953Srrs				/************************NO TCB, SET TO default stuff ******************/
3113163953Srrs				SCTP_INP_WLOCK(inp);
3114163953Srrs				/*
3115163953Srrs				 * For the TOS/FLOWLABEL stuff you set it
3116163953Srrs				 * with the options on the socket
3117163953Srrs				 */
3118163953Srrs				if (paddrp->spp_pathmaxrxt) {
3119163953Srrs					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3120163953Srrs				}
3121163953Srrs				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3122163953Srrs					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3123163953Srrs					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3124163953Srrs				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3125163953Srrs					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3126163953Srrs				}
3127163953Srrs				SCTP_INP_WUNLOCK(inp);
3128163953Srrs			}
3129163953Srrs		}
3130163953Srrs		break;
3131163953Srrs	case SCTP_RTOINFO:
3132163953Srrs		{
3133163953Srrs			struct sctp_rtoinfo *srto;
3134163953Srrs
3135166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3136166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3137166675Srrs
3138166675Srrs			if (stcb) {
3139166675Srrs				/* Set in ms we hope :-) */
3140167598Srrs				if (srto->srto_initial)
3141166675Srrs					stcb->asoc.initial_rto = srto->srto_initial;
3142167598Srrs				if (srto->srto_max)
3143166675Srrs					stcb->asoc.maxrto = srto->srto_max;
3144167598Srrs				if (srto->srto_min)
3145166675Srrs					stcb->asoc.minrto = srto->srto_min;
3146166675Srrs				SCTP_TCB_UNLOCK(stcb);
3147166675Srrs			} else {
3148163953Srrs				SCTP_INP_WLOCK(inp);
3149163953Srrs				/*
3150163953Srrs				 * If we have a null asoc, its default for
3151163953Srrs				 * the endpoint
3152163953Srrs				 */
3153167598Srrs				if (srto->srto_initial)
3154163953Srrs					inp->sctp_ep.initial_rto = srto->srto_initial;
3155167598Srrs				if (srto->srto_max)
3156163953Srrs					inp->sctp_ep.sctp_maxrto = srto->srto_max;
3157167598Srrs				if (srto->srto_min)
3158163953Srrs					inp->sctp_ep.sctp_minrto = srto->srto_min;
3159163953Srrs				SCTP_INP_WUNLOCK(inp);
3160163953Srrs			}
3161163953Srrs		}
3162163953Srrs		break;
3163163953Srrs	case SCTP_ASSOCINFO:
3164163953Srrs		{
3165163953Srrs			struct sctp_assocparams *sasoc;
3166163953Srrs
3167166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3168166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3169166675Srrs
3170163953Srrs			if (stcb) {
3171163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3172163953Srrs					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3173163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3174163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3175163953Srrs				sasoc->sasoc_local_rwnd = 0;
3176163953Srrs				if (stcb->asoc.cookie_life)
3177163953Srrs					stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3178167598Srrs				stcb->asoc.delayed_ack = sasoc->sasoc_sack_delay;
3179167598Srrs				if (sasoc->sasoc_sack_freq) {
3180167598Srrs					stcb->asoc.sack_freq = sasoc->sasoc_sack_freq;
3181167598Srrs				}
3182163953Srrs				SCTP_TCB_UNLOCK(stcb);
3183163953Srrs			} else {
3184163953Srrs				SCTP_INP_WLOCK(inp);
3185163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3186163953Srrs					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3187163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
3188163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3189163953Srrs				sasoc->sasoc_local_rwnd = 0;
3190163953Srrs				if (sasoc->sasoc_cookie_life)
3191163953Srrs					inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3192167598Srrs				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sasoc->sasoc_sack_delay);
3193167598Srrs				if (sasoc->sasoc_sack_freq) {
3194167598Srrs					inp->sctp_ep.sctp_sack_freq = sasoc->sasoc_sack_freq;
3195167598Srrs				}
3196163953Srrs				SCTP_INP_WUNLOCK(inp);
3197163953Srrs			}
3198163953Srrs		}
3199163953Srrs		break;
3200163953Srrs	case SCTP_INITMSG:
3201163953Srrs		{
3202163953Srrs			struct sctp_initmsg *sinit;
3203163953Srrs
3204166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3205163953Srrs			SCTP_INP_WLOCK(inp);
3206163953Srrs			if (sinit->sinit_num_ostreams)
3207163953Srrs				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3208163953Srrs
3209163953Srrs			if (sinit->sinit_max_instreams)
3210163953Srrs				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3211163953Srrs
3212163953Srrs			if (sinit->sinit_max_attempts)
3213163953Srrs				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3214163953Srrs
3215167598Srrs			if (sinit->sinit_max_init_timeo)
3216163953Srrs				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3217163953Srrs			SCTP_INP_WUNLOCK(inp);
3218163953Srrs		}
3219163953Srrs		break;
3220163953Srrs	case SCTP_PRIMARY_ADDR:
3221163953Srrs		{
3222163953Srrs			struct sctp_setprim *spa;
3223163953Srrs			struct sctp_nets *net, *lnet;
3224163953Srrs
3225166675Srrs			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3226166675Srrs			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3227163953Srrs
3228166675Srrs			net = NULL;
3229166675Srrs			if (stcb) {
3230166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3231166675Srrs			} else {
3232166675Srrs				/*
3233166675Srrs				 * We increment here since
3234166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3235166675Srrs				 * decrement if it finds the stcb as long as
3236166675Srrs				 * the locked tcb (last argument) is NOT a
3237166675Srrs				 * TCB.. aka NULL.
3238166675Srrs				 */
3239163953Srrs				SCTP_INP_INCR_REF(inp);
3240163953Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3241163953Srrs				    (struct sockaddr *)&spa->ssp_addr,
3242163953Srrs				    &net, NULL, NULL);
3243163953Srrs				if (stcb == NULL) {
3244163953Srrs					SCTP_INP_DECR_REF(inp);
3245163953Srrs				}
3246163953Srrs			}
3247166675Srrs
3248166675Srrs			if ((stcb) && (net)) {
3249166675Srrs				if ((net != stcb->asoc.primary_destination) &&
3250166675Srrs				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3251166675Srrs					/* Ok we need to set it */
3252166675Srrs					lnet = stcb->asoc.primary_destination;
3253166675Srrs					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3254166675Srrs						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3255166675Srrs							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3256166675Srrs						}
3257166675Srrs						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3258163953Srrs					}
3259163953Srrs				}
3260166675Srrs			} else {
3261166675Srrs				error = EINVAL;
3262163953Srrs			}
3263166675Srrs			if (stcb) {
3264166675Srrs				SCTP_TCB_UNLOCK(stcb);
3265166675Srrs			}
3266163953Srrs		}
3267163953Srrs		break;
3268167598Srrs	case SCTP_SET_DYNAMIC_PRIMARY:
3269167598Srrs		{
3270167598Srrs			union sctp_sockstore *ss;
3271163953Srrs
3272167598Srrs			error = priv_check_cred(curthread->td_ucred,
3273167598Srrs			    PRIV_NETINET_RESERVEDPORT,
3274167598Srrs			    SUSER_ALLOWJAIL);
3275167598Srrs			if (error)
3276167598Srrs				break;
3277167598Srrs
3278167598Srrs			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3279167598Srrs			/* SUPER USER CHECK? */
3280167598Srrs			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3281167598Srrs		}
3282167598Srrs		break;
3283163953Srrs	case SCTP_SET_PEER_PRIMARY_ADDR:
3284163953Srrs		{
3285163953Srrs			struct sctp_setpeerprim *sspp;
3286163953Srrs
3287166675Srrs			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3288166675Srrs			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3289163953Srrs
3290166675Srrs			if (stcb) {
3291166675Srrs				if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3292166675Srrs					error = EINVAL;
3293166675Srrs				}
3294166675Srrs			} else {
3295163953Srrs				error = EINVAL;
3296163953Srrs			}
3297163953Srrs			SCTP_TCB_UNLOCK(stcb);
3298163953Srrs		}
3299163953Srrs		break;
3300163953Srrs	case SCTP_BINDX_ADD_ADDR:
3301163953Srrs		{
3302163953Srrs			struct sctp_getaddresses *addrs;
3303163953Srrs			struct sockaddr *addr_touse;
3304163953Srrs			struct sockaddr_in sin;
3305163953Srrs
3306166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3307166675Srrs
3308163953Srrs			/* see if we're bound all already! */
3309163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3310163953Srrs				error = EINVAL;
3311163953Srrs				break;
3312163953Srrs			}
3313167598Srrs			/* Is the VRF one we have */
3314163953Srrs			addr_touse = addrs->addr;
3315167695Srrs#if defined(INET6)
3316163953Srrs			if (addrs->addr->sa_family == AF_INET6) {
3317163953Srrs				struct sockaddr_in6 *sin6;
3318163953Srrs
3319163953Srrs				sin6 = (struct sockaddr_in6 *)addr_touse;
3320163953Srrs				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3321163953Srrs					in6_sin6_2_sin(&sin, sin6);
3322163953Srrs					addr_touse = (struct sockaddr *)&sin;
3323163953Srrs				}
3324163953Srrs			}
3325167695Srrs#endif
3326163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3327163953Srrs				if (p == NULL) {
3328163953Srrs					/* Can't get proc for Net/Open BSD */
3329163953Srrs					error = EINVAL;
3330163953Srrs					break;
3331163953Srrs				}
3332163953Srrs				error = sctp_inpcb_bind(so, addr_touse, p);
3333163953Srrs				break;
3334163953Srrs			}
3335163953Srrs			/*
3336163953Srrs			 * No locks required here since bind and mgmt_ep_sa
3337163953Srrs			 * all do their own locking. If we do something for
3338163953Srrs			 * the FIX: below we may need to lock in that case.
3339163953Srrs			 */
3340163953Srrs			if (addrs->sget_assoc_id == 0) {
3341163953Srrs				/* add the address */
3342163953Srrs				struct sctp_inpcb *lep;
3343163953Srrs
3344163953Srrs				((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3345167598Srrs				lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
3346163953Srrs				if (lep != NULL) {
3347163953Srrs					/*
3348163953Srrs					 * We must decrement the refcount
3349163953Srrs					 * since we have the ep already and
3350163953Srrs					 * are binding. No remove going on
3351163953Srrs					 * here.
3352163953Srrs					 */
3353163953Srrs					SCTP_INP_DECR_REF(inp);
3354163953Srrs				}
3355163953Srrs				if (lep == inp) {
3356163953Srrs					/* already bound to it.. ok */
3357163953Srrs					break;
3358163953Srrs				} else if (lep == NULL) {
3359163953Srrs					((struct sockaddr_in *)addr_touse)->sin_port = 0;
3360163953Srrs					error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3361167598Srrs					    SCTP_ADD_IP_ADDRESS, vrf_id);
3362163953Srrs				} else {
3363163953Srrs					error = EADDRNOTAVAIL;
3364163953Srrs				}
3365163953Srrs				if (error)
3366163953Srrs					break;
3367163953Srrs
3368163953Srrs			} else {
3369163953Srrs				/*
3370163953Srrs				 * FIX: decide whether we allow assoc based
3371163953Srrs				 * bindx
3372163953Srrs				 */
3373163953Srrs			}
3374163953Srrs		}
3375163953Srrs		break;
3376163953Srrs	case SCTP_BINDX_REM_ADDR:
3377163953Srrs		{
3378163953Srrs			struct sctp_getaddresses *addrs;
3379163953Srrs			struct sockaddr *addr_touse;
3380163953Srrs			struct sockaddr_in sin;
3381163953Srrs
3382166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3383163953Srrs			/* see if we're bound all already! */
3384163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3385163953Srrs				error = EINVAL;
3386163953Srrs				break;
3387163953Srrs			}
3388163953Srrs			addr_touse = addrs->addr;
3389167695Srrs#if defined(INET6)
3390163953Srrs			if (addrs->addr->sa_family == AF_INET6) {
3391163953Srrs				struct sockaddr_in6 *sin6;
3392163953Srrs
3393163953Srrs				sin6 = (struct sockaddr_in6 *)addr_touse;
3394163953Srrs				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3395163953Srrs					in6_sin6_2_sin(&sin, sin6);
3396163953Srrs					addr_touse = (struct sockaddr *)&sin;
3397163953Srrs				}
3398163953Srrs			}
3399167695Srrs#endif
3400163953Srrs			/*
3401163953Srrs			 * No lock required mgmt_ep_sa does its own locking.
3402163953Srrs			 * If the FIX: below is ever changed we may need to
3403163953Srrs			 * lock before calling association level binding.
3404163953Srrs			 */
3405163953Srrs			if (addrs->sget_assoc_id == 0) {
3406163953Srrs				/* delete the address */
3407163953Srrs				sctp_addr_mgmt_ep_sa(inp, addr_touse,
3408167598Srrs				    SCTP_DEL_IP_ADDRESS, vrf_id);
3409163953Srrs			} else {
3410163953Srrs				/*
3411163953Srrs				 * FIX: decide whether we allow assoc based
3412163953Srrs				 * bindx
3413163953Srrs				 */
3414163953Srrs			}
3415163953Srrs		}
3416163953Srrs		break;
3417163953Srrs	default:
3418163953Srrs		error = ENOPROTOOPT;
3419163953Srrs		break;
3420163953Srrs	}			/* end switch (opt) */
3421163953Srrs	return (error);
3422163953Srrs}
3423163953Srrs
3424163953Srrs
3425163953Srrsint
3426163953Srrssctp_ctloutput(struct socket *so, struct sockopt *sopt)
3427163953Srrs{
3428166675Srrs	void *optval = NULL;
3429166675Srrs	size_t optsize = 0;
3430163953Srrs	struct sctp_inpcb *inp;
3431166675Srrs	void *p;
3432166675Srrs	int error = 0;
3433163953Srrs
3434163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3435163953Srrs	if (inp == 0) {
3436163953Srrs		/* I made the same as TCP since we are not setup? */
3437163953Srrs		return (ECONNRESET);
3438163953Srrs	}
3439163953Srrs	if (sopt->sopt_level != IPPROTO_SCTP) {
3440163953Srrs		/* wrong proto level... send back up to IP */
3441163953Srrs#ifdef INET6
3442163953Srrs		if (INP_CHECK_SOCKAF(so, AF_INET6))
3443163953Srrs			error = ip6_ctloutput(so, sopt);
3444163953Srrs		else
3445163953Srrs#endif				/* INET6 */
3446163953Srrs			error = ip_ctloutput(so, sopt);
3447163953Srrs		return (error);
3448163953Srrs	}
3449166675Srrs	optsize = sopt->sopt_valsize;
3450166675Srrs	if (optsize) {
3451166675Srrs		SCTP_MALLOC(optval, void *, optsize, "SCTPSockOpt");
3452166675Srrs		if (optval == NULL) {
3453163953Srrs			return (ENOBUFS);
3454163953Srrs		}
3455166675Srrs		error = sooptcopyin(sopt, optval, optsize, optsize);
3456163953Srrs		if (error) {
3457166675Srrs			SCTP_FREE(optval);
3458163953Srrs			goto out;
3459163953Srrs		}
3460163953Srrs	}
3461166675Srrs	p = (void *)sopt->sopt_td;
3462163953Srrs	if (sopt->sopt_dir == SOPT_SET) {
3463166675Srrs		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3464163953Srrs	} else if (sopt->sopt_dir == SOPT_GET) {
3465166675Srrs		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3466163953Srrs	} else {
3467163953Srrs		error = EINVAL;
3468163953Srrs	}
3469166675Srrs	if ((error == 0) && (optval != NULL)) {
3470166675Srrs		error = sooptcopyout(sopt, optval, optsize);
3471166675Srrs		SCTP_FREE(optval);
3472166675Srrs	} else if (optval != NULL) {
3473166675Srrs		SCTP_FREE(optval);
3474163953Srrs	}
3475163953Srrsout:
3476163953Srrs	return (error);
3477163953Srrs}
3478163953Srrs
3479163953Srrs
3480163953Srrsstatic int
3481163953Srrssctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3482163953Srrs{
3483163953Srrs	int error = 0;
3484163953Srrs	int create_lock_on = 0;
3485167598Srrs	uint32_t vrf_id;
3486163953Srrs	struct sctp_inpcb *inp;
3487163953Srrs	struct sctp_tcb *stcb = NULL;
3488163953Srrs
3489163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3490163953Srrs	if (inp == 0) {
3491163953Srrs		/* I made the same as TCP since we are not setup? */
3492163953Srrs		return (ECONNRESET);
3493163953Srrs	}
3494163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
3495163953Srrs	create_lock_on = 1;
3496163953Srrs
3497163953Srrs	SCTP_INP_INCR_REF(inp);
3498163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3499163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3500163953Srrs		/* Should I really unlock ? */
3501163953Srrs		error = EFAULT;
3502163953Srrs		goto out_now;
3503163953Srrs	}
3504163953Srrs#ifdef INET6
3505163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3506163953Srrs	    (addr->sa_family == AF_INET6)) {
3507163953Srrs		error = EINVAL;
3508163953Srrs		goto out_now;
3509163953Srrs	}
3510163953Srrs#endif				/* INET6 */
3511163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3512163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
3513163953Srrs		/* Bind a ephemeral port */
3514163953Srrs		error = sctp_inpcb_bind(so, NULL, p);
3515163953Srrs		if (error) {
3516163953Srrs			goto out_now;
3517163953Srrs		}
3518163953Srrs	}
3519163953Srrs	/* Now do we connect? */
3520163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3521163953Srrs		error = EINVAL;
3522163953Srrs		goto out_now;
3523163953Srrs	}
3524163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3525163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3526163953Srrs		/* We are already connected AND the TCP model */
3527163953Srrs		error = EADDRINUSE;
3528163953Srrs		goto out_now;
3529163953Srrs	}
3530163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3531163953Srrs		SCTP_INP_RLOCK(inp);
3532163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3533163953Srrs		if (stcb)
3534163953Srrs			SCTP_TCB_UNLOCK(stcb);
3535163953Srrs		SCTP_INP_RUNLOCK(inp);
3536163953Srrs	} else {
3537163953Srrs		/*
3538166675Srrs		 * We increment here since sctp_findassociation_ep_addr()
3539166675Srrs		 * wil do a decrement if it finds the stcb as long as the
3540166675Srrs		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3541163953Srrs		 */
3542163953Srrs		SCTP_INP_INCR_REF(inp);
3543163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3544163953Srrs		if (stcb == NULL) {
3545163953Srrs			SCTP_INP_DECR_REF(inp);
3546163953Srrs		}
3547163953Srrs	}
3548163953Srrs	if (stcb != NULL) {
3549163953Srrs		/* Already have or am bring up an association */
3550163953Srrs		error = EALREADY;
3551163953Srrs		goto out_now;
3552163953Srrs	}
3553167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
3554163953Srrs	/* We are GOOD to go */
3555167598Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3556163953Srrs	if (stcb == NULL) {
3557163953Srrs		/* Gak! no memory */
3558167598Srrs		goto out_now;
3559163953Srrs	}
3560163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3561163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3562163953Srrs		/* Set the connected flag so we can queue data */
3563163953Srrs		soisconnecting(so);
3564163953Srrs	}
3565163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3566163953Srrs	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3567163953Srrs
3568163953Srrs	/* initialize authentication parameters for the assoc */
3569163953Srrs	sctp_initialize_auth_params(inp, stcb);
3570163953Srrs
3571163953Srrs	sctp_send_initiate(inp, stcb);
3572163953Srrsout_now:
3573163953Srrs	if (create_lock_on)
3574163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
3575163953Srrs
3576163953Srrs	if (stcb)
3577163953Srrs		SCTP_TCB_UNLOCK(stcb);
3578163953Srrs	SCTP_INP_DECR_REF(inp);
3579163953Srrs	return error;
3580163953Srrs}
3581163953Srrs
3582163953Srrsint
3583163953Srrssctp_listen(struct socket *so, int backlog, struct thread *p)
3584163953Srrs{
3585163953Srrs	/*
3586163953Srrs	 * Note this module depends on the protocol processing being called
3587163953Srrs	 * AFTER any socket level flags and backlog are applied to the
3588163953Srrs	 * socket. The traditional way that the socket flags are applied is
3589163953Srrs	 * AFTER protocol processing. We have made a change to the
3590163953Srrs	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3591163953Srrs	 * place if the socket API for SCTP is to work properly.
3592163953Srrs	 */
3593163953Srrs
3594163953Srrs	int error = 0;
3595163953Srrs	struct sctp_inpcb *inp;
3596163953Srrs
3597163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3598163953Srrs	if (inp == 0) {
3599163953Srrs		/* I made the same as TCP since we are not setup? */
3600163953Srrs		return (ECONNRESET);
3601163953Srrs	}
3602163953Srrs	SCTP_INP_RLOCK(inp);
3603163953Srrs#ifdef SCTP_LOCK_LOGGING
3604163953Srrs	sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3605163953Srrs#endif
3606163953Srrs	SOCK_LOCK(so);
3607163953Srrs	error = solisten_proto_check(so);
3608163953Srrs	if (error) {
3609163953Srrs		SOCK_UNLOCK(so);
3610163953Srrs		return (error);
3611163953Srrs	}
3612163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3613163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3614163953Srrs		/* We are already connected AND the TCP model */
3615163953Srrs		SCTP_INP_RUNLOCK(inp);
3616163953Srrs		SOCK_UNLOCK(so);
3617163953Srrs		return (EADDRINUSE);
3618163953Srrs	}
3619163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3620163953Srrs		/* We must do a bind. */
3621166675Srrs		SOCK_UNLOCK(so);
3622163953Srrs		SCTP_INP_RUNLOCK(inp);
3623163953Srrs		if ((error = sctp_inpcb_bind(so, NULL, p))) {
3624163953Srrs			/* bind error, probably perm */
3625163953Srrs			return (error);
3626163953Srrs		}
3627166675Srrs		SOCK_LOCK(so);
3628163953Srrs	} else {
3629163953Srrs		SCTP_INP_RUNLOCK(inp);
3630163953Srrs	}
3631163953Srrs	/* It appears for 7.0 and on, we must always call this. */
3632163953Srrs	solisten_proto(so, backlog);
3633163953Srrs
3634163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3635163953Srrs		/* remove the ACCEPTCONN flag for one-to-many sockets */
3636163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3637163953Srrs	}
3638163953Srrs	if (backlog == 0) {
3639163953Srrs		/* turning off listen */
3640163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3641163953Srrs	}
3642163953Srrs	SOCK_UNLOCK(so);
3643163953Srrs	return (error);
3644163953Srrs}
3645163953Srrs
3646163953Srrsstatic int sctp_defered_wakeup_cnt = 0;
3647163953Srrs
3648163953Srrsint
3649163953Srrssctp_accept(struct socket *so, struct sockaddr **addr)
3650163953Srrs{
3651163953Srrs	struct sctp_tcb *stcb;
3652163953Srrs	struct sctp_inpcb *inp;
3653163953Srrs	union sctp_sockstore store;
3654163953Srrs
3655163953Srrs	int error;
3656163953Srrs
3657163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3658163953Srrs
3659163953Srrs	if (inp == 0) {
3660163953Srrs		return (ECONNRESET);
3661163953Srrs	}
3662163953Srrs	SCTP_INP_RLOCK(inp);
3663163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3664163953Srrs		return (ENOTSUP);
3665163953Srrs	}
3666163953Srrs	if (so->so_state & SS_ISDISCONNECTED) {
3667163953Srrs		SCTP_INP_RUNLOCK(inp);
3668163953Srrs		return (ECONNABORTED);
3669163953Srrs	}
3670163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3671163953Srrs	if (stcb == NULL) {
3672163953Srrs		SCTP_INP_RUNLOCK(inp);
3673163953Srrs		return (ECONNRESET);
3674163953Srrs	}
3675163953Srrs	SCTP_TCB_LOCK(stcb);
3676163953Srrs	SCTP_INP_RUNLOCK(inp);
3677163953Srrs	store = stcb->asoc.primary_destination->ro._l_addr;
3678163953Srrs	SCTP_TCB_UNLOCK(stcb);
3679163953Srrs	if (store.sa.sa_family == AF_INET) {
3680163953Srrs		struct sockaddr_in *sin;
3681163953Srrs
3682163953Srrs		SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3683163953Srrs		sin->sin_family = AF_INET;
3684163953Srrs		sin->sin_len = sizeof(*sin);
3685163953Srrs		sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3686163953Srrs		sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3687163953Srrs		*addr = (struct sockaddr *)sin;
3688163953Srrs	} else {
3689163953Srrs		struct sockaddr_in6 *sin6;
3690163953Srrs
3691163953Srrs		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3692163953Srrs		sin6->sin6_family = AF_INET6;
3693163953Srrs		sin6->sin6_len = sizeof(*sin6);
3694163953Srrs		sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3695163953Srrs
3696163953Srrs		sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3697164085Srrs		if ((error = sa6_recoverscope(sin6)) != 0) {
3698164085Srrs			SCTP_FREE_SONAME(sin6);
3699163953Srrs			return (error);
3700164085Srrs		}
3701163953Srrs		*addr = (struct sockaddr *)sin6;
3702163953Srrs	}
3703163953Srrs	/* Wake any delayed sleep action */
3704163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3705166086Srrs		SCTP_INP_WLOCK(inp);
3706163953Srrs		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3707163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3708163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3709166086Srrs			SCTP_INP_WUNLOCK(inp);
3710163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3711163953Srrs			if (sowriteable(inp->sctp_socket)) {
3712163953Srrs				sowwakeup_locked(inp->sctp_socket);
3713163953Srrs			} else {
3714163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3715163953Srrs			}
3716166086Srrs			SCTP_INP_WLOCK(inp);
3717163953Srrs		}
3718163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3719163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3720166086Srrs			SCTP_INP_WUNLOCK(inp);
3721163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3722163953Srrs			if (soreadable(inp->sctp_socket)) {
3723163953Srrs				sctp_defered_wakeup_cnt++;
3724163953Srrs				sorwakeup_locked(inp->sctp_socket);
3725163953Srrs			} else {
3726163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3727163953Srrs			}
3728166086Srrs			SCTP_INP_WLOCK(inp);
3729163953Srrs		}
3730166086Srrs		SCTP_INP_WUNLOCK(inp);
3731163953Srrs	}
3732163953Srrs	return (0);
3733163953Srrs}
3734163953Srrs
3735163953Srrsint
3736163953Srrssctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3737163953Srrs{
3738163953Srrs	struct sockaddr_in *sin;
3739167598Srrs	uint32_t vrf_id;
3740163953Srrs	struct sctp_inpcb *inp;
3741167695Srrs	struct sctp_ifa *sctp_ifa;
3742163953Srrs
3743163953Srrs	/*
3744163953Srrs	 * Do the malloc first in case it blocks.
3745163953Srrs	 */
3746163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3747163953Srrs	sin->sin_family = AF_INET;
3748163953Srrs	sin->sin_len = sizeof(*sin);
3749163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3750163953Srrs	if (!inp) {
3751163953Srrs		SCTP_FREE_SONAME(sin);
3752163953Srrs		return ECONNRESET;
3753163953Srrs	}
3754163953Srrs	SCTP_INP_RLOCK(inp);
3755163953Srrs	sin->sin_port = inp->sctp_lport;
3756163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3757163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3758163953Srrs			struct sctp_tcb *stcb;
3759163953Srrs			struct sockaddr_in *sin_a;
3760163953Srrs			struct sctp_nets *net;
3761163953Srrs			int fnd;
3762163953Srrs
3763163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3764163953Srrs			if (stcb == NULL) {
3765163953Srrs				goto notConn;
3766163953Srrs			}
3767163953Srrs			fnd = 0;
3768163953Srrs			sin_a = NULL;
3769163953Srrs			SCTP_TCB_LOCK(stcb);
3770163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3771163953Srrs				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3772164085Srrs				if (sin_a == NULL)
3773164085Srrs					/* this will make coverity happy */
3774164085Srrs					continue;
3775164085Srrs
3776163953Srrs				if (sin_a->sin_family == AF_INET) {
3777163953Srrs					fnd = 1;
3778163953Srrs					break;
3779163953Srrs				}
3780163953Srrs			}
3781163953Srrs			if ((!fnd) || (sin_a == NULL)) {
3782163953Srrs				/* punt */
3783163953Srrs				SCTP_TCB_UNLOCK(stcb);
3784163953Srrs				goto notConn;
3785163953Srrs			}
3786167598Srrs			vrf_id = SCTP_DEFAULT_VRFID;
3787167598Srrs
3788167598Srrs			sctp_ifa = sctp_source_address_selection(inp,
3789167598Srrs			    stcb,
3790167598Srrs			    (struct route *)&net->ro,
3791167598Srrs			    net, 0, vrf_id);
3792167598Srrs			if (sctp_ifa) {
3793167598Srrs				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
3794167598Srrs				sctp_free_ifa(sctp_ifa);
3795167598Srrs			}
3796163953Srrs			SCTP_TCB_UNLOCK(stcb);
3797163953Srrs		} else {
3798163953Srrs			/* For the bound all case you get back 0 */
3799163953Srrs	notConn:
3800163953Srrs			sin->sin_addr.s_addr = 0;
3801163953Srrs		}
3802163953Srrs
3803163953Srrs	} else {
3804163953Srrs		/* Take the first IPv4 address in the list */
3805163953Srrs		struct sctp_laddr *laddr;
3806163953Srrs		int fnd = 0;
3807163953Srrs
3808163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3809167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
3810163953Srrs				struct sockaddr_in *sin_a;
3811163953Srrs
3812167598Srrs				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
3813163953Srrs				sin->sin_addr = sin_a->sin_addr;
3814163953Srrs				fnd = 1;
3815163953Srrs				break;
3816163953Srrs			}
3817163953Srrs		}
3818163953Srrs		if (!fnd) {
3819163953Srrs			SCTP_FREE_SONAME(sin);
3820163953Srrs			SCTP_INP_RUNLOCK(inp);
3821163953Srrs			return ENOENT;
3822163953Srrs		}
3823163953Srrs	}
3824163953Srrs	SCTP_INP_RUNLOCK(inp);
3825163953Srrs	(*addr) = (struct sockaddr *)sin;
3826163953Srrs	return (0);
3827163953Srrs}
3828163953Srrs
3829163953Srrsint
3830163953Srrssctp_peeraddr(struct socket *so, struct sockaddr **addr)
3831163953Srrs{
3832163953Srrs	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
3833166086Srrs	int fnd;
3834163953Srrs	struct sockaddr_in *sin_a;
3835163953Srrs	struct sctp_inpcb *inp;
3836163953Srrs	struct sctp_tcb *stcb;
3837163953Srrs	struct sctp_nets *net;
3838163953Srrs
3839163953Srrs	/* Do the malloc first in case it blocks. */
3840163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3841163953Srrs	if ((inp == NULL) ||
3842163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3843163953Srrs		/* UDP type and listeners will drop out here */
3844163953Srrs		return (ENOTCONN);
3845163953Srrs	}
3846163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3847163953Srrs	sin->sin_family = AF_INET;
3848163953Srrs	sin->sin_len = sizeof(*sin);
3849163953Srrs
3850163953Srrs	/* We must recapture incase we blocked */
3851163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3852163953Srrs	if (!inp) {
3853163953Srrs		SCTP_FREE_SONAME(sin);
3854163953Srrs		return ECONNRESET;
3855163953Srrs	}
3856163953Srrs	SCTP_INP_RLOCK(inp);
3857163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3858163953Srrs	if (stcb)
3859163953Srrs		SCTP_TCB_LOCK(stcb);
3860163953Srrs	SCTP_INP_RUNLOCK(inp);
3861163953Srrs	if (stcb == NULL) {
3862163953Srrs		SCTP_FREE_SONAME(sin);
3863163953Srrs		return ECONNRESET;
3864163953Srrs	}
3865163953Srrs	fnd = 0;
3866163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3867163953Srrs		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3868163953Srrs		if (sin_a->sin_family == AF_INET) {
3869163953Srrs			fnd = 1;
3870163953Srrs			sin->sin_port = stcb->rport;
3871163953Srrs			sin->sin_addr = sin_a->sin_addr;
3872163953Srrs			break;
3873163953Srrs		}
3874163953Srrs	}
3875163953Srrs	SCTP_TCB_UNLOCK(stcb);
3876163953Srrs	if (!fnd) {
3877163953Srrs		/* No IPv4 address */
3878163953Srrs		SCTP_FREE_SONAME(sin);
3879163953Srrs		return ENOENT;
3880163953Srrs	}
3881163953Srrs	(*addr) = (struct sockaddr *)sin;
3882163953Srrs	return (0);
3883163953Srrs}
3884163953Srrs
3885163953Srrsstruct pr_usrreqs sctp_usrreqs = {
3886163953Srrs	.pru_abort = sctp_abort,
3887163953Srrs	.pru_accept = sctp_accept,
3888163953Srrs	.pru_attach = sctp_attach,
3889163953Srrs	.pru_bind = sctp_bind,
3890163953Srrs	.pru_connect = sctp_connect,
3891163953Srrs	.pru_control = in_control,
3892163953Srrs	.pru_close = sctp_close,
3893163953Srrs	.pru_detach = sctp_close,
3894163953Srrs	.pru_sopoll = sopoll_generic,
3895163953Srrs	.pru_disconnect = sctp_disconnect,
3896163953Srrs	.pru_listen = sctp_listen,
3897163953Srrs	.pru_peeraddr = sctp_peeraddr,
3898163953Srrs	.pru_send = sctp_sendm,
3899163953Srrs	.pru_shutdown = sctp_shutdown,
3900163953Srrs	.pru_sockaddr = sctp_ingetaddr,
3901163953Srrs	.pru_sosend = sctp_sosend,
3902163953Srrs	.pru_soreceive = sctp_soreceive
3903163953Srrs};
3904