2 * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
41 #include <netinet6/sctp6_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
57 /* Init the SCTP pcb in sctp_pcb.c */
62 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
63 sctp_max_chunks_on_queue = (nmbclusters / 8);
65 * Allow a user to take no more than 1/2 the number of clusters or
66 * the SB_MAX whichever is smaller for the send window.
68 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
69 sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
70 ((nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
72 * Now for the recv window, should we take the same amount? or
73 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
74 * now I will just copy.
76 sctp_recvspace = sctp_sendspace;
84 * cleanup of the sctppcbinfo structure.
85 * Assumes that the sctppcbinfo lock is held.
88 sctp_pcbinfo_cleanup(void)
90 /* free the hash tables */
91 if (sctppcbinfo.sctp_asochash != NULL)
92 SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
93 if (sctppcbinfo.sctp_ephash != NULL)
94 SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
95 if (sctppcbinfo.sctp_tcpephash != NULL)
96 SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
97 if (sctppcbinfo.sctp_restarthash != NULL)
98 SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104 struct sctp_tcb *stcb,
105 struct sctp_nets *net,
108 struct sctp_tmit_chunk *chk;
110 /* Adjust that too */
111 stcb->asoc.smallest_mtu = nxtsz;
112 /* now off to subtract IP_DF flag if needed */
114 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
115 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
116 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
119 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
120 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122 * For this guy we also mark for immediate resend
123 * since we sent to big of chunk
125 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
126 if (chk->sent != SCTP_DATAGRAM_RESEND) {
127 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
129 chk->sent = SCTP_DATAGRAM_RESEND;
130 chk->rec.data.doing_fast_retransmit = 0;
131 #ifdef SCTP_FLIGHT_LOGGING
132 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
133 chk->whoTo->flight_size,
135 (uintptr_t) chk->whoTo,
136 chk->rec.data.TSN_seq);
138 /* Clear any time so NO RTT is being done */
140 sctp_flight_size_decrease(chk);
141 sctp_total_flight_decrease(stcb, chk);
147 sctp_notify_mbuf(struct sctp_inpcb *inp,
148 struct sctp_tcb *stcb,
149 struct sctp_nets *net,
154 int totsz, tmr_stopped = 0;
158 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
159 (ip == NULL) || (sh == NULL)) {
161 SCTP_TCB_UNLOCK(stcb);
164 /* First job is to verify the vtag matches what I would send */
165 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
166 SCTP_TCB_UNLOCK(stcb);
169 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
171 if (icmph->icmp_type != ICMP_UNREACH) {
172 /* We only care about unreachable */
173 SCTP_TCB_UNLOCK(stcb);
176 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
177 /* not a unreachable message due to frag. */
178 SCTP_TCB_UNLOCK(stcb);
183 nxtsz = ntohs(icmph->icmp_seq);
186 * old type router that does not tell us what the next size
187 * mtu is. Rats we will have to guess (in a educated fashion
190 nxtsz = find_next_best_mtu(totsz);
192 /* Stop any PMTU timer */
193 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
195 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
196 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
198 /* Adjust destination size limit */
199 if (net->mtu > nxtsz) {
202 /* now what about the ep? */
203 if (stcb->asoc.smallest_mtu > nxtsz) {
204 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
207 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
209 SCTP_TCB_UNLOCK(stcb);
214 sctp_notify(struct sctp_inpcb *inp,
218 struct sctp_tcb *stcb,
219 struct sctp_nets *net)
222 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
223 (sh == NULL) || (to == NULL)) {
226 /* First job is to verify the vtag matches what I would send */
227 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
230 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
232 if ((error == EHOSTUNREACH) || /* Host is not reachable */
233 (error == EHOSTDOWN) || /* Host is down */
234 (error == ECONNREFUSED) || /* Host refused the connection, (not
236 (error == ENOPROTOOPT) /* SCTP is not present on host */
239 * Hmm reachablity problems we must examine closely. If its
240 * not reachable, we may have lost a network. Or if there is
241 * NO protocol at the other end named SCTP. well we consider
244 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
245 if (net->dest_state & SCTP_ADDR_REACHABLE) {
246 /* Ok that destination is NOT reachable */
247 printf("ICMP (thresh %d/%d) takes interface %p down\n",
249 net->failure_threshold,
252 net->dest_state &= ~SCTP_ADDR_REACHABLE;
253 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
254 net->error_count = net->failure_threshold + 1;
255 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
256 stcb, SCTP_FAILED_THRESHOLD,
260 SCTP_TCB_UNLOCK(stcb);
263 * Here the peer is either playing tricks on us,
264 * including an address that belongs to someone who
265 * does not support SCTP OR was a userland
266 * implementation that shutdown and now is dead. In
267 * either case treat it like a OOTB abort with no
270 sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
271 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
272 /* no need to unlock here, since the TCB is gone */
275 /* Send all others to the app */
277 SCTP_TCB_UNLOCK(stcb);
280 if (inp->sctp_socket) {
281 #ifdef SCTP_LOCK_LOGGING
282 sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
284 SOCK_LOCK(inp->sctp_socket);
285 inp->sctp_socket->so_error = error;
286 sctp_sowwakeup(inp, inp->sctp_socket);
287 SOCK_UNLOCK(inp->sctp_socket);
293 sctp_ctlinput(cmd, sa, vip)
302 /* FIX, for non-bsd is this right? */
303 vrf_id = SCTP_DEFAULT_VRFID;
304 if (sa->sa_family != AF_INET ||
305 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
308 if (PRC_IS_REDIRECT(cmd)) {
310 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
314 struct sctp_inpcb *inp = NULL;
315 struct sctp_tcb *stcb = NULL;
316 struct sctp_nets *net = NULL;
317 struct sockaddr_in to, from;
319 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
320 bzero(&to, sizeof(to));
321 bzero(&from, sizeof(from));
322 from.sin_family = to.sin_family = AF_INET;
323 from.sin_len = to.sin_len = sizeof(to);
324 from.sin_port = sh->src_port;
325 from.sin_addr = ip->ip_src;
326 to.sin_port = sh->dest_port;
327 to.sin_addr = ip->ip_dst;
330 * 'to' holds the dest of the packet that failed to be sent.
331 * 'from' holds our local endpoint address. Thus we reverse
332 * the to and the from in the lookup.
334 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
335 (struct sockaddr *)&to,
336 &inp, &net, 1, vrf_id);
337 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
338 if (cmd != PRC_MSGSIZE) {
341 if (cmd == PRC_HOSTDEAD) {
344 cm = inetctlerrmap[cmd];
346 sctp_notify(inp, cm, sh,
347 (struct sockaddr *)&to, stcb,
350 /* handle possible ICMP size messages */
351 sctp_notify_mbuf(inp, stcb, net, ip, sh);
354 if ((stcb == NULL) && (inp != NULL)) {
355 /* reduce ref-count */
357 SCTP_INP_DECR_REF(inp);
358 SCTP_INP_WUNLOCK(inp);
366 sctp_getcred(SYSCTL_HANDLER_ARGS)
369 struct sockaddr_in addrs[2];
370 struct sctp_inpcb *inp;
371 struct sctp_nets *net;
372 struct sctp_tcb *stcb;
377 /* FIX, for non-bsd is this right? */
378 vrf_id = SCTP_DEFAULT_VRFID;
381 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
382 * visibility is scoped using cr_canseesocket(), which it is not
385 error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
390 error = SYSCTL_IN(req, addrs, sizeof(addrs));
394 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
396 &inp, &net, 1, vrf_id);
397 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
398 if ((inp != NULL) && (stcb == NULL)) {
399 /* reduce ref-count */
401 SCTP_INP_DECR_REF(inp);
407 SCTP_TCB_UNLOCK(stcb);
409 * We use the write lock here, only since in the error leg we need
410 * it. If we used RLOCK, then we would have to
411 * wlock/decr/unlock/rlock. Which in theory could create a hole.
412 * Better to use higher wlock.
416 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
418 SCTP_INP_WUNLOCK(inp);
421 cru2x(inp->sctp_socket->so_cred, &xuc);
422 SCTP_INP_WUNLOCK(inp);
423 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
428 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
429 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
433 sctp_abort(struct socket *so)
435 struct sctp_inpcb *inp;
438 inp = (struct sctp_inpcb *)so->so_pcb;
443 flags = inp->sctp_flags;
444 #ifdef SCTP_LOG_CLOSING
445 sctp_log_closing(inp, NULL, 17);
447 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
448 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
449 #ifdef SCTP_LOG_CLOSING
450 sctp_log_closing(inp, NULL, 16);
452 sctp_inpcb_free(inp, 1, 0);
454 SCTP_SB_CLEAR(so->so_snd);
456 * same for the rcv ones, they are only here for the
459 SCTP_SB_CLEAR(so->so_rcv);
461 /* Now null out the reference, we are completely detached. */
465 flags = inp->sctp_flags;
466 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
467 goto sctp_must_try_again;
474 sctp_attach(struct socket *so, int proto, struct thread *p)
476 struct sctp_inpcb *inp;
477 struct inpcb *ip_inp;
484 inp = (struct sctp_inpcb *)so->so_pcb;
488 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
492 error = sctp_inpcb_alloc(so);
496 inp = (struct sctp_inpcb *)so->so_pcb;
499 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
500 ip_inp = &inp->ip_inp.inp;
501 ip_inp->inp_vflag |= INP_IPV4;
502 ip_inp->inp_ip_ttl = ip_defttl;
505 error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
506 #ifdef SCTP_LOG_CLOSING
507 sctp_log_closing(inp, NULL, 17);
510 flags = inp->sctp_flags;
511 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
512 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
513 #ifdef SCTP_LOG_CLOSING
514 sctp_log_closing(inp, NULL, 15);
516 SCTP_INP_WUNLOCK(inp);
517 sctp_inpcb_free(inp, 1, 0);
519 SCTP_INP_WUNLOCK(inp);
524 SCTP_INP_WUNLOCK(inp);
529 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
531 struct sctp_inpcb *inp;
535 if (addr && addr->sa_family != AF_INET)
536 /* must be a v4 address! */
540 inp = (struct sctp_inpcb *)so->so_pcb;
544 error = sctp_inpcb_bind(so, addr, p);
549 sctp_close(struct socket *so)
551 struct sctp_inpcb *inp;
554 inp = (struct sctp_inpcb *)so->so_pcb;
559 * Inform all the lower layer assoc that we are done.
562 flags = inp->sctp_flags;
563 #ifdef SCTP_LOG_CLOSING
564 sctp_log_closing(inp, NULL, 17);
566 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
567 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
568 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
569 (so->so_rcv.sb_cc > 0)) {
570 #ifdef SCTP_LOG_CLOSING
571 sctp_log_closing(inp, NULL, 13);
573 sctp_inpcb_free(inp, 1, 1);
575 #ifdef SCTP_LOG_CLOSING
576 sctp_log_closing(inp, NULL, 14);
578 sctp_inpcb_free(inp, 0, 1);
581 * The socket is now detached, no matter what the state of
582 * the SCTP association.
585 SCTP_SB_CLEAR(so->so_snd);
587 * same for the rcv ones, they are only here for the
590 SCTP_SB_CLEAR(so->so_rcv);
592 /* Now null out the reference, we are completely detached. */
596 flags = inp->sctp_flags;
597 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
598 goto sctp_must_try_again;
606 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
607 struct mbuf *control, struct thread *p);
611 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
612 struct mbuf *control, struct thread *p)
614 struct sctp_inpcb *inp;
617 inp = (struct sctp_inpcb *)so->so_pcb;
620 sctp_m_freem(control);
626 /* Got to have an to address if we are NOT a connected socket */
627 if ((addr == NULL) &&
628 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
629 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
632 } else if (addr == NULL) {
633 error = EDESTADDRREQ;
636 sctp_m_freem(control);
642 if (addr->sa_family != AF_INET) {
643 /* must be a v4 address! */
646 sctp_m_freem(control);
649 error = EDESTADDRREQ;
654 /* now what about control */
657 printf("huh? control set?\n");
658 sctp_m_freem(inp->control);
661 inp->control = control;
665 SCTP_BUF_NEXT(inp->pkt_last) = m;
668 inp->pkt_last = inp->pkt = m;
671 /* FreeBSD uses a flag passed */
672 ((flags & PRUS_MORETOCOME) == 0)
675 * note with the current version this code will only be used
676 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
677 * re-defining sosend to use the sctp_sosend. One can
678 * optionally switch back to this code (by changing back the
679 * definitions) but this is not advisable. This code is used
680 * by FreeBSD when sending a file with sendfile() though.
684 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
694 sctp_disconnect(struct socket *so)
696 struct sctp_inpcb *inp;
698 inp = (struct sctp_inpcb *)so->so_pcb;
703 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
704 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
706 SCTP_INP_RUNLOCK(inp);
709 struct sctp_association *asoc;
710 struct sctp_tcb *stcb;
712 stcb = LIST_FIRST(&inp->sctp_asoc_list);
714 SCTP_INP_RUNLOCK(inp);
719 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
720 /* We are about to be freed, out of here */
721 SCTP_TCB_UNLOCK(stcb);
722 SCTP_INP_RUNLOCK(inp);
725 if (((so->so_options & SO_LINGER) &&
726 (so->so_linger == 0)) ||
727 (so->so_rcv.sb_cc > 0)) {
728 if (SCTP_GET_STATE(asoc) !=
729 SCTP_STATE_COOKIE_WAIT) {
730 /* Left with Data unread */
733 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
739 struct sctp_paramhdr *ph;
741 ph = mtod(err, struct sctp_paramhdr *);
742 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
743 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
744 ph->param_length = htons(SCTP_BUF_LEN(err));
746 sctp_send_abort_tcb(stcb, err);
747 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
749 SCTP_INP_RUNLOCK(inp);
750 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
751 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
752 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
754 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
755 /* No unlock tcb assoc is gone */
758 if (TAILQ_EMPTY(&asoc->send_queue) &&
759 TAILQ_EMPTY(&asoc->sent_queue) &&
760 (asoc->stream_queue_cnt == 0)) {
761 /* there is nothing queued to send, so done */
762 if (asoc->locked_on_sending) {
765 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
766 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
767 /* only send SHUTDOWN 1st time thru */
768 sctp_stop_timers_for_shutdown(stcb);
769 sctp_send_shutdown(stcb,
770 stcb->asoc.primary_destination);
771 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
772 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
773 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
774 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
776 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
777 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
779 asoc->primary_destination);
780 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
782 asoc->primary_destination);
786 * we still got (or just got) data to send,
787 * so set SHUTDOWN_PENDING
790 * XXX sockets draft says that SCTP_EOF
791 * should be sent with no data. currently,
792 * we will allow user data to be sent first
793 * and move to SHUTDOWN-PENDING
795 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
796 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
797 asoc->primary_destination);
798 if (asoc->locked_on_sending) {
799 /* Locked to send out the data */
800 struct sctp_stream_queue_pending *sp;
802 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
804 printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
805 asoc->locked_on_sending->stream_no);
807 if ((sp->length == 0) && (sp->msg_is_complete == 0))
808 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
811 if (TAILQ_EMPTY(&asoc->send_queue) &&
812 TAILQ_EMPTY(&asoc->sent_queue) &&
813 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
817 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
818 0, M_DONTWAIT, 1, MT_DATA);
824 struct sctp_paramhdr *ph;
827 SCTP_BUF_LEN(op_err) =
828 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
830 struct sctp_paramhdr *);
831 ph->param_type = htons(
832 SCTP_CAUSE_USER_INITIATED_ABT);
833 ph->param_length = htons(SCTP_BUF_LEN(op_err));
834 ippp = (uint32_t *) (ph + 1);
835 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
837 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
838 sctp_send_abort_tcb(stcb, op_err);
839 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
840 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
841 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
842 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
844 SCTP_INP_RUNLOCK(inp);
845 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
849 SCTP_TCB_UNLOCK(stcb);
850 SCTP_INP_RUNLOCK(inp);
854 printf("Not reached reached?\n");
856 /* UDP model does not support this */
857 SCTP_INP_RUNLOCK(inp);
863 sctp_shutdown(struct socket *so)
865 struct sctp_inpcb *inp;
867 inp = (struct sctp_inpcb *)so->so_pcb;
872 /* For UDP model this is a invalid call */
873 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
874 /* Restore the flags that the soshutdown took away. */
875 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
876 /* This proc will wakeup for read and do nothing (I hope) */
877 SCTP_INP_RUNLOCK(inp);
881 * Ok if we reach here its the TCP model and it is either a SHUT_WR
882 * or SHUT_RDWR. This means we put the shutdown flag against it.
885 struct sctp_tcb *stcb;
886 struct sctp_association *asoc;
890 stcb = LIST_FIRST(&inp->sctp_asoc_list);
893 * Ok we hit the case that the shutdown call was
894 * made after an abort or something. Nothing to do
897 SCTP_INP_RUNLOCK(inp);
902 if (TAILQ_EMPTY(&asoc->send_queue) &&
903 TAILQ_EMPTY(&asoc->sent_queue) &&
904 (asoc->stream_queue_cnt == 0)) {
905 if (asoc->locked_on_sending) {
908 /* there is nothing queued to send, so I'm done... */
909 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
910 /* only send SHUTDOWN the first time through */
911 sctp_stop_timers_for_shutdown(stcb);
912 sctp_send_shutdown(stcb,
913 stcb->asoc.primary_destination);
914 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
915 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
916 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
917 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
919 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
920 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
922 asoc->primary_destination);
923 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
925 asoc->primary_destination);
929 * we still got (or just got) data to send, so set
932 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
933 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
934 asoc->primary_destination);
936 if (asoc->locked_on_sending) {
937 /* Locked to send out the data */
938 struct sctp_stream_queue_pending *sp;
940 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
942 printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
943 asoc->locked_on_sending->stream_no);
945 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
946 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
950 if (TAILQ_EMPTY(&asoc->send_queue) &&
951 TAILQ_EMPTY(&asoc->sent_queue) &&
952 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
956 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
957 0, M_DONTWAIT, 1, MT_DATA);
959 /* Fill in the user initiated abort */
960 struct sctp_paramhdr *ph;
963 SCTP_BUF_LEN(op_err) =
964 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
966 struct sctp_paramhdr *);
967 ph->param_type = htons(
968 SCTP_CAUSE_USER_INITIATED_ABT);
969 ph->param_length = htons(SCTP_BUF_LEN(op_err));
970 ippp = (uint32_t *) (ph + 1);
971 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
973 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
974 sctp_abort_an_association(stcb->sctp_ep, stcb,
975 SCTP_RESPONSE_TO_USER_REQ,
980 SCTP_TCB_UNLOCK(stcb);
983 SCTP_INP_RUNLOCK(inp);
988 * copies a "user" presentable address and removes embedded scope, etc.
989 * returns 0 on success, 1 on error
992 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
994 struct sockaddr_in6 lsa6;
996 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
998 memcpy(ss, sa, sa->sa_len);
1005 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1006 struct sctp_tcb *stcb,
1008 struct sockaddr_storage *sas,
1011 struct sctp_ifn *sctp_ifn;
1012 struct sctp_ifa *sctp_ifa;
1013 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1015 int ipv4_addr_legal, ipv6_addr_legal;
1016 struct sctp_vrf *vrf;
1023 /* Turn on all the appropriate scope */
1024 loopback_scope = stcb->asoc.loopback_scope;
1025 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1026 local_scope = stcb->asoc.local_scope;
1027 site_scope = stcb->asoc.site_scope;
1029 /* Turn on ALL scope, since we look at the EP */
1030 loopback_scope = ipv4_local_scope = local_scope =
1033 ipv4_addr_legal = ipv6_addr_legal = 0;
1034 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1035 ipv6_addr_legal = 1;
1036 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1037 ipv4_addr_legal = 1;
1040 ipv4_addr_legal = 1;
1042 vrf = sctp_find_vrf(vrf_id);
1046 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1047 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1048 if ((loopback_scope == 0) &&
1049 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1050 /* Skip loopback if loopback_scope not set */
1053 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1056 * For the BOUND-ALL case, the list
1057 * associated with a TCB is Always
1058 * considered a reverse list.. i.e.
1059 * it lists addresses that are NOT
1060 * part of the association. If this
1061 * is one of those we must skip it.
1063 if (sctp_is_addr_restricted(stcb,
1068 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1069 (ipv4_addr_legal)) {
1070 struct sockaddr_in *sin;
1072 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1073 if (sin->sin_addr.s_addr == 0) {
1075 * we skip unspecifed
1080 if ((ipv4_local_scope == 0) &&
1081 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1084 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1085 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1086 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1087 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1088 actual += sizeof(sizeof(struct sockaddr_in6));
1090 memcpy(sas, sin, sizeof(*sin));
1091 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1092 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1093 actual += sizeof(*sin);
1095 if (actual >= limit) {
1098 } else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1099 (ipv6_addr_legal)) {
1100 struct sockaddr_in6 *sin6;
1102 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1103 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1105 * we skip unspecifed
1110 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1111 if (local_scope == 0)
1113 if (sin6->sin6_scope_id == 0) {
1114 if (sa6_recoverscope(sin6) != 0)
1123 if ((site_scope == 0) &&
1124 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1127 memcpy(sas, sin6, sizeof(*sin6));
1128 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1129 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1130 actual += sizeof(*sin6);
1131 if (actual >= limit) {
1138 struct sctp_laddr *laddr;
1140 /* The list is a NEGATIVE list */
1141 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1143 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1147 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1150 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1151 sas = (struct sockaddr_storage *)((caddr_t)sas +
1152 laddr->ifa->address.sa.sa_len);
1153 actual += laddr->ifa->address.sa.sa_len;
1154 if (actual >= limit) {
1163 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1164 struct sctp_tcb *stcb,
1166 struct sockaddr_storage *sas)
1170 /* fill up addresses for the endpoint's default vrf */
1171 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1177 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1180 struct sctp_vrf *vrf = NULL;
1183 * In both sub-set bound an bound_all cases we return the MAXIMUM
1184 * number of addresses that you COULD get. In reality the sub-set
1185 * bound may have an exclusion list for a given TCB OR in the
1186 * bound-all case a TCB may NOT include the loopback or other
1187 * addresses as well.
1189 vrf = sctp_find_vrf(vrf_id);
1193 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1194 struct sctp_ifn *sctp_ifn;
1195 struct sctp_ifa *sctp_ifa;
1197 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1198 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1199 /* Count them if they are the right type */
1200 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1201 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1202 cnt += sizeof(struct sockaddr_in6);
1204 cnt += sizeof(struct sockaddr_in);
1206 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1207 cnt += sizeof(struct sockaddr_in6);
1211 struct sctp_laddr *laddr;
1213 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1214 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1215 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1216 cnt += sizeof(struct sockaddr_in6);
1218 cnt += sizeof(struct sockaddr_in);
1220 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1221 cnt += sizeof(struct sockaddr_in6);
1228 sctp_count_max_addresses(struct sctp_inpcb *inp)
1232 /* count addresses for the endpoint's default VRF */
1233 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1239 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1240 size_t optsize, void *p, int delay)
1243 int creat_lock_on = 0;
1244 struct sctp_tcb *stcb = NULL;
1245 struct sockaddr *sa;
1246 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr, i;
1252 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1253 printf("Connectx called\n");
1255 #endif /* SCTP_DEBUG */
1257 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1258 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1259 /* We are already connected AND the TCP model */
1260 return (EADDRINUSE);
1262 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1265 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1266 SCTP_INP_RLOCK(inp);
1267 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1268 SCTP_INP_RUNLOCK(inp);
1273 SCTP_INP_INCR_REF(inp);
1274 SCTP_ASOC_CREATE_LOCK(inp);
1276 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1277 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1281 totaddrp = (int *)optval;
1282 totaddr = *totaddrp;
1283 sa = (struct sockaddr *)(totaddrp + 1);
1285 /* account and validate addresses */
1286 for (i = 0; i < totaddr; i++) {
1287 if (sa->sa_family == AF_INET) {
1289 incr = sizeof(struct sockaddr_in);
1290 } else if (sa->sa_family == AF_INET6) {
1291 struct sockaddr_in6 *sin6;
1293 sin6 = (struct sockaddr_in6 *)sa;
1294 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1295 /* Must be non-mapped for connectx */
1300 incr = sizeof(struct sockaddr_in6);
1305 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1307 /* Already have or am bring up an association */
1308 SCTP_ASOC_CREATE_UNLOCK(inp);
1310 SCTP_TCB_UNLOCK(stcb);
1314 if ((at + incr) > optsize) {
1318 sa = (struct sockaddr *)((caddr_t)sa + incr);
1320 sa = (struct sockaddr *)(totaddrp + 1);
1322 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1327 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1329 struct in6pcb *inp6;
1331 inp6 = (struct in6pcb *)inp;
1332 if (SCTP_IPV6_V6ONLY(inp6)) {
1334 * if IPV6_V6ONLY flag, ignore connections destined
1335 * to a v4 addr or v4-mapped addr
1342 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1343 SCTP_PCB_FLAGS_UNBOUND) {
1344 /* Bind a ephemeral port */
1345 error = sctp_inpcb_bind(so, NULL, p);
1350 /* FIX ME: do we want to pass in a vrf on the connect call? */
1351 vrf_id = inp->def_vrf_id;
1353 /* We are GOOD to go */
1354 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1356 /* Gak! no memory */
1359 /* move to second address */
1360 if (sa->sa_family == AF_INET)
1361 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1363 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1365 for (i = 1; i < totaddr; i++) {
1366 if (sa->sa_family == AF_INET) {
1367 incr = sizeof(struct sockaddr_in);
1368 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
1369 /* assoc gone no un-lock */
1370 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1374 } else if (sa->sa_family == AF_INET6) {
1375 incr = sizeof(struct sockaddr_in6);
1376 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
1377 /* assoc gone no un-lock */
1378 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
1383 sa = (struct sockaddr *)((caddr_t)sa + incr);
1385 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1386 /* Fill in the return id */
1387 a_id = (sctp_assoc_t *) optval;
1388 *a_id = sctp_get_associd(stcb);
1390 /* initialize authentication parameters for the assoc */
1391 sctp_initialize_auth_params(inp, stcb);
1394 /* doing delayed connection */
1395 stcb->asoc.delayed_connection = 1;
1396 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1398 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1399 sctp_send_initiate(inp, stcb);
1401 SCTP_TCB_UNLOCK(stcb);
1402 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1403 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1404 /* Set the connected flag so we can queue data */
1409 SCTP_ASOC_CREATE_UNLOCK(inp);
1410 SCTP_INP_DECR_REF(inp);
1414 #define SCTP_FIND_STCB(inp, stcb, assoc_id) \
1415 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { \
1416 SCTP_INP_RLOCK(inp); \
1417 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1419 SCTP_TCB_LOCK(stcb); \
1420 SCTP_INP_RUNLOCK(inp); \
1421 } else if (assoc_id != 0) { \
1422 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1423 if (stcb == NULL) { \
1431 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) \
1432 if (size < sizeof(type)) { \
1436 destp = (type *)srcp; \
1440 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1443 struct sctp_inpcb *inp;
1445 struct sctp_tcb *stcb = NULL;
1447 if (optval == NULL) {
1450 inp = (struct sctp_inpcb *)so->so_pcb;
1457 case SCTP_AUTOCLOSE:
1458 case SCTP_EXPLICIT_EOR:
1459 case SCTP_AUTO_ASCONF:
1460 case SCTP_DISABLE_FRAGMENTS:
1461 case SCTP_I_WANT_MAPPED_V4_ADDR:
1462 case SCTP_USE_EXT_RCVINFO:
1463 SCTP_INP_RLOCK(inp);
1465 case SCTP_DISABLE_FRAGMENTS:
1466 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1468 case SCTP_I_WANT_MAPPED_V4_ADDR:
1469 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1471 case SCTP_AUTO_ASCONF:
1472 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1474 case SCTP_EXPLICIT_EOR:
1475 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1478 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1480 case SCTP_USE_EXT_RCVINFO:
1481 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1483 case SCTP_AUTOCLOSE:
1484 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1485 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1491 error = ENOPROTOOPT;
1492 } /* end switch (sopt->sopt_name) */
1493 if (optname != SCTP_AUTOCLOSE) {
1494 /* make it an "on/off" value */
1497 if (*optsize < sizeof(val)) {
1500 SCTP_INP_RUNLOCK(inp);
1502 /* return the option value */
1503 *(int *)optval = val;
1504 *optsize = sizeof(val);
1508 case SCTP_PARTIAL_DELIVERY_POINT:
1512 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1513 *value = inp->partial_delivery_point;
1514 *optsize = sizeof(uint32_t);
1517 case SCTP_FRAGMENT_INTERLEAVE:
1521 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1522 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1523 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1524 *value = SCTP_FRAG_LEVEL_2;
1526 *value = SCTP_FRAG_LEVEL_1;
1529 *value = SCTP_FRAG_LEVEL_0;
1531 *optsize = sizeof(uint32_t);
1534 case SCTP_CMT_ON_OFF:
1536 struct sctp_assoc_value *av;
1538 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1539 if (sctp_cmt_on_off) {
1540 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1542 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1543 SCTP_TCB_UNLOCK(stcb);
1549 error = ENOPROTOOPT;
1551 *optsize = sizeof(*av);
1554 case SCTP_GET_ADDR_LEN:
1556 struct sctp_assoc_value *av;
1558 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1561 if (av->assoc_value == AF_INET) {
1562 av->assoc_value = sizeof(struct sockaddr_in);
1567 if (av->assoc_value == AF_INET6) {
1568 av->assoc_value = sizeof(struct sockaddr_in6);
1572 *optsize = sizeof(*av);
1575 case SCTP_GET_ASOC_ID_LIST:
1577 struct sctp_assoc_ids *ids;
1581 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1583 SCTP_INP_RLOCK(inp);
1584 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1587 ids->asls_numb_present = 0;
1588 ids->asls_more_to_get = 0;
1589 SCTP_INP_RUNLOCK(inp);
1592 orig = ids->asls_assoc_start;
1593 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1595 stcb = LIST_NEXT(stcb, sctp_tcblist);
1605 ids->asls_numb_present = 0;
1606 ids->asls_more_to_get = 1;
1607 while (at < MAX_ASOC_IDS_RET) {
1608 ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1610 ids->asls_numb_present++;
1611 stcb = LIST_NEXT(stcb, sctp_tcblist);
1613 ids->asls_more_to_get = 0;
1617 SCTP_INP_RUNLOCK(inp);
1622 struct sctp_assoc_value *av;
1624 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1625 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1628 av->assoc_value = stcb->asoc.context;
1629 SCTP_TCB_UNLOCK(stcb);
1631 SCTP_INP_RLOCK(inp);
1632 av->assoc_value = inp->sctp_context;
1633 SCTP_INP_RUNLOCK(inp);
1635 *optsize = sizeof(*av);
1642 SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, *optsize);
1643 *vrf_id = inp->def_vrf_id;
1646 case SCTP_GET_ASOC_VRF:
1648 struct sctp_assoc_value *id;
1650 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1651 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1656 id->assoc_value = stcb->asoc.vrf_id;
1659 case SCTP_GET_VRF_IDS:
1664 case SCTP_GET_NONCE_VALUES:
1666 struct sctp_get_nonce_values *gnv;
1668 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1669 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1672 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1673 gnv->gn_local_tag = stcb->asoc.my_vtag;
1674 SCTP_TCB_UNLOCK(stcb);
1678 *optsize = sizeof(*gnv);
1681 case SCTP_DELAYED_ACK_TIME:
1683 struct sctp_assoc_value *tm;
1685 SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, *optsize);
1686 SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
1689 tm->assoc_value = stcb->asoc.delayed_ack;
1690 SCTP_TCB_UNLOCK(stcb);
1692 SCTP_INP_RLOCK(inp);
1693 tm->assoc_value = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1694 SCTP_INP_RUNLOCK(inp);
1696 *optsize = sizeof(*tm);
1700 case SCTP_GET_SNDBUF_USE:
1702 struct sctp_sockstat *ss;
1704 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1705 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1708 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1709 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1710 stcb->asoc.size_on_all_streams);
1711 SCTP_TCB_UNLOCK(stcb);
1715 *optsize = sizeof(struct sctp_sockstat);
1722 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1724 SCTP_INP_RLOCK(inp);
1725 *value = inp->sctp_ep.max_burst;
1726 SCTP_INP_RUNLOCK(inp);
1727 *optsize = sizeof(uint8_t);
1732 struct sctp_assoc_value *av;
1735 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1737 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1743 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1744 SCTP_TCB_UNLOCK(stcb);
1746 SCTP_INP_RLOCK(inp);
1747 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1748 ovh = SCTP_MED_OVERHEAD;
1750 ovh = SCTP_MED_V4_OVERHEAD;
1752 av->assoc_value = inp->sctp_frag_point - ovh;
1753 SCTP_INP_RUNLOCK(inp);
1755 *optsize = sizeof(struct sctp_assoc_value);
1758 case SCTP_GET_STAT_LOG:
1759 #ifdef SCTP_STAT_LOGGING
1760 error = sctp_fill_stat_log(optval, optsize);
1767 struct sctp_event_subscribe *events;
1769 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1770 memset(events, 0, sizeof(*events));
1771 SCTP_INP_RLOCK(inp);
1772 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1773 events->sctp_data_io_event = 1;
1775 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1776 events->sctp_association_event = 1;
1778 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1779 events->sctp_address_event = 1;
1781 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1782 events->sctp_send_failure_event = 1;
1784 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1785 events->sctp_peer_error_event = 1;
1787 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1788 events->sctp_shutdown_event = 1;
1790 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1791 events->sctp_partial_delivery_event = 1;
1793 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1794 events->sctp_adaptation_layer_event = 1;
1796 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1797 events->sctp_authentication_event = 1;
1799 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1800 events->sctp_stream_reset_events = 1;
1801 SCTP_INP_RUNLOCK(inp);
1802 *optsize = sizeof(struct sctp_event_subscribe);
1806 case SCTP_ADAPTATION_LAYER:
1810 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1812 SCTP_INP_RLOCK(inp);
1813 *value = inp->sctp_ep.adaptation_layer_indicator;
1814 SCTP_INP_RUNLOCK(inp);
1815 *optsize = sizeof(uint32_t);
1818 case SCTP_SET_INITIAL_DBG_SEQ:
1822 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1823 SCTP_INP_RLOCK(inp);
1824 *value = inp->sctp_ep.initial_sequence_debug;
1825 SCTP_INP_RUNLOCK(inp);
1826 *optsize = sizeof(uint32_t);
1829 case SCTP_GET_LOCAL_ADDR_SIZE:
1833 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1834 SCTP_INP_RLOCK(inp);
1835 *value = sctp_count_max_addresses(inp);
1836 SCTP_INP_RUNLOCK(inp);
1837 *optsize = sizeof(uint32_t);
1840 case SCTP_GET_REMOTE_ADDR_SIZE:
1844 struct sctp_nets *net;
1846 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1847 /* FIXME MT: change to sctp_assoc_value? */
1848 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1852 /* Count the sizes */
1853 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1854 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1855 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1856 size += sizeof(struct sockaddr_in6);
1857 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1858 size += sizeof(struct sockaddr_in);
1864 SCTP_TCB_UNLOCK(stcb);
1865 *value = (uint32_t) size;
1869 *optsize = sizeof(uint32_t);
1872 case SCTP_GET_PEER_ADDRESSES:
1874 * Get the address information, an array is passed in to
1875 * fill up we pack it.
1879 struct sockaddr_storage *sas;
1880 struct sctp_nets *net;
1881 struct sctp_getaddresses *saddr;
1883 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1884 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1887 left = (*optsize) - sizeof(struct sctp_getaddresses);
1888 *optsize = sizeof(struct sctp_getaddresses);
1889 sas = (struct sockaddr_storage *)&saddr->addr[0];
1891 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1892 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1893 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1894 cpsz = sizeof(struct sockaddr_in6);
1895 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1896 cpsz = sizeof(struct sockaddr_in);
1902 /* not enough room. */
1905 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1906 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1907 /* Must map the address */
1908 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1909 (struct sockaddr_in6 *)sas);
1911 memcpy(sas, &net->ro._l_addr, cpsz);
1913 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1915 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1919 SCTP_TCB_UNLOCK(stcb);
1925 case SCTP_GET_LOCAL_ADDRESSES:
1927 size_t limit, actual;
1928 struct sockaddr_storage *sas;
1929 struct sctp_getaddresses *saddr;
1931 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1932 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1934 sas = (struct sockaddr_storage *)&saddr->addr[0];
1935 limit = *optsize - sizeof(sctp_assoc_t);
1936 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1938 SCTP_TCB_UNLOCK(stcb);
1939 *optsize = sizeof(struct sockaddr_storage) + actual;
1942 case SCTP_PEER_ADDR_PARAMS:
1944 struct sctp_paddrparams *paddrp;
1945 struct sctp_nets *net;
1947 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1948 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1952 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1955 * We increment here since
1956 * sctp_findassociation_ep_addr() wil do a
1957 * decrement if it finds the stcb as long as
1958 * the locked tcb (last argument) is NOT a
1961 SCTP_INP_INCR_REF(inp);
1962 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1964 SCTP_INP_DECR_REF(inp);
1969 /* Applys to the specific association */
1970 paddrp->spp_flags = 0;
1972 paddrp->spp_pathmaxrxt = net->failure_threshold;
1973 paddrp->spp_pathmtu = net->mtu;
1974 /* get flags for HB */
1975 if (net->dest_state & SCTP_ADDR_NOHB)
1976 paddrp->spp_flags |= SPP_HB_DISABLE;
1978 paddrp->spp_flags |= SPP_HB_ENABLE;
1979 /* get flags for PMTU */
1980 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
1981 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
1983 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
1986 if (net->ro._l_addr.sin.sin_family == AF_INET) {
1987 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
1988 paddrp->spp_flags |= SPP_IPV4_TOS;
1992 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
1993 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
1994 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1999 * No destination so return default
2002 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2003 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2005 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2006 paddrp->spp_flags |= SPP_IPV4_TOS;
2009 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2010 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2012 /* default settings should be these */
2013 if (sctp_is_hb_timer_running(stcb)) {
2014 paddrp->spp_flags |= SPP_HB_ENABLE;
2017 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2018 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2019 SCTP_TCB_UNLOCK(stcb);
2021 /* Use endpoint defaults */
2022 SCTP_INP_RLOCK(inp);
2023 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2024 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2025 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2026 /* get inp's default */
2028 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2029 paddrp->spp_flags |= SPP_IPV4_TOS;
2032 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2033 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2034 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2037 /* can't return this */
2038 paddrp->spp_pathmaxrxt = 0;
2039 paddrp->spp_pathmtu = 0;
2040 /* default behavior, no stcb */
2041 paddrp->spp_flags = SPP_HB_ENABLE | SPP_PMTUD_ENABLE;
2043 SCTP_INP_RUNLOCK(inp);
2045 *optsize = sizeof(struct sctp_paddrparams);
2048 case SCTP_GET_PEER_ADDR_INFO:
2050 struct sctp_paddrinfo *paddri;
2051 struct sctp_nets *net;
2053 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2054 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2058 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2061 * We increment here since
2062 * sctp_findassociation_ep_addr() wil do a
2063 * decrement if it finds the stcb as long as
2064 * the locked tcb (last argument) is NOT a
2067 SCTP_INP_INCR_REF(inp);
2068 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2070 SCTP_INP_DECR_REF(inp);
2074 if ((stcb) && (net)) {
2075 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2076 paddri->spinfo_cwnd = net->cwnd;
2077 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2078 paddri->spinfo_rto = net->RTO;
2079 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2080 SCTP_TCB_UNLOCK(stcb);
2083 SCTP_TCB_UNLOCK(stcb);
2087 *optsize = sizeof(struct sctp_paddrinfo);
2090 case SCTP_PCB_STATUS:
2092 struct sctp_pcbinfo *spcb;
2094 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2095 sctp_fill_pcbinfo(spcb);
2096 *optsize = sizeof(struct sctp_pcbinfo);
2102 struct sctp_nets *net;
2103 struct sctp_status *sstat;
2105 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2106 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2113 * I think passing the state is fine since
2114 * sctp_constants.h will be available to the user
2117 sstat->sstat_state = stcb->asoc.state;
2118 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2119 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2121 * We can't include chunks that have been passed to
2122 * the socket layer. Only things in queue.
2124 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2125 stcb->asoc.cnt_on_all_streams);
2128 sstat->sstat_instrms = stcb->asoc.streamincnt;
2129 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2130 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2131 memcpy(&sstat->sstat_primary.spinfo_address,
2132 &stcb->asoc.primary_destination->ro._l_addr,
2133 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2134 net = stcb->asoc.primary_destination;
2135 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2137 * Again the user can get info from sctp_constants.h
2138 * for what the state of the network is.
2140 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2141 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2142 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2143 sstat->sstat_primary.spinfo_rto = net->RTO;
2144 sstat->sstat_primary.spinfo_mtu = net->mtu;
2145 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2146 SCTP_TCB_UNLOCK(stcb);
2147 *optsize = sizeof(*sstat);
2152 struct sctp_rtoinfo *srto;
2154 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2155 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2158 srto->srto_initial = stcb->asoc.initial_rto;
2159 srto->srto_max = stcb->asoc.maxrto;
2160 srto->srto_min = stcb->asoc.minrto;
2161 SCTP_TCB_UNLOCK(stcb);
2163 SCTP_INP_RLOCK(inp);
2164 srto->srto_initial = inp->sctp_ep.initial_rto;
2165 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2166 srto->srto_min = inp->sctp_ep.sctp_minrto;
2167 SCTP_INP_RUNLOCK(inp);
2169 *optsize = sizeof(*srto);
2172 case SCTP_ASSOCINFO:
2174 struct sctp_assocparams *sasoc;
2176 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2177 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2180 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2181 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2182 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2183 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2184 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2185 sasoc->sasoc_sack_delay = stcb->asoc.delayed_ack;
2186 sasoc->sasoc_sack_freq = stcb->asoc.sack_freq;
2187 SCTP_TCB_UNLOCK(stcb);
2189 SCTP_INP_RLOCK(inp);
2190 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2191 sasoc->sasoc_number_peer_destinations = 0;
2192 sasoc->sasoc_peer_rwnd = 0;
2193 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2194 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2195 sasoc->sasoc_sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2196 sasoc->sasoc_sack_freq = inp->sctp_ep.sctp_sack_freq;
2197 SCTP_INP_RUNLOCK(inp);
2199 *optsize = sizeof(*sasoc);
2202 case SCTP_DEFAULT_SEND_PARAM:
2204 struct sctp_sndrcvinfo *s_info;
2206 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2207 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2210 *s_info = stcb->asoc.def_send;
2211 SCTP_TCB_UNLOCK(stcb);
2213 SCTP_INP_RLOCK(inp);
2214 *s_info = inp->def_send;
2215 SCTP_INP_RUNLOCK(inp);
2217 *optsize = sizeof(*s_info);
2222 struct sctp_initmsg *sinit;
2224 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2225 SCTP_INP_RLOCK(inp);
2226 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2227 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2228 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2229 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2230 SCTP_INP_RUNLOCK(inp);
2231 *optsize = sizeof(*sinit);
2234 case SCTP_PRIMARY_ADDR:
2235 /* we allow a "get" operation on this */
2237 struct sctp_setprim *ssp;
2239 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2240 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2243 /* simply copy out the sockaddr_storage... */
2244 memcpy(&ssp->ssp_addr, &stcb->asoc.primary_destination->ro._l_addr,
2245 ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2246 SCTP_TCB_UNLOCK(stcb);
2250 *optsize = sizeof(*ssp);
2254 case SCTP_HMAC_IDENT:
2256 struct sctp_hmacalgo *shmac;
2257 sctp_hmaclist_t *hmaclist;
2261 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2263 SCTP_INP_RLOCK(inp);
2264 hmaclist = inp->sctp_ep.local_hmacs;
2265 if (hmaclist == NULL) {
2266 /* no HMACs to return */
2267 *optsize = sizeof(*shmac);
2268 SCTP_INP_RUNLOCK(inp);
2271 /* is there room for all of the hmac ids? */
2272 size = sizeof(*shmac) + (hmaclist->num_algo *
2273 sizeof(shmac->shmac_idents[0]));
2274 if ((size_t)(*optsize) < size) {
2276 SCTP_INP_RUNLOCK(inp);
2279 /* copy in the list */
2280 for (i = 0; i < hmaclist->num_algo; i++)
2281 shmac->shmac_idents[i] = hmaclist->hmac[i];
2282 SCTP_INP_RUNLOCK(inp);
2286 case SCTP_AUTH_ACTIVE_KEY:
2288 struct sctp_authkeyid *scact;
2290 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2291 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2294 /* get the active key on the assoc */
2295 scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2296 SCTP_TCB_UNLOCK(stcb);
2298 /* get the endpoint active key */
2299 SCTP_INP_RLOCK(inp);
2300 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2301 SCTP_INP_RUNLOCK(inp);
2303 *optsize = sizeof(*scact);
2306 case SCTP_LOCAL_AUTH_CHUNKS:
2308 struct sctp_authchunks *sac;
2309 sctp_auth_chklist_t *chklist = NULL;
2312 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2313 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2316 /* get off the assoc */
2317 chklist = stcb->asoc.local_auth_chunks;
2318 /* is there enough space? */
2319 size = sctp_auth_get_chklist_size(chklist);
2320 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2323 /* copy in the chunks */
2324 sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2326 SCTP_TCB_UNLOCK(stcb);
2328 /* get off the endpoint */
2329 SCTP_INP_RLOCK(inp);
2330 chklist = inp->sctp_ep.local_auth_chunks;
2331 /* is there enough space? */
2332 size = sctp_auth_get_chklist_size(chklist);
2333 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2336 /* copy in the chunks */
2337 sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2339 SCTP_INP_RUNLOCK(inp);
2341 *optsize = sizeof(struct sctp_authchunks) + size;
2344 case SCTP_PEER_AUTH_CHUNKS:
2346 struct sctp_authchunks *sac;
2347 sctp_auth_chklist_t *chklist = NULL;
2350 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2351 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2354 /* get off the assoc */
2355 chklist = stcb->asoc.peer_auth_chunks;
2356 /* is there enough space? */
2357 size = sctp_auth_get_chklist_size(chklist);
2358 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2361 /* copy in the chunks */
2362 sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2364 SCTP_TCB_UNLOCK(stcb);
2368 *optsize = sizeof(struct sctp_authchunks) + size;
2374 error = ENOPROTOOPT;
2377 } /* end switch (sopt->sopt_name) */
2382 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2387 struct sctp_tcb *stcb = NULL;
2388 struct sctp_inpcb *inp;
2391 if (optval == NULL) {
2392 printf("optval is NULL\n");
2395 inp = (struct sctp_inpcb *)so->so_pcb;
2397 printf("inp is NULL?\n");
2400 vrf_id = inp->def_vrf_id;
2405 case SCTP_AUTOCLOSE:
2406 case SCTP_AUTO_ASCONF:
2407 case SCTP_EXPLICIT_EOR:
2408 case SCTP_DISABLE_FRAGMENTS:
2409 case SCTP_USE_EXT_RCVINFO:
2410 case SCTP_I_WANT_MAPPED_V4_ADDR:
2411 /* copy in the option value */
2412 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2417 case SCTP_DISABLE_FRAGMENTS:
2418 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2420 case SCTP_AUTO_ASCONF:
2421 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2423 case SCTP_EXPLICIT_EOR:
2424 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2426 case SCTP_USE_EXT_RCVINFO:
2427 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2429 case SCTP_I_WANT_MAPPED_V4_ADDR:
2430 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2431 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2437 set_opt = SCTP_PCB_FLAGS_NODELAY;
2439 case SCTP_AUTOCLOSE:
2440 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2442 * The value is in ticks. Note this does not effect
2443 * old associations, only new ones.
2445 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2448 SCTP_INP_WLOCK(inp);
2450 sctp_feature_on(inp, set_opt);
2452 sctp_feature_off(inp, set_opt);
2454 SCTP_INP_WUNLOCK(inp);
2456 case SCTP_PARTIAL_DELIVERY_POINT:
2460 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2461 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2465 inp->partial_delivery_point = *value;
2468 case SCTP_FRAGMENT_INTERLEAVE:
2469 /* not yet until we re-write sctp_recvmsg() */
2473 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2474 if (*level == SCTP_FRAG_LEVEL_2) {
2475 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2476 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2477 } else if (*level == SCTP_FRAG_LEVEL_1) {
2478 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2479 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2480 } else if (*level == SCTP_FRAG_LEVEL_0) {
2481 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2482 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2489 case SCTP_CMT_ON_OFF:
2491 struct sctp_assoc_value *av;
2493 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2494 if (sctp_cmt_on_off) {
2495 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2497 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2498 SCTP_TCB_UNLOCK(stcb);
2503 error = ENOPROTOOPT;
2507 case SCTP_CLR_STAT_LOG:
2508 #ifdef SCTP_STAT_LOGGING
2509 sctp_clr_stat_log();
2516 struct sctp_assoc_value *av;
2518 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2519 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2522 stcb->asoc.context = av->assoc_value;
2523 SCTP_TCB_UNLOCK(stcb);
2525 SCTP_INP_WLOCK(inp);
2526 inp->sctp_context = av->assoc_value;
2527 SCTP_INP_WUNLOCK(inp);
2535 SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, optsize);
2536 if (*vrf_id > SCTP_MAX_VRF_ID) {
2540 inp->def_vrf_id = *vrf_id;
2543 case SCTP_DEL_VRF_ID:
2548 case SCTP_ADD_VRF_ID:
2554 case SCTP_DELAYED_ACK_TIME:
2556 struct sctp_assoc_value *tm;
2558 SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, optsize);
2559 SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
2562 stcb->asoc.delayed_ack = tm->assoc_value;
2563 SCTP_TCB_UNLOCK(stcb);
2565 SCTP_INP_WLOCK(inp);
2566 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(tm->assoc_value);
2567 SCTP_INP_WUNLOCK(inp);
2571 case SCTP_AUTH_CHUNK:
2573 struct sctp_authchunk *sauth;
2575 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2577 SCTP_INP_WLOCK(inp);
2578 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2580 SCTP_INP_WUNLOCK(inp);
2585 struct sctp_authkey *sca;
2586 struct sctp_keyhead *shared_keys;
2587 sctp_sharedkey_t *shared_key;
2588 sctp_key_t *key = NULL;
2591 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2592 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id)
2593 size = optsize - sizeof(*sca);
2596 /* set it on the assoc */
2597 shared_keys = &stcb->asoc.shared_keys;
2598 /* clear the cached keys for this key id */
2599 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2601 * create the new shared key and
2605 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2608 SCTP_TCB_UNLOCK(stcb);
2612 shared_key = sctp_alloc_sharedkey();
2613 if (shared_key == NULL) {
2616 SCTP_TCB_UNLOCK(stcb);
2619 shared_key->key = key;
2620 shared_key->keyid = sca->sca_keynumber;
2621 sctp_insert_sharedkey(shared_keys, shared_key);
2622 SCTP_TCB_UNLOCK(stcb);
2624 /* set it on the endpoint */
2625 SCTP_INP_WLOCK(inp);
2626 shared_keys = &inp->sctp_ep.shared_keys;
2628 * clear the cached keys on all assocs for
2631 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2633 * create the new shared key and
2637 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2640 SCTP_INP_WUNLOCK(inp);
2644 shared_key = sctp_alloc_sharedkey();
2645 if (shared_key == NULL) {
2648 SCTP_INP_WUNLOCK(inp);
2651 shared_key->key = key;
2652 shared_key->keyid = sca->sca_keynumber;
2653 sctp_insert_sharedkey(shared_keys, shared_key);
2654 SCTP_INP_WUNLOCK(inp);
2658 case SCTP_HMAC_IDENT:
2660 struct sctp_hmacalgo *shmac;
2661 sctp_hmaclist_t *hmaclist;
2665 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2666 size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2667 hmaclist = sctp_alloc_hmaclist(size);
2668 if (hmaclist == NULL) {
2672 for (i = 0; i < size; i++) {
2673 hmacid = shmac->shmac_idents[i];
2674 if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2675 /* invalid HMACs were found */ ;
2677 sctp_free_hmaclist(hmaclist);
2678 goto sctp_set_hmac_done;
2681 /* set it on the endpoint */
2682 SCTP_INP_WLOCK(inp);
2683 if (inp->sctp_ep.local_hmacs)
2684 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2685 inp->sctp_ep.local_hmacs = hmaclist;
2686 SCTP_INP_WUNLOCK(inp);
2690 case SCTP_AUTH_ACTIVE_KEY:
2692 struct sctp_authkeyid *scact;
2694 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2695 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2697 /* set the active key on the right place */
2699 /* set the active key on the assoc */
2700 if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2702 SCTP_TCB_UNLOCK(stcb);
2704 /* set the active key on the endpoint */
2705 SCTP_INP_WLOCK(inp);
2706 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2708 SCTP_INP_WUNLOCK(inp);
2712 case SCTP_AUTH_DELETE_KEY:
2714 struct sctp_authkeyid *scdel;
2716 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2717 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2719 /* delete the key from the right place */
2721 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2723 SCTP_TCB_UNLOCK(stcb);
2725 SCTP_INP_WLOCK(inp);
2726 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2728 SCTP_INP_WUNLOCK(inp);
2733 case SCTP_RESET_STREAMS:
2735 struct sctp_stream_reset *strrst;
2736 uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2739 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2740 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2746 if (stcb->asoc.peer_supports_strreset == 0) {
2748 * Peer does not support it, we return
2749 * protocol not supported since this is true
2750 * for this feature and this peer, not the
2751 * socket request in general.
2753 error = EPROTONOSUPPORT;
2754 SCTP_TCB_UNLOCK(stcb);
2757 if (stcb->asoc.stream_reset_outstanding) {
2759 SCTP_TCB_UNLOCK(stcb);
2762 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2764 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2766 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2769 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2773 SCTP_TCB_UNLOCK(stcb);
2776 for (i = 0; i < strrst->strrst_num_streams; i++) {
2779 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2784 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2791 SCTP_TCB_UNLOCK(stcb);
2794 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2795 strrst->strrst_list,
2796 send_out, (stcb->asoc.str_reset_seq_in - 3),
2799 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2800 SCTP_TCB_UNLOCK(stcb);
2804 case SCTP_CONNECT_X:
2805 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2809 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2812 case SCTP_CONNECT_X_DELAYED:
2813 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2817 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2820 case SCTP_CONNECT_X_COMPLETE:
2822 struct sockaddr *sa;
2823 struct sctp_nets *net;
2825 /* FIXME MT: check correct? */
2826 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2829 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2830 SCTP_INP_RLOCK(inp);
2831 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2833 SCTP_TCB_LOCK(stcb);
2834 net = sctp_findnet(stcb, sa);
2836 SCTP_INP_RUNLOCK(inp);
2839 * We increment here since
2840 * sctp_findassociation_ep_addr() wil do a
2841 * decrement if it finds the stcb as long as
2842 * the locked tcb (last argument) is NOT a
2845 SCTP_INP_INCR_REF(inp);
2846 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2848 SCTP_INP_DECR_REF(inp);
2856 if (stcb->asoc.delayed_connection == 1) {
2857 stcb->asoc.delayed_connection = 0;
2858 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2859 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
2860 stcb->asoc.primary_destination,
2861 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
2862 sctp_send_initiate(inp, stcb);
2865 * already expired or did not use delayed
2870 SCTP_TCB_UNLOCK(stcb);
2877 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
2879 SCTP_INP_WLOCK(inp);
2881 inp->sctp_ep.max_burst = *burst;
2883 SCTP_INP_WUNLOCK(inp);
2888 struct sctp_assoc_value *av;
2891 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2892 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2896 SCTP_TCB_UNLOCK(stcb);
2898 SCTP_INP_WLOCK(inp);
2899 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2900 ovh = SCTP_MED_OVERHEAD;
2902 ovh = SCTP_MED_V4_OVERHEAD;
2905 * FIXME MT: I think this is not in tune
2908 if (av->assoc_value) {
2909 inp->sctp_frag_point = (av->assoc_value + ovh);
2913 SCTP_INP_WUNLOCK(inp);
2919 struct sctp_event_subscribe *events;
2921 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
2923 SCTP_INP_WLOCK(inp);
2924 if (events->sctp_data_io_event) {
2925 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2927 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2930 if (events->sctp_association_event) {
2931 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2933 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2936 if (events->sctp_address_event) {
2937 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2939 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2942 if (events->sctp_send_failure_event) {
2943 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2945 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2948 if (events->sctp_peer_error_event) {
2949 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2951 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2954 if (events->sctp_shutdown_event) {
2955 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2957 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2960 if (events->sctp_partial_delivery_event) {
2961 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2963 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2966 if (events->sctp_adaptation_layer_event) {
2967 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2969 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2972 if (events->sctp_authentication_event) {
2973 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2975 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2978 if (events->sctp_stream_reset_events) {
2979 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2981 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2983 SCTP_INP_WUNLOCK(inp);
2987 case SCTP_ADAPTATION_LAYER:
2989 struct sctp_setadaptation *adap_bits;
2991 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
2992 SCTP_INP_WLOCK(inp);
2993 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
2994 SCTP_INP_WUNLOCK(inp);
2998 case SCTP_SET_INITIAL_DBG_SEQ:
3002 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3003 SCTP_INP_WLOCK(inp);
3004 inp->sctp_ep.initial_sequence_debug = *vvv;
3005 SCTP_INP_WUNLOCK(inp);
3009 case SCTP_DEFAULT_SEND_PARAM:
3011 struct sctp_sndrcvinfo *s_info;
3013 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3014 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3017 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3018 stcb->asoc.def_send = *s_info;
3022 SCTP_TCB_UNLOCK(stcb);
3024 SCTP_INP_WLOCK(inp);
3025 inp->def_send = *s_info;
3026 SCTP_INP_WUNLOCK(inp);
3030 case SCTP_PEER_ADDR_PARAMS:
3031 /* Applys to the specific association */
3033 struct sctp_paddrparams *paddrp;
3034 struct sctp_nets *net;
3036 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3037 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3040 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3043 * We increment here since
3044 * sctp_findassociation_ep_addr() wil do a
3045 * decrement if it finds the stcb as long as
3046 * the locked tcb (last argument) is NOT a
3049 SCTP_INP_INCR_REF(inp);
3050 stcb = sctp_findassociation_ep_addr(&inp,
3051 (struct sockaddr *)&paddrp->spp_address,
3054 SCTP_INP_DECR_REF(inp);
3060 /************************TCB SPECIFIC SET ******************/
3062 * do we change the timer for HB, we run
3065 if (paddrp->spp_hbinterval)
3066 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3067 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3068 stcb->asoc.heart_beat_delay = 0;
3070 /* network sets ? */
3072 /************************NET SPECIFIC SET ******************/
3073 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3075 sctp_send_hb(stcb, 1, net);
3077 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3078 net->dest_state |= SCTP_ADDR_NOHB;
3080 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3081 net->dest_state &= ~SCTP_ADDR_NOHB;
3083 if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
3084 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3085 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3086 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3088 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3089 net->mtu = paddrp->spp_pathmtu;
3090 if (net->mtu < stcb->asoc.smallest_mtu)
3091 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3094 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3095 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3096 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3099 if (paddrp->spp_pathmaxrxt)
3100 net->failure_threshold = paddrp->spp_pathmaxrxt;
3102 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3103 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3104 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3109 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3110 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3111 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3116 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3117 if (paddrp->spp_pathmaxrxt)
3118 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3120 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3121 /* Turn back on the timer */
3122 stcb->asoc.hb_is_disabled = 0;
3123 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3125 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3126 int cnt_of_unconf = 0;
3127 struct sctp_nets *lnet;
3129 stcb->asoc.hb_is_disabled = 1;
3130 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3131 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3136 * stop the timer ONLY if we
3137 * have no unconfirmed
3140 if (cnt_of_unconf == 0) {
3141 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3144 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3145 /* start up the timer. */
3146 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3149 if (paddrp->spp_flags & SPP_IPV4_TOS)
3150 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3153 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3154 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3158 SCTP_TCB_UNLOCK(stcb);
3160 /************************NO TCB, SET TO default stuff ******************/
3161 SCTP_INP_WLOCK(inp);
3163 * For the TOS/FLOWLABEL stuff you set it
3164 * with the options on the socket
3166 if (paddrp->spp_pathmaxrxt) {
3167 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3169 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3170 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3171 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3172 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3173 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3175 SCTP_INP_WUNLOCK(inp);
3181 struct sctp_rtoinfo *srto;
3183 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3184 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3187 /* Set in ms we hope :-) */
3188 if (srto->srto_initial)
3189 stcb->asoc.initial_rto = srto->srto_initial;
3191 stcb->asoc.maxrto = srto->srto_max;
3193 stcb->asoc.minrto = srto->srto_min;
3194 SCTP_TCB_UNLOCK(stcb);
3196 SCTP_INP_WLOCK(inp);
3198 * If we have a null asoc, its default for
3201 if (srto->srto_initial)
3202 inp->sctp_ep.initial_rto = srto->srto_initial;
3204 inp->sctp_ep.sctp_maxrto = srto->srto_max;
3206 inp->sctp_ep.sctp_minrto = srto->srto_min;
3207 SCTP_INP_WUNLOCK(inp);
3211 case SCTP_ASSOCINFO:
3213 struct sctp_assocparams *sasoc;
3215 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3216 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3219 if (sasoc->sasoc_asocmaxrxt)
3220 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3221 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3222 sasoc->sasoc_peer_rwnd = 0;
3223 sasoc->sasoc_local_rwnd = 0;
3224 if (stcb->asoc.cookie_life)
3225 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3226 stcb->asoc.delayed_ack = sasoc->sasoc_sack_delay;
3227 if (sasoc->sasoc_sack_freq) {
3228 stcb->asoc.sack_freq = sasoc->sasoc_sack_freq;
3230 SCTP_TCB_UNLOCK(stcb);
3232 SCTP_INP_WLOCK(inp);
3233 if (sasoc->sasoc_asocmaxrxt)
3234 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3235 sasoc->sasoc_number_peer_destinations = 0;
3236 sasoc->sasoc_peer_rwnd = 0;
3237 sasoc->sasoc_local_rwnd = 0;
3238 if (sasoc->sasoc_cookie_life)
3239 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3240 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sasoc->sasoc_sack_delay);
3241 if (sasoc->sasoc_sack_freq) {
3242 inp->sctp_ep.sctp_sack_freq = sasoc->sasoc_sack_freq;
3244 SCTP_INP_WUNLOCK(inp);
3250 struct sctp_initmsg *sinit;
3252 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3253 SCTP_INP_WLOCK(inp);
3254 if (sinit->sinit_num_ostreams)
3255 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3257 if (sinit->sinit_max_instreams)
3258 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3260 if (sinit->sinit_max_attempts)
3261 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3263 if (sinit->sinit_max_init_timeo)
3264 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3265 SCTP_INP_WUNLOCK(inp);
3268 case SCTP_PRIMARY_ADDR:
3270 struct sctp_setprim *spa;
3271 struct sctp_nets *net, *lnet;
3273 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3274 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3278 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3281 * We increment here since
3282 * sctp_findassociation_ep_addr() wil do a
3283 * decrement if it finds the stcb as long as
3284 * the locked tcb (last argument) is NOT a
3287 SCTP_INP_INCR_REF(inp);
3288 stcb = sctp_findassociation_ep_addr(&inp,
3289 (struct sockaddr *)&spa->ssp_addr,
3292 SCTP_INP_DECR_REF(inp);
3296 if ((stcb) && (net)) {
3297 if ((net != stcb->asoc.primary_destination) &&
3298 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3299 /* Ok we need to set it */
3300 lnet = stcb->asoc.primary_destination;
3301 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3302 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3303 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3305 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3312 SCTP_TCB_UNLOCK(stcb);
3316 case SCTP_SET_DYNAMIC_PRIMARY:
3318 union sctp_sockstore *ss;
3320 error = priv_check_cred(curthread->td_ucred,
3321 PRIV_NETINET_RESERVEDPORT,
3326 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3327 /* SUPER USER CHECK? */
3328 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3331 case SCTP_SET_PEER_PRIMARY_ADDR:
3333 struct sctp_setpeerprim *sspp;
3335 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3336 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3338 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3341 SCTP_TCB_UNLOCK(stcb);
3348 case SCTP_BINDX_ADD_ADDR:
3350 struct sctp_getaddresses *addrs;
3351 struct sockaddr *addr_touse;
3352 struct sockaddr_in sin;
3354 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3356 /* see if we're bound all already! */
3357 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3361 /* Is the VRF one we have */
3362 addr_touse = addrs->addr;
3364 if (addrs->addr->sa_family == AF_INET6) {
3365 struct sockaddr_in6 *sin6;
3367 sin6 = (struct sockaddr_in6 *)addr_touse;
3368 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3369 in6_sin6_2_sin(&sin, sin6);
3370 addr_touse = (struct sockaddr *)&sin;
3374 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3376 /* Can't get proc for Net/Open BSD */
3380 error = sctp_inpcb_bind(so, addr_touse, p);
3384 * No locks required here since bind and mgmt_ep_sa
3385 * all do their own locking. If we do something for
3386 * the FIX: below we may need to lock in that case.
3388 if (addrs->sget_assoc_id == 0) {
3389 /* add the address */
3390 struct sctp_inpcb *lep;
3392 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3393 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
3396 * We must decrement the refcount
3397 * since we have the ep already and
3398 * are binding. No remove going on
3401 SCTP_INP_DECR_REF(inp);
3404 /* already bound to it.. ok */
3406 } else if (lep == NULL) {
3407 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3408 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3409 SCTP_ADD_IP_ADDRESS, vrf_id);
3411 error = EADDRNOTAVAIL;
3418 * FIX: decide whether we allow assoc based
3424 case SCTP_BINDX_REM_ADDR:
3426 struct sctp_getaddresses *addrs;
3427 struct sockaddr *addr_touse;
3428 struct sockaddr_in sin;
3430 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3431 /* see if we're bound all already! */
3432 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3436 addr_touse = addrs->addr;
3438 if (addrs->addr->sa_family == AF_INET6) {
3439 struct sockaddr_in6 *sin6;
3441 sin6 = (struct sockaddr_in6 *)addr_touse;
3442 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3443 in6_sin6_2_sin(&sin, sin6);
3444 addr_touse = (struct sockaddr *)&sin;
3449 * No lock required mgmt_ep_sa does its own locking.
3450 * If the FIX: below is ever changed we may need to
3451 * lock before calling association level binding.
3453 if (addrs->sget_assoc_id == 0) {
3454 /* delete the address */
3455 sctp_addr_mgmt_ep_sa(inp, addr_touse,
3456 SCTP_DEL_IP_ADDRESS, vrf_id);
3459 * FIX: decide whether we allow assoc based
3466 error = ENOPROTOOPT;
3468 } /* end switch (opt) */
3474 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3476 void *optval = NULL;
3478 struct sctp_inpcb *inp;
3482 inp = (struct sctp_inpcb *)so->so_pcb;
3484 /* I made the same as TCP since we are not setup? */
3485 return (ECONNRESET);
3487 if (sopt->sopt_level != IPPROTO_SCTP) {
3488 /* wrong proto level... send back up to IP */
3490 if (INP_CHECK_SOCKAF(so, AF_INET6))
3491 error = ip6_ctloutput(so, sopt);
3494 error = ip_ctloutput(so, sopt);
3497 optsize = sopt->sopt_valsize;
3499 SCTP_MALLOC(optval, void *, optsize, "SCTPSockOpt");
3500 if (optval == NULL) {
3503 error = sooptcopyin(sopt, optval, optsize, optsize);
3509 p = (void *)sopt->sopt_td;
3510 if (sopt->sopt_dir == SOPT_SET) {
3511 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3512 } else if (sopt->sopt_dir == SOPT_GET) {
3513 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3517 if ((error == 0) && (optval != NULL)) {
3518 error = sooptcopyout(sopt, optval, optsize);
3520 } else if (optval != NULL) {
3529 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3532 int create_lock_on = 0;
3534 struct sctp_inpcb *inp;
3535 struct sctp_tcb *stcb = NULL;
3537 inp = (struct sctp_inpcb *)so->so_pcb;
3539 /* I made the same as TCP since we are not setup? */
3540 return (ECONNRESET);
3542 SCTP_ASOC_CREATE_LOCK(inp);
3545 SCTP_INP_INCR_REF(inp);
3546 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3547 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3548 /* Should I really unlock ? */
3553 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3554 (addr->sa_family == AF_INET6)) {
3559 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3560 SCTP_PCB_FLAGS_UNBOUND) {
3561 /* Bind a ephemeral port */
3562 error = sctp_inpcb_bind(so, NULL, p);
3567 /* Now do we connect? */
3568 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3572 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3573 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3574 /* We are already connected AND the TCP model */
3578 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3579 SCTP_INP_RLOCK(inp);
3580 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3581 SCTP_INP_RUNLOCK(inp);
3584 * We increment here since sctp_findassociation_ep_addr()
3585 * wil do a decrement if it finds the stcb as long as the
3586 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3588 SCTP_INP_INCR_REF(inp);
3589 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3591 SCTP_INP_DECR_REF(inp);
3593 SCTP_TCB_LOCK(stcb);
3597 /* Already have or am bring up an association */
3601 vrf_id = inp->def_vrf_id;
3602 /* We are GOOD to go */
3603 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3605 /* Gak! no memory */
3608 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3609 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3610 /* Set the connected flag so we can queue data */
3613 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3614 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3616 /* initialize authentication parameters for the assoc */
3617 sctp_initialize_auth_params(inp, stcb);
3619 sctp_send_initiate(inp, stcb);
3620 SCTP_TCB_UNLOCK(stcb);
3623 SCTP_ASOC_CREATE_UNLOCK(inp);
3625 SCTP_INP_DECR_REF(inp);
3630 sctp_listen(struct socket *so, int backlog, struct thread *p)
3633 * Note this module depends on the protocol processing being called
3634 * AFTER any socket level flags and backlog are applied to the
3635 * socket. The traditional way that the socket flags are applied is
3636 * AFTER protocol processing. We have made a change to the
3637 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3638 * place if the socket API for SCTP is to work properly.
3642 struct sctp_inpcb *inp;
3644 inp = (struct sctp_inpcb *)so->so_pcb;
3646 /* I made the same as TCP since we are not setup? */
3647 return (ECONNRESET);
3649 SCTP_INP_RLOCK(inp);
3650 #ifdef SCTP_LOCK_LOGGING
3651 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3654 error = solisten_proto_check(so);
3657 SCTP_INP_RUNLOCK(inp);
3660 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3661 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3662 /* We are already connected AND the TCP model */
3663 SCTP_INP_RUNLOCK(inp);
3665 return (EADDRINUSE);
3667 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3668 /* We must do a bind. */
3670 SCTP_INP_RUNLOCK(inp);
3671 if ((error = sctp_inpcb_bind(so, NULL, p))) {
3672 /* bind error, probably perm */
3677 SCTP_INP_RUNLOCK(inp);
3679 /* It appears for 7.0 and on, we must always call this. */
3680 solisten_proto(so, backlog);
3681 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3682 /* remove the ACCEPTCONN flag for one-to-many sockets */
3683 so->so_options &= ~SO_ACCEPTCONN;
3686 /* turning off listen */
3687 so->so_options &= ~SO_ACCEPTCONN;
3693 static int sctp_defered_wakeup_cnt = 0;
3696 sctp_accept(struct socket *so, struct sockaddr **addr)
3698 struct sctp_tcb *stcb;
3699 struct sctp_inpcb *inp;
3700 union sctp_sockstore store;
3704 inp = (struct sctp_inpcb *)so->so_pcb;
3707 return (ECONNRESET);
3709 SCTP_INP_RLOCK(inp);
3710 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3711 SCTP_INP_RUNLOCK(inp);
3714 if (so->so_state & SS_ISDISCONNECTED) {
3715 SCTP_INP_RUNLOCK(inp);
3716 return (ECONNABORTED);
3718 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3720 SCTP_INP_RUNLOCK(inp);
3721 return (ECONNRESET);
3723 SCTP_TCB_LOCK(stcb);
3724 SCTP_INP_RUNLOCK(inp);
3725 store = stcb->asoc.primary_destination->ro._l_addr;
3726 SCTP_TCB_UNLOCK(stcb);
3727 if (store.sa.sa_family == AF_INET) {
3728 struct sockaddr_in *sin;
3730 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3731 sin->sin_family = AF_INET;
3732 sin->sin_len = sizeof(*sin);
3733 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3734 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3735 *addr = (struct sockaddr *)sin;
3737 struct sockaddr_in6 *sin6;
3739 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3740 sin6->sin6_family = AF_INET6;
3741 sin6->sin6_len = sizeof(*sin6);
3742 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3744 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3745 if ((error = sa6_recoverscope(sin6)) != 0) {
3746 SCTP_FREE_SONAME(sin6);
3749 *addr = (struct sockaddr *)sin6;
3751 /* Wake any delayed sleep action */
3752 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3753 SCTP_INP_WLOCK(inp);
3754 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3755 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3756 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3757 SCTP_INP_WUNLOCK(inp);
3758 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3759 if (sowriteable(inp->sctp_socket)) {
3760 sowwakeup_locked(inp->sctp_socket);
3762 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3764 SCTP_INP_WLOCK(inp);
3766 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3767 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3768 SCTP_INP_WUNLOCK(inp);
3769 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3770 if (soreadable(inp->sctp_socket)) {
3771 sctp_defered_wakeup_cnt++;
3772 sorwakeup_locked(inp->sctp_socket);
3774 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3776 SCTP_INP_WLOCK(inp);
3778 SCTP_INP_WUNLOCK(inp);
3784 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3786 struct sockaddr_in *sin;
3788 struct sctp_inpcb *inp;
3789 struct sctp_ifa *sctp_ifa;
3792 * Do the malloc first in case it blocks.
3794 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3795 sin->sin_family = AF_INET;
3796 sin->sin_len = sizeof(*sin);
3797 inp = (struct sctp_inpcb *)so->so_pcb;
3799 SCTP_FREE_SONAME(sin);
3802 SCTP_INP_RLOCK(inp);
3803 sin->sin_port = inp->sctp_lport;
3804 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3805 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3806 struct sctp_tcb *stcb;
3807 struct sockaddr_in *sin_a;
3808 struct sctp_nets *net;
3811 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3817 SCTP_TCB_LOCK(stcb);
3818 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3819 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3821 /* this will make coverity happy */
3824 if (sin_a->sin_family == AF_INET) {
3829 if ((!fnd) || (sin_a == NULL)) {
3831 SCTP_TCB_UNLOCK(stcb);
3834 vrf_id = inp->def_vrf_id;
3835 sctp_ifa = sctp_source_address_selection(inp,
3837 (sctp_route_t *) & net->ro,
3840 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
3841 sctp_free_ifa(sctp_ifa);
3843 SCTP_TCB_UNLOCK(stcb);
3845 /* For the bound all case you get back 0 */
3847 sin->sin_addr.s_addr = 0;
3851 /* Take the first IPv4 address in the list */
3852 struct sctp_laddr *laddr;
3855 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3856 if (laddr->ifa->address.sa.sa_family == AF_INET) {
3857 struct sockaddr_in *sin_a;
3859 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
3860 sin->sin_addr = sin_a->sin_addr;
3866 SCTP_FREE_SONAME(sin);
3867 SCTP_INP_RUNLOCK(inp);
3871 SCTP_INP_RUNLOCK(inp);
3872 (*addr) = (struct sockaddr *)sin;
3877 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
3879 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
3881 struct sockaddr_in *sin_a;
3882 struct sctp_inpcb *inp;
3883 struct sctp_tcb *stcb;
3884 struct sctp_nets *net;
3886 /* Do the malloc first in case it blocks. */
3887 inp = (struct sctp_inpcb *)so->so_pcb;
3888 if ((inp == NULL) ||
3889 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3890 /* UDP type and listeners will drop out here */
3893 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3894 sin->sin_family = AF_INET;
3895 sin->sin_len = sizeof(*sin);
3897 /* We must recapture incase we blocked */
3898 inp = (struct sctp_inpcb *)so->so_pcb;
3900 SCTP_FREE_SONAME(sin);
3903 SCTP_INP_RLOCK(inp);
3904 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3906 SCTP_TCB_LOCK(stcb);
3907 SCTP_INP_RUNLOCK(inp);
3909 SCTP_FREE_SONAME(sin);
3913 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3914 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3915 if (sin_a->sin_family == AF_INET) {
3917 sin->sin_port = stcb->rport;
3918 sin->sin_addr = sin_a->sin_addr;
3922 SCTP_TCB_UNLOCK(stcb);
3924 /* No IPv4 address */
3925 SCTP_FREE_SONAME(sin);
3928 (*addr) = (struct sockaddr *)sin;
3932 struct pr_usrreqs sctp_usrreqs = {
3933 .pru_abort = sctp_abort,
3934 .pru_accept = sctp_accept,
3935 .pru_attach = sctp_attach,
3936 .pru_bind = sctp_bind,
3937 .pru_connect = sctp_connect,
3938 .pru_control = in_control,
3939 .pru_close = sctp_close,
3940 .pru_detach = sctp_close,
3941 .pru_sopoll = sopoll_generic,
3942 .pru_disconnect = sctp_disconnect,
3943 .pru_listen = sctp_listen,
3944 .pru_peeraddr = sctp_peeraddr,
3945 .pru_send = sctp_sendm,
3946 .pru_shutdown = sctp_shutdown,
3947 .pru_sockaddr = sctp_ingetaddr,
3948 .pru_sosend = sctp_sosend,
3949 .pru_soreceive = sctp_soreceive