sctp_usrreq.c revision 171858
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 *   this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *   the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $	 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 171858 2007-08-16 01:51:22Z rrs $");
35#include <netinet/sctp_os.h>
36#include <sys/proc.h>
37#include <netinet/sctp_pcb.h>
38#include <netinet/sctp_header.h>
39#include <netinet/sctp_var.h>
40#if defined(INET6)
41#include <netinet6/sctp6_var.h>
42#endif
43#include <netinet/sctp_sysctl.h>
44#include <netinet/sctp_output.h>
45#include <netinet/sctp_uio.h>
46#include <netinet/sctp_asconf.h>
47#include <netinet/sctputil.h>
48#include <netinet/sctp_indata.h>
49#include <netinet/sctp_timer.h>
50#include <netinet/sctp_auth.h>
51#include <netinet/sctp_bsd_addr.h>
52#include <netinet/sctp_cc_functions.h>
53
54
55
56
57void
58sctp_init(void)
59{
60	/* Init the SCTP pcb in sctp_pcb.c */
61	u_long sb_max_adj;
62
63	sctp_pcb_init();
64
65
66	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67		sctp_max_chunks_on_queue = (nmbclusters / 8);
68	/*
69	 * Allow a user to take no more than 1/2 the number of clusters or
70	 * the SB_MAX whichever is smaller for the send window.
71	 */
72	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
74	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
75	/*
76	 * Now for the recv window, should we take the same amount? or
77	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78	 * now I will just copy.
79	 */
80	sctp_recvspace = sctp_sendspace;
81
82}
83
84
85
86/*
87 * cleanup of the sctppcbinfo structure.
88 * Assumes that the sctppcbinfo lock is held.
89 */
90void
91sctp_pcbinfo_cleanup(void)
92{
93	/* free the hash tables */
94	if (sctppcbinfo.sctp_asochash != NULL)
95		SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
96	if (sctppcbinfo.sctp_ephash != NULL)
97		SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
98	if (sctppcbinfo.sctp_tcpephash != NULL)
99		SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
100	if (sctppcbinfo.sctp_restarthash != NULL)
101		SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
102}
103
104
105static void
106sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
107    struct sctp_tcb *stcb,
108    struct sctp_nets *net,
109    uint16_t nxtsz)
110{
111	struct sctp_tmit_chunk *chk;
112
113	/* Adjust that too */
114	stcb->asoc.smallest_mtu = nxtsz;
115	/* now off to subtract IP_DF flag if needed */
116#ifdef SCTP_PRINT_FOR_B_AND_M
117	SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
118	    inp, stcb, net, nxtsz);
119#endif
120	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
121		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
123		}
124	}
125	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
126		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
127			/*
128			 * For this guy we also mark for immediate resend
129			 * since we sent to big of chunk
130			 */
131			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
132			if (chk->sent != SCTP_DATAGRAM_RESEND) {
133				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
134			}
135			chk->sent = SCTP_DATAGRAM_RESEND;
136			chk->rec.data.doing_fast_retransmit = 0;
137			if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
138				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
139				    chk->whoTo->flight_size,
140				    chk->book_size,
141				    (uintptr_t) chk->whoTo,
142				    chk->rec.data.TSN_seq);
143			}
144			/* Clear any time so NO RTT is being done */
145			chk->do_rtt = 0;
146			sctp_flight_size_decrease(chk);
147			sctp_total_flight_decrease(stcb, chk);
148		}
149	}
150}
151
152static void
153sctp_notify_mbuf(struct sctp_inpcb *inp,
154    struct sctp_tcb *stcb,
155    struct sctp_nets *net,
156    struct ip *ip,
157    struct sctphdr *sh)
158{
159	struct icmp *icmph;
160	int totsz, tmr_stopped = 0;
161	uint16_t nxtsz;
162
163	/* protection */
164	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165	    (ip == NULL) || (sh == NULL)) {
166		if (stcb != NULL) {
167			SCTP_TCB_UNLOCK(stcb);
168		}
169		return;
170	}
171	/* First job is to verify the vtag matches what I would send */
172	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173		SCTP_TCB_UNLOCK(stcb);
174		return;
175	}
176	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
177	    sizeof(struct ip)));
178	if (icmph->icmp_type != ICMP_UNREACH) {
179		/* We only care about unreachable */
180		SCTP_TCB_UNLOCK(stcb);
181		return;
182	}
183	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184		/* not a unreachable message due to frag. */
185		SCTP_TCB_UNLOCK(stcb);
186		return;
187	}
188	totsz = ip->ip_len;
189
190	nxtsz = ntohs(icmph->icmp_seq);
191	if (nxtsz == 0) {
192		/*
193		 * old type router that does not tell us what the next size
194		 * mtu is. Rats we will have to guess (in a educated fashion
195		 * of course)
196		 */
197		nxtsz = find_next_best_mtu(totsz);
198	}
199	/* Stop any PMTU timer */
200	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
201		tmr_stopped = 1;
202		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
203		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
204	}
205	/* Adjust destination size limit */
206	if (net->mtu > nxtsz) {
207		net->mtu = nxtsz;
208	}
209	/* now what about the ep? */
210	if (stcb->asoc.smallest_mtu > nxtsz) {
211#ifdef SCTP_PRINT_FOR_B_AND_M
212		SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
213		    nxtsz);
214#endif
215		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
216	}
217	if (tmr_stopped)
218		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
219
220	SCTP_TCB_UNLOCK(stcb);
221}
222
223
224void
225sctp_notify(struct sctp_inpcb *inp,
226    int error,
227    struct sctphdr *sh,
228    struct sockaddr *to,
229    struct sctp_tcb *stcb,
230    struct sctp_nets *net)
231{
232	/* protection */
233	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
234	    (sh == NULL) || (to == NULL)) {
235		return;
236	}
237	/* First job is to verify the vtag matches what I would send */
238	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
239		return;
240	}
241	/* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
242
243	if ((error == EHOSTUNREACH) ||	/* Host is not reachable */
244	    (error == EHOSTDOWN) ||	/* Host is down */
245	    (error == ECONNREFUSED) ||	/* Host refused the connection, (not
246					 * an abort?) */
247	    (error == ENOPROTOOPT)	/* SCTP is not present on host */
248	    ) {
249		/*
250		 * Hmm reachablity problems we must examine closely. If its
251		 * not reachable, we may have lost a network. Or if there is
252		 * NO protocol at the other end named SCTP. well we consider
253		 * it a OOTB abort.
254		 */
255		if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
256			if (net->dest_state & SCTP_ADDR_REACHABLE) {
257				/* Ok that destination is NOT reachable */
258				SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
259				    net->error_count,
260				    net->failure_threshold,
261				    net);
262
263				net->dest_state &= ~SCTP_ADDR_REACHABLE;
264				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
265				/*
266				 * JRS 5/14/07 - If a destination is
267				 * unreachable, the PF bit is turned off.
268				 * This allows an unambiguous use of the PF
269				 * bit for destinations that are reachable
270				 * but potentially failed. If the
271				 * destination is set to the unreachable
272				 * state, also set the destination to the PF
273				 * state.
274				 */
275				/*
276				 * Add debug message here if destination is
277				 * not in PF state.
278				 */
279				/* Stop any running T3 timers here? */
280				if (sctp_cmt_on_off && sctp_cmt_pf) {
281					net->dest_state &= ~SCTP_ADDR_PF;
282					SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
283					    net);
284				}
285				net->error_count = net->failure_threshold + 1;
286				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
287				    stcb, SCTP_FAILED_THRESHOLD,
288				    (void *)net);
289			}
290			if (stcb) {
291				SCTP_TCB_UNLOCK(stcb);
292			}
293		} else {
294			/*
295			 * Here the peer is either playing tricks on us,
296			 * including an address that belongs to someone who
297			 * does not support SCTP OR was a userland
298			 * implementation that shutdown and now is dead. In
299			 * either case treat it like a OOTB abort with no
300			 * TCB
301			 */
302			sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
303			sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
304			/* no need to unlock here, since the TCB is gone */
305		}
306	} else {
307		/* Send all others to the app */
308		if (stcb) {
309			SCTP_TCB_UNLOCK(stcb);
310		}
311		if (inp->sctp_socket) {
312#ifdef SCTP_LOCK_LOGGING
313			if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
314				sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
315			}
316#endif
317			SOCK_LOCK(inp->sctp_socket);
318			inp->sctp_socket->so_error = error;
319			sctp_sowwakeup(inp, inp->sctp_socket);
320			SOCK_UNLOCK(inp->sctp_socket);
321		}
322	}
323}
324
325void
326sctp_ctlinput(cmd, sa, vip)
327	int cmd;
328	struct sockaddr *sa;
329	void *vip;
330{
331	struct ip *ip = vip;
332	struct sctphdr *sh;
333	uint32_t vrf_id;
334
335	/* FIX, for non-bsd is this right? */
336	vrf_id = SCTP_DEFAULT_VRFID;
337	if (sa->sa_family != AF_INET ||
338	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
339		return;
340	}
341	if (PRC_IS_REDIRECT(cmd)) {
342		ip = 0;
343	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
344		return;
345	}
346	if (ip) {
347		struct sctp_inpcb *inp = NULL;
348		struct sctp_tcb *stcb = NULL;
349		struct sctp_nets *net = NULL;
350		struct sockaddr_in to, from;
351
352		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
353		bzero(&to, sizeof(to));
354		bzero(&from, sizeof(from));
355		from.sin_family = to.sin_family = AF_INET;
356		from.sin_len = to.sin_len = sizeof(to);
357		from.sin_port = sh->src_port;
358		from.sin_addr = ip->ip_src;
359		to.sin_port = sh->dest_port;
360		to.sin_addr = ip->ip_dst;
361
362		/*
363		 * 'to' holds the dest of the packet that failed to be sent.
364		 * 'from' holds our local endpoint address. Thus we reverse
365		 * the to and the from in the lookup.
366		 */
367		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
368		    (struct sockaddr *)&to,
369		    &inp, &net, 1, vrf_id);
370		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
371			if (cmd != PRC_MSGSIZE) {
372				int cm;
373
374				if (cmd == PRC_HOSTDEAD) {
375					cm = EHOSTUNREACH;
376				} else {
377					cm = inetctlerrmap[cmd];
378				}
379				sctp_notify(inp, cm, sh,
380				    (struct sockaddr *)&to, stcb,
381				    net);
382			} else {
383				/* handle possible ICMP size messages */
384				sctp_notify_mbuf(inp, stcb, net, ip, sh);
385			}
386		} else {
387			if ((stcb == NULL) && (inp != NULL)) {
388				/* reduce ref-count */
389				SCTP_INP_WLOCK(inp);
390				SCTP_INP_DECR_REF(inp);
391				SCTP_INP_WUNLOCK(inp);
392			}
393		}
394	}
395	return;
396}
397
398static int
399sctp_getcred(SYSCTL_HANDLER_ARGS)
400{
401	struct xucred xuc;
402	struct sockaddr_in addrs[2];
403	struct sctp_inpcb *inp;
404	struct sctp_nets *net;
405	struct sctp_tcb *stcb;
406	int error;
407	uint32_t vrf_id;
408
409	/* FIX, for non-bsd is this right? */
410	vrf_id = SCTP_DEFAULT_VRFID;
411
412	error = priv_check(req->td, PRIV_NETINET_GETCRED);
413
414	if (error)
415		return (error);
416
417	error = SYSCTL_IN(req, addrs, sizeof(addrs));
418	if (error)
419		return (error);
420
421	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
422	    sintosa(&addrs[1]),
423	    &inp, &net, 1, vrf_id);
424	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
425		if ((inp != NULL) && (stcb == NULL)) {
426			/* reduce ref-count */
427			SCTP_INP_WLOCK(inp);
428			SCTP_INP_DECR_REF(inp);
429			goto cred_can_cont;
430		}
431		error = ENOENT;
432		goto out;
433	}
434	SCTP_TCB_UNLOCK(stcb);
435	/*
436	 * We use the write lock here, only since in the error leg we need
437	 * it. If we used RLOCK, then we would have to
438	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
439	 * Better to use higher wlock.
440	 */
441	SCTP_INP_WLOCK(inp);
442cred_can_cont:
443	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
444	if (error) {
445		SCTP_INP_WUNLOCK(inp);
446		goto out;
447	}
448	cru2x(inp->sctp_socket->so_cred, &xuc);
449	SCTP_INP_WUNLOCK(inp);
450	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
451out:
452	return (error);
453}
454
455SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
456    0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
457
458
459static void
460sctp_abort(struct socket *so)
461{
462	struct sctp_inpcb *inp;
463	uint32_t flags;
464
465	inp = (struct sctp_inpcb *)so->so_pcb;
466	if (inp == 0)
467		return;
468
469sctp_must_try_again:
470	flags = inp->sctp_flags;
471#ifdef SCTP_LOG_CLOSING
472	sctp_log_closing(inp, NULL, 17);
473#endif
474	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
475	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
476#ifdef SCTP_LOG_CLOSING
477		sctp_log_closing(inp, NULL, 16);
478#endif
479		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
480		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
481		SOCK_LOCK(so);
482		SCTP_SB_CLEAR(so->so_snd);
483		/*
484		 * same for the rcv ones, they are only here for the
485		 * accounting/select.
486		 */
487		SCTP_SB_CLEAR(so->so_rcv);
488
489		/* Now null out the reference, we are completely detached. */
490		so->so_pcb = NULL;
491		SOCK_UNLOCK(so);
492	} else {
493		flags = inp->sctp_flags;
494		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
495			goto sctp_must_try_again;
496		}
497	}
498	return;
499}
500
501static int
502sctp_attach(struct socket *so, int proto, struct thread *p)
503{
504	struct sctp_inpcb *inp;
505	struct inpcb *ip_inp;
506	int error;
507	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
508
509#ifdef IPSEC
510	uint32_t flags;
511
512#endif
513	inp = (struct sctp_inpcb *)so->so_pcb;
514	if (inp != 0) {
515		return EINVAL;
516	}
517	error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
518	if (error) {
519		return error;
520	}
521	error = sctp_inpcb_alloc(so, vrf_id);
522	if (error) {
523		return error;
524	}
525	inp = (struct sctp_inpcb *)so->so_pcb;
526	SCTP_INP_WLOCK(inp);
527
528	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
529	ip_inp = &inp->ip_inp.inp;
530	ip_inp->inp_vflag |= INP_IPV4;
531	ip_inp->inp_ip_ttl = ip_defttl;
532
533#ifdef IPSEC
534	error = ipsec_init_policy(so, &ip_inp->inp_sp);
535#ifdef SCTP_LOG_CLOSING
536	sctp_log_closing(inp, NULL, 17);
537#endif
538	if (error != 0) {
539		flags = inp->sctp_flags;
540		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
541		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
542#ifdef SCTP_LOG_CLOSING
543			sctp_log_closing(inp, NULL, 15);
544#endif
545			SCTP_INP_WUNLOCK(inp);
546			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
547			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
548		} else {
549			SCTP_INP_WUNLOCK(inp);
550		}
551		return error;
552	}
553#endif				/* IPSEC */
554	SCTP_INP_WUNLOCK(inp);
555	return 0;
556}
557
558static int
559sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
560{
561	struct sctp_inpcb *inp;
562	int error;
563
564#ifdef INET6
565	if (addr && addr->sa_family != AF_INET)
566		/* must be a v4 address! */
567		return EINVAL;
568#endif				/* INET6 */
569	if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
570		return EINVAL;
571	}
572	inp = (struct sctp_inpcb *)so->so_pcb;
573	if (inp == 0)
574		return EINVAL;
575
576	error = sctp_inpcb_bind(so, addr, NULL, p);
577	return error;
578}
579
580static void
581sctp_close(struct socket *so)
582{
583	struct sctp_inpcb *inp;
584	uint32_t flags;
585
586	inp = (struct sctp_inpcb *)so->so_pcb;
587	if (inp == 0)
588		return;
589
590	/*
591	 * Inform all the lower layer assoc that we are done.
592	 */
593sctp_must_try_again:
594	flags = inp->sctp_flags;
595#ifdef SCTP_LOG_CLOSING
596	sctp_log_closing(inp, NULL, 17);
597#endif
598	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
599	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
600		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
601		    (so->so_rcv.sb_cc > 0)) {
602#ifdef SCTP_LOG_CLOSING
603			sctp_log_closing(inp, NULL, 13);
604#endif
605			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
606			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
607		} else {
608#ifdef SCTP_LOG_CLOSING
609			sctp_log_closing(inp, NULL, 14);
610#endif
611			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
612			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
613		}
614		/*
615		 * The socket is now detached, no matter what the state of
616		 * the SCTP association.
617		 */
618		SOCK_LOCK(so);
619		SCTP_SB_CLEAR(so->so_snd);
620		/*
621		 * same for the rcv ones, they are only here for the
622		 * accounting/select.
623		 */
624		SCTP_SB_CLEAR(so->so_rcv);
625
626		/* Now null out the reference, we are completely detached. */
627		so->so_pcb = NULL;
628		SOCK_UNLOCK(so);
629	} else {
630		flags = inp->sctp_flags;
631		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
632			goto sctp_must_try_again;
633		}
634	}
635	return;
636}
637
638
639int
640sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
641    struct mbuf *control, struct thread *p);
642
643
644int
645sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
646    struct mbuf *control, struct thread *p)
647{
648	struct sctp_inpcb *inp;
649	int error;
650
651	inp = (struct sctp_inpcb *)so->so_pcb;
652	if (inp == 0) {
653		if (control) {
654			sctp_m_freem(control);
655			control = NULL;
656		}
657		sctp_m_freem(m);
658		return EINVAL;
659	}
660	/* Got to have an to address if we are NOT a connected socket */
661	if ((addr == NULL) &&
662	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
663	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
664	    ) {
665		goto connected_type;
666	} else if (addr == NULL) {
667		error = EDESTADDRREQ;
668		sctp_m_freem(m);
669		if (control) {
670			sctp_m_freem(control);
671			control = NULL;
672		}
673		return (error);
674	}
675#ifdef INET6
676	if (addr->sa_family != AF_INET) {
677		/* must be a v4 address! */
678		sctp_m_freem(m);
679		if (control) {
680			sctp_m_freem(control);
681			control = NULL;
682		}
683		error = EDESTADDRREQ;
684		return EINVAL;
685	}
686#endif				/* INET6 */
687connected_type:
688	/* now what about control */
689	if (control) {
690		if (inp->control) {
691			SCTP_PRINTF("huh? control set?\n");
692			sctp_m_freem(inp->control);
693			inp->control = NULL;
694		}
695		inp->control = control;
696	}
697	/* Place the data */
698	if (inp->pkt) {
699		SCTP_BUF_NEXT(inp->pkt_last) = m;
700		inp->pkt_last = m;
701	} else {
702		inp->pkt_last = inp->pkt = m;
703	}
704	if (
705	/* FreeBSD uses a flag passed */
706	    ((flags & PRUS_MORETOCOME) == 0)
707	    ) {
708		/*
709		 * note with the current version this code will only be used
710		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
711		 * re-defining sosend to use the sctp_sosend. One can
712		 * optionally switch back to this code (by changing back the
713		 * definitions) but this is not advisable. This code is used
714		 * by FreeBSD when sending a file with sendfile() though.
715		 */
716		int ret;
717
718		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
719		inp->pkt = NULL;
720		inp->control = NULL;
721		return (ret);
722	} else {
723		return (0);
724	}
725}
726
727static int
728sctp_disconnect(struct socket *so)
729{
730	struct sctp_inpcb *inp;
731
732	inp = (struct sctp_inpcb *)so->so_pcb;
733	if (inp == NULL) {
734		return (ENOTCONN);
735	}
736	SCTP_INP_RLOCK(inp);
737	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
738	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
739		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
740			/* No connection */
741			SCTP_INP_RUNLOCK(inp);
742			return (0);
743		} else {
744			struct sctp_association *asoc;
745			struct sctp_tcb *stcb;
746
747			stcb = LIST_FIRST(&inp->sctp_asoc_list);
748			if (stcb == NULL) {
749				SCTP_INP_RUNLOCK(inp);
750				return (EINVAL);
751			}
752			SCTP_TCB_LOCK(stcb);
753			asoc = &stcb->asoc;
754			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
755				/* We are about to be freed, out of here */
756				SCTP_TCB_UNLOCK(stcb);
757				SCTP_INP_RUNLOCK(inp);
758				return (0);
759			}
760			if (((so->so_options & SO_LINGER) &&
761			    (so->so_linger == 0)) ||
762			    (so->so_rcv.sb_cc > 0)) {
763				if (SCTP_GET_STATE(asoc) !=
764				    SCTP_STATE_COOKIE_WAIT) {
765					/* Left with Data unread */
766					struct mbuf *err;
767
768					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
769					if (err) {
770						/*
771						 * Fill in the user
772						 * initiated abort
773						 */
774						struct sctp_paramhdr *ph;
775
776						ph = mtod(err, struct sctp_paramhdr *);
777						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
778						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
779						ph->param_length = htons(SCTP_BUF_LEN(err));
780					}
781					sctp_send_abort_tcb(stcb, err);
782					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
783				}
784				SCTP_INP_RUNLOCK(inp);
785				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
786				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
787					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
788				}
789				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
790				/* No unlock tcb assoc is gone */
791				return (0);
792			}
793			if (TAILQ_EMPTY(&asoc->send_queue) &&
794			    TAILQ_EMPTY(&asoc->sent_queue) &&
795			    (asoc->stream_queue_cnt == 0)) {
796				/* there is nothing queued to send, so done */
797				if (asoc->locked_on_sending) {
798					goto abort_anyway;
799				}
800				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
801				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
802					/* only send SHUTDOWN 1st time thru */
803					sctp_stop_timers_for_shutdown(stcb);
804					sctp_send_shutdown(stcb,
805					    stcb->asoc.primary_destination);
806					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
807					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
808					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
809						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
810					}
811					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
812					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
813					    stcb->sctp_ep, stcb,
814					    asoc->primary_destination);
815					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
816					    stcb->sctp_ep, stcb,
817					    asoc->primary_destination);
818				}
819			} else {
820				/*
821				 * we still got (or just got) data to send,
822				 * so set SHUTDOWN_PENDING
823				 */
824				/*
825				 * XXX sockets draft says that SCTP_EOF
826				 * should be sent with no data. currently,
827				 * we will allow user data to be sent first
828				 * and move to SHUTDOWN-PENDING
829				 */
830				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
831				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
832				    asoc->primary_destination);
833				if (asoc->locked_on_sending) {
834					/* Locked to send out the data */
835					struct sctp_stream_queue_pending *sp;
836
837					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
838					if (sp == NULL) {
839						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
840						    asoc->locked_on_sending->stream_no);
841					} else {
842						if ((sp->length == 0) && (sp->msg_is_complete == 0))
843							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
844					}
845				}
846				if (TAILQ_EMPTY(&asoc->send_queue) &&
847				    TAILQ_EMPTY(&asoc->sent_queue) &&
848				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
849					struct mbuf *op_err;
850
851			abort_anyway:
852					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
853					    0, M_DONTWAIT, 1, MT_DATA);
854					if (op_err) {
855						/*
856						 * Fill in the user
857						 * initiated abort
858						 */
859						struct sctp_paramhdr *ph;
860						uint32_t *ippp;
861
862						SCTP_BUF_LEN(op_err) =
863						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
864						ph = mtod(op_err,
865						    struct sctp_paramhdr *);
866						ph->param_type = htons(
867						    SCTP_CAUSE_USER_INITIATED_ABT);
868						ph->param_length = htons(SCTP_BUF_LEN(op_err));
869						ippp = (uint32_t *) (ph + 1);
870						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
871					}
872					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
873					sctp_send_abort_tcb(stcb, op_err);
874					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
875					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
876					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
877						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
878					}
879					SCTP_INP_RUNLOCK(inp);
880					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
881					return (0);
882				}
883			}
884			SCTP_TCB_UNLOCK(stcb);
885			SCTP_INP_RUNLOCK(inp);
886			return (0);
887		}
888		/* not reached */
889	} else {
890		/* UDP model does not support this */
891		SCTP_INP_RUNLOCK(inp);
892		return EOPNOTSUPP;
893	}
894}
895
896int
897sctp_shutdown(struct socket *so)
898{
899	struct sctp_inpcb *inp;
900
901	inp = (struct sctp_inpcb *)so->so_pcb;
902	if (inp == 0) {
903		return EINVAL;
904	}
905	SCTP_INP_RLOCK(inp);
906	/* For UDP model this is a invalid call */
907	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
908		/* Restore the flags that the soshutdown took away. */
909		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
910		/* This proc will wakeup for read and do nothing (I hope) */
911		SCTP_INP_RUNLOCK(inp);
912		return (EOPNOTSUPP);
913	}
914	/*
915	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
916	 * or SHUT_RDWR. This means we put the shutdown flag against it.
917	 */
918	{
919		struct sctp_tcb *stcb;
920		struct sctp_association *asoc;
921
922		socantsendmore(so);
923
924		stcb = LIST_FIRST(&inp->sctp_asoc_list);
925		if (stcb == NULL) {
926			/*
927			 * Ok we hit the case that the shutdown call was
928			 * made after an abort or something. Nothing to do
929			 * now.
930			 */
931			SCTP_INP_RUNLOCK(inp);
932			return (0);
933		}
934		SCTP_TCB_LOCK(stcb);
935		asoc = &stcb->asoc;
936		if (TAILQ_EMPTY(&asoc->send_queue) &&
937		    TAILQ_EMPTY(&asoc->sent_queue) &&
938		    (asoc->stream_queue_cnt == 0)) {
939			if (asoc->locked_on_sending) {
940				goto abort_anyway;
941			}
942			/* there is nothing queued to send, so I'm done... */
943			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
944				/* only send SHUTDOWN the first time through */
945				sctp_stop_timers_for_shutdown(stcb);
946				sctp_send_shutdown(stcb,
947				    stcb->asoc.primary_destination);
948				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
949				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
950				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
951					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
952				}
953				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
954				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
955				    stcb->sctp_ep, stcb,
956				    asoc->primary_destination);
957				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
958				    stcb->sctp_ep, stcb,
959				    asoc->primary_destination);
960			}
961		} else {
962			/*
963			 * we still got (or just got) data to send, so set
964			 * SHUTDOWN_PENDING
965			 */
966			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
967			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
968			    asoc->primary_destination);
969
970			if (asoc->locked_on_sending) {
971				/* Locked to send out the data */
972				struct sctp_stream_queue_pending *sp;
973
974				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
975				if (sp == NULL) {
976					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
977					    asoc->locked_on_sending->stream_no);
978				} else {
979					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
980						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
981					}
982				}
983			}
984			if (TAILQ_EMPTY(&asoc->send_queue) &&
985			    TAILQ_EMPTY(&asoc->sent_queue) &&
986			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
987				struct mbuf *op_err;
988
989		abort_anyway:
990				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
991				    0, M_DONTWAIT, 1, MT_DATA);
992				if (op_err) {
993					/* Fill in the user initiated abort */
994					struct sctp_paramhdr *ph;
995					uint32_t *ippp;
996
997					SCTP_BUF_LEN(op_err) =
998					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
999					ph = mtod(op_err,
1000					    struct sctp_paramhdr *);
1001					ph->param_type = htons(
1002					    SCTP_CAUSE_USER_INITIATED_ABT);
1003					ph->param_length = htons(SCTP_BUF_LEN(op_err));
1004					ippp = (uint32_t *) (ph + 1);
1005					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1006				}
1007				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1008				sctp_abort_an_association(stcb->sctp_ep, stcb,
1009				    SCTP_RESPONSE_TO_USER_REQ,
1010				    op_err);
1011				goto skip_unlock;
1012			}
1013		}
1014		SCTP_TCB_UNLOCK(stcb);
1015	}
1016skip_unlock:
1017	SCTP_INP_RUNLOCK(inp);
1018	return 0;
1019}
1020
1021/*
1022 * copies a "user" presentable address and removes embedded scope, etc.
1023 * returns 0 on success, 1 on error
1024 */
1025static uint32_t
1026sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1027{
1028	struct sockaddr_in6 lsa6;
1029
1030	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1031	    &lsa6);
1032	memcpy(ss, sa, sa->sa_len);
1033	return (0);
1034}
1035
1036
1037
1038static size_t
1039sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1040    struct sctp_tcb *stcb,
1041    size_t limit,
1042    struct sockaddr_storage *sas,
1043    uint32_t vrf_id)
1044{
1045	struct sctp_ifn *sctp_ifn;
1046	struct sctp_ifa *sctp_ifa;
1047	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1048	size_t actual;
1049	int ipv4_addr_legal, ipv6_addr_legal;
1050	struct sctp_vrf *vrf;
1051
1052	actual = 0;
1053	if (limit <= 0)
1054		return (actual);
1055
1056	if (stcb) {
1057		/* Turn on all the appropriate scope */
1058		loopback_scope = stcb->asoc.loopback_scope;
1059		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1060		local_scope = stcb->asoc.local_scope;
1061		site_scope = stcb->asoc.site_scope;
1062	} else {
1063		/* Turn on ALL scope, since we look at the EP */
1064		loopback_scope = ipv4_local_scope = local_scope =
1065		    site_scope = 1;
1066	}
1067	ipv4_addr_legal = ipv6_addr_legal = 0;
1068	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1069		ipv6_addr_legal = 1;
1070		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1071			ipv4_addr_legal = 1;
1072		}
1073	} else {
1074		ipv4_addr_legal = 1;
1075	}
1076	vrf = sctp_find_vrf(vrf_id);
1077	if (vrf == NULL) {
1078		return (0);
1079	}
1080	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1081		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1082			if ((loopback_scope == 0) &&
1083			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1084				/* Skip loopback if loopback_scope not set */
1085				continue;
1086			}
1087			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1088				if (stcb) {
1089					/*
1090					 * For the BOUND-ALL case, the list
1091					 * associated with a TCB is Always
1092					 * considered a reverse list.. i.e.
1093					 * it lists addresses that are NOT
1094					 * part of the association. If this
1095					 * is one of those we must skip it.
1096					 */
1097					if (sctp_is_addr_restricted(stcb,
1098					    sctp_ifa)) {
1099						continue;
1100					}
1101				}
1102				if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1103				    (ipv4_addr_legal)) {
1104					struct sockaddr_in *sin;
1105
1106					sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1107					if (sin->sin_addr.s_addr == 0) {
1108						/*
1109						 * we skip unspecifed
1110						 * addresses
1111						 */
1112						continue;
1113					}
1114					if ((ipv4_local_scope == 0) &&
1115					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1116						continue;
1117					}
1118					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1119						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1120						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1121						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1122						actual += sizeof(struct sockaddr_in6);
1123					} else {
1124						memcpy(sas, sin, sizeof(*sin));
1125						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1126						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1127						actual += sizeof(*sin);
1128					}
1129					if (actual >= limit) {
1130						return (actual);
1131					}
1132				} else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1133				    (ipv6_addr_legal)) {
1134					struct sockaddr_in6 *sin6;
1135
1136					sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1137					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1138						/*
1139						 * we skip unspecifed
1140						 * addresses
1141						 */
1142						continue;
1143					}
1144					if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1145						if (local_scope == 0)
1146							continue;
1147						if (sin6->sin6_scope_id == 0) {
1148							if (sa6_recoverscope(sin6) != 0)
1149								/*
1150								 * bad link
1151								 * local
1152								 * address
1153								 */
1154								continue;
1155						}
1156					}
1157					if ((site_scope == 0) &&
1158					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1159						continue;
1160					}
1161					memcpy(sas, sin6, sizeof(*sin6));
1162					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1163					sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1164					actual += sizeof(*sin6);
1165					if (actual >= limit) {
1166						return (actual);
1167					}
1168				}
1169			}
1170		}
1171	} else {
1172		struct sctp_laddr *laddr;
1173
1174		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1175			if (stcb) {
1176				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1177					continue;
1178				}
1179			}
1180			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1181				continue;
1182
1183			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1184			sas = (struct sockaddr_storage *)((caddr_t)sas +
1185			    laddr->ifa->address.sa.sa_len);
1186			actual += laddr->ifa->address.sa.sa_len;
1187			if (actual >= limit) {
1188				return (actual);
1189			}
1190		}
1191	}
1192	return (actual);
1193}
1194
1195static size_t
1196sctp_fill_up_addresses(struct sctp_inpcb *inp,
1197    struct sctp_tcb *stcb,
1198    size_t limit,
1199    struct sockaddr_storage *sas)
1200{
1201	size_t size = 0;
1202
1203	/* fill up addresses for the endpoint's default vrf */
1204	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1205	    inp->def_vrf_id);
1206	return (size);
1207}
1208
1209static int
1210sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1211{
1212	int cnt = 0;
1213	struct sctp_vrf *vrf = NULL;
1214
1215	/*
1216	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1217	 * number of addresses that you COULD get. In reality the sub-set
1218	 * bound may have an exclusion list for a given TCB OR in the
1219	 * bound-all case a TCB may NOT include the loopback or other
1220	 * addresses as well.
1221	 */
1222	vrf = sctp_find_vrf(vrf_id);
1223	if (vrf == NULL) {
1224		return (0);
1225	}
1226	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1227		struct sctp_ifn *sctp_ifn;
1228		struct sctp_ifa *sctp_ifa;
1229
1230		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1231			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1232				/* Count them if they are the right type */
1233				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1234					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1235						cnt += sizeof(struct sockaddr_in6);
1236					else
1237						cnt += sizeof(struct sockaddr_in);
1238
1239				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1240					cnt += sizeof(struct sockaddr_in6);
1241			}
1242		}
1243	} else {
1244		struct sctp_laddr *laddr;
1245
1246		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1247			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1248				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1249					cnt += sizeof(struct sockaddr_in6);
1250				else
1251					cnt += sizeof(struct sockaddr_in);
1252
1253			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1254				cnt += sizeof(struct sockaddr_in6);
1255		}
1256	}
1257	return (cnt);
1258}
1259
1260static int
1261sctp_count_max_addresses(struct sctp_inpcb *inp)
1262{
1263	int cnt = 0;
1264
1265	/* count addresses for the endpoint's default VRF */
1266	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1267	return (cnt);
1268}
1269
1270static int
1271sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1272    size_t optsize, void *p, int delay)
1273{
1274	int error = 0;
1275	int creat_lock_on = 0;
1276	struct sctp_tcb *stcb = NULL;
1277	struct sockaddr *sa;
1278	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1279	int added = 0;
1280	uint32_t vrf_id;
1281	int bad_addresses = 0;
1282	sctp_assoc_t *a_id;
1283
1284	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1285
1286	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1287	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1288		/* We are already connected AND the TCP model */
1289		return (EADDRINUSE);
1290	}
1291	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1292		return (EINVAL);
1293	}
1294	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1295		SCTP_INP_RLOCK(inp);
1296		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1297		SCTP_INP_RUNLOCK(inp);
1298	}
1299	if (stcb) {
1300		return (EALREADY);
1301	}
1302	SCTP_INP_INCR_REF(inp);
1303	SCTP_ASOC_CREATE_LOCK(inp);
1304	creat_lock_on = 1;
1305	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1306	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1307		error = EFAULT;
1308		goto out_now;
1309	}
1310	totaddrp = (int *)optval;
1311	totaddr = *totaddrp;
1312	sa = (struct sockaddr *)(totaddrp + 1);
1313	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1314	if ((stcb != NULL) || bad_addresses) {
1315		/* Already have or am bring up an association */
1316		SCTP_ASOC_CREATE_UNLOCK(inp);
1317		creat_lock_on = 0;
1318		if (stcb)
1319			SCTP_TCB_UNLOCK(stcb);
1320		if (bad_addresses == 0)
1321			error = EALREADY;
1322		goto out_now;
1323	}
1324#ifdef INET6
1325	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1326	    (num_v6 > 0)) {
1327		error = EINVAL;
1328		goto out_now;
1329	}
1330	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1331	    (num_v4 > 0)) {
1332		struct in6pcb *inp6;
1333
1334		inp6 = (struct in6pcb *)inp;
1335		if (SCTP_IPV6_V6ONLY(inp6)) {
1336			/*
1337			 * if IPV6_V6ONLY flag, ignore connections destined
1338			 * to a v4 addr or v4-mapped addr
1339			 */
1340			error = EINVAL;
1341			goto out_now;
1342		}
1343	}
1344#endif				/* INET6 */
1345	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1346	    SCTP_PCB_FLAGS_UNBOUND) {
1347		/* Bind a ephemeral port */
1348		error = sctp_inpcb_bind(so, NULL, NULL, p);
1349		if (error) {
1350			goto out_now;
1351		}
1352	}
1353	/* FIX ME: do we want to pass in a vrf on the connect call? */
1354	vrf_id = inp->def_vrf_id;
1355
1356	/* We are GOOD to go */
1357	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1358	    (struct thread *)p
1359	    );
1360	if (stcb == NULL) {
1361		/* Gak! no memory */
1362		goto out_now;
1363	}
1364	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1365	/* move to second address */
1366	if (sa->sa_family == AF_INET)
1367		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1368	else
1369		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1370
1371	error = 0;
1372	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1373	/* Fill in the return id */
1374	if (error) {
1375		sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1376		goto out_now;
1377	}
1378	a_id = (sctp_assoc_t *) optval;
1379	*a_id = sctp_get_associd(stcb);
1380
1381	/* initialize authentication parameters for the assoc */
1382	sctp_initialize_auth_params(inp, stcb);
1383
1384	if (delay) {
1385		/* doing delayed connection */
1386		stcb->asoc.delayed_connection = 1;
1387		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1388	} else {
1389		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1390		sctp_send_initiate(inp, stcb);
1391	}
1392	SCTP_TCB_UNLOCK(stcb);
1393	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1394		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1395		/* Set the connected flag so we can queue data */
1396		soisconnecting(so);
1397	}
1398out_now:
1399	if (creat_lock_on) {
1400		SCTP_ASOC_CREATE_UNLOCK(inp);
1401	}
1402	SCTP_INP_DECR_REF(inp);
1403	return error;
1404}
1405
1406#define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1407	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1408	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1409		SCTP_INP_RLOCK(inp); \
1410		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1411		if (stcb) { \
1412			SCTP_TCB_LOCK(stcb); \
1413                } \
1414		SCTP_INP_RUNLOCK(inp); \
1415	} else if (assoc_id != 0) { \
1416		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1417		if (stcb == NULL) { \
1418			error = ENOENT; \
1419			break; \
1420		} \
1421	} else { \
1422		stcb = NULL; \
1423        } \
1424  }
1425
1426
1427#define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
1428	if (size < sizeof(type)) { \
1429		error = EINVAL; \
1430		break; \
1431	} else { \
1432		destp = (type *)srcp; \
1433	} \
1434      }
1435
1436static int
1437sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1438    void *p)
1439{
1440	struct sctp_inpcb *inp;
1441	int error, val = 0;
1442	struct sctp_tcb *stcb = NULL;
1443
1444	if (optval == NULL) {
1445		return (EINVAL);
1446	}
1447	inp = (struct sctp_inpcb *)so->so_pcb;
1448	if (inp == 0)
1449		return EINVAL;
1450	error = 0;
1451
1452	switch (optname) {
1453	case SCTP_NODELAY:
1454	case SCTP_AUTOCLOSE:
1455	case SCTP_EXPLICIT_EOR:
1456	case SCTP_AUTO_ASCONF:
1457	case SCTP_DISABLE_FRAGMENTS:
1458	case SCTP_I_WANT_MAPPED_V4_ADDR:
1459	case SCTP_USE_EXT_RCVINFO:
1460		SCTP_INP_RLOCK(inp);
1461		switch (optname) {
1462		case SCTP_DISABLE_FRAGMENTS:
1463			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1464			break;
1465		case SCTP_I_WANT_MAPPED_V4_ADDR:
1466			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1467			break;
1468		case SCTP_AUTO_ASCONF:
1469			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1470			break;
1471		case SCTP_EXPLICIT_EOR:
1472			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1473			break;
1474		case SCTP_NODELAY:
1475			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1476			break;
1477		case SCTP_USE_EXT_RCVINFO:
1478			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1479			break;
1480		case SCTP_AUTOCLOSE:
1481			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1482				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1483			else
1484				val = 0;
1485			break;
1486
1487		default:
1488			error = ENOPROTOOPT;
1489		}		/* end switch (sopt->sopt_name) */
1490		if (optname != SCTP_AUTOCLOSE) {
1491			/* make it an "on/off" value */
1492			val = (val != 0);
1493		}
1494		if (*optsize < sizeof(val)) {
1495			error = EINVAL;
1496		}
1497		SCTP_INP_RUNLOCK(inp);
1498		if (error == 0) {
1499			/* return the option value */
1500			*(int *)optval = val;
1501			*optsize = sizeof(val);
1502		}
1503		break;
1504	case SCTP_GET_PACKET_LOG:
1505		{
1506#ifdef  SCTP_PACKET_LOGGING
1507			uint8_t *target;
1508			int ret;
1509
1510			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1511			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1512			*optsize = ret;
1513#else
1514			error = EOPNOTSUPP;
1515#endif
1516			break;
1517		}
1518	case SCTP_PARTIAL_DELIVERY_POINT:
1519		{
1520			uint32_t *value;
1521
1522			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1523			*value = inp->partial_delivery_point;
1524			*optsize = sizeof(uint32_t);
1525		}
1526		break;
1527	case SCTP_FRAGMENT_INTERLEAVE:
1528		{
1529			uint32_t *value;
1530
1531			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1532			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1533				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1534					*value = SCTP_FRAG_LEVEL_2;
1535				} else {
1536					*value = SCTP_FRAG_LEVEL_1;
1537				}
1538			} else {
1539				*value = SCTP_FRAG_LEVEL_0;
1540			}
1541			*optsize = sizeof(uint32_t);
1542		}
1543		break;
1544	case SCTP_CMT_ON_OFF:
1545		{
1546			struct sctp_assoc_value *av;
1547
1548			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1549			if (sctp_cmt_on_off) {
1550				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1551				if (stcb) {
1552					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1553					SCTP_TCB_UNLOCK(stcb);
1554
1555				} else {
1556					error = ENOTCONN;
1557				}
1558			} else {
1559				error = ENOPROTOOPT;
1560			}
1561			*optsize = sizeof(*av);
1562		}
1563		break;
1564		/* JRS - Get socket option for pluggable congestion control */
1565	case SCTP_PLUGGABLE_CC:
1566		{
1567			struct sctp_assoc_value *av;
1568
1569			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1570			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1571			if (stcb) {
1572				av->assoc_value = stcb->asoc.congestion_control_module;
1573				SCTP_TCB_UNLOCK(stcb);
1574			} else {
1575				av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1576			}
1577			*optsize = sizeof(*av);
1578		}
1579		break;
1580	case SCTP_GET_ADDR_LEN:
1581		{
1582			struct sctp_assoc_value *av;
1583
1584			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1585			error = EINVAL;
1586#ifdef INET
1587			if (av->assoc_value == AF_INET) {
1588				av->assoc_value = sizeof(struct sockaddr_in);
1589				error = 0;
1590			}
1591#endif
1592#ifdef INET6
1593			if (av->assoc_value == AF_INET6) {
1594				av->assoc_value = sizeof(struct sockaddr_in6);
1595				error = 0;
1596			}
1597#endif
1598			*optsize = sizeof(*av);
1599		}
1600		break;
1601	case SCTP_GET_ASSOC_NUMBER:
1602		{
1603			uint32_t *value, cnt;
1604
1605			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1606			cnt = 0;
1607			SCTP_INP_RLOCK(inp);
1608			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1609				cnt++;
1610			}
1611			SCTP_INP_RUNLOCK(inp);
1612			*value = cnt;
1613			*optsize = sizeof(uint32_t);
1614		}
1615		break;
1616
1617	case SCTP_GET_ASSOC_ID_LIST:
1618		{
1619			struct sctp_assoc_ids *ids;
1620			unsigned int at, limit;
1621
1622			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1623			at = 0;
1624			limit = *optsize / sizeof(sctp_assoc_t);
1625			SCTP_INP_RLOCK(inp);
1626			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1627				if (at < limit) {
1628					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1629				} else {
1630					error = EINVAL;
1631					break;
1632				}
1633			}
1634			SCTP_INP_RUNLOCK(inp);
1635			*optsize = at * sizeof(sctp_assoc_t);
1636		}
1637		break;
1638	case SCTP_CONTEXT:
1639		{
1640			struct sctp_assoc_value *av;
1641
1642			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1643			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1644
1645			if (stcb) {
1646				av->assoc_value = stcb->asoc.context;
1647				SCTP_TCB_UNLOCK(stcb);
1648			} else {
1649				SCTP_INP_RLOCK(inp);
1650				av->assoc_value = inp->sctp_context;
1651				SCTP_INP_RUNLOCK(inp);
1652			}
1653			*optsize = sizeof(*av);
1654		}
1655		break;
1656	case SCTP_VRF_ID:
1657		{
1658			uint32_t *default_vrfid;
1659
1660			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1661			*default_vrfid = inp->def_vrf_id;
1662			break;
1663		}
1664	case SCTP_GET_ASOC_VRF:
1665		{
1666			struct sctp_assoc_value *id;
1667
1668			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1669			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1670			if (stcb == NULL) {
1671				error = EINVAL;
1672				break;
1673			}
1674			id->assoc_value = stcb->asoc.vrf_id;
1675			break;
1676		}
1677	case SCTP_GET_VRF_IDS:
1678		{
1679			error = EOPNOTSUPP;
1680			break;
1681		}
1682	case SCTP_GET_NONCE_VALUES:
1683		{
1684			struct sctp_get_nonce_values *gnv;
1685
1686			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1687			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1688
1689			if (stcb) {
1690				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1691				gnv->gn_local_tag = stcb->asoc.my_vtag;
1692				SCTP_TCB_UNLOCK(stcb);
1693			} else {
1694				error = ENOTCONN;
1695			}
1696			*optsize = sizeof(*gnv);
1697		}
1698		break;
1699	case SCTP_DELAYED_SACK:
1700		{
1701			struct sctp_sack_info *sack;
1702
1703			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1704			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1705			if (stcb) {
1706				sack->sack_delay = stcb->asoc.delayed_ack;
1707				sack->sack_freq = stcb->asoc.sack_freq;
1708				SCTP_TCB_UNLOCK(stcb);
1709			} else {
1710				SCTP_INP_RLOCK(inp);
1711				sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1712				sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1713				SCTP_INP_RUNLOCK(inp);
1714			}
1715			*optsize = sizeof(*sack);
1716		}
1717		break;
1718
1719	case SCTP_GET_SNDBUF_USE:
1720		{
1721			struct sctp_sockstat *ss;
1722
1723			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1724			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1725
1726			if (stcb) {
1727				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1728				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1729				    stcb->asoc.size_on_all_streams);
1730				SCTP_TCB_UNLOCK(stcb);
1731			} else {
1732				error = ENOTCONN;
1733			}
1734			*optsize = sizeof(struct sctp_sockstat);
1735		}
1736		break;
1737	case SCTP_MAX_BURST:
1738		{
1739			uint8_t *value;
1740
1741			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1742
1743			SCTP_INP_RLOCK(inp);
1744			*value = inp->sctp_ep.max_burst;
1745			SCTP_INP_RUNLOCK(inp);
1746			*optsize = sizeof(uint8_t);
1747		}
1748		break;
1749	case SCTP_MAXSEG:
1750		{
1751			struct sctp_assoc_value *av;
1752			int ovh;
1753
1754			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1755			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1756
1757			if (stcb) {
1758				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1759				SCTP_TCB_UNLOCK(stcb);
1760			} else {
1761				SCTP_INP_RLOCK(inp);
1762				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1763					ovh = SCTP_MED_OVERHEAD;
1764				} else {
1765					ovh = SCTP_MED_V4_OVERHEAD;
1766				}
1767				if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1768					av->assoc_value = 0;
1769				else
1770					av->assoc_value = inp->sctp_frag_point - ovh;
1771				SCTP_INP_RUNLOCK(inp);
1772			}
1773			*optsize = sizeof(struct sctp_assoc_value);
1774		}
1775		break;
1776	case SCTP_GET_STAT_LOG:
1777		error = sctp_fill_stat_log(optval, optsize);
1778		break;
1779	case SCTP_EVENTS:
1780		{
1781			struct sctp_event_subscribe *events;
1782
1783			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1784			memset(events, 0, sizeof(*events));
1785			SCTP_INP_RLOCK(inp);
1786			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1787				events->sctp_data_io_event = 1;
1788
1789			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1790				events->sctp_association_event = 1;
1791
1792			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1793				events->sctp_address_event = 1;
1794
1795			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1796				events->sctp_send_failure_event = 1;
1797
1798			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1799				events->sctp_peer_error_event = 1;
1800
1801			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1802				events->sctp_shutdown_event = 1;
1803
1804			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1805				events->sctp_partial_delivery_event = 1;
1806
1807			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1808				events->sctp_adaptation_layer_event = 1;
1809
1810			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1811				events->sctp_authentication_event = 1;
1812
1813			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1814				events->sctp_stream_reset_events = 1;
1815			SCTP_INP_RUNLOCK(inp);
1816			*optsize = sizeof(struct sctp_event_subscribe);
1817		}
1818		break;
1819
1820	case SCTP_ADAPTATION_LAYER:
1821		{
1822			uint32_t *value;
1823
1824			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1825
1826			SCTP_INP_RLOCK(inp);
1827			*value = inp->sctp_ep.adaptation_layer_indicator;
1828			SCTP_INP_RUNLOCK(inp);
1829			*optsize = sizeof(uint32_t);
1830		}
1831		break;
1832	case SCTP_SET_INITIAL_DBG_SEQ:
1833		{
1834			uint32_t *value;
1835
1836			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1837			SCTP_INP_RLOCK(inp);
1838			*value = inp->sctp_ep.initial_sequence_debug;
1839			SCTP_INP_RUNLOCK(inp);
1840			*optsize = sizeof(uint32_t);
1841		}
1842		break;
1843	case SCTP_GET_LOCAL_ADDR_SIZE:
1844		{
1845			uint32_t *value;
1846
1847			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1848			SCTP_INP_RLOCK(inp);
1849			*value = sctp_count_max_addresses(inp);
1850			SCTP_INP_RUNLOCK(inp);
1851			*optsize = sizeof(uint32_t);
1852		}
1853		break;
1854	case SCTP_GET_REMOTE_ADDR_SIZE:
1855		{
1856			uint32_t *value;
1857			size_t size;
1858			struct sctp_nets *net;
1859
1860			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1861			/* FIXME MT: change to sctp_assoc_value? */
1862			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1863
1864			if (stcb) {
1865				size = 0;
1866				/* Count the sizes */
1867				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1868					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1869					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1870						size += sizeof(struct sockaddr_in6);
1871					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1872						size += sizeof(struct sockaddr_in);
1873					} else {
1874						/* huh */
1875						break;
1876					}
1877				}
1878				SCTP_TCB_UNLOCK(stcb);
1879				*value = (uint32_t) size;
1880			} else {
1881				error = ENOTCONN;
1882			}
1883			*optsize = sizeof(uint32_t);
1884		}
1885		break;
1886	case SCTP_GET_PEER_ADDRESSES:
1887		/*
1888		 * Get the address information, an array is passed in to
1889		 * fill up we pack it.
1890		 */
1891		{
1892			size_t cpsz, left;
1893			struct sockaddr_storage *sas;
1894			struct sctp_nets *net;
1895			struct sctp_getaddresses *saddr;
1896
1897			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1898			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1899
1900			if (stcb) {
1901				left = (*optsize) - sizeof(struct sctp_getaddresses);
1902				*optsize = sizeof(struct sctp_getaddresses);
1903				sas = (struct sockaddr_storage *)&saddr->addr[0];
1904
1905				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1906					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1907					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1908						cpsz = sizeof(struct sockaddr_in6);
1909					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1910						cpsz = sizeof(struct sockaddr_in);
1911					} else {
1912						/* huh */
1913						break;
1914					}
1915					if (left < cpsz) {
1916						/* not enough room. */
1917						break;
1918					}
1919					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1920					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1921						/* Must map the address */
1922						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1923						    (struct sockaddr_in6 *)sas);
1924					} else {
1925						memcpy(sas, &net->ro._l_addr, cpsz);
1926					}
1927					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1928
1929					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1930					left -= cpsz;
1931					*optsize += cpsz;
1932				}
1933				SCTP_TCB_UNLOCK(stcb);
1934			} else {
1935				error = ENOENT;
1936			}
1937		}
1938		break;
1939	case SCTP_GET_LOCAL_ADDRESSES:
1940		{
1941			size_t limit, actual;
1942			struct sockaddr_storage *sas;
1943			struct sctp_getaddresses *saddr;
1944
1945			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1946			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1947
1948			sas = (struct sockaddr_storage *)&saddr->addr[0];
1949			limit = *optsize - sizeof(sctp_assoc_t);
1950			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1951			if (stcb) {
1952				SCTP_TCB_UNLOCK(stcb);
1953			}
1954			*optsize = sizeof(struct sockaddr_storage) + actual;
1955		}
1956		break;
1957	case SCTP_PEER_ADDR_PARAMS:
1958		{
1959			struct sctp_paddrparams *paddrp;
1960			struct sctp_nets *net;
1961
1962			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1963			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1964
1965			net = NULL;
1966			if (stcb) {
1967				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1968			} else {
1969				/*
1970				 * We increment here since
1971				 * sctp_findassociation_ep_addr() wil do a
1972				 * decrement if it finds the stcb as long as
1973				 * the locked tcb (last argument) is NOT a
1974				 * TCB.. aka NULL.
1975				 */
1976				SCTP_INP_INCR_REF(inp);
1977				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1978				if (stcb == NULL) {
1979					SCTP_INP_DECR_REF(inp);
1980				}
1981			}
1982
1983			if (stcb) {
1984				/* Applys to the specific association */
1985				paddrp->spp_flags = 0;
1986				if (net) {
1987					int ovh;
1988
1989					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1990						ovh = SCTP_MED_OVERHEAD;
1991					} else {
1992						ovh = SCTP_MED_V4_OVERHEAD;
1993					}
1994
1995
1996					paddrp->spp_pathmaxrxt = net->failure_threshold;
1997					paddrp->spp_pathmtu = net->mtu - ovh;
1998					/* get flags for HB */
1999					if (net->dest_state & SCTP_ADDR_NOHB)
2000						paddrp->spp_flags |= SPP_HB_DISABLE;
2001					else
2002						paddrp->spp_flags |= SPP_HB_ENABLE;
2003					/* get flags for PMTU */
2004					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2005						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2006					} else {
2007						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2008					}
2009#ifdef INET
2010					if (net->ro._l_addr.sin.sin_family == AF_INET) {
2011						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2012						paddrp->spp_flags |= SPP_IPV4_TOS;
2013					}
2014#endif
2015#ifdef INET6
2016					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2017						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2018						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2019					}
2020#endif
2021				} else {
2022					/*
2023					 * No destination so return default
2024					 * value
2025					 */
2026					int cnt = 0;
2027
2028					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2029					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2030#ifdef INET
2031					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2032					paddrp->spp_flags |= SPP_IPV4_TOS;
2033#endif
2034#ifdef INET6
2035					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2036					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2037#endif
2038					/* default settings should be these */
2039					if (stcb->asoc.hb_is_disabled == 0) {
2040						paddrp->spp_flags |= SPP_HB_ENABLE;
2041					} else {
2042						paddrp->spp_flags |= SPP_HB_DISABLE;
2043					}
2044					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2045						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2046							cnt++;
2047						}
2048					}
2049					if (cnt) {
2050						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2051					}
2052				}
2053				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2054				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2055				SCTP_TCB_UNLOCK(stcb);
2056			} else {
2057				/* Use endpoint defaults */
2058				SCTP_INP_RLOCK(inp);
2059				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2060				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2061				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2062				/* get inp's default */
2063#ifdef INET
2064				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2065				paddrp->spp_flags |= SPP_IPV4_TOS;
2066#endif
2067#ifdef INET6
2068				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2069					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2070					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2071				}
2072#endif
2073				/* can't return this */
2074				paddrp->spp_pathmtu = 0;
2075
2076				/* default behavior, no stcb */
2077				paddrp->spp_flags = SPP_PMTUD_ENABLE;
2078
2079				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2080					paddrp->spp_flags |= SPP_HB_ENABLE;
2081				} else {
2082					paddrp->spp_flags |= SPP_HB_DISABLE;
2083				}
2084				SCTP_INP_RUNLOCK(inp);
2085			}
2086			*optsize = sizeof(struct sctp_paddrparams);
2087		}
2088		break;
2089	case SCTP_GET_PEER_ADDR_INFO:
2090		{
2091			struct sctp_paddrinfo *paddri;
2092			struct sctp_nets *net;
2093
2094			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2095			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2096
2097			net = NULL;
2098			if (stcb) {
2099				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2100			} else {
2101				/*
2102				 * We increment here since
2103				 * sctp_findassociation_ep_addr() wil do a
2104				 * decrement if it finds the stcb as long as
2105				 * the locked tcb (last argument) is NOT a
2106				 * TCB.. aka NULL.
2107				 */
2108				SCTP_INP_INCR_REF(inp);
2109				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2110				if (stcb == NULL) {
2111					SCTP_INP_DECR_REF(inp);
2112				}
2113			}
2114
2115			if ((stcb) && (net)) {
2116				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2117				paddri->spinfo_cwnd = net->cwnd;
2118				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2119				paddri->spinfo_rto = net->RTO;
2120				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2121				SCTP_TCB_UNLOCK(stcb);
2122			} else {
2123				if (stcb) {
2124					SCTP_TCB_UNLOCK(stcb);
2125				}
2126				error = ENOENT;
2127			}
2128			*optsize = sizeof(struct sctp_paddrinfo);
2129		}
2130		break;
2131	case SCTP_PCB_STATUS:
2132		{
2133			struct sctp_pcbinfo *spcb;
2134
2135			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2136			sctp_fill_pcbinfo(spcb);
2137			*optsize = sizeof(struct sctp_pcbinfo);
2138		}
2139		break;
2140
2141	case SCTP_STATUS:
2142		{
2143			struct sctp_nets *net;
2144			struct sctp_status *sstat;
2145
2146			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2147			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2148
2149			if (stcb == NULL) {
2150				error = EINVAL;
2151				break;
2152			}
2153			/*
2154			 * I think passing the state is fine since
2155			 * sctp_constants.h will be available to the user
2156			 * land.
2157			 */
2158			sstat->sstat_state = stcb->asoc.state;
2159			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2160			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2161			/*
2162			 * We can't include chunks that have been passed to
2163			 * the socket layer. Only things in queue.
2164			 */
2165			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2166			    stcb->asoc.cnt_on_all_streams);
2167
2168
2169			sstat->sstat_instrms = stcb->asoc.streamincnt;
2170			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2171			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2172			memcpy(&sstat->sstat_primary.spinfo_address,
2173			    &stcb->asoc.primary_destination->ro._l_addr,
2174			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2175			net = stcb->asoc.primary_destination;
2176			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2177			/*
2178			 * Again the user can get info from sctp_constants.h
2179			 * for what the state of the network is.
2180			 */
2181			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2182			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2183			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2184			sstat->sstat_primary.spinfo_rto = net->RTO;
2185			sstat->sstat_primary.spinfo_mtu = net->mtu;
2186			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2187			SCTP_TCB_UNLOCK(stcb);
2188			*optsize = sizeof(*sstat);
2189		}
2190		break;
2191	case SCTP_RTOINFO:
2192		{
2193			struct sctp_rtoinfo *srto;
2194
2195			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2196			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2197
2198			if (stcb) {
2199				srto->srto_initial = stcb->asoc.initial_rto;
2200				srto->srto_max = stcb->asoc.maxrto;
2201				srto->srto_min = stcb->asoc.minrto;
2202				SCTP_TCB_UNLOCK(stcb);
2203			} else {
2204				SCTP_INP_RLOCK(inp);
2205				srto->srto_initial = inp->sctp_ep.initial_rto;
2206				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2207				srto->srto_min = inp->sctp_ep.sctp_minrto;
2208				SCTP_INP_RUNLOCK(inp);
2209			}
2210			*optsize = sizeof(*srto);
2211		}
2212		break;
2213	case SCTP_ASSOCINFO:
2214		{
2215			struct sctp_assocparams *sasoc;
2216			uint32_t oldval;
2217
2218			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2219			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2220
2221			if (stcb) {
2222				oldval = sasoc->sasoc_cookie_life;
2223				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2224				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2225				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2226				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2227				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2228				SCTP_TCB_UNLOCK(stcb);
2229			} else {
2230				SCTP_INP_RLOCK(inp);
2231				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2232				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2233				sasoc->sasoc_number_peer_destinations = 0;
2234				sasoc->sasoc_peer_rwnd = 0;
2235				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2236				SCTP_INP_RUNLOCK(inp);
2237			}
2238			*optsize = sizeof(*sasoc);
2239		}
2240		break;
2241	case SCTP_DEFAULT_SEND_PARAM:
2242		{
2243			struct sctp_sndrcvinfo *s_info;
2244
2245			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2246			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2247
2248			if (stcb) {
2249				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2250				SCTP_TCB_UNLOCK(stcb);
2251			} else {
2252				SCTP_INP_RLOCK(inp);
2253				memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2254				SCTP_INP_RUNLOCK(inp);
2255			}
2256			*optsize = sizeof(*s_info);
2257		}
2258		break;
2259	case SCTP_INITMSG:
2260		{
2261			struct sctp_initmsg *sinit;
2262
2263			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2264			SCTP_INP_RLOCK(inp);
2265			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2266			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2267			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2268			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2269			SCTP_INP_RUNLOCK(inp);
2270			*optsize = sizeof(*sinit);
2271		}
2272		break;
2273	case SCTP_PRIMARY_ADDR:
2274		/* we allow a "get" operation on this */
2275		{
2276			struct sctp_setprim *ssp;
2277
2278			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2279			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2280
2281			if (stcb) {
2282				/* simply copy out the sockaddr_storage... */
2283				int len;
2284
2285				len = *optsize;
2286				if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2287					len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2288
2289				memcpy(&ssp->ssp_addr,
2290				    &stcb->asoc.primary_destination->ro._l_addr,
2291				    len);
2292				SCTP_TCB_UNLOCK(stcb);
2293			} else {
2294				error = EINVAL;
2295			}
2296			*optsize = sizeof(*ssp);
2297		}
2298		break;
2299
2300	case SCTP_HMAC_IDENT:
2301		{
2302			struct sctp_hmacalgo *shmac;
2303			sctp_hmaclist_t *hmaclist;
2304			uint32_t size;
2305			int i;
2306
2307			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2308
2309			SCTP_INP_RLOCK(inp);
2310			hmaclist = inp->sctp_ep.local_hmacs;
2311			if (hmaclist == NULL) {
2312				/* no HMACs to return */
2313				*optsize = sizeof(*shmac);
2314				SCTP_INP_RUNLOCK(inp);
2315				break;
2316			}
2317			/* is there room for all of the hmac ids? */
2318			size = sizeof(*shmac) + (hmaclist->num_algo *
2319			    sizeof(shmac->shmac_idents[0]));
2320			if ((size_t)(*optsize) < size) {
2321				error = EINVAL;
2322				SCTP_INP_RUNLOCK(inp);
2323				break;
2324			}
2325			/* copy in the list */
2326			for (i = 0; i < hmaclist->num_algo; i++)
2327				shmac->shmac_idents[i] = hmaclist->hmac[i];
2328			SCTP_INP_RUNLOCK(inp);
2329			*optsize = size;
2330			break;
2331		}
2332	case SCTP_AUTH_ACTIVE_KEY:
2333		{
2334			struct sctp_authkeyid *scact;
2335
2336			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2337			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2338
2339			if (stcb) {
2340				/* get the active key on the assoc */
2341				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2342				SCTP_TCB_UNLOCK(stcb);
2343			} else {
2344				/* get the endpoint active key */
2345				SCTP_INP_RLOCK(inp);
2346				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2347				SCTP_INP_RUNLOCK(inp);
2348			}
2349			*optsize = sizeof(*scact);
2350			break;
2351		}
2352	case SCTP_LOCAL_AUTH_CHUNKS:
2353		{
2354			struct sctp_authchunks *sac;
2355			sctp_auth_chklist_t *chklist = NULL;
2356			size_t size = 0;
2357
2358			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2359			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2360
2361			if (stcb) {
2362				/* get off the assoc */
2363				chklist = stcb->asoc.local_auth_chunks;
2364				/* is there enough space? */
2365				size = sctp_auth_get_chklist_size(chklist);
2366				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2367					error = EINVAL;
2368				} else {
2369					/* copy in the chunks */
2370					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2371				}
2372				SCTP_TCB_UNLOCK(stcb);
2373			} else {
2374				/* get off the endpoint */
2375				SCTP_INP_RLOCK(inp);
2376				chklist = inp->sctp_ep.local_auth_chunks;
2377				/* is there enough space? */
2378				size = sctp_auth_get_chklist_size(chklist);
2379				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2380					error = EINVAL;
2381				} else {
2382					/* copy in the chunks */
2383					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2384				}
2385				SCTP_INP_RUNLOCK(inp);
2386			}
2387			*optsize = sizeof(struct sctp_authchunks) + size;
2388			break;
2389		}
2390	case SCTP_PEER_AUTH_CHUNKS:
2391		{
2392			struct sctp_authchunks *sac;
2393			sctp_auth_chklist_t *chklist = NULL;
2394			size_t size = 0;
2395
2396			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2397			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2398
2399			if (stcb) {
2400				/* get off the assoc */
2401				chklist = stcb->asoc.peer_auth_chunks;
2402				/* is there enough space? */
2403				size = sctp_auth_get_chklist_size(chklist);
2404				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2405					error = EINVAL;
2406				} else {
2407					/* copy in the chunks */
2408					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2409				}
2410				SCTP_TCB_UNLOCK(stcb);
2411			} else {
2412				error = ENOENT;
2413			}
2414			*optsize = sizeof(struct sctp_authchunks) + size;
2415			break;
2416		}
2417
2418
2419	default:
2420		error = ENOPROTOOPT;
2421		*optsize = 0;
2422		break;
2423	}			/* end switch (sopt->sopt_name) */
2424	return (error);
2425}
2426
2427static int
2428sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2429    void *p)
2430{
2431	int error, set_opt;
2432	uint32_t *mopt;
2433	struct sctp_tcb *stcb = NULL;
2434	struct sctp_inpcb *inp;
2435	uint32_t vrf_id;
2436
2437	if (optval == NULL) {
2438		SCTP_PRINTF("optval is NULL\n");
2439		return (EINVAL);
2440	}
2441	inp = (struct sctp_inpcb *)so->so_pcb;
2442	if (inp == 0) {
2443		SCTP_PRINTF("inp is NULL?\n");
2444		return EINVAL;
2445	}
2446	vrf_id = inp->def_vrf_id;
2447
2448	error = 0;
2449	switch (optname) {
2450	case SCTP_NODELAY:
2451	case SCTP_AUTOCLOSE:
2452	case SCTP_AUTO_ASCONF:
2453	case SCTP_EXPLICIT_EOR:
2454	case SCTP_DISABLE_FRAGMENTS:
2455	case SCTP_USE_EXT_RCVINFO:
2456	case SCTP_I_WANT_MAPPED_V4_ADDR:
2457		/* copy in the option value */
2458		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2459		set_opt = 0;
2460		if (error)
2461			break;
2462		switch (optname) {
2463		case SCTP_DISABLE_FRAGMENTS:
2464			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2465			break;
2466		case SCTP_AUTO_ASCONF:
2467			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2468			break;
2469		case SCTP_EXPLICIT_EOR:
2470			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2471			break;
2472		case SCTP_USE_EXT_RCVINFO:
2473			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2474			break;
2475		case SCTP_I_WANT_MAPPED_V4_ADDR:
2476			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2477				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2478			} else {
2479				return (EINVAL);
2480			}
2481			break;
2482		case SCTP_NODELAY:
2483			set_opt = SCTP_PCB_FLAGS_NODELAY;
2484			break;
2485		case SCTP_AUTOCLOSE:
2486			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2487			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2488				return (EINVAL);
2489			}
2490			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2491			/*
2492			 * The value is in ticks. Note this does not effect
2493			 * old associations, only new ones.
2494			 */
2495			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2496			break;
2497		}
2498		SCTP_INP_WLOCK(inp);
2499		if (*mopt != 0) {
2500			sctp_feature_on(inp, set_opt);
2501		} else {
2502			sctp_feature_off(inp, set_opt);
2503		}
2504		SCTP_INP_WUNLOCK(inp);
2505		break;
2506	case SCTP_PARTIAL_DELIVERY_POINT:
2507		{
2508			uint32_t *value;
2509
2510			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2511			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2512				error = EINVAL;
2513				break;
2514			}
2515			inp->partial_delivery_point = *value;
2516		}
2517		break;
2518	case SCTP_FRAGMENT_INTERLEAVE:
2519		/* not yet until we re-write sctp_recvmsg() */
2520		{
2521			uint32_t *level;
2522
2523			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2524			if (*level == SCTP_FRAG_LEVEL_2) {
2525				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2526				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2527			} else if (*level == SCTP_FRAG_LEVEL_1) {
2528				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2529				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2530			} else if (*level == SCTP_FRAG_LEVEL_0) {
2531				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2532				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2533
2534			} else {
2535				error = EINVAL;
2536			}
2537		}
2538		break;
2539	case SCTP_CMT_ON_OFF:
2540		{
2541			struct sctp_assoc_value *av;
2542
2543			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2544			if (sctp_cmt_on_off) {
2545				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2546				if (stcb) {
2547					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2548					SCTP_TCB_UNLOCK(stcb);
2549				} else {
2550					error = ENOTCONN;
2551				}
2552			} else {
2553				error = ENOPROTOOPT;
2554			}
2555		}
2556		break;
2557		/* JRS - Set socket option for pluggable congestion control */
2558	case SCTP_PLUGGABLE_CC:
2559		{
2560			struct sctp_assoc_value *av;
2561
2562			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2563			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2564			if (stcb) {
2565				switch (av->assoc_value) {
2566					/*
2567					 * JRS - Standard TCP congestion
2568					 * control
2569					 */
2570				case SCTP_CC_RFC2581:
2571					{
2572						stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2573						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2574						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2575						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2576						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2577						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2578						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2579						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2580						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2581						SCTP_TCB_UNLOCK(stcb);
2582						break;
2583					}
2584					/*
2585					 * JRS - High Speed TCP congestion
2586					 * control (Floyd)
2587					 */
2588				case SCTP_CC_HSTCP:
2589					{
2590						stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2591						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2592						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2593						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2594						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2595						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2596						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2597						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2598						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2599						SCTP_TCB_UNLOCK(stcb);
2600						break;
2601					}
2602					/* JRS - HTCP congestion control */
2603				case SCTP_CC_HTCP:
2604					{
2605						stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2606						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2607						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2608						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2609						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2610						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2611						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2612						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2613						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2614						SCTP_TCB_UNLOCK(stcb);
2615						break;
2616					}
2617					/*
2618					 * JRS - All other values are
2619					 * invalid
2620					 */
2621				default:
2622					{
2623						error = EINVAL;
2624						SCTP_TCB_UNLOCK(stcb);
2625						break;
2626					}
2627				}
2628			} else {
2629				switch (av->assoc_value) {
2630				case SCTP_CC_RFC2581:
2631				case SCTP_CC_HSTCP:
2632				case SCTP_CC_HTCP:
2633					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2634					break;
2635				default:
2636					error = EINVAL;
2637					break;
2638				};
2639			}
2640		}
2641		break;
2642	case SCTP_CLR_STAT_LOG:
2643		error = EOPNOTSUPP;
2644		break;
2645	case SCTP_CONTEXT:
2646		{
2647			struct sctp_assoc_value *av;
2648
2649			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2650			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2651
2652			if (stcb) {
2653				stcb->asoc.context = av->assoc_value;
2654				SCTP_TCB_UNLOCK(stcb);
2655			} else {
2656				SCTP_INP_WLOCK(inp);
2657				inp->sctp_context = av->assoc_value;
2658				SCTP_INP_WUNLOCK(inp);
2659			}
2660		}
2661		break;
2662	case SCTP_VRF_ID:
2663		{
2664			uint32_t *default_vrfid;
2665
2666			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2667			if (*default_vrfid > SCTP_MAX_VRF_ID) {
2668				error = EINVAL;
2669				break;
2670			}
2671			inp->def_vrf_id = *default_vrfid;
2672			break;
2673		}
2674	case SCTP_DEL_VRF_ID:
2675		{
2676			error = EOPNOTSUPP;
2677			break;
2678		}
2679	case SCTP_ADD_VRF_ID:
2680		{
2681			error = EOPNOTSUPP;
2682			break;
2683		}
2684	case SCTP_DELAYED_SACK:
2685		{
2686			struct sctp_sack_info *sack;
2687
2688			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2689			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2690			if (sack->sack_delay) {
2691				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2692					sack->sack_delay = SCTP_MAX_SACK_DELAY;
2693			}
2694			if (stcb) {
2695				if (sack->sack_delay) {
2696					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2697						sack->sack_delay = TICKS_TO_MSEC(1);
2698					}
2699					stcb->asoc.delayed_ack = sack->sack_delay;
2700				}
2701				if (sack->sack_freq) {
2702					stcb->asoc.sack_freq = sack->sack_freq;
2703				}
2704				SCTP_TCB_UNLOCK(stcb);
2705			} else {
2706				SCTP_INP_WLOCK(inp);
2707				if (sack->sack_delay) {
2708					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2709						sack->sack_delay = TICKS_TO_MSEC(1);
2710					}
2711					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2712				}
2713				if (sack->sack_freq) {
2714					inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2715				}
2716				SCTP_INP_WUNLOCK(inp);
2717			}
2718			break;
2719		}
2720	case SCTP_AUTH_CHUNK:
2721		{
2722			struct sctp_authchunk *sauth;
2723
2724			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2725
2726			SCTP_INP_WLOCK(inp);
2727			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2728				error = EINVAL;
2729			SCTP_INP_WUNLOCK(inp);
2730			break;
2731		}
2732	case SCTP_AUTH_KEY:
2733		{
2734			struct sctp_authkey *sca;
2735			struct sctp_keyhead *shared_keys;
2736			sctp_sharedkey_t *shared_key;
2737			sctp_key_t *key = NULL;
2738			size_t size;
2739
2740			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2741			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2742			size = optsize - sizeof(*sca);
2743
2744			if (stcb) {
2745				/* set it on the assoc */
2746				shared_keys = &stcb->asoc.shared_keys;
2747				/* clear the cached keys for this key id */
2748				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2749				/*
2750				 * create the new shared key and
2751				 * insert/replace it
2752				 */
2753				if (size > 0) {
2754					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2755					if (key == NULL) {
2756						error = ENOMEM;
2757						SCTP_TCB_UNLOCK(stcb);
2758						break;
2759					}
2760				}
2761				shared_key = sctp_alloc_sharedkey();
2762				if (shared_key == NULL) {
2763					sctp_free_key(key);
2764					error = ENOMEM;
2765					SCTP_TCB_UNLOCK(stcb);
2766					break;
2767				}
2768				shared_key->key = key;
2769				shared_key->keyid = sca->sca_keynumber;
2770				sctp_insert_sharedkey(shared_keys, shared_key);
2771				SCTP_TCB_UNLOCK(stcb);
2772			} else {
2773				/* set it on the endpoint */
2774				SCTP_INP_WLOCK(inp);
2775				shared_keys = &inp->sctp_ep.shared_keys;
2776				/*
2777				 * clear the cached keys on all assocs for
2778				 * this key id
2779				 */
2780				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2781				/*
2782				 * create the new shared key and
2783				 * insert/replace it
2784				 */
2785				if (size > 0) {
2786					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2787					if (key == NULL) {
2788						error = ENOMEM;
2789						SCTP_INP_WUNLOCK(inp);
2790						break;
2791					}
2792				}
2793				shared_key = sctp_alloc_sharedkey();
2794				if (shared_key == NULL) {
2795					sctp_free_key(key);
2796					error = ENOMEM;
2797					SCTP_INP_WUNLOCK(inp);
2798					break;
2799				}
2800				shared_key->key = key;
2801				shared_key->keyid = sca->sca_keynumber;
2802				sctp_insert_sharedkey(shared_keys, shared_key);
2803				SCTP_INP_WUNLOCK(inp);
2804			}
2805			break;
2806		}
2807	case SCTP_HMAC_IDENT:
2808		{
2809			struct sctp_hmacalgo *shmac;
2810			sctp_hmaclist_t *hmaclist;
2811			uint32_t hmacid;
2812			size_t size, i, found;
2813
2814			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2815			size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2816			hmaclist = sctp_alloc_hmaclist(size);
2817			if (hmaclist == NULL) {
2818				error = ENOMEM;
2819				break;
2820			}
2821			for (i = 0; i < size; i++) {
2822				hmacid = shmac->shmac_idents[i];
2823				if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2824					 /* invalid HMACs were found */ ;
2825					error = EINVAL;
2826					sctp_free_hmaclist(hmaclist);
2827					goto sctp_set_hmac_done;
2828				}
2829			}
2830			found = 0;
2831			for (i = 0; i < hmaclist->num_algo; i++) {
2832				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
2833					/* already in list */
2834					found = 1;
2835				}
2836			}
2837			if (!found) {
2838				sctp_free_hmaclist(hmaclist);
2839				error = EINVAL;
2840				break;
2841			}
2842			/* set it on the endpoint */
2843			SCTP_INP_WLOCK(inp);
2844			if (inp->sctp_ep.local_hmacs)
2845				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2846			inp->sctp_ep.local_hmacs = hmaclist;
2847			SCTP_INP_WUNLOCK(inp);
2848	sctp_set_hmac_done:
2849			break;
2850		}
2851	case SCTP_AUTH_ACTIVE_KEY:
2852		{
2853			struct sctp_authkeyid *scact;
2854
2855			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2856			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2857
2858			/* set the active key on the right place */
2859			if (stcb) {
2860				/* set the active key on the assoc */
2861				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2862					error = EINVAL;
2863				SCTP_TCB_UNLOCK(stcb);
2864			} else {
2865				/* set the active key on the endpoint */
2866				SCTP_INP_WLOCK(inp);
2867				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2868					error = EINVAL;
2869				SCTP_INP_WUNLOCK(inp);
2870			}
2871			break;
2872		}
2873	case SCTP_AUTH_DELETE_KEY:
2874		{
2875			struct sctp_authkeyid *scdel;
2876
2877			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2878			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2879
2880			/* delete the key from the right place */
2881			if (stcb) {
2882				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2883					error = EINVAL;
2884				SCTP_TCB_UNLOCK(stcb);
2885			} else {
2886				SCTP_INP_WLOCK(inp);
2887				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2888					error = EINVAL;
2889				SCTP_INP_WUNLOCK(inp);
2890			}
2891			break;
2892		}
2893
2894	case SCTP_RESET_STREAMS:
2895		{
2896			struct sctp_stream_reset *strrst;
2897			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2898			int i;
2899
2900			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2901			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2902
2903			if (stcb == NULL) {
2904				error = ENOENT;
2905				break;
2906			}
2907			if (stcb->asoc.peer_supports_strreset == 0) {
2908				/*
2909				 * Peer does not support it, we return
2910				 * protocol not supported since this is true
2911				 * for this feature and this peer, not the
2912				 * socket request in general.
2913				 */
2914				error = EPROTONOSUPPORT;
2915				SCTP_TCB_UNLOCK(stcb);
2916				break;
2917			}
2918			if (stcb->asoc.stream_reset_outstanding) {
2919				error = EALREADY;
2920				SCTP_TCB_UNLOCK(stcb);
2921				break;
2922			}
2923			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2924				send_in = 1;
2925			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2926				send_out = 1;
2927			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2928				send_in = 1;
2929				send_out = 1;
2930			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2931				send_tsn = 1;
2932			} else {
2933				error = EINVAL;
2934				SCTP_TCB_UNLOCK(stcb);
2935				break;
2936			}
2937			for (i = 0; i < strrst->strrst_num_streams; i++) {
2938				if ((send_in) &&
2939
2940				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2941					error = EINVAL;
2942					goto get_out;
2943				}
2944				if ((send_out) &&
2945				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2946					error = EINVAL;
2947					goto get_out;
2948				}
2949			}
2950			if (error) {
2951		get_out:
2952				SCTP_TCB_UNLOCK(stcb);
2953				break;
2954			}
2955			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2956			    strrst->strrst_list,
2957			    send_out, (stcb->asoc.str_reset_seq_in - 3),
2958			    send_in, send_tsn);
2959
2960			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2961			SCTP_TCB_UNLOCK(stcb);
2962		}
2963		break;
2964
2965	case SCTP_CONNECT_X:
2966		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2967			error = EINVAL;
2968			break;
2969		}
2970		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2971		break;
2972
2973	case SCTP_CONNECT_X_DELAYED:
2974		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2975			error = EINVAL;
2976			break;
2977		}
2978		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2979		break;
2980
2981	case SCTP_CONNECT_X_COMPLETE:
2982		{
2983			struct sockaddr *sa;
2984			struct sctp_nets *net;
2985
2986			/* FIXME MT: check correct? */
2987			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2988
2989			/* find tcb */
2990			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2991				SCTP_INP_RLOCK(inp);
2992				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2993				if (stcb) {
2994					SCTP_TCB_LOCK(stcb);
2995					net = sctp_findnet(stcb, sa);
2996				}
2997				SCTP_INP_RUNLOCK(inp);
2998			} else {
2999				/*
3000				 * We increment here since
3001				 * sctp_findassociation_ep_addr() wil do a
3002				 * decrement if it finds the stcb as long as
3003				 * the locked tcb (last argument) is NOT a
3004				 * TCB.. aka NULL.
3005				 */
3006				SCTP_INP_INCR_REF(inp);
3007				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3008				if (stcb == NULL) {
3009					SCTP_INP_DECR_REF(inp);
3010				}
3011			}
3012
3013			if (stcb == NULL) {
3014				error = ENOENT;
3015				break;
3016			}
3017			if (stcb->asoc.delayed_connection == 1) {
3018				stcb->asoc.delayed_connection = 0;
3019				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3020				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3021				    stcb->asoc.primary_destination,
3022				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3023				sctp_send_initiate(inp, stcb);
3024			} else {
3025				/*
3026				 * already expired or did not use delayed
3027				 * connectx
3028				 */
3029				error = EALREADY;
3030			}
3031			SCTP_TCB_UNLOCK(stcb);
3032		}
3033		break;
3034	case SCTP_MAX_BURST:
3035		{
3036			uint8_t *burst;
3037
3038			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3039
3040			SCTP_INP_WLOCK(inp);
3041			if (*burst) {
3042				inp->sctp_ep.max_burst = *burst;
3043			}
3044			SCTP_INP_WUNLOCK(inp);
3045		}
3046		break;
3047	case SCTP_MAXSEG:
3048		{
3049			struct sctp_assoc_value *av;
3050			int ovh;
3051
3052			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3053			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3054
3055			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3056				ovh = SCTP_MED_OVERHEAD;
3057			} else {
3058				ovh = SCTP_MED_V4_OVERHEAD;
3059			}
3060			if (stcb) {
3061				if (av->assoc_value) {
3062					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3063				} else {
3064					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3065				}
3066				SCTP_TCB_UNLOCK(stcb);
3067			} else {
3068				SCTP_INP_WLOCK(inp);
3069				/*
3070				 * FIXME MT: I think this is not in tune
3071				 * with the API ID
3072				 */
3073				if (av->assoc_value) {
3074					inp->sctp_frag_point = (av->assoc_value + ovh);
3075				} else {
3076					inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3077				}
3078				SCTP_INP_WUNLOCK(inp);
3079			}
3080		}
3081		break;
3082	case SCTP_EVENTS:
3083		{
3084			struct sctp_event_subscribe *events;
3085
3086			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3087
3088			SCTP_INP_WLOCK(inp);
3089			if (events->sctp_data_io_event) {
3090				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3091			} else {
3092				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3093			}
3094
3095			if (events->sctp_association_event) {
3096				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3097			} else {
3098				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3099			}
3100
3101			if (events->sctp_address_event) {
3102				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3103			} else {
3104				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3105			}
3106
3107			if (events->sctp_send_failure_event) {
3108				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3109			} else {
3110				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3111			}
3112
3113			if (events->sctp_peer_error_event) {
3114				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3115			} else {
3116				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3117			}
3118
3119			if (events->sctp_shutdown_event) {
3120				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3121			} else {
3122				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3123			}
3124
3125			if (events->sctp_partial_delivery_event) {
3126				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3127			} else {
3128				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3129			}
3130
3131			if (events->sctp_adaptation_layer_event) {
3132				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3133			} else {
3134				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3135			}
3136
3137			if (events->sctp_authentication_event) {
3138				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3139			} else {
3140				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3141			}
3142
3143			if (events->sctp_stream_reset_events) {
3144				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3145			} else {
3146				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3147			}
3148			SCTP_INP_WUNLOCK(inp);
3149		}
3150		break;
3151
3152	case SCTP_ADAPTATION_LAYER:
3153		{
3154			struct sctp_setadaptation *adap_bits;
3155
3156			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3157			SCTP_INP_WLOCK(inp);
3158			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3159			SCTP_INP_WUNLOCK(inp);
3160		}
3161		break;
3162#ifdef SCTP_DEBUG
3163	case SCTP_SET_INITIAL_DBG_SEQ:
3164		{
3165			uint32_t *vvv;
3166
3167			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3168			SCTP_INP_WLOCK(inp);
3169			inp->sctp_ep.initial_sequence_debug = *vvv;
3170			SCTP_INP_WUNLOCK(inp);
3171		}
3172		break;
3173#endif
3174	case SCTP_DEFAULT_SEND_PARAM:
3175		{
3176			struct sctp_sndrcvinfo *s_info;
3177
3178			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3179			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3180
3181			if (stcb) {
3182				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3183					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3184				} else {
3185					error = EINVAL;
3186				}
3187				SCTP_TCB_UNLOCK(stcb);
3188			} else {
3189				SCTP_INP_WLOCK(inp);
3190				memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3191				SCTP_INP_WUNLOCK(inp);
3192			}
3193		}
3194		break;
3195	case SCTP_PEER_ADDR_PARAMS:
3196		/* Applys to the specific association */
3197		{
3198			struct sctp_paddrparams *paddrp;
3199			struct sctp_nets *net;
3200
3201			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3202			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3203			net = NULL;
3204			if (stcb) {
3205				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3206			} else {
3207				/*
3208				 * We increment here since
3209				 * sctp_findassociation_ep_addr() wil do a
3210				 * decrement if it finds the stcb as long as
3211				 * the locked tcb (last argument) is NOT a
3212				 * TCB.. aka NULL.
3213				 */
3214				SCTP_INP_INCR_REF(inp);
3215				stcb = sctp_findassociation_ep_addr(&inp,
3216				    (struct sockaddr *)&paddrp->spp_address,
3217				    &net, NULL, NULL);
3218				if (stcb == NULL) {
3219					SCTP_INP_DECR_REF(inp);
3220				}
3221			}
3222			/* sanity checks */
3223			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3224				if (stcb)
3225					SCTP_TCB_UNLOCK(stcb);
3226				return (EINVAL);
3227			}
3228			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3229				if (stcb)
3230					SCTP_TCB_UNLOCK(stcb);
3231				return (EINVAL);
3232			}
3233			if (stcb) {
3234				/************************TCB SPECIFIC SET ******************/
3235				/*
3236				 * do we change the timer for HB, we run
3237				 * only one?
3238				 */
3239				int ovh = 0;
3240
3241				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3242					ovh = SCTP_MED_OVERHEAD;
3243				} else {
3244					ovh = SCTP_MED_V4_OVERHEAD;
3245				}
3246
3247				if (paddrp->spp_hbinterval)
3248					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3249				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3250					stcb->asoc.heart_beat_delay = 0;
3251
3252				/* network sets ? */
3253				if (net) {
3254					/************************NET SPECIFIC SET ******************/
3255					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3256						/* on demand HB */
3257						if (sctp_send_hb(stcb, 1, net) < 0) {
3258							/* asoc destroyed */
3259							error = EINVAL;
3260							break;
3261						}
3262					}
3263					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3264						net->dest_state |= SCTP_ADDR_NOHB;
3265					}
3266					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3267						net->dest_state &= ~SCTP_ADDR_NOHB;
3268					}
3269					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3270						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3271							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3272							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3273						}
3274						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3275							net->mtu = paddrp->spp_pathmtu + ovh;
3276							if (net->mtu < stcb->asoc.smallest_mtu) {
3277#ifdef SCTP_PRINT_FOR_B_AND_M
3278								SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3279								    net->mtu);
3280#endif
3281								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3282							}
3283						}
3284					}
3285					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3286						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3287							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3288						}
3289					}
3290					if (paddrp->spp_pathmaxrxt)
3291						net->failure_threshold = paddrp->spp_pathmaxrxt;
3292#ifdef INET
3293					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3294						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3295							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3296						}
3297					}
3298#endif
3299#ifdef INET6
3300					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3301						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3302							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3303						}
3304					}
3305#endif
3306				} else {
3307					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3308					if (paddrp->spp_pathmaxrxt)
3309						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3310
3311					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3312						/* Turn back on the timer */
3313						stcb->asoc.hb_is_disabled = 0;
3314						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3315					}
3316					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3317						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3318							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3319								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3320								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3321							}
3322							if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3323								net->mtu = paddrp->spp_pathmtu + ovh;
3324								if (net->mtu < stcb->asoc.smallest_mtu) {
3325#ifdef SCTP_PRINT_FOR_B_AND_M
3326									SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3327									    net->mtu);
3328#endif
3329									sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3330								}
3331							}
3332						}
3333					}
3334					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3335						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3336							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3337								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3338							}
3339						}
3340					}
3341					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3342						int cnt_of_unconf = 0;
3343						struct sctp_nets *lnet;
3344
3345						stcb->asoc.hb_is_disabled = 1;
3346						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3347							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3348								cnt_of_unconf++;
3349							}
3350						}
3351						/*
3352						 * stop the timer ONLY if we
3353						 * have no unconfirmed
3354						 * addresses
3355						 */
3356						if (cnt_of_unconf == 0) {
3357							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3358								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3359								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3360							}
3361						}
3362					}
3363					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3364						/* start up the timer. */
3365						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3366							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3367						}
3368					}
3369#ifdef INET
3370					if (paddrp->spp_flags & SPP_IPV4_TOS)
3371						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3372#endif
3373#ifdef INET6
3374					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3375						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3376#endif
3377
3378				}
3379				SCTP_TCB_UNLOCK(stcb);
3380			} else {
3381				/************************NO TCB, SET TO default stuff ******************/
3382				SCTP_INP_WLOCK(inp);
3383				/*
3384				 * For the TOS/FLOWLABEL stuff you set it
3385				 * with the options on the socket
3386				 */
3387				if (paddrp->spp_pathmaxrxt) {
3388					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3389				}
3390				if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3391					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3392				else if (paddrp->spp_hbinterval) {
3393					if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3394						paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3395					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3396				}
3397				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3398					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3399
3400				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3401					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3402				}
3403				SCTP_INP_WUNLOCK(inp);
3404			}
3405		}
3406		break;
3407	case SCTP_RTOINFO:
3408		{
3409			struct sctp_rtoinfo *srto;
3410			uint32_t new_init, new_min, new_max;
3411
3412			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3413			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3414
3415			if (stcb) {
3416				if (srto->srto_initial)
3417					new_init = srto->srto_initial;
3418				else
3419					new_init = stcb->asoc.initial_rto;
3420				if (srto->srto_max)
3421					new_max = srto->srto_max;
3422				else
3423					new_max = stcb->asoc.maxrto;
3424				if (srto->srto_min)
3425					new_min = srto->srto_min;
3426				else
3427					new_min = stcb->asoc.minrto;
3428				if ((new_min <= new_init) && (new_init <= new_max)) {
3429					stcb->asoc.initial_rto = new_init;
3430					stcb->asoc.maxrto = new_max;
3431					stcb->asoc.minrto = new_min;
3432				} else {
3433					error = EDOM;
3434				}
3435				SCTP_TCB_UNLOCK(stcb);
3436			} else {
3437				SCTP_INP_WLOCK(inp);
3438				if (srto->srto_initial)
3439					new_init = srto->srto_initial;
3440				else
3441					new_init = inp->sctp_ep.initial_rto;
3442				if (srto->srto_max)
3443					new_max = srto->srto_max;
3444				else
3445					new_max = inp->sctp_ep.sctp_maxrto;
3446				if (srto->srto_min)
3447					new_min = srto->srto_min;
3448				else
3449					new_min = inp->sctp_ep.sctp_minrto;
3450				if ((new_min <= new_init) && (new_init <= new_max)) {
3451					inp->sctp_ep.initial_rto = new_init;
3452					inp->sctp_ep.sctp_maxrto = new_max;
3453					inp->sctp_ep.sctp_minrto = new_min;
3454				} else {
3455					error = EDOM;
3456				}
3457				SCTP_INP_WUNLOCK(inp);
3458			}
3459		}
3460		break;
3461	case SCTP_ASSOCINFO:
3462		{
3463			struct sctp_assocparams *sasoc;
3464
3465			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3466			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3467			if (sasoc->sasoc_cookie_life) {
3468				/* boundary check the cookie life */
3469				if (sasoc->sasoc_cookie_life < 1000)
3470					sasoc->sasoc_cookie_life = 1000;
3471				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3472					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3473				}
3474			}
3475			if (stcb) {
3476				if (sasoc->sasoc_asocmaxrxt)
3477					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3478				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3479				sasoc->sasoc_peer_rwnd = 0;
3480				sasoc->sasoc_local_rwnd = 0;
3481				if (sasoc->sasoc_cookie_life) {
3482					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3483				}
3484				SCTP_TCB_UNLOCK(stcb);
3485			} else {
3486				SCTP_INP_WLOCK(inp);
3487				if (sasoc->sasoc_asocmaxrxt)
3488					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3489				sasoc->sasoc_number_peer_destinations = 0;
3490				sasoc->sasoc_peer_rwnd = 0;
3491				sasoc->sasoc_local_rwnd = 0;
3492				if (sasoc->sasoc_cookie_life) {
3493					inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3494				}
3495				SCTP_INP_WUNLOCK(inp);
3496			}
3497		}
3498		break;
3499	case SCTP_INITMSG:
3500		{
3501			struct sctp_initmsg *sinit;
3502
3503			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3504			SCTP_INP_WLOCK(inp);
3505			if (sinit->sinit_num_ostreams)
3506				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3507
3508			if (sinit->sinit_max_instreams)
3509				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3510
3511			if (sinit->sinit_max_attempts)
3512				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3513
3514			if (sinit->sinit_max_init_timeo)
3515				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3516			SCTP_INP_WUNLOCK(inp);
3517		}
3518		break;
3519	case SCTP_PRIMARY_ADDR:
3520		{
3521			struct sctp_setprim *spa;
3522			struct sctp_nets *net, *lnet;
3523
3524			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3525			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3526
3527			net = NULL;
3528			if (stcb) {
3529				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3530			} else {
3531				/*
3532				 * We increment here since
3533				 * sctp_findassociation_ep_addr() wil do a
3534				 * decrement if it finds the stcb as long as
3535				 * the locked tcb (last argument) is NOT a
3536				 * TCB.. aka NULL.
3537				 */
3538				SCTP_INP_INCR_REF(inp);
3539				stcb = sctp_findassociation_ep_addr(&inp,
3540				    (struct sockaddr *)&spa->ssp_addr,
3541				    &net, NULL, NULL);
3542				if (stcb == NULL) {
3543					SCTP_INP_DECR_REF(inp);
3544				}
3545			}
3546
3547			if ((stcb) && (net)) {
3548				if ((net != stcb->asoc.primary_destination) &&
3549				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3550					/* Ok we need to set it */
3551					lnet = stcb->asoc.primary_destination;
3552					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3553						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3554							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3555						}
3556						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3557					}
3558				}
3559			} else {
3560				error = EINVAL;
3561			}
3562			if (stcb) {
3563				SCTP_TCB_UNLOCK(stcb);
3564			}
3565		}
3566		break;
3567	case SCTP_SET_DYNAMIC_PRIMARY:
3568		{
3569			union sctp_sockstore *ss;
3570
3571			error = priv_check(curthread,
3572			    PRIV_NETINET_RESERVEDPORT);
3573			if (error)
3574				break;
3575
3576			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3577			/* SUPER USER CHECK? */
3578			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3579		}
3580		break;
3581	case SCTP_SET_PEER_PRIMARY_ADDR:
3582		{
3583			struct sctp_setpeerprim *sspp;
3584
3585			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3586			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3587			if (stcb != NULL) {
3588				struct sctp_ifa *ifa;
3589
3590				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3591				    stcb->asoc.vrf_id, 0);
3592				if (ifa == NULL) {
3593					error = EINVAL;
3594					goto out_of_it;
3595				}
3596				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3597					/*
3598					 * Must validate the ifa found is in
3599					 * our ep
3600					 */
3601					struct sctp_laddr *laddr;
3602					int found = 0;
3603
3604					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3605						if (laddr->ifa == NULL) {
3606							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3607							    __FUNCTION__);
3608							continue;
3609						}
3610						if (laddr->ifa == ifa) {
3611							found = 1;
3612							break;
3613						}
3614					}
3615					if (!found) {
3616						error = EINVAL;
3617						goto out_of_it;
3618					}
3619				}
3620				if (sctp_set_primary_ip_address_sa(stcb,
3621				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
3622					error = EINVAL;
3623				}
3624		out_of_it:
3625				SCTP_TCB_UNLOCK(stcb);
3626			} else {
3627				error = EINVAL;
3628			}
3629
3630		}
3631		break;
3632	case SCTP_BINDX_ADD_ADDR:
3633		{
3634			struct sctp_getaddresses *addrs;
3635			size_t sz;
3636			struct thread *td;
3637			int prison = 0;
3638
3639			td = (struct thread *)p;
3640			if (jailed(td->td_ucred)) {
3641				prison = 1;
3642			}
3643			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3644			    optsize);
3645			if (addrs->addr->sa_family == AF_INET) {
3646				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3647				if (optsize < sz) {
3648					error = EINVAL;
3649					break;
3650				}
3651				if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3652					error = EADDRNOTAVAIL;
3653				}
3654			} else if (addrs->addr->sa_family == AF_INET6) {
3655				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3656				if (optsize < sz) {
3657					error = EINVAL;
3658					break;
3659				}
3660				/* JAIL XXXX Add else here for V6 */
3661			}
3662			sctp_bindx_add_address(so, inp, addrs->addr,
3663			    addrs->sget_assoc_id, vrf_id,
3664			    &error, p);
3665		}
3666		break;
3667	case SCTP_BINDX_REM_ADDR:
3668		{
3669			struct sctp_getaddresses *addrs;
3670			size_t sz;
3671			struct thread *td;
3672			int prison = 0;
3673
3674			td = (struct thread *)p;
3675			if (jailed(td->td_ucred)) {
3676				prison = 1;
3677			}
3678			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3679			if (addrs->addr->sa_family == AF_INET) {
3680				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3681				if (optsize < sz) {
3682					error = EINVAL;
3683					break;
3684				}
3685				if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3686					error = EADDRNOTAVAIL;
3687				}
3688			} else if (addrs->addr->sa_family == AF_INET6) {
3689				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3690				if (optsize < sz) {
3691					error = EINVAL;
3692					break;
3693				}
3694				/* JAIL XXXX Add else here for V6 */
3695			}
3696			sctp_bindx_delete_address(so, inp, addrs->addr,
3697			    addrs->sget_assoc_id, vrf_id,
3698			    &error);
3699		}
3700		break;
3701	default:
3702		error = ENOPROTOOPT;
3703		break;
3704	}			/* end switch (opt) */
3705	return (error);
3706}
3707
3708
3709int
3710sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3711{
3712	void *optval = NULL;
3713	size_t optsize = 0;
3714	struct sctp_inpcb *inp;
3715	void *p;
3716	int error = 0;
3717
3718	inp = (struct sctp_inpcb *)so->so_pcb;
3719	if (inp == 0) {
3720		/* I made the same as TCP since we are not setup? */
3721		return (ECONNRESET);
3722	}
3723	if (sopt->sopt_level != IPPROTO_SCTP) {
3724		/* wrong proto level... send back up to IP */
3725#ifdef INET6
3726		if (INP_CHECK_SOCKAF(so, AF_INET6))
3727			error = ip6_ctloutput(so, sopt);
3728		else
3729#endif				/* INET6 */
3730			error = ip_ctloutput(so, sopt);
3731		return (error);
3732	}
3733	optsize = sopt->sopt_valsize;
3734	if (optsize) {
3735		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
3736		if (optval == NULL) {
3737			return (ENOBUFS);
3738		}
3739		error = sooptcopyin(sopt, optval, optsize, optsize);
3740		if (error) {
3741			SCTP_FREE(optval, SCTP_M_SOCKOPT);
3742			goto out;
3743		}
3744	}
3745	p = (void *)sopt->sopt_td;
3746	if (sopt->sopt_dir == SOPT_SET) {
3747		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3748	} else if (sopt->sopt_dir == SOPT_GET) {
3749		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3750	} else {
3751		error = EINVAL;
3752	}
3753	if ((error == 0) && (optval != NULL)) {
3754		error = sooptcopyout(sopt, optval, optsize);
3755		SCTP_FREE(optval, SCTP_M_SOCKOPT);
3756	} else if (optval != NULL) {
3757		SCTP_FREE(optval, SCTP_M_SOCKOPT);
3758	}
3759out:
3760	return (error);
3761}
3762
3763
3764static int
3765sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3766{
3767	int error = 0;
3768	int create_lock_on = 0;
3769	uint32_t vrf_id;
3770	struct sctp_inpcb *inp;
3771	struct sctp_tcb *stcb = NULL;
3772
3773	inp = (struct sctp_inpcb *)so->so_pcb;
3774	if (inp == 0) {
3775		/* I made the same as TCP since we are not setup? */
3776		return (ECONNRESET);
3777	}
3778	if (addr == NULL)
3779		return EINVAL;
3780
3781	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
3782		return (EINVAL);
3783	}
3784	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
3785		return (EINVAL);
3786	}
3787	SCTP_ASOC_CREATE_LOCK(inp);
3788	create_lock_on = 1;
3789
3790	SCTP_INP_INCR_REF(inp);
3791	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3792	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3793		/* Should I really unlock ? */
3794		error = EFAULT;
3795		goto out_now;
3796	}
3797#ifdef INET6
3798	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3799	    (addr->sa_family == AF_INET6)) {
3800		error = EINVAL;
3801		goto out_now;
3802	}
3803#endif				/* INET6 */
3804	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3805	    SCTP_PCB_FLAGS_UNBOUND) {
3806		/* Bind a ephemeral port */
3807		error = sctp_inpcb_bind(so, NULL, NULL, p);
3808		if (error) {
3809			goto out_now;
3810		}
3811	}
3812	/* Now do we connect? */
3813	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3814		error = EINVAL;
3815		goto out_now;
3816	}
3817	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3818	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3819		/* We are already connected AND the TCP model */
3820		error = EADDRINUSE;
3821		goto out_now;
3822	}
3823	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3824		SCTP_INP_RLOCK(inp);
3825		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3826		SCTP_INP_RUNLOCK(inp);
3827	} else {
3828		/*
3829		 * We increment here since sctp_findassociation_ep_addr()
3830		 * wil do a decrement if it finds the stcb as long as the
3831		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3832		 */
3833		SCTP_INP_INCR_REF(inp);
3834		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3835		if (stcb == NULL) {
3836			SCTP_INP_DECR_REF(inp);
3837		} else {
3838			SCTP_TCB_LOCK(stcb);
3839		}
3840	}
3841	if (stcb != NULL) {
3842		/* Already have or am bring up an association */
3843		error = EALREADY;
3844		goto out_now;
3845	}
3846	vrf_id = inp->def_vrf_id;
3847	/* We are GOOD to go */
3848	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
3849	if (stcb == NULL) {
3850		/* Gak! no memory */
3851		goto out_now;
3852	}
3853	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3854		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3855		/* Set the connected flag so we can queue data */
3856		soisconnecting(so);
3857	}
3858	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3859	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3860
3861	/* initialize authentication parameters for the assoc */
3862	sctp_initialize_auth_params(inp, stcb);
3863
3864	sctp_send_initiate(inp, stcb);
3865	SCTP_TCB_UNLOCK(stcb);
3866out_now:
3867	if (create_lock_on) {
3868		SCTP_ASOC_CREATE_UNLOCK(inp);
3869	}
3870	SCTP_INP_DECR_REF(inp);
3871	return error;
3872}
3873
3874int
3875sctp_listen(struct socket *so, int backlog, struct thread *p)
3876{
3877	/*
3878	 * Note this module depends on the protocol processing being called
3879	 * AFTER any socket level flags and backlog are applied to the
3880	 * socket. The traditional way that the socket flags are applied is
3881	 * AFTER protocol processing. We have made a change to the
3882	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3883	 * place if the socket API for SCTP is to work properly.
3884	 */
3885
3886	int error = 0;
3887	struct sctp_inpcb *inp;
3888
3889	inp = (struct sctp_inpcb *)so->so_pcb;
3890	if (inp == 0) {
3891		/* I made the same as TCP since we are not setup? */
3892		return (ECONNRESET);
3893	}
3894	SCTP_INP_RLOCK(inp);
3895#ifdef SCTP_LOCK_LOGGING
3896	if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
3897		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3898	}
3899#endif
3900	SOCK_LOCK(so);
3901	error = solisten_proto_check(so);
3902	if (error) {
3903		SOCK_UNLOCK(so);
3904		SCTP_INP_RUNLOCK(inp);
3905		return (error);
3906	}
3907	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3908	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3909		/* We are already connected AND the TCP model */
3910		SCTP_INP_RUNLOCK(inp);
3911		SOCK_UNLOCK(so);
3912		return (EADDRINUSE);
3913	}
3914	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3915		/* We must do a bind. */
3916		SOCK_UNLOCK(so);
3917		SCTP_INP_RUNLOCK(inp);
3918		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
3919			/* bind error, probably perm */
3920			return (error);
3921		}
3922		SOCK_LOCK(so);
3923	} else {
3924		SCTP_INP_RUNLOCK(inp);
3925	}
3926	/* It appears for 7.0 and on, we must always call this. */
3927	solisten_proto(so, backlog);
3928	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3929		/* remove the ACCEPTCONN flag for one-to-many sockets */
3930		so->so_options &= ~SO_ACCEPTCONN;
3931	}
3932	if (backlog == 0) {
3933		/* turning off listen */
3934		so->so_options &= ~SO_ACCEPTCONN;
3935	}
3936	SOCK_UNLOCK(so);
3937	return (error);
3938}
3939
3940static int sctp_defered_wakeup_cnt = 0;
3941
3942int
3943sctp_accept(struct socket *so, struct sockaddr **addr)
3944{
3945	struct sctp_tcb *stcb;
3946	struct sctp_inpcb *inp;
3947	union sctp_sockstore store;
3948
3949	int error;
3950
3951	inp = (struct sctp_inpcb *)so->so_pcb;
3952
3953	if (inp == 0) {
3954		return (ECONNRESET);
3955	}
3956	SCTP_INP_RLOCK(inp);
3957	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3958		SCTP_INP_RUNLOCK(inp);
3959		return (ENOTSUP);
3960	}
3961	if (so->so_state & SS_ISDISCONNECTED) {
3962		SCTP_INP_RUNLOCK(inp);
3963		return (ECONNABORTED);
3964	}
3965	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3966	if (stcb == NULL) {
3967		SCTP_INP_RUNLOCK(inp);
3968		return (ECONNRESET);
3969	}
3970	SCTP_TCB_LOCK(stcb);
3971	SCTP_INP_RUNLOCK(inp);
3972	store = stcb->asoc.primary_destination->ro._l_addr;
3973	SCTP_TCB_UNLOCK(stcb);
3974	if (store.sa.sa_family == AF_INET) {
3975		struct sockaddr_in *sin;
3976
3977		SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3978		sin->sin_family = AF_INET;
3979		sin->sin_len = sizeof(*sin);
3980		sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3981		sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3982		*addr = (struct sockaddr *)sin;
3983	} else {
3984		struct sockaddr_in6 *sin6;
3985
3986		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3987		sin6->sin6_family = AF_INET6;
3988		sin6->sin6_len = sizeof(*sin6);
3989		sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3990
3991		sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3992		if ((error = sa6_recoverscope(sin6)) != 0) {
3993			SCTP_FREE_SONAME(sin6);
3994			return (error);
3995		}
3996		*addr = (struct sockaddr *)sin6;
3997	}
3998	/* Wake any delayed sleep action */
3999	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4000		SCTP_INP_WLOCK(inp);
4001		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4002		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4003			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4004			SCTP_INP_WUNLOCK(inp);
4005			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4006			if (sowriteable(inp->sctp_socket)) {
4007				sowwakeup_locked(inp->sctp_socket);
4008			} else {
4009				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4010			}
4011			SCTP_INP_WLOCK(inp);
4012		}
4013		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4014			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4015			SCTP_INP_WUNLOCK(inp);
4016			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4017			if (soreadable(inp->sctp_socket)) {
4018				sctp_defered_wakeup_cnt++;
4019				sorwakeup_locked(inp->sctp_socket);
4020			} else {
4021				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4022			}
4023			SCTP_INP_WLOCK(inp);
4024		}
4025		SCTP_INP_WUNLOCK(inp);
4026	}
4027	return (0);
4028}
4029
4030int
4031sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4032{
4033	struct sockaddr_in *sin;
4034	uint32_t vrf_id;
4035	struct sctp_inpcb *inp;
4036	struct sctp_ifa *sctp_ifa;
4037
4038	/*
4039	 * Do the malloc first in case it blocks.
4040	 */
4041	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4042	sin->sin_family = AF_INET;
4043	sin->sin_len = sizeof(*sin);
4044	inp = (struct sctp_inpcb *)so->so_pcb;
4045	if (!inp) {
4046		SCTP_FREE_SONAME(sin);
4047		return ECONNRESET;
4048	}
4049	SCTP_INP_RLOCK(inp);
4050	sin->sin_port = inp->sctp_lport;
4051	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4052		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4053			struct sctp_tcb *stcb;
4054			struct sockaddr_in *sin_a;
4055			struct sctp_nets *net;
4056			int fnd;
4057
4058			stcb = LIST_FIRST(&inp->sctp_asoc_list);
4059			if (stcb == NULL) {
4060				goto notConn;
4061			}
4062			fnd = 0;
4063			sin_a = NULL;
4064			SCTP_TCB_LOCK(stcb);
4065			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4066				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4067				if (sin_a == NULL)
4068					/* this will make coverity happy */
4069					continue;
4070
4071				if (sin_a->sin_family == AF_INET) {
4072					fnd = 1;
4073					break;
4074				}
4075			}
4076			if ((!fnd) || (sin_a == NULL)) {
4077				/* punt */
4078				SCTP_TCB_UNLOCK(stcb);
4079				goto notConn;
4080			}
4081			vrf_id = inp->def_vrf_id;
4082			sctp_ifa = sctp_source_address_selection(inp,
4083			    stcb,
4084			    (sctp_route_t *) & net->ro,
4085			    net, 0, vrf_id);
4086			if (sctp_ifa) {
4087				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4088				sctp_free_ifa(sctp_ifa);
4089			}
4090			SCTP_TCB_UNLOCK(stcb);
4091		} else {
4092			/* For the bound all case you get back 0 */
4093	notConn:
4094			sin->sin_addr.s_addr = 0;
4095		}
4096
4097	} else {
4098		/* Take the first IPv4 address in the list */
4099		struct sctp_laddr *laddr;
4100		int fnd = 0;
4101
4102		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4103			if (laddr->ifa->address.sa.sa_family == AF_INET) {
4104				struct sockaddr_in *sin_a;
4105
4106				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4107				sin->sin_addr = sin_a->sin_addr;
4108				fnd = 1;
4109				break;
4110			}
4111		}
4112		if (!fnd) {
4113			SCTP_FREE_SONAME(sin);
4114			SCTP_INP_RUNLOCK(inp);
4115			return ENOENT;
4116		}
4117	}
4118	SCTP_INP_RUNLOCK(inp);
4119	(*addr) = (struct sockaddr *)sin;
4120	return (0);
4121}
4122
4123int
4124sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4125{
4126	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4127	int fnd;
4128	struct sockaddr_in *sin_a;
4129	struct sctp_inpcb *inp;
4130	struct sctp_tcb *stcb;
4131	struct sctp_nets *net;
4132
4133	/* Do the malloc first in case it blocks. */
4134	inp = (struct sctp_inpcb *)so->so_pcb;
4135	if ((inp == NULL) ||
4136	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4137		/* UDP type and listeners will drop out here */
4138		return (ENOTCONN);
4139	}
4140	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4141	sin->sin_family = AF_INET;
4142	sin->sin_len = sizeof(*sin);
4143
4144	/* We must recapture incase we blocked */
4145	inp = (struct sctp_inpcb *)so->so_pcb;
4146	if (!inp) {
4147		SCTP_FREE_SONAME(sin);
4148		return ECONNRESET;
4149	}
4150	SCTP_INP_RLOCK(inp);
4151	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4152	if (stcb) {
4153		SCTP_TCB_LOCK(stcb);
4154	}
4155	SCTP_INP_RUNLOCK(inp);
4156	if (stcb == NULL) {
4157		SCTP_FREE_SONAME(sin);
4158		return ECONNRESET;
4159	}
4160	fnd = 0;
4161	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4162		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4163		if (sin_a->sin_family == AF_INET) {
4164			fnd = 1;
4165			sin->sin_port = stcb->rport;
4166			sin->sin_addr = sin_a->sin_addr;
4167			break;
4168		}
4169	}
4170	SCTP_TCB_UNLOCK(stcb);
4171	if (!fnd) {
4172		/* No IPv4 address */
4173		SCTP_FREE_SONAME(sin);
4174		return ENOENT;
4175	}
4176	(*addr) = (struct sockaddr *)sin;
4177	return (0);
4178}
4179
4180struct pr_usrreqs sctp_usrreqs = {
4181	.pru_abort = sctp_abort,
4182	.pru_accept = sctp_accept,
4183	.pru_attach = sctp_attach,
4184	.pru_bind = sctp_bind,
4185	.pru_connect = sctp_connect,
4186	.pru_control = in_control,
4187	.pru_close = sctp_close,
4188	.pru_detach = sctp_close,
4189	.pru_sopoll = sopoll_generic,
4190	.pru_disconnect = sctp_disconnect,
4191	.pru_listen = sctp_listen,
4192	.pru_peeraddr = sctp_peeraddr,
4193	.pru_send = sctp_sendm,
4194	.pru_shutdown = sctp_shutdown,
4195	.pru_sockaddr = sctp_ingetaddr,
4196	.pru_sosend = sctp_sosend,
4197	.pru_soreceive = sctp_soreceive
4198};
4199