2 * Copyright (c) 2001-2007, by 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>
51 #include <netinet/sctp_bsd_addr.h>
59 /* Init the SCTP pcb in sctp_pcb.c */
64 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
65 sctp_max_chunks_on_queue = (nmbclusters / 8);
67 * Allow a user to take no more than 1/2 the number of clusters or
68 * the SB_MAX whichever is smaller for the send window.
70 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
71 sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
72 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
74 * Now for the recv window, should we take the same amount? or
75 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
76 * now I will just copy.
78 sctp_recvspace = sctp_sendspace;
86 * cleanup of the sctppcbinfo structure.
87 * Assumes that the sctppcbinfo lock is held.
90 sctp_pcbinfo_cleanup(void)
92 /* free the hash tables */
93 if (sctppcbinfo.sctp_asochash != NULL)
94 SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
95 if (sctppcbinfo.sctp_ephash != NULL)
96 SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
97 if (sctppcbinfo.sctp_tcpephash != NULL)
98 SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
99 if (sctppcbinfo.sctp_restarthash != NULL)
100 SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
105 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
106 struct sctp_tcb *stcb,
107 struct sctp_nets *net,
110 struct sctp_tmit_chunk *chk;
112 /* Adjust that too */
113 stcb->asoc.smallest_mtu = nxtsz;
114 /* now off to subtract IP_DF flag if needed */
115 #ifdef SCTP_PRINT_FOR_B_AND_M
116 SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
117 inp, stcb, net, nxtsz);
119 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
120 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
121 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
124 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
125 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
127 * For this guy we also mark for immediate resend
128 * since we sent to big of chunk
130 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
131 if (chk->sent != SCTP_DATAGRAM_RESEND) {
132 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
134 chk->sent = SCTP_DATAGRAM_RESEND;
135 chk->rec.data.doing_fast_retransmit = 0;
136 #ifdef SCTP_FLIGHT_LOGGING
137 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
138 chk->whoTo->flight_size,
140 (uintptr_t) chk->whoTo,
141 chk->rec.data.TSN_seq);
143 /* Clear any time so NO RTT is being done */
145 sctp_flight_size_decrease(chk);
146 sctp_total_flight_decrease(stcb, chk);
152 sctp_notify_mbuf(struct sctp_inpcb *inp,
153 struct sctp_tcb *stcb,
154 struct sctp_nets *net,
159 int totsz, tmr_stopped = 0;
163 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
164 (ip == NULL) || (sh == NULL)) {
166 SCTP_TCB_UNLOCK(stcb);
170 /* First job is to verify the vtag matches what I would send */
171 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
172 SCTP_TCB_UNLOCK(stcb);
175 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
177 if (icmph->icmp_type != ICMP_UNREACH) {
178 /* We only care about unreachable */
179 SCTP_TCB_UNLOCK(stcb);
182 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
183 /* not a unreachable message due to frag. */
184 SCTP_TCB_UNLOCK(stcb);
189 nxtsz = ntohs(icmph->icmp_seq);
192 * old type router that does not tell us what the next size
193 * mtu is. Rats we will have to guess (in a educated fashion
196 nxtsz = find_next_best_mtu(totsz);
198 /* Stop any PMTU timer */
199 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
201 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
202 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
204 /* Adjust destination size limit */
205 if (net->mtu > nxtsz) {
208 /* now what about the ep? */
209 if (stcb->asoc.smallest_mtu > nxtsz) {
210 #ifdef SCTP_PRINT_FOR_B_AND_M
211 SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
214 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
217 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
219 SCTP_TCB_UNLOCK(stcb);
224 sctp_notify(struct sctp_inpcb *inp,
228 struct sctp_tcb *stcb,
229 struct sctp_nets *net)
232 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
233 (sh == NULL) || (to == NULL)) {
236 /* First job is to verify the vtag matches what I would send */
237 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
240 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
242 if ((error == EHOSTUNREACH) || /* Host is not reachable */
243 (error == EHOSTDOWN) || /* Host is down */
244 (error == ECONNREFUSED) || /* Host refused the connection, (not
246 (error == ENOPROTOOPT) /* SCTP is not present on host */
249 * Hmm reachablity problems we must examine closely. If its
250 * not reachable, we may have lost a network. Or if there is
251 * NO protocol at the other end named SCTP. well we consider
254 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
255 if (net->dest_state & SCTP_ADDR_REACHABLE) {
256 /* Ok that destination is NOT reachable */
257 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
259 net->failure_threshold,
262 net->dest_state &= ~SCTP_ADDR_REACHABLE;
263 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
264 net->error_count = net->failure_threshold + 1;
265 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
266 stcb, SCTP_FAILED_THRESHOLD,
270 SCTP_TCB_UNLOCK(stcb);
274 * Here the peer is either playing tricks on us,
275 * including an address that belongs to someone who
276 * does not support SCTP OR was a userland
277 * implementation that shutdown and now is dead. In
278 * either case treat it like a OOTB abort with no
281 sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
282 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
283 /* no need to unlock here, since the TCB is gone */
286 /* Send all others to the app */
288 SCTP_TCB_UNLOCK(stcb);
290 if (inp->sctp_socket) {
291 #ifdef SCTP_LOCK_LOGGING
292 sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
294 SOCK_LOCK(inp->sctp_socket);
295 inp->sctp_socket->so_error = error;
296 sctp_sowwakeup(inp, inp->sctp_socket);
297 SOCK_UNLOCK(inp->sctp_socket);
303 sctp_ctlinput(cmd, sa, vip)
312 /* FIX, for non-bsd is this right? */
313 vrf_id = SCTP_DEFAULT_VRFID;
314 if (sa->sa_family != AF_INET ||
315 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
318 if (PRC_IS_REDIRECT(cmd)) {
320 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
324 struct sctp_inpcb *inp = NULL;
325 struct sctp_tcb *stcb = NULL;
326 struct sctp_nets *net = NULL;
327 struct sockaddr_in to, from;
329 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
330 bzero(&to, sizeof(to));
331 bzero(&from, sizeof(from));
332 from.sin_family = to.sin_family = AF_INET;
333 from.sin_len = to.sin_len = sizeof(to);
334 from.sin_port = sh->src_port;
335 from.sin_addr = ip->ip_src;
336 to.sin_port = sh->dest_port;
337 to.sin_addr = ip->ip_dst;
340 * 'to' holds the dest of the packet that failed to be sent.
341 * 'from' holds our local endpoint address. Thus we reverse
342 * the to and the from in the lookup.
344 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
345 (struct sockaddr *)&to,
346 &inp, &net, 1, vrf_id);
347 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
348 if (cmd != PRC_MSGSIZE) {
351 if (cmd == PRC_HOSTDEAD) {
354 cm = inetctlerrmap[cmd];
356 sctp_notify(inp, cm, sh,
357 (struct sockaddr *)&to, stcb,
360 /* handle possible ICMP size messages */
361 sctp_notify_mbuf(inp, stcb, net, ip, sh);
364 if ((stcb == NULL) && (inp != NULL)) {
365 /* reduce ref-count */
367 SCTP_INP_DECR_REF(inp);
368 SCTP_INP_WUNLOCK(inp);
376 sctp_getcred(SYSCTL_HANDLER_ARGS)
379 struct sockaddr_in addrs[2];
380 struct sctp_inpcb *inp;
381 struct sctp_nets *net;
382 struct sctp_tcb *stcb;
386 /* FIX, for non-bsd is this right? */
387 vrf_id = SCTP_DEFAULT_VRFID;
389 error = priv_check(req->td, PRIV_NETINET_GETCRED);
394 error = SYSCTL_IN(req, addrs, sizeof(addrs));
398 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
400 &inp, &net, 1, vrf_id);
401 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
402 if ((inp != NULL) && (stcb == NULL)) {
403 /* reduce ref-count */
405 SCTP_INP_DECR_REF(inp);
411 SCTP_TCB_UNLOCK(stcb);
413 * We use the write lock here, only since in the error leg we need
414 * it. If we used RLOCK, then we would have to
415 * wlock/decr/unlock/rlock. Which in theory could create a hole.
416 * Better to use higher wlock.
420 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
422 SCTP_INP_WUNLOCK(inp);
425 cru2x(inp->sctp_socket->so_cred, &xuc);
426 SCTP_INP_WUNLOCK(inp);
427 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
432 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
433 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
437 sctp_abort(struct socket *so)
439 struct sctp_inpcb *inp;
442 inp = (struct sctp_inpcb *)so->so_pcb;
447 flags = inp->sctp_flags;
448 #ifdef SCTP_LOG_CLOSING
449 sctp_log_closing(inp, NULL, 17);
451 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
452 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
453 #ifdef SCTP_LOG_CLOSING
454 sctp_log_closing(inp, NULL, 16);
456 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
457 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
459 SCTP_SB_CLEAR(so->so_snd);
461 * same for the rcv ones, they are only here for the
464 SCTP_SB_CLEAR(so->so_rcv);
466 /* Now null out the reference, we are completely detached. */
470 flags = inp->sctp_flags;
471 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
472 goto sctp_must_try_again;
479 sctp_attach(struct socket *so, int proto, struct thread *p)
481 struct sctp_inpcb *inp;
482 struct inpcb *ip_inp;
484 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
490 inp = (struct sctp_inpcb *)so->so_pcb;
494 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
498 error = sctp_inpcb_alloc(so, vrf_id);
502 inp = (struct sctp_inpcb *)so->so_pcb;
505 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
506 ip_inp = &inp->ip_inp.inp;
507 ip_inp->inp_vflag |= INP_IPV4;
508 ip_inp->inp_ip_ttl = ip_defttl;
511 error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
512 #ifdef SCTP_LOG_CLOSING
513 sctp_log_closing(inp, NULL, 17);
516 flags = inp->sctp_flags;
517 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
518 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
519 #ifdef SCTP_LOG_CLOSING
520 sctp_log_closing(inp, NULL, 15);
522 SCTP_INP_WUNLOCK(inp);
523 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
524 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
526 SCTP_INP_WUNLOCK(inp);
531 SCTP_INP_WUNLOCK(inp);
536 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
538 struct sctp_inpcb *inp;
542 if (addr && addr->sa_family != AF_INET)
543 /* must be a v4 address! */
546 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
549 inp = (struct sctp_inpcb *)so->so_pcb;
553 error = sctp_inpcb_bind(so, addr, p);
558 sctp_close(struct socket *so)
560 struct sctp_inpcb *inp;
563 inp = (struct sctp_inpcb *)so->so_pcb;
568 * Inform all the lower layer assoc that we are done.
571 flags = inp->sctp_flags;
572 #ifdef SCTP_LOG_CLOSING
573 sctp_log_closing(inp, NULL, 17);
575 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
576 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
577 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
578 (so->so_rcv.sb_cc > 0)) {
579 #ifdef SCTP_LOG_CLOSING
580 sctp_log_closing(inp, NULL, 13);
582 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
583 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
585 #ifdef SCTP_LOG_CLOSING
586 sctp_log_closing(inp, NULL, 14);
588 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
589 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
592 * The socket is now detached, no matter what the state of
593 * the SCTP association.
596 SCTP_SB_CLEAR(so->so_snd);
598 * same for the rcv ones, they are only here for the
601 SCTP_SB_CLEAR(so->so_rcv);
603 /* Now null out the reference, we are completely detached. */
607 flags = inp->sctp_flags;
608 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
609 goto sctp_must_try_again;
617 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
618 struct mbuf *control, struct thread *p);
622 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
623 struct mbuf *control, struct thread *p)
625 struct sctp_inpcb *inp;
628 inp = (struct sctp_inpcb *)so->so_pcb;
631 sctp_m_freem(control);
637 /* Got to have an to address if we are NOT a connected socket */
638 if ((addr == NULL) &&
639 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
640 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
643 } else if (addr == NULL) {
644 error = EDESTADDRREQ;
647 sctp_m_freem(control);
653 if (addr->sa_family != AF_INET) {
654 /* must be a v4 address! */
657 sctp_m_freem(control);
660 error = EDESTADDRREQ;
665 /* now what about control */
668 SCTP_PRINTF("huh? control set?\n");
669 sctp_m_freem(inp->control);
672 inp->control = control;
676 SCTP_BUF_NEXT(inp->pkt_last) = m;
679 inp->pkt_last = inp->pkt = m;
682 /* FreeBSD uses a flag passed */
683 ((flags & PRUS_MORETOCOME) == 0)
686 * note with the current version this code will only be used
687 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
688 * re-defining sosend to use the sctp_sosend. One can
689 * optionally switch back to this code (by changing back the
690 * definitions) but this is not advisable. This code is used
691 * by FreeBSD when sending a file with sendfile() though.
695 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
705 sctp_disconnect(struct socket *so)
707 struct sctp_inpcb *inp;
709 inp = (struct sctp_inpcb *)so->so_pcb;
714 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
715 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
717 SCTP_INP_RUNLOCK(inp);
720 struct sctp_association *asoc;
721 struct sctp_tcb *stcb;
723 stcb = LIST_FIRST(&inp->sctp_asoc_list);
725 SCTP_INP_RUNLOCK(inp);
730 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
731 /* We are about to be freed, out of here */
732 SCTP_TCB_UNLOCK(stcb);
733 SCTP_INP_RUNLOCK(inp);
736 if (((so->so_options & SO_LINGER) &&
737 (so->so_linger == 0)) ||
738 (so->so_rcv.sb_cc > 0)) {
739 if (SCTP_GET_STATE(asoc) !=
740 SCTP_STATE_COOKIE_WAIT) {
741 /* Left with Data unread */
744 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
750 struct sctp_paramhdr *ph;
752 ph = mtod(err, struct sctp_paramhdr *);
753 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
754 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
755 ph->param_length = htons(SCTP_BUF_LEN(err));
757 sctp_send_abort_tcb(stcb, err);
758 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
760 SCTP_INP_RUNLOCK(inp);
761 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
762 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
763 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
765 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
766 /* No unlock tcb assoc is gone */
769 if (TAILQ_EMPTY(&asoc->send_queue) &&
770 TAILQ_EMPTY(&asoc->sent_queue) &&
771 (asoc->stream_queue_cnt == 0)) {
772 /* there is nothing queued to send, so done */
773 if (asoc->locked_on_sending) {
776 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
777 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
778 /* only send SHUTDOWN 1st time thru */
779 sctp_stop_timers_for_shutdown(stcb);
780 sctp_send_shutdown(stcb,
781 stcb->asoc.primary_destination);
782 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
783 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
784 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
785 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
787 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
788 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
790 asoc->primary_destination);
791 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
793 asoc->primary_destination);
797 * we still got (or just got) data to send,
798 * so set SHUTDOWN_PENDING
801 * XXX sockets draft says that SCTP_EOF
802 * should be sent with no data. currently,
803 * we will allow user data to be sent first
804 * and move to SHUTDOWN-PENDING
806 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
807 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
808 asoc->primary_destination);
809 if (asoc->locked_on_sending) {
810 /* Locked to send out the data */
811 struct sctp_stream_queue_pending *sp;
813 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
815 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
816 asoc->locked_on_sending->stream_no);
818 if ((sp->length == 0) && (sp->msg_is_complete == 0))
819 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
822 if (TAILQ_EMPTY(&asoc->send_queue) &&
823 TAILQ_EMPTY(&asoc->sent_queue) &&
824 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
828 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
829 0, M_DONTWAIT, 1, MT_DATA);
835 struct sctp_paramhdr *ph;
838 SCTP_BUF_LEN(op_err) =
839 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
841 struct sctp_paramhdr *);
842 ph->param_type = htons(
843 SCTP_CAUSE_USER_INITIATED_ABT);
844 ph->param_length = htons(SCTP_BUF_LEN(op_err));
845 ippp = (uint32_t *) (ph + 1);
846 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
848 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
849 sctp_send_abort_tcb(stcb, op_err);
850 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
851 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
852 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
853 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
855 SCTP_INP_RUNLOCK(inp);
856 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
860 SCTP_TCB_UNLOCK(stcb);
861 SCTP_INP_RUNLOCK(inp);
866 /* UDP model does not support this */
867 SCTP_INP_RUNLOCK(inp);
873 sctp_shutdown(struct socket *so)
875 struct sctp_inpcb *inp;
877 inp = (struct sctp_inpcb *)so->so_pcb;
882 /* For UDP model this is a invalid call */
883 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
884 /* Restore the flags that the soshutdown took away. */
885 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
886 /* This proc will wakeup for read and do nothing (I hope) */
887 SCTP_INP_RUNLOCK(inp);
891 * Ok if we reach here its the TCP model and it is either a SHUT_WR
892 * or SHUT_RDWR. This means we put the shutdown flag against it.
895 struct sctp_tcb *stcb;
896 struct sctp_association *asoc;
900 stcb = LIST_FIRST(&inp->sctp_asoc_list);
903 * Ok we hit the case that the shutdown call was
904 * made after an abort or something. Nothing to do
907 SCTP_INP_RUNLOCK(inp);
912 if (TAILQ_EMPTY(&asoc->send_queue) &&
913 TAILQ_EMPTY(&asoc->sent_queue) &&
914 (asoc->stream_queue_cnt == 0)) {
915 if (asoc->locked_on_sending) {
918 /* there is nothing queued to send, so I'm done... */
919 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
920 /* only send SHUTDOWN the first time through */
921 sctp_stop_timers_for_shutdown(stcb);
922 sctp_send_shutdown(stcb,
923 stcb->asoc.primary_destination);
924 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
925 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
926 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
927 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
929 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
930 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
932 asoc->primary_destination);
933 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
935 asoc->primary_destination);
939 * we still got (or just got) data to send, so set
942 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
943 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
944 asoc->primary_destination);
946 if (asoc->locked_on_sending) {
947 /* Locked to send out the data */
948 struct sctp_stream_queue_pending *sp;
950 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
952 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
953 asoc->locked_on_sending->stream_no);
955 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
956 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
960 if (TAILQ_EMPTY(&asoc->send_queue) &&
961 TAILQ_EMPTY(&asoc->sent_queue) &&
962 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
966 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
967 0, M_DONTWAIT, 1, MT_DATA);
969 /* Fill in the user initiated abort */
970 struct sctp_paramhdr *ph;
973 SCTP_BUF_LEN(op_err) =
974 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
976 struct sctp_paramhdr *);
977 ph->param_type = htons(
978 SCTP_CAUSE_USER_INITIATED_ABT);
979 ph->param_length = htons(SCTP_BUF_LEN(op_err));
980 ippp = (uint32_t *) (ph + 1);
981 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
983 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
984 sctp_abort_an_association(stcb->sctp_ep, stcb,
985 SCTP_RESPONSE_TO_USER_REQ,
990 SCTP_TCB_UNLOCK(stcb);
993 SCTP_INP_RUNLOCK(inp);
998 * copies a "user" presentable address and removes embedded scope, etc.
999 * returns 0 on success, 1 on error
1002 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1004 struct sockaddr_in6 lsa6;
1006 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1008 memcpy(ss, sa, sa->sa_len);
1015 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1016 struct sctp_tcb *stcb,
1018 struct sockaddr_storage *sas,
1021 struct sctp_ifn *sctp_ifn;
1022 struct sctp_ifa *sctp_ifa;
1023 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1025 int ipv4_addr_legal, ipv6_addr_legal;
1026 struct sctp_vrf *vrf;
1033 /* Turn on all the appropriate scope */
1034 loopback_scope = stcb->asoc.loopback_scope;
1035 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1036 local_scope = stcb->asoc.local_scope;
1037 site_scope = stcb->asoc.site_scope;
1039 /* Turn on ALL scope, since we look at the EP */
1040 loopback_scope = ipv4_local_scope = local_scope =
1043 ipv4_addr_legal = ipv6_addr_legal = 0;
1044 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1045 ipv6_addr_legal = 1;
1046 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1047 ipv4_addr_legal = 1;
1050 ipv4_addr_legal = 1;
1052 vrf = sctp_find_vrf(vrf_id);
1056 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1057 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1058 if ((loopback_scope == 0) &&
1059 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1060 /* Skip loopback if loopback_scope not set */
1063 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1066 * For the BOUND-ALL case, the list
1067 * associated with a TCB is Always
1068 * considered a reverse list.. i.e.
1069 * it lists addresses that are NOT
1070 * part of the association. If this
1071 * is one of those we must skip it.
1073 if (sctp_is_addr_restricted(stcb,
1078 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1079 (ipv4_addr_legal)) {
1080 struct sockaddr_in *sin;
1082 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1083 if (sin->sin_addr.s_addr == 0) {
1085 * we skip unspecifed
1090 if ((ipv4_local_scope == 0) &&
1091 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1094 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1095 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1096 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1097 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1098 actual += sizeof(sizeof(struct sockaddr_in6));
1100 memcpy(sas, sin, sizeof(*sin));
1101 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1102 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1103 actual += sizeof(*sin);
1105 if (actual >= limit) {
1108 } else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1109 (ipv6_addr_legal)) {
1110 struct sockaddr_in6 *sin6;
1112 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1113 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1115 * we skip unspecifed
1120 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1121 if (local_scope == 0)
1123 if (sin6->sin6_scope_id == 0) {
1124 if (sa6_recoverscope(sin6) != 0)
1133 if ((site_scope == 0) &&
1134 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1137 memcpy(sas, sin6, sizeof(*sin6));
1138 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1139 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1140 actual += sizeof(*sin6);
1141 if (actual >= limit) {
1148 struct sctp_laddr *laddr;
1150 /* The list is a NEGATIVE list */
1151 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1153 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1157 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1160 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1161 sas = (struct sockaddr_storage *)((caddr_t)sas +
1162 laddr->ifa->address.sa.sa_len);
1163 actual += laddr->ifa->address.sa.sa_len;
1164 if (actual >= limit) {
1173 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1174 struct sctp_tcb *stcb,
1176 struct sockaddr_storage *sas)
1180 /* fill up addresses for the endpoint's default vrf */
1181 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1187 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1190 struct sctp_vrf *vrf = NULL;
1193 * In both sub-set bound an bound_all cases we return the MAXIMUM
1194 * number of addresses that you COULD get. In reality the sub-set
1195 * bound may have an exclusion list for a given TCB OR in the
1196 * bound-all case a TCB may NOT include the loopback or other
1197 * addresses as well.
1199 vrf = sctp_find_vrf(vrf_id);
1203 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1204 struct sctp_ifn *sctp_ifn;
1205 struct sctp_ifa *sctp_ifa;
1207 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1208 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1209 /* Count them if they are the right type */
1210 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1211 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1212 cnt += sizeof(struct sockaddr_in6);
1214 cnt += sizeof(struct sockaddr_in);
1216 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1217 cnt += sizeof(struct sockaddr_in6);
1221 struct sctp_laddr *laddr;
1223 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1224 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1225 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1226 cnt += sizeof(struct sockaddr_in6);
1228 cnt += sizeof(struct sockaddr_in);
1230 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1231 cnt += sizeof(struct sockaddr_in6);
1238 sctp_count_max_addresses(struct sctp_inpcb *inp)
1242 /* count addresses for the endpoint's default VRF */
1243 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1248 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1249 size_t optsize, void *p, int delay)
1252 int creat_lock_on = 0;
1253 struct sctp_tcb *stcb = NULL;
1254 struct sockaddr *sa;
1255 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1258 int bad_addresses = 0;
1261 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1263 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1264 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1265 /* We are already connected AND the TCP model */
1266 return (EADDRINUSE);
1268 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1271 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1272 SCTP_INP_RLOCK(inp);
1273 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1274 SCTP_INP_RUNLOCK(inp);
1279 SCTP_INP_INCR_REF(inp);
1280 SCTP_ASOC_CREATE_LOCK(inp);
1282 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1283 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1287 totaddrp = (int *)optval;
1288 totaddr = *totaddrp;
1289 sa = (struct sockaddr *)(totaddrp + 1);
1290 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1291 if ((stcb != NULL) || bad_addresses) {
1292 /* Already have or am bring up an association */
1293 SCTP_ASOC_CREATE_UNLOCK(inp);
1295 SCTP_TCB_UNLOCK(stcb);
1296 if (bad_addresses == 0)
1301 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1306 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1308 struct in6pcb *inp6;
1310 inp6 = (struct in6pcb *)inp;
1311 if (SCTP_IPV6_V6ONLY(inp6)) {
1313 * if IPV6_V6ONLY flag, ignore connections destined
1314 * to a v4 addr or v4-mapped addr
1321 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1322 SCTP_PCB_FLAGS_UNBOUND) {
1323 /* Bind a ephemeral port */
1324 error = sctp_inpcb_bind(so, NULL, p);
1329 /* FIX ME: do we want to pass in a vrf on the connect call? */
1330 vrf_id = inp->def_vrf_id;
1332 /* We are GOOD to go */
1333 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1335 /* Gak! no memory */
1338 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1339 /* move to second address */
1340 if (sa->sa_family == AF_INET)
1341 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1343 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1346 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1347 /* Fill in the return id */
1349 sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1352 a_id = (sctp_assoc_t *) optval;
1353 *a_id = sctp_get_associd(stcb);
1355 /* initialize authentication parameters for the assoc */
1356 sctp_initialize_auth_params(inp, stcb);
1359 /* doing delayed connection */
1360 stcb->asoc.delayed_connection = 1;
1361 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1363 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1364 sctp_send_initiate(inp, stcb);
1366 SCTP_TCB_UNLOCK(stcb);
1367 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1368 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1369 /* Set the connected flag so we can queue data */
1373 if (creat_lock_on) {
1374 SCTP_ASOC_CREATE_UNLOCK(inp);
1376 SCTP_INP_DECR_REF(inp);
1380 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1381 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1382 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1383 SCTP_INP_RLOCK(inp); \
1384 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1386 SCTP_TCB_LOCK(stcb); \
1388 SCTP_INP_RUNLOCK(inp); \
1389 } else if (assoc_id != 0) { \
1390 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1391 if (stcb == NULL) { \
1401 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1402 if (size < sizeof(type)) { \
1406 destp = (type *)srcp; \
1411 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1414 struct sctp_inpcb *inp;
1416 struct sctp_tcb *stcb = NULL;
1418 if (optval == NULL) {
1421 inp = (struct sctp_inpcb *)so->so_pcb;
1428 case SCTP_AUTOCLOSE:
1429 case SCTP_EXPLICIT_EOR:
1430 case SCTP_AUTO_ASCONF:
1431 case SCTP_DISABLE_FRAGMENTS:
1432 case SCTP_I_WANT_MAPPED_V4_ADDR:
1433 case SCTP_USE_EXT_RCVINFO:
1434 SCTP_INP_RLOCK(inp);
1436 case SCTP_DISABLE_FRAGMENTS:
1437 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1439 case SCTP_I_WANT_MAPPED_V4_ADDR:
1440 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1442 case SCTP_AUTO_ASCONF:
1443 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1445 case SCTP_EXPLICIT_EOR:
1446 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1449 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1451 case SCTP_USE_EXT_RCVINFO:
1452 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1454 case SCTP_AUTOCLOSE:
1455 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1456 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1462 error = ENOPROTOOPT;
1463 } /* end switch (sopt->sopt_name) */
1464 if (optname != SCTP_AUTOCLOSE) {
1465 /* make it an "on/off" value */
1468 if (*optsize < sizeof(val)) {
1471 SCTP_INP_RUNLOCK(inp);
1473 /* return the option value */
1474 *(int *)optval = val;
1475 *optsize = sizeof(val);
1478 case SCTP_GET_PACKET_LOG:
1480 #ifdef SCTP_PACKET_LOGGING
1484 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1485 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1492 case SCTP_PARTIAL_DELIVERY_POINT:
1496 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1497 *value = inp->partial_delivery_point;
1498 *optsize = sizeof(uint32_t);
1501 case SCTP_FRAGMENT_INTERLEAVE:
1505 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1506 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1507 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1508 *value = SCTP_FRAG_LEVEL_2;
1510 *value = SCTP_FRAG_LEVEL_1;
1513 *value = SCTP_FRAG_LEVEL_0;
1515 *optsize = sizeof(uint32_t);
1518 case SCTP_CMT_ON_OFF:
1520 struct sctp_assoc_value *av;
1522 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1523 if (sctp_cmt_on_off) {
1524 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1526 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1527 SCTP_TCB_UNLOCK(stcb);
1533 error = ENOPROTOOPT;
1535 *optsize = sizeof(*av);
1538 case SCTP_GET_ADDR_LEN:
1540 struct sctp_assoc_value *av;
1542 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1545 if (av->assoc_value == AF_INET) {
1546 av->assoc_value = sizeof(struct sockaddr_in);
1551 if (av->assoc_value == AF_INET6) {
1552 av->assoc_value = sizeof(struct sockaddr_in6);
1556 *optsize = sizeof(*av);
1559 case SCTP_GET_ASSOC_NUMBER:
1561 uint32_t *value, cnt;
1563 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1565 SCTP_INP_RLOCK(inp);
1566 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1569 SCTP_INP_RUNLOCK(inp);
1571 *optsize = sizeof(uint32_t);
1575 case SCTP_GET_ASSOC_ID_LIST:
1577 struct sctp_assoc_ids *ids;
1578 unsigned int at, limit;
1580 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1582 limit = *optsize / sizeof(sctp_assoc_t);
1583 SCTP_INP_RLOCK(inp);
1584 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1586 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1592 SCTP_INP_RUNLOCK(inp);
1593 *optsize = at * sizeof(sctp_assoc_t);
1598 struct sctp_assoc_value *av;
1600 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1601 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1604 av->assoc_value = stcb->asoc.context;
1605 SCTP_TCB_UNLOCK(stcb);
1607 SCTP_INP_RLOCK(inp);
1608 av->assoc_value = inp->sctp_context;
1609 SCTP_INP_RUNLOCK(inp);
1611 *optsize = sizeof(*av);
1616 uint32_t *default_vrfid;
1618 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1619 *default_vrfid = inp->def_vrf_id;
1622 case SCTP_GET_ASOC_VRF:
1624 struct sctp_assoc_value *id;
1626 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1627 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1632 id->assoc_value = stcb->asoc.vrf_id;
1635 case SCTP_GET_VRF_IDS:
1640 case SCTP_GET_NONCE_VALUES:
1642 struct sctp_get_nonce_values *gnv;
1644 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1645 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1648 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1649 gnv->gn_local_tag = stcb->asoc.my_vtag;
1650 SCTP_TCB_UNLOCK(stcb);
1654 *optsize = sizeof(*gnv);
1657 case SCTP_DELAYED_SACK:
1659 struct sctp_sack_info *sack;
1661 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1662 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1664 sack->sack_delay = stcb->asoc.delayed_ack;
1665 sack->sack_freq = stcb->asoc.sack_freq;
1666 SCTP_TCB_UNLOCK(stcb);
1668 SCTP_INP_RLOCK(inp);
1669 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1670 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1671 SCTP_INP_RUNLOCK(inp);
1673 *optsize = sizeof(*sack);
1677 case SCTP_GET_SNDBUF_USE:
1679 struct sctp_sockstat *ss;
1681 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1682 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1685 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1686 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1687 stcb->asoc.size_on_all_streams);
1688 SCTP_TCB_UNLOCK(stcb);
1692 *optsize = sizeof(struct sctp_sockstat);
1695 case SCTP_MAX_BURST:
1699 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1701 SCTP_INP_RLOCK(inp);
1702 *value = inp->sctp_ep.max_burst;
1703 SCTP_INP_RUNLOCK(inp);
1704 *optsize = sizeof(uint8_t);
1709 struct sctp_assoc_value *av;
1712 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1713 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1716 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1717 SCTP_TCB_UNLOCK(stcb);
1719 SCTP_INP_RLOCK(inp);
1720 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1721 ovh = SCTP_MED_OVERHEAD;
1723 ovh = SCTP_MED_V4_OVERHEAD;
1725 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1726 av->assoc_value = 0;
1728 av->assoc_value = inp->sctp_frag_point - ovh;
1729 SCTP_INP_RUNLOCK(inp);
1731 *optsize = sizeof(struct sctp_assoc_value);
1734 case SCTP_GET_STAT_LOG:
1735 #ifdef SCTP_STAT_LOGGING
1736 error = sctp_fill_stat_log(optval, optsize);
1743 struct sctp_event_subscribe *events;
1745 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1746 memset(events, 0, sizeof(*events));
1747 SCTP_INP_RLOCK(inp);
1748 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1749 events->sctp_data_io_event = 1;
1751 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1752 events->sctp_association_event = 1;
1754 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1755 events->sctp_address_event = 1;
1757 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1758 events->sctp_send_failure_event = 1;
1760 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1761 events->sctp_peer_error_event = 1;
1763 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1764 events->sctp_shutdown_event = 1;
1766 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1767 events->sctp_partial_delivery_event = 1;
1769 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1770 events->sctp_adaptation_layer_event = 1;
1772 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1773 events->sctp_authentication_event = 1;
1775 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1776 events->sctp_stream_reset_events = 1;
1777 SCTP_INP_RUNLOCK(inp);
1778 *optsize = sizeof(struct sctp_event_subscribe);
1782 case SCTP_ADAPTATION_LAYER:
1786 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1788 SCTP_INP_RLOCK(inp);
1789 *value = inp->sctp_ep.adaptation_layer_indicator;
1790 SCTP_INP_RUNLOCK(inp);
1791 *optsize = sizeof(uint32_t);
1794 case SCTP_SET_INITIAL_DBG_SEQ:
1798 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1799 SCTP_INP_RLOCK(inp);
1800 *value = inp->sctp_ep.initial_sequence_debug;
1801 SCTP_INP_RUNLOCK(inp);
1802 *optsize = sizeof(uint32_t);
1805 case SCTP_GET_LOCAL_ADDR_SIZE:
1809 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1810 SCTP_INP_RLOCK(inp);
1811 *value = sctp_count_max_addresses(inp);
1812 SCTP_INP_RUNLOCK(inp);
1813 *optsize = sizeof(uint32_t);
1816 case SCTP_GET_REMOTE_ADDR_SIZE:
1820 struct sctp_nets *net;
1822 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1823 /* FIXME MT: change to sctp_assoc_value? */
1824 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1828 /* Count the sizes */
1829 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1830 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1831 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1832 size += sizeof(struct sockaddr_in6);
1833 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1834 size += sizeof(struct sockaddr_in);
1840 SCTP_TCB_UNLOCK(stcb);
1841 *value = (uint32_t) size;
1845 *optsize = sizeof(uint32_t);
1848 case SCTP_GET_PEER_ADDRESSES:
1850 * Get the address information, an array is passed in to
1851 * fill up we pack it.
1855 struct sockaddr_storage *sas;
1856 struct sctp_nets *net;
1857 struct sctp_getaddresses *saddr;
1859 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1860 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1863 left = (*optsize) - sizeof(struct sctp_getaddresses);
1864 *optsize = sizeof(struct sctp_getaddresses);
1865 sas = (struct sockaddr_storage *)&saddr->addr[0];
1867 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1868 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1869 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1870 cpsz = sizeof(struct sockaddr_in6);
1871 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1872 cpsz = sizeof(struct sockaddr_in);
1878 /* not enough room. */
1881 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1882 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1883 /* Must map the address */
1884 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1885 (struct sockaddr_in6 *)sas);
1887 memcpy(sas, &net->ro._l_addr, cpsz);
1889 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1891 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1895 SCTP_TCB_UNLOCK(stcb);
1901 case SCTP_GET_LOCAL_ADDRESSES:
1903 size_t limit, actual;
1904 struct sockaddr_storage *sas;
1905 struct sctp_getaddresses *saddr;
1907 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1908 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1910 sas = (struct sockaddr_storage *)&saddr->addr[0];
1911 limit = *optsize - sizeof(sctp_assoc_t);
1912 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1914 SCTP_TCB_UNLOCK(stcb);
1916 *optsize = sizeof(struct sockaddr_storage) + actual;
1919 case SCTP_PEER_ADDR_PARAMS:
1921 struct sctp_paddrparams *paddrp;
1922 struct sctp_nets *net;
1924 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1925 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1929 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1932 * We increment here since
1933 * sctp_findassociation_ep_addr() wil do a
1934 * decrement if it finds the stcb as long as
1935 * the locked tcb (last argument) is NOT a
1938 SCTP_INP_INCR_REF(inp);
1939 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1941 SCTP_INP_DECR_REF(inp);
1946 /* Applys to the specific association */
1947 paddrp->spp_flags = 0;
1951 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1952 ovh = SCTP_MED_OVERHEAD;
1954 ovh = SCTP_MED_V4_OVERHEAD;
1958 paddrp->spp_pathmaxrxt = net->failure_threshold;
1959 paddrp->spp_pathmtu = net->mtu - ovh;
1960 /* get flags for HB */
1961 if (net->dest_state & SCTP_ADDR_NOHB)
1962 paddrp->spp_flags |= SPP_HB_DISABLE;
1964 paddrp->spp_flags |= SPP_HB_ENABLE;
1965 /* get flags for PMTU */
1966 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
1967 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
1969 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
1972 if (net->ro._l_addr.sin.sin_family == AF_INET) {
1973 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
1974 paddrp->spp_flags |= SPP_IPV4_TOS;
1978 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
1979 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
1980 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1985 * No destination so return default
1990 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
1991 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
1993 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
1994 paddrp->spp_flags |= SPP_IPV4_TOS;
1997 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
1998 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2000 /* default settings should be these */
2001 if (stcb->asoc.hb_is_disabled == 0) {
2002 paddrp->spp_flags |= SPP_HB_ENABLE;
2004 paddrp->spp_flags |= SPP_HB_DISABLE;
2006 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2007 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2012 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2015 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2016 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2017 SCTP_TCB_UNLOCK(stcb);
2019 /* Use endpoint defaults */
2020 SCTP_INP_RLOCK(inp);
2021 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2022 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2023 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2024 /* get inp's default */
2026 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2027 paddrp->spp_flags |= SPP_IPV4_TOS;
2030 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2031 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2032 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2035 /* can't return this */
2036 paddrp->spp_pathmtu = 0;
2038 /* default behavior, no stcb */
2039 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2041 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2042 paddrp->spp_flags |= SPP_HB_ENABLE;
2044 paddrp->spp_flags |= SPP_HB_DISABLE;
2046 SCTP_INP_RUNLOCK(inp);
2048 *optsize = sizeof(struct sctp_paddrparams);
2051 case SCTP_GET_PEER_ADDR_INFO:
2053 struct sctp_paddrinfo *paddri;
2054 struct sctp_nets *net;
2056 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2057 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2061 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2064 * We increment here since
2065 * sctp_findassociation_ep_addr() wil do a
2066 * decrement if it finds the stcb as long as
2067 * the locked tcb (last argument) is NOT a
2070 SCTP_INP_INCR_REF(inp);
2071 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2073 SCTP_INP_DECR_REF(inp);
2077 if ((stcb) && (net)) {
2078 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2079 paddri->spinfo_cwnd = net->cwnd;
2080 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2081 paddri->spinfo_rto = net->RTO;
2082 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2083 SCTP_TCB_UNLOCK(stcb);
2086 SCTP_TCB_UNLOCK(stcb);
2090 *optsize = sizeof(struct sctp_paddrinfo);
2093 case SCTP_PCB_STATUS:
2095 struct sctp_pcbinfo *spcb;
2097 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2098 sctp_fill_pcbinfo(spcb);
2099 *optsize = sizeof(struct sctp_pcbinfo);
2105 struct sctp_nets *net;
2106 struct sctp_status *sstat;
2108 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2109 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2116 * I think passing the state is fine since
2117 * sctp_constants.h will be available to the user
2120 sstat->sstat_state = stcb->asoc.state;
2121 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2122 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2124 * We can't include chunks that have been passed to
2125 * the socket layer. Only things in queue.
2127 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2128 stcb->asoc.cnt_on_all_streams);
2131 sstat->sstat_instrms = stcb->asoc.streamincnt;
2132 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2133 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2134 memcpy(&sstat->sstat_primary.spinfo_address,
2135 &stcb->asoc.primary_destination->ro._l_addr,
2136 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2137 net = stcb->asoc.primary_destination;
2138 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2140 * Again the user can get info from sctp_constants.h
2141 * for what the state of the network is.
2143 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2144 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2145 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2146 sstat->sstat_primary.spinfo_rto = net->RTO;
2147 sstat->sstat_primary.spinfo_mtu = net->mtu;
2148 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2149 SCTP_TCB_UNLOCK(stcb);
2150 *optsize = sizeof(*sstat);
2155 struct sctp_rtoinfo *srto;
2157 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2158 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2161 srto->srto_initial = stcb->asoc.initial_rto;
2162 srto->srto_max = stcb->asoc.maxrto;
2163 srto->srto_min = stcb->asoc.minrto;
2164 SCTP_TCB_UNLOCK(stcb);
2166 SCTP_INP_RLOCK(inp);
2167 srto->srto_initial = inp->sctp_ep.initial_rto;
2168 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2169 srto->srto_min = inp->sctp_ep.sctp_minrto;
2170 SCTP_INP_RUNLOCK(inp);
2172 *optsize = sizeof(*srto);
2175 case SCTP_ASSOCINFO:
2177 struct sctp_assocparams *sasoc;
2179 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2180 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2183 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2184 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2185 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2186 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2187 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2188 SCTP_TCB_UNLOCK(stcb);
2190 SCTP_INP_RLOCK(inp);
2191 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2192 sasoc->sasoc_number_peer_destinations = 0;
2193 sasoc->sasoc_peer_rwnd = 0;
2194 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2195 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2196 SCTP_INP_RUNLOCK(inp);
2198 *optsize = sizeof(*sasoc);
2201 case SCTP_DEFAULT_SEND_PARAM:
2203 struct sctp_sndrcvinfo *s_info;
2205 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2206 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2209 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2210 SCTP_TCB_UNLOCK(stcb);
2212 SCTP_INP_RLOCK(inp);
2213 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2214 SCTP_INP_RUNLOCK(inp);
2216 *optsize = sizeof(*s_info);
2221 struct sctp_initmsg *sinit;
2223 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2224 SCTP_INP_RLOCK(inp);
2225 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2226 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2227 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2228 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2229 SCTP_INP_RUNLOCK(inp);
2230 *optsize = sizeof(*sinit);
2233 case SCTP_PRIMARY_ADDR:
2234 /* we allow a "get" operation on this */
2236 struct sctp_setprim *ssp;
2238 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2239 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2242 /* simply copy out the sockaddr_storage... */
2246 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2247 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2249 memcpy(&ssp->ssp_addr,
2250 &stcb->asoc.primary_destination->ro._l_addr,
2252 SCTP_TCB_UNLOCK(stcb);
2256 *optsize = sizeof(*ssp);
2260 case SCTP_HMAC_IDENT:
2262 struct sctp_hmacalgo *shmac;
2263 sctp_hmaclist_t *hmaclist;
2267 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2269 SCTP_INP_RLOCK(inp);
2270 hmaclist = inp->sctp_ep.local_hmacs;
2271 if (hmaclist == NULL) {
2272 /* no HMACs to return */
2273 *optsize = sizeof(*shmac);
2274 SCTP_INP_RUNLOCK(inp);
2277 /* is there room for all of the hmac ids? */
2278 size = sizeof(*shmac) + (hmaclist->num_algo *
2279 sizeof(shmac->shmac_idents[0]));
2280 if ((size_t)(*optsize) < size) {
2282 SCTP_INP_RUNLOCK(inp);
2285 /* copy in the list */
2286 for (i = 0; i < hmaclist->num_algo; i++)
2287 shmac->shmac_idents[i] = hmaclist->hmac[i];
2288 SCTP_INP_RUNLOCK(inp);
2292 case SCTP_AUTH_ACTIVE_KEY:
2294 struct sctp_authkeyid *scact;
2296 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2297 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2300 /* get the active key on the assoc */
2301 scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2302 SCTP_TCB_UNLOCK(stcb);
2304 /* get the endpoint active key */
2305 SCTP_INP_RLOCK(inp);
2306 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2307 SCTP_INP_RUNLOCK(inp);
2309 *optsize = sizeof(*scact);
2312 case SCTP_LOCAL_AUTH_CHUNKS:
2314 struct sctp_authchunks *sac;
2315 sctp_auth_chklist_t *chklist = NULL;
2318 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2319 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2322 /* get off the assoc */
2323 chklist = stcb->asoc.local_auth_chunks;
2324 /* is there enough space? */
2325 size = sctp_auth_get_chklist_size(chklist);
2326 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2329 /* copy in the chunks */
2330 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2332 SCTP_TCB_UNLOCK(stcb);
2334 /* get off the endpoint */
2335 SCTP_INP_RLOCK(inp);
2336 chklist = inp->sctp_ep.local_auth_chunks;
2337 /* is there enough space? */
2338 size = sctp_auth_get_chklist_size(chklist);
2339 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2342 /* copy in the chunks */
2343 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2345 SCTP_INP_RUNLOCK(inp);
2347 *optsize = sizeof(struct sctp_authchunks) + size;
2350 case SCTP_PEER_AUTH_CHUNKS:
2352 struct sctp_authchunks *sac;
2353 sctp_auth_chklist_t *chklist = NULL;
2356 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2357 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2360 /* get off the assoc */
2361 chklist = stcb->asoc.peer_auth_chunks;
2362 /* is there enough space? */
2363 size = sctp_auth_get_chklist_size(chklist);
2364 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2367 /* copy in the chunks */
2368 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2370 SCTP_TCB_UNLOCK(stcb);
2374 *optsize = sizeof(struct sctp_authchunks) + size;
2380 error = ENOPROTOOPT;
2383 } /* end switch (sopt->sopt_name) */
2388 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2393 struct sctp_tcb *stcb = NULL;
2394 struct sctp_inpcb *inp;
2397 if (optval == NULL) {
2398 SCTP_PRINTF("optval is NULL\n");
2401 inp = (struct sctp_inpcb *)so->so_pcb;
2403 SCTP_PRINTF("inp is NULL?\n");
2406 vrf_id = inp->def_vrf_id;
2411 case SCTP_AUTOCLOSE:
2412 case SCTP_AUTO_ASCONF:
2413 case SCTP_EXPLICIT_EOR:
2414 case SCTP_DISABLE_FRAGMENTS:
2415 case SCTP_USE_EXT_RCVINFO:
2416 case SCTP_I_WANT_MAPPED_V4_ADDR:
2417 /* copy in the option value */
2418 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2423 case SCTP_DISABLE_FRAGMENTS:
2424 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2426 case SCTP_AUTO_ASCONF:
2427 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2429 case SCTP_EXPLICIT_EOR:
2430 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2432 case SCTP_USE_EXT_RCVINFO:
2433 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2435 case SCTP_I_WANT_MAPPED_V4_ADDR:
2436 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2437 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2443 set_opt = SCTP_PCB_FLAGS_NODELAY;
2445 case SCTP_AUTOCLOSE:
2446 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2447 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2450 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2452 * The value is in ticks. Note this does not effect
2453 * old associations, only new ones.
2455 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2458 SCTP_INP_WLOCK(inp);
2460 sctp_feature_on(inp, set_opt);
2462 sctp_feature_off(inp, set_opt);
2464 SCTP_INP_WUNLOCK(inp);
2466 case SCTP_PARTIAL_DELIVERY_POINT:
2470 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2471 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2475 inp->partial_delivery_point = *value;
2478 case SCTP_FRAGMENT_INTERLEAVE:
2479 /* not yet until we re-write sctp_recvmsg() */
2483 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2484 if (*level == SCTP_FRAG_LEVEL_2) {
2485 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2486 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2487 } else if (*level == SCTP_FRAG_LEVEL_1) {
2488 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2489 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2490 } else if (*level == SCTP_FRAG_LEVEL_0) {
2491 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2492 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2499 case SCTP_CMT_ON_OFF:
2501 struct sctp_assoc_value *av;
2503 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2504 if (sctp_cmt_on_off) {
2505 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2507 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2508 SCTP_TCB_UNLOCK(stcb);
2513 error = ENOPROTOOPT;
2517 case SCTP_CLR_STAT_LOG:
2518 #ifdef SCTP_STAT_LOGGING
2519 sctp_clr_stat_log();
2526 struct sctp_assoc_value *av;
2528 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2529 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2532 stcb->asoc.context = av->assoc_value;
2533 SCTP_TCB_UNLOCK(stcb);
2535 SCTP_INP_WLOCK(inp);
2536 inp->sctp_context = av->assoc_value;
2537 SCTP_INP_WUNLOCK(inp);
2543 uint32_t *default_vrfid;
2545 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2546 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2550 inp->def_vrf_id = *default_vrfid;
2553 case SCTP_DEL_VRF_ID:
2558 case SCTP_ADD_VRF_ID:
2563 case SCTP_DELAYED_SACK:
2565 struct sctp_sack_info *sack;
2567 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2568 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2570 if (sack->sack_delay) {
2571 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2572 sack->sack_delay = TICKS_TO_MSEC(1);
2574 stcb->asoc.delayed_ack = sack->sack_delay;
2576 if (sack->sack_freq) {
2577 stcb->asoc.sack_freq = sack->sack_freq;
2579 SCTP_TCB_UNLOCK(stcb);
2581 SCTP_INP_WLOCK(inp);
2582 if (sack->sack_delay) {
2583 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2584 sack->sack_delay = TICKS_TO_MSEC(1);
2586 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2588 if (sack->sack_freq) {
2589 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2591 SCTP_INP_WUNLOCK(inp);
2595 case SCTP_AUTH_CHUNK:
2597 struct sctp_authchunk *sauth;
2599 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2601 SCTP_INP_WLOCK(inp);
2602 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2604 SCTP_INP_WUNLOCK(inp);
2609 struct sctp_authkey *sca;
2610 struct sctp_keyhead *shared_keys;
2611 sctp_sharedkey_t *shared_key;
2612 sctp_key_t *key = NULL;
2615 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2616 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2617 size = optsize - sizeof(*sca);
2620 /* set it on the assoc */
2621 shared_keys = &stcb->asoc.shared_keys;
2622 /* clear the cached keys for this key id */
2623 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2625 * create the new shared key and
2629 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2632 SCTP_TCB_UNLOCK(stcb);
2636 shared_key = sctp_alloc_sharedkey();
2637 if (shared_key == NULL) {
2640 SCTP_TCB_UNLOCK(stcb);
2643 shared_key->key = key;
2644 shared_key->keyid = sca->sca_keynumber;
2645 sctp_insert_sharedkey(shared_keys, shared_key);
2646 SCTP_TCB_UNLOCK(stcb);
2648 /* set it on the endpoint */
2649 SCTP_INP_WLOCK(inp);
2650 shared_keys = &inp->sctp_ep.shared_keys;
2652 * clear the cached keys on all assocs for
2655 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2657 * create the new shared key and
2661 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2664 SCTP_INP_WUNLOCK(inp);
2668 shared_key = sctp_alloc_sharedkey();
2669 if (shared_key == NULL) {
2672 SCTP_INP_WUNLOCK(inp);
2675 shared_key->key = key;
2676 shared_key->keyid = sca->sca_keynumber;
2677 sctp_insert_sharedkey(shared_keys, shared_key);
2678 SCTP_INP_WUNLOCK(inp);
2682 case SCTP_HMAC_IDENT:
2684 struct sctp_hmacalgo *shmac;
2685 sctp_hmaclist_t *hmaclist;
2687 size_t size, i, found;
2689 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2690 size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2691 hmaclist = sctp_alloc_hmaclist(size);
2692 if (hmaclist == NULL) {
2696 for (i = 0; i < size; i++) {
2697 hmacid = shmac->shmac_idents[i];
2698 if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2699 /* invalid HMACs were found */ ;
2701 sctp_free_hmaclist(hmaclist);
2702 goto sctp_set_hmac_done;
2706 for (i = 0; i < hmaclist->num_algo; i++) {
2707 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
2708 /* already in list */
2713 sctp_free_hmaclist(hmaclist);
2717 /* set it on the endpoint */
2718 SCTP_INP_WLOCK(inp);
2719 if (inp->sctp_ep.local_hmacs)
2720 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2721 inp->sctp_ep.local_hmacs = hmaclist;
2722 SCTP_INP_WUNLOCK(inp);
2726 case SCTP_AUTH_ACTIVE_KEY:
2728 struct sctp_authkeyid *scact;
2730 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2731 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2733 /* set the active key on the right place */
2735 /* set the active key on the assoc */
2736 if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2738 SCTP_TCB_UNLOCK(stcb);
2740 /* set the active key on the endpoint */
2741 SCTP_INP_WLOCK(inp);
2742 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2744 SCTP_INP_WUNLOCK(inp);
2748 case SCTP_AUTH_DELETE_KEY:
2750 struct sctp_authkeyid *scdel;
2752 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2753 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2755 /* delete the key from the right place */
2757 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2759 SCTP_TCB_UNLOCK(stcb);
2761 SCTP_INP_WLOCK(inp);
2762 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2764 SCTP_INP_WUNLOCK(inp);
2769 case SCTP_RESET_STREAMS:
2771 struct sctp_stream_reset *strrst;
2772 uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2775 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2776 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2782 if (stcb->asoc.peer_supports_strreset == 0) {
2784 * Peer does not support it, we return
2785 * protocol not supported since this is true
2786 * for this feature and this peer, not the
2787 * socket request in general.
2789 error = EPROTONOSUPPORT;
2790 SCTP_TCB_UNLOCK(stcb);
2793 if (stcb->asoc.stream_reset_outstanding) {
2795 SCTP_TCB_UNLOCK(stcb);
2798 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2800 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2802 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2805 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2809 SCTP_TCB_UNLOCK(stcb);
2812 for (i = 0; i < strrst->strrst_num_streams; i++) {
2815 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2820 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2827 SCTP_TCB_UNLOCK(stcb);
2830 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2831 strrst->strrst_list,
2832 send_out, (stcb->asoc.str_reset_seq_in - 3),
2835 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2836 SCTP_TCB_UNLOCK(stcb);
2840 case SCTP_CONNECT_X:
2841 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2845 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2848 case SCTP_CONNECT_X_DELAYED:
2849 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2853 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2856 case SCTP_CONNECT_X_COMPLETE:
2858 struct sockaddr *sa;
2859 struct sctp_nets *net;
2861 /* FIXME MT: check correct? */
2862 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2865 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2866 SCTP_INP_RLOCK(inp);
2867 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2869 SCTP_TCB_LOCK(stcb);
2870 net = sctp_findnet(stcb, sa);
2872 SCTP_INP_RUNLOCK(inp);
2875 * We increment here since
2876 * sctp_findassociation_ep_addr() wil do a
2877 * decrement if it finds the stcb as long as
2878 * the locked tcb (last argument) is NOT a
2881 SCTP_INP_INCR_REF(inp);
2882 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2884 SCTP_INP_DECR_REF(inp);
2892 if (stcb->asoc.delayed_connection == 1) {
2893 stcb->asoc.delayed_connection = 0;
2894 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2895 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
2896 stcb->asoc.primary_destination,
2897 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
2898 sctp_send_initiate(inp, stcb);
2901 * already expired or did not use delayed
2906 SCTP_TCB_UNLOCK(stcb);
2909 case SCTP_MAX_BURST:
2913 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
2915 SCTP_INP_WLOCK(inp);
2917 inp->sctp_ep.max_burst = *burst;
2919 SCTP_INP_WUNLOCK(inp);
2924 struct sctp_assoc_value *av;
2927 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2928 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2930 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2931 ovh = SCTP_MED_OVERHEAD;
2933 ovh = SCTP_MED_V4_OVERHEAD;
2936 if (av->assoc_value) {
2937 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
2939 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2941 SCTP_TCB_UNLOCK(stcb);
2943 SCTP_INP_WLOCK(inp);
2945 * FIXME MT: I think this is not in tune
2948 if (av->assoc_value) {
2949 inp->sctp_frag_point = (av->assoc_value + ovh);
2951 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2953 SCTP_INP_WUNLOCK(inp);
2959 struct sctp_event_subscribe *events;
2961 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
2963 SCTP_INP_WLOCK(inp);
2964 if (events->sctp_data_io_event) {
2965 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2967 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2970 if (events->sctp_association_event) {
2971 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2973 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2976 if (events->sctp_address_event) {
2977 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2979 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2982 if (events->sctp_send_failure_event) {
2983 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2985 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2988 if (events->sctp_peer_error_event) {
2989 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2991 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2994 if (events->sctp_shutdown_event) {
2995 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2997 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3000 if (events->sctp_partial_delivery_event) {
3001 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3003 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3006 if (events->sctp_adaptation_layer_event) {
3007 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3009 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3012 if (events->sctp_authentication_event) {
3013 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3015 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3018 if (events->sctp_stream_reset_events) {
3019 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3021 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3023 SCTP_INP_WUNLOCK(inp);
3027 case SCTP_ADAPTATION_LAYER:
3029 struct sctp_setadaptation *adap_bits;
3031 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3032 SCTP_INP_WLOCK(inp);
3033 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3034 SCTP_INP_WUNLOCK(inp);
3038 case SCTP_SET_INITIAL_DBG_SEQ:
3042 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3043 SCTP_INP_WLOCK(inp);
3044 inp->sctp_ep.initial_sequence_debug = *vvv;
3045 SCTP_INP_WUNLOCK(inp);
3049 case SCTP_DEFAULT_SEND_PARAM:
3051 struct sctp_sndrcvinfo *s_info;
3053 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3054 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3057 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3058 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3062 SCTP_TCB_UNLOCK(stcb);
3064 SCTP_INP_WLOCK(inp);
3065 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3066 SCTP_INP_WUNLOCK(inp);
3070 case SCTP_PEER_ADDR_PARAMS:
3071 /* Applys to the specific association */
3073 struct sctp_paddrparams *paddrp;
3074 struct sctp_nets *net;
3076 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3077 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3080 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3083 * We increment here since
3084 * sctp_findassociation_ep_addr() wil do a
3085 * decrement if it finds the stcb as long as
3086 * the locked tcb (last argument) is NOT a
3089 SCTP_INP_INCR_REF(inp);
3090 stcb = sctp_findassociation_ep_addr(&inp,
3091 (struct sockaddr *)&paddrp->spp_address,
3094 SCTP_INP_DECR_REF(inp);
3098 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3100 SCTP_TCB_UNLOCK(stcb);
3103 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3105 SCTP_TCB_UNLOCK(stcb);
3109 /************************TCB SPECIFIC SET ******************/
3111 * do we change the timer for HB, we run
3116 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3117 ovh = SCTP_MED_OVERHEAD;
3119 ovh = SCTP_MED_V4_OVERHEAD;
3122 if (paddrp->spp_hbinterval)
3123 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3124 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3125 stcb->asoc.heart_beat_delay = 0;
3127 /* network sets ? */
3129 /************************NET SPECIFIC SET ******************/
3130 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3132 (void)sctp_send_hb(stcb, 1, net);
3134 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3135 net->dest_state |= SCTP_ADDR_NOHB;
3137 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3138 net->dest_state &= ~SCTP_ADDR_NOHB;
3140 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3141 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3142 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3143 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3145 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3146 net->mtu = paddrp->spp_pathmtu + ovh;
3147 if (net->mtu < stcb->asoc.smallest_mtu) {
3148 #ifdef SCTP_PRINT_FOR_B_AND_M
3149 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3152 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3156 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3157 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3158 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3161 if (paddrp->spp_pathmaxrxt)
3162 net->failure_threshold = paddrp->spp_pathmaxrxt;
3164 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3165 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3166 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3171 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3172 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3173 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3178 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3179 if (paddrp->spp_pathmaxrxt)
3180 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3182 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3183 /* Turn back on the timer */
3184 stcb->asoc.hb_is_disabled = 0;
3185 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3187 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3188 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3189 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3190 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3191 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3193 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3194 net->mtu = paddrp->spp_pathmtu + ovh;
3195 if (net->mtu < stcb->asoc.smallest_mtu) {
3196 #ifdef SCTP_PRINT_FOR_B_AND_M
3197 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3200 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3205 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3206 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3207 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3208 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3212 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3213 int cnt_of_unconf = 0;
3214 struct sctp_nets *lnet;
3216 stcb->asoc.hb_is_disabled = 1;
3217 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3218 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3223 * stop the timer ONLY if we
3224 * have no unconfirmed
3227 if (cnt_of_unconf == 0) {
3228 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3229 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3230 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3234 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3235 /* start up the timer. */
3236 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3237 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3241 if (paddrp->spp_flags & SPP_IPV4_TOS)
3242 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3245 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3246 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3250 SCTP_TCB_UNLOCK(stcb);
3252 /************************NO TCB, SET TO default stuff ******************/
3253 SCTP_INP_WLOCK(inp);
3255 * For the TOS/FLOWLABEL stuff you set it
3256 * with the options on the socket
3258 if (paddrp->spp_pathmaxrxt) {
3259 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3261 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3262 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3263 else if (paddrp->spp_hbinterval)
3264 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3266 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3267 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3269 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3270 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3272 SCTP_INP_WUNLOCK(inp);
3278 struct sctp_rtoinfo *srto;
3279 uint32_t new_init, new_min, new_max;
3281 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3282 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3285 if (srto->srto_initial)
3286 new_init = srto->srto_initial;
3288 new_init = stcb->asoc.initial_rto;
3290 new_max = srto->srto_max;
3292 new_max = stcb->asoc.maxrto;
3294 new_min = srto->srto_min;
3296 new_min = stcb->asoc.minrto;
3297 if ((new_min <= new_init) && (new_init <= new_max)) {
3298 stcb->asoc.initial_rto = new_init;
3299 stcb->asoc.maxrto = new_max;
3300 stcb->asoc.minrto = new_min;
3304 SCTP_TCB_UNLOCK(stcb);
3306 SCTP_INP_WLOCK(inp);
3307 if (srto->srto_initial)
3308 new_init = srto->srto_initial;
3310 new_init = inp->sctp_ep.initial_rto;
3312 new_max = srto->srto_max;
3314 new_max = inp->sctp_ep.sctp_maxrto;
3316 new_min = srto->srto_min;
3318 new_min = inp->sctp_ep.sctp_minrto;
3319 if ((new_min <= new_init) && (new_init <= new_max)) {
3320 inp->sctp_ep.initial_rto = new_init;
3321 inp->sctp_ep.sctp_maxrto = new_max;
3322 inp->sctp_ep.sctp_minrto = new_min;
3326 SCTP_INP_WUNLOCK(inp);
3330 case SCTP_ASSOCINFO:
3332 struct sctp_assocparams *sasoc;
3334 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3335 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3338 if (sasoc->sasoc_asocmaxrxt)
3339 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3340 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3341 sasoc->sasoc_peer_rwnd = 0;
3342 sasoc->sasoc_local_rwnd = 0;
3343 if (sasoc->sasoc_cookie_life) {
3344 if (sasoc->sasoc_cookie_life < 1000)
3345 sasoc->sasoc_cookie_life = 1000;
3346 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3348 SCTP_TCB_UNLOCK(stcb);
3350 SCTP_INP_WLOCK(inp);
3351 if (sasoc->sasoc_asocmaxrxt)
3352 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3353 sasoc->sasoc_number_peer_destinations = 0;
3354 sasoc->sasoc_peer_rwnd = 0;
3355 sasoc->sasoc_local_rwnd = 0;
3356 if (sasoc->sasoc_cookie_life) {
3357 if (sasoc->sasoc_cookie_life < 1000)
3358 sasoc->sasoc_cookie_life = 1000;
3359 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3361 SCTP_INP_WUNLOCK(inp);
3367 struct sctp_initmsg *sinit;
3369 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3370 SCTP_INP_WLOCK(inp);
3371 if (sinit->sinit_num_ostreams)
3372 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3374 if (sinit->sinit_max_instreams)
3375 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3377 if (sinit->sinit_max_attempts)
3378 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3380 if (sinit->sinit_max_init_timeo)
3381 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3382 SCTP_INP_WUNLOCK(inp);
3385 case SCTP_PRIMARY_ADDR:
3387 struct sctp_setprim *spa;
3388 struct sctp_nets *net, *lnet;
3390 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3391 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3395 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3398 * We increment here since
3399 * sctp_findassociation_ep_addr() wil do a
3400 * decrement if it finds the stcb as long as
3401 * the locked tcb (last argument) is NOT a
3404 SCTP_INP_INCR_REF(inp);
3405 stcb = sctp_findassociation_ep_addr(&inp,
3406 (struct sockaddr *)&spa->ssp_addr,
3409 SCTP_INP_DECR_REF(inp);
3413 if ((stcb) && (net)) {
3414 if ((net != stcb->asoc.primary_destination) &&
3415 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3416 /* Ok we need to set it */
3417 lnet = stcb->asoc.primary_destination;
3418 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3419 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3420 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3422 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3429 SCTP_TCB_UNLOCK(stcb);
3433 case SCTP_SET_DYNAMIC_PRIMARY:
3435 union sctp_sockstore *ss;
3437 error = priv_check(curthread,
3438 PRIV_NETINET_RESERVEDPORT);
3442 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3443 /* SUPER USER CHECK? */
3444 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3447 case SCTP_SET_PEER_PRIMARY_ADDR:
3449 struct sctp_setpeerprim *sspp;
3451 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3452 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3454 struct sctp_ifa *ifa;
3456 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3457 stcb->asoc.vrf_id, 0);
3462 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3464 * Must validate the ifa found is in
3467 struct sctp_laddr *laddr;
3470 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3471 if (laddr->ifa == NULL) {
3472 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3476 if (laddr->ifa == ifa) {
3486 if (sctp_set_primary_ip_address_sa(stcb,
3487 (struct sockaddr *)&sspp->sspp_addr) != 0) {
3491 SCTP_TCB_UNLOCK(stcb);
3498 case SCTP_BINDX_ADD_ADDR:
3500 struct sctp_getaddresses *addrs;
3501 struct sockaddr *addr_touse;
3502 struct sockaddr_in sin;
3504 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3506 /* see if we're bound all already! */
3507 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3511 /* Is the VRF one we have */
3512 addr_touse = addrs->addr;
3514 if (addrs->addr->sa_family == AF_INET6) {
3515 struct sockaddr_in6 *sin6;
3517 if (addrs->addr->sa_len != sizeof(struct sockaddr_in6)) {
3521 sin6 = (struct sockaddr_in6 *)addr_touse;
3522 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3523 in6_sin6_2_sin(&sin, sin6);
3524 addr_touse = (struct sockaddr *)&sin;
3528 if (addrs->addr->sa_family == AF_INET) {
3529 if (addrs->addr->sa_len != sizeof(struct sockaddr_in)) {
3534 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3537 /* Can't get proc for Net/Open BSD */
3541 error = sctp_inpcb_bind(so, addr_touse, p);
3545 * No locks required here since bind and mgmt_ep_sa
3546 * all do their own locking. If we do something for
3547 * the FIX: below we may need to lock in that case.
3549 if (addrs->sget_assoc_id == 0) {
3550 /* add the address */
3551 struct sctp_inpcb *lep;
3553 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3554 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
3557 * We must decrement the refcount
3558 * since we have the ep already and
3559 * are binding. No remove going on
3562 SCTP_INP_DECR_REF(inp);
3565 /* already bound to it.. ok */
3567 } else if (lep == NULL) {
3568 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3569 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3570 SCTP_ADD_IP_ADDRESS, vrf_id);
3579 * FIX: decide whether we allow assoc based
3585 case SCTP_BINDX_REM_ADDR:
3587 struct sctp_getaddresses *addrs;
3588 struct sockaddr *addr_touse;
3589 struct sockaddr_in sin;
3591 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3592 /* see if we're bound all already! */
3593 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3597 addr_touse = addrs->addr;
3599 if (addrs->addr->sa_family == AF_INET6) {
3600 struct sockaddr_in6 *sin6;
3602 if (addrs->addr->sa_len != sizeof(struct sockaddr_in6)) {
3606 sin6 = (struct sockaddr_in6 *)addr_touse;
3607 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3608 in6_sin6_2_sin(&sin, sin6);
3609 addr_touse = (struct sockaddr *)&sin;
3613 if (addrs->addr->sa_family == AF_INET) {
3614 if (addrs->addr->sa_len != sizeof(struct sockaddr_in)) {
3620 * No lock required mgmt_ep_sa does its own locking.
3621 * If the FIX: below is ever changed we may need to
3622 * lock before calling association level binding.
3624 if (addrs->sget_assoc_id == 0) {
3625 /* delete the address */
3626 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3627 SCTP_DEL_IP_ADDRESS,
3631 * FIX: decide whether we allow assoc based
3638 error = ENOPROTOOPT;
3640 } /* end switch (opt) */
3646 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3648 void *optval = NULL;
3650 struct sctp_inpcb *inp;
3654 inp = (struct sctp_inpcb *)so->so_pcb;
3656 /* I made the same as TCP since we are not setup? */
3657 return (ECONNRESET);
3659 if (sopt->sopt_level != IPPROTO_SCTP) {
3660 /* wrong proto level... send back up to IP */
3662 if (INP_CHECK_SOCKAF(so, AF_INET6))
3663 error = ip6_ctloutput(so, sopt);
3666 error = ip_ctloutput(so, sopt);
3669 optsize = sopt->sopt_valsize;
3671 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
3672 if (optval == NULL) {
3675 error = sooptcopyin(sopt, optval, optsize, optsize);
3677 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3681 p = (void *)sopt->sopt_td;
3682 if (sopt->sopt_dir == SOPT_SET) {
3683 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3684 } else if (sopt->sopt_dir == SOPT_GET) {
3685 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3689 if ((error == 0) && (optval != NULL)) {
3690 error = sooptcopyout(sopt, optval, optsize);
3691 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3692 } else if (optval != NULL) {
3693 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3701 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3704 int create_lock_on = 0;
3706 struct sctp_inpcb *inp;
3707 struct sctp_tcb *stcb = NULL;
3709 inp = (struct sctp_inpcb *)so->so_pcb;
3711 /* I made the same as TCP since we are not setup? */
3712 return (ECONNRESET);
3717 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
3720 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
3723 SCTP_ASOC_CREATE_LOCK(inp);
3726 SCTP_INP_INCR_REF(inp);
3727 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3728 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3729 /* Should I really unlock ? */
3734 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3735 (addr->sa_family == AF_INET6)) {
3740 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3741 SCTP_PCB_FLAGS_UNBOUND) {
3742 /* Bind a ephemeral port */
3743 error = sctp_inpcb_bind(so, NULL, p);
3748 /* Now do we connect? */
3749 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3753 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3754 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3755 /* We are already connected AND the TCP model */
3759 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3760 SCTP_INP_RLOCK(inp);
3761 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3762 SCTP_INP_RUNLOCK(inp);
3765 * We increment here since sctp_findassociation_ep_addr()
3766 * wil do a decrement if it finds the stcb as long as the
3767 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3769 SCTP_INP_INCR_REF(inp);
3770 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3772 SCTP_INP_DECR_REF(inp);
3774 SCTP_TCB_LOCK(stcb);
3778 /* Already have or am bring up an association */
3782 vrf_id = inp->def_vrf_id;
3783 /* We are GOOD to go */
3784 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3786 /* Gak! no memory */
3789 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3790 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3791 /* Set the connected flag so we can queue data */
3794 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3795 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3797 /* initialize authentication parameters for the assoc */
3798 sctp_initialize_auth_params(inp, stcb);
3800 sctp_send_initiate(inp, stcb);
3801 SCTP_TCB_UNLOCK(stcb);
3803 if (create_lock_on) {
3804 SCTP_ASOC_CREATE_UNLOCK(inp);
3806 SCTP_INP_DECR_REF(inp);
3811 sctp_listen(struct socket *so, int backlog, struct thread *p)
3814 * Note this module depends on the protocol processing being called
3815 * AFTER any socket level flags and backlog are applied to the
3816 * socket. The traditional way that the socket flags are applied is
3817 * AFTER protocol processing. We have made a change to the
3818 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3819 * place if the socket API for SCTP is to work properly.
3823 struct sctp_inpcb *inp;
3825 inp = (struct sctp_inpcb *)so->so_pcb;
3827 /* I made the same as TCP since we are not setup? */
3828 return (ECONNRESET);
3830 SCTP_INP_RLOCK(inp);
3831 #ifdef SCTP_LOCK_LOGGING
3832 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3835 error = solisten_proto_check(so);
3838 SCTP_INP_RUNLOCK(inp);
3841 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3842 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3843 /* We are already connected AND the TCP model */
3844 SCTP_INP_RUNLOCK(inp);
3846 return (EADDRINUSE);
3848 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3849 /* We must do a bind. */
3851 SCTP_INP_RUNLOCK(inp);
3852 if ((error = sctp_inpcb_bind(so, NULL, p))) {
3853 /* bind error, probably perm */
3858 SCTP_INP_RUNLOCK(inp);
3860 /* It appears for 7.0 and on, we must always call this. */
3861 solisten_proto(so, backlog);
3862 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3863 /* remove the ACCEPTCONN flag for one-to-many sockets */
3864 so->so_options &= ~SO_ACCEPTCONN;
3867 /* turning off listen */
3868 so->so_options &= ~SO_ACCEPTCONN;
3874 static int sctp_defered_wakeup_cnt = 0;
3877 sctp_accept(struct socket *so, struct sockaddr **addr)
3879 struct sctp_tcb *stcb;
3880 struct sctp_inpcb *inp;
3881 union sctp_sockstore store;
3885 inp = (struct sctp_inpcb *)so->so_pcb;
3888 return (ECONNRESET);
3890 SCTP_INP_RLOCK(inp);
3891 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3892 SCTP_INP_RUNLOCK(inp);
3895 if (so->so_state & SS_ISDISCONNECTED) {
3896 SCTP_INP_RUNLOCK(inp);
3897 return (ECONNABORTED);
3899 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3901 SCTP_INP_RUNLOCK(inp);
3902 return (ECONNRESET);
3904 SCTP_TCB_LOCK(stcb);
3905 SCTP_INP_RUNLOCK(inp);
3906 store = stcb->asoc.primary_destination->ro._l_addr;
3907 SCTP_TCB_UNLOCK(stcb);
3908 if (store.sa.sa_family == AF_INET) {
3909 struct sockaddr_in *sin;
3911 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3912 sin->sin_family = AF_INET;
3913 sin->sin_len = sizeof(*sin);
3914 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3915 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3916 *addr = (struct sockaddr *)sin;
3918 struct sockaddr_in6 *sin6;
3920 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3921 sin6->sin6_family = AF_INET6;
3922 sin6->sin6_len = sizeof(*sin6);
3923 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3925 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3926 if ((error = sa6_recoverscope(sin6)) != 0) {
3927 SCTP_FREE_SONAME(sin6);
3930 *addr = (struct sockaddr *)sin6;
3932 /* Wake any delayed sleep action */
3933 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3934 SCTP_INP_WLOCK(inp);
3935 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3936 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3937 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3938 SCTP_INP_WUNLOCK(inp);
3939 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3940 if (sowriteable(inp->sctp_socket)) {
3941 sowwakeup_locked(inp->sctp_socket);
3943 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3945 SCTP_INP_WLOCK(inp);
3947 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3948 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3949 SCTP_INP_WUNLOCK(inp);
3950 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3951 if (soreadable(inp->sctp_socket)) {
3952 sctp_defered_wakeup_cnt++;
3953 sorwakeup_locked(inp->sctp_socket);
3955 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3957 SCTP_INP_WLOCK(inp);
3959 SCTP_INP_WUNLOCK(inp);
3965 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3967 struct sockaddr_in *sin;
3969 struct sctp_inpcb *inp;
3970 struct sctp_ifa *sctp_ifa;
3973 * Do the malloc first in case it blocks.
3975 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3976 sin->sin_family = AF_INET;
3977 sin->sin_len = sizeof(*sin);
3978 inp = (struct sctp_inpcb *)so->so_pcb;
3980 SCTP_FREE_SONAME(sin);
3983 SCTP_INP_RLOCK(inp);
3984 sin->sin_port = inp->sctp_lport;
3985 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3986 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3987 struct sctp_tcb *stcb;
3988 struct sockaddr_in *sin_a;
3989 struct sctp_nets *net;
3992 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3998 SCTP_TCB_LOCK(stcb);
3999 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4000 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4002 /* this will make coverity happy */
4005 if (sin_a->sin_family == AF_INET) {
4010 if ((!fnd) || (sin_a == NULL)) {
4012 SCTP_TCB_UNLOCK(stcb);
4015 vrf_id = inp->def_vrf_id;
4016 sctp_ifa = sctp_source_address_selection(inp,
4018 (sctp_route_t *) & net->ro,
4021 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4022 sctp_free_ifa(sctp_ifa);
4024 SCTP_TCB_UNLOCK(stcb);
4026 /* For the bound all case you get back 0 */
4028 sin->sin_addr.s_addr = 0;
4032 /* Take the first IPv4 address in the list */
4033 struct sctp_laddr *laddr;
4036 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4037 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4038 struct sockaddr_in *sin_a;
4040 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4041 sin->sin_addr = sin_a->sin_addr;
4047 SCTP_FREE_SONAME(sin);
4048 SCTP_INP_RUNLOCK(inp);
4052 SCTP_INP_RUNLOCK(inp);
4053 (*addr) = (struct sockaddr *)sin;
4058 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4060 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4062 struct sockaddr_in *sin_a;
4063 struct sctp_inpcb *inp;
4064 struct sctp_tcb *stcb;
4065 struct sctp_nets *net;
4067 /* Do the malloc first in case it blocks. */
4068 inp = (struct sctp_inpcb *)so->so_pcb;
4069 if ((inp == NULL) ||
4070 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4071 /* UDP type and listeners will drop out here */
4074 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4075 sin->sin_family = AF_INET;
4076 sin->sin_len = sizeof(*sin);
4078 /* We must recapture incase we blocked */
4079 inp = (struct sctp_inpcb *)so->so_pcb;
4081 SCTP_FREE_SONAME(sin);
4084 SCTP_INP_RLOCK(inp);
4085 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4087 SCTP_TCB_LOCK(stcb);
4089 SCTP_INP_RUNLOCK(inp);
4091 SCTP_FREE_SONAME(sin);
4095 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4096 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4097 if (sin_a->sin_family == AF_INET) {
4099 sin->sin_port = stcb->rport;
4100 sin->sin_addr = sin_a->sin_addr;
4104 SCTP_TCB_UNLOCK(stcb);
4106 /* No IPv4 address */
4107 SCTP_FREE_SONAME(sin);
4110 (*addr) = (struct sockaddr *)sin;
4114 struct pr_usrreqs sctp_usrreqs = {
4115 .pru_abort = sctp_abort,
4116 .pru_accept = sctp_accept,
4117 .pru_attach = sctp_attach,
4118 .pru_bind = sctp_bind,
4119 .pru_connect = sctp_connect,
4120 .pru_control = in_control,
4121 .pru_close = sctp_close,
4122 .pru_detach = sctp_close,
4123 .pru_sopoll = sopoll_generic,
4124 .pru_disconnect = sctp_disconnect,
4125 .pru_listen = sctp_listen,
4126 .pru_peeraddr = sctp_peeraddr,
4127 .pru_send = sctp_sendm,
4128 .pru_shutdown = sctp_shutdown,
4129 .pru_sockaddr = sctp_ingetaddr,
4130 .pru_sosend = sctp_sosend,
4131 .pru_soreceive = sctp_soreceive