sctp_usrreq.c revision 169352
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 169352 2007-05-08 00:21:05Z 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_uio.h>
46163953Srrs#include <netinet/sctp_asconf.h>
47163953Srrs#include <netinet/sctputil.h>
48163953Srrs#include <netinet/sctp_indata.h>
49163953Srrs#include <netinet/sctp_timer.h>
50163953Srrs#include <netinet/sctp_auth.h>
51164085Srrs
52163953Srrs
53163953Srrs
54163953Srrsvoid
55163953Srrssctp_init(void)
56163953Srrs{
57163953Srrs	/* Init the SCTP pcb in sctp_pcb.c */
58163953Srrs	u_long sb_max_adj;
59163953Srrs
60163953Srrs	sctp_pcb_init();
61163953Srrs
62163953Srrs	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
63163953Srrs		sctp_max_chunks_on_queue = (nmbclusters / 8);
64163953Srrs	/*
65163953Srrs	 * Allow a user to take no more than 1/2 the number of clusters or
66163953Srrs	 * the SB_MAX whichever is smaller for the send window.
67163953Srrs	 */
68163953Srrs	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
69163953Srrs	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
70163953Srrs	    ((nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
71163953Srrs	/*
72163953Srrs	 * Now for the recv window, should we take the same amount? or
73163953Srrs	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
74163953Srrs	 * now I will just copy.
75163953Srrs	 */
76163953Srrs	sctp_recvspace = sctp_sendspace;
77163953Srrs
78163953Srrs
79163953Srrs}
80163953Srrs
81163953Srrs
82166023Srrs
83166023Srrs/*
84166023Srrs * cleanup of the sctppcbinfo structure.
85166023Srrs * Assumes that the sctppcbinfo lock is held.
86166023Srrs */
87166023Srrsvoid
88166023Srrssctp_pcbinfo_cleanup(void)
89166023Srrs{
90166023Srrs	/* free the hash tables */
91166023Srrs	if (sctppcbinfo.sctp_asochash != NULL)
92166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
93166023Srrs	if (sctppcbinfo.sctp_ephash != NULL)
94166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
95166023Srrs	if (sctppcbinfo.sctp_tcpephash != NULL)
96166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
97166023Srrs	if (sctppcbinfo.sctp_restarthash != NULL)
98166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
99166023Srrs}
100166023Srrs
101163953Srrs
102163953Srrsstatic void
103167695Srrssctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104163953Srrs    struct sctp_tcb *stcb,
105163953Srrs    struct sctp_nets *net,
106163953Srrs    uint16_t nxtsz)
107163953Srrs{
108163953Srrs	struct sctp_tmit_chunk *chk;
109163953Srrs
110163953Srrs	/* Adjust that too */
111163953Srrs	stcb->asoc.smallest_mtu = nxtsz;
112163953Srrs	/* now off to subtract IP_DF flag if needed */
113169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
114169352Srrs	printf("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
115169352Srrs	    inp, stcb, net, nxtsz);
116169352Srrs#endif
117163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
118163953Srrs		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
119163953Srrs			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
120163953Srrs		}
121163953Srrs	}
122163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
123163953Srrs		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
124163953Srrs			/*
125163953Srrs			 * For this guy we also mark for immediate resend
126163953Srrs			 * since we sent to big of chunk
127163953Srrs			 */
128163953Srrs			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
129163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
130163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
131163953Srrs			}
132163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
133163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
134168709Srrs#ifdef SCTP_FLIGHT_LOGGING
135168709Srrs			sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
136168709Srrs			    chk->whoTo->flight_size,
137168709Srrs			    chk->book_size,
138168709Srrs			    (uintptr_t) chk->whoTo,
139168709Srrs			    chk->rec.data.TSN_seq);
140168709Srrs#endif
141163953Srrs			/* Clear any time so NO RTT is being done */
142163953Srrs			chk->do_rtt = 0;
143168709Srrs			sctp_flight_size_decrease(chk);
144168709Srrs			sctp_total_flight_decrease(stcb, chk);
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) {
207169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
208169352Srrs		printf("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
209169352Srrs		    nxtsz);
210169352Srrs#endif
211167695Srrs		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
212163953Srrs	}
213163953Srrs	if (tmr_stopped)
214163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
215163953Srrs
216163953Srrs	SCTP_TCB_UNLOCK(stcb);
217163953Srrs}
218163953Srrs
219163953Srrs
220163953Srrsvoid
221163953Srrssctp_notify(struct sctp_inpcb *inp,
222167695Srrs    int error,
223163953Srrs    struct sctphdr *sh,
224163953Srrs    struct sockaddr *to,
225163953Srrs    struct sctp_tcb *stcb,
226163953Srrs    struct sctp_nets *net)
227163953Srrs{
228163953Srrs	/* protection */
229163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
230163953Srrs	    (sh == NULL) || (to == NULL)) {
231163953Srrs		return;
232163953Srrs	}
233163953Srrs	/* First job is to verify the vtag matches what I would send */
234163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
235163953Srrs		return;
236163953Srrs	}
237163953Srrs	/* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
238163953Srrs
239167695Srrs	if ((error == EHOSTUNREACH) ||	/* Host is not reachable */
240167695Srrs	    (error == EHOSTDOWN) ||	/* Host is down */
241167695Srrs	    (error == ECONNREFUSED) ||	/* Host refused the connection, (not
242163953Srrs					 * an abort?) */
243167695Srrs	    (error == ENOPROTOOPT)	/* SCTP is not present on host */
244163953Srrs	    ) {
245163953Srrs		/*
246163953Srrs		 * Hmm reachablity problems we must examine closely. If its
247163953Srrs		 * not reachable, we may have lost a network. Or if there is
248163953Srrs		 * NO protocol at the other end named SCTP. well we consider
249163953Srrs		 * it a OOTB abort.
250163953Srrs		 */
251167695Srrs		if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
252163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
253163953Srrs				/* Ok that destination is NOT reachable */
254167598Srrs				printf("ICMP (thresh %d/%d) takes interface %p down\n",
255167598Srrs				    net->error_count,
256167598Srrs				    net->failure_threshold,
257167598Srrs				    net);
258167598Srrs
259163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
260163953Srrs				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
261163953Srrs				net->error_count = net->failure_threshold + 1;
262163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
263163953Srrs				    stcb, SCTP_FAILED_THRESHOLD,
264163953Srrs				    (void *)net);
265163953Srrs			}
266163953Srrs			if (stcb)
267163953Srrs				SCTP_TCB_UNLOCK(stcb);
268163953Srrs		} else {
269163953Srrs			/*
270163953Srrs			 * Here the peer is either playing tricks on us,
271163953Srrs			 * including an address that belongs to someone who
272163953Srrs			 * does not support SCTP OR was a userland
273163953Srrs			 * implementation that shutdown and now is dead. In
274163953Srrs			 * either case treat it like a OOTB abort with no
275163953Srrs			 * TCB
276163953Srrs			 */
277163953Srrs			sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
278165220Srrs			sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
279163953Srrs			/* no need to unlock here, since the TCB is gone */
280163953Srrs		}
281163953Srrs	} else {
282163953Srrs		/* Send all others to the app */
283163953Srrs		if (stcb)
284163953Srrs			SCTP_TCB_UNLOCK(stcb);
285163953Srrs
286163953Srrs
287163953Srrs		if (inp->sctp_socket) {
288163953Srrs#ifdef SCTP_LOCK_LOGGING
289163953Srrs			sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
290163953Srrs#endif
291163953Srrs			SOCK_LOCK(inp->sctp_socket);
292167695Srrs			inp->sctp_socket->so_error = error;
293163953Srrs			sctp_sowwakeup(inp, inp->sctp_socket);
294163953Srrs			SOCK_UNLOCK(inp->sctp_socket);
295163953Srrs		}
296163953Srrs	}
297163953Srrs}
298163953Srrs
299163953Srrsvoid
300163953Srrssctp_ctlinput(cmd, sa, vip)
301163953Srrs	int cmd;
302163953Srrs	struct sockaddr *sa;
303163953Srrs	void *vip;
304163953Srrs{
305163953Srrs	struct ip *ip = vip;
306163953Srrs	struct sctphdr *sh;
307167598Srrs	uint32_t vrf_id;
308163953Srrs
309168299Srrs	/* FIX, for non-bsd is this right? */
310167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
311163953Srrs	if (sa->sa_family != AF_INET ||
312163953Srrs	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
313163953Srrs		return;
314163953Srrs	}
315163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
316163953Srrs		ip = 0;
317163953Srrs	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
318163953Srrs		return;
319163953Srrs	}
320163953Srrs	if (ip) {
321163953Srrs		struct sctp_inpcb *inp = NULL;
322163953Srrs		struct sctp_tcb *stcb = NULL;
323163953Srrs		struct sctp_nets *net = NULL;
324163953Srrs		struct sockaddr_in to, from;
325163953Srrs
326163953Srrs		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
327163953Srrs		bzero(&to, sizeof(to));
328163953Srrs		bzero(&from, sizeof(from));
329163953Srrs		from.sin_family = to.sin_family = AF_INET;
330163953Srrs		from.sin_len = to.sin_len = sizeof(to);
331163953Srrs		from.sin_port = sh->src_port;
332163953Srrs		from.sin_addr = ip->ip_src;
333163953Srrs		to.sin_port = sh->dest_port;
334163953Srrs		to.sin_addr = ip->ip_dst;
335163953Srrs
336163953Srrs		/*
337163953Srrs		 * 'to' holds the dest of the packet that failed to be sent.
338163953Srrs		 * 'from' holds our local endpoint address. Thus we reverse
339163953Srrs		 * the to and the from in the lookup.
340163953Srrs		 */
341163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
342163953Srrs		    (struct sockaddr *)&to,
343167598Srrs		    &inp, &net, 1, vrf_id);
344163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
345163953Srrs			if (cmd != PRC_MSGSIZE) {
346163953Srrs				int cm;
347163953Srrs
348163953Srrs				if (cmd == PRC_HOSTDEAD) {
349163953Srrs					cm = EHOSTUNREACH;
350163953Srrs				} else {
351163953Srrs					cm = inetctlerrmap[cmd];
352163953Srrs				}
353163953Srrs				sctp_notify(inp, cm, sh,
354163953Srrs				    (struct sockaddr *)&to, stcb,
355163953Srrs				    net);
356163953Srrs			} else {
357163953Srrs				/* handle possible ICMP size messages */
358163953Srrs				sctp_notify_mbuf(inp, stcb, net, ip, sh);
359163953Srrs			}
360163953Srrs		} else {
361163953Srrs			if ((stcb == NULL) && (inp != NULL)) {
362163953Srrs				/* reduce ref-count */
363163953Srrs				SCTP_INP_WLOCK(inp);
364163953Srrs				SCTP_INP_DECR_REF(inp);
365163953Srrs				SCTP_INP_WUNLOCK(inp);
366163953Srrs			}
367163953Srrs		}
368163953Srrs	}
369163953Srrs	return;
370163953Srrs}
371163953Srrs
372163953Srrsstatic int
373163953Srrssctp_getcred(SYSCTL_HANDLER_ARGS)
374163953Srrs{
375164085Srrs	struct xucred xuc;
376163953Srrs	struct sockaddr_in addrs[2];
377163953Srrs	struct sctp_inpcb *inp;
378163953Srrs	struct sctp_nets *net;
379163953Srrs	struct sctp_tcb *stcb;
380164085Srrs	int error;
381167598Srrs	uint32_t vrf_id;
382163953Srrs
383168299Srrs
384168299Srrs	/* FIX, for non-bsd is this right? */
385167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
386168299Srrs
387164039Srwatson	/*
388164039Srwatson	 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
389164039Srwatson	 * visibility is scoped using cr_canseesocket(), which it is not
390164039Srwatson	 * here.
391164039Srwatson	 */
392167598Srrs	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
393167598Srrs	    SUSER_ALLOWJAIL);
394163953Srrs	if (error)
395163953Srrs		return (error);
396164039Srwatson
397163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
398163953Srrs	if (error)
399163953Srrs		return (error);
400163953Srrs
401163953Srrs	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
402163953Srrs	    sintosa(&addrs[1]),
403167598Srrs	    &inp, &net, 1, vrf_id);
404163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
405163953Srrs		if ((inp != NULL) && (stcb == NULL)) {
406163953Srrs			/* reduce ref-count */
407163953Srrs			SCTP_INP_WLOCK(inp);
408163953Srrs			SCTP_INP_DECR_REF(inp);
409164085Srrs			goto cred_can_cont;
410163953Srrs		}
411163953Srrs		error = ENOENT;
412163953Srrs		goto out;
413163953Srrs	}
414163953Srrs	SCTP_TCB_UNLOCK(stcb);
415164085Srrs	/*
416164085Srrs	 * We use the write lock here, only since in the error leg we need
417164085Srrs	 * it. If we used RLOCK, then we would have to
418164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
419164085Srrs	 * Better to use higher wlock.
420164085Srrs	 */
421164085Srrs	SCTP_INP_WLOCK(inp);
422164085Srrscred_can_cont:
423164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
424164085Srrs	if (error) {
425164085Srrs		SCTP_INP_WUNLOCK(inp);
426164085Srrs		goto out;
427164085Srrs	}
428164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
429164085Srrs	SCTP_INP_WUNLOCK(inp);
430164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
431163953Srrsout:
432163953Srrs	return (error);
433163953Srrs}
434163953Srrs
435163953SrrsSYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
436163953Srrs    0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
437163953Srrs
438163953Srrs
439163953Srrsstatic void
440163953Srrssctp_abort(struct socket *so)
441163953Srrs{
442163953Srrs	struct sctp_inpcb *inp;
443163953Srrs	uint32_t flags;
444163953Srrs
445163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
446163953Srrs	if (inp == 0)
447163953Srrs		return;
448163953Srrs
449163953Srrssctp_must_try_again:
450163953Srrs	flags = inp->sctp_flags;
451163953Srrs#ifdef SCTP_LOG_CLOSING
452163953Srrs	sctp_log_closing(inp, NULL, 17);
453163953Srrs#endif
454163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
455163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
456163953Srrs#ifdef SCTP_LOG_CLOSING
457163953Srrs		sctp_log_closing(inp, NULL, 16);
458163953Srrs#endif
459163953Srrs		sctp_inpcb_free(inp, 1, 0);
460163953Srrs		SOCK_LOCK(so);
461167695Srrs		SCTP_SB_CLEAR(so->so_snd);
462163953Srrs		/*
463163953Srrs		 * same for the rcv ones, they are only here for the
464163953Srrs		 * accounting/select.
465163953Srrs		 */
466167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
467167695Srrs
468167695Srrs		/* Now null out the reference, we are completely detached. */
469163953Srrs		so->so_pcb = NULL;
470163953Srrs		SOCK_UNLOCK(so);
471163953Srrs	} else {
472163953Srrs		flags = inp->sctp_flags;
473163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
474163953Srrs			goto sctp_must_try_again;
475163953Srrs		}
476163953Srrs	}
477163953Srrs	return;
478163953Srrs}
479163953Srrs
480163953Srrsstatic int
481163953Srrssctp_attach(struct socket *so, int proto, struct thread *p)
482163953Srrs{
483163953Srrs	struct sctp_inpcb *inp;
484163953Srrs	struct inpcb *ip_inp;
485166086Srrs	int error;
486163953Srrs
487163953Srrs#ifdef IPSEC
488163953Srrs	uint32_t flags;
489163953Srrs
490163953Srrs#endif
491163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
492163953Srrs	if (inp != 0) {
493163953Srrs		return EINVAL;
494163953Srrs	}
495167695Srrs	error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
496163953Srrs	if (error) {
497163953Srrs		return error;
498163953Srrs	}
499163953Srrs	error = sctp_inpcb_alloc(so);
500163953Srrs	if (error) {
501163953Srrs		return error;
502163953Srrs	}
503163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
504163953Srrs	SCTP_INP_WLOCK(inp);
505163953Srrs
506163953Srrs	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
507163953Srrs	ip_inp = &inp->ip_inp.inp;
508163953Srrs	ip_inp->inp_vflag |= INP_IPV4;
509163953Srrs	ip_inp->inp_ip_ttl = ip_defttl;
510163953Srrs
511163953Srrs#ifdef IPSEC
512163953Srrs	error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
513163953Srrs#ifdef SCTP_LOG_CLOSING
514163953Srrs	sctp_log_closing(inp, NULL, 17);
515163953Srrs#endif
516163953Srrs	if (error != 0) {
517163953Srrs		flags = inp->sctp_flags;
518163953Srrs		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
519163953Srrs		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
520163953Srrs#ifdef SCTP_LOG_CLOSING
521163953Srrs			sctp_log_closing(inp, NULL, 15);
522163953Srrs#endif
523169352Srrs			SCTP_INP_WUNLOCK(inp);
524163953Srrs			sctp_inpcb_free(inp, 1, 0);
525169254Srrs		} else {
526169352Srrs			SCTP_INP_WUNLOCK(inp);
527163953Srrs		}
528163953Srrs		return error;
529163953Srrs	}
530163953Srrs#endif				/* IPSEC */
531163953Srrs	SCTP_INP_WUNLOCK(inp);
532163953Srrs	return 0;
533163953Srrs}
534163953Srrs
535163953Srrsstatic int
536163953Srrssctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
537163953Srrs{
538163953Srrs	struct sctp_inpcb *inp;
539166086Srrs	int error;
540163953Srrs
541163953Srrs#ifdef INET6
542163953Srrs	if (addr && addr->sa_family != AF_INET)
543163953Srrs		/* must be a v4 address! */
544163953Srrs		return EINVAL;
545163953Srrs#endif				/* INET6 */
546163953Srrs
547163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
548163953Srrs	if (inp == 0)
549163953Srrs		return EINVAL;
550163953Srrs
551163953Srrs	error = sctp_inpcb_bind(so, addr, p);
552163953Srrs	return error;
553163953Srrs}
554163953Srrs
555163953Srrsstatic void
556163953Srrssctp_close(struct socket *so)
557163953Srrs{
558163953Srrs	struct sctp_inpcb *inp;
559163953Srrs	uint32_t flags;
560163953Srrs
561163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
562163953Srrs	if (inp == 0)
563163953Srrs		return;
564163953Srrs
565163953Srrs	/*
566163953Srrs	 * Inform all the lower layer assoc that we are done.
567163953Srrs	 */
568163953Srrssctp_must_try_again:
569163953Srrs	flags = inp->sctp_flags;
570163953Srrs#ifdef SCTP_LOG_CLOSING
571163953Srrs	sctp_log_closing(inp, NULL, 17);
572163953Srrs#endif
573163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
574163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
575163953Srrs		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
576163953Srrs		    (so->so_rcv.sb_cc > 0)) {
577163953Srrs#ifdef SCTP_LOG_CLOSING
578163953Srrs			sctp_log_closing(inp, NULL, 13);
579163953Srrs#endif
580163953Srrs			sctp_inpcb_free(inp, 1, 1);
581163953Srrs		} else {
582163953Srrs#ifdef SCTP_LOG_CLOSING
583163953Srrs			sctp_log_closing(inp, NULL, 14);
584163953Srrs#endif
585163953Srrs			sctp_inpcb_free(inp, 0, 1);
586163953Srrs		}
587163953Srrs		/*
588163953Srrs		 * The socket is now detached, no matter what the state of
589163953Srrs		 * the SCTP association.
590163953Srrs		 */
591163953Srrs		SOCK_LOCK(so);
592167695Srrs		SCTP_SB_CLEAR(so->so_snd);
593163953Srrs		/*
594163953Srrs		 * same for the rcv ones, they are only here for the
595163953Srrs		 * accounting/select.
596163953Srrs		 */
597167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
598167695Srrs
599167695Srrs		/* Now null out the reference, we are completely detached. */
600163953Srrs		so->so_pcb = NULL;
601163953Srrs		SOCK_UNLOCK(so);
602163953Srrs	} else {
603163953Srrs		flags = inp->sctp_flags;
604163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
605163953Srrs			goto sctp_must_try_again;
606163953Srrs		}
607163953Srrs	}
608163953Srrs	return;
609163953Srrs}
610163953Srrs
611163953Srrs
612163953Srrsint
613163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
614163953Srrs    struct mbuf *control, struct thread *p);
615163953Srrs
616163953Srrs
617163953Srrsint
618163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
619163953Srrs    struct mbuf *control, struct thread *p)
620163953Srrs{
621163953Srrs	struct sctp_inpcb *inp;
622163953Srrs	int error;
623163953Srrs
624163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
625163953Srrs	if (inp == 0) {
626163953Srrs		if (control) {
627163953Srrs			sctp_m_freem(control);
628163953Srrs			control = NULL;
629163953Srrs		}
630163953Srrs		sctp_m_freem(m);
631163953Srrs		return EINVAL;
632163953Srrs	}
633163953Srrs	/* Got to have an to address if we are NOT a connected socket */
634163953Srrs	if ((addr == NULL) &&
635163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
636163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
637163953Srrs	    ) {
638163953Srrs		goto connected_type;
639163953Srrs	} else if (addr == NULL) {
640163953Srrs		error = EDESTADDRREQ;
641163953Srrs		sctp_m_freem(m);
642163953Srrs		if (control) {
643163953Srrs			sctp_m_freem(control);
644163953Srrs			control = NULL;
645163953Srrs		}
646163953Srrs		return (error);
647163953Srrs	}
648163953Srrs#ifdef INET6
649163953Srrs	if (addr->sa_family != AF_INET) {
650163953Srrs		/* must be a v4 address! */
651163953Srrs		sctp_m_freem(m);
652163953Srrs		if (control) {
653163953Srrs			sctp_m_freem(control);
654163953Srrs			control = NULL;
655163953Srrs		}
656163953Srrs		error = EDESTADDRREQ;
657163953Srrs		return EINVAL;
658163953Srrs	}
659163953Srrs#endif				/* INET6 */
660163953Srrsconnected_type:
661163953Srrs	/* now what about control */
662163953Srrs	if (control) {
663163953Srrs		if (inp->control) {
664163953Srrs			printf("huh? control set?\n");
665163953Srrs			sctp_m_freem(inp->control);
666163953Srrs			inp->control = NULL;
667163953Srrs		}
668163953Srrs		inp->control = control;
669163953Srrs	}
670163953Srrs	/* Place the data */
671163953Srrs	if (inp->pkt) {
672165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
673163953Srrs		inp->pkt_last = m;
674163953Srrs	} else {
675163953Srrs		inp->pkt_last = inp->pkt = m;
676163953Srrs	}
677163953Srrs	if (
678163953Srrs	/* FreeBSD uses a flag passed */
679163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
680163953Srrs	    ) {
681163953Srrs		/*
682163953Srrs		 * note with the current version this code will only be used
683163953Srrs		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
684163953Srrs		 * re-defining sosend to use the sctp_sosend. One can
685163953Srrs		 * optionally switch back to this code (by changing back the
686163953Srrs		 * definitions) but this is not advisable. This code is used
687163953Srrs		 * by FreeBSD when sending a file with sendfile() though.
688163953Srrs		 */
689163953Srrs		int ret;
690163953Srrs
691163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
692163953Srrs		inp->pkt = NULL;
693163953Srrs		inp->control = NULL;
694163953Srrs		return (ret);
695163953Srrs	} else {
696163953Srrs		return (0);
697163953Srrs	}
698163953Srrs}
699163953Srrs
700163953Srrsstatic int
701163953Srrssctp_disconnect(struct socket *so)
702163953Srrs{
703163953Srrs	struct sctp_inpcb *inp;
704163953Srrs
705163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
706163953Srrs	if (inp == NULL) {
707163953Srrs		return (ENOTCONN);
708163953Srrs	}
709163953Srrs	SCTP_INP_RLOCK(inp);
710163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
711166675Srrs		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
712163953Srrs			/* No connection */
713163953Srrs			SCTP_INP_RUNLOCK(inp);
714163953Srrs			return (0);
715163953Srrs		} else {
716163953Srrs			struct sctp_association *asoc;
717163953Srrs			struct sctp_tcb *stcb;
718163953Srrs
719163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
720163953Srrs			if (stcb == NULL) {
721163953Srrs				SCTP_INP_RUNLOCK(inp);
722163953Srrs				return (EINVAL);
723163953Srrs			}
724163953Srrs			SCTP_TCB_LOCK(stcb);
725163953Srrs			asoc = &stcb->asoc;
726163953Srrs			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
727163953Srrs				/* We are about to be freed, out of here */
728163953Srrs				SCTP_TCB_UNLOCK(stcb);
729163953Srrs				SCTP_INP_RUNLOCK(inp);
730163953Srrs				return (0);
731163953Srrs			}
732163953Srrs			if (((so->so_options & SO_LINGER) &&
733163953Srrs			    (so->so_linger == 0)) ||
734163953Srrs			    (so->so_rcv.sb_cc > 0)) {
735163953Srrs				if (SCTP_GET_STATE(asoc) !=
736163953Srrs				    SCTP_STATE_COOKIE_WAIT) {
737163953Srrs					/* Left with Data unread */
738163953Srrs					struct mbuf *err;
739163953Srrs
740163953Srrs					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
741163953Srrs					if (err) {
742163953Srrs						/*
743163953Srrs						 * Fill in the user
744163953Srrs						 * initiated abort
745163953Srrs						 */
746163953Srrs						struct sctp_paramhdr *ph;
747163953Srrs
748163953Srrs						ph = mtod(err, struct sctp_paramhdr *);
749165647Srrs						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
750163953Srrs						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
751165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(err));
752163953Srrs					}
753163953Srrs					sctp_send_abort_tcb(stcb, err);
754163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
755163953Srrs				}
756163953Srrs				SCTP_INP_RUNLOCK(inp);
757163953Srrs				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
758163953Srrs				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
759163953Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
760163953Srrs				}
761165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
762163953Srrs				/* No unlock tcb assoc is gone */
763163953Srrs				return (0);
764163953Srrs			}
765163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
766163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
767163953Srrs			    (asoc->stream_queue_cnt == 0)) {
768163953Srrs				/* there is nothing queued to send, so done */
769163953Srrs				if (asoc->locked_on_sending) {
770163953Srrs					goto abort_anyway;
771163953Srrs				}
772166675Srrs				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
773166675Srrs				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
774163953Srrs					/* only send SHUTDOWN 1st time thru */
775163953Srrs					sctp_stop_timers_for_shutdown(stcb);
776163953Srrs					sctp_send_shutdown(stcb,
777163953Srrs					    stcb->asoc.primary_destination);
778163953Srrs					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
779166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
780166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
781166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
782166675Srrs					}
783163953Srrs					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
784163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
785163953Srrs					    stcb->sctp_ep, stcb,
786163953Srrs					    asoc->primary_destination);
787163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
788163953Srrs					    stcb->sctp_ep, stcb,
789163953Srrs					    asoc->primary_destination);
790163953Srrs				}
791163953Srrs			} else {
792163953Srrs				/*
793163953Srrs				 * we still got (or just got) data to send,
794163953Srrs				 * so set SHUTDOWN_PENDING
795163953Srrs				 */
796163953Srrs				/*
797163953Srrs				 * XXX sockets draft says that SCTP_EOF
798163953Srrs				 * should be sent with no data. currently,
799163953Srrs				 * we will allow user data to be sent first
800163953Srrs				 * and move to SHUTDOWN-PENDING
801163953Srrs				 */
802163953Srrs				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
803163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
804163953Srrs				    asoc->primary_destination);
805163953Srrs				if (asoc->locked_on_sending) {
806163953Srrs					/* Locked to send out the data */
807163953Srrs					struct sctp_stream_queue_pending *sp;
808163953Srrs
809163953Srrs					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
810163953Srrs					if (sp == NULL) {
811163959Srrs						printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
812163953Srrs						    asoc->locked_on_sending->stream_no);
813163953Srrs					} else {
814163953Srrs						if ((sp->length == 0) && (sp->msg_is_complete == 0))
815163953Srrs							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
816163953Srrs					}
817163953Srrs				}
818163953Srrs				if (TAILQ_EMPTY(&asoc->send_queue) &&
819163953Srrs				    TAILQ_EMPTY(&asoc->sent_queue) &&
820163953Srrs				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
821163953Srrs					struct mbuf *op_err;
822163953Srrs
823163953Srrs			abort_anyway:
824163953Srrs					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
825163953Srrs					    0, M_DONTWAIT, 1, MT_DATA);
826163953Srrs					if (op_err) {
827163953Srrs						/*
828163953Srrs						 * Fill in the user
829163953Srrs						 * initiated abort
830163953Srrs						 */
831163953Srrs						struct sctp_paramhdr *ph;
832163953Srrs						uint32_t *ippp;
833163953Srrs
834165647Srrs						SCTP_BUF_LEN(op_err) =
835163953Srrs						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
836163953Srrs						ph = mtod(op_err,
837163953Srrs						    struct sctp_paramhdr *);
838163953Srrs						ph->param_type = htons(
839163953Srrs						    SCTP_CAUSE_USER_INITIATED_ABT);
840165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(op_err));
841163953Srrs						ippp = (uint32_t *) (ph + 1);
842165220Srrs						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
843163953Srrs					}
844165220Srrs					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
845163953Srrs					sctp_send_abort_tcb(stcb, op_err);
846163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
847163953Srrs					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
848163953Srrs					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
849163953Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
850163953Srrs					}
851163953Srrs					SCTP_INP_RUNLOCK(inp);
852165220Srrs					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
853163953Srrs					return (0);
854163953Srrs				}
855163953Srrs			}
856163953Srrs			SCTP_TCB_UNLOCK(stcb);
857163953Srrs			SCTP_INP_RUNLOCK(inp);
858163953Srrs			return (0);
859163953Srrs		}
860163953Srrs		/* not reached */
861168299Srrs		printf("Not reached reached?\n");
862163953Srrs	} else {
863163953Srrs		/* UDP model does not support this */
864163953Srrs		SCTP_INP_RUNLOCK(inp);
865163953Srrs		return EOPNOTSUPP;
866163953Srrs	}
867163953Srrs}
868163953Srrs
869163953Srrsint
870163953Srrssctp_shutdown(struct socket *so)
871163953Srrs{
872163953Srrs	struct sctp_inpcb *inp;
873163953Srrs
874163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
875163953Srrs	if (inp == 0) {
876163953Srrs		return EINVAL;
877163953Srrs	}
878163953Srrs	SCTP_INP_RLOCK(inp);
879163953Srrs	/* For UDP model this is a invalid call */
880163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
881163953Srrs		/* Restore the flags that the soshutdown took away. */
882163953Srrs		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
883163953Srrs		/* This proc will wakeup for read and do nothing (I hope) */
884163953Srrs		SCTP_INP_RUNLOCK(inp);
885163953Srrs		return (EOPNOTSUPP);
886163953Srrs	}
887163953Srrs	/*
888163953Srrs	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
889163953Srrs	 * or SHUT_RDWR. This means we put the shutdown flag against it.
890163953Srrs	 */
891163953Srrs	{
892163953Srrs		struct sctp_tcb *stcb;
893163953Srrs		struct sctp_association *asoc;
894163953Srrs
895163953Srrs		socantsendmore(so);
896163953Srrs
897163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
898163953Srrs		if (stcb == NULL) {
899163953Srrs			/*
900163953Srrs			 * Ok we hit the case that the shutdown call was
901163953Srrs			 * made after an abort or something. Nothing to do
902163953Srrs			 * now.
903163953Srrs			 */
904168299Srrs			SCTP_INP_RUNLOCK(inp);
905163953Srrs			return (0);
906163953Srrs		}
907163953Srrs		SCTP_TCB_LOCK(stcb);
908163953Srrs		asoc = &stcb->asoc;
909163953Srrs		if (TAILQ_EMPTY(&asoc->send_queue) &&
910163953Srrs		    TAILQ_EMPTY(&asoc->sent_queue) &&
911163953Srrs		    (asoc->stream_queue_cnt == 0)) {
912163953Srrs			if (asoc->locked_on_sending) {
913163953Srrs				goto abort_anyway;
914163953Srrs			}
915163953Srrs			/* there is nothing queued to send, so I'm done... */
916163953Srrs			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
917163953Srrs				/* only send SHUTDOWN the first time through */
918163953Srrs				sctp_stop_timers_for_shutdown(stcb);
919163953Srrs				sctp_send_shutdown(stcb,
920163953Srrs				    stcb->asoc.primary_destination);
921163953Srrs				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
922166675Srrs				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
923166675Srrs				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
924166675Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
925166675Srrs				}
926163953Srrs				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
927163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
928163953Srrs				    stcb->sctp_ep, stcb,
929163953Srrs				    asoc->primary_destination);
930163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
931163953Srrs				    stcb->sctp_ep, stcb,
932163953Srrs				    asoc->primary_destination);
933163953Srrs			}
934163953Srrs		} else {
935163953Srrs			/*
936163953Srrs			 * we still got (or just got) data to send, so set
937163953Srrs			 * SHUTDOWN_PENDING
938163953Srrs			 */
939163953Srrs			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
940163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
941163953Srrs			    asoc->primary_destination);
942163953Srrs
943163953Srrs			if (asoc->locked_on_sending) {
944163953Srrs				/* Locked to send out the data */
945163953Srrs				struct sctp_stream_queue_pending *sp;
946163953Srrs
947163953Srrs				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
948163953Srrs				if (sp == NULL) {
949163959Srrs					printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
950163953Srrs					    asoc->locked_on_sending->stream_no);
951163953Srrs				} else {
952163953Srrs					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
953163953Srrs						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
954163953Srrs					}
955163953Srrs				}
956163953Srrs			}
957163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
958163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
959163953Srrs			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
960163953Srrs				struct mbuf *op_err;
961163953Srrs
962163953Srrs		abort_anyway:
963163953Srrs				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
964163953Srrs				    0, M_DONTWAIT, 1, MT_DATA);
965163953Srrs				if (op_err) {
966163953Srrs					/* Fill in the user initiated abort */
967163953Srrs					struct sctp_paramhdr *ph;
968163953Srrs					uint32_t *ippp;
969163953Srrs
970165647Srrs					SCTP_BUF_LEN(op_err) =
971163953Srrs					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
972163953Srrs					ph = mtod(op_err,
973163953Srrs					    struct sctp_paramhdr *);
974163953Srrs					ph->param_type = htons(
975163953Srrs					    SCTP_CAUSE_USER_INITIATED_ABT);
976165647Srrs					ph->param_length = htons(SCTP_BUF_LEN(op_err));
977163953Srrs					ippp = (uint32_t *) (ph + 1);
978165220Srrs					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
979163953Srrs				}
980165220Srrs				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
981163953Srrs				sctp_abort_an_association(stcb->sctp_ep, stcb,
982163953Srrs				    SCTP_RESPONSE_TO_USER_REQ,
983163953Srrs				    op_err);
984163953Srrs				goto skip_unlock;
985163953Srrs			}
986163953Srrs		}
987163953Srrs		SCTP_TCB_UNLOCK(stcb);
988163953Srrs	}
989163953Srrsskip_unlock:
990163953Srrs	SCTP_INP_RUNLOCK(inp);
991163953Srrs	return 0;
992163953Srrs}
993163953Srrs
994163953Srrs/*
995163953Srrs * copies a "user" presentable address and removes embedded scope, etc.
996163953Srrs * returns 0 on success, 1 on error
997163953Srrs */
998163953Srrsstatic uint32_t
999163953Srrssctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1000163953Srrs{
1001163953Srrs	struct sockaddr_in6 lsa6;
1002163953Srrs
1003163953Srrs	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1004163953Srrs	    &lsa6);
1005163953Srrs	memcpy(ss, sa, sa->sa_len);
1006163953Srrs	return (0);
1007163953Srrs}
1008163953Srrs
1009163953Srrs
1010163953Srrs
1011166675Srrsstatic size_t
1012168124Srrssctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1013163953Srrs    struct sctp_tcb *stcb,
1014166675Srrs    size_t limit,
1015167598Srrs    struct sockaddr_storage *sas,
1016167598Srrs    uint32_t vrf_id)
1017163953Srrs{
1018167598Srrs	struct sctp_ifn *sctp_ifn;
1019167598Srrs	struct sctp_ifa *sctp_ifa;
1020166675Srrs	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1021166675Srrs	size_t actual;
1022163953Srrs	int ipv4_addr_legal, ipv6_addr_legal;
1023167598Srrs	struct sctp_vrf *vrf;
1024163953Srrs
1025163953Srrs	actual = 0;
1026163953Srrs	if (limit <= 0)
1027163953Srrs		return (actual);
1028163953Srrs
1029163953Srrs	if (stcb) {
1030163953Srrs		/* Turn on all the appropriate scope */
1031163953Srrs		loopback_scope = stcb->asoc.loopback_scope;
1032163953Srrs		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1033163953Srrs		local_scope = stcb->asoc.local_scope;
1034163953Srrs		site_scope = stcb->asoc.site_scope;
1035163953Srrs	} else {
1036163953Srrs		/* Turn on ALL scope, since we look at the EP */
1037163953Srrs		loopback_scope = ipv4_local_scope = local_scope =
1038163953Srrs		    site_scope = 1;
1039163953Srrs	}
1040163953Srrs	ipv4_addr_legal = ipv6_addr_legal = 0;
1041163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1042163953Srrs		ipv6_addr_legal = 1;
1043166023Srrs		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1044163953Srrs			ipv4_addr_legal = 1;
1045163953Srrs		}
1046163953Srrs	} else {
1047163953Srrs		ipv4_addr_legal = 1;
1048163953Srrs	}
1049167598Srrs	vrf = sctp_find_vrf(vrf_id);
1050167598Srrs	if (vrf == NULL) {
1051167598Srrs		return (0);
1052167598Srrs	}
1053163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1054167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1055163953Srrs			if ((loopback_scope == 0) &&
1056167598Srrs			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1057163953Srrs				/* Skip loopback if loopback_scope not set */
1058163953Srrs				continue;
1059163953Srrs			}
1060167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1061163953Srrs				if (stcb) {
1062163953Srrs					/*
1063163953Srrs					 * For the BOUND-ALL case, the list
1064163953Srrs					 * associated with a TCB is Always
1065163953Srrs					 * considered a reverse list.. i.e.
1066163953Srrs					 * it lists addresses that are NOT
1067163953Srrs					 * part of the association. If this
1068163953Srrs					 * is one of those we must skip it.
1069163953Srrs					 */
1070163953Srrs					if (sctp_is_addr_restricted(stcb,
1071167598Srrs					    sctp_ifa)) {
1072163953Srrs						continue;
1073163953Srrs					}
1074163953Srrs				}
1075167598Srrs				if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1076163953Srrs				    (ipv4_addr_legal)) {
1077163953Srrs					struct sockaddr_in *sin;
1078163953Srrs
1079167598Srrs					sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1080163953Srrs					if (sin->sin_addr.s_addr == 0) {
1081163953Srrs						/*
1082163953Srrs						 * we skip unspecifed
1083163953Srrs						 * addresses
1084163953Srrs						 */
1085163953Srrs						continue;
1086163953Srrs					}
1087163953Srrs					if ((ipv4_local_scope == 0) &&
1088163953Srrs					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1089163953Srrs						continue;
1090163953Srrs					}
1091163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1092163953Srrs						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1093163953Srrs						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1094163953Srrs						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1095163953Srrs						actual += sizeof(sizeof(struct sockaddr_in6));
1096163953Srrs					} else {
1097163953Srrs						memcpy(sas, sin, sizeof(*sin));
1098163953Srrs						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1099163953Srrs						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1100163953Srrs						actual += sizeof(*sin);
1101163953Srrs					}
1102163953Srrs					if (actual >= limit) {
1103163953Srrs						return (actual);
1104163953Srrs					}
1105167598Srrs				} else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1106163953Srrs				    (ipv6_addr_legal)) {
1107163953Srrs					struct sockaddr_in6 *sin6;
1108163953Srrs
1109167598Srrs					sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1110163953Srrs					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1111163953Srrs						/*
1112163953Srrs						 * we skip unspecifed
1113163953Srrs						 * addresses
1114163953Srrs						 */
1115163953Srrs						continue;
1116163953Srrs					}
1117163953Srrs					if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1118163953Srrs						if (local_scope == 0)
1119163953Srrs							continue;
1120163953Srrs						if (sin6->sin6_scope_id == 0) {
1121163953Srrs							if (sa6_recoverscope(sin6) != 0)
1122163953Srrs								/*
1123163953Srrs								 * bad link
1124163953Srrs								 * local
1125163953Srrs								 * address
1126163953Srrs								 */
1127163953Srrs								continue;
1128163953Srrs						}
1129163953Srrs					}
1130163953Srrs					if ((site_scope == 0) &&
1131163953Srrs					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1132163953Srrs						continue;
1133163953Srrs					}
1134163953Srrs					memcpy(sas, sin6, sizeof(*sin6));
1135163953Srrs					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1136163953Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1137163953Srrs					actual += sizeof(*sin6);
1138163953Srrs					if (actual >= limit) {
1139163953Srrs						return (actual);
1140163953Srrs					}
1141163953Srrs				}
1142163953Srrs			}
1143163953Srrs		}
1144163953Srrs	} else {
1145163953Srrs		struct sctp_laddr *laddr;
1146163953Srrs
1147167598Srrs		/* The list is a NEGATIVE list */
1148167598Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1149167598Srrs			if (stcb) {
1150167598Srrs				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1151163953Srrs					continue;
1152163953Srrs				}
1153163953Srrs			}
1154167598Srrs			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1155167598Srrs				continue;
1156167598Srrs
1157167598Srrs			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1158167598Srrs			sas = (struct sockaddr_storage *)((caddr_t)sas +
1159167598Srrs			    laddr->ifa->address.sa.sa_len);
1160167598Srrs			actual += laddr->ifa->address.sa.sa_len;
1161167598Srrs			if (actual >= limit) {
1162167598Srrs				return (actual);
1163163953Srrs			}
1164163953Srrs		}
1165163953Srrs	}
1166163953Srrs	return (actual);
1167163953Srrs}
1168163953Srrs
1169168124Srrsstatic size_t
1170168124Srrssctp_fill_up_addresses(struct sctp_inpcb *inp,
1171168124Srrs    struct sctp_tcb *stcb,
1172168124Srrs    size_t limit,
1173168124Srrs    struct sockaddr_storage *sas)
1174168124Srrs{
1175168124Srrs	size_t size = 0;
1176168124Srrs
1177168124Srrs	/* fill up addresses for the endpoint's default vrf */
1178168124Srrs	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1179168124Srrs	    inp->def_vrf_id);
1180168124Srrs	return (size);
1181168124Srrs}
1182168124Srrs
1183163953Srrsstatic int
1184168124Srrssctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1185163953Srrs{
1186163953Srrs	int cnt = 0;
1187167598Srrs	struct sctp_vrf *vrf = NULL;
1188163953Srrs
1189163953Srrs	/*
1190163953Srrs	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1191163953Srrs	 * number of addresses that you COULD get. In reality the sub-set
1192163953Srrs	 * bound may have an exclusion list for a given TCB OR in the
1193163953Srrs	 * bound-all case a TCB may NOT include the loopback or other
1194163953Srrs	 * addresses as well.
1195163953Srrs	 */
1196167598Srrs	vrf = sctp_find_vrf(vrf_id);
1197167598Srrs	if (vrf == NULL) {
1198167598Srrs		return (0);
1199167598Srrs	}
1200163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1201167598Srrs		struct sctp_ifn *sctp_ifn;
1202167598Srrs		struct sctp_ifa *sctp_ifa;
1203163953Srrs
1204167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1205167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1206163953Srrs				/* Count them if they are the right type */
1207167598Srrs				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1208163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1209163953Srrs						cnt += sizeof(struct sockaddr_in6);
1210163953Srrs					else
1211163953Srrs						cnt += sizeof(struct sockaddr_in);
1212163953Srrs
1213167598Srrs				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1214163953Srrs					cnt += sizeof(struct sockaddr_in6);
1215163953Srrs			}
1216163953Srrs		}
1217163953Srrs	} else {
1218163953Srrs		struct sctp_laddr *laddr;
1219163953Srrs
1220163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1221167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1222163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1223163953Srrs					cnt += sizeof(struct sockaddr_in6);
1224163953Srrs				else
1225163953Srrs					cnt += sizeof(struct sockaddr_in);
1226163953Srrs
1227167598Srrs			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1228163953Srrs				cnt += sizeof(struct sockaddr_in6);
1229163953Srrs		}
1230163953Srrs	}
1231163953Srrs	return (cnt);
1232163953Srrs}
1233163953Srrs
1234168124Srrsstatic int
1235168124Srrssctp_count_max_addresses(struct sctp_inpcb *inp)
1236168124Srrs{
1237168124Srrs	int cnt = 0;
1238166675Srrs
1239168124Srrs	/* count addresses for the endpoint's default VRF */
1240168124Srrs	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1241168124Srrs	return (cnt);
1242168124Srrs}
1243168124Srrs
1244163953Srrsstatic int
1245166675Srrssctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1246166675Srrs    size_t optsize, void *p, int delay)
1247163953Srrs{
1248163953Srrs	int error = 0;
1249163953Srrs	int creat_lock_on = 0;
1250163953Srrs	struct sctp_tcb *stcb = NULL;
1251163953Srrs	struct sockaddr *sa;
1252169352Srrs	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1253169352Srrs	int added = 0;
1254167598Srrs	uint32_t vrf_id;
1255167598Srrs	sctp_assoc_t *a_id;
1256163953Srrs
1257163953Srrs#ifdef SCTP_DEBUG
1258163953Srrs	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1259163953Srrs		printf("Connectx called\n");
1260163953Srrs	}
1261163953Srrs#endif				/* SCTP_DEBUG */
1262163953Srrs
1263163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1264163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1265163953Srrs		/* We are already connected AND the TCP model */
1266163953Srrs		return (EADDRINUSE);
1267163953Srrs	}
1268163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1269163953Srrs		return (EINVAL);
1270163953Srrs	}
1271163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1272163953Srrs		SCTP_INP_RLOCK(inp);
1273163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1274163953Srrs		SCTP_INP_RUNLOCK(inp);
1275163953Srrs	}
1276163953Srrs	if (stcb) {
1277163953Srrs		return (EALREADY);
1278163953Srrs	}
1279163953Srrs	SCTP_INP_INCR_REF(inp);
1280163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
1281163953Srrs	creat_lock_on = 1;
1282163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1283163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1284163953Srrs		error = EFAULT;
1285163953Srrs		goto out_now;
1286163953Srrs	}
1287166675Srrs	totaddrp = (int *)optval;
1288163953Srrs	totaddr = *totaddrp;
1289163953Srrs	sa = (struct sockaddr *)(totaddrp + 1);
1290169352Srrs	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)));
1291169352Srrs	if (stcb != NULL) {
1292169352Srrs		/* Already have or am bring up an association */
1293169352Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1294169352Srrs		creat_lock_on = 0;
1295169352Srrs		SCTP_TCB_UNLOCK(stcb);
1296169352Srrs		error = EALREADY;
1297169352Srrs		goto out_now;
1298163953Srrs	}
1299163953Srrs#ifdef INET6
1300163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1301163953Srrs	    (num_v6 > 0)) {
1302163953Srrs		error = EINVAL;
1303163953Srrs		goto out_now;
1304163953Srrs	}
1305163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1306163953Srrs	    (num_v4 > 0)) {
1307163953Srrs		struct in6pcb *inp6;
1308163953Srrs
1309163953Srrs		inp6 = (struct in6pcb *)inp;
1310166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1311163953Srrs			/*
1312163953Srrs			 * if IPV6_V6ONLY flag, ignore connections destined
1313163953Srrs			 * to a v4 addr or v4-mapped addr
1314163953Srrs			 */
1315163953Srrs			error = EINVAL;
1316163953Srrs			goto out_now;
1317163953Srrs		}
1318163953Srrs	}
1319163953Srrs#endif				/* INET6 */
1320163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1321163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
1322163953Srrs		/* Bind a ephemeral port */
1323163953Srrs		error = sctp_inpcb_bind(so, NULL, p);
1324163953Srrs		if (error) {
1325163953Srrs			goto out_now;
1326163953Srrs		}
1327163953Srrs	}
1328167695Srrs	/* FIX ME: do we want to pass in a vrf on the connect call? */
1329167695Srrs	vrf_id = inp->def_vrf_id;
1330167695Srrs
1331163953Srrs	/* We are GOOD to go */
1332167598Srrs	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1333163953Srrs	if (stcb == NULL) {
1334163953Srrs		/* Gak! no memory */
1335163953Srrs		goto out_now;
1336163953Srrs	}
1337169352Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1338163953Srrs	/* move to second address */
1339163953Srrs	if (sa->sa_family == AF_INET)
1340163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1341163953Srrs	else
1342163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1343163953Srrs
1344169352Srrs	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1345167598Srrs	/* Fill in the return id */
1346167598Srrs	a_id = (sctp_assoc_t *) optval;
1347167598Srrs	*a_id = sctp_get_associd(stcb);
1348163953Srrs
1349163953Srrs	/* initialize authentication parameters for the assoc */
1350163953Srrs	sctp_initialize_auth_params(inp, stcb);
1351163953Srrs
1352163953Srrs	if (delay) {
1353163953Srrs		/* doing delayed connection */
1354163953Srrs		stcb->asoc.delayed_connection = 1;
1355163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1356163953Srrs	} else {
1357163953Srrs		SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1358163953Srrs		sctp_send_initiate(inp, stcb);
1359163953Srrs	}
1360163953Srrs	SCTP_TCB_UNLOCK(stcb);
1361163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1362163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1363163953Srrs		/* Set the connected flag so we can queue data */
1364163953Srrs		soisconnecting(so);
1365163953Srrs	}
1366163953Srrsout_now:
1367163953Srrs	if (creat_lock_on)
1368163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1369163953Srrs	SCTP_INP_DECR_REF(inp);
1370163953Srrs	return error;
1371163953Srrs}
1372163953Srrs
1373166675Srrs#define SCTP_FIND_STCB(inp, stcb, assoc_id) \
1374166675Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { \
1375166675Srrs		SCTP_INP_RLOCK(inp); \
1376166675Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1377166675Srrs		if (stcb) \
1378166675Srrs			SCTP_TCB_LOCK(stcb); \
1379166675Srrs		SCTP_INP_RUNLOCK(inp); \
1380166675Srrs	} else if (assoc_id != 0) { \
1381166675Srrs		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1382166675Srrs		if (stcb == NULL) { \
1383166675Srrs			error = ENOENT; \
1384166675Srrs			break; \
1385166675Srrs		} \
1386166675Srrs	} else { \
1387166675Srrs		stcb = NULL; \
1388166675Srrs	}
1389163953Srrs
1390166675Srrs#define SCTP_CHECK_AND_CAST(destp, srcp, type, size) \
1391166675Srrs	if (size < sizeof(type)) { \
1392166675Srrs		error = EINVAL; \
1393166675Srrs		break; \
1394166675Srrs	} else { \
1395166675Srrs		destp = (type *)srcp; \
1396166675Srrs	}
1397163953Srrs
1398163953Srrsstatic int
1399166675Srrssctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1400166675Srrs    void *p)
1401163953Srrs{
1402163953Srrs	struct sctp_inpcb *inp;
1403166675Srrs	int error, val = 0;
1404163953Srrs	struct sctp_tcb *stcb = NULL;
1405163953Srrs
1406166675Srrs	if (optval == NULL) {
1407166675Srrs		return (EINVAL);
1408166675Srrs	}
1409163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1410163953Srrs	if (inp == 0)
1411163953Srrs		return EINVAL;
1412163953Srrs	error = 0;
1413163953Srrs
1414166675Srrs	switch (optname) {
1415163953Srrs	case SCTP_NODELAY:
1416163953Srrs	case SCTP_AUTOCLOSE:
1417163953Srrs	case SCTP_EXPLICIT_EOR:
1418163953Srrs	case SCTP_AUTO_ASCONF:
1419163953Srrs	case SCTP_DISABLE_FRAGMENTS:
1420163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
1421163953Srrs	case SCTP_USE_EXT_RCVINFO:
1422163953Srrs		SCTP_INP_RLOCK(inp);
1423166675Srrs		switch (optname) {
1424163953Srrs		case SCTP_DISABLE_FRAGMENTS:
1425166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1426163953Srrs			break;
1427163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
1428166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1429163953Srrs			break;
1430163953Srrs		case SCTP_AUTO_ASCONF:
1431166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1432163953Srrs			break;
1433163953Srrs		case SCTP_EXPLICIT_EOR:
1434166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1435163953Srrs			break;
1436163953Srrs		case SCTP_NODELAY:
1437166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1438163953Srrs			break;
1439163953Srrs		case SCTP_USE_EXT_RCVINFO:
1440166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1441163953Srrs			break;
1442163953Srrs		case SCTP_AUTOCLOSE:
1443163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1444166675Srrs				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1445163953Srrs			else
1446166675Srrs				val = 0;
1447163953Srrs			break;
1448163953Srrs
1449163953Srrs		default:
1450163953Srrs			error = ENOPROTOOPT;
1451163953Srrs		}		/* end switch (sopt->sopt_name) */
1452166675Srrs		if (optname != SCTP_AUTOCLOSE) {
1453163953Srrs			/* make it an "on/off" value */
1454166675Srrs			val = (val != 0);
1455163953Srrs		}
1456166675Srrs		if (*optsize < sizeof(val)) {
1457163953Srrs			error = EINVAL;
1458163953Srrs		}
1459163953Srrs		SCTP_INP_RUNLOCK(inp);
1460163953Srrs		if (error == 0) {
1461163953Srrs			/* return the option value */
1462166675Srrs			*(int *)optval = val;
1463166675Srrs			*optsize = sizeof(val);
1464163953Srrs		}
1465163953Srrs		break;
1466167598Srrs
1467163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
1468163953Srrs		{
1469166675Srrs			uint32_t *value;
1470166675Srrs
1471166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1472166675Srrs			*value = inp->partial_delivery_point;
1473166675Srrs			*optsize = sizeof(uint32_t);
1474163953Srrs		}
1475163953Srrs		break;
1476163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
1477163953Srrs		{
1478166675Srrs			uint32_t *value;
1479166675Srrs
1480166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1481168943Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1482168943Srrs				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1483168943Srrs					*value = SCTP_FRAG_LEVEL_2;
1484168943Srrs				} else {
1485168943Srrs					*value = SCTP_FRAG_LEVEL_1;
1486168943Srrs				}
1487168943Srrs			} else {
1488168943Srrs				*value = SCTP_FRAG_LEVEL_0;
1489168943Srrs			}
1490166675Srrs			*optsize = sizeof(uint32_t);
1491163953Srrs		}
1492163953Srrs		break;
1493163953Srrs	case SCTP_CMT_ON_OFF:
1494163953Srrs		{
1495166675Srrs			struct sctp_assoc_value *av;
1496166675Srrs
1497166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1498166675Srrs			if (sctp_cmt_on_off) {
1499166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1500166675Srrs				if (stcb) {
1501166675Srrs					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1502166675Srrs					SCTP_TCB_UNLOCK(stcb);
1503166675Srrs
1504166675Srrs				} else {
1505166675Srrs					error = ENOTCONN;
1506166675Srrs				}
1507166675Srrs			} else {
1508166675Srrs				error = ENOPROTOOPT;
1509163953Srrs			}
1510166675Srrs			*optsize = sizeof(*av);
1511163953Srrs		}
1512163953Srrs		break;
1513163953Srrs	case SCTP_GET_ADDR_LEN:
1514163953Srrs		{
1515163953Srrs			struct sctp_assoc_value *av;
1516163953Srrs
1517166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1518163953Srrs			error = EINVAL;
1519167598Srrs#ifdef INET
1520163953Srrs			if (av->assoc_value == AF_INET) {
1521163953Srrs				av->assoc_value = sizeof(struct sockaddr_in);
1522163953Srrs				error = 0;
1523163953Srrs			}
1524163953Srrs#endif
1525167598Srrs#ifdef INET6
1526163953Srrs			if (av->assoc_value == AF_INET6) {
1527163953Srrs				av->assoc_value = sizeof(struct sockaddr_in6);
1528163953Srrs				error = 0;
1529163953Srrs			}
1530163953Srrs#endif
1531166675Srrs			*optsize = sizeof(*av);
1532163953Srrs		}
1533163953Srrs		break;
1534163953Srrs	case SCTP_GET_ASOC_ID_LIST:
1535163953Srrs		{
1536163953Srrs			struct sctp_assoc_ids *ids;
1537163953Srrs			int cnt, at;
1538163953Srrs			uint16_t orig;
1539163953Srrs
1540166675Srrs			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1541163953Srrs			cnt = 0;
1542163953Srrs			SCTP_INP_RLOCK(inp);
1543163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1544163953Srrs			if (stcb == NULL) {
1545163953Srrs		none_out_now:
1546163953Srrs				ids->asls_numb_present = 0;
1547163953Srrs				ids->asls_more_to_get = 0;
1548163953Srrs				SCTP_INP_RUNLOCK(inp);
1549163953Srrs				break;
1550163953Srrs			}
1551163953Srrs			orig = ids->asls_assoc_start;
1552163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1553163953Srrs			while (orig) {
1554163953Srrs				stcb = LIST_NEXT(stcb, sctp_tcblist);
1555163953Srrs				orig--;
1556163953Srrs				cnt--;
1557163953Srrs				if (stcb == NULL)
1558163953Srrs					goto none_out_now;
1559163953Srrs			}
1560163953Srrs			if (stcb == NULL)
1561163953Srrs				goto none_out_now;
1562163953Srrs
1563163953Srrs			at = 0;
1564163953Srrs			ids->asls_numb_present = 0;
1565163953Srrs			ids->asls_more_to_get = 1;
1566163953Srrs			while (at < MAX_ASOC_IDS_RET) {
1567163953Srrs				ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1568163953Srrs				at++;
1569163953Srrs				ids->asls_numb_present++;
1570163953Srrs				stcb = LIST_NEXT(stcb, sctp_tcblist);
1571163953Srrs				if (stcb == NULL) {
1572163953Srrs					ids->asls_more_to_get = 0;
1573163953Srrs					break;
1574163953Srrs				}
1575163953Srrs			}
1576163953Srrs			SCTP_INP_RUNLOCK(inp);
1577163953Srrs		}
1578163953Srrs		break;
1579163953Srrs	case SCTP_CONTEXT:
1580163953Srrs		{
1581163953Srrs			struct sctp_assoc_value *av;
1582163953Srrs
1583166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1584166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1585166675Srrs
1586166675Srrs			if (stcb) {
1587166675Srrs				av->assoc_value = stcb->asoc.context;
1588166675Srrs				SCTP_TCB_UNLOCK(stcb);
1589163953Srrs			} else {
1590166675Srrs				SCTP_INP_RLOCK(inp);
1591163953Srrs				av->assoc_value = inp->sctp_context;
1592166675Srrs				SCTP_INP_RUNLOCK(inp);
1593163953Srrs			}
1594166675Srrs			*optsize = sizeof(*av);
1595163953Srrs		}
1596163953Srrs		break;
1597167598Srrs	case SCTP_VRF_ID:
1598167598Srrs		{
1599167598Srrs			uint32_t *vrf_id;
1600167598Srrs
1601167598Srrs			SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, *optsize);
1602167598Srrs			*vrf_id = inp->def_vrf_id;
1603167598Srrs			break;
1604167598Srrs		}
1605167598Srrs	case SCTP_GET_ASOC_VRF:
1606167598Srrs		{
1607167598Srrs			struct sctp_assoc_value *id;
1608167598Srrs
1609167598Srrs			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1610167598Srrs			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1611167598Srrs			if (stcb == NULL) {
1612167598Srrs				error = EINVAL;
1613167598Srrs				break;
1614167598Srrs			}
1615167598Srrs			id->assoc_value = stcb->asoc.vrf_id;
1616167598Srrs			break;
1617167598Srrs		}
1618167598Srrs	case SCTP_GET_VRF_IDS:
1619167598Srrs		{
1620167598Srrs			error = EOPNOTSUPP;
1621167598Srrs			break;
1622167598Srrs		}
1623163953Srrs	case SCTP_GET_NONCE_VALUES:
1624163953Srrs		{
1625163953Srrs			struct sctp_get_nonce_values *gnv;
1626163953Srrs
1627166675Srrs			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1628166675Srrs			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1629166675Srrs
1630166675Srrs			if (stcb) {
1631163953Srrs				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1632163953Srrs				gnv->gn_local_tag = stcb->asoc.my_vtag;
1633163953Srrs				SCTP_TCB_UNLOCK(stcb);
1634166675Srrs			} else {
1635166675Srrs				error = ENOTCONN;
1636163953Srrs			}
1637166675Srrs			*optsize = sizeof(*gnv);
1638163953Srrs		}
1639163953Srrs		break;
1640163953Srrs	case SCTP_DELAYED_ACK_TIME:
1641163953Srrs		{
1642163953Srrs			struct sctp_assoc_value *tm;
1643163953Srrs
1644166675Srrs			SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, *optsize);
1645166675Srrs			SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
1646163953Srrs
1647166675Srrs			if (stcb) {
1648166675Srrs				tm->assoc_value = stcb->asoc.delayed_ack;
1649166675Srrs				SCTP_TCB_UNLOCK(stcb);
1650166675Srrs			} else {
1651163953Srrs				SCTP_INP_RLOCK(inp);
1652166675Srrs				tm->assoc_value = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1653163953Srrs				SCTP_INP_RUNLOCK(inp);
1654163953Srrs			}
1655166675Srrs			*optsize = sizeof(*tm);
1656163953Srrs		}
1657163953Srrs		break;
1658163953Srrs
1659163953Srrs	case SCTP_GET_SNDBUF_USE:
1660166675Srrs		{
1661163953Srrs			struct sctp_sockstat *ss;
1662163953Srrs
1663166675Srrs			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1664166675Srrs			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1665166675Srrs
1666166675Srrs			if (stcb) {
1667166675Srrs				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1668166675Srrs				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1669166675Srrs				    stcb->asoc.size_on_all_streams);
1670166675Srrs				SCTP_TCB_UNLOCK(stcb);
1671166675Srrs			} else {
1672163953Srrs				error = ENOTCONN;
1673163953Srrs			}
1674166675Srrs			*optsize = sizeof(struct sctp_sockstat);
1675163953Srrs		}
1676163953Srrs		break;
1677163953Srrs	case SCTP_MAXBURST:
1678163953Srrs		{
1679166675Srrs			uint8_t *value;
1680163953Srrs
1681166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1682166675Srrs
1683163953Srrs			SCTP_INP_RLOCK(inp);
1684166675Srrs			*value = inp->sctp_ep.max_burst;
1685163953Srrs			SCTP_INP_RUNLOCK(inp);
1686166675Srrs			*optsize = sizeof(uint8_t);
1687163953Srrs		}
1688163953Srrs		break;
1689163953Srrs	case SCTP_MAXSEG:
1690163953Srrs		{
1691167598Srrs			struct sctp_assoc_value *av;
1692163953Srrs			int ovh;
1693163953Srrs
1694167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1695168859Srrs			if (av->assoc_id) {
1696168859Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1697168859Srrs			} else {
1698168859Srrs				stcb = NULL;
1699168859Srrs			}
1700163953Srrs
1701167598Srrs			if (stcb) {
1702167598Srrs				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1703167598Srrs				SCTP_TCB_UNLOCK(stcb);
1704163953Srrs			} else {
1705167598Srrs				SCTP_INP_RLOCK(inp);
1706167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1707167598Srrs					ovh = SCTP_MED_OVERHEAD;
1708167598Srrs				} else {
1709167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
1710167598Srrs				}
1711167598Srrs				av->assoc_value = inp->sctp_frag_point - ovh;
1712167598Srrs				SCTP_INP_RUNLOCK(inp);
1713163953Srrs			}
1714167598Srrs			*optsize = sizeof(struct sctp_assoc_value);
1715163953Srrs		}
1716163953Srrs		break;
1717163953Srrs	case SCTP_GET_STAT_LOG:
1718163953Srrs#ifdef SCTP_STAT_LOGGING
1719167598Srrs		error = sctp_fill_stat_log(optval, optsize);
1720167598Srrs#else
1721163953Srrs		error = EOPNOTSUPP;
1722163953Srrs#endif
1723163953Srrs		break;
1724163953Srrs	case SCTP_EVENTS:
1725163953Srrs		{
1726163953Srrs			struct sctp_event_subscribe *events;
1727163953Srrs
1728166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1729163953Srrs			memset(events, 0, sizeof(*events));
1730163953Srrs			SCTP_INP_RLOCK(inp);
1731163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1732163953Srrs				events->sctp_data_io_event = 1;
1733163953Srrs
1734163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1735163953Srrs				events->sctp_association_event = 1;
1736163953Srrs
1737163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1738163953Srrs				events->sctp_address_event = 1;
1739163953Srrs
1740163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1741163953Srrs				events->sctp_send_failure_event = 1;
1742163953Srrs
1743163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1744163953Srrs				events->sctp_peer_error_event = 1;
1745163953Srrs
1746163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1747163953Srrs				events->sctp_shutdown_event = 1;
1748163953Srrs
1749163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1750163953Srrs				events->sctp_partial_delivery_event = 1;
1751163953Srrs
1752163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1753163953Srrs				events->sctp_adaptation_layer_event = 1;
1754163953Srrs
1755163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1756163953Srrs				events->sctp_authentication_event = 1;
1757163953Srrs
1758163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1759163953Srrs				events->sctp_stream_reset_events = 1;
1760163953Srrs			SCTP_INP_RUNLOCK(inp);
1761166675Srrs			*optsize = sizeof(struct sctp_event_subscribe);
1762163953Srrs		}
1763163953Srrs		break;
1764163953Srrs
1765163953Srrs	case SCTP_ADAPTATION_LAYER:
1766166675Srrs		{
1767166675Srrs			uint32_t *value;
1768166675Srrs
1769166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1770166675Srrs
1771166675Srrs			SCTP_INP_RLOCK(inp);
1772166675Srrs			*value = inp->sctp_ep.adaptation_layer_indicator;
1773166675Srrs			SCTP_INP_RUNLOCK(inp);
1774166675Srrs			*optsize = sizeof(uint32_t);
1775163953Srrs		}
1776163953Srrs		break;
1777163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
1778166675Srrs		{
1779166675Srrs			uint32_t *value;
1780166675Srrs
1781166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1782166675Srrs			SCTP_INP_RLOCK(inp);
1783166675Srrs			*value = inp->sctp_ep.initial_sequence_debug;
1784166675Srrs			SCTP_INP_RUNLOCK(inp);
1785166675Srrs			*optsize = sizeof(uint32_t);
1786163953Srrs		}
1787163953Srrs		break;
1788163953Srrs	case SCTP_GET_LOCAL_ADDR_SIZE:
1789166675Srrs		{
1790166675Srrs			uint32_t *value;
1791166675Srrs
1792166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1793166675Srrs			SCTP_INP_RLOCK(inp);
1794168124Srrs			*value = sctp_count_max_addresses(inp);
1795166675Srrs			SCTP_INP_RUNLOCK(inp);
1796166675Srrs			*optsize = sizeof(uint32_t);
1797163953Srrs		}
1798163953Srrs		break;
1799163953Srrs	case SCTP_GET_REMOTE_ADDR_SIZE:
1800163953Srrs		{
1801166675Srrs			uint32_t *value;
1802166675Srrs			size_t size;
1803163953Srrs			struct sctp_nets *net;
1804163953Srrs
1805166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1806166675Srrs			/* FIXME MT: change to sctp_assoc_value? */
1807166675Srrs			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1808166675Srrs
1809166675Srrs			if (stcb) {
1810166675Srrs				size = 0;
1811166675Srrs				/* Count the sizes */
1812166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1813166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1814166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1815166675Srrs						size += sizeof(struct sockaddr_in6);
1816166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1817166675Srrs						size += sizeof(struct sockaddr_in);
1818166675Srrs					} else {
1819166675Srrs						/* huh */
1820166675Srrs						break;
1821166675Srrs					}
1822163953Srrs				}
1823166675Srrs				SCTP_TCB_UNLOCK(stcb);
1824166675Srrs				*value = (uint32_t) size;
1825166675Srrs			} else {
1826166675Srrs				error = ENOTCONN;
1827163953Srrs			}
1828166675Srrs			*optsize = sizeof(uint32_t);
1829163953Srrs		}
1830163953Srrs		break;
1831163953Srrs	case SCTP_GET_PEER_ADDRESSES:
1832163953Srrs		/*
1833163953Srrs		 * Get the address information, an array is passed in to
1834163953Srrs		 * fill up we pack it.
1835163953Srrs		 */
1836163953Srrs		{
1837166675Srrs			size_t cpsz, left;
1838163953Srrs			struct sockaddr_storage *sas;
1839163953Srrs			struct sctp_nets *net;
1840163953Srrs			struct sctp_getaddresses *saddr;
1841163953Srrs
1842166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1843166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1844163953Srrs
1845166675Srrs			if (stcb) {
1846166675Srrs				left = (*optsize) - sizeof(struct sctp_getaddresses);
1847166675Srrs				*optsize = sizeof(struct sctp_getaddresses);
1848166675Srrs				sas = (struct sockaddr_storage *)&saddr->addr[0];
1849166675Srrs
1850166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1851166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1852166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1853166675Srrs						cpsz = sizeof(struct sockaddr_in6);
1854166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1855166675Srrs						cpsz = sizeof(struct sockaddr_in);
1856166675Srrs					} else {
1857166675Srrs						/* huh */
1858166675Srrs						break;
1859166675Srrs					}
1860166675Srrs					if (left < cpsz) {
1861166675Srrs						/* not enough room. */
1862166675Srrs						break;
1863166675Srrs					}
1864166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1865166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1866166675Srrs						/* Must map the address */
1867166675Srrs						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1868166675Srrs						    (struct sockaddr_in6 *)sas);
1869166675Srrs					} else {
1870166675Srrs						memcpy(sas, &net->ro._l_addr, cpsz);
1871166675Srrs					}
1872166675Srrs					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1873166675Srrs
1874166675Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1875166675Srrs					left -= cpsz;
1876166675Srrs					*optsize += cpsz;
1877163953Srrs				}
1878166675Srrs				SCTP_TCB_UNLOCK(stcb);
1879166675Srrs			} else {
1880166675Srrs				error = ENOENT;
1881163953Srrs			}
1882163953Srrs		}
1883163953Srrs		break;
1884163953Srrs	case SCTP_GET_LOCAL_ADDRESSES:
1885163953Srrs		{
1886166675Srrs			size_t limit, actual;
1887163953Srrs			struct sockaddr_storage *sas;
1888163953Srrs			struct sctp_getaddresses *saddr;
1889163953Srrs
1890166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1891166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1892163953Srrs
1893163953Srrs			sas = (struct sockaddr_storage *)&saddr->addr[0];
1894166675Srrs			limit = *optsize - sizeof(sctp_assoc_t);
1895168124Srrs			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1896163953Srrs			if (stcb)
1897163953Srrs				SCTP_TCB_UNLOCK(stcb);
1898166675Srrs			*optsize = sizeof(struct sockaddr_storage) + actual;
1899163953Srrs		}
1900163953Srrs		break;
1901163953Srrs	case SCTP_PEER_ADDR_PARAMS:
1902163953Srrs		{
1903163953Srrs			struct sctp_paddrparams *paddrp;
1904163953Srrs			struct sctp_nets *net;
1905163953Srrs
1906166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1907166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1908163953Srrs
1909163953Srrs			net = NULL;
1910166675Srrs			if (stcb) {
1911166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1912166675Srrs			} else {
1913166675Srrs				/*
1914166675Srrs				 * We increment here since
1915166675Srrs				 * sctp_findassociation_ep_addr() wil do a
1916166675Srrs				 * decrement if it finds the stcb as long as
1917166675Srrs				 * the locked tcb (last argument) is NOT a
1918166675Srrs				 * TCB.. aka NULL.
1919166675Srrs				 */
1920166675Srrs				SCTP_INP_INCR_REF(inp);
1921166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1922163953Srrs				if (stcb == NULL) {
1923166675Srrs					SCTP_INP_DECR_REF(inp);
1924163953Srrs				}
1925163953Srrs			}
1926163953Srrs
1927163953Srrs			if (stcb) {
1928163953Srrs				/* Applys to the specific association */
1929163953Srrs				paddrp->spp_flags = 0;
1930163953Srrs				if (net) {
1931163953Srrs					paddrp->spp_pathmaxrxt = net->failure_threshold;
1932163953Srrs					paddrp->spp_pathmtu = net->mtu;
1933163953Srrs					/* get flags for HB */
1934163953Srrs					if (net->dest_state & SCTP_ADDR_NOHB)
1935163953Srrs						paddrp->spp_flags |= SPP_HB_DISABLE;
1936163953Srrs					else
1937163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
1938163953Srrs					/* get flags for PMTU */
1939165647Srrs					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
1940163953Srrs						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
1941163953Srrs					} else {
1942163953Srrs						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
1943163953Srrs					}
1944167598Srrs#ifdef INET
1945163953Srrs					if (net->ro._l_addr.sin.sin_family == AF_INET) {
1946163953Srrs						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
1947163953Srrs						paddrp->spp_flags |= SPP_IPV4_TOS;
1948163953Srrs					}
1949163953Srrs#endif
1950167598Srrs#ifdef INET6
1951163953Srrs					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
1952163953Srrs						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
1953163953Srrs						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1954163953Srrs					}
1955163953Srrs#endif
1956163953Srrs				} else {
1957163953Srrs					/*
1958163953Srrs					 * No destination so return default
1959163953Srrs					 * value
1960163953Srrs					 */
1961163953Srrs					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
1962163953Srrs					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
1963167598Srrs#ifdef INET
1964163953Srrs					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
1965163953Srrs					paddrp->spp_flags |= SPP_IPV4_TOS;
1966163953Srrs#endif
1967167598Srrs#ifdef INET6
1968163953Srrs					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
1969163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1970163953Srrs#endif
1971163953Srrs					/* default settings should be these */
1972163953Srrs					if (sctp_is_hb_timer_running(stcb)) {
1973163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
1974163953Srrs					}
1975163953Srrs				}
1976163953Srrs				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
1977163953Srrs				paddrp->spp_assoc_id = sctp_get_associd(stcb);
1978163953Srrs				SCTP_TCB_UNLOCK(stcb);
1979163953Srrs			} else {
1980163953Srrs				/* Use endpoint defaults */
1981163953Srrs				SCTP_INP_RLOCK(inp);
1982163953Srrs				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
1983163953Srrs				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1984163953Srrs				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
1985163953Srrs				/* get inp's default */
1986167598Srrs#ifdef INET
1987163953Srrs				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
1988163953Srrs				paddrp->spp_flags |= SPP_IPV4_TOS;
1989163953Srrs#endif
1990167598Srrs#ifdef INET6
1991163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1992163953Srrs					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
1993163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1994163953Srrs				}
1995163953Srrs#endif
1996163953Srrs				/* can't return this */
1997163953Srrs				paddrp->spp_pathmaxrxt = 0;
1998163953Srrs				paddrp->spp_pathmtu = 0;
1999163953Srrs				/* default behavior, no stcb */
2000167598Srrs				paddrp->spp_flags = SPP_HB_ENABLE | SPP_PMTUD_ENABLE;
2001163953Srrs
2002163953Srrs				SCTP_INP_RUNLOCK(inp);
2003163953Srrs			}
2004166675Srrs			*optsize = sizeof(struct sctp_paddrparams);
2005163953Srrs		}
2006163953Srrs		break;
2007163953Srrs	case SCTP_GET_PEER_ADDR_INFO:
2008163953Srrs		{
2009163953Srrs			struct sctp_paddrinfo *paddri;
2010163953Srrs			struct sctp_nets *net;
2011163953Srrs
2012166675Srrs			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2013166675Srrs			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2014166675Srrs
2015163953Srrs			net = NULL;
2016166675Srrs			if (stcb) {
2017166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2018166675Srrs			} else {
2019166675Srrs				/*
2020166675Srrs				 * We increment here since
2021166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2022166675Srrs				 * decrement if it finds the stcb as long as
2023166675Srrs				 * the locked tcb (last argument) is NOT a
2024166675Srrs				 * TCB.. aka NULL.
2025166675Srrs				 */
2026166675Srrs				SCTP_INP_INCR_REF(inp);
2027166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2028166675Srrs				if (stcb == NULL) {
2029166675Srrs					SCTP_INP_DECR_REF(inp);
2030163953Srrs				}
2031166675Srrs			}
2032163953Srrs
2033166675Srrs			if ((stcb) && (net)) {
2034166675Srrs				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2035166675Srrs				paddri->spinfo_cwnd = net->cwnd;
2036166675Srrs				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2037166675Srrs				paddri->spinfo_rto = net->RTO;
2038166675Srrs				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2039166675Srrs				SCTP_TCB_UNLOCK(stcb);
2040163953Srrs			} else {
2041163953Srrs				if (stcb) {
2042163953Srrs					SCTP_TCB_UNLOCK(stcb);
2043163953Srrs				}
2044163953Srrs				error = ENOENT;
2045163953Srrs			}
2046166675Srrs			*optsize = sizeof(struct sctp_paddrinfo);
2047163953Srrs		}
2048163953Srrs		break;
2049163953Srrs	case SCTP_PCB_STATUS:
2050163953Srrs		{
2051163953Srrs			struct sctp_pcbinfo *spcb;
2052163953Srrs
2053166675Srrs			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2054163953Srrs			sctp_fill_pcbinfo(spcb);
2055166675Srrs			*optsize = sizeof(struct sctp_pcbinfo);
2056163953Srrs		}
2057163953Srrs		break;
2058167598Srrs
2059163953Srrs	case SCTP_STATUS:
2060163953Srrs		{
2061163953Srrs			struct sctp_nets *net;
2062163953Srrs			struct sctp_status *sstat;
2063163953Srrs
2064166675Srrs			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2065166675Srrs			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2066163953Srrs
2067163953Srrs			if (stcb == NULL) {
2068163953Srrs				error = EINVAL;
2069163953Srrs				break;
2070163953Srrs			}
2071163953Srrs			/*
2072163953Srrs			 * I think passing the state is fine since
2073163953Srrs			 * sctp_constants.h will be available to the user
2074163953Srrs			 * land.
2075163953Srrs			 */
2076163953Srrs			sstat->sstat_state = stcb->asoc.state;
2077163953Srrs			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2078163953Srrs			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2079163953Srrs			/*
2080163953Srrs			 * We can't include chunks that have been passed to
2081163953Srrs			 * the socket layer. Only things in queue.
2082163953Srrs			 */
2083163953Srrs			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2084163953Srrs			    stcb->asoc.cnt_on_all_streams);
2085163953Srrs
2086163953Srrs
2087163953Srrs			sstat->sstat_instrms = stcb->asoc.streamincnt;
2088163953Srrs			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2089163953Srrs			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2090163953Srrs			memcpy(&sstat->sstat_primary.spinfo_address,
2091163953Srrs			    &stcb->asoc.primary_destination->ro._l_addr,
2092163953Srrs			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2093163953Srrs			net = stcb->asoc.primary_destination;
2094163953Srrs			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2095163953Srrs			/*
2096163953Srrs			 * Again the user can get info from sctp_constants.h
2097163953Srrs			 * for what the state of the network is.
2098163953Srrs			 */
2099163953Srrs			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2100163953Srrs			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2101163953Srrs			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2102163953Srrs			sstat->sstat_primary.spinfo_rto = net->RTO;
2103163953Srrs			sstat->sstat_primary.spinfo_mtu = net->mtu;
2104163953Srrs			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2105163953Srrs			SCTP_TCB_UNLOCK(stcb);
2106166675Srrs			*optsize = sizeof(*sstat);
2107163953Srrs		}
2108163953Srrs		break;
2109163953Srrs	case SCTP_RTOINFO:
2110163953Srrs		{
2111163953Srrs			struct sctp_rtoinfo *srto;
2112163953Srrs
2113166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2114166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2115166675Srrs
2116166675Srrs			if (stcb) {
2117166675Srrs				srto->srto_initial = stcb->asoc.initial_rto;
2118166675Srrs				srto->srto_max = stcb->asoc.maxrto;
2119166675Srrs				srto->srto_min = stcb->asoc.minrto;
2120166675Srrs				SCTP_TCB_UNLOCK(stcb);
2121166675Srrs			} else {
2122163953Srrs				SCTP_INP_RLOCK(inp);
2123163953Srrs				srto->srto_initial = inp->sctp_ep.initial_rto;
2124163953Srrs				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2125163953Srrs				srto->srto_min = inp->sctp_ep.sctp_minrto;
2126163953Srrs				SCTP_INP_RUNLOCK(inp);
2127163953Srrs			}
2128166675Srrs			*optsize = sizeof(*srto);
2129163953Srrs		}
2130163953Srrs		break;
2131163953Srrs	case SCTP_ASSOCINFO:
2132163953Srrs		{
2133163953Srrs			struct sctp_assocparams *sasoc;
2134163953Srrs
2135166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2136166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2137163953Srrs
2138163953Srrs			if (stcb) {
2139163953Srrs				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2140163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2141163953Srrs				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2142163953Srrs				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2143163953Srrs				sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2144167598Srrs				sasoc->sasoc_sack_delay = stcb->asoc.delayed_ack;
2145167598Srrs				sasoc->sasoc_sack_freq = stcb->asoc.sack_freq;
2146163953Srrs				SCTP_TCB_UNLOCK(stcb);
2147163953Srrs			} else {
2148163953Srrs				SCTP_INP_RLOCK(inp);
2149163953Srrs				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2150163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
2151163953Srrs				sasoc->sasoc_peer_rwnd = 0;
2152163953Srrs				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2153163953Srrs				sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2154167598Srrs				sasoc->sasoc_sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2155167598Srrs				sasoc->sasoc_sack_freq = inp->sctp_ep.sctp_sack_freq;
2156163953Srrs				SCTP_INP_RUNLOCK(inp);
2157163953Srrs			}
2158166675Srrs			*optsize = sizeof(*sasoc);
2159163953Srrs		}
2160163953Srrs		break;
2161163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2162163953Srrs		{
2163163953Srrs			struct sctp_sndrcvinfo *s_info;
2164163953Srrs
2165166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2166166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2167166675Srrs
2168166675Srrs			if (stcb) {
2169166675Srrs				*s_info = stcb->asoc.def_send;
2170166675Srrs				SCTP_TCB_UNLOCK(stcb);
2171166675Srrs			} else {
2172163953Srrs				SCTP_INP_RLOCK(inp);
2173166675Srrs				*s_info = inp->def_send;
2174163953Srrs				SCTP_INP_RUNLOCK(inp);
2175163953Srrs			}
2176166675Srrs			*optsize = sizeof(*s_info);
2177163953Srrs		}
2178163953Srrs		break;
2179163953Srrs	case SCTP_INITMSG:
2180163953Srrs		{
2181163953Srrs			struct sctp_initmsg *sinit;
2182163953Srrs
2183166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2184163953Srrs			SCTP_INP_RLOCK(inp);
2185163953Srrs			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2186163953Srrs			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2187163953Srrs			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2188163953Srrs			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2189163953Srrs			SCTP_INP_RUNLOCK(inp);
2190166675Srrs			*optsize = sizeof(*sinit);
2191163953Srrs		}
2192163953Srrs		break;
2193163953Srrs	case SCTP_PRIMARY_ADDR:
2194163953Srrs		/* we allow a "get" operation on this */
2195163953Srrs		{
2196163953Srrs			struct sctp_setprim *ssp;
2197163953Srrs
2198166675Srrs			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2199166675Srrs			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2200166675Srrs
2201166675Srrs			if (stcb) {
2202166675Srrs				/* simply copy out the sockaddr_storage... */
2203166675Srrs				memcpy(&ssp->ssp_addr, &stcb->asoc.primary_destination->ro._l_addr,
2204166675Srrs				    ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2205166675Srrs				SCTP_TCB_UNLOCK(stcb);
2206166675Srrs			} else {
2207163953Srrs				error = EINVAL;
2208163953Srrs			}
2209166675Srrs			*optsize = sizeof(*ssp);
2210163953Srrs		}
2211163953Srrs		break;
2212163953Srrs
2213163953Srrs	case SCTP_HMAC_IDENT:
2214163953Srrs		{
2215163953Srrs			struct sctp_hmacalgo *shmac;
2216163953Srrs			sctp_hmaclist_t *hmaclist;
2217163953Srrs			uint32_t size;
2218163953Srrs			int i;
2219163953Srrs
2220166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2221166675Srrs
2222163953Srrs			SCTP_INP_RLOCK(inp);
2223163953Srrs			hmaclist = inp->sctp_ep.local_hmacs;
2224163953Srrs			if (hmaclist == NULL) {
2225163953Srrs				/* no HMACs to return */
2226166675Srrs				*optsize = sizeof(*shmac);
2227168299Srrs				SCTP_INP_RUNLOCK(inp);
2228163953Srrs				break;
2229163953Srrs			}
2230163953Srrs			/* is there room for all of the hmac ids? */
2231163953Srrs			size = sizeof(*shmac) + (hmaclist->num_algo *
2232163953Srrs			    sizeof(shmac->shmac_idents[0]));
2233166675Srrs			if ((size_t)(*optsize) < size) {
2234163953Srrs				error = EINVAL;
2235163953Srrs				SCTP_INP_RUNLOCK(inp);
2236163953Srrs				break;
2237163953Srrs			}
2238163953Srrs			/* copy in the list */
2239163953Srrs			for (i = 0; i < hmaclist->num_algo; i++)
2240163953Srrs				shmac->shmac_idents[i] = hmaclist->hmac[i];
2241163953Srrs			SCTP_INP_RUNLOCK(inp);
2242166675Srrs			*optsize = size;
2243163953Srrs			break;
2244163953Srrs		}
2245163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2246163953Srrs		{
2247163953Srrs			struct sctp_authkeyid *scact;
2248163953Srrs
2249166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2250166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2251166675Srrs
2252166675Srrs			if (stcb) {
2253163953Srrs				/* get the active key on the assoc */
2254163953Srrs				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2255163953Srrs				SCTP_TCB_UNLOCK(stcb);
2256163953Srrs			} else {
2257163953Srrs				/* get the endpoint active key */
2258163953Srrs				SCTP_INP_RLOCK(inp);
2259163953Srrs				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2260163953Srrs				SCTP_INP_RUNLOCK(inp);
2261163953Srrs			}
2262166675Srrs			*optsize = sizeof(*scact);
2263163953Srrs			break;
2264163953Srrs		}
2265163953Srrs	case SCTP_LOCAL_AUTH_CHUNKS:
2266163953Srrs		{
2267163953Srrs			struct sctp_authchunks *sac;
2268163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2269166675Srrs			size_t size = 0;
2270163953Srrs
2271166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2272166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2273166675Srrs
2274166675Srrs			if (stcb) {
2275163953Srrs				/* get off the assoc */
2276163953Srrs				chklist = stcb->asoc.local_auth_chunks;
2277163953Srrs				/* is there enough space? */
2278163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2279166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2280163953Srrs					error = EINVAL;
2281166675Srrs				} else {
2282166675Srrs					/* copy in the chunks */
2283166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2284163953Srrs				}
2285163953Srrs				SCTP_TCB_UNLOCK(stcb);
2286163953Srrs			} else {
2287163953Srrs				/* get off the endpoint */
2288163953Srrs				SCTP_INP_RLOCK(inp);
2289163953Srrs				chklist = inp->sctp_ep.local_auth_chunks;
2290163953Srrs				/* is there enough space? */
2291163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2292166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2293163953Srrs					error = EINVAL;
2294166675Srrs				} else {
2295166675Srrs					/* copy in the chunks */
2296166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2297163953Srrs				}
2298163953Srrs				SCTP_INP_RUNLOCK(inp);
2299163953Srrs			}
2300166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2301163953Srrs			break;
2302163953Srrs		}
2303163953Srrs	case SCTP_PEER_AUTH_CHUNKS:
2304163953Srrs		{
2305163953Srrs			struct sctp_authchunks *sac;
2306163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2307166675Srrs			size_t size = 0;
2308163953Srrs
2309166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2310166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2311166675Srrs
2312166675Srrs			if (stcb) {
2313166675Srrs				/* get off the assoc */
2314166675Srrs				chklist = stcb->asoc.peer_auth_chunks;
2315166675Srrs				/* is there enough space? */
2316166675Srrs				size = sctp_auth_get_chklist_size(chklist);
2317166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2318166675Srrs					error = EINVAL;
2319166675Srrs				} else {
2320166675Srrs					/* copy in the chunks */
2321166675Srrs					sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2322166675Srrs				}
2323166675Srrs				SCTP_TCB_UNLOCK(stcb);
2324166675Srrs			} else {
2325163953Srrs				error = ENOENT;
2326163953Srrs			}
2327166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2328163953Srrs			break;
2329163953Srrs		}
2330163953Srrs
2331163953Srrs
2332163953Srrs	default:
2333163953Srrs		error = ENOPROTOOPT;
2334166675Srrs		*optsize = 0;
2335163953Srrs		break;
2336163953Srrs	}			/* end switch (sopt->sopt_name) */
2337163953Srrs	return (error);
2338163953Srrs}
2339163953Srrs
2340163953Srrsstatic int
2341166675Srrssctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2342166675Srrs    void *p)
2343163953Srrs{
2344166675Srrs	int error, set_opt;
2345166675Srrs	uint32_t *mopt;
2346163953Srrs	struct sctp_tcb *stcb = NULL;
2347163953Srrs	struct sctp_inpcb *inp;
2348167598Srrs	uint32_t vrf_id;
2349163953Srrs
2350166675Srrs	if (optval == NULL) {
2351167598Srrs		printf("optval is NULL\n");
2352163953Srrs		return (EINVAL);
2353163953Srrs	}
2354163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
2355167598Srrs	if (inp == 0) {
2356167598Srrs		printf("inp is NULL?\n");
2357163953Srrs		return EINVAL;
2358167598Srrs	}
2359168299Srrs	vrf_id = inp->def_vrf_id;
2360163953Srrs
2361163953Srrs	error = 0;
2362166675Srrs	switch (optname) {
2363163953Srrs	case SCTP_NODELAY:
2364163953Srrs	case SCTP_AUTOCLOSE:
2365163953Srrs	case SCTP_AUTO_ASCONF:
2366163953Srrs	case SCTP_EXPLICIT_EOR:
2367163953Srrs	case SCTP_DISABLE_FRAGMENTS:
2368163953Srrs	case SCTP_USE_EXT_RCVINFO:
2369163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
2370163953Srrs		/* copy in the option value */
2371166675Srrs		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2372163953Srrs		set_opt = 0;
2373163953Srrs		if (error)
2374163953Srrs			break;
2375166675Srrs		switch (optname) {
2376163953Srrs		case SCTP_DISABLE_FRAGMENTS:
2377163953Srrs			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2378163953Srrs			break;
2379163953Srrs		case SCTP_AUTO_ASCONF:
2380163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2381163953Srrs			break;
2382163953Srrs		case SCTP_EXPLICIT_EOR:
2383163953Srrs			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2384163953Srrs			break;
2385163953Srrs		case SCTP_USE_EXT_RCVINFO:
2386163953Srrs			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2387163953Srrs			break;
2388163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
2389163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2390163953Srrs				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2391163953Srrs			} else {
2392163953Srrs				return (EINVAL);
2393163953Srrs			}
2394163953Srrs			break;
2395163953Srrs		case SCTP_NODELAY:
2396163953Srrs			set_opt = SCTP_PCB_FLAGS_NODELAY;
2397163953Srrs			break;
2398163953Srrs		case SCTP_AUTOCLOSE:
2399163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2400163953Srrs			/*
2401163953Srrs			 * The value is in ticks. Note this does not effect
2402163953Srrs			 * old associations, only new ones.
2403163953Srrs			 */
2404163953Srrs			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2405163953Srrs			break;
2406163953Srrs		}
2407163953Srrs		SCTP_INP_WLOCK(inp);
2408163953Srrs		if (*mopt != 0) {
2409163953Srrs			sctp_feature_on(inp, set_opt);
2410163953Srrs		} else {
2411163953Srrs			sctp_feature_off(inp, set_opt);
2412163953Srrs		}
2413163953Srrs		SCTP_INP_WUNLOCK(inp);
2414163953Srrs		break;
2415163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
2416163953Srrs		{
2417166675Srrs			uint32_t *value;
2418166675Srrs
2419166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2420167736Srrs			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2421167736Srrs				error = EINVAL;
2422167736Srrs				break;
2423167736Srrs			}
2424166675Srrs			inp->partial_delivery_point = *value;
2425163953Srrs		}
2426163953Srrs		break;
2427163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
2428163953Srrs		/* not yet until we re-write sctp_recvmsg() */
2429163953Srrs		{
2430168943Srrs			uint32_t *level;
2431163953Srrs
2432168943Srrs			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2433168943Srrs			if (*level == SCTP_FRAG_LEVEL_2) {
2434163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2435168943Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2436168943Srrs			} else if (*level == SCTP_FRAG_LEVEL_1) {
2437168943Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2438168943Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2439168943Srrs			} else if (*level == SCTP_FRAG_LEVEL_0) {
2440168943Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2441168943Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2442168943Srrs
2443163953Srrs			} else {
2444168943Srrs				error = EINVAL;
2445163953Srrs			}
2446163953Srrs		}
2447163953Srrs		break;
2448163953Srrs	case SCTP_CMT_ON_OFF:
2449163953Srrs		{
2450163953Srrs			struct sctp_assoc_value *av;
2451163953Srrs
2452166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2453166675Srrs			if (sctp_cmt_on_off) {
2454166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2455166675Srrs				if (stcb) {
2456163953Srrs					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2457166675Srrs					SCTP_TCB_UNLOCK(stcb);
2458163953Srrs				} else {
2459166675Srrs					error = ENOTCONN;
2460163953Srrs				}
2461166675Srrs			} else {
2462166675Srrs				error = ENOPROTOOPT;
2463163953Srrs			}
2464163953Srrs		}
2465163953Srrs		break;
2466163953Srrs	case SCTP_CLR_STAT_LOG:
2467163953Srrs#ifdef SCTP_STAT_LOGGING
2468163953Srrs		sctp_clr_stat_log();
2469163953Srrs#else
2470163953Srrs		error = EOPNOTSUPP;
2471163953Srrs#endif
2472163953Srrs		break;
2473163953Srrs	case SCTP_CONTEXT:
2474163953Srrs		{
2475163953Srrs			struct sctp_assoc_value *av;
2476163953Srrs
2477166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2478166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2479166675Srrs
2480166675Srrs			if (stcb) {
2481166675Srrs				stcb->asoc.context = av->assoc_value;
2482166675Srrs				SCTP_TCB_UNLOCK(stcb);
2483163953Srrs			} else {
2484166675Srrs				SCTP_INP_WLOCK(inp);
2485163953Srrs				inp->sctp_context = av->assoc_value;
2486166675Srrs				SCTP_INP_WUNLOCK(inp);
2487163953Srrs			}
2488163953Srrs		}
2489163953Srrs		break;
2490167598Srrs	case SCTP_VRF_ID:
2491167598Srrs		{
2492167598Srrs			uint32_t *vrf_id;
2493167598Srrs
2494167598Srrs			SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, optsize);
2495167598Srrs			if (*vrf_id > SCTP_MAX_VRF_ID) {
2496167598Srrs				error = EINVAL;
2497167598Srrs				break;
2498167598Srrs			}
2499167598Srrs			inp->def_vrf_id = *vrf_id;
2500167598Srrs			break;
2501167598Srrs		}
2502167598Srrs	case SCTP_DEL_VRF_ID:
2503167598Srrs		{
2504167598Srrs			error = EOPNOTSUPP;
2505167598Srrs			break;
2506167598Srrs		}
2507167598Srrs	case SCTP_ADD_VRF_ID:
2508167598Srrs		{
2509167598Srrs			error = EOPNOTSUPP;
2510167598Srrs			break;
2511167598Srrs		}
2512167598Srrs
2513163953Srrs	case SCTP_DELAYED_ACK_TIME:
2514163953Srrs		{
2515163953Srrs			struct sctp_assoc_value *tm;
2516163953Srrs
2517166675Srrs			SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, optsize);
2518166675Srrs			SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
2519163953Srrs
2520166675Srrs			if (stcb) {
2521166675Srrs				stcb->asoc.delayed_ack = tm->assoc_value;
2522166675Srrs				SCTP_TCB_UNLOCK(stcb);
2523166675Srrs			} else {
2524163953Srrs				SCTP_INP_WLOCK(inp);
2525166675Srrs				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(tm->assoc_value);
2526163953Srrs				SCTP_INP_WUNLOCK(inp);
2527163953Srrs			}
2528166675Srrs			break;
2529163953Srrs		}
2530163953Srrs	case SCTP_AUTH_CHUNK:
2531163953Srrs		{
2532163953Srrs			struct sctp_authchunk *sauth;
2533163953Srrs
2534166675Srrs			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2535166675Srrs
2536166675Srrs			SCTP_INP_WLOCK(inp);
2537166675Srrs			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2538163953Srrs				error = EINVAL;
2539166675Srrs			SCTP_INP_WUNLOCK(inp);
2540163953Srrs			break;
2541163953Srrs		}
2542163953Srrs	case SCTP_AUTH_KEY:
2543163953Srrs		{
2544163953Srrs			struct sctp_authkey *sca;
2545163953Srrs			struct sctp_keyhead *shared_keys;
2546163953Srrs			sctp_sharedkey_t *shared_key;
2547163953Srrs			sctp_key_t *key = NULL;
2548166675Srrs			size_t size;
2549163953Srrs
2550166675Srrs			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2551166675Srrs			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id)
2552166675Srrs			    size = optsize - sizeof(*sca);
2553166675Srrs
2554166675Srrs			if (stcb) {
2555163953Srrs				/* set it on the assoc */
2556163953Srrs				shared_keys = &stcb->asoc.shared_keys;
2557163953Srrs				/* clear the cached keys for this key id */
2558163953Srrs				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2559163953Srrs				/*
2560163953Srrs				 * create the new shared key and
2561163953Srrs				 * insert/replace it
2562163953Srrs				 */
2563163953Srrs				if (size > 0) {
2564163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2565163953Srrs					if (key == NULL) {
2566163953Srrs						error = ENOMEM;
2567163953Srrs						SCTP_TCB_UNLOCK(stcb);
2568163953Srrs						break;
2569163953Srrs					}
2570163953Srrs				}
2571163953Srrs				shared_key = sctp_alloc_sharedkey();
2572163953Srrs				if (shared_key == NULL) {
2573163953Srrs					sctp_free_key(key);
2574163953Srrs					error = ENOMEM;
2575163953Srrs					SCTP_TCB_UNLOCK(stcb);
2576163953Srrs					break;
2577163953Srrs				}
2578163953Srrs				shared_key->key = key;
2579163953Srrs				shared_key->keyid = sca->sca_keynumber;
2580163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2581163953Srrs				SCTP_TCB_UNLOCK(stcb);
2582163953Srrs			} else {
2583166675Srrs				/* set it on the endpoint */
2584163953Srrs				SCTP_INP_WLOCK(inp);
2585163953Srrs				shared_keys = &inp->sctp_ep.shared_keys;
2586163953Srrs				/*
2587163953Srrs				 * clear the cached keys on all assocs for
2588163953Srrs				 * this key id
2589163953Srrs				 */
2590163953Srrs				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2591163953Srrs				/*
2592163953Srrs				 * create the new shared key and
2593163953Srrs				 * insert/replace it
2594163953Srrs				 */
2595163953Srrs				if (size > 0) {
2596163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2597163953Srrs					if (key == NULL) {
2598163953Srrs						error = ENOMEM;
2599163953Srrs						SCTP_INP_WUNLOCK(inp);
2600163953Srrs						break;
2601163953Srrs					}
2602163953Srrs				}
2603163953Srrs				shared_key = sctp_alloc_sharedkey();
2604163953Srrs				if (shared_key == NULL) {
2605163953Srrs					sctp_free_key(key);
2606163953Srrs					error = ENOMEM;
2607163953Srrs					SCTP_INP_WUNLOCK(inp);
2608163953Srrs					break;
2609163953Srrs				}
2610163953Srrs				shared_key->key = key;
2611163953Srrs				shared_key->keyid = sca->sca_keynumber;
2612163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2613163953Srrs				SCTP_INP_WUNLOCK(inp);
2614163953Srrs			}
2615163953Srrs			break;
2616163953Srrs		}
2617163953Srrs	case SCTP_HMAC_IDENT:
2618163953Srrs		{
2619163953Srrs			struct sctp_hmacalgo *shmac;
2620163953Srrs			sctp_hmaclist_t *hmaclist;
2621163953Srrs			uint32_t hmacid;
2622166675Srrs			size_t size, i;
2623163953Srrs
2624166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2625166675Srrs			size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2626163953Srrs			hmaclist = sctp_alloc_hmaclist(size);
2627163953Srrs			if (hmaclist == NULL) {
2628163953Srrs				error = ENOMEM;
2629163953Srrs				break;
2630163953Srrs			}
2631163953Srrs			for (i = 0; i < size; i++) {
2632163953Srrs				hmacid = shmac->shmac_idents[i];
2633163953Srrs				if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2634163953Srrs					 /* invalid HMACs were found */ ;
2635163953Srrs					error = EINVAL;
2636164085Srrs					sctp_free_hmaclist(hmaclist);
2637163953Srrs					goto sctp_set_hmac_done;
2638163953Srrs				}
2639163953Srrs			}
2640163953Srrs			/* set it on the endpoint */
2641163953Srrs			SCTP_INP_WLOCK(inp);
2642163953Srrs			if (inp->sctp_ep.local_hmacs)
2643163953Srrs				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2644163953Srrs			inp->sctp_ep.local_hmacs = hmaclist;
2645163953Srrs			SCTP_INP_WUNLOCK(inp);
2646163953Srrs	sctp_set_hmac_done:
2647163953Srrs			break;
2648163953Srrs		}
2649163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2650163953Srrs		{
2651163953Srrs			struct sctp_authkeyid *scact;
2652163953Srrs
2653166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2654166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2655166675Srrs
2656163953Srrs			/* set the active key on the right place */
2657166675Srrs			if (stcb) {
2658163953Srrs				/* set the active key on the assoc */
2659163953Srrs				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2660163953Srrs					error = EINVAL;
2661163953Srrs				SCTP_TCB_UNLOCK(stcb);
2662163953Srrs			} else {
2663163953Srrs				/* set the active key on the endpoint */
2664163953Srrs				SCTP_INP_WLOCK(inp);
2665163953Srrs				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2666163953Srrs					error = EINVAL;
2667163953Srrs				SCTP_INP_WUNLOCK(inp);
2668163953Srrs			}
2669163953Srrs			break;
2670163953Srrs		}
2671163953Srrs	case SCTP_AUTH_DELETE_KEY:
2672163953Srrs		{
2673163953Srrs			struct sctp_authkeyid *scdel;
2674163953Srrs
2675166675Srrs			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2676166675Srrs			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2677166675Srrs
2678163953Srrs			/* delete the key from the right place */
2679166675Srrs			if (stcb) {
2680163953Srrs				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2681163953Srrs					error = EINVAL;
2682163953Srrs				SCTP_TCB_UNLOCK(stcb);
2683163953Srrs			} else {
2684163953Srrs				SCTP_INP_WLOCK(inp);
2685163953Srrs				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2686163953Srrs					error = EINVAL;
2687163953Srrs				SCTP_INP_WUNLOCK(inp);
2688163953Srrs			}
2689163953Srrs			break;
2690163953Srrs		}
2691163953Srrs
2692163953Srrs	case SCTP_RESET_STREAMS:
2693163953Srrs		{
2694163953Srrs			struct sctp_stream_reset *strrst;
2695163953Srrs			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2696163953Srrs			int i;
2697163953Srrs
2698166675Srrs			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2699166675Srrs			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2700163953Srrs
2701163953Srrs			if (stcb == NULL) {
2702163953Srrs				error = ENOENT;
2703163953Srrs				break;
2704163953Srrs			}
2705163953Srrs			if (stcb->asoc.peer_supports_strreset == 0) {
2706163953Srrs				/*
2707163953Srrs				 * Peer does not support it, we return
2708163953Srrs				 * protocol not supported since this is true
2709163953Srrs				 * for this feature and this peer, not the
2710163953Srrs				 * socket request in general.
2711163953Srrs				 */
2712163953Srrs				error = EPROTONOSUPPORT;
2713163953Srrs				SCTP_TCB_UNLOCK(stcb);
2714163953Srrs				break;
2715163953Srrs			}
2716163953Srrs			if (stcb->asoc.stream_reset_outstanding) {
2717163953Srrs				error = EALREADY;
2718163953Srrs				SCTP_TCB_UNLOCK(stcb);
2719163953Srrs				break;
2720163953Srrs			}
2721163953Srrs			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2722163953Srrs				send_in = 1;
2723163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2724163953Srrs				send_out = 1;
2725163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2726163953Srrs				send_in = 1;
2727163953Srrs				send_out = 1;
2728163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2729163953Srrs				send_tsn = 1;
2730163953Srrs			} else {
2731163953Srrs				error = EINVAL;
2732163953Srrs				SCTP_TCB_UNLOCK(stcb);
2733163953Srrs				break;
2734163953Srrs			}
2735163953Srrs			for (i = 0; i < strrst->strrst_num_streams; i++) {
2736163953Srrs				if ((send_in) &&
2737163953Srrs
2738163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2739163953Srrs					error = EINVAL;
2740163953Srrs					goto get_out;
2741163953Srrs				}
2742163953Srrs				if ((send_out) &&
2743163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2744163953Srrs					error = EINVAL;
2745163953Srrs					goto get_out;
2746163953Srrs				}
2747163953Srrs			}
2748163953Srrs			if (error) {
2749163953Srrs		get_out:
2750163953Srrs				SCTP_TCB_UNLOCK(stcb);
2751163953Srrs				break;
2752163953Srrs			}
2753163953Srrs			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2754163953Srrs			    strrst->strrst_list,
2755163953Srrs			    send_out, (stcb->asoc.str_reset_seq_in - 3),
2756163953Srrs			    send_in, send_tsn);
2757163953Srrs
2758163953Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2759163953Srrs			SCTP_TCB_UNLOCK(stcb);
2760163953Srrs		}
2761163953Srrs		break;
2762166675Srrs
2763163953Srrs	case SCTP_CONNECT_X:
2764166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2765163953Srrs			error = EINVAL;
2766163953Srrs			break;
2767163953Srrs		}
2768166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2769163953Srrs		break;
2770163953Srrs
2771163953Srrs	case SCTP_CONNECT_X_DELAYED:
2772166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2773163953Srrs			error = EINVAL;
2774163953Srrs			break;
2775163953Srrs		}
2776166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2777163953Srrs		break;
2778163953Srrs
2779163953Srrs	case SCTP_CONNECT_X_COMPLETE:
2780163953Srrs		{
2781163953Srrs			struct sockaddr *sa;
2782163953Srrs			struct sctp_nets *net;
2783163953Srrs
2784166675Srrs			/* FIXME MT: check correct? */
2785166675Srrs			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2786166675Srrs
2787163953Srrs			/* find tcb */
2788163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2789163953Srrs				SCTP_INP_RLOCK(inp);
2790163953Srrs				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2791163953Srrs				if (stcb) {
2792163953Srrs					SCTP_TCB_LOCK(stcb);
2793163953Srrs					net = sctp_findnet(stcb, sa);
2794163953Srrs				}
2795163953Srrs				SCTP_INP_RUNLOCK(inp);
2796163953Srrs			} else {
2797166675Srrs				/*
2798166675Srrs				 * We increment here since
2799166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2800166675Srrs				 * decrement if it finds the stcb as long as
2801166675Srrs				 * the locked tcb (last argument) is NOT a
2802166675Srrs				 * TCB.. aka NULL.
2803166675Srrs				 */
2804163953Srrs				SCTP_INP_INCR_REF(inp);
2805163953Srrs				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2806163953Srrs				if (stcb == NULL) {
2807163953Srrs					SCTP_INP_DECR_REF(inp);
2808163953Srrs				}
2809163953Srrs			}
2810163953Srrs
2811163953Srrs			if (stcb == NULL) {
2812163953Srrs				error = ENOENT;
2813163953Srrs				break;
2814163953Srrs			}
2815163953Srrs			if (stcb->asoc.delayed_connection == 1) {
2816163953Srrs				stcb->asoc.delayed_connection = 0;
2817163953Srrs				SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2818165220Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
2819165220Srrs				    stcb->asoc.primary_destination,
2820165220Srrs				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
2821163953Srrs				sctp_send_initiate(inp, stcb);
2822163953Srrs			} else {
2823163953Srrs				/*
2824163953Srrs				 * already expired or did not use delayed
2825163953Srrs				 * connectx
2826163953Srrs				 */
2827163953Srrs				error = EALREADY;
2828163953Srrs			}
2829163953Srrs			SCTP_TCB_UNLOCK(stcb);
2830163953Srrs		}
2831163953Srrs		break;
2832163953Srrs	case SCTP_MAXBURST:
2833163953Srrs		{
2834163953Srrs			uint8_t *burst;
2835163953Srrs
2836166675Srrs			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
2837166675Srrs
2838163953Srrs			SCTP_INP_WLOCK(inp);
2839163953Srrs			if (*burst) {
2840163953Srrs				inp->sctp_ep.max_burst = *burst;
2841163953Srrs			}
2842163953Srrs			SCTP_INP_WUNLOCK(inp);
2843163953Srrs		}
2844163953Srrs		break;
2845163953Srrs	case SCTP_MAXSEG:
2846163953Srrs		{
2847167598Srrs			struct sctp_assoc_value *av;
2848163953Srrs			int ovh;
2849163953Srrs
2850167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2851167598Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2852166675Srrs
2853167598Srrs			if (stcb) {
2854167598Srrs				error = EINVAL;
2855167598Srrs				SCTP_TCB_UNLOCK(stcb);
2856163953Srrs			} else {
2857167598Srrs				SCTP_INP_WLOCK(inp);
2858167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2859167598Srrs					ovh = SCTP_MED_OVERHEAD;
2860167598Srrs				} else {
2861167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
2862167598Srrs				}
2863167598Srrs				/*
2864167598Srrs				 * FIXME MT: I think this is not in tune
2865167598Srrs				 * with the API ID
2866167598Srrs				 */
2867167598Srrs				if (av->assoc_value) {
2868167598Srrs					inp->sctp_frag_point = (av->assoc_value + ovh);
2869167598Srrs				} else {
2870167598Srrs					error = EINVAL;
2871167598Srrs				}
2872167598Srrs				SCTP_INP_WUNLOCK(inp);
2873163953Srrs			}
2874163953Srrs		}
2875163953Srrs		break;
2876163953Srrs	case SCTP_EVENTS:
2877163953Srrs		{
2878163953Srrs			struct sctp_event_subscribe *events;
2879163953Srrs
2880166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
2881166675Srrs
2882163953Srrs			SCTP_INP_WLOCK(inp);
2883163953Srrs			if (events->sctp_data_io_event) {
2884163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2885163953Srrs			} else {
2886163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2887163953Srrs			}
2888163953Srrs
2889163953Srrs			if (events->sctp_association_event) {
2890163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2891163953Srrs			} else {
2892163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2893163953Srrs			}
2894163953Srrs
2895163953Srrs			if (events->sctp_address_event) {
2896163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2897163953Srrs			} else {
2898163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2899163953Srrs			}
2900163953Srrs
2901163953Srrs			if (events->sctp_send_failure_event) {
2902163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2903163953Srrs			} else {
2904163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2905163953Srrs			}
2906163953Srrs
2907163953Srrs			if (events->sctp_peer_error_event) {
2908163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2909163953Srrs			} else {
2910163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2911163953Srrs			}
2912163953Srrs
2913163953Srrs			if (events->sctp_shutdown_event) {
2914163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2915163953Srrs			} else {
2916163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2917163953Srrs			}
2918163953Srrs
2919163953Srrs			if (events->sctp_partial_delivery_event) {
2920163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2921163953Srrs			} else {
2922163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2923163953Srrs			}
2924163953Srrs
2925163953Srrs			if (events->sctp_adaptation_layer_event) {
2926163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2927163953Srrs			} else {
2928163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2929163953Srrs			}
2930163953Srrs
2931163953Srrs			if (events->sctp_authentication_event) {
2932163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2933163953Srrs			} else {
2934163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2935163953Srrs			}
2936163953Srrs
2937163953Srrs			if (events->sctp_stream_reset_events) {
2938163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2939163953Srrs			} else {
2940163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2941163953Srrs			}
2942163953Srrs			SCTP_INP_WUNLOCK(inp);
2943163953Srrs		}
2944163953Srrs		break;
2945163953Srrs
2946163953Srrs	case SCTP_ADAPTATION_LAYER:
2947163953Srrs		{
2948163953Srrs			struct sctp_setadaptation *adap_bits;
2949163953Srrs
2950166675Srrs			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
2951163953Srrs			SCTP_INP_WLOCK(inp);
2952163953Srrs			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
2953163953Srrs			SCTP_INP_WUNLOCK(inp);
2954163953Srrs		}
2955163953Srrs		break;
2956166675Srrs#ifdef SCTP_DEBUG
2957163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
2958163953Srrs		{
2959163953Srrs			uint32_t *vvv;
2960163953Srrs
2961166675Srrs			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
2962163953Srrs			SCTP_INP_WLOCK(inp);
2963163953Srrs			inp->sctp_ep.initial_sequence_debug = *vvv;
2964163953Srrs			SCTP_INP_WUNLOCK(inp);
2965163953Srrs		}
2966163953Srrs		break;
2967166675Srrs#endif
2968163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2969163953Srrs		{
2970163953Srrs			struct sctp_sndrcvinfo *s_info;
2971163953Srrs
2972166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
2973166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2974163953Srrs
2975166675Srrs			if (stcb) {
2976166675Srrs				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
2977166675Srrs					stcb->asoc.def_send = *s_info;
2978163953Srrs				} else {
2979166675Srrs					error = EINVAL;
2980163953Srrs				}
2981166675Srrs				SCTP_TCB_UNLOCK(stcb);
2982166675Srrs			} else {
2983166675Srrs				SCTP_INP_WLOCK(inp);
2984163953Srrs				inp->def_send = *s_info;
2985166675Srrs				SCTP_INP_WUNLOCK(inp);
2986163953Srrs			}
2987163953Srrs		}
2988163953Srrs		break;
2989163953Srrs	case SCTP_PEER_ADDR_PARAMS:
2990163953Srrs		/* Applys to the specific association */
2991163953Srrs		{
2992163953Srrs			struct sctp_paddrparams *paddrp;
2993163953Srrs			struct sctp_nets *net;
2994163953Srrs
2995166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
2996166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2997163953Srrs			net = NULL;
2998166675Srrs			if (stcb) {
2999166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3000166675Srrs			} else {
3001166675Srrs				/*
3002166675Srrs				 * We increment here since
3003166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3004166675Srrs				 * decrement if it finds the stcb as long as
3005166675Srrs				 * the locked tcb (last argument) is NOT a
3006166675Srrs				 * TCB.. aka NULL.
3007166675Srrs				 */
3008166675Srrs				SCTP_INP_INCR_REF(inp);
3009166675Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3010166675Srrs				    (struct sockaddr *)&paddrp->spp_address,
3011166675Srrs				    &net, NULL, NULL);
3012163953Srrs				if (stcb == NULL) {
3013166675Srrs					SCTP_INP_DECR_REF(inp);
3014163953Srrs				}
3015163953Srrs			}
3016166675Srrs
3017166675Srrs
3018163953Srrs			if (stcb) {
3019163953Srrs				/************************TCB SPECIFIC SET ******************/
3020163953Srrs				/*
3021163953Srrs				 * do we change the timer for HB, we run
3022163953Srrs				 * only one?
3023163953Srrs				 */
3024163953Srrs				if (paddrp->spp_hbinterval)
3025163953Srrs					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3026163953Srrs				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3027163953Srrs					stcb->asoc.heart_beat_delay = 0;
3028163953Srrs
3029163953Srrs				/* network sets ? */
3030163953Srrs				if (net) {
3031163953Srrs					/************************NET SPECIFIC SET ******************/
3032163953Srrs					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3033163953Srrs						/* on demand HB */
3034163953Srrs						sctp_send_hb(stcb, 1, net);
3035163953Srrs					}
3036163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3037163953Srrs						net->dest_state |= SCTP_ADDR_NOHB;
3038163953Srrs					}
3039163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3040163953Srrs						net->dest_state &= ~SCTP_ADDR_NOHB;
3041163953Srrs					}
3042163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
3043165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3044165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3045165220Srrs							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3046163953Srrs						}
3047163953Srrs						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3048163953Srrs							net->mtu = paddrp->spp_pathmtu;
3049169352Srrs							if (net->mtu < stcb->asoc.smallest_mtu) {
3050169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
3051169352Srrs								printf("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3052169352Srrs								    net->mtu);
3053169352Srrs#endif
3054167695Srrs								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3055169352Srrs							}
3056163953Srrs						}
3057163953Srrs					}
3058163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3059165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3060163953Srrs							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3061163953Srrs						}
3062163953Srrs					}
3063163953Srrs					if (paddrp->spp_pathmaxrxt)
3064163953Srrs						net->failure_threshold = paddrp->spp_pathmaxrxt;
3065167598Srrs#ifdef INET
3066163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3067163953Srrs						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3068163953Srrs							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3069163953Srrs						}
3070163953Srrs					}
3071163953Srrs#endif
3072167598Srrs#ifdef INET6
3073163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3074163953Srrs						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3075163953Srrs							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3076163953Srrs						}
3077163953Srrs					}
3078163953Srrs#endif
3079163953Srrs				} else {
3080163953Srrs					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3081163953Srrs					if (paddrp->spp_pathmaxrxt)
3082163953Srrs						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3083163953Srrs
3084163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3085163953Srrs						/* Turn back on the timer */
3086163953Srrs						stcb->asoc.hb_is_disabled = 0;
3087163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3088163953Srrs					}
3089163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3090163953Srrs						int cnt_of_unconf = 0;
3091163953Srrs						struct sctp_nets *lnet;
3092163953Srrs
3093163953Srrs						stcb->asoc.hb_is_disabled = 1;
3094163953Srrs						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3095163953Srrs							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3096163953Srrs								cnt_of_unconf++;
3097163953Srrs							}
3098163953Srrs						}
3099163953Srrs						/*
3100163953Srrs						 * stop the timer ONLY if we
3101163953Srrs						 * have no unconfirmed
3102163953Srrs						 * addresses
3103163953Srrs						 */
3104163953Srrs						if (cnt_of_unconf == 0) {
3105165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3106163953Srrs						}
3107163953Srrs					}
3108163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3109163953Srrs						/* start up the timer. */
3110163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3111163953Srrs					}
3112167598Srrs#ifdef INET
3113163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS)
3114163953Srrs						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3115163953Srrs#endif
3116167598Srrs#ifdef INET6
3117163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3118163953Srrs						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3119163953Srrs#endif
3120163953Srrs
3121163953Srrs				}
3122163953Srrs				SCTP_TCB_UNLOCK(stcb);
3123163953Srrs			} else {
3124163953Srrs				/************************NO TCB, SET TO default stuff ******************/
3125163953Srrs				SCTP_INP_WLOCK(inp);
3126163953Srrs				/*
3127163953Srrs				 * For the TOS/FLOWLABEL stuff you set it
3128163953Srrs				 * with the options on the socket
3129163953Srrs				 */
3130163953Srrs				if (paddrp->spp_pathmaxrxt) {
3131163953Srrs					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3132163953Srrs				}
3133163953Srrs				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3134163953Srrs					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3135163953Srrs					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3136163953Srrs				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3137163953Srrs					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3138163953Srrs				}
3139163953Srrs				SCTP_INP_WUNLOCK(inp);
3140163953Srrs			}
3141163953Srrs		}
3142163953Srrs		break;
3143163953Srrs	case SCTP_RTOINFO:
3144163953Srrs		{
3145163953Srrs			struct sctp_rtoinfo *srto;
3146163953Srrs
3147166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3148166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3149166675Srrs
3150166675Srrs			if (stcb) {
3151166675Srrs				/* Set in ms we hope :-) */
3152167598Srrs				if (srto->srto_initial)
3153166675Srrs					stcb->asoc.initial_rto = srto->srto_initial;
3154167598Srrs				if (srto->srto_max)
3155166675Srrs					stcb->asoc.maxrto = srto->srto_max;
3156167598Srrs				if (srto->srto_min)
3157166675Srrs					stcb->asoc.minrto = srto->srto_min;
3158166675Srrs				SCTP_TCB_UNLOCK(stcb);
3159166675Srrs			} else {
3160163953Srrs				SCTP_INP_WLOCK(inp);
3161163953Srrs				/*
3162163953Srrs				 * If we have a null asoc, its default for
3163163953Srrs				 * the endpoint
3164163953Srrs				 */
3165167598Srrs				if (srto->srto_initial)
3166163953Srrs					inp->sctp_ep.initial_rto = srto->srto_initial;
3167167598Srrs				if (srto->srto_max)
3168163953Srrs					inp->sctp_ep.sctp_maxrto = srto->srto_max;
3169167598Srrs				if (srto->srto_min)
3170163953Srrs					inp->sctp_ep.sctp_minrto = srto->srto_min;
3171163953Srrs				SCTP_INP_WUNLOCK(inp);
3172163953Srrs			}
3173163953Srrs		}
3174163953Srrs		break;
3175163953Srrs	case SCTP_ASSOCINFO:
3176163953Srrs		{
3177163953Srrs			struct sctp_assocparams *sasoc;
3178163953Srrs
3179166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3180166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3181166675Srrs
3182163953Srrs			if (stcb) {
3183163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3184163953Srrs					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3185163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3186163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3187163953Srrs				sasoc->sasoc_local_rwnd = 0;
3188163953Srrs				if (stcb->asoc.cookie_life)
3189163953Srrs					stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3190167598Srrs				stcb->asoc.delayed_ack = sasoc->sasoc_sack_delay;
3191167598Srrs				if (sasoc->sasoc_sack_freq) {
3192167598Srrs					stcb->asoc.sack_freq = sasoc->sasoc_sack_freq;
3193167598Srrs				}
3194163953Srrs				SCTP_TCB_UNLOCK(stcb);
3195163953Srrs			} else {
3196163953Srrs				SCTP_INP_WLOCK(inp);
3197163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3198163953Srrs					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3199163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
3200163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3201163953Srrs				sasoc->sasoc_local_rwnd = 0;
3202163953Srrs				if (sasoc->sasoc_cookie_life)
3203163953Srrs					inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3204167598Srrs				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sasoc->sasoc_sack_delay);
3205167598Srrs				if (sasoc->sasoc_sack_freq) {
3206167598Srrs					inp->sctp_ep.sctp_sack_freq = sasoc->sasoc_sack_freq;
3207167598Srrs				}
3208163953Srrs				SCTP_INP_WUNLOCK(inp);
3209163953Srrs			}
3210163953Srrs		}
3211163953Srrs		break;
3212163953Srrs	case SCTP_INITMSG:
3213163953Srrs		{
3214163953Srrs			struct sctp_initmsg *sinit;
3215163953Srrs
3216166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3217163953Srrs			SCTP_INP_WLOCK(inp);
3218163953Srrs			if (sinit->sinit_num_ostreams)
3219163953Srrs				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3220163953Srrs
3221163953Srrs			if (sinit->sinit_max_instreams)
3222163953Srrs				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3223163953Srrs
3224163953Srrs			if (sinit->sinit_max_attempts)
3225163953Srrs				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3226163953Srrs
3227167598Srrs			if (sinit->sinit_max_init_timeo)
3228163953Srrs				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3229163953Srrs			SCTP_INP_WUNLOCK(inp);
3230163953Srrs		}
3231163953Srrs		break;
3232163953Srrs	case SCTP_PRIMARY_ADDR:
3233163953Srrs		{
3234163953Srrs			struct sctp_setprim *spa;
3235163953Srrs			struct sctp_nets *net, *lnet;
3236163953Srrs
3237166675Srrs			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3238166675Srrs			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3239163953Srrs
3240166675Srrs			net = NULL;
3241166675Srrs			if (stcb) {
3242166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3243166675Srrs			} else {
3244166675Srrs				/*
3245166675Srrs				 * We increment here since
3246166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3247166675Srrs				 * decrement if it finds the stcb as long as
3248166675Srrs				 * the locked tcb (last argument) is NOT a
3249166675Srrs				 * TCB.. aka NULL.
3250166675Srrs				 */
3251163953Srrs				SCTP_INP_INCR_REF(inp);
3252163953Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3253163953Srrs				    (struct sockaddr *)&spa->ssp_addr,
3254163953Srrs				    &net, NULL, NULL);
3255163953Srrs				if (stcb == NULL) {
3256163953Srrs					SCTP_INP_DECR_REF(inp);
3257163953Srrs				}
3258163953Srrs			}
3259166675Srrs
3260166675Srrs			if ((stcb) && (net)) {
3261166675Srrs				if ((net != stcb->asoc.primary_destination) &&
3262166675Srrs				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3263166675Srrs					/* Ok we need to set it */
3264166675Srrs					lnet = stcb->asoc.primary_destination;
3265166675Srrs					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3266166675Srrs						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3267166675Srrs							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3268166675Srrs						}
3269166675Srrs						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3270163953Srrs					}
3271163953Srrs				}
3272166675Srrs			} else {
3273166675Srrs				error = EINVAL;
3274163953Srrs			}
3275166675Srrs			if (stcb) {
3276166675Srrs				SCTP_TCB_UNLOCK(stcb);
3277166675Srrs			}
3278163953Srrs		}
3279163953Srrs		break;
3280167598Srrs	case SCTP_SET_DYNAMIC_PRIMARY:
3281167598Srrs		{
3282167598Srrs			union sctp_sockstore *ss;
3283163953Srrs
3284167598Srrs			error = priv_check_cred(curthread->td_ucred,
3285167598Srrs			    PRIV_NETINET_RESERVEDPORT,
3286167598Srrs			    SUSER_ALLOWJAIL);
3287167598Srrs			if (error)
3288167598Srrs				break;
3289167598Srrs
3290167598Srrs			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3291167598Srrs			/* SUPER USER CHECK? */
3292167598Srrs			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3293167598Srrs		}
3294167598Srrs		break;
3295163953Srrs	case SCTP_SET_PEER_PRIMARY_ADDR:
3296163953Srrs		{
3297163953Srrs			struct sctp_setpeerprim *sspp;
3298163953Srrs
3299166675Srrs			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3300166675Srrs			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3301169208Srrs			if (stcb != NULL) {
3302166675Srrs				if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3303166675Srrs					error = EINVAL;
3304166675Srrs				}
3305169208Srrs				SCTP_TCB_UNLOCK(stcb);
3306166675Srrs			} else {
3307163953Srrs				error = EINVAL;
3308163953Srrs			}
3309169208Srrs
3310163953Srrs		}
3311163953Srrs		break;
3312163953Srrs	case SCTP_BINDX_ADD_ADDR:
3313163953Srrs		{
3314163953Srrs			struct sctp_getaddresses *addrs;
3315163953Srrs			struct sockaddr *addr_touse;
3316163953Srrs			struct sockaddr_in sin;
3317163953Srrs
3318166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3319166675Srrs
3320163953Srrs			/* see if we're bound all already! */
3321163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3322163953Srrs				error = EINVAL;
3323163953Srrs				break;
3324163953Srrs			}
3325167598Srrs			/* Is the VRF one we have */
3326163953Srrs			addr_touse = addrs->addr;
3327167695Srrs#if defined(INET6)
3328163953Srrs			if (addrs->addr->sa_family == AF_INET6) {
3329163953Srrs				struct sockaddr_in6 *sin6;
3330163953Srrs
3331163953Srrs				sin6 = (struct sockaddr_in6 *)addr_touse;
3332163953Srrs				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3333163953Srrs					in6_sin6_2_sin(&sin, sin6);
3334163953Srrs					addr_touse = (struct sockaddr *)&sin;
3335163953Srrs				}
3336163953Srrs			}
3337167695Srrs#endif
3338163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3339163953Srrs				if (p == NULL) {
3340163953Srrs					/* Can't get proc for Net/Open BSD */
3341163953Srrs					error = EINVAL;
3342163953Srrs					break;
3343163953Srrs				}
3344163953Srrs				error = sctp_inpcb_bind(so, addr_touse, p);
3345163953Srrs				break;
3346163953Srrs			}
3347163953Srrs			/*
3348163953Srrs			 * No locks required here since bind and mgmt_ep_sa
3349163953Srrs			 * all do their own locking. If we do something for
3350163953Srrs			 * the FIX: below we may need to lock in that case.
3351163953Srrs			 */
3352163953Srrs			if (addrs->sget_assoc_id == 0) {
3353163953Srrs				/* add the address */
3354163953Srrs				struct sctp_inpcb *lep;
3355163953Srrs
3356163953Srrs				((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3357167598Srrs				lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
3358163953Srrs				if (lep != NULL) {
3359163953Srrs					/*
3360163953Srrs					 * We must decrement the refcount
3361163953Srrs					 * since we have the ep already and
3362163953Srrs					 * are binding. No remove going on
3363163953Srrs					 * here.
3364163953Srrs					 */
3365163953Srrs					SCTP_INP_DECR_REF(inp);
3366163953Srrs				}
3367163953Srrs				if (lep == inp) {
3368163953Srrs					/* already bound to it.. ok */
3369163953Srrs					break;
3370163953Srrs				} else if (lep == NULL) {
3371163953Srrs					((struct sockaddr_in *)addr_touse)->sin_port = 0;
3372163953Srrs					error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3373167598Srrs					    SCTP_ADD_IP_ADDRESS, vrf_id);
3374163953Srrs				} else {
3375163953Srrs					error = EADDRNOTAVAIL;
3376163953Srrs				}
3377163953Srrs				if (error)
3378163953Srrs					break;
3379163953Srrs
3380163953Srrs			} else {
3381163953Srrs				/*
3382163953Srrs				 * FIX: decide whether we allow assoc based
3383163953Srrs				 * bindx
3384163953Srrs				 */
3385163953Srrs			}
3386163953Srrs		}
3387163953Srrs		break;
3388163953Srrs	case SCTP_BINDX_REM_ADDR:
3389163953Srrs		{
3390163953Srrs			struct sctp_getaddresses *addrs;
3391163953Srrs			struct sockaddr *addr_touse;
3392163953Srrs			struct sockaddr_in sin;
3393163953Srrs
3394166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3395163953Srrs			/* see if we're bound all already! */
3396163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3397163953Srrs				error = EINVAL;
3398163953Srrs				break;
3399163953Srrs			}
3400163953Srrs			addr_touse = addrs->addr;
3401167695Srrs#if defined(INET6)
3402163953Srrs			if (addrs->addr->sa_family == AF_INET6) {
3403163953Srrs				struct sockaddr_in6 *sin6;
3404163953Srrs
3405163953Srrs				sin6 = (struct sockaddr_in6 *)addr_touse;
3406163953Srrs				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3407163953Srrs					in6_sin6_2_sin(&sin, sin6);
3408163953Srrs					addr_touse = (struct sockaddr *)&sin;
3409163953Srrs				}
3410163953Srrs			}
3411167695Srrs#endif
3412163953Srrs			/*
3413163953Srrs			 * No lock required mgmt_ep_sa does its own locking.
3414163953Srrs			 * If the FIX: below is ever changed we may need to
3415163953Srrs			 * lock before calling association level binding.
3416163953Srrs			 */
3417163953Srrs			if (addrs->sget_assoc_id == 0) {
3418163953Srrs				/* delete the address */
3419163953Srrs				sctp_addr_mgmt_ep_sa(inp, addr_touse,
3420167598Srrs				    SCTP_DEL_IP_ADDRESS, vrf_id);
3421163953Srrs			} else {
3422163953Srrs				/*
3423163953Srrs				 * FIX: decide whether we allow assoc based
3424163953Srrs				 * bindx
3425163953Srrs				 */
3426163953Srrs			}
3427163953Srrs		}
3428163953Srrs		break;
3429163953Srrs	default:
3430163953Srrs		error = ENOPROTOOPT;
3431163953Srrs		break;
3432163953Srrs	}			/* end switch (opt) */
3433163953Srrs	return (error);
3434163953Srrs}
3435163953Srrs
3436163953Srrs
3437163953Srrsint
3438163953Srrssctp_ctloutput(struct socket *so, struct sockopt *sopt)
3439163953Srrs{
3440166675Srrs	void *optval = NULL;
3441166675Srrs	size_t optsize = 0;
3442163953Srrs	struct sctp_inpcb *inp;
3443166675Srrs	void *p;
3444166675Srrs	int error = 0;
3445163953Srrs
3446163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3447163953Srrs	if (inp == 0) {
3448163953Srrs		/* I made the same as TCP since we are not setup? */
3449163953Srrs		return (ECONNRESET);
3450163953Srrs	}
3451163953Srrs	if (sopt->sopt_level != IPPROTO_SCTP) {
3452163953Srrs		/* wrong proto level... send back up to IP */
3453163953Srrs#ifdef INET6
3454163953Srrs		if (INP_CHECK_SOCKAF(so, AF_INET6))
3455163953Srrs			error = ip6_ctloutput(so, sopt);
3456163953Srrs		else
3457163953Srrs#endif				/* INET6 */
3458163953Srrs			error = ip_ctloutput(so, sopt);
3459163953Srrs		return (error);
3460163953Srrs	}
3461166675Srrs	optsize = sopt->sopt_valsize;
3462166675Srrs	if (optsize) {
3463166675Srrs		SCTP_MALLOC(optval, void *, optsize, "SCTPSockOpt");
3464166675Srrs		if (optval == NULL) {
3465163953Srrs			return (ENOBUFS);
3466163953Srrs		}
3467166675Srrs		error = sooptcopyin(sopt, optval, optsize, optsize);
3468163953Srrs		if (error) {
3469166675Srrs			SCTP_FREE(optval);
3470163953Srrs			goto out;
3471163953Srrs		}
3472163953Srrs	}
3473166675Srrs	p = (void *)sopt->sopt_td;
3474163953Srrs	if (sopt->sopt_dir == SOPT_SET) {
3475166675Srrs		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3476163953Srrs	} else if (sopt->sopt_dir == SOPT_GET) {
3477166675Srrs		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3478163953Srrs	} else {
3479163953Srrs		error = EINVAL;
3480163953Srrs	}
3481166675Srrs	if ((error == 0) && (optval != NULL)) {
3482166675Srrs		error = sooptcopyout(sopt, optval, optsize);
3483166675Srrs		SCTP_FREE(optval);
3484166675Srrs	} else if (optval != NULL) {
3485166675Srrs		SCTP_FREE(optval);
3486163953Srrs	}
3487163953Srrsout:
3488163953Srrs	return (error);
3489163953Srrs}
3490163953Srrs
3491163953Srrs
3492163953Srrsstatic int
3493163953Srrssctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3494163953Srrs{
3495163953Srrs	int error = 0;
3496163953Srrs	int create_lock_on = 0;
3497167598Srrs	uint32_t vrf_id;
3498163953Srrs	struct sctp_inpcb *inp;
3499163953Srrs	struct sctp_tcb *stcb = NULL;
3500163953Srrs
3501163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3502163953Srrs	if (inp == 0) {
3503163953Srrs		/* I made the same as TCP since we are not setup? */
3504163953Srrs		return (ECONNRESET);
3505163953Srrs	}
3506163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
3507163953Srrs	create_lock_on = 1;
3508163953Srrs
3509163953Srrs	SCTP_INP_INCR_REF(inp);
3510163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3511163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3512163953Srrs		/* Should I really unlock ? */
3513163953Srrs		error = EFAULT;
3514163953Srrs		goto out_now;
3515163953Srrs	}
3516163953Srrs#ifdef INET6
3517163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3518163953Srrs	    (addr->sa_family == AF_INET6)) {
3519163953Srrs		error = EINVAL;
3520163953Srrs		goto out_now;
3521163953Srrs	}
3522163953Srrs#endif				/* INET6 */
3523163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3524163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
3525163953Srrs		/* Bind a ephemeral port */
3526163953Srrs		error = sctp_inpcb_bind(so, NULL, p);
3527163953Srrs		if (error) {
3528163953Srrs			goto out_now;
3529163953Srrs		}
3530163953Srrs	}
3531163953Srrs	/* Now do we connect? */
3532163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3533163953Srrs		error = EINVAL;
3534163953Srrs		goto out_now;
3535163953Srrs	}
3536163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3537163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3538163953Srrs		/* We are already connected AND the TCP model */
3539163953Srrs		error = EADDRINUSE;
3540163953Srrs		goto out_now;
3541163953Srrs	}
3542163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3543163953Srrs		SCTP_INP_RLOCK(inp);
3544163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3545163953Srrs		SCTP_INP_RUNLOCK(inp);
3546163953Srrs	} else {
3547163953Srrs		/*
3548166675Srrs		 * We increment here since sctp_findassociation_ep_addr()
3549166675Srrs		 * wil do a decrement if it finds the stcb as long as the
3550166675Srrs		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3551163953Srrs		 */
3552163953Srrs		SCTP_INP_INCR_REF(inp);
3553163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3554163953Srrs		if (stcb == NULL) {
3555163953Srrs			SCTP_INP_DECR_REF(inp);
3556168299Srrs		} else {
3557168299Srrs			SCTP_TCB_LOCK(stcb);
3558163953Srrs		}
3559163953Srrs	}
3560163953Srrs	if (stcb != NULL) {
3561163953Srrs		/* Already have or am bring up an association */
3562163953Srrs		error = EALREADY;
3563163953Srrs		goto out_now;
3564163953Srrs	}
3565168299Srrs	vrf_id = inp->def_vrf_id;
3566163953Srrs	/* We are GOOD to go */
3567167598Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3568163953Srrs	if (stcb == NULL) {
3569163953Srrs		/* Gak! no memory */
3570167598Srrs		goto out_now;
3571163953Srrs	}
3572163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3573163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3574163953Srrs		/* Set the connected flag so we can queue data */
3575163953Srrs		soisconnecting(so);
3576163953Srrs	}
3577163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3578163953Srrs	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3579163953Srrs
3580163953Srrs	/* initialize authentication parameters for the assoc */
3581163953Srrs	sctp_initialize_auth_params(inp, stcb);
3582163953Srrs
3583163953Srrs	sctp_send_initiate(inp, stcb);
3584168299Srrs	SCTP_TCB_UNLOCK(stcb);
3585163953Srrsout_now:
3586163953Srrs	if (create_lock_on)
3587163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
3588163953Srrs
3589163953Srrs	SCTP_INP_DECR_REF(inp);
3590163953Srrs	return error;
3591163953Srrs}
3592163953Srrs
3593163953Srrsint
3594163953Srrssctp_listen(struct socket *so, int backlog, struct thread *p)
3595163953Srrs{
3596163953Srrs	/*
3597163953Srrs	 * Note this module depends on the protocol processing being called
3598163953Srrs	 * AFTER any socket level flags and backlog are applied to the
3599163953Srrs	 * socket. The traditional way that the socket flags are applied is
3600163953Srrs	 * AFTER protocol processing. We have made a change to the
3601163953Srrs	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3602163953Srrs	 * place if the socket API for SCTP is to work properly.
3603163953Srrs	 */
3604163953Srrs
3605163953Srrs	int error = 0;
3606163953Srrs	struct sctp_inpcb *inp;
3607163953Srrs
3608163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3609163953Srrs	if (inp == 0) {
3610163953Srrs		/* I made the same as TCP since we are not setup? */
3611163953Srrs		return (ECONNRESET);
3612163953Srrs	}
3613163953Srrs	SCTP_INP_RLOCK(inp);
3614163953Srrs#ifdef SCTP_LOCK_LOGGING
3615163953Srrs	sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3616163953Srrs#endif
3617163953Srrs	SOCK_LOCK(so);
3618163953Srrs	error = solisten_proto_check(so);
3619163953Srrs	if (error) {
3620163953Srrs		SOCK_UNLOCK(so);
3621169208Srrs		SCTP_INP_RUNLOCK(inp);
3622163953Srrs		return (error);
3623163953Srrs	}
3624163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3625163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3626163953Srrs		/* We are already connected AND the TCP model */
3627163953Srrs		SCTP_INP_RUNLOCK(inp);
3628163953Srrs		SOCK_UNLOCK(so);
3629163953Srrs		return (EADDRINUSE);
3630163953Srrs	}
3631163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3632163953Srrs		/* We must do a bind. */
3633166675Srrs		SOCK_UNLOCK(so);
3634163953Srrs		SCTP_INP_RUNLOCK(inp);
3635163953Srrs		if ((error = sctp_inpcb_bind(so, NULL, p))) {
3636163953Srrs			/* bind error, probably perm */
3637163953Srrs			return (error);
3638163953Srrs		}
3639166675Srrs		SOCK_LOCK(so);
3640163953Srrs	} else {
3641163953Srrs		SCTP_INP_RUNLOCK(inp);
3642163953Srrs	}
3643163953Srrs	/* It appears for 7.0 and on, we must always call this. */
3644163953Srrs	solisten_proto(so, backlog);
3645163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3646163953Srrs		/* remove the ACCEPTCONN flag for one-to-many sockets */
3647163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3648163953Srrs	}
3649163953Srrs	if (backlog == 0) {
3650163953Srrs		/* turning off listen */
3651163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3652163953Srrs	}
3653163953Srrs	SOCK_UNLOCK(so);
3654163953Srrs	return (error);
3655163953Srrs}
3656163953Srrs
3657163953Srrsstatic int sctp_defered_wakeup_cnt = 0;
3658163953Srrs
3659163953Srrsint
3660163953Srrssctp_accept(struct socket *so, struct sockaddr **addr)
3661163953Srrs{
3662163953Srrs	struct sctp_tcb *stcb;
3663163953Srrs	struct sctp_inpcb *inp;
3664163953Srrs	union sctp_sockstore store;
3665163953Srrs
3666163953Srrs	int error;
3667163953Srrs
3668163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3669163953Srrs
3670163953Srrs	if (inp == 0) {
3671163953Srrs		return (ECONNRESET);
3672163953Srrs	}
3673163953Srrs	SCTP_INP_RLOCK(inp);
3674163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3675168299Srrs		SCTP_INP_RUNLOCK(inp);
3676163953Srrs		return (ENOTSUP);
3677163953Srrs	}
3678163953Srrs	if (so->so_state & SS_ISDISCONNECTED) {
3679163953Srrs		SCTP_INP_RUNLOCK(inp);
3680163953Srrs		return (ECONNABORTED);
3681163953Srrs	}
3682163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3683163953Srrs	if (stcb == NULL) {
3684163953Srrs		SCTP_INP_RUNLOCK(inp);
3685163953Srrs		return (ECONNRESET);
3686163953Srrs	}
3687163953Srrs	SCTP_TCB_LOCK(stcb);
3688163953Srrs	SCTP_INP_RUNLOCK(inp);
3689163953Srrs	store = stcb->asoc.primary_destination->ro._l_addr;
3690163953Srrs	SCTP_TCB_UNLOCK(stcb);
3691163953Srrs	if (store.sa.sa_family == AF_INET) {
3692163953Srrs		struct sockaddr_in *sin;
3693163953Srrs
3694163953Srrs		SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3695163953Srrs		sin->sin_family = AF_INET;
3696163953Srrs		sin->sin_len = sizeof(*sin);
3697163953Srrs		sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3698163953Srrs		sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3699163953Srrs		*addr = (struct sockaddr *)sin;
3700163953Srrs	} else {
3701163953Srrs		struct sockaddr_in6 *sin6;
3702163953Srrs
3703163953Srrs		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3704163953Srrs		sin6->sin6_family = AF_INET6;
3705163953Srrs		sin6->sin6_len = sizeof(*sin6);
3706163953Srrs		sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3707163953Srrs
3708163953Srrs		sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3709164085Srrs		if ((error = sa6_recoverscope(sin6)) != 0) {
3710164085Srrs			SCTP_FREE_SONAME(sin6);
3711163953Srrs			return (error);
3712164085Srrs		}
3713163953Srrs		*addr = (struct sockaddr *)sin6;
3714163953Srrs	}
3715163953Srrs	/* Wake any delayed sleep action */
3716163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3717166086Srrs		SCTP_INP_WLOCK(inp);
3718163953Srrs		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3719163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3720163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3721166086Srrs			SCTP_INP_WUNLOCK(inp);
3722163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3723163953Srrs			if (sowriteable(inp->sctp_socket)) {
3724163953Srrs				sowwakeup_locked(inp->sctp_socket);
3725163953Srrs			} else {
3726163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3727163953Srrs			}
3728166086Srrs			SCTP_INP_WLOCK(inp);
3729163953Srrs		}
3730163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3731163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3732166086Srrs			SCTP_INP_WUNLOCK(inp);
3733163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3734163953Srrs			if (soreadable(inp->sctp_socket)) {
3735163953Srrs				sctp_defered_wakeup_cnt++;
3736163953Srrs				sorwakeup_locked(inp->sctp_socket);
3737163953Srrs			} else {
3738163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3739163953Srrs			}
3740166086Srrs			SCTP_INP_WLOCK(inp);
3741163953Srrs		}
3742166086Srrs		SCTP_INP_WUNLOCK(inp);
3743163953Srrs	}
3744163953Srrs	return (0);
3745163953Srrs}
3746163953Srrs
3747163953Srrsint
3748163953Srrssctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3749163953Srrs{
3750163953Srrs	struct sockaddr_in *sin;
3751167598Srrs	uint32_t vrf_id;
3752163953Srrs	struct sctp_inpcb *inp;
3753167695Srrs	struct sctp_ifa *sctp_ifa;
3754163953Srrs
3755163953Srrs	/*
3756163953Srrs	 * Do the malloc first in case it blocks.
3757163953Srrs	 */
3758163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3759163953Srrs	sin->sin_family = AF_INET;
3760163953Srrs	sin->sin_len = sizeof(*sin);
3761163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3762163953Srrs	if (!inp) {
3763163953Srrs		SCTP_FREE_SONAME(sin);
3764163953Srrs		return ECONNRESET;
3765163953Srrs	}
3766163953Srrs	SCTP_INP_RLOCK(inp);
3767163953Srrs	sin->sin_port = inp->sctp_lport;
3768163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3769163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3770163953Srrs			struct sctp_tcb *stcb;
3771163953Srrs			struct sockaddr_in *sin_a;
3772163953Srrs			struct sctp_nets *net;
3773163953Srrs			int fnd;
3774163953Srrs
3775163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3776163953Srrs			if (stcb == NULL) {
3777163953Srrs				goto notConn;
3778163953Srrs			}
3779163953Srrs			fnd = 0;
3780163953Srrs			sin_a = NULL;
3781163953Srrs			SCTP_TCB_LOCK(stcb);
3782163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3783163953Srrs				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3784164085Srrs				if (sin_a == NULL)
3785164085Srrs					/* this will make coverity happy */
3786164085Srrs					continue;
3787164085Srrs
3788163953Srrs				if (sin_a->sin_family == AF_INET) {
3789163953Srrs					fnd = 1;
3790163953Srrs					break;
3791163953Srrs				}
3792163953Srrs			}
3793163953Srrs			if ((!fnd) || (sin_a == NULL)) {
3794163953Srrs				/* punt */
3795163953Srrs				SCTP_TCB_UNLOCK(stcb);
3796163953Srrs				goto notConn;
3797163953Srrs			}
3798168299Srrs			vrf_id = inp->def_vrf_id;
3799167598Srrs			sctp_ifa = sctp_source_address_selection(inp,
3800167598Srrs			    stcb,
3801168299Srrs			    (sctp_route_t *) & net->ro,
3802167598Srrs			    net, 0, vrf_id);
3803167598Srrs			if (sctp_ifa) {
3804167598Srrs				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
3805167598Srrs				sctp_free_ifa(sctp_ifa);
3806167598Srrs			}
3807163953Srrs			SCTP_TCB_UNLOCK(stcb);
3808163953Srrs		} else {
3809163953Srrs			/* For the bound all case you get back 0 */
3810163953Srrs	notConn:
3811163953Srrs			sin->sin_addr.s_addr = 0;
3812163953Srrs		}
3813163953Srrs
3814163953Srrs	} else {
3815163953Srrs		/* Take the first IPv4 address in the list */
3816163953Srrs		struct sctp_laddr *laddr;
3817163953Srrs		int fnd = 0;
3818163953Srrs
3819163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3820167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
3821163953Srrs				struct sockaddr_in *sin_a;
3822163953Srrs
3823167598Srrs				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
3824163953Srrs				sin->sin_addr = sin_a->sin_addr;
3825163953Srrs				fnd = 1;
3826163953Srrs				break;
3827163953Srrs			}
3828163953Srrs		}
3829163953Srrs		if (!fnd) {
3830163953Srrs			SCTP_FREE_SONAME(sin);
3831163953Srrs			SCTP_INP_RUNLOCK(inp);
3832163953Srrs			return ENOENT;
3833163953Srrs		}
3834163953Srrs	}
3835163953Srrs	SCTP_INP_RUNLOCK(inp);
3836163953Srrs	(*addr) = (struct sockaddr *)sin;
3837163953Srrs	return (0);
3838163953Srrs}
3839163953Srrs
3840163953Srrsint
3841163953Srrssctp_peeraddr(struct socket *so, struct sockaddr **addr)
3842163953Srrs{
3843163953Srrs	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
3844166086Srrs	int fnd;
3845163953Srrs	struct sockaddr_in *sin_a;
3846163953Srrs	struct sctp_inpcb *inp;
3847163953Srrs	struct sctp_tcb *stcb;
3848163953Srrs	struct sctp_nets *net;
3849163953Srrs
3850163953Srrs	/* Do the malloc first in case it blocks. */
3851163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3852163953Srrs	if ((inp == NULL) ||
3853163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3854163953Srrs		/* UDP type and listeners will drop out here */
3855163953Srrs		return (ENOTCONN);
3856163953Srrs	}
3857163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3858163953Srrs	sin->sin_family = AF_INET;
3859163953Srrs	sin->sin_len = sizeof(*sin);
3860163953Srrs
3861163953Srrs	/* We must recapture incase we blocked */
3862163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3863163953Srrs	if (!inp) {
3864163953Srrs		SCTP_FREE_SONAME(sin);
3865163953Srrs		return ECONNRESET;
3866163953Srrs	}
3867163953Srrs	SCTP_INP_RLOCK(inp);
3868163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3869163953Srrs	if (stcb)
3870163953Srrs		SCTP_TCB_LOCK(stcb);
3871163953Srrs	SCTP_INP_RUNLOCK(inp);
3872163953Srrs	if (stcb == NULL) {
3873163953Srrs		SCTP_FREE_SONAME(sin);
3874163953Srrs		return ECONNRESET;
3875163953Srrs	}
3876163953Srrs	fnd = 0;
3877163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3878163953Srrs		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3879163953Srrs		if (sin_a->sin_family == AF_INET) {
3880163953Srrs			fnd = 1;
3881163953Srrs			sin->sin_port = stcb->rport;
3882163953Srrs			sin->sin_addr = sin_a->sin_addr;
3883163953Srrs			break;
3884163953Srrs		}
3885163953Srrs	}
3886163953Srrs	SCTP_TCB_UNLOCK(stcb);
3887163953Srrs	if (!fnd) {
3888163953Srrs		/* No IPv4 address */
3889163953Srrs		SCTP_FREE_SONAME(sin);
3890163953Srrs		return ENOENT;
3891163953Srrs	}
3892163953Srrs	(*addr) = (struct sockaddr *)sin;
3893163953Srrs	return (0);
3894163953Srrs}
3895163953Srrs
3896163953Srrsstruct pr_usrreqs sctp_usrreqs = {
3897163953Srrs	.pru_abort = sctp_abort,
3898163953Srrs	.pru_accept = sctp_accept,
3899163953Srrs	.pru_attach = sctp_attach,
3900163953Srrs	.pru_bind = sctp_bind,
3901163953Srrs	.pru_connect = sctp_connect,
3902163953Srrs	.pru_control = in_control,
3903163953Srrs	.pru_close = sctp_close,
3904163953Srrs	.pru_detach = sctp_close,
3905163953Srrs	.pru_sopoll = sopoll_generic,
3906163953Srrs	.pru_disconnect = sctp_disconnect,
3907163953Srrs	.pru_listen = sctp_listen,
3908163953Srrs	.pru_peeraddr = sctp_peeraddr,
3909163953Srrs	.pru_send = sctp_sendm,
3910163953Srrs	.pru_shutdown = sctp_shutdown,
3911163953Srrs	.pru_sockaddr = sctp_ingetaddr,
3912163953Srrs	.pru_sosend = sctp_sosend,
3913163953Srrs	.pru_soreceive = sctp_soreceive
3914163953Srrs};
3915