2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart, rrs@lakerest.net and
4 * Michael Tuexen, tuexen@fh-muenster.de
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * a) Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * b) Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the distribution.
17 * c) Neither the name of Cisco Systems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
34 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
40 #include <netinet/sctp_pcb.h>
41 #include <netinet/sctp_header.h>
42 #include <netinet/sctp_var.h>
45 #include <netinet/sctp_sysctl.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctputil.h>
50 #include <netinet/sctp_indata.h>
51 #include <netinet/sctp_timer.h>
52 #include <netinet/sctp_auth.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #include <netinet/udp.h>
58 extern struct sctp_cc_functions sctp_cc_functions[];
59 extern struct sctp_ss_functions sctp_ss_functions[];
66 /* Initialize and modify the sysctled variables */
68 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
69 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
71 * Allow a user to take no more than 1/2 the number of clusters or
72 * the SB_MAX whichever is smaller for the send window.
74 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
75 SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
76 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
78 * Now for the recv window, should we take the same amount? or
79 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
80 * now I will just copy.
82 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
84 SCTP_BASE_VAR(first_time) = 0;
85 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
87 #if defined(SCTP_PACKET_LOGGING)
88 SCTP_BASE_VAR(packet_log_writers) = 0;
89 SCTP_BASE_VAR(packet_log_end) = 0;
90 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
105 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
106 struct sctp_tcb *stcb,
107 struct sctp_nets *net,
110 struct sctp_tmit_chunk *chk;
113 /* Adjust that too */
114 stcb->asoc.smallest_mtu = nxtsz;
115 /* now off to subtract IP_DF flag if needed */
116 overhead = IP_HDR_SIZE;
117 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
118 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
120 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
121 if ((chk->send_size + overhead) > nxtsz) {
122 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
125 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
126 if ((chk->send_size + overhead) > nxtsz) {
128 * For this guy we also mark for immediate resend
129 * since we sent to big of chunk
131 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
132 if (chk->sent < SCTP_DATAGRAM_RESEND) {
133 sctp_flight_size_decrease(chk);
134 sctp_total_flight_decrease(stcb, chk);
136 if (chk->sent != SCTP_DATAGRAM_RESEND) {
137 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
139 chk->sent = SCTP_DATAGRAM_RESEND;
140 chk->rec.data.doing_fast_retransmit = 0;
141 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
142 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
143 chk->whoTo->flight_size,
145 (uintptr_t) chk->whoTo,
146 chk->rec.data.TSN_seq);
148 /* Clear any time so NO RTT is being done */
155 sctp_notify_mbuf(struct sctp_inpcb *inp,
156 struct sctp_tcb *stcb,
157 struct sctp_nets *net,
162 int totsz, tmr_stopped = 0;
166 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
167 (ip == NULL) || (sh == NULL)) {
169 SCTP_TCB_UNLOCK(stcb);
173 /* First job is to verify the vtag matches what I would send */
174 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
175 SCTP_TCB_UNLOCK(stcb);
178 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
180 if (icmph->icmp_type != ICMP_UNREACH) {
181 /* We only care about unreachable */
182 SCTP_TCB_UNLOCK(stcb);
185 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
186 /* not a unreachable message due to frag. */
187 SCTP_TCB_UNLOCK(stcb);
192 nxtsz = ntohs(icmph->icmp_nextmtu);
195 * old type router that does not tell us what the next size
196 * mtu is. Rats we will have to guess (in a educated fashion
199 nxtsz = sctp_get_prev_mtu(totsz);
201 /* Stop any PMTU timer */
202 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
204 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
205 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
207 /* Adjust destination size limit */
208 if (net->mtu > nxtsz) {
211 net->mtu -= sizeof(struct udphdr);
214 /* now what about the ep? */
215 if (stcb->asoc.smallest_mtu > nxtsz) {
216 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
219 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
221 SCTP_TCB_UNLOCK(stcb);
226 sctp_notify(struct sctp_inpcb *inp,
230 struct sctp_tcb *stcb,
231 struct sctp_nets *net)
233 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
242 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
243 (sh == NULL) || (to == NULL)) {
245 SCTP_TCB_UNLOCK(stcb);
248 /* First job is to verify the vtag matches what I would send */
249 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
250 SCTP_TCB_UNLOCK(stcb);
253 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
255 if (icmph->icmp_type != ICMP_UNREACH) {
256 /* We only care about unreachable */
257 SCTP_TCB_UNLOCK(stcb);
260 if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
261 (icmph->icmp_code == ICMP_UNREACH_HOST) ||
262 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
263 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
264 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
265 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
266 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
267 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
270 * Hmm reachablity problems we must examine closely. If its
271 * not reachable, we may have lost a network. Or if there is
272 * NO protocol at the other end named SCTP. well we consider
275 if (net->dest_state & SCTP_ADDR_REACHABLE) {
276 /* Ok that destination is NOT reachable */
277 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
279 net->failure_threshold,
282 net->dest_state &= ~SCTP_ADDR_REACHABLE;
283 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
285 * JRS 5/14/07 - If a destination is unreachable,
286 * the PF bit is turned off. This allows an
287 * unambiguous use of the PF bit for destinations
288 * that are reachable but potentially failed. If the
289 * destination is set to the unreachable state, also
290 * set the destination to the PF state.
293 * Add debug message here if destination is not in
296 /* Stop any running T3 timers here? */
297 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
298 (stcb->asoc.sctp_cmt_pf > 0)) {
299 net->dest_state &= ~SCTP_ADDR_PF;
300 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
303 net->error_count = net->failure_threshold + 1;
304 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
305 stcb, SCTP_FAILED_THRESHOLD,
306 (void *)net, SCTP_SO_NOT_LOCKED);
308 SCTP_TCB_UNLOCK(stcb);
309 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
310 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
312 * Here the peer is either playing tricks on us, including
313 * an address that belongs to someone who does not support
314 * SCTP OR was a userland implementation that shutdown and
315 * now is dead. In either case treat it like a OOTB abort
318 reason = SCTP_PEER_FAULTY;
319 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
320 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
321 so = SCTP_INP_SO(inp);
322 atomic_add_int(&stcb->asoc.refcnt, 1);
323 SCTP_TCB_UNLOCK(stcb);
324 SCTP_SOCKET_LOCK(so, 1);
326 atomic_subtract_int(&stcb->asoc.refcnt, 1);
328 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
329 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
330 SCTP_SOCKET_UNLOCK(so, 1);
331 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
333 /* no need to unlock here, since the TCB is gone */
335 SCTP_TCB_UNLOCK(stcb);
340 sctp_ctlinput(cmd, sa, vip)
349 /* FIX, for non-bsd is this right? */
350 vrf_id = SCTP_DEFAULT_VRFID;
351 if (sa->sa_family != AF_INET ||
352 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
355 if (PRC_IS_REDIRECT(cmd)) {
357 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
361 struct sctp_inpcb *inp = NULL;
362 struct sctp_tcb *stcb = NULL;
363 struct sctp_nets *net = NULL;
364 struct sockaddr_in to, from;
366 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
367 bzero(&to, sizeof(to));
368 bzero(&from, sizeof(from));
369 from.sin_family = to.sin_family = AF_INET;
370 from.sin_len = to.sin_len = sizeof(to);
371 from.sin_port = sh->src_port;
372 from.sin_addr = ip->ip_src;
373 to.sin_port = sh->dest_port;
374 to.sin_addr = ip->ip_dst;
377 * 'to' holds the dest of the packet that failed to be sent.
378 * 'from' holds our local endpoint address. Thus we reverse
379 * the to and the from in the lookup.
381 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
382 (struct sockaddr *)&to,
383 &inp, &net, 1, vrf_id);
384 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
385 if (cmd != PRC_MSGSIZE) {
386 sctp_notify(inp, ip, sh,
387 (struct sockaddr *)&to, stcb,
390 /* handle possible ICMP size messages */
391 sctp_notify_mbuf(inp, stcb, net, ip, sh);
394 if ((stcb == NULL) && (inp != NULL)) {
395 /* reduce ref-count */
397 SCTP_INP_DECR_REF(inp);
398 SCTP_INP_WUNLOCK(inp);
401 SCTP_TCB_UNLOCK(stcb);
409 sctp_getcred(SYSCTL_HANDLER_ARGS)
412 struct sockaddr_in addrs[2];
413 struct sctp_inpcb *inp;
414 struct sctp_nets *net;
415 struct sctp_tcb *stcb;
419 /* FIX, for non-bsd is this right? */
420 vrf_id = SCTP_DEFAULT_VRFID;
422 error = priv_check(req->td, PRIV_NETINET_GETCRED);
427 error = SYSCTL_IN(req, addrs, sizeof(addrs));
431 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
433 &inp, &net, 1, vrf_id);
434 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
435 if ((inp != NULL) && (stcb == NULL)) {
436 /* reduce ref-count */
438 SCTP_INP_DECR_REF(inp);
441 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
445 SCTP_TCB_UNLOCK(stcb);
447 * We use the write lock here, only since in the error leg we need
448 * it. If we used RLOCK, then we would have to
449 * wlock/decr/unlock/rlock. Which in theory could create a hole.
450 * Better to use higher wlock.
454 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
456 SCTP_INP_WUNLOCK(inp);
459 cru2x(inp->sctp_socket->so_cred, &xuc);
460 SCTP_INP_WUNLOCK(inp);
461 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
466 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
467 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
471 sctp_abort(struct socket *so)
473 struct sctp_inpcb *inp;
476 inp = (struct sctp_inpcb *)so->so_pcb;
481 flags = inp->sctp_flags;
482 #ifdef SCTP_LOG_CLOSING
483 sctp_log_closing(inp, NULL, 17);
485 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
486 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
487 #ifdef SCTP_LOG_CLOSING
488 sctp_log_closing(inp, NULL, 16);
490 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
491 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
493 SCTP_SB_CLEAR(so->so_snd);
495 * same for the rcv ones, they are only here for the
498 SCTP_SB_CLEAR(so->so_rcv);
500 /* Now null out the reference, we are completely detached. */
504 flags = inp->sctp_flags;
505 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
506 goto sctp_must_try_again;
513 sctp_attach(struct socket *so, int proto, struct thread *p)
515 struct sctp_inpcb *inp;
516 struct inpcb *ip_inp;
518 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
525 inp = (struct sctp_inpcb *)so->so_pcb;
527 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
530 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
531 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
536 error = sctp_inpcb_alloc(so, vrf_id);
540 inp = (struct sctp_inpcb *)so->so_pcb;
542 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
543 ip_inp = &inp->ip_inp.inp;
544 ip_inp->inp_vflag |= INP_IPV4;
545 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
547 error = ipsec_init_policy(so, &ip_inp->inp_sp);
548 #ifdef SCTP_LOG_CLOSING
549 sctp_log_closing(inp, NULL, 17);
553 flags = inp->sctp_flags;
554 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
555 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
556 #ifdef SCTP_LOG_CLOSING
557 sctp_log_closing(inp, NULL, 15);
559 SCTP_INP_WUNLOCK(inp);
560 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
561 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
563 flags = inp->sctp_flags;
564 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
567 SCTP_INP_WUNLOCK(inp);
573 SCTP_INP_WUNLOCK(inp);
578 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
580 struct sctp_inpcb *inp = NULL;
584 if (addr && addr->sa_family != AF_INET) {
585 /* must be a v4 address! */
586 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
590 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
591 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
594 inp = (struct sctp_inpcb *)so->so_pcb;
596 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
599 error = sctp_inpcb_bind(so, addr, NULL, p);
604 sctp_close(struct socket *so)
606 struct sctp_inpcb *inp;
609 inp = (struct sctp_inpcb *)so->so_pcb;
614 * Inform all the lower layer assoc that we are done.
617 flags = inp->sctp_flags;
618 #ifdef SCTP_LOG_CLOSING
619 sctp_log_closing(inp, NULL, 17);
621 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
622 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
623 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
624 (so->so_rcv.sb_cc > 0)) {
625 #ifdef SCTP_LOG_CLOSING
626 sctp_log_closing(inp, NULL, 13);
628 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
629 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
631 #ifdef SCTP_LOG_CLOSING
632 sctp_log_closing(inp, NULL, 14);
634 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
635 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
638 * The socket is now detached, no matter what the state of
639 * the SCTP association.
642 SCTP_SB_CLEAR(so->so_snd);
644 * same for the rcv ones, they are only here for the
647 SCTP_SB_CLEAR(so->so_rcv);
649 /* Now null out the reference, we are completely detached. */
653 flags = inp->sctp_flags;
654 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
655 goto sctp_must_try_again;
663 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
664 struct mbuf *control, struct thread *p);
668 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
669 struct mbuf *control, struct thread *p)
671 struct sctp_inpcb *inp;
674 inp = (struct sctp_inpcb *)so->so_pcb;
677 sctp_m_freem(control);
680 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
684 /* Got to have an to address if we are NOT a connected socket */
685 if ((addr == NULL) &&
686 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
687 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
690 } else if (addr == NULL) {
691 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
692 error = EDESTADDRREQ;
695 sctp_m_freem(control);
701 if (addr->sa_family != AF_INET) {
702 /* must be a v4 address! */
703 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
706 sctp_m_freem(control);
709 error = EDESTADDRREQ;
714 /* now what about control */
717 SCTP_PRINTF("huh? control set?\n");
718 sctp_m_freem(inp->control);
721 inp->control = control;
725 SCTP_BUF_NEXT(inp->pkt_last) = m;
728 inp->pkt_last = inp->pkt = m;
731 /* FreeBSD uses a flag passed */
732 ((flags & PRUS_MORETOCOME) == 0)
735 * note with the current version this code will only be used
736 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
737 * re-defining sosend to use the sctp_sosend. One can
738 * optionally switch back to this code (by changing back the
739 * definitions) but this is not advisable. This code is used
740 * by FreeBSD when sending a file with sendfile() though.
744 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
754 sctp_disconnect(struct socket *so)
756 struct sctp_inpcb *inp;
758 inp = (struct sctp_inpcb *)so->so_pcb;
760 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
764 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
765 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
766 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
768 SCTP_INP_RUNLOCK(inp);
771 struct sctp_association *asoc;
772 struct sctp_tcb *stcb;
774 stcb = LIST_FIRST(&inp->sctp_asoc_list);
776 SCTP_INP_RUNLOCK(inp);
777 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
782 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
783 /* We are about to be freed, out of here */
784 SCTP_TCB_UNLOCK(stcb);
785 SCTP_INP_RUNLOCK(inp);
788 if (((so->so_options & SO_LINGER) &&
789 (so->so_linger == 0)) ||
790 (so->so_rcv.sb_cc > 0)) {
791 if (SCTP_GET_STATE(asoc) !=
792 SCTP_STATE_COOKIE_WAIT) {
793 /* Left with Data unread */
796 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
802 struct sctp_paramhdr *ph;
804 ph = mtod(err, struct sctp_paramhdr *);
805 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
806 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
807 ph->param_length = htons(SCTP_BUF_LEN(err));
809 #if defined(SCTP_PANIC_ON_ABORT)
810 panic("disconnect does an abort");
812 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
813 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
815 SCTP_INP_RUNLOCK(inp);
816 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
817 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
818 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
820 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
821 /* No unlock tcb assoc is gone */
824 if (TAILQ_EMPTY(&asoc->send_queue) &&
825 TAILQ_EMPTY(&asoc->sent_queue) &&
826 (asoc->stream_queue_cnt == 0)) {
827 /* there is nothing queued to send, so done */
828 if (asoc->locked_on_sending) {
831 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
832 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
833 /* only send SHUTDOWN 1st time thru */
834 sctp_stop_timers_for_shutdown(stcb);
835 sctp_send_shutdown(stcb,
836 stcb->asoc.primary_destination);
837 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
838 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
839 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
840 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
842 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
843 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
844 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
846 asoc->primary_destination);
847 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
849 asoc->primary_destination);
853 * we still got (or just got) data to send,
854 * so set SHUTDOWN_PENDING
857 * XXX sockets draft says that SCTP_EOF
858 * should be sent with no data. currently,
859 * we will allow user data to be sent first
860 * and move to SHUTDOWN-PENDING
862 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
863 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
864 asoc->primary_destination);
865 if (asoc->locked_on_sending) {
866 /* Locked to send out the data */
867 struct sctp_stream_queue_pending *sp;
869 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
871 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
872 asoc->locked_on_sending->stream_no);
874 if ((sp->length == 0) && (sp->msg_is_complete == 0))
875 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
878 if (TAILQ_EMPTY(&asoc->send_queue) &&
879 TAILQ_EMPTY(&asoc->sent_queue) &&
880 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
884 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
885 0, M_DONTWAIT, 1, MT_DATA);
891 struct sctp_paramhdr *ph;
894 SCTP_BUF_LEN(op_err) =
895 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
897 struct sctp_paramhdr *);
898 ph->param_type = htons(
899 SCTP_CAUSE_USER_INITIATED_ABT);
900 ph->param_length = htons(SCTP_BUF_LEN(op_err));
901 ippp = (uint32_t *) (ph + 1);
902 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
904 #if defined(SCTP_PANIC_ON_ABORT)
905 panic("disconnect does an abort");
908 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
909 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
910 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
911 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
912 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
913 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
915 SCTP_INP_RUNLOCK(inp);
916 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
919 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
922 soisdisconnecting(so);
923 SCTP_TCB_UNLOCK(stcb);
924 SCTP_INP_RUNLOCK(inp);
929 /* UDP model does not support this */
930 SCTP_INP_RUNLOCK(inp);
931 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
937 sctp_flush(struct socket *so, int how)
940 * We will just clear out the values and let subsequent close clear
941 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
942 * they will not be able to read the data, the socket will block
943 * that from happening.
945 struct sctp_inpcb *inp;
947 inp = (struct sctp_inpcb *)so->so_pcb;
949 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
953 /* For the 1 to many model this does nothing */
954 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
955 SCTP_INP_RUNLOCK(inp);
958 SCTP_INP_RUNLOCK(inp);
959 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
961 * First make sure the sb will be happy, we don't use these
962 * except maybe the count
965 SCTP_INP_READ_LOCK(inp);
966 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
967 SCTP_INP_READ_UNLOCK(inp);
968 SCTP_INP_WUNLOCK(inp);
969 so->so_rcv.sb_cc = 0;
970 so->so_rcv.sb_mbcnt = 0;
971 so->so_rcv.sb_mb = NULL;
973 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
975 * First make sure the sb will be happy, we don't use these
976 * except maybe the count
978 so->so_snd.sb_cc = 0;
979 so->so_snd.sb_mbcnt = 0;
980 so->so_snd.sb_mb = NULL;
987 sctp_shutdown(struct socket *so)
989 struct sctp_inpcb *inp;
991 inp = (struct sctp_inpcb *)so->so_pcb;
993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
997 /* For UDP model this is a invalid call */
998 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
999 /* Restore the flags that the soshutdown took away. */
1000 SOCKBUF_LOCK(&so->so_rcv);
1001 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1002 SOCKBUF_UNLOCK(&so->so_rcv);
1003 /* This proc will wakeup for read and do nothing (I hope) */
1004 SCTP_INP_RUNLOCK(inp);
1005 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1006 return (EOPNOTSUPP);
1009 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1010 * or SHUT_RDWR. This means we put the shutdown flag against it.
1013 struct sctp_tcb *stcb;
1014 struct sctp_association *asoc;
1017 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1018 SCTP_INP_RUNLOCK(inp);
1023 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1026 * Ok we hit the case that the shutdown call was
1027 * made after an abort or something. Nothing to do
1030 SCTP_INP_RUNLOCK(inp);
1033 SCTP_TCB_LOCK(stcb);
1035 if (TAILQ_EMPTY(&asoc->send_queue) &&
1036 TAILQ_EMPTY(&asoc->sent_queue) &&
1037 (asoc->stream_queue_cnt == 0)) {
1038 if (asoc->locked_on_sending) {
1041 /* there is nothing queued to send, so I'm done... */
1042 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1043 /* only send SHUTDOWN the first time through */
1044 sctp_stop_timers_for_shutdown(stcb);
1045 sctp_send_shutdown(stcb,
1046 stcb->asoc.primary_destination);
1047 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1048 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1049 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1050 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1052 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1053 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1054 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1055 stcb->sctp_ep, stcb,
1056 asoc->primary_destination);
1057 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1058 stcb->sctp_ep, stcb,
1059 asoc->primary_destination);
1063 * we still got (or just got) data to send, so set
1066 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1067 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1068 asoc->primary_destination);
1070 if (asoc->locked_on_sending) {
1071 /* Locked to send out the data */
1072 struct sctp_stream_queue_pending *sp;
1074 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1076 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1077 asoc->locked_on_sending->stream_no);
1079 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1080 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1084 if (TAILQ_EMPTY(&asoc->send_queue) &&
1085 TAILQ_EMPTY(&asoc->sent_queue) &&
1086 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1087 struct mbuf *op_err;
1090 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1091 0, M_DONTWAIT, 1, MT_DATA);
1093 /* Fill in the user initiated abort */
1094 struct sctp_paramhdr *ph;
1097 SCTP_BUF_LEN(op_err) =
1098 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1100 struct sctp_paramhdr *);
1101 ph->param_type = htons(
1102 SCTP_CAUSE_USER_INITIATED_ABT);
1103 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1104 ippp = (uint32_t *) (ph + 1);
1105 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1107 #if defined(SCTP_PANIC_ON_ABORT)
1108 panic("shutdown does an abort");
1110 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1111 sctp_abort_an_association(stcb->sctp_ep, stcb,
1112 SCTP_RESPONSE_TO_USER_REQ,
1113 op_err, SCTP_SO_LOCKED);
1116 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1119 SCTP_TCB_UNLOCK(stcb);
1122 SCTP_INP_RUNLOCK(inp);
1127 * copies a "user" presentable address and removes embedded scope, etc.
1128 * returns 0 on success, 1 on error
1131 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1134 struct sockaddr_in6 lsa6;
1136 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1139 memcpy(ss, sa, sa->sa_len);
1146 * NOTE: assumes addr lock is held
1149 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1150 struct sctp_tcb *stcb,
1152 struct sockaddr_storage *sas,
1155 struct sctp_ifn *sctp_ifn;
1156 struct sctp_ifa *sctp_ifa;
1157 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1159 int ipv4_addr_legal, ipv6_addr_legal;
1160 struct sctp_vrf *vrf;
1167 /* Turn on all the appropriate scope */
1168 loopback_scope = stcb->asoc.loopback_scope;
1169 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1170 local_scope = stcb->asoc.local_scope;
1171 site_scope = stcb->asoc.site_scope;
1173 /* Turn on ALL scope, since we look at the EP */
1174 loopback_scope = ipv4_local_scope = local_scope =
1177 ipv4_addr_legal = ipv6_addr_legal = 0;
1178 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1179 ipv6_addr_legal = 1;
1180 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1181 ipv4_addr_legal = 1;
1184 ipv4_addr_legal = 1;
1186 vrf = sctp_find_vrf(vrf_id);
1190 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1191 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1192 if ((loopback_scope == 0) &&
1193 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1194 /* Skip loopback if loopback_scope not set */
1197 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1200 * For the BOUND-ALL case, the list
1201 * associated with a TCB is Always
1202 * considered a reverse list.. i.e.
1203 * it lists addresses that are NOT
1204 * part of the association. If this
1205 * is one of those we must skip it.
1207 if (sctp_is_addr_restricted(stcb,
1212 switch (sctp_ifa->address.sa.sa_family) {
1214 if (ipv4_addr_legal) {
1215 struct sockaddr_in *sin;
1217 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1218 if (sin->sin_addr.s_addr == 0) {
1226 if ((ipv4_local_scope == 0) &&
1227 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1231 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1232 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1233 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1234 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1235 actual += sizeof(struct sockaddr_in6);
1238 memcpy(sas, sin, sizeof(*sin));
1239 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1240 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1241 actual += sizeof(*sin);
1245 if (actual >= limit) {
1254 if (ipv6_addr_legal) {
1255 struct sockaddr_in6 *sin6;
1257 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1258 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1266 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1267 if (local_scope == 0)
1269 if (sin6->sin6_scope_id == 0) {
1270 if (sa6_recoverscope(sin6) != 0)
1288 if ((site_scope == 0) &&
1289 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1292 memcpy(sas, sin6, sizeof(*sin6));
1293 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1294 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1295 actual += sizeof(*sin6);
1296 if (actual >= limit) {
1311 struct sctp_laddr *laddr;
1313 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1315 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1319 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1322 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1323 sas = (struct sockaddr_storage *)((caddr_t)sas +
1324 laddr->ifa->address.sa.sa_len);
1325 actual += laddr->ifa->address.sa.sa_len;
1326 if (actual >= limit) {
1335 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1336 struct sctp_tcb *stcb,
1338 struct sockaddr_storage *sas)
1342 SCTP_IPI_ADDR_RLOCK();
1343 /* fill up addresses for the endpoint's default vrf */
1344 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1346 SCTP_IPI_ADDR_RUNLOCK();
1351 * NOTE: assumes addr lock is held
1354 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1357 struct sctp_vrf *vrf = NULL;
1360 * In both sub-set bound an bound_all cases we return the MAXIMUM
1361 * number of addresses that you COULD get. In reality the sub-set
1362 * bound may have an exclusion list for a given TCB OR in the
1363 * bound-all case a TCB may NOT include the loopback or other
1364 * addresses as well.
1366 vrf = sctp_find_vrf(vrf_id);
1370 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1371 struct sctp_ifn *sctp_ifn;
1372 struct sctp_ifa *sctp_ifa;
1374 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1375 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1376 /* Count them if they are the right type */
1377 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1378 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1379 cnt += sizeof(struct sockaddr_in6);
1381 cnt += sizeof(struct sockaddr_in);
1383 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1384 cnt += sizeof(struct sockaddr_in6);
1388 struct sctp_laddr *laddr;
1390 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1391 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1392 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1393 cnt += sizeof(struct sockaddr_in6);
1395 cnt += sizeof(struct sockaddr_in);
1397 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1398 cnt += sizeof(struct sockaddr_in6);
1405 sctp_count_max_addresses(struct sctp_inpcb *inp)
1409 SCTP_IPI_ADDR_RLOCK();
1410 /* count addresses for the endpoint's default VRF */
1411 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1412 SCTP_IPI_ADDR_RUNLOCK();
1417 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1418 size_t optsize, void *p, int delay)
1421 int creat_lock_on = 0;
1422 struct sctp_tcb *stcb = NULL;
1423 struct sockaddr *sa;
1424 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1427 int bad_addresses = 0;
1430 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1432 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1433 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1434 /* We are already connected AND the TCP model */
1435 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1436 return (EADDRINUSE);
1438 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1439 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1440 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1443 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1444 SCTP_INP_RLOCK(inp);
1445 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1446 SCTP_INP_RUNLOCK(inp);
1449 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1452 SCTP_INP_INCR_REF(inp);
1453 SCTP_ASOC_CREATE_LOCK(inp);
1455 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1456 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1457 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1461 totaddrp = (int *)optval;
1462 totaddr = *totaddrp;
1463 sa = (struct sockaddr *)(totaddrp + 1);
1464 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1465 if ((stcb != NULL) || bad_addresses) {
1466 /* Already have or am bring up an association */
1467 SCTP_ASOC_CREATE_UNLOCK(inp);
1470 SCTP_TCB_UNLOCK(stcb);
1471 if (bad_addresses == 0) {
1472 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1478 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1483 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1485 struct in6pcb *inp6;
1487 inp6 = (struct in6pcb *)inp;
1488 if (SCTP_IPV6_V6ONLY(inp6)) {
1490 * if IPV6_V6ONLY flag, ignore connections destined
1491 * to a v4 addr or v4-mapped addr
1493 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1499 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1500 SCTP_PCB_FLAGS_UNBOUND) {
1501 /* Bind a ephemeral port */
1502 error = sctp_inpcb_bind(so, NULL, NULL, p);
1507 /* FIX ME: do we want to pass in a vrf on the connect call? */
1508 vrf_id = inp->def_vrf_id;
1511 /* We are GOOD to go */
1512 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1516 /* Gak! no memory */
1519 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1520 /* move to second address */
1521 if (sa->sa_family == AF_INET)
1522 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1524 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1527 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1528 /* Fill in the return id */
1530 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1533 a_id = (sctp_assoc_t *) optval;
1534 *a_id = sctp_get_associd(stcb);
1536 /* initialize authentication parameters for the assoc */
1537 sctp_initialize_auth_params(inp, stcb);
1540 /* doing delayed connection */
1541 stcb->asoc.delayed_connection = 1;
1542 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1544 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1545 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1547 SCTP_TCB_UNLOCK(stcb);
1548 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1549 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1550 /* Set the connected flag so we can queue data */
1554 if (creat_lock_on) {
1555 SCTP_ASOC_CREATE_UNLOCK(inp);
1557 SCTP_INP_DECR_REF(inp);
1561 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1562 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1563 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1564 SCTP_INP_RLOCK(inp); \
1565 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1567 SCTP_TCB_LOCK(stcb); \
1569 SCTP_INP_RUNLOCK(inp); \
1570 } else if (assoc_id != 0) { \
1571 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1572 if (stcb == NULL) { \
1573 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1583 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1584 if (size < sizeof(type)) { \
1585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1589 destp = (type *)srcp; \
1594 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1597 struct sctp_inpcb *inp = NULL;
1599 struct sctp_tcb *stcb = NULL;
1601 if (optval == NULL) {
1602 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1605 inp = (struct sctp_inpcb *)so->so_pcb;
1607 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1614 case SCTP_AUTOCLOSE:
1615 case SCTP_EXPLICIT_EOR:
1616 case SCTP_AUTO_ASCONF:
1617 case SCTP_DISABLE_FRAGMENTS:
1618 case SCTP_I_WANT_MAPPED_V4_ADDR:
1619 case SCTP_USE_EXT_RCVINFO:
1620 SCTP_INP_RLOCK(inp);
1622 case SCTP_DISABLE_FRAGMENTS:
1623 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1625 case SCTP_I_WANT_MAPPED_V4_ADDR:
1626 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1628 case SCTP_AUTO_ASCONF:
1629 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1630 /* only valid for bound all sockets */
1631 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1638 case SCTP_EXPLICIT_EOR:
1639 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1642 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1644 case SCTP_USE_EXT_RCVINFO:
1645 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1647 case SCTP_AUTOCLOSE:
1648 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1649 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1655 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1656 error = ENOPROTOOPT;
1657 } /* end switch (sopt->sopt_name) */
1658 if (optname != SCTP_AUTOCLOSE) {
1659 /* make it an "on/off" value */
1662 if (*optsize < sizeof(val)) {
1663 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1667 SCTP_INP_RUNLOCK(inp);
1669 /* return the option value */
1670 *(int *)optval = val;
1671 *optsize = sizeof(val);
1674 case SCTP_GET_PACKET_LOG:
1676 #ifdef SCTP_PACKET_LOGGING
1680 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1681 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1684 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1689 case SCTP_REUSE_PORT:
1693 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1694 /* Can't do this for a 1-m socket */
1698 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1699 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1700 *optsize = sizeof(uint32_t);
1703 case SCTP_PARTIAL_DELIVERY_POINT:
1707 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1708 *value = inp->partial_delivery_point;
1709 *optsize = sizeof(uint32_t);
1712 case SCTP_FRAGMENT_INTERLEAVE:
1716 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1717 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1718 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1719 *value = SCTP_FRAG_LEVEL_2;
1721 *value = SCTP_FRAG_LEVEL_1;
1724 *value = SCTP_FRAG_LEVEL_0;
1726 *optsize = sizeof(uint32_t);
1729 case SCTP_CMT_ON_OFF:
1731 struct sctp_assoc_value *av;
1733 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1734 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1736 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1737 SCTP_TCB_UNLOCK(stcb);
1739 SCTP_INP_RLOCK(inp);
1740 av->assoc_value = inp->sctp_cmt_on_off;
1741 SCTP_INP_RUNLOCK(inp);
1743 *optsize = sizeof(*av);
1746 /* JRS - Get socket option for pluggable congestion control */
1747 case SCTP_PLUGGABLE_CC:
1749 struct sctp_assoc_value *av;
1751 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1752 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1754 av->assoc_value = stcb->asoc.congestion_control_module;
1755 SCTP_TCB_UNLOCK(stcb);
1757 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1759 *optsize = sizeof(*av);
1762 /* RS - Get socket option for pluggable stream scheduling */
1763 case SCTP_PLUGGABLE_SS:
1765 struct sctp_assoc_value *av;
1767 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1768 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1770 av->assoc_value = stcb->asoc.stream_scheduling_module;
1771 SCTP_TCB_UNLOCK(stcb);
1773 av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1775 *optsize = sizeof(*av);
1780 struct sctp_stream_value *av;
1782 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1783 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1785 if (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1786 &av->stream_value) < 0) {
1787 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1790 *optsize = sizeof(*av);
1792 SCTP_TCB_UNLOCK(stcb);
1795 * Can't get stream value without
1798 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1803 case SCTP_GET_ADDR_LEN:
1805 struct sctp_assoc_value *av;
1807 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1810 if (av->assoc_value == AF_INET) {
1811 av->assoc_value = sizeof(struct sockaddr_in);
1816 if (av->assoc_value == AF_INET6) {
1817 av->assoc_value = sizeof(struct sockaddr_in6);
1822 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1824 *optsize = sizeof(*av);
1827 case SCTP_GET_ASSOC_NUMBER:
1829 uint32_t *value, cnt;
1831 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1833 SCTP_INP_RLOCK(inp);
1834 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1837 SCTP_INP_RUNLOCK(inp);
1839 *optsize = sizeof(uint32_t);
1843 case SCTP_GET_ASSOC_ID_LIST:
1845 struct sctp_assoc_ids *ids;
1846 unsigned int at, limit;
1848 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1850 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1851 SCTP_INP_RLOCK(inp);
1852 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1854 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1861 SCTP_INP_RUNLOCK(inp);
1862 ids->gaids_number_of_ids = at;
1863 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1868 struct sctp_assoc_value *av;
1870 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1871 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1874 av->assoc_value = stcb->asoc.context;
1875 SCTP_TCB_UNLOCK(stcb);
1877 SCTP_INP_RLOCK(inp);
1878 av->assoc_value = inp->sctp_context;
1879 SCTP_INP_RUNLOCK(inp);
1881 *optsize = sizeof(*av);
1886 uint32_t *default_vrfid;
1888 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1889 *default_vrfid = inp->def_vrf_id;
1892 case SCTP_GET_ASOC_VRF:
1894 struct sctp_assoc_value *id;
1896 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1897 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1900 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1903 id->assoc_value = stcb->asoc.vrf_id;
1906 case SCTP_GET_VRF_IDS:
1908 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1912 case SCTP_GET_NONCE_VALUES:
1914 struct sctp_get_nonce_values *gnv;
1916 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1917 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1920 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1921 gnv->gn_local_tag = stcb->asoc.my_vtag;
1922 SCTP_TCB_UNLOCK(stcb);
1924 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1927 *optsize = sizeof(*gnv);
1930 case SCTP_DELAYED_SACK:
1932 struct sctp_sack_info *sack;
1934 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1935 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1937 sack->sack_delay = stcb->asoc.delayed_ack;
1938 sack->sack_freq = stcb->asoc.sack_freq;
1939 SCTP_TCB_UNLOCK(stcb);
1941 SCTP_INP_RLOCK(inp);
1942 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1943 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1944 SCTP_INP_RUNLOCK(inp);
1946 *optsize = sizeof(*sack);
1950 case SCTP_GET_SNDBUF_USE:
1952 struct sctp_sockstat *ss;
1954 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1955 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1958 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1959 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1960 stcb->asoc.size_on_all_streams);
1961 SCTP_TCB_UNLOCK(stcb);
1963 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1966 *optsize = sizeof(struct sctp_sockstat);
1969 case SCTP_MAX_BURST:
1973 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1975 SCTP_INP_RLOCK(inp);
1976 if (inp->sctp_ep.max_burst < 256) {
1977 *value = inp->sctp_ep.max_burst;
1981 SCTP_INP_RUNLOCK(inp);
1982 *optsize = sizeof(uint8_t);
1987 struct sctp_assoc_value *av;
1990 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1991 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1994 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1995 SCTP_TCB_UNLOCK(stcb);
1997 SCTP_INP_RLOCK(inp);
1998 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1999 ovh = SCTP_MED_OVERHEAD;
2001 ovh = SCTP_MED_V4_OVERHEAD;
2003 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2004 av->assoc_value = 0;
2006 av->assoc_value = inp->sctp_frag_point - ovh;
2007 SCTP_INP_RUNLOCK(inp);
2009 *optsize = sizeof(struct sctp_assoc_value);
2012 case SCTP_GET_STAT_LOG:
2013 error = sctp_fill_stat_log(optval, optsize);
2017 struct sctp_event_subscribe *events;
2019 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2020 memset(events, 0, sizeof(*events));
2021 SCTP_INP_RLOCK(inp);
2022 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2023 events->sctp_data_io_event = 1;
2025 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2026 events->sctp_association_event = 1;
2028 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2029 events->sctp_address_event = 1;
2031 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2032 events->sctp_send_failure_event = 1;
2034 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2035 events->sctp_peer_error_event = 1;
2037 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2038 events->sctp_shutdown_event = 1;
2040 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2041 events->sctp_partial_delivery_event = 1;
2043 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2044 events->sctp_adaptation_layer_event = 1;
2046 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2047 events->sctp_authentication_event = 1;
2049 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2050 events->sctp_sender_dry_event = 1;
2052 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2053 events->sctp_stream_reset_event = 1;
2054 SCTP_INP_RUNLOCK(inp);
2055 *optsize = sizeof(struct sctp_event_subscribe);
2059 case SCTP_ADAPTATION_LAYER:
2063 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2065 SCTP_INP_RLOCK(inp);
2066 *value = inp->sctp_ep.adaptation_layer_indicator;
2067 SCTP_INP_RUNLOCK(inp);
2068 *optsize = sizeof(uint32_t);
2071 case SCTP_SET_INITIAL_DBG_SEQ:
2075 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2076 SCTP_INP_RLOCK(inp);
2077 *value = inp->sctp_ep.initial_sequence_debug;
2078 SCTP_INP_RUNLOCK(inp);
2079 *optsize = sizeof(uint32_t);
2082 case SCTP_GET_LOCAL_ADDR_SIZE:
2086 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2087 SCTP_INP_RLOCK(inp);
2088 *value = sctp_count_max_addresses(inp);
2089 SCTP_INP_RUNLOCK(inp);
2090 *optsize = sizeof(uint32_t);
2093 case SCTP_GET_REMOTE_ADDR_SIZE:
2097 struct sctp_nets *net;
2099 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2100 /* FIXME MT: change to sctp_assoc_value? */
2101 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2105 /* Count the sizes */
2106 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2107 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2108 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2109 size += sizeof(struct sockaddr_in6);
2110 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2111 size += sizeof(struct sockaddr_in);
2117 SCTP_TCB_UNLOCK(stcb);
2118 *value = (uint32_t) size;
2120 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2123 *optsize = sizeof(uint32_t);
2126 case SCTP_GET_PEER_ADDRESSES:
2128 * Get the address information, an array is passed in to
2129 * fill up we pack it.
2133 struct sockaddr_storage *sas;
2134 struct sctp_nets *net;
2135 struct sctp_getaddresses *saddr;
2137 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2138 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2141 left = (*optsize) - sizeof(struct sctp_getaddresses);
2142 *optsize = sizeof(struct sctp_getaddresses);
2143 sas = (struct sockaddr_storage *)&saddr->addr[0];
2145 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2146 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2147 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2148 cpsz = sizeof(struct sockaddr_in6);
2149 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2150 cpsz = sizeof(struct sockaddr_in);
2156 /* not enough room. */
2160 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2161 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2162 /* Must map the address */
2163 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2164 (struct sockaddr_in6 *)sas);
2167 memcpy(sas, &net->ro._l_addr, cpsz);
2171 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2173 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2177 SCTP_TCB_UNLOCK(stcb);
2179 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2184 case SCTP_GET_LOCAL_ADDRESSES:
2186 size_t limit, actual;
2187 struct sockaddr_storage *sas;
2188 struct sctp_getaddresses *saddr;
2190 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2191 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2193 sas = (struct sockaddr_storage *)&saddr->addr[0];
2194 limit = *optsize - sizeof(sctp_assoc_t);
2195 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2197 SCTP_TCB_UNLOCK(stcb);
2199 *optsize = sizeof(struct sockaddr_storage) + actual;
2202 case SCTP_PEER_ADDR_PARAMS:
2204 struct sctp_paddrparams *paddrp;
2205 struct sctp_nets *net;
2207 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2208 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2212 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2215 * We increment here since
2216 * sctp_findassociation_ep_addr() wil do a
2217 * decrement if it finds the stcb as long as
2218 * the locked tcb (last argument) is NOT a
2221 SCTP_INP_INCR_REF(inp);
2222 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2224 SCTP_INP_DECR_REF(inp);
2227 if (stcb && (net == NULL)) {
2228 struct sockaddr *sa;
2230 sa = (struct sockaddr *)&paddrp->spp_address;
2231 if (sa->sa_family == AF_INET) {
2232 struct sockaddr_in *sin;
2234 sin = (struct sockaddr_in *)sa;
2235 if (sin->sin_addr.s_addr) {
2237 SCTP_TCB_UNLOCK(stcb);
2238 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2241 } else if (sa->sa_family == AF_INET6) {
2242 struct sockaddr_in6 *sin6;
2244 sin6 = (struct sockaddr_in6 *)sa;
2245 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2247 SCTP_TCB_UNLOCK(stcb);
2248 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2252 error = EAFNOSUPPORT;
2253 SCTP_TCB_UNLOCK(stcb);
2254 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2259 /* Applys to the specific association */
2260 paddrp->spp_flags = 0;
2264 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2265 ovh = SCTP_MED_OVERHEAD;
2267 ovh = SCTP_MED_V4_OVERHEAD;
2271 paddrp->spp_pathmaxrxt = net->failure_threshold;
2272 paddrp->spp_pathmtu = net->mtu - ovh;
2273 /* get flags for HB */
2274 if (net->dest_state & SCTP_ADDR_NOHB)
2275 paddrp->spp_flags |= SPP_HB_DISABLE;
2277 paddrp->spp_flags |= SPP_HB_ENABLE;
2278 /* get flags for PMTU */
2279 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2280 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2282 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2285 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2286 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2287 paddrp->spp_flags |= SPP_IPV4_TOS;
2291 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2292 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2293 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2298 * No destination so return default
2303 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2304 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2306 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2307 paddrp->spp_flags |= SPP_IPV4_TOS;
2310 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2311 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2313 /* default settings should be these */
2314 if (stcb->asoc.hb_is_disabled == 0) {
2315 paddrp->spp_flags |= SPP_HB_ENABLE;
2317 paddrp->spp_flags |= SPP_HB_DISABLE;
2319 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2320 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2325 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2328 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2329 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2330 SCTP_TCB_UNLOCK(stcb);
2332 /* Use endpoint defaults */
2333 SCTP_INP_RLOCK(inp);
2334 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2335 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2336 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2337 /* get inp's default */
2339 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2340 paddrp->spp_flags |= SPP_IPV4_TOS;
2343 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2344 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2345 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2348 /* can't return this */
2349 paddrp->spp_pathmtu = 0;
2351 /* default behavior, no stcb */
2352 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2354 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2355 paddrp->spp_flags |= SPP_HB_ENABLE;
2357 paddrp->spp_flags |= SPP_HB_DISABLE;
2359 SCTP_INP_RUNLOCK(inp);
2361 *optsize = sizeof(struct sctp_paddrparams);
2364 case SCTP_GET_PEER_ADDR_INFO:
2366 struct sctp_paddrinfo *paddri;
2367 struct sctp_nets *net;
2369 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2370 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2374 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2377 * We increment here since
2378 * sctp_findassociation_ep_addr() wil do a
2379 * decrement if it finds the stcb as long as
2380 * the locked tcb (last argument) is NOT a
2383 SCTP_INP_INCR_REF(inp);
2384 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2386 SCTP_INP_DECR_REF(inp);
2390 if ((stcb) && (net)) {
2391 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2392 /* It's unconfirmed */
2393 paddri->spinfo_state = SCTP_UNCONFIRMED;
2394 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2396 paddri->spinfo_state = SCTP_ACTIVE;
2399 paddri->spinfo_state = SCTP_INACTIVE;
2401 paddri->spinfo_cwnd = net->cwnd;
2402 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2403 paddri->spinfo_rto = net->RTO;
2404 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2405 SCTP_TCB_UNLOCK(stcb);
2408 SCTP_TCB_UNLOCK(stcb);
2410 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2413 *optsize = sizeof(struct sctp_paddrinfo);
2416 case SCTP_PCB_STATUS:
2418 struct sctp_pcbinfo *spcb;
2420 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2421 sctp_fill_pcbinfo(spcb);
2422 *optsize = sizeof(struct sctp_pcbinfo);
2428 struct sctp_nets *net;
2429 struct sctp_status *sstat;
2431 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2432 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2435 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2440 * I think passing the state is fine since
2441 * sctp_constants.h will be available to the user
2444 sstat->sstat_state = stcb->asoc.state;
2445 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2446 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2447 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2449 * We can't include chunks that have been passed to
2450 * the socket layer. Only things in queue.
2452 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2453 stcb->asoc.cnt_on_all_streams);
2456 sstat->sstat_instrms = stcb->asoc.streamincnt;
2457 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2458 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2459 memcpy(&sstat->sstat_primary.spinfo_address,
2460 &stcb->asoc.primary_destination->ro._l_addr,
2461 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2462 net = stcb->asoc.primary_destination;
2463 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2465 * Again the user can get info from sctp_constants.h
2466 * for what the state of the network is.
2468 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2469 /* It's unconfirmed */
2470 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2471 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2473 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2476 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2478 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2479 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2480 sstat->sstat_primary.spinfo_rto = net->RTO;
2481 sstat->sstat_primary.spinfo_mtu = net->mtu;
2482 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2483 SCTP_TCB_UNLOCK(stcb);
2484 *optsize = sizeof(*sstat);
2489 struct sctp_rtoinfo *srto;
2491 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2492 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2495 srto->srto_initial = stcb->asoc.initial_rto;
2496 srto->srto_max = stcb->asoc.maxrto;
2497 srto->srto_min = stcb->asoc.minrto;
2498 SCTP_TCB_UNLOCK(stcb);
2500 SCTP_INP_RLOCK(inp);
2501 srto->srto_initial = inp->sctp_ep.initial_rto;
2502 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2503 srto->srto_min = inp->sctp_ep.sctp_minrto;
2504 SCTP_INP_RUNLOCK(inp);
2506 *optsize = sizeof(*srto);
2511 struct sctp_timeouts *stimo;
2513 SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2514 SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2517 stimo->stimo_init = stcb->asoc.timoinit;
2518 stimo->stimo_data = stcb->asoc.timodata;
2519 stimo->stimo_sack = stcb->asoc.timosack;
2520 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2521 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2522 stimo->stimo_cookie = stcb->asoc.timocookie;
2523 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2524 SCTP_TCB_UNLOCK(stcb);
2526 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2529 *optsize = sizeof(*stimo);
2532 case SCTP_ASSOCINFO:
2534 struct sctp_assocparams *sasoc;
2537 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2538 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2541 oldval = sasoc->sasoc_cookie_life;
2542 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2543 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2544 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2545 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2546 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2547 SCTP_TCB_UNLOCK(stcb);
2549 SCTP_INP_RLOCK(inp);
2550 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2551 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2552 sasoc->sasoc_number_peer_destinations = 0;
2553 sasoc->sasoc_peer_rwnd = 0;
2554 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2555 SCTP_INP_RUNLOCK(inp);
2557 *optsize = sizeof(*sasoc);
2560 case SCTP_DEFAULT_SEND_PARAM:
2562 struct sctp_sndrcvinfo *s_info;
2564 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2565 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2568 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2569 SCTP_TCB_UNLOCK(stcb);
2571 SCTP_INP_RLOCK(inp);
2572 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2573 SCTP_INP_RUNLOCK(inp);
2575 *optsize = sizeof(*s_info);
2580 struct sctp_initmsg *sinit;
2582 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2583 SCTP_INP_RLOCK(inp);
2584 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2585 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2586 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2587 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2588 SCTP_INP_RUNLOCK(inp);
2589 *optsize = sizeof(*sinit);
2592 case SCTP_PRIMARY_ADDR:
2593 /* we allow a "get" operation on this */
2595 struct sctp_setprim *ssp;
2597 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2598 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2601 /* simply copy out the sockaddr_storage... */
2605 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2606 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2608 memcpy(&ssp->ssp_addr,
2609 &stcb->asoc.primary_destination->ro._l_addr,
2611 SCTP_TCB_UNLOCK(stcb);
2613 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2616 *optsize = sizeof(*ssp);
2620 case SCTP_HMAC_IDENT:
2622 struct sctp_hmacalgo *shmac;
2623 sctp_hmaclist_t *hmaclist;
2627 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2629 SCTP_INP_RLOCK(inp);
2630 hmaclist = inp->sctp_ep.local_hmacs;
2631 if (hmaclist == NULL) {
2632 /* no HMACs to return */
2633 *optsize = sizeof(*shmac);
2634 SCTP_INP_RUNLOCK(inp);
2637 /* is there room for all of the hmac ids? */
2638 size = sizeof(*shmac) + (hmaclist->num_algo *
2639 sizeof(shmac->shmac_idents[0]));
2640 if ((size_t)(*optsize) < size) {
2641 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2643 SCTP_INP_RUNLOCK(inp);
2646 /* copy in the list */
2647 shmac->shmac_number_of_idents = hmaclist->num_algo;
2648 for (i = 0; i < hmaclist->num_algo; i++) {
2649 shmac->shmac_idents[i] = hmaclist->hmac[i];
2651 SCTP_INP_RUNLOCK(inp);
2655 case SCTP_AUTH_ACTIVE_KEY:
2657 struct sctp_authkeyid *scact;
2659 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2660 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2663 /* get the active key on the assoc */
2664 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2665 SCTP_TCB_UNLOCK(stcb);
2667 /* get the endpoint active key */
2668 SCTP_INP_RLOCK(inp);
2669 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2670 SCTP_INP_RUNLOCK(inp);
2672 *optsize = sizeof(*scact);
2675 case SCTP_LOCAL_AUTH_CHUNKS:
2677 struct sctp_authchunks *sac;
2678 sctp_auth_chklist_t *chklist = NULL;
2681 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2682 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2685 /* get off the assoc */
2686 chklist = stcb->asoc.local_auth_chunks;
2687 /* is there enough space? */
2688 size = sctp_auth_get_chklist_size(chklist);
2689 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2691 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2693 /* copy in the chunks */
2694 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2696 SCTP_TCB_UNLOCK(stcb);
2698 /* get off the endpoint */
2699 SCTP_INP_RLOCK(inp);
2700 chklist = inp->sctp_ep.local_auth_chunks;
2701 /* is there enough space? */
2702 size = sctp_auth_get_chklist_size(chklist);
2703 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2705 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2707 /* copy in the chunks */
2708 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2710 SCTP_INP_RUNLOCK(inp);
2712 *optsize = sizeof(struct sctp_authchunks) + size;
2715 case SCTP_PEER_AUTH_CHUNKS:
2717 struct sctp_authchunks *sac;
2718 sctp_auth_chklist_t *chklist = NULL;
2721 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2722 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2725 /* get off the assoc */
2726 chklist = stcb->asoc.peer_auth_chunks;
2727 /* is there enough space? */
2728 size = sctp_auth_get_chklist_size(chklist);
2729 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2731 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2733 /* copy in the chunks */
2734 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2736 SCTP_TCB_UNLOCK(stcb);
2738 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2741 *optsize = sizeof(struct sctp_authchunks) + size;
2747 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2748 error = ENOPROTOOPT;
2751 } /* end switch (sopt->sopt_name) */
2756 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2761 struct sctp_tcb *stcb = NULL;
2762 struct sctp_inpcb *inp = NULL;
2765 if (optval == NULL) {
2766 SCTP_PRINTF("optval is NULL\n");
2767 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2770 inp = (struct sctp_inpcb *)so->so_pcb;
2772 SCTP_PRINTF("inp is NULL?\n");
2773 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2776 vrf_id = inp->def_vrf_id;
2781 case SCTP_AUTOCLOSE:
2782 case SCTP_AUTO_ASCONF:
2783 case SCTP_EXPLICIT_EOR:
2784 case SCTP_DISABLE_FRAGMENTS:
2785 case SCTP_USE_EXT_RCVINFO:
2786 case SCTP_I_WANT_MAPPED_V4_ADDR:
2787 /* copy in the option value */
2788 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2793 case SCTP_DISABLE_FRAGMENTS:
2794 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2796 case SCTP_AUTO_ASCONF:
2798 * NOTE: we don't really support this flag
2800 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2801 /* only valid for bound all sockets */
2802 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2804 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2808 case SCTP_EXPLICIT_EOR:
2809 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2811 case SCTP_USE_EXT_RCVINFO:
2812 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2814 case SCTP_I_WANT_MAPPED_V4_ADDR:
2815 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2816 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2818 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2823 set_opt = SCTP_PCB_FLAGS_NODELAY;
2825 case SCTP_AUTOCLOSE:
2826 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2827 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2828 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2831 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2833 * The value is in ticks. Note this does not effect
2834 * old associations, only new ones.
2836 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2839 SCTP_INP_WLOCK(inp);
2841 sctp_feature_on(inp, set_opt);
2843 sctp_feature_off(inp, set_opt);
2845 SCTP_INP_WUNLOCK(inp);
2847 case SCTP_REUSE_PORT:
2849 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2850 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2851 /* Can't set it after we are bound */
2855 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2856 /* Can't do this for a 1-m socket */
2861 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2863 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2866 case SCTP_PARTIAL_DELIVERY_POINT:
2870 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2871 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2872 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2876 inp->partial_delivery_point = *value;
2879 case SCTP_FRAGMENT_INTERLEAVE:
2880 /* not yet until we re-write sctp_recvmsg() */
2884 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2885 if (*level == SCTP_FRAG_LEVEL_2) {
2886 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2887 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2888 } else if (*level == SCTP_FRAG_LEVEL_1) {
2889 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2890 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2891 } else if (*level == SCTP_FRAG_LEVEL_0) {
2892 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2893 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2896 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2901 case SCTP_CMT_ON_OFF:
2902 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2903 struct sctp_assoc_value *av;
2905 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2906 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2908 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
2909 if (stcb->asoc.sctp_cmt_on_off > 2) {
2910 stcb->asoc.sctp_cmt_on_off = 2;
2912 SCTP_TCB_UNLOCK(stcb);
2914 SCTP_INP_WLOCK(inp);
2915 inp->sctp_cmt_on_off = av->assoc_value;
2916 if (inp->sctp_cmt_on_off > 2) {
2917 inp->sctp_cmt_on_off = 2;
2919 SCTP_INP_WUNLOCK(inp);
2922 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2923 error = ENOPROTOOPT;
2926 /* JRS - Set socket option for pluggable congestion control */
2927 case SCTP_PLUGGABLE_CC:
2929 struct sctp_assoc_value *av;
2931 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2932 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2934 switch (av->assoc_value) {
2935 case SCTP_CC_RFC2581:
2938 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
2939 stcb->asoc.congestion_control_module = av->assoc_value;
2942 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2946 SCTP_TCB_UNLOCK(stcb);
2948 switch (av->assoc_value) {
2949 case SCTP_CC_RFC2581:
2952 SCTP_INP_WLOCK(inp);
2953 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2954 SCTP_INP_WUNLOCK(inp);
2957 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2964 /* RS - Set socket option for pluggable stream scheduling */
2965 case SCTP_PLUGGABLE_SS:
2967 struct sctp_assoc_value *av;
2969 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2970 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2972 switch (av->assoc_value) {
2973 case SCTP_SS_DEFAULT:
2974 case SCTP_SS_ROUND_ROBIN:
2975 case SCTP_SS_ROUND_ROBIN_PACKET:
2976 case SCTP_SS_PRIORITY:
2977 case SCTP_SS_FAIR_BANDWITH:
2978 case SCTP_SS_FIRST_COME:
2979 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
2980 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
2981 stcb->asoc.stream_scheduling_module = av->assoc_value;
2982 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
2985 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2989 SCTP_TCB_UNLOCK(stcb);
2991 switch (av->assoc_value) {
2992 case SCTP_SS_DEFAULT:
2993 case SCTP_SS_ROUND_ROBIN:
2994 case SCTP_SS_ROUND_ROBIN_PACKET:
2995 case SCTP_SS_PRIORITY:
2996 case SCTP_SS_FAIR_BANDWITH:
2997 case SCTP_SS_FIRST_COME:
2998 SCTP_INP_WLOCK(inp);
2999 inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
3000 SCTP_INP_WUNLOCK(inp);
3003 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3012 struct sctp_stream_value *av;
3014 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
3015 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3017 if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
3018 av->stream_value) < 0) {
3019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3022 SCTP_TCB_UNLOCK(stcb);
3025 * Can't set stream value without
3028 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3033 case SCTP_CLR_STAT_LOG:
3034 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3039 struct sctp_assoc_value *av;
3041 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3042 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3045 stcb->asoc.context = av->assoc_value;
3046 SCTP_TCB_UNLOCK(stcb);
3048 SCTP_INP_WLOCK(inp);
3049 inp->sctp_context = av->assoc_value;
3050 SCTP_INP_WUNLOCK(inp);
3056 uint32_t *default_vrfid;
3058 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
3059 if (*default_vrfid > SCTP_MAX_VRF_ID) {
3060 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3064 inp->def_vrf_id = *default_vrfid;
3067 case SCTP_DEL_VRF_ID:
3069 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3073 case SCTP_ADD_VRF_ID:
3075 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3079 case SCTP_DELAYED_SACK:
3081 struct sctp_sack_info *sack;
3083 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
3084 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
3085 if (sack->sack_delay) {
3086 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3087 sack->sack_delay = SCTP_MAX_SACK_DELAY;
3090 if (sack->sack_delay) {
3091 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3092 sack->sack_delay = TICKS_TO_MSEC(1);
3094 stcb->asoc.delayed_ack = sack->sack_delay;
3096 if (sack->sack_freq) {
3097 stcb->asoc.sack_freq = sack->sack_freq;
3099 SCTP_TCB_UNLOCK(stcb);
3101 SCTP_INP_WLOCK(inp);
3102 if (sack->sack_delay) {
3103 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3104 sack->sack_delay = TICKS_TO_MSEC(1);
3106 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3108 if (sack->sack_freq) {
3109 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3111 SCTP_INP_WUNLOCK(inp);
3115 case SCTP_AUTH_CHUNK:
3117 struct sctp_authchunk *sauth;
3119 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3121 SCTP_INP_WLOCK(inp);
3122 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3123 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3126 SCTP_INP_WUNLOCK(inp);
3131 struct sctp_authkey *sca;
3132 struct sctp_keyhead *shared_keys;
3133 sctp_sharedkey_t *shared_key;
3134 sctp_key_t *key = NULL;
3137 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3138 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3139 size = optsize - sizeof(*sca);
3142 /* set it on the assoc */
3143 shared_keys = &stcb->asoc.shared_keys;
3144 /* clear the cached keys for this key id */
3145 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3147 * create the new shared key and
3151 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3153 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3155 SCTP_TCB_UNLOCK(stcb);
3159 shared_key = sctp_alloc_sharedkey();
3160 if (shared_key == NULL) {
3162 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3164 SCTP_TCB_UNLOCK(stcb);
3167 shared_key->key = key;
3168 shared_key->keyid = sca->sca_keynumber;
3169 error = sctp_insert_sharedkey(shared_keys, shared_key);
3170 SCTP_TCB_UNLOCK(stcb);
3172 /* set it on the endpoint */
3173 SCTP_INP_WLOCK(inp);
3174 shared_keys = &inp->sctp_ep.shared_keys;
3176 * clear the cached keys on all assocs for
3179 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3181 * create the new shared key and
3185 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3187 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3189 SCTP_INP_WUNLOCK(inp);
3193 shared_key = sctp_alloc_sharedkey();
3194 if (shared_key == NULL) {
3196 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3198 SCTP_INP_WUNLOCK(inp);
3201 shared_key->key = key;
3202 shared_key->keyid = sca->sca_keynumber;
3203 error = sctp_insert_sharedkey(shared_keys, shared_key);
3204 SCTP_INP_WUNLOCK(inp);
3208 case SCTP_HMAC_IDENT:
3210 struct sctp_hmacalgo *shmac;
3211 sctp_hmaclist_t *hmaclist;
3217 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3218 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3219 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3223 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3224 if (hmaclist == NULL) {
3225 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3229 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3230 hmacid = shmac->shmac_idents[i];
3231 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3232 /* invalid HMACs were found */ ;
3233 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3235 sctp_free_hmaclist(hmaclist);
3236 goto sctp_set_hmac_done;
3240 for (i = 0; i < hmaclist->num_algo; i++) {
3241 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3242 /* already in list */
3247 sctp_free_hmaclist(hmaclist);
3248 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3252 /* set it on the endpoint */
3253 SCTP_INP_WLOCK(inp);
3254 if (inp->sctp_ep.local_hmacs)
3255 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3256 inp->sctp_ep.local_hmacs = hmaclist;
3257 SCTP_INP_WUNLOCK(inp);
3261 case SCTP_AUTH_ACTIVE_KEY:
3263 struct sctp_authkeyid *scact;
3265 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3267 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3269 /* set the active key on the right place */
3271 /* set the active key on the assoc */
3272 if (sctp_auth_setactivekey(stcb,
3273 scact->scact_keynumber)) {
3274 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3275 SCTP_FROM_SCTP_USRREQ,
3279 SCTP_TCB_UNLOCK(stcb);
3281 /* set the active key on the endpoint */
3282 SCTP_INP_WLOCK(inp);
3283 if (sctp_auth_setactivekey_ep(inp,
3284 scact->scact_keynumber)) {
3285 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3286 SCTP_FROM_SCTP_USRREQ,
3290 SCTP_INP_WUNLOCK(inp);
3294 case SCTP_AUTH_DELETE_KEY:
3296 struct sctp_authkeyid *scdel;
3298 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3300 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3302 /* delete the key from the right place */
3304 if (sctp_delete_sharedkey(stcb,
3305 scdel->scact_keynumber)) {
3306 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3307 SCTP_FROM_SCTP_USRREQ,
3311 SCTP_TCB_UNLOCK(stcb);
3313 SCTP_INP_WLOCK(inp);
3314 if (sctp_delete_sharedkey_ep(inp,
3315 scdel->scact_keynumber)) {
3316 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3317 SCTP_FROM_SCTP_USRREQ,
3321 SCTP_INP_WUNLOCK(inp);
3325 case SCTP_AUTH_DEACTIVATE_KEY:
3327 struct sctp_authkeyid *keyid;
3329 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3331 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3333 /* deactivate the key from the right place */
3335 if (sctp_deact_sharedkey(stcb,
3336 keyid->scact_keynumber)) {
3337 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3338 SCTP_FROM_SCTP_USRREQ,
3342 SCTP_TCB_UNLOCK(stcb);
3344 SCTP_INP_WLOCK(inp);
3345 if (sctp_deact_sharedkey_ep(inp,
3346 keyid->scact_keynumber)) {
3347 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3348 SCTP_FROM_SCTP_USRREQ,
3352 SCTP_INP_WUNLOCK(inp);
3357 case SCTP_RESET_STREAMS:
3359 struct sctp_stream_reset *strrst;
3360 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3362 uint16_t addstrmcnt = 0;
3365 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3366 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3369 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3373 if (stcb->asoc.peer_supports_strreset == 0) {
3375 * Peer does not support it, we return
3376 * protocol not supported since this is true
3377 * for this feature and this peer, not the
3378 * socket request in general.
3380 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3381 error = EPROTONOSUPPORT;
3382 SCTP_TCB_UNLOCK(stcb);
3385 if (stcb->asoc.stream_reset_outstanding) {
3386 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3388 SCTP_TCB_UNLOCK(stcb);
3391 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3393 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3395 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3398 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3400 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3404 /* We can't do that and add streams */
3408 if (stcb->asoc.stream_reset_outstanding) {
3413 /* We allocate here */
3414 addstrmcnt = strrst->strrst_num_streams;
3415 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3416 /* You can't have more than 64k */
3420 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3421 /* Need to allocate more */
3422 struct sctp_stream_out *oldstream;
3423 struct sctp_stream_queue_pending *sp,
3426 oldstream = stcb->asoc.strmout;
3428 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3429 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3431 if (stcb->asoc.strmout == NULL) {
3432 stcb->asoc.strmout = oldstream;
3437 * Ok now we proceed with copying
3438 * the old out stuff and
3439 * initializing the new stuff.
3441 SCTP_TCB_SEND_LOCK(stcb);
3442 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
3443 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3444 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3445 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3446 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3447 stcb->asoc.strmout[i].stream_no = i;
3448 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
3450 * now anything on those
3453 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
3454 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3455 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3458 * Now move assoc pointers
3461 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3462 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3464 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3465 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3468 /* now the new streams */
3469 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3470 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3471 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3472 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3473 stcb->asoc.strmout[i].stream_no = i;
3474 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3475 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3477 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3478 SCTP_FREE(oldstream, SCTP_M_STRMO);
3480 SCTP_TCB_SEND_UNLOCK(stcb);
3483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3485 SCTP_TCB_UNLOCK(stcb);
3488 for (i = 0; i < strrst->strrst_num_streams; i++) {
3491 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3492 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3497 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3498 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3506 SCTP_TCB_UNLOCK(stcb);
3509 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3510 strrst->strrst_list,
3511 send_out, (stcb->asoc.str_reset_seq_in - 3),
3512 send_in, send_tsn, addstream, addstrmcnt);
3514 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3515 SCTP_TCB_UNLOCK(stcb);
3519 case SCTP_CONNECT_X:
3520 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3521 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3525 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3528 case SCTP_CONNECT_X_DELAYED:
3529 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3530 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3534 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3537 case SCTP_CONNECT_X_COMPLETE:
3539 struct sockaddr *sa;
3540 struct sctp_nets *net;
3542 /* FIXME MT: check correct? */
3543 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3546 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3547 SCTP_INP_RLOCK(inp);
3548 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3550 SCTP_TCB_LOCK(stcb);
3551 net = sctp_findnet(stcb, sa);
3553 SCTP_INP_RUNLOCK(inp);
3556 * We increment here since
3557 * sctp_findassociation_ep_addr() wil do a
3558 * decrement if it finds the stcb as long as
3559 * the locked tcb (last argument) is NOT a
3562 SCTP_INP_INCR_REF(inp);
3563 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3565 SCTP_INP_DECR_REF(inp);
3570 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3574 if (stcb->asoc.delayed_connection == 1) {
3575 stcb->asoc.delayed_connection = 0;
3576 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3577 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3578 stcb->asoc.primary_destination,
3579 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3580 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3583 * already expired or did not use delayed
3586 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3589 SCTP_TCB_UNLOCK(stcb);
3592 case SCTP_MAX_BURST:
3596 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3598 SCTP_INP_WLOCK(inp);
3599 inp->sctp_ep.max_burst = *burst;
3600 SCTP_INP_WUNLOCK(inp);
3605 struct sctp_assoc_value *av;
3608 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3609 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3611 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3612 ovh = SCTP_MED_OVERHEAD;
3614 ovh = SCTP_MED_V4_OVERHEAD;
3617 if (av->assoc_value) {
3618 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3620 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3622 SCTP_TCB_UNLOCK(stcb);
3624 SCTP_INP_WLOCK(inp);
3626 * FIXME MT: I think this is not in tune
3629 if (av->assoc_value) {
3630 inp->sctp_frag_point = (av->assoc_value + ovh);
3632 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3634 SCTP_INP_WUNLOCK(inp);
3640 struct sctp_event_subscribe *events;
3642 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3644 SCTP_INP_WLOCK(inp);
3645 if (events->sctp_data_io_event) {
3646 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3648 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3651 if (events->sctp_association_event) {
3652 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3654 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3657 if (events->sctp_address_event) {
3658 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3660 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3663 if (events->sctp_send_failure_event) {
3664 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3666 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3669 if (events->sctp_peer_error_event) {
3670 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3672 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3675 if (events->sctp_shutdown_event) {
3676 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3678 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3681 if (events->sctp_partial_delivery_event) {
3682 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3684 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3687 if (events->sctp_adaptation_layer_event) {
3688 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3690 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3693 if (events->sctp_authentication_event) {
3694 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3696 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3699 if (events->sctp_sender_dry_event) {
3700 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3701 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3702 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3703 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3705 SCTP_TCB_LOCK(stcb);
3708 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3709 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3710 (stcb->asoc.stream_queue_cnt == 0)) {
3711 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3714 SCTP_TCB_UNLOCK(stcb);
3718 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3721 if (events->sctp_stream_reset_event) {
3722 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3724 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3726 SCTP_INP_WUNLOCK(inp);
3730 case SCTP_ADAPTATION_LAYER:
3732 struct sctp_setadaptation *adap_bits;
3734 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3735 SCTP_INP_WLOCK(inp);
3736 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3737 SCTP_INP_WUNLOCK(inp);
3741 case SCTP_SET_INITIAL_DBG_SEQ:
3745 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3746 SCTP_INP_WLOCK(inp);
3747 inp->sctp_ep.initial_sequence_debug = *vvv;
3748 SCTP_INP_WUNLOCK(inp);
3752 case SCTP_DEFAULT_SEND_PARAM:
3754 struct sctp_sndrcvinfo *s_info;
3756 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3757 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3760 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3761 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3763 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3766 SCTP_TCB_UNLOCK(stcb);
3768 SCTP_INP_WLOCK(inp);
3769 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3770 SCTP_INP_WUNLOCK(inp);
3774 case SCTP_PEER_ADDR_PARAMS:
3775 /* Applys to the specific association */
3777 struct sctp_paddrparams *paddrp;
3778 struct sctp_nets *net;
3780 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3781 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3784 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3787 * We increment here since
3788 * sctp_findassociation_ep_addr() wil do a
3789 * decrement if it finds the stcb as long as
3790 * the locked tcb (last argument) is NOT a
3793 SCTP_INP_INCR_REF(inp);
3794 stcb = sctp_findassociation_ep_addr(&inp,
3795 (struct sockaddr *)&paddrp->spp_address,
3798 SCTP_INP_DECR_REF(inp);
3801 if (stcb && (net == NULL)) {
3802 struct sockaddr *sa;
3804 sa = (struct sockaddr *)&paddrp->spp_address;
3805 if (sa->sa_family == AF_INET) {
3806 struct sockaddr_in *sin;
3808 sin = (struct sockaddr_in *)sa;
3809 if (sin->sin_addr.s_addr) {
3810 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3811 SCTP_TCB_UNLOCK(stcb);
3815 } else if (sa->sa_family == AF_INET6) {
3816 struct sockaddr_in6 *sin6;
3818 sin6 = (struct sockaddr_in6 *)sa;
3819 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3820 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3821 SCTP_TCB_UNLOCK(stcb);
3826 error = EAFNOSUPPORT;
3827 SCTP_TCB_UNLOCK(stcb);
3828 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3833 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3835 SCTP_TCB_UNLOCK(stcb);
3836 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3839 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3841 SCTP_TCB_UNLOCK(stcb);
3842 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3846 /************************TCB SPECIFIC SET ******************/
3848 * do we change the timer for HB, we run
3853 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3854 ovh = SCTP_MED_OVERHEAD;
3856 ovh = SCTP_MED_V4_OVERHEAD;
3859 if (paddrp->spp_hbinterval)
3860 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3861 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3862 stcb->asoc.heart_beat_delay = 0;
3864 /* network sets ? */
3866 /************************NET SPECIFIC SET ******************/
3867 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3869 if (sctp_send_hb(stcb, 1, net) < 0) {
3870 /* asoc destroyed */
3871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3876 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3877 net->dest_state |= SCTP_ADDR_NOHB;
3879 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3880 net->dest_state &= ~SCTP_ADDR_NOHB;
3882 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3883 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3884 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3885 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3887 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3888 net->mtu = paddrp->spp_pathmtu + ovh;
3889 if (net->mtu < stcb->asoc.smallest_mtu) {
3890 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3894 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3895 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3896 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3899 if (paddrp->spp_pathmaxrxt)
3900 net->failure_threshold = paddrp->spp_pathmaxrxt;
3902 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3903 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3904 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3909 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3910 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3911 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3916 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3917 if (paddrp->spp_pathmaxrxt)
3918 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3920 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3921 /* Turn back on the timer */
3922 stcb->asoc.hb_is_disabled = 0;
3923 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3925 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3926 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3927 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3928 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3929 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3931 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3932 net->mtu = paddrp->spp_pathmtu + ovh;
3933 if (net->mtu < stcb->asoc.smallest_mtu) {
3934 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3939 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3940 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3941 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3942 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3946 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3947 int cnt_of_unconf = 0;
3948 struct sctp_nets *lnet;
3950 stcb->asoc.hb_is_disabled = 1;
3951 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3952 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3957 * stop the timer ONLY if we
3958 * have no unconfirmed
3961 if (cnt_of_unconf == 0) {
3962 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3963 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3964 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3968 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3969 /* start up the timer. */
3970 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3971 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3975 if (paddrp->spp_flags & SPP_IPV4_TOS)
3976 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3979 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3980 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3984 SCTP_TCB_UNLOCK(stcb);
3986 /************************NO TCB, SET TO default stuff ******************/
3987 SCTP_INP_WLOCK(inp);
3989 * For the TOS/FLOWLABEL stuff you set it
3990 * with the options on the socket
3992 if (paddrp->spp_pathmaxrxt) {
3993 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3995 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3996 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3997 else if (paddrp->spp_hbinterval) {
3998 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3999 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
4000 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
4002 if (paddrp->spp_flags & SPP_HB_ENABLE) {
4003 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4005 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
4006 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4008 SCTP_INP_WUNLOCK(inp);
4014 struct sctp_rtoinfo *srto;
4015 uint32_t new_init, new_min, new_max;
4017 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
4018 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
4021 if (srto->srto_initial)
4022 new_init = srto->srto_initial;
4024 new_init = stcb->asoc.initial_rto;
4026 new_max = srto->srto_max;
4028 new_max = stcb->asoc.maxrto;
4030 new_min = srto->srto_min;
4032 new_min = stcb->asoc.minrto;
4033 if ((new_min <= new_init) && (new_init <= new_max)) {
4034 stcb->asoc.initial_rto = new_init;
4035 stcb->asoc.maxrto = new_max;
4036 stcb->asoc.minrto = new_min;
4038 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4041 SCTP_TCB_UNLOCK(stcb);
4043 SCTP_INP_WLOCK(inp);
4044 if (srto->srto_initial)
4045 new_init = srto->srto_initial;
4047 new_init = inp->sctp_ep.initial_rto;
4049 new_max = srto->srto_max;
4051 new_max = inp->sctp_ep.sctp_maxrto;
4053 new_min = srto->srto_min;
4055 new_min = inp->sctp_ep.sctp_minrto;
4056 if ((new_min <= new_init) && (new_init <= new_max)) {
4057 inp->sctp_ep.initial_rto = new_init;
4058 inp->sctp_ep.sctp_maxrto = new_max;
4059 inp->sctp_ep.sctp_minrto = new_min;
4061 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4064 SCTP_INP_WUNLOCK(inp);
4068 case SCTP_ASSOCINFO:
4070 struct sctp_assocparams *sasoc;
4072 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4073 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4074 if (sasoc->sasoc_cookie_life) {
4075 /* boundary check the cookie life */
4076 if (sasoc->sasoc_cookie_life < 1000)
4077 sasoc->sasoc_cookie_life = 1000;
4078 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4079 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4083 if (sasoc->sasoc_asocmaxrxt)
4084 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4085 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4086 sasoc->sasoc_peer_rwnd = 0;
4087 sasoc->sasoc_local_rwnd = 0;
4088 if (sasoc->sasoc_cookie_life) {
4089 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4091 SCTP_TCB_UNLOCK(stcb);
4093 SCTP_INP_WLOCK(inp);
4094 if (sasoc->sasoc_asocmaxrxt)
4095 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4096 sasoc->sasoc_number_peer_destinations = 0;
4097 sasoc->sasoc_peer_rwnd = 0;
4098 sasoc->sasoc_local_rwnd = 0;
4099 if (sasoc->sasoc_cookie_life) {
4100 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4102 SCTP_INP_WUNLOCK(inp);
4108 struct sctp_initmsg *sinit;
4110 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4111 SCTP_INP_WLOCK(inp);
4112 if (sinit->sinit_num_ostreams)
4113 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4115 if (sinit->sinit_max_instreams)
4116 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4118 if (sinit->sinit_max_attempts)
4119 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4121 if (sinit->sinit_max_init_timeo)
4122 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4123 SCTP_INP_WUNLOCK(inp);
4126 case SCTP_PRIMARY_ADDR:
4128 struct sctp_setprim *spa;
4129 struct sctp_nets *net, *lnet;
4131 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4132 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4136 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4139 * We increment here since
4140 * sctp_findassociation_ep_addr() wil do a
4141 * decrement if it finds the stcb as long as
4142 * the locked tcb (last argument) is NOT a
4145 SCTP_INP_INCR_REF(inp);
4146 stcb = sctp_findassociation_ep_addr(&inp,
4147 (struct sockaddr *)&spa->ssp_addr,
4150 SCTP_INP_DECR_REF(inp);
4154 if ((stcb) && (net)) {
4155 if ((net != stcb->asoc.primary_destination) &&
4156 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4157 /* Ok we need to set it */
4158 lnet = stcb->asoc.primary_destination;
4159 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4160 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4161 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4163 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4167 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4171 SCTP_TCB_UNLOCK(stcb);
4175 case SCTP_SET_DYNAMIC_PRIMARY:
4177 union sctp_sockstore *ss;
4179 error = priv_check(curthread,
4180 PRIV_NETINET_RESERVEDPORT);
4184 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4185 /* SUPER USER CHECK? */
4186 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4189 case SCTP_SET_PEER_PRIMARY_ADDR:
4191 struct sctp_setpeerprim *sspp;
4193 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4194 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4196 struct sctp_ifa *ifa;
4198 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4199 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4201 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4205 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4207 * Must validate the ifa found is in
4210 struct sctp_laddr *laddr;
4213 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4214 if (laddr->ifa == NULL) {
4215 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4219 if (laddr->ifa == ifa) {
4225 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4230 if (sctp_set_primary_ip_address_sa(stcb,
4231 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4232 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4236 SCTP_TCB_UNLOCK(stcb);
4238 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4244 case SCTP_BINDX_ADD_ADDR:
4246 struct sctp_getaddresses *addrs;
4250 td = (struct thread *)p;
4251 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4253 if (addrs->addr->sa_family == AF_INET) {
4254 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4256 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4260 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4261 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4265 } else if (addrs->addr->sa_family == AF_INET6) {
4266 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4268 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4272 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4273 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4274 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4279 error = EAFNOSUPPORT;
4282 sctp_bindx_add_address(so, inp, addrs->addr,
4283 addrs->sget_assoc_id, vrf_id,
4287 case SCTP_BINDX_REM_ADDR:
4289 struct sctp_getaddresses *addrs;
4293 td = (struct thread *)p;
4295 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4296 if (addrs->addr->sa_family == AF_INET) {
4297 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4299 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4303 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4304 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4308 } else if (addrs->addr->sa_family == AF_INET6) {
4309 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4311 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4315 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4316 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4317 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4322 error = EAFNOSUPPORT;
4325 sctp_bindx_delete_address(so, inp, addrs->addr,
4326 addrs->sget_assoc_id, vrf_id,
4331 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4332 error = ENOPROTOOPT;
4334 } /* end switch (opt) */
4339 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4341 void *optval = NULL;
4343 struct sctp_inpcb *inp;
4347 inp = (struct sctp_inpcb *)so->so_pcb;
4349 /* I made the same as TCP since we are not setup? */
4350 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4351 return (ECONNRESET);
4353 if (sopt->sopt_level != IPPROTO_SCTP) {
4354 /* wrong proto level... send back up to IP */
4356 if (INP_CHECK_SOCKAF(so, AF_INET6))
4357 error = ip6_ctloutput(so, sopt);
4360 error = ip_ctloutput(so, sopt);
4363 optsize = sopt->sopt_valsize;
4365 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4366 if (optval == NULL) {
4367 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4370 error = sooptcopyin(sopt, optval, optsize, optsize);
4372 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4376 p = (void *)sopt->sopt_td;
4377 if (sopt->sopt_dir == SOPT_SET) {
4378 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4379 } else if (sopt->sopt_dir == SOPT_GET) {
4380 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4382 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4385 if ((error == 0) && (optval != NULL)) {
4386 error = sooptcopyout(sopt, optval, optsize);
4387 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4388 } else if (optval != NULL) {
4389 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4397 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4400 int create_lock_on = 0;
4402 struct sctp_inpcb *inp;
4403 struct sctp_tcb *stcb = NULL;
4405 inp = (struct sctp_inpcb *)so->so_pcb;
4407 /* I made the same as TCP since we are not setup? */
4408 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4409 return (ECONNRESET);
4412 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4416 if (addr->sa_family == AF_INET6) {
4417 struct sockaddr_in6 *sin6p;
4419 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4420 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4423 sin6p = (struct sockaddr_in6 *)addr;
4424 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4425 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4430 if (addr->sa_family == AF_INET) {
4431 struct sockaddr_in *sinp;
4433 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4434 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4437 sinp = (struct sockaddr_in *)addr;
4438 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4439 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4443 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4444 return (EAFNOSUPPORT);
4446 SCTP_INP_INCR_REF(inp);
4447 SCTP_ASOC_CREATE_LOCK(inp);
4451 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4452 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4453 /* Should I really unlock ? */
4454 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4459 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4460 (addr->sa_family == AF_INET6)) {
4461 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4466 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4467 SCTP_PCB_FLAGS_UNBOUND) {
4468 /* Bind a ephemeral port */
4469 error = sctp_inpcb_bind(so, NULL, NULL, p);
4474 /* Now do we connect? */
4475 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4476 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4477 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4481 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4482 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4483 /* We are already connected AND the TCP model */
4484 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4488 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4489 SCTP_INP_RLOCK(inp);
4490 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4491 SCTP_INP_RUNLOCK(inp);
4494 * We increment here since sctp_findassociation_ep_addr()
4495 * will do a decrement if it finds the stcb as long as the
4496 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4498 SCTP_INP_INCR_REF(inp);
4499 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4501 SCTP_INP_DECR_REF(inp);
4503 SCTP_TCB_UNLOCK(stcb);
4507 /* Already have or am bring up an association */
4508 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4512 vrf_id = inp->def_vrf_id;
4513 /* We are GOOD to go */
4514 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4516 /* Gak! no memory */
4519 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4520 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4521 /* Set the connected flag so we can queue data */
4522 SOCKBUF_LOCK(&so->so_rcv);
4523 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4524 SOCKBUF_UNLOCK(&so->so_rcv);
4525 SOCKBUF_LOCK(&so->so_snd);
4526 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4527 SOCKBUF_UNLOCK(&so->so_snd);
4529 so->so_state &= ~SS_ISDISCONNECTING;
4533 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4534 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4536 /* initialize authentication parameters for the assoc */
4537 sctp_initialize_auth_params(inp, stcb);
4539 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4540 SCTP_TCB_UNLOCK(stcb);
4542 if (create_lock_on) {
4543 SCTP_ASOC_CREATE_UNLOCK(inp);
4545 SCTP_INP_DECR_REF(inp);
4550 sctp_listen(struct socket *so, int backlog, struct thread *p)
4553 * Note this module depends on the protocol processing being called
4554 * AFTER any socket level flags and backlog are applied to the
4555 * socket. The traditional way that the socket flags are applied is
4556 * AFTER protocol processing. We have made a change to the
4557 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4558 * place if the socket API for SCTP is to work properly.
4562 struct sctp_inpcb *inp;
4564 inp = (struct sctp_inpcb *)so->so_pcb;
4566 /* I made the same as TCP since we are not setup? */
4567 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4568 return (ECONNRESET);
4570 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4571 /* See if we have a listener */
4572 struct sctp_inpcb *tinp;
4573 union sctp_sockstore store, *sp;
4576 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4578 struct sctp_laddr *laddr;
4580 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4581 memcpy(&store, &laddr->ifa->address, sizeof(store));
4582 sp->sin.sin_port = inp->sctp_lport;
4583 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4584 if (tinp && (tinp != inp) &&
4585 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4586 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4587 (tinp->sctp_socket->so_qlimit)) {
4589 * we have a listener already and
4592 SCTP_INP_DECR_REF(tinp);
4593 return (EADDRINUSE);
4595 SCTP_INP_DECR_REF(tinp);
4599 /* Setup a local addr bound all */
4600 memset(&store, 0, sizeof(store));
4601 store.sin.sin_port = inp->sctp_lport;
4603 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4604 store.sa.sa_family = AF_INET6;
4605 store.sa.sa_len = sizeof(struct sockaddr_in6);
4608 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4609 store.sa.sa_family = AF_INET;
4610 store.sa.sa_len = sizeof(struct sockaddr_in);
4612 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4613 if (tinp && (tinp != inp) &&
4614 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4615 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4616 (tinp->sctp_socket->so_qlimit)) {
4618 * we have a listener already and its not
4621 SCTP_INP_DECR_REF(tinp);
4622 return (EADDRINUSE);
4624 SCTP_INP_DECR_REF(inp);
4628 SCTP_INP_RLOCK(inp);
4629 #ifdef SCTP_LOCK_LOGGING
4630 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4631 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4635 error = solisten_proto_check(so);
4638 SCTP_INP_RUNLOCK(inp);
4641 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4642 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4644 * The unlucky case - We are in the tcp pool with this guy.
4645 * - Someone else is in the main inp slot. - We must move
4646 * this guy (the listener) to the main slot - We must then
4647 * move the guy that was listener to the TCP Pool.
4649 if (sctp_swap_inpcb_for_listen(inp)) {
4653 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4654 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4655 /* We are already connected AND the TCP model */
4657 SCTP_INP_RUNLOCK(inp);
4659 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4660 return (EADDRINUSE);
4662 SCTP_INP_RUNLOCK(inp);
4663 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4664 /* We must do a bind. */
4666 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4667 /* bind error, probably perm */
4672 /* It appears for 7.0 and on, we must always call this. */
4673 solisten_proto(so, backlog);
4674 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4675 /* remove the ACCEPTCONN flag for one-to-many sockets */
4676 so->so_options &= ~SO_ACCEPTCONN;
4679 /* turning off listen */
4680 so->so_options &= ~SO_ACCEPTCONN;
4686 static int sctp_defered_wakeup_cnt = 0;
4689 sctp_accept(struct socket *so, struct sockaddr **addr)
4691 struct sctp_tcb *stcb;
4692 struct sctp_inpcb *inp;
4693 union sctp_sockstore store;
4699 inp = (struct sctp_inpcb *)so->so_pcb;
4702 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4703 return (ECONNRESET);
4705 SCTP_INP_RLOCK(inp);
4706 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4707 SCTP_INP_RUNLOCK(inp);
4708 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4709 return (EOPNOTSUPP);
4711 if (so->so_state & SS_ISDISCONNECTED) {
4712 SCTP_INP_RUNLOCK(inp);
4713 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4714 return (ECONNABORTED);
4716 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4718 SCTP_INP_RUNLOCK(inp);
4719 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4720 return (ECONNRESET);
4722 SCTP_TCB_LOCK(stcb);
4723 SCTP_INP_RUNLOCK(inp);
4724 store = stcb->asoc.primary_destination->ro._l_addr;
4725 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4726 SCTP_TCB_UNLOCK(stcb);
4727 switch (store.sa.sa_family) {
4730 struct sockaddr_in *sin;
4732 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4735 sin->sin_family = AF_INET;
4736 sin->sin_len = sizeof(*sin);
4737 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4738 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4739 *addr = (struct sockaddr *)sin;
4745 struct sockaddr_in6 *sin6;
4747 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4750 sin6->sin6_family = AF_INET6;
4751 sin6->sin6_len = sizeof(*sin6);
4752 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4754 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4755 if ((error = sa6_recoverscope(sin6)) != 0) {
4756 SCTP_FREE_SONAME(sin6);
4759 *addr = (struct sockaddr *)sin6;
4767 /* Wake any delayed sleep action */
4768 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4769 SCTP_INP_WLOCK(inp);
4770 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4771 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4772 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4773 SCTP_INP_WUNLOCK(inp);
4774 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4775 if (sowriteable(inp->sctp_socket)) {
4776 sowwakeup_locked(inp->sctp_socket);
4778 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4780 SCTP_INP_WLOCK(inp);
4782 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4783 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4784 SCTP_INP_WUNLOCK(inp);
4785 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4786 if (soreadable(inp->sctp_socket)) {
4787 sctp_defered_wakeup_cnt++;
4788 sorwakeup_locked(inp->sctp_socket);
4790 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4792 SCTP_INP_WLOCK(inp);
4794 SCTP_INP_WUNLOCK(inp);
4796 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4797 SCTP_TCB_LOCK(stcb);
4798 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4804 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4806 struct sockaddr_in *sin;
4808 struct sctp_inpcb *inp;
4809 struct sctp_ifa *sctp_ifa;
4812 * Do the malloc first in case it blocks.
4814 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4817 sin->sin_family = AF_INET;
4818 sin->sin_len = sizeof(*sin);
4819 inp = (struct sctp_inpcb *)so->so_pcb;
4821 SCTP_FREE_SONAME(sin);
4822 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4825 SCTP_INP_RLOCK(inp);
4826 sin->sin_port = inp->sctp_lport;
4827 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4828 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4829 struct sctp_tcb *stcb;
4830 struct sockaddr_in *sin_a;
4831 struct sctp_nets *net;
4834 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4840 SCTP_TCB_LOCK(stcb);
4841 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4842 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4844 /* this will make coverity happy */
4847 if (sin_a->sin_family == AF_INET) {
4852 if ((!fnd) || (sin_a == NULL)) {
4854 SCTP_TCB_UNLOCK(stcb);
4857 vrf_id = inp->def_vrf_id;
4858 sctp_ifa = sctp_source_address_selection(inp,
4860 (sctp_route_t *) & net->ro,
4863 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4864 sctp_free_ifa(sctp_ifa);
4866 SCTP_TCB_UNLOCK(stcb);
4868 /* For the bound all case you get back 0 */
4870 sin->sin_addr.s_addr = 0;
4874 /* Take the first IPv4 address in the list */
4875 struct sctp_laddr *laddr;
4878 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4879 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4880 struct sockaddr_in *sin_a;
4882 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4883 sin->sin_addr = sin_a->sin_addr;
4889 SCTP_FREE_SONAME(sin);
4890 SCTP_INP_RUNLOCK(inp);
4891 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4895 SCTP_INP_RUNLOCK(inp);
4896 (*addr) = (struct sockaddr *)sin;
4901 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4903 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4905 struct sockaddr_in *sin_a;
4906 struct sctp_inpcb *inp;
4907 struct sctp_tcb *stcb;
4908 struct sctp_nets *net;
4910 /* Do the malloc first in case it blocks. */
4911 inp = (struct sctp_inpcb *)so->so_pcb;
4912 if ((inp == NULL) ||
4913 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4914 /* UDP type and listeners will drop out here */
4915 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4918 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4921 sin->sin_family = AF_INET;
4922 sin->sin_len = sizeof(*sin);
4924 /* We must recapture incase we blocked */
4925 inp = (struct sctp_inpcb *)so->so_pcb;
4927 SCTP_FREE_SONAME(sin);
4928 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4931 SCTP_INP_RLOCK(inp);
4932 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4934 SCTP_TCB_LOCK(stcb);
4936 SCTP_INP_RUNLOCK(inp);
4938 SCTP_FREE_SONAME(sin);
4939 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4943 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4944 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4945 if (sin_a->sin_family == AF_INET) {
4947 sin->sin_port = stcb->rport;
4948 sin->sin_addr = sin_a->sin_addr;
4952 SCTP_TCB_UNLOCK(stcb);
4954 /* No IPv4 address */
4955 SCTP_FREE_SONAME(sin);
4956 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4959 (*addr) = (struct sockaddr *)sin;
4963 struct pr_usrreqs sctp_usrreqs = {
4964 .pru_abort = sctp_abort,
4965 .pru_accept = sctp_accept,
4966 .pru_attach = sctp_attach,
4967 .pru_bind = sctp_bind,
4968 .pru_connect = sctp_connect,
4969 .pru_control = in_control,
4970 .pru_close = sctp_close,
4971 .pru_detach = sctp_close,
4972 .pru_sopoll = sopoll_generic,
4973 .pru_flush = sctp_flush,
4974 .pru_disconnect = sctp_disconnect,
4975 .pru_listen = sctp_listen,
4976 .pru_peeraddr = sctp_peeraddr,
4977 .pru_send = sctp_sendm,
4978 .pru_shutdown = sctp_shutdown,
4979 .pru_sockaddr = sctp_ingetaddr,
4980 .pru_sosend = sctp_sosend,
4981 .pru_soreceive = sctp_soreceive