2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_var.h>
44 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/udp.h>
57 extern struct sctp_cc_functions sctp_cc_functions[];
58 extern struct sctp_ss_functions sctp_ss_functions[];
65 /* Initialize and modify the sysctled variables */
67 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
68 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
70 * Allow a user to take no more than 1/2 the number of clusters or
71 * the SB_MAX whichever is smaller for the send window.
73 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
74 SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
75 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
77 * Now for the recv window, should we take the same amount? or
78 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
79 * now I will just copy.
81 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
83 SCTP_BASE_VAR(first_time) = 0;
84 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
86 #if defined(SCTP_PACKET_LOGGING)
87 SCTP_BASE_VAR(packet_log_writers) = 0;
88 SCTP_BASE_VAR(packet_log_end) = 0;
89 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
104 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
105 struct sctp_tcb *stcb,
106 struct sctp_nets *net,
109 struct sctp_tmit_chunk *chk;
112 /* Adjust that too */
113 stcb->asoc.smallest_mtu = nxtsz;
114 /* now off to subtract IP_DF flag if needed */
115 overhead = IP_HDR_SIZE;
116 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
117 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
119 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
120 if ((chk->send_size + overhead) > nxtsz) {
121 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
124 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
125 if ((chk->send_size + overhead) > nxtsz) {
127 * For this guy we also mark for immediate resend
128 * since we sent to big of chunk
130 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
131 if (chk->sent < SCTP_DATAGRAM_RESEND) {
132 sctp_flight_size_decrease(chk);
133 sctp_total_flight_decrease(stcb, chk);
135 if (chk->sent != SCTP_DATAGRAM_RESEND) {
136 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
138 chk->sent = SCTP_DATAGRAM_RESEND;
139 chk->rec.data.doing_fast_retransmit = 0;
140 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
141 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
142 chk->whoTo->flight_size,
144 (uintptr_t) chk->whoTo,
145 chk->rec.data.TSN_seq);
147 /* Clear any time so NO RTT is being done */
154 sctp_notify_mbuf(struct sctp_inpcb *inp,
155 struct sctp_tcb *stcb,
156 struct sctp_nets *net,
161 int totsz, tmr_stopped = 0;
165 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
166 (ip == NULL) || (sh == NULL)) {
168 SCTP_TCB_UNLOCK(stcb);
172 /* First job is to verify the vtag matches what I would send */
173 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
174 SCTP_TCB_UNLOCK(stcb);
177 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
179 if (icmph->icmp_type != ICMP_UNREACH) {
180 /* We only care about unreachable */
181 SCTP_TCB_UNLOCK(stcb);
184 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
185 /* not a unreachable message due to frag. */
186 SCTP_TCB_UNLOCK(stcb);
191 nxtsz = ntohs(icmph->icmp_nextmtu);
194 * old type router that does not tell us what the next size
195 * mtu is. Rats we will have to guess (in a educated fashion
198 nxtsz = sctp_get_prev_mtu(totsz);
200 /* Stop any PMTU timer */
201 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
203 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
204 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
206 /* Adjust destination size limit */
207 if (net->mtu > nxtsz) {
210 net->mtu -= sizeof(struct udphdr);
213 /* now what about the ep? */
214 if (stcb->asoc.smallest_mtu > nxtsz) {
215 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
218 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
220 SCTP_TCB_UNLOCK(stcb);
225 sctp_notify(struct sctp_inpcb *inp,
229 struct sctp_tcb *stcb,
230 struct sctp_nets *net)
232 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
241 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
242 (sh == NULL) || (to == NULL)) {
244 SCTP_TCB_UNLOCK(stcb);
247 /* First job is to verify the vtag matches what I would send */
248 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
249 SCTP_TCB_UNLOCK(stcb);
252 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
254 if (icmph->icmp_type != ICMP_UNREACH) {
255 /* We only care about unreachable */
256 SCTP_TCB_UNLOCK(stcb);
259 if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
260 (icmph->icmp_code == ICMP_UNREACH_HOST) ||
261 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
262 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
263 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
264 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
265 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
266 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
269 * Hmm reachablity problems we must examine closely. If its
270 * not reachable, we may have lost a network. Or if there is
271 * NO protocol at the other end named SCTP. well we consider
274 if (net->dest_state & SCTP_ADDR_REACHABLE) {
275 /* Ok that destination is NOT reachable */
276 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
278 net->failure_threshold,
281 net->dest_state &= ~SCTP_ADDR_REACHABLE;
282 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
284 * JRS 5/14/07 - If a destination is unreachable,
285 * the PF bit is turned off. This allows an
286 * unambiguous use of the PF bit for destinations
287 * that are reachable but potentially failed. If the
288 * destination is set to the unreachable state, also
289 * set the destination to the PF state.
292 * Add debug message here if destination is not in
295 /* Stop any running T3 timers here? */
296 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
297 (stcb->asoc.sctp_cmt_pf > 0)) {
298 net->dest_state &= ~SCTP_ADDR_PF;
299 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
302 net->error_count = net->failure_threshold + 1;
303 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
304 stcb, SCTP_FAILED_THRESHOLD,
305 (void *)net, SCTP_SO_NOT_LOCKED);
307 SCTP_TCB_UNLOCK(stcb);
308 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
309 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
311 * Here the peer is either playing tricks on us, including
312 * an address that belongs to someone who does not support
313 * SCTP OR was a userland implementation that shutdown and
314 * now is dead. In either case treat it like a OOTB abort
317 reason = SCTP_PEER_FAULTY;
318 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
319 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
320 so = SCTP_INP_SO(inp);
321 atomic_add_int(&stcb->asoc.refcnt, 1);
322 SCTP_TCB_UNLOCK(stcb);
323 SCTP_SOCKET_LOCK(so, 1);
325 atomic_subtract_int(&stcb->asoc.refcnt, 1);
327 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
328 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
329 SCTP_SOCKET_UNLOCK(so, 1);
330 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
332 /* no need to unlock here, since the TCB is gone */
334 SCTP_TCB_UNLOCK(stcb);
339 sctp_ctlinput(cmd, sa, vip)
348 /* FIX, for non-bsd is this right? */
349 vrf_id = SCTP_DEFAULT_VRFID;
350 if (sa->sa_family != AF_INET ||
351 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
354 if (PRC_IS_REDIRECT(cmd)) {
356 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
360 struct sctp_inpcb *inp = NULL;
361 struct sctp_tcb *stcb = NULL;
362 struct sctp_nets *net = NULL;
363 struct sockaddr_in to, from;
365 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
366 bzero(&to, sizeof(to));
367 bzero(&from, sizeof(from));
368 from.sin_family = to.sin_family = AF_INET;
369 from.sin_len = to.sin_len = sizeof(to);
370 from.sin_port = sh->src_port;
371 from.sin_addr = ip->ip_src;
372 to.sin_port = sh->dest_port;
373 to.sin_addr = ip->ip_dst;
376 * 'to' holds the dest of the packet that failed to be sent.
377 * 'from' holds our local endpoint address. Thus we reverse
378 * the to and the from in the lookup.
380 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
381 (struct sockaddr *)&to,
382 &inp, &net, 1, vrf_id);
383 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
384 if (cmd != PRC_MSGSIZE) {
385 sctp_notify(inp, ip, sh,
386 (struct sockaddr *)&to, stcb,
389 /* handle possible ICMP size messages */
390 sctp_notify_mbuf(inp, stcb, net, ip, sh);
393 if ((stcb == NULL) && (inp != NULL)) {
394 /* reduce ref-count */
396 SCTP_INP_DECR_REF(inp);
397 SCTP_INP_WUNLOCK(inp);
400 SCTP_TCB_UNLOCK(stcb);
408 sctp_getcred(SYSCTL_HANDLER_ARGS)
411 struct sockaddr_in addrs[2];
412 struct sctp_inpcb *inp;
413 struct sctp_nets *net;
414 struct sctp_tcb *stcb;
418 /* FIX, for non-bsd is this right? */
419 vrf_id = SCTP_DEFAULT_VRFID;
421 error = priv_check(req->td, PRIV_NETINET_GETCRED);
426 error = SYSCTL_IN(req, addrs, sizeof(addrs));
430 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
432 &inp, &net, 1, vrf_id);
433 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
434 if ((inp != NULL) && (stcb == NULL)) {
435 /* reduce ref-count */
437 SCTP_INP_DECR_REF(inp);
440 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
444 SCTP_TCB_UNLOCK(stcb);
446 * We use the write lock here, only since in the error leg we need
447 * it. If we used RLOCK, then we would have to
448 * wlock/decr/unlock/rlock. Which in theory could create a hole.
449 * Better to use higher wlock.
453 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
455 SCTP_INP_WUNLOCK(inp);
458 cru2x(inp->sctp_socket->so_cred, &xuc);
459 SCTP_INP_WUNLOCK(inp);
460 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
465 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
466 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
470 sctp_abort(struct socket *so)
472 struct sctp_inpcb *inp;
475 inp = (struct sctp_inpcb *)so->so_pcb;
480 flags = inp->sctp_flags;
481 #ifdef SCTP_LOG_CLOSING
482 sctp_log_closing(inp, NULL, 17);
484 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
485 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
486 #ifdef SCTP_LOG_CLOSING
487 sctp_log_closing(inp, NULL, 16);
489 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
490 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
492 SCTP_SB_CLEAR(so->so_snd);
494 * same for the rcv ones, they are only here for the
497 SCTP_SB_CLEAR(so->so_rcv);
499 /* Now null out the reference, we are completely detached. */
503 flags = inp->sctp_flags;
504 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
505 goto sctp_must_try_again;
512 sctp_attach(struct socket *so, int proto, struct thread *p)
514 struct sctp_inpcb *inp;
515 struct inpcb *ip_inp;
517 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
524 inp = (struct sctp_inpcb *)so->so_pcb;
526 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
529 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
530 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
535 error = sctp_inpcb_alloc(so, vrf_id);
539 inp = (struct sctp_inpcb *)so->so_pcb;
541 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
542 ip_inp = &inp->ip_inp.inp;
543 ip_inp->inp_vflag |= INP_IPV4;
544 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
546 error = ipsec_init_policy(so, &ip_inp->inp_sp);
547 #ifdef SCTP_LOG_CLOSING
548 sctp_log_closing(inp, NULL, 17);
552 flags = inp->sctp_flags;
553 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
554 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
555 #ifdef SCTP_LOG_CLOSING
556 sctp_log_closing(inp, NULL, 15);
558 SCTP_INP_WUNLOCK(inp);
559 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
560 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
562 flags = inp->sctp_flags;
563 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
566 SCTP_INP_WUNLOCK(inp);
572 SCTP_INP_WUNLOCK(inp);
577 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
579 struct sctp_inpcb *inp = NULL;
583 if (addr && addr->sa_family != AF_INET) {
584 /* must be a v4 address! */
585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
589 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
590 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
593 inp = (struct sctp_inpcb *)so->so_pcb;
595 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
598 error = sctp_inpcb_bind(so, addr, NULL, p);
603 sctp_close(struct socket *so)
605 struct sctp_inpcb *inp;
608 inp = (struct sctp_inpcb *)so->so_pcb;
613 * Inform all the lower layer assoc that we are done.
616 flags = inp->sctp_flags;
617 #ifdef SCTP_LOG_CLOSING
618 sctp_log_closing(inp, NULL, 17);
620 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
621 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
622 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
623 (so->so_rcv.sb_cc > 0)) {
624 #ifdef SCTP_LOG_CLOSING
625 sctp_log_closing(inp, NULL, 13);
627 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
628 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
630 #ifdef SCTP_LOG_CLOSING
631 sctp_log_closing(inp, NULL, 14);
633 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
634 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
637 * The socket is now detached, no matter what the state of
638 * the SCTP association.
641 SCTP_SB_CLEAR(so->so_snd);
643 * same for the rcv ones, they are only here for the
646 SCTP_SB_CLEAR(so->so_rcv);
648 /* Now null out the reference, we are completely detached. */
652 flags = inp->sctp_flags;
653 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
654 goto sctp_must_try_again;
662 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
663 struct mbuf *control, struct thread *p);
667 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
668 struct mbuf *control, struct thread *p)
670 struct sctp_inpcb *inp;
673 inp = (struct sctp_inpcb *)so->so_pcb;
676 sctp_m_freem(control);
679 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
683 /* Got to have an to address if we are NOT a connected socket */
684 if ((addr == NULL) &&
685 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
686 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
689 } else if (addr == NULL) {
690 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
691 error = EDESTADDRREQ;
694 sctp_m_freem(control);
700 if (addr->sa_family != AF_INET) {
701 /* must be a v4 address! */
702 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
705 sctp_m_freem(control);
708 error = EDESTADDRREQ;
713 /* now what about control */
716 SCTP_PRINTF("huh? control set?\n");
717 sctp_m_freem(inp->control);
720 inp->control = control;
724 SCTP_BUF_NEXT(inp->pkt_last) = m;
727 inp->pkt_last = inp->pkt = m;
730 /* FreeBSD uses a flag passed */
731 ((flags & PRUS_MORETOCOME) == 0)
734 * note with the current version this code will only be used
735 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
736 * re-defining sosend to use the sctp_sosend. One can
737 * optionally switch back to this code (by changing back the
738 * definitions) but this is not advisable. This code is used
739 * by FreeBSD when sending a file with sendfile() though.
743 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
753 sctp_disconnect(struct socket *so)
755 struct sctp_inpcb *inp;
757 inp = (struct sctp_inpcb *)so->so_pcb;
759 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
763 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
764 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
765 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
767 SCTP_INP_RUNLOCK(inp);
770 struct sctp_association *asoc;
771 struct sctp_tcb *stcb;
773 stcb = LIST_FIRST(&inp->sctp_asoc_list);
775 SCTP_INP_RUNLOCK(inp);
776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
781 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
782 /* We are about to be freed, out of here */
783 SCTP_TCB_UNLOCK(stcb);
784 SCTP_INP_RUNLOCK(inp);
787 if (((so->so_options & SO_LINGER) &&
788 (so->so_linger == 0)) ||
789 (so->so_rcv.sb_cc > 0)) {
790 if (SCTP_GET_STATE(asoc) !=
791 SCTP_STATE_COOKIE_WAIT) {
792 /* Left with Data unread */
795 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
801 struct sctp_paramhdr *ph;
803 ph = mtod(err, struct sctp_paramhdr *);
804 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
805 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
806 ph->param_length = htons(SCTP_BUF_LEN(err));
808 #if defined(SCTP_PANIC_ON_ABORT)
809 panic("disconnect does an abort");
811 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
812 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
814 SCTP_INP_RUNLOCK(inp);
815 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
816 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
817 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
819 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
820 /* No unlock tcb assoc is gone */
823 if (TAILQ_EMPTY(&asoc->send_queue) &&
824 TAILQ_EMPTY(&asoc->sent_queue) &&
825 (asoc->stream_queue_cnt == 0)) {
826 /* there is nothing queued to send, so done */
827 if (asoc->locked_on_sending) {
830 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
831 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
832 /* only send SHUTDOWN 1st time thru */
833 sctp_stop_timers_for_shutdown(stcb);
834 sctp_send_shutdown(stcb,
835 stcb->asoc.primary_destination);
836 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
837 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
838 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
839 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
841 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
842 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
843 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
845 asoc->primary_destination);
846 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
848 asoc->primary_destination);
852 * we still got (or just got) data to send,
853 * so set SHUTDOWN_PENDING
856 * XXX sockets draft says that SCTP_EOF
857 * should be sent with no data. currently,
858 * we will allow user data to be sent first
859 * and move to SHUTDOWN-PENDING
861 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
862 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
863 asoc->primary_destination);
864 if (asoc->locked_on_sending) {
865 /* Locked to send out the data */
866 struct sctp_stream_queue_pending *sp;
868 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
870 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
871 asoc->locked_on_sending->stream_no);
873 if ((sp->length == 0) && (sp->msg_is_complete == 0))
874 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
877 if (TAILQ_EMPTY(&asoc->send_queue) &&
878 TAILQ_EMPTY(&asoc->sent_queue) &&
879 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
883 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
884 0, M_DONTWAIT, 1, MT_DATA);
890 struct sctp_paramhdr *ph;
893 SCTP_BUF_LEN(op_err) =
894 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
896 struct sctp_paramhdr *);
897 ph->param_type = htons(
898 SCTP_CAUSE_USER_INITIATED_ABT);
899 ph->param_length = htons(SCTP_BUF_LEN(op_err));
900 ippp = (uint32_t *) (ph + 1);
901 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
903 #if defined(SCTP_PANIC_ON_ABORT)
904 panic("disconnect does an abort");
907 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
908 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
909 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
910 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
911 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
912 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
914 SCTP_INP_RUNLOCK(inp);
915 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
918 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
921 soisdisconnecting(so);
922 SCTP_TCB_UNLOCK(stcb);
923 SCTP_INP_RUNLOCK(inp);
928 /* UDP model does not support this */
929 SCTP_INP_RUNLOCK(inp);
930 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
936 sctp_flush(struct socket *so, int how)
939 * We will just clear out the values and let subsequent close clear
940 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
941 * they will not be able to read the data, the socket will block
942 * that from happening.
944 struct sctp_inpcb *inp;
946 inp = (struct sctp_inpcb *)so->so_pcb;
948 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
952 /* For the 1 to many model this does nothing */
953 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
954 SCTP_INP_RUNLOCK(inp);
957 SCTP_INP_RUNLOCK(inp);
958 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
960 * First make sure the sb will be happy, we don't use these
961 * except maybe the count
964 SCTP_INP_READ_LOCK(inp);
965 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
966 SCTP_INP_READ_UNLOCK(inp);
967 SCTP_INP_WUNLOCK(inp);
968 so->so_rcv.sb_cc = 0;
969 so->so_rcv.sb_mbcnt = 0;
970 so->so_rcv.sb_mb = NULL;
972 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
974 * First make sure the sb will be happy, we don't use these
975 * except maybe the count
977 so->so_snd.sb_cc = 0;
978 so->so_snd.sb_mbcnt = 0;
979 so->so_snd.sb_mb = NULL;
986 sctp_shutdown(struct socket *so)
988 struct sctp_inpcb *inp;
990 inp = (struct sctp_inpcb *)so->so_pcb;
992 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
996 /* For UDP model this is a invalid call */
997 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
998 /* Restore the flags that the soshutdown took away. */
999 SOCKBUF_LOCK(&so->so_rcv);
1000 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1001 SOCKBUF_UNLOCK(&so->so_rcv);
1002 /* This proc will wakeup for read and do nothing (I hope) */
1003 SCTP_INP_RUNLOCK(inp);
1004 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1005 return (EOPNOTSUPP);
1008 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1009 * or SHUT_RDWR. This means we put the shutdown flag against it.
1012 struct sctp_tcb *stcb;
1013 struct sctp_association *asoc;
1016 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1017 SCTP_INP_RUNLOCK(inp);
1022 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1025 * Ok we hit the case that the shutdown call was
1026 * made after an abort or something. Nothing to do
1029 SCTP_INP_RUNLOCK(inp);
1032 SCTP_TCB_LOCK(stcb);
1034 if (TAILQ_EMPTY(&asoc->send_queue) &&
1035 TAILQ_EMPTY(&asoc->sent_queue) &&
1036 (asoc->stream_queue_cnt == 0)) {
1037 if (asoc->locked_on_sending) {
1040 /* there is nothing queued to send, so I'm done... */
1041 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1042 /* only send SHUTDOWN the first time through */
1043 sctp_stop_timers_for_shutdown(stcb);
1044 sctp_send_shutdown(stcb,
1045 stcb->asoc.primary_destination);
1046 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1047 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1048 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1049 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1051 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1052 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1053 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1054 stcb->sctp_ep, stcb,
1055 asoc->primary_destination);
1056 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1057 stcb->sctp_ep, stcb,
1058 asoc->primary_destination);
1062 * we still got (or just got) data to send, so set
1065 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1066 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1067 asoc->primary_destination);
1069 if (asoc->locked_on_sending) {
1070 /* Locked to send out the data */
1071 struct sctp_stream_queue_pending *sp;
1073 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1075 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1076 asoc->locked_on_sending->stream_no);
1078 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1079 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1083 if (TAILQ_EMPTY(&asoc->send_queue) &&
1084 TAILQ_EMPTY(&asoc->sent_queue) &&
1085 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1086 struct mbuf *op_err;
1089 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1090 0, M_DONTWAIT, 1, MT_DATA);
1092 /* Fill in the user initiated abort */
1093 struct sctp_paramhdr *ph;
1096 SCTP_BUF_LEN(op_err) =
1097 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1099 struct sctp_paramhdr *);
1100 ph->param_type = htons(
1101 SCTP_CAUSE_USER_INITIATED_ABT);
1102 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1103 ippp = (uint32_t *) (ph + 1);
1104 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1106 #if defined(SCTP_PANIC_ON_ABORT)
1107 panic("shutdown does an abort");
1109 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1110 sctp_abort_an_association(stcb->sctp_ep, stcb,
1111 SCTP_RESPONSE_TO_USER_REQ,
1112 op_err, SCTP_SO_LOCKED);
1115 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1118 SCTP_TCB_UNLOCK(stcb);
1121 SCTP_INP_RUNLOCK(inp);
1126 * copies a "user" presentable address and removes embedded scope, etc.
1127 * returns 0 on success, 1 on error
1130 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1133 struct sockaddr_in6 lsa6;
1135 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1138 memcpy(ss, sa, sa->sa_len);
1145 * NOTE: assumes addr lock is held
1148 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1149 struct sctp_tcb *stcb,
1151 struct sockaddr_storage *sas,
1154 struct sctp_ifn *sctp_ifn;
1155 struct sctp_ifa *sctp_ifa;
1156 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1158 int ipv4_addr_legal, ipv6_addr_legal;
1159 struct sctp_vrf *vrf;
1166 /* Turn on all the appropriate scope */
1167 loopback_scope = stcb->asoc.loopback_scope;
1168 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1169 local_scope = stcb->asoc.local_scope;
1170 site_scope = stcb->asoc.site_scope;
1172 /* Turn on ALL scope, since we look at the EP */
1173 loopback_scope = ipv4_local_scope = local_scope =
1176 ipv4_addr_legal = ipv6_addr_legal = 0;
1177 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1178 ipv6_addr_legal = 1;
1179 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1180 ipv4_addr_legal = 1;
1183 ipv4_addr_legal = 1;
1185 vrf = sctp_find_vrf(vrf_id);
1189 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1190 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1191 if ((loopback_scope == 0) &&
1192 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1193 /* Skip loopback if loopback_scope not set */
1196 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1199 * For the BOUND-ALL case, the list
1200 * associated with a TCB is Always
1201 * considered a reverse list.. i.e.
1202 * it lists addresses that are NOT
1203 * part of the association. If this
1204 * is one of those we must skip it.
1206 if (sctp_is_addr_restricted(stcb,
1211 switch (sctp_ifa->address.sa.sa_family) {
1213 if (ipv4_addr_legal) {
1214 struct sockaddr_in *sin;
1216 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1217 if (sin->sin_addr.s_addr == 0) {
1225 if ((ipv4_local_scope == 0) &&
1226 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1230 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1231 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1232 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1233 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1234 actual += sizeof(struct sockaddr_in6);
1237 memcpy(sas, sin, sizeof(*sin));
1238 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1239 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1240 actual += sizeof(*sin);
1244 if (actual >= limit) {
1253 if (ipv6_addr_legal) {
1254 struct sockaddr_in6 *sin6;
1256 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1257 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1265 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1266 if (local_scope == 0)
1268 if (sin6->sin6_scope_id == 0) {
1269 if (sa6_recoverscope(sin6) != 0)
1287 if ((site_scope == 0) &&
1288 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1291 memcpy(sas, sin6, sizeof(*sin6));
1292 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1293 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1294 actual += sizeof(*sin6);
1295 if (actual >= limit) {
1310 struct sctp_laddr *laddr;
1312 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1314 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1318 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1321 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1322 sas = (struct sockaddr_storage *)((caddr_t)sas +
1323 laddr->ifa->address.sa.sa_len);
1324 actual += laddr->ifa->address.sa.sa_len;
1325 if (actual >= limit) {
1334 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1335 struct sctp_tcb *stcb,
1337 struct sockaddr_storage *sas)
1341 SCTP_IPI_ADDR_RLOCK();
1342 /* fill up addresses for the endpoint's default vrf */
1343 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1345 SCTP_IPI_ADDR_RUNLOCK();
1350 * NOTE: assumes addr lock is held
1353 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1356 struct sctp_vrf *vrf = NULL;
1359 * In both sub-set bound an bound_all cases we return the MAXIMUM
1360 * number of addresses that you COULD get. In reality the sub-set
1361 * bound may have an exclusion list for a given TCB OR in the
1362 * bound-all case a TCB may NOT include the loopback or other
1363 * addresses as well.
1365 vrf = sctp_find_vrf(vrf_id);
1369 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1370 struct sctp_ifn *sctp_ifn;
1371 struct sctp_ifa *sctp_ifa;
1373 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1374 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1375 /* Count them if they are the right type */
1376 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1377 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1378 cnt += sizeof(struct sockaddr_in6);
1380 cnt += sizeof(struct sockaddr_in);
1382 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1383 cnt += sizeof(struct sockaddr_in6);
1387 struct sctp_laddr *laddr;
1389 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1390 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1391 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1392 cnt += sizeof(struct sockaddr_in6);
1394 cnt += sizeof(struct sockaddr_in);
1396 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1397 cnt += sizeof(struct sockaddr_in6);
1404 sctp_count_max_addresses(struct sctp_inpcb *inp)
1408 SCTP_IPI_ADDR_RLOCK();
1409 /* count addresses for the endpoint's default VRF */
1410 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1411 SCTP_IPI_ADDR_RUNLOCK();
1416 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1417 size_t optsize, void *p, int delay)
1420 int creat_lock_on = 0;
1421 struct sctp_tcb *stcb = NULL;
1422 struct sockaddr *sa;
1423 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1426 int bad_addresses = 0;
1429 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1431 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1432 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1433 /* We are already connected AND the TCP model */
1434 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1435 return (EADDRINUSE);
1437 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1438 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1439 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1442 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1443 SCTP_INP_RLOCK(inp);
1444 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1445 SCTP_INP_RUNLOCK(inp);
1448 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1451 SCTP_INP_INCR_REF(inp);
1452 SCTP_ASOC_CREATE_LOCK(inp);
1454 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1455 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1456 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1460 totaddrp = (int *)optval;
1461 totaddr = *totaddrp;
1462 sa = (struct sockaddr *)(totaddrp + 1);
1463 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1464 if ((stcb != NULL) || bad_addresses) {
1465 /* Already have or am bring up an association */
1466 SCTP_ASOC_CREATE_UNLOCK(inp);
1469 SCTP_TCB_UNLOCK(stcb);
1470 if (bad_addresses == 0) {
1471 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1477 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1482 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1484 struct in6pcb *inp6;
1486 inp6 = (struct in6pcb *)inp;
1487 if (SCTP_IPV6_V6ONLY(inp6)) {
1489 * if IPV6_V6ONLY flag, ignore connections destined
1490 * to a v4 addr or v4-mapped addr
1492 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1498 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1499 SCTP_PCB_FLAGS_UNBOUND) {
1500 /* Bind a ephemeral port */
1501 error = sctp_inpcb_bind(so, NULL, NULL, p);
1506 /* FIX ME: do we want to pass in a vrf on the connect call? */
1507 vrf_id = inp->def_vrf_id;
1510 /* We are GOOD to go */
1511 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1515 /* Gak! no memory */
1518 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1519 /* move to second address */
1520 if (sa->sa_family == AF_INET)
1521 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1523 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1526 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1527 /* Fill in the return id */
1529 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1532 a_id = (sctp_assoc_t *) optval;
1533 *a_id = sctp_get_associd(stcb);
1535 /* initialize authentication parameters for the assoc */
1536 sctp_initialize_auth_params(inp, stcb);
1539 /* doing delayed connection */
1540 stcb->asoc.delayed_connection = 1;
1541 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1543 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1544 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1546 SCTP_TCB_UNLOCK(stcb);
1547 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1548 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1549 /* Set the connected flag so we can queue data */
1553 if (creat_lock_on) {
1554 SCTP_ASOC_CREATE_UNLOCK(inp);
1556 SCTP_INP_DECR_REF(inp);
1560 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1561 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1562 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1563 SCTP_INP_RLOCK(inp); \
1564 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1566 SCTP_TCB_LOCK(stcb); \
1568 SCTP_INP_RUNLOCK(inp); \
1569 } else if (assoc_id != 0) { \
1570 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1571 if (stcb == NULL) { \
1572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1582 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1583 if (size < sizeof(type)) { \
1584 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1588 destp = (type *)srcp; \
1593 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1596 struct sctp_inpcb *inp = NULL;
1598 struct sctp_tcb *stcb = NULL;
1600 if (optval == NULL) {
1601 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1604 inp = (struct sctp_inpcb *)so->so_pcb;
1606 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1613 case SCTP_AUTOCLOSE:
1614 case SCTP_EXPLICIT_EOR:
1615 case SCTP_AUTO_ASCONF:
1616 case SCTP_DISABLE_FRAGMENTS:
1617 case SCTP_I_WANT_MAPPED_V4_ADDR:
1618 case SCTP_USE_EXT_RCVINFO:
1619 SCTP_INP_RLOCK(inp);
1621 case SCTP_DISABLE_FRAGMENTS:
1622 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1624 case SCTP_I_WANT_MAPPED_V4_ADDR:
1625 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1627 case SCTP_AUTO_ASCONF:
1628 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1629 /* only valid for bound all sockets */
1630 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1632 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1637 case SCTP_EXPLICIT_EOR:
1638 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1641 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1643 case SCTP_USE_EXT_RCVINFO:
1644 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1646 case SCTP_AUTOCLOSE:
1647 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1648 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1654 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1655 error = ENOPROTOOPT;
1656 } /* end switch (sopt->sopt_name) */
1657 if (optname != SCTP_AUTOCLOSE) {
1658 /* make it an "on/off" value */
1661 if (*optsize < sizeof(val)) {
1662 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1666 SCTP_INP_RUNLOCK(inp);
1668 /* return the option value */
1669 *(int *)optval = val;
1670 *optsize = sizeof(val);
1673 case SCTP_GET_PACKET_LOG:
1675 #ifdef SCTP_PACKET_LOGGING
1679 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1680 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1683 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1688 case SCTP_REUSE_PORT:
1692 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1693 /* Can't do this for a 1-m socket */
1697 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1698 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1699 *optsize = sizeof(uint32_t);
1702 case SCTP_PARTIAL_DELIVERY_POINT:
1706 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1707 *value = inp->partial_delivery_point;
1708 *optsize = sizeof(uint32_t);
1711 case SCTP_FRAGMENT_INTERLEAVE:
1715 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1716 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1717 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1718 *value = SCTP_FRAG_LEVEL_2;
1720 *value = SCTP_FRAG_LEVEL_1;
1723 *value = SCTP_FRAG_LEVEL_0;
1725 *optsize = sizeof(uint32_t);
1728 case SCTP_CMT_ON_OFF:
1730 struct sctp_assoc_value *av;
1732 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1733 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1735 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1736 SCTP_TCB_UNLOCK(stcb);
1738 SCTP_INP_RLOCK(inp);
1739 av->assoc_value = inp->sctp_cmt_on_off;
1740 SCTP_INP_RUNLOCK(inp);
1742 *optsize = sizeof(*av);
1745 /* JRS - Get socket option for pluggable congestion control */
1746 case SCTP_PLUGGABLE_CC:
1748 struct sctp_assoc_value *av;
1750 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1751 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1753 av->assoc_value = stcb->asoc.congestion_control_module;
1754 SCTP_TCB_UNLOCK(stcb);
1756 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1758 *optsize = sizeof(*av);
1761 /* RS - Get socket option for pluggable stream scheduling */
1762 case SCTP_PLUGGABLE_SS:
1764 struct sctp_assoc_value *av;
1766 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1767 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1769 av->assoc_value = stcb->asoc.stream_scheduling_module;
1770 SCTP_TCB_UNLOCK(stcb);
1772 av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1774 *optsize = sizeof(*av);
1779 struct sctp_stream_value *av;
1781 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1782 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1784 if (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1785 &av->stream_value) < 0) {
1786 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1789 *optsize = sizeof(*av);
1791 SCTP_TCB_UNLOCK(stcb);
1794 * Can't get stream value without
1797 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1802 case SCTP_GET_ADDR_LEN:
1804 struct sctp_assoc_value *av;
1806 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1809 if (av->assoc_value == AF_INET) {
1810 av->assoc_value = sizeof(struct sockaddr_in);
1815 if (av->assoc_value == AF_INET6) {
1816 av->assoc_value = sizeof(struct sockaddr_in6);
1821 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1823 *optsize = sizeof(*av);
1826 case SCTP_GET_ASSOC_NUMBER:
1828 uint32_t *value, cnt;
1830 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1832 SCTP_INP_RLOCK(inp);
1833 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1836 SCTP_INP_RUNLOCK(inp);
1838 *optsize = sizeof(uint32_t);
1842 case SCTP_GET_ASSOC_ID_LIST:
1844 struct sctp_assoc_ids *ids;
1845 unsigned int at, limit;
1847 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1849 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1850 SCTP_INP_RLOCK(inp);
1851 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1853 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1856 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1860 SCTP_INP_RUNLOCK(inp);
1861 ids->gaids_number_of_ids = at;
1862 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1867 struct sctp_assoc_value *av;
1869 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1870 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1873 av->assoc_value = stcb->asoc.context;
1874 SCTP_TCB_UNLOCK(stcb);
1876 SCTP_INP_RLOCK(inp);
1877 av->assoc_value = inp->sctp_context;
1878 SCTP_INP_RUNLOCK(inp);
1880 *optsize = sizeof(*av);
1885 uint32_t *default_vrfid;
1887 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1888 *default_vrfid = inp->def_vrf_id;
1891 case SCTP_GET_ASOC_VRF:
1893 struct sctp_assoc_value *id;
1895 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1896 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1899 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1902 id->assoc_value = stcb->asoc.vrf_id;
1905 case SCTP_GET_VRF_IDS:
1907 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1911 case SCTP_GET_NONCE_VALUES:
1913 struct sctp_get_nonce_values *gnv;
1915 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1916 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1919 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1920 gnv->gn_local_tag = stcb->asoc.my_vtag;
1921 SCTP_TCB_UNLOCK(stcb);
1923 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1926 *optsize = sizeof(*gnv);
1929 case SCTP_DELAYED_SACK:
1931 struct sctp_sack_info *sack;
1933 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1934 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1936 sack->sack_delay = stcb->asoc.delayed_ack;
1937 sack->sack_freq = stcb->asoc.sack_freq;
1938 SCTP_TCB_UNLOCK(stcb);
1940 SCTP_INP_RLOCK(inp);
1941 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1942 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1943 SCTP_INP_RUNLOCK(inp);
1945 *optsize = sizeof(*sack);
1949 case SCTP_GET_SNDBUF_USE:
1951 struct sctp_sockstat *ss;
1953 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1954 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1957 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1958 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1959 stcb->asoc.size_on_all_streams);
1960 SCTP_TCB_UNLOCK(stcb);
1962 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1965 *optsize = sizeof(struct sctp_sockstat);
1968 case SCTP_MAX_BURST:
1972 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1974 SCTP_INP_RLOCK(inp);
1975 if (inp->sctp_ep.max_burst < 256) {
1976 *value = inp->sctp_ep.max_burst;
1980 SCTP_INP_RUNLOCK(inp);
1981 *optsize = sizeof(uint8_t);
1986 struct sctp_assoc_value *av;
1989 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1990 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1993 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1994 SCTP_TCB_UNLOCK(stcb);
1996 SCTP_INP_RLOCK(inp);
1997 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1998 ovh = SCTP_MED_OVERHEAD;
2000 ovh = SCTP_MED_V4_OVERHEAD;
2002 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2003 av->assoc_value = 0;
2005 av->assoc_value = inp->sctp_frag_point - ovh;
2006 SCTP_INP_RUNLOCK(inp);
2008 *optsize = sizeof(struct sctp_assoc_value);
2011 case SCTP_GET_STAT_LOG:
2012 error = sctp_fill_stat_log(optval, optsize);
2016 struct sctp_event_subscribe *events;
2018 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2019 memset(events, 0, sizeof(*events));
2020 SCTP_INP_RLOCK(inp);
2021 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2022 events->sctp_data_io_event = 1;
2024 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2025 events->sctp_association_event = 1;
2027 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2028 events->sctp_address_event = 1;
2030 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2031 events->sctp_send_failure_event = 1;
2033 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2034 events->sctp_peer_error_event = 1;
2036 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2037 events->sctp_shutdown_event = 1;
2039 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2040 events->sctp_partial_delivery_event = 1;
2042 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2043 events->sctp_adaptation_layer_event = 1;
2045 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2046 events->sctp_authentication_event = 1;
2048 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2049 events->sctp_sender_dry_event = 1;
2051 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2052 events->sctp_stream_reset_event = 1;
2053 SCTP_INP_RUNLOCK(inp);
2054 *optsize = sizeof(struct sctp_event_subscribe);
2058 case SCTP_ADAPTATION_LAYER:
2062 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2064 SCTP_INP_RLOCK(inp);
2065 *value = inp->sctp_ep.adaptation_layer_indicator;
2066 SCTP_INP_RUNLOCK(inp);
2067 *optsize = sizeof(uint32_t);
2070 case SCTP_SET_INITIAL_DBG_SEQ:
2074 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2075 SCTP_INP_RLOCK(inp);
2076 *value = inp->sctp_ep.initial_sequence_debug;
2077 SCTP_INP_RUNLOCK(inp);
2078 *optsize = sizeof(uint32_t);
2081 case SCTP_GET_LOCAL_ADDR_SIZE:
2085 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2086 SCTP_INP_RLOCK(inp);
2087 *value = sctp_count_max_addresses(inp);
2088 SCTP_INP_RUNLOCK(inp);
2089 *optsize = sizeof(uint32_t);
2092 case SCTP_GET_REMOTE_ADDR_SIZE:
2096 struct sctp_nets *net;
2098 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2099 /* FIXME MT: change to sctp_assoc_value? */
2100 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2104 /* Count the sizes */
2105 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2106 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2107 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2108 size += sizeof(struct sockaddr_in6);
2109 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2110 size += sizeof(struct sockaddr_in);
2116 SCTP_TCB_UNLOCK(stcb);
2117 *value = (uint32_t) size;
2119 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2122 *optsize = sizeof(uint32_t);
2125 case SCTP_GET_PEER_ADDRESSES:
2127 * Get the address information, an array is passed in to
2128 * fill up we pack it.
2132 struct sockaddr_storage *sas;
2133 struct sctp_nets *net;
2134 struct sctp_getaddresses *saddr;
2136 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2137 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2140 left = (*optsize) - sizeof(struct sctp_getaddresses);
2141 *optsize = sizeof(struct sctp_getaddresses);
2142 sas = (struct sockaddr_storage *)&saddr->addr[0];
2144 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2145 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2146 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2147 cpsz = sizeof(struct sockaddr_in6);
2148 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2149 cpsz = sizeof(struct sockaddr_in);
2155 /* not enough room. */
2159 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2160 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2161 /* Must map the address */
2162 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2163 (struct sockaddr_in6 *)sas);
2166 memcpy(sas, &net->ro._l_addr, cpsz);
2170 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2172 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2176 SCTP_TCB_UNLOCK(stcb);
2178 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2183 case SCTP_GET_LOCAL_ADDRESSES:
2185 size_t limit, actual;
2186 struct sockaddr_storage *sas;
2187 struct sctp_getaddresses *saddr;
2189 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2190 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2192 sas = (struct sockaddr_storage *)&saddr->addr[0];
2193 limit = *optsize - sizeof(sctp_assoc_t);
2194 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2196 SCTP_TCB_UNLOCK(stcb);
2198 *optsize = sizeof(struct sockaddr_storage) + actual;
2201 case SCTP_PEER_ADDR_PARAMS:
2203 struct sctp_paddrparams *paddrp;
2204 struct sctp_nets *net;
2206 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2207 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2211 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2214 * We increment here since
2215 * sctp_findassociation_ep_addr() wil do a
2216 * decrement if it finds the stcb as long as
2217 * the locked tcb (last argument) is NOT a
2220 SCTP_INP_INCR_REF(inp);
2221 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2223 SCTP_INP_DECR_REF(inp);
2226 if (stcb && (net == NULL)) {
2227 struct sockaddr *sa;
2229 sa = (struct sockaddr *)&paddrp->spp_address;
2230 if (sa->sa_family == AF_INET) {
2231 struct sockaddr_in *sin;
2233 sin = (struct sockaddr_in *)sa;
2234 if (sin->sin_addr.s_addr) {
2236 SCTP_TCB_UNLOCK(stcb);
2237 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2240 } else if (sa->sa_family == AF_INET6) {
2241 struct sockaddr_in6 *sin6;
2243 sin6 = (struct sockaddr_in6 *)sa;
2244 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2246 SCTP_TCB_UNLOCK(stcb);
2247 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2251 error = EAFNOSUPPORT;
2252 SCTP_TCB_UNLOCK(stcb);
2253 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2258 /* Applys to the specific association */
2259 paddrp->spp_flags = 0;
2263 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2264 ovh = SCTP_MED_OVERHEAD;
2266 ovh = SCTP_MED_V4_OVERHEAD;
2270 paddrp->spp_pathmaxrxt = net->failure_threshold;
2271 paddrp->spp_pathmtu = net->mtu - ovh;
2272 /* get flags for HB */
2273 if (net->dest_state & SCTP_ADDR_NOHB)
2274 paddrp->spp_flags |= SPP_HB_DISABLE;
2276 paddrp->spp_flags |= SPP_HB_ENABLE;
2277 /* get flags for PMTU */
2278 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2279 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2281 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2284 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2285 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2286 paddrp->spp_flags |= SPP_IPV4_TOS;
2290 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2291 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2292 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2297 * No destination so return default
2302 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2303 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2305 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2306 paddrp->spp_flags |= SPP_IPV4_TOS;
2309 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2310 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2312 /* default settings should be these */
2313 if (stcb->asoc.hb_is_disabled == 0) {
2314 paddrp->spp_flags |= SPP_HB_ENABLE;
2316 paddrp->spp_flags |= SPP_HB_DISABLE;
2318 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2319 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2324 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2327 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2328 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2329 SCTP_TCB_UNLOCK(stcb);
2331 /* Use endpoint defaults */
2332 SCTP_INP_RLOCK(inp);
2333 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2334 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2335 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2336 /* get inp's default */
2338 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2339 paddrp->spp_flags |= SPP_IPV4_TOS;
2342 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2343 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2344 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2347 /* can't return this */
2348 paddrp->spp_pathmtu = 0;
2350 /* default behavior, no stcb */
2351 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2353 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2354 paddrp->spp_flags |= SPP_HB_ENABLE;
2356 paddrp->spp_flags |= SPP_HB_DISABLE;
2358 SCTP_INP_RUNLOCK(inp);
2360 *optsize = sizeof(struct sctp_paddrparams);
2363 case SCTP_GET_PEER_ADDR_INFO:
2365 struct sctp_paddrinfo *paddri;
2366 struct sctp_nets *net;
2368 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2369 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2373 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2376 * We increment here since
2377 * sctp_findassociation_ep_addr() wil do a
2378 * decrement if it finds the stcb as long as
2379 * the locked tcb (last argument) is NOT a
2382 SCTP_INP_INCR_REF(inp);
2383 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2385 SCTP_INP_DECR_REF(inp);
2389 if ((stcb) && (net)) {
2390 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2391 /* It's unconfirmed */
2392 paddri->spinfo_state = SCTP_UNCONFIRMED;
2393 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2395 paddri->spinfo_state = SCTP_ACTIVE;
2398 paddri->spinfo_state = SCTP_INACTIVE;
2400 paddri->spinfo_cwnd = net->cwnd;
2401 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2402 paddri->spinfo_rto = net->RTO;
2403 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2404 SCTP_TCB_UNLOCK(stcb);
2407 SCTP_TCB_UNLOCK(stcb);
2409 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2412 *optsize = sizeof(struct sctp_paddrinfo);
2415 case SCTP_PCB_STATUS:
2417 struct sctp_pcbinfo *spcb;
2419 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2420 sctp_fill_pcbinfo(spcb);
2421 *optsize = sizeof(struct sctp_pcbinfo);
2427 struct sctp_nets *net;
2428 struct sctp_status *sstat;
2430 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2431 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2434 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2439 * I think passing the state is fine since
2440 * sctp_constants.h will be available to the user
2443 sstat->sstat_state = stcb->asoc.state;
2444 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2445 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2446 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2448 * We can't include chunks that have been passed to
2449 * the socket layer. Only things in queue.
2451 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2452 stcb->asoc.cnt_on_all_streams);
2455 sstat->sstat_instrms = stcb->asoc.streamincnt;
2456 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2457 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2458 memcpy(&sstat->sstat_primary.spinfo_address,
2459 &stcb->asoc.primary_destination->ro._l_addr,
2460 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2461 net = stcb->asoc.primary_destination;
2462 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2464 * Again the user can get info from sctp_constants.h
2465 * for what the state of the network is.
2467 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2468 /* It's unconfirmed */
2469 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2470 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2472 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2475 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2477 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2478 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2479 sstat->sstat_primary.spinfo_rto = net->RTO;
2480 sstat->sstat_primary.spinfo_mtu = net->mtu;
2481 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2482 SCTP_TCB_UNLOCK(stcb);
2483 *optsize = sizeof(*sstat);
2488 struct sctp_rtoinfo *srto;
2490 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2491 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2494 srto->srto_initial = stcb->asoc.initial_rto;
2495 srto->srto_max = stcb->asoc.maxrto;
2496 srto->srto_min = stcb->asoc.minrto;
2497 SCTP_TCB_UNLOCK(stcb);
2499 SCTP_INP_RLOCK(inp);
2500 srto->srto_initial = inp->sctp_ep.initial_rto;
2501 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2502 srto->srto_min = inp->sctp_ep.sctp_minrto;
2503 SCTP_INP_RUNLOCK(inp);
2505 *optsize = sizeof(*srto);
2510 struct sctp_timeouts *stimo;
2512 SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2513 SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2516 stimo->stimo_init = stcb->asoc.timoinit;
2517 stimo->stimo_data = stcb->asoc.timodata;
2518 stimo->stimo_sack = stcb->asoc.timosack;
2519 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2520 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2521 stimo->stimo_cookie = stcb->asoc.timocookie;
2522 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2523 SCTP_TCB_UNLOCK(stcb);
2525 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2528 *optsize = sizeof(*stimo);
2531 case SCTP_ASSOCINFO:
2533 struct sctp_assocparams *sasoc;
2536 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2537 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2540 oldval = sasoc->sasoc_cookie_life;
2541 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2542 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2543 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2544 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2545 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2546 SCTP_TCB_UNLOCK(stcb);
2548 SCTP_INP_RLOCK(inp);
2549 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2550 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2551 sasoc->sasoc_number_peer_destinations = 0;
2552 sasoc->sasoc_peer_rwnd = 0;
2553 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2554 SCTP_INP_RUNLOCK(inp);
2556 *optsize = sizeof(*sasoc);
2559 case SCTP_DEFAULT_SEND_PARAM:
2561 struct sctp_sndrcvinfo *s_info;
2563 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2564 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2567 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2568 SCTP_TCB_UNLOCK(stcb);
2570 SCTP_INP_RLOCK(inp);
2571 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2572 SCTP_INP_RUNLOCK(inp);
2574 *optsize = sizeof(*s_info);
2579 struct sctp_initmsg *sinit;
2581 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2582 SCTP_INP_RLOCK(inp);
2583 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2584 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2585 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2586 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2587 SCTP_INP_RUNLOCK(inp);
2588 *optsize = sizeof(*sinit);
2591 case SCTP_PRIMARY_ADDR:
2592 /* we allow a "get" operation on this */
2594 struct sctp_setprim *ssp;
2596 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2597 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2600 /* simply copy out the sockaddr_storage... */
2604 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2605 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2607 memcpy(&ssp->ssp_addr,
2608 &stcb->asoc.primary_destination->ro._l_addr,
2610 SCTP_TCB_UNLOCK(stcb);
2612 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2615 *optsize = sizeof(*ssp);
2619 case SCTP_HMAC_IDENT:
2621 struct sctp_hmacalgo *shmac;
2622 sctp_hmaclist_t *hmaclist;
2626 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2628 SCTP_INP_RLOCK(inp);
2629 hmaclist = inp->sctp_ep.local_hmacs;
2630 if (hmaclist == NULL) {
2631 /* no HMACs to return */
2632 *optsize = sizeof(*shmac);
2633 SCTP_INP_RUNLOCK(inp);
2636 /* is there room for all of the hmac ids? */
2637 size = sizeof(*shmac) + (hmaclist->num_algo *
2638 sizeof(shmac->shmac_idents[0]));
2639 if ((size_t)(*optsize) < size) {
2640 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2642 SCTP_INP_RUNLOCK(inp);
2645 /* copy in the list */
2646 shmac->shmac_number_of_idents = hmaclist->num_algo;
2647 for (i = 0; i < hmaclist->num_algo; i++) {
2648 shmac->shmac_idents[i] = hmaclist->hmac[i];
2650 SCTP_INP_RUNLOCK(inp);
2654 case SCTP_AUTH_ACTIVE_KEY:
2656 struct sctp_authkeyid *scact;
2658 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2659 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2662 /* get the active key on the assoc */
2663 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2664 SCTP_TCB_UNLOCK(stcb);
2666 /* get the endpoint active key */
2667 SCTP_INP_RLOCK(inp);
2668 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2669 SCTP_INP_RUNLOCK(inp);
2671 *optsize = sizeof(*scact);
2674 case SCTP_LOCAL_AUTH_CHUNKS:
2676 struct sctp_authchunks *sac;
2677 sctp_auth_chklist_t *chklist = NULL;
2680 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2681 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2684 /* get off the assoc */
2685 chklist = stcb->asoc.local_auth_chunks;
2686 /* is there enough space? */
2687 size = sctp_auth_get_chklist_size(chklist);
2688 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2690 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2692 /* copy in the chunks */
2693 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2695 SCTP_TCB_UNLOCK(stcb);
2697 /* get off the endpoint */
2698 SCTP_INP_RLOCK(inp);
2699 chklist = inp->sctp_ep.local_auth_chunks;
2700 /* is there enough space? */
2701 size = sctp_auth_get_chklist_size(chklist);
2702 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2704 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2706 /* copy in the chunks */
2707 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2709 SCTP_INP_RUNLOCK(inp);
2711 *optsize = sizeof(struct sctp_authchunks) + size;
2714 case SCTP_PEER_AUTH_CHUNKS:
2716 struct sctp_authchunks *sac;
2717 sctp_auth_chklist_t *chklist = NULL;
2720 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2721 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2724 /* get off the assoc */
2725 chklist = stcb->asoc.peer_auth_chunks;
2726 /* is there enough space? */
2727 size = sctp_auth_get_chklist_size(chklist);
2728 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2732 /* copy in the chunks */
2733 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2735 SCTP_TCB_UNLOCK(stcb);
2737 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2740 *optsize = sizeof(struct sctp_authchunks) + size;
2746 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2747 error = ENOPROTOOPT;
2750 } /* end switch (sopt->sopt_name) */
2755 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2760 struct sctp_tcb *stcb = NULL;
2761 struct sctp_inpcb *inp = NULL;
2764 if (optval == NULL) {
2765 SCTP_PRINTF("optval is NULL\n");
2766 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2769 inp = (struct sctp_inpcb *)so->so_pcb;
2771 SCTP_PRINTF("inp is NULL?\n");
2772 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2775 vrf_id = inp->def_vrf_id;
2780 case SCTP_AUTOCLOSE:
2781 case SCTP_AUTO_ASCONF:
2782 case SCTP_EXPLICIT_EOR:
2783 case SCTP_DISABLE_FRAGMENTS:
2784 case SCTP_USE_EXT_RCVINFO:
2785 case SCTP_I_WANT_MAPPED_V4_ADDR:
2786 /* copy in the option value */
2787 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2792 case SCTP_DISABLE_FRAGMENTS:
2793 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2795 case SCTP_AUTO_ASCONF:
2797 * NOTE: we don't really support this flag
2799 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2800 /* only valid for bound all sockets */
2801 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2807 case SCTP_EXPLICIT_EOR:
2808 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2810 case SCTP_USE_EXT_RCVINFO:
2811 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2813 case SCTP_I_WANT_MAPPED_V4_ADDR:
2814 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2815 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2817 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2822 set_opt = SCTP_PCB_FLAGS_NODELAY;
2824 case SCTP_AUTOCLOSE:
2825 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2826 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2827 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2830 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2832 * The value is in ticks. Note this does not effect
2833 * old associations, only new ones.
2835 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2838 SCTP_INP_WLOCK(inp);
2840 sctp_feature_on(inp, set_opt);
2842 sctp_feature_off(inp, set_opt);
2844 SCTP_INP_WUNLOCK(inp);
2846 case SCTP_REUSE_PORT:
2848 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2849 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2850 /* Can't set it after we are bound */
2854 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2855 /* Can't do this for a 1-m socket */
2860 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2862 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2865 case SCTP_PARTIAL_DELIVERY_POINT:
2869 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2870 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2875 inp->partial_delivery_point = *value;
2878 case SCTP_FRAGMENT_INTERLEAVE:
2879 /* not yet until we re-write sctp_recvmsg() */
2883 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2884 if (*level == SCTP_FRAG_LEVEL_2) {
2885 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2886 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2887 } else if (*level == SCTP_FRAG_LEVEL_1) {
2888 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2889 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2890 } else if (*level == SCTP_FRAG_LEVEL_0) {
2891 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2892 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2895 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2900 case SCTP_CMT_ON_OFF:
2901 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2902 struct sctp_assoc_value *av;
2904 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2905 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2907 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
2908 if (stcb->asoc.sctp_cmt_on_off > 2) {
2909 stcb->asoc.sctp_cmt_on_off = 2;
2911 SCTP_TCB_UNLOCK(stcb);
2913 SCTP_INP_WLOCK(inp);
2914 inp->sctp_cmt_on_off = av->assoc_value;
2915 if (inp->sctp_cmt_on_off > 2) {
2916 inp->sctp_cmt_on_off = 2;
2918 SCTP_INP_WUNLOCK(inp);
2921 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2922 error = ENOPROTOOPT;
2925 /* JRS - Set socket option for pluggable congestion control */
2926 case SCTP_PLUGGABLE_CC:
2928 struct sctp_assoc_value *av;
2930 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2931 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2933 switch (av->assoc_value) {
2934 case SCTP_CC_RFC2581:
2937 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
2938 stcb->asoc.congestion_control_module = av->assoc_value;
2941 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2945 SCTP_TCB_UNLOCK(stcb);
2947 switch (av->assoc_value) {
2948 case SCTP_CC_RFC2581:
2951 SCTP_INP_WLOCK(inp);
2952 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2953 SCTP_INP_WUNLOCK(inp);
2956 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2963 /* RS - Set socket option for pluggable stream scheduling */
2964 case SCTP_PLUGGABLE_SS:
2966 struct sctp_assoc_value *av;
2968 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2969 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2971 switch (av->assoc_value) {
2972 case SCTP_SS_DEFAULT:
2973 case SCTP_SS_ROUND_ROBIN:
2974 case SCTP_SS_ROUND_ROBIN_PACKET:
2975 case SCTP_SS_PRIORITY:
2976 case SCTP_SS_FAIR_BANDWITH:
2977 case SCTP_SS_FIRST_COME:
2978 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
2979 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
2980 stcb->asoc.stream_scheduling_module = av->assoc_value;
2981 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
2984 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2988 SCTP_TCB_UNLOCK(stcb);
2990 switch (av->assoc_value) {
2991 case SCTP_SS_DEFAULT:
2992 case SCTP_SS_ROUND_ROBIN:
2993 case SCTP_SS_ROUND_ROBIN_PACKET:
2994 case SCTP_SS_PRIORITY:
2995 case SCTP_SS_FAIR_BANDWITH:
2996 case SCTP_SS_FIRST_COME:
2997 SCTP_INP_WLOCK(inp);
2998 inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
2999 SCTP_INP_WUNLOCK(inp);
3002 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3011 struct sctp_stream_value *av;
3013 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
3014 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3016 if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
3017 av->stream_value) < 0) {
3018 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3021 SCTP_TCB_UNLOCK(stcb);
3024 * Can't set stream value without
3027 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3032 case SCTP_CLR_STAT_LOG:
3033 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3038 struct sctp_assoc_value *av;
3040 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3041 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3044 stcb->asoc.context = av->assoc_value;
3045 SCTP_TCB_UNLOCK(stcb);
3047 SCTP_INP_WLOCK(inp);
3048 inp->sctp_context = av->assoc_value;
3049 SCTP_INP_WUNLOCK(inp);
3055 uint32_t *default_vrfid;
3057 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
3058 if (*default_vrfid > SCTP_MAX_VRF_ID) {
3059 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3063 inp->def_vrf_id = *default_vrfid;
3066 case SCTP_DEL_VRF_ID:
3068 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3072 case SCTP_ADD_VRF_ID:
3074 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3078 case SCTP_DELAYED_SACK:
3080 struct sctp_sack_info *sack;
3082 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
3083 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
3084 if (sack->sack_delay) {
3085 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3086 sack->sack_delay = SCTP_MAX_SACK_DELAY;
3089 if (sack->sack_delay) {
3090 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3091 sack->sack_delay = TICKS_TO_MSEC(1);
3093 stcb->asoc.delayed_ack = sack->sack_delay;
3095 if (sack->sack_freq) {
3096 stcb->asoc.sack_freq = sack->sack_freq;
3098 SCTP_TCB_UNLOCK(stcb);
3100 SCTP_INP_WLOCK(inp);
3101 if (sack->sack_delay) {
3102 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3103 sack->sack_delay = TICKS_TO_MSEC(1);
3105 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3107 if (sack->sack_freq) {
3108 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3110 SCTP_INP_WUNLOCK(inp);
3114 case SCTP_AUTH_CHUNK:
3116 struct sctp_authchunk *sauth;
3118 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3120 SCTP_INP_WLOCK(inp);
3121 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3122 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3125 SCTP_INP_WUNLOCK(inp);
3130 struct sctp_authkey *sca;
3131 struct sctp_keyhead *shared_keys;
3132 sctp_sharedkey_t *shared_key;
3133 sctp_key_t *key = NULL;
3136 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3137 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3138 size = optsize - sizeof(*sca);
3141 /* set it on the assoc */
3142 shared_keys = &stcb->asoc.shared_keys;
3143 /* clear the cached keys for this key id */
3144 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3146 * create the new shared key and
3150 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3152 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3154 SCTP_TCB_UNLOCK(stcb);
3158 shared_key = sctp_alloc_sharedkey();
3159 if (shared_key == NULL) {
3161 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3163 SCTP_TCB_UNLOCK(stcb);
3166 shared_key->key = key;
3167 shared_key->keyid = sca->sca_keynumber;
3168 error = sctp_insert_sharedkey(shared_keys, shared_key);
3169 SCTP_TCB_UNLOCK(stcb);
3171 /* set it on the endpoint */
3172 SCTP_INP_WLOCK(inp);
3173 shared_keys = &inp->sctp_ep.shared_keys;
3175 * clear the cached keys on all assocs for
3178 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3180 * create the new shared key and
3184 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3186 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3188 SCTP_INP_WUNLOCK(inp);
3192 shared_key = sctp_alloc_sharedkey();
3193 if (shared_key == NULL) {
3195 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3197 SCTP_INP_WUNLOCK(inp);
3200 shared_key->key = key;
3201 shared_key->keyid = sca->sca_keynumber;
3202 error = sctp_insert_sharedkey(shared_keys, shared_key);
3203 SCTP_INP_WUNLOCK(inp);
3207 case SCTP_HMAC_IDENT:
3209 struct sctp_hmacalgo *shmac;
3210 sctp_hmaclist_t *hmaclist;
3216 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3217 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3218 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3222 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3223 if (hmaclist == NULL) {
3224 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3228 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3229 hmacid = shmac->shmac_idents[i];
3230 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3231 /* invalid HMACs were found */ ;
3232 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3234 sctp_free_hmaclist(hmaclist);
3235 goto sctp_set_hmac_done;
3239 for (i = 0; i < hmaclist->num_algo; i++) {
3240 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3241 /* already in list */
3246 sctp_free_hmaclist(hmaclist);
3247 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3251 /* set it on the endpoint */
3252 SCTP_INP_WLOCK(inp);
3253 if (inp->sctp_ep.local_hmacs)
3254 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3255 inp->sctp_ep.local_hmacs = hmaclist;
3256 SCTP_INP_WUNLOCK(inp);
3260 case SCTP_AUTH_ACTIVE_KEY:
3262 struct sctp_authkeyid *scact;
3264 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3266 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3268 /* set the active key on the right place */
3270 /* set the active key on the assoc */
3271 if (sctp_auth_setactivekey(stcb,
3272 scact->scact_keynumber)) {
3273 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3274 SCTP_FROM_SCTP_USRREQ,
3278 SCTP_TCB_UNLOCK(stcb);
3280 /* set the active key on the endpoint */
3281 SCTP_INP_WLOCK(inp);
3282 if (sctp_auth_setactivekey_ep(inp,
3283 scact->scact_keynumber)) {
3284 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3285 SCTP_FROM_SCTP_USRREQ,
3289 SCTP_INP_WUNLOCK(inp);
3293 case SCTP_AUTH_DELETE_KEY:
3295 struct sctp_authkeyid *scdel;
3297 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3299 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3301 /* delete the key from the right place */
3303 if (sctp_delete_sharedkey(stcb,
3304 scdel->scact_keynumber)) {
3305 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3306 SCTP_FROM_SCTP_USRREQ,
3310 SCTP_TCB_UNLOCK(stcb);
3312 SCTP_INP_WLOCK(inp);
3313 if (sctp_delete_sharedkey_ep(inp,
3314 scdel->scact_keynumber)) {
3315 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3316 SCTP_FROM_SCTP_USRREQ,
3320 SCTP_INP_WUNLOCK(inp);
3324 case SCTP_AUTH_DEACTIVATE_KEY:
3326 struct sctp_authkeyid *keyid;
3328 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3330 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3332 /* deactivate the key from the right place */
3334 if (sctp_deact_sharedkey(stcb,
3335 keyid->scact_keynumber)) {
3336 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3337 SCTP_FROM_SCTP_USRREQ,
3341 SCTP_TCB_UNLOCK(stcb);
3343 SCTP_INP_WLOCK(inp);
3344 if (sctp_deact_sharedkey_ep(inp,
3345 keyid->scact_keynumber)) {
3346 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3347 SCTP_FROM_SCTP_USRREQ,
3351 SCTP_INP_WUNLOCK(inp);
3356 case SCTP_RESET_STREAMS:
3358 struct sctp_stream_reset *strrst;
3359 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3361 uint16_t addstrmcnt = 0;
3364 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3365 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3368 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3372 if (stcb->asoc.peer_supports_strreset == 0) {
3374 * Peer does not support it, we return
3375 * protocol not supported since this is true
3376 * for this feature and this peer, not the
3377 * socket request in general.
3379 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3380 error = EPROTONOSUPPORT;
3381 SCTP_TCB_UNLOCK(stcb);
3384 if (stcb->asoc.stream_reset_outstanding) {
3385 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3387 SCTP_TCB_UNLOCK(stcb);
3390 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3392 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3394 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3397 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3399 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3403 /* We can't do that and add streams */
3407 if (stcb->asoc.stream_reset_outstanding) {
3412 /* We allocate here */
3413 addstrmcnt = strrst->strrst_num_streams;
3414 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3415 /* You can't have more than 64k */
3419 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3420 /* Need to allocate more */
3421 struct sctp_stream_out *oldstream;
3422 struct sctp_stream_queue_pending *sp,
3425 oldstream = stcb->asoc.strmout;
3427 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3428 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3430 if (stcb->asoc.strmout == NULL) {
3431 stcb->asoc.strmout = oldstream;
3436 * Ok now we proceed with copying
3437 * the old out stuff and
3438 * initializing the new stuff.
3440 SCTP_TCB_SEND_LOCK(stcb);
3441 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
3442 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3443 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3444 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3445 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3446 stcb->asoc.strmout[i].stream_no = i;
3447 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
3449 * now anything on those
3452 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
3453 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3454 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3457 * Now move assoc pointers
3460 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3461 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3463 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3464 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3467 /* now the new streams */
3468 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3469 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3470 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3471 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3472 stcb->asoc.strmout[i].stream_no = i;
3473 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3474 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3476 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3477 SCTP_FREE(oldstream, SCTP_M_STRMO);
3479 SCTP_TCB_SEND_UNLOCK(stcb);
3482 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3484 SCTP_TCB_UNLOCK(stcb);
3487 for (i = 0; i < strrst->strrst_num_streams; i++) {
3490 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3491 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3496 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3497 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3505 SCTP_TCB_UNLOCK(stcb);
3508 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3509 strrst->strrst_list,
3510 send_out, (stcb->asoc.str_reset_seq_in - 3),
3511 send_in, send_tsn, addstream, addstrmcnt);
3513 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3514 SCTP_TCB_UNLOCK(stcb);
3518 case SCTP_CONNECT_X:
3519 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3520 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3524 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3527 case SCTP_CONNECT_X_DELAYED:
3528 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3529 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3533 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3536 case SCTP_CONNECT_X_COMPLETE:
3538 struct sockaddr *sa;
3539 struct sctp_nets *net;
3541 /* FIXME MT: check correct? */
3542 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3545 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3546 SCTP_INP_RLOCK(inp);
3547 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3549 SCTP_TCB_LOCK(stcb);
3550 net = sctp_findnet(stcb, sa);
3552 SCTP_INP_RUNLOCK(inp);
3555 * We increment here since
3556 * sctp_findassociation_ep_addr() wil do a
3557 * decrement if it finds the stcb as long as
3558 * the locked tcb (last argument) is NOT a
3561 SCTP_INP_INCR_REF(inp);
3562 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3564 SCTP_INP_DECR_REF(inp);
3569 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3573 if (stcb->asoc.delayed_connection == 1) {
3574 stcb->asoc.delayed_connection = 0;
3575 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3576 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3577 stcb->asoc.primary_destination,
3578 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3579 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3582 * already expired or did not use delayed
3585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3588 SCTP_TCB_UNLOCK(stcb);
3591 case SCTP_MAX_BURST:
3595 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3597 SCTP_INP_WLOCK(inp);
3598 inp->sctp_ep.max_burst = *burst;
3599 SCTP_INP_WUNLOCK(inp);
3604 struct sctp_assoc_value *av;
3607 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3608 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3610 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3611 ovh = SCTP_MED_OVERHEAD;
3613 ovh = SCTP_MED_V4_OVERHEAD;
3616 if (av->assoc_value) {
3617 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3619 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3621 SCTP_TCB_UNLOCK(stcb);
3623 SCTP_INP_WLOCK(inp);
3625 * FIXME MT: I think this is not in tune
3628 if (av->assoc_value) {
3629 inp->sctp_frag_point = (av->assoc_value + ovh);
3631 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3633 SCTP_INP_WUNLOCK(inp);
3639 struct sctp_event_subscribe *events;
3641 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3643 SCTP_INP_WLOCK(inp);
3644 if (events->sctp_data_io_event) {
3645 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3647 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3650 if (events->sctp_association_event) {
3651 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3653 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3656 if (events->sctp_address_event) {
3657 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3659 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3662 if (events->sctp_send_failure_event) {
3663 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3665 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3668 if (events->sctp_peer_error_event) {
3669 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3671 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3674 if (events->sctp_shutdown_event) {
3675 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3677 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3680 if (events->sctp_partial_delivery_event) {
3681 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3683 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3686 if (events->sctp_adaptation_layer_event) {
3687 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3689 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3692 if (events->sctp_authentication_event) {
3693 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3695 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3698 if (events->sctp_sender_dry_event) {
3699 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3700 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3701 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3702 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3704 SCTP_TCB_LOCK(stcb);
3707 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3708 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3709 (stcb->asoc.stream_queue_cnt == 0)) {
3710 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3713 SCTP_TCB_UNLOCK(stcb);
3717 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3720 if (events->sctp_stream_reset_event) {
3721 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3723 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3725 SCTP_INP_WUNLOCK(inp);
3729 case SCTP_ADAPTATION_LAYER:
3731 struct sctp_setadaptation *adap_bits;
3733 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3734 SCTP_INP_WLOCK(inp);
3735 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3736 SCTP_INP_WUNLOCK(inp);
3740 case SCTP_SET_INITIAL_DBG_SEQ:
3744 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3745 SCTP_INP_WLOCK(inp);
3746 inp->sctp_ep.initial_sequence_debug = *vvv;
3747 SCTP_INP_WUNLOCK(inp);
3751 case SCTP_DEFAULT_SEND_PARAM:
3753 struct sctp_sndrcvinfo *s_info;
3755 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3756 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3759 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3760 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3762 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3765 SCTP_TCB_UNLOCK(stcb);
3767 SCTP_INP_WLOCK(inp);
3768 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3769 SCTP_INP_WUNLOCK(inp);
3773 case SCTP_PEER_ADDR_PARAMS:
3774 /* Applys to the specific association */
3776 struct sctp_paddrparams *paddrp;
3777 struct sctp_nets *net;
3779 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3780 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3783 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3786 * We increment here since
3787 * sctp_findassociation_ep_addr() wil do a
3788 * decrement if it finds the stcb as long as
3789 * the locked tcb (last argument) is NOT a
3792 SCTP_INP_INCR_REF(inp);
3793 stcb = sctp_findassociation_ep_addr(&inp,
3794 (struct sockaddr *)&paddrp->spp_address,
3797 SCTP_INP_DECR_REF(inp);
3800 if (stcb && (net == NULL)) {
3801 struct sockaddr *sa;
3803 sa = (struct sockaddr *)&paddrp->spp_address;
3804 if (sa->sa_family == AF_INET) {
3805 struct sockaddr_in *sin;
3807 sin = (struct sockaddr_in *)sa;
3808 if (sin->sin_addr.s_addr) {
3809 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3810 SCTP_TCB_UNLOCK(stcb);
3814 } else if (sa->sa_family == AF_INET6) {
3815 struct sockaddr_in6 *sin6;
3817 sin6 = (struct sockaddr_in6 *)sa;
3818 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3819 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3820 SCTP_TCB_UNLOCK(stcb);
3825 error = EAFNOSUPPORT;
3826 SCTP_TCB_UNLOCK(stcb);
3827 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3832 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3834 SCTP_TCB_UNLOCK(stcb);
3835 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3838 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3840 SCTP_TCB_UNLOCK(stcb);
3841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3845 /************************TCB SPECIFIC SET ******************/
3847 * do we change the timer for HB, we run
3852 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3853 ovh = SCTP_MED_OVERHEAD;
3855 ovh = SCTP_MED_V4_OVERHEAD;
3858 if (paddrp->spp_hbinterval)
3859 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3860 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3861 stcb->asoc.heart_beat_delay = 0;
3863 /* network sets ? */
3865 /************************NET SPECIFIC SET ******************/
3866 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3868 if (sctp_send_hb(stcb, 1, net) < 0) {
3869 /* asoc destroyed */
3870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3875 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3876 net->dest_state |= SCTP_ADDR_NOHB;
3878 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3879 net->dest_state &= ~SCTP_ADDR_NOHB;
3881 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3882 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3883 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3884 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3886 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3887 net->mtu = paddrp->spp_pathmtu + ovh;
3888 if (net->mtu < stcb->asoc.smallest_mtu) {
3889 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3893 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3894 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3895 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3898 if (paddrp->spp_pathmaxrxt)
3899 net->failure_threshold = paddrp->spp_pathmaxrxt;
3901 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3902 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3903 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3908 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3909 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3910 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3915 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3916 if (paddrp->spp_pathmaxrxt)
3917 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3919 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3920 /* Turn back on the timer */
3921 stcb->asoc.hb_is_disabled = 0;
3922 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3924 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3925 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3926 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3927 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3928 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3930 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3931 net->mtu = paddrp->spp_pathmtu + ovh;
3932 if (net->mtu < stcb->asoc.smallest_mtu) {
3933 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3938 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3939 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3940 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3941 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3945 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3946 int cnt_of_unconf = 0;
3947 struct sctp_nets *lnet;
3949 stcb->asoc.hb_is_disabled = 1;
3950 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3951 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3956 * stop the timer ONLY if we
3957 * have no unconfirmed
3960 if (cnt_of_unconf == 0) {
3961 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3962 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3963 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3967 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3968 /* start up the timer. */
3969 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3970 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3974 if (paddrp->spp_flags & SPP_IPV4_TOS)
3975 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3978 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3979 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3983 SCTP_TCB_UNLOCK(stcb);
3985 /************************NO TCB, SET TO default stuff ******************/
3986 SCTP_INP_WLOCK(inp);
3988 * For the TOS/FLOWLABEL stuff you set it
3989 * with the options on the socket
3991 if (paddrp->spp_pathmaxrxt) {
3992 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3994 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3995 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3996 else if (paddrp->spp_hbinterval) {
3997 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3998 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3999 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
4001 if (paddrp->spp_flags & SPP_HB_ENABLE) {
4002 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4004 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
4005 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4007 SCTP_INP_WUNLOCK(inp);
4013 struct sctp_rtoinfo *srto;
4014 uint32_t new_init, new_min, new_max;
4016 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
4017 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
4020 if (srto->srto_initial)
4021 new_init = srto->srto_initial;
4023 new_init = stcb->asoc.initial_rto;
4025 new_max = srto->srto_max;
4027 new_max = stcb->asoc.maxrto;
4029 new_min = srto->srto_min;
4031 new_min = stcb->asoc.minrto;
4032 if ((new_min <= new_init) && (new_init <= new_max)) {
4033 stcb->asoc.initial_rto = new_init;
4034 stcb->asoc.maxrto = new_max;
4035 stcb->asoc.minrto = new_min;
4037 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4040 SCTP_TCB_UNLOCK(stcb);
4042 SCTP_INP_WLOCK(inp);
4043 if (srto->srto_initial)
4044 new_init = srto->srto_initial;
4046 new_init = inp->sctp_ep.initial_rto;
4048 new_max = srto->srto_max;
4050 new_max = inp->sctp_ep.sctp_maxrto;
4052 new_min = srto->srto_min;
4054 new_min = inp->sctp_ep.sctp_minrto;
4055 if ((new_min <= new_init) && (new_init <= new_max)) {
4056 inp->sctp_ep.initial_rto = new_init;
4057 inp->sctp_ep.sctp_maxrto = new_max;
4058 inp->sctp_ep.sctp_minrto = new_min;
4060 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4063 SCTP_INP_WUNLOCK(inp);
4067 case SCTP_ASSOCINFO:
4069 struct sctp_assocparams *sasoc;
4071 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4072 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4073 if (sasoc->sasoc_cookie_life) {
4074 /* boundary check the cookie life */
4075 if (sasoc->sasoc_cookie_life < 1000)
4076 sasoc->sasoc_cookie_life = 1000;
4077 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4078 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4082 if (sasoc->sasoc_asocmaxrxt)
4083 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4084 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4085 sasoc->sasoc_peer_rwnd = 0;
4086 sasoc->sasoc_local_rwnd = 0;
4087 if (sasoc->sasoc_cookie_life) {
4088 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4090 SCTP_TCB_UNLOCK(stcb);
4092 SCTP_INP_WLOCK(inp);
4093 if (sasoc->sasoc_asocmaxrxt)
4094 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4095 sasoc->sasoc_number_peer_destinations = 0;
4096 sasoc->sasoc_peer_rwnd = 0;
4097 sasoc->sasoc_local_rwnd = 0;
4098 if (sasoc->sasoc_cookie_life) {
4099 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4101 SCTP_INP_WUNLOCK(inp);
4107 struct sctp_initmsg *sinit;
4109 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4110 SCTP_INP_WLOCK(inp);
4111 if (sinit->sinit_num_ostreams)
4112 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4114 if (sinit->sinit_max_instreams)
4115 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4117 if (sinit->sinit_max_attempts)
4118 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4120 if (sinit->sinit_max_init_timeo)
4121 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4122 SCTP_INP_WUNLOCK(inp);
4125 case SCTP_PRIMARY_ADDR:
4127 struct sctp_setprim *spa;
4128 struct sctp_nets *net, *lnet;
4130 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4131 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4135 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4138 * We increment here since
4139 * sctp_findassociation_ep_addr() wil do a
4140 * decrement if it finds the stcb as long as
4141 * the locked tcb (last argument) is NOT a
4144 SCTP_INP_INCR_REF(inp);
4145 stcb = sctp_findassociation_ep_addr(&inp,
4146 (struct sockaddr *)&spa->ssp_addr,
4149 SCTP_INP_DECR_REF(inp);
4153 if ((stcb) && (net)) {
4154 if ((net != stcb->asoc.primary_destination) &&
4155 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4156 /* Ok we need to set it */
4157 lnet = stcb->asoc.primary_destination;
4158 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4159 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4160 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4162 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4166 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4170 SCTP_TCB_UNLOCK(stcb);
4174 case SCTP_SET_DYNAMIC_PRIMARY:
4176 union sctp_sockstore *ss;
4178 error = priv_check(curthread,
4179 PRIV_NETINET_RESERVEDPORT);
4183 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4184 /* SUPER USER CHECK? */
4185 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4188 case SCTP_SET_PEER_PRIMARY_ADDR:
4190 struct sctp_setpeerprim *sspp;
4192 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4193 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4195 struct sctp_ifa *ifa;
4197 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4198 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4200 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4204 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4206 * Must validate the ifa found is in
4209 struct sctp_laddr *laddr;
4212 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4213 if (laddr->ifa == NULL) {
4214 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4218 if (laddr->ifa == ifa) {
4224 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4229 if (sctp_set_primary_ip_address_sa(stcb,
4230 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4231 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4235 SCTP_TCB_UNLOCK(stcb);
4237 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4243 case SCTP_BINDX_ADD_ADDR:
4245 struct sctp_getaddresses *addrs;
4249 td = (struct thread *)p;
4250 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4252 if (addrs->addr->sa_family == AF_INET) {
4253 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4255 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4259 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4260 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4264 } else if (addrs->addr->sa_family == AF_INET6) {
4265 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4267 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4271 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4272 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4273 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4278 error = EAFNOSUPPORT;
4281 sctp_bindx_add_address(so, inp, addrs->addr,
4282 addrs->sget_assoc_id, vrf_id,
4286 case SCTP_BINDX_REM_ADDR:
4288 struct sctp_getaddresses *addrs;
4292 td = (struct thread *)p;
4294 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4295 if (addrs->addr->sa_family == AF_INET) {
4296 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4298 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4302 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4303 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4307 } else if (addrs->addr->sa_family == AF_INET6) {
4308 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4310 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4314 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4315 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4316 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4321 error = EAFNOSUPPORT;
4324 sctp_bindx_delete_address(so, inp, addrs->addr,
4325 addrs->sget_assoc_id, vrf_id,
4330 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4331 error = ENOPROTOOPT;
4333 } /* end switch (opt) */
4338 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4340 void *optval = NULL;
4342 struct sctp_inpcb *inp;
4346 inp = (struct sctp_inpcb *)so->so_pcb;
4348 /* I made the same as TCP since we are not setup? */
4349 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4350 return (ECONNRESET);
4352 if (sopt->sopt_level != IPPROTO_SCTP) {
4353 /* wrong proto level... send back up to IP */
4355 if (INP_CHECK_SOCKAF(so, AF_INET6))
4356 error = ip6_ctloutput(so, sopt);
4359 error = ip_ctloutput(so, sopt);
4362 optsize = sopt->sopt_valsize;
4364 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4365 if (optval == NULL) {
4366 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4369 error = sooptcopyin(sopt, optval, optsize, optsize);
4371 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4375 p = (void *)sopt->sopt_td;
4376 if (sopt->sopt_dir == SOPT_SET) {
4377 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4378 } else if (sopt->sopt_dir == SOPT_GET) {
4379 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4381 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4384 if ((error == 0) && (optval != NULL)) {
4385 error = sooptcopyout(sopt, optval, optsize);
4386 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4387 } else if (optval != NULL) {
4388 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4396 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4399 int create_lock_on = 0;
4401 struct sctp_inpcb *inp;
4402 struct sctp_tcb *stcb = NULL;
4404 inp = (struct sctp_inpcb *)so->so_pcb;
4406 /* I made the same as TCP since we are not setup? */
4407 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4408 return (ECONNRESET);
4411 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4415 if (addr->sa_family == AF_INET6) {
4416 struct sockaddr_in6 *sin6p;
4418 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4419 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4422 sin6p = (struct sockaddr_in6 *)addr;
4423 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4424 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4429 if (addr->sa_family == AF_INET) {
4430 struct sockaddr_in *sinp;
4432 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4433 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4436 sinp = (struct sockaddr_in *)addr;
4437 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4438 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4442 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4443 return (EAFNOSUPPORT);
4445 SCTP_INP_INCR_REF(inp);
4446 SCTP_ASOC_CREATE_LOCK(inp);
4450 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4451 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4452 /* Should I really unlock ? */
4453 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4458 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4459 (addr->sa_family == AF_INET6)) {
4460 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4465 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4466 SCTP_PCB_FLAGS_UNBOUND) {
4467 /* Bind a ephemeral port */
4468 error = sctp_inpcb_bind(so, NULL, NULL, p);
4473 /* Now do we connect? */
4474 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4475 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4476 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4480 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4481 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4482 /* We are already connected AND the TCP model */
4483 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4487 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4488 SCTP_INP_RLOCK(inp);
4489 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4490 SCTP_INP_RUNLOCK(inp);
4493 * We increment here since sctp_findassociation_ep_addr()
4494 * will do a decrement if it finds the stcb as long as the
4495 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4497 SCTP_INP_INCR_REF(inp);
4498 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4500 SCTP_INP_DECR_REF(inp);
4502 SCTP_TCB_UNLOCK(stcb);
4506 /* Already have or am bring up an association */
4507 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4511 vrf_id = inp->def_vrf_id;
4512 /* We are GOOD to go */
4513 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4515 /* Gak! no memory */
4518 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4519 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4520 /* Set the connected flag so we can queue data */
4521 SOCKBUF_LOCK(&so->so_rcv);
4522 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4523 SOCKBUF_UNLOCK(&so->so_rcv);
4524 SOCKBUF_LOCK(&so->so_snd);
4525 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4526 SOCKBUF_UNLOCK(&so->so_snd);
4528 so->so_state &= ~SS_ISDISCONNECTING;
4532 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4533 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4535 /* initialize authentication parameters for the assoc */
4536 sctp_initialize_auth_params(inp, stcb);
4538 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4539 SCTP_TCB_UNLOCK(stcb);
4541 if (create_lock_on) {
4542 SCTP_ASOC_CREATE_UNLOCK(inp);
4544 SCTP_INP_DECR_REF(inp);
4549 sctp_listen(struct socket *so, int backlog, struct thread *p)
4552 * Note this module depends on the protocol processing being called
4553 * AFTER any socket level flags and backlog are applied to the
4554 * socket. The traditional way that the socket flags are applied is
4555 * AFTER protocol processing. We have made a change to the
4556 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4557 * place if the socket API for SCTP is to work properly.
4561 struct sctp_inpcb *inp;
4563 inp = (struct sctp_inpcb *)so->so_pcb;
4565 /* I made the same as TCP since we are not setup? */
4566 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4567 return (ECONNRESET);
4569 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4570 /* See if we have a listener */
4571 struct sctp_inpcb *tinp;
4572 union sctp_sockstore store, *sp;
4575 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4577 struct sctp_laddr *laddr;
4579 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4580 memcpy(&store, &laddr->ifa->address, sizeof(store));
4581 sp->sin.sin_port = inp->sctp_lport;
4582 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4583 if (tinp && (tinp != inp) &&
4584 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4585 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4586 (tinp->sctp_socket->so_qlimit)) {
4588 * we have a listener already and
4591 SCTP_INP_DECR_REF(tinp);
4592 return (EADDRINUSE);
4594 SCTP_INP_DECR_REF(tinp);
4598 /* Setup a local addr bound all */
4599 memset(&store, 0, sizeof(store));
4600 store.sin.sin_port = inp->sctp_lport;
4602 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4603 store.sa.sa_family = AF_INET6;
4604 store.sa.sa_len = sizeof(struct sockaddr_in6);
4607 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4608 store.sa.sa_family = AF_INET;
4609 store.sa.sa_len = sizeof(struct sockaddr_in);
4611 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4612 if (tinp && (tinp != inp) &&
4613 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4614 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4615 (tinp->sctp_socket->so_qlimit)) {
4617 * we have a listener already and its not
4620 SCTP_INP_DECR_REF(tinp);
4621 return (EADDRINUSE);
4623 SCTP_INP_DECR_REF(inp);
4627 SCTP_INP_RLOCK(inp);
4628 #ifdef SCTP_LOCK_LOGGING
4629 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4630 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4634 error = solisten_proto_check(so);
4637 SCTP_INP_RUNLOCK(inp);
4640 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4641 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4643 * The unlucky case - We are in the tcp pool with this guy.
4644 * - Someone else is in the main inp slot. - We must move
4645 * this guy (the listener) to the main slot - We must then
4646 * move the guy that was listener to the TCP Pool.
4648 if (sctp_swap_inpcb_for_listen(inp)) {
4652 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4653 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4654 /* We are already connected AND the TCP model */
4656 SCTP_INP_RUNLOCK(inp);
4658 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4659 return (EADDRINUSE);
4661 SCTP_INP_RUNLOCK(inp);
4662 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4663 /* We must do a bind. */
4665 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4666 /* bind error, probably perm */
4671 /* It appears for 7.0 and on, we must always call this. */
4672 solisten_proto(so, backlog);
4673 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4674 /* remove the ACCEPTCONN flag for one-to-many sockets */
4675 so->so_options &= ~SO_ACCEPTCONN;
4678 /* turning off listen */
4679 so->so_options &= ~SO_ACCEPTCONN;
4685 static int sctp_defered_wakeup_cnt = 0;
4688 sctp_accept(struct socket *so, struct sockaddr **addr)
4690 struct sctp_tcb *stcb;
4691 struct sctp_inpcb *inp;
4692 union sctp_sockstore store;
4698 inp = (struct sctp_inpcb *)so->so_pcb;
4701 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4702 return (ECONNRESET);
4704 SCTP_INP_RLOCK(inp);
4705 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4706 SCTP_INP_RUNLOCK(inp);
4707 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4708 return (EOPNOTSUPP);
4710 if (so->so_state & SS_ISDISCONNECTED) {
4711 SCTP_INP_RUNLOCK(inp);
4712 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4713 return (ECONNABORTED);
4715 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4717 SCTP_INP_RUNLOCK(inp);
4718 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4719 return (ECONNRESET);
4721 SCTP_TCB_LOCK(stcb);
4722 SCTP_INP_RUNLOCK(inp);
4723 store = stcb->asoc.primary_destination->ro._l_addr;
4724 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4725 SCTP_TCB_UNLOCK(stcb);
4726 switch (store.sa.sa_family) {
4729 struct sockaddr_in *sin;
4731 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4734 sin->sin_family = AF_INET;
4735 sin->sin_len = sizeof(*sin);
4736 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4737 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4738 *addr = (struct sockaddr *)sin;
4744 struct sockaddr_in6 *sin6;
4746 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4749 sin6->sin6_family = AF_INET6;
4750 sin6->sin6_len = sizeof(*sin6);
4751 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4753 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4754 if ((error = sa6_recoverscope(sin6)) != 0) {
4755 SCTP_FREE_SONAME(sin6);
4758 *addr = (struct sockaddr *)sin6;
4766 /* Wake any delayed sleep action */
4767 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4768 SCTP_INP_WLOCK(inp);
4769 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4770 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4771 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4772 SCTP_INP_WUNLOCK(inp);
4773 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4774 if (sowriteable(inp->sctp_socket)) {
4775 sowwakeup_locked(inp->sctp_socket);
4777 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4779 SCTP_INP_WLOCK(inp);
4781 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4782 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4783 SCTP_INP_WUNLOCK(inp);
4784 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4785 if (soreadable(inp->sctp_socket)) {
4786 sctp_defered_wakeup_cnt++;
4787 sorwakeup_locked(inp->sctp_socket);
4789 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4791 SCTP_INP_WLOCK(inp);
4793 SCTP_INP_WUNLOCK(inp);
4795 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4796 SCTP_TCB_LOCK(stcb);
4797 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4803 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4805 struct sockaddr_in *sin;
4807 struct sctp_inpcb *inp;
4808 struct sctp_ifa *sctp_ifa;
4811 * Do the malloc first in case it blocks.
4813 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4816 sin->sin_family = AF_INET;
4817 sin->sin_len = sizeof(*sin);
4818 inp = (struct sctp_inpcb *)so->so_pcb;
4820 SCTP_FREE_SONAME(sin);
4821 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4824 SCTP_INP_RLOCK(inp);
4825 sin->sin_port = inp->sctp_lport;
4826 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4827 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4828 struct sctp_tcb *stcb;
4829 struct sockaddr_in *sin_a;
4830 struct sctp_nets *net;
4833 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4839 SCTP_TCB_LOCK(stcb);
4840 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4841 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4843 /* this will make coverity happy */
4846 if (sin_a->sin_family == AF_INET) {
4851 if ((!fnd) || (sin_a == NULL)) {
4853 SCTP_TCB_UNLOCK(stcb);
4856 vrf_id = inp->def_vrf_id;
4857 sctp_ifa = sctp_source_address_selection(inp,
4859 (sctp_route_t *) & net->ro,
4862 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4863 sctp_free_ifa(sctp_ifa);
4865 SCTP_TCB_UNLOCK(stcb);
4867 /* For the bound all case you get back 0 */
4869 sin->sin_addr.s_addr = 0;
4873 /* Take the first IPv4 address in the list */
4874 struct sctp_laddr *laddr;
4877 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4878 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4879 struct sockaddr_in *sin_a;
4881 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4882 sin->sin_addr = sin_a->sin_addr;
4888 SCTP_FREE_SONAME(sin);
4889 SCTP_INP_RUNLOCK(inp);
4890 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4894 SCTP_INP_RUNLOCK(inp);
4895 (*addr) = (struct sockaddr *)sin;
4900 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4902 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4904 struct sockaddr_in *sin_a;
4905 struct sctp_inpcb *inp;
4906 struct sctp_tcb *stcb;
4907 struct sctp_nets *net;
4909 /* Do the malloc first in case it blocks. */
4910 inp = (struct sctp_inpcb *)so->so_pcb;
4911 if ((inp == NULL) ||
4912 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4913 /* UDP type and listeners will drop out here */
4914 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4917 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4920 sin->sin_family = AF_INET;
4921 sin->sin_len = sizeof(*sin);
4923 /* We must recapture incase we blocked */
4924 inp = (struct sctp_inpcb *)so->so_pcb;
4926 SCTP_FREE_SONAME(sin);
4927 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4930 SCTP_INP_RLOCK(inp);
4931 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4933 SCTP_TCB_LOCK(stcb);
4935 SCTP_INP_RUNLOCK(inp);
4937 SCTP_FREE_SONAME(sin);
4938 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4942 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4943 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4944 if (sin_a->sin_family == AF_INET) {
4946 sin->sin_port = stcb->rport;
4947 sin->sin_addr = sin_a->sin_addr;
4951 SCTP_TCB_UNLOCK(stcb);
4953 /* No IPv4 address */
4954 SCTP_FREE_SONAME(sin);
4955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4958 (*addr) = (struct sockaddr *)sin;
4962 struct pr_usrreqs sctp_usrreqs = {
4963 .pru_abort = sctp_abort,
4964 .pru_accept = sctp_accept,
4965 .pru_attach = sctp_attach,
4966 .pru_bind = sctp_bind,
4967 .pru_connect = sctp_connect,
4968 .pru_control = in_control,
4969 .pru_close = sctp_close,
4970 .pru_detach = sctp_close,
4971 .pru_sopoll = sopoll_generic,
4972 .pru_flush = sctp_flush,
4973 .pru_disconnect = sctp_disconnect,
4974 .pru_listen = sctp_listen,
4975 .pru_peeraddr = sctp_peeraddr,
4976 .pru_send = sctp_sendm,
4977 .pru_shutdown = sctp_shutdown,
4978 .pru_sockaddr = sctp_ingetaddr,
4979 .pru_sosend = sctp_sosend,
4980 .pru_soreceive = sctp_soreceive